Skip to content
Search

Flow PHP Extension

This extension is the native column backend of Flow. It uses ext-php-rs to read and write CSV, JSON and Parquet natively in Rust. The pure-PHP implementations in flow-php/etl are the canonical reference for the behaviour, and they work without the extension. The extension only makes Flow faster.

Loading the Extension

In php.ini

extension = flow_php

During Development

php -d extension=./ext/modules/flow_php.so your_script.php

Usage

The CSV reader uses the extension implicitly:

<?php

use function Flow\ETL\Adapter\CSV\from_csv;
use function Flow\ETL\DSL\{df, to_stream};

df()
    ->read(from_csv(__DIR__ . '/input.csv')) // read natively when the extension is loaded
    ->write(to_stream(__DIR__ . '/output.txt'))
    ->run();

Flow\ETL\Values\Column\AdaptiveBackend is the default of config_builder()->backend(). When the extension is loaded, it picks the RustBackend of the extension. Each batch column is then a Flow\ETL\Values\Column\RustColumn over an Apache Arrow array. To choose a backend and to see what it builds, read Column Backend.

<?php

use Flow\ETL\Values\Column\RustBackend;

use function Flow\ETL\DSL\int_schema;

$builder = (new RustBackend())->builder(int_schema('id'));
$builder->appendMany([1, '2', 3.0]);

$column = $builder->finish();                        // Flow\ETL\Values\Column\RustColumn
$column->values();                                   // [1, 2, 3]
(new RustBackend())->allocatedBytes();               // bytes held outside PHP's memory manager

Each contract with a Rust and a PHP implementation follows one pattern:

  • The extension registers the interface.
  • The Rust* class implements it.
  • Php* is the PHP implementation.
  • The class that builds the contract picks one of them with extension_loaded().
contract Rust PHP picked by
Flow\ETL\Values\Column\Backend RustBackend PhpBackend AdaptiveBackend
Flow\ETL\Adapter\CSV\IO\Extractor\CSVOpenSource RustCSVOpenSource PhpCSVOpenSource CSVSourceOpener
Flow\ETL\Adapter\CSV\IO\Loader\CSVEncoder RustCSVEncoder PhpCSVEncoder CSVLoader::writer()
Flow\ETL\Adapter\JSON\IO\Extractor\JsonOpenSource RustJsonOpenSource PhpJsonOpenSource JsonFileBatches::open()
Flow\ETL\Adapter\JSON\IO\Loader\JsonEncoder RustJsonEncoder PhpJsonEncoder JsonLoader::writer()
Flow\ETL\Adapter\Parquet\IO\Extractor\ParquetOpenSource RustParquetOpenSource PhpParquetOpenSource ParquetSourceFile::open()
Flow\ETL\IO\Loader\File\FileWriter RustParquetFileWriter ParquetFileWriter ParquetFileWriterFactory::writer()

The extension also registers Flow\ETL\Values\Column\{Column, ColumnBuilder, GroupedTotals, GroupedCounts, GroupedMultisetHashes} (RustColumn, RustColumnBuilder, RustGroupedTotals, RustGroupedCounts, RustGroupedMultisetHashes). It also registers Flow\ETL\Values\Grouping\GroupTable (RustGroupTable). RustBackend returns the last four. Its sources return the iterator Flow\ETL\Values\RustIterator.

CSV and JSON writers

When the extension is loaded, to_csv(), to_json() and to_json_lines() render a batch from the arrow buffers, without a PHP value for each cell. The bytes are the same as the bytes of the PHP writers.

<?php

use function Flow\ETL\Adapter\CSV\{from_csv, to_csv};
use function Flow\ETL\Adapter\JSON\to_json_lines;
use function Flow\ETL\DSL\df;

df()
    ->read(from_csv(__DIR__ . '/input.csv'))
    ->write(to_csv(__DIR__ . '/output.csv')->withDateTimeFormat('Y-m-d H:i:s.u P')) // rendered natively
    ->write(to_json_lines(__DIR__ . '/output.jsonl')->withDateTimeFormat('D, d M Y'))  // `D` and `M`: this column renders in PHP
    ->run();

The native renderer accepts a datetime or date format that uses only the letters Y y m n d j H G i s u v e T P p O Z U c and \ escapes. If a format has a different letter, that column renders in PHP. These columns also render in PHP:

  • xml, xml_element, html and html_element columns
  • a json column written to JSON
  • a list / map / structure that holds one of those

Only those columns render in PHP. The rest of the batch stays native. to_json() and to_json_lines() render natively with the flags JSON_THROW_ON_ERROR, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE and JSON_PRESERVE_ZERO_FRACTION. With a different flag (JSON_PRETTY_PRINT, ...), they write through PHP.

RustCSVEncoder / RustJsonEncoder hold the PHP encoder that renders the PHP columns.

Parquet

This extension has its own Parquet engine, Flow\ETL\Adapter\Parquet\IO\Engine\FlowPhpParquetEngine. Without an engine:, from_parquet() and to_parquet() use it, and the batches are native columns. The memory limit of a run counts the bytes of the engine, because one allocator holds them. No other extension is needed.

loaded from_parquet() / to_parquet()
flow_php FlowPhpParquetEngine, native columns
flow_php, arrow FlowPhpParquetEngine, native columns
arrow PhpParquetEngine, PHP values
neither PhpParquetEngine, PHP values
<?php

use Flow\Parquet\Engine\PhpParquetEngine;

use function Flow\ETL\Adapter\Parquet\{from_parquet, to_parquet};
use function Flow\ETL\DSL\df;

df()
    ->read(from_parquet(__DIR__ . '/orders.parquet'))
    ->offset(50_000) // only the row groups from row 50 000 are decoded
    ->write(to_parquet(__DIR__ . '/copy.parquet'))
    ->run();

df()
    ->read(from_parquet(__DIR__ . '/orders.parquet', engine: new PhpParquetEngine())) // PhpParquetEngine opts out
    ->write(to_parquet(__DIR__ . '/copy.parquet', engine: new PhpParquetEngine()))
    ->run();

A schema type can store a different arrow type than the type that the file column reads as. The read refuses such a type before the first batch. Read and write refusals are the Flow\Parquet\Exception\* that arrow-ext also throws: both extensions build the same engine code. With engine: new RustParquetEngine(), arrow-ext reads the file, the columns are built from PHP values, and a memory limit does not count the bytes of arrow-ext.

Memory returned to the system

When the extension loads on Linux with glibc, it fixes the mmap threshold of the process at 128 KiB. glibc otherwise raises the threshold as large blocks are freed, and the freed buffers stay on the heap. With a fixed threshold, a sort holds what its memory limit counts. The threshold applies to the whole process. To keep your own value, set it in the environment:

MALLOC_MMAP_THRESHOLD_=262144 php your_script.php

Found a typo or an outdated section? Edit this page on GitHub


Contributors

Built in the open.

Join us on GitHub
scroll back to top