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


##########
manifest.go:
##########
@@ -1817,62 +1785,57 @@ func (d *dataFile) initializeMapData() {
 func (d *dataFile) convertAvroValueToIcebergType(v any, fieldID int) any {
        if logicalType, ok := d.fieldIDToLogicalType[fieldID]; ok {
                switch logicalType {
-               case avro.Date:
+               case atype.Date:
+                       // twmb/avro returns time.Time when the file schema has 
logicalType,
+                       // but raw int32 when the file schema omits it.
                        if val, ok := v.(time.Time); ok {
                                return Date(val.Truncate(24*time.Hour).Unix() / 
int64((time.Hour * 24).Seconds()))
                        }
 
                        return Date(v.(int32))
-               case avro.TimeMillis:
+               case atype.TimeMillis:
                        if val, ok := v.(time.Duration); ok {
                                return Time(val.Milliseconds())
                        }
 
                        return Time(v.(int64))
-               case avro.TimeMicros:
+               case atype.TimeMicros:
                        if val, ok := v.(time.Duration); ok {
                                return Time(val.Microseconds())
                        }
 
                        return Time(v.(int64))
-               case avro.TimestampMillis:
+               case atype.TimestampMillis:
                        if val, ok := v.(time.Time); ok {
                                return Timestamp(val.UTC().UnixMilli())
                        }
 
                        return Timestamp(v.(int64))
-               case avro.TimestampMicros:
+               case atype.TimestampMicros:
                        if val, ok := v.(time.Time); ok {
                                return Timestamp(val.UTC().UnixMicro())
                        }
 
                        return Timestamp(v.(int64))
-               case avro.Decimal:
-                       if unionMap, ok := v.(map[string]any); ok {
-                               if val, ok := unionMap["fixed"]; ok {
-                                       if bigRatValue, ok := val.(*big.Rat); 
ok {
-                                               scale := 
d.fieldIDToFixedSize[fieldID]
-                                               scaleFactor := 
new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil)
-                                               unscaled := 
new(big.Int).Mul(bigRatValue.Num(), scaleFactor)
-                                               unscaled = 
unscaled.Div(unscaled, bigRatValue.Denom())
-                                               decimal128Val := 
decimal128.FromBigInt(unscaled)
-
-                                               return DecimalLiteral{
-                                                       Scale: scale,
-                                                       Val:   decimal128Val,
-                                               }
-                                       }
+               case atype.Decimal:
+                       if numStr, ok := v.(json.Number); ok {
+                               scale := d.fieldIDToFixedSize[fieldID]
+                               r := new(big.Rat)
+                               r.SetString(string(numStr))

Review Comment:
   `json.Number` has a `.String()` method, use that instead



##########
manifest.go:
##########
@@ -1817,62 +1785,57 @@ func (d *dataFile) initializeMapData() {
 func (d *dataFile) convertAvroValueToIcebergType(v any, fieldID int) any {
        if logicalType, ok := d.fieldIDToLogicalType[fieldID]; ok {
                switch logicalType {
-               case avro.Date:
+               case atype.Date:
+                       // twmb/avro returns time.Time when the file schema has 
logicalType,
+                       // but raw int32 when the file schema omits it.
                        if val, ok := v.(time.Time); ok {
                                return Date(val.Truncate(24*time.Hour).Unix() / 
int64((time.Hour * 24).Seconds()))
                        }
 
                        return Date(v.(int32))
-               case avro.TimeMillis:
+               case atype.TimeMillis:
                        if val, ok := v.(time.Duration); ok {
                                return Time(val.Milliseconds())
                        }
 
                        return Time(v.(int64))
-               case avro.TimeMicros:
+               case atype.TimeMicros:
                        if val, ok := v.(time.Duration); ok {
                                return Time(val.Microseconds())
                        }
 
                        return Time(v.(int64))
-               case avro.TimestampMillis:
+               case atype.TimestampMillis:
                        if val, ok := v.(time.Time); ok {
                                return Timestamp(val.UTC().UnixMilli())
                        }
 
                        return Timestamp(v.(int64))
-               case avro.TimestampMicros:
+               case atype.TimestampMicros:
                        if val, ok := v.(time.Time); ok {
                                return Timestamp(val.UTC().UnixMicro())
                        }
 
                        return Timestamp(v.(int64))
-               case avro.Decimal:
-                       if unionMap, ok := v.(map[string]any); ok {
-                               if val, ok := unionMap["fixed"]; ok {
-                                       if bigRatValue, ok := val.(*big.Rat); 
ok {
-                                               scale := 
d.fieldIDToFixedSize[fieldID]
-                                               scaleFactor := 
new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil)
-                                               unscaled := 
new(big.Int).Mul(bigRatValue.Num(), scaleFactor)
-                                               unscaled = 
unscaled.Div(unscaled, bigRatValue.Denom())
-                                               decimal128Val := 
decimal128.FromBigInt(unscaled)
-
-                                               return DecimalLiteral{
-                                                       Scale: scale,
-                                                       Val:   decimal128Val,
-                                               }
-                                       }
+               case atype.Decimal:
+                       if numStr, ok := v.(json.Number); ok {
+                               scale := d.fieldIDToFixedSize[fieldID]

Review Comment:
   Why does this come back as a `json.Number` instead of the fixed byte slice 
or `*big.Rat`? Can't we avoid having to parse the string?



##########
manifest.go:
##########
@@ -1817,62 +1785,57 @@ func (d *dataFile) initializeMapData() {
 func (d *dataFile) convertAvroValueToIcebergType(v any, fieldID int) any {
        if logicalType, ok := d.fieldIDToLogicalType[fieldID]; ok {
                switch logicalType {
-               case avro.Date:
+               case atype.Date:
+                       // twmb/avro returns time.Time when the file schema has 
logicalType,
+                       // but raw int32 when the file schema omits it.
                        if val, ok := v.(time.Time); ok {
                                return Date(val.Truncate(24*time.Hour).Unix() / 
int64((time.Hour * 24).Seconds()))
                        }
 
                        return Date(v.(int32))
-               case avro.TimeMillis:
+               case atype.TimeMillis:
                        if val, ok := v.(time.Duration); ok {

Review Comment:
   Should we add the same comment as in the `atype.Date` condition here and 
below?



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