Skip to content

COUNT(<column>) in a HAVING clause makes dynamic argument assume the type of the counted column, not an integer #4011

Open
@tru-axo

Description

@tru-axo

Version

1.29.0

What happened?

Given the query and structure of the tables, the generated Go params for this request looks like this:

type ListUsersParams struct {
	Role     string
	AppLimit string
}

Here, the AppLimit param have assumed the type of a.id instead of count(a.id). If you change a.id to a different column of, say a timestamp for instance, the AppLimit changes to a time.Time accordingly.

Given the count(a.id) in the having clause returns an integer, I would expect it to look more like this:

type ListUsersParams struct {
	Role     string
	AppLimit int
}

Relevant log output

Database schema

create table users (
  id varchar(36) primary key,
  role varchar(255) not null
);

create table apps (
  id varchar(36) primary key,
  user_id varchar(36) default null
);

SQL queries

-- name: ListUsers :many
select
    sqlc.embed(u),
    count(a.id) as num_apps
from
    users u
left join
    apps a on a.user_id = u.id
where
    u.role = ?
group by
    u.id
having
    count(a.id) < sqlc.arg(app_limit);

Configuration

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

Playground URL

https://play.sqlc.dev/p/10dd926ec4b5c35ab0e13941177acc01fd6f34b1b28094b98d2bc2e8abf2a6e9

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions