github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_json_vec_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 "testing" 18 19 . "github.com/whtcorpsinc/check" 20 "github.com/whtcorpsinc/BerolinaSQL/ast" 21 "github.com/whtcorpsinc/milevadb/types" 22 ) 23 24 var vecBuiltinJSONCases = map[string][]vecExprBenchCase{ 25 ast.JSONKeys: { 26 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson}}, 27 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString}, geners: []dataGenerator{&constJSONGener{"{\"a\": {\"c\": 3}, \"b\": 2}"}, &constStrGener{"$.a"}}}, 28 }, 29 ast.JSONArrayAppend: { 30 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson}, 31 geners: []dataGenerator{newNullWrappedGener(0.1, &constJSONGener{"{\"a\": {\"c\": 3}, \"b\": 2}"}), 32 newNullWrappedGener(0.1, &constStrGener{"$.a"}), 33 newNullWrappedGener(0.1, &constJSONGener{"{\"b\": 2}"})}}, 34 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson, types.ETString, types.ETJson}, 35 geners: []dataGenerator{newNullWrappedGener(0.1, &constJSONGener{"{\"a\": {\"c\": 3}, \"b\": 2}"}), 36 newNullWrappedGener(0.1, &constStrGener{"$.a"}), 37 newNullWrappedGener(0.1, &constJSONGener{"{\"b\": 2}"}), 38 newNullWrappedGener(0.1, &constStrGener{"$.b"}), 39 newNullWrappedGener(0.1, &constJSONGener{"{\"x\": 3}"}), 40 }}, 41 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson, types.ETString, types.ETJson}, 42 geners: []dataGenerator{newNullWrappedGener(0.1, &constJSONGener{"{\"a\": {\"c\": 3}, \"b\": 2}"}), 43 newNullWrappedGener(0.1, &constStrGener{"$.a"}), 44 newNullWrappedGener(0.1, &constJSONGener{"{\"b\": 2}"}), 45 newNullWrappedGener(0.1, &constStrGener{"$.x"}), // not exists 46 newNullWrappedGener(0.1, &constJSONGener{"{\"x\": 3}"}), 47 }}, 48 }, 49 ast.JSONContainsPath: { 50 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"{\"a\": {\"c\": {\"d\": 4}}, \"b\": 2}"}, &constStrGener{"one"}, &constStrGener{"$.c"}}}, 51 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"{\"a\": {\"c\": {\"d\": 4}}, \"b\": 2}"}, &constStrGener{"all"}, &constStrGener{"$.a"}, &constStrGener{"$.c"}}}, 52 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"{\"a\": {\"c\": {\"d\": 4}}, \"b\": 2}"}, &constStrGener{"one"}, &constStrGener{"$.*"}}}, 53 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"{\"a\": {\"c\": {\"d\": 4}}, \"b\": 2}"}, &constStrGener{"aLl"}, &constStrGener{"$.a"}, &constStrGener{"$.e"}}}, 54 }, 55 ast.JSONExtract: { 56 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString}, geners: []dataGenerator{nil, &constStrGener{"$.key"}}}, 57 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{nil, &constStrGener{"$.key"}, &constStrGener{"$[0]"}}}, 58 }, 59 ast.JSONLength: { 60 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson}}, 61 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETString}, geners: []dataGenerator{nil, &constStrGener{"$.key"}}}, 62 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETString}, geners: []dataGenerator{nil, &constStrGener{"$.abc"}}}, 63 }, 64 ast.JSONType: {{retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETJson}}}, 65 ast.JSONArray: { 66 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson}}, 67 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETJson}}, 68 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETJson, types.ETJson}}, 69 }, 70 ast.JSONArrayInsert: { 71 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson, types.ETString, types.ETJson}, geners: []dataGenerator{&constJSONGener{"[\"a\", {\"b\": [1, 2]}, [3, 4]]"}, &constStrGener{"$[1]"}, nil, &constStrGener{"$[2][1]"}, nil}}, 72 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson, types.ETString, types.ETJson}, geners: []dataGenerator{&constJSONGener{"[\"a\", {\"b\": [1, 2]}, [3, 4]]"}, &constStrGener{"$[1]"}, &constJSONGener{"[1,2,3,4,5]"}, &constStrGener{"$[2][1]"}, &constJSONGener{"{\"abc\":1,\"def\":2,\"ghi\":[1,2,3,4]}"}}}, 73 }, 74 ast.JSONContains: { 75 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETJson}}, 76 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETJson, types.ETString}, geners: []dataGenerator{nil, nil, &constStrGener{"$.abc"}}}, 77 {retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson, types.ETJson, types.ETString}, geners: []dataGenerator{nil, nil, &constStrGener{"$.key"}}}, 78 }, 79 ast.JSONObject: { 80 { 81 retEvalType: types.ETJson, 82 childrenTypes: []types.EvalType{ 83 types.ETString, types.ETJson, 84 types.ETString, types.ETJson, 85 types.ETString, types.ETJson, 86 types.ETString, types.ETJson, 87 types.ETString, types.ETJson, 88 types.ETString, types.ETJson, 89 types.ETString, types.ETJson, 90 types.ETString, types.ETJson, 91 types.ETString, types.ETJson, 92 types.ETString, types.ETJson, 93 types.ETString, types.ETJson, 94 types.ETString, types.ETJson, 95 }, 96 geners: []dataGenerator{ 97 newRandLenStrGener(10, 20), nil, 98 newRandLenStrGener(10, 20), nil, 99 newRandLenStrGener(10, 20), nil, 100 newRandLenStrGener(10, 20), nil, 101 newRandLenStrGener(10, 20), nil, 102 newRandLenStrGener(10, 20), nil, 103 newRandLenStrGener(10, 20), nil, 104 newRandLenStrGener(10, 20), nil, 105 newRandLenStrGener(10, 20), nil, 106 newRandLenStrGener(10, 20), nil, 107 newRandLenStrGener(10, 20), nil, 108 newRandLenStrGener(10, 20), nil, 109 }, 110 }, 111 }, 112 ast.JSONSet: { 113 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson, types.ETString, types.ETJson}, geners: []dataGenerator{nil, &constStrGener{"$.key"}, nil, &constStrGener{"$.aaa"}, nil}}, 114 }, 115 ast.JSONSearch: { 116 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"[\"abc\", [{\"k\": \"10\"}, \"def\"], {\"x\":\"abc\"}, {\"y\":\"bcd\"}]"}, &constStrGener{"one"}, &constStrGener{"abc"}}}, 117 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"[\"abc\", [{\"k\": \"10\"}, \"def\"], {\"x\":\"abc\"}, {\"y\":\"bcd\"}]"}, &constStrGener{"all"}, &constStrGener{"abc"}}}, 118 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"[\"abc\", [{\"k\": \"10\"}, \"def\"], {\"x\":\"abc\"}, {\"y\":\"bcd\"}]"}, &constStrGener{"all"}, &constStrGener{"%a%"}}}, 119 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"[\"abc\", [{\"k\": \"10\"}, \"def\"], {\"x\":\"abc\"}, {\"y\":\"bcd\"}]"}, &constStrGener{"all"}, &constStrGener{"%a%"}, &constStrGener{}, &constStrGener{"$"}}}, 120 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"[\"abc\", [{\"k\": \"10\"}, \"def\"], {\"x\":\"abc\"}, {\"y\":\"bcd\"}]"}, &constStrGener{"all"}, &constStrGener{"%a%"}, &constStrGener{}}}, 121 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETString, types.ETString, types.ETString}, geners: []dataGenerator{&constJSONGener{"[\"abc\", [{\"k\": \"10\"}, \"def\"], {\"x\":\"abc\"}, {\"y\":\"bcd\"}]"}, &constStrGener{"all"}, &constStrGener{"10"}, &constStrGener{""}, &constStrGener{"$[1][0]"}}}, 122 }, 123 ast.JSONReplace: { 124 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson}, geners: []dataGenerator{nil, &constStrGener{"$.key"}, nil}}, 125 }, 126 ast.JSONStorageSize: {{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson}}}, 127 ast.JSONDepth: {{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETJson}}}, 128 ast.JSONUnquote: { 129 {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETString}, geners: []dataGenerator{newJSONStringGener()}}, 130 }, 131 ast.JSONRemove: { 132 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString}, geners: []dataGenerator{nil, &constStrGener{"$.key"}}}, 133 }, 134 ast.JSONMerge: {{retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETJson, types.ETJson, types.ETJson, types.ETJson}}}, 135 ast.JSONInsert: { 136 {retEvalType: types.ETJson, childrenTypes: []types.EvalType{types.ETJson, types.ETString, types.ETJson, types.ETString, types.ETJson}, geners: []dataGenerator{nil, &constStrGener{"$.aaa"}, nil, &constStrGener{"$.bbb"}, nil}}, 137 }, 138 ast.JSONQuote: { 139 {retEvalType: types.ETString, childrenTypes: []types.EvalType{types.ETString}}, 140 }, 141 } 142 143 func (s *testVectorizeSuite2) TestVectorizedBuiltinJSONFunc(c *C) { 144 testVectorizedBuiltinFunc(c, vecBuiltinJSONCases) 145 } 146 147 func BenchmarkVectorizedBuiltinJSONFunc(b *testing.B) { 148 benchmarkVectorizedBuiltinFunc(b, vecBuiltinJSONCases) 149 }