dreverri commented on code in PR #27333:
URL: https://github.com/apache/datafusion/pull/27333#discussion_r4780197730
##########
datafusion/sql/src/unparser/plan.rs:
##########
@@ -311,7 +311,15 @@ impl Unparser<'_> {
select: &mut SelectBuilder,
fully_absorbed: bool,
) -> Result<bool> {
- let mut exprs = p.expr.clone();
+ // A projection of no columns (e.g. under `count(*)` once
+ // `optimize_projections` has pruned every input column) gets the
+ // same fallback as an empty table scan projection, so dialects
+ // that reject `SELECT FROM` receive `SELECT 1 FROM`.
+ let mut exprs = if p.expr.is_empty() {
+ self.empty_projection_fallback()
Review Comment:
Good catch, thanks. Confirmed: `Projection: []` over `Aggregate:
groupBy=[[]], aggr=[[count(1)]]` unparsed as `SELECT 1 FROM t`, which returns
one row per input row (3 for a 3-row table, 0 for an empty one) instead of 1.
PostgreSQL already had this with `SELECT FROM t`; the fallback made it
executable everywhere.
Fixed in 95a69c292. When none of a `Projection`'s expressions reference a
column and the SELECT would absorb a global aggregate, the aggregate now stays
in a derived table: `SELECT 1 FROM (SELECT count(1) FROM t)`. The condition
also covers constant-only projections like `Projection: 1`, which hit the same
problem. Grouped aggregates are unaffected, since `GROUP BY` already keeps the
cardinality.
Tests: `unparse_projection_without_columns_over_global_aggregate` runs the
unparsed SQL against a 3-row and an empty table, with empty and constant
projections, and checks for exactly one row. `plan_to_sql.rs` has snapshots for
the default and PostgreSQL dialects, the constant case, and a grouped-aggregate
case to check it doesn't over-apply.
These shapes don't seem reachable from SQL through DataFusion's optimizer,
which folds them to `EmptyRelation: rows=1`, so the tests build the plans
directly.
##########
datafusion/core/tests/sql/unparser.rs:
##########
@@ -759,6 +759,26 @@ async fn optimized_filter_with_subquery_alias() ->
Result<()> {
Ok(())
}
+/// `optimize_projections` prunes every column under `count(*)` when the
+/// filter column isn't needed above the filter, leaving a `Projection` with
+/// no expressions. Dialects without empty select list support must not
+/// receive `SELECT FROM`.
+#[tokio::test]
+async fn optimized_count_over_filter_empty_projection() -> Result<()> {
+ let ctx = SessionContext::new();
+ ctx.sql("create table t (id int)").await?.collect().await?;
+ let plan = ctx
+ .sql("select count(*) from t where id > 10")
+ .await?
+ .into_optimized_plan()?;
+ let sql = plan_to_sql(&plan)?.to_string();
Review Comment:
Added in 95a69c292: `optimized_count_over_filter_empty_projection` now also
unparses the optimized plan with `BigQueryDialect`, which gives ``SELECT
count(1) AS `count_40_42_41` FROM (SELECT 1 FROM `t` WHERE (`t`.`id` > 10))``.
The alias is BigQuery's usual escaping of `count(*)`.
##########
datafusion/sql/src/unparser/plan.rs:
##########
Review Comment:
Following up: a review found that the global aggregate case did change
cardinality; that's fixed in 95a69c292.
--
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]