Skip to content

CustomExpression parenthesizes DISTINCT sub-expression, producing invalid aggregate SQL #594

Description

@brianmaissy

Description

In v2.15.0, CustomExpression now parenthesizes sub-expressions (related to the IS_NOT_NULL parenthesization fix). This breaks the pattern of nesting DISTINCT(column) inside CustomExpression for aggregate ORDER BY clauses.

Reproducer

nameColumn := pg.StringColumn("name")
people := pg.NewTable("public", "people", "", nameColumn)

expr := pg.CustomExpression(pg.DISTINCT(nameColumn), pg.Token("ORDER BY"), nameColumn)
agg := pg.Func("array_agg", expr)
stmt := pg.SELECT(agg.AS("names")).FROM(people)
q, _ := stmt.Sql()
fmt.Println(q)

v2.14.1 (correct)

SELECT array_agg(DISTINCT people.name ORDER BY people.name) AS "names"
FROM public.people;

v2.15.0 (broken)

SELECT array_agg((DISTINCT people.name) ORDER BY people.name) AS "names"
FROM public.people;

The extra parentheses around DISTINCT people.name produce a SQL syntax error:

pq: syntax error at or near "DISTINCT" at position 2:19 (42601)

Workaround

Use Token("DISTINCT") instead of DISTINCT():

expr := pg.CustomExpression(pg.Token("DISTINCT"), nameColumn, pg.Token("ORDER BY"), nameColumn)

This produces correct SQL because Token is not parenthesized.

Environment

  • jet v2.15.0
  • PostgreSQL 18.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions