-
Notifications
You must be signed in to change notification settings - Fork 235
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
Support multi string contains [databricks] #11413
Open
res-life
wants to merge
4
commits into
NVIDIA:branch-24.12
Choose a base branch
from
res-life:multi-string-contians
base: branch-24.12
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.
Open
Changes from all 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
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,6 +20,8 @@ import java.nio.charset.Charset | |||||||||||||
import java.text.DecimalFormatSymbols | ||||||||||||||
import java.util.{Locale, Optional} | ||||||||||||||
|
||||||||||||||
import scala.annotation.tailrec | ||||||||||||||
import scala.collection.mutable | ||||||||||||||
import scala.collection.mutable.ArrayBuffer | ||||||||||||||
|
||||||||||||||
import ai.rapids.cudf.{BinaryOp, BinaryOperable, CaptureGroups, ColumnVector, ColumnView, DType, PadSide, RegexProgram, RoundMode, Scalar} | ||||||||||||||
|
@@ -32,6 +34,7 @@ import com.nvidia.spark.rapids.jni.RegexRewriteUtils | |||||||||||||
import com.nvidia.spark.rapids.shims.{ShimExpression, SparkShimImpl} | ||||||||||||||
|
||||||||||||||
import org.apache.spark.sql.catalyst.expressions._ | ||||||||||||||
import org.apache.spark.sql.rapids.catalyst.expressions._ | ||||||||||||||
import org.apache.spark.sql.types._ | ||||||||||||||
import org.apache.spark.sql.vectorized.ColumnarBatch | ||||||||||||||
import org.apache.spark.unsafe.types.UTF8String | ||||||||||||||
|
@@ -391,7 +394,8 @@ case class GpuContains(left: Expression, right: Expression) | |||||||||||||
extends GpuBinaryExpressionArgsAnyScalar | ||||||||||||||
with Predicate | ||||||||||||||
with ImplicitCastInputTypes | ||||||||||||||
with NullIntolerant { | ||||||||||||||
with NullIntolerant | ||||||||||||||
with GpuCombinable { | ||||||||||||||
|
||||||||||||||
override def inputTypes: Seq[DataType] = Seq(StringType) | ||||||||||||||
|
||||||||||||||
|
@@ -411,6 +415,105 @@ case class GpuContains(left: Expression, right: Expression) | |||||||||||||
doColumnar(expandedLhs, rhs) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Get a combiner that can be used to find candidates to combine | ||||||||||||||
*/ | ||||||||||||||
override def getCombiner(): GpuExpressionCombiner = new ContainsCombiner(this) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
case class GpuMultiContains(left: Expression, targets: Seq[UTF8String], output: StructType) | ||||||||||||||
extends GpuExpression with ShimExpression { | ||||||||||||||
|
||||||||||||||
override def otherCopyArgs: Seq[AnyRef] = Nil | ||||||||||||||
|
||||||||||||||
override def dataType: DataType = output | ||||||||||||||
|
||||||||||||||
override def nullable: Boolean = false | ||||||||||||||
|
||||||||||||||
override def prettyName: String = "multi_contains" | ||||||||||||||
|
||||||||||||||
override def columnarEval(batch: ColumnarBatch): GpuColumnVector = { | ||||||||||||||
val targetsBytes = targets.map(t => t.getBytes).toArray | ||||||||||||||
withResource(ColumnVector.fromUTF8Strings(targetsBytes: _*)) { targetsCv => | ||||||||||||||
withResource(left.columnarEval(batch)) { lhs => | ||||||||||||||
withResource(lhs.getBase.stringContains(targetsCv)) { boolCvs => | ||||||||||||||
val retView = ColumnView.makeStructView(batch.numRows(), boolCvs: _*) | ||||||||||||||
GpuColumnVector.from(retView.copyToColumnVector(), dataType) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
override def children: Seq[Expression] = Seq(left) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
class ContainsCombiner(private val exp: GpuContains) extends GpuExpressionCombiner { | ||||||||||||||
private var outputLocation = 0 | ||||||||||||||
/** | ||||||||||||||
* A mapping between an expression and where in the output struct of | ||||||||||||||
* the MultiGetJsonObject will the output be. | ||||||||||||||
*/ | ||||||||||||||
private val toCombine = mutable.HashMap.empty[GpuExpressionEquals, Int] | ||||||||||||||
addExpression(exp) | ||||||||||||||
|
||||||||||||||
override def toString: String = s"ContainsCombiner $toCombine" | ||||||||||||||
|
||||||||||||||
override def hashCode: Int = { | ||||||||||||||
// We already know that we are Contains, and what we can combine is based | ||||||||||||||
// on the string column being the same. | ||||||||||||||
"Contains".hashCode + (exp.left.semanticHash() * 17) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* only combine when targets are literals | ||||||||||||||
*/ | ||||||||||||||
override def equals(o: Any): Boolean = o match { | ||||||||||||||
case other: ContainsCombiner => exp.left.semanticEquals(other.exp.left) && | ||||||||||||||
exp.right.isInstanceOf[GpuLiteral] && other.exp.right.isInstanceOf[GpuLiteral] | ||||||||||||||
Comment on lines
+471
to
+473
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: if you make ContainsCombiner a case class you can use pattern matching instead of manual instanceof checks:
Suggested change
|
||||||||||||||
case _ => false | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
override def addExpression(e: Expression): Unit = { | ||||||||||||||
val localOutputLocation = outputLocation | ||||||||||||||
outputLocation += 1 | ||||||||||||||
val key = GpuExpressionEquals(e) | ||||||||||||||
if (!toCombine.contains(key)) { | ||||||||||||||
toCombine.put(key, localOutputLocation) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
override def useCount: Int = toCombine.size | ||||||||||||||
|
||||||||||||||
private def fieldName(id: Int): String = | ||||||||||||||
s"_mc_$id" | ||||||||||||||
|
||||||||||||||
@tailrec | ||||||||||||||
private def extractLiteral(exp: Expression): GpuLiteral = exp match { | ||||||||||||||
case l: GpuLiteral => l | ||||||||||||||
case a: Alias => extractLiteral(a.child) | ||||||||||||||
case other => throw new RuntimeException("Unsupported expression in contains combiner, " + | ||||||||||||||
"should be a literal type, actual type is " + other.getClass.getName) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private lazy val multiContains: GpuMultiContains = { | ||||||||||||||
val input = toCombine.head._1.e.asInstanceOf[GpuContains].left | ||||||||||||||
val fieldsNPaths = toCombine.toSeq.map { | ||||||||||||||
case (k, id) => | ||||||||||||||
(id, k.e) | ||||||||||||||
}.sortBy(_._1).map { | ||||||||||||||
case (id, e: GpuContains) => | ||||||||||||||
val target = extractLiteral(e.right).value.asInstanceOf[UTF8String] | ||||||||||||||
(StructField(fieldName(id), e.dataType, e.nullable), target) | ||||||||||||||
} | ||||||||||||||
val dt = StructType(fieldsNPaths.map(_._1)) | ||||||||||||||
GpuMultiContains(input, fieldsNPaths.map(_._2), dt) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
override def getReplacementExpression(e: Expression): Expression = { | ||||||||||||||
val localId = toCombine(GpuExpressionEquals(e)) | ||||||||||||||
GpuGetStructField(multiContains, localId, Some(fieldName(localId))) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
case class GpuSubstring(str: Expression, pos: Expression, len: Expression) | ||||||||||||||
|
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.
nit: prefer Python constants
However, the pytest case id will be more readable if, instead of a boolean, parameters are strings
@pytest.mark.parametrize('string_contains_mode', ['multiContains', 'singleContains'], ids=idfn)