← Back to Pull requests

Fall back for ANSI BIGINT SUM in bounded ROWS windows [databricks] #17189

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 30, 2026 07:05 UTC
Author

JaCoCo production line coverage: +19 lines (sql-plugin +19: 16 shared src/main/scala + 3 spark330 TryModeShim.scala; single-run fix-line coverage, Spark 3.3.0 / Scala 2.12.21, base b2e742d1; spark340/spark400db173 shim variants outside shim 330).

Towards #17072.

Description

With ANSI enabled, bounded ROWS windows containing SUM(BIGINT) could return a result when Spark CPU raises an intermediate overflow, e.g. [Long.MaxValue, 1, -1]. Fall back the containing WindowExec to CPU for finite, unequal bounds; sibling aggregates in that node also fall back. Document this compatibility restriction.

Single-row and non-ANSI windows retain their existing GPU eligibility. Other input types retain their existing GPU eligibility. Inline small-Decimal rewrites (precision ≤ 8) remain GPU eligible; an optimizer may instead project their unscaled values into BIGINT attributes, which use the same conservative fallback. Other non-running frames that remain on GPU (half-unbounded ROWS with positive upper or finite lower bounds, full-unbounded frames, and bounded RANGE frames) and the pure-GPU follow-up (avoiding CPU fallback) remain tracked under #17072.

