vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/distinct.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  	"vitess.io/vitess/go/vt/vterrors"
    21  	"vitess.io/vitess/go/vt/vtgate/engine"
    22  )
    23  
    24  var _ logicalPlan = (*distinct)(nil)
    25  
    26  // distinct is the logicalPlan for engine.Distinct.
    27  type distinct struct {
    28  	logicalPlanCommon
    29  	checkCols      []engine.CheckCol
    30  	needToTruncate bool
    31  }
    32  
    33  func newDistinct(source logicalPlan, checkCols []engine.CheckCol, needToTruncate bool) logicalPlan {
    34  	return &distinct{
    35  		logicalPlanCommon: newBuilderCommon(source),
    36  		checkCols:         checkCols,
    37  		needToTruncate:    needToTruncate,
    38  	}
    39  }
    40  
    41  func newDistinctV3(source logicalPlan) logicalPlan {
    42  	return &distinct{logicalPlanCommon: newBuilderCommon(source)}
    43  }
    44  
    45  func (d *distinct) Primitive() engine.Primitive {
    46  	if d.checkCols == nil {
    47  		// If we are missing the checkCols information, we are on the V3 planner and should produce a V3 Distinct
    48  		return &engine.DistinctV3{Source: d.input.Primitive()}
    49  	}
    50  	truncate := false
    51  	if d.needToTruncate {
    52  		for _, col := range d.checkCols {
    53  			if col.WsCol != nil {
    54  				truncate = true
    55  				break
    56  			}
    57  		}
    58  	}
    59  	return &engine.Distinct{
    60  		Source:    d.input.Primitive(),
    61  		CheckCols: d.checkCols,
    62  		Truncate:  truncate,
    63  	}
    64  }
    65  
    66  // Rewrite implements the logicalPlan interface
    67  func (d *distinct) Rewrite(inputs ...logicalPlan) error {
    68  	if len(inputs) != 1 {
    69  		return vterrors.VT13001("distinct: wrong number of inputs")
    70  	}
    71  	d.input = inputs[0]
    72  	return nil
    73  }
    74  
    75  // Inputs implements the logicalPlan interface
    76  func (d *distinct) Inputs() []logicalPlan {
    77  	return []logicalPlan{d.input}
    78  }