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] Handle edge case for effective_properties in databricks_sql_table #4153

Merged
merged 2 commits into from
Oct 29, 2024
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
29 changes: 21 additions & 8 deletions catalog/resource_sql_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

var MaxSqlExecWaitTimeout = 50
var optionPrefixes = []string{"option.", "spark.sql.dataSourceOptions."}

type SqlColumnInfo struct {
Name string `json:"name"`
Expand Down Expand Up @@ -67,7 +68,6 @@ type SqlTableInfo struct {
}

func (ti SqlTableInfo) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema {

caseInsensitiveFields := []string{"name", "catalog_name", "schema_name"}
for _, field := range caseInsensitiveFields {
s.SchemaPath(field).SetCustomSuppressDiff(common.EqualFoldDiffSuppress)
Expand Down Expand Up @@ -598,18 +598,31 @@ func ResourceSqlTable() common.Resource {
// If the user specified a property but the value of that property has changed, that will appear
// as a change in the effective property/option. To cause a diff to be detected, we need to
// reset the effective property/option to the requested value.
userSpecifiedProperties := d.Get("properties").(map[string]interface{})
userSpecifiedOptions := d.Get("options").(map[string]interface{})
effectiveProperties := d.Get("effective_properties").(map[string]interface{})
diff := make(map[string]interface{})
userSpecifiedProperties := d.Get("properties").(map[string]any)
userSpecifiedOptions := d.Get("options").(map[string]any)
effectiveProperties := d.Get("effective_properties").(map[string]any)
diff := make(map[string]any)
for k, userSpecifiedValue := range userSpecifiedProperties {
if effectiveValue, ok := effectiveProperties[k]; !ok || effectiveValue != userSpecifiedValue {
diff[k] = userSpecifiedValue
}
}
for k, userSpecifiedValue := range userSpecifiedOptions {
if effectiveValue, ok := effectiveProperties["option."+k]; !ok || effectiveValue != userSpecifiedValue {
diff["option."+k] = userSpecifiedValue
for userOptName, userSpecifiedValue := range userSpecifiedOptions {
var found bool
var effectiveValue any
var effectOptName string
// If the option is not found, check if the user specified the option without the prefix
// i.e. if user specified `multiLine` for JSON, then backend returns `spark.sql.dataSourceOptions.multiLine`
for _, prefix := range optionPrefixes {
effectOptName = prefix + userOptName
if v, ok := effectiveProperties[effectOptName]; ok {
found = true
effectiveValue = v
break
}
}
if !found || effectiveValue != userSpecifiedValue {
diff[effectOptName] = userSpecifiedValue
}
}
if len(diff) > 0 {
Expand Down
7 changes: 5 additions & 2 deletions catalog/resource_sql_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,15 +1625,18 @@ func TestResourceSqlTable_Diff_ExistingResource(t *testing.T) {
}
options = {
"myopt" = "myval"
"multiLine" = "true"
}`,
map[string]string{
"properties.%": "1",
"properties.myprop": "myval",
"options.%": "1",
"options.%": "2",
"options.myopt": "myval",
"effective_properties.%": "2",
"options.multiLine": "true",
"effective_properties.%": "3",
"effective_properties.myprop": "myval",
"effective_properties.option.myopt": "myval",
"effective_properties.spark.sql.dataSourceOptions.multiLine": "true",
},
nil,
},
Expand Down
Loading