rekbun opened a new pull request, #60453:
URL: https://github.com/apache/spark/pull/60453

   ### What changes were proposed in this pull request?
   
   A partial hash aggregate with the codegen fast hash map 
(`spark.sql.codegen.aggregate.map.twolevel.enabled`, on by default) registers a 
task-completion hook that closes the map, in case its output is not fully 
consumed. `close()` frees the map's memory page, but the hook keeps the map, 
and with it its bucket array, reachable until the task ends.
   
   This PR makes `close()` drop the bucket array, both in 
`RowBasedAggregateHashMap` and in the map that `VectorizedHashMapGenerator` 
generates.
   
   Nothing probes a map after it is closed:
   - The maps are closed once their output is done.
   - With adaptive partial aggregation, they are closed only after they are 
frozen, and a frozen fast map is no longer probed.
   
   ### Why are the changes needed?
   
   The bucket array is 512 KiB at the default 
`spark.sql.codegen.aggregate.fastHashMap.capacityBit` of 16. It is on the heap 
and not tracked by the memory manager.
   
   A task that coalesces many partitions of such an aggregate creates one map 
per partition, even for an empty partition, and holds all of their bucket 
arrays until it ends.
   
   Nothing on master coalesces below a partial aggregate today. A keyed 
aggregate over `GroupPartitionsExec`, for example, is planned as one aggregate 
above it.
   
   SPARK-60265 (#59522) changes that. It packs a scan's input splits into fewer 
tasks below a shuffle and above any partial aggregate, so each packed task runs 
one partial aggregate per split. In a local test with 2,048 splits packed into 
one task:
   - **Before this change:** the task held 2,048 maps and 1 GiB of bucket 
arrays until it ended, with 1,093 MiB of heap retained after GC.
   - **With this change:** it held no bucket arrays.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No.
   
   ### How was this patch tested?
   
   A new test in `HashAggregateExecSuite` checks that closing a fast hash map 
drops its buckets and leaves no memory allocated.
   
   Locally, with JDK 21:
   - `HashAggregateExecSuite`, `AdaptivePartialAggregationSuite`, 
`PartialAggregationBypassSuite`, `DataFrameAggregateSuite`, the single-level, 
two-level and vectorized two-level `AggregateHashMap` suites, 
`WholeStageCodegenSuite` and `CollationAggregationSuite`: 1068 tests passed (3 
ignored in `WholeStageCodegenSuite`, as on master).
   - Hive's `HashAggregationQuerySuite` and 
`HashAggregationQueryWithControlledFallbackSuite`: 52 tests passed.
   - SQL scalastyle (main and test), Java checkstyle and 
`sql/mimaReportBinaryIssues` passed.
   
   ```bash
   build/sbt -Phive \
     'sql/testOnly 
org.apache.spark.sql.execution.aggregate.HashAggregateExecSuite 
org.apache.spark.sql.execution.aggregate.AdaptivePartialAggregationSuite 
org.apache.spark.sql.execution.aggregate.PartialAggregationBypassSuite 
org.apache.spark.sql.DataFrameAggregateSuite 
org.apache.spark.sql.SingleLevelAggregateHashMapSuite 
org.apache.spark.sql.TwoLevelAggregateHashMapSuite 
org.apache.spark.sql.TwoLevelAggregateHashMapWithVectorizedMapSuite 
org.apache.spark.sql.execution.WholeStageCodegenSuite 
org.apache.spark.sql.collation.CollationAggregationSuite' \
     'hive/testOnly 
org.apache.spark.sql.hive.execution.HashAggregationQuerySuite 
org.apache.spark.sql.hive.execution.HashAggregationQueryWithControlledFallbackSuite'
 \
     'sql/scalastyle' 'sql/Test/scalastyle' 'sql/checkstyle' 
'sql/mimaReportBinaryIssues'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


-- 
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]

Reply via email to