vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/vindex_bindvars/main_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package vtgate 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "os" 24 "testing" 25 26 "vitess.io/vitess/go/test/endtoend/utils" 27 28 "github.com/stretchr/testify/require" 29 "gotest.tools/assert" 30 31 "vitess.io/vitess/go/mysql" 32 "vitess.io/vitess/go/test/endtoend/cluster" 33 ) 34 35 var ( 36 clusterInstance *cluster.LocalProcessCluster 37 vtParams mysql.ConnParams 38 KeyspaceName = "ks" 39 Cell = "test" 40 SchemaSQL = `CREATE TABLE t1 ( 41 id BIGINT NOT NULL, 42 field BIGINT NOT NULL, 43 field2 BIGINT, 44 field3 BIGINT, 45 field4 BIGINT, 46 field5 BIGINT, 47 field6 BIGINT, 48 PRIMARY KEY (id) 49 ) ENGINE=Innodb; 50 51 CREATE TABLE lookup1 ( 52 field BIGINT NOT NULL, 53 keyspace_id binary(8), 54 UNIQUE KEY (field) 55 ) ENGINE=Innodb; 56 57 CREATE TABLE lookup2 ( 58 field2 BIGINT NOT NULL, 59 keyspace_id binary(8), 60 UNIQUE KEY (field2) 61 ) ENGINE=Innodb; 62 63 CREATE TABLE lookup3 ( 64 field3 BIGINT NOT NULL, 65 keyspace_id binary(8), 66 UNIQUE KEY (field3) 67 ) ENGINE=Innodb; 68 69 CREATE TABLE lookup4 ( 70 field4 BIGINT NOT NULL, 71 keyspace_id binary(8), 72 UNIQUE KEY (field4) 73 ) ENGINE=Innodb; 74 75 CREATE TABLE lookup5 ( 76 field5 BIGINT NOT NULL, 77 keyspace_id binary(8), 78 UNIQUE KEY (field5) 79 ) ENGINE=Innodb; 80 81 CREATE TABLE lookup6 ( 82 field6 BIGINT NOT NULL, 83 keyspace_id binary(8), 84 UNIQUE KEY (field6) 85 ) ENGINE=Innodb; 86 87 CREATE TABLE thex ( 88 id VARBINARY(64) NOT NULL, 89 field BIGINT NOT NULL, 90 PRIMARY KEY (id) 91 ) ENGINE=InnoDB; 92 ` 93 94 VSchema = ` 95 { 96 "sharded": true, 97 "vindexes": { 98 "hash": { 99 "type": "hash" 100 }, 101 "binary_vdx": { 102 "type": "binary" 103 }, 104 "binary_md5_vdx": { 105 "type": "binary_md5" 106 }, 107 "xxhash_vdx": { 108 "type": "xxhash" 109 }, 110 "numeric_vdx": { 111 "type": "numeric" 112 }, 113 "lookup1": { 114 "type": "consistent_lookup", 115 "params": { 116 "table": "lookup1", 117 "from": "field", 118 "to": "keyspace_id", 119 "ignore_nulls": "true" 120 }, 121 "owner": "t1" 122 }, 123 "lookup2": { 124 "type": "consistent_lookup", 125 "params": { 126 "table": "lookup2", 127 "from": "field2", 128 "to": "keyspace_id", 129 "ignore_nulls": "true" 130 }, 131 "owner": "t1" 132 }, 133 "lookup3": { 134 "type": "lookup", 135 "params": { 136 "from": "field3", 137 "no_verify": "true", 138 "table": "lookup3", 139 "to": "keyspace_id" 140 }, 141 "owner": "t1" 142 }, 143 "lookup4": { 144 "type": "lookup", 145 "params": { 146 "from": "field4", 147 "read_lock": "exclusive", 148 "table": "lookup4", 149 "to": "keyspace_id" 150 }, 151 "owner": "t1" 152 }, 153 "lookup5": { 154 "type": "lookup", 155 "params": { 156 "from": "field5", 157 "read_lock": "shared", 158 "table": "lookup5", 159 "to": "keyspace_id" 160 }, 161 "owner": "t1" 162 }, 163 "lookup6": { 164 "type": "lookup", 165 "params": { 166 "from": "field6", 167 "read_lock": "none", 168 "table": "lookup6", 169 "to": "keyspace_id" 170 }, 171 "owner": "t1" 172 } 173 }, 174 "tables": { 175 "t1": { 176 "column_vindexes": [ 177 { 178 "column": "id", 179 "name": "hash" 180 }, 181 { 182 "column": "field", 183 "name": "lookup1" 184 }, 185 { 186 "column": "field2", 187 "name": "lookup2" 188 }, 189 { 190 "column": "field3", 191 "name": "lookup3" 192 }, 193 { 194 "column": "field4", 195 "name": "lookup4" 196 }, 197 { 198 "column": "field5", 199 "name": "lookup5" 200 }, 201 { 202 "column": "field6", 203 "name": "lookup6" 204 } 205 ] 206 }, 207 "lookup1": { 208 "column_vindexes": [ 209 { 210 "column": "field", 211 "name": "hash" 212 } 213 ] 214 }, 215 "lookup2": { 216 "column_vindexes": [ 217 { 218 "column": "field2", 219 "name": "hash" 220 } 221 ] 222 }, 223 "lookup3": { 224 "column_vindexes": [ 225 { 226 "column": "field3", 227 "name": "binary_md5_vdx" 228 } 229 ] 230 }, 231 "lookup4": { 232 "column_vindexes": [ 233 { 234 "column": "field4", 235 "name": "binary_md5_vdx" 236 } 237 ] 238 }, 239 "lookup5": { 240 "column_vindexes": [ 241 { 242 "column": "field5", 243 "name": "binary_md5_vdx" 244 } 245 ] 246 }, 247 "lookup6": { 248 "column_vindexes": [ 249 { 250 "column": "field6", 251 "name": "binary_md5_vdx" 252 } 253 ] 254 }, 255 "thex": { 256 "column_vindexes": [ 257 { 258 "column": "id", 259 "name": "binary_vdx" 260 } 261 ] 262 } 263 } 264 }` 265 ) 266 267 func TestMain(m *testing.M) { 268 defer cluster.PanicHandler(nil) 269 flag.Parse() 270 271 exitCode := func() int { 272 clusterInstance = cluster.NewCluster(Cell, "localhost") 273 defer clusterInstance.Teardown() 274 275 // Start topo server 276 err := clusterInstance.StartTopo() 277 if err != nil { 278 return 1 279 } 280 281 // Start keyspace 282 keyspace := &cluster.Keyspace{ 283 Name: KeyspaceName, 284 SchemaSQL: SchemaSQL, 285 VSchema: VSchema, 286 } 287 err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false) 288 if err != nil { 289 return 1 290 } 291 292 // Start vtgate 293 err = clusterInstance.StartVtgate() 294 if err != nil { 295 return 1 296 } 297 vtParams = mysql.ConnParams{ 298 Host: clusterInstance.Hostname, 299 Port: clusterInstance.VtgateMySQLPort, 300 } 301 return m.Run() 302 }() 303 os.Exit(exitCode) 304 } 305 306 func TestVindexHexTypes(t *testing.T) { 307 defer cluster.PanicHandler(t) 308 ctx := context.Background() 309 conn, err := mysql.Connect(ctx, &vtParams) 310 require.Nil(t, err) 311 defer conn.Close() 312 313 utils.Exec(t, conn, "INSERT INTO thex (id, field) VALUES "+ 314 "(0x01,1), "+ 315 "(x'a5',2), "+ 316 "(0x48656c6c6f20476f7068657221,3), "+ 317 "(x'c26caa1a5eb94096d29a1bec',4)") 318 result := utils.Exec(t, conn, "select id, field from thex order by id") 319 320 expected := 321 "[[VARBINARY(\"\\x01\") INT64(1)] " + 322 "[VARBINARY(\"Hello Gopher!\") INT64(3)] " + 323 "[VARBINARY(\"\\xa5\") INT64(2)] " + 324 "[VARBINARY(\"\\xc2l\\xaa\\x1a^\\xb9@\\x96Қ\\x1b\\xec\") INT64(4)]]" 325 assert.Equal(t, expected, fmt.Sprintf("%v", result.Rows)) 326 } 327 328 func TestVindexBindVarOverlap(t *testing.T) { 329 defer cluster.PanicHandler(t) 330 ctx := context.Background() 331 conn, err := mysql.Connect(ctx, &vtParams) 332 require.Nil(t, err) 333 defer conn.Close() 334 335 utils.Exec(t, conn, "INSERT INTO t1 (id, field, field2, field3, field4, field5, field6) VALUES "+ 336 "(0,1,2,3,4,5,6), "+ 337 "(1,2,3,4,5,6,7), "+ 338 "(2,3,4,5,6,7,8), "+ 339 "(3,4,5,6,7,8,9), "+ 340 "(4,5,6,7,8,9,10), "+ 341 "(5,6,7,8,9,10,11), "+ 342 "(6,7,8,9,10,11,12), "+ 343 "(7,8,9,10,11,12,13), "+ 344 "(8,9,10,11,12,13,14), "+ 345 "(9,10,11,12,13,14,15), "+ 346 "(10,11,12,13,14,15,16), "+ 347 "(11,12,13,14,15,16,17), "+ 348 "(12,13,14,15,16,17,18), "+ 349 "(13,14,15,16,17,18,19), "+ 350 "(14,15,16,17,18,19,20), "+ 351 "(15,16,17,18,19,20,21), "+ 352 "(16,17,18,19,20,21,22), "+ 353 "(17,18,19,20,21,22,23), "+ 354 "(18,19,20,21,22,23,24), "+ 355 "(19,20,21,22,23,24,25), "+ 356 "(20,21,22,23,24,25,26)") 357 result := utils.Exec(t, conn, "select id, field, field2, field3, field4, field5, field6 from t1 order by id") 358 359 expected := 360 "[[INT64(0) INT64(1) INT64(2) INT64(3) INT64(4) INT64(5) INT64(6)] " + 361 "[INT64(1) INT64(2) INT64(3) INT64(4) INT64(5) INT64(6) INT64(7)] " + 362 "[INT64(2) INT64(3) INT64(4) INT64(5) INT64(6) INT64(7) INT64(8)] " + 363 "[INT64(3) INT64(4) INT64(5) INT64(6) INT64(7) INT64(8) INT64(9)] " + 364 "[INT64(4) INT64(5) INT64(6) INT64(7) INT64(8) INT64(9) INT64(10)] " + 365 "[INT64(5) INT64(6) INT64(7) INT64(8) INT64(9) INT64(10) INT64(11)] " + 366 "[INT64(6) INT64(7) INT64(8) INT64(9) INT64(10) INT64(11) INT64(12)] " + 367 "[INT64(7) INT64(8) INT64(9) INT64(10) INT64(11) INT64(12) INT64(13)] " + 368 "[INT64(8) INT64(9) INT64(10) INT64(11) INT64(12) INT64(13) INT64(14)] " + 369 "[INT64(9) INT64(10) INT64(11) INT64(12) INT64(13) INT64(14) INT64(15)] " + 370 "[INT64(10) INT64(11) INT64(12) INT64(13) INT64(14) INT64(15) INT64(16)] " + 371 "[INT64(11) INT64(12) INT64(13) INT64(14) INT64(15) INT64(16) INT64(17)] " + 372 "[INT64(12) INT64(13) INT64(14) INT64(15) INT64(16) INT64(17) INT64(18)] " + 373 "[INT64(13) INT64(14) INT64(15) INT64(16) INT64(17) INT64(18) INT64(19)] " + 374 "[INT64(14) INT64(15) INT64(16) INT64(17) INT64(18) INT64(19) INT64(20)] " + 375 "[INT64(15) INT64(16) INT64(17) INT64(18) INT64(19) INT64(20) INT64(21)] " + 376 "[INT64(16) INT64(17) INT64(18) INT64(19) INT64(20) INT64(21) INT64(22)] " + 377 "[INT64(17) INT64(18) INT64(19) INT64(20) INT64(21) INT64(22) INT64(23)] " + 378 "[INT64(18) INT64(19) INT64(20) INT64(21) INT64(22) INT64(23) INT64(24)] " + 379 "[INT64(19) INT64(20) INT64(21) INT64(22) INT64(23) INT64(24) INT64(25)] " + 380 "[INT64(20) INT64(21) INT64(22) INT64(23) INT64(24) INT64(25) INT64(26)]]" 381 assert.Equal(t, expected, fmt.Sprintf("%v", result.Rows)) 382 }