Skip to content

Conversation

@Jiashu-Hu
Copy link
Contributor

@Jiashu-Hu Jiashu-Hu commented Mar 11, 2025

…revent this specific situation

Which issue does this PR close?

Rationale for this change

This PR fixes a bug in PushDownFilter where a Filter node is incorrectly removed when its child is an extension node with no inputs (e.g., Filter: false -> TestNode becomes TestNode). This change ensures the Filter is preserved in such cases to maintain the intended query logic.

What changes are included in this PR?

Modified the rewrite method in PushDownFilter to add a specific check for LogicalPlan::Extension nodes with no children.
When an extension node has no inputs, the Filter is kept by resetting its input and returning it unchanged using Transformed::no.

Are these changes tested?

Yes, the changes are tested with an updated unit test test_push_down_filter_to_user_defined_node that:

Creates a TestUserNode (an extension node with no inputs).
Applies a Filter with a constant expression (false).
Verifies the optimized plan retains the Filter instead of removing it.

Are there any user-facing changes?

No, this is an internal optimization fix and does not alter the public API or user-facing behavior. It only ensures correct query execution for plans involving extension nodes with no children.

@github-actions github-actions bot added the optimizer Optimizer rules label Mar 11, 2025
Comment on lines 782 to 789
// This prevents the Filter from being removed when the extension node has no children,
// so we return the original Filter unchanged.
LogicalPlan::Extension(extension_plan)
if extension_plan.node.inputs().is_empty() =>
{
filter.input = Arc::new(LogicalPlan::Extension(extension_plan));
Ok(Transformed::no(LogicalPlan::Filter(filter)))
}
Copy link
Member

Choose a reason for hiding this comment

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

How about adding the part to the branch of LogicalPlan::Extension, so we don't need to add a such new branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can absolutely do that. I will do it later today. Thank you very much for your review!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved logic into the branch of LogicalPlan::Extension, thanks!

@alamb
Copy link
Contributor

alamb commented Mar 11, 2025

FYI @helgikrs -- can you also help review this PR?

let plan_schema = Arc::clone(plan.schema());

let LogicalPlan::Filter(mut filter) = plan else {
let LogicalPlan::Filter(mut filter) = plan.clone() else {

Choose a reason for hiding this comment

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

I don't think this clone is necessary--is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not necessary, I've removed it and committed to the new version. Thanks for your review!

}

#[test]
fn test_push_down_filter_to_user_defined_node_2() -> Result<()> {

Choose a reason for hiding this comment

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

As far as I can tell, this test is identical to the test_push_down_filter_to_user_defined_node test except for the comments. Consider removing one of them, or consolidating the common logic to remove the duplication if they are supposed to test different things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, thank you very much for your review! I've removed the second test in the latest commit.

Copy link

@helgikrs helgikrs left a comment

Choose a reason for hiding this comment

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

LGTM -- thanks

Copy link
Member

@xudong963 xudong963 left a comment

Choose a reason for hiding this comment

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

Thank you

@xudong963 xudong963 merged commit ce502ab into apache:main Mar 12, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PushDownFilter eliminates Filters with leaf Extension nodes

4 participants