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

How to build a field of type Struct with DataFusion SQL? #2043

Closed
lquerel opened this issue Mar 21, 2022 · 11 comments
Closed

How to build a field of type Struct with DataFusion SQL? #2043

lquerel opened this issue Mar 21, 2022 · 11 comments
Labels
enhancement New feature or request

Comments

@lquerel
Copy link

lquerel commented Mar 21, 2022

DataFusion already gives us a way to access nested fields with this syntax field["nested_field"] (field being an arrow struct field). However I didn't find any example or documentation to do the reverse operation, i.e. creating a struct from multiple fields. A pseudo SQL query (inspired from BIGQUERY) will be something like: SELECT STRUCT(a AS field_1, b AS field_2) AS struct_name FROM table. Is there a way to express this type of construct with DataFusion SQL?

Support for this type of functionality will be particularly helpful for dealing with data sources that support nested fields (e.g. parquet and JSON) and when the query output MUST also be stored in a nested form. Example of use cases:

  • Remove privacy sensitive fields from a struct field.
  • Apply some transformations on some nested fields.
  • Compute new nested fields based on the existing nested fields (e.g. add country, city nested fields from an ip_address nested field).

Proposed solution:

  • Add a new operator to create a field of type Struct. Simple syntax: STRUCT (expr AS field_name, ...) AS struct_field_name
  • Support an EXCEPT keyword to remove one or several fields from a struct. e.g. SELECT STRUCT (struct_a.* EXCEPT (field_1, field_2)) FROM xyz. This type of syntax is robust to schema evolution as new fields will be automatically captured by the wildcard operator.
  • Support wildcard operators to combine multiple structs into a single one. e.g. SELECT STRUCT(struct_a.*, struct_b.*) AS combined_struct FROM xyz. This must fail if the structures a and b have fields with the same name.
@lquerel lquerel added the enhancement New feature or request label Mar 21, 2022
@matthewmturner
Copy link
Contributor

Hello - thanks for raising this, im quite interested in operations on structs as well. Do you have any examples of how other engines such as spark / postgres / athena etc handle what you are proposing?

@lquerel
Copy link
Author

lquerel commented Mar 21, 2022

Different approaches to represent a structure in the projection part of a SELECT.

BigQuery syntax:
STRUCT('Bob' AS name, 38 AS age) AS users
STRUCT<name string, age integer>('Bob', 50) as user // syntax to specify explicitly the type of each field

Spark syntax:
NAMED_STRUCT('name', 'Bob', 'age', 50) as users

Postgres and Athena syntax:
CAST(ROW('Bob', 38) AS ROW(name VARCHAR, age INTEGER)) AS users

Snowflake syntax:
OBJECT_CONSTRUCT('name', 'Bob'::VARCHAR, 'age', 50::INTEGER)

@alamb
Copy link
Contributor

alamb commented Apr 24, 2022

BTW I started collecting known issues in struct support in #2326

@Ted-Jiang
Copy link
Member

@lquerel Are you taking this feature? If not i am glad to take this.❤️

@lquerel
Copy link
Author

lquerel commented Apr 29, 2022

@Ted-Jiang No unfortunately I can't right now. Thanks in advance.

@lquerel
Copy link
Author

lquerel commented May 13, 2022

@Ted-Jiang thanks for your PR. Really nice to see that integrated so quickly.

Does this PR allow to create nested structs ?

@Ted-Jiang
Copy link
Member

@Ted-Jiang thanks for your PR. Really nice to see that integrated so quickly.

Does this PR allow to create nested structs ?

Sorry for the delay, No we can't create a nested, I think we should modify it in sql-parser for support this.

@chitralverma
Copy link

@Ted-Jiang based on #2389 and this I was wondering what all is possible to create struct columns in datafusion SQL.

for example I was trying to do something like the below but its broke,

select struct(a,b,c) from table

Failed to execute query: Arrow error: Invalid argument error: column types must match schema types, expected Struct([]) but found Struct([Field { name: \"c0\", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: None }]) at column index 0"

@alamb
Copy link
Contributor

alamb commented Jun 23, 2023

I believe this was fixed by #6594 (thanks to @jiangzhx)

@wjones127
Copy link
Member

For those looking for how to set the names, the named_struct method was added later: #9743

@alamb
Copy link
Contributor

alamb commented Jun 6, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants