DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #72
The code block shown below contains an error. The code block is intended to return the exact number of distinct values in column division in DataFrame storesDF. Identify the error. Code block…
The correct answer is A. The approx_count_distinct() operation needs a second argument to set the rsd parameter to. The approx_count_distinct() function uses the HyperLogLog++ algorithm and is inherently approximate - it cannot return an exact distinct count regardless of configuration. The second argument, rsd (relative standard deviation), controls the accuracy trade-off: a smaller rsd…
Question
The code block shown below contains an error. The code block is intended to return the exact number of distinct values in column division in DataFrame storesDF. Identify the error. Code block:
storesDF.agg(approx_count_distinct(col("division")).alias("divisionDist inct"))
Options
- AThe approx_count_distinct() operation needs a second argument to set the rsd parameter to
- BThere is no alias() operation for the approx_count_distinct() operation's output.
- CThere is no way to return an exact distinct number in Spark because the data Is distributed across
- DThe approx_count_distinct()operation is not a standalone function - it should be used as a method
- EThe approx_count_distinct() operation cannot determine an exact number of distinct values in a
How the community answered
(13 responses)- A92% (12)
- E8% (1)
Explanation
The approx_count_distinct() function uses the HyperLogLog++ algorithm and is inherently approximate - it cannot return an exact distinct count regardless of configuration. The second argument, rsd (relative standard deviation), controls the accuracy trade-off: a smaller rsd yields higher accuracy but costs more compute. To get an exact distinct count in Spark, you must use countDistinct() (DataFrame API) or COUNT(DISTINCT ...) (SQL), not approx_count_distinct(). The error is that the chosen function is fundamentally the wrong tool for the stated goal of returning an exact count.
Topics
Community Discussion
No community discussion yet for this question.