Repository: ArroyoSystems/arroyo (language: rust)

Issue:
Queries with `count(case when [cond] then 1 else null end)` fail to plan
Queries of the structure

```sql
select 
  count(case when action = 'click' then 1 else null end),
from stream
group by tumble(interval '1 second');
```

Currently fail to plan, with an error like 

```
expected aggregate function, not COUNT(CASE WHEN stream.action = Utf8("click") 
THEN Int64(1) ELSE Int64(NULL) END) AS COUNT(CASE WHEN stream.action = Utf8("click") 
THEN Int64(1) ELSE NULL END)
```

However, other case statements do work correctly, for example:

```sql
sum(case when action = 'click' then 1 else 0 end)
```

This appears to be an issue with how we identify aggregate expressions within an alias (caused here by the implicit type conversion due to the null)


Discussion / hints:

Fix patch:
diff --git a/arroyo-sql/src/expressions.rs b/arroyo-sql/src/expressions.rs
index 509862988..c5fe6c34a 100644
--- a/arroyo-sql/src/expressions.rs
+++ b/arroyo-sql/src/expressions.rs
@@ -1093,6 +1093,7 @@ impl AggregationExpression {
                     aggregator,
                 })
             }
+            Expr::Alias(alias) => Self::try_from_expression(ctx, &alias.expr),
             _ => bail!("expected aggregate function, not {}", expr),
         }
     }
diff --git a/arroyo-sql/src/types.rs b/arroyo-sql/src/types.rs
index 13fe30801..27022893d 100644
--- a/arroyo-sql/src/types.rs
+++ b/arroyo-sql/src/types.rs
@@ -547,10 +547,10 @@ impl TypeDef {
 
     pub fn get_literal(scalar: &ScalarValue) -> syn::Expr {
         if scalar.is_null() {
-            return parse_quote!("None");
+            return parse_quote!(None);
         }
         match scalar {
-            ScalarValue::Null => parse_quote!("None"),
+            ScalarValue::Null => parse_quote!(None),
             ScalarValue::Boolean(Some(value)) => parse_quote!(#value),
             ScalarValue::Float32(Some(value)) => parse_quote!(#value),
             ScalarValue::Float64(Some(value)) => parse_quote!(#value),


Test patch:
diff --git a/arroyo-sql-testing/src/full_query_tests.rs b/arroyo-sql-testing/src/full_query_tests.rs
index b4e04a6e9..fe2de49ba 100644
--- a/arroyo-sql-testing/src/full_query_tests.rs
+++ b/arroyo-sql-testing/src/full_query_tests.rs
@@ -134,3 +134,9 @@ full_pipeline_codegen! {"session_window",
 "SELECT count(*), session(INTERVAL '10' SECOND) AS window
 from nexmark
 group by window, auction.id; "}
+
+full_pipeline_codegen! {"count_over_case",
+"SELECT count(case when person.name = 'click' then 1 else null end) as clicks
+from nexmark
+group by tumble(interval '1 second');
+"}