From eb7ac4598440734c9ed9d2531ff37ac7c74e3a5b Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 04:10:20 +0000 Subject: [PATCH 1/2] 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 } From e3646b8391e48a110b6a58e7c344797e1c83196e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 04:13:50 +0000 Subject: [PATCH 2/2] duckdb: cover rowid with an analyze case Replaces the generated-Go golden from #4697 with an analyze case over the same schema and queries, so the test pins what sqlc infers for rowid rather than the code it generates. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LuRZ4JY6kCxkjBm6hN7Lf3 --- .../testdata/analyze_rowid/duckdb/exec.json | 5 + .../duckdb/query.sql | 0 .../duckdb/schema.sql | 0 .../testdata/analyze_rowid/duckdb/stdout.json | 257 ++++++++++++++++++ .../testdata/duckdb_rowid/duckdb/exec.json | 3 - .../testdata/duckdb_rowid/duckdb/go/db.go | 31 --- .../testdata/duckdb_rowid/duckdb/go/models.go | 22 -- .../duckdb_rowid/duckdb/go/query.sql.go | 193 ------------- .../testdata/duckdb_rowid/duckdb/sqlc.yaml | 10 - 9 files changed, 262 insertions(+), 259 deletions(-) create mode 100644 internal/endtoend/testdata/analyze_rowid/duckdb/exec.json rename internal/endtoend/testdata/{duckdb_rowid => analyze_rowid}/duckdb/query.sql (100%) rename internal/endtoend/testdata/{duckdb_rowid => analyze_rowid}/duckdb/schema.sql (100%) create mode 100644 internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json delete mode 100644 internal/endtoend/testdata/duckdb_rowid/duckdb/exec.json delete mode 100644 internal/endtoend/testdata/duckdb_rowid/duckdb/go/db.go delete mode 100644 internal/endtoend/testdata/duckdb_rowid/duckdb/go/models.go delete mode 100644 internal/endtoend/testdata/duckdb_rowid/duckdb/go/query.sql.go delete mode 100644 internal/endtoend/testdata/duckdb_rowid/duckdb/sqlc.yaml 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/duckdb_rowid/duckdb/query.sql b/internal/endtoend/testdata/analyze_rowid/duckdb/query.sql similarity index 100% rename from internal/endtoend/testdata/duckdb_rowid/duckdb/query.sql rename to internal/endtoend/testdata/analyze_rowid/duckdb/query.sql diff --git a/internal/endtoend/testdata/duckdb_rowid/duckdb/schema.sql b/internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql similarity index 100% rename from internal/endtoend/testdata/duckdb_rowid/duckdb/schema.sql rename to internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql 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..aa69694d50 --- /dev/null +++ b/internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json @@ -0,0 +1,257 @@ +[ + { + "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": "UpdatePlanPrice", + "cmd": ":exec", + "columns": [], + "params": [ + { + "number": 1, + "column": { + "name": "price", + "type": { + "name": "double", + "nullable": true + }, + "table": "plan" + } + }, + { + "number": 2, + "column": { + "name": "rowid", + "type": { + "name": "bigint", + "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": "ListLedger", + "cmd": ":many", + "columns": [ + { + "name": "rowid", + "type": { + "name": "varchar" + }, + "table": "ledger" + }, + { + "name": "amount", + "type": { + "name": "integer", + "nullable": true + }, + "table": "ledger" + } + ], + "params": [] + }, + { + "name": "GetLedgerAmount", + "cmd": ":one", + "columns": [ + { + "name": "amount", + "type": { + "name": "integer", + "nullable": true + }, + "table": "ledger" + } + ], + "params": [ + { + "number": 1, + "column": { + "name": "rowid", + "type": { + "name": "varchar" + }, + "table": "ledger" + } + } + ] + } +] diff --git a/internal/endtoend/testdata/duckdb_rowid/duckdb/exec.json b/internal/endtoend/testdata/duckdb_rowid/duckdb/exec.json deleted file mode 100644 index 2e996ca79d..0000000000 --- a/internal/endtoend/testdata/duckdb_rowid/duckdb/exec.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "contexts": ["base"] -} diff --git a/internal/endtoend/testdata/duckdb_rowid/duckdb/go/db.go b/internal/endtoend/testdata/duckdb_rowid/duckdb/go/db.go deleted file mode 100644 index 32faa9f2b8..0000000000 --- a/internal/endtoend/testdata/duckdb_rowid/duckdb/go/db.go +++ /dev/null @@ -1,31 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 - -package querytest - -import ( - "context" - "database/sql" -) - -type DBTX interface { - ExecContext(context.Context, string, ...any) (sql.Result, error) - PrepareContext(context.Context, string) (*sql.Stmt, error) - QueryContext(context.Context, string, ...any) (*sql.Rows, error) - QueryRowContext(context.Context, string, ...any) *sql.Row -} - -func New(db DBTX) *Queries { - return &Queries{db: db} -} - -type Queries struct { - db DBTX -} - -func (q *Queries) WithTx(tx *sql.Tx) *Queries { - return &Queries{ - db: tx, - } -} diff --git a/internal/endtoend/testdata/duckdb_rowid/duckdb/go/models.go b/internal/endtoend/testdata/duckdb_rowid/duckdb/go/models.go deleted file mode 100644 index 062d0be4b2..0000000000 --- a/internal/endtoend/testdata/duckdb_rowid/duckdb/go/models.go +++ /dev/null @@ -1,22 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 - -package querytest - -import ( - "database/sql" - - "github.com/google/uuid" -) - -type Ledger struct { - Rowid string - Amount sql.NullInt32 -} - -type Plan struct { - ID uuid.UUID - ShipSize string - Price sql.NullFloat64 -} diff --git a/internal/endtoend/testdata/duckdb_rowid/duckdb/go/query.sql.go b/internal/endtoend/testdata/duckdb_rowid/duckdb/go/query.sql.go deleted file mode 100644 index 2ad39b5de6..0000000000 --- a/internal/endtoend/testdata/duckdb_rowid/duckdb/go/query.sql.go +++ /dev/null @@ -1,193 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: query.sql - -package querytest - -import ( - "context" - "database/sql" - - "github.com/google/uuid" -) - -const createPlan = `-- name: CreatePlan :exec -INSERT INTO plan VALUES ($1, $2, $3); -` - -type CreatePlanParams struct { - ID uuid.UUID - ShipSize string - Price sql.NullFloat64 -} - -func (q *Queries) CreatePlan(ctx context.Context, arg CreatePlanParams) error { - _, err := q.db.ExecContext(ctx, createPlan, arg.ID, arg.ShipSize, arg.Price) - return err -} - -const deletePlan = `-- name: DeletePlan :one -DELETE FROM plan WHERE rowid = $1 RETURNING id, ship_size, price; -` - -func (q *Queries) DeletePlan(ctx context.Context, rowid sql.NullInt64) (Plan, error) { - row := q.db.QueryRowContext(ctx, deletePlan, rowid) - var i Plan - err := row.Scan(&i.ID, &i.ShipSize, &i.Price) - return i, err -} - -const getLedgerAmount = `-- name: GetLedgerAmount :one -SELECT amount FROM ledger WHERE rowid = $1; -` - -func (q *Queries) GetLedgerAmount(ctx context.Context, rowid string) (sql.NullInt32, error) { - row := q.db.QueryRowContext(ctx, getLedgerAmount, rowid) - var amount sql.NullInt32 - err := row.Scan(&amount) - return amount, err -} - -const getPlanByRowID = `-- name: GetPlanByRowID :one -SELECT id FROM plan WHERE rowid = $1; -` - -func (q *Queries) GetPlanByRowID(ctx context.Context, rowid sql.NullInt64) (uuid.UUID, error) { - row := q.db.QueryRowContext(ctx, getPlanByRowID, rowid) - var id uuid.UUID - err := row.Scan(&id) - return id, err -} - -const listLedger = `-- name: ListLedger :many -SELECT rowid, amount FROM ledger; -` - -func (q *Queries) ListLedger(ctx context.Context) ([]Ledger, error) { - rows, err := q.db.QueryContext(ctx, listLedger) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Ledger - for rows.Next() { - var i Ledger - if err := rows.Scan(&i.Rowid, &i.Amount); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listPlanRowIDs = `-- name: ListPlanRowIDs :many -SELECT rowid, id FROM plan; -` - -type ListPlanRowIDsRow struct { - Rowid sql.NullInt64 - ID uuid.UUID -} - -func (q *Queries) ListPlanRowIDs(ctx context.Context) ([]ListPlanRowIDsRow, error) { - rows, err := q.db.QueryContext(ctx, listPlanRowIDs) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListPlanRowIDsRow - for rows.Next() { - var i ListPlanRowIDsRow - if err := rows.Scan(&i.Rowid, &i.ID); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listPlans = `-- name: ListPlans :many -SELECT id, ship_size, price FROM plan ORDER BY rowid; -` - -func (q *Queries) ListPlans(ctx context.Context) ([]Plan, error) { - rows, err := q.db.QueryContext(ctx, listPlans) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Plan - for rows.Next() { - var i Plan - if err := rows.Scan(&i.ID, &i.ShipSize, &i.Price); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listQualifiedPlanRowIDs = `-- name: ListQualifiedPlanRowIDs :many -SELECT p.rowid, p.id FROM plan p ORDER BY p.rowid; -` - -type ListQualifiedPlanRowIDsRow struct { - Rowid sql.NullInt64 - ID uuid.UUID -} - -func (q *Queries) ListQualifiedPlanRowIDs(ctx context.Context) ([]ListQualifiedPlanRowIDsRow, error) { - rows, err := q.db.QueryContext(ctx, listQualifiedPlanRowIDs) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListQualifiedPlanRowIDsRow - for rows.Next() { - var i ListQualifiedPlanRowIDsRow - if err := rows.Scan(&i.Rowid, &i.ID); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const updatePlanPrice = `-- name: UpdatePlanPrice :exec -UPDATE plan SET price = $1 WHERE rowid = $2; -` - -type UpdatePlanPriceParams struct { - Price sql.NullFloat64 - Rowid sql.NullInt64 -} - -func (q *Queries) UpdatePlanPrice(ctx context.Context, arg UpdatePlanPriceParams) error { - _, err := q.db.ExecContext(ctx, updatePlanPrice, arg.Price, arg.Rowid) - return err -} diff --git a/internal/endtoend/testdata/duckdb_rowid/duckdb/sqlc.yaml b/internal/endtoend/testdata/duckdb_rowid/duckdb/sqlc.yaml deleted file mode 100644 index 07d6d9d3b8..0000000000 --- a/internal/endtoend/testdata/duckdb_rowid/duckdb/sqlc.yaml +++ /dev/null @@ -1,10 +0,0 @@ -version: "2" -sql: - - engine: "duckdb" - schema: "schema.sql" - queries: "query.sql" - gen: - go: - package: "querytest" - out: "go" - sql_package: "database/sql"