1. A data engineer writes a BigQuery User-Defined Function (UDF) to calculate a discount percentage based on customer segment and purchase history. The UDF is written in SQL and includes a nested SELECT query. The function is used in a WHERE clause of a large query processing 10 billion rows. Performance is unexpectedly slow. What is the most likely cause?
- A. SQL UDFs are interpreted at runtime and cannot be optimized by the BigQuery query planner; rewrite as a JavaScript UDF instead
- B. The UDF is being executed for every row, and the nested SELECT causes a subquery to run per row; materialize the lookup table or use a JOIN instead✓ Correct
- C. BigQuery UDFs do not support nested SELECT statements; you must refactor to use only aggregate functions
- D. The UDF is inline expanded, which is correct, but the WHERE clause predicate pushdown doesn't work; move the UDF to the SELECT clause
Explanation
The correct answer is that the UDF is executed per row, and the nested SELECT causes excessive subqueries. When a UDF with a SELECT statement is applied to billions of rows in a WHERE clause, each row execution triggers the nested SELECT, resulting in billions of subquery evaluations. The solution is to materialize the lookup table or use a JOIN instead. Answer A is wrong; SQL UDFs are actually optimized well by BigQuery's planner. Answer C is false; SQL UDFs support nested SELECT statements. Answer D is misleading; moving the UDF to SELECT doesn't solve the issue if the underlying logic is inefficient.