forked from NVIDIA/spark-rapids
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support Dictionary Late Materialization (push down to GPU)
- Loading branch information
1 parent
5f1af86
commit 76639b0
Showing
10 changed files
with
811 additions
and
145 deletions.
There are no files selected for viewing
This file contains 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
389 changes: 324 additions & 65 deletions
389
.../scala/org/apache/spark/sql/execution/datasources/parquet/rapids/AsyncParquetReader.scala
Large diffs are not rendered by default.
Oops, something went wrong.
131 changes: 131 additions & 0 deletions
131
...in/scala/org/apache/spark/sql/execution/datasources/parquet/rapids/DictLateMatUtils.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.datasources.parquet.rapids | ||
|
||
import java.util.Optional | ||
|
||
import scala.collection.mutable | ||
|
||
import ai.rapids.cudf.{DType, HostColumnVector, HostColumnVectorCore, HostMemoryBuffer} | ||
import org.apache.parquet.column.ColumnDescriptor | ||
import org.apache.parquet.column.page.{DataPage, DataPageV1, DataPageV2, DictionaryPage, PageReader, PageReadStore} | ||
import org.apache.parquet.column.values.dictionary.PlainValuesDictionary.PlainBinaryDictionary | ||
|
||
import org.apache.spark.internal.Logging | ||
|
||
case class DictLatMatInfo(dictVector: HostColumnVector, dictPageOffsets: Array[Int]) | ||
|
||
object DictLateMatUtils extends Logging { | ||
|
||
def extractDict(rowGroups: Seq[PageReadStore], | ||
descriptor: ColumnDescriptor): Option[DictLatMatInfo] = { | ||
|
||
val dictPages = mutable.ArrayBuffer[DictionaryPage]() | ||
|
||
// Go through each RowGroup and each page inside them to check if all pages use Dictionary. | ||
// Dictionary late materialization only works if all pages use Dictionary. | ||
rowGroups.foreach { rowGroup => | ||
val pageReader = rowGroup.getPageReader(descriptor) | ||
val dictPage = pageReader.readDictionaryPage() | ||
if (dictPage == null || !isAllDictEncoded(pageReader)) { | ||
return None | ||
} | ||
dictPages += dictPage | ||
} | ||
|
||
Some(combineDictPages(dictPages, descriptor)) | ||
} | ||
|
||
private def combineDictPages(dictPages: Seq[DictionaryPage], | ||
descriptor: ColumnDescriptor): DictLatMatInfo = { | ||
val pageOffsets = mutable.ArrayBuffer[Int](0) | ||
var rowNum: Int = 0 | ||
|
||
val dictionaries = dictPages.map { dictPage => | ||
val dictionary = dictPage.getEncoding.initDictionary(descriptor, dictPage) | ||
.asInstanceOf[PlainBinaryDictionary] | ||
rowNum += dictionary.getMaxId + 1 | ||
pageOffsets += rowNum | ||
dictionary | ||
} | ||
|
||
var charNum: Int = 0 | ||
val offsetBuf = HostMemoryBuffer.allocate((rowNum + 1) * 4L) | ||
offsetBuf.setInt(0, 0) | ||
var i = 1 | ||
dictionaries.foreach { dict => | ||
(0 to dict.getMaxId).foreach { j => | ||
charNum += dict.decodeToBinary(j).length() | ||
offsetBuf.setInt(i * 4L, charNum) | ||
i += 1 | ||
} | ||
} | ||
|
||
val charBuf = HostMemoryBuffer.allocate(charNum) | ||
i = 0 | ||
dictionaries.foreach { dict => | ||
(0 to dict.getMaxId).foreach { j => | ||
val ba = dict.decodeToBinary(j).getBytes | ||
charBuf.setBytes(offsetBuf.getInt(i * 4L), ba, 0, ba.length) | ||
i += 1 | ||
} | ||
} | ||
|
||
val dictVector = new HostColumnVector(DType.STRING, rowNum, Optional.of(0L), | ||
charBuf, null, offsetBuf, new java.util.ArrayList[HostColumnVectorCore]()) | ||
|
||
DictLatMatInfo(dictVector, pageOffsets.toArray) | ||
} | ||
|
||
private def isAllDictEncoded(pageReader: PageReader): Boolean = { | ||
require(ccPageReader.isInstance(pageReader), | ||
"Only supports org.apache.parquet.hadoop.ColumnChunkPageReadStore.ColumnChunkPageReader") | ||
val rawPagesField = ccPageReader.getDeclaredField("compressedPages") | ||
rawPagesField.setAccessible(true) | ||
|
||
val pageQueue = rawPagesField.get(pageReader).asInstanceOf[java.util.ArrayDeque[DataPage]] | ||
val swapQueue = new java.util.ArrayDeque[DataPage]() | ||
var allDictEncoded = true | ||
|
||
while (!pageQueue.isEmpty) { | ||
swapQueue.addLast(pageQueue.pollFirst()) | ||
if (allDictEncoded) { | ||
allDictEncoded = swapQueue.getLast match { | ||
case p: DataPageV1 => | ||
p.getValueEncoding.usesDictionary() | ||
case p: DataPageV2 => | ||
p.getDataEncoding.usesDictionary() | ||
} | ||
} | ||
} | ||
while (!swapQueue.isEmpty) { | ||
pageQueue.addLast(swapQueue.pollFirst()) | ||
} | ||
|
||
allDictEncoded | ||
} | ||
|
||
private val ccPageReader: Class[_] = { | ||
val ccPageReadStore = Class.forName("org.apache.parquet.hadoop.ColumnChunkPageReadStore") | ||
val ccPageReader = ccPageReadStore.getDeclaredClasses.find { memberClz => | ||
memberClz.getSimpleName.equals("ColumnChunkPageReader") | ||
} | ||
require(ccPageReader.nonEmpty, "can NOT find the Class definition of ColumnChunkPageReader") | ||
ccPageReader.get | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...la/org/apache/spark/sql/execution/datasources/parquet/rapids/OffHeapBinaryDictionary.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.execution.datasources.parquet.rapids; | ||
|
||
import java.io.Closeable; | ||
|
||
import ai.rapids.cudf.HostMemoryBuffer; | ||
import org.apache.parquet.column.Dictionary; | ||
import org.apache.parquet.column.values.dictionary.PlainValuesDictionary.PlainBinaryDictionary; | ||
|
||
public class OffHeapBinaryDictionary extends Dictionary implements Closeable { | ||
|
||
public OffHeapBinaryDictionary(PlainBinaryDictionary binDict) { | ||
super(binDict.getEncoding()); | ||
this.size = binDict.getMaxId() + 1; | ||
offsets = new int[this.size + 1]; | ||
for (int i = 0; i < this.size; i++) { | ||
offsets[i + 1] = offsets[i] + binDict.decodeToBinary(i).length(); | ||
} | ||
data = HostMemoryBuffer.allocate(offsets[this.size]); | ||
for (int i = 0; i < this.size; i++) { | ||
byte[] ba = binDict.decodeToBinary(i).getBytes(); | ||
data.setBytes(offsets[i], ba, 0, ba.length); | ||
} | ||
} | ||
|
||
public HostMemoryBuffer getData() { | ||
return data; | ||
} | ||
|
||
public int[] getOffsets() { | ||
return offsets; | ||
} | ||
|
||
@Override | ||
public int getMaxId() { | ||
return this.size; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (data != null) { | ||
data.close(); | ||
} | ||
} | ||
|
||
private final int size; | ||
private final int[] offsets; | ||
private final HostMemoryBuffer data; | ||
|
||
} |
This file contains 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
This file contains 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
Oops, something went wrong.