From 56e0e4d702dac6d3087b7f5aa288fbae8b51440a Mon Sep 17 00:00:00 2001 From: Tony Coder <407243179@qq.com> Date: Tue, 1 Dec 2026 22:12:03 +0000 Subject: [PATCH] fix(duckdb): type one-argument unnest as a nullable element Seed DuckDB unnest(anyarray) -> anyelement, nullable because a list element can be NULL, following Spanner's unnest. FROM unnest and a select-list unnest(nums) over INTEGER[] are then the element type (sql.NullInt32), not unknown and not any. Calls with more arguments, including recursive and max_depth, stay untyped. Fixes #4712 Signed-off-by: Tony Coder <407243179@qq.com> --- internal/core/analyzer/expr.go | 13 ++ internal/core/analyzer/scope.go | 7 +- .../testdata/analyze_unnest/duckdb/exec.json | 5 + .../analyze_unnest/duckdb/fixture.sql | 6 + .../testdata/analyze_unnest/duckdb/query.sql | 20 +++ .../testdata/analyze_unnest/duckdb/schema.sql | 6 + .../analyze_unnest/duckdb/stdout.json | 139 ++++++++++++++++++ .../engine/duckdb/dialect/functions.jsonl | 1 + internal/goldeneye/duckdb/duckdb.go | 27 ++++ 9 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 internal/endtoend/testdata/analyze_unnest/duckdb/exec.json create mode 100644 internal/endtoend/testdata/analyze_unnest/duckdb/fixture.sql create mode 100644 internal/endtoend/testdata/analyze_unnest/duckdb/query.sql create mode 100644 internal/endtoend/testdata/analyze_unnest/duckdb/schema.sql create mode 100644 internal/endtoend/testdata/analyze_unnest/duckdb/stdout.json diff --git a/internal/core/analyzer/expr.go b/internal/core/analyzer/expr.go index 62a69b273d..9e345e6faa 100644 --- a/internal/core/analyzer/expr.go +++ b/internal/core/analyzer/expr.go @@ -894,6 +894,13 @@ func (a *analyzer) typeFuncCall(f *ast.FuncCall) (exprType, error) { // rather than failing the query. return exprType{nullable: true}, nil } + // DuckDB seeds only unnest(anyarray). unnest(list, recursive := true) + // and unnest(list, max_depth := n) change the result shape, so a call + // with any other number of arguments stays untyped, as it did when + // unnest was not seeded at all. + if name == "unnest" && len(args) != 1 && a.seededDuckDB() { + return exprType{nullable: true}, nil + } p := a.pickOverload(overloads, argOIDs) // An argument that is a bare placeholder takes the parameter's type, // unless the parameter is polymorphic, when it takes what the other @@ -929,6 +936,12 @@ func (a *analyzer) typeFuncCall(f *ast.FuncCall) (exprType, error) { return ret, nil } +// seededDuckDB reports whether the catalog was seeded as DuckDB. +func (a *analyzer) seededDuckDB() bool { + oid, err := a.cat.DialectOID("duckdb") + return err == nil && oid != 0 && oid == a.cat.SeededDialectOID() +} + // returnTemplate fills a seed's return template — Decimal(18, $2) — from // the call: a $n that stands for an integer literal takes its value, and // one that stands for a typed argument takes its type. A template that diff --git a/internal/core/analyzer/scope.go b/internal/core/analyzer/scope.go index 218a8e0b11..f922e01e73 100644 --- a/internal/core/analyzer/scope.go +++ b/internal/core/analyzer/scope.go @@ -117,11 +117,16 @@ func (a *analyzer) bindRangeFunction(rf *ast.RangeFunction) (scopeRel, error) { colName = rel.alias } col := core.ClassColumn{Name: colName, TypeOID: t.typeOID, Type: t.expr, NotNull: !t.nullable} - if t.typeOID == 0 && t.expr == nil { + if t.typeOID == 0 && t.expr == nil && len(overloads[0].ArgTypes) == len(listItems(call.Args)) { p := overloads[0] col.TypeOID, col.NotNull = p.ReturnTypeOID, !p.ReturnNullable } rel.cols = []core.ClassColumn{col} + // "unnest(items) AS u(it)" names the relation u and the column it. + // Without a column list the relation alias is the column name. + if rf.Alias != nil { + renameColumns(&rel, rf.Alias.Colnames) + } return rel, nil } diff --git a/internal/endtoend/testdata/analyze_unnest/duckdb/exec.json b/internal/endtoend/testdata/analyze_unnest/duckdb/exec.json new file mode 100644 index 0000000000..56cb4b3fff --- /dev/null +++ b/internal/endtoend/testdata/analyze_unnest/duckdb/exec.json @@ -0,0 +1,5 @@ +{ + "command": "analyze", + "args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"], + "contexts": ["base"] +} diff --git a/internal/endtoend/testdata/analyze_unnest/duckdb/fixture.sql b/internal/endtoend/testdata/analyze_unnest/duckdb/fixture.sql new file mode 100644 index 0000000000..ea502e5d29 --- /dev/null +++ b/internal/endtoend/testdata/analyze_unnest/duckdb/fixture.sql @@ -0,0 +1,6 @@ +INSERT INTO transactions VALUES ( + 'x', + [NULL, {'type': 'fee', 'amount': 1}], + [1, NULL], + [true, NULL] +); diff --git a/internal/endtoend/testdata/analyze_unnest/duckdb/query.sql b/internal/endtoend/testdata/analyze_unnest/duckdb/query.sql new file mode 100644 index 0000000000..a2cc3bf2d1 --- /dev/null +++ b/internal/endtoend/testdata/analyze_unnest/duckdb/query.sql @@ -0,0 +1,20 @@ +-- name: ListItems :many +SELECT t.id, u.it FROM transactions t, unnest(t.items) AS u(it); + +-- name: ListNums :many +SELECT u.n FROM transactions t, unnest(t.nums) AS u(n); + +-- name: UnnestBare :many +SELECT unnest FROM transactions t, unnest(t.nums); + +-- name: UnnestAlias :many +SELECT n FROM transactions t, unnest(t.nums) AS n; + +-- name: UnnestSelect :many +SELECT unnest(nums) AS n FROM transactions; + +-- name: UnnestBoth :many +SELECT unnest(nums) AS n, unnest(flags) AS f FROM transactions; + +-- name: UnnestFiltered :many +SELECT unnest(nums) AS n FROM transactions WHERE id = $1; diff --git a/internal/endtoend/testdata/analyze_unnest/duckdb/schema.sql b/internal/endtoend/testdata/analyze_unnest/duckdb/schema.sql new file mode 100644 index 0000000000..a5edd84931 --- /dev/null +++ b/internal/endtoend/testdata/analyze_unnest/duckdb/schema.sql @@ -0,0 +1,6 @@ +CREATE TABLE transactions ( + id VARCHAR NOT NULL, + items STRUCT(type VARCHAR, amount INTEGER)[], + nums INTEGER[], + flags BOOLEAN[] +); diff --git a/internal/endtoend/testdata/analyze_unnest/duckdb/stdout.json b/internal/endtoend/testdata/analyze_unnest/duckdb/stdout.json new file mode 100644 index 0000000000..50cf70ddf1 --- /dev/null +++ b/internal/endtoend/testdata/analyze_unnest/duckdb/stdout.json @@ -0,0 +1,139 @@ +[ + { + "name": "ListItems", + "cmd": ":many", + "columns": [ + { + "name": "id", + "type": { + "name": "varchar" + }, + "table": "transactions" + }, + { + "name": "it", + "type": { + "name": "struct", + "nullable": true, + "args": [ + { + "label": "type", + "type": { + "name": "varchar" + } + }, + { + "label": "amount", + "type": { + "name": "integer" + } + } + ] + } + } + ], + "params": [] + }, + { + "name": "ListNums", + "cmd": ":many", + "columns": [ + { + "name": "n", + "type": { + "name": "integer", + "nullable": true + } + } + ], + "params": [] + }, + { + "name": "UnnestBare", + "cmd": ":many", + "columns": [ + { + "name": "unnest", + "type": { + "name": "integer", + "nullable": true + } + } + ], + "params": [] + }, + { + "name": "UnnestAlias", + "cmd": ":many", + "columns": [ + { + "name": "n", + "type": { + "name": "integer", + "nullable": true + } + } + ], + "params": [] + }, + { + "name": "UnnestSelect", + "cmd": ":many", + "columns": [ + { + "name": "n", + "type": { + "name": "integer", + "nullable": true + } + } + ], + "params": [] + }, + { + "name": "UnnestBoth", + "cmd": ":many", + "columns": [ + { + "name": "n", + "type": { + "name": "integer", + "nullable": true + } + }, + { + "name": "f", + "type": { + "name": "boolean", + "nullable": true + } + } + ], + "params": [] + }, + { + "name": "UnnestFiltered", + "cmd": ":many", + "columns": [ + { + "name": "n", + "type": { + "name": "integer", + "nullable": true + } + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "id", + "type": { + "name": "varchar" + }, + "table": "transactions" + } + } + ] + } +] diff --git a/internal/engine/duckdb/dialect/functions.jsonl b/internal/engine/duckdb/dialect/functions.jsonl index 84ac52aa49..4b7c8e6575 100644 --- a/internal/engine/duckdb/dialect/functions.jsonl +++ b/internal/engine/duckdb/dialect/functions.jsonl @@ -2576,6 +2576,7 @@ {"name":"union_extract","args":[{"type":"union"},{"type":"varchar"}],"returns":"any"} {"name":"union_tag","args":[{"type":"union"}],"returns":"any"} {"name":"union_value","args":[{"type":"any","mode":"v"}],"returns":"union"} +{"name":"unnest","args":[{"type":"anyarray"}],"returns":"anyelement","nullable":true} {"name":"unpivot_list","args":[{"type":"any","mode":"v"}],"returns":"list"} {"name":"upper","args":[{"type":"varchar"}],"returns":"varchar"} {"name":"url_decode","args":[{"type":"varchar"}],"returns":"varchar"} diff --git a/internal/goldeneye/duckdb/duckdb.go b/internal/goldeneye/duckdb/duckdb.go index 1005a2d5ea..6257a4c134 100644 --- a/internal/goldeneye/duckdb/duckdb.go +++ b/internal/goldeneye/duckdb/duckdb.go @@ -380,9 +380,36 @@ ORDER BY function_name, parameter_types::VARCHAR, return_type`, &rows) seenFunc[key] = true funcs = append(funcs, fn) } + // unnest is a table function. duckdb_functions() gives it no scalar + // return type, so the query above skips it, and FROM unnest is then an + // unknown function. It is seeded the way PostgreSQL seeds unnest, and + // nullable the way Spanner's unnest is: a list element can be NULL. + funcs = insertByName(funcs, dialect.Function{ + Name: "unnest", + Args: []dialect.Arg{{Type: "anyarray"}}, + Returns: "anyelement", + Nullable: true, + }) return funcs, operators, nil } +// insertByName inserts fn among functions of an earlier name, which is +// where a bytewise sort of the function list puts it. A function already +// seeded under that name is left as the database described it. +func insertByName(funcs []dialect.Function, fn dialect.Function) []dialect.Function { + i := 0 + for i < len(funcs) && funcs[i].Name < fn.Name { + i++ + } + if i < len(funcs) && funcs[i].Name == fn.Name { + return funcs + } + funcs = append(funcs, dialect.Function{}) + copy(funcs[i+1:], funcs[i:]) + funcs[i] = fn + return funcs +} + // Generate reads the dialect from the CLI. func Generate(ctx context.Context, binary string) (dialect.Files, error) { types, err := readTypes(ctx, binary)