Skip to content

Commit 2bb57ce

Browse files
authored
Merge pull request #201 from swordqiu/hotfix/qj-order-by-group-by-fields-consistency
fix: order by fields must be in group by (compatible for Dameng)
2 parents d41512b + 8af2609 commit 2bb57ce

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

querydefs.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,30 @@ func queryString(tq *SQuery, tmpFields ...IQueryField) string {
195195
}
196196
if tq.groupBy != nil && len(tq.groupBy) > 0 {
197197
buf.WriteString(" GROUP BY ")
198-
for i, f := range tq.groupBy {
198+
groupByFields := make(map[string]IQueryField)
199+
for i := range tq.groupBy {
200+
f := tq.groupBy[i]
201+
if _, ok := groupByFields[f.Reference()]; ok {
202+
continue
203+
}
199204
if i > 0 {
200205
buf.WriteString(", ")
201206
}
202207
buf.WriteString(f.Reference())
208+
groupByFields[f.Reference()] = f
209+
}
210+
// DAMENG SQL Compatibility, all order by fields should be in group by
211+
for i := range tq.orderBy {
212+
f := tq.orderBy[i]
213+
if _, ok := groupByFields[f.field.Reference()]; ok {
214+
continue
215+
}
216+
if ff, ok := f.field.(IFunctionQueryField); ok && ff.IsAggregate() {
217+
continue
218+
}
219+
buf.WriteString(", ")
220+
buf.WriteString(f.field.Reference())
221+
groupByFields[f.field.Reference()] = f.field
203222
}
204223
}
205224
/*if tq.having != nil {
@@ -208,7 +227,8 @@ func queryString(tq *SQuery, tmpFields ...IQueryField) string {
208227
}*/
209228
if tq.orderBy != nil && len(tq.orderBy) > 0 {
210229
buf.WriteString(" ORDER BY ")
211-
for i, f := range tq.orderBy {
230+
for i := range tq.orderBy {
231+
f := tq.orderBy[i]
212232
if i > 0 {
213233
buf.WriteString(", ")
214234
}

0 commit comments

Comments
 (0)