github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/ordering_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package opt_test 12 13 import ( 14 "reflect" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/sql/opt" 18 ) 19 20 func TestOrdering(t *testing.T) { 21 // Add Ordering props. 22 ordering := opt.Ordering{1, 5} 23 24 if ordering.Empty() { 25 t.Error("ordering not empty") 26 } 27 28 if !ordering.Provides(ordering) { 29 t.Error("ordering should provide itself") 30 } 31 32 if !ordering.Provides(opt.Ordering{1}) { 33 t.Error("ordering should provide the prefix ordering") 34 } 35 36 if (opt.Ordering{}).Provides(ordering) { 37 t.Error("empty ordering should not provide ordering") 38 } 39 40 if !ordering.Provides(opt.Ordering{}) { 41 t.Error("ordering should provide the empty ordering") 42 } 43 44 if !ordering.ColSet().Equals(opt.MakeColSet(1, 5)) { 45 t.Error("ordering colset should equal the ordering columns") 46 } 47 48 if !(opt.Ordering{}).ColSet().Equals(opt.ColSet{}) { 49 t.Error("empty ordering should have empty column set") 50 } 51 52 if !ordering.Equals(ordering) { 53 t.Error("ordering should be equal with itself") 54 } 55 56 if ordering.Equals(opt.Ordering{}) { 57 t.Error("ordering should not equal the empty ordering") 58 } 59 60 if (opt.Ordering{}).Equals(ordering) { 61 t.Error("empty ordering should not equal ordering") 62 } 63 64 common := ordering.CommonPrefix(opt.Ordering{1}) 65 if exp := (opt.Ordering{1}); !reflect.DeepEqual(common, exp) { 66 t.Errorf("expected common prefix %s, got %s", exp, common) 67 } 68 common = ordering.CommonPrefix(opt.Ordering{1, 2, 3}) 69 if exp := (opt.Ordering{1}); !reflect.DeepEqual(common, exp) { 70 t.Errorf("expected common prefix %s, got %s", exp, common) 71 } 72 common = ordering.CommonPrefix(opt.Ordering{1, 5, 6}) 73 if exp := (opt.Ordering{1, 5}); !reflect.DeepEqual(common, exp) { 74 t.Errorf("expected common prefix %s, got %s", exp, common) 75 } 76 } 77 78 func TestOrderingSet(t *testing.T) { 79 expect := func(s opt.OrderingSet, exp string) { 80 t.Helper() 81 if actual := s.String(); actual != exp { 82 t.Errorf("expected %s; got %s", exp, actual) 83 } 84 } 85 var s opt.OrderingSet 86 expect(s, "") 87 s.Add(opt.Ordering{1, 2}) 88 expect(s, "(+1,+2)") 89 s.Add(opt.Ordering{1, -2, 3}) 90 expect(s, "(+1,+2) (+1,-2,+3)") 91 // Add an ordering that already exists. 92 s.Add(opt.Ordering{1, -2, 3}) 93 expect(s, "(+1,+2) (+1,-2,+3)") 94 // Add an ordering that is a prefix of an existing ordering. 95 s.Add(opt.Ordering{1, -2}) 96 expect(s, "(+1,+2) (+1,-2,+3)") 97 // Add an ordering that has an existing ordering as a prefix. 98 s.Add(opt.Ordering{1, 2, 5}) 99 expect(s, "(+1,+2,+5) (+1,-2,+3)") 100 101 s2 := s.Copy() 102 s2.RestrictToPrefix(opt.Ordering{1}) 103 expect(s2, "(+1,+2,+5) (+1,-2,+3)") 104 s2 = s.Copy() 105 s2.RestrictToPrefix(opt.Ordering{1, 2}) 106 expect(s2, "(+1,+2,+5)") 107 s2 = s.Copy() 108 s2.RestrictToPrefix(opt.Ordering{2}) 109 expect(s2, "") 110 111 s2 = s.Copy() 112 s2.RestrictToCols(opt.MakeColSet(1, 2, 3, 5)) 113 expect(s2, "(+1,+2,+5) (+1,-2,+3)") 114 115 s2 = s.Copy() 116 s2.RestrictToCols(opt.MakeColSet(1, 2, 3)) 117 expect(s2, "(+1,+2) (+1,-2,+3)") 118 119 s2 = s.Copy() 120 s2.RestrictToCols(opt.MakeColSet(1, 2)) 121 expect(s2, "(+1,+2) (+1,-2)") 122 123 s2 = s.Copy() 124 s2.RestrictToCols(opt.MakeColSet(1, 3)) 125 expect(s2, "(+1)") 126 127 s2 = s.Copy() 128 s2.RestrictToCols(opt.MakeColSet(2, 3)) 129 expect(s2, "") 130 }