Skip to content

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

Closed
@goldmedal

Description

@goldmedal

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions