jackwener commented on code in PR #6372:
URL: https://github.com/apache/arrow-datafusion/pull/6372#discussion_r1201661724


##########
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:
   > select (select t1_id from t1) as b, (select count(1) from t2) as c
   
   Because this sql is `project subquery`, it's a special situation, this rule 
can't handle it.
   
   `HyPer` Paper mention this condition.



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

Reply via email to