sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Delete query with join returns panic: expected range var error

Open wouthoekstra opened this issue 10 months ago • 1 comments

Version

1.28.0

What happened?

When generating a query with a delete statement that uses a join, sqlc returns this error:

panic: expected range var

The code itself works in my database if I run it native. I've made a simplified version of my database and query to show the issue.

Relevant log output

panic: expected range var

goroutine 7 [running]:
github.com/sqlc-dev/sqlc/internal/engine/dolphin.convertToRangeVarList(0xc00006aa40?, 0xc0004bb3b0)
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/engine/dolphin/utils.go:68 +0x328
github.com/sqlc-dev/sqlc/internal/engine/dolphin.(*cc).convertDeleteStmt(0xc00006aa40, 0xc000b93cc0)
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/engine/dolphin/convert.go:302 +0x90
github.com/sqlc-dev/sqlc/internal/engine/dolphin.(*cc).convert(0xc00006aa40, {0x1e8c7e0?, 0xc000b93cc0?})
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/engine/dolphin/convert.go:1557 +0x3e5
github.com/sqlc-dev/sqlc/internal/engine/dolphin.(*Parser).Parse(0xc0000a70f8, {0x1e692e0?, 0xc0007f14c0?})
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/engine/dolphin/parse.go:63 +0x205
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0xc000474008, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/compile.go:75 +0x24e
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/compiler/engine.go:72
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0xc000438866, 0x5}, ...}, ...)
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/generate.go:322 +0x2dc
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
	/home/wout/go/pkg/mod/github.com/sqlc-dev/[email protected]/internal/cmd/process.go:107 +0x81a
golang.org/x/sync/errgroup.(*Group).Go.func1()
	/home/wout/go/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:78 +0x50
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
	/home/wout/go/pkg/mod/golang.org/x/[email protected]/errgroup/errgroup.go:75 +0x96

Database schema

CREATE TABLE `conflicts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `currency_id` int(10) unsigned NOT NULL,
  `blockchain_id` int(10) unsigned NOT NULL,
  `reference` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

SQL queries

-- name: DeleteConflicts :execrows
DELETE conflicts
FROM conflicts
  JOIN (
      SELECT blockchain_id, reference
        FROM conflicts
    GROUP BY blockchain_id, reference
      HAVING COUNT(DISTINCT currency_id) = 1
  ) AS resolved_conflicts
    ON conflicts.blockchain_id = resolved_conflicts.blockchain_id
   AND conflicts.reference = resolved_conflicts.reference;

Configuration

version: "2"
cloud:
    organization: ""
    project: ""
    hostname: ""
sql:
  - schema: ./sql/schema.sql
    queries: ./sql/queries
    engine: mysql
    gen:
      go:
        package: sqlc
        out: ./_sqlc
        emit_json_tags: true
        emit_prepared_queries: false
        emit_interface: false
        emit_exact_table_names: true
overrides:
    go: null
plugins: []
rules: []
options: {}

Playground URL

https://play.sqlc.dev/p/8b36238bfbc667bc2478394d370938fdaad11bd2c82df63a5e59f17b699b24f0

What operating system are you using?

Linux

What database engines are you using?

MySQL

What type of code are you generating?

Go

wouthoekstra avatar Feb 17 '25 10:02 wouthoekstra

It seems specifically for using a subquery in the JOIN clause.

DELETE curr FROM objects_online curr LEFT JOIN (SELECT object_id FROM object_variants_online_next) next ON ... crashes while DELETE curr FROM objects_online curr LEFT JOIN object_variants_online_next next ON ... doesn't.

Jille avatar Apr 11 '25 10:04 Jille