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
37 changes: 33 additions & 4 deletions spark/src/main/scala/org/apache/comet/serde/arrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -546,26 +546,55 @@ object CometArrayFilter extends CometExpressionSerde[ArrayFilter] {
object CometSize extends CometExpressionSerde[Size] {

override def getSupportLevel(expr: Size): SupportLevel = {
// TODO respect spark.sql.legacy.sizeOfNull
expr.child.dataType match {
case _: ArrayType => Compatible()
case _: MapType => Unsupported(Some("size does not support map inputs"))
case other =>
// this should be unreachable because Spark only supports map and array inputs
Unsupported(Some(s"Unsupported child data type: $other"))
}

}

override def convert(
expr: Size,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
val arrayExprProto = exprToProto(expr.child, inputs, binding)
for {
isNotNullExprProto <- createIsNotNullExprProto(expr, inputs, binding)
sizeScalarExprProto <- scalarFunctionExprToProto("size", arrayExprProto)
emptyLiteralExprProto <- createLiteralExprProto(SQLConf.get.legacySizeOfNull)
} yield {
val caseWhenExpr = ExprOuterClass.CaseWhen
.newBuilder()
.addWhen(isNotNullExprProto)
.addThen(sizeScalarExprProto)
.setElseExpr(emptyLiteralExprProto)
.build()
ExprOuterClass.Expr
.newBuilder()
.setCaseWhen(caseWhenExpr)
.build()
}
}

private def createIsNotNullExprProto(
expr: Size,
inputs: Seq[Attribute],
binding: Boolean): Option[ExprOuterClass.Expr] = {
createUnaryExpr(
expr,
expr.child,
inputs,
binding,
(builder, unaryExpr) => builder.setIsNotNull(unaryExpr))
}

val sizeScalarExpr = scalarFunctionExprToProto("size", arrayExprProto)
optExprWithInfo(sizeScalarExpr, expr)
private def createLiteralExprProto(legacySizeOfNull: Boolean): Option[ExprOuterClass.Expr] = {
val value = if (legacySizeOfNull) -1 else null
exprToProto(Literal(value, IntegerType), Seq.empty)
}

}

trait ArraysBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.sql.CometTestBase
import org.apache.spark.sql.catalyst.expressions.{ArrayAppend, ArrayDistinct, ArrayExcept, ArrayInsert, ArrayIntersect, ArrayJoin, ArrayRepeat, ArraysOverlap, ArrayUnion}
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.ArrayType

import org.apache.comet.CometSparkSessionExtensions.{isSpark35Plus, isSpark40Plus}
Expand Down Expand Up @@ -871,4 +872,22 @@ class CometArrayExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelp
}
}
}

test("size - respect to legacySizeOfNull") {
val table = "t1"
withSQLConf(CometConf.COMET_NATIVE_SCAN_IMPL.key -> CometConf.SCAN_NATIVE_ICEBERG_COMPAT) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to make sure the ansi mode is off here. According to Spark

This function returns -1 for null input only if spark.sql.ansi.enabled is false and spark.sql.legacy.sizeOfNull is true. Otherwise, it returns null for null input. With the default settings, the function returns null for null input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @comphead. ANSI mode is disabled by default, but for safety, I've explicitly added the setting.

withTable(table) {
sql(s"create table $table(col array<string>) using parquet")
sql(s"insert into $table values(null)")
withSQLConf(SQLConf.LEGACY_SIZE_OF_NULL.key -> "false") {
checkSparkAnswerAndOperator(sql(s"select size(col) from $table"))
}
withSQLConf(
SQLConf.LEGACY_SIZE_OF_NULL.key -> "true",
SQLConf.ANSI_ENABLED.key -> "false") {
checkSparkAnswerAndOperator(sql(s"select size(col) from $table"))
}
}
}
}
}
Loading