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

     1  /*
     2  Copyright 2021 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 operators
    18  
    19  import (
    20  	"vitess.io/vitess/go/vt/sqlparser"
    21  	"vitess.io/vitess/go/vt/vterrors"
    22  	"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops"
    23  	"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
    24  	"vitess.io/vitess/go/vt/vtgate/semantics"
    25  	"vitess.io/vitess/go/vt/vtgate/vindexes"
    26  )
    27  
    28  type (
    29  	Table struct {
    30  		QTable  *QueryTable
    31  		VTable  *vindexes.Table
    32  		Columns []*sqlparser.ColName
    33  
    34  		noInputs
    35  	}
    36  	ColNameColumns interface {
    37  		GetColumns() []*sqlparser.ColName
    38  		AddCol(*sqlparser.ColName)
    39  	}
    40  )
    41  
    42  var _ ops.PhysicalOperator = (*Table)(nil)
    43  
    44  // IPhysical implements the PhysicalOperator interface
    45  func (to *Table) IPhysical() {}
    46  
    47  // Clone implements the Operator interface
    48  func (to *Table) Clone([]ops.Operator) ops.Operator {
    49  	var columns []*sqlparser.ColName
    50  	for _, name := range to.Columns {
    51  		columns = append(columns, sqlparser.CloneRefOfColName(name))
    52  	}
    53  	return &Table{
    54  		QTable:  to.QTable,
    55  		VTable:  to.VTable,
    56  		Columns: columns,
    57  	}
    58  }
    59  
    60  // Introduces implements the PhysicalOperator interface
    61  func (to *Table) Introduces() semantics.TableSet {
    62  	return to.QTable.ID
    63  }
    64  
    65  // AddPredicate implements the PhysicalOperator interface
    66  func (to *Table) AddPredicate(_ *plancontext.PlanningContext, expr sqlparser.Expr) (ops.Operator, error) {
    67  	return newFilter(to, expr), nil
    68  }
    69  
    70  func (to *Table) AddColumn(_ *plancontext.PlanningContext, e sqlparser.Expr) (int, error) {
    71  	return addColumn(to, e)
    72  }
    73  
    74  func (to *Table) GetColumns() []*sqlparser.ColName {
    75  	return to.Columns
    76  }
    77  func (to *Table) AddCol(col *sqlparser.ColName) {
    78  	to.Columns = append(to.Columns, col)
    79  }
    80  
    81  func (to *Table) TablesUsed() []string {
    82  	if sqlparser.SystemSchema(to.QTable.Table.Qualifier.String()) {
    83  		return nil
    84  	}
    85  	return SingleQualifiedIdentifier(to.VTable.Keyspace, to.VTable.Name)
    86  }
    87  
    88  func addColumn(op ColNameColumns, e sqlparser.Expr) (int, error) {
    89  	col, ok := e.(*sqlparser.ColName)
    90  	if !ok {
    91  		return 0, vterrors.VT13001("cannot push this expression to a table/vindex")
    92  	}
    93  	cols := op.GetColumns()
    94  	for idx, column := range cols {
    95  		if col.Name.Equal(column.Name) {
    96  			return idx, nil
    97  		}
    98  	}
    99  	offset := len(cols)
   100  	op.AddCol(col)
   101  	return offset, nil
   102  }