hequn8128 commented on a change in pull request #11531: 
[FLINK-16748][python][doc] Add Python UDTF doc
URL: https://github.com/apache/flink/pull/11531#discussion_r407952095
 
 

 ##########
 File path: docs/dev/table/functions/udfs.zh.md
 ##########
 @@ -247,53 +255,51 @@ tableEnv.sqlQuery("SELECT a, word, length FROM MyTable 
LEFT JOIN LATERAL TABLE(s
 </div>
 
 <div data-lang="python" markdown="1">
-{% highlight python %}
-'''
-Java code:
+In order to define a Python table function, one can extend the base class 
TableFunction in pyflink.table.udtf and Implement an evaluation method. The 
behavior of a Python table function is determined by the evaluation method 
which is named eval.
 
-// The generic type "Tuple2<String, Integer>" determines the schema of the 
returned table as (String, Integer).
-// The java class must have a public no-argument constructor and can be 
founded in current java classloader.
-public class Split extends TableFunction<Tuple2<String, Integer>> {
-    private String separator = " ";
-    
-    public void eval(String str) {
-        for (String s : str.split(separator)) {
-            // use collect(...) to emit a row
-            collect(new Tuple2<String, Integer>(s, s.length()));
-        }
-    }
-}
-'''
+In the Python Table API, a Python table function is used with `.join_lateral` 
or `.left_outer_join_lateral`. The `join_lateral` operator (cross) joins each 
row from the outer table (table on the left of the operator) with all rows 
produced by the table-valued function (which is on the right side of the 
operator). The `left_outer_join_lateral` operator joins each row from the outer 
table (table on the left of the operator) with all rows produced by the 
table-valued function (which is on the right side of the operator) and 
preserves outer rows for which the table function returns an empty table. In 
SQL use `LATERAL TABLE(<TableFunction>)` with CROSS JOIN and LEFT JOIN with an 
ON TRUE join condition (see examples below).
 
-table_env = BatchTableEnvironment.create(env)
-my_table = ...  # type: Table, table schema: [a: String]
+<span class="label label-info">Note</span> Currently, Python UDTF is supported 
in old planner both under streaming and batch mode while is only supported 
under streaming mode in Blink planner.
 
-# Register the java function.
-table_env.register_java_function("split", "my.java.function.Split")
+The following example shows how to define a Python table function, registered 
it in the TableEnvironment, and call it in a query. Note that you can configure 
your table function via a constructor before it is registered:
 
-# Use the table function in the Python Table API. "as" specifies the field 
names of the table.
-my_table.join_lateral("split(a) as (word, length)").select("a, word, length")
-my_table.left_outer_join_lateral("split(a) as (word, length)").select("a, 
word, length")
+{% highlight python %}
+class Split(TableFunction):
+    def eval(self, string):
+        for s in string.split(" "):
+            yield s, len(s)
+
+env = StreamExecutionEnvironment.get_execution_environment()
+table_env = StreamTableEnvironment.create(env)
+my_table = ...  # type: Table, table schema: [a: String]
 
-# Register the python function.
+# register the Python Table Function
+table_env.register_function("split", udtf(Split(), DataTypes.STRING(), 
[DataTypes.STRING(), DataTypes.INT()]))
 
-# Use the table function in SQL with LATERAL and TABLE keywords.
-# CROSS JOIN a table function (equivalent to "join" in Table API).
+# use the Python Table Function in Python Table API
+my_table.join_lateral("split(a) as (word, length)")
+my_table.left_outer_join_lateral("split(a) as (word, length)")
+
+# use the Python Table function in SQL API
 table_env.sql_query("SELECT a, word, length FROM MyTable, LATERAL 
TABLE(split(a)) as T(word, length)")
-# LEFT JOIN a table function (equivalent to "left_outer_join" in Table API).
 table_env.sql_query("SELECT a, word, length FROM MyTable LEFT JOIN LATERAL 
TABLE(split(a)) as T(word, length) ON TRUE")
+
 {% endhighlight %}
+
+There are many ways to define a Python table function besides extending the 
base class `TableFunction`.
+Please refer to the [Python Table Function]({{ site.baseurl 
}}/dev/table/python/python_udfs.html#table-functions) documentation for more 
details.
 
 Review comment:
   Change the link to the Chinese doc link, i.e., `zh/dev/`

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