forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_valued_index_test.go
318 lines (293 loc) · 11.6 KB
/
multi_valued_index_test.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2022 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package multivaluedindex
import (
"context"
"testing"
"github.com/pingcap/tidb/pkg/errno"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/sessiontxn"
"github.com/pingcap/tidb/pkg/table"
"github.com/pingcap/tidb/pkg/tablecodec"
"github.com/pingcap/tidb/pkg/testkit"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/codec"
"github.com/stretchr/testify/require"
)
func TestWriteMultiValuedIndex(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(pk int primary key, a json, index idx((cast(a as signed array))))")
tk.MustExec("insert into t1 values (1, '[1,2,2,3]')")
tk.MustExec("insert into t1 values (2, '[1,2,3]')")
tk.MustExec("insert into t1 values (3, '[]')")
tk.MustExec("insert into t1 values (4, '[2,3,4]')")
tk.MustExec("insert into t1 values (5, null)")
tk.MustExec("insert into t1 values (6, '1')")
t1, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 11)
checkKey(t, t1.IndexPrefix(), index, store, [][]types.Datum{
{types.NewDatum(nil), types.NewIntDatum(5)},
{types.NewIntDatum(1), types.NewIntDatum(1)},
{types.NewIntDatum(1), types.NewIntDatum(2)},
{types.NewIntDatum(1), types.NewIntDatum(6)},
{types.NewIntDatum(2), types.NewIntDatum(1)},
{types.NewIntDatum(2), types.NewIntDatum(2)},
{types.NewIntDatum(2), types.NewIntDatum(4)},
{types.NewIntDatum(3), types.NewIntDatum(1)},
{types.NewIntDatum(3), types.NewIntDatum(2)},
{types.NewIntDatum(3), types.NewIntDatum(4)},
{types.NewIntDatum(4), types.NewIntDatum(4)},
})
}
}
tk.MustExec("delete from t1")
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 0)
}
}
tk.MustExec("drop table t1")
tk.MustExec("create table t1(pk int primary key, a json, index idx((cast(a as char(5) array))))")
tk.MustExec("insert into t1 values (1, '[\"abc\", \"abc \"]')")
tk.MustExec("insert into t1 values (2, '[\"b\"]')")
tk.MustExec("insert into t1 values (3, '[\"b \"]')")
tk.MustQuery("select pk from t1 where 'b ' member of (a)").Check(testkit.Rows("3"))
t1, err = dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 4)
checkKey(t, t1.IndexPrefix(), index, store, [][]types.Datum{
{types.NewBytesDatum([]byte("abc")), types.NewIntDatum(1)},
{types.NewBytesDatum([]byte("abc ")), types.NewIntDatum(1)},
{types.NewBytesDatum([]byte("b")), types.NewIntDatum(2)},
{types.NewBytesDatum([]byte("b ")), types.NewIntDatum(3)},
})
}
}
tk.MustExec("update t1 set a = json_array_append(a, '$', 'bcd') where pk = 1")
tk.MustExec("update t1 set a = '[]' where pk = 2")
tk.MustExec("update t1 set a = '[\"abc\"]' where pk = 3")
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 4)
checkKey(t, t1.IndexPrefix(), index, store, [][]types.Datum{
{types.NewBytesDatum([]byte("abc")), types.NewIntDatum(1)},
{types.NewBytesDatum([]byte("abc")), types.NewIntDatum(3)},
{types.NewBytesDatum([]byte("abc ")), types.NewIntDatum(1)},
{types.NewBytesDatum([]byte("bcd")), types.NewIntDatum(1)},
})
}
}
tk.MustExec("delete from t1")
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 0)
}
}
tk.MustExec("drop table t1")
tk.MustExec("create table t1(pk int primary key, a json, index idx((cast(a as unsigned array))))")
tk.MustExec("insert into t1 values (1, '[1,2,2,3]')")
tk.MustExec("insert into t1 values (2, '[1,2,3]')")
tk.MustExec("insert into t1 values (3, '[]')")
tk.MustExec("insert into t1 values (4, '[2,3,4]')")
tk.MustExec("insert into t1 values (5, null)")
tk.MustExec("insert into t1 values (6, '1')")
t1, err = dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 11)
checkKey(t, t1.IndexPrefix(), index, store, [][]types.Datum{
{types.NewDatum(nil), types.NewIntDatum(5)},
{types.NewUintDatum(1), types.NewIntDatum(1)},
{types.NewUintDatum(1), types.NewIntDatum(2)},
{types.NewUintDatum(1), types.NewIntDatum(6)},
{types.NewUintDatum(2), types.NewIntDatum(1)},
{types.NewUintDatum(2), types.NewIntDatum(2)},
{types.NewUintDatum(2), types.NewIntDatum(4)},
{types.NewUintDatum(3), types.NewIntDatum(1)},
{types.NewUintDatum(3), types.NewIntDatum(2)},
{types.NewUintDatum(3), types.NewIntDatum(4)},
{types.NewUintDatum(4), types.NewIntDatum(4)},
})
}
}
tk.MustExec("delete from t1")
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 0)
}
}
}
func TestWriteMultiValuedIndexPartitionTable(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t1
(
pk int primary key,
a json,
index idx ((cast(a as signed array)))
) partition by range columns (pk) (partition p0 values less than (10), partition p1 values less than (20));`)
tk.MustExec("insert into t1 values (1, '[1,2,2,3]')")
tk.MustExec("insert into t1 values (11, '[1,2,3]')")
tk.MustExec("insert into t1 values (2, '[]')")
tk.MustExec("insert into t1 values (12, '[2,3,4]')")
tk.MustExec("insert into t1 values (3, null)")
tk.MustExec("insert into t1 values (13, null)")
t1, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
expect := map[string]struct {
count int
vals [][]types.Datum
}{
"p0": {4, [][]types.Datum{
{types.NewDatum(nil), types.NewIntDatum(3)},
{types.NewIntDatum(1), types.NewIntDatum(1)},
{types.NewIntDatum(2), types.NewIntDatum(1)},
{types.NewIntDatum(3), types.NewIntDatum(1)},
}},
"p1": {7, [][]types.Datum{
{types.NewDatum(nil), types.NewIntDatum(13)},
{types.NewIntDatum(1), types.NewIntDatum(11)},
{types.NewIntDatum(2), types.NewIntDatum(11)},
{types.NewIntDatum(2), types.NewIntDatum(12)},
{types.NewIntDatum(3), types.NewIntDatum(11)},
{types.NewIntDatum(3), types.NewIntDatum(12)},
{types.NewIntDatum(4), types.NewIntDatum(12)},
}},
}
for _, def := range t1.Meta().GetPartitionInfo().Definitions {
partition := t1.(table.PartitionedTable).GetPartition(def.ID)
for _, index := range partition.Indices() {
if index.Meta().MVIndex {
checkCount(t, partition.IndexPrefix(), index, store, expect[def.Name.L].count)
checkKey(t, partition.IndexPrefix(), index, store, expect[def.Name.L].vals)
}
}
}
tk.MustExec("delete from t1")
for _, def := range t1.Meta().GetPartitionInfo().Definitions {
partition := t1.(table.PartitionedTable).GetPartition(def.ID)
for _, index := range partition.Indices() {
if index.Meta().MVIndex {
checkCount(t, partition.IndexPrefix(), index, store, 0)
}
}
}
}
func TestWriteMultiValuedIndexUnique(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(pk int primary key, a json, unique index idx((cast(a as signed array))))")
tk.MustExec("insert into t1 values (1, '[1,2,2]')")
tk.MustGetErrCode("insert into t1 values (2, '[1]')", errno.ErrDupEntry)
tk.MustExec("insert into t1 values (3, '[3,3,4]')")
t1, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 4)
checkKey(t, t1.IndexPrefix(), index, store, [][]types.Datum{
{types.NewIntDatum(1)},
{types.NewIntDatum(2)},
{types.NewIntDatum(3)},
{types.NewIntDatum(4)},
})
}
}
}
func TestWriteMultiValuedIndexComposite(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(pk int primary key, a json, c int, d int, index idx(c, (cast(a as signed array)), d))")
tk.MustExec("insert into t1 values (1, '[1,2,2]', 1, 1)")
tk.MustExec("insert into t1 values (2, '[2,2,2]', 2, 2)")
tk.MustExec("insert into t1 values (3, '[3,3,4]', 3, 3)")
tk.MustExec("insert into t1 values (4, null, 4, 4)")
tk.MustExec("insert into t1 values (5, '[]', 5, 5)")
t1, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
for _, index := range t1.Indices() {
if index.Meta().MVIndex {
checkCount(t, t1.IndexPrefix(), index, store, 6)
checkKey(t, t1.IndexPrefix(), index, store, [][]types.Datum{
{types.NewIntDatum(1), types.NewIntDatum(1), types.NewIntDatum(1), types.NewIntDatum(1)},
{types.NewIntDatum(1), types.NewIntDatum(2), types.NewIntDatum(1), types.NewIntDatum(1)},
{types.NewIntDatum(2), types.NewIntDatum(2), types.NewIntDatum(2), types.NewIntDatum(2)},
{types.NewIntDatum(3), types.NewIntDatum(3), types.NewIntDatum(3), types.NewIntDatum(3)},
{types.NewIntDatum(3), types.NewIntDatum(4), types.NewIntDatum(3), types.NewIntDatum(3)},
{types.NewIntDatum(4), types.NewDatum(nil), types.NewIntDatum(4), types.NewIntDatum(4)},
})
}
}
}
func checkCount(t *testing.T, prefix kv.Key, index table.Index, store kv.Storage, except int) {
c := 0
checkIndex(t, prefix, index, store, func(it kv.Iterator) {
c++
})
require.Equal(t, except, c)
}
func checkKey(t *testing.T, prefix kv.Key, index table.Index, store kv.Storage, except [][]types.Datum) {
idx := 0
checkIndex(t, prefix, index, store, func(it kv.Iterator) {
indexKey := decodeIndexKey(t, it.Key())
require.Equal(t, except[idx], indexKey)
idx++
})
}
func checkIndex(t *testing.T, prefix kv.Key, index table.Index, store kv.Storage, fn func(kv.Iterator)) {
startKey := codec.EncodeInt(prefix, index.Meta().ID)
prefix.Next()
se := testkit.NewTestKit(t, store).Session()
err := sessiontxn.NewTxn(context.Background(), se)
require.NoError(t, err)
txn, err := se.Txn(true)
require.NoError(t, err)
it, err := txn.Iter(startKey, prefix.PrefixNext())
require.NoError(t, err)
for it.Valid() && it.Key().HasPrefix(prefix) {
fn(it)
err = it.Next()
require.NoError(t, err)
}
it.Close()
se.Close()
}
func decodeIndexKey(t *testing.T, key kv.Key) []types.Datum {
var idLen = 8
var prefixLen = 1 + idLen /*tableID*/ + 2
_, _, isRecord, err := tablecodec.DecodeKeyHead(key)
require.NoError(t, err)
require.False(t, isRecord)
indexKey := key[prefixLen+idLen:]
var datumValues []types.Datum
for len(indexKey) > 0 {
remain, d, err := codec.DecodeOne(indexKey)
require.NoError(t, err)
datumValues = append(datumValues, d)
indexKey = remain
}
return datumValues
}