pola-rs/polars · critical · ValueError

native sink row count

Error message

native sink row count {num_rows} does not match Parquet metadata row count {parquet_metadata.num_rows} for '{file_path}'

What it means

When constructing the Iceberg DataFile from sink metadata, _data_file_from_sink_metadata cross-checks the row count the native sink reported against the row count recorded in the written Parquet file's metadata. A mismatch means the output file is inconsistent with the sink's accounting, so polars raises ValueError to prevent committing corrupt metadata.

Solutions

  1. Re-run the sink to a fresh, exclusive output location ensuring no concurrent writers touch the same files.
  2. Verify the Parquet file is complete and readable (parquet_metadata.row_count vs file integrity) and check for disk-full/interrupted writes.
  3. Upgrade polars/pyiceberg if the mismatch stems from a known sink bug; retry after confirming no other process modified the files.

Example fix

// before
df.sink_iceberg(shared_target)  # another job also writes here -> row count mismatch

// after
import uuid
df.sink_iceberg(f"{target_base}/{uuid.uuid4()}")  # unique path per sink run
Defensive patterns

Strategy: try-catch

Validate before calling

import pyarrow.parquet as pq
md = pq.read_metadata(file_path)
assert md.num_rows == expected_rows, "Parquet file inconsistent with sink output"

Type guard

def file_is_consistent(file_path: str, expected_rows: int) -> bool:
    import pyarrow.parquet as pq
    return pq.read_metadata(file_path).num_rows == expected_rows

Try / catch

try:
    df.sink_iceberg(target)
except ValueError as e:
    if "row count" in str(e) and "does not match" in str(e):
        retry_sink_to_fresh_path()
    else:
        raise

Prevention

When it happens

Trigger: During sink_iceberg finalization, num_rows (from the sink) differs from parquet_metadata.num_rows for the produced file — typically indicating a partially written, concurrently modified, or truncated Parquet file.

Common situations: Concurrent writers/compaction touching the same data files, disk-full or interrupted writes, or external jobs rewriting files between sink completion and metadata commit.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/b40747d970e9f8db. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/iceberg/_sink.py:202

    import pyarrow as pa
    import pyarrow.parquet as pq
    from pyiceberg.io.pyarrow import (
        MetricModeTypes,
        _check_pyarrow_schema_compatible,
        data_file_statistics_from_parquet_metadata,
    )
    from pyiceberg.manifest import DataFile, DataFileContent, FileFormat

    schema = table_metadata.schema()
    file_path, num_rows, num_bytes, parquet_metadata_bytes = sinked_file
    parquet_metadata = pq.read_metadata(pa.BufferReader(parquet_metadata_bytes))

    if parquet_metadata.num_rows != num_rows:
        msg = (
            f"native sink row count {num_rows} does not match Parquet metadata "
            f"row count {parquet_metadata.num_rows} for '{file_path}'"
        )
        raise ValueError(msg)

    _check_pyarrow_schema_compatible(schema, parquet_metadata.schema.to_arrow_schema())
    statistics = data_file_statistics_from_parquet_metadata(
        parquet_metadata=parquet_metadata,
        stats_columns=statistics_plan,
        parquet_column_mapping=parquet_column_mapping,
    )
    partition = _infer_partition_from_statistics(
        statistics, table_metadata.spec(), schema
    )
    serialized_statistics = statistics.to_serialized_dict()
    for source_id, metrics_mode in nested_metrics_modes.items():
        serialized_statistics["lower_bounds"].pop(source_id, None)
        serialized_statistics["upper_bounds"].pop(source_id, None)
        if metrics_mode is MetricModeTypes.NONE:
            serialized_statistics["value_counts"].pop(source_id, None)
            serialized_statistics["null_value_counts"].pop(source_id, None)
            serialized_statistics["nan_value_counts"].pop(source_id, None)

View on GitHub (pinned to fe841f959e)