vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/simple_projection.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 engine
    18  
    19  import (
    20  	"context"
    21  
    22  	"vitess.io/vitess/go/sqltypes"
    23  	querypb "vitess.io/vitess/go/vt/proto/query"
    24  )
    25  
    26  var _ Primitive = (*SimpleProjection)(nil)
    27  
    28  // SimpleProjection selects which columns to keep from the input
    29  type SimpleProjection struct {
    30  	// Cols defines the column numbers from the underlying primitive
    31  	// to be returned.
    32  	Cols  []int
    33  	Input Primitive
    34  }
    35  
    36  // NeedsTransaction implements the Primitive interface
    37  func (sc *SimpleProjection) NeedsTransaction() bool {
    38  	return sc.Input.NeedsTransaction()
    39  }
    40  
    41  // RouteType returns a description of the query routing type used by the primitive
    42  func (sc *SimpleProjection) RouteType() string {
    43  	return sc.Input.RouteType()
    44  }
    45  
    46  // GetKeyspaceName specifies the Keyspace that this primitive routes to.
    47  func (sc *SimpleProjection) GetKeyspaceName() string {
    48  	return sc.Input.GetKeyspaceName()
    49  }
    50  
    51  // GetTableName specifies the table that this primitive routes to.
    52  func (sc *SimpleProjection) GetTableName() string {
    53  	return sc.Input.GetTableName()
    54  }
    55  
    56  // TryExecute performs a non-streaming exec.
    57  func (sc *SimpleProjection) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
    58  	inner, err := vcursor.ExecutePrimitive(ctx, sc.Input, bindVars, wantfields)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return sc.buildResult(inner), nil
    63  }
    64  
    65  // TryStreamExecute performs a streaming exec.
    66  func (sc *SimpleProjection) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool, callback func(*sqltypes.Result) error) error {
    67  	return vcursor.StreamExecutePrimitive(ctx, sc.Input, bindVars, wantfields, func(inner *sqltypes.Result) error {
    68  		return callback(sc.buildResult(inner))
    69  	})
    70  }
    71  
    72  // GetFields fetches the field info.
    73  func (sc *SimpleProjection) GetFields(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) {
    74  	inner, err := sc.Input.GetFields(ctx, vcursor, bindVars)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	return &sqltypes.Result{Fields: sc.buildFields(inner)}, nil
    79  }
    80  
    81  // Inputs returns the input to this primitive
    82  func (sc *SimpleProjection) Inputs() []Primitive {
    83  	return []Primitive{sc.Input}
    84  }
    85  
    86  // buildResult builds a new result by pulling the necessary columns from
    87  // the input in the requested order.
    88  func (sc *SimpleProjection) buildResult(inner *sqltypes.Result) *sqltypes.Result {
    89  	result := &sqltypes.Result{Fields: sc.buildFields(inner)}
    90  	result.Rows = make([][]sqltypes.Value, 0, len(inner.Rows))
    91  	for _, innerRow := range inner.Rows {
    92  		row := make([]sqltypes.Value, 0, len(sc.Cols))
    93  		for _, col := range sc.Cols {
    94  			row = append(row, innerRow[col])
    95  		}
    96  		result.Rows = append(result.Rows, row)
    97  	}
    98  	result.RowsAffected = inner.RowsAffected
    99  	return result
   100  }
   101  
   102  func (sc *SimpleProjection) buildFields(inner *sqltypes.Result) []*querypb.Field {
   103  	if len(inner.Fields) == 0 {
   104  		return nil
   105  	}
   106  	fields := make([]*querypb.Field, 0, len(sc.Cols))
   107  	for _, col := range sc.Cols {
   108  		fields = append(fields, inner.Fields[col])
   109  	}
   110  	return fields
   111  }
   112  
   113  func (sc *SimpleProjection) description() PrimitiveDescription {
   114  	other := map[string]any{
   115  		"Columns": sc.Cols,
   116  	}
   117  	return PrimitiveDescription{
   118  		OperatorType: "SimpleProjection",
   119  		Other:        other,
   120  	}
   121  }