zeroshade commented on code in PR #2717:
URL: https://github.com/apache/iceberg-go/pull/2717#discussion_r4722104676


##########
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:
   Defaults that aren't `long` decode through `json.Unmarshal` into `float64`, 
which can't hold nanosecond timestamps exactly. 
`"initial-default":1510871468123456789` on a `timestamp_ns` field is rewritten 
as `"2017-11-16T22:31:08.123456768"`. Before this change Java refused to load 
that metadata; now it loads with a value that is 21ns off. Could 
`decodeNestedFieldDefault` decode time and timestamp defaults into `int64`, the 
way it already does for `Int64Type`? This case could then take an `int64`, and 
the scan path would read the exact value too.



##########
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:
   nit: Go formats years after 9999 without a sign (`Date(3000000)` → 
`"10183-09-21"`), and `StringLiteral.To(DateType)` can't parse that back, so a 
scan of such a default panics in `defaultToScalar`. Java writes `+10183-09-21`. 
Rejecting out-of-range values here, or a test that pins the behavior, would be 
enough.



-- 
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]

Reply via email to