sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.51k stars 810 forks source link

Panic nil pointer deference on derived table #3679

Open boyter opened 3 weeks ago

boyter commented 3 weeks ago

Version

1.27.0

What happened?

sqlc 1.2.7.0 generation panics and crashes when using a derived table.

Relevant log output

$ sqlc version
v1.27.0

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

goroutine 3 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).sourceTables(0x1400068b408, 0x14000a249c0, {0x1046fd1e0?, 0x1400031c6e0?})
    github.com/sqlc-dev/sqlc/internal/compiler/output_columns.go:601 +0x920
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0x1400068b408, 0x14000a249c0, {0x1046fd1e0, 0x1400031c6e0})
    github.com/sqlc-dev/sqlc/internal/compiler/output_columns.go:55 +0x34
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0x1400068b408, 0x140001c07c0, {0x14000f3e13e, 0x3b}, 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(0x1400068b408, {0x1046fd3a0, 0x140001c07c0}, {0x14000f3e000, 0x17a}, {{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(0x1400068b408, {{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}, {0x1400033740a, 0x6}, ...}, ...)
    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/sync@v0.8.0/errgroup/errgroup.go:78 +0x58
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
    golang.org/x/sync@v0.8.0/errgroup/errgroup.go:75 +0x98

Database schema

create table if not exists eg
(
    id    integer constraint data_pk primary key autoincrement,
    name  text not null
) STRICT;

SQL queries

-- name: SelectEg :one
select * from (select * from eg);

-- name: SelectEg2 :one
select id from (select id from eg);

-- either of the above will trigger the issue

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "assets/db/queries.sql"
    schema: "assets/db/migrations.sql"
    gen:
      go:
        package: "database"
        out: "pkg/database/"

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go