Class: Duckdb

Inherits:
Object
  • Object
show all
Defined in:
lib/raka/lang/duckdb/impl.rb

Overview

DuckDB protocol with two modes:

  1. Persistent mode: operations on .db file with CREATE TABLE

  2. Ad-hoc mode: parquet in/out using COPY operations

Instance Method Summary collapse

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_cmdObject



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