zero323 commented on a change in pull request #27278: 
[SPARK-30569][SQL][PYSPARK][SPARKR] Add percentile_approx DSL functions.
URL: https://github.com/apache/spark/pull/27278#discussion_r368509689
 
 

 ##########
 File path: python/pyspark/sql/functions.py
 ##########
 @@ -582,6 +582,51 @@ def nanvl(col1, col2):
     return Column(sc._jvm.functions.nanvl(_to_java_column(col1), 
_to_java_column(col2)))
 
 
+@since(3.0)
+def percentile_approx(col, percentage, accuracy=10000):
+    """Returns the approximate percentile value of numeric column col at the 
given percentage.
+    The value of percentage must be between 0.0 and 1.0.
+
+    The accuracy parameter (default: 10000)
+    is a positive numeric literal which controls approximation accuracy at the 
cost of memory.
+    Higher value of accuracy yields better accuracy, 1.0/accuracy is the 
relative error
+    of the approximation.
+
+    When percentage is an array, each value of the percentage array must be 
between 0.0 and 1.0.
+    In this case, returns the approximate percentile array of column col
+    at the given percentage array.
+
+    >>> df = (spark.range(0, 1000, 1, 1)
+    ...     .withColumn("id", col("id") % 3)
+    ...     .withColumn("value", randn(42) + col("id") * 10))
+    >>> (df
+    ...     .select(percentile_approx("value", [0.25, 0.5, 0.75], 
1000000).alias("quantiles"))
+    ...     .show(truncate=False))
+    +----------------------------------------------------------+
+    |quantiles                                                 |
+    +----------------------------------------------------------+
+    |[0.7264430125286507, 9.98975299938167, 19.335304783039014]|
 
 Review comment:
   Maybe `printSchema`? I think the main point is to show that schema can 
differ from input to input.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to