QuestionQ222

VerifiedUsing Spark SQL

The code block below has an error. It is intended to create and register a SQL UDF named ASSESS_PERFORMANCE by using the Scala function assessPerformance(), then apply it to the customerSatisfaction column in the stores table. Identify the error.

Code block:

spark.udf.register(“ASSESS_PERFORMANCE”, assessPerforance)  
spark.sql(“SELECT customerSatisfaction, assessPerformance(customerSatisfaction) AS result FROM stores”)  
Explanation

A UDF registered under the SQL name ASSESS_PERFORMANCE must be invoked by that registered name. SQL function identifiers are case-insensitive, but assessPerformance is a different identifier because it lacks the underscore; the SQL expression should use ASSESS_PERFORMANCE(customerSatisfaction). Spark's UDF registration API accepts the UDF name first and the Scala function second, and registered UDFs can be called from SQL.

Learn more

Community Discussion

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