diff --git a/.editorconfig b/.editorconfig index 9f04669ec..c5039ac16 100644 --- a/.editorconfig +++ b/.editorconfig @@ -32,4 +32,4 @@ indent_style = space indent_size = 4 [*.g4] -indent_style = space \ No newline at end of file +indent_style = space diff --git a/.github/workflows/grammar-ci.yml b/.github/workflows/grammar-ci.yml index 234e10adc..6d5efc0b6 100644 --- a/.github/workflows/grammar-ci.yml +++ b/.github/workflows/grammar-ci.yml @@ -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" @@ -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" @@ -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" diff --git a/Makefile b/Makefile index 912d5d930..9e5da3a62 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 + @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 diff --git a/compliance/expressions/valid.txt b/compliance/expressions/valid.txt index 598a8ec57..b707d4ce7 100644 --- a/compliance/expressions/valid.txt +++ b/compliance/expressions/valid.txt @@ -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 @@ -88,6 +89,7 @@ dataset.field schema.dataset."Field" date first +SUM(order.sales) / COUNT(DISTINCT order.customer_id) a -- trailing comment a + 1 /* inline */ + 2 diff --git a/core-spec/grammar/OssieExpression.g4 b/core-spec/grammar/OssieExpression.g4 index 1084b7901..291d3ea13 100644 --- a/core-spec/grammar/OssieExpression.g4 +++ b/core-spec/grammar/OssieExpression.g4 @@ -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 @@ -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 @@ -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 diff --git a/core-spec/grammar/README.md b/core-spec/grammar/README.md index 2aee50114..c28e00cc9 100644 --- a/core-spec/grammar/README.md +++ b/core-spec/grammar/README.md @@ -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 @@ -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)). diff --git a/core-spec/grammar/ruff.toml b/core-spec/grammar/ruff.toml deleted file mode 100644 index a04b3c710..000000000 --- a/core-spec/grammar/ruff.toml +++ /dev/null @@ -1,26 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Ruff settings for grammar/ only; the repository has no shared ruff configuration yet. -line-length = 100 -target-version = "py311" - -# Parser files generated by ANTLR (if someone generates them into this folder) are not linted. -extend-exclude = ["OssieExpression*.py", "gen", "generated"] - -[lint] -select = ["E", "F", "I", "B", "UP"] diff --git a/core-spec/grammar/tests/conftest.py b/core-spec/grammar/tests/conftest.py index b74ef955e..7852d72f1 100644 --- a/core-spec/grammar/tests/conftest.py +++ b/core-spec/grammar/tests/conftest.py @@ -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" diff --git a/core-spec/grammar/tests/test_expression_grammar.py b/core-spec/grammar/tests/test_expression_grammar.py index 8f04517f9..3f37a13aa 100644 --- a/core-spec/grammar/tests/test_expression_grammar.py +++ b/core-spec/grammar/tests/test_expression_grammar.py @@ -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"} @@ -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" @@ -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): + 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" diff --git a/ruff.toml b/ruff.toml index 30ee39474..4436fb6ab 100644 --- a/ruff.toml +++ b/ruff.toml @@ -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