0.0
No release in over 3 years
This module provides Polars table adapter for duckdb. You can access Polars::DataFrame as duckdb table by using this module.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 1.4.4.0
>= 0.25.0
 Project Readme

ruby-duckdb-polars

Description

This gem duckdb-polars provides a Polars DataFrame table adapter for ruby-duckdb. You can query Ruby's Polars::DataFrame objects using SQL through DuckDB by using this gem. This gem is a sample implementation of a DuckDB::TableFunction.

Requirement

How to install

gem install duckdb-polars

Or add the following line to your Gemfile:

gem 'duckdb-polars'

Usage

Basic usage

require 'duckdb'
require 'duckdb/polars-df'

db = DuckDB::Database.open
con = db.connect

df = Polars::DataFrame.new(
  {
    id: [1, 2, 3],
    name: %w[Alice Bob Charlie],
    age: [30, 25, 35]
  }
)

DuckDB::Polars::DataFrame::TableAdapter.register!

con.execute('SET threads=1') # currently the Polars table adapter is not thread-safe, so we set threads to 1
con.expose_as_table(df, 'polars_table')
result = con.query('SELECT * FROM polars_table()')
result.each do |row|
  puts row.inspect
end
# => [1, "Alice", 30]
# => [2, "Bob", 25]
# => [3, "Charlie", 35]

Specifying column types

By default, columns are inferred from the DataFrame's schema (Int64BIGINT, StringVARCHAR; all other types fall back to VARCHAR). You can override column types using the columns option.

require 'duckdb'
require 'duckdb/polars-df'

db = DuckDB::Database.open
con = db.connect

df = Polars::DataFrame.new(
  {
    id: [1, 2, 3],
    name: %w[Alice Bob Charlie],
    age: [30, 25, 35]
  }
)

DuckDB::Polars::DataFrame::TableAdapter.register!

con.execute('SET threads=1') # currently the Polars table adapter is not thread-safe, so we set threads to 1
con.expose_as_table(
  df, 'polars_table',
  columns: {
    'id' => :bigint,
    'name' => :varchar,
    'age' => :bigint
  }
)

result = con.query('SELECT * FROM polars_table()')
result.each do |row|
  puts row.inspect
end
# => [1, "Alice", 30]
# => [2, "Bob", 25]
# => [3, "Charlie", 35]

License

MIT License.