Skip to content

Commit

Permalink
Merge pull request #293 from Mytherin/noexperimentalpushdown
Browse files Browse the repository at this point in the history
Enable filter pushdown by default - and add more extensive testing for filtering on all types
  • Loading branch information
Mytherin authored Feb 1, 2025
2 parents 478ee8f + 2113d33 commit ecabb61
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/postgres_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ static void LoadInternal(DatabaseInstance &db) {
config.AddExtensionOption("pg_connection_cache", "Whether or not to use the connection cache", LogicalType::BOOLEAN,
Value::BOOLEAN(true), PostgresConnectionPool::PostgresSetConnectionCache);
config.AddExtensionOption("pg_experimental_filter_pushdown",
"Whether or not to use filter pushdown (currently experimental)", LogicalType::BOOLEAN,
Value::BOOLEAN(false));
"Whether or not to use filter pushdown", LogicalType::BOOLEAN,
Value::BOOLEAN(true));
config.AddExtensionOption("pg_null_byte_replacement",
"When writing NULL bytes to Postgres, replace them with the given character",
LogicalType::VARCHAR, Value(), SetPostgresNullByteReplacement);
Expand Down
26 changes: 24 additions & 2 deletions src/postgres_filter_pushdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ string PostgresFilterPushdown::TransformComparision(ExpressionType type) {
}
}

string TransformBlob(const string &val) {
char const HEX_DIGITS[] = "0123456789ABCDEF";

string result = "'\\x";
for(idx_t i = 0; i < val.size(); i++) {
uint8_t byte_val = static_cast<uint8_t>(val[i]);
result += HEX_DIGITS[(byte_val >> 4) & 0xf];
result += HEX_DIGITS[byte_val & 0xf];
}
result += "'::BYTEA";
return result;
}

string TransformLiteral(const Value &val) {
switch (val.type().id()) {
case LogicalTypeId::BLOB:
return TransformBlob(StringValue::Get(val));
default:
return KeywordHelper::WriteQuoted(val.ToString());
}
}

string PostgresFilterPushdown::TransformFilter(string &column_name, TableFilter &filter) {
switch (filter.filter_type) {
case TableFilterType::IS_NULL:
Expand All @@ -51,7 +73,7 @@ string PostgresFilterPushdown::TransformFilter(string &column_name, TableFilter
}
case TableFilterType::CONSTANT_COMPARISON: {
auto &constant_filter = filter.Cast<ConstantFilter>();
auto constant_string = KeywordHelper::WriteQuoted(constant_filter.constant.ToString());
auto constant_string = TransformLiteral(constant_filter.constant);
auto operator_string = TransformComparision(constant_filter.comparison_type);
return StringUtil::Format("%s %s %s", column_name, operator_string, constant_string);
}
Expand All @@ -72,7 +94,7 @@ string PostgresFilterPushdown::TransformFilter(string &column_name, TableFilter
if (!in_list.empty()) {
in_list += ", ";
}
in_list += KeywordHelper::WriteQuoted(val.ToString());
in_list += TransformLiteral(val);
}
return column_name + " IN (" + in_list + ")";
}
Expand Down
20 changes: 13 additions & 7 deletions test/sql/storage/attach_types.test
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ FROM test_all_types();
statement ok
CREATE OR REPLACE TABLE s.all_types AS FROM all_types_tbl

#mode output_result
#
#statement ok
#DESCRIBE SELECT COLUMNS(*)::VARCHAR FROM all_types_tbl
#
#mode skip

query IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
SELECT COLUMNS(*)::VARCHAR FROM all_types_tbl
----
Expand All @@ -76,3 +69,16 @@ SELECT COLUMNS(*)::VARCHAR FROM s.all_types
false -128 -32768 -2147483648 -9223372036854775808 0 0 0 2000-01-01 00:00:00 2000-01-01 01:02:03 2000-01-01 01:02:03 2000-01-01 01:02:03 2000-01-01 01:02:03 00:00:00+15:00 2000-01-01 01:02:03 -999.9 -99999.9999 -999999999999.999999 -9999999999999999999999999999.9999999999 00000000-0000-0000-0000-000000000000 00:00:00 🦆🦆🦆🦆🦆🦆 thisisalongblob\x00withnullbytes 0010001001011100010101011010111 DUCK_DUCK_ENUM enum_0 enum_0 [] [] [] [] [] [🦆🦆🦆🦆🦆🦆, goose, NULL]
true 127 32767 2147483647 9223372036854775807 255 65535 4294967295 2000-01-01 24:00:00 2000-01-01 01:02:03 2000-01-01 01:02:03 2000-01-01 01:02:03 2000-01-01 01:02:03 00:00:00+15:00 2000-01-01 01:02:03 999.9 99999.9999 999999999999.999999 9999999999999999999999999999.9999999999 ffffffff-ffff-ffff-ffff-ffffffffffff 83 years 3 months 999 days 00:16:39.999999 goo se \x00\x00\x00a 10101 GOOSE enum_299 enum_69999 [42, 999, NULL, NULL, -42] [42.0, nan, inf, -inf, NULL, -42.0] [1970-01-01, infinity, -infinity, NULL, 2022-05-12] [1970-01-01 00:00:00, infinity, -infinity, NULL, 2022-05-12 16:23:45] [1970-01-01 00:00:00+00, infinity, -infinity, NULL, 2022-05-12 23:23:45+00] [🦆🦆🦆🦆🦆🦆, goose, NULL]
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

# filter pushdown
foreach column_name bool tinyint smallint int bigint utinyint usmallint uint date time timestamp timestamp_s timestamp_ms timestamp_ns time_tz timestamp_tz dec_4_1 dec_9_4 dec_18_6 dec38_10 uuid interval varchar blob bit small_enum medium_enum large_enum int_array double_array date_array timestamp_array timestamptz_array varchar_array

statement ok
SET VARIABLE minimum_value=(SELECT MIN(${column_name}) min_val FROM s.all_types);

query I
SELECT ANY_VALUE(${column_name})=getvariable('minimum_value') FROM s.all_types WHERE ${column_name}=getvariable('minimum_value')
----
true

endloop

0 comments on commit ecabb61

Please sign in to comment.