Skip to content

Commit 9ad865a

Browse files
committed
fix: fall back to Spark for MapSort with unsupported key types
Arrow's sort_to_indices does not support Struct (and other complex) key types, so map_sort fails at runtime when the map key is a struct. Check key type via supportedScalarSortElementType and fall back to Spark when the key type is not natively sortable. This fixes 4 CollationSuite failures in spark-sql-auto-sql_core-1 for Spark 4.0: 'Group by on map containing structs with ...'.
1 parent 0480e82 commit 9ad865a

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

spark/src/main/spark-4.0/org/apache/comet/shims/CometExprShim.scala

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import org.apache.spark.sql.catalyst.expressions.json.StructsToJsonEvaluator
2424
import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, StaticInvoke}
2525
import org.apache.spark.sql.internal.SQLConf
2626
import org.apache.spark.sql.internal.types.StringTypeWithCollation
27-
import org.apache.spark.sql.types.{BinaryType, BooleanType, DataTypes, StringType}
27+
import org.apache.spark.sql.types.{BinaryType, BooleanType, DataTypes, MapType, StringType}
2828

2929
import org.apache.comet.CometSparkSessionExtensions.withInfo
3030
import org.apache.comet.expressions.{CometCast, CometEvalMode}
3131
import org.apache.comet.serde.{CommonStringExprs, Compatible, ExprOuterClass, Incompatible}
3232
import org.apache.comet.serde.ExprOuterClass.{BinaryOutputStyle, Expr}
33-
import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, optExprWithInfo, scalarFunctionExprToProto, scalarFunctionExprToProtoWithReturnType}
33+
import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, optExprWithInfo, scalarFunctionExprToProto, scalarFunctionExprToProtoWithReturnType, supportedScalarSortElementType}
3434

3535
/**
3636
* `CometExprShim` acts as a shim for parsing expressions from different Spark versions.
@@ -129,13 +129,19 @@ trait CometExprShim extends CommonStringExprs {
129129
}
130130

131131
case ms: MapSort =>
132-
val childExpr = exprToProtoInternal(ms.child, inputs, binding)
133-
val mapSortExpr = scalarFunctionExprToProtoWithReturnType(
134-
"map_sort",
135-
ms.dataType,
136-
failOnError = false,
137-
childExpr)
138-
optExprWithInfo(mapSortExpr, ms, ms.child)
132+
val keyType = ms.dataType.asInstanceOf[MapType].keyType
133+
if (!supportedScalarSortElementType(keyType)) {
134+
withInfo(ms, s"MapSort on map with key type $keyType is not supported")
135+
None
136+
} else {
137+
val childExpr = exprToProtoInternal(ms.child, inputs, binding)
138+
val mapSortExpr = scalarFunctionExprToProtoWithReturnType(
139+
"map_sort",
140+
ms.dataType,
141+
failOnError = false,
142+
childExpr)
143+
optExprWithInfo(mapSortExpr, ms, ms.child)
144+
}
139145

140146
case _ => None
141147
}

0 commit comments

Comments
 (0)