This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new ca5fb7dde1 feat: Add CrossJoin match case to unparser (#10371)
ca5fb7dde1 is described below
commit ca5fb7dde184cf61e32bbf43ed6beb081bdf11ee
Author: Suriya Kandaswamy <[email protected]>
AuthorDate: Sun May 5 07:25:11 2024 -0400
feat: Add CrossJoin match case to unparser (#10371)
* add CrossJoin match case to unparser
* add crossjoin test
---
datafusion/sql/src/unparser/plan.rs | 32 ++++++++++++++++++++++++++++++++
datafusion/sql/tests/sql_integration.rs | 33 +++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+)
diff --git a/datafusion/sql/src/unparser/plan.rs
b/datafusion/sql/src/unparser/plan.rs
index c9b0a8a04c..de36fb0371 100644
--- a/datafusion/sql/src/unparser/plan.rs
+++ b/datafusion/sql/src/unparser/plan.rs
@@ -300,6 +300,38 @@ impl Unparser<'_> {
Ok(())
}
+ LogicalPlan::CrossJoin(cross_join) => {
+ // Cross joins are the same as unconditional inner joins
+ let mut right_relation = RelationBuilder::default();
+
+ self.select_to_sql_recursively(
+ cross_join.left.as_ref(),
+ query,
+ select,
+ relation,
+ )?;
+ self.select_to_sql_recursively(
+ cross_join.right.as_ref(),
+ query,
+ select,
+ &mut right_relation,
+ )?;
+
+ let ast_join = ast::Join {
+ relation: right_relation.build()?,
+ join_operator: self.join_operator_to_sql(
+ JoinType::Inner,
+
ast::JoinConstraint::On(ast::Expr::Value(ast::Value::Boolean(
+ true,
+ ))),
+ ),
+ };
+ let mut from = select.pop_from().unwrap();
+ from.push_join(ast_join);
+ select.push_from(from);
+
+ Ok(())
+ }
LogicalPlan::SubqueryAlias(plan_alias) => {
// Handle bottom-up to allocate relation
self.select_to_sql_recursively(
diff --git a/datafusion/sql/tests/sql_integration.rs
b/datafusion/sql/tests/sql_integration.rs
index 0af0604963..1a300d11b0 100644
--- a/datafusion/sql/tests/sql_integration.rs
+++ b/datafusion/sql/tests/sql_integration.rs
@@ -4674,6 +4674,39 @@ fn roundtrip_statement() -> Result<()> {
Ok(())
}
+#[test]
+fn roundtrip_crossjoin() -> Result<()> {
+ let query = "select j1.j1_id, j2.j2_string from j1, j2";
+
+ let dialect = GenericDialect {};
+ let statement = Parser::new(&dialect)
+ .try_with_sql(query)?
+ .parse_statement()?;
+
+ let context = MockContextProvider::default();
+ let sql_to_rel = SqlToRel::new(&context);
+ let plan = sql_to_rel.sql_statement_to_plan(statement).unwrap();
+
+ let roundtrip_statement = plan_to_sql(&plan)?;
+
+ let actual = format!("{}", &roundtrip_statement);
+ println!("roundtrip sql: {actual}");
+ println!("plan {}", plan.display_indent());
+
+ let plan_roundtrip = sql_to_rel
+ .sql_statement_to_plan(roundtrip_statement.clone())
+ .unwrap();
+
+ let expected = "Projection: j1.j1_id, j2.j2_string\
+ \n Inner Join: Filter: Boolean(true)\
+ \n TableScan: j1\
+ \n TableScan: j2";
+
+ assert_eq!(format!("{plan_roundtrip:?}"), expected);
+
+ Ok(())
+}
+
#[cfg(test)]
#[ctor::ctor]
fn init() {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]