Skip to content

Commit 5c62dfd

Browse files
committed
Get numeric datatype ranges via a static method
1 parent eb661da commit 5c62dfd

File tree

1 file changed

+20
-52
lines changed

1 file changed

+20
-52
lines changed

dbldatagen/nrange.py

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@
1515

1616
_OLD_MIN_OPTION = 'min'
1717
_OLD_MAX_OPTION = 'max'
18-
_MIN_BYTE_VALUE = -(2**4 - 1)
19-
_MAX_BYTE_VALUE = (2**4 - 1)
20-
_MIN_SHORT_VALUE = -(2**8 - 1)
21-
_MAX_SHORT_VALUE = (2**8 - 1)
22-
_MIN_INT_VALUE = -(2**16 - 1)
23-
_MAX_INT_VALUE = (2**16 - 1)
24-
_MIN_LONG_VALUE = -(2**32 - 1)
25-
_MAX_LONG_VALUE = (2**32 - 1)
26-
_MIN_FLOAT_VALUE = -3.402e38
27-
_MAX_FLOAT_VALUE = 3.402e38
28-
_MIN_DOUBLE_VALUE = -1.79769e308
29-
_MAX_DOUBLE_VALUE = 1.79769e308
3018

3119

3220
class NRange(DataRange):
@@ -95,47 +83,12 @@ def adjustForColumnDatatype(self, ctype):
9583
:param ctype: Spark SQL type instance to adjust range for
9684
:returns: No return value - executes for effect only
9785
"""
98-
if type(ctype) is DecimalType:
99-
if self.minValue is None:
100-
self.minValue = 0.0
101-
if self.maxValue is None:
102-
self.maxValue = math.pow(10, ctype.precision - ctype.scale) - 1.0
103-
104-
if type(ctype) is FloatType:
86+
numeric_types = [DecimalType, FloatType, DoubleType, ByteType, ShortType, IntegerType, LongType]
87+
if type(ctype) in numeric_types:
10588
if self.minValue is None:
106-
self.minValue = _MIN_FLOAT_VALUE
89+
self.minValue = NRange._getNumericDataTypeRange(ctype)[0]
10790
if self.maxValue is None:
108-
self.maxValue = _MAX_FLOAT_VALUE
109-
110-
if type(ctype) is DoubleType:
111-
if self.minValue is None:
112-
self.minValue = _MIN_DOUBLE_VALUE
113-
if self.maxValue is None:
114-
self.maxValue = _MAX_DOUBLE_VALUE
115-
116-
if type(ctype) is ByteType:
117-
if self.minValue is None:
118-
self.minValue = _MIN_BYTE_VALUE
119-
if self.maxValue is None:
120-
self.maxValue = _MAX_BYTE_VALUE
121-
122-
if type(ctype) is ShortType:
123-
if self.minValue is None:
124-
self.minValue = _MIN_SHORT_VALUE
125-
if self.maxValue is None:
126-
self.maxValue = _MAX_SHORT_VALUE
127-
128-
if type(ctype) is IntegerType:
129-
if self.minValue is None:
130-
self.minValue = _MIN_INT_VALUE
131-
if self.maxValue is None:
132-
self.maxValue = _MAX_INT_VALUE
133-
134-
if type(ctype) is LongType:
135-
if self.minValue is None:
136-
self.minValue = _MIN_LONG_VALUE
137-
if self.maxValue is None:
138-
self.maxValue = _MAX_LONG_VALUE
91+
self.maxValue = NRange._getNumericDataTypeRange(ctype)[1]
13992

14093
if type(ctype) is ShortType and self.maxValue is not None:
14194
assert self.maxValue <= 65536, "`maxValue` must be in range of short"
@@ -191,7 +144,8 @@ def getScale(self):
191144
# return maximum scale of components
192145
return max(smin, smax, sstep)
193146

194-
def _precision_and_scale(self, x):
147+
@staticmethod
148+
def _precision_and_scale(x):
195149
max_digits = 14
196150
int_part = int(abs(x))
197151
magnitude = 1 if int_part == 0 else int(math.log10(int_part)) + 1
@@ -204,3 +158,17 @@ def _precision_and_scale(self, x):
204158
frac_digits /= 10
205159
scale = int(math.log10(frac_digits))
206160
return (magnitude + scale, scale)
161+
162+
@staticmethod
163+
def _getNumericDataTypeRange(ctype):
164+
value_ranges = {
165+
ByteType: (0, (2 ** 4 - 1)),
166+
ShortType: (0, (2 ** 8 - 1)),
167+
IntegerType: (0, (2 ** 16 - 1)),
168+
LongType: (0, (2 ** 32 - 1)),
169+
FloatType: (0.0, 3.402e38),
170+
DoubleType: (0.0, 1.79769e308)
171+
}
172+
if type(ctype) is DecimalType:
173+
return 0.0, math.pow(10, ctype.precision - ctype.scale) - 1.0
174+
return value_ranges.get(type(ctype), None)

0 commit comments

Comments
 (0)