Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2563#discussion_r18384157
  
    --- Diff: python/pyspark/sql.py ---
    @@ -385,51 +398,35 @@ def _parse_datatype_string(datatype_string):
         >>> check_datatype(complex_maptype)
         True
         """
    -    index = datatype_string.find("(")
    -    if index == -1:
    -        # It is a primitive type.
    -        index = len(datatype_string)
    -    type_or_field = datatype_string[:index]
    -    rest_part = datatype_string[index + 1:len(datatype_string) - 1].strip()
    -
    -    if type_or_field in _all_primitive_types:
    -        return _all_primitive_types[type_or_field]()
    -
    -    elif type_or_field == "ArrayType":
    -        last_comma_index = rest_part.rfind(",")
    -        containsNull = True
    -        if rest_part[last_comma_index + 1:].strip().lower() == "false":
    -            containsNull = False
    -        elementType = _parse_datatype_string(
    -            rest_part[:last_comma_index].strip())
    -        return ArrayType(elementType, containsNull)
    -
    -    elif type_or_field == "MapType":
    -        last_comma_index = rest_part.rfind(",")
    -        valueContainsNull = True
    -        if rest_part[last_comma_index + 1:].strip().lower() == "false":
    -            valueContainsNull = False
    -        keyType, valueType = _parse_datatype_list(
    -            rest_part[:last_comma_index].strip())
    -        return MapType(keyType, valueType, valueContainsNull)
    -
    -    elif type_or_field == "StructField":
    -        first_comma_index = rest_part.find(",")
    -        name = rest_part[:first_comma_index].strip()
    -        last_comma_index = rest_part.rfind(",")
    -        nullable = True
    -        if rest_part[last_comma_index + 1:].strip().lower() == "false":
    -            nullable = False
    -        dataType = _parse_datatype_string(
    -            rest_part[first_comma_index + 1:last_comma_index].strip())
    -        return StructField(name, dataType, nullable)
    -
    -    elif type_or_field == "StructType":
    -        # rest_part should be in the format like
    -        # List(StructField(field1,IntegerType,false)).
    -        field_list_string = rest_part[rest_part.find("(") + 1:-1]
    -        fields = _parse_datatype_list(field_list_string)
    +    return _parse_datatype_json_value(json.loads(json_string))
    +
    +
    +def _parse_datatype_json_value(json_value):
    +    if type(json_value) is unicode and json_value in 
_all_primitive_types.keys():
    +        return _all_primitive_types[json_value]()
    +    elif 'array' in json_value:
    +        array_type = json_value['array']
    +        element_type = _parse_datatype_json_value(array_type['type'])
    +        contains_null = array_type['containsNull']
    +        return ArrayType(element_type, contains_null)
    --- End diff --
    
    If the jsonValue has one level, then these lines can be written like this:
    ```
    if json_value['type'] == 'array':
      return ArrayType(json_value['element'], json_value[''containsNull''])
    ```
    Also, it will be much easier to to like this:
    
    ```
    class ArrayType:
       @classmethod
       def load_from_json(cls, json):
            return ArrayType(json['element'], json[''containsNull''])
    
    types[json_value['type']].load_from_json(json_value)
    ```



---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to