Class: Duckdb
- Inherits:
-
Object
- Object
- Duckdb
- Defined in:
- lib/raka/lang/duckdb/impl.rb
Overview
DuckDB protocol with two modes:
-
Persistent mode: operations on .db file with CREATE TABLE
-
Ad-hoc mode: parquet in/out using COPY operations
Instance Method Summary collapse
- #build(code, _task) ⇒ Object
- #duckdb_cmd ⇒ Object
-
#initialize(database: nil, params: {}) ⇒ Duckdb
constructor
A new instance of Duckdb.
- #run_script(env, fname, task) ⇒ Object
Constructor Details
#initialize(database: nil, params: {}) ⇒ Duckdb
Returns a new instance of Duckdb.
20 21 22 23 24 |
# File 'lib/raka/lang/duckdb/impl.rb', line 20 def initialize(database: nil, params: {}) @params = params @database = database @mode = @database ? :persistent : :adhoc end |
Instance Method Details
#build(code, _task) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/raka/lang/duckdb/impl.rb', line 35 def build(code, _task) # Replace parameter placeholders processed_code = code (@params || {}).each do |key, value| processed_code = processed_code.gsub("$#{key}", "'#{value}'") end case @mode when :persistent "DROP TABLE IF EXISTS :_name_; CREATE TABLE :_name_ AS (#{processed_code});" when :adhoc "COPY (#{processed_code}) TO ':output:' (FORMAT PARQUET);" end end |
#duckdb_cmd ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/raka/lang/duckdb/impl.rb', line 26 def duckdb_cmd case @mode when :persistent "duckdb #{@database}" when :adhoc 'duckdb' end end |
#run_script(env, fname, task) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/raka/lang/duckdb/impl.rb', line 50 def run_script(env, fname, task) case @mode when :persistent bash env, %( #{duckdb_cmd} -c "$(cat #{fname} | sed 's|:_name_|#{task.output_stem}|g')" | tee #{fname}.log echo "#{@database}" > #{task.name} ) when :adhoc bash env, %( cat #{fname} | sed 's|:output:|#{task.name}|g' | #{duckdb_cmd} | tee #{fname}.log ) end end |