@@ -42,10 +42,21 @@ type TableSchema struct {
4242 ForcedIndexForVerification string // Forced index name
4343 PaginationKeyColumn * schema.TableColumn
4444 PaginationKeyIndex int
45+ GeneratedColumnIndecesOnRawTable []int
4546
4647 rowMd5Query string
4748}
4849
50+ // isColumnGenerated evaluates wheter a go_myslq.schema.TableColumn is generated or not.
51+ func isColumnGenerated (tc * schema.TableColumn ) bool {
52+ return tc .IsVirtual || tc .IsStored
53+ }
54+
55+ // isColumnIndexGenerated evaluetes whether a TableSchema column is generated, by index.
56+ func (t * TableSchema ) IsColumnIndexGenerated (idx int ) bool {
57+ return isColumnGenerated (& t .Columns [idx ])
58+ }
59+
4960// This query returns the MD5 hash for a row on this table. This query is valid
5061// for both the source and the target shard.
5162//
@@ -90,11 +101,12 @@ func (t *TableSchema) RowMd5Query() string {
90101 }
91102
92103 columns := make ([]schema.TableColumn , 0 , len (t .Columns ))
93- for _ , column := range t .Columns {
104+ for i , column := range t .Columns {
94105 _ , isCompressed := t .CompressedColumnsForVerification [column .Name ]
95106 _ , isIgnored := t .IgnoredColumnsForVerification [column .Name ]
107+ isGenerated := t .IsColumnIndexGenerated (i )
96108
97- if isCompressed || isIgnored || column . IsVirtual {
109+ if isCompressed || isIgnored || isGenerated {
98110 continue
99111 }
100112
@@ -151,6 +163,53 @@ func MaxPaginationKeys(db *sql.DB, tables []*TableSchema, logger *logrus.Entry)
151163 return tablesWithData , emptyTables , nil
152164}
153165
166+ // removeInvisibleIndeces removes all invisible idx references from a go_mysql.schema.Table.
167+ func removeInvisibleIndeces (ts * schema.Table ) {
168+ j := 0
169+ for i , index := range ts .Indexes {
170+ if ! index .Visible {
171+ continue
172+ }
173+ ts .Indexes [j ] = ts .Indexes [i ]
174+ j ++
175+ }
176+ ts .Indexes = ts .Indexes [:j ]
177+ }
178+
179+ // removeGeneratedColumns removes all generated (virtual, stored) columns from a go_mysql.schema.Table.
180+ // Returns a slice removed column indeces.
181+ func removeGeneratedColumns (ts * schema.Table ) []int {
182+ removedIdxs := []int {}
183+
184+ newColumns := make ([]schema.TableColumn , 0 , len (ts .Columns ))
185+ newPKColumns := make ([]int , 0 , len (ts .Columns ))
186+ newUnsignedColumns := make ([]int , 0 , len (ts .Columns ))
187+
188+ j := 0
189+ for i , col := range ts .Columns {
190+ if isColumnGenerated (& col ) {
191+ removedIdxs = append (removedIdxs , i )
192+ continue
193+ }
194+
195+ // regenerate PK and unsigned column indeces
196+ newColumns = append (newColumns , col )
197+ if ts .IsPrimaryKey (i ) {
198+ newPKColumns = append (newPKColumns , j )
199+ }
200+ if col .IsUnsigned {
201+ newUnsignedColumns = append (newUnsignedColumns , j )
202+ }
203+ j ++
204+ }
205+
206+ ts .Columns = newColumns
207+ ts .PKColumns = newPKColumns
208+ ts .UnsignedColumns = newUnsignedColumns
209+
210+ return removedIdxs
211+ }
212+
154213func LoadTables (db * sql.DB , tableFilter TableFilter , columnCompressionConfig ColumnCompressionConfig , columnIgnoreConfig ColumnIgnoreConfig , forceIndexConfig ForceIndexConfig , cascadingPaginationColumnConfig * CascadingPaginationColumnConfig ) (TableSchemaCache , error ) {
155214 logger := logrus .WithField ("tag" , "table_schema_cache" )
156215
@@ -189,20 +248,16 @@ func LoadTables(db *sql.DB, tableFilter TableFilter, columnCompressionConfig Col
189248 return tableSchemaCache , err
190249 }
191250
192- // Filter out invisible indexes
193- visibleIndexes := make ([]* schema.Index , 0 , len (tableSchema .Indexes ))
194- for _ , index := range tableSchema .Indexes {
195- if index .Visible {
196- visibleIndexes = append (visibleIndexes , index )
197- }
198- }
199- tableSchema .Indexes = visibleIndexes
251+ // filter out unwanted indeces and columns
252+ removeInvisibleIndeces (tableSchema )
253+ generatedColumnIdxs := removeGeneratedColumns (tableSchema )
200254
201255 tableSchemas = append (tableSchemas , & TableSchema {
202256 Table : tableSchema ,
203257 CompressedColumnsForVerification : columnCompressionConfig .CompressedColumnsFor (dbname , table ),
204258 IgnoredColumnsForVerification : columnIgnoreConfig .IgnoredColumnsFor (dbname , table ),
205259 ForcedIndexForVerification : forceIndexConfig .IndexFor (dbname , table ),
260+ GeneratedColumnIndecesOnRawTable : generatedColumnIdxs ,
206261 })
207262 }
208263
@@ -448,7 +503,7 @@ func maxPaginationKey(db *sql.DB, table *TableSchema) (PaginationKey, bool, erro
448503 if err != nil {
449504 break
450505 }
451-
506+
452507 var binValue []byte
453508 switch v := val .(type ) {
454509 case []byte :
@@ -458,7 +513,7 @@ func maxPaginationKey(db *sql.DB, table *TableSchema) (PaginationKey, bool, erro
458513 default :
459514 err = fmt .Errorf ("expected binary/string for max key, got %T" , val )
460515 }
461-
516+
462517 if err == nil {
463518 result = NewBinaryKeyWithColumn (primaryKeyColumn .Name , binValue )
464519 }
0 commit comments