Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Long timestamp default decoding #443

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var (
timeType = reflect.TypeOf(time.Time{})
ratType = reflect.TypeOf(big.Rat{})
durType = reflect.TypeOf(LogicalDuration{})
timeType = reflect.TypeOf(time.Time{})
timeDurationType = reflect.TypeOf(time.Duration(0))
ratType = reflect.TypeOf(big.Rat{})
durType = reflect.TypeOf(LogicalDuration{})
)

type null struct{}
Expand Down
22 changes: 12 additions & 10 deletions codec_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ func createDecoderOfNative(schema *PrimitiveSchema, typ reflect2.Type) ValDecode
convert: createLongConverter(schema.encodedType),
}

case st == Long && lt == "":
case st == Long:
isTimestamp := (lt == TimestampMillis || lt == TimestampMicros)
if isTimestamp && typ.Type1() == timeDurationType {
return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
typ.Type1().String(), schema.Type(), lt)}
}
if resolved {
return &longConvCodec[int64]{convert: createLongConverter(schema.encodedType)}
}
return &longCodec[int64]{}

case lt != "":
return &errorDecoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
typ.String(), schema.Type(), lt)}

default:
break
}
Expand Down Expand Up @@ -245,13 +246,14 @@ func createEncoderOfNative(schema Schema, typ reflect2.Type) ValEncoder {
case st == Long && lt == TimeMicros: // time.Duration
return &timeMicrosCodec{}

case st == Long && lt == "":
case st == Long:
isTimestamp := (lt == TimestampMillis || lt == TimestampMicros)
if isTimestamp && typ.Type1() == timeDurationType {
return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
typ.Type1().String(), schema.Type(), lt)}
}
return &longCodec[int64]{}

case lt != "":
return &errorEncoder{err: fmt.Errorf("avro: %s is unsupported for Avro %s and logicalType %s",
typ.String(), schema.Type(), lt)}

default:
break
}
Expand Down
57 changes: 57 additions & 0 deletions schema_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package avro_test

import (
"math/big"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -815,6 +816,62 @@ func TestSchemaCompatibility_Resolve(t *testing.T) {
"b": map[string]any{"a": int64(20)},
},
},
{
name: "Record Writer Field Missing With Long timestamp-millis Default",
reader: `{
"type":"record", "name":"test", "namespace": "org.hamba.avro",
"fields":[
{"name": "a", "type": "string"},
{
"name": "b",
"type": {
"type": "long",
"logicalType": "timestamp-millis"
},
"default": ` + strconv.FormatInt(1725616800000, 10) + `
}
]
}`,
writer: `{
"type":"record", "name":"test", "namespace": "org.hamba.avro",
"fields":[
{"name": "a", "type": "string"}
]
}`,
value: map[string]any{"a": "foo"},
want: map[string]any{
"a": "foo",
"b": time.UnixMilli(1725616800000).UTC(), // 2024-09-06 10:00:00
},
},
{
name: "Record Writer Field Missing With Long timestamp-micros Default",
reader: `{
"type":"record", "name":"test", "namespace": "org.hamba.avro",
"fields":[
{"name": "a", "type": "string"},
{
"name": "b",
"type": {
"type": "long",
"logicalType": "timestamp-micros"
},
"default": ` + strconv.FormatInt(1725616800000000, 10) + `
}
]
}`,
writer: `{
"type":"record", "name":"test", "namespace": "org.hamba.avro",
"fields":[
{"name": "a", "type": "string"}
]
}`,
value: map[string]any{"a": "foo"},
want: map[string]any{
"a": "foo",
"b": time.UnixMicro(1725616800000000).UTC(), // 2024-09-06 10:00:00
},
},
}

for _, test := range tests {
Expand Down
Loading