-
Notifications
You must be signed in to change notification settings - Fork 240
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
Add in support for explode on maps #3175
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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
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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,7 +23,7 @@ import ai.rapids.cudf.{ColumnVector, ContiguousTable, NvtxColor, Table} | |||||
import org.apache.spark.TaskContext | ||||||
import org.apache.spark.rdd.RDD | ||||||
import org.apache.spark.sql.catalyst.InternalRow | ||||||
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, CreateArray, Expression, Generator} | ||||||
import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeSet, Expression, Generator} | ||||||
import org.apache.spark.sql.catalyst.plans.physical.Partitioning | ||||||
import org.apache.spark.sql.execution.{GenerateExec, SparkPlan, UnaryExecNode} | ||||||
import org.apache.spark.sql.rapids.GpuCreateArray | ||||||
|
@@ -228,24 +228,121 @@ abstract class GpuExplodeBase extends GpuUnevaluableUnaryExpression with GpuGene | |||||
|
||||||
// Infer result schema of GenerateExec from input schema | ||||||
protected def resultSchema(inputSchema: Array[DataType], | ||||||
genOffset: Int, | ||||||
includePos: Boolean = false): Array[DataType] = { | ||||||
genOffset: Int): Array[DataType] = { | ||||||
val outputSchema = ArrayBuffer[DataType]() | ||||||
inputSchema.zipWithIndex.foreach { | ||||||
// extract output type of explode from input ArrayData | ||||||
case (dataType, index) if index == genOffset => | ||||||
require(dataType.isInstanceOf[ArrayType], "GpuExplode only supports ArrayData now") | ||||||
if (includePos) { | ||||||
if (position) { | ||||||
outputSchema += IntegerType | ||||||
} | ||||||
outputSchema += dataType.asInstanceOf[ArrayType].elementType | ||||||
dataType match { | ||||||
case ArrayType(elementType, _) => | ||||||
outputSchema += elementType | ||||||
case MapType(keyType, valueType, _) => | ||||||
outputSchema += keyType | ||||||
outputSchema += valueType | ||||||
} | ||||||
// map types of other required columns | ||||||
case (dataType, _) => | ||||||
outputSchema += dataType | ||||||
} | ||||||
outputSchema.toArray | ||||||
} | ||||||
|
||||||
/** | ||||||
* A function that will do the explode or position explode | ||||||
*/ | ||||||
private[this] def explodeFun(inputTable: Table, genOffset: Int, outer: Boolean): Table = { | ||||||
if (position) { | ||||||
if (outer) { | ||||||
inputTable.explodeOuterPosition(genOffset) | ||||||
} else { | ||||||
inputTable.explodePosition(genOffset) | ||||||
} | ||||||
} else { | ||||||
if (outer) { | ||||||
inputTable.explodeOuter(genOffset) | ||||||
} else { | ||||||
inputTable.explode(genOffset) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
override def generate(inputBatch: ColumnarBatch, | ||||||
generatorOffset: Int, | ||||||
outer: Boolean): ColumnarBatch = { | ||||||
|
||||||
require(inputBatch.numCols() - 1 == generatorOffset, | ||||||
s"Internal Error ${getClass.getSimpleName} supports one and only one input attribute.") | ||||||
val schema = resultSchema(GpuColumnVector.extractTypes(inputBatch), generatorOffset) | ||||||
|
||||||
withResource(GpuColumnVector.from(inputBatch)) { table => | ||||||
withResource(explodeFun(table, generatorOffset, outer)) { exploded => | ||||||
child.dataType match { | ||||||
case _: ArrayType => | ||||||
GpuColumnVector.from(exploded, schema) | ||||||
case MapType(kt, vt, _) => | ||||||
// We need to pull the key and value of of the struct column | ||||||
withResource(convertMapOutput(exploded, generatorOffset, kt, vt, outer)) { fixed => | ||||||
GpuColumnVector.from(fixed, schema) | ||||||
} | ||||||
case other => | ||||||
throw new IllegalArgumentException( | ||||||
s"$other is not supported as explode input right now") | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
private[this] def convertMapOutput(exploded: Table, | ||||||
genOffset: Int, | ||||||
kt: DataType, | ||||||
vt: DataType, | ||||||
fixChildValidity: Boolean): Table = { | ||||||
val numPos = if (position) 1 else 0 | ||||||
// scalastyle:off line.size.limit | ||||||
// The input will look like the following, and we just want to expand the key, value in the | ||||||
// struct into separate columns | ||||||
// INDEX [0, genOffset)| genOffset | genOffset + numPos | [genOffset + numPos + 1, exploded.getNumberOfColumns) | ||||||
// SOME INPUT COLUMNS | POS COLUMN? | STRUCT(KEY, VALUE) | MORE INPUT COLUMNS | ||||||
// scalastyle:on line.size.limit | ||||||
val structPos = genOffset + numPos | ||||||
withResource (ArrayBuffer.empty[ColumnVector] ) { newColumns => | ||||||
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. Nit: some odd extra whitespace here.
Suggested change
|
||||||
(0 until exploded.getNumberOfColumns).foreach { index => | ||||||
if (index == structPos) { | ||||||
val kvStructCol = exploded.getColumn(index) | ||||||
if (fixChildValidity) { | ||||||
// TODO once explode outer is fixed remove the following workaround | ||||||
// https://github.com/rapidsai/cudf/issues/9003 | ||||||
withResource(kvStructCol.isNull) { isNull => | ||||||
newColumns += withResource(kvStructCol.getChildColumnView(0)) { keyView => | ||||||
withResource(GpuScalar.from(null, kt)) { nullKey => | ||||||
isNull.ifElse(nullKey, keyView) | ||||||
} | ||||||
} | ||||||
newColumns += withResource(kvStructCol.getChildColumnView(1)) { valueView => | ||||||
withResource(GpuScalar.from(null, vt)) { nullValue => | ||||||
isNull.ifElse(nullValue, valueView) | ||||||
} | ||||||
} | ||||||
} | ||||||
} else { | ||||||
newColumns += withResource(kvStructCol.getChildColumnView(0)) { keyView => | ||||||
keyView.copyToColumnVector() | ||||||
} | ||||||
newColumns += withResource(kvStructCol.getChildColumnView(1)) { valueView => | ||||||
valueView.copyToColumnVector() | ||||||
} | ||||||
} | ||||||
} else { | ||||||
newColumns += exploded.getColumn(index).incRefCount() | ||||||
} | ||||||
} | ||||||
new Table(newColumns: _*) | ||||||
} | ||||||
} | ||||||
|
||||||
override def fixedLenLazyExpressions: Seq[Expression] = child match { | ||||||
// GpuLiteral of ArrayData will be converted to GpuCreateArray with GpuLiterals | ||||||
case GpuCreateArray(expressions, _) => expressions | ||||||
|
@@ -332,45 +429,10 @@ abstract class GpuExplodeBase extends GpuUnevaluableUnaryExpression with GpuGene | |||||
} | ||||||
|
||||||
case class GpuExplode(child: Expression) extends GpuExplodeBase { | ||||||
|
||||||
override def generate(inputBatch: ColumnarBatch, | ||||||
generatorOffset: Int, | ||||||
outer: Boolean): ColumnarBatch = { | ||||||
|
||||||
require(inputBatch.numCols() - 1 == generatorOffset, | ||||||
"Internal Error GpuExplode supports one and only one input attribute.") | ||||||
val schema = resultSchema(GpuColumnVector.extractTypes(inputBatch), generatorOffset) | ||||||
val explodeFun = (t: Table) => | ||||||
if (outer) t.explodeOuter(generatorOffset) else t.explode(generatorOffset) | ||||||
withResource(GpuColumnVector.from(inputBatch)) { table => | ||||||
withResource(explodeFun(table)) { exploded => | ||||||
GpuColumnVector.from(exploded, schema) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
override val position: Boolean = false | ||||||
} | ||||||
|
||||||
case class GpuPosExplode(child: Expression) extends GpuExplodeBase { | ||||||
|
||||||
override def generate(inputBatch: ColumnarBatch, | ||||||
generatorOffset: Int, | ||||||
outer: Boolean): ColumnarBatch = { | ||||||
|
||||||
require(inputBatch.numCols() - 1 == generatorOffset, | ||||||
"Internal Error GpuPosExplode supports one and only one input attribute.") | ||||||
val schema = resultSchema( | ||||||
GpuColumnVector.extractTypes(inputBatch), generatorOffset, includePos = true) | ||||||
val explodePosFun = (t: Table) => | ||||||
if (outer) t.explodeOuterPosition(generatorOffset) else t.explodePosition(generatorOffset) | ||||||
withResource(GpuColumnVector.from(inputBatch)) { table => | ||||||
withResource(explodePosFun(table)) { exploded => | ||||||
GpuColumnVector.from(exploded, schema) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
override def position: Boolean = true | ||||||
} | ||||||
|
||||||
|
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
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.