sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.37k stars 803 forks source link

Wrong type generation on multi column compare #1549

Closed InbarPerry closed 1 year ago

InbarPerry commented 2 years ago

Version

Other

What happened?

When comparing 2 columns with different types, the type of the first one is generated for both columns as input. This is common use case when implementing keyset pagination on time, where some tie-breaker is needed.

Relevant log output

type ListAuthorsParams struct {
    CreatedAt sql.NullTime
    ID        sql.NullTime
}

Both columns are generated as sql.NullTime even though ID should be generated as a string.

Database schema

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text,
  created_at timestamp with time zone
);

SQL queries

-- name: ListAuthors :many
SELECT * FROM authors
WHERE (created_at, id) < (@created_at, @id)
ORDER BY created_at DESC, id DESC
LIMIT 500;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/35226bc3aac300aebd4eeaa42e2a258b4567e691ab99de4e701d32c37316da83

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

InbarPerry commented 2 years ago

I managed to workaround this issue by explicit cast the type:

-- name: ListAuthors :many
SELECT * FROM authors
WHERE (created_at, id) < (@created_at, @id::text)
ORDER BY created_at DESC, id DESC
LIMIT 500;