Skip to content

parser: arithmetic on the right-hand side of a comparison fails (WHERE a = 1 + 2) #596

Closed

Description

@ajitpratap0

Summary

An arithmetic expression on the right-hand side of a comparison fails to parse. The left-hand side and the SELECT list are fine.

Reproduced on main (c0bb385) and v1.14.0 with gosqlx.Parse:

SQL Result
SELECT * FROM t WHERE a = 1 + 2 E2002 ... column 29: expected statement, got PLUS
SELECT * FROM t WHERE a = b * 2 expected statement, got MUL
SELECT * FROM t WHERE a > b - 1 expected statement, got MINUS
SELECT * FROM t WHERE (a = 1 + 2) expected ), got PLUS
SELECT * FROM t WHERE a + 1 = 2 parses
SELECT a + 1 FROM t parses
SELECT * FROM t WHERE a = (1 + 2) parses

It also breaks placeholder arithmetic such as a = :1 + :2, a = $1 + 1 and a = ? + 1.

Likely cause

The comparison operand on the right is parsed at a precedence level that stops before additive/multiplicative operators, and the statement then ends at the +/*/- token. The fix belongs in the binary-operator precedence chain in pkg/sql/parser (comparison should parse its right operand with the additive level, not the primary level).

Expected

All of the above parse, with a = (1 + 2) precedence (arithmetic binds tighter than comparison), across all dialects; SQL() round-trips.

Found during the pre-merge review of #595 (not caused by it). Tracked from #566.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions