aboutsummaryrefslogtreecommitdiff
path: root/python/pyspark/sql/functions.py
diff options
context:
space:
mode:
authorDongjoon Hyun <dongjoon@apache.org>2016-04-19 22:28:11 -0700
committerDavies Liu <davies.liu@gmail.com>2016-04-19 22:28:11 -0700
commit14869ae64eb27830179d4954a5dc3e0a1e1330b4 (patch)
treec294dde39b5d77c6086b3d08a726b1c8b401b95a /python/pyspark/sql/functions.py
parent6f1ec1f2670cd55bc852a810ca9d5c6a2651a9f2 (diff)
downloadspark-14869ae64eb27830179d4954a5dc3e0a1e1330b4.tar.gz
spark-14869ae64eb27830179d4954a5dc3e0a1e1330b4.tar.bz2
spark-14869ae64eb27830179d4954a5dc3e0a1e1330b4.zip
[SPARK-14639] [PYTHON] [R] Add `bround` function in Python/R.
## What changes were proposed in this pull request? This issue aims to expose Scala `bround` function in Python/R API. `bround` function is implemented in SPARK-14614 by extending current `round` function. We used the following semantics from Hive. ```java public static double bround(double input, int scale) { if (Double.isNaN(input) || Double.isInfinite(input)) { return input; } return BigDecimal.valueOf(input).setScale(scale, RoundingMode.HALF_EVEN).doubleValue(); } ``` After this PR, `pyspark` and `sparkR` also support `bround` function. **PySpark** ```python >>> from pyspark.sql.functions import bround >>> sqlContext.createDataFrame([(2.5,)], ['a']).select(bround('a', 0).alias('r')).collect() [Row(r=2.0)] ``` **SparkR** ```r > df = createDataFrame(sqlContext, data.frame(x = c(2.5, 3.5))) > head(collect(select(df, bround(df$x, 0)))) bround(x, 0) 1 2 2 4 ``` ## How was this patch tested? Pass the Jenkins tests (including new testcases). Author: Dongjoon Hyun <dongjoon@apache.org> Closes #12509 from dongjoon-hyun/SPARK-14639.
Diffstat (limited to 'python/pyspark/sql/functions.py')
-rw-r--r--python/pyspark/sql/functions.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/python/pyspark/sql/functions.py b/python/pyspark/sql/functions.py
index 5017ab5b36..dac842c0ce 100644
--- a/python/pyspark/sql/functions.py
+++ b/python/pyspark/sql/functions.py
@@ -467,16 +467,29 @@ def randn(seed=None):
@since(1.5)
def round(col, scale=0):
"""
- Round the value of `e` to `scale` decimal places if `scale` >= 0
+ Round the given value to `scale` decimal places using HALF_UP rounding mode if `scale` >= 0
or at integral part when `scale` < 0.
- >>> sqlContext.createDataFrame([(2.546,)], ['a']).select(round('a', 1).alias('r')).collect()
- [Row(r=2.5)]
+ >>> sqlContext.createDataFrame([(2.5,)], ['a']).select(round('a', 0).alias('r')).collect()
+ [Row(r=3.0)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.round(_to_java_column(col), scale))
+@since(2.0)
+def bround(col, scale=0):
+ """
+ Round the given value to `scale` decimal places using HALF_EVEN rounding mode if `scale` >= 0
+ or at integral part when `scale` < 0.
+
+ >>> sqlContext.createDataFrame([(2.5,)], ['a']).select(bround('a', 0).alias('r')).collect()
+ [Row(r=2.0)]
+ """
+ sc = SparkContext._active_spark_context
+ return Column(sc._jvm.functions.bround(_to_java_column(col), scale))
+
+
@since(1.5)
def shiftLeft(col, numBits):
"""Shift the given value numBits left.