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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Flow is a PHP-based, strongly typed data processing framework with a low memory
[![Latest Unstable Version](https://poser.pugx.org/flow-php/flow/v/unstable)](https://packagist.org/packages/flow-php/flow)
[![License](https://poser.pugx.org/flow-php/flow/license)](https://packagist.org/packages/flow-php/flow)
[![Test Suite](https://github.com/flow-php/flow/actions/workflows/test-suite.yml/badge.svg?branch=1.x)](https://github.com/flow-php/flow/actions/workflows/test-suite.yml)
[![libs.tech recommends](https://libs.tech/project/370006659/badge.svg)](https://libs.tech/project/370006659/flow)
[![OpenSSF Best Practices](https://www.bestpractices.dev/projects/10009/badge)](https://www.bestpractices.dev/projects/10009)

- 📈 [Project Roadmap](https://github.com/orgs/flow-php/projects/1)
Expand Down
13 changes: 10 additions & 3 deletions documentation/components/core/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,21 @@ rows.
first input batch, a join when it begins. Each live operation gets its own limit. The limit and the process cap
compare live memory, `memory_get_usage()` plus native backend bytes.

- The default is `FLOW_MAX_MEMORY`, else 70% of the `memory_limit` of PHP, or 1 GiB for a `memory_limit` of `-1`.
- The default is `FLOW_MAX_MEMORY`, else 70% of the `memory_limit` of PHP, or 4 GiB for a `memory_limit` of `-1`.
- With a launcher of `flow-php/parallel`, each worker process takes the limit divided by the launcher's `slots`, and
the parent keeps the full limit. `FLOW_MAX_MEMORY=4G` and 8 slots give each worker 512 MiB for each operation.
- After every spill and after its last run, a sort returns freed memory to the system through the backend. With
`flow_php` on Linux (glibc) that is `malloc_trim(0)`. On other systems nothing happens. On macOS the system
allocator keeps freed large blocks. If you start PHP with `MallocLargeCache=0` in the environment, the process
returns them. In the 10gb sort this lowered the memory of the process tree by about a third and made the sort 14%
to 28% slower.
- With a finite `memory_limit`, the process cap is 70% of it, or `FLOW_MAX_MEMORY`. A `FLOW_MAX_MEMORY` above
`memory_limit` throws when the config is built.
- A process over its cap also makes the operation spill once it added `min(128 MiB, cap / 16)`, and each sorted run
then takes up to that floor instead of one batch. Close to `memory_limit`, that floor can still pass it.
- A sort that would take the process past the cap spills in smaller runs.
- With `memory_limit = -1` there is no process cap, so k operations running at once can use k x the limit. Set a finite
`memory_limit`, or a lower `memoryLimit()`, to bound it.
- With `memory_limit = -1` there is no process cap, so k operations running at once in one process can use k x the
limit. Set a finite `memory_limit`, or a lower `memoryLimit()`, to bound it.

## Overriding the algorithm for one sort

Expand Down
10 changes: 10 additions & 0 deletions documentation/components/extensions/arrow-ext.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ moves the array out. `RustParquetFileWriter::writeArrowBatch()` takes the same p
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
<?php

\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.

## Development

### Build Commands
Expand Down
7 changes: 7 additions & 0 deletions documentation/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ postgresql://user:pass@localhost/app?options=-c%20statement_timeout%3D30s%20-c%2
Or `withSessionSettings(['statement_timeout' => '30s', 'lock_timeout' => '10s'])`, or `session_settings` in the
Symfony bundle.

### 21) `flow-php/etl` - the default memory limit is 4 GiB, `ExternalSort::reduce()` takes streams

| Before | After |
|-----------------------------------------------------------------------|--------------------------------------------------------------------------------|
| `MemoryLimit::default()` with `memory_limit = -1`: 1 GiB | 4 GiB (`MemoryLimit::WITHOUT_MEMORY_LIMIT_GB = 4`) |
| `ExternalSort::reduce(list<BucketRun>, KWayMerge, int, ?int)` | `reduce(list<Generator<Rows>>, KWayMerge, int, ?int)`, removes no run |

---

## Upgrading from 0.46.x to 0.47.x
Expand Down
12 changes: 11 additions & 1 deletion src/core/etl/src/Flow/ETL/Foundation/Describe/Rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@

final readonly class Rebuild
{
/**
* @param int<1, max> $memoryShare how many processes share one memory limit
*/
public function __construct(
private ProgressStages $progress = new VoidProgress(),
) {}
public int $memoryShare = 1,
) {
// @mago-ignore analysis:impossible-condition,redundant-comparison
if ($memoryShare < 1) {
// @mago-ignore analysis:no-value
throw InvalidArgumentException::because('Rebuild memory share must be at least 1, given: %d', $memoryShare);
}
}

/**
* Every entry of $normalized rebuilt with of(), keys kept.
Expand Down
28 changes: 23 additions & 5 deletions src/core/etl/src/Flow/ETL/Functions/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Flow\Types\Type;
use Flow\Types\Type\Logical\DateTimeType;
use Flow\Types\Type\Logical\DateType;
use Flow\Types\Type\Logical\UuidType;
use Flow\Types\Type\Logical\XMLType;
use Flow\Types\Type\Native\StringType;
use Flow\Types\Type\TypeFactory;
Expand Down Expand Up @@ -120,18 +121,33 @@ public function eval(Rows $rows, Backend $backend): Column
if (type_bare($this->type) instanceof StringType) {
$column = $this->value->eval($rows, $backend);
$type = type_bare($column->type());
// an OptionalType column keeps the per-row lane
$text = $column->type() instanceof StringType;

if ($type instanceof DateTimeType || $type instanceof DateType) {
/** @var list<?int> $physicals */
$physicals = $column->physicals();

if ($text || $type instanceof UuidType || $type instanceof DateTimeType || $type instanceof DateType) {
if ($column->nullCount() > 0 && !type_is_nullable($this->type)) {
throw EvaluationException::at(
(int) array_search(null, $physicals, true),
(int) array_search(null, $column->physicals(), true),
new InvalidArgumentException('Cast function requires non-null value'),
);
}

if ($text) {
return $backend->adopt(Definitions::of('value', type_string(), true), $column);
}

if ($type instanceof UuidType) {
/** @var list<?string> $bytes */
$bytes = $column->physicals();
$builder = $backend->builder(Definitions::of('value', type_string(), true));
$builder->appendPhysicals((new TextValues())->uuids($bytes), $column->nullCount());

return $builder->finish();
}

/** @var list<?int> $physicals */
$physicals = $column->physicals();

return (new ResultColumn($backend))->of($this, (new TextValues())->dateTimes(
$type,
$physicals,
Expand All @@ -142,6 +158,8 @@ public function eval(Rows $rows, Backend $backend): Column
if ($type instanceof XMLType) {
$values = $column->physicals();
$documents = new XmlDocumentPhysical();
} else {
$values = $column->values();
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/core/etl/src/Flow/ETL/Functions/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
use Flow\ETL\Values\Column\Column;
use Flow\ETL\Values\Rows;
use Flow\Types\Type;
use Flow\Types\Type\Native\StringType;
use Flow\Types\Type\Nullability;
use SensitiveParameter;

use function Flow\Types\DSL\type_array;
use function Flow\Types\DSL\type_bare;
use function Flow\Types\DSL\type_boolean;
use function Flow\Types\DSL\type_string;
use function in_array;
Expand Down Expand Up @@ -79,7 +81,13 @@ public function returns(): Type

public function eval(Rows $rows, Backend $backend): Column
{
$haystacks = (new Parameter($this->haystack))->asTypes($rows, $backend, type_string(), type_array());
$haystack = $this->haystack->eval($rows, $backend);

if (type_bare($haystack->type()) instanceof StringType) {
return $backend->contains($haystack, (new Parameter($this->needle))->asStringColumn($rows, $backend));
}

$haystacks = (new Parameter($this->haystack))->typesOf($haystack, type_string(), type_array());
$needles = (new Parameter($this->needle))->asStrings($rows, $backend);
$results = [];
$i = 0;
Expand Down
30 changes: 4 additions & 26 deletions src/core/etl/src/Flow/ETL/Functions/EndsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

namespace Flow\ETL\Functions;

use Exception;
use Flow\ETL\Foundation\Describe\Description;
use Flow\ETL\Foundation\Describe\Rebuild;
use Flow\ETL\Foundation\Exception\EvaluationException;
use Flow\ETL\Functions\Evaluation\ResultColumn;
use Flow\ETL\Values\Column\Backend;
use Flow\ETL\Values\Column\Column;
use Flow\ETL\Values\Rows;
Expand All @@ -17,7 +14,6 @@
use SensitiveParameter;

use function Flow\Types\DSL\type_boolean;
use function str_ends_with;

final class EndsWith implements ScalarFunction
{
Expand Down Expand Up @@ -75,27 +71,9 @@ public function returns(): Type

public function eval(Rows $rows, Backend $backend): Column
{
$haystacks = (new Parameter($this->haystack))->asStrings($rows, $backend);
$needles = (new Parameter($this->needle))->asStrings($rows, $backend);
$results = [];
$i = 0;

try {
foreach ($haystacks as $i => $haystack) {
$needle = $needles[$i];

if ($haystack === null || $needle === null) {
$results[] = null;

continue;
}

$results[] = str_ends_with($haystack, $needle);
}
} catch (Exception $e) {
throw EvaluationException::at($i, $e);
}

return (new ResultColumn($backend))->of($this, $results);
return $backend->endsWith(
(new Parameter($this->haystack))->asStringColumn($rows, $backend),
(new Parameter($this->needle))->asStringColumn($rows, $backend),
);
}
}
84 changes: 55 additions & 29 deletions src/core/etl/src/Flow/ETL/Functions/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ public function arraysOf(Column $column): array
return $arrays;
}

/**
* @template T
*
* @param Type<T> ...$types
*
* @return list<?T>
*/
public function typesOf(Column $column, Type ...$types): array
{
$accepted = [];

// @mago-ignore analysis:mixed-assignment
foreach ($column->values() as $i => $value) {
if ($value === null) {
$accepted[] = null;

continue;
}

foreach ($types as $nextType) {
if ($nextType->isValid($value)) {
$accepted[] = $value;

continue 2;
}
}

throw new EvaluationException(
$i,
new InvalidArgumentException(sprintf(
'Expected one of "%s", got "%s".',
implode('", "', array_map(static fn(Type $type): string => $type->toString(), $types)),
get_debug_type($value),
)),
);
}

return $accepted;
}

/**
* @return list<?bool>
*/
Expand Down Expand Up @@ -182,6 +222,20 @@ public function asIntegerColumn(Rows $rows, Backend $backend): Column
return $builder->finish();
}

public function asStringColumn(Rows $rows, Backend $backend): Column
{
$column = $this->function->eval($rows, $backend);

if (type_bare($column->type()) instanceof StringType) {
return $column;
}

$builder = $backend->builder(Definitions::of('value', type_string(), true));
$builder->appendMany($this->checked($column->values(), type_string()));

return $builder->finish();
}

/**
* @throws InvalidArgumentException a column of another type, which Plus, Minus and Multiply refuse when they bind
*/
Expand Down Expand Up @@ -314,35 +368,7 @@ public function asStrings(Rows $rows, Backend $backend, ?string $default = null)
*/
public function asTypes(Rows $rows, Backend $backend, Type ...$types): array
{
$accepted = [];

// @mago-ignore analysis:mixed-assignment
foreach ($this->values($rows, $backend) as $i => $value) {
if ($value === null) {
$accepted[] = null;

continue;
}

foreach ($types as $nextType) {
if ($nextType->isValid($value)) {
$accepted[] = $value;

continue 2;
}
}

throw new EvaluationException(
$i,
new InvalidArgumentException(sprintf(
'Expected one of "%s", got "%s".',
implode('", "', array_map(static fn(Type $type): string => $type->toString(), $types)),
get_debug_type($value),
)),
);
}

return $accepted;
return $this->typesOf($this->function->eval($rows, $backend), ...$types);
}

/**
Expand Down
30 changes: 4 additions & 26 deletions src/core/etl/src/Flow/ETL/Functions/StartsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

namespace Flow\ETL\Functions;

use Exception;
use Flow\ETL\Foundation\Describe\Description;
use Flow\ETL\Foundation\Describe\Rebuild;
use Flow\ETL\Foundation\Exception\EvaluationException;
use Flow\ETL\Functions\Evaluation\ResultColumn;
use Flow\ETL\Values\Column\Backend;
use Flow\ETL\Values\Column\Column;
use Flow\ETL\Values\Rows;
Expand All @@ -17,7 +14,6 @@
use SensitiveParameter;

use function Flow\Types\DSL\type_boolean;
use function str_starts_with;

final class StartsWith implements ScalarFunction
{
Expand Down Expand Up @@ -75,27 +71,9 @@ public function returns(): Type

public function eval(Rows $rows, Backend $backend): Column
{
$haystacks = (new Parameter($this->haystack))->asStrings($rows, $backend);
$needles = (new Parameter($this->needle))->asStrings($rows, $backend);
$results = [];
$i = 0;

try {
foreach ($haystacks as $i => $haystack) {
$needle = $needles[$i];

if ($haystack === null || $needle === null) {
$results[] = null;

continue;
}

$results[] = str_starts_with($haystack, $needle);
}
} catch (Exception $e) {
throw EvaluationException::at($i, $e);
}

return (new ResultColumn($backend))->of($this, $results);
return $backend->startsWith(
(new Parameter($this->haystack))->asStringColumn($rows, $backend),
(new Parameter($this->needle))->asStringColumn($rows, $backend),
);
}
}
Loading
Loading