Skip to content

Apache DataFusion SQL Query Engine

ARROW-1716: [Format/JSON] Use string integer value for Decimals in JSON

Phillip Cloud446ab02e6ba7
diff --git a/integration/integration_test.py b/integration/integration_test.py
index 59a1de5a46..205176eccc 100644
--- a/integration/integration_test.py
+++ b/integration/integration_test.py
@@ -65,24 +65,16 @@ def rands(nchars):
return ''.join(np.random.choice(RANDS_CHARS, nchars))
-if six.PY2:
- def frombytes(o):
- return o
+def tobytes(o):
+ if isinstance(o, six.text_type):
+ return o.encode('utf8')
+ return o
- def tobytes(o):
- if isinstance(o, unicode):
- return o.encode('utf8')
- else:
- return o
-else:
- def tobytes(o):
- if isinstance(o, str):
- return o.encode('utf8')
- else:
- return o
- def frombytes(o):
+def frombytes(o):
+ if isinstance(o, six.binary_type):
return o.decode('utf8')
+ return o
# from the merge_arrow_pr.py script
@@ -177,7 +169,7 @@ class PrimitiveType(DataType):
class PrimitiveColumn(Column):
def __init__(self, name, count, is_valid, values):
- Column.__init__(self, name, count)
+ super(PrimitiveColumn, self).__init__(name, count)
self.is_valid = is_valid
self.values = values
@@ -191,15 +183,16 @@ class PrimitiveColumn(Column):
]
-TEST_INT_MIN = - 2**31 + 1
-TEST_INT_MAX = 2**31 - 1
+TEST_INT_MAX = 2 ** 31 - 1
+TEST_INT_MIN = ~TEST_INT_MAX
+
class IntegerType(PrimitiveType):
def __init__(self, name, is_signed, bit_width, nullable=True,
min_value=TEST_INT_MIN,
max_value=TEST_INT_MAX):
- PrimitiveType.__init__(self, name, nullable=nullable)
+ super(IntegerType, self).__init__(name, nullable=nullable)
self.is_signed = is_signed
self.bit_width = bit_width
self.min_value = min_value
@@ -239,9 +232,11 @@ class DateType(IntegerType):
MILLISECOND = 1
def __init__(self, name, unit, nullable=True):
- self.unit = unit
bit_width = 32 if unit == self.DAY else 64
- IntegerType.__init__(self, name, True, bit_width, nullable=nullable)
+ super(DateType, self).__init__(
+ name, True, bit_width, nullable=nullable
+ )
+ self.unit = unit
def _get_type(self):
return OrderedDict([
@@ -268,9 +263,10 @@ class TimeType(IntegerType):
}
def __init__(self, name, unit='s', nullable=True):
+ super(TimeType, self).__init__(
+ name, True, self.BIT_WIDTHS[unit], nullable=nullable
+ )
self.unit = unit
- IntegerType.__init__(self, name, True, self.BIT_WIDTHS[unit],
- nullable=nullable)
def _get_type(self):
return OrderedDict([
@@ -283,9 +279,9 @@ class TimeType(IntegerType):
class TimestampType(IntegerType):
def __init__(self, name, unit='s', tz=None, nullable=True):
+ super(TimestampType, self).__init__(name, True, 64, nullable=nullable)
self.unit = unit
self.tz = tz
- IntegerType.__init__(self, name, True, 64, nullable=nullable)
def _get_type(self):
fields = [
@@ -302,7 +298,7 @@ class TimestampType(IntegerType):
class FloatingPointType(PrimitiveType):
def __init__(self, name, bit_width, nullable=True):
- PrimitiveType.__init__(self, name, nullable=nullable)
+ super(FloatingPointType, self).__init__(name, nullable=nullable)
self.bit_width = bit_width
self.precision = {
@@ -331,13 +327,30 @@ class FloatingPointType(PrimitiveType):
return PrimitiveColumn(name, size, is_valid, values)
-class DecimalType(PrimitiveType):
- def __init__(self, name, bit_width, precision, scale, nullable=True):
- PrimitiveType.__init__(self, name, nullable=True)
+DECIMAL_PRECISION_TO_VALUE = {
+ key: (1 << (8 * i - 1)) - 1 for i, key in enumerate(
+ [1, 3, 5, 7, 10, 12, 15, 17, 19, 22, 24, 27, 29, 32, 34, 36],
+ start=1,
+ )
+}
- self.bit_width = bit_width
+
+def decimal_range_from_precision(precision):
+ assert 1 <= precision <= 38
+ try:
+ max_value = DECIMAL_PRECISION_TO_VALUE[precision]
+ except KeyError:
+ return decimal_range_from_precision(precision - 1)
+ else:
+ return ~max_value, max_value
+
+
+class DecimalType(PrimitiveType):
+ def __init__(self, name, precision, scale, bit_width=128, nullable=True):
+ super(DecimalType, self).__init__(name, nullable=True)
self.precision = precision
self.scale = scale
+ self.bit_width = bit_width
@property
def numpy_type(self):
@@ -359,7 +372,8 @@ class DecimalType(PrimitiveType):
('typeBitWidth', self.bit_width)])])])
def generate_column(self, size, name=None):
- values = [random.randint(0, 2**self.bit_width - 1) for x in range(size)]
+ min_value, max_value = decimal_range_from_precision(self.precision)
+ values = [random.randint(min_value, max_value) for _ in range(size)]
is_valid = self._make_is_valid(size)
if name is None:
@@ -369,14 +383,12 @@ class DecimalType(PrimitiveType):
class DecimalColumn(PrimitiveColumn):
- def __init__(self, name, count, is_valid, values, bit_width):
- PrimitiveColumn.__init__(self, name, count, is_valid, values)
+ def __init__(self, name, count, is_valid, values, bit_width=128):
+ super(DecimalColumn, self).__init__(name, count, is_valid, values)
self.bit_width = bit_width
- self.hex_width = bit_width / 4
def _encode_value(self, x):
- hex_format_str = '%%0%dx' % self.hex_width
- return (hex_format_str % x).upper()
+ return str(x)
class BooleanType(PrimitiveType):
@@ -510,7 +522,7 @@ class StringColumn(BinaryColumn):
class ListType(DataType):
def __init__(self, name, value_type, nullable=True):
- DataType.__init__(self, name, nullable=nullable)
+ super(ListType, self).__init__(name, nullable=nullable)
self.value_type = value_type
def _get_type(self):
@@ -553,7 +565,7 @@ class ListType(DataType):
class ListColumn(Column):
def __init__(self, name, count, is_valid, offsets, values):
- Column.__init__(self, name, count)
+ super(ListColumn, self).__init__(name, count)
self.is_valid = is_valid
self.offsets = offsets
self.values = values
@@ -571,7 +583,7 @@ class ListColumn(Column):
class StructType(DataType):
def __init__(self, name, field_types, nullable=True):
- DataType.__init__(self, name, nullable=nullable)
+ super(StructType, self).__init__(name, nullable=nullable)
self.field_types = field_types
def _get_type(self):
@@ -620,7 +632,7 @@ class Dictionary(object):
class DictionaryType(DataType):
def __init__(self, name, index_type, dictionary, nullable=True):
- DataType.__init__(self, name, nullable=nullable)
+ super(DictionaryType, self).__init__(name, nullable=nullable)
assert isinstance(index_type, IntegerType)
assert isinstance(dictionary, Dictionary)
@@ -655,7 +667,7 @@ class DictionaryType(DataType):
class StructColumn(Column):
def __init__(self, name, count, is_valid, field_values):
- Column.__init__(self, name, count)
+ super(StructColumn, self).__init__(name, count)
self.is_valid = is_valid
self.field_values = field_values
@@ -758,11 +770,12 @@ def generate_primitive_case(batch_sizes):
def generate_decimal_case():
fields = [
- DecimalType('f1', 128, 24, 10, True),
- DecimalType('f2', 128, 32, -10, True)
+ DecimalType(name='f{}'.format(i), precision=precision, scale=2)
+ for i, precision in enumerate(range(3, 39))
]
- batch_sizes = [7, 10]
+ possible_batch_sizes = 7, 10
+ batch_sizes = [possible_batch_sizes[i % 2] for i in range(len(fields))]
return _generate_file('decimal', fields, batch_sizes)
@@ -867,8 +880,9 @@ class IntegrationRunner(object):
def _compare_implementations(self, producer, consumer):
print('##########################################################')
- print('{0} producing, {1} consuming'.format(producer.name,
- consumer.name))
+ print(
+ '{0} producing, {1} consuming'.format(producer.name, consumer.name)
+ )
print('##########################################################')
for json_path in self.json_files:
@@ -1033,6 +1047,7 @@ def run_all_tests(debug=False):
runner.run()
print('-- All tests passed!')
+
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Arrow integration test CLI')
parser.add_argument('--debug', dest='debug', action='store_true',