Skip to content

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

Closed

Description

@lquerel

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.

Activity

  1. matthewmturner commented on Mar 21, 2022

    @matthewmturner
    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?

  2. lquerel commented on Mar 21, 2022

    @lquerel
    Author

    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)

  3. alamb commented on Apr 24, 2022

    @alamb
    Contributor

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

  4. Ted-Jiang commented on Apr 29, 2022

    @Ted-Jiang
    Member

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

  5. lquerel commented on Apr 29, 2022

    @lquerel
    Author

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

  6. lquerel commented on May 13, 2022

    @lquerel
    Author

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

    Does this PR allow to create nested structs ?

  7. Ted-Jiang commented on May 16, 2022

    @Ted-Jiang
    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.

  8. chitralverma commented on Apr 25, 2023

    @chitralverma

    @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"
    
  9. alamb commented on Jun 23, 2023

    @alamb
    Contributor

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

  10. wjones127 commented on Jun 6, 2024

    @wjones127
    Member

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

  11. alamb commented on Jun 6, 2024

    @alamb
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions