andygrove commented on code in PR #6899:
URL: https://github.com/apache/datafusion-comet/pull/6899#discussion_r4264697929
##########
native/core/src/execution/planner.rs:
##########
@@ -1750,6 +1750,49 @@ impl PhysicalPlanner {
let (scans, shuffle_scans, child) =
self.create_plan(&children[0], inputs, partition_count)?;
+ // Spark plans a global aggregate with neither keys nor
functions when its output
+ // goes unused, as in `df.agg(sum($"a")).count()`. Each
partition emits one row
+ // with no columns, even for an empty input. DataFusion can't
run that aggregate:
+ // 55.2's AggregateStream can't build a batch with no columns,
and newer versions
+ // reject an aggregate with neither keys nor functions
(apache/datafusion#25786).
+ // So count the rows and project the count away. Counting
reads the whole input as
+ // Spark does, so errors below the aggregate still surface.
The partial and final
+ // stages both take this path, since the final one only
collapses the partial
+ // one's rows.
+ if agg.grouping_exprs.is_empty() && agg.agg_exprs.is_empty() {
+ let schema = child.schema();
+ let count = Self::create_aggr_func_expr(
+ "count",
+ Arc::clone(&schema),
+
vec![Arc::new(Literal::new(ScalarValue::Int64(Some(1))))],
Review Comment:
Switched to `ScalarValue::Null` in bdf78f58c, and the comment now calls it a
throwaway `count(NULL)`.
##########
native/core/src/execution/planner.rs:
##########
@@ -1750,6 +1750,49 @@ impl PhysicalPlanner {
let (scans, shuffle_scans, child) =
self.create_plan(&children[0], inputs, partition_count)?;
+ // Spark plans a global aggregate with neither keys nor
functions when its output
+ // goes unused, as in `df.agg(sum($"a")).count()`. Each
partition emits one row
+ // with no columns, even for an empty input. DataFusion can't
run that aggregate:
Review Comment:
Cut it from nine lines to five in bdf78f58c.
##########
spark/src/test/resources/sql-tests/expressions/aggregate/global_empty_aggregate.sql:
##########
@@ -0,0 +1,68 @@
+-- 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.
+
+-- ColumnPruning leaves a global aggregate with neither keys nor functions
when nothing reads its
+-- output. Each stage of the partial/final pair emits one row with no columns
per partition, even
+-- for an empty input.
+-- Config: spark.comet.exec.localTableScan.enabled=true
+
+-- Five files. With the test session's parallelism of 5, the scan reads them
as five partitions, so
+-- the final stage collapses five rows into one.
+statement
+CREATE TABLE empty_agg_multi(a int) USING parquet
+
+statement
+INSERT INTO empty_agg_multi SELECT CAST(id AS INT) FROM range(0, 10, 1, 5)
+
+-- No files at all.
+statement
+CREATE TABLE empty_agg_none(a int) USING parquet
+
+statement
+CREATE TABLE empty_agg_division(n int, d int) USING parquet
+
+statement
+INSERT INTO empty_agg_division SELECT 1, 0
+
+query
+SELECT 1 FROM (SELECT count(*) FROM empty_agg_multi)
+
+query
+SELECT 1 FROM (SELECT count(*) FROM empty_agg_multi WHERE a > 100)
+
+query
+SELECT 1 FROM (SELECT count(*) FROM empty_agg_none)
+
+-- Dataset.count() over an aggregated DataFrame plans this shape.
+query
+SELECT count(*) FROM (SELECT sum(a) FROM empty_agg_multi)
+
+query
+SELECT count(*) FROM (SELECT sum(a) FROM empty_agg_none)
+
+query
+SELECT 1 FROM (SELECT count(*) FROM VALUES (1) AS v(a))
+
+query
+SELECT 1 FROM (SELECT count(*) FROM VALUES (1) AS v(a) WHERE a > 5)
Review Comment:
Added both in 7a9cd7104. The `SELECT 1 FROM (SELECT count(*) ...)` queries
already went through that projection, though: `CollapseProject` folds the outer
`SELECT 1` into the pruned aggregate, so Spark plans `Aggregate(Nil, [1 AS 1],
child)` and the final stage's `resultExpressions` is `[1 AS 1]` (checked on
3.5.8 and 4.1.1). I added a comment to the fixture saying so. The HAVING cases
reach the same plan straight from the parser, so they keep that path covered if
the optimizer stops collapsing the projection.
--
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]