sam-1112 commented on code in PR #7566: URL: https://github.com/apache/datafusion-comet/pull/7566#discussion_r4739510851
########## spark/src/test/spark-4.x/org/apache/comet/rules/CometAggregateCollationSuite.scala: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.comet.rules + +import scala.jdk.CollectionConverters._ + +import org.apache.spark.sql.{CometTestBase, Row} +import org.apache.spark.sql.catalyst.expressions.aggregate.Partial +import org.apache.spark.sql.comet.CometBaseAggregate +import org.apache.spark.sql.execution.aggregate.BaseAggregateExec +import org.apache.spark.sql.types.{StringType, StructField, StructType} + +import org.apache.comet.CometConf +import org.apache.comet.serde.QueryPlanSerde + +/** + * `canAggregateBeConverted` must reject the same grouping keys as `CometBaseAggregate.doConvert`. + * End-to-end SQL cannot reach this today: Spark sorts collated grouping keys and Comet declines + * that sort, so the aggregate never gets a native child. Plan the query with Comet disabled and + * call the check directly instead. See https://github.com/apache/datafusion-comet/issues/6906. + */ +class CometAggregateCollationSuite extends CometTestBase { + + private def partialAggregate(keyType: StringType): BaseAggregateExec = { + val schema = StructType(Seq(StructField("k", keyType))) + val rows = Seq(Row("a"), Row("A"), Row("b")).asJava + var aggregate: BaseAggregateExec = null + withSQLConf(CometConf.COMET_ENABLED.key -> "false") { + withTempView("collation_agg") { + spark.createDataFrame(rows, schema).createOrReplaceTempView("collation_agg") + val plan = + spark + .sql("SELECT k, count(*) FROM collation_agg GROUP BY k") + .queryExecution + .executedPlan + aggregate = stripAQEPlan(plan).collectFirst { + case agg: BaseAggregateExec + if agg.aggregateExpressions.nonEmpty && + agg.aggregateExpressions.forall(_.mode == Partial) => + agg + }.get + } + } + assert(aggregate.groupingExpressions.map(_.dataType) == Seq(keyType), aggregate.toString) + aggregate + } + + test("canAggregateBeConverted rejects a non-default collated grouping key (issue #6906)") { + val aggregate = partialAggregate(StringType("UTF8_LCASE")) + assert(QueryPlanSerde.isStringCollationType(aggregate.groupingExpressions.head.dataType)) + assert(!CometExecRule(spark).canAggregateBeConverted(aggregate, Partial)) + assert( Review Comment: Could we also exercise `doConvert` directly with this collated grouping key and assert that it returns `None` and records the collation fallback reason on the aggregate? The current assertions cover the preflight check and the helper’s return value, but do not verify that actual conversion applies the shared guard and preserves the diagnostic. Calling the converter directly would avoid the underlying sort masking this behavior in an end-to-end query. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
