forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
104 lines (89 loc) · 2.6 KB
/
util.go
File metadata and controls
104 lines (89 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2018 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 diff
import (
"math/rand"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb-tools/pkg/dbutil"
"github.com/pingcap/tidb-tools/pkg/utils"
"github.com/pingcap/tidb/types"
log "github.com/sirupsen/logrus"
)
func equalStrings(str1, str2 []string) bool {
if len(str1) != len(str2) {
return false
}
for i := 0; i < len(str1); i++ {
if str1[i] != str2[i] {
return false
}
}
return true
}
func removeColumns(tableInfo *model.TableInfo, columns []string) *model.TableInfo {
if len(columns) == 0 {
return tableInfo
}
removeColMap := utils.SliceToMap(columns)
for i := 0; i < len(tableInfo.Indices); i++ {
index := tableInfo.Indices[i]
for j := 0; j < len(index.Columns); j++ {
col := index.Columns[j]
if _, ok := removeColMap[col.Name.O]; ok {
index.Columns = append(index.Columns[:j], index.Columns[j+1:]...)
j--
if len(index.Columns) == 0 {
tableInfo.Indices = append(tableInfo.Indices[:i], tableInfo.Indices[i+1:]...)
i--
}
}
}
}
for j := 0; j < len(tableInfo.Columns); j++ {
col := tableInfo.Columns[j]
if _, ok := removeColMap[col.Name.O]; ok {
tableInfo.Columns = append(tableInfo.Columns[:j], tableInfo.Columns[j+1:]...)
j--
}
}
return tableInfo
}
func getColumnsFromIndex(index *model.IndexInfo, tableInfo *model.TableInfo) []*model.ColumnInfo {
indexColumns := make([]*model.ColumnInfo, 0, len(index.Columns))
for _, indexColumn := range index.Columns {
for _, column := range tableInfo.Columns {
if column.Name.O == indexColumn.Name.O {
indexColumns = append(indexColumns, column)
}
}
}
return indexColumns
}
func getRandomN(total, num int) []int {
if num > total {
log.Warnf("the num %d is greater than total %d", num, total)
num = total
}
totalArray := make([]int, 0, total)
for i := 0; i < total; i++ {
totalArray = append(totalArray, i)
}
for j := 0; j < num; j++ {
r := j + rand.Intn(total-j)
totalArray[j], totalArray[r] = totalArray[r], totalArray[j]
}
return totalArray[:num]
}
func needQuotes(ft types.FieldType) bool {
return !(dbutil.IsNumberType(ft.Tp) || dbutil.IsFloatType(ft.Tp))
}