QuestionQ24

VerifiedUsing Spark SQL

A data engineer is working with the num_df DataFrame:

num_df = spark.range(5).toDF(“num”)  

The engineer is using the Python UDF:

def cubefunc(val):  
    return val ** 3  

Which code fragment registers and invokes this UDF as a Spark SQL function for use with the num_df DataFrame?

Explanation

spark.udf.register associates a Python function with a SQL function name, which can then be invoked in selectExpr. The cube operation returns Python integer values, so IntegerType() is the matching declared Spark SQL return type. PySpark documents that a registered Python function’s produced value must match its specified return type.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!