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

CASE WHEN does not work in SELECT field #2261

Closed
dansimau opened this issue May 4, 2023 · 4 comments
Closed

CASE WHEN does not work in SELECT field #2261

dansimau opened this issue May 4, 2023 · 4 comments
Labels
📚 mysql bug Something isn't working 🔧 golang

Comments

@dansimau
Copy link
Contributor

dansimau commented May 4, 2023

Version

1.17.2

What happened?

sqlc generate fails in MySQL if there is a CASE statement as a SELECT field.

Relevant log output

sqlc generate failed.
# package db
query.sql:9:1: parse table name: unexpected node type: *ast.Integer

Database schema

CREATE TABLE users (
  id   int PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

SQL queries

-- name: UserExists :one
SELECT
    CASE WHEN
        EXISTS (
            SELECT * FROM users WHERE id = '123'
        )
    THEN 1
    ELSE 0
END AS result;

Configuration

No response

Playground URL

https://play.sqlc.dev/p/5aeef78626b3adaf91194dfd05854ca22d717e15114c01f8bb21d98826a39848

What operating system are you using?

No response

What database engines are you using?

MySQL

What type of code are you generating?

Go

@dansimau dansimau added bug Something isn't working triage New issues that hasn't been reviewed labels May 4, 2023
@Sub6Resources
Copy link

Sub6Resources commented May 6, 2023

Seeing the same issue with PostgreSQL engine

Notably, it doesn't fail when the above query is modified to:

SELECT
    CASE WHEN
        EXISTS (
            SELECT * FROM users WHERE id = '123'
        )
    THEN (SELECT 1)
    ELSE (SELECT 0)
END AS result;

But the generated Go code returns an interface{} rather than an int as expected

@Sub6Resources
Copy link

Looks like #2238 may have fixed this issue. I don't have time to build from source and see, but hopefully should be included in the next release.

@gnuletik
Copy link

gnuletik commented May 30, 2023

@Sub6Resources you're right!

I gave a try to go run github.com/kyleconroy/sqlc/cmd/sqlc@main generate -f ./sqlc.yaml with Postgres.
And the issue was properly fixed with the proper generated types.

Here was my usecase: https://play.sqlc.dev/p/5dabc6d1291c706bc3d65181b53dc138714136785e958d02368aeedbca85764e

@kyleconroy kyleconroy added 📚 mysql 🔧 golang and removed triage New issues that hasn't been reviewed labels Jun 6, 2023
@kyleconroy
Copy link
Collaborator

Fixed! https://play.sqlc.dev/p/b8dad882c40da78fdce5449adff8abeba66f920a82882b8f647260b246b77e2c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 mysql bug Something isn't working 🔧 golang
Projects
None yet
Development

No branches or pull requests

4 participants