Skip to content
Open
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
1 change: 1 addition & 0 deletions .nextchanges/bundles/7842.md
Original file line number Diff line number Diff line change
@@ -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))
20 changes: 0 additions & 20 deletions acceptance/bundle/python/numeric-variable-default/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
20 changes: 19 additions & 1 deletion bundle/config/mutator/python/apply_python_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package python

import (
"fmt"
"strconv"

"github.com/databricks/cli/bundle/config/mutator/resourcemutator"

Expand All @@ -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)
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Generated] nit: applyPythonOutput's doc comment lists where the result differs from Python's output (kept locations, undone empty deletes). Keeping the original float when Python returns a whole int is a third case; add a bullet there?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 4f62523.

}

if !valuePath.HasPrefix(resourcesPath) {
return dyn.InvalidValue, fmt.Errorf("unexpected change at %q (update)", valuePath.String())
}
Expand Down Expand Up @@ -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)
}
51 changes: 51 additions & 0 deletions bundle/config/mutator/python/apply_python_output_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package python

import (
"math"
"testing"

"github.com/databricks/cli/bundle/config/mutator/resourcemutator"
Expand All @@ -10,6 +11,7 @@ import (

"github.com/databricks/cli/libs/dyn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type applyPythonOutputTestCase struct {
Expand Down Expand Up @@ -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
Expand Down
Loading