github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/invertedexpr/geo_expression_test.go (about) 1 // Copyright 2020 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 invertedexpr 12 13 import ( 14 "testing" 15 16 "github.com/cockroachdb/cockroach/pkg/geo/geoindex" 17 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 18 "github.com/gogo/protobuf/proto" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestUnionKeySpansToProto(t *testing.T) { 23 defer leaktest.AfterTest(t)() 24 25 type testCase struct { 26 uks geoindex.UnionKeySpans 27 expected string 28 } 29 cases := []testCase{ 30 { 31 uks: []geoindex.KeySpan{{Start: 5, End: 6}, {Start: 10, End: 11}, {Start: 1, End: 3}}, 32 expected: "spans_to_read:<start:\"\\215\" end:\"\\216\" > " + 33 "spans_to_read:<start:\"\\222\" end:\"\\223\" > " + 34 "spans_to_read:<start:\"\\211\" end:\"\\213\" > " + 35 "node:<" + 36 "factored_union_spans:<start:\"\\215\" end:\"\\216\" > " + 37 "factored_union_spans:<start:\"\\222\" end:\"\\223\" > " + 38 "factored_union_spans:<start:\"\\211\" end:\"\\213\" > > ", 39 }, 40 { 41 uks: nil, 42 expected: "<nil>", 43 }, 44 } 45 for _, c := range cases { 46 require.Equal(t, c.expected, proto.CompactTextString(GeoUnionKeySpansToProto(c.uks))) 47 } 48 } 49 50 func TestRPKeyExprToProto(t *testing.T) { 51 defer leaktest.AfterTest(t)() 52 53 type testCase struct { 54 rpx geoindex.RPKeyExpr 55 expected string 56 err string 57 } 58 cases := []testCase{ 59 { 60 rpx: nil, 61 expected: "<nil>", 62 }, 63 { 64 // Union of two keys. 65 rpx: []geoindex.RPExprElement{geoindex.Key(5), geoindex.Key(10), geoindex.RPSetUnion}, 66 expected: "spans_to_read:<start:\"\\215\" end:\"\\216\" > " + 67 "spans_to_read:<start:\"\\222\" end:\"\\223\" > " + 68 "node:<" + 69 "factored_union_spans:<start:\"\\215\" end:\"\\216\" > " + 70 "factored_union_spans:<start:\"\\222\" end:\"\\223\" > > ", 71 }, 72 { 73 // Intersection of two keys. 74 rpx: []geoindex.RPExprElement{geoindex.Key(5), geoindex.Key(10), geoindex.RPSetIntersection}, 75 expected: "spans_to_read:<start:\"\\215\" end:\"\\216\" > " + 76 "spans_to_read:<start:\"\\222\" end:\"\\223\" > " + 77 "node:<" + 78 "operator:SetIntersection " + 79 "left:<factored_union_spans:<start:\"\\222\" end:\"\\223\" > > " + 80 "right:<factored_union_spans:<start:\"\\215\" end:\"\\216\" > > > ", 81 }, 82 { 83 // Single key. 84 rpx: []geoindex.RPExprElement{geoindex.Key(5)}, 85 expected: "spans_to_read:<start:\"\\215\" end:\"\\216\" > " + 86 "node:<factored_union_spans:<start:\"\\215\" end:\"\\216\" > > ", 87 }, 88 { 89 // Malformed. 90 rpx: []geoindex.RPExprElement{geoindex.Key(5), geoindex.RPSetUnion}, 91 err: "malformed expression: F0/L30/000000000000000000000000000002 \\U", 92 }, 93 { 94 // Expression as represented in the proto: 7 ∩ (5 U 10 U 3 U 4 U (2 ∩ 1)) 95 // Unions have been collapsed wherever possible into a single slice of 96 // FactoredUnionSpans. 97 rpx: []geoindex.RPExprElement{ 98 geoindex.Key(5), geoindex.Key(10), geoindex.RPSetUnion, 99 geoindex.Key(1), geoindex.Key(2), geoindex.RPSetIntersection, 100 geoindex.RPSetUnion, 101 geoindex.Key(3), geoindex.Key(4), geoindex.RPSetUnion, 102 geoindex.RPSetUnion, 103 geoindex.Key(7), 104 geoindex.RPSetIntersection, 105 }, 106 expected: "spans_to_read:<start:\"\\215\" end:\"\\216\" > " + 107 "spans_to_read:<start:\"\\222\" end:\"\\223\" > " + 108 "spans_to_read:<start:\"\\211\" end:\"\\212\" > " + 109 "spans_to_read:<start:\"\\212\" end:\"\\213\" > " + 110 "spans_to_read:<start:\"\\213\" end:\"\\214\" > " + 111 "spans_to_read:<start:\"\\214\" end:\"\\215\" > " + 112 "spans_to_read:<start:\"\\217\" end:\"\\220\" > " + 113 "node:<operator:SetIntersection " + 114 "left:<factored_union_spans:<start:\"\\217\" end:\"\\220\" > > " + 115 "right:<" + 116 "factored_union_spans:<start:\"\\215\" end:\"\\216\" > " + 117 "factored_union_spans:<start:\"\\222\" end:\"\\223\" > " + 118 "factored_union_spans:<start:\"\\213\" end:\"\\214\" > " + 119 "factored_union_spans:<start:\"\\214\" end:\"\\215\" > " + 120 "operator:SetIntersection " + 121 "left:<factored_union_spans:<start:\"\\212\" end:\"\\213\" > > " + 122 "right:<factored_union_spans:<start:\"\\211\" end:\"\\212\" > > > > ", 123 }, 124 } 125 for _, c := range cases { 126 rpxProto, err := GeoRPKeyExprToProto(c.rpx) 127 if len(c.err) == 0 { 128 require.NoError(t, err) 129 require.Equal(t, c.expected, proto.CompactTextString(rpxProto)) 130 } else { 131 require.Equal(t, c.err, err.Error()) 132 } 133 } 134 }