{"record":{"id":"7f77835b2a91e131","repo":"vitessio/vitess","slug":"group-by-expression-does-not-reference-an-alias-in","errorCode":null,"errorMessage":"group by expression does not reference an alias in the select list: %v","messagePattern":"group by expression does not reference an alias in the select list: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go","lineNumber":561,"sourceCode":"func (tpb *tablePlanBuilder) addCol(ident sqlparser.IdentifierCI) {\n\ttpb.sendSelect.AddSelectExpr(&sqlparser.AliasedExpr{\n\t\tExpr: &sqlparser.ColName{Name: ident},\n\t})\n}\n\nfunc (tpb *tablePlanBuilder) analyzeGroupBy(groupBy *sqlparser.GroupBy) error {\n\tif groupBy == nil {\n\t\t// If there's no grouping, the it's an insertNormal.\n\t\treturn nil\n\t}\n\tfor _, expr := range groupBy.Exprs {\n\t\tcolname, ok := expr.(*sqlparser.ColName)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"unsupported non-column name or alias in group by clause: %v\", sqlparser.String(expr))\n\t\t}\n\t\tcexpr := tpb.findCol(colname.Name)\n\t\tif cexpr == nil {\n\t\t\treturn fmt.Errorf(\"group by expression does not reference an alias in the select list: %v\", sqlparser.String(expr))\n\t\t}\n\t\tif cexpr.operation != opExpr {\n\t\t\treturn fmt.Errorf(\"group by expression is not allowed to reference an aggregate expression: %v\", sqlparser.String(expr))\n\t\t}\n\t\tcexpr.isGrouped = true\n\t}\n\t// If all colExprs are grouped, then it's an insertIgnore.\n\ttpb.onInsert = insertIgnore\n\tfor _, cExpr := range tpb.colExprs {\n\t\tif !cExpr.isGrouped {\n\t\t\t// If some colExprs are not grouped, then it's an insertOnDup.\n\t\t\ttpb.onInsert = insertOnDup\n\t\t\tbreak\n\t\t}\n\t}\n\treturn nil\n}\n","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go#L543-L579","documentation":"Every GROUP BY column must match an alias defined in the SELECT list of a vreplication table plan. findCol looks up the group-by column among tpb.colExprs; if not found, the builder cannot correlate the grouping with what is being materialized and errors out.","triggerScenarios":"Materialize/vreplication SELECT ... GROUP BY col where col is not one of the (aliased) select-list items, e.g. SELECT sum(x) AS s FROM t GROUP BY y; analyzeGroupBy's findCol(y) returns nil.","commonSituations":"Users grouping by a column they forgot to select; queries where the group-by column has a different name than the select alias.","solutions":["Add the grouping column to the SELECT list with an alias matching the GROUP BY name.","Ensure the GROUP BY name exactly matches the select-list alias/column name.","Remember the supported pattern: SELECT <groupcol>, <aggregates> ... GROUP BY <groupcol>."],"exampleFix":"// before\nselect sum(x) as s from t group by y\n// after\nselect y, sum(x) as s from t group by y","handlingStrategy":"validation","validationCode":"// Require every GROUP BY name to appear as a select-list column alias\nfunc groupByInSelectList(sel *sqlparser.Select) bool {\n    names := map[string]bool{}\n    for _, e := range sel.SelectExprs {\n        if ae, ok := e.(*sqlparser.AliasedExpr); ok {\n            if c, ok := ae.Expr.(*sqlparser.ColName); ok {\n                n := ae.As.String(); if n == \"\" { n = c.Name.String() }\n                names[n] = true\n            }\n        }\n    }\n    for _, e := range sel.GroupBy.Exprs {\n        c, ok := e.(*sqlparser.ColName)\n        if !ok || !names[c.Name.String()] { return false }\n    }\n    return true\n}","typeGuard":"func groupColSelected(c *sqlparser.ColName, names map[string]bool) bool { return names[c.Name.String()] }","tryCatchPattern":null,"preventionTips":["Always include the grouping column in the SELECT list.","Match GROUP BY names exactly to select-list aliases."],"tags":["vreplication","sql","group-by","vttablet"],"backgroundTag":"unsupported-group-by-expression","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}