viirya commented on code in PR #6899:
URL: https://github.com/apache/datafusion-comet/pull/6899#discussion_r4263103279


##########
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:
   Every query in this file has empty `resultExpressions`, so none of them goes 
through the path where `buildAggOp` wraps a `Projection` around the `HashAgg`. 
With this PR, that projection now sits on top of the count-and-project plan. 
Plain SQL can produce that shape. HAVING without GROUP BY parses to 
`Aggregate(Nil, [1 AS x], child)` (see 
`AstBuilder.withSelectQuerySpecification`). Could we add a couple of cases like 
these? Spark returns one row even for the table with no files.
   
   ```sql
   query
   SELECT 1 AS x FROM empty_agg_multi HAVING true
   
   query
   SELECT 1 AS x FROM empty_agg_none HAVING true
   ```
   



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