# Iceberg sink

> Floe writes Iceberg tables for sink.accepted.format: iceberg through an Iceberg REST catalog (e.g. Polaris).

Source: https://openlakeforge.dev/floe/sinks/iceberg/

Floe writes Iceberg tables for `sink.accepted.format: iceberg` through an
Iceberg REST catalog (e.g. Polaris).

**A catalog is required (0.8).** The sink needs a `sink.accepted.iceberg` block
that resolves to a `catalogs.definitions` entry, through `iceberg.catalog` or
`catalogs.default`. `floe validate` rejects a catalog-less Iceberg sink with
`sink.accepted.format=iceberg requires sink.accepted.iceberg with a catalog in 0.8`.
Floe 0.7's catalog-less filesystem tables are not supported: a path-only table
has no safe commit protocol for concurrent runs.

## Example config

```yaml
storages:
  default: "s3_out"
  definitions:
    - name: "s3_out"
      type: "s3"
      bucket: "my-bucket"
      region: "us-east-1"

catalogs:
  default: "polaris"
  definitions:
    - name: "polaris"
      type: "rest"
      uri: "https://polaris.example.com/api/catalog"
      warehouse: "lakehouse"

entities:
  - name: "customer"
    source:
      format: "csv"
      path: "/data/in/customers.csv"
    sink:
      accepted:
        format: "iceberg"
        path: "customer_iceberg"
        iceberg: {} # uses catalogs.default
    policy:
      severity: "warn"
    schema:
      columns:
        - name: "customer_id"
          type: "string"
```

## Table location

The table is created in the catalog on its first write (and its namespace, if missing). Its
location is, in order:

- `sink.accepted.iceberg.location`, when set;
- `<warehouse_prefix>/<namespace>/<table>` on the catalog's `warehouse_storage`, when the catalog
  sets `warehouse_storage` or `warehouse_prefix`;
- otherwise `sink.accepted.path` on `sink.accepted.storage`.

