Skip to content
Closed
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
5 changes: 5 additions & 0 deletions internal/endtoend/testdata/analyze_rowid/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"]
}
20 changes: 20 additions & 0 deletions internal/endtoend/testdata/analyze_rowid/duckdb/query.sql
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/analyze_rowid/duckdb/schema.sql
Original file line number Diff line number Diff line change
@@ -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);
213 changes: 213 additions & 0 deletions internal/endtoend/testdata/analyze_rowid/duckdb/stdout.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
]
17 changes: 16 additions & 1 deletion internal/engine/duckdb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading