Skip to content

Commit

Permalink
Explicilty convert FLOAT4/FLOAT8 values before filter operation
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaruza committed Sep 19, 2024
1 parent 51d3a30 commit 95fb4d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/pgduckdb_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace pgduckdb {

template <class T, class OP>
bool
TemplatedFilterOperation(Datum &value, const duckdb::Value &constant) {
return OP::Operation((T)value, constant.GetValueUnsafe<T>());
TemplatedFilterOperation(T value, const duckdb::Value &constant) {
return OP::Operation(value, constant.GetValueUnsafe<T>());
}

template <class OP>
Expand Down Expand Up @@ -55,9 +55,9 @@ FilterOperationSwitch(Datum &value, duckdb::Value &constant, Oid type_oid) {
case INT8OID:
return TemplatedFilterOperation<int64_t, OP>(value, constant);
case FLOAT4OID:
return TemplatedFilterOperation<float, OP>(value, constant);
return TemplatedFilterOperation<float, OP>(DatumGetFloat4(value), constant);
case FLOAT8OID:
return TemplatedFilterOperation<double, OP>(value, constant);
return TemplatedFilterOperation<double, OP>(DatumGetFloat8(value), constant);
case DATEOID: {
Datum date_datum = static_cast<int32_t>(value + pgduckdb::PGDUCKDB_DUCK_DATE_OFFSET);
return TemplatedFilterOperation<int32_t, OP>(date_datum, constant);
Expand Down
30 changes: 30 additions & 0 deletions test/regression/expected/query_filter.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE query_filter_int(a INT);
INSERT INTO query_filter_int SELECT g FROM generate_series(1,100) g;
SELECT COUNT(*) FROM query_filter_int WHERE a <= 50;
count
-------
50
(1 row)

DROP TABLE query_filter_int;
CREATE TABLE query_filter_float(a FLOAT8);
INSERT INTO query_filter_float VALUES (0.9), (1.0), (1.1);
SELECT COUNT(*) = 1 FROM query_filter_float WHERE a < 1.0;
?column?
----------
t
(1 row)

SELECT COUNT(*) = 2 FROM query_filter_float WHERE a <= 1.0;
?column?
----------
t
(1 row)

SELECT COUNT(*) = 2 FROM query_filter_float WHERE a < 1.1;
?column?
----------
t
(1 row)

DROP TABLE query_filter_float;
1 change: 1 addition & 0 deletions test/regression/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ test: duckdb_only_functions
test: cte
test: create_table_as
test: standard_conforming_strings
test: query_filter
12 changes: 12 additions & 0 deletions test/regression/sql/query_filter.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE query_filter_int(a INT);
INSERT INTO query_filter_int SELECT g FROM generate_series(1,100) g;
SELECT COUNT(*) FROM query_filter_int WHERE a <= 50;
DROP TABLE query_filter_int;

CREATE TABLE query_filter_float(a FLOAT8);
INSERT INTO query_filter_float VALUES (0.9), (1.0), (1.1);
SELECT COUNT(*) = 1 FROM query_filter_float WHERE a < 1.0;
SELECT COUNT(*) = 2 FROM query_filter_float WHERE a <= 1.0;
SELECT COUNT(*) = 2 FROM query_filter_float WHERE a < 1.1;

DROP TABLE query_filter_float;

0 comments on commit 95fb4d9

Please sign in to comment.