Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/scripts/monorepo-split.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ while true; do
fi

find "$build" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} +
cp -a "${local_path}/." "$build/"
cp -aL "${local_path}/." "$build/"
git -C "$build" add --all
changes="$(git -C "$build" status --porcelain)"

Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/job-arrow-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ jobs:
- name: Test arrow crate
working-directory: src/extension/arrow-ext
env: *rust-env
run: cargo test --locked --features arrow-validate
run: cargo test --workspace --locked --features arrow-validate

- name: Lint arrow crate
working-directory: src/extension/arrow-ext
env: *rust-env
run: cargo fmt --check && cargo clippy --locked --all-targets -- -D warnings
run: cargo fmt --all --check && cargo clippy --workspace --locked --all-targets -- -D warnings

- name: Install Composer Dependencies
uses: ./.github/actions/composer-install
Expand All @@ -90,6 +90,12 @@ jobs:
- name: Run Parquet Integration Tests with Arrow
run: just test --testsuite=lib-parquet-integration ${{ (matrix.php == '8.3' && runner.os == 'Linux') && '--coverage-clover=./var/phpunit/coverage/clover/coverage.xml' || '' }}

- name: Run Parquet Adapter Unit Tests with Arrow
run: just test --testsuite=adapter-parquet-unit

- name: Run Parquet Adapter Integration Tests with Arrow
run: just test --testsuite=adapter-parquet-integration

- name: Upload to Codecov
if: ${{ !cancelled() && matrix.php == '8.3' && runner.os == 'Linux' }}
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
Expand Down Expand Up @@ -137,7 +143,7 @@ jobs:
- name: Prepare local package for PIE installation
run: |
# git archive applies export-subst like GitHub release archives, so the build resolves its version from .git_archival.txt
git archive HEAD src/extension/arrow-ext | tar -x -C "$RUNNER_TEMP"
git archive HEAD src/extension/arrow-ext src/extension/parquet-engine | tar -x -C "$RUNNER_TEMP"
cd "$RUNNER_TEMP/src/extension/arrow-ext"
jq '. + {"version": "0.0.9999"}' composer.json > composer.tmp.json
mv composer.tmp.json composer.json
Expand Down
30 changes: 28 additions & 2 deletions .github/workflows/job-flow-php-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ jobs:
env: *rust-env
run: cargo fmt --all --check && cargo clippy --workspace --locked --all-targets -- -D warnings

- name: Check both extensions lock the same flow-parquet-engine dependencies
working-directory: src/extension
run: |
tree() { (cd "$1" && cargo tree --locked -p flow-parquet-engine --prefix none -e normal --target all | sed 's/ (.*)//' | sort -u); }
diff <(tree arrow-ext) <(tree flow-php-ext)

- name: Install Composer Dependencies
uses: ./.github/actions/composer-install
with:
Expand All @@ -81,7 +87,7 @@ jobs:
- name: Run PHPT tests
working-directory: src/extension/flow-php-ext
env: *rust-env
run: make test ARROW_SO=../arrow-ext/ext/modules/arrow.so
run: make test

- name: Install extension and verify
working-directory: src/extension/flow-php-ext
Expand All @@ -105,6 +111,26 @@ jobs:
- name: Run CSV Adapter Integration Tests with Flow PHP Extension
run: just test --testsuite=adapter-csv-integration

- name: Run Parquet Adapter Unit Tests with Flow PHP Extension
run: just test --testsuite=adapter-parquet-unit

- name: Run Parquet Adapter Integration Tests with Flow PHP Extension
run: just test --testsuite=adapter-parquet-integration

- name: Install arrow extension next to flow_php
working-directory: src/extension/arrow-ext
run: |
EXT_DIR=$(php -r 'echo ini_get("extension_dir");')
sudo cp ext/modules/arrow.so "$EXT_DIR/"
echo "extension=arrow.so" | sudo tee -a "$(php -r 'echo php_ini_loaded_file();')"
php -m | grep -x arrow

- name: Run Parquet Adapter Unit Tests with both extensions
run: just test --testsuite=adapter-parquet-unit

- name: Run Parquet Adapter Integration Tests with both extensions
run: just test --testsuite=adapter-parquet-integration

- name: Upload to Codecov
if: ${{ !cancelled() && matrix.php == '8.3' && runner.os == 'Linux' }}
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
Expand Down Expand Up @@ -152,7 +178,7 @@ jobs:
- name: Prepare local package for PIE installation
run: |
# git archive applies export-subst like GitHub release archives, so the build resolves its version from .git_archival.txt
git archive HEAD src/extension/flow-php-ext | tar -x -C "$RUNNER_TEMP"
git archive HEAD src/extension/flow-php-ext src/extension/parquet-engine | tar -x -C "$RUNNER_TEMP"
cd "$RUNNER_TEMP/src/extension/flow-php-ext"
jq '. + {"version": "0.0.9999"}' composer.json > composer.tmp.json
mv composer.tmp.json composer.json
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-arrow-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
paths:
- 'src/extension/arrow-ext/**'
- 'src/extension/parquet-engine/**'
- '.github/workflows/test-arrow-extension.yml'
- '.github/workflows/job-arrow-extension.yml'
- '.github/actions/setup-php-env/**'
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-flow-php-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- 'src/extension/flow-php-ext/**'
# the flow-php-ext build and PHPT tests load arrow-ext
- 'src/extension/arrow-ext/**'
- 'src/extension/parquet-engine/**'
- '.github/workflows/test-flow-php-extension.yml'
- '.github/workflows/job-flow-php-extension.yml'
- '.github/actions/setup-php-env/**'
Expand Down
10 changes: 7 additions & 3 deletions .nix/pkgs/php-arrow-ext/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
}:

