{"record":{"id":"91385e6c1613caf1","repo":"pola-rs/polars","slug":"sink-to-iceberg-table-with-sort-order","errorCode":null,"errorMessage":"sink to Iceberg table with sort order","messagePattern":"sink to Iceberg table with sort order","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/iceberg/_sink.py","lineNumber":505,"sourceCode":"    def _attach_resolved_sink(self, plf: PyLazyFrame) -> PyLazyFrame:\n        from pyiceberg.table import TableProperties\n        from pyiceberg.utils.properties import property_as_bool, property_as_int\n\n        import polars as pl\n\n        table = self.table()\n        table_metadata = table.metadata\n        table_properties = table_metadata.properties\n\n        if self.schema_mode == \"overwrite\" and table.spec().fields:\n            msg = \"schema_mode='overwrite' is not supported for partitioned Iceberg tables\"\n            raise NotImplementedError(msg)\n\n        partition_key_exprs = _partition_key_exprs(table, self.source_schema)\n\n        if table.sort_order().fields:\n            msg = \"sink to Iceberg table with sort order\"\n            raise NotImplementedError(msg)\n\n        if location_provider_impl := table_properties.get(\n            TableProperties.WRITE_PY_LOCATION_PROVIDER_IMPL\n        ):\n            msg = (\n                \"sink to Iceberg table with custom location provider\"\n                f\" '{location_provider_impl}'\"\n            )\n            raise NotImplementedError(msg)\n\n        object_storage_enabled = property_as_bool(\n            table_properties,\n            TableProperties.OBJECT_STORE_ENABLED,\n            TableProperties.OBJECT_STORE_ENABLED_DEFAULT,\n        )\n        object_storage_partitioned_paths = (\n            property_as_bool(\n                table_properties,","sourceCodeStart":487,"sourceCodeEnd":523,"githubUrl":"https://github.com/pola-rs/polars/blob/fc24390824009e8fc5b7870e256273c1b140a019/py-polars/src/polars/io/iceberg/_sink.py#L487-L523","documentation":"The polars Iceberg sink does not support writing to tables that define a sort order. If `table.sort_order().fields` is non-empty, the sink raises NotImplementedError because it would produce data files that do not honor the table's declared sort order, violating table metadata expectations.","triggerScenarios":"`sink_iceberg` targeting an Iceberg table whose metadata sets a sort order (e.g. created by Spark with `SORTED BY` / `write ordered by`).","commonSituations":"Sinking into an existing production table that was created with a sort order; copying a sink pipeline from an unsorted dev table to a sorted prod table; engine migrations where the source table carried over its sort order.","solutions":["Sink into a table without a sort order (create a new table omitting the sort-order clause)","If the sort order is unnecessary, replace/recreate the table with an unsorted spec (unsorted sort order)","Sort the LazyFrame yourself (`.sort(...)`) so data honors the intent, though the sink still requires the table metadata to declare no sort order — combine with recreating the table","Track polars releases; sink support for sorted tables may be added later"],"exampleFix":"// before: table created with a sort order\n# CREATE TABLE ... PARTITIONED BY (day) ... SORTED BY (event_time)\n// after: sink into an unsorted table\n# CREATE TABLE ... PARTITIONED BY (day)  -- no sort order\nlf.sink_iceberg(table)","handlingStrategy":"validation","validationCode":"if table.sort_order().fields:\n    raise ValueError(f\"table '{table.name()}' has a sort order; polars sink_iceberg does not support sorted tables\")","typeGuard":null,"tryCatchPattern":"try:\n    lf.sink_iceberg(table)\nexcept NotImplementedError as e:\n    if 'sort order' in str(e):\n        raise RuntimeError(\"sink into an unsorted table or recreate the table without a sort order\") from e\n    raise","preventionTips":["Check `table.sort_order().fields` before sinking","Create target tables without SORTED BY / sort-order clauses if polars will write them","If ordering matters, `.sort(...)` the LazyFrame yourself and use an unsorted table spec"],"tags":["iceberg","sort-order","unsupported","polars"],"backgroundTag":"unsupported-table-sort-order","analyzedSha":"fc24390824009e8fc5b7870e256273c1b140a019","analyzedAt":"2026-09-02T20:08:03.494Z","contentChangedAt":"2026-09-02T20:08:03.494Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}