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

     1  /*
     2  Copyright 2022 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/vterrors"
    21  	"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/rewrite"
    22  
    23  	"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops"
    24  )
    25  
    26  var errNotHorizonPlanned = vterrors.VT12001("query cannot be fully operator planned")
    27  
    28  func planHorizons(in ops.Operator) (ops.Operator, error) {
    29  	return rewrite.TopDown(in, func(in ops.Operator) (ops.Operator, rewrite.TreeIdentity, rewrite.VisitRule, error) {
    30  		switch in := in.(type) {
    31  		case *Horizon:
    32  			op, err := planHorizon(in)
    33  			if err != nil {
    34  				return nil, rewrite.SameTree, rewrite.SkipChildren, err
    35  			}
    36  			return op, rewrite.NewTree, rewrite.VisitChildren, nil
    37  		case *Route:
    38  			return in, rewrite.SameTree, rewrite.SkipChildren, nil
    39  		default:
    40  			return in, rewrite.SameTree, rewrite.VisitChildren, nil
    41  		}
    42  	})
    43  }
    44  
    45  func planHorizon(in *Horizon) (ops.Operator, error) {
    46  	rb, isRoute := in.Source.(*Route)
    47  	if !isRoute {
    48  		return in, nil
    49  	}
    50  	if isRoute && rb.IsSingleShard() && in.Select.GetLimit() == nil {
    51  		return planSingleShardRoute(rb, in)
    52  	}
    53  
    54  	return nil, errNotHorizonPlanned
    55  }
    56  func planSingleShardRoute(rb *Route, horizon *Horizon) (ops.Operator, error) {
    57  	rb.Source, horizon.Source = horizon, rb.Source
    58  	return rb, nil
    59  }