github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/generator/string_vec.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 // +build ignore 15 16 package main 17 18 import ( 19 "bytes" 20 "flag" 21 "go/format" 22 "io/ioutil" 23 "log" 24 "path/filepath" 25 "text/template" 26 27 . "github.com/whtcorpsinc/milevadb/memex/generator/helper" 28 ) 29 30 const header = `// Copyright 2020 WHTCORPS INC, Inc. 31 // 32 // Licensed under the Apache License, Version 2.0 (the "License"); 33 // you may not use this file except in compliance with the License. 34 // You may obtain a copy of the License at 35 // 36 // http://www.apache.org/licenses/LICENSE-2.0 37 // 38 // Unless required by applicable law or agreed to in writing, software 39 // distributed under the License is distributed on an "AS IS" BASIS, 40 // See the License for the specific language governing permissions and 41 // limitations under the License. 42 43 // Code generated by go generate in memex/generator; DO NOT EDIT. 44 45 package memex 46 ` 47 48 const newLine = "\n" 49 50 const builtinStringImports = `import ( 51 "github.com/whtcorpsinc/milevadb/types" 52 "github.com/whtcorpsinc/milevadb/soliton/chunk" 53 ) 54 ` 55 56 var builtinStringVecTpl = template.Must(template.New("").Parse(` 57 // vecEvalInt evals FIELD(str,str1,str2,str3,...). 58 // See https://dev.allegrosql.com/doc/refman/5.7/en/string-functions.html#function_field 59 func (b *builtinField{{ .TypeName }}Sig) vecEvalInt(input *chunk.Chunk, result *chunk.DeferredCauset) error { 60 n := input.NumEvents() 61 buf0, err := b.bufSlabPredictor.get(types.ET{{ .ETName }}, n) 62 if err != nil { 63 return err 64 } 65 defer b.bufSlabPredictor.put(buf0) 66 if err := b.args[0].VecEval{{ .TypeName }}(b.ctx, input, buf0); err != nil { 67 return err 68 } 69 buf1, err := b.bufSlabPredictor.get(types.ET{{ .ETName }}, n) 70 if err != nil { 71 return err 72 } 73 defer b.bufSlabPredictor.put(buf1) 74 {{ if .Fixed }} 75 arg0 := buf0.{{ .TypeNameInDeferredCauset }}s() 76 {{ end }} 77 result.ResizeInt64(n, false) 78 i64s := result.Int64s() 79 for i := 0; i < n; i++ { 80 i64s[i] = 0 81 } 82 for i := 1; i < len(b.args); i++ { 83 if err := b.args[i].VecEval{{ .TypeName }}(b.ctx, input, buf1); err != nil { 84 return err 85 } 86 {{ if .Fixed }} 87 arg1 := buf1.{{ .TypeNameInDeferredCauset }}s() 88 {{ end }} 89 for j := 0; j < n; j++ { 90 if i64s[j] > 0 || buf0.IsNull(j) || buf1.IsNull(j) { 91 continue 92 } 93 {{ if .Fixed }} 94 if arg0[j] == arg1[j] { 95 {{ else }} 96 {{ if eq .TypeName "String" }} 97 if b.ctor.Compare(buf0.GetString(j), buf1.GetString(j)) == 0 { 98 {{ else }} 99 if buf0.Get{{ .TypeName }}(j) == buf1.Get{{ .TypeName }}(j) { 100 {{ end }} 101 {{ end }} 102 i64s[j] = int64(i) 103 } 104 } 105 } 106 return nil 107 } 108 109 func (b *builtinField{{ .TypeName }}Sig) vectorized() bool { 110 return true 111 } 112 `)) 113 114 var builtinStringVecTestTpl = template.Must(template.New("").Parse(` 115 import ( 116 "testing" 117 118 . "github.com/whtcorpsinc/check" 119 "github.com/whtcorpsinc/BerolinaSQL/ast" 120 "github.com/whtcorpsinc/milevadb/types" 121 ) 122 123 var vecGeneratedBuiltinStringCases = map[string][]vecExprBenchCase{ 124 ast.Field: { 125 {{ range . }} 126 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ET{{ .ETName }}, types.ET{{ .ETName }}, types.ET{{ .ETName }}, types.ET{{ .ETName }}}}, 127 {{ end }} 128 }, 129 } 130 131 func (s *testEvaluatorSuite) TestVectorizedGeneratedBuiltinStringEvalOneVec(c *C) { 132 testVectorizedEvalOneVec(c, vecGeneratedBuiltinStringCases) 133 } 134 135 func (s *testEvaluatorSuite) TestVectorizedGeneratedBuiltinStringFunc(c *C) { 136 testVectorizedBuiltinFunc(c, vecGeneratedBuiltinStringCases) 137 } 138 139 func BenchmarkVectorizedGeneratedBuiltinStringEvalOneVec(b *testing.B) { 140 benchmarkVectorizedEvalOneVec(b, vecGeneratedBuiltinStringCases) 141 } 142 143 func BenchmarkVectorizedGeneratedBuiltinStringFunc(b *testing.B) { 144 benchmarkVectorizedBuiltinFunc(b, vecGeneratedBuiltinStringCases) 145 } 146 `)) 147 148 var typesMap = []TypeContext{ 149 TypeInt, 150 TypeReal, 151 TypeString, 152 } 153 154 func generateDotGo(fileName string, types []TypeContext) (err error) { 155 w := new(bytes.Buffer) 156 w.WriteString(header) 157 w.WriteString(newLine) 158 w.WriteString(builtinStringImports) 159 160 for _, typeCtx := range types { 161 err := builtinStringVecTpl.InterDircute(w, typeCtx) 162 if err != nil { 163 return err 164 } 165 } 166 data, err := format.Source(w.Bytes()) 167 if err != nil { 168 log.Println("[Warn]", fileName+": gofmt failed", err) 169 data = w.Bytes() // write original data for debugging 170 } 171 return ioutil.WriteFile(fileName, data, 0644) 172 } 173 174 func generateTestDotGo(fileName string, types []TypeContext) error { 175 w := new(bytes.Buffer) 176 w.WriteString(header) 177 w.WriteString(newLine) 178 179 err := builtinStringVecTestTpl.InterDircute(w, types) 180 if err != nil { 181 return err 182 } 183 184 data, err := format.Source(w.Bytes()) 185 if err != nil { 186 log.Println("[Warn]", fileName+": gofmt failed", err) 187 data = w.Bytes() // write original data for debugging 188 } 189 return ioutil.WriteFile(fileName, data, 0644) 190 } 191 192 // generateOneFile generate one xxx.go file and the associated xxx_test.go file. 193 func generateOneFile(fileNamePrefix string, types []TypeContext) (err error) { 194 err = generateDotGo(fileNamePrefix+".go", types) 195 if err != nil { 196 return 197 } 198 err = generateTestDotGo(fileNamePrefix+"_test.go", types) 199 return 200 } 201 202 func main() { 203 flag.Parse() 204 var err error 205 outputDir := "." 206 err = generateOneFile(filepath.Join(outputDir, "builtin_string_vec_generated"), typesMap) 207 if err != nil { 208 log.Fatalln("generateOneFile", err) 209 } 210 }