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-polarsOr 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 (Int64 → BIGINT, String → VARCHAR; 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]