mbutrovich commented on code in PR #6899:
URL: https://github.com/apache/datafusion-comet/pull/6899#discussion_r4260176788
##########
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:
Should this be a `NULL` literal instead of `Int64(1)`? `AggregateStream`
evaluates the aggregate's argument for every input batch
([`aggregate_stream.rs`](https://github.com/apache/datafusion/blob/500744d5c25499055e3b2ca46b022bc425989203/datafusion/physical-plan/src/aggregates/aggregate_stream.rs#L464)),
and expands a scalar argument to the batch's length
([`utils.rs`](https://github.com/apache/datafusion/blob/500744d5c25499055e3b2ca46b022bc425989203/datafusion/physical-expr-common/src/utils.rs#L423)).
For `Int64(1)` that allocates and fills a new `Int64Array` of `num_rows`
values on every batch
([`scalar/mod.rs`](https://github.com/apache/datafusion/blob/500744d5c25499055e3b2ca46b022bc425989203/datafusion/common/src/scalar/mod.rs#L3494)),
and the projection then drops the count. For `ScalarValue::Null` it returns a
cached `NullArray`
([`scalar/mod.rs`](https://github.com/apache/datafusion/blob/500744d5c25499055e3b2ca46b022bc425989203/datafusion/common/src/scalar/mod.rs#L3822),
[`
cache.rs`](https://github.com/apache/datafusion/blob/500744d5c25499055e3b2ca46b022bc425989203/datafusion/common/src/scalar/cache.rs#L99-L118)),
which has no buffers. The aggregate still pulls every batch from its input, so
errors below it still surface.
If you make this change, the comment above would need to say that the
aggregate is a throwaway `count(NULL)` rather than a row count.
```suggestion
vec![Arc::new(Literal::new(ScalarValue::Null))],
```
--
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]