Skip to content

Fix ParamRef.Number always 0 for $N bind parameters - #1

Open
notthelewis wants to merge 1 commit into
pgplex:mainfrom
notthelewis:fix-param-ordinal-lexer
Open

notthelewis wants to merge 1 commit into
pgplex:mainfrom
notthelewis:fix-param-ordinal-lexer

Conversation

@notthelewis

Copy link
Copy Markdown

Summary

  • parserLexer.Lex() in parser/parse.go copies lexer token data into the yacc semantic value via a switch on token type, but had no case PARAM:. The lexer (Lexer.lexParam) correctly scans a bind parameter's number into Token.Ival, but without a matching case it fell through to default, which only ever sets lval.str. lval.ival was left at its zero value, so every $N parsed as ParamRef{Number: 0} regardless of its actual position.
  • Adds the missing case PARAM: lval.ival = tok.Ival.

Found while building a downstream tool that translates parsed Postgres SQL into another dialect and needed the actual parameter ordinals preserved.

Test plan

  • Added parser/parsertest/paramref_test.go, covering a query with a repeated parameter ($1) and a second one ($2) across both the target list and a WHERE clause.
  • go test ./... passes locally (all existing packages, including parsertest, pgregress, and tests/grammar).

parserLexer.Lex() copies lexer token data into the yacc semantic
value via a switch on token type, but had no case for PARAM ($1, $2,
...). The lexer (Lexer.lexParam) correctly scans the parameter's
number into Token.Ival, but without a matching case it fell through
to default, which only ever sets lval.str — so lval.ival stayed at
its zero value and every ParamRef parsed with Number 0 regardless of
its actual position in the query.

Adds a case copying tok.Ival into lval.ival, plus a regression test
covering repeated/multiple parameters in both the target list and a
WHERE clause.
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium risk] Fixes bind parameter number parsing in SQL parser.

The PR appears safe to merge; no actionable issue was identified.

Summary

The PR copies bind-parameter ordinals from lexer tokens into parser semantic values and adds a regression test for repeated and distinct parameters.

  • $1 and $2 now retain their numbers in ParamRef.Number.
  • The test checks parameters in a SELECT target list and WHERE clause.

Reviews (1) · Last reviewed commit: "Fix ParamRef.Number always 0 for $N bind..."

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.

1 participant