Skip to content

Conversation

Damon07
Copy link
Contributor

@Damon07 Damon07 commented Jun 24, 2025

This PR fixes #18028. ToExpression of dynamic table filter may return constant true expression which causes the column referenced in the filter to be lost.

@duckdb-draftbot duckdb-draftbot marked this pull request as draft June 24, 2025 08:58
@Damon07 Damon07 marked this pull request as ready for review June 24, 2025 08:58
Copy link
Collaborator

@Mytherin Mytherin left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! Looks good - some comments below

PRAGMA enable_verification

statement ok
INSTALL tpch
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be require tpch

CALL dbgen(sf=0.1);

statement ok
SELECT * FROM (SELECT * FROM lineitem LIMIT 500) ORDER BY l_orderkey DESC LIMIT 10;
Copy link
Collaborator

Choose a reason for hiding this comment

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

dbgen is pretty slow - can we either add this to one of the existing TPC-H tests (e.g. test/sql/tpch/tpch_topn.test_slow) or write a failing test that uses a (smaller synthetic) dataset that likely also exhibits the problem.

@@ -256,7 +259,12 @@ void RemoveUnusedColumns::VisitOperator(LogicalOperator &op) {
ColumnBinding filter_binding(get.table_index, index.GetIndex());
auto column_ref = make_uniq<BoundColumnRefExpression>(std::move(column_type), filter_binding);
auto filter_expr = filter.second->ToExpression(*column_ref);
VisitExpression(&filter_expr);
if (filter_expr->GetExpressionClass() == ExpressionClass::BOUND_CONSTANT) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use filter_expr->IsScalar() instead?

VisitExpression(&filter_expr);
if (filter_expr->GetExpressionClass() == ExpressionClass::BOUND_CONSTANT) {
AddBinding(*column_ref);
column_refs.push_back(std::move(column_ref));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of having the special code here - can we just set filter_expr = std::move(column_ref)? Then we don't need the extra column_refs vector either.

@Mytherin
Copy link
Collaborator

Can we also retarget this to v1.3-ossivalis so the fix can ship in v1.3.2?

@Damon07 Damon07 changed the base branch from main to v1.3-ossivalis June 25, 2025 01:28
@Damon07 Damon07 changed the base branch from v1.3-ossivalis to main June 25, 2025 01:30
@Damon07 Damon07 changed the base branch from main to v1.3-ossivalis June 25, 2025 03:07
@Damon07 Damon07 marked this pull request as draft June 25, 2025 03:09
@Damon07 Damon07 marked this pull request as ready for review June 25, 2025 03:10
@Damon07 Damon07 marked this pull request as draft June 25, 2025 03:18
@Damon07 Damon07 marked this pull request as ready for review June 25, 2025 03:18
@Mytherin Mytherin merged commit 15ef853 into duckdb:v1.3-ossivalis Jun 25, 2025
50 of 51 checks passed
@Mytherin
Copy link
Collaborator

Thanks!

github-actions bot pushed a commit to duckdb/duckdb-r that referenced this pull request Jun 28, 2025
Fix handling dynamic table filters in RemoveUnusedColumns (duckdb/duckdb#18033)
github-actions bot added a commit to duckdb/duckdb-r that referenced this pull request Jun 28, 2025
Fix handling dynamic table filters in RemoveUnusedColumns (duckdb/duckdb#18033)

Co-authored-by: krlmlr <krlmlr@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Could not find column index for table filter, for nested selects
2 participants