vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/plancontext/planning_context.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 plancontext
    18  
    19  import (
    20  	querypb "vitess.io/vitess/go/vt/proto/query"
    21  	"vitess.io/vitess/go/vt/sqlparser"
    22  	"vitess.io/vitess/go/vt/vtgate/semantics"
    23  )
    24  
    25  type PlanningContext struct {
    26  	ReservedVars *sqlparser.ReservedVars
    27  	SemTable     *semantics.SemTable
    28  	VSchema      VSchema
    29  
    30  	// here we add all predicates that were created because of a join condition
    31  	// e.g. [FROM tblA JOIN tblB ON a.colA = b.colB] will be rewritten to [FROM tblB WHERE :a_colA = b.colB],
    32  	// if we assume that tblB is on the RHS of the join. This last predicate in the WHERE clause is added to the
    33  	// map below
    34  	JoinPredicates     map[sqlparser.Expr][]sqlparser.Expr
    35  	SkipPredicates     map[sqlparser.Expr]any
    36  	PlannerVersion     querypb.ExecuteOptions_PlannerVersion
    37  	RewriteDerivedExpr bool
    38  }
    39  
    40  func NewPlanningContext(reservedVars *sqlparser.ReservedVars, semTable *semantics.SemTable, vschema VSchema, version querypb.ExecuteOptions_PlannerVersion) *PlanningContext {
    41  	ctx := &PlanningContext{
    42  		ReservedVars:   reservedVars,
    43  		SemTable:       semTable,
    44  		VSchema:        vschema,
    45  		JoinPredicates: map[sqlparser.Expr][]sqlparser.Expr{},
    46  		SkipPredicates: map[sqlparser.Expr]any{},
    47  		PlannerVersion: version,
    48  	}
    49  	return ctx
    50  }
    51  
    52  func (c PlanningContext) IsSubQueryToReplace(e sqlparser.Expr) bool {
    53  	ext, ok := e.(*sqlparser.Subquery)
    54  	if !ok {
    55  		return false
    56  	}
    57  	for _, extractedSubq := range c.SemTable.GetSubqueryNeedingRewrite() {
    58  		if extractedSubq.NeedsRewrite && c.SemTable.EqualsExpr(extractedSubq.Subquery, ext) {
    59  			return true
    60  		}
    61  	}
    62  	return false
    63  }