Class: DuckDB::TableFunction::BindInfo

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

Overview

The DuckDB::TableFunction::BindInfo encapsulates information for the bind phase of table functions.

During the bind phase, you can:

  • Access parameters passed to the function

  • Define the output schema (columns)

  • Set performance hints (cardinality)

  • Report errors

Example:

table_function.bind do |bind_info|
  # Get parameters
  limit = bind_info.get_parameter(0).to_i

  # Define output schema
  bind_info.add_result_column('id', DuckDB::LogicalType::BIGINT)
  bind_info.add_result_column('name', DuckDB::LogicalType::VARCHAR)

  # Set cardinality hint
  bind_info.set_cardinality(limit, true)
end

Instance Method Summary collapse

Instance Method Details

#add_result_column(name, type) ⇒ Object



29
30
31
# File 'lib/duckdb/table_function/bind_info.rb', line 29

def add_result_column(name, type)
  _add_result_column(name.to_s, DuckDB::LogicalType.resolve(type))
end

#get_named_parameter(name) ⇒ nil

Gets the named parameter value, or nil if not provided.

param = bind_info.get_named_parameter('limit')

Returns:

  • (nil)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'ext/duckdb/table_function_bind_info.c', line 100

static VALUE rbduckdb_bind_info_get_named_parameter(VALUE self, VALUE name) {
    rubyDuckDBBindInfo *ctx;
    const char *param_name;
    duckdb_value param_value;
    VALUE result;

    TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);

    param_name = StringValueCStr(name);
    param_value = duckdb_bind_get_named_parameter(ctx->bind_info, param_name);

    // If parameter not found, return nil
    if (!param_value) {
        return Qnil;
    }

    result = rbduckdb_duckdb_value_to_ruby(param_value);

    duckdb_destroy_value(&param_value);

    return result;
}

#get_parameter(index) ⇒ Object

Gets the parameter value at the given index.

param = bind_info.get_parameter(0)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'ext/duckdb/table_function_bind_info.c', line 74

static VALUE rbduckdb_bind_info_get_parameter(VALUE self, VALUE index) {
    rubyDuckDBBindInfo *ctx;
    idx_t idx;
    duckdb_value param_value;
    VALUE result;

    TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);

    idx = NUM2ULL(index);
    param_value = duckdb_bind_get_parameter(ctx->bind_info, idx);

    result = rbduckdb_duckdb_value_to_ruby(param_value);

    duckdb_destroy_value(&param_value);

    return result;
}

#parameter_countInteger

Returns the number of parameters passed to the table function.

bind_info.parameter_count  # => 2

Returns:

  • (Integer)


55
56
57
58
59
60
61
62
63
64
# File 'ext/duckdb/table_function_bind_info.c', line 55

static VALUE rbduckdb_bind_info_parameter_count(VALUE self) {
    rubyDuckDBBindInfo *ctx;
    idx_t count;

    TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);

    count = duckdb_bind_get_parameter_count(ctx->bind_info);

    return ULL2NUM(count);
}

#set_cardinality(cardinality, is_exact) ⇒ self

Sets the estimated number of rows this function will return.

bind_info.set_cardinality(100, true)  # Exactly 100 rows
bind_info.set_cardinality(1000, false)  # Approximately 1000 rows

Returns:

  • (self)


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

static VALUE rbduckdb_bind_info_set_cardinality(VALUE self, VALUE cardinality, VALUE is_exact) {
    rubyDuckDBBindInfo *ctx;
    idx_t card;
    bool exact;

    TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);

    card = NUM2ULL(cardinality);
    exact = RTEST(is_exact);

    duckdb_bind_set_cardinality(ctx->bind_info, card, exact);

    return self;
}

#set_error(error_message) ⇒ self

Reports an error during bind phase.

bind_info.set_error('Invalid parameter value')

Returns:

  • (self)


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

static VALUE rbduckdb_bind_info_set_error(VALUE self, VALUE error) {
    rubyDuckDBBindInfo *ctx;
    const char *error_msg;

    TypedData_Get_Struct(self, rubyDuckDBBindInfo, &bind_info_data_type, ctx);

    error_msg = StringValueCStr(error);
    duckdb_bind_set_error(ctx->bind_info, error_msg);

    return self;
}