Skip to content

Commit 3066741

Browse files
committed
opt: make scalar list custom func accessor safer
The custom functions `FirstScalarListExpr` and `SecondScalarListExpr` have been replaced by `ScalarExprAt`. This function is safer because it returns ok=false if the given index is out-of-bounds instead of panicking. This function has been added to `general_funcs.go` instead of `comp_funcs.go` in preparation for usage outside of `comp.opt`. Release note: None
1 parent e8b7b97 commit 3066741

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

pkg/sql/opt/norm/comp_funcs.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ func (c *CustomFuncs) NormalizeTupleEquality(left, right memo.ScalarListExpr) op
7676
return result
7777
}
7878

79-
// FirstScalarListExpr returns the first ScalarExpr in the given list.
80-
func (c *CustomFuncs) FirstScalarListExpr(list memo.ScalarListExpr) opt.ScalarExpr {
81-
return list[0]
82-
}
83-
84-
// SecondScalarListExpr returns the second ScalarExpr in the given list.
85-
func (c *CustomFuncs) SecondScalarListExpr(list memo.ScalarListExpr) opt.ScalarExpr {
86-
return list[1]
87-
}
88-
8979
// MakeTimeZoneFunction constructs a new timezone() function with the given zone
9080
// and timestamp as arguments. The type of the function result is TIMESTAMPTZ if
9181
// ts is of type TIMESTAMP, or TIMESTAMP if is of type TIMESTAMPTZ.

pkg/sql/opt/norm/general_funcs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,22 @@ func (c *CustomFuncs) IsLeakproof(expr memo.RelExpr) bool {
658658
return expr.Relational().VolatilitySet.IsLeakproof()
659659
}
660660

661+
// ----------------------------------------------------------------------
662+
//
663+
// ScalarListExpr functions
664+
// General functions related to ScalarListExpr.
665+
//
666+
// ----------------------------------------------------------------------
667+
668+
// ScalarExprAt returns the ScalarExpr in the i-th position in the given list.
669+
// Returns ok=false if i is out of bounds.
670+
func (c *CustomFuncs) ScalarExprAt(list memo.ScalarListExpr, i int) (_ opt.ScalarExpr, ok bool) {
671+
if i >= 0 && i < len(list) {
672+
return list[i], true
673+
}
674+
return nil, false
675+
}
676+
661677
// ----------------------------------------------------------------------
662678
//
663679
// Ordering functions

pkg/sql/opt/norm/rules/comp.opt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@
220220
(Function $args:* $private:(FunctionPrivate "timezone"))
221221
$right:(ConstValue) &
222222
(IsTimestampTZ $right) &
223-
(IsTimestamp $ts:(SecondScalarListExpr $args)) &
224-
^(IsConstValueOrGroupOfConstValues $ts)
223+
(Let ($ts $tsOk):(ScalarExprAt $args 1) $tsOk) &
224+
(IsTimestamp $ts) &
225+
^(IsConstValueOrGroupOfConstValues $ts) &
226+
(Let ($zone $zoneOk):(ScalarExprAt $args 0) $zoneOk)
225227
)
226228
=>
227-
((OpName)
228-
$ts
229-
(MakeTimeZoneFunction (FirstScalarListExpr $args) $right)
230-
)
229+
((OpName) $ts (MakeTimeZoneFunction $zone $right))
231230

232231
# NormalizeCmpTimeZoneFunctionTZ normalizes timezone functions within
233232
# comparison operators. It only matches expressions when:
@@ -249,14 +248,13 @@
249248
(Function $args:* $private:(FunctionPrivate "timezone"))
250249
$right:(ConstValue) &
251250
(IsTimestamp $right) &
252-
(IsTimestampTZ $tz:(SecondScalarListExpr $args)) &
253-
^(IsConstValueOrGroupOfConstValues $tz)
251+
(Let ($tz $tzOk):(ScalarExprAt $args 1) $tzOk) &
252+
(IsTimestampTZ $tz) &
253+
^(IsConstValueOrGroupOfConstValues $tz) &
254+
(Let ($zone $zoneOk):(ScalarExprAt $args 0) $zoneOk)
254255
)
255256
=>
256-
((OpName)
257-
$tz
258-
(MakeTimeZoneFunction (FirstScalarListExpr $args) $right)
259-
)
257+
((OpName) $tz (MakeTimeZoneFunction $zone $right))
260258

261259
# FoldEqTrue replaces x = True with x.
262260
[FoldEqTrue, Normalize]

0 commit comments

Comments
 (0)