invalid memory address or nil pointer dereference when trying to generate go code from sqlite query
Version
1.29.0
What happened?
I have these 2 queries
When i try to run sqlc generate it returns an error saying invalid memory address
Not quite sure what i did wrong, and also sorry if the issue seems disconnected since this is my 1st issue
any help would be appreciated TY
Relevant log output
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x11448a7]
goroutine 11 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).sourceTables(0xc00038d408, 0xc0005f2fc0, {0x1ebe7a0?, 0xc00047e630?})
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/output_columns.go:601 +0xaa7
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0xc00038d408, 0xc0005f2fc0, {0x1ebe7a0, 0xc00047e630})
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/output_columns.go:55 +0x45
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0xc00038d408, 0xc00077bd40, {0xc0006118bb, 0x1d7}, 0x1)
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/analyze.go:180 +0xcdd
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).analyzeQuery(...)
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/analyze.go:110
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQuery(0xc00038d408, {0x1ebe960, 0xc00077bd40}, {0xc000611680, 0x415}, {{0x0, 0x0, {0x0, 0x0}, 0x0, ...}})
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/parse.go:103 +0x5b5
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0xc00038d408, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/compile.go:81 +0x467
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/engine.go:72
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0xc0003302b0, 0x6}, ...}, ...)
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/generate.go:322 +0x2f6
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/process.go:107 +0x857
golang.org/x/sync/errgroup.(*Group).Go.func1()
/go/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:79 +0x50
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
/go/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:76 +0x96
Database schema
CREATE TABLE
IF NOT EXISTS cs_scoreboard (id INTEGER PRIMARY KEY, score INTEGER NOT NULL);
SQL queries
-- name: GetCSScoreForId :one
SELECT
rank, id, score
FROM (
SELECT
DENSE_RANK() OVER (ORDER BY score DESC) rank_position,
id,
score
FROM
cs_scoreboard
)
WHERE
id = ?;
-- name: GetCSWinners :many
SELECT
rank, id, score
FROM (
SELECT
DENSE_RANK() OVER (ORDER BY score DESC) rank_position,
id,
score
FROM
cs_scoreboard
)
WHERE
rank <= 3;
Configuration
version: "2"
sql:
- engine: "sqlite"
queries: "data/sql/"
schema: "data/sql/schema.sql"
gen:
go:
package: "data"
out: "data"
Playground URL
No response
What operating system are you using?
Linux
What database engines are you using?
SQLite
What type of code are you generating?
Go
I have been the same problem with mysql. but in macos
https://github.com/sqlc-dev/sqlc/issues/3965#issuecomment-2884639222
same issue. Also in macOS, but from the stack trace, it doesn't seem like an OS problem
@traut in my comment I solved it https://github.com/sqlc-dev/sqlc/issues/3963#issuecomment-2887802595
I found a workaround by adding an alias to the subquery (as someAlias)
SELECT aaa, bbb, BOOL_OR(can_be_managed)
FROM
(
SELECT aaa, bbb, true as can_be_managed FROM agent
UNION
SELECT DISTINCT aaa, bbb, false as can_be_managed FROM http
) as someAlias
GROUP BY aaa, bbb;