storj / dbx

A neat codegen-based database wrapper written in Go
Apache License 2.0
24 stars 3 forks source link

CRDB requires GROUP BY to be before ORDER BY #8

Closed mniewrzal closed 12 months ago

mniewrzal commented 1 year ago

It looks that if GROUP BY is after ORDER BY crdb is returning error ERROR: at or near "group": syntax error (SQLSTATE 42601)

Sample query

read limitoffset (
    select bucket_metainfo.project_id bucket_metainfo.name
    where bucket_metainfo.project_id > ?
    where bucket_metainfo.name > ?
    groupby bucket_metainfo.project_id bucket_metainfo.name
    orderby (
        asc bucket_metainfo.project_id 
        asc bucket_metainfo.name
    )
)

SELECT bucket_metainfos.project_id, bucket_metainfos.name FROM bucket_metainfos WHERE bucket_metainfos.project_id > ? AND bucket_metainfos.name > ? ORDER BY bucket_metainfos.project_id, bucket_metainfos.name GROUP BY bucket_metainfos.project_id, bucket_metainfos.name LIMIT ? OFFSET ?

See example https://review.dev.storj.io/c/storj/storj/+/11211 and failing test TestBatchBuckets/Cockroach

storj-gerrit[bot] commented 12 months ago

Change ir/xform,sql: move group by before order by mentions this issue.