github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/explain.go (about)

     1  // Copyright 2023 Dolthub, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package planbuilder
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/dolthub/vitess/go/vt/sqlparser"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/plan"
    24  )
    25  
    26  func (b *Builder) buildExplain(inScope *scope, n *sqlparser.Explain) (outScope *scope) {
    27  	outScope = inScope.push()
    28  	childScope := b.build(inScope, n.Statement, "")
    29  
    30  	describeOptions := sql.DescribeOptions{
    31  		Analyze: n.Analyze,
    32  	}
    33  
    34  	formatFlags := strings.Split(n.ExplainFormat, "_")
    35  	for _, flag := range formatFlags {
    36  		switch strings.ToLower(flag) {
    37  		case "", sqlparser.TreeStr:
    38  		// tree format, do nothing
    39  		case "debug":
    40  			describeOptions.Debug = true
    41  		case "estimates":
    42  			describeOptions.Estimates = true
    43  		default:
    44  			err := errInvalidDescribeFormat.New(n.ExplainFormat, "tree")
    45  			b.handleErr(err)
    46  		}
    47  	}
    48  
    49  	outScope.node = plan.NewDescribeQuery(describeOptions, childScope.node)
    50  	return outScope
    51  }