You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "lukaswelsch (via GitHub)" <gi...@apache.org> on 2026/04/20 13:20:22 UTC

[I] ArrowScan to_table fails if the data is mixed between dict-encoded strings and plain strings. [iceberg-python]

lukaswelsch opened a new issue, #3260:
URL: https://github.com/apache/iceberg-python/issues/3260

   ### Apache Iceberg version
   
   0.11.0 (latest release)
   
   ### Please describe the bug 🐞
   
   We have recently updated our functions to call the pyiceberg table.append() function with dict encoded arrow tables. Now we have in our iceberg tables mixed data from before this change, (where our data still is stored as string) and after the change, where the data is stored as dict-encoded strings. 
   
   If we now call to_arrow() of a DataScan class, on this table we get this error:
   
   ```
   pyarrow.lib.ArrowTypeError: Unable to merge: Field col has incompatible types: string vs dictionary<values=string, indices=int32, ordered=0>
   ```
   
   Here is a minimal example that reproduces this error:
   ```python
   from pyiceberg.io.pyarrow import ArrowScan
   from pyiceberg.table import ALWAYS_TRUE
   from pyiceberg.schema import Schema
   from pyiceberg.types import NestedField
   from pyiceberg.types import StringType
   
   import pyarrow as pa
   
   
   def create_scan_with_mixed_dict_encode_not_encode() -> ArrowScan:
       schema = Schema(
           NestedField(field_id=1, name="col", field_type=StringType(), required=False)
       )
   
       class FakeTableMetadata:
           def schema(self) -> Schema:
               return schema
   
       scan = ArrowScan(table_metadata=FakeTableMetadata(),
                        io=object(),
                        projected_schema=schema,
                        row_filter=ALWAYS_TRUE)
   
       def _batches_for_repro(self, _tasks):
           str_values = pa.array(["a"], type=pa.string())
           yield pa.record_batch([str_values], names=["col"])
           yield pa.record_batch([str_values.dictionary_encode()], names=["col"])
   
       ArrowScan.to_record_batches = _batches_for_repro
       return scan
   
   
   if __name__ == "__main__":
       scan = create_scan_with_mixed_dict_encode_not_encode()
       arrow_table = ArrowScan.to_table(scan, tasks=[])
   ```
   
   I am happy to provide a bugfix PR, but I need a small guidance on the best approach. 
   One idea is to cast each batch in to_table to the arrow_schema. The more performant way is to check for each batch, if the schema is different. If they are different, then find the dict_encoded col and only cast that one to string. 
   
   
   ### Willingness to contribute
   
   - [x] I can contribute a fix for this bug independently
   - [x] I would be willing to contribute a fix for this bug with guidance from the Iceberg community
   - [ ] I cannot contribute a fix for this bug at this time


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [I] ArrowScan to_table fails if the data is mixed between dict-encoded strings and plain strings. [iceberg-python]

Posted by "hzmsh (via GitHub)" <gi...@apache.org>.
hzmsh commented on issue #3260:
URL: https://github.com/apache/iceberg-python/issues/3260#issuecomment-6198820440

   This still reproduces on `main` (b32392dc) and 0.12.0. A common way to hit it is a pandas `Categorical` column, followed by a normal append:
   
   ```python
   import pandas as pd, pyarrow as pa
   
   # table has a string column `status`
   tbl.append(pa.Table.from_pandas(pd.DataFrame({"id": [1, 2], "status": pd.Categorical(["new", "done"])}), preserve_index=False))
   tbl.append(pa.Table.from_pandas(pd.DataFrame({"id": [3], "status": ["new"]}), preserve_index=False))
   
   tbl.scan().to_arrow()   # ArrowTypeError: Unable to merge: Field status has incompatible types: large_string vs dictionary<values=string, indices=int8, ...>
   tbl.scan().to_pandas()  # same
   tbl.scan().to_polars()  # same
   ```
   
   `to_arrow_batch_reader()` works because it casts each batch to the table schema, but `to_table` concatenates the per-file tables as they are.
   
   @Fokko, would you be open to a fix in `ArrowScan.to_table` that decodes dictionary columns to plain strings before concatenating, unless the column is listed in `dictionary_columns`? This matches `to_arrow_batch_reader()`, and Iceberg has no dictionary type, so `string` is what the schema says. It is a small change (about 10 lines plus tests).
   
   One behavior change to flag: a table where every file was written from categorical data currently comes back as `category` in `to_pandas()`. With this fix it would come back as a plain string dtype unless the column is passed in `dictionary_columns`.
   
   The same error came up in #2250, where you asked what outcome users expect; the answer there was the type from the table schema, which is what this fix returns. Three earlier PRs for this (#3444, #3667, #3849) were closed as stale without review. If this direction works for you, I'm happy to open a PR and credit those authors.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [I] ArrowScan to_table fails if the data is mixed between dict-encoded strings and plain strings. [iceberg-python]

Posted by "anna-dv (via GitHub)" <gi...@apache.org>.
anna-dv commented on issue #3260:
URL: https://github.com/apache/iceberg-python/issues/3260#issuecomment-4328520669

   Thanks a lot, @lukaswelsch, for opening the issue!
   
   Here's some additional context which also might be helpful:
   This issue becomes really problematic when working with iceberg tables "living" in AWS Athena/Trino if these tables or their parts often get optimized for performance through an [Athena's OPTIMIZE query](https://docs.aws.amazon.com/athena/latest/ug/optimize-statement.html) (or the one [from Trino](https://trino.io/docs/current/connector/iceberg.html#optimize)).
   
   Because, as it turned out, if optimization procedure mentioned above finds parquet files eligible for optimization, it rewrites them in a way that all `dict-encoded string` columns (if there were any) again become a `plain string` ones. It also seems like there is no way to influence this optimization behavior as of now (or at least, I'm not aware of it). So any table created using `dictionary_encode()` and being continuously optimized for performance in Athena/Trino inevitably enters that mixed `dict-encoded string + plain string` at some point and the incremental scans start failing. 
   
   The interesting thing here is though, before seeing the error message from the issue above (`pyarrow.lib.ArrowTypeError: ...`), one also gets a warning from pyiceberg that [Iceberg does not have a dictionary type. <class 'pyarrow.lib.DictionaryType'> will be inferred as string on read.](https://github.com/apache/iceberg-python/blob/8dee48a8e0218353f706133ed035334869a7ee12/pyiceberg/io/pyarrow.py#L1222), but seems like this inference is never happening and the read still fails afterwards.
   
   Looking forward to the updates on this one and would also be happy to help if needed! ✌️


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org