iceberg-python: [BUG] Valid column characters fail on to_arrow() or to_pandas() ArrowInvalid: No match for FieldRef.Name

Apache Iceberg version

0.6.0 (latest release)

Please describe the bug šŸž

Related to: https://github.com/apache/iceberg-python/issues/81

Platform: MacOS M1 Python v3.12

pyiceberg 0.6.0
pyarrow 15.0.2
pandas 2.2.1

I believe this is a bug, but I may also be misunderstanding how pyiceberg and pyarrow are working with iceberg tables and thus I may be doing something wrong. However, when I sanitize the column name before writing the data to remove : . - / I’m able to query just fine.

My understanding is that the following iceberg column name is a valid name TEST:A1B2.RAW.ABC-GG-1-A. With the caveat that it is NOT a nested field (which I don’t need). I’m able to write the data to the iceberg table and it shows the metadata with the fully qualified name of TEST:A1B2.RAW.ABC-GG-1-A in the metadata json.

It appears to be only fail when I want to read the data. I’m following the basic getting started in the pyiceberg

table = catalog.load_table("A1B2.A1-301")

# neither table scan works (throws the same error):
df_pyarrow = table.scan().to_arrow()
df_panda = table.scan().to_pandas()

I created a sample project that reproduces my problem with the pyarrow.lib.ArrowInvalid: No match for FieldRef.Name(TEST_x3AA1B2_x2ERAW_x2EABC_x2DGG_x2D1_x2DA) in TEST:A1B2.RAW.ABC-GG-1-A: double error.

Also to clarify, these column names do need to be in this format as their format has a very specific use case within our hardware environments. We try to follow a URI style naming schema for our columns & sensors.

Image showing metadata is storing channel name as expected. image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

Oof, this is a big one. Thanks for reporting this @gwindes and thanks @kevinjqliu for jumping on this, and getting to the bottom of it. I’m also looping in @HonahX here since we want to include this fix in 0.6.1.

@Fokko Are you familiar with this behavior? I can’t find any documentations on it. The original PR by Ryan (https://github.com/apache/iceberg/pull/601) suggests that this was done to be compatible with Avro, since Avro spec does not allow special characters.

I don’t think Avro is the issue, we reference the fields using the field-id.

  • For writing, we want to have the same behavior as Java.
  • For reading in PyIceberg we have an additional step in PyIceberg: When reading we read the Parquet files using the original column names, and we rename the fields afterward in this visitor. We could correct it there, but we want to make sure that we don’t write any invalid Parquet filenames in the first place.

Generated a Parquet file using both Spark and Python:

image image

Looking at the Python file:

parq 00000-0-624f6b17-7d8d-435e-9fbe-217caeb25ca4.parquet --schema 

 # Schema 
 <pyarrow._parquet.ParquetSchema object at 0x108ba68c0>
required group field_id=-1 schema {
  optional double field_id=1 ABC-GG-1-A;
}

And the Spark file:

parq 00000-0-875786b8-dbcd-4f8c-80a7-d261205a0333-0-00001.parquet --schema 

 # Schema 
 <pyarrow._parquet.ParquetSchema object at 0x126623780>
required group field_id=-1 table {
  required double field_id=1 ABC_x2DGG_x2D1_x2DA;
}

I would argue that the Python one is correct, but probably missing some context. The names shouldn’t matter in the end, so probably we should look them up by ID.

Further research shows that when I use daft that I’m able to read and use the to_arrow() functionality just fine. This is interesting especially because daft utilizes pyiceberg.

The column name transformation behavior is part of the Java Iceberg spec when reading/writing parquet files. Specifically, the transformed schema is pushed down to parquet reader/writer. I suspect this is happening since the Java parquet implementation supports both Avro and parquet schema (See parquet cli). So to be compatible with both parquet and Avro schemas, this column name transformation behavior is used.

From what I’ve seen, libraries in other languages do not do this. This means these libraries can read/write parquet files having special characters in their column names.

Daft uses the Rust Arrow library which can read parquet files with special characters in their column names. Similarly, pyarrow can read it as well.

I checked major parquet libraries in Python, Rust, Golang and they can all support reading special characters in parquet column names.

This was a super interesting deep dive.

So Iceberg has an obscure behavior of transforming column names with special characters. As you see above, TEST:A1B2.RAW.ABC-GG-1-A is transformed into TEST_x3AA1B2_x2ERAW_x2EABC_x2DGG_x2D1_x2DA. This is mentioned in #83 and refers to the AvroSchemaUtil::makeCompatibleName function.

Java Iceberg Behavior

When there is a special character in the column name, Iceberg will transform the column name first before writing to parquet. The resulting parquet file will have the transformed column name while Iceberg retains the original column name in the metadata. When writing, Iceberg will write parquet files with the transformed column name. When reading, Iceberg will perform the transformation to read the transformed column name. This is done by matching the column id.

Python Iceberg Behavior

The issue in PyIceberg here is not the read side, it’s the write side! When an Iceberg table’s column name has special characters, the parquet files should contain the transformed column name. Instead, PyIceberg writes the column name with the special characters.

That is the issue above, there is a mismatch between the expected column name (transformed, TEST_x3AA1B2_x2ERAW_x2EABC_x2DGG_x2D1_x2DA) and the actual column name (untransformed, TEST:A1B2.RAW.ABC-GG-1-A).

@gwindes The fixes for the read side (#597) and write side (#590) are merged. Thanks for reporting this issue!

The issue has been fixed by this PR #597, which will be included in 0.6.1. Now, pyiceberg could read parquet files with original column names and those with transformed column names. I will leave this issue open for a while since the discussion and PR for aligning write behavior with java implementation is still open: #590

Sorry for being late here. Thanks everyone for the valuable discussion, PR and tests. I would like to add some insight on the read side. Regarding @Fokko 's comment:

If we truly read by Field-IDs the names should be irrelevant, so we should probably update our mapping to ensure we correctly project by IDs.

I think we do correctly project by IDs. The real problem is the way that we sanitize the column names. In https://github.com/apache/iceberg-python/pull/83, we add sanitize the file_schema in _task_to_table with the assumption that the column name of the parquet file follows the Avro Naming spec. However, I think the ā€œsanitizationā€ should be more general here: it should just ensure that the final file_project_schema contains the same column names of the parquet file’s schema.

The names in file_schema are different from the actual column names in the parquet file because we first try to load the file schema from the json string stored in the parquet file metadata: link

file_schema = (
            Schema.model_validate_json(schema_raw) if schema_raw is not None else pyarrow_to_schema(physical_schema, name_mapping)
        )

Parquet files written by iceberg java contain this metadata json string. The json string represents the iceberg table schema at the time of writting the file. Therefore, it contains un-sanitized column names.

Since we always need to run a visitor to sanitize/ensure column names match, how about we just get the file_schema directly from the pyarrow physical schema?

file_schema = pyarrow_to_schema(physical_schema, name_mapping)

This way, we can ensure that the column names match, and thus do not need to sanitize the column names later.

I have verified that changing to this can fix both the sanitization issue in #83 and the issue here. Given that we want to align the writing behavior with the java implementation, we should also proceed #590.

Appreciate any thoughts and suggestions on this!

@gwindes please take a look at #590, it worked for me locally using the sample project you provided above