Skip to content

Commit 1b697f6

Browse files
committed
fix(core): name an unnamed result column by position
targetName returned "?column?" for a result column with no alias that is not a column reference or a function call. The Go generator uses a non-empty name as written, so it emitted "var ?column? ..." and sqlc generate failed with "illegal character U+003F". Return an empty name. The generator already falls back to column_N for an empty name, which is what the PostgreSQL and SQLite engines produce. Adds the duckdb_unnamed_columns end-to-end case.
1 parent 3ad8b20 commit 1b697f6

8 files changed

Lines changed: 209 additions & 1 deletion

File tree

‎internal/core/analyzer/projection.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func (a *analyzer) decorateSource(col *core.Column, attOID int64, tableAlias str
105105

106106
// targetName picks the output name for a target. fields is the already
107107
// flattened field list when rt.Val is a column reference, and nil otherwise.
108+
// A target with no name returns "", so code generation names it by position
109+
// (column_1) as it does for the other engines. "?column?" is not a valid
110+
// identifier in the generated code.
108111
func targetName(rt *ast.ResTarget, fields []string) string {
109112
if rt.Name != nil && *rt.Name != "" {
110113
return *rt.Name
@@ -117,7 +120,7 @@ func targetName(rt *ast.ResTarget, fields []string) string {
117120
return name
118121
}
119122
}
120-
return "?column?"
123+
return ""
121124
}
122125

123126
func isStar(fields []string) bool {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"contexts": ["base"]}

‎internal/endtoend/testdata/duckdb_unnamed_columns/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_unnamed_columns/duckdb/go/models.go‎

Lines changed: 15 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_unnamed_columns/duckdb/go/query.sql.go‎

Lines changed: 136 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: Cast :many
2+
SELECT CAST(age AS BIGINT) FROM people;
3+
4+
-- name: Concat :many
5+
SELECT name || '!' FROM people;
6+
7+
-- name: Two :many
8+
SELECT id + 1, age * 2 FROM people;
9+
10+
-- name: Mixed :many
11+
SELECT id, upper(name), age + 1 AS next_age, id * 2 FROM people;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE people (id INTEGER NOT NULL, name VARCHAR NOT NULL, age INTEGER);
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"

0 commit comments

Comments
 (0)