Skip to content

feat(databricks): convert VAR_POP, VAR_SAMP, STDDEV_SAMP and STDDEV_POP within Databricks's own spread - #352

Queued
kvlonge wants to merge 4 commits into
mainfrom
feat/databricks-moments-2
Queued

kvlonge wants to merge 4 commits into
mainfrom
feat/databricks-moments-2

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Why

Databricks VAR_POP, VAR_SAMP, STDDEV_SAMP, STDDEV_POP and aliases VARIANCE, STDDEV and STD were refused. Databricks's own results move with row order and partitioning, so none matches exactly.

Changes

  • VAR_POP repeats Spark's row-by-row moment update in DuckDB's row order (central_moment_order, exact per recorded order).
  • The other six convert in two passes, mean then squared deviation (moment_spread). DISTINCT and OVER stay refused.
  • A divergence can give a float limit per data family, and a case names its family. Verify holds a case to its family's limit, compares a case of no family exactly, and fails when a declared family has no passed case.
  • Limits are each family's largest spread in Databricks's results for the same rows, in ulps: small integers 0+1 (Databricks fuses a multiply-add), mixed magnitudes 4, large mean with 3-4 rows 1,536, six rows 376,525,814, 24 partitioned rows 1,409,026,987, grouped rows 484,425,842, 4,097-100,000 rows 124,099,191,718.
  • Exploratory recordings saw up to 1.29e9 ulps (five large-mean values) and 4.3e11 (range partitions); the policy uses the corpus.
  • Evidence pin adds 260 cases with repeats, recorded on 2026.47.
  • An IN subquery's divergences no longer mark outer result columns.
  • Signs off the six Databricks declared-refusal groups (27 statements, each a raise) and row-pattern matching, which gains MATCH_RECOGNIZE NEXT and PREV. The 13 forms those statements exercise move from refused to unmet_obligations, not matching: a loud raise is not a correct result.

Verification

  • Pinned verify: 6342 cases, 3513 passed, 0 failed. No shared statement got weaker. Ten forms the new cases use (DOUBLE, + - * / %, RANGE, GROUP BY, hints, TVF) count within ulps through them. MATCH_RECOGNIZE FIRST and LAST have no obligations, so with their statements under the signed group they read no_obligations.
  • Unit tests for the conversions, per-family limits and limits beyond 2^32 ulps.
  • make check-ci.

VAR_POP runs on Databricks as Spark's CentralMomentAgg: a running count,
mean and second central moment updated row by row in DOUBLE. The converted
SQL repeats that update exactly in the order DuckDB collects the rows, so
its result is one Databricks itself returns for some order of the same
rows. It relies on the central_moment_order divergence, bounded by the
spread Databricks's own results showed over forced row orders.

VAR_SAMP, STDDEV_SAMP, STDDEV_POP and their aliases stay refused: alone in
an aggregation Databricks computes them otherwise, and its recordings of
the same rows differ from the update.
… Databricks's own spread

VAR_SAMP, STDDEV_SAMP, STDDEV_POP and the aliases VARIANCE, STDDEV and STD
convert in two passes over the rows DuckDB collects: the mean, then the
sum of squared deviations from it. Databricks's own result for the same
rows moves with row order, partitioning, file layout and the other
aggregates beside it, so the conversion relies on the moment_spread
divergence.

A divergence can now give a 64-bit float limit per family of data, and a
case names its family. Verify holds each case to its own family's limit
only, compares a case of no family exactly, and fails when a declared
family has no passed case relying on it. Each limit is the largest spread
between Databricks's recorded results for the same rows of that family,
plus one unit for three small integers, where Databricks fuses the
multiply and add of each squared deviation.

The evidence pin moves to the commit holding those recordings.
A query nested in an expression, such as an IN subquery in WHERE, named its
own column positions for the divergences it relied on, and verify read
them as the outer result's columns. A function only in a WHERE subquery
then widened the comparison of an unrelated result column. A subquery's
divergences now name only the column whose expression holds it.
…ern matching

Each of the six declared-refusal groups (collect-list-order, first-last-order,
float-sum-order, regex-column-pattern, to-binary-odd-hex and
url-decode-invalid-utf8) has a reason matching its loud divergence in the
Databricks policy, and every one of its 27 statements raises on DuckDB rather
than return another value, so all six are signed off.

MATCH_RECOGNIZE's NEXT and PREV navigate rows within a row-pattern match,
which DuckDB cannot evaluate either, so they join the row-pattern-matching
group, which is signed off with them.

In the baseline, the 13 forms the declared statements exercise move from
refused to unmet obligations, since the obligations citing those statements
stay unmet, and the eight row-pattern forms become not translatable.
@kvlonge
kvlonge enabled auto-merge November 30, 2026 17:53
@kvlonge
kvlonge added this pull request to the merge queue Nov 30, 2026
Any commits made after this event will not be merged.

This branch has not been deployed

No deployments
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.

1 participant