-
Notifications
You must be signed in to change notification settings - Fork 0
/
lattice.go
787 lines (691 loc) · 17.6 KB
/
lattice.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// Copyright 2019 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 schemacmp
import (
"fmt"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/types"
)
type IncompatibleError struct {
Msg string
Args []interface{}
}
const (
ErrMsgTypeMismatch = "type mismatch (%T vs %T)"
ErrMsgTupleLengthMismatch = "tuple length mismatch (%d vs %d)"
ErrMsgDistinctSingletons = "distinct singletons (%v vs %v)"
ErrMsgIncompatibleType = "incompatible mysql type (%v vs %v)"
ErrMsgAtTupleIndex = "at tuple index %d: %v"
ErrMsgAtMapKey = "at map key %q: %v"
ErrMsgNonInclusiveBitSets = "non-inclusive bit sets (%#x vs %#x)"
ErrMsgContradictingOrders = "combining contradicting orders (%d && %d)"
ErrMsgStringListElemMismatch = "at string list index %d: distinct values (%q vs %q)"
)
func (e *IncompatibleError) Error() string {
return fmt.Sprintf(e.Msg, e.Args...)
}
func typeMismatchError(a, b Lattice) *IncompatibleError {
return &IncompatibleError{
Msg: ErrMsgTypeMismatch,
Args: []interface{}{a, b},
}
}
func tupleLengthMismatchError(a, b int) *IncompatibleError {
return &IncompatibleError{
Msg: ErrMsgTupleLengthMismatch,
Args: []interface{}{a, b},
}
}
func distinctSingletonsErrors(a, b interface{}) *IncompatibleError {
return &IncompatibleError{
Msg: ErrMsgDistinctSingletons,
Args: []interface{}{a, b},
}
}
func incompatibleTypeError(a, b interface{}) *IncompatibleError {
return &IncompatibleError{
Msg: ErrMsgIncompatibleType,
Args: []interface{}{a, b},
}
}
func wrapTupleIndexError(i int, inner error) *IncompatibleError {
return &IncompatibleError{
Msg: ErrMsgAtTupleIndex,
Args: []interface{}{i, inner},
}
}
func wrapMapKeyError(key string, inner error) *IncompatibleError {
return &IncompatibleError{
Msg: ErrMsgAtMapKey,
Args: []interface{}{key, inner},
}
}
// Lattice is implemented for types which forms a join-semilattice.
type Lattice interface {
// Unwrap returns the underlying object supporting the lattice. This
// operation is deep.
Unwrap() interface{}
// Compare this instance with another instance.
//
// Returns -1 if `self < other`, 0 if `self == other`, 1 if `self > other`.
// Returns `IncompatibleError` if the two instances are not ordered.
Compare(other Lattice) (int, error)
// Join finds the "least upper bound" of two Lattice instances. The result
// is `>=` both inputs. Returns an error if the join does not exist.
Join(other Lattice) (Lattice, error)
}
// Bool is a boolean implementing Lattice where `false < true`.
type Bool bool
// Unwrap implements Lattice
func (a Bool) Unwrap() interface{} {
return bool(a)
}
// Compare implements Lattice.
func (a Bool) Compare(other Lattice) (int, error) {
b, ok := other.(Bool)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a == b:
return 0, nil
case bool(a):
return 1, nil
default:
return -1, nil
}
}
// Join implements Lattice
func (a Bool) Join(other Lattice) (Lattice, error) {
b, ok := other.(Bool)
if !ok {
return nil, typeMismatchError(a, other)
}
return a || b, nil
}
type singleton struct{ value interface{} }
// Unwrap implements Lattice
func (a singleton) Unwrap() interface{} {
return a.value
}
// Singleton wraps an unordered value. Distinct instances of Singleton are
// incompatible.
func Singleton(value interface{}) Lattice {
return singleton{value: value}
}
// Compare implements Lattice.
func (a singleton) Compare(other Lattice) (int, error) {
b, ok := other.(singleton)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a.value != b.value:
return 0, distinctSingletonsErrors(a.value, b.value)
default:
return 0, nil
}
}
// Join implements Lattice
func (a singleton) Join(other Lattice) (Lattice, error) {
b, ok := other.(singleton)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case a.value != b.value:
return nil, distinctSingletonsErrors(a.value, b.value)
default:
return a, nil
}
}
// Equality allows custom equality.
type Equality interface {
// Equals returns true if this instance should be equal to another object.
Equals(other Equality) bool
}
type equalitySingleton struct{ Equality }
// EqualitySingleton wraps an unordered value with equality defined by custom
// code instead of the `==` operator.
func EqualitySingleton(value Equality) Lattice {
return equalitySingleton{Equality: value}
}
// Unwrap implements Lattice.
func (a equalitySingleton) Unwrap() interface{} {
return a.Equality
}
// Compare implements Lattice.
func (a equalitySingleton) Compare(other Lattice) (int, error) {
b, ok := other.(equalitySingleton)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case !a.Equals(b.Equality):
return 0, distinctSingletonsErrors(a.Equality, b.Equality)
default:
return 0, nil
}
}
// Join implements Lattice
func (a equalitySingleton) Join(other Lattice) (Lattice, error) {
b, ok := other.(equalitySingleton)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case !a.Equals(b.Equality):
return nil, distinctSingletonsErrors(a.Equality, b.Equality)
default:
return a, nil
}
}
// BitSet is a set of bits where `a < b` iff `a` is a subset of `b`.
type BitSet uint
// Unwrap implements Lattice.
func (a BitSet) Unwrap() interface{} {
return uint(a)
}
// Compare implements Lattice.
func (a BitSet) Compare(other Lattice) (int, error) {
b, ok := other.(BitSet)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a == b:
return 0, nil
case a&^b == 0:
return -1, nil
case b&^a == 0:
return 1, nil
default:
return 0, &IncompatibleError{
Msg: ErrMsgNonInclusiveBitSets,
Args: []interface{}{a, b},
}
}
}
// Join implements Lattice.
func (a BitSet) Join(other Lattice) (Lattice, error) {
b, ok := other.(BitSet)
if !ok {
return nil, typeMismatchError(a, other)
}
return a | b, nil
}
// Byte is a byte implementing Lattice.
type Byte byte
// Unwrap implements Lattice.
func (a Byte) Unwrap() interface{} {
return byte(a)
}
// Compare implements Lattice.
func (a Byte) Compare(other Lattice) (int, error) {
b, ok := other.(Byte)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a == b:
return 0, nil
case a > b:
return 1, nil
default:
return -1, nil
}
}
// Join implements Lattice.
func (a Byte) Join(other Lattice) (Lattice, error) {
b, ok := other.(Byte)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case a >= b:
return a, nil
default:
return b, nil
}
}
// fieldTp is a mysql column field type implementing lattice.
// It is used for the column field type (`github.com/pingcap/tidb/parser/types.FieldType.Tp`).
type fieldTp struct {
value byte
}
// FieldTp is used for the column field type (`github.com/pingcap/tidb/parser/types.FieldType.Tp`).
func FieldTp(value byte) Lattice {
return fieldTp{value: value}
}
// Unwrap implements Lattice
func (a fieldTp) Unwrap() interface{} {
return a.value
}
// Compare implements Lattice.
func (a fieldTp) Compare(other Lattice) (int, error) {
b, ok := other.(fieldTp)
if !ok {
return 0, typeMismatchError(a, other)
}
if a.value == b.value {
return 0, nil
}
// TODO: add more comparable type check here.
// maybe we can ref https://github.com/pingcap/tidb/blob/38f4d869d86c3b274e7d1998a52243a30b125c80/types/field_type.go#L325 later.
if mysql.IsIntegerType(a.value) && mysql.IsIntegerType(b.value) {
// special handle for integer type.
return compareMySQLIntegerType(a.value, b.value), nil
}
if types.IsTypeBlob(a.value) && types.IsTypeBlob(b.value) {
// special handle for blob type.
return compareMySQLBlobType(a.value, b.value), nil
}
return 0, incompatibleTypeError(a.value, b.value)
}
// Join implements Lattice.
func (a fieldTp) Join(other Lattice) (Lattice, error) {
b, ok := other.(fieldTp)
if !ok {
return nil, typeMismatchError(a, other)
}
if a.value == b.value {
return a, nil
}
if mysql.IsIntegerType(a.value) && mysql.IsIntegerType(b.value) {
// special handle for integer type.
if compareMySQLIntegerType(a.value, b.value) < 0 {
return b, nil
}
return a, nil
}
if types.IsTypeBlob(a.value) && types.IsTypeBlob(b.value) {
// special handle for blob type.
if compareMySQLBlobType(a.value, b.value) < 0 {
return b, nil
}
return a, nil
}
return nil, incompatibleTypeError(a.value, b.value)
}
// Int is an int implementing Lattice.
type Int int
// Unwrap implements Lattice.
func (a Int) Unwrap() interface{} {
return int(a)
}
// Compare implements Lattice.
func (a Int) Compare(other Lattice) (int, error) {
b, ok := other.(Int)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a == b:
return 0, nil
case a > b:
return 1, nil
default:
return -1, nil
}
}
// Join implements Lattice.
func (a Int) Join(other Lattice) (Lattice, error) {
b, ok := other.(Int)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case a >= b:
return a, nil
default:
return b, nil
}
}
// Int64 is an int64 implementing Lattice.
type Int64 int64
// Unwrap implements Lattice.
func (a Int64) Unwrap() interface{} {
return int64(a)
}
// Compare implements Lattice.
func (a Int64) Compare(other Lattice) (int, error) {
b, ok := other.(Int64)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a == b:
return 0, nil
case a > b:
return 1, nil
default:
return -1, nil
}
}
// Join implements Lattice.
func (a Int64) Join(other Lattice) (Lattice, error) {
b, ok := other.(Int64)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case a >= b:
return a, nil
default:
return b, nil
}
}
// Uint is a uint implementing Lattice.
type Uint uint
// Unwrap implements Lattice.
func (a Uint) Unwrap() interface{} {
return uint(a)
}
// Compare implements Lattice.
func (a Uint) Compare(other Lattice) (int, error) {
b, ok := other.(Uint)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a == b:
return 0, nil
case a > b:
return 1, nil
default:
return -1, nil
}
}
// Join implements Lattice.
func (a Uint) Join(other Lattice) (Lattice, error) {
b, ok := other.(Uint)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case a >= b:
return a, nil
default:
return b, nil
}
}
// Tuple of Lattice instances. Given two Tuples `a` and `b`, we define `a <= b`
// iff `a[i] <= b[i]` for all `i`.
type Tuple []Lattice
// Unwrap implements Lattice. The returned type is a `[]interface{}`.
func (a Tuple) Unwrap() interface{} {
res := make([]interface{}, 0, len(a))
for _, value := range a {
res = append(res, value.Unwrap())
}
return res
}
// Compare implements Lattice.
func (a Tuple) Compare(other Lattice) (int, error) {
b, ok := other.(Tuple)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case len(a) != len(b):
return 0, tupleLengthMismatchError(len(a), len(b))
}
result := 0
for i, left := range a {
res, err := left.Compare(b[i])
if err != nil {
return 0, wrapTupleIndexError(i, err)
}
result, err = CombineCompareResult(result, res)
if err != nil {
return 0, wrapTupleIndexError(i, err)
}
}
return result, nil
}
// CombineCompareResult combines two comparison results.
func CombineCompareResult(x int, y int) (int, error) {
switch {
case x == y || y == 0:
return x, nil
case x == 0:
return y, nil
default:
return 0, &IncompatibleError{
Msg: ErrMsgContradictingOrders,
Args: []interface{}{x, y},
}
}
}
// Join implements Lattice
func (a Tuple) Join(other Lattice) (Lattice, error) {
b, ok := other.(Tuple)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case len(a) != len(b):
return nil, tupleLengthMismatchError(len(a), len(b))
}
result := make(Tuple, 0, len(a))
for i, left := range a {
res, err := left.Join(b[i])
if err != nil {
return nil, wrapTupleIndexError(i, err)
}
result = append(result, res)
}
return result, nil
}
type maybe struct{ Lattice }
// Maybe includes `nil` as the universal lower bound of the original Lattice.
func Maybe(inner Lattice) Lattice {
return maybe{Lattice: inner}
}
// MaybeSingletonInterface is a convenient function calling `Maybe(Singleton(value))`.
func MaybeSingletonInterface(value interface{}) Lattice {
if value == nil {
return Maybe(nil)
}
return Maybe(Singleton(value))
}
// MaybeSingletonString is a convenient function calling `Maybe(Singleton(s))`.
func MaybeSingletonString(s string) Lattice {
if len(s) == 0 {
return Maybe(nil)
}
return Maybe(Singleton(s))
}
// Unwrap implements Lattice.
func (a maybe) Unwrap() interface{} {
if a.Lattice != nil {
return a.Lattice.Unwrap()
}
return nil
}
// Compare implements Lattice.
func (a maybe) Compare(other Lattice) (int, error) {
b, ok := other.(maybe)
switch {
case !ok:
return 0, typeMismatchError(a, other)
case a.Lattice == nil && b.Lattice == nil:
return 0, nil
case a.Lattice == nil:
return -1, nil
case b.Lattice == nil:
return 1, nil
default:
return a.Lattice.Compare(b.Lattice)
}
}
// Join implements Lattice.
func (a maybe) Join(other Lattice) (Lattice, error) {
b, ok := other.(maybe)
switch {
case !ok:
return nil, typeMismatchError(a, other)
case a.Lattice == nil:
return b, nil
case b.Lattice == nil:
return a, nil
default:
join, err := a.Lattice.Join(b.Lattice)
if err != nil {
return nil, err
}
return maybe{Lattice: join}, nil
}
}
// StringList is a list of string where `a <= b` iff `a == b[:len(a)]`.
type StringList []string
// Unwrap implements Lattice.
func (a StringList) Unwrap() interface{} {
return []string(a)
}
// Compare implements Lattice.
func (a StringList) Compare(other Lattice) (int, error) {
b, ok := other.(StringList)
if !ok {
return 0, typeMismatchError(a, other)
}
minLen := len(a)
if minLen > len(b) {
minLen = len(b)
}
for i := 0; i < minLen; i++ {
if a[i] != b[i] {
return 0, &IncompatibleError{
Msg: ErrMsgStringListElemMismatch,
Args: []interface{}{i, a[i], b[i]},
}
}
}
switch {
case len(a) == len(b):
return 0, nil
case len(a) < len(b):
return -1, nil
default:
return 1, nil
}
}
// Join implements Lattice.
func (a StringList) Join(other Lattice) (Lattice, error) {
cmp, err := a.Compare(other)
switch {
case err != nil:
return nil, err
case cmp <= 0:
return other, nil
default:
return a, nil
}
}
// LatticeMap is a map of Lattice objects keyed by strings.
type LatticeMap interface {
// New creates an empty LatticeMap of the same type as the receiver.
New() LatticeMap
// Insert inserts a key-value pair into the map.
Insert(key string, value Lattice)
// Get obtains the Lattice object at the given key. Returns nil if the key
// does not exist.
Get(key string) Lattice
// ForEach iterates the map.
ForEach(func(key string, value Lattice) error) error
// CompareWithNil returns the comparison result when the value is compared
// with a non-existing entry.
CompareWithNil(value Lattice) (int, error)
// JoinWithNil returns the result when the value is joined with a
// non-existing entry. If the joined result should be non-existing, this
// method should return nil, nil.
JoinWithNil(value Lattice) (Lattice, error)
// ShouldDeleteIncompatibleJoin returns true if two incompatible entries
// should be deleted instead of propagating the error.
ShouldDeleteIncompatibleJoin() bool
}
type latticeMap struct{ LatticeMap }
// Unwrap implements Lattice.
func (a latticeMap) Unwrap() interface{} {
res := make(map[string]interface{})
a.ForEach(func(key string, value Lattice) error {
res[key] = value.Unwrap()
return nil
})
return res
}
func (a latticeMap) iter(other Lattice, action func(k string, av, bv Lattice) error) error {
b, ok := other.(latticeMap)
if !ok {
return typeMismatchError(a, other)
}
visitedKeys := make(map[string]struct{})
err := a.ForEach(func(k string, av Lattice) error {
visitedKeys[k] = struct{}{}
return action(k, av, b.Get(k))
})
if err != nil {
return err
}
return b.ForEach(func(k string, bv Lattice) error {
if _, ok := visitedKeys[k]; ok {
return nil
}
return action(k, nil, bv)
})
}
// Compare implements Lattice.
func (a latticeMap) Compare(other Lattice) (int, error) {
result := 0
err := a.iter(other, func(k string, av, bv Lattice) error {
var (
cmpRes int
e error
)
switch {
case av != nil && bv != nil:
cmpRes, e = av.Compare(bv)
case av != nil:
cmpRes, e = a.CompareWithNil(av)
default:
cmpRes, e = a.CompareWithNil(bv)
cmpRes = -cmpRes
}
if e != nil {
return wrapMapKeyError(k, e)
}
result, e = CombineCompareResult(result, cmpRes)
if e != nil {
return wrapMapKeyError(k, e)
}
return nil
})
return result, err
}
// Join implements Lattice.
func (a latticeMap) Join(other Lattice) (Lattice, error) {
result := a.New()
err := a.iter(other, func(k string, av, bv Lattice) error {
var (
joinRes Lattice
e error
)
switch {
case av != nil && bv != nil:
joinRes, e = av.Join(bv)
case av != nil:
joinRes, e = a.JoinWithNil(av)
default:
joinRes, e = a.JoinWithNil(bv)
}
if e != nil && !a.ShouldDeleteIncompatibleJoin() {
return wrapMapKeyError(k, e)
}
if e == nil && joinRes != nil {
result.Insert(k, joinRes)
}
return nil
})
if err != nil {
return nil, err
}
return latticeMap{LatticeMap: result}, nil
}
// Map wraps a LatticeMap instance into a Lattice.
func Map(lm LatticeMap) Lattice {
return latticeMap{LatticeMap: lm}
}