Skip to content
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

Switch and/or to use new cudf binops to improve performance #4501

Merged
merged 4 commits into from
Jan 21, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 2 additions & 131 deletions sql-plugin/src/main/scala/org/apache/spark/sql/rapids/predicates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,81 +56,14 @@ case class GpuNot(child: Expression) extends CudfUnaryExpression
}
revans2 marked this conversation as resolved.
Show resolved Hide resolved
}

object GpuLogicHelper {
def eqNullAware(lhs: GpuScalar, rhs: Boolean): Boolean =
lhs.isValid && (lhs.getValue.asInstanceOf[Boolean] == rhs)
}

case class GpuAnd(left: Expression, right: Expression) extends CudfBinaryOperator with Predicate {
override def inputType: AbstractDataType = BooleanType

override def symbol: String = "&&"

override def sqlOperator: String = "AND"

override def binaryOp: BinaryOp = BinaryOp.LOGICAL_AND

import GpuLogicHelper._

// The CUDF implementation of `and` will return a null if either input is null
// Spark does not.
// |LHS/RHS| TRUE | FALSE | NULL |
// |TRUE | TRUE | FALSE | NULL |
// |FALSE | FALSE | FALSE | FALSE |
// |NULL | NULL | FALSE | NULL |
// So we have to make some adjustments.
// IF (A <=> FALSE, FALSE,
// IF (B <=> FALSE, FALSE,
// A cudf_and B))

override def doColumnar(lhs: GpuColumnVector, rhs: GpuColumnVector): ColumnVector = {
val l = lhs.getBase
val r = rhs.getBase
withResource(Scalar.fromBool(false)) { falseVal =>
val firstPass = withResource(l.and(r)) { lAndR =>
withResource(l.equalToNullAware(falseVal)) { lIsFalse =>
lIsFalse.ifElse(falseVal, lAndR)
}
}
withResource(firstPass) { firstPass =>
withResource(r.equalToNullAware(falseVal)) { rIsFalse =>
rIsFalse.ifElse(falseVal, firstPass)
}
}
}
}

override def doColumnar(lhs: GpuScalar, rhs: GpuColumnVector): ColumnVector = {
val l = lhs.getBase
val r = rhs.getBase
withResource(Scalar.fromBool(false)) { falseVal =>
if (eqNullAware(lhs, false)) {
ColumnVector.fromScalar(falseVal, r.getRowCount.toInt)
} else {
withResource(l.and(r)) { lAndR =>
withResource(r.equalToNullAware(falseVal)) { rIsFalse =>
rIsFalse.ifElse(falseVal, lAndR)
}
}
}
}
}

override def doColumnar(lhs: GpuColumnVector, rhs: GpuScalar): ColumnVector = {
val l = lhs.getBase
val r = rhs.getBase
withResource(Scalar.fromBool(false)) { falseVal =>
if (eqNullAware(rhs, false)) {
ColumnVector.fromScalar(falseVal, l.getRowCount.toInt)
} else {
withResource(l.and(r)) { lAndR =>
withResource(l.equalToNullAware(falseVal)) { lIsFalse =>
lIsFalse.ifElse(falseVal, lAndR)
}
}
}
}
}
override def binaryOp: BinaryOp = BinaryOp.NULL_LOGICAL_AND
}

case class GpuOr(left: Expression, right: Expression) extends CudfBinaryOperator with Predicate {
Expand All @@ -140,69 +73,7 @@ case class GpuOr(left: Expression, right: Expression) extends CudfBinaryOperator

override def sqlOperator: String = "OR"

override def binaryOp: BinaryOp = BinaryOp.LOGICAL_OR

import GpuLogicHelper._

// The CUDF implementation of `or` will return a null if either input is null
// Spark does not.
// |LHS/RHS| TRUE | FALSE | NULL |
// |TRUE | TRUE | TRUE | TRUE |
// |FALSE | TRUE | FALSE | NULL |
// |NULL | TRUE | NULL | NULL |
// So we have to make some adjustments.
// IF (A <=> TRUE, TRUE,
// IF (B <=> TRUE, TRUE,
// A cudf_or B))

override def doColumnar(lhs: GpuColumnVector, rhs: GpuColumnVector): ColumnVector = {
val l = lhs.getBase
val r = rhs.getBase
withResource(Scalar.fromBool(true)) { trueVal =>
val firstPass = withResource(l.or(r)) { lOrR =>
withResource(l.equalToNullAware(trueVal)) { lIsTrue =>
lIsTrue.ifElse(trueVal, lOrR)
}
}
withResource(firstPass) { firstPass =>
withResource(r.equalToNullAware(trueVal)) { rIsTrue =>
rIsTrue.ifElse(trueVal, firstPass)
}
}
}
}

override def doColumnar(lhs: GpuScalar, rhs: GpuColumnVector): ColumnVector = {
val l = lhs.getBase
val r = rhs.getBase
withResource(Scalar.fromBool(true)) { trueVal =>
if (eqNullAware(lhs, true)) {
ColumnVector.fromScalar(trueVal, r.getRowCount.toInt)
} else {
withResource(l.or(r)) { lOrR =>
withResource(r.equalToNullAware(trueVal)) { rIsTrue =>
rIsTrue.ifElse(trueVal, lOrR)
}
}
}
}
}

override def doColumnar(lhs: GpuColumnVector, rhs: GpuScalar): ColumnVector = {
val l = lhs.getBase
val r = rhs.getBase
withResource(Scalar.fromBool(true)) { trueVal =>
if (eqNullAware(rhs, true)) {
ColumnVector.fromScalar(trueVal, l.getRowCount.toInt)
} else {
withResource(l.or(r)) { lOrR =>
withResource(l.equalToNullAware(trueVal)) { lIsFalse =>
lIsFalse.ifElse(trueVal, lOrR)
}
}
}
}
}
override def binaryOp: BinaryOp = BinaryOp.NULL_LOGICAL_OR
}

abstract class CudfBinaryComparison extends CudfBinaryOperator with Predicate {
Expand Down