hequn8128 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_r334222347
########## File path: flink-python/pyflink/fn_execution/operations.py ########## @@ -79,6 +82,50 @@ 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,BOOLEAN + # 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 DATE + elif j_type == '\x01' or 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 == '\x02' or 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 == '\x03' or 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) + # the type is YearMonthInterval + elif j_type == '\x04' or j_type == 4: + self._constant_value = relativedelta(months=pickled_data) Review comment: dateutil.relativedelta is a third part module. It is not good to use it as the default type. For java/scala UDFs, YearMonthInterval will be converted to Integer and be passed to the eval() method. Maybe we can also pass int to the python UDF? (Same for the DayTimeInterval) What do you think? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services