Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compliance/expressions/valid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ ROUND(x, 2)

# Operators and predicates
first_name || ' ' || last_name
'Total: ' || qty * price || ' EUR'
a || b + c - d || e
Comment on lines +64 to +65

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these expressions already parse cleanly on main under the old # additiveValue rule, so adding them to valid.txt doesn't guard against a precedence regression.

Could we add an AST-shape test in core-spec/grammar/tests/test_expression_grammar.py (mirroring test_multiplication_binds_tighter_than_addition) to pin that + binds tighter than || (and optionally that || remains left-associative)?

For example:

  def test_addition_binds_tighter_than_concat(ossie):
     value = top(ossie, "a || b + c")
     assert kind(value) == "ValueOnlyExpr"
     concat = value.valueExpr()
     assert kind(concat) == "ConcatValue"
     assert kind(concat.valueExpr(1)) == "AdditiveValue"

x % 2
-x + +y * (z - 1) / 2
a <> b AND a != b AND a <= b AND a >= b
Expand Down
7 changes: 4 additions & 3 deletions core-spec/grammar/OssieExpression.g4
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ options { caseInsensitive = true; }
//
// Operands of comparisons and predicates are arithmetic expressions: `a = b = c` needs parentheses.
//
// Precedence follows the list in the spec. The spec does not say where `||` sits: it is parsed at
// the level of + and -.
// Precedence follows the list in the spec. The spec does not say where `||` sits: it is parsed at a
// level of its own below + and -, as in sqlglot and PostgreSQL: `a || b + c` is `a || (b + c)`.

expression
: expr EOF
Expand All @@ -58,7 +58,8 @@ valueExpr
: primary # primaryValue
| op=(PLUS | MINUS) valueExpr # unaryValue
| valueExpr op=(STAR | SLASH | PERCENT) valueExpr # multiplicativeValue
| valueExpr op=(PLUS | MINUS | CONCAT) valueExpr # additiveValue
| valueExpr op=(PLUS | MINUS) valueExpr # additiveValue
| valueExpr CONCAT valueExpr # concatValue
;

primary
Expand Down
3 changes: 2 additions & 1 deletion core-spec/grammar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ generated parser is not committed.
The precedence levels follow the list in the spec's "Operator Precedence" section. Two points
are not decided there:

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm echoing @MrBlack1995 comment here: keeping The spec's portable form is CONCAT(a, b); a || b only appears as a dialect form. contradicts core-spec/expression_language.md, which lists both CONCAT and || under String Manipulation (REQUIRED).

I suggest updating this bullet to something like:

  - `||` is parsed at a level of its own below `+` and `-` (above comparisons), matching `sqlglot`
    and PostgreSQL: `a || b + c` is `a || (b + c)`. The spec lists `||` as a `REQUIRED`
    concatenation operator alongside `CONCAT(a, b)`, but does not state its precedence level.

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.

- Comparisons do not chain: `a = b = c` needs parentheses.

Expand Down
Loading