class DuckDB::ScalarFunction
DuckDB::ScalarFunction encapsulates DuckDB’s scalar function
@note DuckDB::ScalarFunction is experimental. @note DuckDB::ScalarFunction must be used with threads=1 in DuckDB.
Set this with: connection.execute('SET threads=1')
Public Class Methods
Source
# File lib/duckdb/scalar_function.rb, line 39 def self.create(name:, return_type:, parameter_type: nil, parameter_types: nil, &) # rubocop:disable Metrics/MethodLength raise ArgumentError, 'Block required' unless block_given? raise ArgumentError, 'Cannot specify both parameter_type and parameter_types' if parameter_type && parameter_types params = if parameter_type [parameter_type] elsif parameter_types parameter_types else [] end sf = new sf.name = name.to_s sf.return_type = return_type params.each { |type| sf.add_parameter(type) } sf.set_function(&) sf end
Create and configure a scalar function in one call
@param name [String, Symbol] the function name @param return_type [DuckDB::LogicalType] the return type @param parameter_type [DuckDB::LogicalType, nil] single parameter type (use this OR parameter_types) @param parameter_types [Array<DuckDB::LogicalType>, nil] multiple parameter types @yield [*args] the function implementation @return [DuckDB::ScalarFunction] configured scalar function ready to register @raise [ArgumentError] if block is not provided or both parameter_type and parameter_types are specified
@example Single parameter function
sf = DuckDB::ScalarFunction.create( name: :triple, return_type: DuckDB::LogicalType::INTEGER, parameter_type: DuckDB::LogicalType::INTEGER ) { |v| v * 3 }
@example Multiple parameters
sf = DuckDB::ScalarFunction.create( name: :add, return_type: DuckDB::LogicalType::INTEGER, parameter_types: [DuckDB::LogicalType::INTEGER, DuckDB::LogicalType::INTEGER] ) { |a, b| a + b }
@example No parameters (constant function)
sf = DuckDB::ScalarFunction.create( name: :random_num, return_type: DuckDB::LogicalType::INTEGER ) { rand(100) }
Source
static VALUE duckdb_scalar_function_initialize(VALUE self) {
rubyDuckDBScalarFunction *p;
TypedData_Get_Struct(self, rubyDuckDBScalarFunction, &scalar_function_data_type, p);
p->scalar_function = duckdb_create_scalar_function();
p->function_proc = Qnil;
return self;
}
Public Instance Methods
Source
# File lib/duckdb/scalar_function.rb, line 88 def add_parameter(logical_type) raise DuckDB::Error, 'logical_type must be a DuckDB::LogicalType' unless logical_type.is_a?(DuckDB::LogicalType) unless SUPPORTED_TYPES.include?(logical_type.type) type_list = SUPPORTED_TYPES.map(&:upcase).join(', ') raise DuckDB::Error, "Only #{type_list} parameter types are currently supported" end _add_parameter(logical_type) end
Adds a parameter to the scalar function. Currently supports BIGINT, BLOB, BOOLEAN, DATE, DOUBLE, FLOAT, INTEGER, SMALLINT, TIME, TIMESTAMP, TINYINT, UBIGINT, UINTEGER, USMALLINT, UTINYINT, and VARCHAR types.
@param logical_type [DuckDB::LogicalType] the parameter type @return [DuckDB::ScalarFunction] self @raise [DuckDB::Error] if the type is not supported
Source
# File lib/duckdb/scalar_function.rb, line 107 def return_type=(logical_type) raise DuckDB::Error, 'logical_type must be a DuckDB::LogicalType' unless logical_type.is_a?(DuckDB::LogicalType) unless SUPPORTED_TYPES.include?(logical_type.type) type_list = SUPPORTED_TYPES.map(&:upcase).join(', ') raise DuckDB::Error, "Only #{type_list} return types are currently supported" end _set_return_type(logical_type) end
Sets the return type for the scalar function. Currently supports BIGINT, BLOB, BOOLEAN, DATE, DOUBLE, FLOAT, INTEGER, SMALLINT, TIME, TIMESTAMP, TINYINT, UBIGINT, UINTEGER, USMALLINT, UTINYINT, and VARCHAR types.
@param logical_type [DuckDB::LogicalType] the return type @return [DuckDB::ScalarFunction] self @raise [DuckDB::Error] if the type is not supported
Source
static VALUE rbduckdb_scalar_function_set_name(VALUE self, VALUE name) {
rubyDuckDBScalarFunction *p;
TypedData_Get_Struct(self, rubyDuckDBScalarFunction, &scalar_function_data_type, p);
const char *str = StringValuePtr(name);
duckdb_scalar_function_set_name(p->scalar_function, str);
return self;
}