Skip to content

Always add parentheses when formatting BinaryExpr with SchemaDisplay #16209

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

hendrikmakait
Copy link
Contributor

@hendrikmakait hendrikmakait commented May 29, 2025

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

  • Adds a regression test
  • Always add parentheses when formatting BinaryExpr via SchemaDisplay

Are these changes tested?

Yes, added a regression test

Are there any user-facing changes?

No

@github-actions github-actions bot added the logical-expr Logical plan and expressions label May 29, 2025
@hendrikmakait hendrikmakait changed the title Fix SchemaDisplay for nested BinaryExpr Always add parentheses when formattingBinaryExpr with SchemaDisplay May 29, 2025
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

@alamb
Copy link
Contributor

alamb commented May 30, 2025

Given how widely used this code is I suspect this change will be quite invasive / require changing many tests

@hendrikmakait hendrikmakait marked this pull request as draft May 30, 2025 12:18
@github-actions github-actions bot added sql SQL Planner optimizer Optimizer rules core Core DataFusion crate substrait Changes to the substrait crate labels May 31, 2025
@github-actions github-actions bot added the documentation Improvements or additions to documentation label May 31, 2025
@github-actions github-actions bot added the sqllogictest SQL Logic Tests (.slt) label May 31, 2025
@@ -2500,7 +2500,7 @@ impl Display for SchemaDisplay<'_> {
}
}
Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
write!(f, "{} {op} {}", SchemaDisplay(left), SchemaDisplay(right),)
write!(f, "({} {op} {})", SchemaDisplay(left), SchemaDisplay(right),)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the actual change.

@hendrikmakait hendrikmakait marked this pull request as ready for review May 31, 2025 20:07
@xudong963
Copy link
Member

@hendrikmakait Hi, there are still a few places that need to change

@hendrikmakait hendrikmakait changed the title Always add parentheses when formattingBinaryExpr with SchemaDisplay Always add parentheses when formatting BinaryExpr with SchemaDisplay Jun 4, 2025
@hendrikmakait
Copy link
Contributor Author

@xudong963: CI is green now.

@xudong963
Copy link
Member

@xudong963: CI is green now.

cc @alamb for another look

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.

Thanks @hendrikmakait and @xudong963 !

I agree this is better, but I see a bunch of tests where the parens are added even when they aren't necessary to distinguish order

Maybe we could add a check and only add parens to the display if they were needed (in some sort of Expr::Binary operator tree 🤔 )

"| 2025-05-19 |",
"+-----------------------------------+",
"+-------------------------------------+",
"| make_date((t.y + Int64(1)),t.m,t.d) |",
Copy link
Contributor

Choose a reason for hiding this comment

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

The parens here seem unecessary (the top most expression) -- I wonder if we can avoid that somehow

@hendrikmakait
Copy link
Contributor Author

@alamb: That's a fair point, I actually brought it up in the issue (#16054 (comment)). I'm happy to adjust the implementation.

@alamb
Copy link
Contributor

alamb commented Jun 8, 2025

@alamb: That's a fair point, I actually brought it up in the issue (#16054 (comment)). I'm happy to adjust the implementation.

I think we should try to adjust if possible. Thank you @hendrikmakait

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate documentation Improvements or additions to documentation logical-expr Logical plan and expressions optimizer Optimizer rules sql SQL Planner sqllogictest SQL Logic Tests (.slt) substrait Changes to the substrait crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expr formatting missing parentheses
3 participants