sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

SUM() returns `interface{}` type for mysql

Open meblum opened this issue 1 year ago • 2 comments

Version

1.27.0

What happened?

Querying SELECT SUM(val) FROM authors will return interface{} instead if int

Relevant log output

No response

Database schema

CREATE TABLE authors (
  val   INT NOT NULL
);

SQL queries

-- name: GetSum :one
SELECT SUM(val) FROM authors;

Configuration

{
  "version": "2",
  "sql": [{
    "schema": "schema.sql",
    "queries": "query.sql",
    "engine": "mysql",
    "gen": {
      "go": {
        "out": "db"
      }
    }
  }]
}

Playground URL

https://play.sqlc.dev/p/a38fb22db5ce7e2309fd44c197b20ade5e3b8f8f61085b8b39656ee651ff4ddf

What operating system are you using?

No response

What database engines are you using?

MySQL

What type of code are you generating?

Go

meblum avatar Sep 04 '24 18:09 meblum

Same BTW for MAX() with PostgreSQL on an INTEGER-type column. Seems like a more general issue.

curio77 avatar Sep 06 '24 10:09 curio77

I had started on this a long time ago: https://github.com/sqlc-dev/sqlc/pull/3223

It wasn't trivial to fix sadly.

Jille avatar Oct 03 '24 15:10 Jille

Looking forward to see #3223 completed and merged!

maddsua avatar Dec 03 '24 19:12 maddsua

I'm not currently working on it. Feel free to fork.

Jille avatar Dec 04 '24 08:12 Jille

Noted

maddsua avatar Dec 04 '24 22:12 maddsua

The following workaround fixes this issue: SELECT 0 + SUM(val) FROM table; see in playground.

Originally posted by @sknig in #1965

meblum avatar Jul 01 '25 17:07 meblum