Skip to content

fix(grammar): parse || below + and -, like sqlglot and PostgreSQL - #749

Open
j0hannesz wants to merge 1 commit into
apache:mainfrom
j0hannesz:fix/grammar-concat-precedence
Open

j0hannesz wants to merge 1 commit into
apache:mainfrom
j0hannesz:fix/grammar-concat-precedence

Conversation

@j0hannesz

Copy link
Copy Markdown
Contributor

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

Summary

Related Issues

Checklist

Specification

  • Spec changes are included in core-spec/ and follow the existing structure
  • Spec changes have a [DISCUSS] thread on dev@ with a stated end date, and reach lazy consensus (or pass a [VOTE]) before merge
  • Breaking changes to the spec are clearly called out in the summary
  • For a material spec change, docs/spec_changes.md records rationale, compatibility and migration impact, or the PR explains why no entry is needed

Ontology

  • Ontology changes in ontology/ are consistent with spec changes
  • New or modified terms are defined and documented

Converters

Validation

  • Validation rules in validation/ are updated if the spec changed
  • New validation cases are covered by tests

Documentation

  • docs/ is updated to reflect any user-facing changes
  • New features or behaviors are documented with examples where appropriate
  • CONTRIBUTING.md is updated if the contribution process changed

Examples

  • examples/ are added or updated for any new spec constructs

Tests

  • All existing tests pass (pytest / CI green)
  • New functionality is covered by tests

Compliance

  • ASF license headers are present on all new source files
  • New third-party dependencies follow Third-Party Dependencies
  • If third-party source code is vendored/copied (not just declared as a dependency), NOTICE and/or LICENSE have been updated per ASF policy

`||` 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.
@ashlearnnext-star

Copy link
Copy Markdown
Contributor

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

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 + c as a || (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 + c parses as (a || b) + c. After: a || (b + c), a || b = c as (a || b) = c, a || b * c as a || (b * c), -a || b as (-a) || b, and a || b || c left-associative. Both versions accept all 68 lines of valid.txt and reject all 15 lines of invalid.txt.

Two small points:

  1. The README bullet this PR rewrites still ends with "a || b only appears as a dialect form", while expression_language.md lists || under String Manipulation as REQUIRED. See the inline comment.

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

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants