Repository navigation
Conversation
`||` shared a level with `+` and `-`, so `a || b + c` read as `(a || b) + c`. sqlglot and PostgreSQL read it as `a || (b + c)`, which also type-checks when b and c are numeric. Give `||` its own left-associative level between additive and comparison. Update the grammar comment and README, and add mixed `||` / arithmetic cases to compliance/expressions/valid.txt.
|
Thanks for picking this up. The precedence change looks right to me: sqlglot parses a || b + c as a || (b + c), and DuckDB returns x3 for SELECT 'x' || 1 + 2. Heads-up that #719 makes the same grammar change. It also adds a parse-tree test (test_addition_binds_tighter_than_concat) and updates the README line that calls || a dialect-only form. One small thing: the two new lines in valid.txt also pass on main, since the old rule already accepts them. A tree-shape test like the one in #719 would cover the new precedence. Minor and non-blocking: Oracle keeps || on the same level as +/-, so mixing them without parentheses can behave differently there. Would it make sense to combine this with #719? That one has conflicts at the moment, so this PR could also be the one to land if it picks up the test. |
MrBlack1995
left a comment
There was a problem hiding this comment.
Thanks for this. The precedence now matches sqlglot and PostgreSQL, so the grammar and the validator read a || b + c the same way. This also closes the question from #657.
I checked it independently:
- sqlglot 30.31.0 reads
a || b + casa || (b + c)under its default dialect, which is the one validate.py uses, and under postgres, snowflake, bigquery, databricks, trino, clickhouse and redshift. - PostgreSQL's precedence table puts "any other operator", which covers
||, below binary+and-and above BETWEEN, IN, LIKE and the comparisons. That is where the grammar now puts it. - I built the grammar before and after the change with ANTLR 4.11.1 and a small Java harness. Before:
a || b + cparses as(a || b) + c. After:a || (b + c),a || b = cas(a || b) = c,a || b * casa || (b * c),-a || bas(-a) || b, anda || b || cleft-associative. Both versions accept all 68 lines of valid.txt and reject all 15 lines of invalid.txt.
Two small points:
-
The README bullet this PR rewrites still ends with "
a || bonly appears as a dialect form", while expression_language.md lists||under String Manipulation as REQUIRED. See the inline comment. -
The spec's own precedence list ("Operator Precedence" in expression_language.md) has no row for
||. The grammar now records a choice, which is reasonable for a reference grammar. If it should be the portable rule, it needs a row in that list and an entry in docs/spec_changes.md. That can be a separate change.
Not run: the Python grammar test suite (no antlr4 Python runtime here) and the pinned ANTLR 4.13.2. The build with 4.11.1 ran with -Werror and gave no warnings.
| - `||` is accepted, at the level of `+` and `-`. The spec's portable form is `CONCAT(a, b)`; | ||
| - `||` is accepted, at a level of its own below `+` and `-` (above comparisons), like sqlglot and | ||
| PostgreSQL: `a || b + c` is `a || (b + c)`. The spec's portable form is `CONCAT(a, b)`; | ||
| `a || b` only appears as a dialect form. |
There was a problem hiding this comment.
This clause conflicts with expression_language.md, which lists || as a REQUIRED string operator (String Manipulation table). Suggest dropping it, or saying that || is a required operator and CONCAT(a, b) is the other spelling.
||shared a level with+and-, soa || b + cread as(a || b) + c. sqlglot and PostgreSQL read it asa || (b + c), which also type-checks when b and c are numeric.Give
||its own left-associative level between additive and comparison. Update the grammar comment and README, and add mixed||/ arithmetic cases to compliance/expressions/valid.txt.Summary
Related Issues
Checklist
Specification
core-spec/and follow the existing structure[DISCUSS]thread ondev@with a stated end date, and reach lazy consensus (or pass a[VOTE]) before mergedocs/spec_changes.mdrecords rationale, compatibility and migration impact, or the PR explains why no entry is neededOntology
ontology/are consistent with spec changesConverters
Validation
validation/are updated if the spec changedDocumentation
docs/is updated to reflect any user-facing changesCONTRIBUTING.mdis updated if the contribution process changedExamples
examples/are added or updated for any new spec constructsTests
pytest/ CI green)Compliance
NOTICEand/orLICENSEhave been updated per ASF policy