Skip to content

Commit 5d94812

Browse files
committed
duckdb: resolve the rowid pseudo-column
Signed-off-by: Ryo Kusnadi <[email protected]>
1 parent 3ad8b20 commit 5d94812

8 files changed

Lines changed: 281 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}

‎internal/endtoend/testdata/duckdb_rowid/duckdb/go/db.go‎

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/duckdb_rowid/duckdb/go/models.go‎

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/duckdb_rowid/duckdb/go/query.sql.go‎

Lines changed: 169 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- name: WithRowID :many
2+
SELECT rowid, id FROM plan;
3+
4+
-- name: WithQualifiedRowID :many
5+
SELECT p.rowid, p.id FROM plan p;
6+
7+
-- name: ByRowID :one
8+
SELECT id FROM plan WHERE rowid = $1;
9+
10+
-- name: OrderByRowID :many
11+
SELECT id FROM plan ORDER BY rowid;
12+
13+
-- name: StarSkipsRowID :many
14+
SELECT * FROM plan;
15+
16+
-- name: DeclaredRowID :many
17+
SELECT rowid, name FROM tag;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE plan (
2+
id UUID NOT NULL,
3+
ship_size VARCHAR NOT NULL,
4+
price DOUBLE
5+
);
6+
7+
CREATE TABLE tag (
8+
rowid VARCHAR NOT NULL,
9+
name VARCHAR NOT NULL
10+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "duckdb"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "database/sql"

‎internal/engine/duckdb/convert.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,25 @@ func (c *cc) convertCreateTableInfo(info *dw.CreateTableInfo) ast.Node {
10611061
for i := range info.Columns {
10621062
stmt.Cols = append(stmt.Cols, c.convertColumnDef(&info.Columns[i], primaryKey))
10631063
}
1064+
1065+
// Every base table also has a rowid pseudo-column. It is a nullable
1066+
// BIGINT that queries can select, filter and sort on, but it is not part
1067+
// of the table's columns. A column the table declares as rowid takes
1068+
// precedence.
1069+
hasRowID := false
1070+
for _, col := range stmt.Cols {
1071+
if col.Colname == "rowid" {
1072+
hasRowID = true
1073+
break
1074+
}
1075+
}
1076+
if !hasRowID {
1077+
stmt.Cols = append(stmt.Cols, &ast.ColumnDef{
1078+
Colname: "rowid",
1079+
IsHidden: true,
1080+
TypeName: &ast.TypeName{Name: "bigint"},
1081+
})
1082+
}
10641083
return stmt
10651084
}
10661085

0 commit comments

Comments
 (0)