Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly convert datum values before filter operation #194

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/pgduckdb_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ extern "C" {
#include "postgres.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"

#include "utils/date.h"
#include "utils/timestamp.h"
}

#include "pgduckdb/pgduckdb_filter.hpp"
Expand All @@ -16,8 +17,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 @@ -45,26 +46,26 @@ static bool
FilterOperationSwitch(Datum &value, duckdb::Value &constant, Oid type_oid) {
switch (type_oid) {
case BOOLOID:
return TemplatedFilterOperation<bool, OP>(value, constant);
return TemplatedFilterOperation<bool, OP>(DatumGetBool(value), constant);
case CHAROID:
return TemplatedFilterOperation<uint8_t, OP>(value, constant);
return TemplatedFilterOperation<uint8_t, OP>(DatumGetChar(value), constant);
case INT2OID:
return TemplatedFilterOperation<int16_t, OP>(value, constant);
return TemplatedFilterOperation<int16_t, OP>(DatumGetInt16(value), constant);
case INT4OID:
return TemplatedFilterOperation<int32_t, OP>(value, constant);
return TemplatedFilterOperation<int32_t, OP>(DatumGetInt32(value), constant);
case INT8OID:
return TemplatedFilterOperation<int64_t, OP>(value, constant);
return TemplatedFilterOperation<int64_t, OP>(DatumGetInt64(value), constant);
case FLOAT4OID:
return TemplatedFilterOperation<float, OP>(value, constant);
return TemplatedFilterOperation<float, OP>(DatumGetFloat4(value), constant);
Copy link
Collaborator

@JelteF JelteF Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should probably do this explicit conversion for all types here, not just for floats.

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);
int32_t date = DatumGetDateADT(value) + pgduckdb::PGDUCKDB_DUCK_DATE_OFFSET;
return TemplatedFilterOperation<int32_t, OP>(date, constant);
}
case TIMESTAMPOID: {
Datum timestamp_datum = static_cast<int64_t>(value + pgduckdb::PGDUCKDB_DUCK_TIMESTAMP_OFFSET);
return TemplatedFilterOperation<int64_t, OP>(timestamp_datum, constant);
int64_t timestamp = DatumGetTimestamp(value) + pgduckdb::PGDUCKDB_DUCK_TIMESTAMP_OFFSET;
return TemplatedFilterOperation<int64_t, OP>(timestamp, constant);
}
case TEXTOID:
case VARCHAROID:
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(*) FROM query_filter_float WHERE a < 1.0;
count
-------
1
(1 row)

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

SELECT COUNT(*) FROM query_filter_float WHERE a < 1.1;
count
-------
2
(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(*) FROM query_filter_float WHERE a < 1.0;
SELECT COUNT(*) FROM query_filter_float WHERE a <= 1.0;
SELECT COUNT(*) FROM query_filter_float WHERE a < 1.1;

DROP TABLE query_filter_float;