dianfu commented on a change in pull request #9858: [FLINK-14208][python] 
Optimize Python UDFs with parameters of constant values
URL: https://github.com/apache/flink/pull/9858#discussion_r333376429
 
 

 ##########
 File path: flink-python/pyflink/fn_execution/operations.py
 ##########
 @@ -79,6 +82,53 @@ def get(self, value):
         return self.scalar_function_invoker.invoke_eval(value)
 
 
+class ConstantInputGetter(InputGetter):
+    """
+    InputGetter for the input argument which is a constant value.
+
+    :param constant_value: the constant value of the column
+    """
+
+    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
+        # the pickled_data doesn't need to transfer to anther python object
+        if j_type == '\x00' or j_type == 0:
+            self._constant_value = pickled_data
+        # the type is BOOLEAN
+        elif j_type == '\x01' or j_type == 1:
+            self._constant_value = True if pickled_data == 'true' else False
+        # the type is DATE
+        elif j_type == '\x02' or j_type == 2:
+            self._constant_value = \
+                datetime.date(year=1970, month=1, day=1) + 
datetime.timedelta(days=pickled_data)
+        # the type is TIME
+        elif j_type == '\x03' or j_type == 3:
+            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 set contains TIMESTAMP
+        elif j_type == '\x04' or j_type == 4:
+            self._constant_value = \
+                datetime.datetime(year=1970, month=1, day=1, hour=0, minute=0, 
second=0) \
+                + datetime.timedelta(milliseconds=pickled_data)
+        # the type set is YEAR_INTERVAL_TYPES
+        elif j_type == '\x05' or j_type == 5:
+            self._constant_value = relativedelta(months=pickled_data)
+        # the type set is DAY_INTERVAL_TYPES
+        elif j_type == '\x06' or j_type == 6:
+            self._constant_value = 
datetime.timedelta(milliseconds=pickled_data)
+        else:
+            raise Exception("Unknown type %s, it may be encode/decode error" % 
str(j_type))
 
 Review comment:
   Unknown type %s, should never happen

----------------------------------------------------------------
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

Reply via email to