Skip to content

Render prefix ~ operator with a space - #2533

Merged
LucaCappelletti94 merged 1 commit into
mainfrom
postgres-pgbitnot-display-space
Sep 21, 2026
Merged

LucaCappelletti94 merged 1 commit into
mainfrom
postgres-pgbitnot-display-space

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

Fuzz round-trip found SELECT ~ -1 rendering as SELECT ~-1, which PostgreSQL retokenizes as the single ~- custom operator and fails to re-parse. SELECT ~ ~1 renders SELECT ~~1 and fails in every dialect, ~ @2 renders ~@2 and ~ #x renders ~# x in PostgreSQL.

Expr::UnaryOp display concatenates operator and operand in its final arm. BitwiseNot joins the spaced arm beside Not and Hash, the same remedy #2531 applies to @. Canonical output for SELECT ~expr becomes SELECT ~ expr, pinned with one_statement_parses_to in parse_generic_unary_ops.

@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.

Comment thread src/ast/mod.rs Outdated
@@ -1961,6 +1961,7 @@ impl fmt::Display for Expr {
} else if matches!(

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.

As a stylistic suggestion here to avoid issues in the future, I suggest changing the structure of this code to a sigle match so we can explicitly see which operators are handled in what form

like

match op {
  UnaryOperator::PGPostfixFactorial =>   write!(f, "{expr}{op}"),
  UnaryOperator::Not
                        | UnaryOperator::BitwiseNot
                        | UnaryOperator::Hash
                        | UnaryOperator::AtDashAt
                        | UnaryOperator::DoubleAt
                        | UnaryOperator::QuestionDash
                        | UnaryOperator::QuestionPipe =>  write!(f, "{op} {expr}"
  // TODO explcitly list out the other operators here so the list is explicitl
}

Also when I was typing that it seems like UnaryOperator::PGPostfixFactorial is the same as the else match 🤔

@LucaCappelletti94
LucaCappelletti94 force-pushed the postgres-pgbitnot-display-space branch from 199e401 to a2345b2 Compare September 21, 2026 13:21
Comment thread src/ast/mod.rs
| UnaryOperator::PGAbs
| UnaryOperator::QuestionDash
| UnaryOperator::QuestionPipe => write!(f, "{op} {expr}"),
UnaryOperator::Plus

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.

❤️

@alamb
alamb added this pull request to the merge queue Sep 21, 2026
@alamb

alamb commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Nice -- thank you @LucaCappelletti94

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Sep 21, 2026
@LucaCappelletti94
LucaCappelletti94 added this pull request to the merge queue Sep 21, 2026
Merged via the queue into main with commit 0718147 Sep 21, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants