Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-2721

Fix MapType compatibility issues with reading Parquet datasets

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.0.1
    • 1.1.0
    • SQL
    • None

    Description

      Parquet-thrift (along with most likely other implementations of parquet) supports null values in a map and this makes any thrift generated parquet files that contain a map unreadable by spark sql due to the following code in parquet-thrift for generating the schema for maps:

      parquet.thrift.ThriftSchemaConverter.java
        @Override
        public void visit(ThriftType.MapType mapType) {
          final ThriftField mapKeyField = mapType.getKey();
          final ThriftField mapValueField = mapType.getValue();
      
          //save env for map
          String mapName = currentName;
          Type.Repetition mapRepetition = currentRepetition;
      
          //=========handle key
          currentFieldPath.push(mapKeyField);
          currentName = "key";
          currentRepetition = REQUIRED;
          mapKeyField.getType().accept(this);
          Type keyType = currentType;//currentType is the already converted type
          currentFieldPath.pop();
      
          //=========handle value
          currentFieldPath.push(mapValueField);
          currentName = "value";
          currentRepetition = OPTIONAL;
          mapValueField.getType().accept(this);
          Type valueType = currentType;
          currentFieldPath.pop();
      
          if (keyType == null && valueType == null) {
            currentType = null;
            return;
          }
      
          if (keyType == null && valueType != null)
            throw new ThriftProjectionException("key of map is not specified in projection: " + currentFieldPath);
      
          //restore Env
          currentName = mapName;
          currentRepetition = mapRepetition;
          currentType = ConversionPatterns.mapType(currentRepetition, currentName,
                  keyType,
                  valueType);
        }
      

      Which causes an error on the spark side when we reach this step in the toDataType function that asserts that both the key and value are of repetition level REQUIRED:

      org.apache.spark.sql.parquet.ParquetTypes.scala
              case ParquetOriginalType.MAP => {
                assert(
                  !groupType.getFields.apply(0).isPrimitive,
                  "Parquet Map type malformatted: expected nested group for map!")
                val keyValueGroup = groupType.getFields.apply(0).asGroupType()
                assert(
                  keyValueGroup.getFieldCount == 2,
                  "Parquet Map type malformatted: nested group should have 2 (key, value) fields!")
                val keyType = toDataType(keyValueGroup.getFields.apply(0))
                println("here")
                assert(keyValueGroup.getFields.apply(0).getRepetition == Repetition.REQUIRED)
                val valueType = toDataType(keyValueGroup.getFields.apply(1))
                assert(keyValueGroup.getFields.apply(1).getRepetition == Repetition.REQUIRED)
                new MapType(keyType, valueType)
              }
      

      Currently I have modified parquet-thrift to use repetition REQUIRED just to make spark sql able to work on the parquet files since we don't actually use null values in our maps. However it would be preferred to use parquet-thrift and spark sql out of the box and have them work nicely together with our existing thrift data types without having to modify dependencies.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              rrusso2007 Rob Russo
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: