← Back to Pull requests

[SPARK-58315][SQL] Refactor DSv2 scan statistics handling #57487

Closed
unknown closed this pull request without merging on Jul 31, 2026 07:34 UTC
View on GitHub
unknown unknown commented on Jul 24, 2026 04:28 UTC
Author

Refactor how a DataSource V2 (DSv2) scan reports statistics, aligning it with
the v1 (LogicalRelation / CatalogStatistics.toPlanStats) path.

  • computeStats gates on cboEnabled || planStatsEnabled: full statistics

  • only when needed, otherwise a cheaper size-only estimate via the new
    SupportsReportStatistics.estimateSizeInBytes() default method.
  • New SupportsReportStatistics.reflectsFullyPushedDownFilters() default method

  • (true). When a connector returns false, V2ScanRelationPushDown re-adds
    the fully pushed, deterministic filters above the scan so FilterEstimation
    adjusts the stats. The rewrite is row-equivalent, idempotent, and best-effort
    under column pruning (pruned-reference predicates are dropped).
  • transformV2Stats infers sizeInBytes from the row count when no size is

  • reported (matching v1); the dead defaultRowCount parameter is removed.
  • V1ScanWrapper implements SupportsReportStatistics, delegating to the

  • wrapped v1 scan so its stats are no longer dropped.
  • Shared logic extracted into V2StatisticsUtils (null-safe).
  • Both new methods have defaults that preserve existing behavior, so existing
    connectors need no change.

    Why are the changes needed?

    To let DSv2 connectors produce statistics more cheaply and let Spark plan on
    more accurate statistics, matching v1:

  • Cheaper when only size is needed: most queries (CBO/plan stats off) only need

  • sizeInBytes, so a connector can skip the expensive row/column statistics.
  • Accurate after pushdown: whole-table stats are adjusted by FilterEstimation

  • instead of planning on too-large estimates, improving plan choices.
  • Fuller use of reported stats: v1 scan stats and a row-count-derived size are

  • now used instead of the default size, at no extra cost to the connector.

    Does this PR introduce _any_ user-facing change?


    New API introduced but no behavior change by default except for the following cases:
  • when a source reports a row count but no size, the size is inferred from the row count (previously the

  • default size was used)
  • a v1 scan implementing SupportsReportStatistics now has its reported statistics surfaced (previously dropped).


  • Both changes make the estimates more accurate and align with the v1 path.

    How was this patch tested?

    New unit and end-to-end tests in DataSourceV2RelationSuite and
    DataSourceV2Suite cover: the CBO / plan-stats gating, size inference
    (including the zero-row case), null/empty statistics handling, V1ScanWrapper
    delegation, and the post-pushdown filter re-adding (including the recursion
    through stacked residual filters).

    Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Code

    Diff: 347 lines (+260 / -87) across 4 files Reviewer: yyanyy Review duration: 10265m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.42) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 347 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    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.350
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #57487"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 18.3% · Safe to merge
    18.3%
    Repository