github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/var_expr.go (about)

     1  // Copyright 2015 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 tree
    12  
    13  // isVar returns true if the expression's value can vary during plan
    14  // execution. The parameter allowConstPlaceholders should be true
    15  // in the common case of scalar expressions that will be evaluated
    16  // in the context of the execution of a prepared query, where the
    17  // placeholder will have the same value for every row processed.
    18  // It is set to false for scalar expressions that are not
    19  // evaluated as part of query execution, eg. DEFAULT expressions.
    20  func isVar(expr Expr) bool {
    21  	switch expr.(type) {
    22  	case VariableExpr:
    23  		return true
    24  	case *Placeholder:
    25  		return true
    26  	}
    27  	return false
    28  }
    29  
    30  type containsVarsVisitor struct {
    31  	containsVars bool
    32  }
    33  
    34  var _ Visitor = &containsVarsVisitor{}
    35  
    36  func (v *containsVarsVisitor) VisitPre(expr Expr) (recurse bool, newExpr Expr) {
    37  	if !v.containsVars && isVar(expr) {
    38  		v.containsVars = true
    39  	}
    40  	if v.containsVars {
    41  		return false, expr
    42  	}
    43  	return true, expr
    44  }
    45  
    46  func (*containsVarsVisitor) VisitPost(expr Expr) Expr { return expr }
    47  
    48  // ContainsVars returns true if the expression contains any variables.
    49  // (variables = sub-expressions, placeholders, indexed vars, etc.)
    50  func ContainsVars(expr Expr) bool {
    51  	v := containsVarsVisitor{containsVars: false}
    52  	WalkExprConst(&v, expr)
    53  	return v.containsVars
    54  }
    55  
    56  // DecimalOne represents the constant 1 as DECIMAL.
    57  var DecimalOne DDecimal
    58  
    59  func init() {
    60  	DecimalOne.SetInt64(1)
    61  }