allisonwang-db commented on code in PR #42272: URL: https://github.com/apache/spark/pull/42272#discussion_r1283791586
########## examples/src/main/python/sql/udtf.py: ########## @@ -0,0 +1,169 @@ +# +# 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. +# + +""" +A simple example demonstrating Python UDTFs in Spark +Run with: + ./bin/spark-submit examples/src/main/python/sql/udtf.py +""" + +# NOTE that this file is imported in user guide in PySpark documentation. +# The codes are referred via line numbers. See also `literalinclude` directive in Sphinx. +import pandas as pd +from typing import Iterator, Any + +from pyspark.sql import SparkSession +from pyspark.sql.pandas.utils import require_minimum_pandas_version, require_minimum_pyarrow_version + +# Python UDTFs use Arrow by default. +require_minimum_pandas_version() +require_minimum_pyarrow_version() + + +def python_udtf_simple_example(spark: SparkSession): + + from pyspark.sql.functions import lit, udtf + + class SimpleUDTF: + def eval(self, x: int, y: int): + yield x + y, x - y + + # Now, create a Python UDTF using the defined class and specify a return type + func = udtf(SimpleUDTF, returnType="c1: int, c2: int") + + func(lit(1), lit(2)).show() + # +---+---+ + # | c1| c2| + # +---+---+ + # | 3| -1| + # +---+---+ + + +def python_udtf_registration(spark: SparkSession): + + from pyspark.sql.functions import udtf + + # Use the decorator to define the UDTF. + @udtf(returnType="c1: int, c2: int") + class PlusOne: + def eval(self, x: int): + yield x, x + 1 + + # Register the UDTF + spark.udtf.register("plus_one", PlusOne) + + # Use the UDTF in SQL + spark.sql("SELECT * FROM plus_one(1)").show() + # +---+---+ + # | c1| c2| + # +---+---+ + # | 1| 2| + # +---+---+ + + # Use the UDTF in SQL with lateral join + spark.sql("SELECT * FROM VALUES (0, 1), (1, 2) t(x, y), LATERAL plus_one(x)").show() + # +---+---+---+---+ + # | x| y| c1| c2| + # +---+---+---+---+ + # | 0| 1| 0| 1| + # | 1| 2| 1| 2| + # +---+---+---+---+ + + +def python_udtf_terminate_example(spark: SparkSession): + + from pyspark.sql.functions import udtf + + @udtf(returnType="cnt: int") + class CountUDTF: + def __init__(self): + self.count = 0 + + def eval(self, x): + self.count += 1 + + def terminate(self): + yield self.count, Review Comment: Yes, each element corresponds to one column in the output schema. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
