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

Make the struct function return the correct data type. #6594

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/functions.slt
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,25 @@ SELECT v1, v2, ROWNUMBER() OVER(ORDER BY v1) from test;

statement ok
drop table test

# Scalar function struct
statement ok
create table simple_struct_test (
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please also add an example test where the column names don't happen to line up with the column names?

Like

create table test(x boolean) as values (true);
select struct(x) from test

?

I expect the output is named c1 even though the column is named x

To make the output struct named x I think would be a significant change to the type resolution code as it would need to get a Schema rather than a [DataType]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for your help.
i have updated the current test case:

create table simple_struct_test (
    c1 boolean,
    c2 INT,
    c3 FLOAT,
    c4 DOUBLE,
    a VARCHAR,
    b TEXT
) as select *
from (values
  (true, 1,3.1,3.14,'str','text')
);

it's should cover the scenario you mentioned.

c1 boolean,
c2 INT,
c3 FLOAT,
c4 DOUBLE,
a VARCHAR,
b TEXT
) as select *
from (values
(true, 1,3.1,3.14,'str','text')
);

query ?
SELECT struct(c1,c2,c3,c4,a,b) from simple_struct_test
----
{c0: 1, c1: 1, c2: 3.1, c3: 3.14, c4: str, c5: text}

statement ok
drop table simple_struct_test
9 changes: 8 additions & 1 deletion datafusion/expr/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,14 @@ pub fn return_type(
_ => Ok(DataType::Float64),
},

BuiltinScalarFunction::Struct => Ok(DataType::Struct(Fields::empty())),
BuiltinScalarFunction::Struct => {
let return_fields = input_expr_types
.iter()
.enumerate()
.map(|(pos, dt)| Field::new(format!("c{pos}"), dt.clone(), true))
.collect::<Vec<Field>>();
Ok(DataType::Struct(Fields::from(return_fields)))
}

BuiltinScalarFunction::Atan2 => match &input_expr_types[0] {
DataType::Float32 => Ok(DataType::Float32),
Expand Down