You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to build a field of type Struct with DataFusion SQL? #2043
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.
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?
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
Activity
matthewmturner commentedon Mar 21, 2022
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 commentedon 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 usersSTRUCT<name string, age integer>('Bob', 50) as user // syntax to specify explicitly the type of each fieldSpark syntax:
NAMED_STRUCT('name', 'Bob', 'age', 50) as usersPostgres and Athena syntax:
CAST(ROW('Bob', 38) AS ROW(name VARCHAR, age INTEGER)) AS usersSnowflake syntax:
OBJECT_CONSTRUCT('name', 'Bob'::VARCHAR, 'age', 50::INTEGER)alamb commentedon Apr 24, 2022
BTW I started collecting known issues in struct support in #2326
Ted-Jiang commentedon Apr 29, 2022
@lquerel Are you taking this feature? If not i am glad to take this.❤️
lquerel commentedon Apr 29, 2022
@Ted-Jiang No unfortunately I can't right now. Thanks in advance.
lquerel commentedon 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 commentedon May 16, 2022
Sorry for the delay, No we can't create a nested, I think we should modify it in
sql-parserfor support this.chitralverma commentedon Apr 25, 2023
@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,
alamb commentedon Jun 23, 2023
I believe this was fixed by #6594 (thanks to @jiangzhx)
wjones127 commentedon Jun 6, 2024
For those looking for how to set the names, the
named_structmethod was added later: #9743alamb commentedon Jun 6, 2024
Doc reference: https://datafusion.apache.org/user-guide/sql/scalar_functions.html#struct-functions