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: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ indent_style = space
indent_size = 4

[*.g4]
indent_style = space
indent_style = space
4 changes: 3 additions & 1 deletion .github/workflows/grammar-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ on:
paths:
- "core-spec/grammar/**"
- "compliance/expressions/**"
- "compliance/**/*.ossie.yaml"
- "core-spec/expression_language.md"
- "examples/**"
- ".github/workflows/grammar-ci.yml"
Expand All @@ -32,6 +33,7 @@ on:
paths:
- "core-spec/grammar/**"
- "compliance/expressions/**"
- "compliance/**/*.ossie.yaml"
- "core-spec/expression_language.md"
- "examples/**"
- ".github/workflows/grammar-ci.yml"
Expand All @@ -57,7 +59,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install uv
uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
version: "0.12.19"

Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ setup: python-setup ## Set up the local development environment
clean: python-clean ## Remove caches, build artifacts, and virtual environments
@echo "--- Removing root caches, virtual environment, and Python bytecode ---"
@rm -rf .coverage .hypothesis .mypy_cache .pytest_cache .ruff_cache .venv
@find python validation scripts compliance -type d -name "__pycache__" -prune -exec rm -rf {} +
@find python validation scripts compliance core-spec/grammar -type d -name "__pycache__" -prune -exec rm -rf {} +
@echo "--- Cleanup complete ---"

.PHONY: test
Expand Down Expand Up @@ -134,6 +134,13 @@ spec-generate: check-dependencies ## Regenerate spec.yaml enums from ossie-schem
@$(UV) run --no-project scripts/generate-spec-types.py --apply
@echo "--- Generated spec types updated ---"

.PHONY: test-grammar
test-grammar: DEPENDENCIES := $(UV) java
test-grammar: check-dependencies ## Run ANTLR4 expression grammar tests

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: make test doesn't run this target, which makes sense since java is optional. Could the help text say "(needs java; not part of make test)" so people don't assume it's covered?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

@echo "--- Running expression grammar tests ---"
@$(UV) run --no-project --with pytest --with pyyaml --with antlr4-python3-runtime==4.13.2 -m pytest core-spec/grammar/tests/
@echo "--- Expression grammar tests complete ---"

##@ Compliance

.PHONY: compliance
Expand Down
2 changes: 2 additions & 0 deletions compliance/expressions/valid.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ DATEADD(month, -1, order_date)
PI()
COALESCE(a, b, c)
ROUND(x, 2)
EXISTS_IN()

# Operators and predicates
first_name || ' ' || last_name
Expand Down Expand Up @@ -88,6 +89,7 @@ dataset.field
schema.dataset."Field"
date
first
SUM(order.sales) / COUNT(DISTINCT order.customer_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: this only covers order at the top level. Could we also add SUM(x) OVER (ORDER BY order), so we know the identifier still works right after the ORDER BY keyword?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

a -- trailing comment
a + 1 /* inline */ + 2

Expand Down
9 changes: 5 additions & 4 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
// below + and -, matching sqlglot and PostgreSQL.

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 Expand Up @@ -90,7 +91,7 @@ identifier
nonReservedWord
: DATE | TIME | TIMESTAMP | TIMESTAMP_NTZ
| FIRST | LAST | NULLS | ROW | ROWS | RANGE | UNBOUNDED | PRECEDING | FOLLOWING | CURRENT
| FILTER | OVER | WITHIN | GROUP | PARTITION | ASC | DESC | POSITION
| FILTER | OVER | WITHIN | GROUP | PARTITION | ORDER | ASC | DESC | POSITION
;

literal
Expand Down
12 changes: 7 additions & 5 deletions core-spec/grammar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ 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)`;
`a || b` only appears as a dialect form.
- `||` is parsed below `+` and `-` (so `a || b + c` is `a || (b + c)`), matching `sqlglot` and
PostgreSQL. The spec lists `||` as a `REQUIRED` concatenation operator but does not state its
precedence level.
- Comparisons do not chain: `a = b = c` needs parentheses.

## Tests
Expand All @@ -53,8 +54,9 @@ uv run --with pytest --with pyyaml --with antlr4-python3-runtime==4.13.2 \

The conformance fixtures are in [`compliance/expressions/`](../../compliance/expressions/), so any
parser can run them. They hold one expression per line (`#` starts a comment):
`valid.txt` must parse, `invalid.txt` must be rejected. `test_example_models_parse` also
parses every `ANSI_SQL` / `OSSIE_SQL_2026` expression and top-level `sql` in `examples/*.yaml`.
`valid.txt` must parse, `invalid.txt` must be rejected. `test_example_models_parse` and
`test_compliance_models_parse` also parse every `ANSI_SQL` / `OSSIE_SQL_2026` expression and
`sql` field in `examples/*.yaml` and `compliance/**/*.ossie.yaml`.

