Class: DuckDB::LogicalType

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

Instance Method Summary collapse

Constructor Details

#initialize(type_id_arg) ⇒ Object



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

static VALUE initialize(VALUE self, VALUE type_id_arg) {
    rubyDuckDBLogicalType *ctx;
    duckdb_type type = (duckdb_type)NUM2INT(type_id_arg);
    duckdb_logical_type new_logical_type;

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    if (ctx->logical_type) {
        duckdb_destroy_logical_type(&(ctx->logical_type));
    }

    new_logical_type = duckdb_create_logical_type(type);

    if (!new_logical_type || duckdb_get_type_id(new_logical_type) == DUCKDB_TYPE_INVALID) {
        if (new_logical_type) {
            duckdb_destroy_logical_type(&new_logical_type);
        }
        rb_raise(rb_eArgError, "Invalid or unsupported logical type ID: %d", type);
    }

    ctx->logical_type = new_logical_type;

    return self;
}

Instance Method Details

#logical_type.internal_typeSymbol

Returns the logical type’s internal type.

Returns:

  • (Symbol)


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'ext/duckdb/logical_type.c', line 335

static VALUE duckdb_logical_type__internal_type(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    duckdb_type type_id;
    duckdb_type internal_type_id;

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    type_id = duckdb_get_type_id(ctx->logical_type);
    switch (type_id) {
        case DUCKDB_TYPE_DECIMAL:
            internal_type_id = duckdb_decimal_internal_type(ctx->logical_type);
            break;
        case DUCKDB_TYPE_ENUM:
            internal_type_id = duckdb_enum_internal_type(ctx->logical_type);
            break;
        default:
            internal_type_id = DUCKDB_TYPE_INVALID;
    }

    return INT2FIX(internal_type_id);
}

#logical_type.child_countInteger

Returns the number of children of a struct type, otherwise 0.

Returns:

  • (Integer)


130
131
132
133
134
# File 'ext/duckdb/logical_type.c', line 130

static VALUE duckdb_logical_type_child_count(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    return INT2FIX(duckdb_struct_type_child_count(ctx->logical_type));
}

#logical_type.child_name(index) ⇒ String

Returns the name of the struct child at the specified index.

Returns:

  • (String)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'ext/duckdb/logical_type.c', line 143

static VALUE duckdb_logical_type_child_name_at(VALUE self, VALUE cidx) {
    rubyDuckDBLogicalType *ctx;
    VALUE cname;
    const char *child_name;
    idx_t idx = NUM2ULL(cidx);

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    child_name = duckdb_struct_type_child_name(ctx->logical_type, idx);
    if (child_name == NULL) {
        rb_raise(eDuckDBError, "fail to get name of %llu child", (unsigned long long)idx);
    }
    cname = rb_str_new_cstr(child_name);
    duckdb_free((void *)child_name);
    return cname;
}

#logical_type.child_typeDuckDB::LogicalType

Returns the child logical type for list and map types, otherwise nil.

Returns:



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/duckdb/logical_type.c', line 167

static VALUE duckdb_logical_type_child_type(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    duckdb_type type_id;
    duckdb_logical_type child_logical_type;
    VALUE logical_type = Qnil;

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    type_id = duckdb_get_type_id(ctx->logical_type);

    switch(type_id) {
        case DUCKDB_TYPE_LIST:
        case DUCKDB_TYPE_MAP:
            child_logical_type = duckdb_list_type_child_type(ctx->logical_type);
            logical_type = rbduckdb_create_logical_type(child_logical_type);
            break;
        case DUCKDB_TYPE_ARRAY:
            child_logical_type = duckdb_array_type_child_type(ctx->logical_type);
            logical_type = rbduckdb_create_logical_type(child_logical_type);
            break;
        default:
            logical_type = Qnil;
    }
    return logical_type;
}

#logical_type.child_type_at(index) ⇒ DuckDB::LogicalType

Returns the child logical type for struct types at the specified index as a DuckDB::LogicalType object.

Returns:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'ext/duckdb/logical_type.c', line 200

static VALUE duckdb_logical_type_child_type_at(VALUE self, VALUE cidx) {
    rubyDuckDBLogicalType *ctx;
    duckdb_logical_type struct_child_type;
    idx_t idx = NUM2ULL(cidx);

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    struct_child_type = duckdb_struct_type_child_type(ctx->logical_type, idx);
    if (struct_child_type == NULL) {
        rb_raise(eDuckDBError,
                 "Failed to get the struct child type at index %llu",
                 (unsigned long long)idx);
    }

    return rbduckdb_create_logical_type(struct_child_type);
}

#logical_type.dictionary_sizeInteger

Returns the dictionary size of the enum type.

Returns:

  • (Integer)


364
365
366
367
368
# File 'ext/duckdb/logical_type.c', line 364

static VALUE duckdb_logical_type_dictionary_size(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    return INT2FIX(duckdb_enum_dictionary_size(ctx->logical_type));
}

#logical_type.dictionary_value_at(index) ⇒ String

Returns the dictionary value at the specified index.

Returns:

  • (String)


377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/duckdb/logical_type.c', line 377

static VALUE duckdb_logical_type_dictionary_value_at(VALUE self, VALUE didx) {
    rubyDuckDBLogicalType *ctx;
    VALUE dvalue;
    const char *dict_value;
    idx_t idx = NUM2ULL(didx);

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    dict_value = duckdb_enum_dictionary_value(ctx->logical_type, idx);
    if (dict_value == NULL) {
        rb_raise(eDuckDBError, "fail to get dictionary value of %llu", (unsigned long long)idx);
    }
    dvalue = rb_utf8_str_new_cstr(dict_value);
    duckdb_free((void *)dict_value);
    return dvalue;
}

#each_child_nameObject

Iterates over each struct child name.

When a block is provided, this method yields each struct child name in order. It also returns the total number of children yielded.

struct_logical_type.each_child_name do |name|
  puts "Struct child: #{name}"
end

If no block is given, an Enumerator is returned, which can be used to retrieve all child names.

names = struct_logical_type.each_child_name.to_a
# => ["child1", "child2"]


150
151
152
153
154
155
156
# File 'lib/duckdb/logical_type.rb', line 150

def each_child_name
  return to_enum(__method__) {child_count} unless block_given?

  child_count.times do |i|
    yield child_name_at(i)
  end
end

#each_child_typeObject

Iterates over each struct child type.

When a block is provided, this method yields each struct child type in order. It also returns the total number of children yielded.

struct_logical_type.each_child_type do |logical_type|
  puts "Struct child type: #{logical_type.type}"
end

If no block is given, an Enumerator is returned, which can be used to retrieve all child logical types.

types = struct_logical_type.each_child_type.map(&:type)
# => [:integer, :varchar]


172
173
174
175
176
177
178
# File 'lib/duckdb/logical_type.rb', line 172

def each_child_type
  return to_enum(__method__) {child_count} unless block_given?

  child_count.times do |i|
    yield child_type_at(i)
  end
end

#each_dictionary_valueObject

Iterates over each enum dictionary value.

When a block is provided, this method yields each enum dictionary value in order. It also returns the total number of dictionary values yielded.

enum_logical_type.each_value do |value|
  puts "Enum value: #{value}"
end

If no block is given, an Enumerator is returned, which can be used to retrieve all enum dictionary values.

values = enum_logical_type.each_value.to_a
# => ["happy", "sad"]


194
195
196
197
198
199
200
# File 'lib/duckdb/logical_type.rb', line 194

def each_dictionary_value
  return to_enum(__method__) {dictionary_size} unless block_given?

  dictionary_size.times do |i|
    yield dictionary_value_at(i)
  end
end

#each_member_nameObject

Iterates over each union member name.

