Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate fails for UPDATE with JOIN #3329

Open
lushenle opened this issue Apr 11, 2024 · 0 comments
Open

Generate fails for UPDATE with JOIN #3329

lushenle opened this issue Apr 11, 2024 · 0 comments
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@lushenle
Copy link

Version

1.26.0

What happened?

MySQL UPDATE with JOIN panics with panic: expected range var

Relevant log output

panic: expected range var

goroutine 8 [running]:
github.com/sqlc-dev/sqlc/internal/engine/dolphin.convertToRangeVarList(0x140007b09e0?, 0x140007df998)
	github.com/sqlc-dev/sqlc/internal/engine/dolphin/utils.go:68 +0x360
github.com/sqlc-dev/sqlc/internal/engine/dolphin.(*cc).convertUpdateStmt(0x140007b09e0, 0x140007d94a0)
	github.com/sqlc-dev/sqlc/internal/engine/dolphin/convert.go:547 +0x88
github.com/sqlc-dev/sqlc/internal/engine/dolphin.(*cc).convert(0x140007b09e0, {0x10492ebe0?, 0x140007d94a0?})
	github.com/sqlc-dev/sqlc/internal/engine/dolphin/convert.go:1825 +0xba0
github.com/sqlc-dev/sqlc/internal/engine/dolphin.(*Parser).Parse(0x140001a3458, {0x10490ac88?, 0x140003482e0?})
	github.com/sqlc-dev/sqlc/internal/engine/dolphin/parse.go:63 +0x1b4
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).parseQueries(0x140005d3208, {{0x0, 0x0, {0x0, 0x0}, 0x0, 0x0, 0x0, 0x0}})
	github.com/sqlc-dev/sqlc/internal/compiler/compile.go:75 +0x1e8
github.com/sqlc-dev/sqlc/internal/compiler.(*Compiler).ParseQueries(...)
	github.com/sqlc-dev/sqlc/internal/compiler/engine.go:76
github.com/sqlc-dev/sqlc/internal/cmd.parse({_, _}, {_, _}, {_, _}, {{0x0, 0x0}, {0x1400069a20a, 0x5}, ...}, ...)
	github.com/sqlc-dev/sqlc/internal/cmd/generate.go:322 +0x23c
github.com/sqlc-dev/sqlc/internal/cmd.processQuerySets.func1()
	github.com/sqlc-dev/sqlc/internal/cmd/process.go:107 +0x68c
golang.org/x/sync/errgroup.(*Group).Go.func1()
	golang.org/x/sync@v0.6.0/errgroup/errgroup.go:78 +0x58
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 1
	golang.org/x/sync@v0.6.0/errgroup/errgroup.go:75 +0x98

Database schema

CREATE TABLE `host_summary` (
    `id` binary(16) NOT NULL DEFAULT (uuid_to_bin(uuid())),
    `private_region` char(8) NOT NULL,
    `vdc` char(8) NOT NULL,
    `host_type` char(32) NOT NULL,
    `host_count` int NOT NULL DEFAULT '0',
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;

CREATE TABLE `host` (
  	`id` binary(16) NOT NULL DEFAULT (uuid_to_bin(uuid())),
    `vdc` char(64) NOT NULL,
    `private_region` varchar(64) NOT NULL,
  	`host_type` char(12) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci;

SQL queries

-- name: UpdateHostSummary :execresult
UPDATE host_summary
    JOIN (
        SELECT COUNT(*) AS count,
            vdc,
            host_type
        FROM host
        WHERE vdc = sqlc.arg('vdc')
            AND host_type = sqlc.arg('host_type')
        GROUP BY vdc,
            host_type
    ) AS counts ON host_summary.vdc = counts.vdc
    AND host_summary.host_type = counts.host_type
SET host_summary.host_count = counts.count;

Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "db/query/"
    schema: "db/schema/"
    gen:
      go:
        package: "db"
        out: "pkg/db"
        emit_prepared_queries: false
        emit_interface: true
        emit_exact_table_names: false
        emit_empty_slices: true
        emit_exported_queries: false
        emit_json_tags: true
        json_tags_case_style: "snake"
        overrides:
          - column: "*.uuid"
            go_type: github.com/google/uuid.UUID

Playground URL

https://play.sqlc.dev/p/1086795c00eb4f72df5ef8550b1ca588e2db496b3b90c098c38ccb9ee492b467

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

@lushenle lushenle added bug Something isn't working triage New issues that hasn't been reviewed labels Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

1 participant