Skip to content

duckdb: fix unknown column error for the rowid pseudo-column - #4678

Open
RyoKusnadi wants to merge 1 commit into
sqlc-dev:mainfrom
RyoKusnadi:fix/duckdb-rowid-column
Open

RyoKusnadi wants to merge 1 commit into
sqlc-dev:mainfrom
RyoKusnadi:fix/duckdb-rowid-column

Conversation

@RyoKusnadi

Copy link
Copy Markdown

Problem: On the DuckDB engine, rowid fails with unknown column "rowid" in a SELECT list, as p.rowid, in WHERE, and in ORDER BY. DuckDB accepts all of these.

How it happens: Any DuckDB query that uses rowid on a table from schema.sql hits it. The table has no declared rowid column, and the engine only knew declared columns.

Steps to reproduce: Use the schema and queries from the issue (SELECT rowid, id FROM plan, WHERE rowid = $1) with engine: duckdb, then run sqlc generate.

Expected: Code is generated, with rowid as a nullable BIGINT (what DESCRIBE reports).
Actual: unknown column "rowid".

Cause: convertCreateTableInfo in internal/engine/duckdb/convert.go only emitted the declared columns. SQLite's fts5 hidden columns already use ColumnDef.IsHidden for "resolves by name, not in * or models". The DuckDB side never used it.

Fix: Add a hidden nullable bigint rowid column to every CREATE TABLE. Star expansion and the generated model stay the same. If the table declares its own rowid column, that one wins and nothing is added.

Scope: CREATE TABLE ... AS and views get no rowid. I haven't checked whether DuckDB exposes it for them. I did not touch #4677 (table alias as a value) or #4659 (table functions).

Testing: New case internal/endtoend/testdata/duckdb_rowid (select, qualified, WHERE, ORDER BY, SELECT * skips rowid, user-declared rowid). It needs no database. Before the fix it fails with the four errors from the issue. After: go test -C internal/endtoend -run 'TestReplay/base' . passes. go vet and gofmt are clean on the duckdb package. I did not run the full suite or the tests that need PostgreSQL/MySQL or a live DuckDB.

Environment: fix/duckdb-rowid-column @ 5d94812, go1.27.1, darwin/arm64.

Fixes #4676

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

duckdb: the rowid pseudo-column is an unknown column

1 participant