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_r296639935
##########
File path: flink-python/pyflink/table/types.py
##########
@@ -457,12 +496,381 @@ def to_sql_type(self, dt):
if dt is not None:
seconds = (calendar.timegm(dt.utctimetuple()) if dt.tzinfo
else time.mktime(dt.timetuple()))
- return int(seconds) * 10**6 + dt.microsecond
+ return int(seconds) * 10 ** 6 + dt.microsecond
def from_sql_type(self, ts):
if ts is not None:
- # using int to avoid precision loss in float
- return datetime.datetime.fromtimestamp(ts //
10**6).replace(microsecond=ts % 10**6)
+ return datetime.datetime.fromtimestamp(ts // 10 **
6).replace(microsecond=ts % 10 ** 6)
+
+
+class ZonedTimestampType(AtomicType):
+ """
+ Timestamp data type with time zone. SQL TIMESTAMP WITH TIME ZONE.
+
+ Consisting of ``year-month-day hour:minute:second[.fractional] zone`` with
up to nanosecond
+ precision and values ranging from {@code 0000-01-01 00:00:00.000000000
+14:59} to
+ ``9999-12-31 23:59:59.999999999 -14:59``. Compared to the SQL standard,
leap seconds (23:59:60
+ and 23:59:61) are not supported.
+
+ The value will be stored internally all date and time fields, to a
precision of
+ nanoseconds, and a time-zone, with a zone offset used to handle ambiguous
local date-times.
+
+ The precision must be greater than or equal to 0 and less than or equal to
9.
+
+ :param precision: int, the number of digits of fractional seconds
(default: 6)
+ :param nullable: boolean, whether the field can be null (None) or not.
+ """
+
+ def __init__(self, precision=6, nullable=True):
+ super(ZonedTimestampType, self).__init__(nullable)
+ assert 0 <= precision <= 9
+ self.precision = precision
+
+ def __repr__(self):
+ return "ZonedTimestampType(%s, %s)" % (self.precision,
str(self._nullable).lower())
+
+ def need_conversion(self):
+ return True
+
+ def to_sql_type(self, dt):
+ if dt is not None:
+ seconds = (calendar.timegm(dt.utctimetuple()) if dt.tzinfo
+ else time.mktime(dt.timetuple()))
+ tzinfo = dt.tzinfo if dt.tzinfo else datetime.datetime.now(
+ datetime.timezone.utc).astimezone().tzinfo
+ offset = int(tzinfo.utcoffset(dt).total_seconds())
+ return int(seconds + offset) * 10 ** 6 + dt.microsecond, offset
+
+ def from_sql_type(self, zoned_ts):
+ if zoned_ts is not None:
+ from dateutil import tz
+ ts = zoned_ts[0] - zoned_ts[1] * 10 ** 6
+ tzinfo = tz.tzoffset(None, zoned_ts[1])
+ return datetime.datetime.fromtimestamp(ts // 10 ** 6,
tz=tzinfo).replace(
+ microsecond=ts % 10 ** 6)
+
+
+class Resolution(object):
+ """
+ Helper class for defining the resolution of an interval.
+
+ :param unit: value defined in the constants of :class:`IntervalUnit`.
+ :param precision: the number of digits of years (=year precision) or the
number of digits of
+ days (=day precision) or the number of digits of
fractional seconds (
+ =fractional precision).
+ """
+
+ class IntervalUnit(object):
+ SECOND = 0
+ MINUTE = 1
+ HOUR = 2
+ DAY = 3
+ MONTH = 4
+ YEAR = 5
+
+ def __init__(self, unit, precision=-1):
+ self._unit = unit
+ self._precision = precision
+
+ @property
+ def unit(self):
+ return self._unit
+
+ @property
+ def precision(self):
+ return self._precision
+
+ def __str__(self):
+ return '%s(%s)' % (self._unit, self._precision)
Review comment:
It seems that the actual value of self._unit is a number. I think it will be
better if we make a conversion between the number and its actual meaning?
----------------------------------------------------------------
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