sqlc-dev / sqlc

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

sqlc panics on union all with subqueries using mysql #2453

Open andrewmbenton opened 1 year ago

andrewmbenton commented 1 year ago

Version

Other

What happened?

From https://github.com/kyleconroy/sqlc/issues/2416#issuecomment-1633767537, sqlc panics on the below query going back to at least version v1.14.0.

Relevant log output

panic: interface conversion: ast.Node is *ast.SetOprSelectList, not *ast.SelectStmt

goroutine 6 [running]:
github.com/kyleconroy/sqlc/internal/engine/dolphin.(*cc).convertSetOprSelectList(0xc000a7ebc0?, 0xc0001e4930)
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/engine/dolphin/convert.go:1212 +0x505
github.com/kyleconroy/sqlc/internal/engine/dolphin.(*cc).convertSetOprStmt(0x20?, 0xc000100000?)
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/engine/dolphin/convert.go:1255 +0x7b
github.com/kyleconroy/sqlc/internal/engine/dolphin.(*cc).convert(0xc000810000?, {0x2804eb0?, 0xc0001e48c0?})
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/engine/dolphin/convert.go:1735 +0x20f6
github.com/kyleconroy/sqlc/internal/engine/dolphin.(*Parser).Parse(0xc000015130, {0x27f0380?, 0xc000a4e0a0?})
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/engine/dolphin/parse.go:63 +0x205
github.com/kyleconroy/sqlc/internal/compiler.(*Compiler).parseQueries(0xc00021dc00, {{0x40?, 0xf1?, {0x0?, 0x1?}, 0x1?}})
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/compiler/compile.go:74 +0x26d
github.com/kyleconroy/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/compiler/engine.go:49
github.com/kyleconroy/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0xc0004520f6, 0x5}, {0xc00043f140, 0x1, ...}, ...}, ...)
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/cmd/generate.go:350 +0x285
github.com/kyleconroy/sqlc/internal/cmd.Generate.func1()
        /go/pkg/mod/github.com/kyleconroy/sqlc@v1.19.0/internal/cmd/generate.go:224 +0x827
golang.org/x/sync/errgroup.(*Group).Go.func1()
        /go/pkg/mod/golang.org/x/sync@v0.3.0/errgroup/errgroup.go:75 +0x64
created by golang.org/x/sync/errgroup.(*Group).Go
        /go/pkg/mod/golang.org/x/sync@v0.3.0/errgroup/errgroup.go:72 +0xa5

Database schema

CREATE TABLE authors (
  id   INTEGER PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

SQL queries

(select id
from authors
where id < ? or id = (select min(id) from authors)
order by id desc
limit 1)
union all
(select id
from authors
where id > ? or id = (select max(id) from authors)
order by id asc
limit 1);

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "mysql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/77d407f03779300c3bdefb4b7efda45ce2f65e5ee98c2257f0dd01c06cb52b91

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

No response

araufdogan commented 1 year ago

Is there any progress?

I have the same problem in my query. The problem occurs when I use union all in the subquery.

-- name: Search :one
SELECT * FROM ( 
    (
        SELECT a.ref_no FROM a WHERE a.id = ?
    ) 
    UNION ALL 
    (
        SELECT a.ref_no FROM a WHERE a.id = ?
    )
) AS s LIMIT 1;

macOS, MYSQL, Go, sqlc v1.23.0