Class: DuckDB::LogicalType
- Inherits:
-
Object
- Object
- DuckDB::LogicalType
- Defined in:
- lib/duckdb/logical_type.rb,
ext/duckdb/logical_type.c
Overview
rubocop:disable Metrics/ClassLength
Class Method Summary collapse
-
.create_array(type, size) ⇒ Object
Creates an array logical type with the given child type and size.
-
.create_decimal(width, scale) ⇒ Object
Creates a decimal logical type with the given width and scale.
-
.create_enum(*members) ⇒ Object
Creates an enum logical type with the given members.
-
.create_list(type) ⇒ Object
Creates a list logical type with the given child type.
-
.create_map(key_type, value_type) ⇒ Object
Creates a map logical type with the given key and value types.
-
.create_struct(**members) ⇒ Object
Creates a struct logical type with the given member names and types.
-
.create_union(**members) ⇒ Object
Creates a union logical type with the given member names and types.
- .resolve(symbol) ⇒ Object
Instance Method Summary collapse
- #child_count ⇒ Object
- #child_name_at(cidx) ⇒ Object
- #child_type ⇒ Object
- #child_type_at(cidx) ⇒ Object
- #dictionary_size ⇒ Object
- #dictionary_value_at(didx) ⇒ Object
-
#each_child_name ⇒ Object
Iterates over each struct child name.
-
#each_child_type ⇒ Object
Iterates over each struct child type.
-
#each_dictionary_value ⇒ Object
Iterates over each enum dictionary value.
-
#each_member_name ⇒ Object
Iterates over each union member name.
-
#each_member_type ⇒ Object
Iterates over each union member type.
- #get_alias ⇒ Object (also: #alias)
- #initialize(type_id_arg) ⇒ Object constructor
-
#inspect ⇒ Object
:nodoc:.
-
#internal_type ⇒ Object
returns logical type's internal type symbol for Decimal or Enum types
:unknownmeans that the logical type's type is unknown/unsupported by ruby-duckdb. - #key_type ⇒ Object
- #member_count ⇒ Object
- #member_name_at(midx) ⇒ Object
- #member_type_at(midx) ⇒ Object
- #scale ⇒ Object
- #set_alias(aname) ⇒ Object (also: #alias=)
- #size ⇒ Object
-
#to_s ⇒ Object
:nodoc:.
-
#type ⇒ Object
returns logical type's type symbol
:unknownmeans that the logical type's type is unknown/unsupported by ruby-duckdb. - #value_type ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(type_id_arg) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'ext/duckdb/logical_type.c', line 66
static VALUE logical_type_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;
}
|
Class Method Details
.create_array(type, size) ⇒ Object
Creates an array logical type with the given child type and size.
The type argument can be a symbol or a DuckDB::LogicalType instance.
The size argument specifies the fixed size of the array.
require 'duckdb'
array_type = DuckDB::LogicalType.create_array(:integer, 3)
array_type.type #=> :array
array_type.child_type.type #=> :integer
array_type.size #=> 3
83 84 85 |
# File 'lib/duckdb/logical_type.rb', line 83 def create_array(type, size) _create_array_type(LogicalType.resolve(type), size) end |
.create_decimal(width, scale) ⇒ Object
Creates a decimal logical type with the given width and scale.
require 'duckdb'
decimal_type = DuckDB::LogicalType.create_decimal(18, 3)
decimal_type.type #=> :decimal
decimal_type.width #=> 18
decimal_type.scale #=> 3
174 175 176 177 178 179 |
# File 'lib/duckdb/logical_type.rb', line 174 def create_decimal(width, scale) raise DuckDB::Error, 'width must be between 1 and 38' unless Converter::RANGE_DECIMAL_WIDTH.cover?(width) raise DuckDB::Error, "scale must be between 0 and width(#{width})" unless (0..width).cover?(scale) _create_decimal_type(width, scale) end |
.create_enum(*members) ⇒ Object
Creates an enum logical type with the given members.
Each member must be a String representing an enum member.
require 'duckdb'
enum_type = DuckDB::LogicalType.create_enum('happy', 'sad', 'neutral')
enum_type.type #=> :enum
enum_type.dictionary_size #=> 3
enum_type.dictionary_value_at(0) #=> "happy"
162 163 164 |
# File 'lib/duckdb/logical_type.rb', line 162 def create_enum(*members) _create_enum_type(members.map(&:to_s)) end |
.create_list(type) ⇒ Object
Creates a list logical type with the given child type.
The type argument can be a symbol or a DuckDB::LogicalType instance.
require 'duckdb'
list_type = DuckDB::LogicalType.create_list(:integer)
list_type.type #=> :list
list_type.child_type.type #=> :integer
nested_list = DuckDB::LogicalType.create_list(list_type)
nested_list.child_type.type #=> :list
99 100 101 |
# File 'lib/duckdb/logical_type.rb', line 99 def create_list(type) _create_list_type(LogicalType.resolve(type)) end |
.create_map(key_type, value_type) ⇒ Object
Creates a map logical type with the given key and value types.
The key_type and value_type arguments can be symbols or
DuckDB::LogicalType instances.
require 'duckdb'
map_type = DuckDB::LogicalType.create_map(:integer, :varchar)
map_type.type #=> :map
map_type.key_type.type #=> :integer
map_type.value_type.type #=> :varchar
114 115 116 |
# File 'lib/duckdb/logical_type.rb', line 114 def create_map(key_type, value_type) _create_map_type(LogicalType.resolve(key_type), LogicalType.resolve(value_type)) end |
.create_struct(**members) ⇒ Object
Creates a struct logical type with the given member names and types.
The keyword arguments map member names to types. Each type can be a symbol or a DuckDB::LogicalType instance.
require 'duckdb'
struct_type = DuckDB::LogicalType.create_struct(name: :varchar, age: :integer)
struct_type.type #=> :struct
struct_type.child_count #=> 2
struct_type.child_name_at(0) #=> "name"
struct_type.child_type_at(0).type #=> :varchar
147 148 149 150 |
# File 'lib/duckdb/logical_type.rb', line 147 def create_struct(**members) resolved = members.transform_values { |v| LogicalType.resolve(v) } _create_struct_type(resolved) end |
.create_union(**members) ⇒ Object
Creates a union logical type with the given member names and types.
The keyword arguments map member names to types. Each type can be a symbol or a DuckDB::LogicalType instance.
require 'duckdb'
union_type = DuckDB::LogicalType.create_union(num: :integer, str: :varchar)
union_type.type #=> :union
union_type.member_count #=> 2
union_type.member_name_at(0) #=> "num"
union_type.member_type_at(0).type #=> :integer
130 131 132 133 |
# File 'lib/duckdb/logical_type.rb', line 130 def create_union(**members) resolved = members.transform_values { |v| LogicalType.resolve(v) } _create_union_type(resolved) end |
.resolve(symbol) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/duckdb/logical_type.rb', line 62 def resolve(symbol) return symbol if symbol.is_a?(DuckDB::LogicalType) raise_resolve_error(symbol) unless symbol.respond_to?(:upcase) DuckDB::LogicalType.const_get(symbol.upcase) rescue NameError raise_resolve_error(symbol) end |
Instance Method Details
#child_count ⇒ Object
131 132 133 134 135 |
# File 'ext/duckdb/logical_type.c', line 131
static VALUE 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));
}
|
#child_name_at(cidx) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'ext/duckdb/logical_type.c', line 144
static VALUE 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;
}
|
#child_type ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/duckdb/logical_type.c', line 168
static VALUE 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;
}
|
#child_type_at(cidx) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'ext/duckdb/logical_type.c', line 201
static VALUE 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);
}
|
#dictionary_size ⇒ Object
365 366 367 368 369 |
# File 'ext/duckdb/logical_type.c', line 365
static VALUE 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));
}
|
#dictionary_value_at(didx) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'ext/duckdb/logical_type.c', line 378
static VALUE 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_name ⇒ Object
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"]
281 282 283 284 285 286 287 |
# File 'lib/duckdb/logical_type.rb', line 281 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_type ⇒ Object
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]
303 304 305 306 307 308 309 |
# File 'lib/duckdb/logical_type.rb', line 303 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_value ⇒ Object
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"]
325 326 327 328 329 330 331 |
# File 'lib/duckdb/logical_type.rb', line 325 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_name ⇒ Object
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"]
237 238 239 240 241 242 243 |
# File 'lib/duckdb/logical_type.rb', line 237 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_type ⇒ Object
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]
259 260 261 262 263 264 265 |
# File 'lib/duckdb/logical_type.rb', line 259 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 |
#get_alias ⇒ Object Also known as: alias
402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'ext/duckdb/logical_type.c', line 402
static VALUE 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;
}
|
#inspect ⇒ Object
:nodoc:
334 335 336 |
# File 'lib/duckdb/logical_type.rb', line 334 def inspect "<#{self.class}::#{type.upcase}>" end |
#internal_type ⇒ Object
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
218 219 220 221 |
# File 'lib/duckdb/logical_type.rb', line 218 def internal_type type_id = _internal_type DuckDB::Converter::IntToSym.type_to_sym(type_id) end |
#key_type ⇒ Object
238 239 240 241 242 243 244 245 246 247 |
# File 'ext/duckdb/logical_type.c', line 238
static VALUE 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;
}
|
#member_count ⇒ Object
274 275 276 277 278 |
# File 'ext/duckdb/logical_type.c', line 274
static VALUE 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));
}
|
#member_name_at(midx) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'ext/duckdb/logical_type.c', line 287
static VALUE 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;
}
|
#member_type_at(midx) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'ext/duckdb/logical_type.c', line 312
static VALUE 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);
}
|
#scale ⇒ Object
118 119 120 121 122 |
# File 'ext/duckdb/logical_type.c', line 118
static VALUE 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));
}
|
#set_alias(aname) ⇒ Object Also known as: alias=
424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'ext/duckdb/logical_type.c', line 424
static VALUE 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;
}
|
#size ⇒ Object
225 226 227 228 229 |
# File 'ext/duckdb/logical_type.c', line 225
static VALUE 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));
}
|
#to_s ⇒ Object
:nodoc:
339 340 341 |
# File 'lib/duckdb/logical_type.rb', line 339 def to_s inspect end |
#type ⇒ Object
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
200 201 202 203 |
# File 'lib/duckdb/logical_type.rb', line 200 def type type_id = _type DuckDB::Converter::IntToSym.type_to_sym(type_id) end |
#value_type ⇒ Object
256 257 258 259 260 261 262 263 264 265 |
# File 'ext/duckdb/logical_type.c', line 256
static VALUE 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;
}
|
#width ⇒ Object
105 106 107 108 109 |
# File 'ext/duckdb/logical_type.c', line 105
static VALUE 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));
}
|