1. A data engineer has the following PySpark code: ```python from pyspark.sql.functions import col, when df_clean = df.withColumn( "priority", when(col("severity") == "critical", 1) .when(col("severity") == "high", 2) .when(col("severity") == "medium", 3) .otherwise(4) ) ``` Which Spark SQL expression is the closest equivalent to this PySpark transformation?
- A. ```sql SELECT *, IIF(severity = 'critical', 1, IIF(severity = 'high', 2, IIF(severity = 'medium', 3, 4))) AS priority FROM df ```
- B. ```sql SELECT *, CASE severity WHEN 'critical' THEN 1 WHEN 'high' THEN 2 WHEN 'medium' THEN 3 ELSE 4 END AS priority FROM df ```✓ Correct
- C. ```sql SELECT *, IF(severity IN ('critical','high','medium'), severity, 4) AS priority FROM df ```
- D. ```sql SELECT *, COALESCE(severity = 'critical', 1, severity = 'high', 2, 4) AS priority FROM df ```
Explanation
The PySpark `when().when().otherwise()` chain is a multi-branch conditional equivalent to a SQL `CASE` expression. Option B correctly uses a simple CASE expression that maps each severity string to the corresponding integer, with ELSE 4 for all other values — exactly matching the PySpark logic. Option A uses `IIF()`, which is not a standard Spark SQL function (it exists in some other SQL dialects but not Spark). Option C uses `IF()` with an `IN` check, which returns the string `severity` value (not an integer) and doesn't differentiate between critical/high/medium. Option D misuses `COALESCE`, which returns the first non-null argument from a list of values, not a conditional mapping.