vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/plan_description_test.go (about) 1 /* 2 Copyright 2020 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 engine 18 19 import ( 20 "testing" 21 22 "vitess.io/vitess/go/vt/vtgate/evalengine" 23 24 "vitess.io/vitess/go/test/utils" 25 26 "vitess.io/vitess/go/vt/key" 27 "vitess.io/vitess/go/vt/vtgate/vindexes" 28 ) 29 30 func TestCreateRoutePlanDescription(t *testing.T) { 31 route := createRoute() 32 33 planDescription := PrimitiveToPlanDescription(route) 34 35 expected := PrimitiveDescription{ 36 OperatorType: "Route", 37 Variant: "Scatter", 38 Keyspace: &vindexes.Keyspace{Name: "ks"}, 39 TargetDestination: key.DestinationAllShards{}, 40 Other: map[string]any{ 41 "Query": route.Query, 42 "Table": route.GetTableName(), 43 "FieldQuery": route.FieldQuery, 44 "Vindex": route.Vindex.String(), 45 }, 46 Inputs: []PrimitiveDescription{}, 47 } 48 49 utils.MustMatch(t, expected, planDescription, "descriptions did not match") 50 } 51 52 func createRoute() *Route { 53 hash, _ := vindexes.NewHash("vindex name", nil) 54 return &Route{ 55 RoutingParameters: &RoutingParameters{ 56 Opcode: Scatter, 57 Keyspace: &vindexes.Keyspace{Name: "ks"}, 58 TargetDestination: key.DestinationAllShards{}, 59 Vindex: hash.(*vindexes.Hash), 60 }, 61 Query: "select all the things", 62 TableName: "tableName", 63 FieldQuery: "more query", 64 } 65 } 66 67 func TestPlanDescriptionWithInputs(t *testing.T) { 68 route := createRoute() 69 routeDescr := getDescriptionFor(route) 70 count := evalengine.NewLiteralInt(12) 71 offset := evalengine.NewLiteralInt(4) 72 limit := &Limit{ 73 Count: count, 74 Offset: offset, 75 Input: route, 76 } 77 78 planDescription := PrimitiveToPlanDescription(limit) 79 80 expected := PrimitiveDescription{ 81 OperatorType: "Limit", 82 Other: map[string]any{ 83 "Count": evalengine.FormatExpr(count), 84 "Offset": evalengine.FormatExpr(offset), 85 }, 86 Inputs: []PrimitiveDescription{routeDescr}, 87 } 88 89 utils.MustMatch(t, expected, planDescription, "descriptions did not match") 90 } 91 92 func getDescriptionFor(route *Route) PrimitiveDescription { 93 return PrimitiveDescription{ 94 OperatorType: "Route", 95 Variant: route.Opcode.String(), 96 Keyspace: &vindexes.Keyspace{Name: "ks"}, 97 TargetDestination: key.DestinationAllShards{}, 98 Other: map[string]any{ 99 "Query": route.Query, 100 "Table": route.GetTableName(), 101 "FieldQuery": route.FieldQuery, 102 "Vindex": route.Vindex.String(), 103 }, 104 Inputs: []PrimitiveDescription{}, 105 } 106 }