vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/vexplain.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 planbuilder
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
    24  
    25  	"vitess.io/vitess/go/sqltypes"
    26  	"vitess.io/vitess/go/vt/key"
    27  	querypb "vitess.io/vitess/go/vt/proto/query"
    28  	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
    29  	"vitess.io/vitess/go/vt/sqlparser"
    30  	"vitess.io/vitess/go/vt/vterrors"
    31  	"vitess.io/vitess/go/vt/vtgate/engine"
    32  )
    33  
    34  // Builds an explain-plan for the given Primitive
    35  func buildExplainPlan(stmt sqlparser.Explain, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
    36  	switch explain := stmt.(type) {
    37  	case *sqlparser.ExplainTab:
    38  		return explainTabPlan(explain, vschema)
    39  	case *sqlparser.ExplainStmt:
    40  		switch explain.Type {
    41  		case sqlparser.VitessType:
    42  			vschema.PlannerWarning("EXPLAIN FORMAT = VITESS is deprecated, please use VEXPLAIN PLAN instead.")
    43  			return buildVExplainVtgatePlan(explain.Statement, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
    44  		case sqlparser.VTExplainType:
    45  			vschema.PlannerWarning("EXPLAIN FORMAT = VTEXPLAIN is deprecated, please use VEXPLAIN QUERIES instead.")
    46  			return buildVExplainLoggingPlan(&sqlparser.VExplainStmt{Type: sqlparser.QueriesVExplainType, Statement: explain.Statement, Comments: explain.Comments}, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
    47  		default:
    48  			return buildOtherReadAndAdmin(sqlparser.String(explain), vschema)
    49  		}
    50  	}
    51  	return nil, vterrors.VT13001(fmt.Sprintf("unexpected explain type: %T", stmt))
    52  }
    53  
    54  func buildVExplainPlan(vexplainStmt *sqlparser.VExplainStmt, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
    55  	switch vexplainStmt.Type {
    56  	case sqlparser.QueriesVExplainType, sqlparser.AllVExplainType:
    57  		return buildVExplainLoggingPlan(vexplainStmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
    58  	case sqlparser.PlanVExplainType:
    59  		return buildVExplainVtgatePlan(vexplainStmt.Statement, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
    60  	}
    61  	return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] unexpected vtexplain type: %s", vexplainStmt.Type.ToString())
    62  }
    63  
    64  func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema) (*planResult, error) {
    65  	_, _, ks, _, destination, err := vschema.FindTableOrVindex(explain.Table)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	explain.Table.Qualifier = sqlparser.NewIdentifierCS("")
    70  
    71  	if destination == nil {
    72  		destination = key.DestinationAnyShard{}
    73  	}
    74  
    75  	keyspace, err := vschema.FindKeyspace(ks)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	if keyspace == nil {
    80  		return nil, vterrors.VT14004(ks)
    81  	}
    82  
    83  	return newPlanResult(&engine.Send{
    84  		Keyspace:          keyspace,
    85  		TargetDestination: destination,
    86  		Query:             sqlparser.String(explain),
    87  		SingleShardOnly:   true,
    88  	}, singleTable(keyspace.Name, explain.Table.Name.String())), nil
    89  }
    90  
    91  func buildVExplainVtgatePlan(explainStatement sqlparser.Statement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
    92  	innerInstruction, err := createInstructionFor(sqlparser.String(explainStatement), explainStatement, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	description := engine.PrimitiveToPlanDescription(innerInstruction.primitive)
    97  	output, err := json.MarshalIndent(description, "", "\t")
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	fields := []*querypb.Field{
   102  		{Name: "JSON", Type: querypb.Type_VARCHAR},
   103  	}
   104  	rows := []sqltypes.Row{
   105  		{
   106  			sqltypes.NewVarChar(string(output)),
   107  		},
   108  	}
   109  	return newPlanResult(engine.NewRowsPrimitive(rows, fields)), nil
   110  }
   111  
   112  func buildVExplainLoggingPlan(explain *sqlparser.VExplainStmt, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
   113  	input, err := createInstructionFor(sqlparser.String(explain.Statement), explain.Statement, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  	switch input.primitive.(type) {
   118  	case *engine.Insert, *engine.Delete, *engine.Update:
   119  		directives := explain.GetParsedComments().Directives()
   120  		if directives.IsSet(sqlparser.DirectiveVExplainRunDMLQueries) {
   121  			break
   122  		}
   123  		return nil, vterrors.VT09008()
   124  	}
   125  
   126  	return &planResult{primitive: &engine.VExplain{Input: input.primitive, Type: explain.Type}, tables: input.tables}, nil
   127  }