Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow Read external_function where return_type is VARIANT #720

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/resources/external_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,25 @@ func ReadExternalFunction(d *schema.ResourceData, meta interface{}) error {
}
}
case "returns":
// Format in Snowflake DB is returnType(<some number>)
re := regexp.MustCompile(`^(.*)\([0-9]*\)$`)
returnType := desc.Value.String
// We first check for VARIANT
if returnType == "VARIANT" {
if err = d.Set("return_type", returnType); err != nil {
return err
}
break
}

// otherwise, format in Snowflake DB is returnType(<some number>)
re := regexp.MustCompile(`^(\w+)\([0-9]*\)$`)
match := re.FindStringSubmatch(desc.Value.String)
if len(match) < 2 {
return errors.Errorf("return_type %s not recognized", returnType)
}
if err = d.Set("return_type", match[1]); err != nil {
return err
}

case "null handling":
if err = d.Set("null_input_behavior", desc.Value.String); err != nil {
return err
Expand Down
52 changes: 52 additions & 0 deletions pkg/resources/external_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ func expectExternalFunctionRead(mock sqlmock.Sqlmock) {
mock.ExpectQuery(`DESCRIBE FUNCTION "database_name"."schema_name"."my_test_function" \(varchar\)`).WillReturnRows(describeRows)
}

func expectExternalFunctionReadVariant(mock sqlmock.Sqlmock) {
rows := sqlmock.NewRows([]string{"created_on", "name", "schema_name", "is_builtin", "is_aggregate", "is_ansi", "min_num_arguments", "max_num_arguments", "arguments", "description", "catalog_name", "is_table_function", "valid_for_clustering", "is_secure", "is_external_function", "language"}).AddRow("now", "my_test_function", "schema_name", "N", "N", "N", "1", "1", "MY_TEST_FUNCTION(VARCHAR) RETURN VARCHAR", "mock comment", "database_name", "N", "N", "N", "Y", "EXTERNAL")
mock.ExpectQuery(`SHOW EXTERNAL FUNCTIONS LIKE 'my_test_function' IN SCHEMA "database_name"."schema_name"`).WillReturnRows(rows)

describeRows := sqlmock.NewRows([]string{"property", "value"}).
AddRow("returns", "VARIANT"). // VARIANTs are different format
AddRow("null handling", "CALLED ON NULL INPUT").
AddRow("volatility", "IMMUTABLE").
AddRow("body", "https://123456.execute-api.us-west-2.amazonaws.com/prod/my_test_function").
AddRow("headers", "{\"x-custom-header\":\"snowflake\"").
AddRow("context_headers", "[\"CURRENT_TIMESTAMP\"]").
AddRow("max_batch_rows", "not set").
AddRow("compression", "AUTO")

mock.ExpectQuery(`DESCRIBE FUNCTION "database_name"."schema_name"."my_test_function" \(varchar\)`).WillReturnRows(describeRows)
}

func TestExternalFunctionRead(t *testing.T) {
r := require.New(t)

Expand Down Expand Up @@ -96,6 +113,41 @@ func TestExternalFunctionRead(t *testing.T) {
r.Equal("CURRENT_TIMESTAMP", test_func_context_headers[0])
})
}
func TestExternalFunctionReadReturnTypeVariant(t *testing.T) {
r := require.New(t)

d := externalFunction(t, "database_name|schema_name|my_test_function|varchar", map[string]interface{}{"name": "my_test_function", "arg": []interface{}{map[string]interface{}{"name": "data", "type": "varchar"}}, "return_type": "variant", "comment": "mock comment"})

WithMockDb(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
expectExternalFunctionReadVariant(mock)

err := resources.ReadExternalFunction(d, db)
r.NoError(err)
r.Equal("my_test_function", d.Get("name").(string))
r.Equal("mock comment", d.Get("comment").(string))
r.Equal("VARIANT", d.Get("return_type").(string))

args := d.Get("arg").([]interface{})
r.Len(args, 1)
test_func_args := args[0].(map[string]interface{})
r.Len(test_func_args, 2)
r.Equal("data", test_func_args["name"].(string))
r.Equal("varchar", test_func_args["type"].(string))

headers := d.Get("header").([]interface{})
r.Len(headers, 1)
test_func_headers := headers[0].(map[string]interface{})
r.Len(test_func_headers, 2)
r.Equal("x-custom-header", test_func_headers["name"].(string))
r.Equal("snowflake", test_func_headers["value"].(string))

context_headers := d.Get("context_headers").([]interface{})
r.Len(context_headers, 1)
test_func_context_headers := expandStringList(context_headers)
r.Len(test_func_context_headers, 1)
r.Equal("CURRENT_TIMESTAMP", test_func_context_headers[0])
})
}

func TestExternalFunctionDelete(t *testing.T) {
r := require.New(t)
Expand Down