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
17 changes: 10 additions & 7 deletions spark/src/main/scala/org/apache/spark/sql/comet/operators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
import org.apache.spark.sql.execution.joins.{BroadcastHashJoinExec, HashJoin, ShuffledHashJoinExec, SortMergeJoinExec}
import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{ArrayType, BooleanType, ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, MapType, ShortType, StringType, TimestampNTZType}
import org.apache.spark.sql.types.{ArrayType, BooleanType, ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, MapType, ShortType, StringType, StructType, TimestampNTZType}
import org.apache.spark.sql.vectorized.ColumnarBatch
import org.apache.spark.util.SerializableConfiguration
import org.apache.spark.util.io.ChunkedByteBuffer
Expand Down Expand Up @@ -1350,6 +1350,13 @@ case class CometUnionExec(

trait CometBaseAggregate {

private def containsMapType(dt: DataType): Boolean = dt match {
case _: MapType => true
case StructType(fields) => fields.exists(f => containsMapType(f.dataType))
case ArrayType(elementType, _) => containsMapType(elementType)
case _ => false
}

def doConvert(
aggregate: BaseAggregateExec,
builder: Operator.Builder,
Expand Down Expand Up @@ -1377,12 +1384,8 @@ trait CometBaseAggregate {
return None
}

if (groupingExpressions.exists(expr =>
expr.dataType match {
case _: MapType => true
case _ => false
})) {
withInfo(aggregate, "Grouping on map types is not supported")
if (groupingExpressions.exists(expr => containsMapType(expr.dataType))) {
withInfo(aggregate, "Grouping on map-containing types is not supported")
return None
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,32 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}

test("grouping on struct containing map should fallback to Spark") {
withSQLConf(
CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true",
CometConf.COMET_EXEC_SHUFFLE_ENABLED.key -> "true") {
val query =
"""SELECT col1.data['key']
|FROM VALUES
| (NAMED_STRUCT('data', MAP('key', 'value', 'num', '42'))),
| (NAMED_STRUCT('data', MAP('key', 'other', 'num', '7')))
|t (col1)
|GROUP BY col1
|HAVING col1.data['num'] IS NOT NULL
|ORDER BY col1.data['key']
|""".stripMargin

val (_, cometPlan) =
checkSparkAnswerAndFallbackReason(
query,
"Grouping on map-containing types is not supported")

assert(
stripAQEPlan(cometPlan).collect { case s: CometHashAggregateExec => s }.isEmpty,
"Expected aggregate to fall back to Spark for grouping on Struct(Map(...))")
}
}

test("simple SUM, COUNT, MIN, MAX, AVG with non-distinct + null group keys") {
Seq(true, false).foreach { dictionaryEnabled =>
withParquetTable(
Expand Down Expand Up @@ -1990,4 +2016,19 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
sparkPlan.collect { case s: CometHashAggregateExec => s }.size
}

test("group by array of map falls back to Spark (issue #4123)") {
withSQLConf(CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true") {
checkSparkAnswerAndFallbackReason(
"""SELECT a, COUNT(*)
|FROM VALUES
| (ARRAY(MAP('x', 10))),
| (ARRAY(MAP('y', 20))),
| (ARRAY(MAP('x', 10)))
|t (a)
|GROUP BY a
|""".stripMargin,
"Grouping on map-containing types is not supported")
}
}

}
Loading