Follow-up commits:

  • c496856ef: replaces physical-planning SQLConf.get.ansiEnabled lookups with TryModeShim.getEvalMode(sum: Sum) across the spark330 (sum.useAnsiAdd), spark340 (sum.evalMode), and spark400db173 (sum.evalContext.evalMode) shims in both the bounded ROWS SUM(BIGINT) fallback guard and SumRuleMeta.convertToGpu, ensuring plans stay consistent with the analyzed Sum ANSI eval mode even if spark.sql.ansi.enabled changes between analysis and execution.

  • c9e7b6937: clarifies the current restriction scope in docs/compatibility.md (explicitly noting that other non-running frames on GPU do not yet preserve intermediate overflow checks), keeps GpuBatchedBoundedWindowExec in exist_classes for both ANSI modes in test_window_aggs_for_batched_finite_row_windows_{partitioned,unpartitioned}, and asserts non_exist_classes='WindowExec' under is_apache_runtime() for small Decimal(precision <= 8) inputs in _bounded_sum_window_plan_classes.
  • Validation

    Add 28 regression cases (25 original + 3 post-analysis spark.sql.ansi.enabled toggle cases in c496856ef) covering intermediate overflow, NULLs, cancellation order, input types, ANSI settings, post-analysis ANSI mode flips, and frame boundaries, with CPU/GPU result or error parity and execution-plan assertions.

    After follow-up commits c496856ef and c9e7b6937:

  • Spark 3.3.0 / Python 3.10 (c9e7b6937, local RTX 5880 Ada): all 9 modified window_function_test.py test functions (test_decimal_sum_window_ansi, test_decimal_sum_window_no_part_ansi, test_window_aggs_for_rows, test_window_aggs_for_negative_rows_{partitioned,unpartitioned}, test_window_aggs_for_batched_finite_row_windows_{partitioned,unpartitioned}, test_sum_long_ansi_rows_{overflow,negative_overflow}) 252 passed, 2 existing #7429 skips, 0 failed.

  • Spark 3.3.0 / Scala 2.12.21 (c496856ef, JDK 17, local RTX 5880 Ada; no JVM code changed in c9e7b6937): WindowFunctionSuite + WindowRetrySuite 69 passed, 0 failed, 0 ignored.

  • Shim coverage & multi-version compilation (c496856ef): scripts/check-shim-coverage.sh PASS, python3 build/check_shim_compatibility.py coverage PASS (34 buildvers, 4442 requirements, 0 findings), local compiles pass across Spark 330, 340, 400 (Scala 2.13), and 411 (Scala 2.13).

  • Root mvn verify -N (c9e7b6937): Apache RAT 0 unapproved, with-resource-nesting-audit 32/32 passed, scalastyle-all-modules 2137 files 0 errors / 0 warnings.

  • Single-run JaCoCo fix-line coverage (aggregator/target/spark330/new-classes, zero class-ID mismatches; unchanged in c9e7b6937 which touches only docs and Python ITs): sql-plugin +19 covered added production lines (16/30 in shared src/main/scala + 3/3 executable lines in spark330/TryModeShim.scala); no other applicable JVM production group has added production lines (spark340/spark400db173 shim variants are outside shim 330).
  • Previously completed full WindowFunctionSuite and WindowRetrySuite runs (prior commit 20fe33d1):

    | Runtime | Passed | Failed | Canceled |
    | --- | ---: | ---: | ---: |
    | Spark 3.3.0 / Scala 2.12.21, ANSI off | 64 | 0 | 0 |
    | Spark 4.0.5 / Scala 2.13.18, ANSI on | 55 | 0 | 9 |
    | Spark 4.0.5 / Scala 2.13.18, ANSI off | 64 | 0 | 0 |

    All 25 initial cases passed in each run; the nine cancellations are existing #12700 guards. Python window tests explicitly cover ANSI on/off, require the expected GPU execution or CPU fallback, and compare CPU/GPU results or overflow errors. The same 15-function local IT matrix passed on Spark 3.3.0 / Python 3.10 (258 passed, 2 existing #7429 skips) and Spark 4.0.5 / Python 3.12 (260 passed, 0 skipped), with zero failures. Full premerge CI, including both Databricks jobs, passed on 20fe33d1.

    Performance

    Run one complete Q51-derived query on SF3000 (nominal 3 TB) Parquet data, using quantities instead of prices and ROWS BETWEEN 6 PRECEDING AND CURRENT ROW for the two SUM windows. Retain the remaining joins, MAX windows, filtering, ordering, and limit. This custom query scans 2.093 billion fact rows after date pruning; it is not a full TPC-DS suite result.

    Compare parent b2e742d1 with the production fix at 4bf4655c on Spark 3.5.3 / JDK 17, 8 × A100 40 GB, one 16-core/16-GiB executor per GPU. ANSI and AQE are enabled, with 200 initial shuffle partitions and GPU allocation capped at 0.3. Measure query creation through collect(), with warm filesystem caches and no Spark DataFrame caching. Use before/after/after/before application order, two warmups and three measured executions per application; profiling is separate.

    | Full-query measurement | Before | After |
    | --- | ---: | ---: |
    | Median, six samples | 2.013 s | 2.541 s |
    | Sample range | 1.734–2.673 s | 2.201–3.922 s |
    | Host→GPU copies | 17.517 GiB | 22.896 GiB |
    | GPU→Host copies | 7.963 GiB | 10.462 GiB |

    Observed median cost: +26.2% (+527 ms). All 27 retained executions match the CPU schema and four ordered result rows. Plans confirm two CPU SUM windows after the fix, with surrounding GPU execution preserved; 130,892,745 rows cross GPU→CPU and back.

    Separate CUPTI captures on all eight executors report zero dropped records. Copy totals include scan/shuffle traffic and fallback conversions. Copy events overlap, so they do not directly explain the wall-time increase. Results are specific to this workload/configuration; the timing ranges overlap.

    Follow-up commit c496856ef is cold-path only (driver-side planning/tagging O(1) AST field read replacing SQLConf.get.ansiEnabled; executor hot path is unchanged), and c9e7b6937 updates only docs and Python integration tests.

    AI assistance: The implementation, regression tests, and PR description were prepared with Codex and Cursor assistance.

    Checklists

    Documentation

  • Updated for new or modified user-facing features or behaviors

  • No user-facing change
  • Testing

  • Added or modified tests to cover new code paths

  • Covered by existing tests

  • (Please provide the names of the existing tests in the PR description.)
  • Not required
  • Performance

  • Tests ran and results are added in the PR description

  • Issue filed with a link in the PR description

  • Not required
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: thirtiseven Review duration: 5328m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #17189"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%