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

Method normalize

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

Source from the content-addressed store, hash-verified

587}
588
589func (expr *RangeCond) normalize(v *NormalizeVisitor) TypedExpr {
590 leftFrom, from := expr.TypedLeftFrom(), expr.TypedFrom()
591 leftTo, to := expr.TypedLeftTo(), expr.TypedTo()
592 // The visitor hasn't walked down into leftTo; do it now.
593 if leftTo, v.err = v.ctx.NormalizeExpr(leftTo); v.err != nil {
594 return expr
595 }
596
597 if (leftFrom == DNull || from == DNull) && (leftTo == DNull || to == DNull) {
598 return DNull
599 }
600
601 leftCmp := GE
602 rightCmp := LE
603 if expr.Not {
604 leftCmp = LT
605 rightCmp = GT
606 }
607
608 // "a BETWEEN b AND c" -> "a >= b AND a <= c"
609 // "a NOT BETWEEN b AND c" -> "a < b OR a > c"
610 transform := func(from, to TypedExpr) TypedExpr {
611 var newLeft, newRight TypedExpr
612 if from == DNull {
613 newLeft = DNull
614 } else {
615 newLeft = NewTypedComparisonExpr(leftCmp, leftFrom, from).normalize(v)
616 if v.err != nil {
617 return expr
618 }
619 }
620 if to == DNull {
621 newRight = DNull
622 } else {
623 newRight = NewTypedComparisonExpr(rightCmp, leftTo, to).normalize(v)
624 if v.err != nil {
625 return expr
626 }
627 }
628 if expr.Not {
629 return NewTypedOrExpr(newLeft, newRight).normalize(v)
630 }
631 return NewTypedAndExpr(newLeft, newRight).normalize(v)
632 }
633
634 out := transform(from, to)
635 if expr.Symmetric {
636 if expr.Not {
637 // "a NOT BETWEEN SYMMETRIC b AND c" -> "(a < b OR a > c) AND (a < c OR a > b)"
638 out = NewTypedAndExpr(out, transform(to, from)).normalize(v)
639 } else {
640 // "a BETWEEN SYMMETRIC b AND c" -> "(a >= b AND a <= c) OR (a >= c OR a <= b)"
641 out = NewTypedOrExpr(out, transform(to, from)).normalize(v)
642 }
643 }
644 return out
645}
646

Callers

nothing calls this directly

Calls 9

TypedLeftFromMethod · 0.95
TypedFromMethod · 0.95
TypedLeftToMethod · 0.95
TypedToMethod · 0.95
NewTypedComparisonExprFunction · 0.85
NewTypedOrExprFunction · 0.85
NewTypedAndExprFunction · 0.85
NormalizeExprMethod · 0.80
normalizeMethod · 0.65

Tested by

no test coverage detected