ShivamSharma43 commented on code in PR #2717:
URL: https://github.com/apache/iceberg-go/pull/2717#discussion_r4727451226
##########
types.go:
##########
@@ -378,10 +378,61 @@ func defaultValueToJSON(typ Type, value any) any {
if d, ok := value.(Decimal); ok {
return d.String()
}
+ if s, ok := temporalDefaultString(typ, value); ok {
+ return s
+ }
return value
}
+// temporalDefaultString returns the spec's JSON single-value form of a date,
+// time or timestamp default: an ISO-8601 string such as "2017-11-16" or
+// "2017-11-16T22:31:08.123456+00:00", which Java's SingleValueParser requires.
+// It accepts the typed value that AddColumn stores, and the float64 that a
+// numeric default written by older iceberg-go versions decodes to, so those
+// are written as strings from now on. It reports false for any other value.
+func temporalDefaultString(typ Type, value any) (string, bool) {
+ switch typ.(type) {
+ case DateType:
+ if v, ok := temporalDefaultValue[Date](value); ok {
+ return v.ToTime().Format("2006-01-02"), true
+ }
+ case TimeType:
+ if v, ok := temporalDefaultValue[Time](value); ok {
+ return v.ToTime().Format("15:04:05.000000"), true
+ }
+ case TimestampType:
+ if v, ok := temporalDefaultValue[Timestamp](value); ok {
+ return v.ToTime().Format("2006-01-02T15:04:05.000000"),
true
+ }
+ case TimestampTzType:
+ if v, ok := temporalDefaultValue[Timestamp](value); ok {
+ return
v.ToTime().Format("2006-01-02T15:04:05.000000+00:00"), true
+ }
+ case TimestampNsType:
+ if v, ok := temporalDefaultValue[TimestampNano](value); ok {
+ return
v.ToTime().Format("2006-01-02T15:04:05.000000000"), true
+ }
+ case TimestampTzNsType:
+ if v, ok := temporalDefaultValue[TimestampNano](value); ok {
+ return
v.ToTime().Format("2006-01-02T15:04:05.000000000+00:00"), true
+ }
+ }
+
+ return "", false
+}
+
+func temporalDefaultValue[T Date | Time | Timestamp | TimestampNano](value
any) (T, bool) {
+ switch v := value.(type) {
+ case T:
+ return v, true
+ case float64:
Review Comment:
Fixed in 80050d9: `decodeNestedFieldDefault` now decodes a numeric date,
time or timestamp default as an `int64`, then keeps it as the spec string, so
`1510871468123456789` on `timestamp_ns` becomes
`"2017-11-16T22:31:08.123456789"` exactly and the scan reads that string.
`TestNestedFieldRewritesLegacyNumericTemporalDefault` now covers all six types
including that value, and `TestToRequestedSchemaInitialDefaultJSONRoundTrip`
has a legacy `timestamp_ns` case that checks the scanned value. A non-integer
number such as `1.5` is now rejected with `ErrInvalidSchema`.
##########
types.go:
##########
@@ -378,10 +378,61 @@ func defaultValueToJSON(typ Type, value any) any {
if d, ok := value.(Decimal); ok {
return d.String()
}
+ if s, ok := temporalDefaultString(typ, value); ok {
+ return s
+ }
return value
}
+// temporalDefaultString returns the spec's JSON single-value form of a date,
+// time or timestamp default: an ISO-8601 string such as "2017-11-16" or
+// "2017-11-16T22:31:08.123456+00:00", which Java's SingleValueParser requires.
+// It accepts the typed value that AddColumn stores, and the float64 that a
+// numeric default written by older iceberg-go versions decodes to, so those
+// are written as strings from now on. It reports false for any other value.
+func temporalDefaultString(typ Type, value any) (string, bool) {
+ switch typ.(type) {
+ case DateType:
+ if v, ok := temporalDefaultValue[Date](value); ok {
+ return v.ToTime().Format("2006-01-02"), true
Review Comment:
Went with rejecting in 80050d9, since `StringLiteral.To` can't parse a
signed or 5-digit year. Writing a date or timestamp default outside the years
0000 to 9999 now fails with `ErrInvalidArgument` instead of writing a string
that can't be read back, and the same for a time outside a single day (Go would
wrap `Time(-1)` to `23:59:59.999999`). A legacy numeric default like that still
loads, as an `int64`, and fails only when written. Tests:
`TestNestedFieldRejectsUnrepresentableTemporalDefault` (both limits, and that
`0000-01-01` and `9999-12-31` still round-trip),
`TestNestedFieldKeepsUnrepresentableLegacyTemporalDefault`, and
`TestNewSchemaKeepsUnrepresentableTemporalDefault`.
--
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]