ygf11 commented on code in PR #5345: URL: https://github.com/apache/arrow-datafusion/pull/5345#discussion_r1113984893
########## datafusion/optimizer/src/decorrelate_where_exists.rs: ########## @@ -644,4 +690,102 @@ mod tests { assert_plan_eq(&plan, expected) } + + #[test] + fn exists_distinct_subquery() -> Result<()> { + let table_scan = test_table_scan()?; + let subquery_scan = test_table_scan_with_name("sq")?; + let subquery = LogicalPlanBuilder::from(subquery_scan) + .filter((lit(1u32) + col("sq.a")).gt(col("test.a") * lit(2u32)))? + .project(vec![col("sq.c")])? + .distinct()? + .build()?; + let plan = LogicalPlanBuilder::from(table_scan) + .filter(exists(Arc::new(subquery)))? + .project(vec![col("test.b")])? + .build()?; + + let expected = "Projection: test.b [b:UInt32]\ + \n LeftSemi Join: Filter: UInt32(1) + sq.a > test.a * UInt32(2) [a:UInt32, b:UInt32, c:UInt32]\ + \n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\ + \n Distinct: [a:UInt32, c:UInt32]\ + \n Projection: sq.a, sq.c [a:UInt32, c:UInt32]\ + \n TableScan: sq [a:UInt32, b:UInt32, c:UInt32]"; + + assert_plan_eq(&plan, expected) + } + + #[test] + fn exists_distinct_subquery_with_literal() -> Result<()> { + let table_scan = test_table_scan()?; + let subquery_scan = test_table_scan_with_name("sq")?; + let subquery = LogicalPlanBuilder::from(subquery_scan) + .filter((lit(1u32) + col("sq.a")).gt(col("test.a") * lit(2u32)))? + .project(vec![lit(1u32), col("sq.c")])? + .distinct()? + .build()?; + let plan = LogicalPlanBuilder::from(table_scan) + .filter(exists(Arc::new(subquery)))? + .project(vec![col("test.b")])? + .build()?; + + let expected = "Projection: test.b [b:UInt32]\ + \n LeftSemi Join: Filter: UInt32(1) + sq.a > test.a * UInt32(2) [a:UInt32, b:UInt32, c:UInt32]\ + \n TableScan: test [a:UInt32, b:UInt32, c:UInt32]\ + \n Distinct: [a:UInt32, c:UInt32]\ + \n Projection: sq.a, sq.c [a:UInt32, c:UInt32]\ + \n TableScan: sq [a:UInt32, b:UInt32, c:UInt32]"; + Review Comment: The literal will be removed. -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org