Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -191,7 +191,8 @@ object QueryPlanSerde extends Logging with CometExprShim {
classOf[Log2] -> CometLog2,
classOf[Pow] -> CometScalarFunction[Pow]("pow"),
classOf[If] -> CometIf,
classOf[CaseWhen] -> CometCaseWhen)
classOf[CaseWhen] -> CometCaseWhen,
classOf[Coalesce] -> CometCoalesce)

/**
* Mapping of Spark aggregate expression class to Comet expression handler.
Expand Down Expand Up @@ -1078,10 +1079,6 @@ object QueryPlanSerde extends Logging with CometExprShim {
None
}

case a @ Coalesce(_) =>
val exprChildren = a.children.map(exprToProtoInternal(_, inputs, binding))
scalarFunctionExprToProto("coalesce", exprChildren: _*)

// With Spark 3.4, CharVarcharCodegenUtils.readSidePadding gets called to pad spaces for
// char types.
// See https://github.com/apache/spark/pull/38151
Expand Down
47 changes: 46 additions & 1 deletion spark/src/main/scala/org/apache/comet/serde/conditional.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package org.apache.comet.serde

import scala.collection.JavaConverters._

import org.apache.spark.sql.catalyst.expressions.{Attribute, CaseWhen, Expression, If}
import org.apache.spark.sql.catalyst.expressions.{Attribute, CaseWhen, Coalesce, Expression, If, IsNotNull}

import org.apache.comet.CometSparkSessionExtensions.withInfo
import org.apache.comet.serde.QueryPlanSerde.exprToProtoInternal
Expand Down Expand Up @@ -91,3 +91,48 @@ object CometCaseWhen extends CometExpressionSerde[CaseWhen] {
}
}
}

object CometCoalesce extends CometExpressionSerde[Coalesce] {
override def convert(
expr: Coalesce,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val branches = expr.children.dropRight(1).map { child =>
(IsNotNull(child), child)
}
val elseValue = Some(expr.children.last)
var allBranches: Seq[Expression] = Seq()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allBranches seems to be redundant. It ends up being populated with the same contents as branches, but in a different order. It is then only used in a withInfo call when falling back to Spark, so the order there shouldn't matter. I think we could just use branches there instead.

val whenSeq = branches.map(elements => {
allBranches = allBranches :+ elements._1
exprToProtoInternal(elements._1, inputs, binding)
})
val thenSeq = branches.map(elements => {
allBranches = allBranches :+ elements._2
exprToProtoInternal(elements._2, inputs, binding)
})
assert(whenSeq.length == thenSeq.length)
if (whenSeq.forall(_.isDefined) && thenSeq.forall(_.isDefined)) {
val builder = ExprOuterClass.CaseWhen.newBuilder()
builder.addAllWhen(whenSeq.map(_.get).asJava)
builder.addAllThen(thenSeq.map(_.get).asJava)
if (elseValue.isDefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elseValue is explicitly set to Some earlier, so the condition elseValue.isDefined is always true. I don't think that elseValue needs to be an Option?

val elseValueExpr =
exprToProtoInternal(elseValue.get, inputs, binding)
if (elseValueExpr.isDefined) {
builder.setElseExpr(elseValueExpr.get)
} else {
withInfo(expr, elseValue.get)
return None
}
}
Some(
ExprOuterClass.Expr
.newBuilder()
.setCaseWhen(builder)
.build())
} else {
withInfo(expr, allBranches: _*)
None
}
}
}
14 changes: 14 additions & 0 deletions spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}

test("test coalesce lazy eval") {
withSQLConf(
SQLConf.ANSI_ENABLED.key -> "true",
CometConf.COMET_CAST_ALLOW_INCOMPATIBLE.key -> "true") {
val data = Seq((9999999999999L, 0))
withParquetTable(data, "t1") {
val res = spark.sql("""
|SELECT coalesce(_1, CAST(_1 AS TINYINT)) from t1;
| """.stripMargin)
checkSparkAnswerAndOperator(res)
}
}
}

test("dictionary arithmetic") {
// TODO: test ANSI mode
withSQLConf(SQLConf.ANSI_ENABLED.key -> "false", "parquet.enable.dictionary" -> "true") {
Expand Down
Loading