mingmwang commented on code in PR #6372:
URL: https://github.com/apache/arrow-datafusion/pull/6372#discussion_r1201428445
##########
datafusion/core/tests/sql/subqueries.rs:
##########
@@ -690,3 +690,78 @@ async fn support_union_subquery() -> Result<()> {
Ok(())
}
+
+#[tokio::test]
+async fn simple_uncorrelated_scalar_subquery() -> Result<()> {
+ let ctx = create_join_context("t1_id", "t2_id", true)?;
+
+ let sql = "select (select count(*) from t1) as b";
+
+ let msg = format!("Creating logical plan for '{sql}'");
+ let dataframe = ctx.sql(sql).await.expect(&msg);
+ let plan = dataframe.into_optimized_plan()?;
+
+ let expected = vec![
+ "Projection: __scalar_sq_1.__value AS b [b:Int64;N]",
+ " SubqueryAlias: __scalar_sq_1 [__value:Int64;N]",
+ " Projection: COUNT(UInt8(1)) AS __value [__value:Int64;N]",
+ " Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]
[COUNT(UInt8(1)):Int64;N]",
+ " TableScan: t1 projection=[t1_id] [t1_id:UInt32;N]",
+ ];
+ let formatted = plan.display_indent_schema().to_string();
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ assert_eq!(
+ expected, actual,
+ "\n\nexpected:\n\n{expected:#?}\nactual:\n\n{actual:#?}\n\n"
+ );
+
+ // assert data
+ let results = execute_to_batches(&ctx, sql).await;
+ let expected = vec!["+---+", "| b |", "+---+", "| 4 |", "+---+"];
+ assert_batches_eq!(expected, &results);
+
+ Ok(())
+}
+
+#[tokio::test]
+async fn simple_uncorrelated_scalar_subquery2() -> Result<()> {
+ let ctx = create_join_context("t1_id", "t2_id", true)?;
+
+ let sql = "select (select count(*) from t1) as b, (select count(1) from
t2) as c";
Review Comment:
> I suggest also adding an error case here for a query that doesn't produce
a single row? Something like
>
> ```sql
> select (select t1_id from t1) as b, (select count(1) from t2) as c
> ```
>
> In which case I would expect an error
This query can not be de-correlated by this rule, the subquery expression is
kept and the real physical plan is not executable.
--
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]