Skip to content
Merged
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 @@ -192,7 +192,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 @@ -999,10 +1000,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
41 changes: 40 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,42 @@ 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 = expr.children.last
val whenSeq = branches.map(elements => {
exprToProtoInternal(elements._1, inputs, binding)
})
val thenSeq = branches.map(elements => {
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)
val elseValueExpr = exprToProtoInternal(elseValue, inputs, binding)
if (elseValueExpr.isDefined) {
builder.setElseExpr(elseValueExpr.get)
} else {
withInfo(expr, elseValue)
return None
}
Some(
ExprOuterClass.Expr
.newBuilder()
.setCaseWhen(builder)
.build())
} else {
withInfo(expr, branches.map(_._2): _*)
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") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this test verify that lazy evaluation did, in fact, occur?

Copy link
Contributor Author

@coderfender coderfender Sep 17, 2025

Choose a reason for hiding this comment

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

Good question . We have filed the issue in detail along with steps to reproduce here : apache/datafusion#17322

withSQLConf(
SQLConf.ANSI_ENABLED.key -> "true",
CometConf.COMET_EXPR_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