WeiZhong94 commented on a change in pull request #8847: [Flink 12609][python]
Align the Python data types with Java
URL: https://github.com/apache/flink/pull/8847#discussion_r296637997
##########
File path: flink-python/pyflink/table/types.py
##########
@@ -400,55 +401,93 @@ def need_conversion(self):
return True
def to_sql_type(self, t):
- if t.tzinfo is not None:
- offset = t.utcoffset()
- offset = offset if offset else datetime.timedelta()
- offset_microseconds =\
- (offset.days * 86400 + offset.seconds) * 10 ** 6 +
offset.microseconds
- else:
- offset_microseconds = self.EPOCH_ORDINAL
- minutes = t.hour * 60 + t.minute
- seconds = minutes * 60 + t.second
- return seconds * 10**6 + t.microsecond - offset_microseconds
+ if t is not None:
+ if t.tzinfo is not None:
+ offset = t.utcoffset()
+ offset = offset if offset else datetime.timedelta()
+ offset_microseconds =\
+ (offset.days * 86400 + offset.seconds) * 10 ** 6 +
offset.microseconds
+ else:
+ offset_microseconds = self.EPOCH_ORDINAL
+ minutes = t.hour * 60 + t.minute
+ seconds = minutes * 60 + t.second
+ return seconds * 10 ** 6 + t.microsecond - offset_microseconds
Review comment:
current logic of `TimeType#to_sql_type` will return a negative value, if we
put the negative return value to `TimeType#from_sql_type` directly, it will
throw a ValueError. e.g.
`DataTypes.TIME().from_sql_type(DataTypes.TIME().to_sql_type(datetime.time(1,
0, 0)))` will throw: `ValueError: hour must be in 0..23`. And with current
logic of `TimeType#to_sql_type` and `TimeType#from_sql_type`,
`DataTypes.TIME().from_sql_type(DataTypes.TIME().to_sql_type(datetime.time(12,
0, 0))) == datetime.time(12, 0, 0)` is not true.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services