Skip to content

Conversation

@goldmedal
Copy link
Contributor

Which issue does this PR close?

Closes #11144.

Rationale for this change

DataFusion will plan the array construction and the elements' access to the corresponding scalar functions. If we don't handle those cases, they will be unparsed to ast::Expr::ScalarFunction which isn't a common SQL syntax.

Array Construction

SELECT [1, 2, 3] -> make_array(1, 2, 3)

Elements accessing

SELECT [1, 2, 3][1] -> array_element(make_array(1,2,3), 1)

This PR tries to allow the array SQL to do SQL-Plan-SQL roundtrip.

What changes are included in this PR?

  • Unparse make_array to ast::Expr::Array
  • Unparse array_element to ast::Expr::Subscript

Are these changes tested?

expr unit tests and SQL roundtrip tests.

Are there any user-facing changes?

no

@github-actions github-actions bot added the sql SQL Planner label Nov 14, 2024
match func_name {
"make_array" => self.make_array_to_sql(args),
"array_element" => self.array_element_to_sql(args),
// TODO: support for the construct and access functions of the `map` and `struct` types
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar cases exist for map and struct. I'll file another issue for them.

@goldmedal goldmedal marked this pull request as ready for review November 14, 2024 16:45
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Makes sense to me -- thank you @goldmedal

SUM(id) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM person
GROUP BY GROUPING SETS ((id, first_name, last_name), (first_name, last_name), (last_name))"#,
"SELECT ARRAY[1, 2, 3]",
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

@alamb alamb merged commit b75563b into apache:main Nov 17, 2024
25 checks passed
@alamb
Copy link
Contributor

alamb commented Nov 17, 2024

Thanks again @goldmedal

@goldmedal goldmedal deleted the feature/11144-unparse-array branch November 18, 2024 06:21
@goldmedal
Copy link
Contributor Author

Thanks @alamb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support unparsing the Value Plan of Array (List) to SQL String

2 participants