forked from pingcap/tidb
/
range.go
138 lines (125 loc) · 3.21 KB
/
range.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
138
// Copyright 2017 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 types
import (
"fmt"
"math"
"strconv"
"strings"
"github.com/pingcap/tidb/sessionctx/variable"
)
// IntColumnRange represents a range for a integer column, both low and high are inclusive.
type IntColumnRange struct {
LowVal int64
HighVal int64
}
// IsPoint returns if the table range is a point.
func (tr *IntColumnRange) IsPoint() bool {
return tr.HighVal == tr.LowVal
}
func (tr IntColumnRange) String() string {
var l, r string
if tr.LowVal == math.MinInt64 {
l = "(-inf"
} else {
l = "[" + strconv.FormatInt(tr.LowVal, 10)
}
if tr.HighVal == math.MaxInt64 {
r = "+inf)"
} else if tr.HighVal == math.MinInt64 {
// This branch is for nil
r = "-inf)"
} else {
r = strconv.FormatInt(tr.HighVal, 10) + "]"
}
return l + "," + r
}
// ColumnRange represents a range for a column.
type ColumnRange struct {
Low Datum
High Datum
lowExcl bool
highExcl bool
}
// IndexRange represents a range for an index.
type IndexRange struct {
LowVal []Datum
HighVal []Datum
LowExclude bool // Low value is exclusive.
HighExclude bool // High value is exclusive.
}
// IsPoint returns if the index range is a point.
func (ir *IndexRange) IsPoint(sc *variable.StatementContext) bool {
if len(ir.LowVal) != len(ir.HighVal) {
return false
}
for i := range ir.LowVal {
a := ir.LowVal[i]
b := ir.HighVal[i]
if a.Kind() == KindMinNotNull || b.Kind() == KindMaxValue {
return false
}
cmp, err := a.CompareDatum(sc, b)
if err != nil {
return false
}
if cmp != 0 {
return false
}
}
return !ir.LowExclude && !ir.HighExclude
}
func (ir *IndexRange) String() string {
lowStrs := make([]string, 0, len(ir.LowVal))
for _, d := range ir.LowVal {
lowStrs = append(lowStrs, formatDatum(d))
}
highStrs := make([]string, 0, len(ir.LowVal))
for _, d := range ir.HighVal {
highStrs = append(highStrs, formatDatum(d))
}
l, r := "[", "]"
if ir.LowExclude {
l = "("
}
if ir.HighExclude {
r = ")"
}
return l + strings.Join(lowStrs, " ") + "," + strings.Join(highStrs, " ") + r
}
// Align appends low value and high value up to the number of columns with max value, min not null value or null value.
func (ir *IndexRange) Align(numColumns int) {
for i := len(ir.LowVal); i < numColumns; i++ {
if ir.LowExclude {
ir.LowVal = append(ir.LowVal, MaxValueDatum())
} else {
ir.LowVal = append(ir.LowVal, Datum{})
}
}
for i := len(ir.HighVal); i < numColumns; i++ {
if ir.HighExclude {
ir.HighVal = append(ir.HighVal, Datum{})
} else {
ir.HighVal = append(ir.HighVal, MaxValueDatum())
}
}
}
func formatDatum(d Datum) string {
if d.Kind() == KindMinNotNull {
return "-inf"
}
if d.Kind() == KindMaxValue {
return "+inf"
}
return fmt.Sprintf("%v", d.GetValue())
}