# Parquet sink

> Floe can write accepted output as Parquet files on local, S3, ADLS or GCS storage.

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

Floe can write accepted output as Parquet files on local, S3, ADLS or GCS storage.

Example:
```yaml
entities:
  - name: customers
    source:
      format: csv
      path: /data/in/customers.csv
    sink:
      accepted:
        format: parquet
        path: /data/out/customers
```

Semantics:
- `sink.accepted.format: parquet` writes one or more part files under `sink.accepted.path`.
- Write mode comes from `sink.write_mode`:
  - `overwrite`: write the new parts as `part-00000.parquet`, `part-00001.parquet`, ... and remove the old `part-*.parquet` files once every new part was written
  - `append`: add new `part-<uuid>.parquet` files alongside existing ones.
  - `merge_scd1` / `merge_scd2`: not supported for Parquet; use the [Iceberg](https://openlakeforge.dev/floe/sinks/iceberg/) sink.

## File splitting

Large accepted datasets are split into multiple part files. The split threshold is controlled by
`sink.accepted.options.max_size_per_file` (default 256 MB), measured on the in-memory size of
the rows. Accepted rows are written as the input is read, so memory holds about one part plus one
chunk of input, not the whole entity. Every part is staged as a `_staging-<id>-part-*` file and
copied to its `part-*` name only once the whole input was read and written, so a write that fails
(or a killed run) before that final copy leaves no visible part of that run; orphaned `_staging-*` files are safe to delete.
A crash during the final copy is covered under [Parquet is not transactional](#parquet-is-not-transactional).

```yaml
sink:
  accepted:
    format: parquet
    path: /data/out/orders
    options:
      compression: zstd
      row_group_size: 50000
      max_size_per_file: 268435456   # 256 MB
```

Available options:
- `compression`: `snappy`, `gzip`, `zstd` (default, Polars' default), `uncompressed`
- `row_group_size`: rows per row group (positive integer)
- `max_size_per_file`: part size threshold in bytes (default: 268435456 / 256 MB)

## Peak memory on large inputs

Floe holds no rows beyond the current input chunk and part, for the accepted and the Parquet
rejected sink alike. The one exception is an entity with `primary_key` or `unique_keys`: it keeps
every distinct key it has seen in memory for the whole run, to find duplicates across chunks and
files, and the settings below do not reclaim that memory.

Even without unique keys, the process's peak RSS still grows with the input size. Two effects of
Polars cause most of it, neither a leak:

- Polars reads a CSV input ahead of the rows Floe has consumed: up to two 32 MiB chunks per worker
  thread (256 MiB on 4 cores, 1 GiB on 16), so a file smaller than that is read in whole. Polars 2.0
  memory-mapped the file instead; that file-backed memory also counted in RSS.
- Polars' allocator (jemalloc) keeps freed buffers resident for a while, one cache per worker
  thread, so the more cores, the larger the retained memory.

These environment variables lower the peak at some cost in speed:

| Variable | Effect | Cost |
|---|---|---|
| `_RJEM_MALLOC_CONF=dirty_decay_ms:0,muzzy_decay_ms:0` | jemalloc returns freed memory at once | a slower run |
| `POLARS_MAX_THREADS=<n>` | fewer worker threads, so fewer per-thread caches and less read-ahead | slower reads and checks on big inputs |
| `POLARS_CSV_CHUNK_SIZE=<bytes>` | smaller CSV read-ahead chunks (default 32 MiB) | slower CSV reads |
| `POLARS_CSV_CHUNK_PREFETCH_LIMIT=<n>` | at most `n` CSV chunks read ahead (default twice the worker threads) | slower CSV reads |

`_RJEM_MALLOC_CONF` and `POLARS_MAX_THREADS` are read once, when Polars is first imported or used.
Set them in the environment that starts the process (the container, the Kubernetes Job, the shell);
setting them from Python after `import polars` or `import floe` has no effect, and so does setting
them in a Dagster or Airflow worker that imported Polars already. The two `POLARS_CSV_*` variables
are read each time a CSV scan starts; they are Polars internals rather than a documented API, so
check them again after a Polars upgrade.

## Limitations

### Parquet is not transactional

Parquet has no built-in transaction or atomic-commit semantics. Floe does not implement a
write-ahead log or two-phase commit for Parquet output. This has two practical consequences:

**Failures while writing**

A failure while writing (process crash, out-of-disk, network error on remote storage) can leave an
incomplete `_staging-<id>-part-*` file. Readers of `part-*.parquet` never see it, and the next write
ignores it, so it is safe to delete. Floe does not roll anything back: only the final copy and delete
steps described below can leave visible parts that mix old and new. On local storage that copy replaces
the `part-*` file in place, so a crash during it can also leave one truncated, unreadable part under its final
name; a re-run replaces it.

**Overwrite is not atomic**

`overwrite` first writes every new part under a `_staging-<id>-part-NNNNN.parquet` name, which
readers of `part-*.parquet` do not see. Only when all of them were written does it copy them to
`part-NNNNN.parquet`, delete the old parts that were not replaced, then delete the staged files.
A write failure leaves the previous parts untouched (the staged files are removed). A crash or a
storage error during the final copy and delete steps can leave a mix of new and old parts (rows
duplicated or missing) and orphaned `_staging-*` files, which are safe to delete. The run report
and incremental state are not committed in that case, so Floe re-processes the same inputs on
the next run and a re-run restores a clean directory.

A retry of an `append` without a primary or unique key can duplicate rows (at-least-once); see the [support matrix](https://openlakeforge.dev/floe/reference/support-matrix/#transactional-guarantees).

**If you need transactional accepted writes, use the [Iceberg](https://openlakeforge.dev/floe/sinks/iceberg/) sink instead.**
Each entity's Iceberg write is one atomic catalog commit.

---

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