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
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ object CudfBinaryExpression {
BinaryOp.LESS -> ast.BinaryOperator.LESS,
jlowe marked this conversation as resolved.
Show resolved Hide resolved
BinaryOp.LESS_EQUAL -> ast.BinaryOperator.LESS_EQUAL,
BinaryOp.LOGICAL_AND -> ast.BinaryOperator.NULL_LOGICAL_AND,
BinaryOp.NULL_LOGICAL_AND -> ast.BinaryOperator.NULL_LOGICAL_AND,
jlowe marked this conversation as resolved.
Show resolved Hide resolved
BinaryOp.LOGICAL_OR -> ast.BinaryOperator.NULL_LOGICAL_OR,
BinaryOp.NULL_LOGICAL_OR -> ast.BinaryOperator.NULL_LOGICAL_OR,
BinaryOp.MUL -> ast.BinaryOperator.MUL,
BinaryOp.POW -> ast.BinaryOperator.POW,
BinaryOp.SUB -> ast.BinaryOperator.SUB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.nvidia.spark.rapids

import ai.rapids.cudf.{ColumnVector, NullPolicy, Scalar, ScanAggregation, ScanType, Table, UnaryOp}
import ai.rapids.cudf.ast
import ai.rapids.cudf.{BinaryOp, ColumnVector, DType, NullPolicy, Scalar, ScanAggregation, ScanType, Table, UnaryOp}
import com.nvidia.spark.rapids.RapidsPluginImplicits._
import com.nvidia.spark.rapids.shims.v2.ShimExpression

Expand Down Expand Up @@ -441,15 +440,8 @@ case class GpuCaseWhen(
cumulativePred match {
case Some(prev) =>
withResource(prev) { _ =>
val result = withResource(new Table(prev.getBase, whenBool.getBase)) { t =>
val or = new ast.BinaryOperation(ast.BinaryOperator.NULL_LOGICAL_OR,
new ast.ColumnReference(0),
new ast.ColumnReference(1)
)
withResource(or.compile()) {
_.computeColumn(t)
}
}
val result = prev.getBase.binaryOp(BinaryOp.NULL_LOGICAL_OR,
whenBool.getBase, DType.BOOL8)
GpuColumnVector.from(result, DataTypes.BooleanType)
}
case _ =>
Expand Down
145 changes: 8 additions & 137 deletions sql-plugin/src/main/scala/org/apache/spark/sql/rapids/predicates.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
* Copyright (c) 2019-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down 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 All @@ -216,7 +87,7 @@ abstract class CudfBinaryComparison extends CudfBinaryOperator with Predicate {
case failure => failure
}

def hasFloatingPointInputs = left.dataType == FloatType || left.dataType == DoubleType ||
def hasFloatingPointInputs: Boolean = left.dataType == FloatType || left.dataType == DoubleType ||
right.dataType == FloatType || right.dataType == DoubleType
}

Expand Down Expand Up @@ -246,8 +117,8 @@ case class GpuEqualTo(left: Expression, right: Expression) extends CudfBinaryCom
val result = super.doColumnar(lhs, rhs)
if (hasFloatingPointInputs) {
withResource(result) { result =>
withResource(lhs.getBase.isNan()) { lhsNan =>
withResource(rhs.getBase.isNan()) { rhsNan =>
withResource(lhs.getBase.isNan) { lhsNan =>
withResource(rhs.getBase.isNan) { rhsNan =>
withResource(lhsNan.and(rhsNan)) { lhsNanAndRhsNan =>
lhsNanAndRhsNan.or(result)
}
Expand All @@ -264,7 +135,7 @@ case class GpuEqualTo(left: Expression, right: Expression) extends CudfBinaryCom
if (hasFloatingPointInputs) {
withResource(result) { result =>
withResource(Scalar.fromBool(lhs.isNan)) { lhsNan =>
withResource(rhs.getBase.isNan()) { rhsNan =>
withResource(rhs.getBase.isNan) { rhsNan =>
withResource(lhsNan.and(rhsNan)) { lhsNanAndRhsNan =>
lhsNanAndRhsNan.or(result)
}
Expand Down Expand Up @@ -348,7 +219,7 @@ case class GpuEqualNullSafe(left: Expression, right: Expression) extends CudfBin
val result = super.doColumnar(lhs, rhs)
if (hasFloatingPointInputs) {
withResource(result) { result =>
withResource(lhs.getBase.isNan()) { lhsNan =>
withResource(lhs.getBase.isNan) { lhsNan =>
withResource(Scalar.fromBool(rhs.isNan)) { rhsNan =>
withResource(lhsNan.and(rhsNan)) { lhsNanAndRhsNan =>
lhsNanAndRhsNan.or(result)
Expand Down