mbutrovich commented on code in PR #4816:
URL: https://github.com/apache/datafusion-comet/pull/4816#discussion_r4066321245


##########
spark/src/main/spark-4.x/org/apache/comet/serde/CometListAgg.scala:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.serde
+
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import 
org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, 
ListAgg}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{NullType, StringType}
+
+import org.apache.comet.CometSparkSessionExtensions.withFallbackReason
+import org.apache.comet.serde.QueryPlanSerde.{exprToProto, 
hasNonDefaultStringCollation, serializeDataType}
+
+/**
+ * Spark 4.0+ `LISTAGG` / `STRING_AGG`.
+ *
+ * Comet only supports the simple form: a `StringType` child with a literal 
delimiter and no
+ * `WITHIN GROUP (ORDER BY ...)` clause. DISTINCT is handled by Spark's 
multi-stage plan rewrite
+ * (grouping by the child before the aggregate), so the native side never sees 
it.
+ */
+object CometListAgg extends CometAggregateExpressionSerde[ListAgg] {
+
+  private val binaryChildReason = "`BinaryType` inputs are not supported."
+  private val withinGroupReason = "`WITHIN GROUP (ORDER BY ...)` is not 
supported."
+  private val nonFoldableDelimiterReason = "Non-literal delimiters are not 
supported."
+  private val collationReason = "Non-default string collations are not 
supported."
+  private val distinctReason =
+    "`DISTINCT` falls back to Spark because Comet rejects multi-column 
distinct aggregates."
+
+  override def getUnsupportedReasons(): Seq[String] = Seq(
+    binaryChildReason,
+    withinGroupReason,
+    nonFoldableDelimiterReason,
+    collationReason,
+    distinctReason)
+
+  override def getSupportLevel(expr: ListAgg): SupportLevel = {
+    // Spark's analyzer already enforces `delimiter.foldable`, so this only 
ever rejects
+    // non-string / non-null delimiter types.
+    expr.child.dataType match {
+      case _: StringType if hasNonDefaultStringCollation(expr.child.dataType) 
=>
+        Unsupported(Some(collationReason))
+      case _: StringType =>
+        expr.delimiter.dataType match {
+          case _: StringType if 
hasNonDefaultStringCollation(expr.delimiter.dataType) =>

Review Comment:
   I don't think the scan fallback stops the aggregate serde from running. 
`approx_count_distinct_collation.sql` on main reads a `string COLLATE 
UTF8_LCASE` column from Parquet and asserts `expect_fallback(Unsupported input 
data type)` 
([test](https://github.com/apache/datafusion-comet/blob/09b44ad6fa17f58f4bbccf958c5cf02d790f7334/spark/src/test/resources/sql-tests/expressions/aggregate/approx_count_distinct_collation.sql#L26-L37)).
 That reason comes from `CometApproxCountDistinct.getSupportLevel` 
([`aggregates.scala`](https://github.com/apache/datafusion-comet/blob/09b44ad6fa17f58f4bbccf958c5cf02d790f7334/spark/src/main/scala/org/apache/comet/serde/aggregates.scala#L1291-L1293)),
 so the aggregate's support check runs even though the scan fell back. The same 
should hold for `CometListAgg`. Could you add the same shape here? Wrapping the 
delimiter in `collate(',', 'UTF8_LCASE')` should avoid the analyzer's 
`DATATYPE_MISMATCH`:
   
   ```sql
   statement
   CREATE TABLE la_coll(v string COLLATE UTF8_LCASE, grp string) USING parquet
   
   statement
   INSERT INTO la_coll VALUES ('a', 'g1'), ('B', 'g1')
   
   query expect_fallback(Non-default string collations are not supported)
   SELECT grp, listagg(v, collate(',', 'UTF8_LCASE')) FROM la_coll GROUP BY grp 
ORDER BY grp
   ```
   
   If that passes, the "not reachable via a query" comment at 
`CometListAgg.scala` lines 54-57 and the note at `listagg.sql` lines 169-173 
should go. If it fails, the failure message would show what I'm missing.



-- 
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]

Reply via email to