dstrodtman-db commented on code in PR #42272: URL: https://github.com/apache/spark/pull/42272#discussion_r1300362041
########## examples/src/main/python/sql/udtf.py: ########## @@ -0,0 +1,227 @@ +# +# 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 the User Guides in PySpark documentation. +# The codes are referred via line numbers. See also `literalinclude` directive in Sphinx. +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) -> None: + + class SimpleUDTF: + def eval(self, x: int, y: int) -> Iterator[Any]: + yield x + y, x - y + + from pyspark.sql.functions import lit, udtf + + func = udtf(SimpleUDTF, returnType="c1: int, c2: int") + + func(lit(1), lit(2)).show() # type: ignore + # +---+---+ + # | c1| c2| + # +---+---+ + # | 3| -1| + # +---+---+ + + +def python_udtf_decorator_example(spark: SparkSession) -> None: + + from pyspark.sql.functions import lit, udtf + + @udtf(returnType="c1: int, c2: int") # type: ignore + class SimpleUDTF: + def eval(self, x: int, y: int) -> Iterator[Any]: + yield x + y, x - y + + SimpleUDTF(lit(1), lit(2)).show() # type: ignore + # +---+---+ + # | c1| c2| + # +---+---+ + # | 3| -1| + # +---+---+ + + +def python_udtf_registration(spark: SparkSession) -> None: + + from pyspark.sql.functions import udtf + + @udtf(returnType="c1: int, c2: int") # type: ignore + class PlusOne: + def eval(self, x: int) -> Iterator[Any]: + yield x, x + 1 + + # Register the UDTF + spark.udtf.register("plus_one", PlusOne) # type: ignore + + # 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_arrow_example(spark: SparkSession) -> None: + + from pyspark.sql.functions import udtf + + @udtf(returnType="c1: int, c2: int", useArrow=True) # type: ignore + class PlusOne: + def eval(self, x: int) -> Iterator[Any]: + yield x, x + 1 + + +def python_udtf_terminate_example(spark: SparkSession) -> None: + + from pyspark.sql.functions import udtf + + @udtf(returnType="cnt: int") # type: ignore + class CountUDTF: + def __init__(self) -> None: Review Comment: Just confirming: the class is instantiated with new state information each time the UDTF is called, correct? That is to say: the default value here for `self.count` will always start at 0 each time the UDTF is called, even if there are multiple calls in the same SparkSession. The count only increments during a single call of the UDTF? -- 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]
