From eb7ac4598440734c9ed9d2531ff37ac7c74e3a5b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 04:10:20 +0000 Subject: [PATCH] duckdb: resolve the rowid pseudo-column Every DuckDB base table offers a rowid that a query can select, filter on and order by. The converter now adds it to each table as a hidden, nullable BIGINT column, the way the SQLite engine adds the hidden columns of an fts5 table, so it resolves by name but stays out of star expansions, INSERT targets and the table's model. A table that declares its own rowid keeps that column. Fixes #4676 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LuRZ4JY6kCxkjBm6hN7Lf3 --- .../testdata/analyze_rowid/duckdb/exec.json | 5 + .../testdata/analyze_rowid/duckdb/query.sql | 20 ++ .../testdata/analyze_rowid/duckdb/schema.sql | 4 + .../testdata/analyze_rowid/duckdb/stdout.json | 213 ++++++++++++++++++ internal/engine/duckdb/convert.go | 17 +- 5 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 internal/endtoend/testdata/analyze_rowid/duckdb/exec.json create mode 100644 internal/endtoend/testdata/analyze_rowid/duckdb/query.sql create mode 100644 internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql create mode 100644 internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json diff --git a/internal/endtoend/testdata/analyze_rowid/duckdb/exec.json b/internal/endtoend/testdata/analyze_rowid/duckdb/exec.json new file mode 100644 index 0000000000..56cb4b3fff --- /dev/null +++ b/internal/endtoend/testdata/analyze_rowid/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_rowid/duckdb/query.sql b/internal/endtoend/testdata/analyze_rowid/duckdb/query.sql new file mode 100644 index 0000000000..65bf23097c --- /dev/null +++ b/internal/endtoend/testdata/analyze_rowid/duckdb/query.sql @@ -0,0 +1,20 @@ +-- name: ListPlanRowIDs :many +SELECT rowid, id FROM plan; + +-- name: ListQualifiedPlanRowIDs :many +SELECT p.rowid, p.id FROM plan p ORDER BY p.rowid; + +-- name: GetPlanByRowID :one +SELECT id FROM plan WHERE rowid = $1; + +-- name: ListPlans :many +SELECT * FROM plan ORDER BY rowid; + +-- name: CreatePlan :exec +INSERT INTO plan VALUES ($1, $2, $3); + +-- name: DeletePlan :one +DELETE FROM plan WHERE rowid = $1 RETURNING *; + +-- name: GetLedgerRowID :one +SELECT rowid, amount FROM ledger WHERE rowid = $1; diff --git a/internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql b/internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql new file mode 100644 index 0000000000..2a7e50cc5b --- /dev/null +++ b/internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql @@ -0,0 +1,4 @@ +CREATE TABLE plan (id UUID NOT NULL, ship_size VARCHAR NOT NULL, price DOUBLE); + +-- A declared column takes the place of the pseudo-column. +CREATE TABLE ledger (rowid VARCHAR NOT NULL, amount INTEGER); diff --git a/internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json b/internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json new file mode 100644 index 0000000000..0cfdbd5df2 --- /dev/null +++ b/internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json @@ -0,0 +1,213 @@ +[ + { + "name": "ListPlanRowIDs", + "cmd": ":many", + "columns": [ + { + "name": "rowid", + "type": { + "name": "bigint", + "nullable": true + }, + "table": "plan" + }, + { + "name": "id", + "type": { + "name": "uuid" + }, + "table": "plan" + } + ], + "params": [] + }, + { + "name": "ListQualifiedPlanRowIDs", + "cmd": ":many", + "columns": [ + { + "name": "rowid", + "type": { + "name": "bigint", + "nullable": true + }, + "table": "plan" + }, + { + "name": "id", + "type": { + "name": "uuid" + }, + "table": "plan" + } + ], + "params": [] + }, + { + "name": "GetPlanByRowID", + "cmd": ":one", + "columns": [ + { + "name": "id", + "type": { + "name": "uuid" + }, + "table": "plan" + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "rowid", + "type": { + "name": "bigint", + "nullable": true + }, + "table": "plan" + } + } + ] + }, + { + "name": "ListPlans", + "cmd": ":many", + "columns": [ + { + "name": "id", + "type": { + "name": "uuid" + }, + "table": "plan" + }, + { + "name": "ship_size", + "type": { + "name": "varchar" + }, + "table": "plan" + }, + { + "name": "price", + "type": { + "name": "double", + "nullable": true + }, + "table": "plan" + } + ], + "params": [] + }, + { + "name": "CreatePlan", + "cmd": ":exec", + "columns": [], + "params": [ + { + "number": 1, + "column": { + "name": "id", + "type": { + "name": "uuid" + }, + "table": "plan" + } + }, + { + "number": 2, + "column": { + "name": "ship_size", + "type": { + "name": "varchar" + }, + "table": "plan" + } + }, + { + "number": 3, + "column": { + "name": "price", + "type": { + "name": "double", + "nullable": true + }, + "table": "plan" + } + } + ] + }, + { + "name": "DeletePlan", + "cmd": ":one", + "columns": [ + { + "name": "id", + "type": { + "name": "uuid" + }, + "table": "plan" + }, + { + "name": "ship_size", + "type": { + "name": "varchar" + }, + "table": "plan" + }, + { + "name": "price", + "type": { + "name": "double", + "nullable": true + }, + "table": "plan" + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "rowid", + "type": { + "name": "bigint", + "nullable": true + }, + "table": "plan" + } + } + ] + }, + { + "name": "GetLedgerRowID", + "cmd": ":one", + "columns": [ + { + "name": "rowid", + "type": { + "name": "varchar" + }, + "table": "ledger" + }, + { + "name": "amount", + "type": { + "name": "integer", + "nullable": true + }, + "table": "ledger" + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "rowid", + "type": { + "name": "varchar" + }, + "table": "ledger" + } + } + ] + } +] diff --git a/internal/engine/duckdb/convert.go b/internal/engine/duckdb/convert.go index 63a2c7ec9d..9c2d0c853f 100644 --- a/internal/engine/duckdb/convert.go +++ b/internal/engine/duckdb/convert.go @@ -1055,8 +1055,23 @@ func (c *cc) convertCreateTableInfo(info *dw.CreateTableInfo) ast.Node { } } + declaresRowid := false for i := range info.Columns { - stmt.Cols = append(stmt.Cols, c.convertColumnDef(&info.Columns[i], primaryKey)) + col := c.convertColumnDef(&info.Columns[i], primaryKey) + declaresRowid = declaresRowid || strings.EqualFold(col.Colname, "rowid") + stmt.Cols = append(stmt.Cols, col) + } + + // A base table also offers a hidden rowid, a BIGINT that DuckDB reports + // as nullable, unless the table declares a column of that name itself. + // Hidden keeps it out of star expansions and the table's model, as for + // the hidden columns of a SQLite fts5 table. + if !declaresRowid { + stmt.Cols = append(stmt.Cols, &ast.ColumnDef{ + Colname: "rowid", + IsHidden: true, + TypeName: &ast.TypeName{Name: "bigint"}, + }) } return stmt }