Skip to content

fix(compliance): read the part of DATEDIFF and DATEADD as a part, not a column - #754

Open
jbonofre wants to merge 2 commits into
apache:mainfrom
jbonofre:fix/grader-date-part
Open

jbonofre wants to merge 2 commits into
apache:mainfrom
jbonofre:fix/grader-date-part

Conversation

@jbonofre

Copy link
Copy Markdown
Member

Follow-up to #631, addressing the non-blocking review comments.

Changes

  • The grader reads the part of DATEDIFF and DATEADD as a part. 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, {CUBE}.order_date, {CUBE}.ship_date), which reads a day column the table doesn't have. It comes back as DATEDIFF(sales.day, ...) and was graded cosmetic, because the grader drops the qualifier of a name no dataset declares.

    The grader now reads a bare first argument (or a string literal) as the part before resolving qualifiers. A qualified first argument stays a column and needs review. validate.py works around the same parser behavior in iter_columns. The grader keeps its own copy because it imports nothing from the repository, and apache/ossie-converters loads it by path.

  • Tests built from dbt's and cube's real M6 returns. Two more cover a different part or swapped dates (both need review), and DATEADD in a field expression, where a part read as a column would resolve to the field's own dataset.

  • Docs:

Testing

  • PYTHONPATH=. pytest test_grade.py: 54 tests pass with sqlglot 30.33.0, and with 30.31.0, the version the converters grade with. Without the fix, 3 of the 4 new tests fail. The fourth (a different part, swapped dates) checks that the change does not make a real difference look cosmetic.
  • I graded real converter output with main's grader and with this one: the 120 portability cases of the latest Interop matrix run on apache/ossie-converters main (44241134765), and 195 cases from a local run of the 14 graded converters on the current M0-M6 fixtures. The only differences are the two M6 findings above: dbt goes from review to cosmetic, and cube from cosmetic to review.
  • validate.py passes all 13 fixtures with no warnings on main (6160adf), with sqlglot 30.33.0.

Related Issues

Refs apache/ossie-converters#24.

… 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.
…e 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 apache#486 and
apache#343, which apache#631 resolved by carrying it as a dataset-scoped metric.

@MrBlack1995 MrBlack1995 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking this one, it is the M6 gap I asked about on #631. Reading the bare first argument as the part before resolving qualifiers is the right place for it, and the dbt and cube returns make good tests.

What I checked here:

  • The 54 grader tests pass on the branch.
  • Since #756 and #757 landed, this conflicts with main in grade.py and test_grade.py. I resolved it locally by keeping main's version and calling _read_date_parts(tree) right after sqlglot.parse_one(text, read=reader) in canonical: all 61 tests pass (main's plus yours), so the part reading works under both NULL-ordering readers from #756.
  • A few cases beyond the tests, on that merged version: 'day' vs day and Day vs DAY are cosmetic; a different part, or DATEADD(day, 7, ...) vs DATEADD(day, 8, ...), needs review; an end date that is an expression (CURRENT_DATE, ship_date + 1) still compares; a qualified first argument (sales.day) stays a column and needs review.

One note for the ladder, not for this PR: the grader can now tell that dbt's DATEDIFF(DAY, ...) is the same text as the authored one, but not that the same text can return different numbers on different engines for month and year. That is the open question in #743, where djwaldo's probe now points to boundary counting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants