{"record":{"id":"67601139698a0bcd","repo":"vitessio/vitess","slug":"unsupported-multiple-columns-in-sum-clause-v","errorCode":null,"errorMessage":"unsupported multiple columns in sum clause: %v","messagePattern":"unsupported multiple columns in sum clause: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go","lineNumber":503,"sourceCode":"\t\t\t// The vstreamer responds with \"keyspace_id\" as the field name for this request.\n\t\t\tcexpr.expr = &sqlparser.ColName{Name: sqlparser.NewIdentifierCI(\"keyspace_id\")}\n\t\t\treturn cexpr, nil\n\t\t}\n\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:","sourceCodeStart":485,"sourceCodeEnd":521,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vttablet/tabletmanager/vreplication/table_plan_builder.go#L485-L521","documentation":"SUM in a vreplication table plan must take exactly one argument. analyzeExpr rejects SUM(a, b) or SUM() with 'unsupported multiple columns in sum clause' because the plan builder stores only a single column reference for the opSum operation.","triggerScenarios":"A Materialize/vreplication SELECT containing SUM with zero or more than one argument, e.g. SELECT SUM(a, b) FROM t; analyzeExpr (via analyzeExprs) hits the len(expr.GetArgs()) != 1 branch.","commonSituations":"Hand-edited materialization queries; users trying to sum multiple columns in one aggregate instead of summing each column separately; copy-paste from spreadsheets-style formulas.","solutions":["Split into multiple single-argument sums: SUM(a), SUM(b).","If you need a+b then sum the result upstream or compute an expression column outside vreplication.","Ensure the SUM argument is exactly one unqualified column name (see also non-column and qualifier restrictions)."],"exampleFix":"// before\nselect sum(a, b) as total from t\n// after\nselect sum(a) as sum_a, sum(b) as sum_b from t","handlingStrategy":"validation","validationCode":"// Ensure SUM has exactly one argument before building the plan\nfunc validSumArgCount(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    return len(sum.Args) == 1\n}","typeGuard":"func isSingleArgSum(n sqlparser.SQLNode) (ok bool) { s, is := n.(*sqlparser.Sum); return is && len(s.Args) == 1 }","tryCatchPattern":null,"preventionTips":["Always write SUM(col) with exactly one column argument.","Split multi-column sums into separate select-list items."],"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"}