Print a space between adjacent prefix unary operators - #2424
Open
shuvamk wants to merge 1 commit into
Open
Conversation
`Expr::UnaryOp`'s `Display` writes `{op}{expr}` with no separator, so when
the operand is itself a `UnaryOp` the two operator glyphs are emitted glued
together and the output no longer round-trips:
SELECT ~ ~ 1 -> SELECT ~~1 -> "Expected: an expression, found: ~~"
`SELECT - -1` prints as `SELECT --1`, which is the same defect but only
breaks on dialects where `--` opens a line comment; MySQL requires
whitespace after `--` and reparses it unchanged.
The Postgres case is worse than a parse error, because `@@` is a distinct
operator: `SELECT @ @ 1` (abs of abs) prints as `SELECT @@1` and silently
reparses as `UnaryOperator::DoubleAt` applied to `1`.
Extend the existing "needs a space" condition so it also fires when the
operand is another `Expr::UnaryOp`. Operators already in that list and
non-unary operands are unaffected, so `-1` and `NOT a` are unchanged.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Expr::UnaryOp'sDisplaywrites{op}{expr}with no separator, so a nested unary operand is printed glued to the operator:prints as
SELECT ~~1, which no longer reparses on any dialect (Expected: an expression, found: ~~).SELECT - -1prints asSELECT --1the same way; that one only breaks where--opens a line comment, so MySQL is unaffected.On PostgreSQL it changes meaning rather than failing, because
@@is its own operator:SELECT @ @ 1(abs of abs) prints asSELECT @@1, which reparses asUnaryOperator::DoubleAtapplied to1.The fix extends the existing "needs a space" condition so it also fires when the operand is another
Expr::UnaryOp. Non-unary operands are unchanged, so-1andNOT aprint as before.Tests are
parse_nested_unary_opsintests/sqlparser_common.rsandparse_nested_pg_unary_opsintests/sqlparser_postgres.rs; I checked that both fail without the source change. TheAGENTS.mdpre-commit checks are clean locally.