vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/joinGen4.go (about)

     1  /*
     2  Copyright 2019 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  	"fmt"
    21  
    22  	"vitess.io/vitess/go/vt/sqlparser"
    23  	"vitess.io/vitess/go/vt/vterrors"
    24  	"vitess.io/vitess/go/vt/vtgate/engine"
    25  	"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
    26  	"vitess.io/vitess/go/vt/vtgate/semantics"
    27  )
    28  
    29  var _ logicalPlan = (*joinGen4)(nil)
    30  
    31  // joinGen4 is used to build a Join primitive.
    32  // It's used to build an inner join and only used by the Gen4 planner
    33  type joinGen4 struct {
    34  	// Left and Right are the nodes for the join.
    35  	Left, Right logicalPlan
    36  
    37  	// The Opcode tells us if this is an inner or outer join
    38  	Opcode engine.JoinOpcode
    39  
    40  	// These are the columns that will be produced by this plan.
    41  	// Negative offsets come from the LHS, and positive from the RHS
    42  	Cols []int
    43  
    44  	// Vars are the columns that will be sent from the LHS to the RHS
    45  	// the number is the offset on the LHS result, and the string is the bind variable name used in the RHS
    46  	Vars map[string]int
    47  
    48  	// LHSColumns are the columns from the LHS used for the join.
    49  	// These are the same columns pushed on the LHS that are now used in the Vars field
    50  	LHSColumns []*sqlparser.ColName
    51  
    52  	gen4Plan
    53  }
    54  
    55  // WireupGen4 implements the logicalPlan interface
    56  func (j *joinGen4) WireupGen4(ctx *plancontext.PlanningContext) error {
    57  	err := j.Left.WireupGen4(ctx)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	return j.Right.WireupGen4(ctx)
    62  }
    63  
    64  // Primitive implements the logicalPlan interface
    65  func (j *joinGen4) Primitive() engine.Primitive {
    66  	return &engine.Join{
    67  		Left:   j.Left.Primitive(),
    68  		Right:  j.Right.Primitive(),
    69  		Cols:   j.Cols,
    70  		Vars:   j.Vars,
    71  		Opcode: j.Opcode,
    72  	}
    73  }
    74  
    75  // Inputs implements the logicalPlan interface
    76  func (j *joinGen4) Inputs() []logicalPlan {
    77  	return []logicalPlan{j.Left, j.Right}
    78  }
    79  
    80  // Rewrite implements the logicalPlan interface
    81  func (j *joinGen4) Rewrite(inputs ...logicalPlan) error {
    82  	if len(inputs) != 2 {
    83  		return vterrors.VT13001(fmt.Sprintf("wrong number of children in joinGen4 rewrite, got: %d, expect: 2", len(inputs)))
    84  	}
    85  	j.Left = inputs[0]
    86  	j.Right = inputs[1]
    87  	return nil
    88  }
    89  
    90  // ContainsTables implements the logicalPlan interface
    91  func (j *joinGen4) ContainsTables() semantics.TableSet {
    92  	return j.Left.ContainsTables().Merge(j.Right.ContainsTables())
    93  }
    94  
    95  // OutputColumns implements the logicalPlan interface
    96  func (j *joinGen4) OutputColumns() []sqlparser.SelectExpr {
    97  	return getOutputColumnsFromJoin(j.Cols, j.Left.OutputColumns(), j.Right.OutputColumns())
    98  }