sqldef icon indicating copy to clipboard operation
sqldef copied to clipboard

psqldef panics on valid VIEW with nested CASE

Open chumaltd opened this issue 1 year ago • 1 comments

psqldef gets panic on parsing online VIEWs with nested CASE. This is split from #454, as they have different causes.

Platform

  • OS: Linux
  • RDBMS: PostgreSQL
  • Version: v0.16.9

--export output

$ psqldef -Upostgres -hlocalhost -p5432 --export some_db

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

goroutine 1 [running]:
github.com/k0kubun/sqldef/database/postgres.PostgresParser.parseExpr({{0x0?}, 0x0?}, 0x0?)
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:293 +0x29
github.com/k0kubun/sqldef/database/postgres.PostgresParser.parseExpr({{0x0?}, 0x0?}, 0x0?)
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:351 +0xbe6
github.com/k0kubun/sqldef/database/postgres.PostgresParser.parseResTarget({{0x203000?}, 0x0?}, 0xc0000c23c0)
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:281 +0x23b
github.com/k0kubun/sqldef/database/postgres.PostgresParser.parseSelectStmt({{0x0?}, 0x0?}, 0xc000141b80)
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:201 +0x1f6
github.com/k0kubun/sqldef/database/postgres.PostgresParser.parseViewStmt({{0x1?}, 0x68?}, 0xc000303f00)
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:172 +0xaf
github.com/k0kubun/sqldef/database/postgres.PostgresParser.parseStmt({{0xc00040527f?}, 0x0?}, 0xc0003d0fa0?)
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:76 +0x185
github.com/k0kubun/sqldef/database/postgres.PostgresParser.Parse({{0x2b?}, 0x40?}, {0xc000400000, 0x5498})
        /home/runner/work/sqldef/sqldef/database/postgres/parser.go:45 +0x195
github.com/k0kubun/sqldef/schema.ParseDDLs(0x203000?, {0x410700?, 0xc000129280?}, {0xc000400000?, 0xf4240?}, {0xc00003a600, 0x6})
        /home/runner/work/sqldef/sqldef/schema/parser.go:464 +0x62
github.com/k0kubun/sqldef.Run(0x1, {0x412e18, 0xc00027c360}, {0x410700, 0xc000129280}, 0xc000197730)
        /home/runner/work/sqldef/sqldef/sqldef.go:44 +0x152
main.main()
        /home/runner/work/sqldef/sqldef/cmd/psqldef/psqldef.go:148 +0x3f3

Input SQL

This is a reproducible DDL.

CREATE VIEW nested_case AS
  SELECT CASE
    WHEN 1 > 6 THEN 3
    ELSE CASE 4 + 1
      WHEN 1 THEN 1
      ELSE NULL
    END
END AS value;

chumaltd avatar Oct 27 '23 07:10 chumaltd

With MATERIALIZED VIEW, psqldef doesn't panics, but puts syntax error. This behavior looks almost OK, but just have quoting issue.

--export output

$ psqldef -Upostgres -hlocalhost -p5432 --export some_db

2023/10/28 11:32:38 found syntax error when parsing DDL "CREATE MATERIALIZED VIEW public.nested_case AS SELECT CASE WHEN (1 > 6) THEN 3 ELSE CASE (4 + 1) WHEN 1 THEN 1 ELSE NULL::integer END END AS value": syntax error at position 147 near 'value'

Input SQL

CREATE MATERIALIZED VIEW nested_case AS
  SELECT CASE
    WHEN 1 > 6 THEN 3
    ELSE CASE 4 + 1
      WHEN 1 THEN 1
      ELSE NULL
    END
END AS value;

chumaltd avatar Oct 28 '23 02:10 chumaltd