Skip to content

Commit 8aa6681

Browse files
brkyvzrxin
authored andcommitted
[SPARK-7358][SQL] Move DataFrame mathfunctions into functions
After a discussion on the user mailing list, it was decided to put all UDF's under `o.a.s.sql.functions` cc rxin Author: Burak Yavuz <[email protected]> Closes apache#5923 from brkyvz/move-math-funcs and squashes the following commits: a8dc3f7 [Burak Yavuz] address comments cf7a7bb [Burak Yavuz] [SPARK-7358] Move DataFrame mathfunctions into functions (cherry picked from commit ba2b566) Signed-off-by: Reynold Xin <[email protected]>
1 parent b5cd7dc commit 8aa6681

File tree

8 files changed

+543
-492
lines changed

8 files changed

+543
-492
lines changed

python/pyspark/sql/functions.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ def _(col):
5151
return _
5252

5353

54+
def _create_binary_mathfunction(name, doc=""):
55+
""" Create a binary mathfunction by name"""
56+
def _(col1, col2):
57+
sc = SparkContext._active_spark_context
58+
# users might write ints for simplicity. This would throw an error on the JVM side.
59+
jc = getattr(sc._jvm.functions, name)(col1._jc if isinstance(col1, Column) else float(col1),
60+
col2._jc if isinstance(col2, Column) else float(col2))
61+
return Column(jc)
62+
_.__name__ = name
63+
_.__doc__ = doc
64+
return _
65+
66+
5467
_functions = {
5568
'lit': 'Creates a :class:`Column` of literal value.',
5669
'col': 'Returns a :class:`Column` based on the given column name.',
@@ -63,6 +76,34 @@ def _(col):
6376
'sqrt': 'Computes the square root of the specified float value.',
6477
'abs': 'Computes the absolute value.',
6578

79+
# unary math functions
80+
'acos': 'Computes the cosine inverse of the given value; the returned angle is in the range' +
81+
'0.0 through pi.',
82+
'asin': 'Computes the sine inverse of the given value; the returned angle is in the range' +
83+
'-pi/2 through pi/2.',
84+
'atan': 'Computes the tangent inverse of the given value.',
85+
'cbrt': 'Computes the cube-root of the given value.',
86+
'ceil': 'Computes the ceiling of the given value.',
87+
'cos': 'Computes the cosine of the given value.',
88+
'cosh': 'Computes the hyperbolic cosine of the given value.',
89+
'exp': 'Computes the exponential of the given value.',
90+
'expm1': 'Computes the exponential of the given value minus one.',
91+
'floor': 'Computes the floor of the given value.',
92+
'log': 'Computes the natural logarithm of the given value.',
93+
'log10': 'Computes the logarithm of the given value in Base 10.',
94+
'log1p': 'Computes the natural logarithm of the given value plus one.',
95+
'rint': 'Returns the double value that is closest in value to the argument and' +
96+
' is equal to a mathematical integer.',
97+
'signum': 'Computes the signum of the given value.',
98+
'sin': 'Computes the sine of the given value.',
99+
'sinh': 'Computes the hyperbolic sine of the given value.',
100+
'tan': 'Computes the tangent of the given value.',
101+
'tanh': 'Computes the hyperbolic tangent of the given value.',
102+
'toDegrees': 'Converts an angle measured in radians to an approximately equivalent angle ' +
103+
'measured in degrees.',
104+
'toRadians': 'Converts an angle measured in degrees to an approximately equivalent angle ' +
105+
'measured in radians.',
106+
66107
'max': 'Aggregate function: returns the maximum value of the expression in a group.',
67108
'min': 'Aggregate function: returns the minimum value of the expression in a group.',
68109
'first': 'Aggregate function: returns the first value in a group.',
@@ -74,10 +115,21 @@ def _(col):
74115
'sumDistinct': 'Aggregate function: returns the sum of distinct values in the expression.',
75116
}
76117

118+
# math functions that take two arguments as input
119+
_binary_mathfunctions = {
120+
'atan2': 'Returns the angle theta from the conversion of rectangular coordinates (x, y) to' +
121+
'polar coordinates (r, theta).',
122+
'hypot': 'Computes `sqrt(a^2^ + b^2^)` without intermediate overflow or underflow.',
123+
'pow': 'Returns the value of the first argument raised to the power of the second argument.'
124+
}
125+
77126
for _name, _doc in _functions.items():
78127
globals()[_name] = _create_function(_name, _doc)
128+
for _name, _doc in _binary_mathfunctions.items():
129+
globals()[_name] = _create_binary_mathfunction(_name, _doc)
79130
del _name, _doc
80131
__all__ += _functions.keys()
132+
__all__ += _binary_mathfunctions.keys()
81133
__all__.sort()
82134

83135

python/pyspark/sql/mathfunctions.py

Lines changed: 0 additions & 101 deletions
This file was deleted.

python/pyspark/sql/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def test_crosstab(self):
416416

417417
def test_math_functions(self):
418418
df = self.sc.parallelize([Row(a=i, b=2 * i) for i in range(10)]).toDF()
419-
from pyspark.sql import mathfunctions as functions
419+
from pyspark.sql import functions
420420
import math
421421

422422
def get_values(l):

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,11 +1217,11 @@ class ExpressionEvaluationSuite extends ExpressionEvaluationBaseSuite {
12171217
unaryMathFunctionEvaluation(Tanh, math.tanh)
12181218
}
12191219

1220-
test("toDeg") {
1220+
test("toDegrees") {
12211221
unaryMathFunctionEvaluation(ToDegrees, math.toDegrees)
12221222
}
12231223

1224-
test("toRad") {
1224+
test("toRadians") {
12251225
unaryMathFunctionEvaluation(ToRadians, math.toRadians)
12261226
}
12271227

0 commit comments

Comments
 (0)