spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gatorsmile <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-11980] [SQL] Fix json_tuple and add tes...
Date Thu, 26 Nov 2015 06:06:20 GMT
Github user gatorsmile commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9977#discussion_r45946595
  
    --- Diff: python/pyspark/sql/functions.py ---
    @@ -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()
    --- End diff --
    
    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!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message