-
Notifications
You must be signed in to change notification settings - Fork 310
syncer(dm): centralize row image layout mapping #12750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sqlmodel | ||
|
|
||
| import "github.com/pingcap/tidb/pkg/meta/model" | ||
|
|
||
| // RowImageLayout describes how a binlog row image maps to source table columns. | ||
| type RowImageLayout struct { | ||
| columns []*model.ColumnInfo | ||
| visibleColumns []*model.ColumnInfo | ||
| visibleOffsetByColumnOffset []int | ||
| writableColumns []*model.ColumnInfo | ||
| } | ||
|
|
||
| // NewRowImageLayout creates a RowImageLayout from source and target TableInfo. | ||
| func NewRowImageLayout(source, target *model.TableInfo) RowImageLayout { | ||
| return NewRowImageLayoutFromColumns(source.Columns, target.Columns) | ||
| } | ||
|
|
||
| // NewRowImageLayoutFromColumns creates a RowImageLayout from source and target columns. | ||
| func NewRowImageLayoutFromColumns(sourceColumns, targetColumns []*model.ColumnInfo) RowImageLayout { | ||
| visibleColumns := VisibleColumns(sourceColumns) | ||
| visibleOffsetByColumnOffset := make([]int, len(sourceColumns)) | ||
| for i := range visibleOffsetByColumnOffset { | ||
| visibleOffsetByColumnOffset[i] = -1 | ||
| } | ||
| for i, column := range visibleColumns { | ||
| visibleOffsetByColumnOffset[column.Offset] = i | ||
| } | ||
|
|
||
| layout := RowImageLayout{ | ||
| columns: sourceColumns, | ||
| visibleColumns: visibleColumns, | ||
| visibleOffsetByColumnOffset: visibleOffsetByColumnOffset, | ||
| } | ||
| if targetColumns != nil { | ||
| layout.writableColumns = writableSourceColumns(visibleColumns, targetColumns) | ||
| } | ||
| return layout | ||
| } | ||
|
Comment on lines
+32
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In func NewRowImageLayoutFromColumns(sourceColumns, targetColumns []*model.ColumnInfo) RowImageLayout {\n\tvisibleColumns := VisibleColumns(sourceColumns)\n\tmaxOffset := -1\n\tfor _, column := range sourceColumns {\n\t\tif column.Offset > maxOffset {\n\t\t maxOffset = column.Offset\n\t\t}\n\t}\n\tvisibleOffsetByColumnOffset := make([]int, maxOffset+1)\n\tfor i := range visibleOffsetByColumnOffset {\n\t\tvisibleOffsetByColumnOffset[i] = -1\n\t}\n\tfor i, column := range visibleColumns {\n\t\tif column.Offset >= 0 && column.Offset <= maxOffset {\n\t\t visibleOffsetByColumnOffset[column.Offset] = i\n\t\t}\n\t}\n\n\tlayout := RowImageLayout{\n\t\tcolumns: sourceColumns,\n\t\tvisibleColumns: visibleColumns,\n\t\tvisibleOffsetByColumnOffset: visibleOffsetByColumnOffset,\n\t}\n\tif targetColumns != nil {\n\t\tlayout.writableColumns = writableSourceColumns(visibleColumns, targetColumns)\n\t}\n\treturn layout\n} |
||
|
|
||
| // VisibleColumns returns the visible columns from the given table columns. | ||
| func VisibleColumns(columns []*model.ColumnInfo) []*model.ColumnInfo { | ||
| ret := make([]*model.ColumnInfo, 0, len(columns)) | ||
| for _, col := range columns { | ||
| if !col.Hidden { | ||
| ret = append(ret, col) | ||
| } | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| // VisibleColumnCount returns the number of visible columns. | ||
| func VisibleColumnCount(columns []*model.ColumnInfo) int { | ||
| count := 0 | ||
| for _, col := range columns { | ||
| if !col.Hidden { | ||
| count++ | ||
| } | ||
| } | ||
| return count | ||
| } | ||
|
|
||
| // VisibleColumns returns the visible source columns in the row image. | ||
| func (l RowImageLayout) VisibleColumns() []*model.ColumnInfo { | ||
| return l.visibleColumns | ||
| } | ||
|
|
||
| // VisibleColumnCount returns the visible source column count in the row image. | ||
| func (l RowImageLayout) VisibleColumnCount() int { | ||
| return len(l.visibleColumns) | ||
| } | ||
|
|
||
| // WritableColumns returns visible source columns that can be written to the target table. | ||
| func (l RowImageLayout) WritableColumns() []*model.ColumnInfo { | ||
| return l.writableColumns | ||
| } | ||
|
|
||
| // FullValues expands a visible-only row image to the source table column layout. | ||
| func (l RowImageLayout) FullValues(row []any) ([]any, bool) { | ||
| if len(row) == len(l.columns) { | ||
| return row, true | ||
| } | ||
| if len(row) != len(l.visibleColumns) { | ||
| return nil, false | ||
| } | ||
|
|
||
| fullValues := make([]any, len(l.columns)) | ||
| for i, col := range l.visibleColumns { | ||
| fullValues[col.Offset] = row[i] | ||
| } | ||
| return fullValues, true | ||
| } | ||
|
Comment on lines
+91
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In func (l RowImageLayout) FullValues(row []any) ([]any, bool) {\n\tif len(row) == len(l.columns) {\n\t return row, true\n\t}\n\tif len(row) != len(l.visibleColumns) {\n\t return nil, false\n\t}\n\n\tfullValues := make([]any, len(l.columns))\n\tfor i, col := range l.visibleColumns {\n\t\tif col.Offset >= 0 && col.Offset < len(fullValues) {\n\t\t fullValues[col.Offset] = row[i]\n\t\t}\n\t}\n\treturn fullValues, true\n} |
||
|
|
||
| // SourceColumnCountForVisibleColumnCount returns the source-column prefix width | ||
| // that contains exactly columnCount visible columns. | ||
| func (l RowImageLayout) SourceColumnCountForVisibleColumnCount(columnCount int) (int, bool) { | ||
| visibleCount := 0 | ||
| sourceColumnCount := len(l.columns) | ||
| for i, col := range l.columns { | ||
| if col.Hidden { | ||
| continue | ||
| } | ||
| visibleCount++ | ||
| if visibleCount > columnCount { | ||
| sourceColumnCount = i | ||
| break | ||
| } | ||
| } | ||
| if visibleCount < columnCount { | ||
| return 0, false | ||
| } | ||
| return sourceColumnCount, true | ||
| } | ||
|
|
||
| func (l RowImageLayout) isFullValues(values []any) bool { | ||
| return len(values) == len(l.columns) | ||
| } | ||
|
|
||
| func (l RowImageLayout) columnsForValues(values []any) []*model.ColumnInfo { | ||
| if l.isFullValues(values) { | ||
| return l.columns | ||
| } | ||
| return l.visibleColumns | ||
| } | ||
|
|
||
| func (l RowImageLayout) columnsAndValuesByIndex( | ||
| indexInfo *model.IndexInfo, | ||
| values []any, | ||
| ) ([]*model.ColumnInfo, []any) { | ||
| cols := make([]*model.ColumnInfo, 0, len(indexInfo.Columns)) | ||
| vals := make([]any, 0, len(indexInfo.Columns)) | ||
| for _, column := range indexInfo.Columns { | ||
| offset := l.valueOffset(column.Offset, values) | ||
| cols = append(cols, l.columns[column.Offset]) | ||
| vals = append(vals, values[offset]) | ||
| } | ||
| return cols, vals | ||
| } | ||
|
|
||
| func (l RowImageLayout) valuesByIndex(indexInfo *model.IndexInfo, values []any) []any { | ||
| ret := make([]any, 0, len(indexInfo.Columns)) | ||
| if values == nil { | ||
| return ret | ||
| } | ||
| for _, column := range indexInfo.Columns { | ||
| offset := l.valueOffset(column.Offset, values) | ||
| ret = append(ret, values[offset]) | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| func (l RowImageLayout) valueOffset(columnOffset int, values []any) int { | ||
| if l.isFullValues(values) { | ||
| return columnOffset | ||
| } | ||
| return l.visibleOffsetByColumnOffset[columnOffset] | ||
| } | ||
|
Comment on lines
+164
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In func (l RowImageLayout) valueOffset(columnOffset int, values []any) int {\n\tif l.isFullValues(values) {\n\t return columnOffset\n\t}\n\tif columnOffset < 0 || columnOffset >= len(l.visibleOffsetByColumnOffset) {\n\t return -1\n\t}\n\treturn l.visibleOffsetByColumnOffset[columnOffset]\n} |
||
|
|
||
| func (l RowImageLayout) valueByOffset(columnOffset int, values []any) any { | ||
| return values[l.valueOffset(columnOffset, values)] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package sqlmodel | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| timodel "github.com/pingcap/tidb/pkg/meta/model" | ||
| "github.com/pingcap/tiflow/pkg/util/testutil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRowImageLayoutWithInterleavedHiddenColumn(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tableInfo := mockTableInfo(t, "CREATE TABLE t ("+ | ||
| "id INT PRIMARY KEY, a INT, b INT, UNIQUE KEY idx_expr ((a + b)))") | ||
| hidden := testutil.HiddenColumnName(t, tableInfo) | ||
| testutil.ReorderColumnsByName(t, tableInfo, "id", "a", hidden, "b") | ||
|
|
||
| layout := NewRowImageLayout(tableInfo, tableInfo) | ||
| require.Equal(t, 3, layout.VisibleColumnCount()) | ||
| require.Equal(t, []string{"id", "a", "b"}, columnNames(layout.VisibleColumns())) | ||
| require.Equal(t, 2, layout.valueOffset(3, []any{1, 2, 3})) | ||
| require.Equal(t, 3, layout.valueByOffset(3, []any{1, 2, 3})) | ||
|
|
||
| fullValues, ok := layout.FullValues([]any{1, 2, 3}) | ||
| require.True(t, ok) | ||
| require.Equal(t, []any{1, 2, nil, 3}, fullValues) | ||
|
|
||
| sourceColumnCount, ok := layout.SourceColumnCountForVisibleColumnCount(2) | ||
| require.True(t, ok) | ||
| require.Equal(t, 3, sourceColumnCount) | ||
|
|
||
| sourceColumnCount, ok = layout.SourceColumnCountForVisibleColumnCount(3) | ||
| require.True(t, ok) | ||
| require.Equal(t, len(tableInfo.Columns), sourceColumnCount) | ||
|
|
||
| _, ok = layout.SourceColumnCountForVisibleColumnCount(4) | ||
| require.False(t, ok) | ||
| } | ||
|
|
||
| func columnNames(columns []*timodel.ColumnInfo) []string { | ||
| names := make([]string, 0, len(columns)) | ||
| for _, column := range columns { | ||
| names = append(names, column.Name.L) | ||
| } | ||
| return names | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function
tableInfoForVisibleColumnCountdoes not check if the inputtableInfoisnil. If anilpointer is passed, callingtableInfo.Columnswill cause a nil pointer dereference panic. Adding a defensivenilcheck at the beginning of the function will make it more robust.