From 1550ef956580e8c9b0504e7b92ce2c88baaa7b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JB=20Onofr=C3=A9?= Date: Wed, 18 Nov 2026 07:41:57 +0100 Subject: [PATCH 1/2] fix(compliance): read the part of DATEDIFF and DATEADD as a part, not a column sqlglot's default parser does not know the spec's part-first DATEDIFF(part, start, end) and DATEADD(part, amount, date). It reads the part as a column and, for DATEDIFF, the end date as the unit. Two converters' real M6 returns were graded wrong in opposite directions: - dbt returns AVG(DATEDIFF(DAY, order_date, ship_date)), which is the same expression, but it was graded "review: normalized trees differ". - cube exports DATEDIFF({CUBE}.day, ...), which reads a day column the table does not have. It came back as DATEDIFF(sales.day, ...) and was graded cosmetic, because the qualifier of a name no dataset declares is dropped. The grader now reads a bare first argument as the part before resolving qualifiers. A qualified first argument stays a column and needs review. --- compliance/portability/grader/README.md | 6 +++ compliance/portability/grader/grade.py | 41 +++++++++++++++++- compliance/portability/grader/test_grade.py | 48 +++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/compliance/portability/grader/README.md b/compliance/portability/grader/README.md index 90c7fb25..f6d1bb2c 100644 --- a/compliance/portability/grader/README.md +++ b/compliance/portability/grader/README.md @@ -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 diff --git a/compliance/portability/grader/grade.py b/compliance/portability/grader/grade.py index ce0016b4..07b8fe58 100644 --- a/compliance/portability/grader/grade.py +++ b/compliance/portability/grader/grade.py @@ -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 @@ -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 @@ -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()])) diff --git a/compliance/portability/grader/test_grade.py b/compliance/portability/grader/test_grade.py index 30199ea9..efba00fe 100644 --- a/compliance/portability/grader/test_grade.py +++ b/compliance/portability/grader/test_grade.py @@ -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) From 51fd2efa6324238c888e9ba890d40d1cb68608fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JB=20Onofr=C3=A9?= Date: Wed, 18 Nov 2026 07:41:58 +0100 Subject: [PATCH 2/2] docs(compliance): update the portability ladder's rung range, baseline and bare-form rule The manifest header still said rungs M0-M3 and gave the validation baseline from before M4-M6. The README still said the bare form was pending #486 and #343, which #631 resolved by carrying it as a dataset-scoped metric. --- compliance/portability/README.md | 7 ++++--- compliance/portability/manifest.yaml | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compliance/portability/README.md b/compliance/portability/README.md index 3ad9f35c..203e1e53 100644 --- a/compliance/portability/README.md +++ b/compliance/portability/README.md @@ -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. diff --git a/compliance/portability/manifest.yaml b/compliance/portability/manifest.yaml index 10a4c68f..6321cb29 100644 --- a/compliance/portability/manifest.yaml +++ b/compliance/portability/manifest.yaml @@ -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 @@ -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: