Skip to content

Commit addc3c6

Browse files
committed
opt/optgen: compile string and integer literals as Go values
Previously, optgen would sometimes compile string and integer literals, e.g., `"foo"` and `1`, as Go strings and integers, and other times as `*tree.DString`s and `*tree.DInt`s, depending on the context. Now, these literals are always compiled as Go values. In addition to helping prevent confusion, this can help avoid unnecessary allocations for datums when they are not needed, e.g., see `NormalizeLikeAny`. Release note: None
1 parent cdf2449 commit addc3c6

File tree

8 files changed

+22
-18
lines changed

8 files changed

+22
-18
lines changed

pkg/sql/opt/norm/general_funcs.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,11 @@ func (c *CustomFuncs) CanAddConstInts(first tree.Datum, second tree.Datum) bool
15401540
return ok
15411541
}
15421542

1543+
// DInt returns a new *tree.DInt with the given integer value.
1544+
func (c *CustomFuncs) DInt(i tree.DInt) *tree.DInt {
1545+
return tree.NewDInt(i)
1546+
}
1547+
15431548
// IntConst constructs a Const holding a DInt.
15441549
func (c *CustomFuncs) IntConst(d *tree.DInt) opt.ScalarExpr {
15451550
return c.f.ConstructConst(d, types.Int)
@@ -1562,9 +1567,9 @@ func (c *CustomFuncs) StringFromConst(expr opt.ScalarExpr) (string, bool) {
15621567

15631568
// ConstStringEquals returns true if e is a constant string expression and is
15641569
// equal to other.
1565-
func (c *CustomFuncs) ConstStringEquals(e opt.ScalarExpr, other *tree.DString) bool {
1570+
func (c *CustomFuncs) ConstStringEquals(e opt.ScalarExpr, other string) bool {
15661571
if eStr, ok := c.StringFromConst(e); ok {
1567-
return eStr == string(*other)
1572+
return eStr == other
15681573
}
15691574
return false
15701575
}

pkg/sql/opt/norm/rules/decorrelate.opt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@
707707
$left:*
708708
$right:(Limit $input:* (Const $limit:*) $ordering:*) &
709709
(HasOuterCols $right) &
710-
(IsGreaterThan $limit 1)
710+
(IsGreaterThan $limit (DInt 1))
711711
$on:*
712712
$private:*
713713
)

pkg/sql/opt/norm/rules/groupby.opt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
(ConstructProjectionFromDistinctOn
325325
(Limit
326326
$input
327-
(IntConst 1)
327+
(IntConst (DInt 1))
328328
(GroupingInputOrdering $groupingPrivate)
329329
)
330330
(MakeEmptyColSet)
@@ -610,7 +610,7 @@
610610
# a. The aggregate only references cols from the Window operator's input.
611611
# b. The aggregate is a ConstAgg (or ConstNotNull, AnyNotNull, or FirstAgg)
612612
# that passes through the result of a window function.
613-
#
613+
#
614614
# Assuming all of the above are satisfied, each GroupBy aggregate that only
615615
# references the Window's input can be left alone (5a). Then, each ConstAgg
616616
# referencing a window function can be replaced by that function (5b).

pkg/sql/opt/norm/rules/scalar.opt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ $input
240240
[
241241
(Subquery
242242
(Project
243-
(Limit $input (IntConst 1) (EmptyOrdering))
243+
(Limit
244+
$input
245+
(IntConst (DInt 1))
246+
(EmptyOrdering)
247+
)
244248
[ (ProjectionsItem (True) (MakeBoolCol)) ]
245249
(MakeEmptyColSet)
246250
)

pkg/sql/opt/optgen/cmd/optgen/explorer_gen.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (g *explorerGen) generate(compiled *lang.CompiledExpr, w io.Writer) {
3232
g.w.writeIndent("\"github.com/cockroachdb/cockroach/pkg/sql/opt\"\n")
3333
g.w.writeIndent("\"github.com/cockroachdb/cockroach/pkg/sql/opt/memo\"\n")
3434
g.w.writeIndent("\"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical\"\n")
35-
g.w.writeIndent("\"github.com/cockroachdb/cockroach/pkg/sql/sem/tree\"\n")
3635
g.w.unnest(")\n\n")
3736

3837
g.genDispatcher()

pkg/sql/opt/optgen/cmd/optgen/rule_gen.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -916,12 +916,12 @@ func (g *newRuleGen) genNestedExpr(e lang.Expr) {
916916
g.w.write("%s", string(t.Result.Label))
917917

918918
case *lang.StringExpr:
919-
// Literal string expressions construct DString datums.
920-
g.w.write("tree.NewDString(%s)", t)
919+
// Literal string expressions construct Go strings.
920+
g.w.write("%s", t)
921921

922922
case *lang.NumberExpr:
923-
// Literal numeric expressions construct DInt datums.
924-
g.w.write("tree.NewDInt(%s)", t)
923+
// Literal numeric expressions construct Go ints.
924+
g.w.write("%s", t)
925925

926926
case *lang.NameExpr:
927927
// OpName literal expressions construct an op identifier like SelectOp,

pkg/sql/opt/optgen/cmd/optgen/testdata/explorer

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ import (
7878
"github.com/cockroachdb/cockroach/pkg/sql/opt"
7979
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
8080
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
81-
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
8281
)
8382

8483
func (_e *explorer) exploreGroupMember(
@@ -270,7 +269,6 @@ import (
270269
"github.com/cockroachdb/cockroach/pkg/sql/opt"
271270
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
272271
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
273-
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
274272
)
275273

276274
func (_e *explorer) exploreGroupMember(
@@ -393,7 +391,6 @@ import (
393391
"github.com/cockroachdb/cockroach/pkg/sql/opt"
394392
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
395393
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
396-
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
397394
)
398395

399396
func (_e *explorer) exploreGroupMember(
@@ -550,7 +547,6 @@ import (
550547
"github.com/cockroachdb/cockroach/pkg/sql/opt"
551548
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
552549
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
553-
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
554550
)
555551

556552
func (_e *explorer) exploreGroupMember(

pkg/sql/opt/xform/rules/groupby.opt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
$input
6161
[ (FiltersItem (IsNot $variable (Null (AnyType)))) ]
6262
)
63-
(IntConst 1)
63+
(IntConst (DInt 1))
6464
(MakeOrderingChoiceFromColumn (OpName $agg) $col)
6565
)
6666
[ (AggregationsItem (ConstAgg $variable) $aggPrivate) ]
@@ -108,7 +108,7 @@
108108
(MakeProjectFromPassthroughAggs
109109
(Limit
110110
$input
111-
(IntConst 1)
111+
(IntConst (DInt 1))
112112
(MakeOrderingChoiceFromColumn Min $col)
113113
)
114114
$aggregations
@@ -140,7 +140,7 @@
140140
(MakeProjectFromPassthroughAggs
141141
(Limit
142142
$input
143-
(IntConst 1)
143+
(IntConst (DInt 1))
144144
(MakeOrderingChoiceFromColumn Max $col)
145145
)
146146
$aggregations

0 commit comments

Comments
 (0)