let
extensions = ../../../src/extension;
extSrc = builtins.path {
path = ../../../src/extension/arrow-ext;
path = extensions;
name = "arrow-ext-src";
filter = path: type:
let baseName = baseNameOf path;
in !(
let
baseName = baseNameOf path;
folder = builtins.head (lib.splitString "/" (lib.removePrefix "${toString extensions}/" (toString path)));
in (folder == "arrow-ext" || folder == "parquet-engine") && !(
baseName == "target" ||
baseName == "vendor" ||
baseName == "ext" ||
Expand All @@ -29,6 +32,7 @@ let
version = arrow-ext-version;

src = extSrc;
sourceRoot = "arrow-ext-src/arrow-ext";

cargoLock.lockFile = ../../../src/extension/arrow-ext/Cargo.lock;

Expand Down
10 changes: 7 additions & 3 deletions .nix/pkgs/php-flow-php-ext/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
}:

let
extensions = ../../../src/extension;
extSrc = builtins.path {
path = ../../../src/extension/flow-php-ext;
path = extensions;
name = "flow-php-ext-src";
filter = path: type:
let baseName = baseNameOf path;
in !(
let
baseName = baseNameOf path;
folder = builtins.head (lib.splitString "/" (lib.removePrefix "${toString extensions}/" (toString path)));
in (folder == "flow-php-ext" || folder == "parquet-engine") && !(
baseName == "target" ||
baseName == "vendor" ||
baseName == "ext" ||
Expand All @@ -29,6 +32,7 @@ let
version = flow-php-ext-version;

src = extSrc;
sourceRoot = "flow-php-ext-src/flow-php-ext";

cargoLock.lockFile = ../../../src/extension/flow-php-ext/Cargo.lock;

Expand Down
25 changes: 13 additions & 12 deletions documentation/components/adapters/parquet.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,35 @@ For the installation instructions, read the [installation page](/documentation/i

## Engines

Without `engine:`, `AdaptiveParquetEngine` opens the file. The loaded extensions, arrow-ext and
[`flow_php`](/documentation/components/extensions/flow-php-ext.md), decide the lane:
Without `engine:`, the loaded extension decides the engine:

| arrow-ext | `flow_php` | Engine | Batches |
|-----------|------------|---------------------|----------------------------------------------------------------------|
| no | no | `PhpParquetEngine` | PHP columns |
| yes | no | `RustParquetEngine` | PHP columns built from the values of arrow-ext |
| no | yes | `PhpParquetEngine` | native columns built from the values of the PHP engine |
| yes | yes | `RustParquetEngine` | batches of arrow-ext directly into native columns, and back on write |
| [`flow_php`](/documentation/components/extensions/flow-php-ext.md) | Engine | Batches | Counted by the memory limit |
|--------------------------------------------------------------------|------------------------|--------------------------------------------------|-----------------------------|
| yes | `FlowPhpParquetEngine` | native columns, read and written in `flow_php` | yes |
| no | `PhpParquetEngine` | PHP columns | yes |

`RustParquetEngine` needs little-endian byte order. Native columns come from the
[column backend](/documentation/components/core/column-backend.md). An explicit engine is always used.
`PhpParquetEngine` does not use arrow-ext:
arrow-ext does not change the default. `FlowPhpParquetEngine` needs little-endian byte order, and other files are read
by `PhpParquetEngine`. Native columns come from the [column backend](/documentation/components/core/column-backend.md).
An explicit engine is always used:

```php
<?php

use Flow\ETL\Adapter\Parquet\IO\Engine\FlowPhpParquetEngine;
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', engine: new PhpParquetEngine()))
->read(from_parquet(__DIR__ . '/orders.parquet', engine: new FlowPhpParquetEngine()))
->write(to_parquet(__DIR__ . '/copy.parquet', engine: new PhpParquetEngine()))
->run();
```

`engine: new RustParquetEngine()` selects arrow-ext. ETL then builds its columns from PHP values, and a memory limit
does not count the bytes that arrow-ext holds.

Flow `float` columns are written as Parquet `DOUBLE`.

## Schema
Expand Down
17 changes: 2 additions & 15 deletions documentation/components/extensions/arrow-ext.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ The Arrow Rust crates have more I/O features. This extension can add them in the
- Nested types: LIST, STRUCT, MAP (arbitrarily nested)
- Compression codecs: UNCOMPRESSED, SNAPPY, GZIP, ZSTD, LZ4_RAW, BROTLI
- Column projection, offset and limit, from one read of the footer
- Batches that go to other extensions through the [Arrow C Data Interface](https://arrow.apache.org/docs/format/CDataInterface.html)

## Requirements

Expand Down Expand Up @@ -87,7 +86,6 @@ $writer = new Writer();
```php
<?php

use Flow\Arrow\Parquet\RustBatchReader;
use Flow\Parquet\Engine\{RustParquetFileReader, RustParquetFileWriter};
use Flow\Parquet\Engine\Arrow\{OptionsConverter, SchemaConverter};
use Flow\Parquet\Options;
Expand Down Expand Up @@ -122,23 +120,11 @@ $writer = new RustParquetFileWriter( // Flow\Parquet\Parque
$writer->writeBatch([['id' => 1, 'name' => 'a']]); // an array or any Traversable of rows
$writer->writeColumns(['id' => [2, 3], 'name' => ['b', null]]);
$writer->close(); // footer, then the stream closed

$batches = new RustBatchReader($file, ['id', 'name'], 1_000, $plan); // batch size, rows to read
$batches->schema(); // Flow\Arrow\RustArrowSchema
$batches->next(); // Flow\Arrow\RustParquetBatch, null after the last
```

Refusals are the same `Flow\Parquet\Exception\*` that the PHP engine throws. An exception from the stream comes out
unchanged.

### Arrow C Data Interface

`RustParquetBatch` (`RustBatchReader::next()`) carries one struct array: `arrowSchemaAddress()` (`FFI_ArrowSchema*`) and
`arrowArrayAddress()` (`FFI_ArrowArray*`). The consumer is another extension. It reads the schema by reference and
moves the array out. `RustParquetFileWriter::writeArrowBatch()` takes the same pair from a final internal class of
any extension that has both methods. The children of the array match the writer columns by name. Userland code cannot
make either class: the classes are final, and their constructors throw.

### Native memory

```php ignore
Expand All @@ -147,7 +133,8 @@ make either class: the classes are final, and their constructors throw.
\Flow\Arrow\allocated_bytes(); // int, the bytes the extension holds outside PHP's memory manager in this process
```

`flow_php` adds these bytes to `RustBackend::allocatedBytes()`, so a memory limit counts them.
A memory limit of Flow ETL does not count these bytes. Flow ETL reads and writes Parquet with the engine of
[`flow_php`](/documentation/components/extensions/flow-php-ext.md), whose bytes the limit counts.

## Development

Expand Down
41 changes: 23 additions & 18 deletions documentation/components/extensions/flow-php-ext.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ package: flow-php/flow-php-ext
[TOC]

This extension is the native column backend of Flow. It uses [ext-php-rs](https://github.com/extphprs/ext-php-rs)
to read and write CSV and JSON natively in Rust. It reads and writes Parquet through the
[arrow extension](/documentation/components/extensions/arrow-ext.md). The pure-PHP implementations in `flow-php/etl`
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.

Expand Down Expand Up @@ -121,17 +120,16 @@ natively with the flags `JSON_THROW_ON_ERROR`, `JSON_UNESCAPED_SLASHES`, `JSON_U

## Parquet

When this extension and the [arrow extension](/documentation/components/extensions/arrow-ext.md) are both loaded,
`from_parquet()` and `to_parquet()` without an `engine:` read and write native columns. arrow-ext reads and writes the
file. Its batches go into `RustColumn`s (and back) through the Arrow C Data Interface. There is no copy and no PHP
value for each cell.
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, arrow | native columns through the Arrow C Data Interface |
| arrow | `RustParquetEngine` (arrow-ext), PHP values |
| flow_php | `PhpParquetEngine`, and `RustBackend` adopts the values into native columns |
| neither | `PhpParquetEngine` |
| 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
<?php
Expand All @@ -154,11 +152,18 @@ df()
```

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. `RustBackend::allocatedBytes()` counts the imported batches while they live. It does not
count the read buffers of arrow-ext: the projected column chunks of a row group and the decode buffers. A
`MemoryBudget` therefore does not see them. Read and write refusals are the `Flow\Parquet\Exception\*` of arrow-ext,
as with `RustParquetEngine`.
type before the first batch. Read and write refusals are the `Flow\Parquet\Exception\*` that
[arrow-ext](/documentation/components/extensions/arrow-ext.md) 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.

`RustParquetOpenSource` reads through `Flow\Arrow\Parquet\RustBatchReader` of arrow-ext. `RustParquetFileWriter` writes
through `Flow\Parquet\Engine\RustParquetFileWriter` 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:

```bash
MALLOC_MMAP_THRESHOLD_=262144 php your_script.php
```
Loading
Loading