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

Use struct instead of named_struct when there are no aliases #9897

Merged
merged 5 commits into from
Apr 2, 2024

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Apr 1, 2024

Which issue does this PR close?

Closes #9839

Rationale for this change

The names of the columns created by callling struct changed in #9743

Before #9743

select struct(a, b) from t;
+-----------------+
| struct(t.a,t.b) |
+-----------------+
| {c0: 1, c1: 2}  |
| {c0: 3, c1: 4}  |
+-----------------+

After #9743

(note how the column name is different)

select struct(a as field_a, b) from t;
+--------------------------------------------------+
| named_struct(Utf8("field_a"),t.a,Utf8("c1"),t.b) |
+--------------------------------------------------+
| {field_a: 1, c1: 2}                              |
| {field_a: 3, c1: 4}                              |
+--------------------------------------------------+

This change caused #9891 as well as removed test coverage for struct

What changes are included in this PR?

  1. Use struct when there are no named fields
  2. reverts the test change in [CI] Use alias for table.struct #9894

Are these changes tested?

Yes, by existing tests

Are there any user-facing changes?

The column names are now the same as they were prior to #9894

@github-actions github-actions bot added sql SQL Planner sqllogictest SQL Logic Tests (.slt) labels Apr 1, 2024
@@ -2288,39 +2288,39 @@ select struct(time,load1,load2,host) from t1;

# can have an aggregate function with an inner coalesce
query TR
select t2.info['c3'] as host, sum(coalesce(t2.info)['c1']) from (select struct(time,load1,load2,host) as info from t1) t2 where t2.info['c3'] IS NOT NULL group by t2.info['c3'] order by host;
select t2."struct(t1.time,t1.load1,t1.load2,t1.host)"['c3'] as host, sum(coalesce(t2."struct(t1.time,t1.load1,t1.load2,t1.host)")['c1']) from (select struct(time,load1,load2,host) from t1) t2 where t2."struct(t1.time,t1.load1,t1.load2,t1.host)"['c3'] IS NOT NULL group by t2."struct(t1.time,t1.load1,t1.load2,t1.host)"['c3'] order by host;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This reverts the change from #9894

.iter()
.any(|value| matches!(value, SQLExpr::Named { .. }))
{
self.create_named_struct(values, input_schema, planner_context)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the values in create_named_struct will be SQLExpr::Named in this PR, so maybe we can also make a change here?
https://github.com/apache/arrow-datafusion/blob/d8d521ac8b90002fa0ba1f91456051a9775ae193/datafusion/sql/src/expr/mod.rs#L610-L631

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, ignore this. One case is only some values are Named so no change is needed.

Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

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

thanks @alamb 1 improvement I can see here is we over iterating through values.
I mean 1 iteration is do identify named/not_named and when creating struct/named_struct its another one. prob it can be optimized

@alamb
Copy link
Contributor Author

alamb commented Apr 2, 2024

thanks @alamb 1 improvement I can see here is we over iterating through values. I mean 1 iteration is do identify named/not_named and when creating struct/named_struct its another one. prob it can be optimized

I agree it does two iterations over the value. I think the current approach basically requires two iterations because if we are doing named_struct we need to add names for each column that don't have a name.

So in other words, struct(1, 2, 3 as other_col) needs to become struct(1 as c1, 2 as c2, 3 as other_col) but we don't know until we see 3 as other_col that we need to add in c1 and c2) 🤔

I would like to spend my optimization budget on bigger things initially if that is ok (specifically #9637)

@alamb alamb merged commit a6ff1fe into apache:main Apr 2, 2024
25 checks passed
@alamb
Copy link
Contributor Author

alamb commented Apr 2, 2024

Thanks again for the reviews 🙏

wiedld pushed a commit to influxdata/arrow-datafusion that referenced this pull request Apr 5, 2024
…che#9897)

* Revert "use alias (apache#9894)"

This reverts commit 9487ca0.

* Use `struct` instead of `named_struct` when there are no aliases

* Update docs

* fmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove struct UDF, and use named_struct everywhere
3 participants