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

Refactor UnwrapCastInComparison to remove Expr clones #10115

Conversation

peter-toth
Copy link
Contributor

Which issue does this PR close?

Part of #9637, follow-up to #10087.

Rationale for this change

Eliminates remaining Expr clones.

What changes are included in this PR?

This PR:

  • Refactors UnwrapCastExprRewriter to remove Expr clones.
  • Adds handling of a few unhandled errors in the BinaryExpr case as if any error occurs this optimizer rule should return the original expression.

Are these changes tested?

Yes, with existing UTs.

Are there any user-facing changes?

No.

@github-actions github-actions bot added the optimizer Optimizer rules label Apr 17, 2024
@peter-toth peter-toth marked this pull request as draft April 17, 2024 14:07
@peter-toth peter-toth force-pushed the refactor-unwrapcastincomparison-to-remove-expr-clones branch 2 times, most recently from cefe8b9 to 295ef58 Compare April 17, 2024 15:22
@peter-toth peter-toth force-pushed the refactor-unwrapcastincomparison-to-remove-expr-clones branch from 295ef58 to 22b55f4 Compare April 17, 2024 15:30
@peter-toth peter-toth marked this pull request as ready for review April 17, 2024 15:56
@peter-toth
Copy link
Contributor Author

The test failure:

[SQL] SELECT * FROM (
    SELECT 1 as c, 2 as d
    UNION ALL
    SELECT 1 as c, 3 AS d
) as a FULL JOIN (SELECT 1 as e, 3 AS f) AS rhs ON a.c=rhs.e;
[Diff] (-expected|+actual)
-   1 2 1 3
-   1 3 1 3
+   1 3 1 3
+   1 2 1 3
at test_files/joins.slt:3676

seems unrelated.

fn f_up(&mut self, expr: Expr) -> Result<Transformed<Expr>> {
match &expr {
fn f_up(&mut self, mut expr: Expr) -> Result<Transformed<Expr>> {
match &mut expr {
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 was trying a few different patterns to remove Expr clones in this rule. The match &mut expr way seems to be most straightforward as expr is still available after pattern matching so we can return it unchanged if needed (return Ok(Transformed::no(expr));) but we can also mutate some parts of the expr if needed.

};
// unwrap the cast/try_cast for the left expr
**left =
mem::replace(left_expr, Expr::Literal(ScalarValue::Null));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would be nice to have a default Expr so that we could use mem::take().

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yeah!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shall I open a follow-up PR that adds default for Expr?

Copy link
Contributor

Choose a reason for hiding this comment

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

mem::take is a nice thing to avoid cloning, I think I used it to avoid String cloning other day, and we can have a default Expr with panicking inside I'd say...

Copy link
Contributor Author

@peter-toth peter-toth Apr 18, 2024

Choose a reason for hiding this comment

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

I've opened a minor PR to add default for Expr: #10127, but as Expr is just an enum, I don't know how to panic inside it.

@alamb
Copy link
Contributor

alamb commented Apr 17, 2024

CI failure I think is due to #10119, so I retriggered the run

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.

Very nice @peter-toth -- I actually tried this msyself last week and I was not able to get it to work. 👌

// do nothing
}
Expr::BinaryExpr(BinaryExpr { left, op, right })
if {
Copy link
Contributor

Choose a reason for hiding this comment

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

Today I learned you can add statements in a {} as part of a match clause. Nice!

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. I used to wrap another function.

Copy link
Contributor

@jayzhan211 jayzhan211 left a comment

Choose a reason for hiding this comment

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

👍

@jayzhan211
Copy link
Contributor

Thanks @peter-toth @alamb @comphead

@jayzhan211 jayzhan211 merged commit 88c98e1 into apache:main Apr 18, 2024
24 checks passed
@peter-toth
Copy link
Contributor Author

Thanks all 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.

4 participants