Skip to content

fix: write decimal column defaults as strings in metadata JSON - #2696

Open
ShivamSharma43 wants to merge 1 commit into
apache:mainfrom
ShivamSharma43:fix-decimal-default-json
Open

ShivamSharma43 wants to merge 1 commit into
apache:mainfrom
ShivamSharma43:fix-decimal-default-json

Conversation

@ShivamSharma43

Copy link
Copy Markdown
Contributor

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 for decimal(P, S), and what Java's SingleValueParser 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

iceberg.Decimal has no JSON encoding and its decimal128 value has no
exported fields, so a decimal initial-default or write-default was
written as {"Val":{},"Scale":2} and the value was lost. Write the
spec's single-value form, a string such as "14.20", as Java's
SingleValueParser does.

Closes apache#2695

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 10:00

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decimal column defaults are written to metadata JSON as {"Val":{},"Scale":2}

2 participants