sqlc-dev / sqlc

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

Distinct JSON_ARRAYAGG causes syntax error with MySQL #3676

Open dalobstah opened 3 weeks ago

dalobstah commented 3 weeks ago

Version

1.27.0

What happened?

MySQL and MariaDB support using DISTINCT for columns in JSON_ARRAYAGG to ensure unique values. GROUP_CONCAT also supports this syntax which seems to work as expected in sqlc. When I try using JSON_ARRAYAGG(DISTINCT <column>) however, I get a syntax error on valid SQL.

Syntax for JSON_ARRAYAGG from the MariaDB docs:

JSON_ARRAYAGG([DISTINCT] expr
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [LIMIT {[offset,] row_count | row_count OFFSET offset}])

Relevant log output

# package issue
query.sql:2:30: syntax error near "DISTINCT `id`) FROM `ids`;" "

Database schema

CREATE TABLE ids (
  id   BIGINT
);

SQL queries

-- name: GetIDs :one
SELECT JSON_ARRAYAGG(DISTINCT `id`) FROM `ids`;

Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "issue"
        out: "issue"

Playground URL

https://play.sqlc.dev/p/8d89731835ebee2564e1351fe6354772a4047787fa675975e5c318ad965fc632

What operating system are you using?

Windows, macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go