-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql,ccl: allow builtins to be registered from anywhere
Previously, builtin functions needed to be created from inside the builtins package. This lead to a giant package with all the implementations, and also made it difficult to register non-CCL builtins conditionally on the build being non-oss. This commit makes builtin registration capable of happening in any init() code by making everything that needs to iterate over all builtins provide a hook instead of forcing a particular build order. This commit only leverages that to move sql telemetry logic out of the builtins package. The following commit uses it to create a CCL builtin function. Release note: None Release justification: Refactor necessary for CCL functions.
- Loading branch information
Showing
35 changed files
with
234 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package utilccl | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinsregistry" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
) | ||
|
||
// RegisterCCLBuiltin adds a builtin defined in CCL code to the global builtins registry. | ||
func RegisterCCLBuiltin(name string, description string, overload tree.Overload) { | ||
props := tree.FunctionProperties{ | ||
Class: tree.NormalClass, | ||
Category: `CCL-only internal function`, | ||
} | ||
|
||
builtinsregistry.Register(name, &props, []tree.Overload{overload}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2017 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package utilccl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
"github.com/cockroachdb/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func unexportedFunction(s string) string { | ||
return "behold: " + s | ||
} | ||
|
||
func TestRegister(t *testing.T) { | ||
RegisterCCLBuiltin("test_builtin_from_unexported_function", "", unexportedFunction) | ||
RegisterCCLBuiltin("test_builtin_with_error", "", func(s string) (string, error) { | ||
if s == "dog" { | ||
return "pet the dog", nil | ||
} | ||
return s, errors.New("please provide dog") | ||
}) | ||
resolver := func(fn string) (*tree.FunctionDefinition, error) { | ||
name := tree.UnresolvedName{NumParts: 1, Parts: [4]string{fn}} | ||
return name.ResolveFunction(nil) | ||
} | ||
wrapper, err := resolver("test_builtin_from_unexported_function") | ||
require.NoError(t, err) | ||
require.Equal(t, "(: string) -> string", wrapper.Definition[0].(*tree.Overload).Signature(true)) | ||
|
||
args := tree.Datums{tree.NewDString("dog")} | ||
ctx := eval.MakeTestingEvalContext(nil) | ||
o, err := wrapper.Definition[0].(*tree.Overload).Fn.(eval.FnOverload)(&ctx, args) | ||
require.NoError(t, err) | ||
require.Equal(t, o.ResolvedType(), types.String) | ||
require.Equal(t, "'behold: dog'", o.String()) | ||
|
||
petter, err := resolver("test_builtin_with_error") | ||
require.NoError(t, err) | ||
require.Equal(t, "(: string) -> string", petter.Definition[0].(*tree.Overload).Signature(true)) | ||
|
||
_, err = petter.Definition[0].(*tree.Overload).Fn.(eval.FnOverload)(&ctx, args) | ||
require.NoError(t, err) | ||
|
||
args[0] = tree.NewDString("no dog") | ||
_, err = petter.Definition[0].(*tree.Overload).Fn.(eval.FnOverload)(&ctx, args) | ||
require.Error(t, err) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.