From 5a727367567b33c694b8c1668442f38914e29b18 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 21 Nov 2023 23:48:38 -0800 Subject: [PATCH 01/10] add functions to sdk --- pkg/sdk/client.go | 2 + pkg/sdk/functions_def.go | 410 +++++++++++ pkg/sdk/functions_dto_builders_gen.go | 663 ++++++++++++++++++ pkg/sdk/functions_dto_gen.go | 190 +++++ pkg/sdk/functions_gen.go | 282 ++++++++ pkg/sdk/functions_gen_test.go | 528 ++++++++++++++ pkg/sdk/functions_impl_gen.go | 538 ++++++++++++++ pkg/sdk/functions_validations_gen.go | 138 ++++ pkg/sdk/poc/main.go | 1 + pkg/sdk/testint/functions_integration_test.go | 407 +++++++++++ 10 files changed, 3159 insertions(+) create mode 100644 pkg/sdk/functions_def.go create mode 100644 pkg/sdk/functions_dto_builders_gen.go create mode 100644 pkg/sdk/functions_dto_gen.go create mode 100644 pkg/sdk/functions_gen.go create mode 100644 pkg/sdk/functions_gen_test.go create mode 100644 pkg/sdk/functions_impl_gen.go create mode 100644 pkg/sdk/functions_validations_gen.go create mode 100644 pkg/sdk/testint/functions_integration_test.go diff --git a/pkg/sdk/client.go b/pkg/sdk/client.go index ac448aabc4..61fa4cf175 100644 --- a/pkg/sdk/client.go +++ b/pkg/sdk/client.go @@ -38,6 +38,7 @@ type Client struct { ExternalTables ExternalTables FailoverGroups FailoverGroups FileFormats FileFormats + Functions Functions Grants Grants MaskingPolicies MaskingPolicies NetworkPolicies NetworkPolicies @@ -165,6 +166,7 @@ func (c *Client) initialize() { c.ExternalTables = &externalTables{client: c} c.FailoverGroups = &failoverGroups{client: c} c.FileFormats = &fileFormats{client: c} + c.Functions = &functions{client: c} c.Grants = &grants{client: c} c.MaskingPolicies = &maskingPolicies{client: c} c.NetworkPolicies = &networkPolicies{client: c} diff --git a/pkg/sdk/functions_def.go b/pkg/sdk/functions_def.go new file mode 100644 index 0000000000..95519d4859 --- /dev/null +++ b/pkg/sdk/functions_def.go @@ -0,0 +1,410 @@ +package sdk + +import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator" + +//go:generate go run ./poc/main.go + +var functionArgument = g.NewQueryStruct("FunctionArgument"). + Text("ArgName", g.KeywordOptions().NoQuotes()). + Text("ArgDataType", g.KeywordOptions().NoQuotes()) + +var functionArgumentType = g.NewQueryStruct("FunctionArgumentType"). + Text("ArgDataType", g.KeywordOptions().NoQuotes()) + +var functionColumn = g.NewQueryStruct("FunctionColumn"). + Text("ColumnName", g.KeywordOptions().NoQuotes()). + Text("ColumnDataType", g.KeywordOptions().NoQuotes()) + +var functionSecret = g.NewQueryStruct("FunctionSecret"). + Text("SecretVariableName", g.KeywordOptions().SingleQuotes()). + Text("SecretName", g.KeywordOptions().NoQuotes()) + +var functionReturns = g.NewQueryStruct("FunctionReturns"). + OptionalText("ResultDataType", g.KeywordOptions()). + OptionalQueryStructField( + "Table", + g.NewQueryStruct("FunctionReturnsTable"). + ListQueryStructField( + "Columns", + functionColumn, + g.ParameterOptions().Parentheses().NoEquals(), + ), + g.KeywordOptions().SQL("TABLE"), + ) + +var functionSet = g.NewQueryStruct("FunctionSet"). + OptionalTextAssignment("LOG_LEVEL", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("TRACE_LEVEL", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). + OptionalSQL("SECURE") + +var functionUnset = g.NewQueryStruct("FunctionUnset"). + OptionalSQL("SECURE"). + OptionalSQL("COMMENT"). + OptionalSQL("LOG_LEVEL"). + OptionalSQL("TRACE_LEVEL") + +var ( + functionNullOrNot = g.NewQueryStruct("FunctionNullOrNot").OptionalSQL("NULL").OptionalSQL("NOT NULL") + functionStrictOrNot = g.NewQueryStruct("FunctionStrictOrNot").OptionalSQL("STRICT").OptionalSQL("CALLED ON NULL INPUT") + functionVolatileOrNot = g.NewQueryStruct("FunctionVolatileOrNot").OptionalSQL("VOLATILE").OptionalSQL("IMMUTABLE") + functionImports = g.NewQueryStruct("FunctionImports").Text("Import", g.KeywordOptions().SingleQuotes()) + functionPackages = g.NewQueryStruct("FunctionPackages").Text("Package", g.KeywordOptions().SingleQuotes()) + functionDefinition = g.NewQueryStruct("FunctionDefinition").Text("Definition", g.KeywordOptions()) +) + +var FunctionsDef = g.NewInterface( + "Functions", + "Function", + g.KindOfT[SchemaObjectIdentifier](), +).CustomOperation( + "CreateFunctionForJava", + "https://docs.snowflake.com/en/sql-reference/sql/create-function", + g.NewQueryStruct("CreateFunctionForJava"). + Create(). + OrReplace(). + OptionalSQL("TEMPORARY"). + OptionalSQL("SECURE"). + SQL("FUNCTION"). + IfNotExists(). + Name(). + ListQueryStructField( + "Arguments", + functionArgument, + g.ParameterOptions().Parentheses().NoEquals()). + OptionalSQL("COPY GRANTS"). + OptionalQueryStructField( + "Returns", + functionReturns, + g.KeywordOptions().SQL("RETURNS"), + ). + OptionalQueryStructField( + "NullOrNot", + functionNullOrNot, + g.KeywordOptions(), + ). + SQL("LANGUAGE JAVA"). + OptionalQueryStructField( + "StrictOrNot", + functionStrictOrNot, + g.KeywordOptions(), + ). + OptionalQueryStructField( + "VolatileOrNot", + functionVolatileOrNot, + g.KeywordOptions(), + ). + OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). + ListQueryStructField( + "Imports", + functionImports, + g.ParameterOptions().Parentheses().SQL("IMPORTS"), + ). + ListQueryStructField( + "Packages", + functionPackages, + g.ParameterOptions().Parentheses().SQL("PACKAGES"), + ). + OptionalTextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()). + ListQueryStructField( + "Secrets", + functionSecret, + g.ParameterOptions().Parentheses().SQL("SECRETS"), + ). + OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). + OptionalQueryStructField( + "FunctionDefinition", + functionDefinition, + g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), + ). + WithValidation(g.ValidIdentifier, "name"), +).CustomOperation( + "CreateFunctionForJavascript", + "https://docs.snowflake.com/en/sql-reference/sql/create-function", + g.NewQueryStruct("CreateFunctionForJavascript"). + Create(). + OrReplace(). + OptionalSQL("TEMPORARY"). + OptionalSQL("SECURE"). + SQL("FUNCTION"). + IfNotExists(). + Name(). + ListQueryStructField( + "Arguments", + functionArgument, + g.ParameterOptions().Parentheses().NoEquals()). + OptionalSQL("COPY GRANTS"). + OptionalQueryStructField( + "Returns", + functionReturns, + g.KeywordOptions().SQL("RETURNS"), + ). + OptionalQueryStructField( + "NullOrNot", + functionNullOrNot, + g.KeywordOptions(), + ). + SQL("LANGUAGE JAVASCRIPT"). + OptionalQueryStructField( + "StrictOrNot", + functionStrictOrNot, + g.KeywordOptions(), + ). + OptionalQueryStructField( + "VolatileOrNot", + functionVolatileOrNot, + g.KeywordOptions(), + ). + OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). + OptionalQueryStructField( + "FunctionDefinition", + functionDefinition, + g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), + ). + WithValidation(g.ValidIdentifier, "name"), +).CustomOperation( + "CreateFunctionForPython", + "https://docs.snowflake.com/en/sql-reference/sql/create-function", + g.NewQueryStruct("CreateFunctionForPython"). + Create(). + OrReplace(). + OptionalSQL("TEMPORARY"). + OptionalSQL("SECURE"). + SQL("FUNCTION"). + IfNotExists(). + Name(). + ListQueryStructField( + "Arguments", + functionArgument, + g.ParameterOptions().Parentheses().NoEquals()). + OptionalSQL("COPY GRANTS"). + OptionalQueryStructField( + "Returns", + functionReturns, + g.KeywordOptions().SQL("RETURNS"), + ). + OptionalQueryStructField( + "NullOrNot", + functionNullOrNot, + g.KeywordOptions(), + ). + SQL("LANGUAGE PYTHON"). + OptionalQueryStructField( + "StrictOrNot", + functionStrictOrNot, + g.KeywordOptions(), + ). + OptionalQueryStructField( + "VolatileOrNot", + functionVolatileOrNot, + g.KeywordOptions(), + ). + OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). + ListQueryStructField( + "Imports", + functionImports, + g.ParameterOptions().Parentheses().SQL("IMPORTS"), + ). + ListQueryStructField( + "Packages", + functionPackages, + g.ParameterOptions().Parentheses().SQL("PACKAGES"), + ). + OptionalTextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()). + ListQueryStructField( + "Secrets", + functionSecret, + g.ParameterOptions().Parentheses().SQL("SECRETS"), + ). + OptionalQueryStructField( + "FunctionDefinition", + functionDefinition, + g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), + ). + WithValidation(g.ValidIdentifier, "name"), +).CustomOperation( + "CreateFunctionForScala", + "https://docs.snowflake.com/en/sql-reference/sql/create-function", + g.NewQueryStruct("CreateFunctionForScala"). + Create(). + OrReplace(). + OptionalSQL("TEMPORARY"). + OptionalSQL("SECURE"). + SQL("FUNCTION"). + IfNotExists(). + Name(). + ListQueryStructField( + "Arguments", + functionArgument, + g.ParameterOptions().Parentheses().NoEquals()). + OptionalSQL("COPY GRANTS"). + OptionalQueryStructField( + "Returns", + functionReturns, + g.KeywordOptions().SQL("RETURNS"), + ). + OptionalQueryStructField( + "NullOrNot", + functionNullOrNot, + g.KeywordOptions(), + ). + SQL("LANGUAGE SCALA"). + OptionalQueryStructField( + "StrictOrNot", + functionStrictOrNot, + g.KeywordOptions(), + ). + OptionalQueryStructField( + "VolatileOrNot", + functionVolatileOrNot, + g.KeywordOptions(), + ). + OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). + ListQueryStructField( + "Imports", + functionImports, + g.ParameterOptions().Parentheses().SQL("IMPORTS"), + ). + ListQueryStructField( + "Packages", + functionPackages, + g.ParameterOptions().Parentheses().SQL("PACKAGES"), + ). + OptionalTextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). + OptionalQueryStructField( + "FunctionDefinition", + functionDefinition, + g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), + ). + WithValidation(g.ValidIdentifier, "name"), +).CustomOperation( + "CreateFunctionForSQL", + "https://docs.snowflake.com/en/sql-reference/sql/create-function", + g.NewQueryStruct("CreateFunctionForSQL"). + Create(). + OrReplace(). + OptionalSQL("TEMPORARY"). + OptionalSQL("SECURE"). + SQL("FUNCTION"). + IfNotExists(). + Name(). + ListQueryStructField( + "Arguments", + functionArgument, + g.ParameterOptions().Parentheses().NoEquals()). + OptionalSQL("COPY GRANTS"). + OptionalQueryStructField( + "Returns", + functionReturns, + g.KeywordOptions().SQL("RETURNS"), + ). + OptionalQueryStructField( + "NullOrNot", + functionNullOrNot, + g.KeywordOptions(), + ). + OptionalQueryStructField( + "VolatileOrNot", + functionVolatileOrNot, + g.KeywordOptions(), + ). + OptionalSQL("MEMOIZABLE"). + OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). + OptionalQueryStructField( + "FunctionDefinition", + functionDefinition, + g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), + ). + WithValidation(g.ValidIdentifier, "name"), +).AlterOperation( + "https://docs.snowflake.com/en/sql-reference/sql/alter-function", + g.NewQueryStruct("AlterFunction"). + Alter(). + SQL("FUNCTION"). + IfExists(). + Name(). + ListQueryStructField( + "ArgumentTypes", + functionArgumentType, + g.ParameterOptions().Parentheses().NoEquals()). + OptionalQueryStructField( + "Set", + functionSet, + g.KeywordOptions().SQL("SET"), + ). + OptionalQueryStructField( + "Unset", + functionUnset, + g.KeywordOptions().SQL("UNSET"), + ). + Identifier("RenameTo", g.KindOfTPointer[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")). + SetTags().UnsetTags(). + WithValidation(g.ValidIdentifier, "name"), +).DropOperation( + "https://docs.snowflake.com/en/sql-reference/sql/drop-function", + g.NewQueryStruct("DropFunction"). + Drop(). + SQL("FUNCTION"). + IfExists(). + Name(). + ListQueryStructField( + "ArgumentTypes", + functionArgumentType, + g.ParameterOptions().Parentheses().NoEquals()). + WithValidation(g.ValidIdentifier, "name"), +).ShowOperation( + "https://docs.snowflake.com/en/sql-reference/sql/show-functions", + g.DbStruct("functionRow"). + Field("created_on", "string"). + Field("name", "string"). + Field("schema_name", "string"). + Field("min_num_arguments", "int"). + Field("max_num_arguments", "int"). + Field("arguments", "string"). + Field("is_table_function", "string"). + Field("is_secure", "string"). + Field("is_external_function", "string"). + Field("language", "string"). + Field("is_memoizable", "string"), + g.PlainStruct("Function"). + Field("CreatedOn", "string"). + Field("Name", "string"). + Field("SchemaName", "string"). + Field("MinNumArguments", "int"). + Field("MaxNumArguments", "int"). + Field("Arguments", "string"). + Field("IsTableFunction", "bool"). + Field("IsSecure", "bool"). + Field("IsExternalFunction", "bool"). + Field("Language", "string"). + Field("IsMemoizable", "bool"), + g.NewQueryStruct("ShowFunctions"). + Show(). + SQL("USER FUNCTIONS"). + OptionalLike(). + OptionalIn(), +).DescribeOperation( + g.DescriptionMappingKindSlice, + "https://docs.snowflake.com/en/sql-reference/sql/describe-function", + g.DbStruct("functionDetailRow"). + Field("property", "string"). + Field("value", "string"), + g.PlainStruct("FunctionDetail"). + Field("Property", "string"). + Field("Value", "string"), + g.NewQueryStruct("DescribeFunction"). + Describe(). + SQL("FUNCTION"). + Name(). + ListQueryStructField( + "ArgumentTypes", + functionArgumentType, + g.ParameterOptions().Parentheses().NoEquals()). + WithValidation(g.ValidIdentifier, "name"), +) diff --git a/pkg/sdk/functions_dto_builders_gen.go b/pkg/sdk/functions_dto_builders_gen.go new file mode 100644 index 0000000000..e3652d2ddd --- /dev/null +++ b/pkg/sdk/functions_dto_builders_gen.go @@ -0,0 +1,663 @@ +// Code generated by dto builder generator; DO NOT EDIT. + +package sdk + +import () + +func NewCreateFunctionForJavaFunctionRequest( + name SchemaObjectIdentifier, +) *CreateFunctionForJavaFunctionRequest { + s := CreateFunctionForJavaFunctionRequest{} + s.name = name + return &s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForJavaFunctionRequest { + s.OrReplace = OrReplace + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForJavaFunctionRequest { + s.Temporary = Temporary + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForJavaFunctionRequest { + s.Secure = Secure + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForJavaFunctionRequest { + s.IfNotExists = IfNotExists + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForJavaFunctionRequest { + s.Arguments = Arguments + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForJavaFunctionRequest { + s.CopyGrants = CopyGrants + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForJavaFunctionRequest { + s.Returns = Returns + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForJavaFunctionRequest { + s.ReturnNullValues = &ReturnNullValues + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForJavaFunctionRequest { + s.NullInputBehavior = &NullInputBehavior + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForJavaFunctionRequest { + s.ReturnResultsBehavior = &ReturnResultsBehavior + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateFunctionForJavaFunctionRequest { + s.RuntimeVersion = RuntimeVersion + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithComment(Comment *string) *CreateFunctionForJavaFunctionRequest { + s.Comment = Comment + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateFunctionForJavaFunctionRequest { + s.Imports = Imports + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateFunctionForJavaFunctionRequest { + s.Packages = Packages + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithHandler(Handler *string) *CreateFunctionForJavaFunctionRequest { + s.Handler = Handler + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateFunctionForJavaFunctionRequest { + s.ExternalAccessIntegrations = ExternalAccessIntegrations + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithSecrets(Secrets []FunctionSecretRequest) *CreateFunctionForJavaFunctionRequest { + s.Secrets = Secrets + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithTargetPath(TargetPath *string) *CreateFunctionForJavaFunctionRequest { + s.TargetPath = TargetPath + return s +} + +func (s *CreateFunctionForJavaFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForJavaFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewFunctionArgumentRequest() *FunctionArgumentRequest { + return &FunctionArgumentRequest{} +} + +func (s *FunctionArgumentRequest) WithArgName(ArgName string) *FunctionArgumentRequest { + s.ArgName = ArgName + return s +} + +func (s *FunctionArgumentRequest) WithArgDataType(ArgDataType DataType) *FunctionArgumentRequest { + s.ArgDataType = ArgDataType + return s +} + +func NewFunctionReturnsRequest() *FunctionReturnsRequest { + return &FunctionReturnsRequest{} +} + +func (s *FunctionReturnsRequest) WithResultDataType(ResultDataType DataType) *FunctionReturnsRequest { + s.ResultDataType = &ResultDataType + return s +} + +func (s *FunctionReturnsRequest) WithTable(Table *FunctionReturnsTableRequest) *FunctionReturnsRequest { + s.Table = Table + return s +} + +func NewFunctionReturnsTableRequest() *FunctionReturnsTableRequest { + return &FunctionReturnsTableRequest{} +} + +func (s *FunctionReturnsTableRequest) WithColumns(Columns []FunctionColumnRequest) *FunctionReturnsTableRequest { + s.Columns = Columns + return s +} + +func NewFunctionColumnRequest() *FunctionColumnRequest { + return &FunctionColumnRequest{} +} + +func (s *FunctionColumnRequest) WithColumnName(ColumnName string) *FunctionColumnRequest { + s.ColumnName = ColumnName + return s +} + +func (s *FunctionColumnRequest) WithColumnDataType(ColumnDataType DataType) *FunctionColumnRequest { + s.ColumnDataType = ColumnDataType + return s +} + +func NewFunctionImportsRequest() *FunctionImportsRequest { + return &FunctionImportsRequest{} +} + +func (s *FunctionImportsRequest) WithImport(Import string) *FunctionImportsRequest { + s.Import = Import + return s +} + +func NewFunctionPackagesRequest() *FunctionPackagesRequest { + return &FunctionPackagesRequest{} +} + +func (s *FunctionPackagesRequest) WithPackage(Package string) *FunctionPackagesRequest { + s.Package = Package + return s +} + +func NewFunctionSecretRequest() *FunctionSecretRequest { + return &FunctionSecretRequest{} +} + +func (s *FunctionSecretRequest) WithSecretVariableName(SecretVariableName string) *FunctionSecretRequest { + s.SecretVariableName = SecretVariableName + return s +} + +func (s *FunctionSecretRequest) WithSecretName(SecretName string) *FunctionSecretRequest { + s.SecretName = SecretName + return s +} + +func NewCreateFunctionForJavascriptFunctionRequest( + name SchemaObjectIdentifier, +) *CreateFunctionForJavascriptFunctionRequest { + s := CreateFunctionForJavascriptFunctionRequest{} + s.name = name + return &s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForJavascriptFunctionRequest { + s.OrReplace = OrReplace + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForJavascriptFunctionRequest { + s.Temporary = Temporary + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForJavascriptFunctionRequest { + s.Secure = Secure + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForJavascriptFunctionRequest { + s.IfNotExists = IfNotExists + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForJavascriptFunctionRequest { + s.Arguments = Arguments + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForJavascriptFunctionRequest { + s.CopyGrants = CopyGrants + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForJavascriptFunctionRequest { + s.Returns = Returns + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForJavascriptFunctionRequest { + s.ReturnNullValues = &ReturnNullValues + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForJavascriptFunctionRequest { + s.NullInputBehavior = &NullInputBehavior + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForJavascriptFunctionRequest { + s.ReturnResultsBehavior = &ReturnResultsBehavior + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithComment(Comment *string) *CreateFunctionForJavascriptFunctionRequest { + s.Comment = Comment + return s +} + +func (s *CreateFunctionForJavascriptFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForJavascriptFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewCreateFunctionForPythonFunctionRequest( + name SchemaObjectIdentifier, +) *CreateFunctionForPythonFunctionRequest { + s := CreateFunctionForPythonFunctionRequest{} + s.name = name + return &s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForPythonFunctionRequest { + s.OrReplace = OrReplace + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForPythonFunctionRequest { + s.Temporary = Temporary + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForPythonFunctionRequest { + s.Secure = Secure + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForPythonFunctionRequest { + s.IfNotExists = IfNotExists + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForPythonFunctionRequest { + s.Arguments = Arguments + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForPythonFunctionRequest { + s.CopyGrants = CopyGrants + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForPythonFunctionRequest { + s.Returns = Returns + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForPythonFunctionRequest { + s.ReturnNullValues = &ReturnNullValues + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForPythonFunctionRequest { + s.NullInputBehavior = &NullInputBehavior + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForPythonFunctionRequest { + s.ReturnResultsBehavior = &ReturnResultsBehavior + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateFunctionForPythonFunctionRequest { + s.RuntimeVersion = RuntimeVersion + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithComment(Comment *string) *CreateFunctionForPythonFunctionRequest { + s.Comment = Comment + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateFunctionForPythonFunctionRequest { + s.Imports = Imports + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateFunctionForPythonFunctionRequest { + s.Packages = Packages + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithHandler(Handler *string) *CreateFunctionForPythonFunctionRequest { + s.Handler = Handler + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateFunctionForPythonFunctionRequest { + s.ExternalAccessIntegrations = ExternalAccessIntegrations + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithSecrets(Secrets []FunctionSecretRequest) *CreateFunctionForPythonFunctionRequest { + s.Secrets = Secrets + return s +} + +func (s *CreateFunctionForPythonFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForPythonFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewCreateFunctionForScalaFunctionRequest( + name SchemaObjectIdentifier, +) *CreateFunctionForScalaFunctionRequest { + s := CreateFunctionForScalaFunctionRequest{} + s.name = name + return &s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForScalaFunctionRequest { + s.OrReplace = OrReplace + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForScalaFunctionRequest { + s.Temporary = Temporary + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForScalaFunctionRequest { + s.Secure = Secure + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForScalaFunctionRequest { + s.IfNotExists = IfNotExists + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForScalaFunctionRequest { + s.Arguments = Arguments + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForScalaFunctionRequest { + s.CopyGrants = CopyGrants + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForScalaFunctionRequest { + s.Returns = Returns + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForScalaFunctionRequest { + s.ReturnNullValues = &ReturnNullValues + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForScalaFunctionRequest { + s.NullInputBehavior = &NullInputBehavior + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForScalaFunctionRequest { + s.ReturnResultsBehavior = &ReturnResultsBehavior + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateFunctionForScalaFunctionRequest { + s.RuntimeVersion = RuntimeVersion + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithComment(Comment *string) *CreateFunctionForScalaFunctionRequest { + s.Comment = Comment + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateFunctionForScalaFunctionRequest { + s.Imports = Imports + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateFunctionForScalaFunctionRequest { + s.Packages = Packages + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithHandler(Handler *string) *CreateFunctionForScalaFunctionRequest { + s.Handler = Handler + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithTargetPath(TargetPath *string) *CreateFunctionForScalaFunctionRequest { + s.TargetPath = TargetPath + return s +} + +func (s *CreateFunctionForScalaFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForScalaFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewCreateFunctionForSQLFunctionRequest( + name SchemaObjectIdentifier, +) *CreateFunctionForSQLFunctionRequest { + s := CreateFunctionForSQLFunctionRequest{} + s.name = name + return &s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForSQLFunctionRequest { + s.OrReplace = OrReplace + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForSQLFunctionRequest { + s.Temporary = Temporary + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForSQLFunctionRequest { + s.Secure = Secure + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForSQLFunctionRequest { + s.IfNotExists = IfNotExists + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForSQLFunctionRequest { + s.Arguments = Arguments + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForSQLFunctionRequest { + s.CopyGrants = CopyGrants + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForSQLFunctionRequest { + s.Returns = Returns + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForSQLFunctionRequest { + s.ReturnNullValues = &ReturnNullValues + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForSQLFunctionRequest { + s.ReturnResultsBehavior = &ReturnResultsBehavior + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithMemoizable(Memoizable *bool) *CreateFunctionForSQLFunctionRequest { + s.Memoizable = Memoizable + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithComment(Comment *string) *CreateFunctionForSQLFunctionRequest { + s.Comment = Comment + return s +} + +func (s *CreateFunctionForSQLFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForSQLFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewAlterFunctionRequest( + name SchemaObjectIdentifier, +) *AlterFunctionRequest { + s := AlterFunctionRequest{} + s.name = name + return &s +} + +func (s *AlterFunctionRequest) WithIfExists(IfExists *bool) *AlterFunctionRequest { + s.IfExists = IfExists + return s +} + +func (s *AlterFunctionRequest) WithArgumentTypes(ArgumentTypes []FunctionArgumentTypeRequest) *AlterFunctionRequest { + s.ArgumentTypes = ArgumentTypes + return s +} + +func (s *AlterFunctionRequest) WithSet(Set *FunctionSetRequest) *AlterFunctionRequest { + s.Set = Set + return s +} + +func (s *AlterFunctionRequest) WithUnset(Unset *FunctionUnsetRequest) *AlterFunctionRequest { + s.Unset = Unset + return s +} + +func (s *AlterFunctionRequest) WithRenameTo(RenameTo *SchemaObjectIdentifier) *AlterFunctionRequest { + s.RenameTo = RenameTo + return s +} + +func (s *AlterFunctionRequest) WithSetTags(SetTags []TagAssociation) *AlterFunctionRequest { + s.SetTags = SetTags + return s +} + +func (s *AlterFunctionRequest) WithUnsetTags(UnsetTags []ObjectIdentifier) *AlterFunctionRequest { + s.UnsetTags = UnsetTags + return s +} + +func NewFunctionArgumentTypeRequest() *FunctionArgumentTypeRequest { + return &FunctionArgumentTypeRequest{} +} + +func (s *FunctionArgumentTypeRequest) WithArgDataType(ArgDataType DataType) *FunctionArgumentTypeRequest { + s.ArgDataType = ArgDataType + return s +} + +func NewFunctionSetRequest() *FunctionSetRequest { + return &FunctionSetRequest{} +} + +func (s *FunctionSetRequest) WithLogLevel(LogLevel *string) *FunctionSetRequest { + s.LogLevel = LogLevel + return s +} + +func (s *FunctionSetRequest) WithTraceLevel(TraceLevel *string) *FunctionSetRequest { + s.TraceLevel = TraceLevel + return s +} + +func (s *FunctionSetRequest) WithComment(Comment *string) *FunctionSetRequest { + s.Comment = Comment + return s +} + +func (s *FunctionSetRequest) WithSecure(Secure *bool) *FunctionSetRequest { + s.Secure = Secure + return s +} + +func NewFunctionUnsetRequest() *FunctionUnsetRequest { + return &FunctionUnsetRequest{} +} + +func (s *FunctionUnsetRequest) WithSecure(Secure *bool) *FunctionUnsetRequest { + s.Secure = Secure + return s +} + +func (s *FunctionUnsetRequest) WithComment(Comment *bool) *FunctionUnsetRequest { + s.Comment = Comment + return s +} + +func (s *FunctionUnsetRequest) WithLogLevel(LogLevel *bool) *FunctionUnsetRequest { + s.LogLevel = LogLevel + return s +} + +func (s *FunctionUnsetRequest) WithTraceLevel(TraceLevel *bool) *FunctionUnsetRequest { + s.TraceLevel = TraceLevel + return s +} + +func NewDropFunctionRequest( + name SchemaObjectIdentifier, +) *DropFunctionRequest { + s := DropFunctionRequest{} + s.name = name + return &s +} + +func (s *DropFunctionRequest) WithIfExists(IfExists *bool) *DropFunctionRequest { + s.IfExists = IfExists + return s +} + +func (s *DropFunctionRequest) WithArgumentTypes(ArgumentTypes []FunctionArgumentTypeRequest) *DropFunctionRequest { + s.ArgumentTypes = ArgumentTypes + return s +} + +func NewShowFunctionRequest() *ShowFunctionRequest { + return &ShowFunctionRequest{} +} + +func (s *ShowFunctionRequest) WithLike(pattern string) *ShowFunctionRequest { + s.Like = &Like{Pattern: String(pattern)} + return s +} + +func (s *ShowFunctionRequest) WithIn(In *In) *ShowFunctionRequest { + s.In = In + return s +} + +func NewDescribeFunctionRequest( + name SchemaObjectIdentifier, +) *DescribeFunctionRequest { + s := DescribeFunctionRequest{} + s.name = name + return &s +} + +func (s *DescribeFunctionRequest) WithArgumentTypes(ArgumentTypes []FunctionArgumentTypeRequest) *DescribeFunctionRequest { + s.ArgumentTypes = ArgumentTypes + return s +} diff --git a/pkg/sdk/functions_dto_gen.go b/pkg/sdk/functions_dto_gen.go new file mode 100644 index 0000000000..c9a8f28618 --- /dev/null +++ b/pkg/sdk/functions_dto_gen.go @@ -0,0 +1,190 @@ +package sdk + +//go:generate go run ./dto-builder-generator/main.go + +var ( + _ optionsProvider[CreateFunctionForJavaFunctionOptions] = new(CreateFunctionForJavaFunctionRequest) + _ optionsProvider[CreateFunctionForJavascriptFunctionOptions] = new(CreateFunctionForJavascriptFunctionRequest) + _ optionsProvider[CreateFunctionForPythonFunctionOptions] = new(CreateFunctionForPythonFunctionRequest) + _ optionsProvider[CreateFunctionForScalaFunctionOptions] = new(CreateFunctionForScalaFunctionRequest) + _ optionsProvider[CreateFunctionForSQLFunctionOptions] = new(CreateFunctionForSQLFunctionRequest) + _ optionsProvider[AlterFunctionOptions] = new(AlterFunctionRequest) + _ optionsProvider[DropFunctionOptions] = new(DropFunctionRequest) + _ optionsProvider[ShowFunctionOptions] = new(ShowFunctionRequest) + _ optionsProvider[DescribeFunctionOptions] = new(DescribeFunctionRequest) +) + +type CreateFunctionForJavaFunctionRequest struct { + OrReplace *bool + Temporary *bool + Secure *bool + IfNotExists *bool + name SchemaObjectIdentifier // required + Arguments []FunctionArgumentRequest + CopyGrants *bool + Returns *FunctionReturnsRequest + ReturnNullValues *FunctionReturnNullValues + NullInputBehavior *FunctionNullInputBehavior + ReturnResultsBehavior *FunctionReturnResultsBehavior + RuntimeVersion *string + Comment *string + Imports []FunctionImportsRequest + Packages []FunctionPackagesRequest + Handler *string + ExternalAccessIntegrations []AccountObjectIdentifier + Secrets []FunctionSecretRequest + TargetPath *string + FunctionDefinition *string +} + +type FunctionArgumentRequest struct { + ArgName string + ArgDataType DataType +} + +type FunctionReturnsRequest struct { + ResultDataType *DataType + Table *FunctionReturnsTableRequest +} + +type FunctionReturnsTableRequest struct { + Columns []FunctionColumnRequest +} + +type FunctionColumnRequest struct { + ColumnName string + ColumnDataType DataType +} + +type FunctionImportsRequest struct { + Import string +} + +type FunctionPackagesRequest struct { + Package string +} + +type FunctionSecretRequest struct { + SecretVariableName string + SecretName string +} + +type CreateFunctionForJavascriptFunctionRequest struct { + OrReplace *bool + Temporary *bool + Secure *bool + IfNotExists *bool + name SchemaObjectIdentifier // required + Arguments []FunctionArgumentRequest + CopyGrants *bool + Returns *FunctionReturnsRequest + ReturnNullValues *FunctionReturnNullValues + NullInputBehavior *FunctionNullInputBehavior + ReturnResultsBehavior *FunctionReturnResultsBehavior + Comment *string + FunctionDefinition *string +} + +type CreateFunctionForPythonFunctionRequest struct { + OrReplace *bool + Temporary *bool + Secure *bool + IfNotExists *bool + name SchemaObjectIdentifier // required + Arguments []FunctionArgumentRequest + CopyGrants *bool + Returns *FunctionReturnsRequest + ReturnNullValues *FunctionReturnNullValues + NullInputBehavior *FunctionNullInputBehavior + ReturnResultsBehavior *FunctionReturnResultsBehavior + RuntimeVersion *string + Comment *string + Imports []FunctionImportsRequest + Packages []FunctionPackagesRequest + Handler *string + ExternalAccessIntegrations []AccountObjectIdentifier + Secrets []FunctionSecretRequest + FunctionDefinition *string +} + +type CreateFunctionForScalaFunctionRequest struct { + OrReplace *bool + Temporary *bool + Secure *bool + IfNotExists *bool + name SchemaObjectIdentifier // required + Arguments []FunctionArgumentRequest + CopyGrants *bool + Returns *FunctionReturnsRequest + ReturnNullValues *FunctionReturnNullValues + NullInputBehavior *FunctionNullInputBehavior + ReturnResultsBehavior *FunctionReturnResultsBehavior + RuntimeVersion *string + Comment *string + Imports []FunctionImportsRequest + Packages []FunctionPackagesRequest + Handler *string + TargetPath *string + FunctionDefinition *string +} + +type CreateFunctionForSQLFunctionRequest struct { + OrReplace *bool + Temporary *bool + Secure *bool + IfNotExists *bool + name SchemaObjectIdentifier // required + Arguments []FunctionArgumentRequest + CopyGrants *bool + Returns *FunctionReturnsRequest + ReturnNullValues *FunctionReturnNullValues + ReturnResultsBehavior *FunctionReturnResultsBehavior + Memoizable *bool + Comment *string + FunctionDefinition *string +} + +type AlterFunctionRequest struct { + IfExists *bool + name SchemaObjectIdentifier // required + ArgumentTypes []FunctionArgumentTypeRequest + Set *FunctionSetRequest + Unset *FunctionUnsetRequest + RenameTo *SchemaObjectIdentifier + SetTags []TagAssociation + UnsetTags []ObjectIdentifier +} + +type FunctionArgumentTypeRequest struct { + ArgDataType DataType +} + +type FunctionSetRequest struct { + LogLevel *string + TraceLevel *string + Comment *string + Secure *bool +} + +type FunctionUnsetRequest struct { + Secure *bool + Comment *bool + LogLevel *bool + TraceLevel *bool +} + +type DropFunctionRequest struct { + IfExists *bool + name SchemaObjectIdentifier // required + ArgumentTypes []FunctionArgumentTypeRequest +} + +type ShowFunctionRequest struct { + Like *Like + In *In +} + +type DescribeFunctionRequest struct { + name SchemaObjectIdentifier // required + ArgumentTypes []FunctionArgumentTypeRequest +} diff --git a/pkg/sdk/functions_gen.go b/pkg/sdk/functions_gen.go new file mode 100644 index 0000000000..c648d8640a --- /dev/null +++ b/pkg/sdk/functions_gen.go @@ -0,0 +1,282 @@ +package sdk + +import "context" + +type Functions interface { + CreateFunctionForJava(ctx context.Context, request *CreateFunctionForJavaFunctionRequest) error + CreateFunctionForJavascript(ctx context.Context, request *CreateFunctionForJavascriptFunctionRequest) error + CreateFunctionForPython(ctx context.Context, request *CreateFunctionForPythonFunctionRequest) error + CreateFunctionForScala(ctx context.Context, request *CreateFunctionForScalaFunctionRequest) error + CreateFunctionForSQL(ctx context.Context, request *CreateFunctionForSQLFunctionRequest) error + Alter(ctx context.Context, request *AlterFunctionRequest) error + Drop(ctx context.Context, request *DropFunctionRequest) error + Show(ctx context.Context, request *ShowFunctionRequest) ([]Function, error) + ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Function, error) + Describe(ctx context.Context, request *DescribeFunctionRequest) ([]FunctionDetail, error) +} + +type FunctionNullInputBehavior string + +const ( + FunctionNullInputBehaviorCalledOnNullInput FunctionNullInputBehavior = "CALLED ON NULL INPUT" + FunctionNullInputBehaviorReturnNullInput FunctionNullInputBehavior = "RETURN NULL ON NULL INPUT" + FunctionNullInputBehaviorStrict FunctionNullInputBehavior = "STRICT" +) + +type FunctionReturnResultsBehavior string + +const ( + FunctionReturnResultsBehaviorVolatile FunctionReturnResultsBehavior = "VOLATILE" + FunctionReturnResultsBehaviorImmutable FunctionReturnResultsBehavior = "IMMUTABLE" +) + +type FunctionReturnNullValues string + +const ( + FunctionReturnNullValuesNull FunctionReturnNullValues = "NULL" + FunctionReturnNullValuesNotNull FunctionReturnNullValues = "NOT NULL" +) + +// CreateFunctionForJavaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. +type CreateFunctionForJavaFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` + languageJava bool `ddl:"static" sql:"LANGUAGE JAVA"` + NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` + RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Handler *string `ddl:"parameter,single_quotes" sql:"HANDLER"` + ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` + Secrets []FunctionSecret `ddl:"parameter,parentheses" sql:"SECRETS"` + TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +} + +type FunctionArgument struct { + ArgName string `ddl:"keyword,no_quotes"` + ArgDataType DataType `ddl:"keyword,no_quotes"` +} + +type FunctionReturns struct { + ResultDataType *DataType `ddl:"keyword"` + Table *FunctionReturnsTable `ddl:"keyword" sql:"TABLE"` +} + +type FunctionReturnsTable struct { + Columns []FunctionColumn `ddl:"parameter,parentheses,no_equals"` +} + +type FunctionColumn struct { + ColumnName string `ddl:"keyword,no_quotes"` + ColumnDataType DataType `ddl:"keyword,no_quotes"` +} + +type FunctionImports struct { + Import string `ddl:"keyword,single_quotes"` +} + +type FunctionPackages struct { + Package string `ddl:"keyword,single_quotes"` +} + +type FunctionSecret struct { + SecretVariableName string `ddl:"keyword,single_quotes"` + SecretName string `ddl:"parameter,no_quotes"` +} + +// CreateFunctionForJavascriptFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. +type CreateFunctionForJavascriptFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` + languageJavascript bool `ddl:"static" sql:"LANGUAGE JAVASCRIPT"` + NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +} + +// CreateFunctionForPythonFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. +type CreateFunctionForPythonFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` + languagePython bool `ddl:"static" sql:"LANGUAGE PYTHON"` + NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` + RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Handler *string `ddl:"parameter,single_quotes" sql:"HANDLER"` + ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` + Secrets []FunctionSecret `ddl:"parameter,parentheses" sql:"SECRETS"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +} + +// CreateFunctionForScalaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. +type CreateFunctionForScalaFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` + languageScala bool `ddl:"static" sql:"LANGUAGE SCALA"` + NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` + RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Handler *string `ddl:"parameter,single_quotes" sql:"HANDLER"` + TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +} + +// CreateFunctionForSQLFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. +type CreateFunctionForSQLFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` + ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` + Memoizable *bool `ddl:"keyword" sql:"MEMOIZABLE"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +} + +// AlterFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-function. +type AlterFunctionOptions struct { + alter bool `ddl:"static" sql:"ALTER"` + function bool `ddl:"static" sql:"FUNCTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` + Set *FunctionSet `ddl:"keyword" sql:"SET"` + Unset *FunctionUnset `ddl:"keyword" sql:"UNSET"` + RenameTo *SchemaObjectIdentifier `ddl:"identifier" sql:"RENAME TO"` + SetTags []TagAssociation `ddl:"keyword" sql:"SET TAG"` + UnsetTags []ObjectIdentifier `ddl:"keyword" sql:"UNSET TAG"` +} + +type FunctionArgumentType struct { + ArgDataType DataType `ddl:"keyword,no_quotes"` +} + +type FunctionSet struct { + LogLevel *string `ddl:"parameter,single_quotes" sql:"LOG_LEVEL"` + TraceLevel *string `ddl:"parameter,single_quotes" sql:"TRACE_LEVEL"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Secure *bool `ddl:"keyword" sql:"SECURE"` +} + +type FunctionUnset struct { + Secure *bool `ddl:"keyword" sql:"SECURE"` + Comment *bool `ddl:"keyword" sql:"COMMENT"` + LogLevel *bool `ddl:"keyword" sql:"LOG_LEVEL"` + TraceLevel *bool `ddl:"keyword" sql:"TRACE_LEVEL"` +} + +// DropFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/drop-function. +type DropFunctionOptions struct { + drop bool `ddl:"static" sql:"DROP"` + function bool `ddl:"static" sql:"FUNCTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` +} + +// ShowFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-functions. +type ShowFunctionOptions struct { + show bool `ddl:"static" sql:"SHOW"` + userFunctions bool `ddl:"static" sql:"USER FUNCTIONS"` + Like *Like `ddl:"keyword" sql:"LIKE"` + In *In `ddl:"keyword" sql:"IN"` +} + +type functionRow struct { + CreatedOn string `db:"created_on"` + Name string `db:"name"` + SchemaName string `db:"schema_name"` + MinNumArguments int `db:"min_num_arguments"` + MaxNumArguments int `db:"max_num_arguments"` + Arguments string `db:"arguments"` + IsTableFunction string `db:"is_table_function"` + IsSecure string `db:"is_secure"` + IsExternalFunction string `db:"is_external_function"` + Language string `db:"language"` + IsMemoizable string `db:"is_memoizable"` +} + +type Function struct { + CreatedOn string + Name string + SchemaName string + MinNumArguments int + MaxNumArguments int + Arguments string + IsTableFunction bool + IsSecure bool + IsExternalFunction bool + Language string + IsMemoizable bool +} + +// DescribeFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/describe-function. +type DescribeFunctionOptions struct { + describe bool `ddl:"static" sql:"DESCRIBE"` + function bool `ddl:"static" sql:"FUNCTION"` + name SchemaObjectIdentifier `ddl:"identifier"` + ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` +} + +type functionDetailRow struct { + Property string `db:"property"` + Value string `db:"value"` +} + +type FunctionDetail struct { + Property string + Value string +} diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go new file mode 100644 index 0000000000..554cc4fe11 --- /dev/null +++ b/pkg/sdk/functions_gen_test.go @@ -0,0 +1,528 @@ +package sdk + +import ( + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" +) + +func TestFunctions_CreateFunctionForJava(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *CreateFunctionForJavaFunctionOptions { + return &CreateFunctionForJavaFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*CreateFunctionForJavaFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.OrReplace = Bool(true) + opts.Temporary = Bool(true) + opts.Secure = Bool(true) + opts.IfNotExists = Bool(true) + opts.Arguments = []FunctionArgument{ + { + ArgName: "id", + ArgDataType: DataTypeNumber, + }, + { + ArgName: "name", + ArgDataType: DataTypeVARCHAR, + }, + } + opts.CopyGrants = Bool(true) + opts.Returns = &FunctionReturns{ + Table: &FunctionReturnsTable{ + Columns: []FunctionColumn{ + { + ColumnName: "country_code", + ColumnDataType: DataTypeVARCHAR, + }, + { + ColumnName: "country_name", + ColumnDataType: DataTypeVARCHAR, + }, + }, + }, + } + returnNullValues := FunctionReturnNullValuesNotNull + opts.ReturnNullValues = &returnNullValues + nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput + opts.NullInputBehavior = &nullInputBehavior + returnResultsBehavior := FunctionReturnResultsBehaviorImmutable + opts.ReturnResultsBehavior = &returnResultsBehavior + opts.RuntimeVersion = String("2.0") + opts.Comment = String("comment") + opts.Imports = []FunctionImports{ + { + Import: "@~/my_decrement_udf_package_dir/my_decrement_udf_jar.jar", + }, + } + opts.Packages = []FunctionPackages{ + { + Package: "com.snowflake:snowpark:1.2.0", + }, + } + opts.Handler = String("TestFunc.echoVarchar") + opts.ExternalAccessIntegrations = []AccountObjectIdentifier{ + NewAccountObjectIdentifier("ext_integration"), + } + opts.Secrets = []FunctionSecret{ + { + SecretVariableName: "variable1", + SecretName: "name1", + }, + { + SecretVariableName: "variable2", + SecretName: "name2", + }, + } + opts.TargetPath = String("@~/testfunc.jar") + opts.FunctionDefinition = String("return id + name;") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (id NUMBER, name VARCHAR) COPY GRANTS RETURNS TABLE (country_code VARCHAR, country_name VARCHAR) NOT NULL LANGUAGE JAVA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@~/my_decrement_udf_package_dir/my_decrement_udf_jar.jar') PACKAGES = ('com.snowflake:snowpark:1.2.0') HANDLER = 'TestFunc.echoVarchar' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) TARGET_PATH = '@~/testfunc.jar' AS 'return id + name;'`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_CreateFunctionForJavascript(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *CreateFunctionForJavascriptFunctionOptions { + return &CreateFunctionForJavascriptFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*CreateFunctionForJavascriptFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.OrReplace = Bool(true) + opts.Temporary = Bool(true) + opts.Secure = Bool(true) + opts.Arguments = []FunctionArgument{ + { + ArgName: "d", + ArgDataType: DataTypeFloat, + }, + } + opts.CopyGrants = Bool(true) + float := DataTypeFloat + opts.Returns = &FunctionReturns{ + ResultDataType: &float, + } + returnNullValues := FunctionReturnNullValuesNotNull + opts.ReturnNullValues = &returnNullValues + nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput + opts.NullInputBehavior = &nullInputBehavior + returnResultsBehavior := FunctionReturnResultsBehaviorImmutable + opts.ReturnResultsBehavior = &returnResultsBehavior + opts.Comment = String("comment") + opts.FunctionDefinition = String("return 1;") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (d FLOAT) COPY GRANTS RETURNS FLOAT NOT NULL LANGUAGE JAVASCRIPT CALLED ON NULL INPUT IMMUTABLE COMMENT = 'comment' AS 'return 1;'`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_CreateFunctionForPython(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *CreateFunctionForPythonFunctionOptions { + return &CreateFunctionForPythonFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*CreateFunctionForPythonFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.OrReplace = Bool(true) + opts.Temporary = Bool(true) + opts.Secure = Bool(true) + opts.IfNotExists = Bool(true) + opts.Arguments = []FunctionArgument{ + { + ArgName: "i", + ArgDataType: DataTypeNumber, + }, + } + opts.CopyGrants = Bool(true) + varint := DataTypeVariant + opts.Returns = &FunctionReturns{ + ResultDataType: &varint, + } + returnNullValues := FunctionReturnNullValuesNotNull + opts.ReturnNullValues = &returnNullValues + nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput + opts.NullInputBehavior = &nullInputBehavior + returnResultsBehavior := FunctionReturnResultsBehaviorImmutable + opts.ReturnResultsBehavior = &returnResultsBehavior + opts.RuntimeVersion = String("3.8") + opts.Comment = String("comment") + opts.Imports = []FunctionImports{ + { + Import: "numpy", + }, + { + Import: "pandas", + }, + } + opts.Packages = []FunctionPackages{ + { + Package: "numpy", + }, + { + Package: "pandas", + }, + } + opts.Handler = String("udf") + opts.ExternalAccessIntegrations = []AccountObjectIdentifier{ + NewAccountObjectIdentifier("ext_integration"), + } + opts.Secrets = []FunctionSecret{ + { + SecretVariableName: "variable1", + SecretName: "name1", + }, + { + SecretVariableName: "variable2", + SecretName: "name2", + }, + } + opts.FunctionDefinition = String("import numpy as np") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (i NUMBER) COPY GRANTS RETURNS VARIANT NOT NULL LANGUAGE PYTHON CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '3.8' COMMENT = 'comment' IMPORTS = ('numpy', 'pandas') PACKAGES = ('numpy', 'pandas') HANDLER = 'udf' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) AS 'import numpy as np'`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_CreateFunctionForScala(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *CreateFunctionForScalaFunctionOptions { + return &CreateFunctionForScalaFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*CreateFunctionForScalaFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.OrReplace = Bool(true) + opts.Temporary = Bool(true) + opts.Secure = Bool(true) + opts.IfNotExists = Bool(true) + opts.Arguments = []FunctionArgument{ + { + ArgName: "x", + ArgDataType: DataTypeVARCHAR, + }, + } + opts.CopyGrants = Bool(true) + varchar := DataTypeVARCHAR + opts.Returns = &FunctionReturns{ + ResultDataType: &varchar, + } + returnNullValues := FunctionReturnNullValuesNotNull + opts.ReturnNullValues = &returnNullValues + nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput + opts.NullInputBehavior = &nullInputBehavior + returnResultsBehavior := FunctionReturnResultsBehaviorImmutable + opts.ReturnResultsBehavior = &returnResultsBehavior + opts.RuntimeVersion = String("2.0") + opts.Comment = String("comment") + opts.Imports = []FunctionImports{ + { + Import: "@udf_libs/echohandler.jar", + }, + } + opts.Handler = String("Echo.echoVarchar") + opts.FunctionDefinition = String("return x") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (x VARCHAR) COPY GRANTS RETURNS VARCHAR NOT NULL LANGUAGE SCALA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@udf_libs/echohandler.jar') HANDLER = 'Echo.echoVarchar' AS 'return x'`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_CreateFunctionForSQL(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *CreateFunctionForSQLFunctionOptions { + return &CreateFunctionForSQLFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*CreateFunctionForSQLFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + opts.OrReplace = Bool(true) + opts.Temporary = Bool(true) + opts.Secure = Bool(true) + opts.IfNotExists = Bool(true) + opts.CopyGrants = Bool(true) + dt := DataTypeFloat + opts.Returns = &FunctionReturns{ + ResultDataType: &dt, + } + returnNullValues := FunctionReturnNullValuesNotNull + opts.ReturnNullValues = &returnNullValues + returnResultsBehavior := FunctionReturnResultsBehaviorImmutable + opts.ReturnResultsBehavior = &returnResultsBehavior + opts.Memoizable = Bool(true) + opts.Comment = String("comment") + opts.FunctionDefinition = String("3.141592654::FLOAT") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s COPY GRANTS RETURNS FLOAT NOT NULL IMMUTABLE MEMOIZABLE COMMENT = 'comment' AS '3.141592654::FLOAT'`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_Drop(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *DropFunctionOptions { + return &DropFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*DropFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := &DropFunctionOptions{ + name: id, + } + opts.IfExists = Bool(true) + opts.ArgumentTypes = []FunctionArgumentType{ + { + ArgDataType: DataTypeVARCHAR, + }, + { + ArgDataType: DataTypeNumber, + }, + } + assertOptsValidAndSQLEquals(t, opts, `DROP FUNCTION IF EXISTS %s (VARCHAR, NUMBER)`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_Alter(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *AlterFunctionOptions { + return &AlterFunctionOptions{ + name: id, + IfExists: Bool(true), + ArgumentTypes: []FunctionArgumentType{ + { + ArgDataType: DataTypeVARCHAR, + }, + { + ArgDataType: DataTypeNumber, + }, + }, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*AlterFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("alter: rename to", func(t *testing.T) { + opts := defaultOpts() + target := NewSchemaObjectIdentifier(id.DatabaseName(), id.SchemaName(), random.StringN(12)) + opts.RenameTo = &target + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) RENAME TO %s`, id.FullyQualifiedName(), opts.RenameTo.FullyQualifiedName()) + }) + + t.Run("alter: set log level", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &FunctionSet{ + LogLevel: String("DEBUG"), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET LOG_LEVEL = 'DEBUG'`, id.FullyQualifiedName()) + }) + + t.Run("alter: set trace level", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &FunctionSet{ + TraceLevel: String("DEBUG"), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET TRACE_LEVEL = 'DEBUG'`, id.FullyQualifiedName()) + }) + + t.Run("alter: set comment", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &FunctionSet{ + Comment: String("comment"), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET COMMENT = 'comment'`, id.FullyQualifiedName()) + }) + + t.Run("alter: set secure", func(t *testing.T) { + opts := defaultOpts() + opts.Set = &FunctionSet{ + Secure: Bool(true), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET SECURE`, id.FullyQualifiedName()) + }) + + t.Run("alter: unset secure", func(t *testing.T) { + opts := defaultOpts() + opts.Unset = &FunctionUnset{ + Secure: Bool(true), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET SECURE`, id.FullyQualifiedName()) + }) + + t.Run("alter: unset comment", func(t *testing.T) { + opts := defaultOpts() + opts.Unset = &FunctionUnset{ + Comment: Bool(true), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET COMMENT`, id.FullyQualifiedName()) + }) + + t.Run("alter: set tags", func(t *testing.T) { + opts := defaultOpts() + opts.SetTags = []TagAssociation{ + { + Name: NewAccountObjectIdentifier("tag1"), + Value: "value1", + }, + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET TAG "tag1" = 'value1'`, id.FullyQualifiedName()) + }) + + t.Run("alter: unset tags", func(t *testing.T) { + opts := defaultOpts() + opts.UnsetTags = []ObjectIdentifier{ + NewAccountObjectIdentifier("tag1"), + NewAccountObjectIdentifier("tag2"), + } + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET TAG "tag1", "tag2"`, id.FullyQualifiedName()) + }) +} + +func TestFunctions_Show(t *testing.T) { + defaultOpts := func() *ShowFunctionOptions { + return &ShowFunctionOptions{} + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*ShowFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: empty like", func(t *testing.T) { + opts := defaultOpts() + opts.Like = &Like{} + assertOptsInvalidJoinedErrors(t, opts, ErrPatternRequiredForLikeKeyword) + }) + + t.Run("show with empty options", func(t *testing.T) { + opts := defaultOpts() + assertOptsValidAndSQLEquals(t, opts, `SHOW USER FUNCTIONS`) + }) + + t.Run("show with like", func(t *testing.T) { + opts := defaultOpts() + opts.Like = &Like{ + Pattern: String("pattern"), + } + assertOptsValidAndSQLEquals(t, opts, `SHOW USER FUNCTIONS LIKE 'pattern'`) + }) + + t.Run("show with in", func(t *testing.T) { + opts := defaultOpts() + opts.In = &In{ + Account: Bool(true), + } + assertOptsValidAndSQLEquals(t, opts, `SHOW USER FUNCTIONS IN ACCOUNT`) + }) +} + +func TestFunctions_Describe(t *testing.T) { + id := RandomSchemaObjectIdentifier() + + defaultOpts := func() *DescribeFunctionOptions { + return &DescribeFunctionOptions{ + name: id, + } + } + + t.Run("validation: nil options", func(t *testing.T) { + opts := (*DescribeFunctionOptions)(nil) + assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) + }) + + t.Run("validation: incorrect identifier", func(t *testing.T) { + opts := defaultOpts() + opts.name = NewSchemaObjectIdentifier("", "", "") + assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) + }) + + t.Run("all options", func(t *testing.T) { + opts := defaultOpts() + assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s`, id.FullyQualifiedName()) + }) +} diff --git a/pkg/sdk/functions_impl_gen.go b/pkg/sdk/functions_impl_gen.go new file mode 100644 index 0000000000..23a3702b40 --- /dev/null +++ b/pkg/sdk/functions_impl_gen.go @@ -0,0 +1,538 @@ +package sdk + +import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" +) + +var _ Functions = (*functions)(nil) + +type functions struct { + client *Client +} + +func (v *functions) CreateFunctionForJava(ctx context.Context, request *CreateFunctionForJavaFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) CreateFunctionForJavascript(ctx context.Context, request *CreateFunctionForJavascriptFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) CreateFunctionForPython(ctx context.Context, request *CreateFunctionForPythonFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) CreateFunctionForScala(ctx context.Context, request *CreateFunctionForScalaFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) CreateFunctionForSQL(ctx context.Context, request *CreateFunctionForSQLFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) Alter(ctx context.Context, request *AlterFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) Drop(ctx context.Context, request *DropFunctionRequest) error { + opts := request.toOpts() + return validateAndExec(v.client, ctx, opts) +} + +func (v *functions) Show(ctx context.Context, request *ShowFunctionRequest) ([]Function, error) { + opts := request.toOpts() + dbRows, err := validateAndQuery[functionRow](v.client, ctx, opts) + if err != nil { + return nil, err + } + resultList := convertRows[functionRow, Function](dbRows) + return resultList, nil +} + +func (v *functions) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Function, error) { + request := NewShowFunctionRequest().WithLike(id.Name()) + functions, err := v.Show(ctx, request) + if err != nil { + return nil, err + } + return collections.FindOne(functions, func(r Function) bool { return r.Name == id.Name() }) +} + +func (v *functions) Describe(ctx context.Context, request *DescribeFunctionRequest) ([]FunctionDetail, error) { + opts := request.toOpts() + rows, err := validateAndQuery[functionDetailRow](v.client, ctx, opts) + if err != nil { + return nil, err + } + return convertRows[functionDetailRow, FunctionDetail](rows), nil +} + +func (r *CreateFunctionForJavaFunctionRequest) toOpts() *CreateFunctionForJavaFunctionOptions { + opts := &CreateFunctionForJavaFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + IfNotExists: r.IfNotExists, + name: r.name, + + CopyGrants: r.CopyGrants, + + RuntimeVersion: r.RuntimeVersion, + Comment: r.Comment, + + Handler: r.Handler, + ExternalAccessIntegrations: r.ExternalAccessIntegrations, + + TargetPath: r.TargetPath, + } + if r.Arguments != nil { + s := make([]FunctionArgument, len(r.Arguments)) + for i, v := range r.Arguments { + s[i] = FunctionArgument{ + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + } + } + opts.Arguments = s + } + if r.Returns != nil { + opts.Returns = &FunctionReturns{ + ResultDataType: r.Returns.ResultDataType, + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, + } + } + opts.Returns.Table.Columns = s + } + } + } + if r.ReturnNullValues != nil { + opts.ReturnNullValues = r.ReturnNullValues + } + if r.NullInputBehavior != nil { + opts.NullInputBehavior = r.NullInputBehavior + } + if r.ReturnResultsBehavior != nil { + opts.ReturnResultsBehavior = r.ReturnResultsBehavior + } + if r.Imports != nil { + s := make([]FunctionImports, len(r.Imports)) + for i, v := range r.Imports { + s[i] = FunctionImports{ + Import: v.Import, + } + } + opts.Imports = s + } + if r.Packages != nil { + s := make([]FunctionPackages, len(r.Packages)) + for i, v := range r.Packages { + s[i] = FunctionPackages{ + Package: v.Package, + } + } + opts.Packages = s + } + if r.Secrets != nil { + s := make([]FunctionSecret, len(r.Secrets)) + for i, v := range r.Secrets { + s[i] = FunctionSecret{ + SecretVariableName: v.SecretVariableName, + SecretName: v.SecretName, + } + } + opts.Secrets = s + } + if r.FunctionDefinition != nil { + opts.FunctionDefinition = r.FunctionDefinition + } + return opts +} + +func (r *CreateFunctionForJavascriptFunctionRequest) toOpts() *CreateFunctionForJavascriptFunctionOptions { + opts := &CreateFunctionForJavascriptFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + IfNotExists: r.IfNotExists, + name: r.name, + + CopyGrants: r.CopyGrants, + + Comment: r.Comment, + } + if r.Arguments != nil { + s := make([]FunctionArgument, len(r.Arguments)) + for i, v := range r.Arguments { + s[i] = FunctionArgument{ + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + } + } + opts.Arguments = s + } + if r.Returns != nil { + opts.Returns = &FunctionReturns{ + ResultDataType: r.Returns.ResultDataType, + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, + } + } + opts.Returns.Table.Columns = s + } + } + } + if r.ReturnNullValues != nil { + opts.ReturnNullValues = r.ReturnNullValues + } + if r.NullInputBehavior != nil { + opts.NullInputBehavior = r.NullInputBehavior + } + if r.ReturnResultsBehavior != nil { + opts.ReturnResultsBehavior = r.ReturnResultsBehavior + } + if r.FunctionDefinition != nil { + opts.FunctionDefinition = r.FunctionDefinition + } + return opts +} + +func (r *CreateFunctionForPythonFunctionRequest) toOpts() *CreateFunctionForPythonFunctionOptions { + opts := &CreateFunctionForPythonFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + IfNotExists: r.IfNotExists, + name: r.name, + + CopyGrants: r.CopyGrants, + + RuntimeVersion: r.RuntimeVersion, + Comment: r.Comment, + + Handler: r.Handler, + ExternalAccessIntegrations: r.ExternalAccessIntegrations, + } + if r.Arguments != nil { + s := make([]FunctionArgument, len(r.Arguments)) + for i, v := range r.Arguments { + s[i] = FunctionArgument{ + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + } + } + opts.Arguments = s + } + if r.Returns != nil { + opts.Returns = &FunctionReturns{ + ResultDataType: r.Returns.ResultDataType, + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, + } + } + opts.Returns.Table.Columns = s + } + } + } + if r.ReturnNullValues != nil { + opts.ReturnNullValues = r.ReturnNullValues + } + if r.NullInputBehavior != nil { + opts.NullInputBehavior = r.NullInputBehavior + } + if r.ReturnResultsBehavior != nil { + opts.ReturnResultsBehavior = r.ReturnResultsBehavior + } + if r.Imports != nil { + s := make([]FunctionImports, len(r.Imports)) + for i, v := range r.Imports { + s[i] = FunctionImports{ + Import: v.Import, + } + } + opts.Imports = s + } + if r.Packages != nil { + s := make([]FunctionPackages, len(r.Packages)) + for i, v := range r.Packages { + s[i] = FunctionPackages{ + Package: v.Package, + } + } + opts.Packages = s + } + if r.Secrets != nil { + s := make([]FunctionSecret, len(r.Secrets)) + for i, v := range r.Secrets { + s[i] = FunctionSecret{ + SecretVariableName: v.SecretVariableName, + SecretName: v.SecretName, + } + } + opts.Secrets = s + } + if r.FunctionDefinition != nil { + opts.FunctionDefinition = r.FunctionDefinition + } + return opts +} + +func (r *CreateFunctionForScalaFunctionRequest) toOpts() *CreateFunctionForScalaFunctionOptions { + opts := &CreateFunctionForScalaFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + IfNotExists: r.IfNotExists, + name: r.name, + + CopyGrants: r.CopyGrants, + + RuntimeVersion: r.RuntimeVersion, + Comment: r.Comment, + + Handler: r.Handler, + TargetPath: r.TargetPath, + } + if r.Arguments != nil { + s := make([]FunctionArgument, len(r.Arguments)) + for i, v := range r.Arguments { + s[i] = FunctionArgument{ + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + } + } + opts.Arguments = s + } + if r.Returns != nil { + opts.Returns = &FunctionReturns{ + ResultDataType: r.Returns.ResultDataType, + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, + } + } + opts.Returns.Table.Columns = s + } + } + } + if r.ReturnNullValues != nil { + opts.ReturnNullValues = r.ReturnNullValues + } + if r.NullInputBehavior != nil { + opts.NullInputBehavior = r.NullInputBehavior + } + if r.ReturnResultsBehavior != nil { + opts.ReturnResultsBehavior = r.ReturnResultsBehavior + } + if r.Imports != nil { + s := make([]FunctionImports, len(r.Imports)) + for i, v := range r.Imports { + s[i] = FunctionImports{ + Import: v.Import, + } + } + opts.Imports = s + } + if r.Packages != nil { + s := make([]FunctionPackages, len(r.Packages)) + for i, v := range r.Packages { + s[i] = FunctionPackages{ + Package: v.Package, + } + } + opts.Packages = s + } + if r.FunctionDefinition != nil { + opts.FunctionDefinition = r.FunctionDefinition + } + return opts +} + +func (r *CreateFunctionForSQLFunctionRequest) toOpts() *CreateFunctionForSQLFunctionOptions { + opts := &CreateFunctionForSQLFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + IfNotExists: r.IfNotExists, + name: r.name, + + CopyGrants: r.CopyGrants, + + Memoizable: r.Memoizable, + Comment: r.Comment, + } + if r.Arguments != nil { + s := make([]FunctionArgument, len(r.Arguments)) + for i, v := range r.Arguments { + s[i] = FunctionArgument{ + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + } + } + opts.Arguments = s + } + if r.Returns != nil { + opts.Returns = &FunctionReturns{ + ResultDataType: r.Returns.ResultDataType, + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, + } + } + opts.Returns.Table.Columns = s + } + } + } + if r.ReturnNullValues != nil { + opts.ReturnNullValues = r.ReturnNullValues + } + if r.ReturnResultsBehavior != nil { + opts.ReturnResultsBehavior = r.ReturnResultsBehavior + } + if r.FunctionDefinition != nil { + opts.FunctionDefinition = r.FunctionDefinition + } + return opts +} + +func (r *AlterFunctionRequest) toOpts() *AlterFunctionOptions { + opts := &AlterFunctionOptions{ + IfExists: r.IfExists, + name: r.name, + + RenameTo: r.RenameTo, + SetTags: r.SetTags, + UnsetTags: r.UnsetTags, + } + if r.ArgumentTypes != nil { + s := make([]FunctionArgumentType, len(r.ArgumentTypes)) + for i, v := range r.ArgumentTypes { + s[i] = FunctionArgumentType{ + ArgDataType: v.ArgDataType, + } + } + opts.ArgumentTypes = s + } + if r.Set != nil { + opts.Set = &FunctionSet{ + LogLevel: r.Set.LogLevel, + TraceLevel: r.Set.TraceLevel, + Comment: r.Set.Comment, + Secure: r.Set.Secure, + } + } + if r.Unset != nil { + opts.Unset = &FunctionUnset{ + Secure: r.Unset.Secure, + Comment: r.Unset.Comment, + LogLevel: r.Unset.LogLevel, + TraceLevel: r.Unset.TraceLevel, + } + } + return opts +} + +func (r *DropFunctionRequest) toOpts() *DropFunctionOptions { + opts := &DropFunctionOptions{ + IfExists: r.IfExists, + name: r.name, + } + if r.ArgumentTypes != nil { + s := make([]FunctionArgumentType, len(r.ArgumentTypes)) + for i, v := range r.ArgumentTypes { + s[i] = FunctionArgumentType{ + ArgDataType: v.ArgDataType, + } + } + opts.ArgumentTypes = s + } + return opts +} + +func (r *ShowFunctionRequest) toOpts() *ShowFunctionOptions { + opts := &ShowFunctionOptions{ + Like: r.Like, + In: r.In, + } + return opts +} + +func (r functionRow) convert() *Function { + return &Function{ + CreatedOn: r.CreatedOn, + Name: r.Name, + SchemaName: r.SchemaName, + MinNumArguments: r.MinNumArguments, + MaxNumArguments: r.MaxNumArguments, + Arguments: r.Arguments, + IsTableFunction: r.IsTableFunction == "Y", + IsSecure: r.IsSecure == "Y", + IsExternalFunction: r.IsExternalFunction == "Y", + Language: r.Language, + IsMemoizable: r.IsMemoizable == "Y", + } +} + +func (r *DescribeFunctionRequest) toOpts() *DescribeFunctionOptions { + opts := &DescribeFunctionOptions{ + name: r.name, + } + if r.ArgumentTypes != nil { + s := make([]FunctionArgumentType, len(r.ArgumentTypes)) + for i, v := range r.ArgumentTypes { + s[i] = FunctionArgumentType{ + ArgDataType: v.ArgDataType, + } + } + opts.ArgumentTypes = s + } + return opts +} + +func (r functionDetailRow) convert() *FunctionDetail { + return &FunctionDetail{ + Property: r.Property, + Value: r.Value, + } +} diff --git a/pkg/sdk/functions_validations_gen.go b/pkg/sdk/functions_validations_gen.go new file mode 100644 index 0000000000..62a8b8fd78 --- /dev/null +++ b/pkg/sdk/functions_validations_gen.go @@ -0,0 +1,138 @@ +package sdk + +var ( + _ validatable = new(CreateFunctionForJavaFunctionOptions) + _ validatable = new(CreateFunctionForJavascriptFunctionOptions) + _ validatable = new(CreateFunctionForPythonFunctionOptions) + _ validatable = new(CreateFunctionForScalaFunctionOptions) + _ validatable = new(CreateFunctionForSQLFunctionOptions) + _ validatable = new(AlterFunctionOptions) + _ validatable = new(DropFunctionOptions) + _ validatable = new(ShowFunctionOptions) + _ validatable = new(DescribeFunctionOptions) +) + +func (v *FunctionReturns) validate() error { + if v == nil { + return ErrNilOptions + } + var errs []error + if ok := exactlyOneValueSet(v.ResultDataType, v.Table); !ok { + errs = append(errs, errOneOf("Returns.ResultDataType", "Returns.Table")) + } + return JoinErrors(errs...) +} + +func (opts *CreateFunctionForJavaFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if err := opts.Returns.validate(); err != nil { + errs = append(errs, err) + } + return JoinErrors(errs...) +} + +func (opts *CreateFunctionForJavascriptFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if err := opts.Returns.validate(); err != nil { + errs = append(errs, err) + } + return JoinErrors(errs...) +} + +func (opts *CreateFunctionForPythonFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if err := opts.Returns.validate(); err != nil { + errs = append(errs, err) + } + return JoinErrors(errs...) +} + +func (opts *CreateFunctionForScalaFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if err := opts.Returns.validate(); err != nil { + errs = append(errs, err) + } + return JoinErrors(errs...) +} + +func (opts *CreateFunctionForSQLFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if err := opts.Returns.validate(); err != nil { + errs = append(errs, err) + } + return JoinErrors(errs...) +} + +func (opts *AlterFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + return JoinErrors(errs...) +} + +func (opts *DropFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + return JoinErrors(errs...) +} + +func (opts *ShowFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if valueSet(opts.Like) && !valueSet(opts.Like.Pattern) { + errs = append(errs, ErrPatternRequiredForLikeKeyword) + } + return JoinErrors(errs...) +} + +func (opts *DescribeFunctionOptions) validate() error { + if opts == nil { + return ErrNilOptions + } + var errs []error + if !ValidObjectIdentifier(opts.name) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + return JoinErrors(errs...) +} diff --git a/pkg/sdk/poc/main.go b/pkg/sdk/poc/main.go index 817f5a115a..0782b3a868 100644 --- a/pkg/sdk/poc/main.go +++ b/pkg/sdk/poc/main.go @@ -24,6 +24,7 @@ var definitionMapping = map[string]*generator.Interface{ "application_roles_def.go": sdk.ApplicationRolesDef, "views_def.go": sdk.ViewsDef, "stages_def.go": sdk.StagesDef, + "functions_def.go": sdk.FunctionsDef, } func main() { diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go new file mode 100644 index 0000000000..f0f4819667 --- /dev/null +++ b/pkg/sdk/testint/functions_integration_test.go @@ -0,0 +1,407 @@ +package testint + +import ( + "context" + "errors" + "fmt" + "testing" + "time" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/collections" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestInt_CreateFunctions(t *testing.T) { + client := testClient(t) + ctx := context.Background() + + _, warehouseCleanup := createWarehouse(t, client) + t.Cleanup(warehouseCleanup) + databaseTest, databaseCleanup := createDatabase(t, client) + t.Cleanup(databaseCleanup) + schemaTest, schemaCleanup := createSchema(t, client, databaseTest) + t.Cleanup(schemaCleanup) + + cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, argumentTypes []sdk.DataType) func() { + return func() { + es := []sdk.FunctionArgumentTypeRequest{} + for _, item := range argumentTypes { + es = append(es, *sdk.NewFunctionArgumentTypeRequest().WithArgDataType(item)) + } + err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id).WithArgumentTypes(es)) + if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { + return + } + require.NoError(t, err) + } + } + + t.Run("create function for Java", func(t *testing.T) { + name := "echo_varchar" + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + + definition := ` + class TestFunc { + public static String echoVarchar(String x) { + return x; + } + }` + target := fmt.Sprintf("@~/tf-%d.jar", time.Now().Unix()) + returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVARCHAR) + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR) + request := sdk.NewCreateFunctionForJavaFunctionRequest(id). + WithOrReplace(sdk.Bool(true)). + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput). + WithReturns(returnsRequest). + WithHandler(sdk.String("TestFunc.echoVarchar")). + WithTargetPath(&target). + WithFunctionDefinition(&definition) + err := client.Functions.CreateFunctionForJava(ctx, request) + require.NoError(t, err) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"VARCHAR"})) + + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + require.NoError(t, err) + require.Equal(t, 1, len(functions)) + }) + + t.Run("create function for Javascript", func(t *testing.T) { + name := "js_factorial" + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + + definition := ` + if (D <= 0) { + return 1; + } else { + var result = 1; + for (var i = 2; i <= D; i++) { + result = result * i; + } + return result; + } + ` + returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("d").WithArgDataType(sdk.DataTypeFloat) + request := sdk.NewCreateFunctionForJavascriptFunctionRequest(id). + WithOrReplace(sdk.Bool(true)). + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput). + WithReturns(returnsRequest). + WithFunctionDefinition(&definition) + err := client.Functions.CreateFunctionForJavascript(ctx, request) + require.NoError(t, err) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) + + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + require.NoError(t, err) + require.Equal(t, 1, len(functions)) + }) + + t.Run("create function for Python", func(t *testing.T) { + name := random.StringN(8) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + + definition := ` +def dump(i): + print("Hello World!") + ` + returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVariant) + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("i").WithArgDataType(sdk.DataTypeNumber) + request := sdk.NewCreateFunctionForPythonFunctionRequest(id). + WithOrReplace(sdk.Bool(true)). + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + WithReturns(returnsRequest). + WithRuntimeVersion(sdk.String("3.8")). + WithHandler(sdk.String("dump")). + WithFunctionDefinition(&definition) + err := client.Functions.CreateFunctionForPython(ctx, request) + require.NoError(t, err) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"int"})) + + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + require.NoError(t, err) + require.Equal(t, 1, len(functions)) + }) + + t.Run("create function for Scala", func(t *testing.T) { + name := "echo_varchar" + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + + definition := ` + class Echo { + def echoVarchar(x : String): String = { + return x + } + } + ` + returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVARCHAR) + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR) + request := sdk.NewCreateFunctionForScalaFunctionRequest(id). + WithOrReplace(sdk.Bool(true)). + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + WithReturns(returnsRequest). + WithRuntimeVersion(sdk.String("2.12")). + WithHandler(sdk.String("Echo.echoVarchar")). + WithFunctionDefinition(&definition) + err := client.Functions.CreateFunctionForScala(ctx, request) + require.NoError(t, err) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"VARCHAR"})) + + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + require.NoError(t, err) + require.Equal(t, 1, len(functions)) + }) + + t.Run("create function for SQL", func(t *testing.T) { + name := random.String() + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + + definition := "3.141592654::FLOAT" + returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeFloat) + request := sdk.NewCreateFunctionForSQLFunctionRequest(id). + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + WithOrReplace(sdk.Bool(true)). + WithComment(sdk.String("comment")). + WithReturns(returnsRequest). + WithFunctionDefinition(&definition) + err := client.Functions.CreateFunctionForSQL(ctx, request) + require.NoError(t, err) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"FLOAT"})) + + function, err := client.Functions.ShowByID(ctx, id) + require.NoError(t, err) + require.Equal(t, id.Name(), function.Name) + require.Equal(t, "SQL", function.Language) + }) +} + +func TestInt_AlterAndShowFunctions(t *testing.T) { + client := testClient(t) + ctx := context.Background() + + _, warehouseCleanup := createWarehouse(t, client) + t.Cleanup(warehouseCleanup) + databaseTest, databaseCleanup := createDatabase(t, client) + t.Cleanup(databaseCleanup) + schemaTest, schemaCleanup := createSchema(t, client, databaseTest) + t.Cleanup(schemaCleanup) + tagTest, tagCleanup := createTag(t, client, databaseTest, schemaTest) + t.Cleanup(tagCleanup) + + cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, argumentTypes []sdk.DataType) func() { + return func() { + es := []sdk.FunctionArgumentTypeRequest{} + for _, item := range argumentTypes { + es = append(es, *sdk.NewFunctionArgumentTypeRequest().WithArgDataType(item)) + } + err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id).WithArgumentTypes(es)) + if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { + return + } + require.NoError(t, err) + } + } + + createFunctionForSQLHandle := func(t *testing.T, cleanup bool) *sdk.Function { + t.Helper() + + definition := "3.141592654::FLOAT" + returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeFloat) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.String()) + request := sdk.NewCreateFunctionForSQLFunctionRequest(id). + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + WithOrReplace(sdk.Bool(true)). + WithReturns(returnsRequest). + WithFunctionDefinition(&definition) + err := client.Functions.CreateFunctionForSQL(ctx, request) + require.NoError(t, err) + if cleanup { + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) + } + function, err := client.Functions.ShowByID(ctx, id) + require.NoError(t, err) + return function + } + + defaultArgumentTypes := []sdk.FunctionArgumentTypeRequest{ + *sdk.NewFunctionArgumentTypeRequest().WithArgDataType(sdk.DataTypeFloat), + } + + t.Run("alter function: rename", func(t *testing.T) { + f := createFunctionForSQLHandle(t, false) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + nid := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.String()) + request := sdk.NewAlterFunctionRequest(id).WithRenameTo(&nid).WithArgumentTypes(defaultArgumentTypes) + err := client.Functions.Alter(ctx, request) + if err != nil { + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"FLOAT"})) + } else { + t.Cleanup(cleanupFunctionHandle(nid, []sdk.DataType{"FLOAT"})) + } + require.NoError(t, err) + + _, err = client.Functions.ShowByID(ctx, id) + assert.ErrorIs(t, err, collections.ErrObjectNotFound) + + e, err := client.Functions.ShowByID(ctx, nid) + require.NoError(t, err) + require.Equal(t, nid.Name(), e.Name) + }) + + t.Run("alter function: set log level", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + set := sdk.NewFunctionSetRequest().WithLogLevel(sdk.String("DEBUG")) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("alter function: unset log level", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + unset := sdk.NewFunctionUnsetRequest().WithLogLevel(sdk.Bool(true)) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("alter function: set trace level", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + set := sdk.NewFunctionSetRequest().WithTraceLevel(sdk.String("ALWAYS")) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("alter function: unset trace level", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + unset := sdk.NewFunctionUnsetRequest().WithTraceLevel(sdk.Bool(true)) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("alter function: set comment", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + set := sdk.NewFunctionSetRequest().WithComment(sdk.String("comment")) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("alter function: unset comment", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + unset := sdk.NewFunctionUnsetRequest().WithComment(sdk.Bool(true)) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("alter function: set secure", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + set := sdk.NewFunctionSetRequest().WithSecure(sdk.Bool(true)) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + + e, err := client.Functions.ShowByID(ctx, id) + require.NoError(t, err) + require.Equal(t, true, e.IsSecure) + }) + + t.Run("alter function: unset secure", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + unset := sdk.NewFunctionUnsetRequest().WithSecure(sdk.Bool(true)) + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + + e, err := client.Functions.ShowByID(ctx, id) + require.NoError(t, err) + require.Equal(t, false, e.IsSecure) + }) + + t.Run("alter function: set and unset tags", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + setTags := []sdk.TagAssociation{ + { + Name: tagTest.ID(), + Value: "abc", + }, + } + request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSetTags(setTags) + err := client.Functions.Alter(ctx, request) + require.NoError(t, err) + + unsetTags := []sdk.ObjectIdentifier{ + tagTest.ID(), + } + request = sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnsetTags(unsetTags) + err = client.Functions.Alter(ctx, request) + require.NoError(t, err) + }) + + t.Run("show function for SQL: without like", func(t *testing.T) { + f1 := createFunctionForSQLHandle(t, true) + f2 := createFunctionForSQLHandle(t, true) + + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + require.NoError(t, err) + + require.Equal(t, 2, len(functions)) + require.Contains(t, functions, *f1) + require.Contains(t, functions, *f2) + }) + + t.Run("show function for SQL: with like", func(t *testing.T) { + f1 := createFunctionForSQLHandle(t, true) + f2 := createFunctionForSQLHandle(t, true) + + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(f1.Name)) + require.NoError(t, err) + + require.Equal(t, 1, len(functions)) + require.Contains(t, functions, *f1) + require.NotContains(t, functions, *f2) + }) + + t.Run("show function for SQL: no matches", func(t *testing.T) { + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(random.String())) + require.NoError(t, err) + require.Equal(t, 0, len(functions)) + }) + + t.Run("describe function for SQL", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + + request := sdk.NewDescribeFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes) + details, err := client.Functions.Describe(ctx, request) + require.NoError(t, err) + require.Greater(t, len(details), 0) + }) +} From fc6b2e02e7b2434d3dc7754c4965551484516dc8 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Sun, 26 Nov 2023 21:05:51 -0800 Subject: [PATCH 02/10] update --- pkg/sdk/functions_def.go | 52 +++++---- pkg/sdk/functions_dto_builders_gen.go | 102 ++++++------------ pkg/sdk/functions_dto_gen.go | 19 ++-- pkg/sdk/functions_gen.go | 43 +++++--- pkg/sdk/functions_gen_test.go | 18 ++-- pkg/sdk/functions_impl_gen.go | 32 +++--- pkg/sdk/functions_validations_gen.go | 3 + pkg/sdk/testint/functions_integration_test.go | 93 ++++++---------- 8 files changed, 163 insertions(+), 199 deletions(-) diff --git a/pkg/sdk/functions_def.go b/pkg/sdk/functions_def.go index 95519d4859..0d9fed671a 100644 --- a/pkg/sdk/functions_def.go +++ b/pkg/sdk/functions_def.go @@ -6,7 +6,8 @@ import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/gen var functionArgument = g.NewQueryStruct("FunctionArgument"). Text("ArgName", g.KeywordOptions().NoQuotes()). - Text("ArgDataType", g.KeywordOptions().NoQuotes()) + Text("ArgDataType", g.KeywordOptions().NoQuotes()). + OptionalTextAssignment("Default", g.ParameterOptions().SingleQuotes()) var functionArgumentType = g.NewQueryStruct("FunctionArgumentType"). Text("ArgDataType", g.KeywordOptions().NoQuotes()) @@ -21,7 +22,7 @@ var functionSecret = g.NewQueryStruct("FunctionSecret"). var functionReturns = g.NewQueryStruct("FunctionReturns"). OptionalText("ResultDataType", g.KeywordOptions()). - OptionalQueryStructField( + QueryStructField( "Table", g.NewQueryStruct("FunctionReturnsTable"). ListQueryStructField( @@ -73,7 +74,7 @@ var FunctionsDef = g.NewInterface( functionArgument, g.ParameterOptions().Parentheses().NoEquals()). OptionalSQL("COPY GRANTS"). - OptionalQueryStructField( + QueryStructField( "Returns", functionReturns, g.KeywordOptions().SQL("RETURNS"), @@ -94,7 +95,7 @@ var FunctionsDef = g.NewInterface( functionVolatileOrNot, g.KeywordOptions(), ). - OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). + TextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). ListQueryStructField( "Imports", @@ -106,7 +107,7 @@ var FunctionsDef = g.NewInterface( functionPackages, g.ParameterOptions().Parentheses().SQL("PACKAGES"), ). - OptionalTextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()). ListQueryStructField( "Secrets", @@ -114,7 +115,7 @@ var FunctionsDef = g.NewInterface( g.ParameterOptions().Parentheses().SQL("SECRETS"), ). OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). - OptionalQueryStructField( + QueryStructField( "FunctionDefinition", functionDefinition, g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), @@ -136,7 +137,7 @@ var FunctionsDef = g.NewInterface( functionArgument, g.ParameterOptions().Parentheses().NoEquals()). OptionalSQL("COPY GRANTS"). - OptionalQueryStructField( + QueryStructField( "Returns", functionReturns, g.KeywordOptions().SQL("RETURNS"), @@ -158,7 +159,7 @@ var FunctionsDef = g.NewInterface( g.KeywordOptions(), ). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - OptionalQueryStructField( + QueryStructField( "FunctionDefinition", functionDefinition, g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), @@ -180,7 +181,7 @@ var FunctionsDef = g.NewInterface( functionArgument, g.ParameterOptions().Parentheses().NoEquals()). OptionalSQL("COPY GRANTS"). - OptionalQueryStructField( + QueryStructField( "Returns", functionReturns, g.KeywordOptions().SQL("RETURNS"), @@ -213,14 +214,14 @@ var FunctionsDef = g.NewInterface( functionPackages, g.ParameterOptions().Parentheses().SQL("PACKAGES"), ). - OptionalTextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()). ListQueryStructField( "Secrets", functionSecret, g.ParameterOptions().Parentheses().SQL("SECRETS"), ). - OptionalQueryStructField( + QueryStructField( "FunctionDefinition", functionDefinition, g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), @@ -242,7 +243,7 @@ var FunctionsDef = g.NewInterface( functionArgument, g.ParameterOptions().Parentheses().NoEquals()). OptionalSQL("COPY GRANTS"). - OptionalQueryStructField( + QueryStructField( "Returns", functionReturns, g.KeywordOptions().SQL("RETURNS"), @@ -275,9 +276,9 @@ var FunctionsDef = g.NewInterface( functionPackages, g.ParameterOptions().Parentheses().SQL("PACKAGES"), ). - OptionalTextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). - OptionalQueryStructField( + QueryStructField( "FunctionDefinition", functionDefinition, g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), @@ -299,7 +300,7 @@ var FunctionsDef = g.NewInterface( functionArgument, g.ParameterOptions().Parentheses().NoEquals()). OptionalSQL("COPY GRANTS"). - OptionalQueryStructField( + QueryStructField( "Returns", functionReturns, g.KeywordOptions().SQL("RETURNS"), @@ -316,7 +317,7 @@ var FunctionsDef = g.NewInterface( ). OptionalSQL("MEMOIZABLE"). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - OptionalQueryStructField( + QueryStructField( "FunctionDefinition", functionDefinition, g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), @@ -345,7 +346,8 @@ var FunctionsDef = g.NewInterface( ). Identifier("RenameTo", g.KindOfTPointer[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")). SetTags().UnsetTags(). - WithValidation(g.ValidIdentifier, "name"), + WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ExactlyOneValueSet, "Set", "Unset", "SetTags", "UnsetTags", "RenameTo"), ).DropOperation( "https://docs.snowflake.com/en/sql-reference/sql/drop-function", g.NewQueryStruct("DropFunction"). @@ -359,15 +361,21 @@ var FunctionsDef = g.NewInterface( g.ParameterOptions().Parentheses().NoEquals()). WithValidation(g.ValidIdentifier, "name"), ).ShowOperation( - "https://docs.snowflake.com/en/sql-reference/sql/show-functions", + "https://docs.snowflake.com/en/sql-reference/sql/show-user-functions", g.DbStruct("functionRow"). Field("created_on", "string"). Field("name", "string"). Field("schema_name", "string"). + Field("is_builtin", "bool"). + Field("is_aggregate", "bool"). + Field("is_ansi", "bool"). Field("min_num_arguments", "int"). Field("max_num_arguments", "int"). Field("arguments", "string"). - Field("is_table_function", "string"). + Field("description", "string"). + Field("catalog_name", "string"). + Field("is_table_function", "bool"). + Field("valid_for_clustering", "bool"). Field("is_secure", "string"). Field("is_external_function", "string"). Field("language", "string"). @@ -376,10 +384,14 @@ var FunctionsDef = g.NewInterface( Field("CreatedOn", "string"). Field("Name", "string"). Field("SchemaName", "string"). + Field("IsBuiltin", "bool"). + Field("IsAggregate", "bool"). + Field("IsAnsi", "bool"). Field("MinNumArguments", "int"). Field("MaxNumArguments", "int"). Field("Arguments", "string"). Field("IsTableFunction", "bool"). + Field("ValidForClustering", "bool"). Field("IsSecure", "bool"). Field("IsExternalFunction", "bool"). Field("Language", "string"). @@ -391,7 +403,7 @@ var FunctionsDef = g.NewInterface( OptionalIn(), ).DescribeOperation( g.DescriptionMappingKindSlice, - "https://docs.snowflake.com/en/sql-reference/sql/describe-function", + "https://docs.snowflake.com/en/sql-reference/sql/desc-function", g.DbStruct("functionDetailRow"). Field("property", "string"). Field("value", "string"), diff --git a/pkg/sdk/functions_dto_builders_gen.go b/pkg/sdk/functions_dto_builders_gen.go index e3652d2ddd..e81c4d0430 100644 --- a/pkg/sdk/functions_dto_builders_gen.go +++ b/pkg/sdk/functions_dto_builders_gen.go @@ -6,9 +6,15 @@ import () func NewCreateFunctionForJavaFunctionRequest( name SchemaObjectIdentifier, + returns *FunctionReturnsRequest, + handler string, + functionDefinition string, ) *CreateFunctionForJavaFunctionRequest { s := CreateFunctionForJavaFunctionRequest{} s.name = name + s.Returns = returns + s.Handler = handler + s.FunctionDefinition = functionDefinition return &s } @@ -42,11 +48,6 @@ func (s *CreateFunctionForJavaFunctionRequest) WithCopyGrants(CopyGrants *bool) return s } -func (s *CreateFunctionForJavaFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForJavaFunctionRequest { - s.Returns = Returns - return s -} - func (s *CreateFunctionForJavaFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForJavaFunctionRequest { s.ReturnNullValues = &ReturnNullValues return s @@ -82,11 +83,6 @@ func (s *CreateFunctionForJavaFunctionRequest) WithPackages(Packages []FunctionP return s } -func (s *CreateFunctionForJavaFunctionRequest) WithHandler(Handler *string) *CreateFunctionForJavaFunctionRequest { - s.Handler = Handler - return s -} - func (s *CreateFunctionForJavaFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateFunctionForJavaFunctionRequest { s.ExternalAccessIntegrations = ExternalAccessIntegrations return s @@ -102,11 +98,6 @@ func (s *CreateFunctionForJavaFunctionRequest) WithTargetPath(TargetPath *string return s } -func (s *CreateFunctionForJavaFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForJavaFunctionRequest { - s.FunctionDefinition = FunctionDefinition - return s -} - func NewFunctionArgumentRequest() *FunctionArgumentRequest { return &FunctionArgumentRequest{} } @@ -121,6 +112,11 @@ func (s *FunctionArgumentRequest) WithArgDataType(ArgDataType DataType) *Functio return s } +func (s *FunctionArgumentRequest) WithDefault(Default *string) *FunctionArgumentRequest { + s.Default = Default + return s +} + func NewFunctionReturnsRequest() *FunctionReturnsRequest { return &FunctionReturnsRequest{} } @@ -192,9 +188,12 @@ func (s *FunctionSecretRequest) WithSecretName(SecretName string) *FunctionSecre func NewCreateFunctionForJavascriptFunctionRequest( name SchemaObjectIdentifier, + returns *FunctionReturnsRequest, + functionDefinition string, ) *CreateFunctionForJavascriptFunctionRequest { s := CreateFunctionForJavascriptFunctionRequest{} s.name = name + s.FunctionDefinition = functionDefinition return &s } @@ -228,11 +227,6 @@ func (s *CreateFunctionForJavascriptFunctionRequest) WithCopyGrants(CopyGrants * return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForJavascriptFunctionRequest { - s.Returns = Returns - return s -} - func (s *CreateFunctionForJavascriptFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForJavascriptFunctionRequest { s.ReturnNullValues = &ReturnNullValues return s @@ -253,16 +247,19 @@ func (s *CreateFunctionForJavascriptFunctionRequest) WithComment(Comment *string return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForJavascriptFunctionRequest { - s.FunctionDefinition = FunctionDefinition - return s -} - func NewCreateFunctionForPythonFunctionRequest( name SchemaObjectIdentifier, + returns *FunctionReturnsRequest, + runtimeVersion string, + handler string, + functionDefinition string, ) *CreateFunctionForPythonFunctionRequest { s := CreateFunctionForPythonFunctionRequest{} s.name = name + s.Returns = returns + s.RuntimeVersion = runtimeVersion + s.Handler = handler + s.FunctionDefinition = functionDefinition return &s } @@ -296,11 +293,6 @@ func (s *CreateFunctionForPythonFunctionRequest) WithCopyGrants(CopyGrants *bool return s } -func (s *CreateFunctionForPythonFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForPythonFunctionRequest { - s.Returns = Returns - return s -} - func (s *CreateFunctionForPythonFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForPythonFunctionRequest { s.ReturnNullValues = &ReturnNullValues return s @@ -316,11 +308,6 @@ func (s *CreateFunctionForPythonFunctionRequest) WithReturnResultsBehavior(Retur return s } -func (s *CreateFunctionForPythonFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateFunctionForPythonFunctionRequest { - s.RuntimeVersion = RuntimeVersion - return s -} - func (s *CreateFunctionForPythonFunctionRequest) WithComment(Comment *string) *CreateFunctionForPythonFunctionRequest { s.Comment = Comment return s @@ -336,11 +323,6 @@ func (s *CreateFunctionForPythonFunctionRequest) WithPackages(Packages []Functio return s } -func (s *CreateFunctionForPythonFunctionRequest) WithHandler(Handler *string) *CreateFunctionForPythonFunctionRequest { - s.Handler = Handler - return s -} - func (s *CreateFunctionForPythonFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateFunctionForPythonFunctionRequest { s.ExternalAccessIntegrations = ExternalAccessIntegrations return s @@ -351,16 +333,17 @@ func (s *CreateFunctionForPythonFunctionRequest) WithSecrets(Secrets []FunctionS return s } -func (s *CreateFunctionForPythonFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForPythonFunctionRequest { - s.FunctionDefinition = FunctionDefinition - return s -} - func NewCreateFunctionForScalaFunctionRequest( name SchemaObjectIdentifier, + returns *FunctionReturnsRequest, + handler string, + functionDefinition string, ) *CreateFunctionForScalaFunctionRequest { s := CreateFunctionForScalaFunctionRequest{} s.name = name + s.Returns = returns + s.Handler = handler + s.FunctionDefinition = functionDefinition return &s } @@ -394,11 +377,6 @@ func (s *CreateFunctionForScalaFunctionRequest) WithCopyGrants(CopyGrants *bool) return s } -func (s *CreateFunctionForScalaFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForScalaFunctionRequest { - s.Returns = Returns - return s -} - func (s *CreateFunctionForScalaFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForScalaFunctionRequest { s.ReturnNullValues = &ReturnNullValues return s @@ -434,26 +412,20 @@ func (s *CreateFunctionForScalaFunctionRequest) WithPackages(Packages []Function return s } -func (s *CreateFunctionForScalaFunctionRequest) WithHandler(Handler *string) *CreateFunctionForScalaFunctionRequest { - s.Handler = Handler - return s -} - func (s *CreateFunctionForScalaFunctionRequest) WithTargetPath(TargetPath *string) *CreateFunctionForScalaFunctionRequest { s.TargetPath = TargetPath return s } -func (s *CreateFunctionForScalaFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForScalaFunctionRequest { - s.FunctionDefinition = FunctionDefinition - return s -} - func NewCreateFunctionForSQLFunctionRequest( name SchemaObjectIdentifier, + returns *FunctionReturnsRequest, + functionDefinition string, ) *CreateFunctionForSQLFunctionRequest { s := CreateFunctionForSQLFunctionRequest{} s.name = name + s.Returns = returns + s.FunctionDefinition = functionDefinition return &s } @@ -487,11 +459,6 @@ func (s *CreateFunctionForSQLFunctionRequest) WithCopyGrants(CopyGrants *bool) * return s } -func (s *CreateFunctionForSQLFunctionRequest) WithReturns(Returns *FunctionReturnsRequest) *CreateFunctionForSQLFunctionRequest { - s.Returns = Returns - return s -} - func (s *CreateFunctionForSQLFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForSQLFunctionRequest { s.ReturnNullValues = &ReturnNullValues return s @@ -512,11 +479,6 @@ func (s *CreateFunctionForSQLFunctionRequest) WithComment(Comment *string) *Crea return s } -func (s *CreateFunctionForSQLFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateFunctionForSQLFunctionRequest { - s.FunctionDefinition = FunctionDefinition - return s -} - func NewAlterFunctionRequest( name SchemaObjectIdentifier, ) *AlterFunctionRequest { diff --git a/pkg/sdk/functions_dto_gen.go b/pkg/sdk/functions_dto_gen.go index c9a8f28618..3c908a9c67 100644 --- a/pkg/sdk/functions_dto_gen.go +++ b/pkg/sdk/functions_dto_gen.go @@ -30,16 +30,17 @@ type CreateFunctionForJavaFunctionRequest struct { Comment *string Imports []FunctionImportsRequest Packages []FunctionPackagesRequest - Handler *string + Handler string ExternalAccessIntegrations []AccountObjectIdentifier Secrets []FunctionSecretRequest TargetPath *string - FunctionDefinition *string + FunctionDefinition string } type FunctionArgumentRequest struct { ArgName string ArgDataType DataType + Default *string } type FunctionReturnsRequest struct { @@ -82,7 +83,7 @@ type CreateFunctionForJavascriptFunctionRequest struct { NullInputBehavior *FunctionNullInputBehavior ReturnResultsBehavior *FunctionReturnResultsBehavior Comment *string - FunctionDefinition *string + FunctionDefinition string } type CreateFunctionForPythonFunctionRequest struct { @@ -97,14 +98,14 @@ type CreateFunctionForPythonFunctionRequest struct { ReturnNullValues *FunctionReturnNullValues NullInputBehavior *FunctionNullInputBehavior ReturnResultsBehavior *FunctionReturnResultsBehavior - RuntimeVersion *string + RuntimeVersion string Comment *string Imports []FunctionImportsRequest Packages []FunctionPackagesRequest - Handler *string + Handler string ExternalAccessIntegrations []AccountObjectIdentifier Secrets []FunctionSecretRequest - FunctionDefinition *string + FunctionDefinition string } type CreateFunctionForScalaFunctionRequest struct { @@ -123,9 +124,9 @@ type CreateFunctionForScalaFunctionRequest struct { Comment *string Imports []FunctionImportsRequest Packages []FunctionPackagesRequest - Handler *string + Handler string TargetPath *string - FunctionDefinition *string + FunctionDefinition string } type CreateFunctionForSQLFunctionRequest struct { @@ -141,7 +142,7 @@ type CreateFunctionForSQLFunctionRequest struct { ReturnResultsBehavior *FunctionReturnResultsBehavior Memoizable *bool Comment *string - FunctionDefinition *string + FunctionDefinition string } type AlterFunctionRequest struct { diff --git a/pkg/sdk/functions_gen.go b/pkg/sdk/functions_gen.go index c648d8640a..81765ade0d 100644 --- a/pkg/sdk/functions_gen.go +++ b/pkg/sdk/functions_gen.go @@ -17,7 +17,7 @@ type Functions interface { type FunctionNullInputBehavior string -const ( +var ( FunctionNullInputBehaviorCalledOnNullInput FunctionNullInputBehavior = "CALLED ON NULL INPUT" FunctionNullInputBehaviorReturnNullInput FunctionNullInputBehavior = "RETURN NULL ON NULL INPUT" FunctionNullInputBehaviorStrict FunctionNullInputBehavior = "STRICT" @@ -25,14 +25,14 @@ const ( type FunctionReturnResultsBehavior string -const ( +var ( FunctionReturnResultsBehaviorVolatile FunctionReturnResultsBehavior = "VOLATILE" FunctionReturnResultsBehaviorImmutable FunctionReturnResultsBehavior = "IMMUTABLE" ) type FunctionReturnNullValues string -const ( +var ( FunctionReturnNullValuesNull FunctionReturnNullValues = "NULL" FunctionReturnNullValuesNotNull FunctionReturnNullValues = "NOT NULL" ) @@ -57,16 +57,17 @@ type CreateFunctionForJavaFunctionOptions struct { Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` - Handler *string `ddl:"parameter,single_quotes" sql:"HANDLER"` + Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` Secrets []FunctionSecret `ddl:"parameter,parentheses" sql:"SECRETS"` TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } type FunctionArgument struct { ArgName string `ddl:"keyword,no_quotes"` ArgDataType DataType `ddl:"keyword,no_quotes"` + Default *string `ddl:"parameter,no_quotes" sql:"DEFAULT"` } type FunctionReturns struct { @@ -113,7 +114,7 @@ type CreateFunctionForJavascriptFunctionOptions struct { NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // CreateFunctionForPythonFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. @@ -132,14 +133,14 @@ type CreateFunctionForPythonFunctionOptions struct { languagePython bool `ddl:"static" sql:"LANGUAGE PYTHON"` NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + RuntimeVersion string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` - Handler *string `ddl:"parameter,single_quotes" sql:"HANDLER"` + Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` Secrets []FunctionSecret `ddl:"parameter,parentheses" sql:"SECRETS"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // CreateFunctionForScalaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. @@ -158,13 +159,13 @@ type CreateFunctionForScalaFunctionOptions struct { languageScala bool `ddl:"static" sql:"LANGUAGE SCALA"` NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` - Handler *string `ddl:"parameter,single_quotes" sql:"HANDLER"` + Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // CreateFunctionForSQLFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. @@ -183,7 +184,7 @@ type CreateFunctionForSQLFunctionOptions struct { ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` Memoizable *bool `ddl:"keyword" sql:"MEMOIZABLE"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // AlterFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-function. @@ -227,7 +228,7 @@ type DropFunctionOptions struct { ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` } -// ShowFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-functions. +// ShowFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-user-functions. type ShowFunctionOptions struct { show bool `ddl:"static" sql:"SHOW"` userFunctions bool `ddl:"static" sql:"USER FUNCTIONS"` @@ -239,10 +240,16 @@ type functionRow struct { CreatedOn string `db:"created_on"` Name string `db:"name"` SchemaName string `db:"schema_name"` + IsBuiltIn string `db:"is_builtin"` + IsAggregate string `db:"is_aggregate"` + IsAnsi string `db:"is_ansi"` MinNumArguments int `db:"min_num_arguments"` MaxNumArguments int `db:"max_num_arguments"` Arguments string `db:"arguments"` + Description string `db:"description"` + CatalogName string `db:"catalog_name"` IsTableFunction string `db:"is_table_function"` + ValidForClustering string `db:"valid_for_clustering"` IsSecure string `db:"is_secure"` IsExternalFunction string `db:"is_external_function"` Language string `db:"language"` @@ -253,17 +260,23 @@ type Function struct { CreatedOn string Name string SchemaName string + IsBuiltIn bool + IsAggregate bool + IsAnsi bool MinNumArguments int MaxNumArguments int Arguments string + Description string + CatalogName string IsTableFunction bool + ValidForClustering bool IsSecure bool IsExternalFunction bool Language string IsMemoizable bool } -// DescribeFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/describe-function. +// DescribeFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-function. type DescribeFunctionOptions struct { describe bool `ddl:"static" sql:"DESCRIBE"` function bool `ddl:"static" sql:"FUNCTION"` diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go index 554cc4fe11..46f6359913 100644 --- a/pkg/sdk/functions_gen_test.go +++ b/pkg/sdk/functions_gen_test.go @@ -75,7 +75,7 @@ func TestFunctions_CreateFunctionForJava(t *testing.T) { Package: "com.snowflake:snowpark:1.2.0", }, } - opts.Handler = String("TestFunc.echoVarchar") + opts.Handler = "TestFunc.echoVarchar" opts.ExternalAccessIntegrations = []AccountObjectIdentifier{ NewAccountObjectIdentifier("ext_integration"), } @@ -90,7 +90,7 @@ func TestFunctions_CreateFunctionForJava(t *testing.T) { }, } opts.TargetPath = String("@~/testfunc.jar") - opts.FunctionDefinition = String("return id + name;") + opts.FunctionDefinition = "return id + name;" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (id NUMBER, name VARCHAR) COPY GRANTS RETURNS TABLE (country_code VARCHAR, country_name VARCHAR) NOT NULL LANGUAGE JAVA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@~/my_decrement_udf_package_dir/my_decrement_udf_jar.jar') PACKAGES = ('com.snowflake:snowpark:1.2.0') HANDLER = 'TestFunc.echoVarchar' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) TARGET_PATH = '@~/testfunc.jar' AS 'return id + name;'`, id.FullyQualifiedName()) }) } @@ -138,7 +138,7 @@ func TestFunctions_CreateFunctionForJavascript(t *testing.T) { returnResultsBehavior := FunctionReturnResultsBehaviorImmutable opts.ReturnResultsBehavior = &returnResultsBehavior opts.Comment = String("comment") - opts.FunctionDefinition = String("return 1;") + opts.FunctionDefinition = "return 1;" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (d FLOAT) COPY GRANTS RETURNS FLOAT NOT NULL LANGUAGE JAVASCRIPT CALLED ON NULL INPUT IMMUTABLE COMMENT = 'comment' AS 'return 1;'`, id.FullyQualifiedName()) }) } @@ -186,7 +186,7 @@ func TestFunctions_CreateFunctionForPython(t *testing.T) { opts.NullInputBehavior = &nullInputBehavior returnResultsBehavior := FunctionReturnResultsBehaviorImmutable opts.ReturnResultsBehavior = &returnResultsBehavior - opts.RuntimeVersion = String("3.8") + opts.RuntimeVersion = "3.8" opts.Comment = String("comment") opts.Imports = []FunctionImports{ { @@ -204,7 +204,7 @@ func TestFunctions_CreateFunctionForPython(t *testing.T) { Package: "pandas", }, } - opts.Handler = String("udf") + opts.Handler = "udf" opts.ExternalAccessIntegrations = []AccountObjectIdentifier{ NewAccountObjectIdentifier("ext_integration"), } @@ -218,7 +218,7 @@ func TestFunctions_CreateFunctionForPython(t *testing.T) { SecretName: "name2", }, } - opts.FunctionDefinition = String("import numpy as np") + opts.FunctionDefinition = "import numpy as np" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (i NUMBER) COPY GRANTS RETURNS VARIANT NOT NULL LANGUAGE PYTHON CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '3.8' COMMENT = 'comment' IMPORTS = ('numpy', 'pandas') PACKAGES = ('numpy', 'pandas') HANDLER = 'udf' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) AS 'import numpy as np'`, id.FullyQualifiedName()) }) } @@ -273,8 +273,8 @@ func TestFunctions_CreateFunctionForScala(t *testing.T) { Import: "@udf_libs/echohandler.jar", }, } - opts.Handler = String("Echo.echoVarchar") - opts.FunctionDefinition = String("return x") + opts.Handler ="Echo.echoVarchar" + opts.FunctionDefinition = "return x" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (x VARCHAR) COPY GRANTS RETURNS VARCHAR NOT NULL LANGUAGE SCALA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@udf_libs/echohandler.jar') HANDLER = 'Echo.echoVarchar' AS 'return x'`, id.FullyQualifiedName()) }) } @@ -316,7 +316,7 @@ func TestFunctions_CreateFunctionForSQL(t *testing.T) { opts.ReturnResultsBehavior = &returnResultsBehavior opts.Memoizable = Bool(true) opts.Comment = String("comment") - opts.FunctionDefinition = String("3.141592654::FLOAT") + opts.FunctionDefinition = "3.141592654::FLOAT" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s COPY GRANTS RETURNS FLOAT NOT NULL IMMUTABLE MEMOIZABLE COMMENT = 'comment' AS '3.141592654::FLOAT'`, id.FullyQualifiedName()) }) } diff --git a/pkg/sdk/functions_impl_gen.go b/pkg/sdk/functions_impl_gen.go index 23a3702b40..f1be1337a9 100644 --- a/pkg/sdk/functions_impl_gen.go +++ b/pkg/sdk/functions_impl_gen.go @@ -99,6 +99,7 @@ func (r *CreateFunctionForJavaFunctionRequest) toOpts() *CreateFunctionForJavaFu s[i] = FunctionArgument{ ArgName: v.ArgName, ArgDataType: v.ArgDataType, + Default: v.Default, } } opts.Arguments = s @@ -158,9 +159,7 @@ func (r *CreateFunctionForJavaFunctionRequest) toOpts() *CreateFunctionForJavaFu } opts.Secrets = s } - if r.FunctionDefinition != nil { - opts.FunctionDefinition = r.FunctionDefinition - } + opts.FunctionDefinition = r.FunctionDefinition return opts } @@ -182,6 +181,7 @@ func (r *CreateFunctionForJavascriptFunctionRequest) toOpts() *CreateFunctionFor s[i] = FunctionArgument{ ArgName: v.ArgName, ArgDataType: v.ArgDataType, + Default: v.Default, } } opts.Arguments = s @@ -213,9 +213,8 @@ func (r *CreateFunctionForJavascriptFunctionRequest) toOpts() *CreateFunctionFor if r.ReturnResultsBehavior != nil { opts.ReturnResultsBehavior = r.ReturnResultsBehavior } - if r.FunctionDefinition != nil { - opts.FunctionDefinition = r.FunctionDefinition - } + + opts.FunctionDefinition = r.FunctionDefinition return opts } @@ -241,6 +240,7 @@ func (r *CreateFunctionForPythonFunctionRequest) toOpts() *CreateFunctionForPyth s[i] = FunctionArgument{ ArgName: v.ArgName, ArgDataType: v.ArgDataType, + Default: v.Default, } } opts.Arguments = s @@ -300,9 +300,7 @@ func (r *CreateFunctionForPythonFunctionRequest) toOpts() *CreateFunctionForPyth } opts.Secrets = s } - if r.FunctionDefinition != nil { - opts.FunctionDefinition = r.FunctionDefinition - } + opts.FunctionDefinition = r.FunctionDefinition return opts } @@ -328,6 +326,7 @@ func (r *CreateFunctionForScalaFunctionRequest) toOpts() *CreateFunctionForScala s[i] = FunctionArgument{ ArgName: v.ArgName, ArgDataType: v.ArgDataType, + Default: v.Default, } } opts.Arguments = s @@ -377,9 +376,7 @@ func (r *CreateFunctionForScalaFunctionRequest) toOpts() *CreateFunctionForScala } opts.Packages = s } - if r.FunctionDefinition != nil { - opts.FunctionDefinition = r.FunctionDefinition - } + opts.FunctionDefinition = r.FunctionDefinition return opts } @@ -402,6 +399,7 @@ func (r *CreateFunctionForSQLFunctionRequest) toOpts() *CreateFunctionForSQLFunc s[i] = FunctionArgument{ ArgName: v.ArgName, ArgDataType: v.ArgDataType, + Default: v.Default, } } opts.Arguments = s @@ -430,9 +428,7 @@ func (r *CreateFunctionForSQLFunctionRequest) toOpts() *CreateFunctionForSQLFunc if r.ReturnResultsBehavior != nil { opts.ReturnResultsBehavior = r.ReturnResultsBehavior } - if r.FunctionDefinition != nil { - opts.FunctionDefinition = r.FunctionDefinition - } + opts.FunctionDefinition = r.FunctionDefinition return opts } @@ -503,10 +499,16 @@ func (r functionRow) convert() *Function { CreatedOn: r.CreatedOn, Name: r.Name, SchemaName: r.SchemaName, + IsBuiltIn: r.IsBuiltIn == "Y", + IsAggregate: r.IsAggregate == "Y", + IsAnsi: r.IsAnsi == "Y", MinNumArguments: r.MinNumArguments, MaxNumArguments: r.MaxNumArguments, Arguments: r.Arguments, + Description: r.Description, + CatalogName: r.CatalogName, IsTableFunction: r.IsTableFunction == "Y", + ValidForClustering: r.ValidForClustering == "Y", IsSecure: r.IsSecure == "Y", IsExternalFunction: r.IsExternalFunction == "Y", Language: r.Language, diff --git a/pkg/sdk/functions_validations_gen.go b/pkg/sdk/functions_validations_gen.go index 62a8b8fd78..0ec3c77252 100644 --- a/pkg/sdk/functions_validations_gen.go +++ b/pkg/sdk/functions_validations_gen.go @@ -101,6 +101,9 @@ func (opts *AlterFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } + if !exactlyOneValueSet(opts.Set, opts.Unset, opts.SetTags, opts.UnsetTags, opts.RenameTo) { + errs = append(errs, errExactlyOneOf("Set", "Unset", "SetTags", "UnsetTags", "RenameTo")) + } return JoinErrors(errs...) } diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index f0f4819667..6c7a210b39 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -18,13 +18,6 @@ func TestInt_CreateFunctions(t *testing.T) { client := testClient(t) ctx := context.Background() - _, warehouseCleanup := createWarehouse(t, client) - t.Cleanup(warehouseCleanup) - databaseTest, databaseCleanup := createDatabase(t, client) - t.Cleanup(databaseCleanup) - schemaTest, schemaCleanup := createSchema(t, client, databaseTest) - t.Cleanup(schemaCleanup) - cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, argumentTypes []sdk.DataType) func() { return func() { es := []sdk.FunctionArgumentTypeRequest{} @@ -41,7 +34,7 @@ func TestInt_CreateFunctions(t *testing.T) { t.Run("create function for Java", func(t *testing.T) { name := "echo_varchar" - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) definition := ` class TestFunc { @@ -51,15 +44,12 @@ func TestInt_CreateFunctions(t *testing.T) { }` target := fmt.Sprintf("@~/tf-%d.jar", time.Now().Unix()) returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVARCHAR) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR) - request := sdk.NewCreateFunctionForJavaFunctionRequest(id). + argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR).WithDefault(sdk.String("abc")) + request := sdk.NewCreateFunctionForJavaFunctionRequest(id, returnsRequest, "TestFunc.echoVarchar", definition). WithOrReplace(sdk.Bool(true)). WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput). - WithReturns(returnsRequest). - WithHandler(sdk.String("TestFunc.echoVarchar")). - WithTargetPath(&target). - WithFunctionDefinition(&definition) + WithTargetPath(&target) err := client.Functions.CreateFunctionForJava(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"VARCHAR"})) @@ -71,7 +61,7 @@ func TestInt_CreateFunctions(t *testing.T) { t.Run("create function for Javascript", func(t *testing.T) { name := "js_factorial" - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) definition := ` if (D <= 0) { @@ -86,12 +76,10 @@ func TestInt_CreateFunctions(t *testing.T) { ` returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("d").WithArgDataType(sdk.DataTypeFloat) - request := sdk.NewCreateFunctionForJavascriptFunctionRequest(id). + request := sdk.NewCreateFunctionForJavascriptFunctionRequest(id, returnsRequest, definition). WithOrReplace(sdk.Bool(true)). WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput). - WithReturns(returnsRequest). - WithFunctionDefinition(&definition) + WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput) err := client.Functions.CreateFunctionForJavascript(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) @@ -103,7 +91,7 @@ func TestInt_CreateFunctions(t *testing.T) { t.Run("create function for Python", func(t *testing.T) { name := random.StringN(8) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) definition := ` def dump(i): @@ -111,13 +99,9 @@ def dump(i): ` returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVariant) argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("i").WithArgDataType(sdk.DataTypeNumber) - request := sdk.NewCreateFunctionForPythonFunctionRequest(id). + request := sdk.NewCreateFunctionForPythonFunctionRequest(id, returnsRequest, "3.8", "dump", definition). WithOrReplace(sdk.Bool(true)). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithReturns(returnsRequest). - WithRuntimeVersion(sdk.String("3.8")). - WithHandler(sdk.String("dump")). - WithFunctionDefinition(&definition) + WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}) err := client.Functions.CreateFunctionForPython(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"int"})) @@ -129,7 +113,7 @@ def dump(i): t.Run("create function for Scala", func(t *testing.T) { name := "echo_varchar" - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) definition := ` class Echo { @@ -140,13 +124,10 @@ def dump(i): ` returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVARCHAR) argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR) - request := sdk.NewCreateFunctionForScalaFunctionRequest(id). + request := sdk.NewCreateFunctionForScalaFunctionRequest(id, returnsRequest, "Echo.echoVarchar", definition). WithOrReplace(sdk.Bool(true)). WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithReturns(returnsRequest). - WithRuntimeVersion(sdk.String("2.12")). - WithHandler(sdk.String("Echo.echoVarchar")). - WithFunctionDefinition(&definition) + WithRuntimeVersion(sdk.String("2.12")) err := client.Functions.CreateFunctionForScala(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"VARCHAR"})) @@ -158,17 +139,15 @@ def dump(i): t.Run("create function for SQL", func(t *testing.T) { name := random.String() - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) definition := "3.141592654::FLOAT" returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeFloat) - request := sdk.NewCreateFunctionForSQLFunctionRequest(id). + request := sdk.NewCreateFunctionForSQLFunctionRequest(id, returnsRequest, definition). WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). WithOrReplace(sdk.Bool(true)). - WithComment(sdk.String("comment")). - WithReturns(returnsRequest). - WithFunctionDefinition(&definition) + WithComment(sdk.String("comment")) err := client.Functions.CreateFunctionForSQL(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"FLOAT"})) @@ -184,13 +163,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { client := testClient(t) ctx := context.Background() - _, warehouseCleanup := createWarehouse(t, client) - t.Cleanup(warehouseCleanup) - databaseTest, databaseCleanup := createDatabase(t, client) - t.Cleanup(databaseCleanup) - schemaTest, schemaCleanup := createSchema(t, client, databaseTest) - t.Cleanup(schemaCleanup) - tagTest, tagCleanup := createTag(t, client, databaseTest, schemaTest) + tagTest, tagCleanup := createTag(t, client, testDb(t), testSchema(t)) t.Cleanup(tagCleanup) cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, argumentTypes []sdk.DataType) func() { @@ -213,12 +186,10 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { definition := "3.141592654::FLOAT" returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeFloat) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.String()) - request := sdk.NewCreateFunctionForSQLFunctionRequest(id). + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, random.String()) + request := sdk.NewCreateFunctionForSQLFunctionRequest(id, returnsRequest, definition). WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithOrReplace(sdk.Bool(true)). - WithReturns(returnsRequest). - WithFunctionDefinition(&definition) + WithOrReplace(sdk.Bool(true)) err := client.Functions.CreateFunctionForSQL(ctx, request) require.NoError(t, err) if cleanup { @@ -236,8 +207,8 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: rename", func(t *testing.T) { f := createFunctionForSQLHandle(t, false) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) - nid := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.String()) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) + nid := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, random.String()) request := sdk.NewAlterFunctionRequest(id).WithRenameTo(&nid).WithArgumentTypes(defaultArgumentTypes) err := client.Functions.Alter(ctx, request) if err != nil { @@ -258,7 +229,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: set log level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) set := sdk.NewFunctionSetRequest().WithLogLevel(sdk.String("DEBUG")) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) err := client.Functions.Alter(ctx, request) @@ -268,7 +239,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: unset log level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) unset := sdk.NewFunctionUnsetRequest().WithLogLevel(sdk.Bool(true)) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) err := client.Functions.Alter(ctx, request) @@ -278,7 +249,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: set trace level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) set := sdk.NewFunctionSetRequest().WithTraceLevel(sdk.String("ALWAYS")) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) err := client.Functions.Alter(ctx, request) @@ -288,7 +259,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: unset trace level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) unset := sdk.NewFunctionUnsetRequest().WithTraceLevel(sdk.Bool(true)) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) err := client.Functions.Alter(ctx, request) @@ -298,7 +269,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: set comment", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) set := sdk.NewFunctionSetRequest().WithComment(sdk.String("comment")) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) err := client.Functions.Alter(ctx, request) @@ -308,7 +279,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: unset comment", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) unset := sdk.NewFunctionUnsetRequest().WithComment(sdk.Bool(true)) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) err := client.Functions.Alter(ctx, request) @@ -318,7 +289,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: set secure", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) set := sdk.NewFunctionSetRequest().WithSecure(sdk.Bool(true)) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) err := client.Functions.Alter(ctx, request) @@ -332,7 +303,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: unset secure", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) unset := sdk.NewFunctionUnsetRequest().WithSecure(sdk.Bool(true)) request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) err := client.Functions.Alter(ctx, request) @@ -346,7 +317,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: set and unset tags", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) setTags := []sdk.TagAssociation{ { Name: tagTest.ID(), @@ -397,7 +368,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("describe function for SQL", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) request := sdk.NewDescribeFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes) details, err := client.Functions.Describe(ctx, request) From 17ef49f54533d3cbd08bf2147ba5f4c8eecb7a07 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 4 Dec 2023 23:01:00 -0800 Subject: [PATCH 03/10] update functions --- pkg/sdk/common_types.go | 39 ++ pkg/sdk/functions_def.go | 296 ++++--------- pkg/sdk/functions_dto_builders_gen.go | 406 ++++++++--------- pkg/sdk/functions_dto_gen.go | 161 ++++--- pkg/sdk/functions_gen.go | 366 +++++++--------- pkg/sdk/functions_gen_test.go | 302 +++++++------ pkg/sdk/functions_impl_gen.go | 408 +++++++----------- pkg/sdk/functions_validations_gen.go | 104 +++-- pkg/sdk/poc/generator/keyword_builders.go | 4 +- pkg/sdk/testint/functions_integration_test.go | 315 +++++++------- 10 files changed, 1106 insertions(+), 1295 deletions(-) diff --git a/pkg/sdk/common_types.go b/pkg/sdk/common_types.go index 57a4f862e0..b2a41d1e80 100644 --- a/pkg/sdk/common_types.go +++ b/pkg/sdk/common_types.go @@ -207,3 +207,42 @@ func (row *propertyRow) toBoolProperty() *BoolProperty { Description: row.Description, } } + +type NullInputBehavior string + +func NullInputBehaviorPointer(v NullInputBehavior) *NullInputBehavior { + return &v +} + +const ( + NullInputBehaviorCalledOnNullInput NullInputBehavior = "CALLED ON NULL INPUT" + NullInputBehaviorReturnNullInput NullInputBehavior = "RETURN NULL ON NULL INPUT" + NullInputBehaviorStrict NullInputBehavior = "STRICT" +) + +type ReturnResultsBehavior string + +var ( + ReturnResultsBehaviorVolatile ReturnResultsBehavior = "VOLATILE" + ReturnResultsBehaviorImmutable ReturnResultsBehavior = "IMMUTABLE" +) + +func ReturnResultsBehaviorPointer(v ReturnResultsBehavior) *ReturnResultsBehavior { + return &v +} + +type ReturnNullValues string + +var ( + ReturnNullValuesNull ReturnNullValues = "NULL" + ReturnNullValuesNotNull ReturnNullValues = "NOT NULL" +) + +func ReturnNullValuesPointer(v ReturnNullValues) *ReturnNullValues { + return &v +} + +type Secret struct { + VariableName string `ddl:"keyword,single_quotes"` + Name string `ddl:"parameter,no_quotes"` +} diff --git a/pkg/sdk/functions_def.go b/pkg/sdk/functions_def.go index 0d9fed671a..55f7250820 100644 --- a/pkg/sdk/functions_def.go +++ b/pkg/sdk/functions_def.go @@ -5,24 +5,22 @@ import g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/gen //go:generate go run ./poc/main.go var functionArgument = g.NewQueryStruct("FunctionArgument"). - Text("ArgName", g.KeywordOptions().NoQuotes()). - Text("ArgDataType", g.KeywordOptions().NoQuotes()). - OptionalTextAssignment("Default", g.ParameterOptions().SingleQuotes()) - -var functionArgumentType = g.NewQueryStruct("FunctionArgumentType"). - Text("ArgDataType", g.KeywordOptions().NoQuotes()) + Text("ArgName", g.KeywordOptions().NoQuotes().Required()). + PredefinedQueryStructField("ArgDataType", "DataType", g.KeywordOptions().NoQuotes().Required()). + PredefinedQueryStructField("DefaultValue", "*string", g.ParameterOptions().NoEquals().SQL("DEFAULT")) var functionColumn = g.NewQueryStruct("FunctionColumn"). - Text("ColumnName", g.KeywordOptions().NoQuotes()). - Text("ColumnDataType", g.KeywordOptions().NoQuotes()) - -var functionSecret = g.NewQueryStruct("FunctionSecret"). - Text("SecretVariableName", g.KeywordOptions().SingleQuotes()). - Text("SecretName", g.KeywordOptions().NoQuotes()) + Text("ColumnName", g.KeywordOptions().NoQuotes().Required()). + PredefinedQueryStructField("ColumnDataType", "DataType", g.KeywordOptions().NoQuotes().Required()) var functionReturns = g.NewQueryStruct("FunctionReturns"). - OptionalText("ResultDataType", g.KeywordOptions()). - QueryStructField( + OptionalQueryStructField( + "ResultDataType", + g.NewQueryStruct("FunctionReturnsResultDataType"). + PredefinedQueryStructField("ResultDataType", "DataType", g.KeywordOptions().NoQuotes().Required()), + g.KeywordOptions(), + ). + OptionalQueryStructField( "Table", g.NewQueryStruct("FunctionReturnsTable"). ListQueryStructField( @@ -31,27 +29,11 @@ var functionReturns = g.NewQueryStruct("FunctionReturns"). g.ParameterOptions().Parentheses().NoEquals(), ), g.KeywordOptions().SQL("TABLE"), - ) - -var functionSet = g.NewQueryStruct("FunctionSet"). - OptionalTextAssignment("LOG_LEVEL", g.ParameterOptions().SingleQuotes()). - OptionalTextAssignment("TRACE_LEVEL", g.ParameterOptions().SingleQuotes()). - OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - OptionalSQL("SECURE") - -var functionUnset = g.NewQueryStruct("FunctionUnset"). - OptionalSQL("SECURE"). - OptionalSQL("COMMENT"). - OptionalSQL("LOG_LEVEL"). - OptionalSQL("TRACE_LEVEL") + ).WithValidation(g.ExactlyOneValueSet, "ResultDataType", "Table") var ( - functionNullOrNot = g.NewQueryStruct("FunctionNullOrNot").OptionalSQL("NULL").OptionalSQL("NOT NULL") - functionStrictOrNot = g.NewQueryStruct("FunctionStrictOrNot").OptionalSQL("STRICT").OptionalSQL("CALLED ON NULL INPUT") - functionVolatileOrNot = g.NewQueryStruct("FunctionVolatileOrNot").OptionalSQL("VOLATILE").OptionalSQL("IMMUTABLE") - functionImports = g.NewQueryStruct("FunctionImports").Text("Import", g.KeywordOptions().SingleQuotes()) - functionPackages = g.NewQueryStruct("FunctionPackages").Text("Package", g.KeywordOptions().SingleQuotes()) - functionDefinition = g.NewQueryStruct("FunctionDefinition").Text("Definition", g.KeywordOptions()) + functionImports = g.NewQueryStruct("FunctionImports").Text("Import", g.KeywordOptions().SingleQuotes()) + functionPackages = g.NewQueryStruct("FunctionPackages").Text("Package", g.KeywordOptions().SingleQuotes()) ) var FunctionsDef = g.NewInterface( @@ -59,9 +41,9 @@ var FunctionsDef = g.NewInterface( "Function", g.KindOfT[SchemaObjectIdentifier](), ).CustomOperation( - "CreateFunctionForJava", - "https://docs.snowflake.com/en/sql-reference/sql/create-function", - g.NewQueryStruct("CreateFunctionForJava"). + "CreateForJava", + "https://docs.snowflake.com/en/sql-reference/sql/create-function#java-handler", + g.NewQueryStruct("CreateForJava"). Create(). OrReplace(). OptionalSQL("TEMPORARY"). @@ -77,25 +59,13 @@ var FunctionsDef = g.NewInterface( QueryStructField( "Returns", functionReturns, - g.KeywordOptions().SQL("RETURNS"), - ). - OptionalQueryStructField( - "NullOrNot", - functionNullOrNot, - g.KeywordOptions(), + g.KeywordOptions().SQL("RETURNS").Required(), ). + PredefinedQueryStructField("ReturnNullValues", "*ReturnNullValues", g.KeywordOptions()). SQL("LANGUAGE JAVA"). - OptionalQueryStructField( - "StrictOrNot", - functionStrictOrNot, - g.KeywordOptions(), - ). - OptionalQueryStructField( - "VolatileOrNot", - functionVolatileOrNot, - g.KeywordOptions(), - ). - TextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). + PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()). + PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). + OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). ListQueryStructField( "Imports", @@ -107,30 +77,22 @@ var FunctionsDef = g.NewInterface( functionPackages, g.ParameterOptions().Parentheses().SQL("PACKAGES"), ). - TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes().Required()). ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()). - ListQueryStructField( - "Secrets", - functionSecret, - g.ParameterOptions().Parentheses().SQL("SECRETS"), - ). + ListAssignment("SECRETS", "Secret", g.ParameterOptions().Parentheses()). OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). - QueryStructField( - "FunctionDefinition", - functionDefinition, - g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), - ). - WithValidation(g.ValidIdentifier, "name"), + PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")). + WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"), ).CustomOperation( - "CreateFunctionForJavascript", - "https://docs.snowflake.com/en/sql-reference/sql/create-function", - g.NewQueryStruct("CreateFunctionForJavascript"). + "CreateForJavascript", + "https://docs.snowflake.com/en/sql-reference/sql/create-function#javascript-handler", + g.NewQueryStruct("CreateForJavascript"). Create(). OrReplace(). OptionalSQL("TEMPORARY"). OptionalSQL("SECURE"). SQL("FUNCTION"). - IfNotExists(). Name(). ListQueryStructField( "Arguments", @@ -140,35 +102,19 @@ var FunctionsDef = g.NewInterface( QueryStructField( "Returns", functionReturns, - g.KeywordOptions().SQL("RETURNS"), - ). - OptionalQueryStructField( - "NullOrNot", - functionNullOrNot, - g.KeywordOptions(), + g.KeywordOptions().SQL("RETURNS").Required(), ). + PredefinedQueryStructField("ReturnNullValues", "*ReturnNullValues", g.KeywordOptions()). SQL("LANGUAGE JAVASCRIPT"). - OptionalQueryStructField( - "StrictOrNot", - functionStrictOrNot, - g.KeywordOptions(), - ). - OptionalQueryStructField( - "VolatileOrNot", - functionVolatileOrNot, - g.KeywordOptions(), - ). + PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()). + PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - QueryStructField( - "FunctionDefinition", - functionDefinition, - g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), - ). + PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()). WithValidation(g.ValidIdentifier, "name"), ).CustomOperation( - "CreateFunctionForPython", - "https://docs.snowflake.com/en/sql-reference/sql/create-function", - g.NewQueryStruct("CreateFunctionForPython"). + "CreateForPython", + "https://docs.snowflake.com/en/sql-reference/sql/create-function#python-handler", + g.NewQueryStruct("CreateForPython"). Create(). OrReplace(). OptionalSQL("TEMPORARY"). @@ -184,25 +130,13 @@ var FunctionsDef = g.NewInterface( QueryStructField( "Returns", functionReturns, - g.KeywordOptions().SQL("RETURNS"), - ). - OptionalQueryStructField( - "NullOrNot", - functionNullOrNot, - g.KeywordOptions(), + g.KeywordOptions().SQL("RETURNS").Required(), ). + PredefinedQueryStructField("ReturnNullValues", "*ReturnNullValues", g.KeywordOptions()). SQL("LANGUAGE PYTHON"). - OptionalQueryStructField( - "StrictOrNot", - functionStrictOrNot, - g.KeywordOptions(), - ). - OptionalQueryStructField( - "VolatileOrNot", - functionVolatileOrNot, - g.KeywordOptions(), - ). - OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). + PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()). + PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). + TextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes().Required()). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). ListQueryStructField( "Imports", @@ -214,23 +148,16 @@ var FunctionsDef = g.NewInterface( functionPackages, g.ParameterOptions().Parentheses().SQL("PACKAGES"), ). - TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes().Required()). ListAssignment("EXTERNAL_ACCESS_INTEGRATIONS", "AccountObjectIdentifier", g.ParameterOptions().Parentheses()). - ListQueryStructField( - "Secrets", - functionSecret, - g.ParameterOptions().Parentheses().SQL("SECRETS"), - ). - QueryStructField( - "FunctionDefinition", - functionDefinition, - g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), - ). - WithValidation(g.ValidIdentifier, "name"), + ListAssignment("SECRETS", "Secret", g.ParameterOptions().Parentheses()). + PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")). + WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"), ).CustomOperation( - "CreateFunctionForScala", - "https://docs.snowflake.com/en/sql-reference/sql/create-function", - g.NewQueryStruct("CreateFunctionForScala"). + "CreateForScala", + "https://docs.snowflake.com/en/sql-reference/sql/create-function#scala-handler", + g.NewQueryStruct("CreateForScala"). Create(). OrReplace(). OptionalSQL("TEMPORARY"). @@ -243,27 +170,11 @@ var FunctionsDef = g.NewInterface( functionArgument, g.ParameterOptions().Parentheses().NoEquals()). OptionalSQL("COPY GRANTS"). - QueryStructField( - "Returns", - functionReturns, - g.KeywordOptions().SQL("RETURNS"), - ). - OptionalQueryStructField( - "NullOrNot", - functionNullOrNot, - g.KeywordOptions(), - ). + PredefinedQueryStructField("ResultDataType", "DataType", g.ParameterOptions().NoEquals().SQL("RETURNS").Required()). + PredefinedQueryStructField("ReturnNullValues", "*ReturnNullValues", g.KeywordOptions()). SQL("LANGUAGE SCALA"). - OptionalQueryStructField( - "StrictOrNot", - functionStrictOrNot, - g.KeywordOptions(), - ). - OptionalQueryStructField( - "VolatileOrNot", - functionVolatileOrNot, - g.KeywordOptions(), - ). + PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()). + PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). OptionalTextAssignment("RUNTIME_VERSION", g.ParameterOptions().SingleQuotes()). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). ListQueryStructField( @@ -276,24 +187,20 @@ var FunctionsDef = g.NewInterface( functionPackages, g.ParameterOptions().Parentheses().SQL("PACKAGES"), ). - TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes()). + TextAssignment("HANDLER", g.ParameterOptions().SingleQuotes().Required()). OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). - QueryStructField( - "FunctionDefinition", - functionDefinition, - g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), - ). - WithValidation(g.ValidIdentifier, "name"), + PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")). + WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"), ).CustomOperation( - "CreateFunctionForSQL", - "https://docs.snowflake.com/en/sql-reference/sql/create-function", - g.NewQueryStruct("CreateFunctionForSQL"). + "CreateForSQL", + "https://docs.snowflake.com/en/sql-reference/sql/create-function#sql-handler", + g.NewQueryStruct("CreateForSQL"). Create(). OrReplace(). OptionalSQL("TEMPORARY"). OptionalSQL("SECURE"). SQL("FUNCTION"). - IfNotExists(). Name(). ListQueryStructField( "Arguments", @@ -303,25 +210,13 @@ var FunctionsDef = g.NewInterface( QueryStructField( "Returns", functionReturns, - g.KeywordOptions().SQL("RETURNS"), - ). - OptionalQueryStructField( - "NullOrNot", - functionNullOrNot, - g.KeywordOptions(), - ). - OptionalQueryStructField( - "VolatileOrNot", - functionVolatileOrNot, - g.KeywordOptions(), + g.KeywordOptions().SQL("RETURNS").Required(), ). + PredefinedQueryStructField("ReturnNullValues", "*ReturnNullValues", g.KeywordOptions()). + PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). OptionalSQL("MEMOIZABLE"). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - QueryStructField( - "FunctionDefinition", - functionDefinition, - g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS"), - ). + PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()). WithValidation(g.ValidIdentifier, "name"), ).AlterOperation( "https://docs.snowflake.com/en/sql-reference/sql/alter-function", @@ -330,24 +225,21 @@ var FunctionsDef = g.NewInterface( SQL("FUNCTION"). IfExists(). Name(). - ListQueryStructField( - "ArgumentTypes", - functionArgumentType, - g.ParameterOptions().Parentheses().NoEquals()). - OptionalQueryStructField( - "Set", - functionSet, - g.KeywordOptions().SQL("SET"), - ). - OptionalQueryStructField( - "Unset", - functionUnset, - g.KeywordOptions().SQL("UNSET"), - ). + PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()). Identifier("RenameTo", g.KindOfTPointer[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")). - SetTags().UnsetTags(). + OptionalTextAssignment("SET COMMENT", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("SET LOG_LEVEL", g.ParameterOptions().SingleQuotes()). + OptionalTextAssignment("SET TRACE_LEVEL", g.ParameterOptions().SingleQuotes()). + OptionalSQL("SET SECURE"). + OptionalSQL("UNSET SECURE"). + OptionalSQL("UNSET LOG_LEVEL"). + OptionalSQL("UNSET TRACE_LEVEL"). + OptionalSQL("UNSET COMMENT"). + OptionalSetTags(). + OptionalUnsetTags(). WithValidation(g.ValidIdentifier, "name"). - WithValidation(g.ExactlyOneValueSet, "Set", "Unset", "SetTags", "UnsetTags", "RenameTo"), + WithValidation(g.ValidIdentifierIfSet, "RenameTo"). + WithValidation(g.ExactlyOneValueSet, "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "SetSecure", "UnsetLogLevel", "UnsetTraceLevel", "UnsetSecure", "UnsetComment", "SetTags", "UnsetTags"), ).DropOperation( "https://docs.snowflake.com/en/sql-reference/sql/drop-function", g.NewQueryStruct("DropFunction"). @@ -355,10 +247,7 @@ var FunctionsDef = g.NewInterface( SQL("FUNCTION"). IfExists(). Name(). - ListQueryStructField( - "ArgumentTypes", - functionArgumentType, - g.ParameterOptions().Parentheses().NoEquals()). + PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()). WithValidation(g.ValidIdentifier, "name"), ).ShowOperation( "https://docs.snowflake.com/en/sql-reference/sql/show-user-functions", @@ -366,20 +255,20 @@ var FunctionsDef = g.NewInterface( Field("created_on", "string"). Field("name", "string"). Field("schema_name", "string"). - Field("is_builtin", "bool"). - Field("is_aggregate", "bool"). - Field("is_ansi", "bool"). + Field("is_builtin", "string"). + Field("is_aggregate", "string"). + Field("is_ansi", "string"). Field("min_num_arguments", "int"). Field("max_num_arguments", "int"). Field("arguments", "string"). Field("description", "string"). Field("catalog_name", "string"). - Field("is_table_function", "bool"). - Field("valid_for_clustering", "bool"). - Field("is_secure", "string"). + Field("is_table_function", "string"). + Field("valid_for_clustering", "string"). + Field("is_secure", "sql.NullString"). Field("is_external_function", "string"). Field("language", "string"). - Field("is_memoizable", "string"), + Field("is_memoizable", "sql.NullString"), g.PlainStruct("Function"). Field("CreatedOn", "string"). Field("Name", "string"). @@ -390,6 +279,8 @@ var FunctionsDef = g.NewInterface( Field("MinNumArguments", "int"). Field("MaxNumArguments", "int"). Field("Arguments", "string"). + Field("Description", "string"). + Field("CatalogName", "string"). Field("IsTableFunction", "bool"). Field("ValidForClustering", "bool"). Field("IsSecure", "bool"). @@ -401,7 +292,7 @@ var FunctionsDef = g.NewInterface( SQL("USER FUNCTIONS"). OptionalLike(). OptionalIn(), -).DescribeOperation( +).ShowByIdOperation().DescribeOperation( g.DescriptionMappingKindSlice, "https://docs.snowflake.com/en/sql-reference/sql/desc-function", g.DbStruct("functionDetailRow"). @@ -414,9 +305,6 @@ var FunctionsDef = g.NewInterface( Describe(). SQL("FUNCTION"). Name(). - ListQueryStructField( - "ArgumentTypes", - functionArgumentType, - g.ParameterOptions().Parentheses().NoEquals()). + PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()). WithValidation(g.ValidIdentifier, "name"), ) diff --git a/pkg/sdk/functions_dto_builders_gen.go b/pkg/sdk/functions_dto_builders_gen.go index e81c4d0430..60fade6825 100644 --- a/pkg/sdk/functions_dto_builders_gen.go +++ b/pkg/sdk/functions_dto_builders_gen.go @@ -4,116 +4,115 @@ package sdk import () -func NewCreateFunctionForJavaFunctionRequest( +func NewCreateForJavaFunctionRequest( name SchemaObjectIdentifier, - returns *FunctionReturnsRequest, - handler string, - functionDefinition string, -) *CreateFunctionForJavaFunctionRequest { - s := CreateFunctionForJavaFunctionRequest{} + Returns FunctionReturnsRequest, + Handler string, +) *CreateForJavaFunctionRequest { + s := CreateForJavaFunctionRequest{} s.name = name - s.Returns = returns - s.Handler = handler - s.FunctionDefinition = functionDefinition + s.Returns = Returns + s.Handler = Handler return &s } -func (s *CreateFunctionForJavaFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithOrReplace(OrReplace *bool) *CreateForJavaFunctionRequest { s.OrReplace = OrReplace return s } -func (s *CreateFunctionForJavaFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithTemporary(Temporary *bool) *CreateForJavaFunctionRequest { s.Temporary = Temporary return s } -func (s *CreateFunctionForJavaFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithSecure(Secure *bool) *CreateForJavaFunctionRequest { s.Secure = Secure return s } -func (s *CreateFunctionForJavaFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateForJavaFunctionRequest { s.IfNotExists = IfNotExists return s } -func (s *CreateFunctionForJavaFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateForJavaFunctionRequest { s.Arguments = Arguments return s } -func (s *CreateFunctionForJavaFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateForJavaFunctionRequest { s.CopyGrants = CopyGrants return s } -func (s *CreateFunctionForJavaFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForJavaFunctionRequest { - s.ReturnNullValues = &ReturnNullValues +func (s *CreateForJavaFunctionRequest) WithReturnNullValues(ReturnNullValues *ReturnNullValues) *CreateForJavaFunctionRequest { + s.ReturnNullValues = ReturnNullValues return s } -func (s *CreateFunctionForJavaFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForJavaFunctionRequest { - s.NullInputBehavior = &NullInputBehavior +func (s *CreateForJavaFunctionRequest) WithNullInputBehavior(NullInputBehavior *NullInputBehavior) *CreateForJavaFunctionRequest { + s.NullInputBehavior = NullInputBehavior return s } -func (s *CreateFunctionForJavaFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForJavaFunctionRequest { - s.ReturnResultsBehavior = &ReturnResultsBehavior +func (s *CreateForJavaFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior *ReturnResultsBehavior) *CreateForJavaFunctionRequest { + s.ReturnResultsBehavior = ReturnResultsBehavior return s } -func (s *CreateFunctionForJavaFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateForJavaFunctionRequest { s.RuntimeVersion = RuntimeVersion return s } -func (s *CreateFunctionForJavaFunctionRequest) WithComment(Comment *string) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithComment(Comment *string) *CreateForJavaFunctionRequest { s.Comment = Comment return s } -func (s *CreateFunctionForJavaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateForJavaFunctionRequest { s.Imports = Imports return s } -func (s *CreateFunctionForJavaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateForJavaFunctionRequest { s.Packages = Packages return s } -func (s *CreateFunctionForJavaFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateForJavaFunctionRequest { s.ExternalAccessIntegrations = ExternalAccessIntegrations return s } -func (s *CreateFunctionForJavaFunctionRequest) WithSecrets(Secrets []FunctionSecretRequest) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithSecrets(Secrets []Secret) *CreateForJavaFunctionRequest { s.Secrets = Secrets return s } -func (s *CreateFunctionForJavaFunctionRequest) WithTargetPath(TargetPath *string) *CreateFunctionForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithTargetPath(TargetPath *string) *CreateForJavaFunctionRequest { s.TargetPath = TargetPath return s } -func NewFunctionArgumentRequest() *FunctionArgumentRequest { - return &FunctionArgumentRequest{} -} - -func (s *FunctionArgumentRequest) WithArgName(ArgName string) *FunctionArgumentRequest { - s.ArgName = ArgName +func (s *CreateForJavaFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateForJavaFunctionRequest { + s.FunctionDefinition = FunctionDefinition return s } -func (s *FunctionArgumentRequest) WithArgDataType(ArgDataType DataType) *FunctionArgumentRequest { +func NewFunctionArgumentRequest( + ArgName string, + ArgDataType DataType, +) *FunctionArgumentRequest { + s := FunctionArgumentRequest{} + s.ArgName = ArgName s.ArgDataType = ArgDataType - return s + return &s } -func (s *FunctionArgumentRequest) WithDefault(Default *string) *FunctionArgumentRequest { - s.Default = Default +func (s *FunctionArgumentRequest) WithDefaultValue(DefaultValue *string) *FunctionArgumentRequest { + s.DefaultValue = DefaultValue return s } @@ -121,8 +120,8 @@ func NewFunctionReturnsRequest() *FunctionReturnsRequest { return &FunctionReturnsRequest{} } -func (s *FunctionReturnsRequest) WithResultDataType(ResultDataType DataType) *FunctionReturnsRequest { - s.ResultDataType = &ResultDataType +func (s *FunctionReturnsRequest) WithResultDataType(ResultDataType *FunctionReturnsResultDataTypeRequest) *FunctionReturnsRequest { + s.ResultDataType = ResultDataType return s } @@ -131,6 +130,14 @@ func (s *FunctionReturnsRequest) WithTable(Table *FunctionReturnsTableRequest) * return s } +func NewFunctionReturnsResultDataTypeRequest( + ResultDataType DataType, +) *FunctionReturnsResultDataTypeRequest { + s := FunctionReturnsResultDataTypeRequest{} + s.ResultDataType = ResultDataType + return &s +} + func NewFunctionReturnsTableRequest() *FunctionReturnsTableRequest { return &FunctionReturnsTableRequest{} } @@ -140,18 +147,14 @@ func (s *FunctionReturnsTableRequest) WithColumns(Columns []FunctionColumnReques return s } -func NewFunctionColumnRequest() *FunctionColumnRequest { - return &FunctionColumnRequest{} -} - -func (s *FunctionColumnRequest) WithColumnName(ColumnName string) *FunctionColumnRequest { +func NewFunctionColumnRequest( + ColumnName string, + ColumnDataType DataType, +) *FunctionColumnRequest { + s := FunctionColumnRequest{} s.ColumnName = ColumnName - return s -} - -func (s *FunctionColumnRequest) WithColumnDataType(ColumnDataType DataType) *FunctionColumnRequest { s.ColumnDataType = ColumnDataType - return s + return &s } func NewFunctionImportsRequest() *FunctionImportsRequest { @@ -172,318 +175,303 @@ func (s *FunctionPackagesRequest) WithPackage(Package string) *FunctionPackagesR return s } -func NewFunctionSecretRequest() *FunctionSecretRequest { - return &FunctionSecretRequest{} -} - -func (s *FunctionSecretRequest) WithSecretVariableName(SecretVariableName string) *FunctionSecretRequest { - s.SecretVariableName = SecretVariableName - return s -} - -func (s *FunctionSecretRequest) WithSecretName(SecretName string) *FunctionSecretRequest { - s.SecretName = SecretName - return s -} - -func NewCreateFunctionForJavascriptFunctionRequest( +func NewCreateForJavascriptFunctionRequest( name SchemaObjectIdentifier, - returns *FunctionReturnsRequest, - functionDefinition string, -) *CreateFunctionForJavascriptFunctionRequest { - s := CreateFunctionForJavascriptFunctionRequest{} + Returns FunctionReturnsRequest, + FunctionDefinition *string, +) *CreateForJavascriptFunctionRequest { + s := CreateForJavascriptFunctionRequest{} s.name = name - s.FunctionDefinition = functionDefinition + s.Returns = Returns + s.FunctionDefinition = FunctionDefinition return &s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForJavascriptFunctionRequest { +func (s *CreateForJavascriptFunctionRequest) WithOrReplace(OrReplace *bool) *CreateForJavascriptFunctionRequest { s.OrReplace = OrReplace return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForJavascriptFunctionRequest { +func (s *CreateForJavascriptFunctionRequest) WithTemporary(Temporary *bool) *CreateForJavascriptFunctionRequest { s.Temporary = Temporary return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForJavascriptFunctionRequest { +func (s *CreateForJavascriptFunctionRequest) WithSecure(Secure *bool) *CreateForJavascriptFunctionRequest { s.Secure = Secure return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForJavascriptFunctionRequest { - s.IfNotExists = IfNotExists - return s -} - -func (s *CreateFunctionForJavascriptFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForJavascriptFunctionRequest { +func (s *CreateForJavascriptFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateForJavascriptFunctionRequest { s.Arguments = Arguments return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForJavascriptFunctionRequest { +func (s *CreateForJavascriptFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateForJavascriptFunctionRequest { s.CopyGrants = CopyGrants return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForJavascriptFunctionRequest { - s.ReturnNullValues = &ReturnNullValues +func (s *CreateForJavascriptFunctionRequest) WithReturnNullValues(ReturnNullValues *ReturnNullValues) *CreateForJavascriptFunctionRequest { + s.ReturnNullValues = ReturnNullValues return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForJavascriptFunctionRequest { - s.NullInputBehavior = &NullInputBehavior +func (s *CreateForJavascriptFunctionRequest) WithNullInputBehavior(NullInputBehavior *NullInputBehavior) *CreateForJavascriptFunctionRequest { + s.NullInputBehavior = NullInputBehavior return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForJavascriptFunctionRequest { - s.ReturnResultsBehavior = &ReturnResultsBehavior +func (s *CreateForJavascriptFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior *ReturnResultsBehavior) *CreateForJavascriptFunctionRequest { + s.ReturnResultsBehavior = ReturnResultsBehavior return s } -func (s *CreateFunctionForJavascriptFunctionRequest) WithComment(Comment *string) *CreateFunctionForJavascriptFunctionRequest { +func (s *CreateForJavascriptFunctionRequest) WithComment(Comment *string) *CreateForJavascriptFunctionRequest { s.Comment = Comment return s } -func NewCreateFunctionForPythonFunctionRequest( +func NewCreateForPythonFunctionRequest( name SchemaObjectIdentifier, - returns *FunctionReturnsRequest, - runtimeVersion string, - handler string, - functionDefinition string, -) *CreateFunctionForPythonFunctionRequest { - s := CreateFunctionForPythonFunctionRequest{} + Returns FunctionReturnsRequest, + RuntimeVersion string, + Handler string, +) *CreateForPythonFunctionRequest { + s := CreateForPythonFunctionRequest{} s.name = name - s.Returns = returns - s.RuntimeVersion = runtimeVersion - s.Handler = handler - s.FunctionDefinition = functionDefinition + s.Returns = Returns + s.RuntimeVersion = RuntimeVersion + s.Handler = Handler return &s } -func (s *CreateFunctionForPythonFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithOrReplace(OrReplace *bool) *CreateForPythonFunctionRequest { s.OrReplace = OrReplace return s } -func (s *CreateFunctionForPythonFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithTemporary(Temporary *bool) *CreateForPythonFunctionRequest { s.Temporary = Temporary return s } -func (s *CreateFunctionForPythonFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithSecure(Secure *bool) *CreateForPythonFunctionRequest { s.Secure = Secure return s } -func (s *CreateFunctionForPythonFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateForPythonFunctionRequest { s.IfNotExists = IfNotExists return s } -func (s *CreateFunctionForPythonFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateForPythonFunctionRequest { s.Arguments = Arguments return s } -func (s *CreateFunctionForPythonFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateForPythonFunctionRequest { s.CopyGrants = CopyGrants return s } -func (s *CreateFunctionForPythonFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForPythonFunctionRequest { - s.ReturnNullValues = &ReturnNullValues +func (s *CreateForPythonFunctionRequest) WithReturnNullValues(ReturnNullValues *ReturnNullValues) *CreateForPythonFunctionRequest { + s.ReturnNullValues = ReturnNullValues return s } -func (s *CreateFunctionForPythonFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForPythonFunctionRequest { - s.NullInputBehavior = &NullInputBehavior +func (s *CreateForPythonFunctionRequest) WithNullInputBehavior(NullInputBehavior *NullInputBehavior) *CreateForPythonFunctionRequest { + s.NullInputBehavior = NullInputBehavior return s } -func (s *CreateFunctionForPythonFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForPythonFunctionRequest { - s.ReturnResultsBehavior = &ReturnResultsBehavior +func (s *CreateForPythonFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior *ReturnResultsBehavior) *CreateForPythonFunctionRequest { + s.ReturnResultsBehavior = ReturnResultsBehavior return s } -func (s *CreateFunctionForPythonFunctionRequest) WithComment(Comment *string) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithComment(Comment *string) *CreateForPythonFunctionRequest { s.Comment = Comment return s } -func (s *CreateFunctionForPythonFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateForPythonFunctionRequest { s.Imports = Imports return s } -func (s *CreateFunctionForPythonFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateForPythonFunctionRequest { s.Packages = Packages return s } -func (s *CreateFunctionForPythonFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithExternalAccessIntegrations(ExternalAccessIntegrations []AccountObjectIdentifier) *CreateForPythonFunctionRequest { s.ExternalAccessIntegrations = ExternalAccessIntegrations return s } -func (s *CreateFunctionForPythonFunctionRequest) WithSecrets(Secrets []FunctionSecretRequest) *CreateFunctionForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithSecrets(Secrets []Secret) *CreateForPythonFunctionRequest { s.Secrets = Secrets return s } -func NewCreateFunctionForScalaFunctionRequest( +func (s *CreateForPythonFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateForPythonFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewCreateForScalaFunctionRequest( name SchemaObjectIdentifier, - returns *FunctionReturnsRequest, - handler string, - functionDefinition string, -) *CreateFunctionForScalaFunctionRequest { - s := CreateFunctionForScalaFunctionRequest{} + ResultDataType DataType, + Handler string, +) *CreateForScalaFunctionRequest { + s := CreateForScalaFunctionRequest{} s.name = name - s.Returns = returns - s.Handler = handler - s.FunctionDefinition = functionDefinition + s.ResultDataType = ResultDataType + s.Handler = Handler return &s } -func (s *CreateFunctionForScalaFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithOrReplace(OrReplace *bool) *CreateForScalaFunctionRequest { s.OrReplace = OrReplace return s } -func (s *CreateFunctionForScalaFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithTemporary(Temporary *bool) *CreateForScalaFunctionRequest { s.Temporary = Temporary return s } -func (s *CreateFunctionForScalaFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithSecure(Secure *bool) *CreateForScalaFunctionRequest { s.Secure = Secure return s } -func (s *CreateFunctionForScalaFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateForScalaFunctionRequest { s.IfNotExists = IfNotExists return s } -func (s *CreateFunctionForScalaFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateForScalaFunctionRequest { s.Arguments = Arguments return s } -func (s *CreateFunctionForScalaFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateForScalaFunctionRequest { s.CopyGrants = CopyGrants return s } -func (s *CreateFunctionForScalaFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForScalaFunctionRequest { - s.ReturnNullValues = &ReturnNullValues +func (s *CreateForScalaFunctionRequest) WithReturnNullValues(ReturnNullValues *ReturnNullValues) *CreateForScalaFunctionRequest { + s.ReturnNullValues = ReturnNullValues return s } -func (s *CreateFunctionForScalaFunctionRequest) WithNullInputBehavior(NullInputBehavior FunctionNullInputBehavior) *CreateFunctionForScalaFunctionRequest { - s.NullInputBehavior = &NullInputBehavior +func (s *CreateForScalaFunctionRequest) WithNullInputBehavior(NullInputBehavior *NullInputBehavior) *CreateForScalaFunctionRequest { + s.NullInputBehavior = NullInputBehavior return s } -func (s *CreateFunctionForScalaFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForScalaFunctionRequest { - s.ReturnResultsBehavior = &ReturnResultsBehavior +func (s *CreateForScalaFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior *ReturnResultsBehavior) *CreateForScalaFunctionRequest { + s.ReturnResultsBehavior = ReturnResultsBehavior return s } -func (s *CreateFunctionForScalaFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithRuntimeVersion(RuntimeVersion *string) *CreateForScalaFunctionRequest { s.RuntimeVersion = RuntimeVersion return s } -func (s *CreateFunctionForScalaFunctionRequest) WithComment(Comment *string) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithComment(Comment *string) *CreateForScalaFunctionRequest { s.Comment = Comment return s } -func (s *CreateFunctionForScalaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateForScalaFunctionRequest { s.Imports = Imports return s } -func (s *CreateFunctionForScalaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateForScalaFunctionRequest { s.Packages = Packages return s } -func (s *CreateFunctionForScalaFunctionRequest) WithTargetPath(TargetPath *string) *CreateFunctionForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithTargetPath(TargetPath *string) *CreateForScalaFunctionRequest { s.TargetPath = TargetPath return s } -func NewCreateFunctionForSQLFunctionRequest( +func (s *CreateForScalaFunctionRequest) WithFunctionDefinition(FunctionDefinition *string) *CreateForScalaFunctionRequest { + s.FunctionDefinition = FunctionDefinition + return s +} + +func NewCreateForSQLFunctionRequest( name SchemaObjectIdentifier, - returns *FunctionReturnsRequest, - functionDefinition string, -) *CreateFunctionForSQLFunctionRequest { - s := CreateFunctionForSQLFunctionRequest{} + Returns FunctionReturnsRequest, + FunctionDefinition *string, +) *CreateForSQLFunctionRequest { + s := CreateForSQLFunctionRequest{} s.name = name - s.Returns = returns - s.FunctionDefinition = functionDefinition + s.Returns = Returns + s.FunctionDefinition = FunctionDefinition return &s } -func (s *CreateFunctionForSQLFunctionRequest) WithOrReplace(OrReplace *bool) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithOrReplace(OrReplace *bool) *CreateForSQLFunctionRequest { s.OrReplace = OrReplace return s } -func (s *CreateFunctionForSQLFunctionRequest) WithTemporary(Temporary *bool) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithTemporary(Temporary *bool) *CreateForSQLFunctionRequest { s.Temporary = Temporary return s } -func (s *CreateFunctionForSQLFunctionRequest) WithSecure(Secure *bool) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithSecure(Secure *bool) *CreateForSQLFunctionRequest { s.Secure = Secure return s } -func (s *CreateFunctionForSQLFunctionRequest) WithIfNotExists(IfNotExists *bool) *CreateFunctionForSQLFunctionRequest { - s.IfNotExists = IfNotExists - return s -} - -func (s *CreateFunctionForSQLFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithArguments(Arguments []FunctionArgumentRequest) *CreateForSQLFunctionRequest { s.Arguments = Arguments return s } -func (s *CreateFunctionForSQLFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithCopyGrants(CopyGrants *bool) *CreateForSQLFunctionRequest { s.CopyGrants = CopyGrants return s } -func (s *CreateFunctionForSQLFunctionRequest) WithReturnNullValues(ReturnNullValues FunctionReturnNullValues) *CreateFunctionForSQLFunctionRequest { - s.ReturnNullValues = &ReturnNullValues +func (s *CreateForSQLFunctionRequest) WithReturnNullValues(ReturnNullValues *ReturnNullValues) *CreateForSQLFunctionRequest { + s.ReturnNullValues = ReturnNullValues return s } -func (s *CreateFunctionForSQLFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior FunctionReturnResultsBehavior) *CreateFunctionForSQLFunctionRequest { - s.ReturnResultsBehavior = &ReturnResultsBehavior +func (s *CreateForSQLFunctionRequest) WithReturnResultsBehavior(ReturnResultsBehavior *ReturnResultsBehavior) *CreateForSQLFunctionRequest { + s.ReturnResultsBehavior = ReturnResultsBehavior return s } -func (s *CreateFunctionForSQLFunctionRequest) WithMemoizable(Memoizable *bool) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithMemoizable(Memoizable *bool) *CreateForSQLFunctionRequest { s.Memoizable = Memoizable return s } -func (s *CreateFunctionForSQLFunctionRequest) WithComment(Comment *string) *CreateFunctionForSQLFunctionRequest { +func (s *CreateForSQLFunctionRequest) WithComment(Comment *string) *CreateForSQLFunctionRequest { s.Comment = Comment return s } func NewAlterFunctionRequest( name SchemaObjectIdentifier, + ArgumentDataTypes []DataType, ) *AlterFunctionRequest { s := AlterFunctionRequest{} s.name = name + s.ArgumentDataTypes = ArgumentDataTypes return &s } @@ -492,98 +480,68 @@ func (s *AlterFunctionRequest) WithIfExists(IfExists *bool) *AlterFunctionReques return s } -func (s *AlterFunctionRequest) WithArgumentTypes(ArgumentTypes []FunctionArgumentTypeRequest) *AlterFunctionRequest { - s.ArgumentTypes = ArgumentTypes - return s -} - -func (s *AlterFunctionRequest) WithSet(Set *FunctionSetRequest) *AlterFunctionRequest { - s.Set = Set - return s -} - -func (s *AlterFunctionRequest) WithUnset(Unset *FunctionUnsetRequest) *AlterFunctionRequest { - s.Unset = Unset - return s -} - func (s *AlterFunctionRequest) WithRenameTo(RenameTo *SchemaObjectIdentifier) *AlterFunctionRequest { s.RenameTo = RenameTo return s } -func (s *AlterFunctionRequest) WithSetTags(SetTags []TagAssociation) *AlterFunctionRequest { - s.SetTags = SetTags +func (s *AlterFunctionRequest) WithSetComment(SetComment *string) *AlterFunctionRequest { + s.SetComment = SetComment return s } -func (s *AlterFunctionRequest) WithUnsetTags(UnsetTags []ObjectIdentifier) *AlterFunctionRequest { - s.UnsetTags = UnsetTags - return s -} - -func NewFunctionArgumentTypeRequest() *FunctionArgumentTypeRequest { - return &FunctionArgumentTypeRequest{} -} - -func (s *FunctionArgumentTypeRequest) WithArgDataType(ArgDataType DataType) *FunctionArgumentTypeRequest { - s.ArgDataType = ArgDataType +func (s *AlterFunctionRequest) WithSetLogLevel(SetLogLevel *string) *AlterFunctionRequest { + s.SetLogLevel = SetLogLevel return s } -func NewFunctionSetRequest() *FunctionSetRequest { - return &FunctionSetRequest{} -} - -func (s *FunctionSetRequest) WithLogLevel(LogLevel *string) *FunctionSetRequest { - s.LogLevel = LogLevel +func (s *AlterFunctionRequest) WithSetTraceLevel(SetTraceLevel *string) *AlterFunctionRequest { + s.SetTraceLevel = SetTraceLevel return s } -func (s *FunctionSetRequest) WithTraceLevel(TraceLevel *string) *FunctionSetRequest { - s.TraceLevel = TraceLevel +func (s *AlterFunctionRequest) WithSetSecure(SetSecure *bool) *AlterFunctionRequest { + s.SetSecure = SetSecure return s } -func (s *FunctionSetRequest) WithComment(Comment *string) *FunctionSetRequest { - s.Comment = Comment +func (s *AlterFunctionRequest) WithUnsetSecure(UnsetSecure *bool) *AlterFunctionRequest { + s.UnsetSecure = UnsetSecure return s } -func (s *FunctionSetRequest) WithSecure(Secure *bool) *FunctionSetRequest { - s.Secure = Secure +func (s *AlterFunctionRequest) WithUnsetLogLevel(UnsetLogLevel *bool) *AlterFunctionRequest { + s.UnsetLogLevel = UnsetLogLevel return s } -func NewFunctionUnsetRequest() *FunctionUnsetRequest { - return &FunctionUnsetRequest{} -} - -func (s *FunctionUnsetRequest) WithSecure(Secure *bool) *FunctionUnsetRequest { - s.Secure = Secure +func (s *AlterFunctionRequest) WithUnsetTraceLevel(UnsetTraceLevel *bool) *AlterFunctionRequest { + s.UnsetTraceLevel = UnsetTraceLevel return s } -func (s *FunctionUnsetRequest) WithComment(Comment *bool) *FunctionUnsetRequest { - s.Comment = Comment +func (s *AlterFunctionRequest) WithUnsetComment(UnsetComment *bool) *AlterFunctionRequest { + s.UnsetComment = UnsetComment return s } -func (s *FunctionUnsetRequest) WithLogLevel(LogLevel *bool) *FunctionUnsetRequest { - s.LogLevel = LogLevel +func (s *AlterFunctionRequest) WithSetTags(SetTags []TagAssociation) *AlterFunctionRequest { + s.SetTags = SetTags return s } -func (s *FunctionUnsetRequest) WithTraceLevel(TraceLevel *bool) *FunctionUnsetRequest { - s.TraceLevel = TraceLevel +func (s *AlterFunctionRequest) WithUnsetTags(UnsetTags []ObjectIdentifier) *AlterFunctionRequest { + s.UnsetTags = UnsetTags return s } func NewDropFunctionRequest( name SchemaObjectIdentifier, + ArgumentDataTypes []DataType, ) *DropFunctionRequest { s := DropFunctionRequest{} s.name = name + s.ArgumentDataTypes = ArgumentDataTypes return &s } @@ -592,17 +550,12 @@ func (s *DropFunctionRequest) WithIfExists(IfExists *bool) *DropFunctionRequest return s } -func (s *DropFunctionRequest) WithArgumentTypes(ArgumentTypes []FunctionArgumentTypeRequest) *DropFunctionRequest { - s.ArgumentTypes = ArgumentTypes - return s -} - func NewShowFunctionRequest() *ShowFunctionRequest { return &ShowFunctionRequest{} } -func (s *ShowFunctionRequest) WithLike(pattern string) *ShowFunctionRequest { - s.Like = &Like{Pattern: String(pattern)} +func (s *ShowFunctionRequest) WithLike(Like *Like) *ShowFunctionRequest { + s.Like = Like return s } @@ -613,13 +566,10 @@ func (s *ShowFunctionRequest) WithIn(In *In) *ShowFunctionRequest { func NewDescribeFunctionRequest( name SchemaObjectIdentifier, + ArgumentDataTypes []DataType, ) *DescribeFunctionRequest { s := DescribeFunctionRequest{} s.name = name + s.ArgumentDataTypes = ArgumentDataTypes return &s } - -func (s *DescribeFunctionRequest) WithArgumentTypes(ArgumentTypes []FunctionArgumentTypeRequest) *DescribeFunctionRequest { - s.ArgumentTypes = ArgumentTypes - return s -} diff --git a/pkg/sdk/functions_dto_gen.go b/pkg/sdk/functions_dto_gen.go index 3c908a9c67..74a0713df5 100644 --- a/pkg/sdk/functions_dto_gen.go +++ b/pkg/sdk/functions_dto_gen.go @@ -3,18 +3,18 @@ package sdk //go:generate go run ./dto-builder-generator/main.go var ( - _ optionsProvider[CreateFunctionForJavaFunctionOptions] = new(CreateFunctionForJavaFunctionRequest) - _ optionsProvider[CreateFunctionForJavascriptFunctionOptions] = new(CreateFunctionForJavascriptFunctionRequest) - _ optionsProvider[CreateFunctionForPythonFunctionOptions] = new(CreateFunctionForPythonFunctionRequest) - _ optionsProvider[CreateFunctionForScalaFunctionOptions] = new(CreateFunctionForScalaFunctionRequest) - _ optionsProvider[CreateFunctionForSQLFunctionOptions] = new(CreateFunctionForSQLFunctionRequest) - _ optionsProvider[AlterFunctionOptions] = new(AlterFunctionRequest) - _ optionsProvider[DropFunctionOptions] = new(DropFunctionRequest) - _ optionsProvider[ShowFunctionOptions] = new(ShowFunctionRequest) - _ optionsProvider[DescribeFunctionOptions] = new(DescribeFunctionRequest) + _ optionsProvider[CreateForJavaFunctionOptions] = new(CreateForJavaFunctionRequest) + _ optionsProvider[CreateForJavascriptFunctionOptions] = new(CreateForJavascriptFunctionRequest) + _ optionsProvider[CreateForPythonFunctionOptions] = new(CreateForPythonFunctionRequest) + _ optionsProvider[CreateForScalaFunctionOptions] = new(CreateForScalaFunctionRequest) + _ optionsProvider[CreateForSQLFunctionOptions] = new(CreateForSQLFunctionRequest) + _ optionsProvider[AlterFunctionOptions] = new(AlterFunctionRequest) + _ optionsProvider[DropFunctionOptions] = new(DropFunctionRequest) + _ optionsProvider[ShowFunctionOptions] = new(ShowFunctionRequest) + _ optionsProvider[DescribeFunctionOptions] = new(DescribeFunctionRequest) ) -type CreateFunctionForJavaFunctionRequest struct { +type CreateForJavaFunctionRequest struct { OrReplace *bool Temporary *bool Secure *bool @@ -22,39 +22,43 @@ type CreateFunctionForJavaFunctionRequest struct { name SchemaObjectIdentifier // required Arguments []FunctionArgumentRequest CopyGrants *bool - Returns *FunctionReturnsRequest - ReturnNullValues *FunctionReturnNullValues - NullInputBehavior *FunctionNullInputBehavior - ReturnResultsBehavior *FunctionReturnResultsBehavior + Returns FunctionReturnsRequest // required + ReturnNullValues *ReturnNullValues + NullInputBehavior *NullInputBehavior + ReturnResultsBehavior *ReturnResultsBehavior RuntimeVersion *string Comment *string Imports []FunctionImportsRequest Packages []FunctionPackagesRequest - Handler string + Handler string // required ExternalAccessIntegrations []AccountObjectIdentifier - Secrets []FunctionSecretRequest + Secrets []Secret TargetPath *string - FunctionDefinition string + FunctionDefinition *string } type FunctionArgumentRequest struct { - ArgName string - ArgDataType DataType - Default *string + ArgName string // required + ArgDataType DataType // required + DefaultValue *string } type FunctionReturnsRequest struct { - ResultDataType *DataType + ResultDataType *FunctionReturnsResultDataTypeRequest Table *FunctionReturnsTableRequest } +type FunctionReturnsResultDataTypeRequest struct { + ResultDataType DataType // required +} + type FunctionReturnsTableRequest struct { Columns []FunctionColumnRequest } type FunctionColumnRequest struct { - ColumnName string - ColumnDataType DataType + ColumnName string // required + ColumnDataType DataType // required } type FunctionImportsRequest struct { @@ -65,28 +69,22 @@ type FunctionPackagesRequest struct { Package string } -type FunctionSecretRequest struct { - SecretVariableName string - SecretName string -} - -type CreateFunctionForJavascriptFunctionRequest struct { +type CreateForJavascriptFunctionRequest struct { OrReplace *bool Temporary *bool Secure *bool - IfNotExists *bool name SchemaObjectIdentifier // required Arguments []FunctionArgumentRequest CopyGrants *bool - Returns *FunctionReturnsRequest - ReturnNullValues *FunctionReturnNullValues - NullInputBehavior *FunctionNullInputBehavior - ReturnResultsBehavior *FunctionReturnResultsBehavior + Returns FunctionReturnsRequest // required + ReturnNullValues *ReturnNullValues + NullInputBehavior *NullInputBehavior + ReturnResultsBehavior *ReturnResultsBehavior Comment *string - FunctionDefinition string + FunctionDefinition *string // required } -type CreateFunctionForPythonFunctionRequest struct { +type CreateForPythonFunctionRequest struct { OrReplace *bool Temporary *bool Secure *bool @@ -94,21 +92,21 @@ type CreateFunctionForPythonFunctionRequest struct { name SchemaObjectIdentifier // required Arguments []FunctionArgumentRequest CopyGrants *bool - Returns *FunctionReturnsRequest - ReturnNullValues *FunctionReturnNullValues - NullInputBehavior *FunctionNullInputBehavior - ReturnResultsBehavior *FunctionReturnResultsBehavior - RuntimeVersion string + Returns FunctionReturnsRequest // required + ReturnNullValues *ReturnNullValues + NullInputBehavior *NullInputBehavior + ReturnResultsBehavior *ReturnResultsBehavior + RuntimeVersion string // required Comment *string Imports []FunctionImportsRequest Packages []FunctionPackagesRequest - Handler string + Handler string // required ExternalAccessIntegrations []AccountObjectIdentifier - Secrets []FunctionSecretRequest - FunctionDefinition string + Secrets []Secret + FunctionDefinition *string } -type CreateFunctionForScalaFunctionRequest struct { +type CreateForScalaFunctionRequest struct { OrReplace *bool Temporary *bool Secure *bool @@ -116,68 +114,55 @@ type CreateFunctionForScalaFunctionRequest struct { name SchemaObjectIdentifier // required Arguments []FunctionArgumentRequest CopyGrants *bool - Returns *FunctionReturnsRequest - ReturnNullValues *FunctionReturnNullValues - NullInputBehavior *FunctionNullInputBehavior - ReturnResultsBehavior *FunctionReturnResultsBehavior + ResultDataType DataType // required + ReturnNullValues *ReturnNullValues + NullInputBehavior *NullInputBehavior + ReturnResultsBehavior *ReturnResultsBehavior RuntimeVersion *string Comment *string Imports []FunctionImportsRequest Packages []FunctionPackagesRequest - Handler string + Handler string // required TargetPath *string - FunctionDefinition string + FunctionDefinition *string } -type CreateFunctionForSQLFunctionRequest struct { +type CreateForSQLFunctionRequest struct { OrReplace *bool Temporary *bool Secure *bool - IfNotExists *bool name SchemaObjectIdentifier // required Arguments []FunctionArgumentRequest CopyGrants *bool - Returns *FunctionReturnsRequest - ReturnNullValues *FunctionReturnNullValues - ReturnResultsBehavior *FunctionReturnResultsBehavior + Returns FunctionReturnsRequest // required + ReturnNullValues *ReturnNullValues + ReturnResultsBehavior *ReturnResultsBehavior Memoizable *bool Comment *string - FunctionDefinition string + FunctionDefinition *string // required } type AlterFunctionRequest struct { - IfExists *bool - name SchemaObjectIdentifier // required - ArgumentTypes []FunctionArgumentTypeRequest - Set *FunctionSetRequest - Unset *FunctionUnsetRequest - RenameTo *SchemaObjectIdentifier - SetTags []TagAssociation - UnsetTags []ObjectIdentifier -} - -type FunctionArgumentTypeRequest struct { - ArgDataType DataType -} - -type FunctionSetRequest struct { - LogLevel *string - TraceLevel *string - Comment *string - Secure *bool -} - -type FunctionUnsetRequest struct { - Secure *bool - Comment *bool - LogLevel *bool - TraceLevel *bool + IfExists *bool + name SchemaObjectIdentifier // required + ArgumentDataTypes []DataType // required + RenameTo *SchemaObjectIdentifier + SetComment *string + SetLogLevel *string + SetTraceLevel *string + SetSecure *bool + UnsetSecure *bool + UnsetLogLevel *bool + UnsetTraceLevel *bool + UnsetComment *bool + SetTags []TagAssociation + UnsetTags []ObjectIdentifier } type DropFunctionRequest struct { - IfExists *bool - name SchemaObjectIdentifier // required - ArgumentTypes []FunctionArgumentTypeRequest + IfExists *bool + name SchemaObjectIdentifier // required + ArgumentDataTypes []DataType // required } type ShowFunctionRequest struct { @@ -186,6 +171,6 @@ type ShowFunctionRequest struct { } type DescribeFunctionRequest struct { - name SchemaObjectIdentifier // required - ArgumentTypes []FunctionArgumentTypeRequest + name SchemaObjectIdentifier // required + ArgumentDataTypes []DataType // required } diff --git a/pkg/sdk/functions_gen.go b/pkg/sdk/functions_gen.go index 81765ade0d..3e57959dee 100644 --- a/pkg/sdk/functions_gen.go +++ b/pkg/sdk/functions_gen.go @@ -1,13 +1,16 @@ package sdk -import "context" +import ( + "context" + "database/sql" +) type Functions interface { - CreateFunctionForJava(ctx context.Context, request *CreateFunctionForJavaFunctionRequest) error - CreateFunctionForJavascript(ctx context.Context, request *CreateFunctionForJavascriptFunctionRequest) error - CreateFunctionForPython(ctx context.Context, request *CreateFunctionForPythonFunctionRequest) error - CreateFunctionForScala(ctx context.Context, request *CreateFunctionForScalaFunctionRequest) error - CreateFunctionForSQL(ctx context.Context, request *CreateFunctionForSQLFunctionRequest) error + CreateForJava(ctx context.Context, request *CreateForJavaFunctionRequest) error + CreateForJavascript(ctx context.Context, request *CreateForJavascriptFunctionRequest) error + CreateForPython(ctx context.Context, request *CreateForPythonFunctionRequest) error + CreateForScala(ctx context.Context, request *CreateForScalaFunctionRequest) error + CreateForSQL(ctx context.Context, request *CreateForSQLFunctionRequest) error Alter(ctx context.Context, request *AlterFunctionRequest) error Drop(ctx context.Context, request *DropFunctionRequest) error Show(ctx context.Context, request *ShowFunctionRequest) ([]Function, error) @@ -15,64 +18,46 @@ type Functions interface { Describe(ctx context.Context, request *DescribeFunctionRequest) ([]FunctionDetail, error) } -type FunctionNullInputBehavior string - -var ( - FunctionNullInputBehaviorCalledOnNullInput FunctionNullInputBehavior = "CALLED ON NULL INPUT" - FunctionNullInputBehaviorReturnNullInput FunctionNullInputBehavior = "RETURN NULL ON NULL INPUT" - FunctionNullInputBehaviorStrict FunctionNullInputBehavior = "STRICT" -) - -type FunctionReturnResultsBehavior string - -var ( - FunctionReturnResultsBehaviorVolatile FunctionReturnResultsBehavior = "VOLATILE" - FunctionReturnResultsBehaviorImmutable FunctionReturnResultsBehavior = "IMMUTABLE" -) - -type FunctionReturnNullValues string - -var ( - FunctionReturnNullValuesNull FunctionReturnNullValues = "NULL" - FunctionReturnNullValuesNotNull FunctionReturnNullValues = "NOT NULL" -) - -// CreateFunctionForJavaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. -type CreateFunctionForJavaFunctionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` - Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` - Secure *bool `ddl:"keyword" sql:"SECURE"` - function bool `ddl:"static" sql:"FUNCTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` - CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` - Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` - ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` - languageJava bool `ddl:"static" sql:"LANGUAGE JAVA"` - NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` - ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` - Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` - Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` - ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` - Secrets []FunctionSecret `ddl:"parameter,parentheses" sql:"SECRETS"` - TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` - FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +// CreateForJavaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function#java-handler. +type CreateForJavaFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *ReturnNullValues `ddl:"keyword"` + languageJava bool `ddl:"static" sql:"LANGUAGE JAVA"` + NullInputBehavior *NullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` + RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` + ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` + Secrets []Secret `ddl:"parameter,parentheses" sql:"SECRETS"` + TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } type FunctionArgument struct { - ArgName string `ddl:"keyword,no_quotes"` - ArgDataType DataType `ddl:"keyword,no_quotes"` - Default *string `ddl:"parameter,no_quotes" sql:"DEFAULT"` + ArgName string `ddl:"keyword,no_quotes"` + ArgDataType DataType `ddl:"keyword,no_quotes"` + DefaultValue *string `ddl:"parameter,no_equals" sql:"DEFAULT"` } type FunctionReturns struct { - ResultDataType *DataType `ddl:"keyword"` - Table *FunctionReturnsTable `ddl:"keyword" sql:"TABLE"` + ResultDataType *FunctionReturnsResultDataType `ddl:"keyword"` + Table *FunctionReturnsTable `ddl:"keyword" sql:"TABLE"` +} + +type FunctionReturnsResultDataType struct { + ResultDataType DataType `ddl:"keyword,no_quotes"` } type FunctionReturnsTable struct { @@ -92,140 +77,121 @@ type FunctionPackages struct { Package string `ddl:"keyword,single_quotes"` } -type FunctionSecret struct { - SecretVariableName string `ddl:"keyword,single_quotes"` - SecretName string `ddl:"parameter,no_quotes"` +// CreateForJavascriptFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function#javascript-handler. +type CreateForJavascriptFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *ReturnNullValues `ddl:"keyword"` + languageJavascript bool `ddl:"static" sql:"LANGUAGE JAVASCRIPT"` + NullInputBehavior *NullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } -// CreateFunctionForJavascriptFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. -type CreateFunctionForJavascriptFunctionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` - Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` - Secure *bool `ddl:"keyword" sql:"SECURE"` - function bool `ddl:"static" sql:"FUNCTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` - CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` - Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` - ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` - languageJavascript bool `ddl:"static" sql:"LANGUAGE JAVASCRIPT"` - NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` - ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +// CreateForPythonFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function#python-handler. +type CreateForPythonFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *ReturnNullValues `ddl:"keyword"` + languagePython bool `ddl:"static" sql:"LANGUAGE PYTHON"` + NullInputBehavior *NullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` + RuntimeVersion string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` + ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` + Secrets []Secret `ddl:"parameter,parentheses" sql:"SECRETS"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } -// CreateFunctionForPythonFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. -type CreateFunctionForPythonFunctionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` - Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` - Secure *bool `ddl:"keyword" sql:"SECURE"` - function bool `ddl:"static" sql:"FUNCTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` - CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` - Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` - ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` - languagePython bool `ddl:"static" sql:"LANGUAGE PYTHON"` - NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` - ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - RuntimeVersion string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` - Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` - Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` - ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` - Secrets []FunctionSecret `ddl:"parameter,parentheses" sql:"SECRETS"` - FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +// CreateForScalaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function#scala-handler. +type CreateForScalaFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + ResultDataType DataType `ddl:"parameter,no_equals" sql:"RETURNS"` + ReturnNullValues *ReturnNullValues `ddl:"keyword"` + languageScala bool `ddl:"static" sql:"LANGUAGE SCALA"` + NullInputBehavior *NullInputBehavior `ddl:"keyword"` + ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` + RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` + TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } -// CreateFunctionForScalaFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. -type CreateFunctionForScalaFunctionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` - Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` - Secure *bool `ddl:"keyword" sql:"SECURE"` - function bool `ddl:"static" sql:"FUNCTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` - CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` - Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` - ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` - languageScala bool `ddl:"static" sql:"LANGUAGE SCALA"` - NullInputBehavior *FunctionNullInputBehavior `ddl:"keyword"` - ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` - Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` - Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` - TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` - FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` -} - -// CreateFunctionForSQLFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function. -type CreateFunctionForSQLFunctionOptions struct { - create bool `ddl:"static" sql:"CREATE"` - OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` - Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` - Secure *bool `ddl:"keyword" sql:"SECURE"` - function bool `ddl:"static" sql:"FUNCTION"` - IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` - CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` - Returns *FunctionReturns `ddl:"keyword" sql:"RETURNS"` - ReturnNullValues *FunctionReturnNullValues `ddl:"keyword"` - ReturnResultsBehavior *FunctionReturnResultsBehavior `ddl:"keyword"` - Memoizable *bool `ddl:"keyword" sql:"MEMOIZABLE"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` +// CreateForSQLFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function#sql-handler. +type CreateForSQLFunctionOptions struct { + create bool `ddl:"static" sql:"CREATE"` + OrReplace *bool `ddl:"keyword" sql:"OR REPLACE"` + Temporary *bool `ddl:"keyword" sql:"TEMPORARY"` + Secure *bool `ddl:"keyword" sql:"SECURE"` + function bool `ddl:"static" sql:"FUNCTION"` + name SchemaObjectIdentifier `ddl:"identifier"` + Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` + Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` + ReturnNullValues *ReturnNullValues `ddl:"keyword"` + ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` + Memoizable *bool `ddl:"keyword" sql:"MEMOIZABLE"` + Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` + FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // AlterFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-function. type AlterFunctionOptions struct { - alter bool `ddl:"static" sql:"ALTER"` - function bool `ddl:"static" sql:"FUNCTION"` - IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` - Set *FunctionSet `ddl:"keyword" sql:"SET"` - Unset *FunctionUnset `ddl:"keyword" sql:"UNSET"` - RenameTo *SchemaObjectIdentifier `ddl:"identifier" sql:"RENAME TO"` - SetTags []TagAssociation `ddl:"keyword" sql:"SET TAG"` - UnsetTags []ObjectIdentifier `ddl:"keyword" sql:"UNSET TAG"` -} - -type FunctionArgumentType struct { - ArgDataType DataType `ddl:"keyword,no_quotes"` -} - -type FunctionSet struct { - LogLevel *string `ddl:"parameter,single_quotes" sql:"LOG_LEVEL"` - TraceLevel *string `ddl:"parameter,single_quotes" sql:"TRACE_LEVEL"` - Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Secure *bool `ddl:"keyword" sql:"SECURE"` -} - -type FunctionUnset struct { - Secure *bool `ddl:"keyword" sql:"SECURE"` - Comment *bool `ddl:"keyword" sql:"COMMENT"` - LogLevel *bool `ddl:"keyword" sql:"LOG_LEVEL"` - TraceLevel *bool `ddl:"keyword" sql:"TRACE_LEVEL"` + alter bool `ddl:"static" sql:"ALTER"` + function bool `ddl:"static" sql:"FUNCTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + ArgumentDataTypes []DataType `ddl:"keyword,parentheses"` + RenameTo *SchemaObjectIdentifier `ddl:"identifier" sql:"RENAME TO"` + SetComment *string `ddl:"parameter,single_quotes" sql:"SET COMMENT"` + SetLogLevel *string `ddl:"parameter,single_quotes" sql:"SET LOG_LEVEL"` + SetTraceLevel *string `ddl:"parameter,single_quotes" sql:"SET TRACE_LEVEL"` + SetSecure *bool `ddl:"keyword" sql:"SET SECURE"` + UnsetSecure *bool `ddl:"keyword" sql:"UNSET SECURE"` + UnsetLogLevel *bool `ddl:"keyword" sql:"UNSET LOG_LEVEL"` + UnsetTraceLevel *bool `ddl:"keyword" sql:"UNSET TRACE_LEVEL"` + UnsetComment *bool `ddl:"keyword" sql:"UNSET COMMENT"` + SetTags []TagAssociation `ddl:"keyword" sql:"SET TAG"` + UnsetTags []ObjectIdentifier `ddl:"keyword" sql:"UNSET TAG"` } // DropFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/drop-function. type DropFunctionOptions struct { - drop bool `ddl:"static" sql:"DROP"` - function bool `ddl:"static" sql:"FUNCTION"` - IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` - name SchemaObjectIdentifier `ddl:"identifier"` - ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` + drop bool `ddl:"static" sql:"DROP"` + function bool `ddl:"static" sql:"FUNCTION"` + IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` + name SchemaObjectIdentifier `ddl:"identifier"` + ArgumentDataTypes []DataType `ddl:"keyword,parentheses"` } // ShowFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-user-functions. @@ -237,30 +203,30 @@ type ShowFunctionOptions struct { } type functionRow struct { - CreatedOn string `db:"created_on"` - Name string `db:"name"` - SchemaName string `db:"schema_name"` - IsBuiltIn string `db:"is_builtin"` - IsAggregate string `db:"is_aggregate"` - IsAnsi string `db:"is_ansi"` - MinNumArguments int `db:"min_num_arguments"` - MaxNumArguments int `db:"max_num_arguments"` - Arguments string `db:"arguments"` - Description string `db:"description"` - CatalogName string `db:"catalog_name"` - IsTableFunction string `db:"is_table_function"` - ValidForClustering string `db:"valid_for_clustering"` - IsSecure string `db:"is_secure"` - IsExternalFunction string `db:"is_external_function"` - Language string `db:"language"` - IsMemoizable string `db:"is_memoizable"` + CreatedOn string `db:"created_on"` + Name string `db:"name"` + SchemaName string `db:"schema_name"` + IsBuiltin string `db:"is_builtin"` + IsAggregate string `db:"is_aggregate"` + IsAnsi string `db:"is_ansi"` + MinNumArguments int `db:"min_num_arguments"` + MaxNumArguments int `db:"max_num_arguments"` + Arguments string `db:"arguments"` + Description string `db:"description"` + CatalogName string `db:"catalog_name"` + IsTableFunction string `db:"is_table_function"` + ValidForClustering string `db:"valid_for_clustering"` + IsSecure sql.NullString `db:"is_secure"` + IsExternalFunction string `db:"is_external_function"` + Language string `db:"language"` + IsMemoizable sql.NullString `db:"is_memoizable"` } type Function struct { CreatedOn string Name string SchemaName string - IsBuiltIn bool + IsBuiltin bool IsAggregate bool IsAnsi bool MinNumArguments int @@ -278,10 +244,10 @@ type Function struct { // DescribeFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-function. type DescribeFunctionOptions struct { - describe bool `ddl:"static" sql:"DESCRIBE"` - function bool `ddl:"static" sql:"FUNCTION"` - name SchemaObjectIdentifier `ddl:"identifier"` - ArgumentTypes []FunctionArgumentType `ddl:"parameter,parentheses,no_equals"` + describe bool `ddl:"static" sql:"DESCRIBE"` + function bool `ddl:"static" sql:"FUNCTION"` + name SchemaObjectIdentifier `ddl:"identifier"` + ArgumentDataTypes []DataType `ddl:"keyword,parentheses"` } type functionDetailRow struct { diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go index 46f6359913..bc04a445cc 100644 --- a/pkg/sdk/functions_gen_test.go +++ b/pkg/sdk/functions_gen_test.go @@ -1,22 +1,23 @@ package sdk import ( + "errors" "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" ) -func TestFunctions_CreateFunctionForJava(t *testing.T) { +func TestFunctions_CreateForJava(t *testing.T) { id := RandomSchemaObjectIdentifier() - defaultOpts := func() *CreateFunctionForJavaFunctionOptions { - return &CreateFunctionForJavaFunctionOptions{ + defaultOpts := func() *CreateForJavaFunctionOptions { + return &CreateForJavaFunctionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - opts := (*CreateFunctionForJavaFunctionOptions)(nil) + opts := (*CreateForJavaFunctionOptions)(nil) assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) @@ -26,24 +27,37 @@ func TestFunctions_CreateFunctionForJava(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: function definition", func(t *testing.T) { + opts := defaultOpts() + opts.TargetPath = String("@~/testfunc.jar") + opts.Packages = []FunctionPackages{ + { + Package: "com.snowflake:snowpark:1.2.0", + }, + } + assertOptsInvalidJoinedErrors(t, opts, errors.New("TARGET_PATH must be nil when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, errors.New("PACKAGES must be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, errors.New("IMPORTS must not be empty when AS is nil")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) opts.Temporary = Bool(true) opts.Secure = Bool(true) - opts.IfNotExists = Bool(true) opts.Arguments = []FunctionArgument{ { ArgName: "id", ArgDataType: DataTypeNumber, }, { - ArgName: "name", - ArgDataType: DataTypeVARCHAR, + ArgName: "name", + ArgDataType: DataTypeVARCHAR, + DefaultValue: String("'test'"), }, } opts.CopyGrants = Bool(true) - opts.Returns = &FunctionReturns{ + opts.Returns = FunctionReturns{ Table: &FunctionReturnsTable{ Columns: []FunctionColumn{ { @@ -57,12 +71,9 @@ func TestFunctions_CreateFunctionForJava(t *testing.T) { }, }, } - returnNullValues := FunctionReturnNullValuesNotNull - opts.ReturnNullValues = &returnNullValues - nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput - opts.NullInputBehavior = &nullInputBehavior - returnResultsBehavior := FunctionReturnResultsBehaviorImmutable - opts.ReturnResultsBehavior = &returnResultsBehavior + opts.ReturnNullValues = ReturnNullValuesPointer(ReturnNullValuesNotNull) + opts.NullInputBehavior = NullInputBehaviorPointer(NullInputBehaviorCalledOnNullInput) + opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.RuntimeVersion = String("2.0") opts.Comment = String("comment") opts.Imports = []FunctionImports{ @@ -79,33 +90,33 @@ func TestFunctions_CreateFunctionForJava(t *testing.T) { opts.ExternalAccessIntegrations = []AccountObjectIdentifier{ NewAccountObjectIdentifier("ext_integration"), } - opts.Secrets = []FunctionSecret{ + opts.Secrets = []Secret{ { - SecretVariableName: "variable1", - SecretName: "name1", + VariableName: "variable1", + Name: "name1", }, { - SecretVariableName: "variable2", - SecretName: "name2", + VariableName: "variable2", + Name: "name2", }, } opts.TargetPath = String("@~/testfunc.jar") - opts.FunctionDefinition = "return id + name;" - assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (id NUMBER, name VARCHAR) COPY GRANTS RETURNS TABLE (country_code VARCHAR, country_name VARCHAR) NOT NULL LANGUAGE JAVA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@~/my_decrement_udf_package_dir/my_decrement_udf_jar.jar') PACKAGES = ('com.snowflake:snowpark:1.2.0') HANDLER = 'TestFunc.echoVarchar' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) TARGET_PATH = '@~/testfunc.jar' AS 'return id + name;'`, id.FullyQualifiedName()) + opts.FunctionDefinition = String("return id + name;") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (id NUMBER, name VARCHAR DEFAULT 'test') COPY GRANTS RETURNS TABLE (country_code VARCHAR, country_name VARCHAR) NOT NULL LANGUAGE JAVA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@~/my_decrement_udf_package_dir/my_decrement_udf_jar.jar') PACKAGES = ('com.snowflake:snowpark:1.2.0') HANDLER = 'TestFunc.echoVarchar' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) TARGET_PATH = '@~/testfunc.jar' AS 'return id + name;'`, id.FullyQualifiedName()) }) } -func TestFunctions_CreateFunctionForJavascript(t *testing.T) { +func TestFunctions_CreateForJavascript(t *testing.T) { id := RandomSchemaObjectIdentifier() - defaultOpts := func() *CreateFunctionForJavascriptFunctionOptions { - return &CreateFunctionForJavascriptFunctionOptions{ + defaultOpts := func() *CreateForJavascriptFunctionOptions { + return &CreateForJavascriptFunctionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - opts := (*CreateFunctionForJavascriptFunctionOptions)(nil) + opts := (*CreateForJavascriptFunctionOptions)(nil) assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) @@ -122,38 +133,37 @@ func TestFunctions_CreateFunctionForJavascript(t *testing.T) { opts.Secure = Bool(true) opts.Arguments = []FunctionArgument{ { - ArgName: "d", - ArgDataType: DataTypeFloat, + ArgName: "d", + ArgDataType: DataTypeFloat, + DefaultValue: String("1.0"), }, } opts.CopyGrants = Bool(true) - float := DataTypeFloat - opts.Returns = &FunctionReturns{ - ResultDataType: &float, - } - returnNullValues := FunctionReturnNullValuesNotNull - opts.ReturnNullValues = &returnNullValues - nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput - opts.NullInputBehavior = &nullInputBehavior - returnResultsBehavior := FunctionReturnResultsBehaviorImmutable - opts.ReturnResultsBehavior = &returnResultsBehavior + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeFloat, + }, + } + opts.ReturnNullValues = ReturnNullValuesPointer(ReturnNullValuesNotNull) + opts.NullInputBehavior = NullInputBehaviorPointer(NullInputBehaviorCalledOnNullInput) + opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.Comment = String("comment") - opts.FunctionDefinition = "return 1;" - assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (d FLOAT) COPY GRANTS RETURNS FLOAT NOT NULL LANGUAGE JAVASCRIPT CALLED ON NULL INPUT IMMUTABLE COMMENT = 'comment' AS 'return 1;'`, id.FullyQualifiedName()) + opts.FunctionDefinition = String("return 1;") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (d FLOAT DEFAULT 1.0) COPY GRANTS RETURNS FLOAT NOT NULL LANGUAGE JAVASCRIPT CALLED ON NULL INPUT IMMUTABLE COMMENT = 'comment' AS 'return 1;'`, id.FullyQualifiedName()) }) } -func TestFunctions_CreateFunctionForPython(t *testing.T) { +func TestFunctions_CreateForPython(t *testing.T) { id := RandomSchemaObjectIdentifier() - defaultOpts := func() *CreateFunctionForPythonFunctionOptions { - return &CreateFunctionForPythonFunctionOptions{ + defaultOpts := func() *CreateForPythonFunctionOptions { + return &CreateForPythonFunctionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - opts := (*CreateFunctionForPythonFunctionOptions)(nil) + opts := (*CreateForPythonFunctionOptions)(nil) assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) @@ -163,29 +173,37 @@ func TestFunctions_CreateFunctionForPython(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: function definition", func(t *testing.T) { + opts := defaultOpts() + opts.Packages = []FunctionPackages{ + { + Package: "com.snowflake:snowpark:1.2.0", + }, + } + assertOptsInvalidJoinedErrors(t, opts, errors.New("IMPORTS must not be empty when AS is nil")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) opts.Temporary = Bool(true) opts.Secure = Bool(true) - opts.IfNotExists = Bool(true) opts.Arguments = []FunctionArgument{ { - ArgName: "i", - ArgDataType: DataTypeNumber, + ArgName: "i", + ArgDataType: DataTypeNumber, + DefaultValue: String("1"), }, } opts.CopyGrants = Bool(true) - varint := DataTypeVariant - opts.Returns = &FunctionReturns{ - ResultDataType: &varint, - } - returnNullValues := FunctionReturnNullValuesNotNull - opts.ReturnNullValues = &returnNullValues - nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput - opts.NullInputBehavior = &nullInputBehavior - returnResultsBehavior := FunctionReturnResultsBehaviorImmutable - opts.ReturnResultsBehavior = &returnResultsBehavior + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeVariant, + }, + } + opts.ReturnNullValues = ReturnNullValuesPointer(ReturnNullValuesNotNull) + opts.NullInputBehavior = NullInputBehaviorPointer(NullInputBehaviorCalledOnNullInput) + opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.RuntimeVersion = "3.8" opts.Comment = String("comment") opts.Imports = []FunctionImports{ @@ -208,32 +226,32 @@ func TestFunctions_CreateFunctionForPython(t *testing.T) { opts.ExternalAccessIntegrations = []AccountObjectIdentifier{ NewAccountObjectIdentifier("ext_integration"), } - opts.Secrets = []FunctionSecret{ + opts.Secrets = []Secret{ { - SecretVariableName: "variable1", - SecretName: "name1", + VariableName: "variable1", + Name: "name1", }, { - SecretVariableName: "variable2", - SecretName: "name2", + VariableName: "variable2", + Name: "name2", }, } - opts.FunctionDefinition = "import numpy as np" - assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (i NUMBER) COPY GRANTS RETURNS VARIANT NOT NULL LANGUAGE PYTHON CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '3.8' COMMENT = 'comment' IMPORTS = ('numpy', 'pandas') PACKAGES = ('numpy', 'pandas') HANDLER = 'udf' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) AS 'import numpy as np'`, id.FullyQualifiedName()) + opts.FunctionDefinition = String("import numpy as np") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (i NUMBER DEFAULT 1) COPY GRANTS RETURNS VARIANT NOT NULL LANGUAGE PYTHON CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '3.8' COMMENT = 'comment' IMPORTS = ('numpy', 'pandas') PACKAGES = ('numpy', 'pandas') HANDLER = 'udf' EXTERNAL_ACCESS_INTEGRATIONS = ("ext_integration") SECRETS = ('variable1' = name1, 'variable2' = name2) AS 'import numpy as np'`, id.FullyQualifiedName()) }) } -func TestFunctions_CreateFunctionForScala(t *testing.T) { +func TestFunctions_CreateForScala(t *testing.T) { id := RandomSchemaObjectIdentifier() - defaultOpts := func() *CreateFunctionForScalaFunctionOptions { - return &CreateFunctionForScalaFunctionOptions{ + defaultOpts := func() *CreateForScalaFunctionOptions { + return &CreateForScalaFunctionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - opts := (*CreateFunctionForScalaFunctionOptions)(nil) + opts := (*CreateForScalaFunctionOptions)(nil) assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) @@ -243,29 +261,36 @@ func TestFunctions_CreateFunctionForScala(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: function definition", func(t *testing.T) { + opts := defaultOpts() + opts.TargetPath = String("@~/testfunc.jar") + opts.Packages = []FunctionPackages{ + { + Package: "com.snowflake:snowpark:1.2.0", + }, + } + assertOptsInvalidJoinedErrors(t, opts, errors.New("TARGET_PATH must be nil when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, errors.New("PACKAGES must be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, errors.New("IMPORTS must not be empty when AS is nil")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) opts.Temporary = Bool(true) opts.Secure = Bool(true) - opts.IfNotExists = Bool(true) opts.Arguments = []FunctionArgument{ { - ArgName: "x", - ArgDataType: DataTypeVARCHAR, + ArgName: "x", + ArgDataType: DataTypeVARCHAR, + DefaultValue: String("'test'"), }, } opts.CopyGrants = Bool(true) - varchar := DataTypeVARCHAR - opts.Returns = &FunctionReturns{ - ResultDataType: &varchar, - } - returnNullValues := FunctionReturnNullValuesNotNull - opts.ReturnNullValues = &returnNullValues - nullInputBehavior := FunctionNullInputBehaviorCalledOnNullInput - opts.NullInputBehavior = &nullInputBehavior - returnResultsBehavior := FunctionReturnResultsBehaviorImmutable - opts.ReturnResultsBehavior = &returnResultsBehavior + opts.ResultDataType = DataTypeVARCHAR + opts.ReturnNullValues = ReturnNullValuesPointer(ReturnNullValuesNotNull) + opts.NullInputBehavior = NullInputBehaviorPointer(NullInputBehaviorCalledOnNullInput) + opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.RuntimeVersion = String("2.0") opts.Comment = String("comment") opts.Imports = []FunctionImports{ @@ -273,23 +298,23 @@ func TestFunctions_CreateFunctionForScala(t *testing.T) { Import: "@udf_libs/echohandler.jar", }, } - opts.Handler ="Echo.echoVarchar" - opts.FunctionDefinition = "return x" - assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s (x VARCHAR) COPY GRANTS RETURNS VARCHAR NOT NULL LANGUAGE SCALA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@udf_libs/echohandler.jar') HANDLER = 'Echo.echoVarchar' AS 'return x'`, id.FullyQualifiedName()) + opts.Handler = "Echo.echoVarchar" + opts.FunctionDefinition = String("return x") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (x VARCHAR DEFAULT 'test') COPY GRANTS RETURNS VARCHAR NOT NULL LANGUAGE SCALA CALLED ON NULL INPUT IMMUTABLE RUNTIME_VERSION = '2.0' COMMENT = 'comment' IMPORTS = ('@udf_libs/echohandler.jar') HANDLER = 'Echo.echoVarchar' AS 'return x'`, id.FullyQualifiedName()) }) } -func TestFunctions_CreateFunctionForSQL(t *testing.T) { +func TestFunctions_CreateForSQL(t *testing.T) { id := RandomSchemaObjectIdentifier() - defaultOpts := func() *CreateFunctionForSQLFunctionOptions { - return &CreateFunctionForSQLFunctionOptions{ + defaultOpts := func() *CreateForSQLFunctionOptions { + return &CreateForSQLFunctionOptions{ name: id, } } t.Run("validation: nil options", func(t *testing.T) { - opts := (*CreateFunctionForSQLFunctionOptions)(nil) + opts := (*CreateForSQLFunctionOptions)(nil) assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) @@ -304,20 +329,25 @@ func TestFunctions_CreateFunctionForSQL(t *testing.T) { opts.OrReplace = Bool(true) opts.Temporary = Bool(true) opts.Secure = Bool(true) - opts.IfNotExists = Bool(true) + opts.Arguments = []FunctionArgument{ + { + ArgName: "message", + ArgDataType: "VARCHAR", + DefaultValue: String("'test'"), + }, + } opts.CopyGrants = Bool(true) - dt := DataTypeFloat - opts.Returns = &FunctionReturns{ - ResultDataType: &dt, - } - returnNullValues := FunctionReturnNullValuesNotNull - opts.ReturnNullValues = &returnNullValues - returnResultsBehavior := FunctionReturnResultsBehaviorImmutable - opts.ReturnResultsBehavior = &returnResultsBehavior + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeFloat, + }, + } + opts.ReturnNullValues = ReturnNullValuesPointer(ReturnNullValuesNotNull) + opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.Memoizable = Bool(true) opts.Comment = String("comment") - opts.FunctionDefinition = "3.141592654::FLOAT" - assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION IF NOT EXISTS %s COPY GRANTS RETURNS FLOAT NOT NULL IMMUTABLE MEMOIZABLE COMMENT = 'comment' AS '3.141592654::FLOAT'`, id.FullyQualifiedName()) + opts.FunctionDefinition = String("3.141592654::FLOAT") + assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (message VARCHAR DEFAULT 'test') COPY GRANTS RETURNS FLOAT NOT NULL IMMUTABLE MEMOIZABLE COMMENT = 'comment' AS '3.141592654::FLOAT'`, id.FullyQualifiedName()) }) } @@ -346,14 +376,7 @@ func TestFunctions_Drop(t *testing.T) { name: id, } opts.IfExists = Bool(true) - opts.ArgumentTypes = []FunctionArgumentType{ - { - ArgDataType: DataTypeVARCHAR, - }, - { - ArgDataType: DataTypeNumber, - }, - } + opts.ArgumentDataTypes = []DataType{DataTypeVARCHAR, DataTypeNumber} assertOptsValidAndSQLEquals(t, opts, `DROP FUNCTION IF EXISTS %s (VARCHAR, NUMBER)`, id.FullyQualifiedName()) }) } @@ -363,16 +386,9 @@ func TestFunctions_Alter(t *testing.T) { defaultOpts := func() *AlterFunctionOptions { return &AlterFunctionOptions{ - name: id, - IfExists: Bool(true), - ArgumentTypes: []FunctionArgumentType{ - { - ArgDataType: DataTypeVARCHAR, - }, - { - ArgDataType: DataTypeNumber, - }, - }, + name: id, + IfExists: Bool(true), + ArgumentDataTypes: []DataType{DataTypeVARCHAR, DataTypeNumber}, } } @@ -387,6 +403,18 @@ func TestFunctions_Alter(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: exactly one field should be present", func(t *testing.T) { + opts := defaultOpts() + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterFunctionOptions", "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "SetSecure", "UnsetLogLevel", "UnsetTraceLevel", "UnsetSecure", "UnsetComment", "SetTags", "UnsetTags")) + }) + + t.Run("validation: exactly one field should be present", func(t *testing.T) { + opts := defaultOpts() + opts.SetLogLevel = String("DEBUG") + opts.UnsetComment = Bool(true) + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("AlterFunctionOptions", "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "SetSecure", "UnsetLogLevel", "UnsetTraceLevel", "UnsetSecure", "UnsetComment", "SetTags", "UnsetTags")) + }) + t.Run("alter: rename to", func(t *testing.T) { opts := defaultOpts() target := NewSchemaObjectIdentifier(id.DatabaseName(), id.SchemaName(), random.StringN(12)) @@ -396,49 +424,49 @@ func TestFunctions_Alter(t *testing.T) { t.Run("alter: set log level", func(t *testing.T) { opts := defaultOpts() - opts.Set = &FunctionSet{ - LogLevel: String("DEBUG"), - } + opts.SetLogLevel = String("DEBUG") assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET LOG_LEVEL = 'DEBUG'`, id.FullyQualifiedName()) }) t.Run("alter: set trace level", func(t *testing.T) { opts := defaultOpts() - opts.Set = &FunctionSet{ - TraceLevel: String("DEBUG"), - } + opts.SetTraceLevel = String("DEBUG") assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET TRACE_LEVEL = 'DEBUG'`, id.FullyQualifiedName()) }) t.Run("alter: set comment", func(t *testing.T) { opts := defaultOpts() - opts.Set = &FunctionSet{ - Comment: String("comment"), - } + opts.SetComment = String("comment") assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET COMMENT = 'comment'`, id.FullyQualifiedName()) }) t.Run("alter: set secure", func(t *testing.T) { opts := defaultOpts() - opts.Set = &FunctionSet{ - Secure: Bool(true), - } + opts.SetSecure = Bool(true) assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) SET SECURE`, id.FullyQualifiedName()) }) + t.Run("alter: unset log level", func(t *testing.T) { + opts := defaultOpts() + opts.UnsetLogLevel = Bool(true) + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET LOG_LEVEL`, id.FullyQualifiedName()) + }) + + t.Run("alter: unset trace level", func(t *testing.T) { + opts := defaultOpts() + opts.UnsetTraceLevel = Bool(true) + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET TRACE_LEVEL`, id.FullyQualifiedName()) + }) + t.Run("alter: unset secure", func(t *testing.T) { opts := defaultOpts() - opts.Unset = &FunctionUnset{ - Secure: Bool(true), - } + opts.UnsetSecure = Bool(true) assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET SECURE`, id.FullyQualifiedName()) }) t.Run("alter: unset comment", func(t *testing.T) { opts := defaultOpts() - opts.Unset = &FunctionUnset{ - Comment: Bool(true), - } + opts.UnsetComment = Bool(true) assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) UNSET COMMENT`, id.FullyQualifiedName()) }) @@ -473,12 +501,6 @@ func TestFunctions_Show(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) }) - t.Run("validation: empty like", func(t *testing.T) { - opts := defaultOpts() - opts.Like = &Like{} - assertOptsInvalidJoinedErrors(t, opts, ErrPatternRequiredForLikeKeyword) - }) - t.Run("show with empty options", func(t *testing.T) { opts := defaultOpts() assertOptsValidAndSQLEquals(t, opts, `SHOW USER FUNCTIONS`) diff --git a/pkg/sdk/functions_impl_gen.go b/pkg/sdk/functions_impl_gen.go index f1be1337a9..70426de5ae 100644 --- a/pkg/sdk/functions_impl_gen.go +++ b/pkg/sdk/functions_impl_gen.go @@ -12,27 +12,27 @@ type functions struct { client *Client } -func (v *functions) CreateFunctionForJava(ctx context.Context, request *CreateFunctionForJavaFunctionRequest) error { +func (v *functions) CreateForJava(ctx context.Context, request *CreateForJavaFunctionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *functions) CreateFunctionForJavascript(ctx context.Context, request *CreateFunctionForJavascriptFunctionRequest) error { +func (v *functions) CreateForJavascript(ctx context.Context, request *CreateForJavascriptFunctionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *functions) CreateFunctionForPython(ctx context.Context, request *CreateFunctionForPythonFunctionRequest) error { +func (v *functions) CreateForPython(ctx context.Context, request *CreateForPythonFunctionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *functions) CreateFunctionForScala(ctx context.Context, request *CreateFunctionForScalaFunctionRequest) error { +func (v *functions) CreateForScala(ctx context.Context, request *CreateForScalaFunctionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } -func (v *functions) CreateFunctionForSQL(ctx context.Context, request *CreateFunctionForSQLFunctionRequest) error { +func (v *functions) CreateForSQL(ctx context.Context, request *CreateForSQLFunctionRequest) error { opts := request.toOpts() return validateAndExec(v.client, ctx, opts) } @@ -58,7 +58,7 @@ func (v *functions) Show(ctx context.Context, request *ShowFunctionRequest) ([]F } func (v *functions) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Function, error) { - request := NewShowFunctionRequest().WithLike(id.Name()) + request := NewShowFunctionRequest().WithIn(&In{Database: NewAccountObjectIdentifier(id.DatabaseName())}).WithLike(&Like{String(id.Name())}) functions, err := v.Show(ctx, request) if err != nil { return nil, err @@ -75,8 +75,8 @@ func (v *functions) Describe(ctx context.Context, request *DescribeFunctionReque return convertRows[functionDetailRow, FunctionDetail](rows), nil } -func (r *CreateFunctionForJavaFunctionRequest) toOpts() *CreateFunctionForJavaFunctionOptions { - opts := &CreateFunctionForJavaFunctionOptions{ +func (r *CreateForJavaFunctionRequest) toOpts() *CreateForJavaFunctionOptions { + opts := &CreateForJavaFunctionOptions{ OrReplace: r.OrReplace, Temporary: r.Temporary, Secure: r.Secure, @@ -85,52 +85,48 @@ func (r *CreateFunctionForJavaFunctionRequest) toOpts() *CreateFunctionForJavaFu CopyGrants: r.CopyGrants, - RuntimeVersion: r.RuntimeVersion, - Comment: r.Comment, + ReturnNullValues: r.ReturnNullValues, + NullInputBehavior: r.NullInputBehavior, + ReturnResultsBehavior: r.ReturnResultsBehavior, + RuntimeVersion: r.RuntimeVersion, + Comment: r.Comment, Handler: r.Handler, ExternalAccessIntegrations: r.ExternalAccessIntegrations, - - TargetPath: r.TargetPath, + Secrets: r.Secrets, + TargetPath: r.TargetPath, + FunctionDefinition: r.FunctionDefinition, } if r.Arguments != nil { s := make([]FunctionArgument, len(r.Arguments)) for i, v := range r.Arguments { s[i] = FunctionArgument{ - ArgName: v.ArgName, - ArgDataType: v.ArgDataType, - Default: v.Default, + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + DefaultValue: v.DefaultValue, } } opts.Arguments = s } - if r.Returns != nil { - opts.Returns = &FunctionReturns{ - ResultDataType: r.Returns.ResultDataType, + opts.Returns = FunctionReturns{} + if r.Returns.ResultDataType != nil { + opts.Returns.ResultDataType = &FunctionReturnsResultDataType{ + ResultDataType: r.Returns.ResultDataType.ResultDataType, } - if r.Returns.Table != nil { - opts.Returns.Table = &FunctionReturnsTable{} - if r.Returns.Table.Columns != nil { - s := make([]FunctionColumn, len(r.Returns.Table.Columns)) - for i, v := range r.Returns.Table.Columns { - s[i] = FunctionColumn{ - ColumnName: v.ColumnName, - ColumnDataType: v.ColumnDataType, - } + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, } - opts.Returns.Table.Columns = s } + opts.Returns.Table.Columns = s } } - if r.ReturnNullValues != nil { - opts.ReturnNullValues = r.ReturnNullValues - } - if r.NullInputBehavior != nil { - opts.NullInputBehavior = r.NullInputBehavior - } - if r.ReturnResultsBehavior != nil { - opts.ReturnResultsBehavior = r.ReturnResultsBehavior - } if r.Imports != nil { s := make([]FunctionImports, len(r.Imports)) for i, v := range r.Imports { @@ -149,77 +145,59 @@ func (r *CreateFunctionForJavaFunctionRequest) toOpts() *CreateFunctionForJavaFu } opts.Packages = s } - if r.Secrets != nil { - s := make([]FunctionSecret, len(r.Secrets)) - for i, v := range r.Secrets { - s[i] = FunctionSecret{ - SecretVariableName: v.SecretVariableName, - SecretName: v.SecretName, - } - } - opts.Secrets = s - } - opts.FunctionDefinition = r.FunctionDefinition return opts } -func (r *CreateFunctionForJavascriptFunctionRequest) toOpts() *CreateFunctionForJavascriptFunctionOptions { - opts := &CreateFunctionForJavascriptFunctionOptions{ - OrReplace: r.OrReplace, - Temporary: r.Temporary, - Secure: r.Secure, - IfNotExists: r.IfNotExists, - name: r.name, +func (r *CreateForJavascriptFunctionRequest) toOpts() *CreateForJavascriptFunctionOptions { + opts := &CreateForJavascriptFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + name: r.name, CopyGrants: r.CopyGrants, - Comment: r.Comment, + ReturnNullValues: r.ReturnNullValues, + NullInputBehavior: r.NullInputBehavior, + ReturnResultsBehavior: r.ReturnResultsBehavior, + Comment: r.Comment, + FunctionDefinition: r.FunctionDefinition, } if r.Arguments != nil { s := make([]FunctionArgument, len(r.Arguments)) for i, v := range r.Arguments { s[i] = FunctionArgument{ - ArgName: v.ArgName, - ArgDataType: v.ArgDataType, - Default: v.Default, + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + DefaultValue: v.DefaultValue, } } opts.Arguments = s } - if r.Returns != nil { - opts.Returns = &FunctionReturns{ - ResultDataType: r.Returns.ResultDataType, + opts.Returns = FunctionReturns{} + if r.Returns.ResultDataType != nil { + opts.Returns.ResultDataType = &FunctionReturnsResultDataType{ + ResultDataType: r.Returns.ResultDataType.ResultDataType, } - if r.Returns.Table != nil { - opts.Returns.Table = &FunctionReturnsTable{} - if r.Returns.Table.Columns != nil { - s := make([]FunctionColumn, len(r.Returns.Table.Columns)) - for i, v := range r.Returns.Table.Columns { - s[i] = FunctionColumn{ - ColumnName: v.ColumnName, - ColumnDataType: v.ColumnDataType, - } + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, } - opts.Returns.Table.Columns = s } + opts.Returns.Table.Columns = s } } - if r.ReturnNullValues != nil { - opts.ReturnNullValues = r.ReturnNullValues - } - if r.NullInputBehavior != nil { - opts.NullInputBehavior = r.NullInputBehavior - } - if r.ReturnResultsBehavior != nil { - opts.ReturnResultsBehavior = r.ReturnResultsBehavior - } - - opts.FunctionDefinition = r.FunctionDefinition return opts } -func (r *CreateFunctionForPythonFunctionRequest) toOpts() *CreateFunctionForPythonFunctionOptions { - opts := &CreateFunctionForPythonFunctionOptions{ +func (r *CreateForPythonFunctionRequest) toOpts() *CreateForPythonFunctionOptions { + opts := &CreateForPythonFunctionOptions{ OrReplace: r.OrReplace, Temporary: r.Temporary, Secure: r.Secure, @@ -228,50 +206,47 @@ func (r *CreateFunctionForPythonFunctionRequest) toOpts() *CreateFunctionForPyth CopyGrants: r.CopyGrants, - RuntimeVersion: r.RuntimeVersion, - Comment: r.Comment, + ReturnNullValues: r.ReturnNullValues, + NullInputBehavior: r.NullInputBehavior, + ReturnResultsBehavior: r.ReturnResultsBehavior, + RuntimeVersion: r.RuntimeVersion, + Comment: r.Comment, Handler: r.Handler, ExternalAccessIntegrations: r.ExternalAccessIntegrations, + Secrets: r.Secrets, + FunctionDefinition: r.FunctionDefinition, } if r.Arguments != nil { s := make([]FunctionArgument, len(r.Arguments)) for i, v := range r.Arguments { s[i] = FunctionArgument{ - ArgName: v.ArgName, - ArgDataType: v.ArgDataType, - Default: v.Default, + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + DefaultValue: v.DefaultValue, } } opts.Arguments = s } - if r.Returns != nil { - opts.Returns = &FunctionReturns{ - ResultDataType: r.Returns.ResultDataType, + opts.Returns = FunctionReturns{} + if r.Returns.ResultDataType != nil { + opts.Returns.ResultDataType = &FunctionReturnsResultDataType{ + ResultDataType: r.Returns.ResultDataType.ResultDataType, } - if r.Returns.Table != nil { - opts.Returns.Table = &FunctionReturnsTable{} - if r.Returns.Table.Columns != nil { - s := make([]FunctionColumn, len(r.Returns.Table.Columns)) - for i, v := range r.Returns.Table.Columns { - s[i] = FunctionColumn{ - ColumnName: v.ColumnName, - ColumnDataType: v.ColumnDataType, - } + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, } - opts.Returns.Table.Columns = s } + opts.Returns.Table.Columns = s } } - if r.ReturnNullValues != nil { - opts.ReturnNullValues = r.ReturnNullValues - } - if r.NullInputBehavior != nil { - opts.NullInputBehavior = r.NullInputBehavior - } - if r.ReturnResultsBehavior != nil { - opts.ReturnResultsBehavior = r.ReturnResultsBehavior - } if r.Imports != nil { s := make([]FunctionImports, len(r.Imports)) for i, v := range r.Imports { @@ -290,74 +265,40 @@ func (r *CreateFunctionForPythonFunctionRequest) toOpts() *CreateFunctionForPyth } opts.Packages = s } - if r.Secrets != nil { - s := make([]FunctionSecret, len(r.Secrets)) - for i, v := range r.Secrets { - s[i] = FunctionSecret{ - SecretVariableName: v.SecretVariableName, - SecretName: v.SecretName, - } - } - opts.Secrets = s - } - opts.FunctionDefinition = r.FunctionDefinition return opts } -func (r *CreateFunctionForScalaFunctionRequest) toOpts() *CreateFunctionForScalaFunctionOptions { - opts := &CreateFunctionForScalaFunctionOptions{ +func (r *CreateForScalaFunctionRequest) toOpts() *CreateForScalaFunctionOptions { + opts := &CreateForScalaFunctionOptions{ OrReplace: r.OrReplace, Temporary: r.Temporary, Secure: r.Secure, IfNotExists: r.IfNotExists, name: r.name, - CopyGrants: r.CopyGrants, - - RuntimeVersion: r.RuntimeVersion, - Comment: r.Comment, + CopyGrants: r.CopyGrants, + ResultDataType: r.ResultDataType, + ReturnNullValues: r.ReturnNullValues, + NullInputBehavior: r.NullInputBehavior, + ReturnResultsBehavior: r.ReturnResultsBehavior, + RuntimeVersion: r.RuntimeVersion, + Comment: r.Comment, - Handler: r.Handler, - TargetPath: r.TargetPath, + Handler: r.Handler, + TargetPath: r.TargetPath, + FunctionDefinition: r.FunctionDefinition, } if r.Arguments != nil { s := make([]FunctionArgument, len(r.Arguments)) for i, v := range r.Arguments { s[i] = FunctionArgument{ - ArgName: v.ArgName, - ArgDataType: v.ArgDataType, - Default: v.Default, + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + DefaultValue: v.DefaultValue, } } opts.Arguments = s } - if r.Returns != nil { - opts.Returns = &FunctionReturns{ - ResultDataType: r.Returns.ResultDataType, - } - if r.Returns.Table != nil { - opts.Returns.Table = &FunctionReturnsTable{} - if r.Returns.Table.Columns != nil { - s := make([]FunctionColumn, len(r.Returns.Table.Columns)) - for i, v := range r.Returns.Table.Columns { - s[i] = FunctionColumn{ - ColumnName: v.ColumnName, - ColumnDataType: v.ColumnDataType, - } - } - opts.Returns.Table.Columns = s - } - } - } - if r.ReturnNullValues != nil { - opts.ReturnNullValues = r.ReturnNullValues - } - if r.NullInputBehavior != nil { - opts.NullInputBehavior = r.NullInputBehavior - } - if r.ReturnResultsBehavior != nil { - opts.ReturnResultsBehavior = r.ReturnResultsBehavior - } if r.Imports != nil { s := make([]FunctionImports, len(r.Imports)) for i, v := range r.Imports { @@ -376,112 +317,82 @@ func (r *CreateFunctionForScalaFunctionRequest) toOpts() *CreateFunctionForScala } opts.Packages = s } - opts.FunctionDefinition = r.FunctionDefinition return opts } -func (r *CreateFunctionForSQLFunctionRequest) toOpts() *CreateFunctionForSQLFunctionOptions { - opts := &CreateFunctionForSQLFunctionOptions{ - OrReplace: r.OrReplace, - Temporary: r.Temporary, - Secure: r.Secure, - IfNotExists: r.IfNotExists, - name: r.name, +func (r *CreateForSQLFunctionRequest) toOpts() *CreateForSQLFunctionOptions { + opts := &CreateForSQLFunctionOptions{ + OrReplace: r.OrReplace, + Temporary: r.Temporary, + Secure: r.Secure, + name: r.name, CopyGrants: r.CopyGrants, - Memoizable: r.Memoizable, - Comment: r.Comment, + ReturnNullValues: r.ReturnNullValues, + ReturnResultsBehavior: r.ReturnResultsBehavior, + Memoizable: r.Memoizable, + Comment: r.Comment, + FunctionDefinition: r.FunctionDefinition, } if r.Arguments != nil { s := make([]FunctionArgument, len(r.Arguments)) for i, v := range r.Arguments { s[i] = FunctionArgument{ - ArgName: v.ArgName, - ArgDataType: v.ArgDataType, - Default: v.Default, + ArgName: v.ArgName, + ArgDataType: v.ArgDataType, + DefaultValue: v.DefaultValue, } } opts.Arguments = s } - if r.Returns != nil { - opts.Returns = &FunctionReturns{ - ResultDataType: r.Returns.ResultDataType, + opts.Returns = FunctionReturns{} + if r.Returns.ResultDataType != nil { + opts.Returns.ResultDataType = &FunctionReturnsResultDataType{ + ResultDataType: r.Returns.ResultDataType.ResultDataType, } - if r.Returns.Table != nil { - opts.Returns.Table = &FunctionReturnsTable{} - if r.Returns.Table.Columns != nil { - s := make([]FunctionColumn, len(r.Returns.Table.Columns)) - for i, v := range r.Returns.Table.Columns { - s[i] = FunctionColumn{ - ColumnName: v.ColumnName, - ColumnDataType: v.ColumnDataType, - } + } + if r.Returns.Table != nil { + opts.Returns.Table = &FunctionReturnsTable{} + if r.Returns.Table.Columns != nil { + s := make([]FunctionColumn, len(r.Returns.Table.Columns)) + for i, v := range r.Returns.Table.Columns { + s[i] = FunctionColumn{ + ColumnName: v.ColumnName, + ColumnDataType: v.ColumnDataType, } - opts.Returns.Table.Columns = s } + opts.Returns.Table.Columns = s } } - if r.ReturnNullValues != nil { - opts.ReturnNullValues = r.ReturnNullValues - } - if r.ReturnResultsBehavior != nil { - opts.ReturnResultsBehavior = r.ReturnResultsBehavior - } - opts.FunctionDefinition = r.FunctionDefinition return opts } func (r *AlterFunctionRequest) toOpts() *AlterFunctionOptions { opts := &AlterFunctionOptions{ - IfExists: r.IfExists, - name: r.name, - - RenameTo: r.RenameTo, - SetTags: r.SetTags, - UnsetTags: r.UnsetTags, - } - if r.ArgumentTypes != nil { - s := make([]FunctionArgumentType, len(r.ArgumentTypes)) - for i, v := range r.ArgumentTypes { - s[i] = FunctionArgumentType{ - ArgDataType: v.ArgDataType, - } - } - opts.ArgumentTypes = s - } - if r.Set != nil { - opts.Set = &FunctionSet{ - LogLevel: r.Set.LogLevel, - TraceLevel: r.Set.TraceLevel, - Comment: r.Set.Comment, - Secure: r.Set.Secure, - } - } - if r.Unset != nil { - opts.Unset = &FunctionUnset{ - Secure: r.Unset.Secure, - Comment: r.Unset.Comment, - LogLevel: r.Unset.LogLevel, - TraceLevel: r.Unset.TraceLevel, - } + IfExists: r.IfExists, + name: r.name, + ArgumentDataTypes: r.ArgumentDataTypes, + RenameTo: r.RenameTo, + SetComment: r.SetComment, + SetLogLevel: r.SetLogLevel, + SetTraceLevel: r.SetTraceLevel, + SetSecure: r.SetSecure, + UnsetSecure: r.UnsetSecure, + UnsetLogLevel: r.UnsetLogLevel, + UnsetTraceLevel: r.UnsetTraceLevel, + UnsetComment: r.UnsetComment, + SetTags: r.SetTags, + UnsetTags: r.UnsetTags, } return opts } func (r *DropFunctionRequest) toOpts() *DropFunctionOptions { opts := &DropFunctionOptions{ - IfExists: r.IfExists, - name: r.name, - } - if r.ArgumentTypes != nil { - s := make([]FunctionArgumentType, len(r.ArgumentTypes)) - for i, v := range r.ArgumentTypes { - s[i] = FunctionArgumentType{ - ArgDataType: v.ArgDataType, - } - } - opts.ArgumentTypes = s + IfExists: r.IfExists, + name: r.name, + ArgumentDataTypes: r.ArgumentDataTypes, } return opts } @@ -495,11 +406,11 @@ func (r *ShowFunctionRequest) toOpts() *ShowFunctionOptions { } func (r functionRow) convert() *Function { - return &Function{ + e := &Function{ CreatedOn: r.CreatedOn, Name: r.Name, SchemaName: r.SchemaName, - IsBuiltIn: r.IsBuiltIn == "Y", + IsBuiltin: r.IsBuiltin == "Y", IsAggregate: r.IsAggregate == "Y", IsAnsi: r.IsAnsi == "Y", MinNumArguments: r.MinNumArguments, @@ -509,25 +420,22 @@ func (r functionRow) convert() *Function { CatalogName: r.CatalogName, IsTableFunction: r.IsTableFunction == "Y", ValidForClustering: r.ValidForClustering == "Y", - IsSecure: r.IsSecure == "Y", IsExternalFunction: r.IsExternalFunction == "Y", Language: r.Language, - IsMemoizable: r.IsMemoizable == "Y", } + if r.IsSecure.Valid { + e.IsSecure = r.IsSecure.String == "Y" + } + if r.IsMemoizable.Valid { + e.IsMemoizable = r.IsMemoizable.String == "Y" + } + return e } func (r *DescribeFunctionRequest) toOpts() *DescribeFunctionOptions { opts := &DescribeFunctionOptions{ - name: r.name, - } - if r.ArgumentTypes != nil { - s := make([]FunctionArgumentType, len(r.ArgumentTypes)) - for i, v := range r.ArgumentTypes { - s[i] = FunctionArgumentType{ - ArgDataType: v.ArgDataType, - } - } - opts.ArgumentTypes = s + name: r.name, + ArgumentDataTypes: r.ArgumentDataTypes, } return opts } diff --git a/pkg/sdk/functions_validations_gen.go b/pkg/sdk/functions_validations_gen.go index 0ec3c77252..c92298b1b0 100644 --- a/pkg/sdk/functions_validations_gen.go +++ b/pkg/sdk/functions_validations_gen.go @@ -1,29 +1,20 @@ package sdk +import "errors" + var ( - _ validatable = new(CreateFunctionForJavaFunctionOptions) - _ validatable = new(CreateFunctionForJavascriptFunctionOptions) - _ validatable = new(CreateFunctionForPythonFunctionOptions) - _ validatable = new(CreateFunctionForScalaFunctionOptions) - _ validatable = new(CreateFunctionForSQLFunctionOptions) + _ validatable = new(CreateForJavaFunctionOptions) + _ validatable = new(CreateForJavascriptFunctionOptions) + _ validatable = new(CreateForPythonFunctionOptions) + _ validatable = new(CreateForScalaFunctionOptions) + _ validatable = new(CreateForSQLFunctionOptions) _ validatable = new(AlterFunctionOptions) _ validatable = new(DropFunctionOptions) _ validatable = new(ShowFunctionOptions) _ validatable = new(DescribeFunctionOptions) ) -func (v *FunctionReturns) validate() error { - if v == nil { - return ErrNilOptions - } - var errs []error - if ok := exactlyOneValueSet(v.ResultDataType, v.Table); !ok { - errs = append(errs, errOneOf("Returns.ResultDataType", "Returns.Table")) - } - return JoinErrors(errs...) -} - -func (opts *CreateFunctionForJavaFunctionOptions) validate() error { +func (opts *CreateForJavaFunctionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -31,13 +22,29 @@ func (opts *CreateFunctionForJavaFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if err := opts.Returns.validate(); err != nil { - errs = append(errs, err) + if everyValueSet(opts.OrReplace, opts.IfNotExists) { + errs = append(errs, errOneOf("CreateForJavaFunctionOptions", "OrReplace", "IfNotExists")) + } + if valueSet(opts.Returns) { + if !exactlyOneValueSet(opts.Returns.ResultDataType, opts.Returns.Table) { + errs = append(errs, errExactlyOneOf("CreateForJavaFunctionOptions.Returns", "ResultDataType", "Table")) + } + } + if opts.FunctionDefinition == nil { + if opts.TargetPath != nil { + errs = append(errs, errors.New("TARGET_PATH must be nil when AS is nil")) + } + if len(opts.Packages) > 0 { + errs = append(errs, errors.New("PACKAGES must be empty when AS is nil")) + } + if len(opts.Imports) == 0 { + errs = append(errs, errors.New("IMPORTS must not be empty when AS is nil")) + } } return JoinErrors(errs...) } -func (opts *CreateFunctionForJavascriptFunctionOptions) validate() error { +func (opts *CreateForJavascriptFunctionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -45,13 +52,15 @@ func (opts *CreateFunctionForJavascriptFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if err := opts.Returns.validate(); err != nil { - errs = append(errs, err) + if valueSet(opts.Returns) { + if !exactlyOneValueSet(opts.Returns.ResultDataType, opts.Returns.Table) { + errs = append(errs, errExactlyOneOf("CreateForJavascriptFunctionOptions.Returns", "ResultDataType", "Table")) + } } return JoinErrors(errs...) } -func (opts *CreateFunctionForPythonFunctionOptions) validate() error { +func (opts *CreateForPythonFunctionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -59,13 +68,23 @@ func (opts *CreateFunctionForPythonFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if err := opts.Returns.validate(); err != nil { - errs = append(errs, err) + if everyValueSet(opts.OrReplace, opts.IfNotExists) { + errs = append(errs, errOneOf("CreateForPythonFunctionOptions", "OrReplace", "IfNotExists")) + } + if valueSet(opts.Returns) { + if !exactlyOneValueSet(opts.Returns.ResultDataType, opts.Returns.Table) { + errs = append(errs, errExactlyOneOf("CreateForPythonFunctionOptions.Returns", "ResultDataType", "Table")) + } + } + if opts.FunctionDefinition == nil { + if len(opts.Imports) == 0 { + errs = append(errs, errors.New("IMPORTS must not be empty when AS is nil")) + } } return JoinErrors(errs...) } -func (opts *CreateFunctionForScalaFunctionOptions) validate() error { +func (opts *CreateForScalaFunctionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -73,13 +92,24 @@ func (opts *CreateFunctionForScalaFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if err := opts.Returns.validate(); err != nil { - errs = append(errs, err) + if everyValueSet(opts.OrReplace, opts.IfNotExists) { + errs = append(errs, errOneOf("CreateForScalaFunctionOptions", "OrReplace", "IfNotExists")) + } + if opts.FunctionDefinition == nil { + if opts.TargetPath != nil { + errs = append(errs, errors.New("TARGET_PATH must be nil when AS is nil")) + } + if len(opts.Packages) > 0 { + errs = append(errs, errors.New("PACKAGES must be empty when AS is nil")) + } + if len(opts.Imports) == 0 { + errs = append(errs, errors.New("IMPORTS must not be empty when AS is nil")) + } } return JoinErrors(errs...) } -func (opts *CreateFunctionForSQLFunctionOptions) validate() error { +func (opts *CreateForSQLFunctionOptions) validate() error { if opts == nil { return ErrNilOptions } @@ -87,8 +117,10 @@ func (opts *CreateFunctionForSQLFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if err := opts.Returns.validate(); err != nil { - errs = append(errs, err) + if valueSet(opts.Returns) { + if !exactlyOneValueSet(opts.Returns.ResultDataType, opts.Returns.Table) { + errs = append(errs, errExactlyOneOf("CreateForSQLFunctionOptions.Returns", "ResultDataType", "Table")) + } } return JoinErrors(errs...) } @@ -101,8 +133,11 @@ func (opts *AlterFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } - if !exactlyOneValueSet(opts.Set, opts.Unset, opts.SetTags, opts.UnsetTags, opts.RenameTo) { - errs = append(errs, errExactlyOneOf("Set", "Unset", "SetTags", "UnsetTags", "RenameTo")) + if opts.RenameTo != nil && !ValidObjectIdentifier(opts.RenameTo) { + errs = append(errs, ErrInvalidObjectIdentifier) + } + if !exactlyOneValueSet(opts.RenameTo, opts.SetComment, opts.SetLogLevel, opts.SetTraceLevel, opts.SetSecure, opts.UnsetLogLevel, opts.UnsetTraceLevel, opts.UnsetSecure, opts.UnsetComment, opts.SetTags, opts.UnsetTags) { + errs = append(errs, errExactlyOneOf("AlterFunctionOptions", "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "SetSecure", "UnsetLogLevel", "UnsetTraceLevel", "UnsetSecure", "UnsetComment", "SetTags", "UnsetTags")) } return JoinErrors(errs...) } @@ -123,9 +158,6 @@ func (opts *ShowFunctionOptions) validate() error { return ErrNilOptions } var errs []error - if valueSet(opts.Like) && !valueSet(opts.Like.Pattern) { - errs = append(errs, ErrPatternRequiredForLikeKeyword) - } return JoinErrors(errs...) } diff --git a/pkg/sdk/poc/generator/keyword_builders.go b/pkg/sdk/poc/generator/keyword_builders.go index db58de2f25..dadb98d34f 100644 --- a/pkg/sdk/poc/generator/keyword_builders.go +++ b/pkg/sdk/poc/generator/keyword_builders.go @@ -79,7 +79,7 @@ func (v *QueryStruct) SetTags() *QueryStruct { } func (v *QueryStruct) OptionalSetTags() *QueryStruct { - return v.setTags(nil) + return v.setTags(KeywordOptions().SQL("SET TAG")) } func (v *QueryStruct) setTags(transformer *KeywordTransformer) *QueryStruct { @@ -91,7 +91,7 @@ func (v *QueryStruct) UnsetTags() *QueryStruct { } func (v *QueryStruct) OptionalUnsetTags() *QueryStruct { - return v.unsetTags(nil) + return v.unsetTags(KeywordOptions().SQL("UNSET TAG")) } func (v *QueryStruct) unsetTags(transformer *KeywordTransformer) *QueryStruct { diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index 6c7a210b39..bd021a6dc7 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -18,13 +18,11 @@ func TestInt_CreateFunctions(t *testing.T) { client := testClient(t) ctx := context.Background() - cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, argumentTypes []sdk.DataType) func() { + databaseTest, schemaTest := testDb(t), testSchema(t) + + cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, dts []sdk.DataType) func() { return func() { - es := []sdk.FunctionArgumentTypeRequest{} - for _, item := range argumentTypes { - es = append(es, *sdk.NewFunctionArgumentTypeRequest().WithArgDataType(item)) - } - err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id).WithArgumentTypes(es)) + err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id, dts)) if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { return } @@ -34,123 +32,133 @@ func TestInt_CreateFunctions(t *testing.T) { t.Run("create function for Java", func(t *testing.T) { name := "echo_varchar" - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` - class TestFunc { - public static String echoVarchar(String x) { - return x; - } - }` +class TestFunc { + public static String echoVarchar(String x) { + return x; + } +}` target := fmt.Sprintf("@~/tf-%d.jar", time.Now().Unix()) - returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVARCHAR) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR).WithDefault(sdk.String("abc")) - request := sdk.NewCreateFunctionForJavaFunctionRequest(id, returnsRequest, "TestFunc.echoVarchar", definition). + dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeVARCHAR) + returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) + argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeVARCHAR).WithDefaultValue(sdk.String("'abc'")) + request := sdk.NewCreateForJavaFunctionRequest(id, *returns, "TestFunc.echoVarchar"). WithOrReplace(sdk.Bool(true)). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput). - WithTargetPath(&target) - err := client.Functions.CreateFunctionForJava(ctx, request) + WithArguments([]sdk.FunctionArgumentRequest{*argument}). + WithNullInputBehavior(sdk.NullInputBehaviorPointer(sdk.NullInputBehaviorCalledOnNullInput)). + WithTargetPath(&target). + WithFunctionDefinition(&definition) + err := client.Functions.CreateForJava(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"VARCHAR"})) - functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + function, err := client.Functions.ShowByID(ctx, id) require.NoError(t, err) - require.Equal(t, 1, len(functions)) + require.Equal(t, id.Name(), function.Name) + require.Equal(t, "JAVA", function.Language) }) t.Run("create function for Javascript", func(t *testing.T) { name := "js_factorial" - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` - if (D <= 0) { - return 1; - } else { - var result = 1; - for (var i = 2; i <= D; i++) { - result = result * i; - } - return result; - } - ` - returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("d").WithArgDataType(sdk.DataTypeFloat) - request := sdk.NewCreateFunctionForJavascriptFunctionRequest(id, returnsRequest, definition). +if (D <= 0) { + return 1; +} else { + var result = 1; + for (var i = 2; i <= D; i++) { + result = result * i; + } + return result; +}` + + dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) + returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) + argument := sdk.NewFunctionArgumentRequest("d", sdk.DataTypeFloat) + request := sdk.NewCreateForJavascriptFunctionRequest(id, *returns, &definition). WithOrReplace(sdk.Bool(true)). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithNullInputBehavior(sdk.FunctionNullInputBehaviorCalledOnNullInput) - err := client.Functions.CreateFunctionForJavascript(ctx, request) + WithArguments([]sdk.FunctionArgumentRequest{*argument}). + WithNullInputBehavior(sdk.NullInputBehaviorPointer(sdk.NullInputBehaviorCalledOnNullInput)) + err := client.Functions.CreateForJavascript(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) - functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + function, err := client.Functions.ShowByID(ctx, id) require.NoError(t, err) - require.Equal(t, 1, len(functions)) + require.Equal(t, id.Name(), function.Name) + require.Equal(t, "JAVASCRIPT", function.Language) }) t.Run("create function for Python", func(t *testing.T) { name := random.StringN(8) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` def dump(i): - print("Hello World!") - ` - returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVariant) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("i").WithArgDataType(sdk.DataTypeNumber) - request := sdk.NewCreateFunctionForPythonFunctionRequest(id, returnsRequest, "3.8", "dump", definition). + print("Hello World!")` + dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeVariant) + returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) + argument := sdk.NewFunctionArgumentRequest("i", sdk.DataTypeNumber) + request := sdk.NewCreateForPythonFunctionRequest(id, *returns, "3.8", "dump"). WithOrReplace(sdk.Bool(true)). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}) - err := client.Functions.CreateFunctionForPython(ctx, request) + WithArguments([]sdk.FunctionArgumentRequest{*argument}). + WithFunctionDefinition(&definition) + err := client.Functions.CreateForPython(ctx, request) require.NoError(t, err) t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"int"})) - functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + function, err := client.Functions.ShowByID(ctx, id) require.NoError(t, err) - require.Equal(t, 1, len(functions)) + require.Equal(t, id.Name(), function.Name) + require.Equal(t, "PYTHON", function.Language) }) t.Run("create function for Scala", func(t *testing.T) { name := "echo_varchar" - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` - class Echo { - def echoVarchar(x : String): String = { - return x - } - } - ` - returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeVARCHAR) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeVARCHAR) - request := sdk.NewCreateFunctionForScalaFunctionRequest(id, returnsRequest, "Echo.echoVarchar", definition). +class Echo { + def echoVarchar(x : String): String = { + return x + } +}` + + argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeVARCHAR) + request := sdk.NewCreateForScalaFunctionRequest(id, sdk.DataTypeVARCHAR, "Echo.echoVarchar"). WithOrReplace(sdk.Bool(true)). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). - WithRuntimeVersion(sdk.String("2.12")) - err := client.Functions.CreateFunctionForScala(ctx, request) + WithArguments([]sdk.FunctionArgumentRequest{*argument}). + WithRuntimeVersion(sdk.String("2.12")). + WithFunctionDefinition(&definition) + err := client.Functions.CreateForScala(ctx, request) require.NoError(t, err) - t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"VARCHAR"})) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeVARCHAR})) - functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) + function, err := client.Functions.ShowByID(ctx, id) require.NoError(t, err) - require.Equal(t, 1, len(functions)) + require.Equal(t, id.Name(), function.Name) + require.Equal(t, "SCALA", function.Language) }) t.Run("create function for SQL", func(t *testing.T) { name := random.String() - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := "3.141592654::FLOAT" - returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeFloat) - request := sdk.NewCreateFunctionForSQLFunctionRequest(id, returnsRequest, definition). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + + dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) + returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) + argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeFloat) + request := sdk.NewCreateForSQLFunctionRequest(id, *returns, &definition). + WithArguments([]sdk.FunctionArgumentRequest{*argument}). WithOrReplace(sdk.Bool(true)). WithComment(sdk.String("comment")) - err := client.Functions.CreateFunctionForSQL(ctx, request) + err := client.Functions.CreateForSQL(ctx, request) require.NoError(t, err) - t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"FLOAT"})) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) function, err := client.Functions.ShowByID(ctx, id) require.NoError(t, err) @@ -159,20 +167,41 @@ def dump(i): }) } -func TestInt_AlterAndShowFunctions(t *testing.T) { +func TestInt_OtherFunctions(t *testing.T) { client := testClient(t) - ctx := context.Background() + ctx := testContext(t) - tagTest, tagCleanup := createTag(t, client, testDb(t), testSchema(t)) + databaseTest, schemaTest := testDb(t), testSchema(t) + tagTest, tagCleanup := createTag(t, client, databaseTest, schemaTest) t.Cleanup(tagCleanup) - cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, argumentTypes []sdk.DataType) func() { + assertFunction := func(t *testing.T, id sdk.SchemaObjectIdentifier, secure bool) { + t.Helper() + + function, err := client.Functions.ShowByID(ctx, id) + require.NoError(t, err) + + assert.NotEmpty(t, function.CreatedOn) + assert.Equal(t, id.Name(), function.Name) + assert.Equal(t, false, function.IsBuiltin) + assert.Equal(t, false, function.IsAggregate) + assert.Equal(t, false, function.IsAnsi) + assert.Equal(t, 1, function.MinNumArguments) + assert.Equal(t, 1, function.MaxNumArguments) + assert.NotEmpty(t, function.Arguments) + assert.NotEmpty(t, function.Description) + assert.NotEmpty(t, function.CatalogName) + assert.Equal(t, false, function.IsTableFunction) + assert.Equal(t, false, function.ValidForClustering) + assert.Equal(t, secure, function.IsSecure) + assert.Equal(t, false, function.IsExternalFunction) + assert.Equal(t, "SQL", function.Language) + assert.Equal(t, false, function.IsMemoizable) + } + + cleanupFunctionHandle := func(id sdk.SchemaObjectIdentifier, dts []sdk.DataType) func() { return func() { - es := []sdk.FunctionArgumentTypeRequest{} - for _, item := range argumentTypes { - es = append(es, *sdk.NewFunctionArgumentTypeRequest().WithArgDataType(item)) - } - err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id).WithArgumentTypes(es)) + err := client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id, dts)) if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) { return } @@ -182,15 +211,17 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { createFunctionForSQLHandle := func(t *testing.T, cleanup bool) *sdk.Function { t.Helper() + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(4)) definition := "3.141592654::FLOAT" - returnsRequest := sdk.NewFunctionReturnsRequest().WithResultDataType(sdk.DataTypeFloat) - argumentRequest := sdk.NewFunctionArgumentRequest().WithArgName("x").WithArgDataType(sdk.DataTypeFloat) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, random.String()) - request := sdk.NewCreateFunctionForSQLFunctionRequest(id, returnsRequest, definition). - WithArguments([]sdk.FunctionArgumentRequest{*argumentRequest}). + + dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) + returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) + argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeFloat) + request := sdk.NewCreateForSQLFunctionRequest(id, *returns, &definition). + WithArguments([]sdk.FunctionArgumentRequest{*argument}). WithOrReplace(sdk.Bool(true)) - err := client.Functions.CreateFunctionForSQL(ctx, request) + err := client.Functions.CreateForSQL(ctx, request) require.NoError(t, err) if cleanup { t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) @@ -200,21 +231,20 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { return function } - defaultArgumentTypes := []sdk.FunctionArgumentTypeRequest{ - *sdk.NewFunctionArgumentTypeRequest().WithArgDataType(sdk.DataTypeFloat), + defaultAlterRequest := func(id sdk.SchemaObjectIdentifier) *sdk.AlterFunctionRequest { + return sdk.NewAlterFunctionRequest(id, []sdk.DataType{sdk.DataTypeFloat}) } t.Run("alter function: rename", func(t *testing.T) { f := createFunctionForSQLHandle(t, false) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - nid := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, random.String()) - request := sdk.NewAlterFunctionRequest(id).WithRenameTo(&nid).WithArgumentTypes(defaultArgumentTypes) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + nid := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(3)) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithRenameTo(&nid)) if err != nil { - t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{"FLOAT"})) + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) } else { - t.Cleanup(cleanupFunctionHandle(nid, []sdk.DataType{"FLOAT"})) + t.Cleanup(cleanupFunctionHandle(nid, []sdk.DataType{sdk.DataTypeFloat})) } require.NoError(t, err) @@ -229,111 +259,95 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { t.Run("alter function: set log level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - set := sdk.NewFunctionSetRequest().WithLogLevel(sdk.String("DEBUG")) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetLogLevel(sdk.String("DEBUG"))) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("alter function: unset log level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - unset := sdk.NewFunctionUnsetRequest().WithLogLevel(sdk.Bool(true)) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetLogLevel(sdk.Bool(true))) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("alter function: set trace level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - set := sdk.NewFunctionSetRequest().WithTraceLevel(sdk.String("ALWAYS")) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetTraceLevel(sdk.String("ALWAYS"))) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("alter function: unset trace level", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - unset := sdk.NewFunctionUnsetRequest().WithTraceLevel(sdk.Bool(true)) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetTraceLevel(sdk.Bool(true))) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("alter function: set comment", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - set := sdk.NewFunctionSetRequest().WithComment(sdk.String("comment")) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetComment(sdk.String("test comment"))) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("alter function: unset comment", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - unset := sdk.NewFunctionUnsetRequest().WithComment(sdk.Bool(true)) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetComment(sdk.Bool(true))) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("alter function: set secure", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - set := sdk.NewFunctionSetRequest().WithSecure(sdk.Bool(true)) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSet(set) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetSecure(sdk.Bool(true))) require.NoError(t, err) - - e, err := client.Functions.ShowByID(ctx, id) - require.NoError(t, err) - require.Equal(t, true, e.IsSecure) + assertFunction(t, id, true) }) t.Run("alter function: unset secure", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) - unset := sdk.NewFunctionUnsetRequest().WithSecure(sdk.Bool(true)) - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnset(unset) - err := client.Functions.Alter(ctx, request) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetSecure(sdk.Bool(true))) require.NoError(t, err) - - e, err := client.Functions.ShowByID(ctx, id) - require.NoError(t, err) - require.Equal(t, false, e.IsSecure) + assertFunction(t, id, false) }) t.Run("alter function: set and unset tags", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) setTags := []sdk.TagAssociation{ { Name: tagTest.ID(), - Value: "abc", + Value: "v1", }, } - request := sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithSetTags(setTags) - err := client.Functions.Alter(ctx, request) + err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetTags(setTags)) require.NoError(t, err) + assertFunction(t, id, false) unsetTags := []sdk.ObjectIdentifier{ tagTest.ID(), } - request = sdk.NewAlterFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes).WithUnsetTags(unsetTags) - err = client.Functions.Alter(ctx, request) + err = client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetTags(unsetTags)) require.NoError(t, err) + assertFunction(t, id, false) }) t.Run("show function for SQL: without like", func(t *testing.T) { @@ -352,7 +366,7 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { f1 := createFunctionForSQLHandle(t, true) f2 := createFunctionForSQLHandle(t, true) - functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(f1.Name)) + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(&sdk.Like{Pattern: &f1.Name})) require.NoError(t, err) require.Equal(t, 1, len(functions)) @@ -361,18 +375,25 @@ func TestInt_AlterAndShowFunctions(t *testing.T) { }) t.Run("show function for SQL: no matches", func(t *testing.T) { - functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(random.String())) + functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(&sdk.Like{Pattern: sdk.String(random.String())})) require.NoError(t, err) require.Equal(t, 0, len(functions)) }) t.Run("describe function for SQL", func(t *testing.T) { f := createFunctionForSQLHandle(t, true) - id := sdk.NewSchemaObjectIdentifier(testDb(t).Name, testSchema(t).Name, f.Name) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) - request := sdk.NewDescribeFunctionRequest(id).WithArgumentTypes(defaultArgumentTypes) + request := sdk.NewDescribeFunctionRequest(id, []sdk.DataType{sdk.DataTypeFloat}) details, err := client.Functions.Describe(ctx, request) require.NoError(t, err) - require.Greater(t, len(details), 0) + pairs := make(map[string]string) + for _, detail := range details { + pairs[detail.Property] = detail.Value + } + require.Equal(t, "SQL", pairs["language"]) + require.Equal(t, "FLOAT", pairs["returns"]) + require.Equal(t, "3.141592654::FLOAT", pairs["body"]) + require.Equal(t, "(X FLOAT)", pairs["signature"]) }) } From 7d9ca23017e75eec10b86e7bf5d6ab6d9ff572e4 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Wed, 6 Dec 2023 23:35:34 -0800 Subject: [PATCH 04/10] update error func --- pkg/sdk/functions_gen_test.go | 15 +++++++-------- pkg/sdk/functions_validations_gen.go | 18 ++++++++---------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go index bc04a445cc..87fcf93663 100644 --- a/pkg/sdk/functions_gen_test.go +++ b/pkg/sdk/functions_gen_test.go @@ -1,7 +1,6 @@ package sdk import ( - "errors" "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" @@ -35,9 +34,9 @@ func TestFunctions_CreateForJava(t *testing.T) { Package: "com.snowflake:snowpark:1.2.0", }, } - assertOptsInvalidJoinedErrors(t, opts, errors.New("TARGET_PATH must be nil when AS is nil")) - assertOptsInvalidJoinedErrors(t, opts, errors.New("PACKAGES must be empty when AS is nil")) - assertOptsInvalidJoinedErrors(t, opts, errors.New("IMPORTS must not be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("TARGET_PATH must be nil when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("PACKAGES must be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) t.Run("all options", func(t *testing.T) { @@ -180,7 +179,7 @@ func TestFunctions_CreateForPython(t *testing.T) { Package: "com.snowflake:snowpark:1.2.0", }, } - assertOptsInvalidJoinedErrors(t, opts, errors.New("IMPORTS must not be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) t.Run("all options", func(t *testing.T) { @@ -269,9 +268,9 @@ func TestFunctions_CreateForScala(t *testing.T) { Package: "com.snowflake:snowpark:1.2.0", }, } - assertOptsInvalidJoinedErrors(t, opts, errors.New("TARGET_PATH must be nil when AS is nil")) - assertOptsInvalidJoinedErrors(t, opts, errors.New("PACKAGES must be empty when AS is nil")) - assertOptsInvalidJoinedErrors(t, opts, errors.New("IMPORTS must not be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("TARGET_PATH must be nil when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("PACKAGES must be empty when AS is nil")) + assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) t.Run("all options", func(t *testing.T) { diff --git a/pkg/sdk/functions_validations_gen.go b/pkg/sdk/functions_validations_gen.go index c92298b1b0..9f386988f4 100644 --- a/pkg/sdk/functions_validations_gen.go +++ b/pkg/sdk/functions_validations_gen.go @@ -1,7 +1,5 @@ package sdk -import "errors" - var ( _ validatable = new(CreateForJavaFunctionOptions) _ validatable = new(CreateForJavascriptFunctionOptions) @@ -32,13 +30,13 @@ func (opts *CreateForJavaFunctionOptions) validate() error { } if opts.FunctionDefinition == nil { if opts.TargetPath != nil { - errs = append(errs, errors.New("TARGET_PATH must be nil when AS is nil")) + errs = append(errs, NewError("TARGET_PATH must be nil when AS is nil")) } if len(opts.Packages) > 0 { - errs = append(errs, errors.New("PACKAGES must be empty when AS is nil")) + errs = append(errs, NewError("PACKAGES must be empty when AS is nil")) } if len(opts.Imports) == 0 { - errs = append(errs, errors.New("IMPORTS must not be empty when AS is nil")) + errs = append(errs, NewError("IMPORTS must not be empty when AS is nil")) } } return JoinErrors(errs...) @@ -78,7 +76,7 @@ func (opts *CreateForPythonFunctionOptions) validate() error { } if opts.FunctionDefinition == nil { if len(opts.Imports) == 0 { - errs = append(errs, errors.New("IMPORTS must not be empty when AS is nil")) + errs = append(errs, NewError("IMPORTS must not be empty when AS is nil")) } } return JoinErrors(errs...) @@ -97,13 +95,13 @@ func (opts *CreateForScalaFunctionOptions) validate() error { } if opts.FunctionDefinition == nil { if opts.TargetPath != nil { - errs = append(errs, errors.New("TARGET_PATH must be nil when AS is nil")) + errs = append(errs, NewError("TARGET_PATH must be nil when AS is nil")) } if len(opts.Packages) > 0 { - errs = append(errs, errors.New("PACKAGES must be empty when AS is nil")) + errs = append(errs, NewError("PACKAGES must be empty when AS is nil")) } if len(opts.Imports) == 0 { - errs = append(errs, errors.New("IMPORTS must not be empty when AS is nil")) + errs = append(errs, NewError("IMPORTS must not be empty when AS is nil")) } } return JoinErrors(errs...) @@ -134,7 +132,7 @@ func (opts *AlterFunctionOptions) validate() error { errs = append(errs, ErrInvalidObjectIdentifier) } if opts.RenameTo != nil && !ValidObjectIdentifier(opts.RenameTo) { - errs = append(errs, ErrInvalidObjectIdentifier) + errs = append(errs, errInvalidIdentifier("AlterFunctionOptions", "RenameTo")) } if !exactlyOneValueSet(opts.RenameTo, opts.SetComment, opts.SetLogLevel, opts.SetTraceLevel, opts.SetSecure, opts.UnsetLogLevel, opts.UnsetTraceLevel, opts.UnsetSecure, opts.UnsetComment, opts.SetTags, opts.UnsetTags) { errs = append(errs, errExactlyOneOf("AlterFunctionOptions", "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "SetSecure", "UnsetLogLevel", "UnsetTraceLevel", "UnsetSecure", "UnsetComment", "SetTags", "UnsetTags")) From 641b0b87b7211ce4f7df8959c0b9ee1409bc8b53 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Thu, 7 Dec 2023 23:41:35 -0800 Subject: [PATCH 05/10] add todo --- pkg/sdk/testint/functions_integration_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index bd021a6dc7..2fd670675e 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -14,6 +14,10 @@ import ( "github.com/stretchr/testify/require" ) +// todo: add tests for: +// - creating functions with different languages from stages i.e. [ TARGET_PATH = '' ] +// - execute and execute-immediate for scripting https://docs.snowflake.com/en/sql-reference/sql/execute-immediate + func TestInt_CreateFunctions(t *testing.T) { client := testClient(t) ctx := context.Background() From f1757e48491ae8455d2349273fe5443091903afa Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Thu, 7 Dec 2023 23:44:26 -0800 Subject: [PATCH 06/10] add todo --- pkg/sdk/testint/functions_integration_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index 2fd670675e..378ba07e49 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -14,9 +14,11 @@ import ( "github.com/stretchr/testify/require" ) -// todo: add tests for: -// - creating functions with different languages from stages i.e. [ TARGET_PATH = '' ] -// - execute and execute-immediate for scripting https://docs.snowflake.com/en/sql-reference/sql/execute-immediate +/* +todo: add tests for: + - creating functions with different languages (java, javascript, python, scala, sql) from stages using [ TARGET_PATH = '' ] + - execute and execute-immediate for scripting https://docs.snowflake.com/en/sql-reference/sql/execute-immediate +*/ func TestInt_CreateFunctions(t *testing.T) { client := testClient(t) From 571486b11b7ec8825b5156f3454475c09ae51819 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 18 Dec 2023 01:53:53 -0800 Subject: [PATCH 07/10] update --- pkg/sdk/functions_gen_test.go | 44 ++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go index 87fcf93663..12e66b6588 100644 --- a/pkg/sdk/functions_gen_test.go +++ b/pkg/sdk/functions_gen_test.go @@ -182,6 +182,24 @@ func TestFunctions_CreateForPython(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) + t.Run("validation: returns", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeFloat, + }, + Table: &FunctionReturnsTable{ + Columns: []FunctionColumn{ + { + ColumnName: "country_code", + ColumnDataType: DataTypeVARCHAR, + }, + }, + }, + } + assertOptsInvalidJoinedErrors(t, opts, ErrExactlyOneOf("CreateForPythonFunctionOptions.Returns", "ResultDataType", "Table")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) @@ -323,6 +341,24 @@ func TestFunctions_CreateForSQL(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: returns", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeFloat, + }, + Table: &FunctionReturnsTable{ + Columns: []FunctionColumn{ + { + ColumnName: "country_code", + ColumnDataType: DataTypeVARCHAR, + }, + }, + }, + } + assertOptsInvalidJoinedErrors(t, opts, ErrExactlyOneOf("CreateForSQLFunctionOptions.Returns", "ResultDataType", "Table")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) @@ -542,8 +578,14 @@ func TestFunctions_Describe(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) - t.Run("all options", func(t *testing.T) { + t.Run("all options no arguments", func(t *testing.T) { opts := defaultOpts() assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s`, id.FullyQualifiedName()) }) + + t.Run("all options with arguments", func(t *testing.T) { + opts := defaultOpts() + opts.ArgumentDataTypes = []DataType{DataTypeVARCHAR, DataTypeNumber} + assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s (%s, %s)`, id.FullyQualifiedName(), DataTypeVARCHAR, DataTypeNumber) + }) } From 2e6c0e9f20e7faff3ade26d6c46012df7f42b0bf Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Mon, 18 Dec 2023 03:39:20 -0800 Subject: [PATCH 08/10] fmt --- pkg/sdk/common_types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/common_types.go b/pkg/sdk/common_types.go index 7905fe2695..4a93ac05bc 100644 --- a/pkg/sdk/common_types.go +++ b/pkg/sdk/common_types.go @@ -197,4 +197,4 @@ func ReturnNullValuesPointer(v ReturnNullValues) *ReturnNullValues { type Secret struct { VariableName string `ddl:"keyword,single_quotes"` Name string `ddl:"parameter,no_quotes"` -} \ No newline at end of file +} From a5436b0cb951f8b7c65ce1ac3ad65a458351b1f9 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Tue, 19 Dec 2023 23:52:06 -0800 Subject: [PATCH 09/10] add validations --- pkg/sdk/functions_def.go | 24 ++-- pkg/sdk/functions_dto_builders_gen.go | 28 ++-- pkg/sdk/functions_dto_gen.go | 20 +-- pkg/sdk/functions_gen.go | 32 ++--- pkg/sdk/functions_gen_test.go | 125 ++++++++++++------ pkg/sdk/functions_impl_gen.go | 24 ++-- pkg/sdk/functions_validations_gen.go | 20 ++- pkg/sdk/testint/functions_integration_test.go | 65 ++++++--- 8 files changed, 212 insertions(+), 126 deletions(-) diff --git a/pkg/sdk/functions_def.go b/pkg/sdk/functions_def.go index 55f7250820..5dddb5b9b5 100644 --- a/pkg/sdk/functions_def.go +++ b/pkg/sdk/functions_def.go @@ -32,8 +32,8 @@ var functionReturns = g.NewQueryStruct("FunctionReturns"). ).WithValidation(g.ExactlyOneValueSet, "ResultDataType", "Table") var ( - functionImports = g.NewQueryStruct("FunctionImports").Text("Import", g.KeywordOptions().SingleQuotes()) - functionPackages = g.NewQueryStruct("FunctionPackages").Text("Package", g.KeywordOptions().SingleQuotes()) + functionImports = g.NewQueryStruct("FunctionImport").Text("Import", g.KeywordOptions().SingleQuotes()) + functionPackages = g.NewQueryStruct("FunctionPackage").Text("Package", g.KeywordOptions().SingleQuotes()) ) var FunctionsDef = g.NewInterface( @@ -54,7 +54,7 @@ var FunctionsDef = g.NewInterface( ListQueryStructField( "Arguments", functionArgument, - g.ParameterOptions().Parentheses().NoEquals()). + g.ListOptions().MustParentheses()). OptionalSQL("COPY GRANTS"). QueryStructField( "Returns", @@ -83,6 +83,7 @@ var FunctionsDef = g.NewInterface( OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")). WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ValidateValueSet, "Handler"). WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"), ).CustomOperation( "CreateForJavascript", @@ -97,7 +98,7 @@ var FunctionsDef = g.NewInterface( ListQueryStructField( "Arguments", functionArgument, - g.ParameterOptions().Parentheses().NoEquals()). + g.ListOptions().MustParentheses()). OptionalSQL("COPY GRANTS"). QueryStructField( "Returns", @@ -109,7 +110,8 @@ var FunctionsDef = g.NewInterface( PredefinedQueryStructField("NullInputBehavior", "*NullInputBehavior", g.KeywordOptions()). PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()). + PredefinedQueryStructField("FunctionDefinition", "string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()). + WithValidation(g.ValidateValueSet, "FunctionDefinition"). WithValidation(g.ValidIdentifier, "name"), ).CustomOperation( "CreateForPython", @@ -125,7 +127,7 @@ var FunctionsDef = g.NewInterface( ListQueryStructField( "Arguments", functionArgument, - g.ParameterOptions().Parentheses().NoEquals()). + g.ListOptions().MustParentheses()). OptionalSQL("COPY GRANTS"). QueryStructField( "Returns", @@ -153,6 +155,8 @@ var FunctionsDef = g.NewInterface( ListAssignment("SECRETS", "Secret", g.ParameterOptions().Parentheses()). PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")). WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ValidateValueSet, "RuntimeVersion"). + WithValidation(g.ValidateValueSet, "Handler"). WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"), ).CustomOperation( "CreateForScala", @@ -168,7 +172,7 @@ var FunctionsDef = g.NewInterface( ListQueryStructField( "Arguments", functionArgument, - g.ParameterOptions().Parentheses().NoEquals()). + g.ListOptions().MustParentheses()). OptionalSQL("COPY GRANTS"). PredefinedQueryStructField("ResultDataType", "DataType", g.ParameterOptions().NoEquals().SQL("RETURNS").Required()). PredefinedQueryStructField("ReturnNullValues", "*ReturnNullValues", g.KeywordOptions()). @@ -191,6 +195,7 @@ var FunctionsDef = g.NewInterface( OptionalTextAssignment("TARGET_PATH", g.ParameterOptions().SingleQuotes()). PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS")). WithValidation(g.ValidIdentifier, "name"). + WithValidation(g.ValidateValueSet, "Handler"). WithValidation(g.ConflictingFields, "OrReplace", "IfNotExists"), ).CustomOperation( "CreateForSQL", @@ -205,7 +210,7 @@ var FunctionsDef = g.NewInterface( ListQueryStructField( "Arguments", functionArgument, - g.ParameterOptions().Parentheses().NoEquals()). + g.ListOptions().MustParentheses()). OptionalSQL("COPY GRANTS"). QueryStructField( "Returns", @@ -216,7 +221,8 @@ var FunctionsDef = g.NewInterface( PredefinedQueryStructField("ReturnResultsBehavior", "*ReturnResultsBehavior", g.KeywordOptions()). OptionalSQL("MEMOIZABLE"). OptionalTextAssignment("COMMENT", g.ParameterOptions().SingleQuotes()). - PredefinedQueryStructField("FunctionDefinition", "*string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()). + PredefinedQueryStructField("FunctionDefinition", "string", g.ParameterOptions().NoEquals().SingleQuotes().SQL("AS").Required()). + WithValidation(g.ValidateValueSet, "FunctionDefinition"). WithValidation(g.ValidIdentifier, "name"), ).AlterOperation( "https://docs.snowflake.com/en/sql-reference/sql/alter-function", diff --git a/pkg/sdk/functions_dto_builders_gen.go b/pkg/sdk/functions_dto_builders_gen.go index 60fade6825..4cb8f160fa 100644 --- a/pkg/sdk/functions_dto_builders_gen.go +++ b/pkg/sdk/functions_dto_builders_gen.go @@ -71,12 +71,12 @@ func (s *CreateForJavaFunctionRequest) WithComment(Comment *string) *CreateForJa return s } -func (s *CreateForJavaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithImports(Imports []FunctionImportRequest) *CreateForJavaFunctionRequest { s.Imports = Imports return s } -func (s *CreateForJavaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateForJavaFunctionRequest { +func (s *CreateForJavaFunctionRequest) WithPackages(Packages []FunctionPackageRequest) *CreateForJavaFunctionRequest { s.Packages = Packages return s } @@ -157,20 +157,20 @@ func NewFunctionColumnRequest( return &s } -func NewFunctionImportsRequest() *FunctionImportsRequest { - return &FunctionImportsRequest{} +func NewFunctionImportRequest() *FunctionImportRequest { + return &FunctionImportRequest{} } -func (s *FunctionImportsRequest) WithImport(Import string) *FunctionImportsRequest { +func (s *FunctionImportRequest) WithImport(Import string) *FunctionImportRequest { s.Import = Import return s } -func NewFunctionPackagesRequest() *FunctionPackagesRequest { - return &FunctionPackagesRequest{} +func NewFunctionPackageRequest() *FunctionPackageRequest { + return &FunctionPackageRequest{} } -func (s *FunctionPackagesRequest) WithPackage(Package string) *FunctionPackagesRequest { +func (s *FunctionPackageRequest) WithPackage(Package string) *FunctionPackageRequest { s.Package = Package return s } @@ -178,7 +178,7 @@ func (s *FunctionPackagesRequest) WithPackage(Package string) *FunctionPackagesR func NewCreateForJavascriptFunctionRequest( name SchemaObjectIdentifier, Returns FunctionReturnsRequest, - FunctionDefinition *string, + FunctionDefinition string, ) *CreateForJavascriptFunctionRequest { s := CreateForJavascriptFunctionRequest{} s.name = name @@ -296,12 +296,12 @@ func (s *CreateForPythonFunctionRequest) WithComment(Comment *string) *CreateFor return s } -func (s *CreateForPythonFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithImports(Imports []FunctionImportRequest) *CreateForPythonFunctionRequest { s.Imports = Imports return s } -func (s *CreateForPythonFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateForPythonFunctionRequest { +func (s *CreateForPythonFunctionRequest) WithPackages(Packages []FunctionPackageRequest) *CreateForPythonFunctionRequest { s.Packages = Packages return s } @@ -388,12 +388,12 @@ func (s *CreateForScalaFunctionRequest) WithComment(Comment *string) *CreateForS return s } -func (s *CreateForScalaFunctionRequest) WithImports(Imports []FunctionImportsRequest) *CreateForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithImports(Imports []FunctionImportRequest) *CreateForScalaFunctionRequest { s.Imports = Imports return s } -func (s *CreateForScalaFunctionRequest) WithPackages(Packages []FunctionPackagesRequest) *CreateForScalaFunctionRequest { +func (s *CreateForScalaFunctionRequest) WithPackages(Packages []FunctionPackageRequest) *CreateForScalaFunctionRequest { s.Packages = Packages return s } @@ -411,7 +411,7 @@ func (s *CreateForScalaFunctionRequest) WithFunctionDefinition(FunctionDefinitio func NewCreateForSQLFunctionRequest( name SchemaObjectIdentifier, Returns FunctionReturnsRequest, - FunctionDefinition *string, + FunctionDefinition string, ) *CreateForSQLFunctionRequest { s := CreateForSQLFunctionRequest{} s.name = name diff --git a/pkg/sdk/functions_dto_gen.go b/pkg/sdk/functions_dto_gen.go index 74a0713df5..78b8224c1a 100644 --- a/pkg/sdk/functions_dto_gen.go +++ b/pkg/sdk/functions_dto_gen.go @@ -28,8 +28,8 @@ type CreateForJavaFunctionRequest struct { ReturnResultsBehavior *ReturnResultsBehavior RuntimeVersion *string Comment *string - Imports []FunctionImportsRequest - Packages []FunctionPackagesRequest + Imports []FunctionImportRequest + Packages []FunctionPackageRequest Handler string // required ExternalAccessIntegrations []AccountObjectIdentifier Secrets []Secret @@ -61,11 +61,11 @@ type FunctionColumnRequest struct { ColumnDataType DataType // required } -type FunctionImportsRequest struct { +type FunctionImportRequest struct { Import string } -type FunctionPackagesRequest struct { +type FunctionPackageRequest struct { Package string } @@ -81,7 +81,7 @@ type CreateForJavascriptFunctionRequest struct { NullInputBehavior *NullInputBehavior ReturnResultsBehavior *ReturnResultsBehavior Comment *string - FunctionDefinition *string // required + FunctionDefinition string // required } type CreateForPythonFunctionRequest struct { @@ -98,8 +98,8 @@ type CreateForPythonFunctionRequest struct { ReturnResultsBehavior *ReturnResultsBehavior RuntimeVersion string // required Comment *string - Imports []FunctionImportsRequest - Packages []FunctionPackagesRequest + Imports []FunctionImportRequest + Packages []FunctionPackageRequest Handler string // required ExternalAccessIntegrations []AccountObjectIdentifier Secrets []Secret @@ -120,8 +120,8 @@ type CreateForScalaFunctionRequest struct { ReturnResultsBehavior *ReturnResultsBehavior RuntimeVersion *string Comment *string - Imports []FunctionImportsRequest - Packages []FunctionPackagesRequest + Imports []FunctionImportRequest + Packages []FunctionPackageRequest Handler string // required TargetPath *string FunctionDefinition *string @@ -139,7 +139,7 @@ type CreateForSQLFunctionRequest struct { ReturnResultsBehavior *ReturnResultsBehavior Memoizable *bool Comment *string - FunctionDefinition *string // required + FunctionDefinition string // required } type AlterFunctionRequest struct { diff --git a/pkg/sdk/functions_gen.go b/pkg/sdk/functions_gen.go index 3e57959dee..b0becf29f6 100644 --- a/pkg/sdk/functions_gen.go +++ b/pkg/sdk/functions_gen.go @@ -27,7 +27,7 @@ type CreateForJavaFunctionOptions struct { function bool `ddl:"static" sql:"FUNCTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + Arguments []FunctionArgument `ddl:"list,must_parentheses"` CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` ReturnNullValues *ReturnNullValues `ddl:"keyword"` @@ -36,8 +36,8 @@ type CreateForJavaFunctionOptions struct { ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` - Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Imports []FunctionImport `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackage `ddl:"parameter,parentheses" sql:"PACKAGES"` Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` Secrets []Secret `ddl:"parameter,parentheses" sql:"SECRETS"` @@ -69,11 +69,11 @@ type FunctionColumn struct { ColumnDataType DataType `ddl:"keyword,no_quotes"` } -type FunctionImports struct { +type FunctionImport struct { Import string `ddl:"keyword,single_quotes"` } -type FunctionPackages struct { +type FunctionPackage struct { Package string `ddl:"keyword,single_quotes"` } @@ -85,7 +85,7 @@ type CreateForJavascriptFunctionOptions struct { Secure *bool `ddl:"keyword" sql:"SECURE"` function bool `ddl:"static" sql:"FUNCTION"` name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + Arguments []FunctionArgument `ddl:"list,must_parentheses"` CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` ReturnNullValues *ReturnNullValues `ddl:"keyword"` @@ -93,7 +93,7 @@ type CreateForJavascriptFunctionOptions struct { NullInputBehavior *NullInputBehavior `ddl:"keyword"` ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // CreateForPythonFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-function#python-handler. @@ -105,7 +105,7 @@ type CreateForPythonFunctionOptions struct { function bool `ddl:"static" sql:"FUNCTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + Arguments []FunctionArgument `ddl:"list,must_parentheses"` CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` ReturnNullValues *ReturnNullValues `ddl:"keyword"` @@ -114,8 +114,8 @@ type CreateForPythonFunctionOptions struct { ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` RuntimeVersion string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` - Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Imports []FunctionImport `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackage `ddl:"parameter,parentheses" sql:"PACKAGES"` Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"parameter,parentheses" sql:"EXTERNAL_ACCESS_INTEGRATIONS"` Secrets []Secret `ddl:"parameter,parentheses" sql:"SECRETS"` @@ -131,7 +131,7 @@ type CreateForScalaFunctionOptions struct { function bool `ddl:"static" sql:"FUNCTION"` IfNotExists *bool `ddl:"keyword" sql:"IF NOT EXISTS"` name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + Arguments []FunctionArgument `ddl:"list,must_parentheses"` CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` ResultDataType DataType `ddl:"parameter,no_equals" sql:"RETURNS"` ReturnNullValues *ReturnNullValues `ddl:"keyword"` @@ -140,8 +140,8 @@ type CreateForScalaFunctionOptions struct { ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` RuntimeVersion *string `ddl:"parameter,single_quotes" sql:"RUNTIME_VERSION"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - Imports []FunctionImports `ddl:"parameter,parentheses" sql:"IMPORTS"` - Packages []FunctionPackages `ddl:"parameter,parentheses" sql:"PACKAGES"` + Imports []FunctionImport `ddl:"parameter,parentheses" sql:"IMPORTS"` + Packages []FunctionPackage `ddl:"parameter,parentheses" sql:"PACKAGES"` Handler string `ddl:"parameter,single_quotes" sql:"HANDLER"` TargetPath *string `ddl:"parameter,single_quotes" sql:"TARGET_PATH"` FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` @@ -155,14 +155,14 @@ type CreateForSQLFunctionOptions struct { Secure *bool `ddl:"keyword" sql:"SECURE"` function bool `ddl:"static" sql:"FUNCTION"` name SchemaObjectIdentifier `ddl:"identifier"` - Arguments []FunctionArgument `ddl:"parameter,parentheses,no_equals"` + Arguments []FunctionArgument `ddl:"list,must_parentheses"` CopyGrants *bool `ddl:"keyword" sql:"COPY GRANTS"` Returns FunctionReturns `ddl:"keyword" sql:"RETURNS"` ReturnNullValues *ReturnNullValues `ddl:"keyword"` ReturnResultsBehavior *ReturnResultsBehavior `ddl:"keyword"` Memoizable *bool `ddl:"keyword" sql:"MEMOIZABLE"` Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"` - FunctionDefinition *string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` + FunctionDefinition string `ddl:"parameter,single_quotes,no_equals" sql:"AS"` } // AlterFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/alter-function. @@ -191,7 +191,7 @@ type DropFunctionOptions struct { function bool `ddl:"static" sql:"FUNCTION"` IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` name SchemaObjectIdentifier `ddl:"identifier"` - ArgumentDataTypes []DataType `ddl:"keyword,parentheses"` + ArgumentDataTypes []DataType `ddl:"keyword,must_parentheses"` } // ShowFunctionOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-user-functions. diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go index 12e66b6588..965fda3df9 100644 --- a/pkg/sdk/functions_gen_test.go +++ b/pkg/sdk/functions_gen_test.go @@ -26,10 +26,16 @@ func TestFunctions_CreateForJava(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: returns", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("CreateForJavaFunctionOptions.Returns", "ResultDataType", "Table")) + }) + t.Run("validation: function definition", func(t *testing.T) { opts := defaultOpts() opts.TargetPath = String("@~/testfunc.jar") - opts.Packages = []FunctionPackages{ + opts.Packages = []FunctionPackage{ { Package: "com.snowflake:snowpark:1.2.0", }, @@ -39,6 +45,16 @@ func TestFunctions_CreateForJava(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) + t.Run("validation: options are missing", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeVARCHAR, + }, + } + assertOptsInvalidJoinedErrors(t, opts, errNotSet("CreateForJavaFunctionOptions", "Handler")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) @@ -75,12 +91,12 @@ func TestFunctions_CreateForJava(t *testing.T) { opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.RuntimeVersion = String("2.0") opts.Comment = String("comment") - opts.Imports = []FunctionImports{ + opts.Imports = []FunctionImport{ { Import: "@~/my_decrement_udf_package_dir/my_decrement_udf_jar.jar", }, } - opts.Packages = []FunctionPackages{ + opts.Packages = []FunctionPackage{ { Package: "com.snowflake:snowpark:1.2.0", }, @@ -125,6 +141,22 @@ func TestFunctions_CreateForJavascript(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("validation: returns", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("CreateForJavascriptFunctionOptions.Returns", "ResultDataType", "Table")) + }) + + t.Run("validation: options are missing", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeVARCHAR, + }, + } + assertOptsInvalidJoinedErrors(t, opts, errNotSet("CreateForJavascriptFunctionOptions", "FunctionDefinition")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) @@ -147,7 +179,7 @@ func TestFunctions_CreateForJavascript(t *testing.T) { opts.NullInputBehavior = NullInputBehaviorPointer(NullInputBehaviorCalledOnNullInput) opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.Comment = String("comment") - opts.FunctionDefinition = String("return 1;") + opts.FunctionDefinition = "return 1;" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (d FLOAT DEFAULT 1.0) COPY GRANTS RETURNS FLOAT NOT NULL LANGUAGE JAVASCRIPT CALLED ON NULL INPUT IMMUTABLE COMMENT = 'comment' AS 'return 1;'`, id.FullyQualifiedName()) }) } @@ -172,32 +204,31 @@ func TestFunctions_CreateForPython(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) - t.Run("validation: function definition", func(t *testing.T) { + t.Run("validation: returns", func(t *testing.T) { opts := defaultOpts() - opts.Packages = []FunctionPackages{ - { - Package: "com.snowflake:snowpark:1.2.0", - }, - } - assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) + opts.Returns = FunctionReturns{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("CreateForPythonFunctionOptions.Returns", "ResultDataType", "Table")) }) - t.Run("validation: returns", func(t *testing.T) { + t.Run("validation: options are missing", func(t *testing.T) { opts := defaultOpts() opts.Returns = FunctionReturns{ ResultDataType: &FunctionReturnsResultDataType{ - ResultDataType: DataTypeFloat, + ResultDataType: DataTypeVARCHAR, }, - Table: &FunctionReturnsTable{ - Columns: []FunctionColumn{ - { - ColumnName: "country_code", - ColumnDataType: DataTypeVARCHAR, - }, - }, + } + assertOptsInvalidJoinedErrors(t, opts, errNotSet("CreateForPythonFunctionOptions", "RuntimeVersion")) + assertOptsInvalidJoinedErrors(t, opts, errNotSet("CreateForPythonFunctionOptions", "Handler")) + }) + + t.Run("validation: function definition", func(t *testing.T) { + opts := defaultOpts() + opts.Packages = []FunctionPackage{ + { + Package: "com.snowflake:snowpark:1.2.0", }, } - assertOptsInvalidJoinedErrors(t, opts, ErrExactlyOneOf("CreateForPythonFunctionOptions.Returns", "ResultDataType", "Table")) + assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) t.Run("all options", func(t *testing.T) { @@ -223,7 +254,7 @@ func TestFunctions_CreateForPython(t *testing.T) { opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.RuntimeVersion = "3.8" opts.Comment = String("comment") - opts.Imports = []FunctionImports{ + opts.Imports = []FunctionImport{ { Import: "numpy", }, @@ -231,7 +262,7 @@ func TestFunctions_CreateForPython(t *testing.T) { Import: "pandas", }, } - opts.Packages = []FunctionPackages{ + opts.Packages = []FunctionPackage{ { Package: "numpy", }, @@ -281,7 +312,7 @@ func TestFunctions_CreateForScala(t *testing.T) { t.Run("validation: function definition", func(t *testing.T) { opts := defaultOpts() opts.TargetPath = String("@~/testfunc.jar") - opts.Packages = []FunctionPackages{ + opts.Packages = []FunctionPackage{ { Package: "com.snowflake:snowpark:1.2.0", }, @@ -291,6 +322,12 @@ func TestFunctions_CreateForScala(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, NewError("IMPORTS must not be empty when AS is nil")) }) + t.Run("validation: options are missing", func(t *testing.T) { + opts := defaultOpts() + opts.ResultDataType = DataTypeVARCHAR + assertOptsInvalidJoinedErrors(t, opts, errNotSet("CreateForScalaFunctionOptions", "Handler")) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.OrReplace = Bool(true) @@ -310,7 +347,7 @@ func TestFunctions_CreateForScala(t *testing.T) { opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.RuntimeVersion = String("2.0") opts.Comment = String("comment") - opts.Imports = []FunctionImports{ + opts.Imports = []FunctionImport{ { Import: "@udf_libs/echohandler.jar", }, @@ -342,21 +379,30 @@ func TestFunctions_CreateForSQL(t *testing.T) { }) t.Run("validation: returns", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{} + assertOptsInvalidJoinedErrors(t, opts, errExactlyOneOf("CreateForSQLFunctionOptions.Returns", "ResultDataType", "Table")) + }) + + t.Run("validation: options are missing", func(t *testing.T) { opts := defaultOpts() opts.Returns = FunctionReturns{ ResultDataType: &FunctionReturnsResultDataType{ - ResultDataType: DataTypeFloat, + ResultDataType: DataTypeVARCHAR, }, - Table: &FunctionReturnsTable{ - Columns: []FunctionColumn{ - { - ColumnName: "country_code", - ColumnDataType: DataTypeVARCHAR, - }, - }, + } + assertOptsInvalidJoinedErrors(t, opts, errNotSet("CreateForSQLFunctionOptions", "FunctionDefinition")) + }) + + t.Run("create with no arguments", func(t *testing.T) { + opts := defaultOpts() + opts.Returns = FunctionReturns{ + ResultDataType: &FunctionReturnsResultDataType{ + ResultDataType: DataTypeFloat, }, } - assertOptsInvalidJoinedErrors(t, opts, ErrExactlyOneOf("CreateForSQLFunctionOptions.Returns", "ResultDataType", "Table")) + opts.FunctionDefinition = "3.141592654::FLOAT" + assertOptsValidAndSQLEquals(t, opts, `CREATE FUNCTION %s () RETURNS FLOAT AS '3.141592654::FLOAT'`, id.FullyQualifiedName()) }) t.Run("all options", func(t *testing.T) { @@ -381,7 +427,7 @@ func TestFunctions_CreateForSQL(t *testing.T) { opts.ReturnResultsBehavior = ReturnResultsBehaviorPointer(ReturnResultsBehaviorImmutable) opts.Memoizable = Bool(true) opts.Comment = String("comment") - opts.FunctionDefinition = String("3.141592654::FLOAT") + opts.FunctionDefinition = "3.141592654::FLOAT" assertOptsValidAndSQLEquals(t, opts, `CREATE OR REPLACE TEMPORARY SECURE FUNCTION %s (message VARCHAR DEFAULT 'test') COPY GRANTS RETURNS FLOAT NOT NULL IMMUTABLE MEMOIZABLE COMMENT = 'comment' AS '3.141592654::FLOAT'`, id.FullyQualifiedName()) }) } @@ -578,14 +624,9 @@ func TestFunctions_Describe(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) - t.Run("all options no arguments", func(t *testing.T) { - opts := defaultOpts() - assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s`, id.FullyQualifiedName()) - }) - - t.Run("all options with arguments", func(t *testing.T) { + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.ArgumentDataTypes = []DataType{DataTypeVARCHAR, DataTypeNumber} - assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s (%s, %s)`, id.FullyQualifiedName(), DataTypeVARCHAR, DataTypeNumber) + assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s (VARCHAR, NUMBER)`, id.FullyQualifiedName()) }) } diff --git a/pkg/sdk/functions_impl_gen.go b/pkg/sdk/functions_impl_gen.go index 70426de5ae..650aef62e9 100644 --- a/pkg/sdk/functions_impl_gen.go +++ b/pkg/sdk/functions_impl_gen.go @@ -128,18 +128,18 @@ func (r *CreateForJavaFunctionRequest) toOpts() *CreateForJavaFunctionOptions { } } if r.Imports != nil { - s := make([]FunctionImports, len(r.Imports)) + s := make([]FunctionImport, len(r.Imports)) for i, v := range r.Imports { - s[i] = FunctionImports{ + s[i] = FunctionImport{ Import: v.Import, } } opts.Imports = s } if r.Packages != nil { - s := make([]FunctionPackages, len(r.Packages)) + s := make([]FunctionPackage, len(r.Packages)) for i, v := range r.Packages { - s[i] = FunctionPackages{ + s[i] = FunctionPackage{ Package: v.Package, } } @@ -248,18 +248,18 @@ func (r *CreateForPythonFunctionRequest) toOpts() *CreateForPythonFunctionOption } } if r.Imports != nil { - s := make([]FunctionImports, len(r.Imports)) + s := make([]FunctionImport, len(r.Imports)) for i, v := range r.Imports { - s[i] = FunctionImports{ + s[i] = FunctionImport{ Import: v.Import, } } opts.Imports = s } if r.Packages != nil { - s := make([]FunctionPackages, len(r.Packages)) + s := make([]FunctionPackage, len(r.Packages)) for i, v := range r.Packages { - s[i] = FunctionPackages{ + s[i] = FunctionPackage{ Package: v.Package, } } @@ -300,18 +300,18 @@ func (r *CreateForScalaFunctionRequest) toOpts() *CreateForScalaFunctionOptions opts.Arguments = s } if r.Imports != nil { - s := make([]FunctionImports, len(r.Imports)) + s := make([]FunctionImport, len(r.Imports)) for i, v := range r.Imports { - s[i] = FunctionImports{ + s[i] = FunctionImport{ Import: v.Import, } } opts.Imports = s } if r.Packages != nil { - s := make([]FunctionPackages, len(r.Packages)) + s := make([]FunctionPackage, len(r.Packages)) for i, v := range r.Packages { - s[i] = FunctionPackages{ + s[i] = FunctionPackage{ Package: v.Package, } } diff --git a/pkg/sdk/functions_validations_gen.go b/pkg/sdk/functions_validations_gen.go index 9f386988f4..3bf1a29ff9 100644 --- a/pkg/sdk/functions_validations_gen.go +++ b/pkg/sdk/functions_validations_gen.go @@ -20,6 +20,9 @@ func (opts *CreateForJavaFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } + if !valueSet(opts.Handler) { + errs = append(errs, errNotSet("CreateForJavaFunctionOptions", "Handler")) + } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateForJavaFunctionOptions", "OrReplace", "IfNotExists")) } @@ -47,6 +50,9 @@ func (opts *CreateForJavascriptFunctionOptions) validate() error { return ErrNilOptions } var errs []error + if !valueSet(opts.FunctionDefinition) { + errs = append(errs, errNotSet("CreateForJavascriptFunctionOptions", "FunctionDefinition")) + } if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } @@ -66,6 +72,12 @@ func (opts *CreateForPythonFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } + if !valueSet(opts.RuntimeVersion) { + errs = append(errs, errNotSet("CreateForPythonFunctionOptions", "RuntimeVersion")) + } + if !valueSet(opts.Handler) { + errs = append(errs, errNotSet("CreateForPythonFunctionOptions", "Handler")) + } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateForPythonFunctionOptions", "OrReplace", "IfNotExists")) } @@ -90,6 +102,9 @@ func (opts *CreateForScalaFunctionOptions) validate() error { if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } + if !valueSet(opts.Handler) { + errs = append(errs, errNotSet("CreateForScalaFunctionOptions", "Handler")) + } if everyValueSet(opts.OrReplace, opts.IfNotExists) { errs = append(errs, errOneOf("CreateForScalaFunctionOptions", "OrReplace", "IfNotExists")) } @@ -112,6 +127,9 @@ func (opts *CreateForSQLFunctionOptions) validate() error { return ErrNilOptions } var errs []error + if !valueSet(opts.FunctionDefinition) { + errs = append(errs, errNotSet("CreateForSQLFunctionOptions", "FunctionDefinition")) + } if !ValidObjectIdentifier(opts.name) { errs = append(errs, ErrInvalidObjectIdentifier) } @@ -132,7 +150,7 @@ func (opts *AlterFunctionOptions) validate() error { errs = append(errs, ErrInvalidObjectIdentifier) } if opts.RenameTo != nil && !ValidObjectIdentifier(opts.RenameTo) { - errs = append(errs, errInvalidIdentifier("AlterFunctionOptions", "RenameTo")) + errs = append(errs, ErrInvalidObjectIdentifier) } if !exactlyOneValueSet(opts.RenameTo, opts.SetComment, opts.SetLogLevel, opts.SetTraceLevel, opts.SetSecure, opts.UnsetLogLevel, opts.UnsetTraceLevel, opts.UnsetSecure, opts.UnsetComment, opts.SetTags, opts.UnsetTags) { errs = append(errs, errExactlyOneOf("AlterFunctionOptions", "RenameTo", "SetComment", "SetLogLevel", "SetTraceLevel", "SetSecure", "UnsetLogLevel", "UnsetTraceLevel", "UnsetSecure", "UnsetComment", "SetTags", "UnsetTags")) diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index 378ba07e49..7b16cac7dd 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -41,11 +41,11 @@ func TestInt_CreateFunctions(t *testing.T) { id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` -class TestFunc { - public static String echoVarchar(String x) { - return x; - } -}` + class TestFunc { + public static String echoVarchar(String x) { + return x; + } + }` target := fmt.Sprintf("@~/tf-%d.jar", time.Now().Unix()) dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeVARCHAR) returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) @@ -71,20 +71,20 @@ class TestFunc { id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` -if (D <= 0) { - return 1; -} else { - var result = 1; - for (var i = 2; i <= D; i++) { - result = result * i; - } - return result; -}` + if (D <= 0) { + return 1; + } else { + var result = 1; + for (var i = 2; i <= D; i++) { + result = result * i; + } + return result; + }` dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) argument := sdk.NewFunctionArgumentRequest("d", sdk.DataTypeFloat) - request := sdk.NewCreateForJavascriptFunctionRequest(id, *returns, &definition). + request := sdk.NewCreateForJavascriptFunctionRequest(id, *returns, definition). WithOrReplace(sdk.Bool(true)). WithArguments([]sdk.FunctionArgumentRequest{*argument}). WithNullInputBehavior(sdk.NullInputBehaviorPointer(sdk.NullInputBehaviorCalledOnNullInput)) @@ -127,11 +127,11 @@ def dump(i): id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) definition := ` -class Echo { - def echoVarchar(x : String): String = { - return x - } -}` + class Echo { + def echoVarchar(x : String): String = { + return x + } + }` argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeVARCHAR) request := sdk.NewCreateForScalaFunctionRequest(id, sdk.DataTypeVARCHAR, "Echo.echoVarchar"). @@ -158,7 +158,7 @@ class Echo { dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeFloat) - request := sdk.NewCreateForSQLFunctionRequest(id, *returns, &definition). + request := sdk.NewCreateForSQLFunctionRequest(id, *returns, definition). WithArguments([]sdk.FunctionArgumentRequest{*argument}). WithOrReplace(sdk.Bool(true)). WithComment(sdk.String("comment")) @@ -171,6 +171,27 @@ class Echo { require.Equal(t, id.Name(), function.Name) require.Equal(t, "SQL", function.Language) }) + + t.Run("create function for SQL with no arguments", func(t *testing.T) { + name := random.String() + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, name) + + definition := "3.141592654::FLOAT" + + dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) + returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) + request := sdk.NewCreateForSQLFunctionRequest(id, *returns, definition). + WithOrReplace(sdk.Bool(true)). + WithComment(sdk.String("comment")) + err := client.Functions.CreateForSQL(ctx, request) + require.NoError(t, err) + t.Cleanup(cleanupFunctionHandle(id, nil)) + + function, err := client.Functions.ShowByID(ctx, id) + require.NoError(t, err) + require.Equal(t, id.Name(), function.Name) + require.Equal(t, "SQL", function.Language) + }) } func TestInt_OtherFunctions(t *testing.T) { @@ -224,7 +245,7 @@ func TestInt_OtherFunctions(t *testing.T) { dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeFloat) - request := sdk.NewCreateForSQLFunctionRequest(id, *returns, &definition). + request := sdk.NewCreateForSQLFunctionRequest(id, *returns, definition). WithArguments([]sdk.FunctionArgumentRequest{*argument}). WithOrReplace(sdk.Bool(true)) err := client.Functions.CreateForSQL(ctx, request) From c7462cbb14d983cc2f232c7880f11c6ef657ca1c Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Wed, 20 Dec 2023 22:28:56 -0800 Subject: [PATCH 10/10] add unit tests for desc --- pkg/sdk/functions_def.go | 6 +- pkg/sdk/functions_gen.go | 4 +- pkg/sdk/functions_gen_test.go | 17 +++ pkg/sdk/poc/generator/field_transformers.go | 5 + pkg/sdk/testint/functions_integration_test.go | 100 ++++++++++++------ 5 files changed, 95 insertions(+), 37 deletions(-) diff --git a/pkg/sdk/functions_def.go b/pkg/sdk/functions_def.go index 5dddb5b9b5..81ac15c91d 100644 --- a/pkg/sdk/functions_def.go +++ b/pkg/sdk/functions_def.go @@ -231,7 +231,7 @@ var FunctionsDef = g.NewInterface( SQL("FUNCTION"). IfExists(). Name(). - PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()). + PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().MustParentheses().Required()). Identifier("RenameTo", g.KindOfTPointer[SchemaObjectIdentifier](), g.IdentifierOptions().SQL("RENAME TO")). OptionalTextAssignment("SET COMMENT", g.ParameterOptions().SingleQuotes()). OptionalTextAssignment("SET LOG_LEVEL", g.ParameterOptions().SingleQuotes()). @@ -253,7 +253,7 @@ var FunctionsDef = g.NewInterface( SQL("FUNCTION"). IfExists(). Name(). - PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()). + PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().MustParentheses().Required()). WithValidation(g.ValidIdentifier, "name"), ).ShowOperation( "https://docs.snowflake.com/en/sql-reference/sql/show-user-functions", @@ -311,6 +311,6 @@ var FunctionsDef = g.NewInterface( Describe(). SQL("FUNCTION"). Name(). - PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().Parentheses().Required()). + PredefinedQueryStructField("ArgumentDataTypes", "[]DataType", g.KeywordOptions().MustParentheses().Required()). WithValidation(g.ValidIdentifier, "name"), ) diff --git a/pkg/sdk/functions_gen.go b/pkg/sdk/functions_gen.go index b0becf29f6..8d62e8c4b2 100644 --- a/pkg/sdk/functions_gen.go +++ b/pkg/sdk/functions_gen.go @@ -171,7 +171,7 @@ type AlterFunctionOptions struct { function bool `ddl:"static" sql:"FUNCTION"` IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` name SchemaObjectIdentifier `ddl:"identifier"` - ArgumentDataTypes []DataType `ddl:"keyword,parentheses"` + ArgumentDataTypes []DataType `ddl:"keyword,must_parentheses"` RenameTo *SchemaObjectIdentifier `ddl:"identifier" sql:"RENAME TO"` SetComment *string `ddl:"parameter,single_quotes" sql:"SET COMMENT"` SetLogLevel *string `ddl:"parameter,single_quotes" sql:"SET LOG_LEVEL"` @@ -247,7 +247,7 @@ type DescribeFunctionOptions struct { describe bool `ddl:"static" sql:"DESCRIBE"` function bool `ddl:"static" sql:"FUNCTION"` name SchemaObjectIdentifier `ddl:"identifier"` - ArgumentDataTypes []DataType `ddl:"keyword,parentheses"` + ArgumentDataTypes []DataType `ddl:"keyword,must_parentheses"` } type functionDetailRow struct { diff --git a/pkg/sdk/functions_gen_test.go b/pkg/sdk/functions_gen_test.go index 965fda3df9..19e552b2cd 100644 --- a/pkg/sdk/functions_gen_test.go +++ b/pkg/sdk/functions_gen_test.go @@ -452,6 +452,11 @@ func TestFunctions_Drop(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("no arguments", func(t *testing.T) { + opts := defaultOpts() + assertOptsValidAndSQLEquals(t, opts, `DROP FUNCTION %s ()`, id.FullyQualifiedName()) + }) + t.Run("all options", func(t *testing.T) { opts := &DropFunctionOptions{ name: id, @@ -503,6 +508,13 @@ func TestFunctions_Alter(t *testing.T) { assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s (VARCHAR, NUMBER) RENAME TO %s`, id.FullyQualifiedName(), opts.RenameTo.FullyQualifiedName()) }) + t.Run("alter: set log level with no arguments", func(t *testing.T) { + opts := defaultOpts() + opts.ArgumentDataTypes = nil + opts.SetLogLevel = String("DEBUG") + assertOptsValidAndSQLEquals(t, opts, `ALTER FUNCTION IF EXISTS %s () SET LOG_LEVEL = 'DEBUG'`, id.FullyQualifiedName()) + }) + t.Run("alter: set log level", func(t *testing.T) { opts := defaultOpts() opts.SetLogLevel = String("DEBUG") @@ -624,6 +636,11 @@ func TestFunctions_Describe(t *testing.T) { assertOptsInvalidJoinedErrors(t, opts, ErrInvalidObjectIdentifier) }) + t.Run("no arguments", func(t *testing.T) { + opts := defaultOpts() + assertOptsValidAndSQLEquals(t, opts, `DESCRIBE FUNCTION %s ()`, id.FullyQualifiedName()) + }) + t.Run("all options", func(t *testing.T) { opts := defaultOpts() opts.ArgumentDataTypes = []DataType{DataTypeVARCHAR, DataTypeNumber} diff --git a/pkg/sdk/poc/generator/field_transformers.go b/pkg/sdk/poc/generator/field_transformers.go index 9e4ebd9f14..f0a3be6513 100644 --- a/pkg/sdk/poc/generator/field_transformers.go +++ b/pkg/sdk/poc/generator/field_transformers.go @@ -47,6 +47,11 @@ func (v *KeywordTransformer) Parentheses() *KeywordTransformer { return v } +func (v *KeywordTransformer) MustParentheses() *KeywordTransformer { + v.parentheses = "must_parentheses" + return v +} + func (v *KeywordTransformer) Transform(f *Field) *Field { addTagIfMissing(f.Tags, "ddl", "keyword") if v.required { diff --git a/pkg/sdk/testint/functions_integration_test.go b/pkg/sdk/testint/functions_integration_test.go index 7b16cac7dd..e4645b6661 100644 --- a/pkg/sdk/testint/functions_integration_test.go +++ b/pkg/sdk/testint/functions_integration_test.go @@ -202,7 +202,7 @@ func TestInt_OtherFunctions(t *testing.T) { tagTest, tagCleanup := createTag(t, client, databaseTest, schemaTest) t.Cleanup(tagCleanup) - assertFunction := func(t *testing.T, id sdk.SchemaObjectIdentifier, secure bool) { + assertFunction := func(t *testing.T, id sdk.SchemaObjectIdentifier, secure bool, withArguments bool) { t.Helper() function, err := client.Functions.ShowByID(ctx, id) @@ -213,8 +213,13 @@ func TestInt_OtherFunctions(t *testing.T) { assert.Equal(t, false, function.IsBuiltin) assert.Equal(t, false, function.IsAggregate) assert.Equal(t, false, function.IsAnsi) - assert.Equal(t, 1, function.MinNumArguments) - assert.Equal(t, 1, function.MaxNumArguments) + if withArguments { + assert.Equal(t, 1, function.MinNumArguments) + assert.Equal(t, 1, function.MaxNumArguments) + } else { + assert.Equal(t, 0, function.MinNumArguments) + assert.Equal(t, 0, function.MaxNumArguments) + } assert.NotEmpty(t, function.Arguments) assert.NotEmpty(t, function.Description) assert.NotEmpty(t, function.CatalogName) @@ -236,7 +241,7 @@ func TestInt_OtherFunctions(t *testing.T) { } } - createFunctionForSQLHandle := func(t *testing.T, cleanup bool) *sdk.Function { + createFunctionForSQLHandle := func(t *testing.T, cleanup bool, withArguments bool) *sdk.Function { t.Helper() id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(4)) @@ -244,14 +249,20 @@ func TestInt_OtherFunctions(t *testing.T) { dt := sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeFloat) returns := sdk.NewFunctionReturnsRequest().WithResultDataType(dt) - argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeFloat) request := sdk.NewCreateForSQLFunctionRequest(id, *returns, definition). - WithArguments([]sdk.FunctionArgumentRequest{*argument}). WithOrReplace(sdk.Bool(true)) + if withArguments { + argument := sdk.NewFunctionArgumentRequest("x", sdk.DataTypeFloat) + request = request.WithArguments([]sdk.FunctionArgumentRequest{*argument}) + } err := client.Functions.CreateForSQL(ctx, request) require.NoError(t, err) if cleanup { - t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) + if withArguments { + t.Cleanup(cleanupFunctionHandle(id, []sdk.DataType{sdk.DataTypeFloat})) + } else { + t.Cleanup(cleanupFunctionHandle(id, nil)) + } } function, err := client.Functions.ShowByID(ctx, id) require.NoError(t, err) @@ -263,7 +274,7 @@ func TestInt_OtherFunctions(t *testing.T) { } t.Run("alter function: rename", func(t *testing.T) { - f := createFunctionForSQLHandle(t, false) + f := createFunctionForSQLHandle(t, false, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) nid := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, random.StringN(3)) @@ -284,79 +295,87 @@ func TestInt_OtherFunctions(t *testing.T) { }) t.Run("alter function: set log level", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetLogLevel(sdk.String("DEBUG"))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: unset log level", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetLogLevel(sdk.Bool(true))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: set trace level", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetTraceLevel(sdk.String("ALWAYS"))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: unset trace level", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetTraceLevel(sdk.Bool(true))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: set comment", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetComment(sdk.String("test comment"))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: unset comment", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetComment(sdk.Bool(true))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: set secure", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetSecure(sdk.Bool(true))) require.NoError(t, err) - assertFunction(t, id, true) + assertFunction(t, id, true, true) + }) + + t.Run("alter function: set secure with no arguments", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true, false) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + err := client.Functions.Alter(ctx, sdk.NewAlterFunctionRequest(id, nil).WithSetSecure(sdk.Bool(true))) + require.NoError(t, err) + assertFunction(t, id, true, false) }) t.Run("alter function: unset secure", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetSecure(sdk.Bool(true))) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("alter function: set and unset tags", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) setTags := []sdk.TagAssociation{ @@ -367,19 +386,19 @@ func TestInt_OtherFunctions(t *testing.T) { } err := client.Functions.Alter(ctx, defaultAlterRequest(id).WithSetTags(setTags)) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) unsetTags := []sdk.ObjectIdentifier{ tagTest.ID(), } err = client.Functions.Alter(ctx, defaultAlterRequest(id).WithUnsetTags(unsetTags)) require.NoError(t, err) - assertFunction(t, id, false) + assertFunction(t, id, false, true) }) t.Run("show function for SQL: without like", func(t *testing.T) { - f1 := createFunctionForSQLHandle(t, true) - f2 := createFunctionForSQLHandle(t, true) + f1 := createFunctionForSQLHandle(t, true, true) + f2 := createFunctionForSQLHandle(t, true, true) functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest()) require.NoError(t, err) @@ -390,8 +409,8 @@ func TestInt_OtherFunctions(t *testing.T) { }) t.Run("show function for SQL: with like", func(t *testing.T) { - f1 := createFunctionForSQLHandle(t, true) - f2 := createFunctionForSQLHandle(t, true) + f1 := createFunctionForSQLHandle(t, true, true) + f2 := createFunctionForSQLHandle(t, true, true) functions, err := client.Functions.Show(ctx, sdk.NewShowFunctionRequest().WithLike(&sdk.Like{Pattern: &f1.Name})) require.NoError(t, err) @@ -408,7 +427,7 @@ func TestInt_OtherFunctions(t *testing.T) { }) t.Run("describe function for SQL", func(t *testing.T) { - f := createFunctionForSQLHandle(t, true) + f := createFunctionForSQLHandle(t, true, true) id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) request := sdk.NewDescribeFunctionRequest(id, []sdk.DataType{sdk.DataTypeFloat}) @@ -423,4 +442,21 @@ func TestInt_OtherFunctions(t *testing.T) { require.Equal(t, "3.141592654::FLOAT", pairs["body"]) require.Equal(t, "(X FLOAT)", pairs["signature"]) }) + + t.Run("describe function for SQL: no arguments", func(t *testing.T) { + f := createFunctionForSQLHandle(t, true, false) + id := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, f.Name) + + request := sdk.NewDescribeFunctionRequest(id, nil) + details, err := client.Functions.Describe(ctx, request) + require.NoError(t, err) + pairs := make(map[string]string) + for _, detail := range details { + pairs[detail.Property] = detail.Value + } + require.Equal(t, "SQL", pairs["language"]) + require.Equal(t, "FLOAT", pairs["returns"]) + require.Equal(t, "3.141592654::FLOAT", pairs["body"]) + require.Equal(t, "()", pairs["signature"]) + }) }