Skip to content

Commit a9a8acc

Browse files
authored
Merge pull request #1073 from suketa/add-float-support-to-scalar-function
Add FLOAT support to ScalarFunction return type
2 parents 43599c5 + c5f2e8e commit a9a8acc

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
All notable changes to this project will be documented in this file.
44

55
# Unreleased
6+
- add FLOAT support to DuckDB::ScalarFunction return type.
67
- add BOOLEAN support to DuckDB::ScalarFunction return type.
78
- add DOUBLE support to DuckDB::ScalarFunction return type.
89
- add BIGINT support to DuckDB::ScalarFunction return type.

ext/duckdb/scalar_function.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ static void vector_set_value_at(duckdb_vector vector, duckdb_logical_type elemen
237237
case DUCKDB_TYPE_BIGINT:
238238
((int64_t *)vector_data)[index] = NUM2LL(value);
239239
break;
240+
case DUCKDB_TYPE_FLOAT:
241+
((float *)vector_data)[index] = (float)NUM2DBL(value);
242+
break;
240243
case DUCKDB_TYPE_DOUBLE:
241244
((double *)vector_data)[index] = NUM2DBL(value);
242245
break;

lib/duckdb/scalar_function.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module DuckDB
44
# DuckDB::ScalarFunction encapsulates DuckDB's scalar function
55
class ScalarFunction
66
# Sets the return type for the scalar function.
7-
# Currently supports INTEGER, BIGINT, DOUBLE, and BOOLEAN types.
7+
# Currently supports BOOLEAN, INTEGER, BIGINT, FLOAT, and DOUBLE types.
88
#
99
# @param logical_type [DuckDB::LogicalType] the return type
1010
# @return [DuckDB::ScalarFunction] self
@@ -13,8 +13,8 @@ def return_type=(logical_type)
1313
raise DuckDB::Error, 'logical_type must be a DuckDB::LogicalType' unless logical_type.is_a?(DuckDB::LogicalType)
1414

1515
# Check if the type is supported
16-
unless %i[boolean integer bigint double].include?(logical_type.type)
17-
raise DuckDB::Error, 'Only BOOLEAN, INTEGER, BIGINT, and DOUBLE return types are currently supported'
16+
unless %i[boolean integer bigint float double].include?(logical_type.type)
17+
raise DuckDB::Error, 'Only BOOLEAN, INTEGER, BIGINT, FLOAT, and DOUBLE return types are currently supported'
1818
end
1919

2020
_set_return_type(logical_type)

test/duckdb_test/scalar_function_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,22 @@ def test_scalar_function_boolean_return_type # rubocop:disable Metrics/MethodLen
197197

198198
assert_equal [[false], [false], [true]], result.to_a
199199
end
200+
201+
def test_scalar_function_float_return_type # rubocop:disable Metrics/MethodLength
202+
@con.execute('SET threads=1')
203+
@con.execute('CREATE TABLE test_table (value FLOAT)')
204+
@con.execute('INSERT INTO test_table VALUES (2.5)')
205+
206+
sf = DuckDB::ScalarFunction.new
207+
sf.name = 'add_half'
208+
sf.add_parameter(DuckDB::LogicalType.new(10)) # FLOAT (type ID 10)
209+
sf.return_type = DuckDB::LogicalType.new(10) # FLOAT
210+
sf.set_function { |v| v + 0.5 }
211+
212+
@con.register_scalar_function(sf)
213+
result = @con.execute('SELECT add_half(value) FROM test_table')
214+
215+
assert_in_delta 3.0, result.first.first, 0.0001
216+
end
200217
end
201218
end

0 commit comments

Comments
 (0)