sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

SQLC panics when generating query for aggregating the result of a UNION

Open eattoe-ctoken opened this issue 6 months ago • 2 comments

Version

1.29.0

What happened?

sqlc generate will panic when trying to generate the following sql query:

-- name: AllThingsTrue :one
WITH t AS (
    SELECT thing, bool_thing FROM things
    WHERE things.thing = $1
), ot AS (
    SELECT thing, bool_thing FROM other_things
    WHERE other_things.thing = $1
)
SELECT 
    thing, -- This column is TEXT
    bool_and(bool_thing)
FROM (
  SELECT thing, bool_thing FROM t
  UNION
  SELECT thing, bool_thing FROM ot
)
GROUP BY thing;

Relevant log output

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x105448320]

goroutine 5 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).sourceTables(0x14000127408, 0x140008f0750, {0x10624d1e0?, 0x140008f8000?})
        github.com/sqlc-dev/sqlc/internal/compiler/output_columns.go:601 +0x920
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0x14000127408, 0x140008f0750, {0x10624d1e0, 0x140008f8000})
        github.com/sqlc-dev/sqlc/internal/compiler/output_columns.go:55 +0x34
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0x14000127408, 0x140006127e0, {0x1400015aa00, 0xb84}, 0x1)
        github.com/sqlc-dev/sqlc/internal/compiler/analyze.go:180 +0x9a8
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).analyzeQuery(...)
        github.com/sqlc-dev/sqlc/internal/compiler/analyze.go:110
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQuery(0x14000127408, {0x10624d3a0, 0x140006127e0}, {0x1400015aa00, 0x13f9}, {{0x0, 0x0, {0x0, 0x0}, 0x0, ...}})
        github.com/sqlc-dev/sqlc/internal/compiler/parse.go:103 +0x480
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0x14000127408, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
        github.com/sqlc-dev/sqlc/internal/compiler/compile.go:81 +0x3b0
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
        github.com/sqlc-dev/sqlc/internal/compiler/engine.go:72
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0x1400011adc0, 0xa}, ...}, ...)
        github.com/sqlc-dev/sqlc/internal/cmd/generate.go:322 +0x234
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
        github.com/sqlc-dev/sqlc/internal/cmd/process.go:107 +0x660
golang.org/x/sync/errgroup.(*Group).Go.func1()
        golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x58
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
        golang.org/x/[email protected]/errgroup/errgroup.go:75 +0x98

Database schema


SQL queries


Configuration


Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

eattoe-ctoken avatar Apr 23 '25 13:04 eattoe-ctoken