Skip to content
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,6 @@ def countDistinct(col, *cols):
return Column(jc)


@since(1.4)
def monotonicallyIncreasingId():
"""
.. note:: Deprecated in 1.6, use monotonically_increasing_id instead.
"""
return monotonically_increasing_id()


@since(1.6)
def input_file_name():
"""Creates a string column for the file name of the current Spark task.
Expand All @@ -305,6 +297,10 @@ def input_file_name():
@since(1.6)
def isnan(col):
"""An expression that returns true iff the column is NaN.

>>> df = sqlContext.createDataFrame([(1.0, float('nan')), (float('nan'), 2.0)], ("a", "b"))
>>> df.select(isnan("a").alias("r1"), isnan(df.a).alias("r2")).collect()
[Row(r1=False, r2=False), Row(r1=True, r2=True)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.isnan(_to_java_column(col)))
Expand All @@ -313,11 +309,23 @@ def isnan(col):
@since(1.6)
def isnull(col):
"""An expression that returns true iff the column is null.

>>> df = sqlContext.createDataFrame([(1, None), (None, 2)], ("a", "b"))
>>> df.select(isnull("a").alias("r1"), isnull(df.a).alias("r2")).collect()
[Row(r1=False, r2=False), Row(r1=True, r2=True)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.isnull(_to_java_column(col)))


@since(1.4)
def monotonicallyIncreasingId():
"""
.. note:: Deprecated in 1.6, use monotonically_increasing_id instead.
"""
return monotonically_increasing_id()


@since(1.6)
def monotonically_increasing_id():
"""A column that generates monotonically increasing 64-bit integers.
Expand All @@ -344,6 +352,10 @@ def nanvl(col1, col2):
"""Returns col1 if it is not NaN, or col2 if col1 is NaN.

Both inputs should be floating point columns (DoubleType or FloatType).

>>> df = sqlContext.createDataFrame([(1.0, float('nan')), (float('nan'), 2.0)], ("a", "b"))
>>> df.select(nanvl("a", "b").alias("r1"), nanvl(df.a, df.b).alias("r2")).collect()
[Row(r1=1.0, r2=1.0), Row(r1=2.0, r2=2.0)]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.nanvl(_to_java_column(col1), _to_java_column(col2)))
Expand Down Expand Up @@ -1460,6 +1472,7 @@ def explode(col):
return Column(jc)


@ignore_unicode_prefix
@since(1.6)
def get_json_object(col, path):
"""
Expand All @@ -1468,22 +1481,47 @@ def get_json_object(col, path):

:param col: string column in json format
:param path: path to the json object to extract

>>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), \
("2", '''{"f1": "value12"}'''), \
("3", '''{"f2": 2}'''), \
("4", None), \
("5", '''{"f1": null}'''), \
("6", '''[invalid JSON string]''')]
>>> df = sqlContext.createDataFrame(data, ("key", "jstring"))
>>> df.select(df.key, get_json_object(df.jstring, '$.f1').alias("c0"), \
get_json_object(df.jstring, '$.f2').alias("c1") ).collect()
[Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None),
Row(key=u'3', c0=None, c1=u'2'), Row(key=u'4', c0=None, c1=None),
Row(key=u'5', c0=u'null', c1=None), Row(key=u'6', c0=None, c1=None)]
"""
sc = SparkContext._active_spark_context
jc = sc._jvm.functions.get_json_object(_to_java_column(col), path)
return Column(jc)


@ignore_unicode_prefix
@since(1.6)
def json_tuple(col, fields):
def json_tuple(col, *fields):
"""Creates a new row for a json column according to the given field names.

:param col: string column in json format
:param fields: list of fields to extract

>>> data = [("1", '''{"f1": "value1", "f2": "value2"}'''), \
("2", '''{"f1": "value12"}'''), \
("3", '''{"f2": 2}'''), \
("4", None), \
("5", '''{"f1": null}'''), \
("6", '''[invalid JSON string]''')]
>>> df = sqlContext.createDataFrame(data, ("key", "jstring"))
>>> df.select(df.key, json_tuple(df.jstring, 'f1', 'f2')).collect()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think one simple case should be enough for Python tests, other corner cases should be tested in Scala.

The Python doc tests will be part of API doc, so it's better to be read friendly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do. I will move the test case of get_json_object to the scala test file. Will simplify the existing test cases of get_json_object and json_tuple. Thanks!

[Row(key=u'1', c0=u'value1', c1=u'value2'), Row(key=u'2', c0=u'value12', c1=None),
Row(key=u'3', c0=None, c1=u'2'), Row(key=u'4', c0=None, c1=None),
Row(key=u'5', c0=None, c1=None), Row(key=u'6', c0=None, c1=None)]
"""
sc = SparkContext._active_spark_context
jc = sc._jvm.functions.json_tuple(_to_java_column(col), fields)
jc = sc._jvm.functions.json_tuple(_to_java_column(col), _to_seq(sc, fields))
return Column(jc)


Expand Down