vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/sql_calc_found_rows.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  	"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 = (*sqlCalcFoundRows)(nil)
    30  
    31  type sqlCalcFoundRows struct {
    32  	LimitQuery, CountQuery logicalPlan
    33  
    34  	// only used by WireUp for V3
    35  	ljt, cjt *jointab
    36  }
    37  
    38  // Wireup implements the logicalPlan interface
    39  func (s *sqlCalcFoundRows) Wireup(logicalPlan, *jointab) error {
    40  	err := s.LimitQuery.Wireup(s.LimitQuery, s.ljt)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	return s.CountQuery.Wireup(s.CountQuery, s.cjt)
    45  }
    46  
    47  // WireupGen4 implements the logicalPlan interface
    48  func (s *sqlCalcFoundRows) WireupGen4(ctx *plancontext.PlanningContext) error {
    49  	err := s.LimitQuery.WireupGen4(ctx)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	return s.CountQuery.WireupGen4(ctx)
    54  }
    55  
    56  // ContainsTables implements the logicalPlan interface
    57  func (s *sqlCalcFoundRows) ContainsTables() semantics.TableSet {
    58  	return s.LimitQuery.ContainsTables()
    59  }
    60  
    61  // Primitive implements the logicalPlan interface
    62  func (s *sqlCalcFoundRows) Primitive() engine.Primitive {
    63  	countPrim := s.CountQuery.Primitive()
    64  	rb, ok := countPrim.(*engine.Route)
    65  	if ok {
    66  		// if our count query is an aggregation, we want the no-match result to still return a zero
    67  		rb.NoRoutesSpecialHandling = true
    68  	}
    69  	return engine.SQLCalcFoundRows{
    70  		LimitPrimitive: s.LimitQuery.Primitive(),
    71  		CountPrimitive: countPrim,
    72  	}
    73  }
    74  
    75  // All the methods below are not implemented. They should not be called on a sqlCalcFoundRows plan
    76  
    77  // Order implements the logicalPlan interface
    78  func (s *sqlCalcFoundRows) Order() int {
    79  	return s.LimitQuery.Order()
    80  }
    81  
    82  // ResultColumns implements the logicalPlan interface
    83  func (s *sqlCalcFoundRows) ResultColumns() []*resultColumn {
    84  	return s.LimitQuery.ResultColumns()
    85  }
    86  
    87  // Reorder implements the logicalPlan interface
    88  func (s *sqlCalcFoundRows) Reorder(order int) {
    89  	s.LimitQuery.Reorder(order)
    90  }
    91  
    92  // SupplyVar implements the logicalPlan interface
    93  func (s *sqlCalcFoundRows) SupplyVar(from, to int, col *sqlparser.ColName, varname string) {
    94  	s.LimitQuery.SupplyVar(from, to, col, varname)
    95  }
    96  
    97  // SupplyCol implements the logicalPlan interface
    98  func (s *sqlCalcFoundRows) SupplyCol(col *sqlparser.ColName) (*resultColumn, int) {
    99  	return s.LimitQuery.SupplyCol(col)
   100  }
   101  
   102  // SupplyWeightString implements the logicalPlan interface
   103  func (s *sqlCalcFoundRows) SupplyWeightString(int, bool) (weightcolNumber int, err error) {
   104  	return 0, UnsupportedSupplyWeightString{Type: "sqlCalcFoundRows"}
   105  }
   106  
   107  // Rewrite implements the logicalPlan interface
   108  func (s *sqlCalcFoundRows) Rewrite(inputs ...logicalPlan) error {
   109  	if len(inputs) != 2 {
   110  		return vterrors.VT13001(fmt.Sprintf("wrong number of inputs for SQL_CALC_FOUND_ROWS: %d", len(inputs)))
   111  	}
   112  	s.LimitQuery = inputs[0]
   113  	s.CountQuery = inputs[1]
   114  	return nil
   115  }
   116  
   117  // Inputs implements the logicalPlan interface
   118  func (s *sqlCalcFoundRows) Inputs() []logicalPlan {
   119  	return []logicalPlan{s.LimitQuery, s.CountQuery}
   120  }
   121  
   122  // OutputColumns implements the logicalPlan interface
   123  func (s *sqlCalcFoundRows) OutputColumns() []sqlparser.SelectExpr {
   124  	return s.LimitQuery.OutputColumns()
   125  }