ShivamSharma43 opened a new pull request, #2696:
URL: https://github.com/apache/iceberg-go/pull/2696
A decimal column default was lost when table metadata was written as JSON.
`AddColumn` stores the default literal's `Any()` value, which for a decimal is
an `iceberg.Decimal`, and `NestedField.MarshalJSON` wrote it as
`{"Val":{},"Scale":2}`. After a catalog reloads the metadata, the default is
gone, and scanning a data file written before the column existed fails instead
of returning the default (see the issue for the reproduction).
### Cause
`defaultValueToJSON` passes every value that isn't hex-encoded through
unchanged. `iceberg.Decimal` has no JSON encoding of its own, and its
`decimal128.Num` has no exported fields, so `encoding/json` writes the empty
struct and the scale.
### Fix
`defaultValueToJSON` now writes an `iceberg.Decimal` as `Decimal.String()`,
for example `"14.20"` or `"-0.05"`. That is the spec's [JSON single-value
serialization](https://iceberg.apache.org/spec/#json-single-value-serialization)
for `decimal(P, S)`, and what Java's
[`SingleValueParser`](https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/SingleValueParser.java)
writes. Reading is unchanged: a string default was already parsed for decimal
fields.
### Testing
- `TestNestedFieldMarshalDecimalDefaultsAsString` in `types_test.go`
marshals a field with decimal initial and write defaults, checks the JSON, and
decodes it back.
- `TestAddColumnDecimalDefaultSurvivesMetadataRoundTrip` in
`table/update_schema_test.go` adds a file to a v3 table, adds a `decimal(9, 2)`
column with default `14.20`, reloads the table from its metadata JSON, and
scans: the old file reads `14.20` for the new column.
Both fail on main (the JSON holds `{"Val":{},"Scale":2}`, and the scan
returns an error) and pass with the fix.
- `go vet` is clean for the root and `table` packages. `go test .` passes.
`go test ./table/` passes except for tests that fail the same way on unmodified
main on Windows (the drive letter is read as a URL scheme):
`TestEmptyStringIdentityPartitionScans`, `TestScanImportedSignedZeroBounds`,
`TestDeleteImportedSignedZerosWithIn`,
`TestDeleteNormallyWrittenSignedZeroWithIn`,
`TestScanLegacyEmptyStringPartitionDeleteBounds`,
`TestScanWithIDLessNameMappingFields`, two
`TestRewriteDataFiles_PartialProgress*` tests and `TestWriteMapType`.
Closes #2695
This PR was prepared with AI assistance (Claude Code).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]