diff --git a/pkg/sql/opt/memo/BUILD.bazel b/pkg/sql/opt/memo/BUILD.bazel index 0933d250cabc..d44105c48479 100644 --- a/pkg/sql/opt/memo/BUILD.bazel +++ b/pkg/sql/opt/memo/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg/testutils/buildutil:buildutil.bzl", "disallowed_imports_test") go_library( name = "memo", @@ -35,7 +36,6 @@ go_library( "//pkg/sql/opt/props/physical", "//pkg/sql/rowenc/keyside", "//pkg/sql/rowenc/valueside", - "//pkg/sql/sem/builtins", "//pkg/sql/sem/cast", "//pkg/sql/sem/eval", "//pkg/sql/sem/tree", @@ -117,3 +117,8 @@ genrule( exec_tools = ["//pkg/sql/opt/optgen/cmd/optgen"], visibility = ["//visibility:public"], ) + +disallowed_imports_test( + "memo", + ["//pkg/sql/sem/builtins"], +) diff --git a/pkg/sql/opt/memo/typing.go b/pkg/sql/opt/memo/typing.go index 17aacc19f9a4..931e1665852a 100644 --- a/pkg/sql/opt/memo/typing.go +++ b/pkg/sql/opt/memo/typing.go @@ -12,7 +12,6 @@ package memo import ( "github.com/cockroachdb/cockroach/pkg/sql/opt" - "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/errors" @@ -98,11 +97,16 @@ func BinaryAllowsNullArgs(op opt.Operator, leftType, rightType *types.T) bool { return o.NullableArgs } +// GetBuiltinProperties is set to builtins.GetBuiltinProperties in an init +// function in the norm package. This allows the memo package to resolve builtin +// functions without importing the builtins package. +var GetBuiltinProperties func(name string) (*tree.FunctionProperties, []tree.Overload) + // AggregateOverloadExists returns whether or not the given operator has a // unary overload which takes the given type as input. func AggregateOverloadExists(agg opt.Operator, typ *types.T) bool { name := opt.AggregateOpReverseMap[agg] - _, overloads := builtins.GetBuiltinProperties(name) + _, overloads := GetBuiltinProperties(name) for _, o := range overloads { if o.Types.MatchAt(typ, 0) { return true @@ -117,7 +121,7 @@ func AggregateOverloadExists(agg opt.Operator, typ *types.T) bool { func FindFunction( e opt.ScalarExpr, name string, ) (props *tree.FunctionProperties, overload *tree.Overload, ok bool) { - props, overloads := builtins.GetBuiltinProperties(name) + props, overloads := GetBuiltinProperties(name) for o := range overloads { overload = &overloads[o] if overload.Types.Length() != e.ChildCount() { diff --git a/pkg/sql/opt/norm/factory.go b/pkg/sql/opt/norm/factory.go index f65ba641c7ea..94b22758c189 100644 --- a/pkg/sql/opt/norm/factory.go +++ b/pkg/sql/opt/norm/factory.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" "github.com/cockroachdb/cockroach/pkg/sql/opt/memo" "github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" + "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -105,6 +106,12 @@ type Factory struct { // expression that is not fully optimized. const maxConstructorStackDepth = 10_000 +// Injecting this builtins dependency in the init function allows the memo +// package to access builtin properties without importing the builtins package. +func init() { + memo.GetBuiltinProperties = builtins.GetBuiltinProperties +} + // Init initializes a Factory structure with a new, blank memo structure inside. // This must be called before the factory can be used (or reused). //