MCPcopy Create free account
hub / github.com/auxten/postgresql-parser / normalize

Method normalize

pkg/sql/sem/tree/normalize.go:219–517  ·  view source on GitHub ↗
(v *NormalizeVisitor)

Source from the content-addressed store, hash-verified

217}
218
219func (expr *ComparisonExpr) normalize(v *NormalizeVisitor) TypedExpr {
220 switch expr.Operator {
221 case EQ, GE, GT, LE, LT:
222 // We want var nodes (VariableExpr, VarName, etc) to be immediate
223 // children of the comparison expression and not second or third
224 // children. That is, we want trees that look like:
225 //
226 // cmp cmp
227 // / \ / \
228 // a op op a
229 // / \ / \
230 // 1 2 1 2
231 //
232 // Not trees that look like:
233 //
234 // cmp cmp cmp cmp
235 // / \ / \ / \ / \
236 // op 2 op 2 1 op 1 op
237 // / \ / \ / \ / \
238 // a 1 1 a a 2 2 a
239 //
240 // We loop attempting to simplify the comparison expression. As a
241 // pre-condition, we know there is at least one variable in the expression
242 // tree or we would not have entered this code path.
243 exprCopied := false
244 for {
245 if expr.TypedLeft() == DNull || expr.TypedRight() == DNull {
246 return DNull
247 }
248
249 if v.isConst(expr.Left) {
250 switch expr.Right.(type) {
251 case *BinaryExpr, VariableExpr:
252 break
253 default:
254 return expr
255 }
256
257 invertedOp, err := invertComparisonOp(expr.Operator)
258 if err != nil {
259 v.err = err
260 return expr
261 }
262
263 // The left side is const and the right side is a binary expression or a
264 // variable. Flip the comparison op so that the right side is const and
265 // the left side is a binary expression or variable.
266 // Create a new ComparisonExpr so the function cache isn't reused.
267 if !exprCopied {
268 exprCopy := *expr
269 expr = &exprCopy
270 exprCopied = true
271 }
272
273 expr = NewTypedComparisonExpr(invertedOp, expr.TypedRight(), expr.TypedLeft())
274 } else if !v.isConst(expr.Right) {
275 return expr
276 }

Callers

nothing calls this directly

Calls 15

TypedLeftMethod · 0.95
TypedRightMethod · 0.95
EvalMethod · 0.95
memoizeFnMethod · 0.95
AddMethod · 0.95
BuildMethod · 0.95
NewObjectBuilderFunction · 0.92
invertComparisonOpFunction · 0.85
NewTypedComparisonExprFunction · 0.85
isVarFunction · 0.85
MakeDJSONFunction · 0.85

Tested by

no test coverage detected