{"record":{"id":"b40747d970e9f8db","repo":"pola-rs/polars","slug":"native-sink-row-count-num-rows-does-not-match-pa","errorCode":null,"errorMessage":"native sink row count {num_rows} does not match Parquet metadata row count {parquet_metadata.num_rows} for '{file_path}'","messagePattern":"native sink row count (.+?) does not match Parquet metadata row count (.+?) for '(.+?)'","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"critical","filePath":"py-polars/src/polars/io/iceberg/_sink.py","lineNumber":202,"sourceCode":"    import pyarrow as pa\n    import pyarrow.parquet as pq\n    from pyiceberg.io.pyarrow import (\n        MetricModeTypes,\n        _check_pyarrow_schema_compatible,\n        data_file_statistics_from_parquet_metadata,\n    )\n    from pyiceberg.manifest import DataFile, DataFileContent, FileFormat\n\n    schema = table_metadata.schema()\n    file_path, num_rows, num_bytes, parquet_metadata_bytes = sinked_file\n    parquet_metadata = pq.read_metadata(pa.BufferReader(parquet_metadata_bytes))\n\n    if parquet_metadata.num_rows != num_rows:\n        msg = (\n            f\"native sink row count {num_rows} does not match Parquet metadata \"\n            f\"row count {parquet_metadata.num_rows} for '{file_path}'\"\n        )\n        raise ValueError(msg)\n\n    _check_pyarrow_schema_compatible(schema, parquet_metadata.schema.to_arrow_schema())\n    statistics = data_file_statistics_from_parquet_metadata(\n        parquet_metadata=parquet_metadata,\n        stats_columns=statistics_plan,\n        parquet_column_mapping=parquet_column_mapping,\n    )\n    partition = _infer_partition_from_statistics(\n        statistics, table_metadata.spec(), schema\n    )\n    serialized_statistics = statistics.to_serialized_dict()\n    for source_id, metrics_mode in nested_metrics_modes.items():\n        serialized_statistics[\"lower_bounds\"].pop(source_id, None)\n        serialized_statistics[\"upper_bounds\"].pop(source_id, None)\n        if metrics_mode is MetricModeTypes.NONE:\n            serialized_statistics[\"value_counts\"].pop(source_id, None)\n            serialized_statistics[\"null_value_counts\"].pop(source_id, None)\n            serialized_statistics[\"nan_value_counts\"].pop(source_id, None)","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/iceberg/_sink.py#L184-L220","documentation":"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.","triggerScenarios":"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.","commonSituations":"Concurrent writers/compaction touching the same data files, disk-full or interrupted writes, or external jobs rewriting files between sink completion and metadata commit.","solutions":["Re-run the sink to a fresh, exclusive output location ensuring no concurrent writers touch the same files.","Verify the Parquet file is complete and readable (parquet_metadata.row_count vs file integrity) and check for disk-full/interrupted writes.","Upgrade polars/pyiceberg if the mismatch stems from a known sink bug; retry after confirming no other process modified the files."],"exampleFix":"// before\ndf.sink_iceberg(shared_target)  # another job also writes here -> row count mismatch\n\n// after\nimport uuid\ndf.sink_iceberg(f\"{target_base}/{uuid.uuid4()}\")  # unique path per sink run","handlingStrategy":"try-catch","validationCode":"import pyarrow.parquet as pq\nmd = pq.read_metadata(file_path)\nassert md.num_rows == expected_rows, \"Parquet file inconsistent with sink output\"","typeGuard":"def file_is_consistent(file_path: str, expected_rows: int) -> bool:\n    import pyarrow.parquet as pq\n    return pq.read_metadata(file_path).num_rows == expected_rows","tryCatchPattern":"try:\n    df.sink_iceberg(target)\nexcept ValueError as e:\n    if \"row count\" in str(e) and \"does not match\" in str(e):\n        retry_sink_to_fresh_path()\n    else:\n        raise","preventionTips":["Give each sink run a unique output path to avoid concurrent-writer corruption.","Monitor disk space and handle write interruptions so files aren't truncated.","Never let external jobs (compaction, expiry) touch files while a sink is committing."],"tags":["iceberg","parquet","row-count","consistency","sink"],"backgroundTag":"internal-invariant-violation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}