andygrove commented on code in PR #4816: URL: https://github.com/apache/datafusion-comet/pull/4816#discussion_r4072634068
########## 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: You were right: the aggregate's support check runs even though the scan falls back. Added the `collate(',', 'UTF8_LCASE')` case, which falls back with `Non-default string collations are not supported`, and removed the "not reachable" comments. ########## docs/source/contributor-guide/expression-audits/agg_funcs.md: ########## @@ -70,6 +70,14 @@ - Comet implementation: the native side delegates to `datafusion_spark::function::aggregate::collect::SparkCollectSet`, which wraps `DistinctArrayAggAccumulator` with `ignore_nulls = true` in a `NullToEmptyListAccumulator` so a final NULL accumulator state becomes an empty array. The `containsNull` mismatch against Spark's declared output type, and its rationale, are identical to [collect_list](#collect-list). - `CometCollectSet` reports `Incompatible` for float and double input when `spark.comet.exec.strictFloatingPoint=true`, because the native distinct comparison treats `NaN == NaN` and collapses repeated `NaN`s into a single element while Spark keeps each one. The native path for floating-point input is then opt-in via `spark.comet.expression.CollectSet.allowIncompatible=true`. All other input types are `Compatible`. +## listagg + +- Spark 3.4.3 (audited 2026-07-03): does not exist. `ListAgg` was added in Spark 4.0. +- Spark 3.5.8 (audited 2026-07-03): does not exist. +- Spark 4.0.1 (audited 2026-07-03): `ListAgg(child, delimiter, orderExpressions)` in `aggregate/collect.scala`. Accepts `StringType` or `BinaryType` inputs; result type matches child. Skips nulls; empty or all-null groups return `NULL`. A `NULL` delimiter is treated as an empty string. `CometListAgg` maps only the simple form: `StringType` child with a literal `StringType`/`NullType` delimiter and no `WITHIN GROUP`. `BinaryType` inputs, `WITHIN GROUP (ORDER BY ...)`, non-literal delimiters, and non-default collations fall back to Spark. `DISTINCT` falls back because Comet rejects multi-column distinct aggregates (`ListAgg` has two children). +- Spark 4.1.1 (audited 2026-07-03): byte-identical to 4.0.1. +- Native accumulator (`SparkListAgg`) returns `Utf8` and carries its intermediate state as `Utf8`. Partial and final always run in the same engine (`supportsMixedPartialFinal` is false), so no cross-engine buffer-schema matching is required. A `GroupsAccumulator` fast path is provided for grouped aggregation. Review Comment: Applied your suggestion. The method names are updated for main's rename to `supportsNativePartialToSparkFinal` / `supportsSparkPartialToNativeFinal`. ########## spark/src/main/spark-4.x/org/apache/comet/serde/CometListAgg.scala: ########## @@ -0,0 +1,104 @@ +/* + * 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} + +/** + * 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. Review Comment: Aligned the serde doc, the `list_agg.rs` module doc and the `expr.proto` comment with the `aggExprToProto` multi-column distinct fallback. -- 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]
