{"record":{"id":"2cbb5a7e96a3e582","repo":"vitessio/vitess","slug":"unsupported-non-column-name-in-sum-clause-v","errorCode":null,"errorMessage":"unsupported non-column name in sum clause: %v","messagePattern":"unsupported non-column name in sum clause: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go","lineNumber":507,"sourceCode":"\t}\n\tif expr, ok := aliased.Expr.(sqlparser.AggrFunc); ok {\n\t\tif sqlparser.IsDistinct(expr) {\n\t\t\treturn nil, fmt.Errorf(\"unsupported distinct expression usage: %v\", sqlparser.String(expr))\n\t\t}\n\t\tswitch fname := expr.AggrName(); fname {\n\t\tcase \"count\":\n\t\t\tif _, ok := expr.(*sqlparser.CountStar); !ok {\n\t\t\t\treturn nil, fmt.Errorf(\"only count(*) is supported: %v\", sqlparser.String(expr))\n\t\t\t}\n\t\t\tcexpr.operation = opCount\n\t\t\treturn cexpr, nil\n\t\tcase \"sum\":\n\t\t\tif len(expr.GetArgs()) != 1 {\n\t\t\t\treturn nil, fmt.Errorf(\"unsupported multiple columns in sum clause: %v\", sqlparser.String(expr))\n\t\t\t}\n\t\t\tinnerCol, ok := expr.GetArg().(*sqlparser.ColName)\n\t\t\tif !ok {\n\t\t\t\treturn nil, fmt.Errorf(\"unsupported non-column name in sum clause: %v\", sqlparser.String(expr))\n\t\t\t}\n\t\t\tif !innerCol.Qualifier.IsEmpty() {\n\t\t\t\treturn nil, fmt.Errorf(\"unsupported qualifier for column: %v\", sqlparser.String(innerCol))\n\t\t\t}\n\t\t\tcexpr.operation = opSum\n\t\t\tcexpr.expr = innerCol\n\t\t\ttpb.addCol(innerCol.Name)\n\t\t\tcexpr.references[innerCol.Name.String()] = true\n\t\t\treturn cexpr, nil\n\t\t}\n\t}\n\terr := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {\n\t\tswitch node := node.(type) {\n\t\tcase *sqlparser.ColName:\n\t\t\tif !node.Qualifier.IsEmpty() {\n\t\t\t\treturn false, fmt.Errorf(\"unsupported qualifier for column: %v\", sqlparser.String(node))\n\t\t\t}\n\t\t\ttpb.addCol(node.Name)","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go#L489-L525","documentation":"SUM's single argument must be a plain column reference in vreplication table plans. Expressions like SUM(a+b), SUM(a*b) or nested aggregates are rejected because the plan builder can only count/replay values of one column (opSum stores a *sqlparser.ColName).","triggerScenarios":"Materialize/vreplication SELECT with SUM(<non-column>), e.g. SUM(price * qty) or SUM(CONCAT(a,b)); analyzeExpr's type assertion of the arg to *sqlparser.ColName fails.","commonSituations":"Users porting existing reporting SQL into a Materialize workflow; computed columns needing expression aggregation that the simple materializer cannot evaluate.","solutions":["Change SUM to take a single bare column, e.g. SUM(price).","Materialize the computed value into a real column first, then SUM that column.","Do the expression aggregation outside vreplication (query the materialized table with VTGate instead)."],"exampleFix":"// before\nselect sum(price * qty) as total from t\n// after\nselect sum(price_times_qty) as total from t  -- with price_times_qty a stored column","handlingStrategy":"validation","validationCode":"// Ensure SUM's argument is a bare column\nfunc validSumArgIsColumn(e sqlparser.Expr) bool {\n    ag, ok := e.(*sqlparser.AliasedExpr)\n    if !ok { return true }\n    sum, ok := ag.Expr.(*sqlparser.Sum)\n    if !ok { return true }\n    _, isCol := sum.Args[0].(*sqlparser.ColName)\n    return isCol\n}","typeGuard":"func isColName(n sqlparser.SQLNode) bool { _, ok := n.(*sqlparser.ColName); return ok }","tryCatchPattern":null,"preventionTips":["Never pass computed expressions to SUM in workflow queries.","Materialize computed values into stored columns first."],"tags":["vreplication","sql","aggregation","vttablet"],"backgroundTag":"unsupported-aggregate-expression","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}