The namespace defaults to `entity.domain`, then `default`; the table name to `entity.name`.
The storage must be S3, GCS or ADLS: `floe validate` rejects a local accepted storage unless the
catalog's `warehouse_storage` points at S3, GCS or ADLS. On ADLS, PyIceberg writes through `adlfs`
(the `adls` extra) at `abfss://<container>@<account>.dfs.core.windows.net/...`; see
[ADLS](https://openlakeforge.dev/floe/storages/adls/). On GCS, `overwrite` and `append` work; merges
and compaction fail the run (see [GCS](https://openlakeforge.dev/floe/storages/gcs/)).

## AWS credentials for S3 writes

Floe passes the S3 storage's `endpoint`, `region` and `path_style_access` to PyIceberg and
DuckDB. Credentials come from the standard AWS credential chain (environment variables, web
identity / IRSA, container credentials, instance metadata); the catalog does not vend them.

## AWS Glue catalog

A `type: glue` catalog writes the table through the AWS Glue Data Catalog, as Floe 0.7 did:

```yaml
catalogs:
  default: "glue"
  definitions:
    - name: "glue"
      type: "glue"
      region: "eu-west-1"
      database: "sales_silver"           # the Glue database that holds the tables
      warehouse_storage: "lakehouse_silver"
      warehouse_prefix: "warehouse/iceberg"
      create_database_if_missing: false  # default true
```

The table is `<database>.<table>`; `sink.accepted.iceberg.namespace` (or `entity.domain`) only
names its location, `<warehouse_prefix>/<namespace>/<table>`. To write one database per domain from
a single catalog, put `{domain}` in `database`, e.g. `database: dev_{domain}_silver`: per
entity it becomes `sink.accepted.iceberg.namespace`, else `entity.domain`, normalized like the
namespace (lowercase, `[a-z0-9_-]`), so the `sales` and `finance` domains write to `dev_sales_silver`
and `dev_finance_silver`. An entity with neither a namespace nor a domain is a `floe validate`
error. `{{VAR}}` placeholders are not applied to catalog definitions: to vary the prefix per stage,
define the catalog in each stage's profile ([profile catalogs](https://openlakeforge.dev/floe/guides/profiles/#environment-catalog-overrides)
replace the config's). Glue credentials come from the AWS credential chain, as for S3. Glue supports `overwrite` and `append`: `floe validate` rejects a
merge write mode, and `floe maintain --compact` needs a REST catalog. `allow_takeover` is
accepted and ignored: Floe writes any Iceberg table in the database (D42).

## REST catalog credentials

REST catalog definitions accept bearer tokens and OAuth2 client credentials:

```yaml
catalogs:
  default: "polaris"
  definitions:
    - name: "polaris"
      type: "rest"
      uri: "http://polaris:8181/api/catalog"
      credential: "client_credentials:${POLARIS_CLIENT_ID}:${POLARIS_CLIENT_SECRET}"
      warehouse: "lakehouse"
      oauth2_server_uri: "http://polaris:8181/api/catalog/v1/oauth/tokens"
      scope: "PRINCIPAL_ROLE:ALL"
```

**Prefer `${ENV_VAR}` placeholders in `credential`** over literal client IDs/secrets
in YAML. They are resolved from the OS environment at run time, including during
`floe run --manifest`, so runner environments can inject REST catalog secrets
without baking raw credentials into config or the manifest. The resolved credential
is held as a secret and is never written to reports or logs.

## Partition spec config

Iceberg accepted sinks can declare a partition spec in config, and Floe executes
it at write time for Iceberg table creation/data file layout:

```yaml
sink:
  accepted:
    format: "iceberg"
    path: "customer_iceberg"
    iceberg: {}
    partition_spec:
      - column: "event_date"
        transform: "day"
      - column: "country"
        transform: "identity"
      - column: "customer_id"
        transform: "bucket"
        num_buckets: 16
```

Current scope:
- partition columns must exist in `schema.columns`
- supported transforms: `identity`, `year`, `month`, `day`, `hour`, `bucket` (with `num_buckets`, D12)
- partition spec is applied to Iceberg table metadata and partitioned file writes
- writes validate partition-spec compatibility with existing tables (no implicit partition-spec evolution)

## Supported scope (current)

- Accepted sink only (`sink.accepted.format: iceberg`), through a REST or AWS Glue catalog
- S3, GCS or ADLS storage for the table data (S3 with a REST catalog is the tested path; ADLS and GCS are `untested` end to end, see the [support matrix](https://openlakeforge.dev/floe/reference/support-matrix/#tested-combinations))
- `sink.write_mode`: `overwrite`, `append`, `merge_scd1` and `merge_scd2`. See [write modes](https://openlakeforge.dev/floe/concepts/write-modes/).
- Each entity's write is one catalog commit. `overwrite` replaces every existing data file in
  that snapshot; `append` adds files; `merge_scd1` runs a DuckDB `MERGE INTO` (merge-on-read).
- `append` snapshots carry `floe.run_id` and `floe.input_hash`, so a retry finds a batch a dead
  run already committed and skips it (`skip_reason: already_committed`).
- Unique checks on `append` read the table's existing keys, and a commit conflict re-checks the
  keys of concurrently added files.
- `sink.accepted.options.compression` and `row_group_size` shape the Parquet data files of
  `overwrite` and `append` writes (PyIceberg's `write.parquet.compression-codec` and
  `write.parquet.row-group-limit`, applied to that write only; the table's properties are not
  changed). Unset, the table's properties and PyIceberg's defaults apply. `merge_scd1` and
  `merge_scd2` write through DuckDB and ignore both options.
- `schema.schema_evolution.mode: add_columns` adds new columns as nullable (D13)
- Unsigned integer columns are widened (`uint8`/`uint16` → `int`, `uint32` → `long`); Iceberg
  has no unsigned types
- Run report `accepted_output` includes:
  - `table_root_uri`, `write_mode`, `files_written`, `parts_written`
  - `table_version` (Iceberg metadata version, when available)
  - `snapshot_id` (when a snapshot exists)
  - `iceberg_catalog_name`, `iceberg_namespace`, `iceberg_table`
  - file sizing metrics (`total_bytes_written`, `avg_file_size_mb`, `small_files_count`)
- Iceberg file sizing metrics are based on Iceberg data files only (metadata/manifests excluded)

## Limitations / non-goals (current)

- `strategy: drop` in `pii` is rejected: the table enforces the full declared schema
- Every commit rewrites the table metadata, which lists every retained snapshot: an `overwrite` of a table with 100 snapshots took about 230 ms of commit and write time against about 50 ms with 2 on a local catalog. Run `floe maintain` to expire snapshots on tables written often
- Snapshot expiry and orphan file removal with `floe maintain` (see [CLI usage](https://openlakeforge.dev/floe/reference/cli/)); compaction is experimental (`--compact --experimental`)

## Guarantees and evidence

What is atomic, what a retry does and which backends are tested: [support matrix](https://openlakeforge.dev/floe/reference/support-matrix/#transactional-guarantees).

## Integration tests

The Polaris and S3 tests are skipped by default. Start a local Polaris + S3 harness, then run
`FLOE_POLARIS_URI=http://localhost:8181/api/catalog uv run pytest -m polaris` (and set
`FLOE_S3_ENDPOINT` for the `-m s3` tests).

## Empty accepted dataset behavior

- The table is created on the first write even when no row is accepted.
- An `append` of no rows commits nothing.
- An `overwrite` of no rows removes every existing data file in one snapshot; on an empty table
  it commits nothing.

---

<small>Synced from floe @ `v1.1.0` (`313b534`), [docs/sinks/iceberg.md](https://github.com/OpenLakeForge/floe/blob/313b53441b3b088290403ed1adb2f3894833b6c5/docs/sinks/iceberg.md).</small>
