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
7 changes: 4 additions & 3 deletions compliance/portability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ grades the real fixtures all-PORTABLE. See `grader/README.md`.
`OSSIE_SQL_2026`. A vendor-tagged input would only exercise one converter,
which defeats a cross-converter measurement.
- **Qualified reference form is primary.** Where a metric references a dataset
field, the qualified `dataset.field` form is the primary fixture and the bare
form is a sibling variant, pending the name-resolution decisions in #486 and
#343.
field, the qualified `dataset.field` form is the primary fixture. The bare form
is a sibling variant carried as a dataset-scoped metric (`datasets[].metrics`),
the only place a metric may name a field without its dataset (Metric Scoping,
#486).
- **Conformance is a floor, not the measurement.** Every fixture passes
`validation/validate.py` on current `main`. Fidelity is what the grader
measures on top of that.
Expand Down
6 changes: 6 additions & 0 deletions compliance/portability/grader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ PLACEHOLDER, ADDED, RELABELED, DROPPED_DIAGNOSED, DROPPED_SILENT, REJECTED.
dataset that declares it. A requalify that names the same dataset is cosmetic;
one that names another dataset (`customers.amount` for `sales.amount`) needs
review.
- **A date part is not a column.** `DATEDIFF(part, start, end)` and
`DATEADD(part, amount, date)` are read part first, as the spec writes them,
before qualifiers are resolved; sqlglot's default parser would read the part as a
column. `DATEDIFF(DAY, order_date, ship_date)` for
`DATEDIFF(day, sales.order_date, sales.ship_date)` is cosmetic; a part written
as a column (`DATEDIFF(sales.day, ...)`) needs review.
- **Textual by default, semantic where it can be.** PORTABLE means the converter
returned the same expression it was given (modulo cosmetic whitespace). When
the input has an `OSSIE_SQL_2026` form (`sql`, or the dialect entry when there
Expand Down
41 changes: 40 additions & 1 deletion compliance/portability/grader/grade.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,43 @@ def datasets(document):
return aliases


def _bare_date_part(node):
"""The part a bare first argument names (``day`` or ``'day'`` as ``DAY``), or None."""
from sqlglot import exp

if isinstance(node, exp.Column) and not node.table:
return node.name.upper()
if isinstance(node, exp.Literal) and node.is_string:
return node.name.upper()
return None


def _read_date_parts(tree):
"""Read DATEDIFF(part, start, end) and DATEADD(part, amount, date) part first, as
the spec writes them (Date Arithmetic in ``expression_language.md``), so the part
is never compared as a column.

sqlglot's default parser does not know these parts. It keeps DATEDIFF's part as a
column in ``this`` and reads the end date into ``unit`` (an upper-cased Var when
bare), and it leaves DATEADD anonymous with the part as its first column. A bare
first argument is the part, so ``DATEDIFF(DAY, order_date, ship_date)`` matches
``DATEDIFF(day, sales.order_date, sales.ship_date)``. A qualified one is a column:
``DATEDIFF(sales.day, ...)`` reads a column named day, not the part."""
from sqlglot import exp

for node in list(tree.find_all(exp.DateDiff)):
part, end = _bare_date_part(node.this), node.args.get("unit")
if part is None or end is None:
continue
node.set("this", exp.column(end.name) if isinstance(end, exp.Var) else end)
node.set("unit", exp.var(part))
for node in list(tree.find_all(exp.Anonymous)):
if node.name.upper() == "DATEADD" and node.expressions:
part = _bare_date_part(node.expressions[0])
if part is not None:
node.expressions[0].replace(exp.var(part))


def _equivalent_tree(left, right, owners=None, home=None, aliases=None):
"""Return True if two SQL expressions are equal after resolving dataset
qualifiers and dropping quoting, False if they differ, or None if it cannot be
Expand All @@ -167,7 +204,8 @@ def _equivalent_tree(left, right, owners=None, home=None, aliases=None):
``amount``. A name no dataset declares, and every name without ``owners``, is
compared without its qualifier, since there is nothing to check it against.
``aliases`` (from _dataset_aliases) maps a renamed dataset's qualifiers on the
returned side, ``right``, to the authored dataset before resolving."""
returned side, ``right``, to the authored dataset before resolving. The part of
DATEDIFF and DATEADD is read first (_read_date_parts), so it is not a column."""
try:
import sqlglot
from sqlglot import exp
Expand All @@ -193,6 +231,7 @@ def canonical(text, renamed=None):
tree = sqlglot.parse_one(text)
except Exception:
return None
_read_date_parts(tree)
for column in tree.find_all(exp.Column):
if renamed and column.table and column.table.lower() in renamed:
column.set("table", exp.to_identifier(renamed[column.table.lower()]))
Expand Down
48 changes: 48 additions & 0 deletions compliance/portability/grader/test_grade.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,51 @@ def test_an_authored_dataset_name_is_never_an_alias():
])
returned = _doc(datasets=[{"name": "customers", "source": "samples.sales.orders"}])
assert grade._dataset_aliases(authored, returned) == {}


# The spec writes DATEDIFF and DATEADD part first. These returns are the real ones for
# M6.date_scalars from dbt and cube (Interop matrix on apache/ossie-converters 625712b
# with the M6 fixture, 2026-11-18).
def _m6_metric_finding(returned_expression):
path = os.path.join(HERE, "m6", "m6_date_scalars.ossie.yaml")
authored, returned = grade._load(path), grade._load(path)
returned["metrics"][0]["expression"]["dialects"][0]["expression"] = returned_expression
report = grade.grade(authored, returned)
return next(f for f in report.findings if f.name == "avg_fulfillment_days")


def test_a_respelled_datediff_part_is_cosmetic():
# dbt upper-cases the part and drops the qualifiers.
finding = _m6_metric_finding("AVG(DATEDIFF(DAY, order_date, ship_date))")
assert finding.outcome == grade.REWRITTEN
assert finding.detail.startswith("cosmetic"), finding.detail


def test_a_datediff_part_written_as_a_column_needs_review():
# cube qualifies the part, which reads a column named day that sales does not have.
finding = _m6_metric_finding("AVG(DATEDIFF(sales.day, sales.order_date, sales.ship_date))")
assert finding.outcome == grade.REWRITTEN
assert finding.detail.startswith("review"), finding.detail


def test_a_datediff_with_another_part_or_swapped_dates_needs_review():
for returned in (
"AVG(DATEDIFF(month, sales.order_date, sales.ship_date))",
"AVG(DATEDIFF(day, sales.ship_date, sales.order_date))",
):
finding = _m6_metric_finding(returned)
assert finding.outcome == grade.REWRITTEN, returned
assert finding.detail.startswith("review"), (returned, finding.detail)


def test_a_dateadd_part_is_not_a_column_of_the_fields_dataset():
# In a field expression a bare column is the field's own dataset, so a part read
# as a column would make sales.day equal day.
authored = _doc(datasets=[_field("sales", "due_date", "ANSI_SQL", "DATEADD(day, 7, order_date)")])
for returned, note in (
("DATEADD(DAY, 7, sales.order_date)", "cosmetic"),
("DATEADD(sales.day, 7, order_date)", "review"),
):
finding = _only(grade.grade(authored, _doc(datasets=[_field("sales", "due_date", "ANSI_SQL", returned)])))
assert finding.outcome == grade.REWRITTEN, returned
assert finding.detail.startswith(note), (returned, finding.detail)
6 changes: 3 additions & 3 deletions compliance/portability/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

# Portability measurement ladder, rungs M0-M3 (strawman for apache/ossie-converters#24, formerly #576).
# Portability measurement ladder, rungs M0-M6 (strawman for apache/ossie-converters#24, formerly #576).
#
# One capability is added per rung so that a difference in a converter's
# round-trip localizes to a single construct. Every rung is authored in a
Expand All @@ -41,8 +41,8 @@
# dataset.metric. M1's element is an intra-dataset field expression, so it
# carries a single fixture.
#
# All fixtures validate.py-pass on apache/ossie main (9531429) with sqlglot
# 29.0.1. Rungs M7 and M8 (window and offset functions) are held pending #587;
# All fixtures validate.py-pass on apache/ossie main (6160adf) with sqlglot
# 30.33.0. Rungs M7 and M8 (window and offset functions) are held pending #587;
# they are not in this set.

outcomes:
Expand Down
Loading