github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_like_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package memex 15 16 import ( 17 . "github.com/whtcorpsinc/check" 18 "github.com/whtcorpsinc/BerolinaSQL/ast" 19 "github.com/whtcorpsinc/BerolinaSQL/terror" 20 "github.com/whtcorpsinc/milevadb/types" 21 "github.com/whtcorpsinc/milevadb/soliton/chunk" 22 "github.com/whtcorpsinc/milevadb/soliton/defCauslate" 23 "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 24 ) 25 26 func (s *testEvaluatorSuite) TestLike(c *C) { 27 tests := []struct { 28 input string 29 pattern string 30 match int 31 }{ 32 {"a", "", 0}, 33 {"a", "a", 1}, 34 {"a", "b", 0}, 35 {"aA", "Aa", 0}, 36 {"aAb", `Aa%`, 0}, 37 {"aAb", "aA_", 1}, 38 {"baab", "b_%b", 1}, 39 {"baab", "b%_b", 1}, 40 {"bab", "b_%b", 1}, 41 {"bab", "b%_b", 1}, 42 {"bb", "b_%b", 0}, 43 {"bb", "b%_b", 0}, 44 {"baabccc", "b_%b%", 1}, 45 } 46 47 for _, tt := range tests { 48 commentf := Commentf(`for input = "%s", pattern = "%s"`, tt.input, tt.pattern) 49 fc := funcs[ast.Like] 50 f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(tt.input, tt.pattern, 0))) 51 c.Assert(err, IsNil, commentf) 52 r, err := evalBuiltinFuncConcurrent(f, chunk.Event{}) 53 c.Assert(err, IsNil, commentf) 54 c.Assert(r, solitonutil.CausetEquals, types.NewCauset(tt.match), commentf) 55 } 56 } 57 58 func (s *testEvaluatorSerialSuites) TestCILike(c *C) { 59 defCauslate.SetNewDefCauslationEnabledForTest(true) 60 defer defCauslate.SetNewDefCauslationEnabledForTest(false) 61 tests := []struct { 62 input string 63 pattern string 64 generalMatch int 65 unicodeMatch int 66 }{ 67 {"a", "", 0, 0}, 68 {"a", "a", 1, 1}, 69 {"a", "á", 1, 1}, 70 {"a", "b", 0, 0}, 71 {"aA", "Aa", 1, 1}, 72 {"áAb", `Aa%`, 1, 1}, 73 {"áAb", `%ab%`, 1, 1}, 74 {"áAb", `%ab`, 1, 1}, 75 {"ÀAb", "aA_", 1, 1}, 76 {"áééá", "a_%a", 1, 1}, 77 {"áééá", "a%_a", 1, 1}, 78 {"áéá", "a_%a", 1, 1}, 79 {"áéá", "a%_a", 1, 1}, 80 {"áá", "a_%a", 0, 0}, 81 {"áá", "a%_a", 0, 0}, 82 {"áééáííí", "a_%a%", 1, 1}, 83 84 // performs matching on a per-character basis 85 // https://dev.allegrosql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like 86 {"ß", "s%", 1, 0}, 87 {"ß", "%s", 1, 0}, 88 {"ß", "ss", 0, 0}, 89 {"ß", "s", 1, 0}, 90 {"ss", "%ß%", 1, 0}, 91 {"ß", "_", 1, 1}, 92 {"ß", "__", 0, 0}, 93 } 94 for _, tt := range tests { 95 commentf := Commentf(`for input = "%s", pattern = "%s"`, tt.input, tt.pattern) 96 fc := funcs[ast.Like] 97 inputs := s.datumsToConstants(types.MakeCausets(tt.input, tt.pattern, 0)) 98 f, err := fc.getFunction(s.ctx, inputs) 99 c.Assert(err, IsNil, commentf) 100 f.setDefCauslator(defCauslate.GetDefCauslator("utf8mb4_general_ci")) 101 r, err := evalBuiltinFunc(f, chunk.Event{}) 102 c.Assert(err, IsNil, commentf) 103 c.Assert(r, solitonutil.CausetEquals, types.NewCauset(tt.generalMatch), commentf) 104 } 105 106 for _, tt := range tests { 107 commentf := Commentf(`for input = "%s", pattern = "%s"`, tt.input, tt.pattern) 108 fc := funcs[ast.Like] 109 inputs := s.datumsToConstants(types.MakeCausets(tt.input, tt.pattern, 0)) 110 f, err := fc.getFunction(s.ctx, inputs) 111 c.Assert(err, IsNil, commentf) 112 f.setDefCauslator(defCauslate.GetDefCauslator("utf8mb4_unicode_ci")) 113 r, err := evalBuiltinFunc(f, chunk.Event{}) 114 c.Assert(err, IsNil, commentf) 115 c.Assert(r, solitonutil.CausetEquals, types.NewCauset(tt.unicodeMatch), commentf) 116 } 117 } 118 119 func (s *testEvaluatorSuite) TestRegexp(c *C) { 120 tests := []struct { 121 pattern string 122 input string 123 match int64 124 err error 125 }{ 126 {"^$", "a", 0, nil}, 127 {"a", "a", 1, nil}, 128 {"a", "b", 0, nil}, 129 {"aA", "aA", 1, nil}, 130 {".", "a", 1, nil}, 131 {"^.$", "ab", 0, nil}, 132 {"..", "b", 0, nil}, 133 {".ab", "aab", 1, nil}, 134 {".*", "abcd", 1, nil}, 135 {"(", "", 0, ErrRegexp}, 136 {"(*", "", 0, ErrRegexp}, 137 {"[a", "", 0, ErrRegexp}, 138 {"\\", "", 0, ErrRegexp}, 139 } 140 for _, tt := range tests { 141 fc := funcs[ast.Regexp] 142 f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(tt.input, tt.pattern))) 143 c.Assert(err, IsNil) 144 match, err := evalBuiltinFunc(f, chunk.Event{}) 145 if tt.err == nil { 146 c.Assert(err, IsNil) 147 c.Assert(match, solitonutil.CausetEquals, types.NewCauset(tt.match), Commentf("%v", tt)) 148 } else { 149 c.Assert(terror.ErrorEqual(err, tt.err), IsTrue) 150 } 151 } 152 }