Skip to content

[Python] Reading parquet file with map without annotated map keys #37389

Closed

Description

@Max0u

Describe the bug, including details regarding any error messages, version, and platform.

Hello,

While reading a generated parquet file from AWS Athena (Trino engine behind) we have the following error:

  File "pypoetry/virtualenvs/lib/python3.10/site-packages/pyarrow/parquet/core.py", line 308, in __init__
    self.reader.open(
  File "pyarrow/_parquet.pyx", line 1239, in pyarrow._parquet.ParquetReader.open
  File "pyarrow/error.pxi", line 100, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: Map keys must be annotated as required.

(parquet-tools give the same error if you want to reproduce)

We don't have this error will reading it with spark. Is it Spark that's being lax with parquet spec? Or an issue with Arrow?

Athena Query if you want to reproduce

UNLOAD (
SELECT ARRAY [
          MAP(ARRAY['key_1', 'key_2'], ARRAY['value_1', 'value_2']),
          MAP(ARRAY['key_1', 'key_2'], ARRAY['value_2', 'value_2']),
          MAP(ARRAY['key_1', 'key_2'], ARRAY['value_3', 'value_2'])
      ]  AS array_map
)
TO 'path' 
WITH (format = 'PARQUET')

If you want directly the file:
https://file.io/Bs6BtISt3GFb

Component(s)

Parquet, Python

Activity

  1. mapleFU commented on Aug 25, 2023

    @mapleFU
    Member

    Would you mind print the schema using Python SDK? Personally I think it's a schema problem. Spark cannot produce "nonnull" field, but maybe we expect the map-key with nonnull.

  2. Max0u commented on Aug 25, 2023

    @Max0u
    Author

    Sure here the schema produce by pyspark:

    >>> df.printSchema()
    root
     |-- array_map: array (nullable = true)
     |    |-- element: map (containsNull = true)
     |    |    |-- key: string
     |    |    |-- value: string (valueContainsNull = true)
    

    I assume you meant pyspark by Python SDK, but let me know if I'm wrong, and I'll be more than pleased to help you!

  3. mapleFU commented on Aug 26, 2023

    @mapleFU
    Member

    Emmm I found that:

    required group field_id=-1 hive_schema {
      optional group field_id=-1 array_map (List) {
        repeated group field_id=-1 bag {
          optional group field_id=-1 array_element (Map) {
            repeated group field_id=-1 key_value (Map) {
              optional binary field_id=-1 key (String);
              optional binary field_id=-1 value (String);
            }
          }
        }
      }
    }
    
    SchemaNode: required group field_id=-1 hive_schema {
      optional group field_id=-1 array_map (List) {
        repeated group field_id=-1 bag {
          optional group field_id=-1 array_element (Map) {
            repeated group field_id=-1 key_value (Map) {
              optional binary field_id=-1 key (String);
              optional binary field_id=-1 value (String);
            }
          }
        }
      }
    }
    

    Which the key is optional. cc @wgtmac do we need to support file like this?

  4. wgtmac commented on Aug 29, 2023

    @wgtmac
    Member

    IMO, this is something that the writer should fix. optional map key is ill-formed as per the spec.

  5. negrusti commented on Aug 31, 2023

    @negrusti

    @wgtmac is there a downside of supporting this as we are encountering such files in the wild and can't influence the writer in any way?

  6. wgtmac commented on Aug 31, 2023

    @wgtmac
    Member

    Technically this can be fixed by postponing the exception until a real null map key data has been caught. But personally I am not in favor of this kind of non-standard fix.

    WDYT? @pitrou @emkornfield

  7. mapleFU commented on Aug 31, 2023

    @mapleFU
    Member

    Maybe we can find the version of PySpark and found how other user solve this problem, since PySpark is so widely used?

  8. wgtmac commented on Aug 31, 2023

    @wgtmac
    Member

    BTW, is there any open issue in the Trino community? @negrusti

  9. negrusti commented on Aug 31, 2023

    @negrusti
  10. wgtmac commented on Aug 31, 2023

    @wgtmac
    Member

    cc @theosib-amazon Not sure if you are still working at AWS Athena.

  11. negrusti commented on Aug 31, 2023

    @negrusti

    @wgtmac my argument for the change is -
    the data is precious and its source might be never available again. The code is secondary. Therefore the code must make every effort to read the data that is imperfect. Otherwise it is like refusing to read a good book because the table of contents has a typo :)

  12. emkornfield commented on Sep 1, 2023

    @emkornfield
    Contributor

    Depending on where the error is thrown it might be possible to provide an override arrow schema that uses a list of key-value pairs, it seems like this approach or delaying error until null might be best

    While I agree data is precious maintaining loop holes for technically invalid files has a cost as well.

  13. mapleFU commented on Sep 1, 2023

    @mapleFU
    Member

    If already have some solving methods we can use that, otherwise I think whether we can regard that as a List<Struct<Key, Value>> and Cast to a Map?

    Since maintaining read a invalid schema from parquet is really dirty.

  14. pitrou commented on Sep 4, 2023

    @pitrou
    Member

    Technically this can be fixed by postponing the exception until a real null map key data has been caught.

    That sounds like quite a bit of additional maintenance. Also, I'm not sure delaying the error until actual nulls are read is really user-friendly...

    I think whether we can regard that as a List<Struct<Key, Value>> and Cast to a Map

    This has the same problem of additional maintenance and delaying the error until much later.

    This might not be an immediate solution, but perhaps a third-party utility could be created to "fix" non-conforming Parquet files by rewriting them? It could address a growing number of non-conformity issues while not forcing mainline implementations to reimplement compatibility hacks.

  15. negrusti commented on Sep 4, 2023

    @negrusti

    @pitrou 3rd party utility will work for our case but I was not able to find one so far. Most of the parquet-manipulating projects I saw are using pyarrow :)

  16. pitrou commented on Sep 4, 2023

    @pitrou
    Member

    Yeah, I know. I was suggesting that perhaps someone would want to work on such a project :-)

  17. wgtmac commented on Sep 4, 2023

    @wgtmac
    Member

    We don't have this error will reading it with spark. Is it Spark that's being lax with parquet spec? Or an issue with Arrow?

    It seems that these rewriting work can be done via Apache Spark?

  18. negrusti commented on Sep 4, 2023

    @negrusti

    @wgtmac did not find a way to manipulate the column metadata with Spark, do you know one?

  19. wgtmac commented on Sep 4, 2023

    @wgtmac
    Member

    @wgtmac did not find a way to manipulate the column metadata with Spark, do you know one?

    Could you please provide one "corrupted" file?

  20. negrusti commented on Sep 4, 2023

    @negrusti

    @wgtmac
    aws s3 cp --no-sign-request s3://overturemaps-us-west-2/release/2023-07-26-alpha.0/theme=transportation/type=connector/20230726_134827_00007_dg6b6_01b086fc-f35b-487c-8d4e-5cdbbdc1785d test.parquet

  21. wgtmac commented on Sep 6, 2023

    @wgtmac
    Member

    @negrusti Sorry for the late reply. I have tried to use Apache Spark to rewrite the file and it seems worked.

    package a.b.c
    
    import org.apache.spark.sql.SparkSession
    
    object ParquetTest {
    
      def main(args: Array[String]) {
        val spark = SparkSession.builder.master("local[1]").appName("Parquet Test").getOrCreate()
        spark.read.parquet("/tmp/test.parquet").repartition(1).write.parquet("/tmp/rewrite")
      }
    
    }

    The original schema using parquet-cli from parquet-mr

    Created by: parquet-mr version 1.12.2-amzn-athena-1 (build 6c6353027ef5d7782e8657ea59b290452cdfdaee)
    Properties:
      writer.time.zone: UTC
    Schema:
    message hive_schema {
      optional binary id (STRING);
      optional int96 updatetime;
      optional int32 version;
      optional int32 level;
      optional binary subtype (STRING);
      optional group connectors (LIST) {
        repeated group bag {
          optional binary array_element (STRING);
        }
      }
      optional binary road (STRING);
      optional group sources (LIST) {
        repeated group bag {
          optional group array_element (MAP) {
            repeated group key_value (MAP_KEY_VALUE) {
              optional binary key (STRING);
              optional binary value (STRING);
            }
          }
        }
      }
      optional group bbox {
        optional double minx;
        optional double maxx;
        optional double miny;
        optional double maxy;
      }
      optional binary geometry;
    }
    

    The schema in the rewritten file is

    Created by: parquet-mr version 1.12.2 (build 77e30c8093386ec52c3cfa6c34b7ef3321322c94)
    Properties:
                       org.apache.spark.version: 3.3.2
      org.apache.spark.sql.parquet.row.metadata: {"type":"struct","fields":[{"name":"id","type":"string","nullable":true,"metadata":{}},{"name":"updatetime","type":"timestamp","nullable":true,"metadata":{}},{"name":"version","type":"integer","nullable":true,"metadata":{}},{"name":"level","type":"integer","nullable":true,"metadata":{}},{"name":"subtype","type":"string","nullable":true,"metadata":{}},{"name":"connectors","type":{"type":"array","elementType":"string","containsNull":true},"nullable":true,"metadata":{}},{"name":"road","type":"string","nullable":true,"metadata":{}},{"name":"sources","type":{"type":"array","elementType":{"type":"map","keyType":"string","valueType":"string","valueContainsNull":true},"containsNull":true},"nullable":true,"metadata":{}},{"name":"bbox","type":{"type":"struct","fields":[{"name":"minx","type":"double","nullable":true,"metadata":{}},{"name":"maxx","type":"double","nullable":true,"metadata":{}},{"name":"miny","type":"double","nullable":true,"metadata":{}},{"name":"maxy","type":"double","nullable":true,"metadata":{}}]},"nullable":true,"metadata":{}},{"name":"geometry","type":"binary","nullable":true,"metadata":{}}]}
    Schema:
    message spark_schema {
      optional binary id (STRING);
      optional int96 updatetime;
      optional int32 version;
      optional int32 level;
      optional binary subtype (STRING);
      optional group connectors (LIST) {
        repeated group list {
          optional binary element (STRING);
        }
      }
      optional binary road (STRING);
      optional group sources (LIST) {
        repeated group list {
          optional group element (MAP) {
            repeated group key_value {
              required binary key (STRING);
              optional binary value (STRING);
            }
          }
        }
      }
      optional group bbox {
        optional double minx;
        optional double maxx;
        optional double miny;
        optional double maxy;
      }
      optional binary geometry;
    }
    
  22. negrusti commented on Sep 6, 2023

    @negrusti

    @wgtmac wow thanks a lot for this - I didn't realize that I just need to write the data without applying any specific modifications!

  23. wgtmac commented on Sep 7, 2023

    @wgtmac
    Member

    If this works for you, feel free to close this issue or let me know if anything wrong happens. @negrusti @Max0u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions