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

feat: remove filters with null constants #8700

Merged

Conversation

asimsedhain
Copy link
Contributor

Which issue does this PR close?

Closes #8689

Rationale for this change

What changes are included in this PR?

Are these changes tested?

DataFusion CLI v34.0.0
❯ create table t(x int) as values (1), (2), (3);
0 rows in set. Query took 0.031 seconds.

❯ explain select x from t where null::bool;
+---------------+---------------+
| plan_type     | plan          |
+---------------+---------------+
| logical_plan  | EmptyRelation |
| physical_plan | EmptyExec     |
|               |               |
+---------------+---------------+
2 rows in set. Query took 0.005 seconds.

Are there any user-facing changes?

@github-actions github-actions bot added the optimizer Optimizer rules label Dec 31, 2023
predicate: Expr::Literal(ScalarValue::Boolean(None)),
input,
..
}) => Ok(Some(LogicalPlan::EmptyRelation(EmptyRelation {
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 merging it into the previous false match arm like this 🤔?

fn try_optimize(
        &self,
        plan: &LogicalPlan,
        _config: &dyn OptimizerConfig,
    ) -> Result<Option<LogicalPlan>> {
        match plan {
            LogicalPlan::Filter(Filter {
                predicate: Expr::Literal(ScalarValue::Boolean(v)),
                input,
                ..
            }) => {
                match *v {
                    // input also can be filter, apply again
                    Some(true) => Ok(Some(
                        self.try_optimize(input, _config)?
                            .unwrap_or_else(|| input.as_ref().clone()),
                    )),
                    Some(false) | None => {
                        Ok(Some(LogicalPlan::EmptyRelation(EmptyRelation {
                            produce_one_row: false,
                            schema: input.schema().clone(),
                        })))
                    }
                }
            }
            _ => Ok(None),
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@asimsedhain asimsedhain force-pushed the df-optimizer/8689/eliminate-null-filter branch from bcbf71f to 3cc08f7 Compare January 1, 2024 21:10
Copy link
Member

@jonahgao jonahgao left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Looks great -- thank you @asimsedhain and @jonahgao for the review

@alamb alamb merged commit c1fe3dd into apache:main Jan 2, 2024
22 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.

Remove filters for NULL constants (in addition to false)
3 participants