Skip to content

Exceptions thrown when column pruning metadata tables from partitioned Iceberg tables #1735

Closed

Description

@RussellSpitzer

A table created with Iceberg 0.8~ has issues when the metadata table is read using column pruning in Spark with Iceberg 0.9.1 or 0.9.0.

scala> val df = spark.read.format("iceberg").load("file:///Users/russellspitzer/Temp/OldIcebergTable#all_entries").show
+------+-------------------+---------------+--------------------+
|status|        snapshot_id|sequence_number|           data_file|
+------+-------------------+---------------+--------------------+
|     1|3638703680170519943|              0|[0, file:/var/fol...|
|     1|3402083355630191798|              0|[0, file:/var/fol...|
|     0|3402083355630191798|              0|[0, file:/var/fol...|
|     2|8080930776000500924|              0|[0, file:/var/fol...|
+------+-------------------+---------------+--------------------+
scala> val df = spark.read.format("iceberg").load("file:///Users/russellspitzer/Temp/OldIcebergTable#all_entries").select("status").show
20/11/06 10:55:27 ERROR Executor: Exception in task 0.0 in stage 2.0 (TID 2)
java.lang.IllegalArgumentException: Missing required field: data_file

This does not occur if you make the table within the same Iceberg Version (0.9.0 or 0.9.1). A different error occurs if you attempt to prune within the data_file construct itself.

val df = spark.read.format("iceberg").load("file:///Users/russellspitzer/Temp/OldIcebergTable#all_entries").select("data_file.file_path").show
20/11/06 11:45:42 ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0)/ 1]
java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Integer

Selecting just the data_file is fine

scala> val df = spark.read.format("iceberg").load("file:///Users/russellspitzer/Temp/OldIcebergTable#all_entries").select("data_file").show
+--------------------+
|           data_file|
+--------------------+
|[0, file:/var/fol...|
|[0, file:/var/fol...|
|[0, file:/var/fol...|
|[0, file:/var/fol...|
+--------------------+

Activity

  1. RussellSpitzer commented on Nov 6, 2020

    @RussellSpitzer
    MemberAuthor

    Attached is a sample table I made which reproduces the issue
    oldicebergtable.zip

  2. RussellSpitzer commented on Nov 6, 2020

    @RussellSpitzer
    MemberAuthor

    Ok these are two Separate issues.

    The "status" issue only happens when dealing with old tables.

    The "data_file.subfield" issue happens with new tables as well

  3. RussellSpitzer commented on Nov 6, 2020

    @RussellSpitzer
    MemberAuthor

    Ok both of these issues seem to be related to trying to file an impossible projection with the AvroReader.

    For the "Status" issue on the old table an empty projection is given,
    For the "subfield" issue the projection is given as if looking for a top level field which seems not to work

  4. RussellSpitzer commented on Nov 6, 2020

    @RussellSpitzer
    MemberAuthor

    The Status issue is caused by a BuildAvroProjection call,
    The empty projection note above is turned into a full manifest entry spec request including data_file as a required field. The actual records present in the "Record" call in BuildAvroProjection have a schema like

    status type:INT pos:0
    snapshot_id type:UNION pos:1
    

    This doesn't have the data_file field which causes the projection to fail with "No field data_file" found

    We get this because in ProjectionDatumReader.java setSchema we attempt to prune an expected schema which contains all files and result in a pruned schema with only the above 2 columns.

  5. RussellSpitzer commented on Nov 7, 2020

    @RussellSpitzer
    MemberAuthor

    Pretty sure the specific error is here

        Set<Integer> projectedIds = TypeUtil.getProjectedIds(expectedSchema)
    

    Given an expected schema

    < 0: Status, 1: snapshot_id, 3: Sequence_number, 2: data_file >
    

    The code returns

    <0 , 1, 3>
    

    So our pruned schema looks like
    Schema prunedSchema = AvroSchemaUtil.pruneColumns(newFileSchema, projectedIds, nameMapping);

    0 status, 1 snapshot_id
    

    Missing the requested data_file column

    This all seems to happen on the executors, and since we shouldn't be reading those other columns in the first place it seem to me like the Projection is actually not being serialized correctly to the executors apart from this error with the bad mapping for projectedIds

  6. RussellSpitzer commented on Nov 7, 2020

    @RussellSpitzer
    MemberAuthor

    From what I can tell this actually shouldn't be working on 0.9.1 but works because of a fluke.

    When running through prune Columns we check whether or not our field posses a selected ID

          Schema fieldSchema = fields.get(field.pos());
          // All primitives are selected by selecting the field, but map and list
          // types can be selected by projecting the keys, values, or elements.
          // This creates two conditions where the field should be selected: if the
          // id is selected or if the result of the field is non-null. The only
          // case where the converted field is non-null is when a map or list is
          // selected by lower IDs.
          if (selectedIds.contains(fieldId)) {
            filteredFields.add(copyField(field, field.schema(), fieldId));
          } else if (fieldSchema != null) {
            hasChange = true;
            filteredFields.add(copyField(field, fieldSchema, fieldId));
          }

    In the 0.9.1 table this correctly also passes through selected field ID's of 0,1,3

    We still get the field.pos() on data_file of 2 so it doesn't match. BUT
    fields.get(2) returns the Partition schema in "r2" partition type:Record pos:0

    Now we aren't actually looking for that field, we are looking for the data_file field. BUT if "filedSchema != null" we follow the secondary pruning pathway above. This means we get to add in a copy where we set
    "data_file to have schema RECORD and the fieldId" we expect. Luckily we are pruning this out at the spark level but we are reading the wrong column data here.

    The 0.8.0 table doesn't have the "r2" record placed in "fields" and instead just get's null
    leading to the error at the beginning of this ticket.

  7. RussellSpitzer commented on Nov 9, 2020

    @RussellSpitzer
    MemberAuthor

    Ok! I'm finally going less crazy, this isn't an issue with old tables, this is broken on new tables as well, any Partitioned table is broken regardless of when it was made. My 0.9.X table I was testing before is also broken if it is partitioned, the key difference was between partitioned and not.

  8. changed the title [-]Exceptions thrown when column pruning metadata tables from older iceberg tables[/-] [+]Exceptions thrown when column pruning metadata tables from partitioned Iceberg tables[/+] on Nov 9, 2020
  9. RussellSpitzer commented on Nov 9, 2020

    @RussellSpitzer
    MemberAuthor

    Repo for IcebergSourceTableTest

    @Test
      public void testEntriesTablePartitionedPrune() throws Exception {
        TableIdentifier tableIdentifier = TableIdentifier.of("db", "entries_test");
        Table table = createTable(tableIdentifier, SCHEMA, SPEC);
        Table entriesTable = loadTable(tableIdentifier, "entries");
    
        List<SimpleRecord> records = Lists.newArrayList(new SimpleRecord(1, "1"));
    
        Dataset<Row> inputDf = spark.createDataFrame(records, SimpleRecord.class);
        inputDf.select("id", "data").write()
            .format("iceberg")
            .mode("append")
            .save(loadLocation(tableIdentifier));
    
        table.refresh();
    
        List<Row> actual = spark.read()
            .format("iceberg")
            .load(loadLocation(tableIdentifier, "entries"))
            .select("status")
            .collectAsList();
    
        Snapshot snapshot = table.currentSnapshot();
    
        Assert.assertEquals("Should only contain one manifest", 1, snapshot.allManifests().size());
        Assert.assertEquals("Results should contain only one status", 1, actual.size());
    }
  10. RussellSpitzer commented on Nov 9, 2020

    @RussellSpitzer
    MemberAuthor

    A more complete view.

    We call pruneColumns with {0 : status,1 : snapshot, 3: sequence_number} as our selected ID's on a second pass (still not sure why we hit this twice or why we request these 3 columns seems like this is a bug too)

    These selectedIds are passed in with the fileSchema correctly but there is a slightly strange behavior within the pruning code

    } else if (filteredFields.size() == record.getFields().size()) {
    return record;

    This filter means that any "empty" schema will automatically not be removed. If empty schemas are never removed then any top level schema element that contains an empty sub schema element will be saved. This happens when a table is unpartitioned since the PartitionType struct subfield of the file schema is empty.

    When the table is unpartitioned the sub schema of "partitionType" is empty, this means filteredFields.size == record.getFields.size() SO the "data_file" field is not pruned out. This means when we go back up the stack and attempt to resolve the top level schema we get a call to "record" with the last parameter "fields" set to

    null,
    null,
    {"type":"record","name":"r2","fields":[{"name":"partition","type":{"type":"record","name":"r102","fields":[]},"field-id":102}]},
    

    This saves us in the "unpartitioned" pruning case because then when we get down to the check

    Schema fieldSchema = fields.get(field.pos());
    // All primitives are selected by selecting the field, but map and list
    // types can be selected by projecting the keys, values, or elements.
    // This creates two conditions where the field should be selected: if the
    // id is selected or if the result of the field is non-null. The only
    // case where the converted field is non-null is when a map or list is
    // selected by lower IDs.

    Fields.get(2) has a field schema Even though we didn't ask for "partition", the Line 97 check only says we cannot ever prune an empty struct. Since we can't prune the empty "partitionStruct" we end up having to keep data_file even though it is not on our list of selected Ids.

    TLDR This is broken because we ask for the wrong field IDs but is unbroken only for unpartitioned tables because we also incorrectly can never prune an empty struct

  11. RussellSpitzer commented on Nov 11, 2020

    @RussellSpitzer
    MemberAuthor

    Overview of bugs found: #1744

    Example of Bug 1

    From (A, B, C, D { foo, bar } )
    Project (A, B, C, D {})
    Returns
    (A, B, C) // Missing D which was required in the projection
    

    Example of Bug 2

    From (A, B, C, D {foo, bar, baz {}} ) // Starting with D containing an field baz which is empty
    Project (A)
    Returns
    (A, D{ baz {} }) // Includes D even though it was not requested
    

    In combination these sometimes negate each other ~ kinda
    Example of interaction of bugs

    From (A, B, C, D {foo, bar, baz{}})
    Project (A, B, C, D{})
    Returns
    (A, B, C, D{ baz{} }) // D is present although with an unrequested field but this satisfies the projection
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions