Class: DuckDB::Vector

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

Overview

The DuckDB::Vector represents a column vector in a data chunk.

Vectors store the actual data for table function output.

Example:

vector = output.get_vector(0)
vector.assign_string_element(0, 'hello')
vector.assign_string_element(1, 'world')

rubocop:disable Lint/EmptyClass

Instance Method Summary collapse

Instance Method Details

#assign_string_element(index, str) ⇒ self

Assigns a string value at the specified index.

vector.assign_string_element(0, 'hello')

Returns:

  • (self)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/duckdb/vector.c', line 93

static VALUE rbduckdb_vector_assign_string_element(VALUE self, VALUE index, VALUE str) {
    rubyDuckDBVector *ctx;
    idx_t idx;
    const char *string_val;

    TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);

    idx = NUM2ULL(index);
    string_val = StringValueCStr(str);

    duckdb_vector_assign_string_element(ctx->vector, idx, string_val);

    return self;
}

#assign_string_element_len(index, str) ⇒ self

Assigns a string/blob value at the specified index with explicit length. Supports strings containing null bytes (for BLOB columns).

vector.assign_string_element_len(0, "\x00\x01\x02\x03")

Returns:

  • (self)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'ext/duckdb/vector.c', line 117

static VALUE rbduckdb_vector_assign_string_element_len(VALUE self, VALUE index, VALUE str) {
    rubyDuckDBVector *ctx;
    idx_t idx;
    const char *string_val;
    idx_t str_len;

    TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);

    idx = NUM2ULL(index);
    string_val = StringValuePtr(str);
    str_len = RSTRING_LEN(str);

    duckdb_vector_assign_string_element_len(ctx->vector, idx, string_val, str_len);

    return self;
}

#get_dataInteger (pointer address)

Gets the raw data pointer for the vector. Returns the memory address as an integer.

ptr = vector.get_data

Returns:

  • (Integer (pointer address))


50
51
52
53
54
55
56
57
58
59
# File 'ext/duckdb/vector.c', line 50

static VALUE rbduckdb_vector_get_data(VALUE self) {
    rubyDuckDBVector *ctx;
    void *data;

    TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);

    data = duckdb_vector_get_data(ctx->vector);

    return ULL2NUM((uintptr_t)data);
}

#get_validityInteger

Gets the validity mask pointer for the vector. Returns nil if all values are valid.

validity = vector.get_validity

Returns:

  • (Integer)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'ext/duckdb/vector.c', line 70

static VALUE rbduckdb_vector_get_validity(VALUE self) {
    rubyDuckDBVector *ctx;
    uint64_t *validity;

    TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);

    validity = duckdb_vector_get_validity(ctx->vector);

    if (!validity) {
        return Qnil;
    }

    return ULL2NUM((uintptr_t)validity);
}

#logical_typeDuckDB::LogicalType

Gets the logical type of the vector.

vector = output.get_vector(0)
type = vector.logical_type
type.id  #=> DuckDB::Type::BIGINT

Returns:



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

static VALUE rbduckdb_vector_logical_type(VALUE self) {
    rubyDuckDBVector *ctx;
    duckdb_logical_type logical_type;

    TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);

    logical_type = duckdb_vector_get_column_type(ctx->vector);

    return rbduckdb_create_logical_type(logical_type);
}

#set_validity(index, valid) ⇒ self

Sets the validity of a value at the specified index.

vector.set_validity(0, false)  # Mark row 0 as NULL
vector.set_validity(1, true)   # Mark row 1 as valid

Returns:

  • (self)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'ext/duckdb/vector.c', line 164

static VALUE rbduckdb_vector_set_validity(VALUE self, VALUE index, VALUE valid) {
    rubyDuckDBVector *ctx;
    idx_t idx;
    uint64_t *validity;

    TypedData_Get_Struct(self, rubyDuckDBVector, &vector_data_type, ctx);

    idx = NUM2ULL(index);

    if (RTEST(valid)) {
        // Setting to valid - ensure validity mask exists and set bit
        duckdb_vector_ensure_validity_writable(ctx->vector);
        validity = duckdb_vector_get_validity(ctx->vector);
        duckdb_validity_set_row_valid(validity, idx);
    } else {
        // Setting to invalid (NULL)
        duckdb_vector_ensure_validity_writable(ctx->vector);
        validity = duckdb_vector_get_validity(ctx->vector);
        duckdb_validity_set_row_invalid(validity, idx);
    }

    return self;
}