github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/transform/expr_transform.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package transform
    12  
    13  import (
    14  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    15  	"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
    16  )
    17  
    18  // ExprTransformContext supports the methods that test expression
    19  // properties and can normalize expressions, defined below.  This
    20  // should be used in planner instance to avoid re-allocation of these
    21  // visitors between uses.
    22  type ExprTransformContext struct {
    23  	normalizeVisitor   tree.NormalizeVisitor
    24  	isAggregateVisitor IsAggregateVisitor
    25  }
    26  
    27  // NormalizeExpr is a wrapper around EvalContex.NormalizeExpr which
    28  // avoids allocation of a normalizeVisitor. See normalize.go for
    29  // details.
    30  func (t *ExprTransformContext) NormalizeExpr(
    31  	ctx *tree.EvalContext, typedExpr tree.TypedExpr,
    32  ) (tree.TypedExpr, error) {
    33  	if ctx.SkipNormalize {
    34  		return typedExpr, nil
    35  	}
    36  	t.normalizeVisitor = tree.MakeNormalizeVisitor(ctx)
    37  	expr, _ := tree.WalkExpr(&t.normalizeVisitor, typedExpr)
    38  	if err := t.normalizeVisitor.Err(); err != nil {
    39  		return nil, err
    40  	}
    41  	return expr.(tree.TypedExpr), nil
    42  }
    43  
    44  // AggregateInExpr determines if an Expr contains an aggregate function.
    45  // TODO(knz/radu): this is not the right way to go about checking
    46  // these things. Instead whatever analysis occurs prior on the expression
    47  // should collect scalar properties (see tree.ScalarProperties) and
    48  // then the collected properties should be tested directly.
    49  func (t *ExprTransformContext) AggregateInExpr(
    50  	expr tree.Expr, searchPath sessiondata.SearchPath,
    51  ) bool {
    52  	if expr == nil {
    53  		return false
    54  	}
    55  
    56  	t.isAggregateVisitor = IsAggregateVisitor{
    57  		searchPath: searchPath,
    58  	}
    59  	tree.WalkExprConst(&t.isAggregateVisitor, expr)
    60  	return t.isAggregateVisitor.Aggregated
    61  }