Skip to content

Fix for dynamic filters generated through top-n #297

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

Merged
merged 1 commit into from
Feb 7, 2025
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
22 changes: 17 additions & 5 deletions src/postgres_filter_pushdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ string PostgresFilterPushdown::CreateExpression(string &column_name, vector<uniq
string op) {
vector<string> filter_entries;
for (auto &filter : filters) {
filter_entries.push_back(TransformFilter(column_name, *filter));
auto filter_str = TransformFilter(column_name, *filter);
if (!filter_str.empty()) {
filter_entries.push_back(std::move(filter_str));
}
}
if (filter_entries.empty()) {
return string();
}
return "(" + StringUtil::Join(filter_entries, " " + op + " ") + ")";
}
Expand Down Expand Up @@ -98,6 +104,8 @@ string PostgresFilterPushdown::TransformFilter(string &column_name, TableFilter
}
return column_name + " IN (" + in_list + ")";
}
case TableFilterType::DYNAMIC_FILTER:
return string();
Copy link

Choose a reason for hiding this comment

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

In pg_duckdb we used a slightly different approach: We do this for any unsupported filter type that's inside an optional filter.

https://github.com/duckdb/pg_duckdb/blob/a9faa3cdb9d36881f793fef4fb519ebc675e4a6d/src/scan/postgres_scan.cpp#L66-L69

default:
throw InternalException("Unsupported table filter type");
}
Expand All @@ -111,13 +119,17 @@ string PostgresFilterPushdown::TransformFilters(const vector<column_t> &column_i
}
string result;
for (auto &entry : filters->filters) {
if (!result.empty()) {
result += " AND ";
}
auto column_name = KeywordHelper::WriteQuoted(names[column_ids[entry.first]], '"');
auto &filter = *entry.second;
auto filter_text = TransformFilter(column_name, filter);

result += TransformFilter(column_name, filter);
if (filter_text.empty()) {
continue;
}
if (!result.empty()) {
result += " AND ";
}
result += filter_text;
}
return result;
}
Expand Down
20 changes: 20 additions & 0 deletions test/sql/storage/attach_top_n.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# name: test/sql/storage/attach_top_n.test
# description: Attach with Top-N queries
# group: [storage]

require postgres_scanner

require-env POSTGRES_TEST_DATABASE_AVAILABLE

statement ok
ATTACH 'dbname=postgresscanner' AS s (TYPE POSTGRES);

query IIIIIIII
select * from s.pg_numtypes order by smallint_col limit 1
----
false -42 -42 -42 -42.01 -42.01 -42.0 -42.01

query IIIIIIII
select * from s.pg_numtypes where smallint_col >= 0 order by 2 limit 1
----
0 0 0 0 0.0 0.0 0.0 0.0
Loading