vranes opened a new pull request, #60611: URL: https://github.com/apache/spark/pull/60611
### What changes were proposed in this pull request? This PR adds whole-stage codegen to `BinByExec`. Bin expansion, and the filters and projections above BIN BY, now run in one generated stage. - The generated code evaluates the child columns once per input row. Then it loops over the bins of that row. The loop uses the same bucket helpers as the iterator path (`DateTimeUtils.timeBucketFromTimestampDTInterval` and `DateTimeUtils.timeBucketFromIndexDTInterval`), so bin boundaries, ratios, and errors do not change. - The loop stops when a LIMIT in the same stage is reached (`limitNotReachedCond`). - The loop checks for a task kill every 1024 bins of one input row. For shorter rows, the kill check of the stage input applies, as it does on the iterator path. - A new internal config, `spark.sql.codegen.binBy.enabled` (default `true`), turns BIN BY codegen off. Like `GenerateExec` and the multi-match loops of hash and sort-merge joins, the generated loop finishes all bins of the current input row before it returns control. When the BIN BY output leaves the generated stage (for example, at an exchange or a top-level LIMIT), `BufferedRowIterator` holds all bins of that row in memory. When an operator in the same stage consumes the rows (for example, a partial aggregate), no rows are buffered. With `spark.sql.codegen.binBy.enabled=false`, BIN BY uses the iterator path, which emits one bin at a time. ### Why are the changes needed? `BinByExec` does not support codegen. It materializes each output row with an `UnsafeProjection`, and it splits whole-stage codegen into separate stages below and above it. With codegen, each bin goes directly to the parent operators in generated code, so fewer intermediate rows are materialized. ### Does this PR introduce _any_ user-facing change? No. Query results do not change. The new config is internal. ### How was this patch tested? New tests in `BinBySuite`: - Codegen fuses BIN BY with a filter on `bin_start` and a projection of wide and nested columns, with `spark.sql.codegen.splitConsumeFuncByOperator` on and off. The results match the results without codegen. - NULL ranges and zero-length ranges give the same results with codegen on and off. - A nondeterministic column keeps one value across all bins of an input row, also under LIMIT. - A LIMIT in the same stage stops the expansion of a 1000-bin row after 5 bins, with BIN BY codegen on and off. `BinBySuite` now runs with `spark.sql.codegen.fallback=false`, so a codegen compile error fails the tests. The new `BinByWithoutCodegenSuite` runs all `BinBySuite` tests with `spark.sql.codegen.binBy.enabled=false`, so the iterator path keeps its test coverage. ``` build/sbt 'sql/testOnly org.apache.spark.sql.BinBySuite org.apache.spark.sql.BinByWithoutCodegenSuite' ``` ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Isaac (Claude Opus 5.5) This pull request and its description were written by Isaac. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
