vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/single_sharded_shortcut.go (about) 1 /* 2 Copyright 2022 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 planbuilder 18 19 import ( 20 "sort" 21 "strings" 22 23 "vitess.io/vitess/go/vt/vtgate/planbuilder/operators" 24 "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" 25 26 "vitess.io/vitess/go/vt/sqlparser" 27 "vitess.io/vitess/go/vt/vtgate/engine" 28 "vitess.io/vitess/go/vt/vtgate/semantics" 29 "vitess.io/vitess/go/vt/vtgate/vindexes" 30 ) 31 32 func unshardedShortcut(ctx *plancontext.PlanningContext, stmt sqlparser.SelectStatement, ks *vindexes.Keyspace) (logicalPlan, []string, error) { 33 // this method is used when the query we are handling has all tables in the same unsharded keyspace 34 sqlparser.SafeRewrite(stmt, nil, func(cursor *sqlparser.Cursor) bool { 35 switch node := cursor.Node().(type) { 36 case sqlparser.SelectExpr: 37 removeKeyspaceFromSelectExpr(node) 38 case sqlparser.TableName: 39 cursor.Replace(sqlparser.TableName{ 40 Name: node.Name, 41 }) 42 } 43 return true 44 }) 45 46 tableNames, err := getTableNames(ctx.SemTable) 47 if err != nil { 48 return nil, nil, err 49 } 50 plan := &routeGen4{ 51 eroute: &engine.Route{ 52 RoutingParameters: &engine.RoutingParameters{ 53 Opcode: engine.Unsharded, 54 Keyspace: ks, 55 }, 56 TableName: strings.Join(escapedTableNames(tableNames), ", "), 57 }, 58 Select: stmt, 59 } 60 61 if err := plan.WireupGen4(ctx); err != nil { 62 return nil, nil, err 63 } 64 return plan, operators.QualifiedTableNames(ks, tableNames), nil 65 } 66 67 func escapedTableNames(tableNames []sqlparser.TableName) []string { 68 escaped := make([]string, len(tableNames)) 69 for i, tableName := range tableNames { 70 escaped[i] = sqlparser.String(tableName) 71 } 72 return escaped 73 } 74 75 func getTableNames(semTable *semantics.SemTable) ([]sqlparser.TableName, error) { 76 tableNameMap := make(map[string]sqlparser.TableName) 77 78 for _, tableInfo := range semTable.Tables { 79 tblObj := tableInfo.GetVindexTable() 80 if tblObj == nil { 81 // probably a derived table 82 continue 83 } 84 if tableInfo.IsInfSchema() { 85 tableNameMap["tableName"] = sqlparser.TableName{ 86 Name: sqlparser.NewIdentifierCS("tableName"), 87 } 88 } else { 89 tableNameMap[sqlparser.String(tblObj.Name)] = sqlparser.TableName{ 90 Name: tblObj.Name, 91 } 92 } 93 } 94 var keys []string 95 for k := range tableNameMap { 96 keys = append(keys, k) 97 } 98 sort.Strings(keys) 99 var tableNames []sqlparser.TableName 100 for _, k := range keys { 101 tableNames = append(tableNames, tableNameMap[k]) 102 } 103 return tableNames, nil 104 }