twmb commented on code in PR #871:
URL: https://github.com/apache/iceberg-go/pull/871#discussion_r3075247632
##########
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:
I went back and forth on what type to choose to unmarshal into when
implementing the avro package. The initial implementation deserialized into
`*big.Rat`, but I switched away from that to better support round tripping
`schema.Decode(&any, []byte)` followed by `json.Marshal` which is done a lot in
redpanda-data/connect.
That said, I did this before adding full correct support for `EncodeJSON`
(recommended for actual correct round tripping) and `CustomType` (which I can
use preserve connect's old behavior that uses json.Marshal) to twmb/avro, and I
don't think it's too late to change the decoding type (the one important place
is connect and I can switch that). IMO both cases of *big.Rat or json.Number
(or a slice / array) are all difficult to use because using a map[string]any is
difficult to use outright. big.Rat _probably_ is the right choice? I'll make
the switch in 1.4.2.
--
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]