Refuse a group_by naming an aggregate column

A group_by key that repeats a folded column made the column a grouping
key and an aggregate at once, and for avg it left the upstream query
grouping on a column the rewrite had removed, so the request failed as
an opaque 502.

- refuse a group_by field that names a folded aggregate or the avg column
- cover the function-then-field ordering of the existing clash check
This commit is contained in:
2026-09-07 00:24:57 +10:00
parent b499e962af
commit 3eb7d53fe8
2 changed files with 53 additions and 0 deletions
+10
View File
@@ -138,6 +138,9 @@ func parseAggregate(query string) (*aggregateSpec, error) {
}
for _, node := range ast[2:] {
for _, f := range groupByFields(node) {
if hasAgg(spec.aggs, f) || (spec.avg && f == avgColumn) {
return nil, groupByClash(f)
}
spec.keys = appendUnique(spec.keys, f)
}
}
@@ -157,6 +160,13 @@ func columnClash(name string) error {
return fmt.Errorf("extract projects the column %q more than once: openvoxdb returns the repeat as %q, which pdbmux can neither key on nor fold", name, name+"_2")
}
// groupByClash refuses a group_by naming an aggregate column: the same key
// cannot both identify a row and be folded across backends. openvoxdb rejects
// the shape too, so refusing here is a 400 instead of a failed upstream query.
func groupByClash(name string) error {
return fmt.Errorf("group_by names the aggregate column %q: pdbmux folds that column across backends, so it cannot also be a grouping key", name)
}
// rewriteAvg replaces the client's avg column with the sum and count of the same
// expression, so the true weighted average can be computed from the shards:
// Postgres avg(x) is sum(x)/count(x), and both of those do combine.