← Back to Pull requests

fix: bound the batch under the operator that expands a large string scalar #7316

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 29, 2026 17:35 UTC
Author

Which issue does this PR close?

Part of #7247. The operators that are still unbounded are tracked in #7689.

Rationale for this change

A string scalar, from a scalar subquery or a literal, is expanded to the batch length by aggregates, the hash partitioner and string kernels. A 256 KiB string over the default 8192-row batch needs 2 GiB of value bytes, past Arrow's 32-bit Utf8 offsets, so native execution panics with offset overflow or fails with byte array offset overflow. Spark evaluates such scalars row by row and succeeds.

The expansion happens in DataFusion (aggregate argument evaluation, concat) as well as in Comet's shuffle partitioner, and a plan-time fallback cannot see a subquery's value. So the fix bounds the rows per batch that reach the expanding operator.

The first version of this PR lowered the session batch size and sliced batches at the scans. Review showed that does not bound what reaches the operator: a filter coalesces its output to its own batch size, a window emits its whole partition, and a scalar built from a subquery result (repeat(<subquery>, 2)) is wider than the result the planner measured. This version enforces the bound at the operator instead.

What changes are included in this PR?

  • A new CometBatchSplitExec in the operators crate, a thin operator over DataFusion's BatchSplitStream, which emits an input batch in slices of at most batch_size rows. Slices share buffers.

  • The planner wraps the input of a projection, filter, hash aggregate (grouping, aggregate inputs and filters), sort with a limit (its keys) or shuffle writer with hash or range partitioning in a CometBatchSplitExec whenever that operator expands a string or binary scalar. The split size is the rows at which the scalars, expanded, reach 1 GiB; for the issue's string that is 4096 rows. A batch at or under that size passes through unsliced, so a scalar of ordinary size never slices anything and never hands a sort zero-copy slices of a shared buffer (apache/datafusion#25804). No width is assumed for what arrives from below, since a filter coalesces toward DataFusion's default, a window emits its whole partition and a broadcast arrives as a few wide batches. A plan without such a scalar is unchanged.

  • A scalar is any column-free subtree of Utf8 or Binary type. The planner evaluates it once on a one-row batch, so a scalar built by an expression counts at its final width. A scalar that a comparison (=, <>, <, <=> and the other operators DataFusion evaluates as a Datum) or an IN list takes as a constant is compared and never expanded, so it is skipped: payload = 'x' installs no split. An operand computed per row, such as the IN member concat(repeat('x', 262144), CAST(id AS STRING)), counts the scalars it contains. A column, a volatile expression such as uuid(), or a JVM UDF is a per-row node: it is never evaluated at plan time, but the scalars beside it are still counted, so concat(repeat('x', 262144), uuid()) is measured at 256 KiB.

  • The split under a filter is transparent to join runtime filtering: the join planner aligns the Spark and native trees through it, so the filter keeps its pushdown permission, and reader attachment crosses it to reach the Parquet scan, since slicing batches discards no rows.

  • executePlan pulls JVM input only when the plan waits for it, as its first call already did, instead of pulling the next batch up front on every call. A limit can then finish on rows a split still holds without reading another batch.

  • The session batch size, ScanExec, ShuffleScanExec and the range source are unchanged from main.
  • How are these changes tested?

  • Fixture operators/large_scalar_string.sql with fourteen queries that fail on main with byte array offset overflow: the grouped count, the hash partitioning key, concat with a subquery and with a literal, the literal over a native range, a filter under the projection, a window under the aggregate, a 64 KiB scalar over a window partition wider than the session batch size, a scalar beside uuid(), repeat of the subquery result, an IN list with a member computed per row, a sort key under a limit, and a string inside a list scalar expanded by IF. All run in query mode, so the plan is checked to be native. NullPropagation is excluded so the grouped count keeps its literal argument rather than becoming count(1).

  • Fixture operators/large_scalar_string_small_batch.sql runs the filter case with spark.comet.batchSize=256, where the scalar fits the session batch size as is.

  • CometExecIteratorLifecycleSuite: a 10-row JVM batch followed by an input that throws, under LIMIT 3, returns three rows without reading the input again. It fails on the loop that pulled JVM input before every poll.

  • CometNativeShuffleSuite: a range shuffle whose ordering expression is a 256 KiB scalar, with no projection between the exchange and the range source.

  • CometJoinSuite: the deterministic residual test also runs a string residual, concat(CAST(p.payload AS STRING), '!') <> '2506!', and checks the reader filter still attaches and prunes row groups with the split under the filter. Rust unit tests cover the filter keeping its permission through the split and reader attachment crossing it.

  • Rust unit tests for CometBatchSplitExec and for the scalar measurement: literals and columns, a scalar built by an expression, the strings inside a list, nested list or struct scalar, a scalar beside a volatile sibling that is not evaluated, IN list members, a scalar compared directly against one computed per row, the planner wrapping the input at the scalar's cap whatever the session batch size, and the split going under a sort with a limit but not under a full sort.

  • CometSqlFileTestSuite for operators/, scalar_subquery and shuffle, CometNativeShuffleSuite and CometJoinSuite, on Spark 4.1, and the core and operators crate test suites.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: rich7420 Review duration: 46366m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.59) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    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%
    44%
    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 #7316"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.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: 59% (MEDIUM) · Higher = larger blast radius
    59%
    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: 44.7% · Lower = rushed or shallow review
    44.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: 32.6% · Safe to merge
    32.6%