When a block is provided, this method yields each union member name in order. It also returns the total number of members yielded.

union_logical_type.each_member_name do |name|
  puts "Union member: #{name}"
end

If no block is given, an Enumerator is returned, which can be used to retrieve all member names.

names = union_logical_type.each_member_name.to_a
# => ["member1", "member2"]


106
107
108
109
110
111
112
# File 'lib/duckdb/logical_type.rb', line 106

def each_member_name
  return to_enum(__method__) {member_count} unless block_given?

  member_count.times do |i|
    yield member_name_at(i)
  end
end

#each_member_typeObject

Iterates over each union member type.

When a block is provided, this method yields each union member logical type in order. It also returns the total number of members yielded.

union_logical_type.each_member_type do |logical_type|
  puts "Union member: #{logical_type.type}"
end

If no block is given, an Enumerator is returned, which can be used to retrieve all member logical types.

names = union_logical_type.each_member_type.map(&:type)
# => [:varchar, :integer]


128
129
130
131
132
133
134
# File 'lib/duckdb/logical_type.rb', line 128

def each_member_type
  return to_enum(__method__) {member_count} unless block_given?

  member_count.times do |i|
    yield member_type_at(i)
  end
end

#logical_type.aliasString Also known as: alias

Returns the alias of the logical type.

Returns:

  • (String)


401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'ext/duckdb/logical_type.c', line 401

static VALUE duckdb_logical_type__get_alias(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    VALUE alias = Qnil;
    const char *_alias;

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    _alias = duckdb_logical_type_get_alias(ctx->logical_type);
    if (_alias != NULL) {
        alias = rb_utf8_str_new_cstr(_alias);
    }
    duckdb_free((void *)_alias);
    return alias;
}

#internal_typeObject

returns logical type’s internal type symbol for Decimal or Enum types ‘:unknown` means that the logical type’s type is unknown/unsupported by ruby-duckdb. ‘:invalid` means that the logical type’s type is invalid in duckdb.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query("CREATE TYPE mood AS ENUM ('happy', 'sad')")
con.query("CREATE TABLE emotions (id INTEGER, enum_col mood)")

users = con.query('SELECT * FROM emotions')
ernum_col = users.columns.find { |col| col.name == 'enum_col' }
enum_col.logical_type.internal_type #=> :utinyint


87
88
89
90
# File 'lib/duckdb/logical_type.rb', line 87

def internal_type
  type_id = _internal_type
  DuckDB::Converter::IntToSym.type_to_sym(type_id)
end

#logical_type.key_typeDuckDB::LogicalType

Returns the key logical type for map type, otherwise nil.

Returns:



237
238
239
240
241
242
243
244
245
246
# File 'ext/duckdb/logical_type.c', line 237

static VALUE duckdb_logical_type_key_type(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    duckdb_logical_type key_logical_type;
    VALUE logical_type = Qnil;

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    key_logical_type = duckdb_map_type_key_type(ctx->logical_type);
    logical_type = rbduckdb_create_logical_type(key_logical_type);
    return logical_type;
}

#logical_type.member_countInteger

Returns the member count of union type, otherwise 0.

Returns:

  • (Integer)


273
274
275
276
277
# File 'ext/duckdb/logical_type.c', line 273

static VALUE duckdb_logical_type_member_count(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    return INT2FIX(duckdb_union_type_member_count(ctx->logical_type));
}

#logical_type.member_name_at(index) ⇒ String

Returns the name of the union member at the specified index.

Returns:

  • (String)


286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'ext/duckdb/logical_type.c', line 286

static VALUE duckdb_logical_type_member_name_at(VALUE self, VALUE midx) {
    rubyDuckDBLogicalType *ctx;
    VALUE mname;
    const char *member_name;
    idx_t idx = NUM2ULL(midx);

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    member_name = duckdb_union_type_member_name(ctx->logical_type, idx);
    if (member_name == NULL) {
        rb_raise(eDuckDBError, "fail to get name of %llu member", (unsigned long long)idx);
    }
    mname = rb_str_new_cstr(member_name);
    duckdb_free((void *)member_name);
    return mname;
}

#logical_type.member_type_at(index) ⇒ DuckDB::LogicalType

Returns the logical type of the union member at the specified index as a DuckDB::LogicalType object.

Returns:



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'ext/duckdb/logical_type.c', line 311

static VALUE duckdb_logical_type_member_type_at(VALUE self, VALUE midx) {
    rubyDuckDBLogicalType *ctx;
    duckdb_logical_type union_member_type;
    idx_t idx = NUM2ULL(midx);

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);

    union_member_type = duckdb_union_type_member_type(ctx->logical_type, idx);
    if (union_member_type == NULL) {
        rb_raise(eDuckDBError,
                 "Failed to get the union member type at index %llu",
                 (unsigned long long)idx);
    }

    return rbduckdb_create_logical_type(union_member_type);
}

#logical_type.scaleInteger

Returns the scale of the decimal column.

Returns:

  • (Integer)


117
118
119
120
121
# File 'ext/duckdb/logical_type.c', line 117

static VALUE duckdb_logical_type_scale(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    return INT2FIX(duckdb_decimal_scale(ctx->logical_type));
}

#logical_type.aliasString Also known as: alias=

Return the set alias of the logical type.

Returns:

  • (String)


423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'ext/duckdb/logical_type.c', line 423

static VALUE duckdb_logical_type__set_alias(VALUE self, VALUE aname) {
    rubyDuckDBLogicalType *ctx;
    VALUE alias = Qnil;
    const char *_alias = StringValuePtr(aname);

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    duckdb_logical_type_set_alias(ctx->logical_type, _alias);
    if (_alias != NULL) {
        alias = rb_utf8_str_new_cstr(_alias);
    }

    return alias;
}

#logical_type.sizeInteger

Returns the size of the array column, otherwise 0.

Returns:

  • (Integer)


224
225
226
227
228
# File 'ext/duckdb/logical_type.c', line 224

static VALUE duckdb_logical_type_size(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    return INT2FIX(duckdb_array_type_array_size(ctx->logical_type));
}

#typeObject

returns logical type’s type symbol ‘:unknown` means that the logical type’s type is unknown/unsupported by ruby-duckdb. ‘:invalid` means that the logical type’s type is invalid in duckdb.

require 'duckdb'
db = DuckDB::Database.open
con = db.connect
con.query('CREATE TABLE climates (id INTEGER, temperature DECIMAIL)')

users = con.query('SELECT * FROM climates')
columns = users.columns
columns.second.logical_type.type #=> :decimal


69
70
71
72
# File 'lib/duckdb/logical_type.rb', line 69

def type
  type_id = _type
  DuckDB::Converter::IntToSym.type_to_sym(type_id)
end

#logical_type.value_typeDuckDB::LogicalType

Returns the value logical type for map type, otherwise nil.

Returns:



255
256
257
258
259
260
261
262
263
264
# File 'ext/duckdb/logical_type.c', line 255

static VALUE duckdb_logical_type_value_type(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    duckdb_logical_type value_logical_type;
    VALUE logical_type = Qnil;

    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    value_logical_type = duckdb_map_type_value_type(ctx->logical_type);
    logical_type = rbduckdb_create_logical_type(value_logical_type);
    return logical_type;
}

#logical_type.widthInteger

Returns the width of the decimal column.

Returns:

  • (Integer)


104
105
106
107
108
# File 'ext/duckdb/logical_type.c', line 104

static VALUE duckdb_logical_type_width(VALUE self) {
    rubyDuckDBLogicalType *ctx;
    TypedData_Get_Struct(self, rubyDuckDBLogicalType, &logical_type_data_type, ctx);
    return INT2FIX(duckdb_decimal_width(ctx->logical_type));
}