diff --git a/go/vt/wrangler/materializer.go b/go/vt/wrangler/materializer.go index 0418df15a1e..183e62f839d 100644 --- a/go/vt/wrangler/materializer.go +++ b/go/vt/wrangler/materializer.go @@ -535,7 +535,12 @@ func (wr *Wrangler) prepareCreateLookup(ctx context.Context, keyspace string, sp } modified = append(modified, line) } - modified = append(modified, fmt.Sprintf(" `%s` varbinary(128),", vindexToCol)) + + if vindex.Params["data_type"] == "" || strings.EqualFold(vindex.Type, "consistent_lookup_unique") || strings.EqualFold(vindex.Type, "consistent_lookup") { + modified = append(modified, fmt.Sprintf(" `%s` varbinary(128),", vindexToCol)) + } else { + modified = append(modified, fmt.Sprintf(" `%s` `%s`,", vindexToCol, vindex.Params["data_type"])) + } buf := sqlparser.NewTrackedBuffer(nil) fmt.Fprintf(buf, " PRIMARY KEY (") prefix := "" @@ -554,7 +559,11 @@ func (wr *Wrangler) prepareCreateLookup(ctx context.Context, keyspace string, sp for i := range vindexFromCols { buf.Myprintf("%v as %v, ", sqlparser.NewColIdent(sourceVindexColumns[i]), sqlparser.NewColIdent(vindexFromCols[i])) } - buf.Myprintf("keyspace_id() as %v ", sqlparser.NewColIdent(vindexToCol)) + if strings.EqualFold(vindexToCol, "keyspace_id") || strings.EqualFold(vindex.Type, "consistent_lookup_unique") || strings.EqualFold(vindex.Type, "consistent_lookup") { + buf.Myprintf("keyspace_id() as %v ", sqlparser.NewColIdent(vindexToCol)) + } else { + buf.Myprintf("%v as %v ", sqlparser.NewColIdent(vindexToCol), sqlparser.NewColIdent(vindexToCol)) + } buf.Myprintf("from %v", sqlparser.NewTableIdent(sourceTableName)) if vindex.Owner != "" { // Only backfill diff --git a/go/vt/wrangler/materializer_test.go b/go/vt/wrangler/materializer_test.go index 4034f92d639..40dacc05c88 100644 --- a/go/vt/wrangler/materializer_test.go +++ b/go/vt/wrangler/materializer_test.go @@ -1162,6 +1162,117 @@ func TestCreateLookupVindexSameKeyspace(t *testing.T) { t.Errorf("same keyspace: got:\n%v, want\n%v", got, want) } } +func TestCreateCustomizedVindex(t *testing.T) { + ms := &vtctldatapb.MaterializeSettings{ + SourceKeyspace: "ks", + TargetKeyspace: "ks", + } + env := newTestMaterializerEnv(t, ms, []string{"0"}, []string{"0"}) + defer env.close() + + specs := &vschemapb.Keyspace{ + Vindexes: map[string]*vschemapb.Vindex{ + "v": { + Type: "lookup_unique", + Params: map[string]string{ + "table": "ks.lkp", + "from": "c1", + "to": "col2", + "data_type": "bigint(20)", + }, + Owner: "t1", + }, + }, + Tables: map[string]*vschemapb.Table{ + "t1": { + ColumnVindexes: []*vschemapb.ColumnVindex{{ + Name: "v", + Column: "col2", + }}, + }, + }, + } + // Dummy sourceSchema + sourceSchema := "CREATE TABLE `t1` (\n" + + " `col1` int(11) NOT NULL AUTO_INCREMENT,\n" + + " `col2` int(11) DEFAULT NULL,\n" + + " PRIMARY KEY (`id`)\n" + + ") ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1" + + vschema := &vschemapb.Keyspace{ + Sharded: true, + Vindexes: map[string]*vschemapb.Vindex{ + "hash": { + Type: "hash", + }, + }, + Tables: map[string]*vschemapb.Table{ + "t1": { + ColumnVindexes: []*vschemapb.ColumnVindex{{ + Name: "hash", + Column: "col1", + }}, + }, + }, + } + want := &vschemapb.Keyspace{ + Sharded: true, + Vindexes: map[string]*vschemapb.Vindex{ + "hash": { + Type: "hash", + }, + "v": { + Type: "lookup_unique", + Params: map[string]string{ + "table": "ks.lkp", + "from": "c1", + "to": "col2", + "data_type": "bigint(20)", + "write_only": "true", + }, + Owner: "t1", + }, + }, + Tables: map[string]*vschemapb.Table{ + "t1": { + ColumnVindexes: []*vschemapb.ColumnVindex{{ + Name: "hash", + Column: "col1", + }, { + Name: "v", + Column: "col2", + }}, + }, + "lkp": { + ColumnVindexes: []*vschemapb.ColumnVindex{{ + Column: "c1", + Name: "hash", + }}, + }, + }, + } + env.tmc.schema[ms.SourceKeyspace+".t1"] = &tabletmanagerdatapb.SchemaDefinition{ + TableDefinitions: []*tabletmanagerdatapb.TableDefinition{{ + Fields: []*querypb.Field{{ + Name: "col1", + Type: querypb.Type_INT64, + }, { + Name: "col2", + Type: querypb.Type_INT64, + }}, + Schema: sourceSchema, + }}, + } + if err := env.topoServ.SaveVSchema(context.Background(), ms.SourceKeyspace, vschema); err != nil { + t.Fatal(err) + } + + _, got, _, err := env.wr.prepareCreateLookup(context.Background(), ms.SourceKeyspace, specs) + require.NoError(t, err) + if !proto.Equal(got, want) { + t.Errorf("customize create lookup error same: got:\n%v, want\n%v", got, want) + } +} func TestCreateLookupVindexFailures(t *testing.T) { topoServ := memorytopo.NewServer("cell")