Skip to content

Improve the complex field access chain for the struct type #1533

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

Closed
goldmedal opened this issue Nov 19, 2024 · 0 comments · Fixed by #1551
Closed

Improve the complex field access chain for the struct type #1533

goldmedal opened this issue Nov 19, 2024 · 0 comments · Fixed by #1551
Assignees

Comments

@goldmedal
Copy link
Contributor

goldmedal commented Nov 19, 2024

Problem

Currently, we support struct syntax such as:

SELECT struct_col.field_1 FROM t1  

However, if the column is a struct array, we cannot use SQL like:

SELECT struct_array_col[1].field_1 FROM t1  

Additionally, we cannot access fields in structs created using a function (e.g., named_struct, the corresponding function in DataFusion):

SELECT named_struct('a', 1, 'b', 2).a  

To address this, we could use the Expr::MapAccess approach:

SELECT named_struct('a', 1, 'b', 2)['a']  

However, the . syntax is more commonly used for structs, as seen in systems like DuckDB or BigQuery.


Proposal

Inspired from how Trino handles such expressions (DereferenceExpression), I propose introducing a new Expr variant:

/// Parses SQL expressions like `<base>.<field>`  
DereferenceExpr {  
    base: Box<Expr>,  
    field: Ident,  
}  

This could potentially replace CompoundIdentifier, as the latter is essentially a specific case of DereferenceExpr:

DereferenceExpr(DereferenceExpr(DereferenceExpr, Ident))  

However, this change might break downstream projects. I prefer not to make such a breaking change in this issue.

After some research, I found that CompositeAccess has the same structure. We can enhance it to support the struct field access.

MapAccess is the better way.

@goldmedal goldmedal self-assigned this Nov 19, 2024
@goldmedal goldmedal changed the title Introduce DereferenceExpr to enhance the struct accessing Improve CompositeAccess to enhance the struct type using Nov 20, 2024
@goldmedal goldmedal changed the title Improve CompositeAccess to enhance the struct type using Improve CompositeAccess to enhance the struct type Nov 20, 2024
@goldmedal goldmedal changed the title Improve CompositeAccess to enhance the struct type Improve the complex field access chain for the struct type Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment