Skip to content

fix: parenthesize nested expression display names#23356

Open
goutamadwant wants to merge 5 commits into
apache:mainfrom
goutamadwant:fix-schema-display-parentheses
Open

fix: parenthesize nested expression display names#23356
goutamadwant wants to merge 5 commits into
apache:mainfrom
goutamadwant:fix-schema-display-parentheses

Conversation

@goutamadwant

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Expression-derived display names could omit required parentheses for nested binary expressions. For example, (1 + 2) * 3 could be displayed as 1 + 2 * 3, which changes how the expression reads.

Unary expressions with binary children had the same ambiguity, such as NOT a = b and unary negative over a binary expression.

What changes are included in this PR?

  • Adds precedence-aware parenthesization for nested binary expressions in SchemaDisplay.
  • Applies the same parenthesization behavior to SqlDisplay.
  • Wraps binary children of unary NOT and unary negative expressions.
  • Adds focused unit coverage for (1 + 2) * 3, unary negative, and NOT with binary children.
  • Updates optimizer expected output where expression display names now include the corrected parentheses.

Are these changes tested?

  • cargo test -p datafusion-expr format_nested_binary_exprs_with_parentheses
  • cargo fmt --all
  • cargo test -p datafusion-expr
  • cargo test -p datafusion-optimizer
  • cargo clippy -p datafusion-expr -p datafusion-optimizer --all-targets --all-features -- -D warnings
  • git diff --check

Are there any user-facing changes?

Yes. Expression-derived column names and SQL-like expression display output now include parentheses when needed to preserve the intended expression structure.

@github-actions github-actions Bot added logical-expr Logical plan and expressions optimizer Optimizer rules labels Jul 7, 2026
@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) substrait Changes to the substrait crate labels Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions optimizer Optimizer rules 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

1 participant