scale defaults to 0 if not specified
MODIFY
fastavro/_read.pyx
fastavro/_read.pyx
@@ -184,7 +184,7 @@ cpdef _read_decimal(data, size, writer_schema):
based on https://github.com/apache/avro/pull/82/
"""
cdef int32 offset
- scale = writer_schema['scale']
+ scale = writer_schema.get('scale', 0)
precision = writer_schema['precision']
datum_byte = str2ints(data)

MODIFY
fastavro/_read_py.py
fastavro/_read_py.py
@@ -167,7 +167,7 @@ def _read_decimal(data, size, writer_schema):
"""
based on https://github.com/apache/avro/pull/82/
"""
- scale = writer_schema['scale']
+ scale = writer_schema.get('scale', 0)
precision = writer_schema['precision']
datum_byte = str2ints(data)

MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -145,7 +145,7 @@ cpdef prepare_bytes_decimal(object data, schema):
cdef bytearray tmp
if not isinstance(data, decimal.Decimal):
return data
- scale = schema['scale']
+ scale = schema.get('scale', 0)
# based on https://github.com/apache/avro/pull/82/
@@ -186,7 +186,7 @@ cpdef prepare_fixed_decimal(object data, schema):
cdef bytearray tmp
if not isinstance(data, decimal.Decimal):
return data
- scale = schema['scale']
+ scale = schema.get('scale', 0)
size = schema['size']
# based on https://github.com/apache/avro/pull/82/

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -108,7 +108,7 @@ def prepare_bytes_decimal(data, schema):
"""Convert decimal.Decimal to bytes"""
if not isinstance(data, decimal.Decimal):
return data
- scale = schema['scale']
+ scale = schema.get('scale', 0)
# based on https://github.com/apache/avro/pull/82/
@@ -149,7 +149,7 @@ def prepare_fixed_decimal(data, schema):
"""Converts decimal.Decimal to fixed length bytes array"""
if not isinstance(data, decimal.Decimal):
return data
- scale = schema['scale']
+ scale = schema.get('scale', 0)
size = schema['size']
# based on https://github.com/apache/avro/pull/82/

MODIFY
tests/test_logical_types.py
tests/test_logical_types.py
@@ -338,3 +338,18 @@ def test_unknown_logical_type():
converted = serialize(unknown_type_schema, data1)
data2 = deserialize(unknown_type_schema, converted)
assert (data1 == data2)
+
+
+def test_default_scale_value():
+ schema = {
+ "name": "test_default_scale_value",
+ "type": "fixed",
+ "size": 8,
+ "logicalType": "decimal",
+ "precision": 15,
+ }
+
+ data1 = Decimal("-2")
+ binary = serialize(schema, data1)
+ data2 = deserialize(schema, binary)
+ assert (data1 == data2)

scale defaults to 0 if not specified