Skip to content

MIN() returns an interface{} #1965

Open

Description

@abh

Version

Other

What happened?

(version 1.16.0)

select min(id) from authors; creates a function that returns an interface{} instead of a NULL-able version of the id type.

#1574 talks about this for DATETIME types, but other issues indicates that it should work for simpler types, maybe?

If not, what's the workaround? Parsing the interface{} is awkward, best I can tell.

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

No response

Playground URL

https://play.sqlc.dev/p/96e9cf0291f2e13cc784144f8abd4d5a556c549f2bfe909a26377703f671ff61

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Nov 23, 2022
  2. d-tsuji commented on Nov 23, 2022

    @d-tsuji
    Contributor

    You can avoid this by casting a typecast. A PostgreSQL example is below.

    -- name: GetMinID :one
    select min(id)::BIGINT from authors;

    Explicit typecasting will generate the intended code.

    func (q *Queries) GetMinID(ctx context.Context) (int64, error) {
    	row := q.db.QueryRowContext(ctx, getMinID)
    	var column_1 int64
    	err := row.Scan(&column_1)
    	return column_1, err
    }

    In v1.16.0, aggregate functions such as MIN() and MAX() are not capable of generating fields of type depending on the column.

  3. abh commented on Dec 19, 2022

    @abh
    Author

    @d-tsuji I'm using MySQL for this project and the equivalent features (CAST(foo as BIGINT)) don't seem to be implemented.

  4. andrewmbenton commented on Jun 6, 2023

    @andrewmbenton
    Member
  5. abh commented on Jul 18, 2023

    @abh
    Author

    It didn't work for my actual use case, but I noticed that in a simple use case doing a sub-select sometimes(?) makes sqlc figure out the correct type.

    select id from (select min(id) from authors) as min_author;

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

  6. sknig commented on Jun 8, 2025

    @sknig

    The workaround I use is to add the result to 0. SELECT 0 + count(value) FROM table; gets correctly recognized as an int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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