Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Only unnest source for EmptyRelation
  • Loading branch information
blaginin committed Mar 11, 2025
commit fb1f67983d82c59bf583cc2cc810fce034b05905
13 changes: 11 additions & 2 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,17 @@ impl Unparser<'_> {
};
if self.dialect.unnest_as_table_factor() && unnest_input_type.is_some() {
if let LogicalPlan::Unnest(unnest) = &p.input.as_ref() {
return self
.unnest_to_table_factor_sql(unnest, query, select, relation);
if let LogicalPlan::Projection(projection) = unnest.input.as_ref()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like something that is a limitation of unnest_to_table_factor_sql -- would it make sense to put the check in there so the check and the code with the assumption are in the same place?

{
if matches!(
projection.input.as_ref(),
LogicalPlan::EmptyRelation(_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add some comments to explain why we match EmptyRelation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, added in 28f5301

) {
return self.unnest_to_table_factor_sql(
unnest, query, select, relation,
);
}
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions datafusion/sql/tests/cases/plan_to_sql.rs
Copy link
Collaborator Author

@blaginin blaginin Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to remove this new test because now (even in main) it produces SELECT * FROM (SELECT [1, 2, 3] AS c) CROSS JOIN UNNEST(c). Feels like this should be discussed separately

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment -- doesn't this PR add a new test?

Copy link
Collaborator Author

@blaginin blaginin Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry -- before I added one more test, but then I have to remove it due to ef4a79d - I think we can discuss in that issue and work on it further

Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,24 @@ fn roundtrip_statement_with_dialect() -> Result<()> {
parser_dialect: Box::new(GenericDialect {}),
unparser_dialect: Box::new(CustomDialectBuilder::default().with_unnest_as_table_factor(true).build()),
},
TestStatementWithDialect {
sql: "SELECT unnest([1, 2, 3, 4]) from unnest([1, 2, 3]);",
expected: r#"SELECT UNNEST([1, 2, 3, 4]) AS UNNEST(make_array(Int64(1),Int64(2),Int64(3),Int64(4))) FROM UNNEST([1, 2, 3])"#,
parser_dialect: Box::new(GenericDialect {}),
unparser_dialect: Box::new(CustomDialectBuilder::default().with_unnest_as_table_factor(true).build()),
},
TestStatementWithDialect {
sql: "SELECT unnest([1, 2, 3, 4]) from unnest([1, 2, 3]);",
expected: r#"SELECT UNNEST([1, 2, 3, 4]) AS UNNEST(make_array(Int64(1),Int64(2),Int64(3),Int64(4))) FROM UNNEST([1, 2, 3])"#,
parser_dialect: Box::new(GenericDialect {}),
unparser_dialect: Box::new(CustomDialectBuilder::default().with_unnest_as_table_factor(true).build()),
},
Comment on lines +636 to +647
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They look like the same. 🤔 I think we can remove one of them.

TestStatementWithDialect {
sql: "select * from (select [1, 2, 3] as c), unnest(c);",
expected: r#"SELECT * FROM (SELECT [1, 2, 3] AS c) CROSS JOIN UNNEST(c)"#,
parser_dialect: Box::new(GenericDialect {}),
unparser_dialect: Box::new(CustomDialectBuilder::default().with_unnest_as_table_factor(true).build()),
},
TestStatementWithDialect {
sql: "SELECT * FROM unnest_table u, UNNEST(u.array_col)",
expected: r#"SELECT * FROM unnest_table AS u CROSS JOIN LATERAL (SELECT UNNEST(u.array_col) AS "UNNEST(outer_ref(u.array_col))")"#,
Expand Down
Loading