sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Go panics when sub query is missing an alias with sqlite

Open leetrout opened this issue 1 year ago • 2 comments

Version

1.26.0

What happened?

I accidentally left an alias off my subquery and I got a panic instead of a useful error message.

Failing playground without alias (included below as well): https://play.sqlc.dev/p/9bd88a936c60738a57503c6314f5805316327b3dbf51a03c49518ff729382b44

Passing playground with alias: https://play.sqlc.dev/p/47e54486a69100bf2e6e61436518f642835859e73a7ba42ab89f0f74234be50c

Example with postgres that gives a useful error message instead of the panic: https://play.sqlc.dev/p/dd2d6eaf25d9bef2b07e1cd7dc5a4b949694ba6d200b419724d13a513c1eaf1a

Relevant log output

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

goroutine 6 [running]:
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).sourceTables(0xc00049c008, 0xc00048ed50, {0x1d84f80?, 0xc0009d9ad0?})
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/output_columns.go:601 +0xaa7
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).outputColumns(0xc00049c008, 0xc00048ed50, {0x1d84f80, 0xc0009d9ad0})
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/output_columns.go:55 +0x45
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler)._analyzeQuery(0xc00049c008, 0xc000f8fde0, {0xc000062fcc, 0x83}, 0x1)
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/analyze.go:180 +0xced
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).analyzeQuery(...)
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/analyze.go:110
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQuery(0xc00049c008, {0x1d85140, 0xc000f8fde0}, {0xc000062c00, 0x5d5}, {{0x0, 0x0, {0x0, 0x0}, 0x0, ...}})
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/parse.go:103 +0x5d5
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0xc00049c008, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/compile.go:81 +0x467
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/engine.go:76
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0xc00047629a, 0x6}, ...}, ...)
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/generate.go:322 +0x30b
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/process.go:107 +0x877
golang.org/x/sync/errgroup.(*Group).Go.func1()
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x56
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
        /home/user/.asdf/installs/golang/1.22.2/packages/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:75 +0x96

Database schema

CREATE TABLE clients (
    id INTEGER NOT NULL, 
    created_at DATETIME DEFAULT (CURRENT_TIMESTAMP) NOT NULL, 
    updated_at DATETIME DEFAULT (CURRENT_TIMESTAMP) NOT NULL, 
    deleted_at DATETIME, 
    external_id VARCHAR(36) NOT NULL, 
    meta JSON NOT NULL, 
    PRIMARY KEY (id), 
    UNIQUE (external_id)
);

SQL queries

-- name: GetNextClientID :one
SELECT max(external_id) FROM (
  SELECT external_id FROM clients
  ORDER BY external_id
  LIMIT ?
);

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "internal/database/query.sql"
    schema: "internal/database/migrations"
    gen:
      go:
        package: "database"
        out: "internal/database"

Playground URL

https://play.sqlc.dev/p/9bd88a936c60738a57503c6314f5805316327b3dbf51a03c49518ff729382b44

What operating system are you using?

Linux

What database engines are you using?

SQLite

What type of code are you generating?

Go

leetrout avatar Aug 02 '24 03:08 leetrout