Python style is checked with `uv run --with ruff ruff check core-spec/grammar/ && uv run --with ruff ruff format --check core-spec/grammar/`
(settings in `ruff.toml`).
(settings in the repository root [`ruff.toml`](../../ruff.toml)).
26 changes: 0 additions & 26 deletions core-spec/grammar/ruff.toml

This file was deleted.

5 changes: 1 addition & 4 deletions core-spec/grammar/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
ANTLR_VERSION = "4.13.2"
# SHA-256 of antlr4-4.13.2-complete.jar; its SHA-1 matches the .sha1 published on Maven Central.
ANTLR_SHA256 = "eae2dfa119a64327444672aff63e9ec35a20180dc5b8090b7a6ab85125df4d76"
ANTLR_URL = (
"https://repo1.maven.org/maven2/org/antlr/antlr4/"
f"{ANTLR_VERSION}/antlr4-{ANTLR_VERSION}-complete.jar"
)
ANTLR_URL = f"https://repo1.maven.org/maven2/org/antlr/antlr4/{ANTLR_VERSION}/antlr4-{ANTLR_VERSION}-complete.jar"

REPO = Path(__file__).resolve().parents[3]
JAR = REPO / "dev" / "lib" / f"antlr4-{ANTLR_VERSION}-complete.jar"
Expand Down
38 changes: 38 additions & 0 deletions core-spec/grammar/tests/test_expression_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

REPO = Path(__file__).resolve().parents[3]
FIXTURES = REPO / "compliance" / "expressions"
COMPLIANCE = REPO / "compliance"
EXAMPLES = REPO / "examples"
PORTABLE_DIALECTS = {"ANSI_SQL", "OSSIE_SQL_2026"}

Expand Down Expand Up @@ -100,10 +101,39 @@ def test_example_models_parse(ossie):
assert not failures


def compliance_expressions():
for path in sorted(COMPLIANCE.glob("**/*.ossie.yaml")):
rel = path.relative_to(REPO)
for text in portable_expressions(yaml.safe_load(path.read_text(encoding="utf-8"))):
yield f"{rel}: {text}", text


def test_compliance_models_parse(ossie):
"""Every portable expression in compliance/**/*.ossie.yaml must parse."""
found = list(compliance_expressions())
assert found, "no expressions found in compliance/**/*.ossie.yaml"
failures = {label: e for label, text in found if (e := parse(ossie, text)[1])}
assert not failures


def kind(ctx):
return type(ctx).__name__.removesuffix("Context")


def test_unary_binds_tighter_than_multiplication_and_addition(ossie):
mul_val = top(ossie, "-a * b")
assert kind(mul_val) == "ValueOnlyExpr"
mul = mul_val.valueExpr()
assert kind(mul) == "MultiplicativeValue"
assert kind(mul.valueExpr(0)) == "UnaryValue"

add_val = top(ossie, "-a + b")
assert kind(add_val) == "ValueOnlyExpr"
add = add_val.valueExpr()
assert kind(add) == "AdditiveValue"
assert kind(add.valueExpr(0)) == "UnaryValue"


def test_multiplication_binds_tighter_than_addition(ossie):
value = top(ossie, "1 + 2 * 3")
assert kind(value) == "ValueOnlyExpr"
Expand All @@ -112,6 +142,14 @@ def test_multiplication_binds_tighter_than_addition(ossie):
assert kind(add.valueExpr(1)) == "MultiplicativeValue"


def test_addition_binds_tighter_than_concat(ossie):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: could we add the mirror case a + b || c here too, asserting ConcatValue with an AdditiveValue on the left? Then both sides of the new level are pinned.

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

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


def test_not_binds_looser_than_comparison(ossie):
not_expr = top(ossie, "NOT a = b")
assert kind(not_expr) == "NotExpr"
Expand Down
7 changes: 6 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
# specific language governing permissions and limitations
# under the License.

src = ["python/src", "validation", "scripts", "compliance"]
src = ["python/src", "validation", "scripts", "compliance", "core-spec/grammar"]
extend-exclude = [
"core-spec/grammar/**/OssieExpression*.py",
"core-spec/grammar/**/gen",
"core-spec/grammar/**/generated",
]

line-length = 130

Expand Down
Loading