Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support struct_expr generate struct in sql #2389

Merged
merged 10 commits into from
May 8, 2022

Conversation

Ted-Jiang
Copy link
Member

Which issue does this PR close?

Related #2043.

Rationale for this change

Support construct a struct in SQL.

select STRUCT(1,2,3);
+------------------------------------+
| struct(Int64(1),Int64(2),Int64(3)) |
+------------------------------------+
| {"f_0": 1, "f_1": 2, "f_2": 3}     |
+------------------------------------+
1 row in set. Query took 0.003 seconds.

What changes are included in this PR?

Are there any user-facing changes?

Now the struct Field only support default name like f_n
I want to use `select struct(1 as a)' as field name, but got error in parsing

 select STRUCT(1 as a);  🤔 Invalid statement: sql parser error: Expected ), found: as

I think it should fix in sql-parse, if wrong plz correct me.
I will create follow up ticket for this, if this PR is fine.

@github-actions github-actions bot added the datafusion Changes in the datafusion crate label Apr 30, 2022
BuiltinScalarFunction::Struct => {
let fields = input_expr_types
.iter()
.map(|x| Field::new("item", x.clone(), true))
Copy link
Member

Choose a reason for hiding this comment

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

This looks like it is generating a struct where all of the fields have the same name. I would expect that to cause an error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fix, It should only need the DataType info, no need for fields name

.map(|(i, arg)| -> (Field, ArrayRef) {
match arg.data_type() {
DataType::Utf8 => (
Field::new(&*format!("f_{}", i), DataType::Utf8, true),
Copy link
Member

Choose a reason for hiding this comment

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

format!("f_{}", i) is duplicated in each match. Could we create the column name once before the match statement?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! Thanks for your advice.

@Ted-Jiang Ted-Jiang requested a review from andygrove May 3, 2022 14:06
@Ted-Jiang Ted-Jiang closed this May 5, 2022
@Ted-Jiang Ted-Jiang reopened this May 5, 2022
@Ted-Jiang Ted-Jiang requested a review from andygrove May 5, 2022 16:34
Field::new(field_name.as_str(), arg.data_type().clone(), true),
arg.clone(),
),
data_type => unimplemented!("struct not support {} type", data_type),
Copy link
Member

Choose a reason for hiding this comment

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

We should return an Err rather than panic here

Copy link
Member

@andygrove andygrove left a comment

Choose a reason for hiding this comment

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

LGTM once the error handling is updated to return Err rather than panic. Thanks @Ted-Jiang

@Ted-Jiang Ted-Jiang closed this May 7, 2022
@Ted-Jiang Ted-Jiang reopened this May 7, 2022
Co-authored-by: Andy Grove <[email protected]>
Copy link
Member

@andygrove andygrove left a comment

Choose a reason for hiding this comment

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

Thanks @Ted-Jiang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants