diff --git a/.nextchanges/bundles/7842.md b/.nextchanges/bundles/7842.md new file mode 100644 index 00000000000..7b56c0edd6a --- /dev/null +++ b/.nextchanges/bundles/7842.md @@ -0,0 +1 @@ +* Fix `bundle` commands failing with "unexpected change at variables.X.default" when a bundle with Python code has a variable default such as `1.0`, `1e3` or `0012345678901234`. ([#7842](https://github.com/databricks/cli/pull/7842)) diff --git a/acceptance/bundle/python/numeric-variable-default/output.txt b/acceptance/bundle/python/numeric-variable-default/output.txt index e7857995953..79a3985141c 100644 --- a/acceptance/bundle/python/numeric-variable-default/output.txt +++ b/acceptance/bundle/python/numeric-variable-default/output.txt @@ -7,28 +7,16 @@ === leading_zero >>> uv run [UV_ARGS] -q [CLI] bundle validate -t leading_zero --output json -Error: internal error when merging output of Python mutator: unexpected change at "variables.id.default" (update) - - -Exit code: 1 {"default":12345678901234,"value":12345678901234} === float_whole >>> uv run [UV_ARGS] -q [CLI] bundle validate -t float_whole --output json -Error: internal error when merging output of Python mutator: unexpected change at "variables.id.default" (update) - - -Exit code: 1 {"default":1,"value":1} === exponent >>> uv run [UV_ARGS] -q [CLI] bundle validate -t exponent --output json -Error: internal error when merging output of Python mutator: unexpected change at "variables.id.default" (update) - - -Exit code: 1 {"default":1000,"value":1000} === float_fraction @@ -39,19 +27,11 @@ Exit code: 1 === above_2_53 >>> uv run [UV_ARGS] -q [CLI] bundle validate -t above_2_53 --output json -Error: internal error when merging output of Python mutator: unexpected change at "variables.id.default" (update) - - -Exit code: 1 {"default":1234567890123456800,"value":1234567890123456800} === complex >>> uv run [UV_ARGS] -q [CLI] bundle validate -t complex --output json -Error: internal error when merging output of Python mutator: unexpected change at "variables.id.default.nested" (update) - - -Exit code: 1 {"default":{"nested":1},"value":{"nested":1}} === huge diff --git a/acceptance/bundle/python/numeric-variable-default/test.toml b/acceptance/bundle/python/numeric-variable-default/test.toml index e5aab2bcbbb..910f5d2add2 100644 --- a/acceptance/bundle/python/numeric-variable-default/test.toml +++ b/acceptance/bundle/python/numeric-variable-default/test.toml @@ -1,4 +1,4 @@ -Badness = 'Whole-number float variable defaults fail with an internal error when the bundle has Python code. A whole float beyond int64 fails differently: the Python output cannot be parsed.' +Badness = 'A whole float beyond int64 fails: the Python output cannot be parsed.' Ignore = [".databricks", "__pycache__", "tmp.json"] # The position depends on the size of the Python output, which includes paths. diff --git a/bundle/config/mutator/python/apply_python_output.go b/bundle/config/mutator/python/apply_python_output.go index fb5452d3b4f..8a4a12be126 100644 --- a/bundle/config/mutator/python/apply_python_output.go +++ b/bundle/config/mutator/python/apply_python_output.go @@ -2,6 +2,7 @@ package python import ( "fmt" + "strconv" "github.com/databricks/cli/bundle/config/mutator/resourcemutator" @@ -24,6 +25,7 @@ type applyPythonOutputResult struct { // Return value is equivalent to output except for: // - if property is unchanged in output, it's original location will be preserved // - if empty sequence/mapping is deleted in output, it's original value will be preserved +// - if a whole float comes back from Python as an int, the original float will be preserved func applyPythonOutput(root, output dyn.Value) (dyn.Value, applyPythonOutputResult, error) { result, visitor := createOverrideVisitor(root, output) merged, err := merge.Override(root, output, visitor) @@ -135,7 +137,11 @@ func createOverrideVisitor(leftRoot, rightRoot dyn.Value) (applyPythonOutputResu return right, nil } }, - VisitUpdate: func(valuePath dyn.Path, _, right dyn.Value) (dyn.Value, error) { + VisitUpdate: func(valuePath dyn.Path, left, right dyn.Value) (dyn.Value, error) { + if isWholeFloatAsInt(left, right) { + return left, nil + } + if !valuePath.HasPrefix(resourcesPath) { return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (update)", valuePath.String()) } @@ -211,3 +217,15 @@ func isOmitemptyDelete(left dyn.Value) bool { return false } } + +// isWholeFloatAsInt reports whether right is the int Python read back for the float left. +// writeInputFile uses json.Marshal, which writes a whole float such as 1.0 as 1, and Python +// parses that as an int. Beyond 2^53 json.Marshal writes the float's shortest decimal +// (1234567890123456789.0 as 1234567890123456800), which differs from the float's exact value. +func isWholeFloatAsInt(left, right dyn.Value) bool { + if left.Kind() != dyn.KindFloat || right.Kind() != dyn.KindInt { + return false + } + f, i := left.MustFloat(), right.MustInt() + return (float64(i) == f && int64(f) == i) || strconv.FormatFloat(f, 'f', -1, 64) == strconv.FormatInt(i, 10) +} diff --git a/bundle/config/mutator/python/apply_python_output_test.go b/bundle/config/mutator/python/apply_python_output_test.go index 94d7e7c39cc..224c49302d5 100644 --- a/bundle/config/mutator/python/apply_python_output_test.go +++ b/bundle/config/mutator/python/apply_python_output_test.go @@ -1,6 +1,7 @@ package python import ( + "math" "testing" "github.com/databricks/cli/bundle/config/mutator/resourcemutator" @@ -10,6 +11,7 @@ import ( "github.com/databricks/cli/libs/dyn" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type applyPythonOutputTestCase struct { @@ -185,6 +187,55 @@ func TestMergeOutput_disallowUpdate(t *testing.T) { assert.EqualError(t, err, `unexpected change at "not_resource" (update)`) } +func TestMergeOutput_wholeFloatAsInt(t *testing.T) { + input := mapOf("not_resource", dyn.V(1.0)) + output := mapOf("not_resource", dyn.V(1)) + + merged, _, err := applyPythonOutput(input, output) + + require.NoError(t, err) + assert.Equal(t, input.Get("not_resource"), merged.Get("not_resource")) +} + +func TestMergeOutput_wholeFloatAsInt_negativeZero(t *testing.T) { + input := mapOf("not_resource", dyn.V(math.Copysign(0, -1))) + output := mapOf("not_resource", dyn.V(0)) + + _, _, err := applyPythonOutput(input, output) + + require.NoError(t, err) +} + +func TestMergeOutput_wholeFloatAsInt_shortestDecimalAbove2p53(t *testing.T) { + // json.Marshal writes 1234567890123456789.0 (stored as 1234567890123456768) as 1234567890123456800. + input := mapOf("not_resource", dyn.V(1234567890123456789.0)) + output := mapOf("not_resource", dyn.V(int64(1234567890123456800))) + + merged, _, err := applyPythonOutput(input, output) + + require.NoError(t, err) + assert.Equal(t, input.Get("not_resource"), merged.Get("not_resource")) +} + +func TestMergeOutput_disallowNearbyIntAbove2p53(t *testing.T) { + // float64(9007199254740993) rounds to 9007199254740992, but Python did change the value. + input := mapOf("not_resource", dyn.V(9007199254740992.0)) + output := mapOf("not_resource", dyn.V(int64(9007199254740993))) + + _, _, err := applyPythonOutput(input, output) + + assert.EqualError(t, err, `unexpected change at "not_resource" (update)`) +} + +func TestMergeOutput_disallowFloatToDifferentInt(t *testing.T) { + input := mapOf("not_resource", dyn.V(1.5)) + output := mapOf("not_resource", dyn.V(1)) + + _, _, err := applyPythonOutput(input, output) + + assert.EqualError(t, err, `unexpected change at "not_resource" (update)`) +} + type overrideVisitorOmitemptyTestCase struct { name string path dyn.Path