Class: DuckDB::Appender

Inherits:
Object
  • Object
show all
Includes:
Converter
Defined in:
lib/duckdb/appender.rb,
ext/duckdb/appender.c

Overview

The DuckDB::Appender encapsulates DuckDB Appender.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE users (id INTEGER, name VARCHAR)')
appender = con.appender('users')
appender.append_row(1, 'Alice')

Constant Summary collapse

RANGE_INT16 =
-32_768..32_767
RANGE_INT32 =
-2_147_483_648..2_147_483_647
RANGE_INT64 =
-9_223_372_036_854_775_808..9_223_372_036_854_775_807

Constants included from Converter

Converter::EPOCH, Converter::EPOCH_UTC, Converter::FLIP_HUGEINT, Converter::HALF_HUGEINT, Converter::HALF_HUGEINT_BIT

Instance Method Summary collapse

Methods included from Converter

_parse_date, _parse_deciaml, _parse_time, _to_date, _to_decimal_from_hugeint, _to_decimal_from_value, _to_hugeint_from_vector, _to_infinity, _to_interval_from_vector, _to_query_progress, _to_time, _to_time_from_duckdb_time, _to_time_from_duckdb_time_tz, _to_time_from_duckdb_timestamp_ms, _to_time_from_duckdb_timestamp_ns, _to_time_from_duckdb_timestamp_s, _to_time_from_duckdb_timestamp_tz, _to_uuid_from_vector

Constructor Details

#initialize(con, schema, table) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/duckdb/appender.c', line 62

static VALUE appender_initialize(VALUE self, VALUE con, VALUE schema, VALUE table) {

    rubyDuckDBConnection *ctxcon;
    rubyDuckDBAppender *ctx;
    char *pschema = 0;

    if (!rb_obj_is_kind_of(con, cDuckDBConnection)) {
        rb_raise(rb_eTypeError, "1st argument should be instance of DackDB::Connection");
    }

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);
    ctxcon = get_struct_connection(con);

    if (schema != Qnil) {
        pschema = StringValuePtr(schema);
    }

    if (duckdb_appender_create(ctxcon->con, pschema, StringValuePtr(table), &(ctx->appender)) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to create appender");
    }
    return self;
}

Instance Method Details

#append(value) ⇒ Object

appends value.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE users (id INTEGER, name VARCHAR)')
appender = con.appender('users')
appender.begin_row
appender.append(1)
appender.append('Alice')
appender.end_row


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/duckdb/appender.rb', line 159

def append(value)
  case value
  when NilClass
    append_null
  when Float
    append_double(value)
  when Integer
    case value
    when RANGE_INT16
      append_int16(value)
    when RANGE_INT32
      append_int32(value)
    when RANGE_INT64
      append_int64(value)
    else
      append_hugeint(value)
    end
  when String
    blob?(value) ? append_blob(value) : append_varchar(value)
  when TrueClass, FalseClass
    append_bool(value)
  when Time
    append_timestamp(value)
  when Date
    append_date(value)
  when DuckDB::Interval
    append_interval(value)
  else
    raise(DuckDB::Error, "not supported type #{value} (#{value.class})")
  end
end

#append_blob(val) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'ext/duckdb/appender.c', line 265

