{"record":{"id":"f72922d954f012dc","repo":"vitessio/vitess","slug":"err-f72922","errorCode":null,"errorMessage":"err","messagePattern":"err","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtgate/planbuilder/operators/union.go","lineNumber":172,"sourceCode":"\nfunc (u *Union) GetSelectFor(source int) *sqlparser.Select {\n\tsrc := u.Sources[source]\n\tfor {\n\t\tswitch op := src.(type) {\n\t\tcase *Horizon:\n\t\t\treturn getFirstSelect(op.Query)\n\t\tcase *Route:\n\t\t\tsrc = op.Source\n\t\tdefault:\n\t\t\tpanic(vterrors.VT13001(\"expected all sources of the UNION to be horizons\"))\n\t\t}\n\t}\n}\n\nfunc (u *Union) AddWSColumn(ctx *plancontext.PlanningContext, offset int, underRoute bool) int {\n\toutputOffset, err := u.addWeightStringToOffset(ctx, offset)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn outputOffset\n}\n\nfunc (u *Union) AddColumn(ctx *plancontext.PlanningContext, reuse bool, gb bool, expr *sqlparser.AliasedExpr) int {\n\tif reuse {\n\t\toffset := u.FindCol(ctx, expr.Expr, false)\n\t\tif offset >= 0 {\n\t\t\treturn offset\n\t\t}\n\t}\n\n\tswitch e := expr.Expr.(type) {\n\tcase *sqlparser.ColName:\n\t\tcols := u.GetColumns(ctx)\n\t\t// here we deal with pure column access on top of the union\n\t\toffset := slices.IndexFunc(cols, func(column *sqlparser.AliasedExpr) bool {\n\t\t\tif column.ColumnName() == expr.ColumnName() {","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtgate/planbuilder/operators/union.go#L154-L190","documentation":"Union.AddWSColumn converts addWeightStringToOffset's error into a panic. When appending a weight_string() column on top of a UNION (needed for ORDER BY collation handling), the helper failed — e.g. the requested offset has no matching source column or branches are inconsistent — so Vitess aborts planning. The message is the bare underlying error ('err'), making it opaque without a stack trace.","triggerScenarios":"Calling Union.AddWSColumn(ctx, offset, underRoute) where addWeightStringToOffset returns an error, which is immediately panicked. Reached when planning ORDER BY on a UNION that requires weight_string columns.","commonSituations":"Queries ordering a UNION by a column requiring collation-aware sorting (weight_string) where the UNION branches' columns don't line up; typically hit on unhandled query shapes during development.","solutions":["Rewrite the query so the ORDER BY column exists identically in every UNION branch.","Avoid ORDER BY expressions requiring weight_string on UNION results (e.g. order in each branch or use an explicit compatible column).","If valid SQL triggers it, file a Vitess issue including the query and schema."],"exampleFix":"// before\nSELECT a FROM t1 UNION SELECT b FROM t2 ORDER BY a COLLATE utf8_bin;\n// after: order by a column present in all branches\nSELECT a FROM t1 UNION SELECT b AS a FROM t2 ORDER BY a;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        err = fmt.Errorf(\"union weight_string planning failed: %v\", r)\n    }\n}()","preventionTips":["ORDER BY columns on UNIONs must exist identically in all branches.","Prefer ordering by a select-list alias over complex expressions.","Test collation-sensitive ORDER BY queries against UNIONs before deploying."],"tags":["query-planning","union","weight-string","panic"],"backgroundTag":"union-orderby-weight-string","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}