
Answer-first summary for fast verification
Answer: `TRANSFORM(food_grains, f -> f.food_grain_qty / 1000)`
The correct answer is **C** because the `TRANSFORM` higher-order function is specifically designed to apply a given function to each element in an array. In this scenario, we need to convert each food grain's quantity from kilograms to tons by dividing the quantity by 1000. The `TRANSFORM` function allows us to do this efficiently by iterating over each element in the `food_grains` array, applying the conversion, and then returning the transformed array. This approach is both concise and effective for the task at hand. Example usage in the query: ```sql TRANSFORM(food_grains, f -> f.food_grain_qty / 1000) as food_grains_qty_in_tons ``` This statement processes each element in the `food_grains` array, where `f` represents an individual element, and performs the division to convert the quantity from kilograms to tons.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
A data engineer is tasked with converting the quantity of food grains from kilograms to tons for a client dealing in agricultural products. The quantities are stored in a table named food_items. Given the schema of this table, which higher-order function should be used in the following query to achieve the desired conversion? (Note: 1 ton = 1000 kg)
SELECT warehouse_id,
food_grains,
____________ as food_grains_qty_in_tons
FROM food_items;
SELECT warehouse_id,
food_grains,
____________ as food_grains_qty_in_tons
FROM food_items;
A
MAP(food_grains, f -> f.food_grain_qty / 1000)
B
CONVERT(food_grains, f -> f.food_grain_qty / 1000)
C
TRANSFORM(food_grains, f -> f.food_grain_qty / 1000)
D
FLAT_MAP(food_grains, f -> f.food_grain_qty / 1000)
E
REDUCE(food_grains, f -> f.food_grain_qty / 1000)
No comments yet.