static VALUE appender_append_blob(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;

    char *pval = StringValuePtr(val);
    idx_t length = (idx_t)RSTRING_LEN(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_blob(ctx->appender, (void *)pval, length) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_bool(val) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/duckdb/appender.c', line 105

static VALUE appender_append_bool(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (val != Qtrue && val != Qfalse) {
        rb_raise(rb_eArgError, "argument must be boolean");
    }

    if (duckdb_append_bool(ctx->appender, (val == Qtrue)) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append boolean");
    }
    return self;
}

#append_date(value) ⇒ Object

appends date value.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE dates (date_value DATE)')
appender = con.appender('dates')
appender.begin_row
appender.append_date(Date.today)
# or
# appender.append_date(Time.now)
# appender.append_date('2021-10-10')
appender.end_row
appender.flush


76
77
78
79
80
# File 'lib/duckdb/appender.rb', line 76

def append_date(value)
  date = to_date(value)

  _append_date(date.year, date.month, date.day)
end

#append_defaultObject



290
291
292
293
294
295
296
297
298
# File 'ext/duckdb/appender.c', line 290

static VALUE appender_append_default(VALUE self) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_default(ctx->appender) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_double(val) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
# File 'ext/duckdb/appender.c', line 227

static VALUE appender_append_double(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    double dval = NUM2DBL(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_double(ctx->appender, dval) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_float(val) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'ext/duckdb/appender.c', line 215

static VALUE appender_append_float(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    float fval = (float)NUM2DBL(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_float(ctx->appender, fval) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_hugeint(value) ⇒ Object

appends huge int value.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE numbers (num HUGEINT)')
appender = con.appender('numbers')
appender
  .begin_row
  .append_hugeint(-170_141_183_460_469_231_731_687_303_715_884_105_727)
  .end_row


37
38
39
40
# File 'lib/duckdb/appender.rb', line 37

def append_hugeint(value)
  lower, upper = integer_to_hugeint(value)
  _append_hugeint(lower, upper)
end

#append_int16(val) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'ext/duckdb/appender.c', line 131

static VALUE appender_append_int16(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    int16_t i16val = (int16_t)NUM2INT(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_int16(ctx->appender, i16val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_int32(val) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'ext/duckdb/appender.c', line 143

static VALUE appender_append_int32(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    int32_t i32val = (int32_t)NUM2INT(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_int32(ctx->appender, i32val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_int64(val) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'ext/duckdb/appender.c', line 155

static VALUE appender_append_int64(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    int64_t i64val = (int64_t)NUM2LL(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_int64(ctx->appender, i64val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_int8(val) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'ext/duckdb/appender.c', line 119

static VALUE appender_append_int8(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    int8_t i8val = (int8_t)NUM2INT(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_int8(ctx->appender, i8val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_interval(value) ⇒ Object

appends interval. The argument must be ISO8601 duration format. WARNING: This method is expremental.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE intervals (interval_value INTERVAL)')
appender = con.appender('intervals')
appender
  .begin_row
  .append_interval('P1Y2D') # => append 1 year 2 days interval.
  .end_row
  .flush


141
142
143
144
# File 'lib/duckdb/appender.rb', line 141

def append_interval(value)
  value = Interval.to_interval(value)
  _append_interval(value.interval_months, value.interval_days, value.interval_micros)
end

#append_nullObject



279
280
281
282
283
284
285
286
287
# File 'ext/duckdb/appender.c', line 279

static VALUE appender_append_null(VALUE self) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_null(ctx->appender) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_row(*args) ⇒ Object

append a row.

appender.append_row(1, 'Alice')

is same as:

appender.begin_row
appender.append(1)
appender.append('Alice')
appender.end_row


203
204
205
206
207
208
209
# File 'lib/duckdb/appender.rb', line 203

def append_row(*args)
  begin_row
  args.each do |arg|
    append(arg)
  end
  end_row
end

#append_time(value) ⇒ Object

appends time value.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE times (time_value TIME)')
appender = con.appender('times')
appender.begin_row
appender.append_time(Time.now)
# or
# appender.append_time('01:01:01')
appender.end_row
appender.flush


97
98
99
100
101
# File 'lib/duckdb/appender.rb', line 97

def append_time(value)
  time = _parse_time(value)

  _append_time(time.hour, time.min, time.sec, time.usec)
end

#append_timestamp(value) ⇒ Object

appends timestamp value.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE timestamps (timestamp_value TIMESTAMP)')
appender = con.appender('timestamps')
appender.begin_row
appender.append_time(Time.now)
# or
# appender.append_time(Date.today)
# appender.append_time('2021-08-01 01:01:01')
appender.end_row
appender.flush


119
120
121
122
123
# File 'lib/duckdb/appender.rb', line 119

def append_timestamp(value)
  time = to_time(value)

  _append_timestamp(time.year, time.month, time.day, time.hour, time.min, time.sec, time.nsec / 1000)
end

#append_uhugeint(value) ⇒ Object

appends unsigned huge int value.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE numbers (num UHUGEINT)')
appender = con.appender('numbers')
appender
  .begin_row
  .append_hugeint(340_282_366_920_938_463_463_374_607_431_768_211_455)
  .end_row


55
56
57
58
# File 'lib/duckdb/appender.rb', line 55

def append_uhugeint(value)
  lower, upper = integer_to_hugeint(value)
  _append_uhugeint(lower, upper)
end

#append_uint16(val) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'ext/duckdb/appender.c', line 179

static VALUE appender_append_uint16(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    uint16_t ui16val = (uint16_t)NUM2UINT(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_uint16(ctx->appender, ui16val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_uint32(val) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'ext/duckdb/appender.c', line 191

static VALUE appender_append_uint32(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    uint32_t ui32val = (uint32_t)NUM2UINT(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_uint32(ctx->appender, ui32val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_uint64(val) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'ext/duckdb/appender.c', line 203

static VALUE appender_append_uint64(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    uint64_t ui64val = (uint64_t)NUM2ULL(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_uint64(ctx->appender, ui64val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_uint8(val) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'ext/duckdb/appender.c', line 167

static VALUE appender_append_uint8(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    int8_t ui8val = (uint8_t)NUM2UINT(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_uint8(ctx->appender, ui8val) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_varchar(val) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'ext/duckdb/appender.c', line 239

static VALUE appender_append_varchar(VALUE self, VALUE val) {
    rubyDuckDBAppender *ctx;
    char *pval = StringValuePtr(val);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_varchar(ctx->appender, pval) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#append_varchar_length(val, len) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/duckdb/appender.c', line 251

static VALUE appender_append_varchar_length(VALUE self, VALUE val, VALUE len) {
    rubyDuckDBAppender *ctx;

    char *pval = StringValuePtr(val);
    idx_t length = (idx_t)NUM2ULL(len);

    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_append_varchar_length(ctx->appender, pval, length) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to append");
    }
    return self;
}

#begin_rowObject



85
86
87
88
89
90
91
92
93
# File 'ext/duckdb/appender.c', line 85

static VALUE appender_begin_row(VALUE self) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_appender_begin_row(ctx->appender) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to flush");
    }
    return self;
}

#closeObject



395
396
397
398
399
400
401
402
403
# File 'ext/duckdb/appender.c', line 395

static VALUE appender_close(VALUE self) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_appender_close(ctx->appender) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to flush");
    }
    return self;
}

#end_rowObject



95
96
97
98
99
100
101
102
103
# File 'ext/duckdb/appender.c', line 95

static VALUE appender_end_row(VALUE self) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_appender_end_row(ctx->appender) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to flush");
    }
    return self;
}

#flushObject



385
386
387
388
389
390
391
392
393
# File 'ext/duckdb/appender.c', line 385

static VALUE appender_flush(VALUE self) {
    rubyDuckDBAppender *ctx;
    TypedData_Get_Struct(self, rubyDuckDBAppender, &appender_data_type, ctx);

    if (duckdb_appender_flush(ctx->appender) == DuckDBError) {
        rb_raise(eDuckDBError, "failed to flush");
    }
    return self;
}