def __init__(self, constant_value): j_type = constant_value[0] serializer = PickleSerializer() pickled_data = serializer.loads(constant_value[1:]) # the type set contains # TINYINT,SMALLINT,INTEGER,BIGINT,FLOAT,DOUBLE,DECIMAL,CHAR,VARCHAR,NULL,BOOLEAN # the pickled_data doesn't need to transfer to anther python object if j_type == 0: self._constant_value = pickled_data # the type is DATE elif j_type == 1: self._constant_value = \ datetime.date(year=1970, month=1, day=1) + datetime.timedelta(days=pickled_data) # the type is TIME elif j_type == 2: seconds, milliseconds = divmod(pickled_data, 1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) self._constant_value = datetime.time(hours, minutes, seconds, milliseconds * 1000) # the type is TIMESTAMP elif j_type == 3: self._constant_value = \ datetime.datetime(year=1970, month=1, day=1, hour=0, minute=0, second=0) \ + datetime.timedelta(milliseconds=pickled_data) else: raise Exception("Unknown type %s, should never happen" % str(j_type))
def _parse_constant_value(constant_value) -> Tuple[str, Any]: j_type = constant_value[0] serializer = PickleSerializer() pickled_data = serializer.loads(constant_value[1:]) # the type set contains # TINYINT,SMALLINT,INTEGER,BIGINT,FLOAT,DOUBLE,DECIMAL,CHAR,VARCHAR,NULL,BOOLEAN # the pickled_data doesn't need to transfer to anther python object if j_type == 0: parsed_constant_value = pickled_data # the type is DATE elif j_type == 1: parsed_constant_value = \ datetime.date(year=1970, month=1, day=1) + datetime.timedelta(days=pickled_data) # the type is TIME elif j_type == 2: seconds, milliseconds = divmod(pickled_data, 1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) parsed_constant_value = datetime.time(hours, minutes, seconds, milliseconds * 1000) # the type is TIMESTAMP elif j_type == 3: parsed_constant_value = \ datetime.datetime(year=1970, month=1, day=1, hour=0, minute=0, second=0) \ + datetime.timedelta(milliseconds=pickled_data) else: raise Exception("Unknown type %s, should never happen" % str(j_type)) def _next_constant_num(): global _constant_num _constant_num = _constant_num + 1 return _constant_num constant_value_name = 'c%s' % _next_constant_num() return constant_value_name, parsed_constant_value