kosiew commented on code in PR #27333:
URL: https://github.com/apache/datafusion/pull/27333#discussion_r4767950185


##########
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:
   Non-blocking: Could we also unparse this optimized plan using 
BigQueryDialect to cover the complete nested query reported in #27332?



##########
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:
   An empty projection above a global aggregate can now produce executable SQL 
that silently loses the aggregate. Preserve the inner aggregate in a derived 
query or reject this shape explicitly, with regression coverage for empty and 
multi-row inputs.



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