Skip to content

feat: remove filters with null constants - #8700

Merged
alamb merged 1 commit into
apache:mainfrom
asimsedhain:df-optimizer/8689/eliminate-null-filter
Jan 2, 2024
Merged

alamb merged 1 commit into
apache:mainfrom
asimsedhain:df-optimizer/8689/eliminate-null-filter

Conversation

@asimsedhain

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@jonahgao jonahgao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me!

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

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

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