forked from vitessio/vitess
/
index_analysis.go
137 lines (126 loc) · 3.25 KB
/
index_analysis.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package planbuilder
import (
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/sqlparser"
)
type IndexScore struct {
Index *schema.Index
ColumnMatch []bool
MatchFailed bool
}
type scoreValue int64
const (
NO_MATCH = scoreValue(-1)
PERFECT_SCORE = scoreValue(0)
)
func NewIndexScore(index *schema.Index) *IndexScore {
return &IndexScore{index, make([]bool, len(index.Columns)), false}
}
func (is *IndexScore) FindMatch(columnName string) int {
if is.MatchFailed {
return -1
}
if index := is.Index.FindColumn(columnName); index != -1 {
is.ColumnMatch[index] = true
return index
}
// If the column is among the data columns, we can still use
// the index without going to the main table
if index := is.Index.FindDataColumn(columnName); index == -1 {
is.MatchFailed = true
}
return -1
}
func (is *IndexScore) GetScore() scoreValue {
if is.MatchFailed {
return NO_MATCH
}
score := NO_MATCH
for i, indexColumn := range is.ColumnMatch {
if indexColumn {
score = scoreValue(is.Index.Cardinality[i])
continue
}
return score
}
return PERFECT_SCORE
}
func NewIndexScoreList(indexes []*schema.Index) []*IndexScore {
scoreList := make([]*IndexScore, len(indexes))
for i, v := range indexes {
scoreList[i] = NewIndexScore(v)
}
return scoreList
}
func getPKValues(conditions []sqlparser.BoolExpr, pkIndex *schema.Index) (pkValues []interface{}, err error) {
pkIndexScore := NewIndexScore(pkIndex)
pkValues = make([]interface{}, len(pkIndexScore.ColumnMatch))
for _, condition := range conditions {
condition, ok := condition.(*sqlparser.ComparisonExpr)
if !ok {
return nil, nil
}
if !sqlparser.StringIn(condition.Operator, sqlparser.AST_EQ, sqlparser.AST_IN) {
return nil, nil
}
index := pkIndexScore.FindMatch(string(condition.Left.(*sqlparser.ColName).Name))
if index == -1 {
return nil, nil
}
switch condition.Operator {
case sqlparser.AST_EQ, sqlparser.AST_IN:
var err error
pkValues[index], err = sqlparser.AsInterface(condition.Right)
if err != nil {
return nil, err
}
default:
panic("unreachable")
}
}
if pkIndexScore.GetScore() == PERFECT_SCORE {
return pkValues, nil
}
return nil, nil
}
func getIndexMatch(conditions []sqlparser.BoolExpr, indexes []*schema.Index) *schema.Index {
indexScores := NewIndexScoreList(indexes)
for _, condition := range conditions {
var col string
switch condition := condition.(type) {
case *sqlparser.ComparisonExpr:
col = string(condition.Left.(*sqlparser.ColName).Name)
case *sqlparser.RangeCond:
col = string(condition.Left.(*sqlparser.ColName).Name)
default:
panic("unreachaable")
}
for _, index := range indexScores {
index.FindMatch(col)
}
}
highScore := NO_MATCH
highScorer := -1
for i, index := range indexScores {
curScore := index.GetScore()
if curScore == NO_MATCH {
continue
}
if curScore == PERFECT_SCORE {
highScorer = i
break
}
// Prefer secondary index over primary key
if curScore >= highScore {
highScore = curScore
highScorer = i
}
}
if highScorer == -1 {
return nil
}
return indexes[highScorer]
}