-
Notifications
You must be signed in to change notification settings - Fork 261
Use new API to do Iceberg partition. #13688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
res-life
wants to merge
12
commits into
NVIDIA:main
Choose a base branch
from
res-life:partition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+55
−81
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cae11f1
Update Iceberg Partitioner
7171910
Update
59e09cd
Update comments
81fc103
Update
75d397c
Fix
92f9af5
Fix memory leak in test code
f2352a3
Cache input columns number
6efe9c9
Fix close too many times
1e2be3e
Update
8bc05d7
Refactor
d94d0c8
Refactor
e4b2db6
Refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ import java.lang.Math.toIntExact | |
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| import ai.rapids.cudf.{ColumnVector => CudfColumnVector, OrderByArg, Scalar, Table} | ||
| import ai.rapids.cudf.{ColumnVector => CudfColumnVector, Table} | ||
| import com.nvidia.spark.rapids.{GpuBoundReference, GpuColumnVector, GpuExpression, GpuLiteral, RapidsHostColumnVector, SpillableColumnarBatch, SpillPriorities} | ||
| import com.nvidia.spark.rapids.Arm.{closeOnExcept, withResource} | ||
| import com.nvidia.spark.rapids.RapidsPluginImplicits.AutoCloseableProducingSeq | ||
|
|
@@ -54,10 +54,37 @@ class GpuIcebergPartitioner(val spec: PartitionSpec, | |
| private val partitionExprs: Seq[GpuExpression] = spec.fields().asScala.map(getPartitionExpr).toSeq | ||
|
|
||
| private val keyColNum: Int = spec.fields().size() | ||
| private val inputColNum: Int = dataSparkType.fields.length | ||
|
|
||
| // key column indices in the table: [key columns, input columns] | ||
| private val keyColIndices: Array[Int] = (0 until keyColNum).toArray | ||
| private val keySortOrders: Array[OrderByArg] = (0 until keyColNum) | ||
| .map(OrderByArg.asc(_, true)) | ||
| .toArray | ||
| // input column indices in the table: [key columns, input columns] | ||
| private val inputColumnIndices: Array[Int] = (keyColNum until (keyColNum + inputColNum)).toArray | ||
|
|
||
| /** | ||
| * Make a new table: [key columns, input columns] | ||
| */ | ||
| private def makeKeysAndInputTable(spillableInput: SpillableColumnarBatch): Table = { | ||
| withResource(spillableInput.getColumnarBatch()) { inputBatch => | ||
| // compute keys columns | ||
| val keyCols = partitionExprs.safeMap(_.columnarEval(inputBatch)) | ||
|
|
||
| // combine keys columns and input columns into a new table | ||
| withResource(keyCols) { _ => | ||
| withResource(GpuColumnVector.from(inputBatch)) { inputTable => | ||
| val numCols = keyCols.size + inputTable.getNumberOfColumns | ||
| val cols = new Array[CudfColumnVector](numCols) | ||
| for (i <- keyCols.indices) { | ||
| cols(i) = keyCols(i).getBase | ||
| } | ||
| for (i <- 0 until inputTable.getNumberOfColumns) { | ||
| cols(i + keyCols.size) = inputTable.getColumn(i) | ||
| } | ||
| new Table(cols:_*) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Partition the `input` columnar batch using iceberg's partition spec. | ||
|
|
@@ -70,94 +97,41 @@ class GpuIcebergPartitioner(val spec: PartitionSpec, | |
| return Seq.empty | ||
| } | ||
|
|
||
| val numRows = input.numRows() | ||
|
|
||
| val spillableInput = closeOnExcept(input) { _ => | ||
| SpillableColumnarBatch(input, ACTIVE_ON_DECK_PRIORITY) | ||
| } | ||
|
|
||
| val (partitionKeys, partitions) = withRetryNoSplit(spillableInput) { scb => | ||
| val parts = withResource(scb.getColumnarBatch()) { inputBatch => | ||
| partitionExprs.safeMap(_.columnarEval(inputBatch)) | ||
| } | ||
| val keysTable = withResource(parts) { _ => | ||
| val arr = new Array[CudfColumnVector](partitionExprs.size) | ||
| for (i <- partitionExprs.indices) { | ||
| arr(i) = parts(i).getBase | ||
| } | ||
| new Table(arr:_*) | ||
| } | ||
|
|
||
| val sortedKeyTableWithRowIdx = withResource(keysTable) { _ => | ||
| withResource(Scalar.fromInt(0)) { zero => | ||
| withResource(CudfColumnVector.sequence(zero, numRows)) { rowIdxCol => | ||
| val totalColCount = keysTable.getNumberOfColumns + 1 | ||
| val allCols = new Array[CudfColumnVector](totalColCount) | ||
|
|
||
| for (i <- 0 until keysTable.getNumberOfColumns) { | ||
| allCols(i) = keysTable.getColumn(i) | ||
| } | ||
| allCols(keysTable.getNumberOfColumns) = rowIdxCol | ||
|
|
||
| withResource(new Table(allCols: _*)) { allColsTable => | ||
| allColsTable.orderBy(keySortOrders: _*) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val (sortedPartitionKeys, splitIds, rowIdxCol) = withResource(sortedKeyTableWithRowIdx) { _ => | ||
| val uniqueKeysTable = sortedKeyTableWithRowIdx.groupBy(keyColIndices: _*) | ||
| .aggregate() | ||
|
|
||
| val sortedUniqueKeysTable = withResource(uniqueKeysTable) { _ => | ||
| uniqueKeysTable.orderBy(keySortOrders: _*) | ||
| } | ||
|
|
||
| val (sortedPartitionKeys, splitIds) = withResource(sortedUniqueKeysTable) { _ => | ||
| val partitionKeys = toPartitionKeys(spec.partitionType(), | ||
| partitionSparkType, | ||
| sortedUniqueKeysTable) | ||
|
|
||
| val splitIdsCv = sortedKeyTableWithRowIdx.upperBound( | ||
| sortedUniqueKeysTable, | ||
| keySortOrders: _*) | ||
|
|
||
| val splitIds = withResource(splitIdsCv) { _ => | ||
| GpuColumnVector.toIntArray(splitIdsCv) | ||
| } | ||
|
|
||
| (partitionKeys, splitIds) | ||
| } | ||
| withRetryNoSplit(spillableInput) { scb => | ||
| // make table: [key columns, input columns] | ||
| val keysAndInputTable = makeKeysAndInputTable(scb) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: the |
||
|
|
||
| val rowIdxCol = sortedKeyTableWithRowIdx.getColumn(keyColNum).incRefCount() | ||
| (sortedPartitionKeys, splitIds, rowIdxCol) | ||
| // split the input columns by the key columns, | ||
| // note: the result does not contain the key columns | ||
| val splitRet = withResource(keysAndInputTable) { _ => | ||
| keysAndInputTable.groupBy(keyColIndices: _*) | ||
| .contiguousSplitGroupsAndGenUniqKeys(inputColumnIndices) | ||
| } | ||
|
|
||
| withResource(rowIdxCol) { _ => | ||
| val inputTable = withResource(scb.getColumnarBatch()) { inputBatch => | ||
| GpuColumnVector.from(inputBatch) | ||
| } | ||
| // generate results | ||
| withResource(splitRet) { _ => | ||
| // generate the partition keys on the host side | ||
| val partitionKeys = toPartitionKeys(spec.partitionType(), | ||
| partitionSparkType, | ||
| splitRet.getUniqKeyTable) | ||
|
|
||
| val sortedDataTable = withResource(inputTable) { _ => | ||
| inputTable.gather(rowIdxCol) | ||
| } | ||
| // release unique table to save GPU memory | ||
| splitRet.closeUniqKeyTable() | ||
|
|
||
| val partitions = withResource(sortedDataTable) { _ => | ||
| sortedDataTable.contiguousSplit(splitIds: _*) | ||
| } | ||
| // get the partitions | ||
| val partitions = splitRet.getGroups | ||
|
|
||
| (sortedPartitionKeys, partitions) | ||
| // combine the partition keys and partitioned tables | ||
| partitionKeys.zip(partitions).map { case (partKey, partition) => | ||
| ColumnarBatchWithPartition(SpillableColumnarBatch(partition, sparkType, SpillPriorities | ||
| .ACTIVE_BATCHING_PRIORITY), partKey) | ||
| }.toSeq | ||
| } | ||
| } | ||
|
|
||
| withResource(partitions) { _ => | ||
| partitionKeys.zip(partitions).map { case (partKey, partition) => | ||
| ColumnarBatchWithPartition(SpillableColumnarBatch(partition, sparkType, SpillPriorities | ||
| .ACTIVE_BATCHING_PRIORITY), partKey) | ||
| }.toSeq | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private def getPartitionExpr(field: PartitionField) | ||
|
|
@@ -208,4 +182,4 @@ object GpuIcebergPartitioner { | |
| }).toArray | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: creating
Tableon line 83 with column references fromkeyColsandinputTable, but those source objects are closed by theirwithResourcewrappers (lines 73, 74) before the table is returned. the returned table contains dangling references to freed GPU memory.the columns need to be
incRefCount()before being added to the new table, or the table construction needs to happen before the source resources are closed