Repository navigation
fix: bound the scalar a join filter, Expand or full sort expands - #7762
Draft
zhangfengcdt wants to merge 31 commits into
Draft
zhangfengcdt wants to merge 31 commits into
zhangfengcdt wants to merge 31 commits into
Conversation
A string scalar from a literal or a scalar subquery is expanded to the batch length by aggregates, the hash partitioner and string kernels. A 256 KiB string over an 8192-row batch needs 2 GiB of value bytes, past Arrow's offsets, and native execution panics where Spark succeeds. Add up the string and binary scalars while planning and lower the session batch size so their expansion stays within 1 GiB. Native sources read the batch size when they run, the range source now included, and the JVM-fed leaves slice wider batches to it.
…calar Rework after review. Capping the input batch size does not bound what reaches the expanding 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 can be wider than the result. The scan slicers also dropped a pending slice under LIMIT and double counted output_rows. A new CometBatchSplitExec, over DataFusion's BatchSplitStream, is placed right under a projection, filter, hash aggregate or hash shuffle writer when that operator's string and binary scalars, expanded to the session batch size, would exceed 1 GiB. The planner measures each column-free subtree of those types by evaluating it once on a one-row batch, so that repeat(<subquery>, n) counts at its final width. IN list members are compared and never expanded, so they are skipped. The session batch size, the scan and shuffle scan operators and the range source are unchanged.
uuid(0) and randstr(10, 0) are column-free string expressions, so the planner evaluated them once at plan time to measure their width, which drew the first value from their seeded generator. Execution then produced Spark's second value onward and the bit-for-bit tests failed. A subtree with a volatile expression yields a value per row rather than a scalar, so it is never measured: Comet's stateful expressions and any DataFusion scalar function declared volatile.
…he scalar cap The split was installed only when the scalar cap fell below the session batch size. With spark.comet.batchSize=256 a 256 KiB scalar allows 4096 rows, so nothing was installed, but DataFusion's filter coalesces toward its own default of 8192 rows whatever the session says, and the projection above it overflowed the 32-bit offsets. The decision now compares the cap against the larger of the session batch size and DataFusion's default, and splits at the smaller of the cap and the session size. A new fixture runs the filter case with a 256-row session.
The split was installed only when the scalar cap fell below the widest batch assumed to arrive, the larger of the session batch size and DataFusion's default. A window emits its whole partition and a broadcast arrives as a few wide batches, so no width can be assumed: a 64 KiB scalar over a 32768-row window partition skipped the split and overflowed the 32-bit offsets. An operator that expands any string or binary scalar now has its input split at the smaller of the scalar cap and the session batch size. A plan without such a scalar is unchanged. The fixture excludes NullPropagation so that the grouped count keeps the wide literal as its argument instead of being rewritten to count(1), and adds the window case above.
…ch-cap # Conflicts: # native/core/src/execution/planner.rs
…keys
The plan-time measurement stopped at the first volatile expression, so
a column-free subtree such as concat(repeat('x', 262144), uuid()) was
measured as zero bytes and left unbounded. A column, a volatile
function, a JVM UDF or a subquery is now a boundary rather than a
reason to give up: the scalars among its siblings are still counted,
and the volatile node itself is never evaluated.
The shuffle writer only bounded hash partitioning keys. Range
partitioning evaluates its ordering expressions in the exchange too,
so those are bounded the same way.
…o join runtime filters
The measurement only looked at the value an IN list tests, so a list
member computed per row, such as concat(repeat('x', 262144),
CAST(id AS STRING)), expanded its scalar unbounded. The scalars inside
such a member now count; a constant member is still compared without
expansion and skipped.
The split that bounds a filter's input sat between the filter and its
Spark child, which broke the pointer alignment the join planner uses
to carry a filter's runtime-filter pushdown permission, and stopped
reader attachment on its way to the Parquet scan. Both now look through
the split and rebuild it over the prepared or filtered child, since
slicing batches discards no rows.
…ch-cap # Conflicts: # native/core/src/execution/planner/join.rs
A scalar operand of a comparison is compared as a Datum and never expanded, so `payload = 'x'` no longer installs a split; an operand computed per row still counts the scalars it contains. The sort bounds its input with its keys, which covers the TopK under a limit.
…ch-cap # Conflicts: # native/core/src/execution/planner/aggregate.rs
A full sort concatenates its input before evaluating its keys, so the split did not bound it, and DataFusion's ExternalSorter reserves each slice of a shared buffer as if it owned the buffer (apache/datafusion#25804). executePlan no longer pulls the next JVM batch on every call. It pulls only when the plan waits for input, so a limit can finish on rows a split still holds without reading another batch.
A one-byte scalar split every batch at the session batch size, so a projection below a full sort handed ExternalSorter zero-copy slices of shared buffers, which it reserves one by one (apache/datafusion#25804). Splitting at the scalar's own cap still keeps each expansion within 1 GiB, and a batch at or under the cap passes through unsliced. The pull_input_batches doc still described pulling before every poll.
Splitting only at the scalar's cap means concat(s, '!') no longer slices the scan's batches, so the read-ahead regression drops the projection and batch size and runs a limit over the scan directly. It still fails on the loop that pulled before every poll.
…ch-cap # Conflicts: # spark/src/test/scala/org/apache/comet/exec/CometNativeShuffleSuite.scala
A scalar whose type nests a string, such as array(repeat('x', 262144)),
was measured as 0 bytes, so nothing bounded its expansion and IF over it
overflowed the 32-bit offsets. The measurement now sums the string and
binary value bytes anywhere inside the evaluated scalar.
A join evaluates its filter over up to a batch size of the row pairs it matched, however wide its inputs, so a split under it cannot bound a large string scalar in the filter. The planner now runs a hash, sort-merge or nested loop join whose filter expands such a scalar under a BatchSizeScopeExec, which lowers the session batch size to the scalar's cap for the join and the operators below it. Expand evaluates its projections over its input batch, so its input gets the same split as a projection's. Part of apache#7689.
DataFusion's sort computes every row's key when it merges its sorted runs and holds all of them, so a key built from a 256 KiB literal over 8192 rows needs 2 GiB however the input is split. Spark's sort compares keys row by row. A full sort whose keys expand string or binary literals past the 1 GiB bound at the session batch size now falls back. Part of apache#7689.
added 6 commits
November 24, 2026 13:26
…ys batch by batch A nested-loop join evaluates its filter for one build row against a whole probe batch, whatever the session batch size, and a range sizes its batches when it is planned. The planner now splits the probe input at the rows the filter's scalar allows. A full sort concatenates the batches it buffers into runs of up to 1 MB before evaluating its keys, so a smaller batch size did not bound them. A full sort whose keys expand a scalar now runs scoped to sort each input batch on its own, over a split at the scalar's cap. The fallback to Spark's sort is a fixed literal size, since its merge holds a batch of keys for every run.
…e to its own PR The estimator now classifies and measures each subtree in a single bottom-up pass, so a deep expression costs linear time at plan time. The module docs list the operators apache#7689 tracks and note the plan-time evaluation of scalars. The change to when execute_plan pulls JVM input moves to a separate pull request.
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #7689.
Stacked on #7316. Until that merges, this diff also shows its commits. The changes here are the commits after
a0e5690a9:b84315957(join filter and Expand),0ba605423(full sort), and55743a726(nested loop join probe side, and the full sort's runs).Rationale for this change
#7316 bounds the batch under the operators that expand a large string or binary scalar. #7689 lists the ones it left out. Each case below failed with
byte array offset overflow.What changes are included in this PR?
CometBatchSizeScopeExecruns a hash, sort-merge or nested loop join with the session batch size lowered to the scalar's cap. It is added only when the join's filter expands such a scalar. It also lowers the batch size of the operators below the join, and it reports the join's metrics as its own.sort_in_place_threshold_bytes(1 MB) and evaluates its keys over each run, so narrow rows give runs far larger than a batch. A full sort whose keys expand a scalar now runs under aCometBatchSizeScopeExecthat sets that threshold to 0, so it sorts each input batch alone. The scope also lowers the batch size to the scalar's cap, which bounds the merge's batches, and the input is split at the cap. That split copies what it cuts, because the sort reserves each slice of a shared buffer as the whole buffer (ExternalSorter memory accounting: remaining gaps on main datafusion#25804).The merge still holds a batch of keys for every run, while Spark's sort compares keys row by row. So a full sort whose keys contain a string or binary literal over 128 KiB falls back to Spark. That is 1 GiB of keys per batch at the default batch size. The limit is fixed rather than scaled by
spark.comet.batchSize, since a smaller batch only makes more runs. A scalar subquery's result is not known at planning, so the fallback does not cover it, but the native path no longer overflows on it. A sort with a limit stays native and is bounded by fix: bound the batch under the operator that expands a large string scalar #7316.ExtractWindowExpressionsmoves them into a projection below the window, which fix: bound the batch under the operator that expands a large string scalar #7316 already bounds.How are these changes tested?
operators/large_scalar_string_join_expand.sql: a sort-merge, a shuffled hash and a broadcast nested loop join whose filter expands a 256 KiB literal over 8192 matched pairs, a broadcast nested loop join whose probe side is a range, a distinct aggregate whose Expand expands it, and a full sort on a key that expands it. Each failed with the offset overflow before this change. The joins and Expand run natively and match Spark. The sort falls back with its reason and matches Spark. A full sort with a 1 KiB key literal over three partitions runs natively and matches Spark.operators/large_scalar_string_sort_small_batch.sql: the full sort atspark.comet.batchSize=4096still falls back.CometBatchSizeScopeExecruns its input at the scoped batch size and leaves the caller's context alone; the planner scopes a join whose filter expands a large scalar, and not one with a small scalar or no filter, and splits a nested loop join's probe input; a sorting scope turns off the sort's concatenation; a full sort gets the sorting scope and a compacting split.CometJoinSuiteandCometAggregateSuitepass.