Skip to content

Commit d1ba54e

Browse files
committed
fix(core): resolve a struct field through its column
typeColumnRef read only the first two parts of a column reference. For i.info.label it resolved the column info and dropped label, so the result had the type of the whole struct. For info.label it looked for a relation named info and reported an unknown column. Resolve the first part as a column when it is not a relation, then walk the remaining parts as fields of the column's struct type. A field is nullable and has no source column. An unknown field is an error. Adds the duckdb_struct_field end-to-end case.
1 parent 3ad8b20 commit d1ba54e

8 files changed

Lines changed: 234 additions & 1 deletion

File tree

‎internal/core/analyzer/expr.go‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func (a *analyzer) typeColumnRef(c *ast.ColumnRef) (exprType, error) {
164164
if err != nil {
165165
return exprType{}, err
166166
}
167+
// Parts after relation.column name fields of a struct column.
168+
var fields []string
169+
if ok && len(parts) > 2 {
170+
fields = parts[2:]
171+
}
167172
// A dotted name may be a column's own, as ClickHouse names the columns
168173
// a Nested column stores as n.a.
169174
if !ok && relation != "" {
@@ -172,6 +177,17 @@ func (a *analyzer) typeColumnRef(c *ast.ColumnRef) (exprType, error) {
172177
return exprType{}, err
173178
}
174179
}
180+
// Otherwise the first part may be a struct column and the rest its
181+
// fields: info.label, when info is no relation in scope.
182+
if !ok && relation != "" {
183+
rel, col, ok, err = a.resolveColumn("", relation)
184+
if err != nil {
185+
return exprType{}, err
186+
}
187+
if ok {
188+
fields = parts[1:]
189+
}
190+
}
175191
if !ok {
176192
if relation != "" {
177193
return exprType{}, fmt.Errorf("unknown column %q.%q", relation, column)
@@ -183,7 +199,33 @@ func (a *analyzer) typeColumnRef(c *ast.ColumnRef) (exprType, error) {
183199
}
184200
return exprType{}, fmt.Errorf("unknown column %q", column)
185201
}
186-
return columnType(rel, col), nil
202+
t := columnType(rel, col)
203+
for _, field := range fields {
204+
f, ok := a.structField(t, field)
205+
if !ok {
206+
return exprType{}, fmt.Errorf("unknown field %q of column %q", field, col.Name)
207+
}
208+
t = f
209+
}
210+
return t, nil
211+
}
212+
213+
// structField is the type of a field of a struct-typed expression. A field
214+
// is never a column of a relation, so the result carries no source column,
215+
// and it is nullable because a struct field can hold NULL.
216+
func (a *analyzer) structField(t exprType, field string) (exprType, bool) {
217+
e := a.exprOf(t)
218+
if e == nil || e.Name != "struct" {
219+
return exprType{}, false
220+
}
221+
for _, arg := range e.Args {
222+
if arg.Type != nil && strings.EqualFold(arg.Label, field) {
223+
out := a.lookupType(arg.Type)
224+
out.nullable = true
225+
return out, true
226+
}
227+
}
228+
return exprType{}, false
187229
}
188230

189231
func flattenFields(fields *ast.List) []string {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"contexts": ["base"]}

‎internal/endtoend/testdata/duckdb_struct_field/duckdb/go/db.go‎

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/duckdb_struct_field/duckdb/go/models.go‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/duckdb_struct_field/duckdb/go/query.sql.go‎

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: Bare :many
2+
SELECT info.label FROM items;
3+
4+
-- name: Qualified :many
5+
SELECT i.info.label AS label, i.info.score AS score FROM items AS i;
6+
7+
-- name: Filtered :many
8+
SELECT id FROM items AS i WHERE i.info.score > $1;
9+
10+
-- name: CaseInsensitive :many
11+
SELECT info.LABEL AS label FROM items;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE items (
2+
id INTEGER NOT NULL,
3+
info STRUCT(label VARCHAR, score DOUBLE, tags VARCHAR[])
4+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "duckdb"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "database/sql"

0 commit comments

Comments
 (0)