Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/core/analyzer/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion internal/core/analyzer/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions internal/endtoend/testdata/analyze_unnest/duckdb/exec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"command": "analyze",
"args": ["--dialect", "duckdb", "--schema", "schema.sql", "query.sql"],
"contexts": ["base"]
}
6 changes: 6 additions & 0 deletions internal/endtoend/testdata/analyze_unnest/duckdb/fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INSERT INTO transactions VALUES (
'x',
[NULL, {'type': 'fee', 'amount': 1}],
[1, NULL],
[true, NULL]
);
20 changes: 20 additions & 0 deletions internal/endtoend/testdata/analyze_unnest/duckdb/query.sql
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions internal/endtoend/testdata/analyze_unnest/duckdb/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE transactions (
id VARCHAR NOT NULL,
items STRUCT(type VARCHAR, amount INTEGER)[],
nums INTEGER[],
flags BOOLEAN[]
);
139 changes: 139 additions & 0 deletions internal/endtoend/testdata/analyze_unnest/duckdb/stdout.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
]
1 change: 1 addition & 0 deletions internal/engine/duckdb/dialect/functions.jsonl
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
27 changes: 27 additions & 0 deletions internal/goldeneye/duckdb/duckdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading