Open goldmedal opened 2 days ago
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):
named_struct
SELECT named_struct('a', 1, 'b', 2).a
To address this, we could use the Expr::MapAccess approach:
Expr::MapAccess
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.
.
~Inspired from how Trino handles such expressions (DereferenceExpression), I propose introducing a new Expr variant:~
Expr
/// 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:~
CompoundIdentifier
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.~
CompositeAccess
MapAccess is the better way.
MapAccess
Problem
Currently, we support struct syntax such as:
However, if the column is a struct array, we cannot use SQL like:
Additionally, we cannot access fields in structs created using a function (e.g.,
named_struct
, the corresponding function in DataFusion):To address this, we could use the
Expr::MapAccess
approach: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:~~This could potentially replace
CompoundIdentifier
, as the latter is essentially a specific case ofDereferenceExpr
:~~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.