Skip to content
  • Dongjoon Hyun's avatar
    14869ae6
    [SPARK-14639] [PYTHON] [R] Add `bround` function in Python/R. · 14869ae6
    Dongjoon Hyun authored
    ## 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.
    14869ae6
    [SPARK-14639] [PYTHON] [R] Add `bround` function in Python/R.
    Dongjoon Hyun authored
    ## 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.
Loading