dianfu commented on a change in pull request #9977: [FLINK-14497][python]
Support primitive data types in Python user-defined functions
URL: https://github.com/apache/flink/pull/9977#discussion_r338917127
##########
File path: flink-python/pyflink/table/tests/test_udf.py
##########
@@ -329,6 +326,142 @@ def test_udf_without_arguments(self):
actual = source_sink_utils.results()
self.assert_equals(actual, ["1,2", "1,2", "1,2"])
+ def test_all_data_types(self):
+ def boolean_func(bool_param):
+ assert isinstance(bool_param, bool), 'bool_param of wrong type %s
!' \
+ % type(bool_param)
+ return bool_param
+
+ def tinyint_func(tinyint_param):
+ assert isinstance(tinyint_param, int), 'tinyint_param of wrong
type %s !' \
+ % type(tinyint_param)
+ return tinyint_param
+
+ def smallint_func(smallint_param):
+ assert isinstance(smallint_param, int), 'smallint_param of wrong
type %s !' \
+ % type(smallint_param)
+ assert smallint_param == 32767, 'smallint_param of wrong value %s'
% smallint_param
+ return smallint_param
+
+ def int_func(int_param):
+ assert isinstance(int_param, int), 'int_param of wrong type %s !' \
+ % type(int_param)
+ assert int_param == -2147483648, 'int_param of wrong value %s' %
int_param
+ return int_param
+
+ def bigint_func(bigint_param):
+ assert isinstance(bigint_param, int), 'bigint_param of wrong type
%s !' \
+ % type(bigint_param)
+ return bigint_param
+
+ def bigint_func_none(bigint_param):
+ assert bigint_param is None, 'bigint_param %s should be None!' %
bigint_param
+ return bigint_param
+
+ def float_func(float_param):
+ assert isinstance(float_param, float) and float_equal(float_param,
1.23, 1e-6), \
+ 'float_param is wrong value %s !' % float_param
+ return float_param
+
+ def double_func(double_param):
+ assert isinstance(double_param, float) and
float_equal(double_param, 1.98932, 1e-7), \
+ 'double_param is wrong value %s !' % double_param
+ return double_param
+
+ def bytes_func(bytes_param):
+ assert bytes_param == b'flink', \
+ 'bytes_param is wrong value %s !' % bytes_param
+ return bytes_param
+
+ def str_func(str_param):
+ assert str_param == 'pyflink', \
+ 'str_param is wrong value %s !' % str_param
+ return str_param
+
+ def date_func(date_param):
+ from datetime import date
+ assert date_param == date(year=2014, month=9, day=13), \
+ 'date_param is wrong value %s !' % date_param
+ return date_param
+
+ self.t_env.register_function(
+ "boolean_func", udf(boolean_func, [DataTypes.BOOLEAN()],
DataTypes.BOOLEAN()))
+
+ self.t_env.register_function(
+ "tinyint_func", udf(tinyint_func, [DataTypes.TINYINT()],
DataTypes.TINYINT()))
+
+ self.t_env.register_function(
+ "smallint_func", udf(smallint_func, [DataTypes.SMALLINT()],
DataTypes.SMALLINT()))
+
+ self.t_env.register_function(
+ "int_func",
Review comment:
one line is enough
----------------------------------------------------------------
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