github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/compare_test.go (about) 1 // Copyright 2017 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 tree 12 13 import ( 14 "context" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/settings/cluster" 18 "github.com/cockroachdb/cockroach/pkg/sql/types" 19 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 20 ) 21 22 func TestEvalComparisonExprCaching(t *testing.T) { 23 defer leaktest.AfterTest(t)() 24 testExprs := []struct { 25 op ComparisonOperator 26 left, right string 27 cacheCount int 28 }{ 29 // Comparisons. 30 {EQ, `0`, `1`, 0}, 31 {LT, `0`, `1`, 0}, 32 // LIKE and NOT LIKE 33 {Like, `TEST`, `T%T`, 1}, 34 {NotLike, `TEST`, `%E%T`, 1}, 35 // ILIKE and NOT ILIKE 36 {ILike, `TEST`, `T%T`, 1}, 37 {NotILike, `TEST`, `%E%T`, 1}, 38 // SIMILAR TO and NOT SIMILAR TO 39 {SimilarTo, `abc`, `(b|c)%`, 1}, 40 {NotSimilarTo, `abc`, `%(b|d)%`, 1}, 41 // ~, !~, ~*, and !~* 42 {RegMatch, `abc`, `(b|c).`, 1}, 43 {NotRegMatch, `abc`, `(b|c).`, 1}, 44 {RegIMatch, `abc`, `(b|c).`, 1}, 45 {NotRegIMatch, `abc`, `(b|c).`, 1}, 46 } 47 for _, d := range testExprs { 48 expr := &ComparisonExpr{ 49 Operator: d.op, 50 Left: NewDString(d.left), 51 Right: NewDString(d.right), 52 } 53 ctx := NewTestingEvalContext(cluster.MakeTestingClusterSettings()) 54 defer ctx.Mon.Stop(context.Background()) 55 ctx.ReCache = NewRegexpCache(8) 56 typedExpr, err := TypeCheck(context.Background(), expr, nil, types.Any) 57 if err != nil { 58 t.Fatalf("%v: %v", d, err) 59 } 60 if _, err := typedExpr.Eval(ctx); err != nil { 61 t.Fatalf("%v: %v", d, err) 62 } 63 if typedExpr.(*ComparisonExpr).fn.Fn == nil { 64 t.Errorf("%s: expected the comparison function to be looked up and memoized, but it wasn't", expr) 65 } 66 if count := ctx.ReCache.Len(); count != d.cacheCount { 67 t.Errorf("%s: expected regular expression cache to contain %d compiled patterns, but found %d", expr, d.cacheCount, count) 68 } 69 } 70 }