Skip to content

fix(validation): name functions as written and require OVER on window functions - #800

Open
jbonofre wants to merge 1 commit into
apache:mainfrom
jbonofre:fix/validation-function-names-and-window-over
Open

jbonofre wants to merge 1 commit into
apache:mainfrom
jbonofre:fix/validation-function-names-and-window-over

Conversation

@jbonofre

Copy link
Copy Markdown
Member

Summary

Follow-up to #792, covering two points from its review.

Functions are named as written. The field aggregate check named a call after sqlglot's class, so the error could name a function that is not in the expression:

Field expression Before After
VAR_POP(x) calls VARIANCE_POP calls VAR_POP
VAR_SAMP(x) calls VARIANCE calls VAR_SAMP
APPROX_COUNT_DISTINCT(x) calls APPROX_DISTINCT calls APPROX_COUNT_DISTINCT
VAR_SAMP(x) + VARIANCE(x) calls VARIANCE calls VAR_SAMP, VARIANCE

VAR_SAMP and VARIANCE parse to the same sqlglot class, so the name is read from the expression text at the call's token position (call.meta). _parse_sql_expression may parse the text as written, as SELECT {text}, or with its first token quoted, so each offset is tried. A slice is used only when it is a name sqlglot reads as that class; otherwise the class's name is kept, as before.

Window functions need an OVER clause. sqlglot classes most ranking and offset functions as aggregates, so in a field RANK() was reported as an aggregation, while ROW_NUMBER() (not an AggFunc in sqlglot) was not reported at all. expression_language.md (Window Functions) defines ROW_NUMBER, RANK, DENSE_RANK, NTILE, PERCENT_RANK, CUME_DIST, LAG, LEAD, FIRST_VALUE, LAST_VALUE and NTH_VALUE only with an OVER clause, in fields and metrics alike. So the OSSIE_SQL_2026 construct check (_portable_sql_construct_error) now rejects them without one, and the field aggregate check leaves them to it:

[SQL] Field 'orders.revenue' in model 'm' (OSSIE_SQL_2026): ROW_NUMBER is a window function and needs an OVER clause

A call in a window's PARTITION BY or ORDER BY, or in the argument of the windowed call, is not windowed, so RANK() OVER (ORDER BY LAG(x)) reports LAG. Other dialects are left alone. The grammar reads these as ordinary function calls, so compliance/expressions/ is unchanged.

Smaller changes: the window detection moves to a shared _is_windowed helper, and _is_bare_projection(tree) runs once for each expression instead of once for each call.

Not in this PR

  • An aggregate in a subquery, such as x + (SELECT SUM(x)), is left to feat(validation): reject queries in portable expressions and parse query sources #698, which rejects queries in portable expressions.
  • An aggregate outside the expression language (LISTAGG, BIT_AND, EVERY, ...) still passes in OSSIE_SQL_2026. Rejecting a function the language does not define is a separate check, since vendor functions belong in a dialect entry.

Impact on existing documents

validate_document over the YAML model and mapping documents in apache/ossie at d4dca4e and apache/ossie-converters at 62a61ba (103 documents, 705 fields) gives the same findings as on main. The windowed LAG(SUM(...), 1) OVER (...) metric in examples/tpcds_semantic_model.yaml stays valid.

Testing

  • pytest validation/tests/ goes from 1026 to 1060 passed (1 skipped) on sqlglot 30.33.0. On 30.19.0 the only failures are the two from validation: two tests fail on sqlglot 30.19.0, the declared minimum #767, the same as on main.
  • New cases: each window function without OVER, and with OVER (including IGNORE/RESPECT NULLS and a frame); a window function nested in a window's ORDER BY or argument; the name as written for every aggregate of the expression language, in lower case, and on each of the three parse paths.
  • make check passes.

Related Issues

Follow-up to #792 (fixes #785).

Checklist

Specification

  • Spec changes are included in core-spec/ and follow the existing structure
  • Spec changes have a [DISCUSS] thread on dev@ with a stated end date, and reach lazy consensus (or pass a [VOTE]) before merge
  • Breaking changes to the spec are clearly called out in the summary
  • For a material spec change, docs/spec_changes.md records rationale, compatibility and migration impact, or the PR explains why no entry is needed (no spec change: the validator now enforces what expression_language.md already says)

Ontology

  • Ontology changes in ontology/ are consistent with spec changes
  • New or modified terms are defined and documented

Converters

Validation

  • Validation rules in validation/ are updated if the spec changed
  • New validation cases are covered by tests

Documentation

  • docs/ is updated to reflect any user-facing changes
  • New features or behaviors are documented with examples where appropriate
  • CONTRIBUTING.md is updated if the contribution process changed

Examples

  • examples/ are added or updated for any new spec constructs

Tests

  • All existing tests pass (pytest / CI green)
  • New functionality is covered by tests

Compliance

  • ASF license headers are present on all new source files
  • New third-party dependencies follow Third-Party Dependencies
  • If third-party source code is vendored/copied (not just declared as a dependency), NOTICE and/or LICENSE have been updated per ASF policy

… functions

Follow-up to apache#792. The field aggregate check named a call after sqlglot's
class, not the function the expression writes, so VAR_POP(x) was reported
as VARIANCE_POP, VAR_SAMP(x) as VARIANCE and APPROX_COUNT_DISTINCT(x) as
APPROX_DISTINCT. The name is now read from the expression text at the
call's token position. _parse_sql_expression may parse the text as
written, as SELECT {text}, or with its first token quoted, so each of
those offsets is tried, and a slice is used only when it is a name sqlglot
reads as the call's class; otherwise the class's name is kept.

Ranking and offset functions were handled by the same check because
sqlglot classes most of them as aggregates: RANK() in a field was reported
as an aggregation, and ROW_NUMBER(), which is not an AggFunc in sqlglot,
was not reported at all. expression_language.md defines each of them only
with an OVER clause, in fields and metrics alike, so the OSSIE_SQL_2026
construct check now rejects ROW_NUMBER, RANK, DENSE_RANK, NTILE,
PERCENT_RANK, CUME_DIST, LAG, LEAD, FIRST_VALUE, LAST_VALUE and NTH_VALUE
without one, and the field aggregate check leaves them to it. The grammar
reads them as ordinary function calls, so compliance/expressions is
unchanged.

The window detection moves to a shared _is_windowed helper, and the bare
projection test runs once for each expression instead of once for each
call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validation: field expressions accept aggregate functions

1 participant