class DuckDB::DataChunk
The DuckDB::DataChunk represents a chunk of data for table function output.
During table function execution, data chunks are used to return rows.
Example:
done = false table_function.init { |_init_info| done = false } table_function.execute do |func_info, output| if done output.size = 0 # Signal completion else # High-level API output.set_value(0, 0, 42) # column 0, row 0, value 42 output.set_value(1, 0, 'Alice') # column 1, row 0, value 'Alice' output.size = 1 done = true end end
Public Instance Methods
Source
static VALUE rbduckdb_data_chunk_column_count(VALUE self) {
rubyDuckDBDataChunk *ctx;
idx_t count;
TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
count = duckdb_data_chunk_get_column_count(ctx->data_chunk);
return ULL2NUM(count);
}
Returns the number of columns in the data chunk.
data_chunk.column_count # => 2
Source
static VALUE rbduckdb_data_chunk_get_vector(VALUE self, VALUE col_idx) {
rubyDuckDBDataChunk *ctx;
idx_t idx;
duckdb_vector vector;
VALUE vector_obj;
rubyDuckDBVector *vector_ctx;
TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
idx = NUM2ULL(col_idx);
vector = duckdb_data_chunk_get_vector(ctx->data_chunk, idx);
// Create Vector wrapper
vector_obj = rb_class_new_instance(0, NULL, cDuckDBVector);
vector_ctx = get_struct_vector(vector_obj);
vector_ctx->vector = vector;
return vector_obj;
}
Gets the vector at the specified column index.
vector = data_chunk.get_vector(0)
Source
# File lib/duckdb/data_chunk.rb, line 48 def set_value(col_idx, row_idx, value) vector = get_vector(col_idx) logical_type = vector.logical_type type_id = logical_type.type # Handle NULL if value.nil? vector.set_validity(row_idx, false) return value end case type_id when :boolean data = vector.get_data MemoryHelper.write_boolean(data, row_idx, value) when :tinyint data = vector.get_data MemoryHelper.write_tinyint(data, row_idx, value) when :smallint data = vector.get_data MemoryHelper.write_smallint(data, row_idx, value) when :integer data = vector.get_data MemoryHelper.write_integer(data, row_idx, value) when :bigint data = vector.get_data MemoryHelper.write_bigint(data, row_idx, value) when :utinyint data = vector.get_data MemoryHelper.write_utinyint(data, row_idx, value) when :usmallint data = vector.get_data MemoryHelper.write_usmallint(data, row_idx, value) when :uinteger data = vector.get_data MemoryHelper.write_uinteger(data, row_idx, value) when :ubigint data = vector.get_data MemoryHelper.write_ubigint(data, row_idx, value) when :float data = vector.get_data MemoryHelper.write_float(data, row_idx, value) when :double data = vector.get_data MemoryHelper.write_double(data, row_idx, value) when :varchar vector.assign_string_element(row_idx, value.to_s) when :blob vector.assign_string_element_len(row_idx, value.to_s) when :timestamp data = vector.get_data MemoryHelper.write_timestamp(data, row_idx, value) when :timestamp_tz data = vector.get_data MemoryHelper.write_timestamp_tz(data, row_idx, value) when :date data = vector.get_data MemoryHelper.write_date(data, row_idx, value) else raise ArgumentError, "Unsupported type for DataChunk#set_value: #{type_id} for value `#{value.inspect}`" end value end
Sets a value at the specified column and row index. Type conversion is automatic based on the column’s logical type.
@param col_idx [Integer] Column index (0-based) @param row_idx [Integer] Row index (0-based) @param value [Object] Value to set (Integer, Float, String, Time, Date, nil) @return [Object] The value that was set
@example Set integer value
output.set_value(0, 0, 42)
@example Set string value
output.set_value(1, 0, 'hello')
@example Set NULL value
output.set_value(0, 1, nil)
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
Source
static VALUE rbduckdb_data_chunk_get_size(VALUE self) {
rubyDuckDBDataChunk *ctx;
idx_t size;
TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
size = duckdb_data_chunk_get_size(ctx->data_chunk);
return ULL2NUM(size);
}
Returns the current number of tuples in the data chunk.
data_chunk.size # => 100
Source
static VALUE rbduckdb_data_chunk_set_size(VALUE self, VALUE size) {
rubyDuckDBDataChunk *ctx;
idx_t sz;
TypedData_Get_Struct(self, rubyDuckDBDataChunk, &data_chunk_data_type, ctx);
sz = NUM2ULL(size);
duckdb_data_chunk_set_size(ctx->data_chunk, sz);
return size;
}
Sets the number of tuples in the data chunk.
data_chunk.size = 50