Skip to content

Crashes with nested COUNT(SELECT(…)) query #4084

@chimbori

Description

@chimbori

Version

1.29.0

What happened?

Crash (panic) with valid SQL:

SELECT COUNT(*) as cnt FROM (
  SELECT DISTINCT col FROM foo
);

Relevant log output

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

goroutine 11 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).sourceTables(0x14000786008, 0x14000846a50, {0x106049b20?, 0x140007f3760?})
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/output_columns.go:601 +0x864
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0x14000786008, 0x14000846a50, {0x106049b20, 0x140007f3760})
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/output_columns.go:55 +0x34
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0x14000786008, 0x14000b103e0, {0x1400005c180, 0x56}, 0x1)
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/analyze.go:180 +0xa04
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).analyzeQuery(...)
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/analyze.go:110
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQuery(0x14000786008, {0x106049ce0, 0x14000b103e0}, {0x1400005c180, 0x58}, {{0x0, 0x0, {0x0, 0x0}, 0x0, ...}})
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/parse.go:103 +0x440
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0x14000786008, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/compile.go:81 +0x398
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/compiler/engine.go:72
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0x140007742f0, 0xa}, ...}, ...)
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/cmd/generate.go:322 +0x224
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
  /Users/REDACTED/go/pkg/mod/github.com/sqlc-dev/sqlc@v1.29.0/internal/cmd/process.go:107 +0x624
golang.org/x/sync/errgroup.(*Group).Go.func1()
  /Users/REDACTED/go/pkg/mod/golang.org/x/sync@v0.13.0/errgroup/errgroup.go:79 +0x4c
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
  /Users/REDACTED/go/pkg/mod/golang.org/x/sync@v0.13.0/errgroup/errgroup.go:76 +0x90

Database schema

CREATE TABLE IF NOT EXISTS foo (
  _id BIGSERIAL PRIMARY KEY,
  col TEXT NOT NULL
);

SQL queries

-- name: CountDistinctFoo :one
SELECT COUNT(*) as cnt FROM (
  SELECT DISTINCT col FROM foo
);

Configuration

version: "2"
sql:
  - engine: "postgresql"
    schema: "migrations.sql"
    queries: "queries.sql"
    gen:
      go:
        package: "db"
        out: "db"
        sql_driver: github.com/jackc/pgx/v5
        sql_package: pgx/v5

Playground URL

https://play.sqlc.dev/p/56af247bf13d2c36734dd97718bc59f91928035be9ea298276092257e5003caa

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions