Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ indent_size = 2
[*.{py,java,toml}]
indent_style = space
indent_size = 4

[*.g4]
indent_style = space
6 changes: 6 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ docs:
- changed-files:
- any-glob-to-any-file: 'docs/**'

grammar:
- changed-files:
- any-glob-to-any-file:
- 'core-spec/grammar/**'
- 'compliance/expressions/**'

validation:
- changed-files:
- any-glob-to-any-file: 'validation/**'
Expand Down
68 changes: 68 additions & 0 deletions .github/workflows/grammar-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# 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.

name: Grammar CI

on:
push:
branches: ["main"]
paths:
- "core-spec/grammar/**"
- "compliance/expressions/**"
- "core-spec/expression_language.md"
- "examples/**"
- ".github/workflows/grammar-ci.yml"
pull_request:
branches: ["main"]
paths:
- "core-spec/grammar/**"
- "compliance/expressions/**"
- "core-spec/expression_language.md"
- "examples/**"
- ".github/workflows/grammar-ci.yml"

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.14"]

steps:
- name: Checkout project
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: ${{ matrix.python-version }}

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

# The tests download the ANTLR 4.13.2 jar from Maven Central into dev/lib/ and verify its SHA-256.
- name: Run grammar tests
run: >
uv run --with pytest --with pyyaml --with antlr4-python3-runtime==4.13.2
-m pytest core-spec/grammar/tests/
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@

# Apache RAT (downloaded by dev/check-license)
dev/lib/

# ANTLR generated output (core-spec/grammar/)
core-spec/grammar/**/gen/
core-spec/grammar/**/OssieExpression*.py
core-spec/grammar/**/*.java
core-spec/grammar/**/*.interp
core-spec/grammar/**/*.tokens
40 changes: 40 additions & 0 deletions compliance/expressions/invalid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.

# Conformance fixture for the OSSIE_SQL_2026 expression language: every line must be rejected by a
# parser for it (valid.txt: every line must be accepted). One expression per line; blank lines
# and lines starting with '#' are skipped.

1 +
SUM(
(a
a b
CASE WHEN a THEN b
x BETWEEN 1
x IN ()
SELECT a FROM t
a IS
'unterminated
CAST(x AS)

# INTERVAL is not part of OSSIE_SQL_2026, date arithmetic uses DATEADD(part, amount, date_expr)
order_date + INTERVAL '7 days'
DATE_ADD(d, INTERVAL 7 DAY)

# DISTINCT needs an argument
SUM(DISTINCT)
COUNT(DISTINCT)
105 changes: 105 additions & 0 deletions compliance/expressions/valid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 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.

# Conformance fixture for the OSSIE_SQL_2026 expression language: every line must be accepted by a
# parser for it (invalid.txt: every line must be rejected). One expression per line; blank lines
# and lines starting with '#' are skipped. The reference parser is core-spec/grammar/.
# Each line exercises a different part of the grammar. Function names are not checked by the
# grammar, so one generic call stands for all of them. Expressions of examples/*.yaml are parsed
# by test_example_models_parse and are not repeated here.

# Aggregates: COUNT(*), DISTINCT, WITHIN GROUP, FILTER
SUM(amount)
COUNT(*)
COUNT(DISTINCT customer_id)
SUM(DISTINCT amount)
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY x)
PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY x DESC NULLS LAST)
SUM(amount) FILTER (WHERE status = 'completed')
COUNT(*) FILTER (WHERE status = 'completed')
COUNT(DISTINCT customer_id) FILTER (WHERE status = 'completed')

# CASE, CAST, EXTRACT, POSITION
SUM(CASE WHEN status = 'completed' THEN amount END)
CASE WHEN a > 1 THEN 'x' WHEN a > 0 THEN 'y' ELSE 'z' END
CASE expression WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'many' END
CAST('2024-01-15' AS DATE)
CAST(x AS DECIMAL(10, 2))
TRY_CAST(x AS INTEGER)
EXTRACT(YEAR FROM date_expr)
POSITION('a' IN str)

# Date and time: current datetime, typed literals, date arithmetic
CURRENT_DATE
CURRENT_DATE()
CURRENT_TIMESTAMP
CURRENT_TIME()
DATE '2024-01-15'
TIMESTAMP_NTZ '2024-01-15 10:30:00'
TIME '10:30:00'
DATEADD(day, 7, order_date)
DATEADD(month, -1, order_date)

# Generic function calls
PI()
COALESCE(a, b, c)
ROUND(x, 2)

# Operators and predicates
first_name || ' ' || last_name
x % 2
-x + +y * (z - 1) / 2
a <> b AND a != b AND a <= b AND a >= b
str LIKE 'a%'
str NOT LIKE 'a%'
str ILIKE '%b_'
x BETWEEN 1 AND 10 AND y NOT BETWEEN a AND b
x IN (1, 2, 3)
x NOT IN ('a', 'b')
x IS NULL
x IS NOT NULL
a IS DISTINCT FROM b
a IS NOT DISTINCT FROM b
NOT expr
NOT a = b
a AND b OR NOT c
TRUE AND NOT FALSE

# Literals, identifiers, comments
1.5e-3 + .5 + 10.
NULL
"Order Date"
"say ""hi"""
dataset.field
schema.dataset."Field"
date
first
a -- trailing comment
a + 1 /* inline */ + 2

# Keywords are case-insensitive
sum(amount) filter (where status = 'completed')
case when a then b else c end

# Window functions: PARTITION BY, ORDER BY, NULLS, frames
ROW_NUMBER() OVER (ORDER BY id)
RANK() OVER (ORDER BY revenue DESC NULLS LAST)
NTILE(4) OVER (PARTITION BY region ORDER BY revenue)
SUM(amount) OVER (PARTITION BY region)
AVG(amount) OVER (ORDER BY order_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)
SUM(amount) OVER (ORDER BY d RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
amount / SUM(amount) OVER () * 100
5 changes: 5 additions & 0 deletions core-spec/expression_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,11 @@ Implementations MAY support additional functions through dialect extensions. The

---

## ANTLR4 Grammar

A machine-readable ANTLR 4 grammar for this language is available in [`grammar/`](grammar/). It is a
tooling aid; where it and this document differ, this document is authoritative.

## Version History

| Version | Date | Changes |
Expand Down
Loading
Loading