dianfu commented on a change in pull request #11784: [FLINK-17120][python] Add 
Cython support for operations
URL: https://github.com/apache/flink/pull/11784#discussion_r409992838
 
 

 ##########
 File path: flink-python/pyflink/fn_execution/fast_operations.pyx
 ##########
 @@ -0,0 +1,242 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# cython: language_level = 3
+# cython: infer_types = True
+# cython: profile=True
+# cython: boundscheck=False, wraparound=False, initializedcheck=False, 
cdivision=True
+
+import datetime
+
+import cloudpickle
+from apache_beam.runners.worker import bundle_processor
+from apache_beam.runners.worker import operation_specs
+
+from pyflink.fn_execution import flink_fn_execution_pb2
+from pyflink.metrics.metricbase import GenericMetricGroup
+from pyflink.serializers import PickleSerializer
+from pyflink.table import FunctionContext
+from pyflink.table.udf import DelegatingScalarFunction, DelegationTableFunction
+
+SCALAR_FUNCTION_URN = "flink:transform:scalar_function:v1"
+TABLE_FUNCTION_URN = "flink:transform:table_function:v1"
+
+cdef class StatelessFunctionOperation(Operation):
+    """
+    Base class of stateless function operation that will execute 
ScalarFunction or TableFunction for
+    each input element.
+    """
+
+    def __init__(self, name, spec, counter_factory, sampler, consumers):
+        super(StatelessFunctionOperation, self).__init__(name, spec, 
counter_factory, sampler)
+        self.consumer = consumers['output'][0]
+        self._value_coder_impl = 
self.consumer.windowed_coder.wrapped_value_coder.get_impl()
+        value_coder = self._value_coder_impl._value_coder
+        from pyflink.fn_execution.coder_impl import ArrowCoderImpl
+        if isinstance(value_coder, ArrowCoderImpl):
+            self._is_python_coder = True
+        else:
+            self._is_python_coder = False
+
+        self.variable_dict = {}
+        self.user_defined_funcs = []
+        self._func_num = 0
+        self._constant_num = 0
+        self.func = self.generate_func(self.spec.serialized_fn.udfs)
+        self._metric_enabled = self.spec.serialized_fn.metric_enabled
+        self.base_metric_group = None
+        if self._metric_enabled:
+            self.base_metric_group = GenericMetricGroup(None, None)
+        for user_defined_func in self.user_defined_funcs:
+            user_defined_func.open(FunctionContext(self.base_metric_group))
+
+    cpdef start(self):
+        with self.scoped_start_state:
+            super(StatelessFunctionOperation, self).start()
+
+    cpdef finish(self):
+        with self.scoped_finish_state:
+            super(StatelessFunctionOperation, self).finish()
+            self._update_gauge(self.base_metric_group)
+
+    cpdef teardown(self):
+        with self.scoped_finish_state:
+            for user_defined_func in self.user_defined_funcs:
+                user_defined_func.close()
+
+    cpdef process(self, WindowedValue o):
+        cdef InputStreamAndFunctionWrapper wrapper
+        with self.scoped_process_state:
+            output_stream = self.consumer.output_stream
+            if self._is_python_coder:
+                self._value_coder_impl.encode_to_stream(self.func(o.value), 
output_stream, True)
+            else:
+                wrapper = InputStreamAndFunctionWrapper(self.func, o.value)
+                self._value_coder_impl.encode_to_stream(wrapper, 
output_stream, True)
+            output_stream.maybe_flush()
+
+    cpdef monitoring_infos(self, transform_id):
 
 Review comment:
   progress_metrics method is missing

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

Reply via email to