{"record":{"id":"eb3b9c64f362f762","repo":"vitessio/vitess","slug":"vindex-column-cannot-be-a-complex-expression-v-eb3b9c","errorCode":null,"errorMessage":"vindex column cannot be a complex expression: %v","messagePattern":"vindex column cannot be a complex expression: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/wrangler/materializer.go","lineNumber":1489,"sourceCode":"func matchColInSelect(col sqlparser.IdentifierCI, sel *sqlparser.Select) (*sqlparser.ColName, error) {\n\tfor _, selExpr := range sel.GetColumns() {\n\t\tswitch selExpr := selExpr.(type) {\n\t\tcase *sqlparser.StarExpr:\n\t\t\treturn &sqlparser.ColName{Name: col}, nil\n\t\tcase *sqlparser.AliasedExpr:\n\t\t\tmatch := selExpr.As\n\t\t\tif match.IsEmpty() {\n\t\t\t\tif colExpr, ok := selExpr.Expr.(*sqlparser.ColName); ok {\n\t\t\t\t\tmatch = colExpr.Name\n\t\t\t\t} else {\n\t\t\t\t\t// Cannot match against a complex expression.\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t}\n\t\t\tif match.Equal(col) {\n\t\t\t\tcolExpr, ok := selExpr.Expr.(*sqlparser.ColName)\n\t\t\t\tif !ok {\n\t\t\t\t\treturn nil, fmt.Errorf(\"vindex column cannot be a complex expression: %v\", sqlparser.String(selExpr))\n\t\t\t\t}\n\t\t\t\treturn colExpr, nil\n\t\t\t}\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"unsupported select expression: %v\", sqlparser.String(selExpr))\n\t\t}\n\t}\n\treturn nil, fmt.Errorf(\"could not find vindex column %v\", sqlparser.String(col))\n}\n\nfunc (mz *materializer) createStreams(ctx context.Context, insertsMap map[string]string) error {\n\treturn mz.forAllTargets(func(target *topo.ShardInfo) error {\n\t\tinserts := insertsMap[target.ShardName()]\n\t\ttargetPrimary, err := mz.wr.ts.GetTablet(ctx, target.PrimaryAlias)\n\t\tif err != nil {\n\t\t\treturn vterrors.Wrapf(err, \"GetTablet(%v) failed\", target.PrimaryAlias)\n\t\t}\n\t\tbuf := &strings.Builder{}","sourceCodeStart":1471,"sourceCodeEnd":1507,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/wrangler/materializer.go#L1471-L1507","documentation":"generateInserts must locate each primary-vindex column in the SourceExpression's SELECT list to build INSERT statements. matchColInSelect found the matching select expression, but it is not a simple column reference (it is a computed expression like CONCAT(name), lower(email), or a function call), so the vindex value cannot be mapped and the error is returned.","triggerScenarios":"SourceExpression selects an expression (function/arithmetical) for a column that is the target table's primary vindex column, e.g. `select concat(first,' ',last) as name from users` where `name` is the vindex column.","commonSituations":"Materializing a sharded table while transforming the vindex column in the SELECT; renaming computed columns with aliases that collide with vindex column names.","solutions":["Select the vindex column as a plain column reference (no functions/expressions), then do transformations after copy","Compute the value upstream in the source table and select the stored column","Materialize into an unsharded or reference table first, then transform"],"exampleFix":"// before\n{\"sourceExpression\":\"select concat(first,' ',last) as name from users\"}  // name is primary vindex\n// after\n{\"sourceExpression\":\"select first, last, name from users\"}","handlingStrategy":"validation","validationCode":"for _, vcol := range primaryVindexColumns {\n    sel, _ := parser.Parse(sourceExpr)\n    if _, err := findVindexColInSelect(sel.(*sqlparser.Select), sqlparser.NewColName(vcol)); err != nil {\n        return fmt.Errorf(\"vindex column %s must be a plain column in SourceExpression\", vcol)\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never wrap primary vindex columns in functions or expressions in copy filters","Select vindex columns verbatim from the source table","Do transformations post-copy, not in the filter"],"tags":["vitess","wrangler","vindex","validation"],"backgroundTag":"vindex-column-expression","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}