apache / datafusion-sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.8k stars 543 forks source link

Improve `CompositeAccess` to enhance the struct type #1533

Open goldmedal opened 2 days ago

goldmedal commented 2 days ago

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.