diff --git a/documentation/components/libs/postgresql.md b/documentation/components/libs/postgresql.md index 2fbedbe0ac..25286f4c25 100644 --- a/documentation/components/libs/postgresql.md +++ b/documentation/components/libs/postgresql.md @@ -215,7 +215,7 @@ echo $query->toSQL(); toSql(); // DELETE FROM users WHERE id = ANY (SELECT user_id FROM inactive_users) ``` +## Delete with CTE (WITH Clause) + +```php +from(table('parents'))->where(eq(col('archived'), literal(true)))), + cte( + 'removed_children', + delete() + ->from('children') + ->where(in_subquery(col('parent_id'), select(col('id'))->from(table('removed_parents')))) + ) +) + ->delete() + ->from('parents') + ->where(in_subquery(col('id'), select(col('id'))->from(table('removed_parents')))); + +echo $query->toSql(); +// WITH removed_parents AS (SELECT id FROM parents WHERE archived = true), removed_children AS (DELETE FROM children WHERE parent_id IN (SELECT id FROM removed_parents)) DELETE FROM parents WHERE id IN (SELECT id FROM removed_parents) +``` + ## RETURNING Clause ```php @@ -118,6 +142,16 @@ $query = delete() echo $query->toSql(); // DELETE FROM users WHERE id = 1 RETURNING * + +// Rename the OLD row alias +$query = delete() + ->from('children') + ->where(eq(col('id'), literal(10))) + ->returningAliases(old: 'o') + ->returning(col('id', 'o')); + +echo $query->toSql(); +// DELETE FROM children WHERE id = 10 RETURNING WITH (OLD AS o) o.id ``` ## Complex WHERE Conditions diff --git a/documentation/components/libs/postgresql/insert-query-builder.md b/documentation/components/libs/postgresql/insert-query-builder.md index b8435b1d30..64763ae478 100644 --- a/documentation/components/libs/postgresql/insert-query-builder.md +++ b/documentation/components/libs/postgresql/insert-query-builder.md @@ -125,6 +125,25 @@ echo $query->toSql(); // INSERT INTO users (name, email) SELECT name, email FROM archived_users ``` +## Insert with CTE + +```php +from('children')->where(eq(col('parent_id'), literal(1)))->returning(col('id'))) +) + ->insert() + ->into('audit') + ->columns('id') + ->select(select(col('id'))->from(table('removed'))); + +echo $query->toSql(); +// WITH removed AS (DELETE FROM children WHERE parent_id = 1 RETURNING id) INSERT INTO audit (id) SELECT id FROM removed +``` + ## Upsert (ON CONFLICT) ### ON CONFLICT DO NOTHING @@ -270,6 +289,17 @@ $query = insert() echo $query->toSql(); // INSERT INTO users (name) VALUES ('John') RETURNING * + +// Rename the NEW row alias +$query = insert() + ->into('children') + ->columns('id', 'parent_id') + ->values(literal(12), literal(1)) + ->returningAliases(new: 'n') + ->returning(col('id', 'n')); + +echo $query->toSql(); +// INSERT INTO children (id, parent_id) VALUES (12, 1) RETURNING WITH (NEW AS n) n.id ``` ## Schema-Qualified Tables diff --git a/documentation/components/libs/postgresql/merge-query-builder.md b/documentation/components/libs/postgresql/merge-query-builder.md index 3c767e141c..5e2eef6f53 100644 --- a/documentation/components/libs/postgresql/merge-query-builder.md +++ b/documentation/components/libs/postgresql/merge-query-builder.md @@ -242,6 +242,26 @@ echo $query->toSql(); // MERGE INTO target t USING source s ON t.id = s.id WHEN NOT MATCHED BY SOURCE THEN DELETE ``` +## Merge with RETURNING + +`merge_action()` returns `INSERT`, `UPDATE` or `DELETE` for each row. `returningAll()` writes `RETURNING *`. + +```php +using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->whenMatched() + ->thenDelete() + ->returning(merge_action(), col('id', 'c')); + +echo $query->toSql(); +// MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING merge_action() , c.id +``` + ## Schema-Qualified Tables ```php diff --git a/documentation/components/libs/postgresql/select-query-builder.md b/documentation/components/libs/postgresql/select-query-builder.md index f884628a7d..890c847110 100644 --- a/documentation/components/libs/postgresql/select-query-builder.md +++ b/documentation/components/libs/postgresql/select-query-builder.md @@ -35,7 +35,7 @@ echo $query->toSql(); use function Flow\PostgreSql\DSL\{ select, star, table, col, - eq, gt, lt, ge, le, ne, between, in_, like, is_null, + eq, gt, lt, ge, le, ne, between, in_, in_subquery, like, is_null, literal, and_, or_, not_ }; @@ -98,6 +98,14 @@ $query = select(star()) echo $query->toSql(); // SELECT * FROM users WHERE status IN ('active', 'pending', 'verified') +// IN (subquery) condition +$query = select(star()) + ->from(table('users')) + ->where(in_subquery(col('id'), select(col('user_id'))->from(table('orders')))); + +echo $query->toSql(); +// SELECT * FROM users WHERE id IN (SELECT user_id FROM orders) + // LIKE condition $query = select(star()) ->from(table('users')) @@ -244,7 +252,7 @@ echo $query->toSql(); toSql(); // WITH RECURSIVE tree AS (...) SELECT * FROM tree +// Recursive CTE with SEARCH (searchDepthFirst() writes DEPTH FIRST) +$query = with( + cte( + 't', + select(literal(1))->unionAll( + select(binary_expr(col('n'), '+', literal(1)))->from(table('t'))->where(lt(col('n'), literal(3))) + ), + ['n'] + )->searchBreadthFirst(['n'], 'ord') +) + ->recursive() + ->select(star()) + ->from(table('t')); + +echo $query->toSql(); +// WITH RECURSIVE t(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 3) SEARCH BREADTH FIRST BY n SET ord SELECT * FROM t + +// Recursive CTE with CYCLE (without the mark values, the cycle mark is true / false) +$query = with( + cte( + 'graph', + select(col('id'), col('link')) + ->from(table('edges')) + ->where(eq(col('id'), literal(1))) + ->unionAll( + select(col('id', 'e'), col('link', 'e')) + ->from(table('edges')->as('e')) + ->join(table('graph'), eq(col('id', 'e'), col('link', 'graph'))) + ), + ['id', 'link'] + )->cycle(['id'], 'is_cycle', 'path', literal('Y'), literal('N')) +) + ->recursive() + ->select(star()) + ->from(table('graph')); + +echo $query->toSql(); +// WITH RECURSIVE graph(id, link) AS (SELECT id, link FROM edges WHERE id = 1 UNION ALL SELECT e.id, e.link FROM edges e JOIN graph ON e.id = graph.link) CYCLE id SET is_cycle TO 'Y' DEFAULT 'N' USING path SELECT * FROM graph + +// Data-modifying CTE: cte() also takes INSERT, UPDATE, DELETE and MERGE queries +$query = with( + cte( + 'updated', + update() + ->update('children') + ->set('parent_id', literal(2)) + ->where(eq(col('id'), literal(10))) + ->returning(col('id'), col('parent_id')) + ) +) + ->select(col('id'), col('parent_id')) + ->from(table('updated')); + +echo $query->toSql(); +// WITH updated AS (UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING id, parent_id) SELECT id, parent_id FROM updated + // Materialized CTE $query = with( cte( diff --git a/documentation/components/libs/postgresql/update-query-builder.md b/documentation/components/libs/postgresql/update-query-builder.md index acef0fcff8..96fe108d02 100644 --- a/documentation/components/libs/postgresql/update-query-builder.md +++ b/documentation/components/libs/postgresql/update-query-builder.md @@ -11,7 +11,7 @@ The Update Query Builder provides a fluent, type-safe interface for constructing ```php update('users') @@ -45,7 +45,7 @@ echo $query->toSql(); ```php toSql(); ```php update('users', 'u') @@ -144,12 +144,31 @@ echo $query->toSql(); // UPDATE products SET price = original_price WHERE id = 1 ``` +## Update with CTE + +```php +from('children')->where(eq(col('parent_id'), literal(1)))->returning(col('id'))) +) + ->update() + ->update('audit') + ->set('note', literal('moved')) + ->where(in_subquery(col('id'), select(col('id'))->from(table('moved')))); + +echo $query->toSql(); +// WITH moved AS (DELETE FROM children WHERE parent_id = 1 RETURNING id) UPDATE audit SET note = 'moved' WHERE id IN (SELECT id FROM moved) +``` + ## RETURNING Clause ```php toSql(); // UPDATE users SET name = 'John' WHERE id = 1 RETURNING * + +// Old and new values through renamed aliases +$query = update() + ->update('children') + ->set('parent_id', literal(2)) + ->where(eq(col('id'), literal(10))) + ->returningAliases(old: 'o', new: 'n') + ->returning(col('parent_id', 'o'), col('parent_id', 'n')); + +echo $query->toSql(); +// UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING WITH (OLD AS o, NEW AS n) o.parent_id, n.parent_id ``` ## Complex WHERE Conditions diff --git a/documentation/upgrading-0.41-0.43.md b/documentation/upgrading-0.41-0.43.md deleted file mode 100644 index 1652cda879..0000000000 --- a/documentation/upgrading-0.41-0.43.md +++ /dev/null @@ -1,666 +0,0 @@ -# Upgrade Guide 0.41.x - 0.43.x - -[TOC] - -This document provides guidelines for upgrading between versions of Flow PHP. Please follow the instructions for your -specific version to ensure a smooth upgrade process. - -Upgrading from older versions: [0.35.x to 0.41.x](/documentation/upgrading-0.35-0.41.md) - ---- - -## Upgrading from 0.42.x to 0.43.x - -### 1) `flow-php/etl` - `to_transformation()` expands a `Transformation` once per loader, not once per batch - -| Before | After | -|-----------------------------------------------------------------------------------|-------------------------| -| `to_transformation(limit(3), $loader)`, 6 batches → 6 rows loaded | 3 rows loaded | -| `to_transformation(add_row_index('n'), $loader)`, 6 batches → `n = [1,1,1,1,1,1]` | `n = [1,2,3,4,5,6]` | -| nested `DataFrame` span per batch | one nested span per run | - -Unchanged: any pipeline using `->collect()`, `drop()`, `select()`, `mask_columns()`, `batch_size()`, `batch_by()`. - -### 2) `flow-php/telemetry` - `Tracer::span()` no longer activates the span - -| Before | After | -|----------------------------------------------------|-------------------------------------------------------------------| -| `$tracer->span('x')` makes the span current | does not; `$tracer->activate($span): Scope` does | -| `$tracer->complete($span)` also detaches the scope | ends the span only | -| `span(..., SpanContext $parentContext)` | `span(..., Context $parent)` | -| `trace(..., SpanContext $parentContext)` | `trace(..., Context $parent)` | -| `Scope::detach()` always returns `0` | returns `Scope::DETACHED`, `Scope::INACTIVE` or `Scope::MISMATCH` | - -Call sites relying on implicit nesting still compile and silently produce siblings. Rewrite each one that needs -children: - -Before: - -```php -$span = $tracer->span('parent'); - -try { - // ... -} finally { - $tracer->complete($span); -} -``` - -After: - -```php -$span = $tracer->span('parent'); -$scope = $tracer->activate($span); - -try { - // ... -} finally { - $scope->detach(); - $tracer->complete($span); -} -``` - -### 3) `flow-php/etl` - window aggregates use the SQL default frame - -On `d = 2024-01-01, 2024-01-02, 2024-01-03, 2024-01-04` and `s = 100, 200, 300, 400`: - -| Before | After | -|-------------------------------------------------------------------------------|-----------------------------------------------| -| `sum(ref('s'))->over(window()->orderBy(ref('d')))` → `1000, 1000, 1000, 1000` | `100, 300, 600, 1000` | -| `average()`, `count()` over an ordered window - whole partition | rows up to the current row's peers | -| `window()->partitionBy(ref('dept'))` - whole partition | unchanged | -| empty frame | `sum()`/`average()` → `null`, `count()` → `0` | - -Restore the previous result: - -```php -sum(ref('s'))->over(window()->orderBy(ref('d'))->rowsBetween(unbounded_preceding(), unbounded_following())); -``` - -### 4) `flow-php/etl` - `count()` over a window is SQL `COUNT` - -On `s = 100, 100, 300` ordered by a distinct column: - -| Before | After | -|---------------------------------------------------------------------------|-------------------------------------------------| -| `count(ref('s'))` counts rows sharing the current row's value → `2, 2, 1` | counts non-null values in the frame → `1, 2, 3` | -| `count()` threw `Count WindowFunction function requires a reference.` | counts every row in the frame (`COUNT(*)`) | - -### 5) `flow-php/etl` - `partitionBy()` no longer sets `orderBy()` - -| Before | After | -|----------------------------------------------------------------------------------------------------------|---------------------------------------------------| -| `window()->orderBy(ref('date'))->partitionBy(ref('dept'))->order()` → `['dept']` | `['date']` | -| `window()->partitionBy(ref('dept'))->order()` → `['dept']` | `[]` | -| `rank()`/`dense_rank()`/`row_number()` over a `partitionBy()`-only window ranked by the partition column | throws `... requires to be ordered by one column` | - -Add the ordering explicitly: - -```php -rank()->over(window()->partitionBy(ref('dept'))->orderBy(ref('salary')->desc())); -``` - -### 6) `flow-php/etl` - `WindowFunction::apply()` receives a `WindowContext` - -| Before | After | -|----------------------------------------------------------|----------------------------------------------------------| -| `apply(Row $row, Rows $partition, FlowContext $context)` | `apply(WindowContext $window)` | -| `$row` | `$window->row()` | -| `$partition` | `$window->partition()` | -| `$context` | `$window->flowContext()` | -| - | `$window->frame()` - rows within the current row's frame | -| - | `$window->index()` - position in the ordered partition | -| `row_number()` on duplicate rows → `1, 1, 3` | `1, 2, 3` | - -Implementations must no longer sort; `$window->partition()` and `$window->frame()` are already ordered. - -### 7) `flow-php/postgresql` - query recording moved to `flow-php/symfony-postgresql-bundle` - -| Before | After | -|--------------------------------------------------|----------------------------------------------------------------------| -| `Flow\PostgreSql\Client\Debug\RecordingClient` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\ProfilerClient` | -| `Flow\PostgreSql\Client\Debug\QueryLog` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\QueryRecorder` | -| `Flow\PostgreSql\Client\Debug\QueryLogOptions` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\QueryRecorderOptions` | -| `Flow\PostgreSql\Client\Debug\RecordedQuery` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\RecordedQuery` | -| `QueryLogOptions::$maxParameters` | `QueryRecorderOptions::$maxRetainedParameters` | -| `QueryLogOptions::maxParameters()` | `QueryRecorderOptions::maxRetainedParameters()` | -| service `flow.postgresql.profiler.query_log` | `flow.postgresql.profiler.query_recorder` | -| config `flow_postgresql.profiler.max_parameters` | `flow_postgresql.profiler.max_retained_parameters` | - -Applies to `flow-php/postgresql` users only through the bundle; `Client\Telemetry` is unchanged. - -### 8) `flow-php/symfony-telemetry-bundle` - cache pools and PSR-18 clients that were silently skipped are now traced - -| Before | After | -|------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------| -| `cache.system`, `cache.validator`, `cache.serializer`, `cache.property_info`, `cache.app`, `cache.doctrine.*`, `cache.http_client.pool` - not traced | traced: `cache.*` spans and `flow.cache.hits` / `flow.cache.misses` | -| pool or client whose class is a `%parameter%` - not traced | traced | -| tag-aware pool whose class is a `%parameter%` - got the non-tag-aware decorator | gets `TagAwareTraceableCacheAdapter` | -| PSR-18 client behind an autoconfigured or abstract parent definition - container build failed with *"has a reference to an abstract definition"* | compiles; the client is traced | -| `instrumentation.cache.exclude_pools` entries for framework pools - had no effect | take effect | - -To keep the previous set of traced pools, exclude the framework's own: - -```yaml -flow_telemetry: - instrumentation: - cache: - exclude_pools: - - 'cache.system' - - 'cache.validator' - - 'cache.serializer' - - 'cache.property_info' - - '/^cache\.doctrine\..*/' - - 'cache.http_client.pool' -``` - -### 9) `flow-php/types` - a `Type` implementation's generic parameter is the value it represents - -| Before | After | -|----------------------------------|------------------------------------------| -| `ListType` | `ListType>` | -| `MapType` | `MapType>` | -| `StructureType` | `StructureType>` | -| `ClassStringType` | `ClassStringType>` | -| `ListType::element(): Type` | `Type>` | -| `MapType::key(): Type` | `Type>` | -| `MapType::value(): Type` | `Type>` | -| `type_string(): Type` | `type_string(): StringType` | - -Update `ListType`, `MapType`, `StructureType` and `ClassStringType` parameters in your own docblocks. - ---- - -## Upgrading from 0.41.x to 0.42.x - -### 1) `flow-php/symfony-telemetry-bundle` - messenger tracing simplified - -| Before (0.41) | After (0.42) | -|----------------------------------------------------------------------|--------------------------------------------------------------------| -| `instrumentation.messenger.trace`: `worker`/`handlers`/`both`/`none` | `instrumentation.messenger.trace`: `true`/`false` (default `true`) | -| `instrumentation.messenger.link`: `dispatcher`/`worker`/`both` | removed | -| `messenger.receive` worker-cycle span | removed | - -The worker-cycle span and the `trace: worker`/`both` modes are gone; a consumed message is a `process` span and a -produced one a `send` span. The `messenger:consume` worker loop is suppressed via -`instrumentation.console.exclude_commands` - which now **fully suppresses** matching commands (previously it only -skipped the console span) and defaults to `['messenger:consume']` - so transport-poll/idle-tick work does not surface -and the long-lived `messenger:consume` console span is dropped. Per-message handler traces are still recorded. Set -`console.exclude_commands: []` to trace the worker loop. - -### 2) `flow-php/symfony-telemetry-bundle` - cache span names unified to dotted lowercase - -| Before | After | -|-----------------------------------------------------------------------|----------------------------------------------------------| -| `Cache Commit {pool}` | `cache.commit` | -| `Cache Save {key} {pool}` | `cache.save` | -| `Cache SaveDeferred {key} {pool}` | `cache.save_deferred` | -| `Cache Delete {key} {pool}` | `cache.delete` | -| `Cache DeleteItem {key} {pool}` | `cache.delete_item` | -| `Cache DeleteItems {pool}` | `cache.delete_items` | -| `Cache Clear {pool}` | `cache.clear` | -| `Cache Prune {pool}` | `cache.prune` | -| `Cache Reset {pool}` | `cache.reset` | -| `Cache InvalidateTags {pool}` | `cache.invalidate_tags` | -| `cache.operation: saveDeferred/deleteItem/deleteItems/invalidateTags` | `save_deferred/delete_item/delete_items/invalidate_tags` | - -Cache spans now match the DBAL/messenger convention (dotted lowercase, low cardinality). The `{key}` and `{pool}` -that were baked into the span name move out of it - they were already available as the `cache.key` and `cache.pool` -attributes. Rename these series in dashboards and alerts, and update any filters on the camelCase `cache.operation` -values. - -### 3) `flow-php/telemetry` - `Sampler::shouldSample()` receives the parent `Context` - -| Before | After | -|--------------------------------------------|--------------------------------------------------------------------| -| `shouldSample(Span $span): SamplingResult` | `shouldSample(Context $parentContext, Span $span): SamplingResult` | -| `$sampler->shouldSample($span)` | `$sampler->shouldSample($context, $span)` | - -Custom `Sampler` implementations (including a `sampler: { type: service }` service in -`flow-php/symfony-telemetry-bundle`) must update the signature and forward `$parentContext` to any delegated sampler. - -### 4) `flow-php/telemetry` - `ResettableContextStorage` removed; `MemoryContextStorage::reset()` removed - -| Before | After | -|---------------------------------------------------|---------| -| `Flow\Telemetry\Context\ResettableContextStorage` | removed | -| `MemoryContextStorage::reset()` | removed | - -The context storage is no longer tagged `kernel.reset` in `flow-php/symfony-telemetry-bundle`; scope balance is -maintained by attach/detach alone. A custom `context_storage` service no longer needs a `reset()` method. - -### 5) `flow-php/symfony-telemetry-bundle` - messenger tracing middleware auto-injected into all buses - -The tracing middleware is now injected into every message bus automatically. If you previously added -`flow.telemetry.messenger.middleware` to a bus's `framework.messenger.buses.*.middleware` list by hand, remove it to -avoid duplicate spans. - -### 6) `flow-php/symfony-telemetry-bundle` - `http_kernel.exclude_paths` now suppresses the whole request - -| Before | After | -|---------------------------------------------------|---------------------------------------------------------| -| Excluded path only skips its own request span | Excluded path suppresses tracing for the entire request | -| DBAL/cache/`kernel.terminate` work still recorded | DBAL/cache/`kernel.terminate` work produces no spans | - -An excluded path now attaches the OpenTelemetry suppression key for the request's duration (through -`kernel.terminate`), so lower-level auto-instrumentation and terminate-phase database writes no longer emit orphan root -spans (e.g. the `/_wdt` toolbar fetch writing an audit row after its response). If you relied on those child spans being -recorded for an excluded path, remove the path from `exclude_paths`. - -### 7) `flow-php/postgresql`, `flow-php/symfony-postgresql-bundle` - `traceTransactions` bool replaced by - -`transactionSpans` mode - -| Before | After | -|----------------------------------------------------------------|--------------------------------------------------------------------------------| -| `postgresql_telemetry_options(traceTransactions: true)` | `postgresql_telemetry_options(transactionSpans: TransactionSpanMode::GROUPED)` | -| `postgresql_telemetry_options(traceTransactions: false)` | `postgresql_telemetry_options(transactionSpans: TransactionSpanMode::OFF)` | -| `PostgreSqlTelemetryOptions` 2nd arg `bool $traceTransactions` | `TransactionSpanMode $transactionSpans` | -| `$options->traceTransactions(false)` | `$options->transactionSpans(TransactionSpanMode::OFF)` | -| `$options->traceTransactions` (property) | `$options->transactionSpans` (`TransactionSpanMode`) | -| config `telemetry.trace_transactions: true` | config `telemetry.transaction_spans: grouped` | -| config `telemetry.trace_transactions: false` | config `telemetry.transaction_spans: off` | - -`TransactionSpanMode::PER_OPERATION` emits a short span per `BEGIN`/`COMMIT`/`ROLLBACK`; `GROUPED` (default) keeps the -single long-lived transaction span. - -The `db.client.operation.duration` metric now uses only low-cardinality dimensions: queries are tagged with -`db.system.name`, `db.namespace`, `db.operation.name`, `db.collection.name`; transactions with `db.system.name`, -`db.namespace`, `db.operation.name` (`begin`/`commit`/`rollback`), `db.transaction.nesting_level`. `db.query.text`, -`db.query.parameter.*`, `server.address` and `db.transaction.savepoint` are no longer metric dimensions. - -### 8) `flow-php/symfony-telemetry-bundle` - DBAL spans, metrics and config aligned with the PostgreSQL client - -| Before | After | -|------------------------------------------------------------------|--------------------------------------------------------------------------------------------| -| `doctrine.dbal.transaction.begin`/`.commit`/`.rollback` (always) | one `BEGIN TRANSACTION` span per transaction (`grouped`) | -| `doctrine.dbal.connection.exec`/`.query` span names | semconv `{db.operation.name} {db.collection.name}` (e.g. `SELECT users`) | -| `doctrine.dbal.statement.execute`/`.prepare` span names | semconv `{db.operation.name} {db.collection.name}` | -| config `instrumentation.dbal.log_sql` | removed - `db.query.text` is always recorded (bounded by `max_sql_length`) | -| - | config `instrumentation.dbal.transaction_spans`: `grouped` (default)/`per_operation`/`off` | -| - | config `instrumentation.dbal.collect_metrics` (default `true`) | -| - | config `instrumentation.dbal.include_parameters`/`max_parameters`/`max_parameter_length` | - -Query spans now carry `db.system.name`, `db.namespace`, `server.address`, `server.port`, `db.operation.name`, -`db.collection.name`, `db.response.returned_rows` and (on error) `db.response.status_code`, and the instrumentation -emits `db.client.operation.duration` and `db.client.response.returned_rows` metrics. If you matched DBAL spans by their -`doctrine.dbal.*` names, switch to the semantic names above. - -### 9) `flow-php/symfony-telemetry-bundle` - custom attribute keys moved out of reserved OTel namespaces - -| Before | After | -|------------------------------------|----------------------------------------| -| `controller` | `flow.symfony.controller` | -| `controller.argument` | `flow.symfony.controller.argument` | -| `code.namespace` + `code.function` | `code.function.name` (`Class::method`) | -| `command.name` | `flow.symfony.command.name` | -| `command.class` | `flow.symfony.command.class` | -| `process.signal` | `flow.symfony.command.signal` | -| `process.exit_code` | `process.exit.code` | -| `db.connection.name` | `flow.db.connection.name` | -| `db.transaction.nesting_level` | `flow.db.transaction.nesting_level` | -| `http.client.name` | `flow.http.client.name` | -| `log.channel` | `flow.log.channel` | - -### 10) `flow-php/symfony-telemetry-bundle` - HTTP spans aligned with the stable HTTP semconv - -| Before | After | -|-------------------------------------------------|----------------------------------------------| -| server span `url.full` | removed | -| server span `url.path` = URI incl. query | path only; query moves to `url.query` | -| - | server span `user_agent.original` | -| client span name `{method} {host}` | `{method}` | -| client span `server.port` only when non-default | always set (scheme default 80/443 filled in) | - -Applies to the HttpKernel server span and to the `http_client`/`psr18_client` client spans. - -### 11) `flow-php/symfony-telemetry-bundle` - messenger destination is the transport; `metrics_duration_unit` removed - -| Before | After | -|--------------------------------------------------------------------|----------------------------------------------------------| -| `messaging.destination.name` = short message class | transport name (consume side); absent on dispatch | -| `messaging.transport` | removed (folded into `messaging.destination.name`) | -| `messaging.consumer.group.name` (metric attribute) | removed | -| `messaging.message.class` | `flow.messenger.message.class` | -| `messaging.symfony.bus` | `flow.messenger.bus` | -| consume span `process {ShortClass}` | `process {transport}` | -| dispatch span `send {ShortClass}` | `send` | -| config `instrumentation.messenger.metrics_duration_unit`: `s`/`ms` | removed - `messaging.process.duration` is always seconds | -| `Instrumentation\Messenger\MessengerMetricDurationUnit` | removed | - -### 12) `flow-php/phpunit-telemetry-bridge` - `test.*` keys aligned with the OTel test registry; durations in seconds - -| Before | After | -|---------------------------------------------------|--------------------------------------------------------| -| `test.name` | `test.case.name` | -| `test.status` | `test.case.result.status` | -| `test.suite` | `test.suite.name` | -| - | `test.suite.run.status`: `success`/`failure`/`skipped` | -| `test.id` | `flow.phpunit.test.id` | -| `test.class` | `flow.phpunit.test.class` | -| `test.method` | `flow.phpunit.test.method` | -| `test.suite.test_count` | `flow.phpunit.suite.test_count` | -| `test.suite.is_root` | `flow.phpunit.suite.is_root` | -| `test.memory.peak_bytes` | `flow.phpunit.test.memory.peak` | -| `test.memory.delta_bytes` | `flow.phpunit.test.memory.delta` | -| `test.duration_ms` span attribute | removed (use the span duration) | -| `exception.message` span attribute | removed (message stays in the span status description) | -| `flow.phpunit.test.duration` unit `ms` | `s` (values rescaled) | -| `flow.phpunit.suite.duration` unit `ms` | `s` (values rescaled) | -| `flow.phpunit.test.memory.*` unit `bytes` | `By` | -| `flow.phpunit.test.count`/`suite.test_count` unit | `{test}` | -| - | resource attribute `telemetry.sdk.version` | - -### 13) `flow-php/filesystem`, `flow-php/etl`, `flow-php/postgresql` - flow-custom keys under `flow.*`; UCUM units - -| Before | After | -|------------------------------------------------------------|------------------------------------------------------| -| `path.uri`/`path.to` | `flow.filesystem.path.uri`/`.path.to` | -| `stream.type` | `flow.filesystem.stream.type` | -| `bytes.total_read`/`bytes.total_written` | `flow.filesystem.bytes.total_read`/`.total_written` | -| `filesystem.operation`/`filesystem.protocol` | `flow.filesystem.operation`/`.protocol` | -| spans `Read {file}`/`Write {file}` | `filesystem.read`/`filesystem.write` | -| `flow.filesystem.*.size` unit `bytes` | `By` | -| `flow.filesystem.*.operations` unit `operations` | `{operation}` | -| `dataframe.id`/`dataframe.name` | `flow.etl.dataframe.id`/`.dataframe.name` | -| `rows.total`/`rows.throughput.per_second` | `flow.etl.rows.total`/`.rows.throughput.per_second` | -| `memory.min.mb`/`memory.max.mb` | `flow.etl.memory.min`/`.memory.max` (values stay MB) | -| `loader.class`/`transformer.class` | `flow.etl.loader.class`/`.transformer.class` | -| `destination.uri`/`loading.rows` | `flow.etl.destination.uri`/`.loading.rows` | -| `transformation.input_rows`/`.output_rows` | `flow.etl.transformation.input_rows`/`.output_rows` | -| `join.type`/`scalar.function` | `flow.etl.join.type`/`.scalar.function` | -| spans `Cache Set {key}`/`Cache Delete {key}`/`Cache Clear` | `cache.set`/`cache.delete`/`cache.clear` | -| `flow.cache.hits`/`.misses` unit `operations` | `{operation}` | -| `flow.etl.rows.processed` unit `rows` | `{row}` | -| `flow.etl.rows.throughput` unit `rows/s/sec` | `{row}/s` | -| `db.transaction.savepoint` | `flow.db.transaction.savepoint` | -| `db.transaction.nesting_level` | `flow.db.transaction.nesting_level` | - -### 14) `flow-php/telemetry`, `flow-php/psr18-telemetry-bridge` - shared semconv constants; UCUM time units - -| Before | After | -|---------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------| -| official keys duplicated per package (`DbAttributes`, `PostgreSqlTelemetryAttributes`, inline strings) | `Flow\Telemetry\SemConvAttributes` / `SemConvMetrics` | -| `TimeUnit::SECONDS->value` = `'sec'` | `'s'` | -| `TimeUnit::MICROSECONDS->value` = `'µs'` | `'us'` | -| PSR-18 client span name `{method} {host}` | `{method}` | -| PSR-18 `server.port` only when non-default | always set (scheme default 80/443 filled in) | -| `FilesystemTelemetryAttributes::ATTR_BYTES_READ`/`ATTR_BYTES_WRITTEN`/`ATTR_PATH_FROM`/`ATTR_PATH_IS_PATTERN`/`ATTR_ERROR_TYPE` | removed | -| `PostgreSqlTelemetryAttributes::DB_QUERY_SUMMARY` + official-key constants | removed (officials via `SemConvAttributes`) | - -### 15) `flow-php/symfony-telemetry-bundle` - `runtime_mode` removed; terminate flushes, process end shuts down - -| Before | After | -|-----------------------------------------------------------------------------|---------------------------------------------| -| `flow_telemetry.runtime_mode: auto`/`classic`/`worker` | removed | -| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\RuntimeModeResolver` | removed | -| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\RuntimeMode` | removed | -| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\WorkerModeDetector` | removed | -| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\EnvironmentWorkerModeDetector` | removed | -| shutdown on `kernel.terminate`/`console.terminate` (classic mode) | flush on terminate; shutdown at process end | - -Drop the `runtime_mode` key from `flow_telemetry` config and remove any `WorkerModeDetector` service overrides. - -### 16) `flow-php/telemetry` - `Telemetry::registerShutdownFunction()` holds a weak reference - -| Before | After | -|--------------------------------------------------|---------------------------------------------------------------| -| strong reference; instance kept alive until exit | weak reference; garbage-collected instances are not shut down | - -Keep the registered `Telemetry` instance referenced for as long as it should be shut down at process end. - -### 17) `flow-php/etl` - `SchemaValidator::isValid()` replaced by `validate(): ValidationContext` - -| Before | After | -|-----------------------------------------------|------------------------------------------------------------------| -| `SchemaValidator::isValid(...): bool` | `SchemaValidator::validate(...): ValidationContext` | -| `$validator->isValid($expected, $given)` | `$validator->validate($expected, $given)->isValid()` | -| `schema_validate(...): bool` | `schema_validate(...): ValidationContext` | -| `new SchemaValidationException($exp, $given)` | `new SchemaValidationException($exp, $given, ValidationContext)` | -| - | `SchemaValidationException::context(): ValidationContext` | - -Custom `SchemaValidator` implementations must return a `Flow\ETL\Schema\Validator\ValidationContext` -built from the missing, mismatched (`MismatchedDefinition`), and unexpected definitions they reject. - -### 18) `flow-php/filesystem` - `Partition` name and value forbid `{` and `}` - -| Before | After | -|----------------------------------|-----------------------------------| -| `new Partition('na{me', 'a}b')` | throws `InvalidArgumentException` | -| `partitionBy()` values with `{}` | throws `InvalidArgumentException` | - -`{name}` in a path is now a partition placeholder resolved from `partitionBy()` columns; strip braces from partition -values before partitioning. - -### 19) `flow-php/etl` - `Cache` stores only `Rows`; cache indexes are stored as `Rows` - -| Before | After | -|---------------------------------------------------|-----------------------------------------------| -| `Cache::get(string): Row\|Rows\|CacheIndex` | `Cache::get(string): Rows` | -| `Cache::set(string, Row\|Rows\|CacheIndex): void` | `Cache::set(string, Rows): void` | -| `$cache->set($id, $row)` | `$cache->set($id, rows($row))` | -| `$cache->set($id, $cacheIndex)` | `$cache->set($id, $cacheIndex->toRows())` | -| `$cache->get($id)` returning `CacheIndex` | `CacheIndex::fromRows($id, $cache->get($id))` | - -Custom `Cache` implementations must adopt the `Rows`-only signatures. - -### 20) `flow-php/etl` - cache and serialization use the Floe (`.floe`) binary format; datetime subclasses rejected - -| Before | After | -|----------------------------------------------------------------------------------------|------------------------------------------------------------------------| -| `config_builder()->serializer()` default `Base64Serializer(new NativePHPSerializer())` | `Flow\Floe\FloeSerializer` | -| `filesystem_cache(..., Serializer $serializer = new NativePHPSerializer())` | `filesystem_cache(..., Serializer $serializer = new FloeSerializer())` | -| `new FilesystemCache($filesystem, $serializer, $cacheDir)` | `new FilesystemCache($filesystem, $cacheDir, $serializer)` | -| caching or serializing a `DateTime`/`DateTimeImmutable` subclass (e.g. Carbon) | throws `Flow\Floe\Exception\FloeException` | - -Delete cache directories written by 0.41 - the old serialized payloads are unreadable. Convert -`DateTime`/`DateTimeImmutable` subclasses to `DateTime`/`DateTimeImmutable` before caching or serializing. - -A Floe file carries exactly one schema, and sort / join / group-by spill batches through it - a pipeline with a drifting -or schemaless source must declare column types at the source so nullable columns carry a concrete type -(`DataFrame::match($schema)` validates but does not retype values): - -```php -->read(from_array($data, schema(str_schema('email'), float_schema('discount', nullable: true)))) -``` - -### 21) `flow-php/symfony-telemetry-bundle` - `HttpKernelSpanSubscriber` takes a - -`RouteNamePathMap` instead of the router - -| Before | After | -|-----------------------------------------------------------|-------------------------------------------------------------------------| -| `new HttpKernelSpanSubscriber(..., router: $router, ...)` | `new HttpKernelSpanSubscriber(..., routePaths: $routeNamePathMap, ...)` | -| `?RouterInterface $router = null` | `?RouteNamePathMap $routePaths = null` | - -Applies only to direct construction; services wired by the bundle need no change. - -### 22) `flow-php/etl` - sort algorithm is an explicit choice; external sort is the default - -| Before | After | -|-----------------------------------------------------------------|---------------------------------------------------------------------------| -| `SortAlgorithms` enum | removed; `ConfigBuilder::sort(memory_sort()` / `external_sort())` | -| `SortAlgorithms::MEMORY_FALLBACK_EXTERNAL_SORT` (default) | removed; default is `external_sort()` | -| `SortAlgorithms::SQLITE_SORT` | removed | -| `SortAlgorithms::useMemory()` | removed | -| `ConfigBuilder::sortMemoryLimit(Unit $unit)` | removed | -| `SortConfigBuilder` | removed; `external_sort()` builder | -| `SortConfigBuilder::sortMemoryLimit(Unit $unit)` | removed | -| `SortConfig` | `MemorySortConfig` \| `ExternalSortConfig` (`Config::$sort`) | -| `SortConfig::$memoryLimit` | removed | -| `SortConfig::SORT_MAX_MEMORY_ENV` / `FLOW_SORT_MAX_MEMORY` env | removed | -| `new MemorySort(Unit $maximumMemory)` | removed; `sort(memory_sort())` | -| `MemorySort` throwing `Flow\ETL\Exception\OutOfMemoryException` | removed | -| `new ExternalSort($cache, $bucketsCount)` | removed; `sortBy()` pipelines `BucketingProcessor` + `MergeSortProcessor` | -| `ConfigBuilder::externalSortFilesystem(string)` | `external_sort()->filesystemProtocol(string)` | -| - | `external_sort()->runSize(int)` (default `10000`) | -| - | `external_sort()->bucketsCount(int)` (default `100`) | -| - | `external_sort()->batchSize(int)` (default `1000`) | -| - | `external_sort()->storage(BucketsStorage)` (default `FilesystemBuckets`) | - -To sort in memory, opt in explicitly: - -```php -data_frame(config_builder()->sort(memory_sort()))->read(...)->sortBy(ref('id'))->run(); -``` - -### 23) `flow-php/etl` - joins emit every matching right row and follow SQL semantics - -| Before | After | -|-----------------------------------------------------------------------|------------------------------------------------------------------------| -| inner/left/right join: first matching row per probe row | one output row per matching pair | -| left/left_anti join: left row dropped on hash collision without match | left row kept | -| duplicated right side rows collapsed into one | preserved | -| `DataFrame::join(..., Join::inner)`: duplicated join columns kept | duplicated join columns dropped (empty prefix) | -| `Equal` on object values (Uuid, etc.): always `false` | compared with `==` | -| `Flow\ETL\Processor\HashJoin\HashTable` | `Flow\ETL\Join\HashJoin\HashTable` | -| `Flow\ETL\Processor\HashJoin\Bucket` | removed | -| - | `Expression::comparison()`, `All::comparisons()`, `Any::comparisons()` | - -`Rows::joinRight()` emits matched rows in left-probe order and unmatched right rows last. - -### 24) `flow-php/etl` - join buckets storage is configurable, spills to disk by default - -| Before | After | -|-----------------------------------------------------------|-------------------------------------------------------------------------| -| `join()` holds right side in memory, keeps left row order | join buckets spilled to disk (Floe files), left row order not preserved | -| - | `ConfigBuilder::join(hash_join())` | -| - | `hash_join()->storage(BucketsStorage)` (default `FilesystemBuckets`) | -| - | `hash_join()->bucketsCount(int)` (default `64`) | -| - | `hash_join()->batchSize(int)` (default `1000`) | -| - | `Flow\ETL\Bucketing\ResidentBucketsStorage` | -| - | `Flow\ETL\Bucketing\Storage\MemoryBuckets` | - -To keep the right side in memory and preserve left row order: - -```php -data_frame(config_builder()->join(hash_join()->storage(new MemoryBuckets()))) - ->read(...) - ->join(...) - ->run(); -``` - -### 25) `flow-php/etl` - `Serializer` works on `Rows` and filesystem streams - -| Before | After | -|-------------------------------------------------------------------------------------------|---------------------------------------------------------------------------| -| `Serializer::serialize(object $serializable): string` | `Serializer::serialize(Rows $rows, DestinationStream $destination): void` | -| `Serializer::unserialize(string $serialized, array $classes): object` | `Serializer::unserialize(SourceStream $source): Rows` | -| `CompressingSerializer`/`NativePHPSerializer` throw `Flow\ETL\Exception\RuntimeException` | throw `Flow\Serializer\Exception\SerializationException` | -| - | `Flow\Floe\FloeSerializer` (the default) | - -### 26) `flow-php/etl` - `EntryFactory` API reshaped - -| Before | After | -|---------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------| -| `EntryFactory::create(string $entryName, mixed $value, Schema\|Definition\|null $schema = null)` | `EntryFactory::create(string $name, mixed $value, ?Type $type = null, ?Metadata $metadata = null)` | -| `EntryFactory::createAs(string $entryName, mixed $value, Type $type, ?Metadata $metadata = null)` | `EntryFactory::cast(string $name, mixed $value, Type $type, ?Metadata $metadata = null)` | -| - | `EntryFactory::fromDefinition(Definition $definition, mixed $value): Entry` | -| `to_entry($name, $data, EntryFactory $entryFactory)` | `to_entry($name, $data, EntryFactory $entryFactory = new EntryFactory())` | -| `ConfigBuilder::build(EntryFactory $entryFactory = new EntryFactory())` | `ConfigBuilder::build()` | - -### 27) `flow-php/etl` - `Encoder`/`Hydrator` contracts; hydrator configured on the context - -| Before | After | -|---------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------| -| - | `Flow\ETL\Row\Encoder`: `encode(list): list`, `decode(list): list` | -| - | `Flow\ETL\Row\Hydrator`: `cast(list, ?Schema): Rows`, `hydrate(list, ?Schema): Rows`, `dehydrate(Rows): list` | -| - | `Flow\ETL\Row\RawRowValues`, `Flow\ETL\Row\TypedRowValues` | -| - | `Flow\ETL\Row\PhpRowHydrator`, `NativeRowHydrator`, `AdaptiveRowHydrator` (the default) | -| - | `ConfigBuilder::hydrator(Hydrator)`, `Config::hydrator()`, `FlowContext::hydrator()` | -| `array_to_row($data, EntryFactory $entryFactory, ...)` | `array_to_row($data, Hydrator $hydrator = new AdaptiveRowHydrator(), ...)` | -| `array_to_rows($data, EntryFactory $entryFactory, ...)` | `array_to_rows($data, Hydrator $hydrator = new AdaptiveRowHydrator(), ...)` | - -`cast()` coerces untrusted values through the schema (`null` schema infers types); `hydrate()` instantiates trusted, -already-typed values and requires a schema. A custom extractor reading raw values (CSV, JSON, XML, Excel, Text, Google -Sheet, PostgreSQL, Doctrine) calls `$context->hydrator()->cast(...)`; a self-describing source (Parquet, Floe) -calls `$context->hydrator()->hydrate(...)`. - -Under a schema, entries emit in schema-definition order (was data-key order with missing columns appended); a schema -column missing from the source hydrates as a typed null (was a `StringEntry` null); a column absent from the schema is -dropped. When the `flow_php` extension is loaded, `AdaptiveRowHydrator` runs hydration natively - pin the pure-PHP -engine with `config_builder()->hydrator(new PhpRowHydrator())`. - -### 28) `flow-php/etl-adapter-csv`, `-json`, `-parquet`, `-xml`, `-excel`, `-doctrine`, `-postgresql`, `-seal`, `-text`, - -`-google-sheet` - per-format `Encoder`s replace normalizers - -| Before | After | -|------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| -| `Flow\ETL\Adapter\CSV\RowsNormalizer`, `RowsNormalizer\EntryNormalizer`, `RowsNormalizer\ScalarCast` | `Flow\ETL\Adapter\CSV\CSVEncoder` | -| `Flow\ETL\Adapter\JSON\RowsNormalizer`, `RowsNormalizer\EntryNormalizer` | `Flow\ETL\Adapter\JSON\JSONEncoder` | -| `Flow\ETL\Adapter\Parquet\RowsNormalizer` | `Flow\ETL\Adapter\Parquet\ParquetEncoder` | -| `Flow\ETL\Adapter\XML\RowsNormalizer`, `RowsNormalizer\EntryNormalizer`, `RowsNormalizer\EntryNormalizer\PHPValueNormalizer` | `Flow\ETL\Adapter\XML\XMLEncoder` | -| `Flow\ETL\Adapter\Excel\RowsNormalizer\ExcelRowsNormalizer` | `Flow\ETL\Adapter\Excel\ExcelEncoder` | -| `Flow\ETL\Adapter\Doctrine\RowsNormalizer` | `Flow\ETL\Adapter\Doctrine\DbalEncoder` | -| `Flow\ETL\Adapter\Seal\RowsNormalizer`, `RowsNormalizer\EntryNormalizer` | `Flow\ETL\Adapter\Seal\SealEncoder` | -| - | `Flow\ETL\Adapter\Text\TextEncoder`, `Flow\ETL\Adapter\PostgreSql\PostgreSqlEncoder`, `Flow\ETL\Adapter\GoogleSheet\GoogleSheetEncoder` | -| `Flow\ETL\Adapter\Doctrine\TypesMap::flowRowTypes(Row)` | `TypesMap::flowSchemaTypes(Schema)` | -| `Flow\ETL\Adapter\PostgreSql\EntryTypesMap::mapEntry(Entry)` | `EntryTypesMap::map(string $column, Type $type, mixed $value)` | -| `InsertQueryBuilder::build(Rows, ...)` | `InsertQueryBuilder::build(array $values, Schema, ...)` | -| `UpdateQueryBuilder::build(Row, ...)`, `DeleteQueryBuilder::build(Row, ...)` | `build(array $value, Schema, ...)` | -| CSV/JSON/XML/Excel/Seal writers (no `dateFormat`) | `dateFormat = 'Y-m-d'` encoder constructor param | -| Excel `timeFormat = 'H:i:s'` | `timeFormat = '%H:%I:%S'` | -| - | `withDateFormat()` on `CSVLoader`, `JsonLoader`, `JsonLinesLoader`, `XMLLoader`, `ExcelLoader`, `SealLoader`; `SealLoader::withDateTimeFormat()` | - -Behavioural changes: CSV/JSON/XML/Excel/Seal `date` columns render with `dateFormat` (`Y-m-d`, was `dateTimeFormat`); -XML `date`/`time` columns render (was `InvalidArgumentException`); Seal `time` columns render as microseconds (was -`null`); Google Sheet `_spread_sheet_id`/`_sheet_name` are appended to the schema under `putInputIntoRows()` (were -dropped when a schema was set); Excel now appends `_input_file_uri` under `putInputIntoRows()`. - -### 29) `flow-php/etl` - null is a first-class `NullEntry`/`NullDefinition`; `from_null` metadata removed - -| Before | After | -|---------------------------------------------------------------------------|--------------------------------------------------------------------------------------| -| `null_entry($name)` → `StringEntry` (type `string`, `from_null` metadata) | `null_entry($name)` → `Flow\ETL\Row\Entry\NullEntry` (type `null`) | -| `null_schema($name)` → `StringDefinition` + `from_null` metadata | `null_schema($name)` → `Flow\ETL\Schema\Definition\NullDefinition` (always nullable) | -| `Flow\ETL\Row\Entry\StringEntry::fromNull($name, $metadata)` | `null_entry($name, $metadata)` | -| `new StringEntry($name, $value, $metadata, fromNull: true)` | `$fromNull` constructor flag removed | -| `Flow\ETL\Schema\Metadata::FROM_NULL` | removed | - -### 30) `flow-php/etl` - group by runs on bucketing storage; only referenced columns are spilled - -| Before | After | -|----------------------------------------------------------------------------|--------------------------------------------------------------------------------------| -| grouping state held in memory | rows partitioned into buckets through `BucketsStorage` (default `FilesystemBuckets`) | -| rows carry all columns into grouping | only `groupBy` refs + aggregator refs are spilled | -| STRICT schema mode throws for a missing aggregate column under `groupBy()` | row gets a `null` entry for it; aggregators skip `null` | -| group output follows input order (incidental) | follows hash-bucket order | -| - | `AggregatingFunction::references(): ?array` | -| - | `ConfigBuilder::groupBy(hash_group_by())` | -| - | `hash_group_by()->storage(BucketsStorage)` (default `FilesystemBuckets`) | -| - | `hash_group_by()->bucketsCount(int)` (default `64`) | -| - | `hash_group_by()->batchSize(int)` (default `1000`) | - -Custom aggregators must implement `references()` - return the references the aggregator reads, or -`null` when they cannot be statically enumerated (disables spill column pruning). - -### 31) `flow-php/etl` - `DataFrame::pivot()` removed; pivot is declared between `groupBy()` and `aggregate()` - -| Before | After | -|---------------------------------------------------|---------------------------------------------------| -| `->groupBy(...)->aggregate(...)->pivot(ref('x'))` | `->groupBy(...)->pivot(ref('x'))->aggregate(...)` | -| `DataFrame::pivot()` | removed; `GroupedDataFrame::pivot()` only | - -### 32) `flow-php/etl-adapter-csv`, `-excel`, `-json`, - -`-xml` - explicit schema no longer projects partition columns away - -| Before | After | -|------------------------------------------------------------------|------------------------------------------------------| -| partition columns undeclared in the schema are dropped from rows | force-added to rows as non-nullable `string` columns | - -Declare the partition column in the schema to control its type. - -### 33) `flow-php/etl` - `Schema` and `Schema\Definition` mutators return a new instance - -| Before | After | -|-----------------------------------------|------------------------------------------------------| -| `$schema->add(str_schema('x'));` | `$schema = $schema->add(str_schema('x'));` | -| `$schema->addMetadata('id', 'k', 'v');` | `$schema = $schema->addMetadata('id', 'k', 'v');` | -| `$definition->setMetadata($metadata);` | `$definition = $definition->setMetadata($metadata);` | - -Assign the result of every `Schema` mutator - `add`, `addAfter`, `addBefore`, `addMetadata`, `gracefulRemove`, -`insertAt`, `keep`, `makeNullable`, `merge`, `moveAfter`, `moveBefore`, `moveTo`, `prepend`, `remove`, `rename`, -`reorder`, `replace`, `setMetadata`, `sort` - and of `Definition::addMetadata()` / `Definition::setMetadata()`. -Discarding it is a silent no-op. diff --git a/documentation/upgrading-0.41-0.44.md b/documentation/upgrading-0.41-0.44.md new file mode 100644 index 0000000000..e71b0c0b0b --- /dev/null +++ b/documentation/upgrading-0.41-0.44.md @@ -0,0 +1,2193 @@ +# Upgrade Guide 0.41.x - 0.44.x + +[TOC] + +This document provides guidelines for upgrading between versions of Flow PHP. Please follow the instructions for your +specific version to ensure a smooth upgrade process. + +Upgrading from older versions: [0.35.x to 0.41.x](/documentation/upgrading-0.35-0.41.md) + +--- + +## Upgrading from 0.43.x to 0.44.x + +### 1) `flow-php/etl-adapter-json` - `to_json()`/`to_json_lines()` write list/map/structure/array entries as nested JSON + +| Before | After | +|-------------------------------------------------------------------|------------------------------------------------| +| `{"tags":"[1,2,3]"}` (escaped JSON string) | `{"tags":[1,2,3]}` | +| nested `DateTimeInterface`/`Uuid`/`UnitEnum` `json_encode`d as-is | datetime format / canonical string / case name | +| non-array value under a container type -> `""` | `null` | + +### 2) `flow-php/etl` - Floe inferred schema keeps the first batch's nullability + +| Before | After | +|--------------------------------------------------------------------|-------------------------------------------------| +| inferred schema - every column nullable | nullability taken from the first batch's values | +| `null` in batch >= 2 in a column non-nullable in batch 1 - written | throws | + +Columns that may only become null in later batches, declare the schema explicitly: +`to_floe($path)->withSchema($schema)`. + +### 3) `flow-php/types` - `detectType([])` returns `list` + +| Before | After | +|----------------------------------------------------------|--------------| +| `(new TypeDetector())->detectType([])` -> `array` | `list` | + +### 4) `flow-php/types` - `detectType()` returns different types for arrays + +| Before | After | +|----------------------------------------------------------------------------------------------------------|------------------------------------------------| +| `detectType([5 => 'a', 6 => []])` -> `map` | `array` | +| `detectType(['a', []])` -> `list` | `array` | +| `detectType([['id' => '1'], []])` -> `list` | `array` | +| `detectType([[1, 2], [null]])` -> `list>` | `list>` | +| `detectType([[], [1, 2]])` -> `list>` | `list>` | +| `detectType([1 => 1, 5 => 2.5])` -> `array` | `map` | +| `detectType(['name' => 'x', 'latlng' => [33, 65.5]])` -> `structure{name: string, latlng: array}` | `structure{name: string, latlng: list}` | + +### 5) `flow-php/types` - `cast()` refuses values it used to coerce, fabricate or wrap + +| Before | After | +|-----------------------------------------------------------------------------------------------------------------------------------------|---------------------------| +| `type_structure(['id' => type_integer(), 'name' => type_string()])->cast(['id' => 1])` -> `['id' => 1, 'name' => '']` | throws `CastingException` | +| same type, `->cast(['id' => 1, 'name' => null])` -> `['id' => 1, 'name' => '']` | throws `CastingException` | +| same type, `->cast([])`, `->cast(null)` -> `['id' => 0, 'name' => '']` | throws `CastingException` | +| `type_structure(['id' => type_integer()], ['name' => type_string()])->cast(['id' => 1, 'name' => null])` -> `['id' => 1, 'name' => '']` | throws `CastingException` | +| `type_list(type_string())->cast(null)` -> `['']` | throws `CastingException` | +| `type_structure(['id' => type_integer()])->cast('{"id":"1"}')` -> throws | `['id' => 1]` | +| `type_list(type_integer())->cast('["1","2"]')` -> throws | `[1, 2]` | +| `type_integer()->cast('abc')` -> `0` | throws `CastingException` | +| `type_integer()->cast('2024-01-01')` -> `2024` | throws `CastingException` | +| `type_integer()->cast([1, 2, 3])` -> `1` | throws `CastingException` | +| `type_integer()->cast(null)` -> `0` | throws `CastingException` | +| `type_integer()->cast('9223372036854775808')` -> `9223372036854775807`; also `'9223372036854775807.0'`, `type_positive_integer()` | throws `CastingException` | +| `type_integer()->cast(1e300)` -> `0` | throws `CastingException` | +| `type_integer()->cast(new DOMElement('e', 'abc'))` -> `0` | throws `CastingException` | +| `type_float()->cast('abc')` -> `0.0` | throws `CastingException` | +| `type_float()->cast([1])` -> `1.0` | throws `CastingException` | +| `type_float()->cast(null)` -> `0.0` | throws `CastingException` | +| `type_boolean()->cast('weird')` -> `true` | throws `CastingException` | +| `type_boolean()->cast('')` -> `false` | throws `CastingException` | +| `type_boolean()->cast([1, 2, 3])` / `->cast(new DateTimeImmutable())` -> `true` | throws `CastingException` | +| `type_boolean()->cast(null)` -> `false` | throws `CastingException` | +| `type_date()->cast('now')` / `type_datetime()->cast('now')` -> current time; also `''`, `'t'`, `'+12'`, `'yesterday'`, `'10:00:00'` | throws `CastingException` | +| `type_datetime()->cast('@1609459200')` -> `2021-01-01`; also `'2024-01'`, `'2024-001'`, and under `type_date()` | throws `CastingException` | +| `type_string()->cast(null)` / `type_scalar()->cast(null)` -> `''` | throws `CastingException` | +| `type_list(type_string())->cast([null])` -> `['']` | throws `CastingException` | +| `type_array()->cast('abc')` -> `['abc']` | throws `CastingException` | +| `type_list(type_integer())->cast(5)` -> `[5]` | throws `CastingException` | +| `type_array()->cast(null)` -> `[]` | throws `CastingException` | +| `type_integer()->cast(new DateTimeImmutable('@1700000000'))` -> `1700000000000000` | `1700000000` | +| `type_float()->cast(new DateTimeImmutable('@1700000000.5'))` -> `1700000000500000.0` | `1700000000.5` | +| `type_integer()->cast(new DateInterval('PT90S'))` -> `90000000` | `90` | +| `type_float()->cast(new DateInterval('PT90S'))` -> `90000000.0` | `90.0` | + +### 6) `flow-php/etl` - `FilesystemStreams` replaced by `FilesSink`, and a failed run discards its sink + +| Before | After | +|-----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| +| `Flow\ETL\Filesystem\FilesystemStreams` | `Flow\ETL\Filesystem\FilesSink` | +| `FilesystemStreams::FLOW_TMP_FILE_PREFIX` | `FilesSink::FLOW_TMP_FILE_PREFIX` | +| `new FilesystemStreams()` + `->setMode($mode)` | `new FilesSink($filesystem, $destination, $mode)` | +| `$streams->writeTo($filesystem, $path, $partitions)` | `$files->writeTo($partitions)` | +| `$streams->isOpen($path, $partitions)` | `$files->touched($partitions)` | +| `$streams->listOpenStreams($path)` | `$files->openStreams()` | +| `$streams->closeStreams($filesystem, $path)` | `$files->publish()` / `$files->abandon()` | +| `$streams->read()` / `->rm()` / `->exists()` / `count()` / `getIterator()` | removed - use the `Filesystem` directly | +| `saveMode()` on one `DataFrame` applied to every later `DataFrame` on the same `Config` | `saveMode()` is set on the sink and belongs to that sink alone | +| a failed run left its `DestinationStream` registered; the retry appended to it | the failed run's sink is discarded, the retry starts clean | +| an abandoned run left its `._flow_php_tmp.` file behind under `Overwrite` | the abandoned run removes it and never renames it over the destination | +| a failed run left its partial file at the destination under the other save modes | it removes any file it created; a destination it did not create is untouched | +| a run that threw or was abandoned never reached its loaders | loaders implementing `Flow\ETL\Loader\Discardable` receive `discard(FlowContext $context)` | +| `Config::filesystemStreams()` | removed | +| `new Config(..., FilesystemStreams $filesystemStreams, ...)` constructor parameter | removed | + +Build `Config` through `Config::builder()` / `Config::default()`. A `Loader` holding per-run state (an open stream, a +writer, a counter) implements `Discardable` and drops that state in `discard()`. + +### 7) `flow-php/etl` - `RetryLoader` no longer retries `InvalidLogicException` by default + +| Before | After | +|----------------------------------------------------------------------|-----------------------------------------------------------------| +| `new RetryLoader($loader)` default strategy `new AnyThrowable(3)` | `new AnyThrowableExcept([InvalidLogicException::class], 3)` | +| `write_with_retries($loader)` default strategy `new AnyThrowable(3)` | `new AnyThrowableExcept([InvalidLogicException::class], 3)` | +| an `InvalidLogicException` was attempted 4 times with delays between | attempted once, no delay | +| - | `retry_any_throwable_except([InvalidLogicException::class], 3)` | + +To keep retrying every throwable: + +```php +write_with_retries($loader, retry_any_throwable(3)); +``` + +### 8) `flow-php/etl` - operations inside a `Transformation` answer for the whole stream + +| Before | After | +|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `$df->sortBy(ref('id'))` inside a `Transformation` sorted each batch on its own | sorts the whole stream | +| `$df->aggregate(...)` / `groupBy()->aggregate()` / `pivot()` / window functions inside a `Transformation` answered per batch | answer for the whole stream | +| `$df->offset(2)` inside a `Transformation` lost rows | skips exactly the offset across the stream | +| `$df->cache($id)` inside a `Transformation` persisted one batch | persists the whole stream | +| `$df->batchBy(...)` / `batch_size(...)` cut chunks at the incoming batches | cut chunks over the stream | +| `$df->join(...)` inside a `Transformation` emitted rows in input order | emits rows grouped by key | +| `$df->partitionBy(...)` inside a `Transformation` | removed - `$df->repartition(...)` groups the whole stream by key, see 60) | +| a `Transformation` calling `$df->fetch()` / `count()` / `schema()` silently answered over an empty stream | throws `InvalidLogicException` | +| `write_with_retries($loader)` around `to_transformation(...)` (any wrapped step) or around a `to_branch(...)` armed with `withTransformation(...)`, at any nesting depth | throws `InvalidLogicException` at the first `load()`, use `to_transformation(..., write_with_retries($loader))` or `to_branch(..., write_with_retries($loader))->withTransformation(...)` | +| `Flow\ETL\Extractor\SwappableRowsExtractor` | removed | + +### 9) `flow-php/etl` - `to_branch()->withTransformation()` drives its `Transformation` once over the whole stream + +| Before | After | +|-----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------| +| the `Transformation` ran on each filtered batch in its own `DataFrame` | one nested pipeline spans the stream | +| `$df->sortBy(...)` in a branch transformation sorted each batch alone | sorts the whole branch stream | +| `$df->aggregate(...)` / `limit()` / other `Processor`-backed operations answered per batch | answer once for the stream | +| a `Transformation` calling `$df->fetch()` / `count()` / `schema()` returned per-batch answers | throws `InvalidLogicException` | +| the wrapped loader received exactly one `load()` per outer batch | receives output as the transformation produces it; blocking operations deliver at `closure()` | +| telemetry `flow.etl.loading.rows` counted post-filter, post-transformation rows | counts the rows offered to the branch | + +### 10) `flow-php/etl-adapter-doctrine`, `-postgresql` - transactional loaders run `closure()` in a transaction + +| Before | After | +|-------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------| +| `to_dbal_transaction()` / `to_pgsql_transaction()` never called `closure()` on wrapped loaders | forwards `closure()` to every wrapped loader, inside one final transaction | +| blocking operations inside a wrapped `Transformation` answered per batch, each batch in its own transaction | answer for the whole stream, delivered at `closure()` in a single transaction | +| - | a failure during the final transaction rolls back the drained delivery and rethrows | +| `withIsolationLevel()` applied to per-batch transactions | applies to every transaction the wrapper opens | + +### 11) `flow-php/etl` - Floe on-disk format v2, existing `.floe` files must be rewritten + +| Before | After | +|-------------------------------------|-------------------------------------------------| +| header version byte `0x01` | `0x02` | +| uuid payload - 36 raw bytes | 4-byte little-endian length prefix + bytes | +| partition values stored in the file | not stored - read from the directory path only | +| reading a v1 file | throws `Floe does not support format version 1` | + +Files written by 0.43.x cannot be read. Regenerate them from their source, or export them with 0.43.x to another +format before upgrading. + +### 12) `flow-php/etl` - Floe rejects columns whose type is only known per value + +| Before | After | +|--------------------------------------------|------------------------------------------------------------| +| `list` element - written with a tag | `Floe does not support values of type "mixed"` | +| `union_schema()` column - written | `Floe does not support columns of type "integer\|string"` | +| `type_structure(..., allow_extra: true)` | `Floe does not support structures that allow extra values` | +| map key other than `integer`/`string` | `Floe does not support map keys of type "..."` | + +Declare an element type, or declare the column with `json_schema()` when the shape is dynamic. + +### 13) `flow-php/etl` - Floe validates every value against its column type + +| Before | After | +|--------------------------------------------------------------------------------------------------|------------------------------------------------------------| +| `'AB-1'` into an `integer` column - wrote `0` | throws `IncompatibleSchemaException` | +| `1.5` into an `integer` column - wrote `1` | throws | +| `1000` into a `string` column - raw `TypeError` | throws `IncompatibleSchemaException` | +| `int` into a `float` column - written | throws | +| `null` into a non-nullable column - written | throws | +| a batch without a NOT NULL session column - written | throws `IncompatibleSchemaException` | +| a batch without a nullable session column | unchanged, reads back as `null` | +| `IncompatibleSchemaException` - `column "x" (...) is not compatible with the session type (...)` | a `Missing Definitions:` / `Mismatched Definitions:` block | +| `floe_options(validate_data: false)` / `Options::withValidateData()` | removed | +| `floe_options($validate_data, $buffer_size, $codec)` | `floe_options($buffer_size, $codec)` | +| `new Options($validateData, $bufferSize, $codec)` | `new Options($bufferSize, $codec)` | + +### 14) `flow-php/etl` - aggregate result type no longer depends on the value + +| Before | After | +|---------------------------------------------------------------------|---------| +| `sum()` over a `float` column, whole total - `int` | `float` | +| `avg()`/`min()`/`max()` over a `float` column, whole result - `int` | `float` | +| `sum()` over an `integer` column - `int` | `float` | + +### 15) `flow-php/etl` - aggregates ignore a row missing the aggregated column + +| Before | After | +|---------------------------------------------------------------------------------------------------|--------------------------------------------| +| missing column under `$df->mode(execution_strict())` - `Sum error: Entry "amount" does not exist` | contributes nothing | +| missing column in the default mode - contributed nothing | unchanged | +| a column no row declares - contributed nothing | throws `SchemaDefinitionNotFoundException` | + +### 16) `flow-php/types` - `EnumType::isValid()` requires an object + +| Before | After | +|-------------------------------------------|-----------------------------------------------------| +| `type_enum(Suit::class)->isValid('Suit')` | `false` (was `true`) | +| `type_enum(Suit::class)->assert('Suit')` | throws `InvalidTypeException` (was raw `TypeError`) | +| `type_enum(Suit::class)->cast('Suit')` | throws `CastingException` (was raw `TypeError`) | + +### 17) `flow-php/etl` - filesystems are passed to sources and sinks, engine algorithms take a storage + +#### Filesystems + +| Before | After | +|----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------| +| `config_builder()->mount(aws_s3_filesystem(...))` + `from_csv(path('aws-s3://x.csv'))` | `from_csv(path('aws-s3://x.csv'), filesystem: aws_s3_filesystem(...))` | +| `config_builder()->unmount($fs)` | removed | +| `$config->fstab()` | removed - use `Flow\Filesystem\DSL\fstab()` for `file_copy()` / `file_move()` | +| `$context->filesystem($path)` | removed - pass `filesystem:` to the source or sink | +| `$context->streams()->list($path, $filter)` -> `SourceStream` | `(new Flow\Filesystem\FileListing($filesystem))->list($path, $filter)` -> `FileStatus` | +| `$context->streams()->writeTo($path, $partitions)` | `$files->writeTo($partitions)` on a sink-held `FilesSink` - see 6) | +| `$context->streams()->closeStreams($path)` in an extractor | removed | +| `new FilesystemStreams($filesystemTable)` | `new FilesSink($filesystem, $destination, $saveMode)` - see 6) | +| a custom file source reading through `FlowContext` | takes `Filesystem $filesystem = new NativeLocalFilesystem()` as its last constructor argument | +| `schema_from_json_schema($s)` resolved local `$ref`s through `fstab()` | takes a trailing `Filesystem $filesystem = new NativeLocalFilesystem()` | +| a `$ref` on `memory://` or `stdout://` | throws - pass the filesystem that serves it | +| `ChartJSLoader::withOutputPath($path)` / `::withTemplate($path)` | both take a trailing `Filesystem $filesystem = new NativeLocalFilesystem()`; `withOutputPath()` throws on a path with no extension | +| `FilePathArgument::getExisting($input, $config)` / `::getNotExisting(...)` (CLI) | `getExisting($input)` / `getNotExisting($input)`; the constructor takes the `Filesystem` | +| a custom `FileLoader` | implements `saveMode()` and `Discardable` - see 6) | +| a custom `Flow\Filesystem\Filesystem` implementation | adds `public function supports(Path $path): bool` - `return $this->mount()->supports($path);` | + +Two `memory_filesystem()` calls are two separate stores: pass the same instance to the writer and the reader. + +#### Filesystem telemetry + +| Before | After | +|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------| +| `withTelemetry()` traced every filesystem: `filesystem.read` / `filesystem.write` spans and filesystem metrics | filesystems are not traced - wrap the filesystem with `traceable_filesystem()` | +| `telemetry_options(filesystem: filesystem_telemetry_options(...))` | removed | +| `TelemetryOptions::filesystem()` / `->filesystem` | removed | +| the pipeline-start debug log's `fstab` field | a `spill` field naming the three spill storages | + +After: + +```php +$fs = traceable_filesystem( + aws_s3_filesystem($bucket, $client), + filesystem_telemetry_config($telemetry, $clock, filesystem_telemetry_options(trace_streams: true)), +); + +data_frame()->read(from_csv(path('aws-s3://x.csv'), filesystem: $fs))->run(); +``` + +#### Engine algorithms + +| Before | After | +|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------| +| `config_builder()->cacheFilesystem('s3')` | `config_builder()->cache($cache)` | +| `external_sort()->filesystemProtocol('file')` | `external_sort()->storage(new FilesystemBuckets($fs, $path))` | +| `hash_join()->filesystemProtocol(...)` / `hash_group_by()->filesystemProtocol(...)` | `->storage(BucketsStorage $storage)` | +| `CacheConfig::$filesystemMount` | removed | +| `SortAlgorithmBuilder::build(FilesystemTable, Path)` | `build(Path $spillRoot)` | +| `new ExternalSortConfig($bucketing, $runSize)` | `new ExternalSortConfig($bucketing, ?BucketsStorage $merge = null, $runSize)` - use named arguments | +| - | `external_sort()->mergeStorage($s)` - merged runs only | + +#### Per-operation algorithm overrides + +| Before | After | +|--------------------------------------|-------------------------------------------------------------------------------------| +| `$df->sortBy(ref('a'), ref('b'))` | `$df->sortBy([ref('a'), ref('b')])` | +| `$df->groupBy('a', 'b')` | `$df->groupBy(['a', 'b'])` | +| `$df->aggregate(sum(ref('a')), ...)` | `$df->aggregate([sum(ref('a')), ...])` | +| - | `$df->sortBy([ref('a')], external_sort()->storage(new MemoryBuckets()))` | +| - | `$df->groupBy(['a'], hash_group_by()->storage($s))` | +| - | `$df->join($right, $on, Join::left, hash_join()->storage($s))` | +| - | `$df->cache('report', cache: $psrCache)` + `from_cache('report', cache: $psrCache)` | + +`GroupedDataFrame::aggregate()` stays variadic: `$df->groupBy(['a'])->aggregate(sum(ref('b')))`. + +#### Save mode, the CLI and the HTTP bridge + +| Before | After | +|---------------------------------------------------------------------|--------------------------------------------------------------------------------------| +| `$df->saveMode(overwrite())` / `$df->mode(overwrite())` | `to_csv($path)->saveMode(overwrite())` - per sink | +| `$df->saveMode(overwrite())->write(write_with_retries(to_csv($p)))` | `$df->write(write_with_retries(to_csv($p)->saveMode(overwrite())))` | +| `to_text($path)` returned `Loader` | returns `TextLoader` | +| `LoaderFactory::get()` returned `Loader` | returns `Loader&FileLoader` | +| `flow read --config .flow.php aws-s3://bucket/x.csv` | `flow pipeline:run pipeline.php`, with the filesystem built inside the pipeline file | +| `new FlowBufferedResponse(..., filesystem: 'memory')` | `new FlowBufferedResponse(..., filesystem: new MemoryFilesystem())` | +| `new FlowStreamedResponse(..., filesystem: 'stdout')` | `new FlowStreamedResponse(..., filesystem: new StdOutFilesystem())` | +| `Output::loader(Path $path)` | `Output::loader(Path $path, Filesystem $filesystem)` | + +### 18) `flow-php/etl` - `equals()` / `notEquals()` return `null` when either side is `null` + +| Before | After | +|---------------------------------------------------------------------------------|-----------------------------------| +| `ref('a')->equals(ref('b'))`, both `null` - `true`, `filter()` keeps the row | `null`, `filter()` drops the row | +| `ref('a')->equals(lit(5))`, `a` is `null` - `false` | `null` | +| `ref('a')->notEquals(lit(5))`, `a` is `null` - `true`, `filter()` keeps the row | `null`, `filter()` drops the row | +| `ref('a')->same(ref('b'))` / `ref('a')->notSame(lit(5))` | unchanged - `null` same as `null` | + +To keep the old result, switch to `same()` / `notSame()`: + +Before: + +```php +$df->filter(ref('a')->equals(ref('b'))); +$df->filter(ref('a')->notEquals(lit(5))); +``` + +After: + +```php +$df->filter(ref('a')->same(ref('b'))); +$df->filter(ref('a')->notSame(lit(5))); +``` + +`same()` compares with `===`: over an integer column `ref('i')->same(lit(1.0))` is `false` where `equals()` is `true`. + +### 19) `flow-php/etl` - execution modes are removed, a function given bad data throws + +| Before | After | +|--------------------------------------------------------------------------------|-------------------------------------------------------------------------| +| default mode: a function given bad data returned `null` | throws - wrap it: `optional($function)` | +| `$df->mode(execution_lenient())` | removed - `optional($function)` per function | +| `$df->mode(execution_strict())` | removed - strict is the only behaviour | +| `coalesce($a, $b)` skipped a branch that threw | rethrows - `coalesce(optional($a), $b)` | +| `cast($v, type_integer())` over `null` returned `null` | throws - `cast($v, type_optional(type_integer()))` | +| `ref('l')->onEach($fn)` set an element whose `$fn` threw to `null` | throws - `ref('l')->onEach(optional($fn))` | +| `ref('s')->indexOf('a')` / `->indexOfLast('a')` over `null` - `false` | throws - `optional(ref('s')->indexOf('a'))` | +| `DataFrame::mode()` | removed | +| `Flow\ETL\Function\ExecutionMode`, `execution_lenient()`, `execution_strict()` | removed | +| `Flow\ETL\Function\Functions`, `FlowContext::functions()` | removed - a custom function throws instead of calling `invalidResult()` | + +Before: + +```php +data_frame()->read($extractor)->withEntry('payload', ref('json')->jsonDecode())->run(); +``` + +After: + +```php +data_frame()->read($extractor)->withEntry('payload', optional(ref('json')->jsonDecode()))->run(); +``` + +### 20) `flow-php/etl` - `not()`, `all()`, `any()` and `isType()` return `null` for a `null` operand + +| Before | After | +|-----------------------------------------------------------------------------------|-----------------------------------| +| `not(ref('a')->equals(lit(1)))`, `a` is `null` - `true`, `filter()` keeps the row | `null`, `filter()` drops the row | +| `all(ref('a')->equals(lit(1)), lit(true))`, `a` is `null` - `false` | `null` | +| `any(ref('a')->equals(lit(1)), lit(false))`, `a` is `null` - `false` | `null` | +| `ref('a')->isType(type_null())`, `a` is `null` - `true` | `null` - use `ref('a')->isNull()` | + +`isNull()`, `isNotNull()`, `same()`, `notSame()` and `exists()` still return `true` or `false`. + +### 21) `flow-php/etl` - expressions and steps are checked before the first row is read + +| Before | After | +|--------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------| +| `$df->filter(ref('id')->greaterThan(ref('created_at')))`, `integer` vs `datetime` - threw at the first row | throws at `schema()` / `run()`, before any row | +| the same failure under `onError(skip_rows_handler())` - handled per row by the error handler | throws; the error handler is not consulted | +| `ref('a')->plus(ref('d'))` (`integer` + `date`) over a source with no rows - no rows | throws `Cannot combine types "integer", "date" - an explicit cast is required.` | +| `$df->filter(ref('id'))` / `$df->until(ref('id'))` / `to_branch(ref('id'), $loader)` over an `integer` column - accepted | throws - compare explicitly: `ref('id')->notEquals(lit(0))` | +| `greatest(ref('i'), ref('s'))` / `least(...)` over `integer` and `string` - `'apple'` for `5`, `'apple'` | throws `InvalidTypeException` - cast one side | +| `coalesce(ref('i'), ref('s'))` / `when($c, ref('i'), ref('s'))` / `match_cases()` with `integer` and `string` arms - typed per value | `string` column | +| `coalesce(ref('i'), lit(true))` - typed per value | throws `InvalidTypeException` | +| `array_expand(ref('j'))` over a `json` column - typed per value | throws `SchemaNotDerivableException` | +| `ref('u')` / `select('u')` over a column declared `integer\|string` - one type per value | throws `UnsupportedUnionTypeException` - declare one type, e.g. `str_schema('u')` | + +Applies to frames read through `from_data_frame()` and to `to_transformation()` bodies: the inner error fails the +outer `run()` / `schema()`. + +### 22) `flow-php/etl` - functions return only values their column type can hold + +| Before | After | +|--------------------------------------------------------------------------------------|---------------------------------------| +| `ref('d')->toDateTime('Y-m-d')` over an unparseable string - `false` | `null` | +| `ref('j')->jsonDecode()` over scalar JSON (`'1'`) - `1` | throws - `cast()` scalar JSON | +| `ulid()` - `Symfony\Component\Uid\Ulid` | base32 `string` | +| `ref('x')->domElementParent()` of a root element - its `DOMDocument` | `null` | +| `ref('x')->xpath($path)` - every matched node, text and attribute nodes included | elements only, `null` when none match | +| `when($condition, $then, $else)` with `$else` of `0`, `false`, `''` or `[]` - `null` | `$else` | +| `round($v, 0)` / `ref('v')->round(0)` - `int` | `float` | + +### 23) `flow-php/etl` - function arguments that decide the output type take plain values + +| Before | After | +|-----------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------| +| `array_sort($a, $sort_function)` - `ScalarFunction\|Sort\|null` | `?Sort` | +| `array_reverse($a, $preserveKeys)` / `->arrayReverse($preserveKeys)` - `ScalarFunction\|bool` | `bool` | +| `->onEach($fn, $preserveKeys)` - `ScalarFunction\|bool` | `bool` | +| `regex()` / `regex_all()` / `->regex()` / `->regexAll()` `$flags` - `ScalarFunction\|int` | `int`; `PREG_OFFSET_CAPTURE`, `PREG_UNMATCHED_AS_NULL` (and `PREG_SET_ORDER` for `regex_all()`) throw at construction | +| `array_get($ref, $path)` / `->arrayGet($path)` - `ScalarFunction\|string` | `string`; a wildcard path throws - use `array_get_collection()` | +| `array_key_rename($ref, $path, $newName)` - `ScalarFunction\|string` | `string` | +| `uuid_v7()` / `uuid_v7(null)` | `uuid_v7($value)` - `$value` required | +| `->domElementNextSibling()` / `->domElementPreviousSibling()` - next / previous node, text included | next / previous element | +| `->domElementNextSibling(true)` / `->domElementPreviousSibling(true)` | argument removed | +| `ref('a')->isType('nope')` - threw at the first row | throws at construction | +| `new Round($value)` - `$precision` defaults to `0` | defaults to `2`, as `round()` | + +### 24) `flow-php/etl` - `cast()` accepts column types only + +| Before | After | +|----------------------------------------------------------------|---------------------------------------------------| +| `cast($v, 'json_pretty')` | removed - throws at construction; use `'json'` | +| `cast($v, 'object')` / `'mixed'` / `'callable'` / `'resource'` | throws `InvalidArgumentException` at construction | + +### 25) `flow-php/etl` - `EntryReference` is renamed to `UnresolvedReference` and is immutable + +| Before | After | +|---------------------------------------------------------|-----------------------------------------------| +| `Flow\ETL\Row\EntryReference` | renamed to `Flow\ETL\Row\UnresolvedReference` | +| `ref()` / `col()` / `entry()` return `EntryReference` | return `UnresolvedReference` | +| `$ref->as('b')` / `->asc()` / `->desc()` changed `$ref` | return a copy - use the returned reference | + +### 26) `flow-php/etl` - custom functions declare their output type and their children + +| Before | After | +|---------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------| +| `class MyFunction extends ScalarFunctionChain` | `final class MyFunction implements ScalarFunction` + `use ScalarFunctionChain;` | +| `ScalarFunction` - `eval()` | also `returns(): Type`, `children(): array`, `withChildren(array $children): static`, `resolved(): bool` | +| a custom `Reference` | implements the `ScalarFunction` members above - `Reference` extends `ScalarFunction` | +| `AggregatingFunction::result(EntryFactory $entryFactory): Entry` | removed - implement `outputName(): string`, `value(): mixed`, `returns(): Type` and the `FunctionTree` members | +| `WindowFunction` | adds `returns(): Type` and the `FunctionTree` members; `over()` returns a copy | +| `Flow\ETL\Function\ScalarFunction\ScalarResult`, `ScalarResult::from()` | removed - return the plain value, declare the type in `returns()` | +| `FrameAccumulator::value(): mixed` | `value(): float\|int\|null` | +| `Parameter::as*()` over a malformed value - `null` / `false` / `$default` | throws `InvalidArgumentException` | +| `Parameter::asBoolean(): bool` | `?bool` | +| `Parameter::asType()` | removed | + +Before: + +```php +final class Shout extends ScalarFunctionChain +{ + public function __construct(private readonly ScalarFunction $value) {} + + public function eval(Row $row, FlowContext $context): ?string + { + $value = (new Parameter($this->value))->asString($row, $context); + + return $value === null ? null : strtoupper($value); + } +} +``` + +After: + +```php +final class Shout implements ScalarFunction +{ + use ScalarFunctionChain; + use ResolvesFromChildren; + + public function __construct(private readonly ScalarFunction $value) {} + + public function eval(Row $row, FlowContext $context): ?string + { + $value = (new Parameter($this->value))->asString($row, $context); + + return $value === null ? null : strtoupper($value); + } + + public function returns(): Type + { + return type_optional(type_string()); + } + + public function children(): array + { + return [$this->value]; + } + + public function withChildren(array $children): static + { + return new self($children[0]); + } +} +``` + +### 27) `flow-php/etl` - user callbacks removed, `call()` takes a `ScalarFunction` and a return type + +| Before | After | +|----------------------------------------------------------------|--------------------------------------------------------------------------| +| `$df->map($callback)` | removed | +| `Flow\ETL\Transformer\CallbackRowTransformer` | removed | +| `to_callable($callable)` / `Flow\ETL\Loader\CallbackLoader` | removed - implement `Flow\ETL\Loader` | +| `Flow\ETL\Transformer\StyleConverter\ArrayKeyConverter` | removed | +| `call('strtoupper', [ref('name')])` | `call(lit('strtoupper'), type_string(), [ref('name')])` | +| `call($callable, $parameters, $return_type = null)` | `call(lit($callable), $return_type, $parameters)` - return type required | +| `->call($callable, $arguments, $refAlias, $returnType = null)` | `->call(lit($callable), $returnType, $arguments, $refAlias)` | +| `new CallUserFunc($callable, $parameters, $returnType)` | `new CallUserFunc($callable, $returnType, $parameters)` | +| `call(..., type_string())` column - `string` | `?string` | +| `lit($closure)`, or a `Closure` inside a `lit()` array | throws `InvalidArgumentException` | +| `(new TypeDetector())->detectType($closure)` | throws `Flow\Types\Exception\InvalidArgumentException` | + +Before: + +```php +$df->map(fn (Row $row): Row => $row->set(str_entry('name', $row->valueOf('name') ?? 'default'))); +``` + +After: + +```php +$df->withEntry('name', coalesce(ref('name'), lit('default'))); +``` + +Logic no scalar function expresses: implement `Flow\ETL\Transformer` and pass it to `$df->with($transformer)`. + +### 28) `flow-php/etl` - `Transformer` requires `bind()` + +| Before | After | +|------------------------------------------------------------------|---------------------------------------------------------| +| `Transformer::transform(Rows $rows, FlowContext $context): Rows` | plus `bind(Schema $input): Flow\ETL\Pipeline\BoundStep` | + +Return `new BoundStep($this, $output)`, where `$output` is the schema the step emits for `$input`. + +### 29) `flow-php/etl` - `Extractor` gains `schema()` and `withSchema()` + +| Before | After | +|----------------------------------------------------------------------|------------------------------------------------------------------| +| `Extractor` declares only `extract(FlowContext $context): Generator` | also `schema(): Schema` and `withSchema(Schema $schema): static` | +| a custom `Extractor` implementing only `extract()` | fatal at load | + +A source that cannot describe its output before reading throws +`Flow\ETL\Exception\SchemaNotDerivableException::extractor(self::class)` from `schema()`. + +### 30) `flow-php/etl` - `Entry` and the `*_entry()` functions are removed, a row is a name-keyed array under a `Schema` + +| Before | After | +|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| +| `Flow\ETL\Row\Entry` and the 18 `Flow\ETL\Row\Entry\*Entry` classes | removed | +| `Flow\ETL\Row\Entries`, `entries(...)` | removed | +| `row(Entry ...$entries)`, `Row::create(...)`, `Row::with(...)` | `row(array $values)` - `row(['id' => 1])` | +| `rows(Row ...$rows)`, `new Rows(Row ...$rows)` | `rows(Schema $schema, Row ...$rows)`, `new Rows(Schema $schema, Row ...$rows)` - see 31) | +| `$row->get('id')` returned an `Entry` | returns the value | +| `$row->valueOf('id')` | `$row->get('id')` | +| `$row->entries()` | `$row->values()` / `$row->names()` | +| `$row->schema()` | `$rows->schema()` | +| `$row->hash()` | `$row->hash($rows->schema())` | +| `$row->add()` / `set()` / `remove()` / `keep()` / `rename()` / `renameMany()` / `map()` / `merge()` / `isEqual()` / `sortEntries()` | removed | +| `$rows->entries()` | removed | +| `Metadata` per entry, free to differ row to row: `int_entry('id', 1, $metadata)` | one `Metadata` per column: `int_schema('id', metadata: $metadata)` | +| `compare_entries_by_name()` / `compare_entries_by_name_desc()` / `compare_entries_by_type()` / `compare_entries_by_type_desc()` / `compare_entries_by_type_and_name()` | removed - sort a schema: `$schema->sort(schema_sort_by_name())` | +| `Flow\ETL\Transformer\OrderEntries\{Comparator, CombinedComparator, NameComparator, TypeComparator, TypePriorities, Order}` | removed - implement `Flow\ETL\Schema\SortingStrategy::compare(Definition $left, Definition $right): int` | + +Before: + +```php +$rows = rows( + row(int_entry('id', 1), str_entry('name', 'Norbert')), + row(int_entry('id', 2), str_entry('name', null)), +); +``` + +After: + +```php +$rows = rows( + schema(int_schema('id'), str_schema('name', nullable: true)), + row(['id' => 1, 'name' => 'Norbert']), + row(['id' => 2, 'name' => null]), +); +``` + +| Before | Column in `schema(...)` | Value in `row([...])` | +|-------------------------------------------------------------|------------------------------------------|-----------------------------------------------------------------| +| `bool_entry('x', true)`, `boolean_entry('x', true)` | `bool_schema('x')` | `true` | +| `int_entry('x', 1)`, `integer_entry('x', 1)` | `int_schema('x')`, `integer_schema('x')` | `1` | +| `float_entry('x', 1.5)` | `float_schema('x')` | `1.5` | +| `float_entry('x', 1)`, `float_entry('x', '1.5')` | `float_schema('x')` | `type_float()->cast(1)`, `type_float()->cast('1.5')` | +| `str_entry('x', 'a')`, `string_entry('x', 'a')` | `str_schema('x')`, `string_schema('x')` | `'a'` | +| `null_entry('x')` | `null_schema('x')` | `null` | +| `datetime_entry('x', '2024-01-02 10:00:00')` | `datetime_schema('x')` | `type_datetime()->cast('2024-01-02 10:00:00')` | +| `date_entry('x', '2024-01-02')` | `date_schema('x')` | `type_date()->cast('2024-01-02')` | +| `time_entry('x', 'PT1H')` | `time_schema('x')` | `type_time()->cast('PT1H')` | +| `enum_entry('x', Suit::Hearts)` | `enum_schema('x', Suit::class)` | `Suit::Hearts` | +| `json_entry('x', ['a' => 1])`, `json_entry('x', '{"a":1}')` | `json_schema('x')` | `type_json()->cast(['a' => 1])`, `type_json()->cast('{"a":1}')` | +| `json_object_entry('x', [])` | `json_schema('x')` | `Flow\Types\Value\Json::fromArray([], asObject: true)` | +| `uuid_entry('x', $uuid)` | `uuid_schema('x')` | `type_uuid()->cast($uuid)` | +| `xml_entry('x', '')` | `xml_schema('x')` | `type_xml()->cast('')` | +| `xml_element_entry('x', '')` | `xml_element_schema('x')` | `type_xml_element()->cast('')` | +| `html_entry('x', $html)` | `html_schema('x')` | `type_html()->cast($html)` | +| `html_element_entry('x', $html)` | `html_element_schema('x')` | `type_html_element()->cast($html)` | +| `structure_entry('x', $value, $type)`, `struct_entry(...)` | `structure_schema('x', $type)` | `$value` | +| `list_entry('x', $value, $type)` | `list_schema('x', $type)` | `$value` | +| `map_entry('x', $value, $type)` | `map_schema('x', $type)` | `$value` | +| `to_entry('x', $value)` | the `*_schema('x')` matching `$value` | `$value` | +| any `*_entry('x', null)` | `*_schema('x', nullable: true)` | `null` | +| any `*_entry('x', $value, $metadata)` | `*_schema('x', metadata: $metadata)` | `$value` | + +### 31) `flow-php/etl` - `Rows` holds only rows that match its schema + +| Before | After | +|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| any row accepted | `new Rows($schema, ...$rows)` throws `Flow\ETL\Exception\SchemaMismatchException` on a wrong-typed value, a `null` in a NOT NULL column, an undeclared column or a missing NOT NULL column | +| - | `Flow\ETL\Exception\ColumnMismatchException`, the `getPrevious()` of `SchemaMismatchException` | +| a row without a declared nullable column | padded with `null` | +| `$rows->toArray()` - keys in each row's insertion order | schema order | +| `$rows->map()` / `flatMap()` / `each()` / `filter()` / `find()` / `findOne()` / `reduce()` / `sort($callable)` | removed - see below | +| `$rows->sortEntries()` / `$df->reorderEntries()` / `Flow\ETL\Transformer\OrderEntriesTransformer` | removed - `$df->select('a', 'b')` sets the column order | +| `Schema::isSame()` ignored column order | order-sensitive | +| `$rows->merge($other)`, `$other` with other columns or another column order - rows concatenated | throws `InvalidArgumentException`: `Cannot merge Rows with different schemas: [a: integer] and [a: string]` | +| declared NOT NULL column, empty cell or short line (`from_csv($p)->withSchema($schema)`) - `null` | throws `SchemaMismatchException` - declare the column `nullable: true` | +| `from_rows($a, $b)` - each batch kept its own columns | every batch carries every column of the combined schema, missing ones as `null` | +| `batches($extractor, $n)` / `batched_by(...)` over child batches of different shapes - mixed in one batch | later child batches matched to the first: missing nullable column padded, extra column throws `SchemaMismatchException` | + +Before: + +```php +$adults = $rows->filter(fn (Row $row): bool => $row->valueOf('age') >= 18); +``` + +After: + +```php +$adults = new Rows($rows->schema(), ...array_filter($rows->all(), fn (Row $row): bool => $row->get('age') >= 18)); +``` + +### 32) `flow-php/etl` / `flow-php/etl-adapter-excel` - `Entry` parameters become the value and its `Definition` + +| Before | After | +|----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------| +| `Definition::matches(Entry $entry)` | `matches(mixed $value)` - the caller checks the name | +| `Row\Comparator::equals(Row $row, Row $nextRow)` | `equals(Row $row, Row $nextRow, Schema $schema)` | +| `RenameEntryStrategy::rename(Row $row): Row` | `renames(Schema $schema): array` - `current_name => new_name`, `[]` when nothing is renamed | +| `CellStyler::style(Entry $entry, int $rowNumber, int $columnIndex, string $sheetName)` | `style(mixed $value, Definition $definition, int $rowNumber, int $columnIndex, string $sheetName)` | +| `new ASCIIValue($value)` - an `Entry` or a scalar/array | `new ASCIIValue(Type $type, mixed $value)` | +| `Dataset\Statistics\Column::__construct(Entry $entry)` | `__construct(Definition $definition, mixed $value)` | +| `Dataset\Statistics\Column::calculate(Entry $entry)` | `add(Definition $definition, mixed $value)` | +| `Dataset\Statistics\Columns::add(Entry $entry)` | `add(Definition $definition, mixed $value)` | +| `new NullRowBuilder(EntryFactory $entryFactory)` + `->collect($row)` | `new NullRowBuilder(Schema $schema)`, `collect()` removed | +| `new RowsBuffer(int $size)` | `new RowsBuffer(Schema $schema, int $size)` | +| `Rows::joinLeft($right, $on, $entryFactory)` / `Rows::joinRight($right, $on, $entryFactory)` | third argument removed | +| `Flow\ETL\Row\EntryFactory`, `Row\Entry\EntryInstantiator`, `Row\Entry\Instantiators` | removed | +| `EntryTypeResolver::fromDefinition()` | removed | + +### 33) `flow-php/etl` - `Config` options move to the extractors or are removed + +| Before | After | +|--------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| +| `data_frame(config_builder()->putInputIntoRows())->read(from_csv($path))` | `data_frame()->read(from_csv($path)->withMetadataColumns(true))` | +| `ConfigBuilder::putInputIntoRows()` / `dontPutInputIntoRows()`, `Config::shouldPutInputIntoRows()` | removed | +| `config_builder()->extractorBatchSize(500)` | `from_csv($path)->withBatchSize(500)` - on each extractor | +| `Config::extractorBatchSize()` | removed - `$extractor->batchSize()` | +| `FlowContext::entryFactory()` | removed | +| `new Config(...)` with `$filesystemTable`, `$putInputIntoRows`, `$extractorBatchSize`, `$entryFactory` | those parameters removed, `HashRepartitionConfig $repartition` added - build through `Config::builder()` | + +`withMetadataColumns()` is on `from_csv()`, `from_json()`, `from_json_lines()`, `from_parquet()`, `from_text()`, +`from_xml()`, `from_excel()`, `from_google_sheet()`, `from_google_sheet_columns()` and `from_floe()`. + +### 34) `flow-php/etl` - extractors yield batches + +| Before | After | +|--------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| every extractor yielded one row per `Rows` | up to `batchSize()` rows per `Rows`: 100 by default, 1000 for `DbalLimitOffsetExtractor`, `DbalKeySetExtractor`, `PostgreSqlLimitOffsetExtractor`, `PostgreSqlKeySetExtractor` and `PostgreSqlCursorExtractor` | +| `Flow\ETL\Pipeline\Optimizer\BatchSizeOptimization` re-cut batches to 1000 rows before `DbalLoader` / `PostgreSqlLoader` | removed - `$df->batchSize(1000)` before `write()` | +| `new BatchExtractor($extractor, chunkSize: 10)` | `new BatchExtractor($extractor, batchSize: 10)` - a size below 1 throws | +| `$df->duplicateRow(...)` appended the copies after the batch's last row | each copy follows the row it duplicates | + +### 35) `flow-php/etl` - `LimitableExtractor` replaced by `LimitPushDown` + +| Before | After | +|--------------------------------------------------------------------------------|----------------------------------------------------------------------------------| +| `Flow\ETL\Extractor\LimitableExtractor` | `Flow\ETL\Extractor\LimitPushDown` | +| `Flow\ETL\Extractor\Limitable` (trait) | `Flow\ETL\Extractor\PushesLimit` | +| `changeLimit(int $limit): void` | `pushLimit(int $limit): void` - a second push can only lower the limit | +| `limit(): ?int` | `pushedLimit(): ?int` | +| `isLimited(): bool` | removed - `pushedLimit() !== null` | +| `incrementReturnedRows()` / `reachedLimit()` / `resetLimit()` | removed | +| a pushed limit replaced the `limit()` step, so the extractor had to stop at it | the `limit()` step stays; stopping early is optional | +| `new LimitReachedException($limit, $previous)` | `new LimitReachedException($limit, $rows, $previous)` - pass `previous:` by name | + +### 36) `flow-php/etl` - `ErrorHandler` answers per stage: extraction, transformation, loading + +| Before | After | +|---------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------| +| `ErrorHandler::throw(Throwable $error, Rows $rows): bool` | `onExtraction(ExtractionError $error): ExtractionAction` - `propagate` / `endSource` | +| `ErrorHandler::skipRows(Throwable $error, Rows $rows): bool` | `onTransformation(TransformationError $error): TransformationAction` - `propagate` / `skipBatch` | +| - | `onLoading(LoadingError $error): LoadingAction` - `propagate` / `skipLoader` | +| an extractor failure always propagated | goes to `onExtraction()`: `IgnoreError` and `SkipRows` end the source, `ThrowError` rethrows | +| `IgnoreError`: a failed transformation ran the remaining steps on the half-transformed batch | the batch is dropped | +| `SkipRows` / `skip_rows_handler()`: a failed transformation emitted the half-transformed batch | the batch is dropped | +| `SkipRows`: a failed loader was swallowed and the batch's remaining steps skipped | rethrown | +| `SkipRows`: a drain failure of `to_transformation()` / `to_branch()` at `closure()` was swallowed | rethrown | + +The error objects and the action enums are in `Flow\ETL\ErrorHandler`. + +Before: + +```php +final class SkipInvalidBatches implements ErrorHandler +{ + public function throw(Throwable $error, Rows $rows): bool + { + return !$error instanceof InvalidArgumentException; + } + + public function skipRows(Throwable $error, Rows $rows): bool + { + return true; + } +} +``` + +After: + +```php +final class SkipInvalidBatches implements ErrorHandler +{ + public function onExtraction(ExtractionError $error): ExtractionAction + { + return ExtractionAction::propagate; + } + + public function onTransformation(TransformationError $error): TransformationAction + { + return $error->cause instanceof InvalidArgumentException + ? TransformationAction::skipBatch + : TransformationAction::propagate; + } + + public function onLoading(LoadingError $error): LoadingAction + { + return $error->cause instanceof InvalidArgumentException + ? LoadingAction::skipLoader + : LoadingAction::propagate; + } +} +``` + +### 37) `flow-php/etl` - `Cache` gains `schema()` + +| Before | After | +|-----------------------------------------------------|------------------------------------------------------------------------------------------| +| - | `Cache::schema(string $key): Schema`, throws `KeyNotInCacheException` for an unknown key | +| a custom `Flow\ETL\Cache` implementation without it | fatal at load | + +### 38) `flow-php/etl` - data cached or serialized by 0.43.x cannot be read back + +| Before | After | +|-----------------------------------------------------------------------------------------------------------------|----------------| +| `Rows` stored by `FilesystemCache`, `PSRSimpleCache`, `ApcuCache`, `PSRCacheBuckets` or `serialize_to_string()` | cannot be read | + +Clear persistent caches and regenerate stored `serialize_to_string()` payloads after upgrading. + +### 39) `flow-php/etl` - in-memory sources infer one nullable schema from the first 20,480 rows + +| Before | After | +|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------| +| `df()->read(from_array([['id' => 1]]))->schema()` - `id: integer` | `id: ?integer`; the same for `from_memory()` and `from_sequence_*()` | +| `from_array()` / `from_memory()` / `from_sequence_*()` typed each row on its own | columns inferred from the first 20,480 rows | +| a row past the sample whose value the inferred type refuses - kept with its own type | throws `Flow\ETL\Exception\InferredSchemaException` | +| - | `from_array($rows)->inferSchema(infer_schema()->sampleSize(-1))` - infer from every row | +| `from_array([['a' => 1], ['a' => 2, 'b' => 'x']])` - first row has no `b` | first row `'b' => null` | +| `from_array([['a' => 'x'], ['a' => 1000]])` - second row `1000` | `'1000'` - values follow the widened column type | +| `from_array([['tz' => new DateTimeZone('Europe/Warsaw')]])` - `'Europe/Warsaw'`, column `string` | `DateTimeZone` object, column `?timezone` | +| `from_array([['s' => ['j' => null]]])` - `s: structure{j: null}` | `s: ?structure{j: ?string}` | +| `from_array([['o' => new stdClass()]])` - `InvalidArgumentException` on `schema()` and on read | `schema()` gives `o: ?string`; the read throws `SchemaMismatchException` | +| `from_array($generator)` - streamed | read in full into `/flow-php-source/` before the first row; an endless generator never returns | +| `from_array($generator)` read twice - `Cannot traverse an already closed generator` | replayed from the spill file | +| - | `from_array($generator, $schema)` or `->withSchema($schema)` - streamed, no spill, one read only | +| - | `from_array($generator, spillRoot: path($dir))` - default is the filesystem's system tmp dir | +| `from_memory($memory)` read again after `$memory->save()` - saw the new columns | keeps the schema of its first non-empty read; create a new `from_memory($memory)` | + +`InferredSchemaException` is not a `SchemaMismatchException`; the mismatch is its `getPrevious()`. + +### 40) `flow-php/etl` - `Hydrator`, `array_to_row()` and `array_to_rows()` require a `Schema` + +| Before | After | +|-----------------------------------------------------------|---------------------------------------------------------------------------------| +| `Hydrator::cast(array $batch, ?Schema $schema = null)` | removed - use `hydrate($batch, $schema)` | +| `Hydrator::hydrate(array $batch, ?Schema $schema = null)` | `hydrate(array $batch, Schema $schema)` | +| `array_to_row($data, $hydrator, $partitions, $schema)` | `array_to_row($data, $schema, $hydrator, $partitions)` | +| `array_to_rows($data, $hydrator, $partitions, $schema)` | `array_to_rows($data, $schema, $hydrator)` - put partition columns into `$data` | +| `array_to_rows($data)` | `df()->read(from_array($data))->fetch()` | + +### 41) `flow-php/etl` / `flow-php/types` - `autoCast()` removed + +| Before | After | +|--------------------------------------------|---------| +| `DataFrame::autoCast()` | removed | +| `Flow\ETL\Transformer\AutoCastTransformer` | removed | +| `Flow\Types\Type\AutoCaster` | removed | + +Before: + +```php +df()->read(from_csv($path))->autoCast()->fetch(); +df()->read(from_array([['id' => '1', 'total' => '10.5']]))->autoCast()->fetch(); +``` + +After: + +```php +df()->read(from_csv($path))->fetch(); +df()->read(from_array([['id' => '1', 'total' => '10.5']], schema(int_schema('id'), float_schema('total'))))->fetch(); +``` + +`from_array()` types by PHP value: numeric strings stay `?string` unless the schema is declared. + +### 42) `flow-php/etl` - a value a declared column refuses throws `SchemaMismatchException` + +| Before | After | +|-------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------| +| `from_csv($path)->withSchema($schema)`, `'n/a'` in a `datetime` column - throws `Flow\Types\Exception\CastingException` | throws `Flow\ETL\Exception\SchemaMismatchException`: `Rows do not match their schema: column "d" (row 1): could not convert 'n/a' (string) to datetime` | +| `getPrevious()` of the thrown exception | `Flow\ETL\Exception\ColumnMismatchException`, never the `CastingException` | +| `type_datetime()->cast('n/a')` - throws `CastingException` | unchanged | + +Replace `catch (CastingException $e)` around a read with `catch (SchemaMismatchException $e)`. + +### 43) `flow-php/etl` - `DataFrame::schema()` describes the plan without running it + +| Before | After | +|--------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------| +| `$df->schema()` ran the pipeline, loaders included, and merged every batch's schema | derived from the source's `schema()` through each step; no row is read, no loader runs | +| `$df->schema()` on a plan containing `joinEach()` - ran the pipeline | throws `DataDependentSchemaException` | +| `$df->schema()` over a source that cannot describe its schema, also through `from_data_frame($inner)` - ran the pipeline | throws `SchemaNotDerivableException` | +| `schema()` reported `dataFrameCompleted` / `dataFrameFailed` telemetry | reports neither | +| `$df->fetch()` returning no rows, `$df->void()->fetch()` - `Rows` with an empty schema | `Rows` carrying the plan's columns | + +### 44) `flow-php/etl` - `printSchema()` prints the plan's schema and takes no limit + +| Before | After | +|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------| +| `printSchema(?int $limit = 20, SchemaFormatter $formatter = new ASCIISchemaFormatter())` | `printSchema(SchemaFormatter $formatter = new ASCIISchemaFormatter())` | +| `$df->printSchema(20, $formatter)` | `$df->printSchema($formatter)` | +| ran the pipeline, loaders included, printing one schema per batch | prints `$df->schema()` once and runs nothing; throws where `schema()` throws | +| `$df->printSchema(); $df->fetch()` - at most 20 rows | every row | + +### 45) `flow-php/etl` - `display()` and `printRows()` render one table + +| Before | After | +|-------------------------------------------------------------|--------------------------------| +| `$df->display()` / `$df->printRows()` - one table per batch | one table | +| `$df->printRows(null)` streamed the frame batch by batch | collects the whole frame first | + +### 46) `flow-php/etl` - `select()` and `groupBy()` refuse an undeclared column + +| Before | After | +|----------------------------------------------------------------------|------------------------------------------------------------------------------------------------| +| `$df->select('missing')` - added a `missing` string column of `null` | throws `SchemaDefinitionNotFoundException`: `Schema definition for entry "missing" not found.` | +| `$df->groupBy(['missing'])` - grouped every row under a `null` key | throws `SchemaDefinitionNotFoundException` | + +### 47) `flow-php/etl` - renaming onto an existing column throws + +| Before | After | +|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------| +| `$df->rename('a_b', 'ab')` while `ab` exists - `a_b` replaced `ab` | throws `SchemaDefinitionNotUniqueException`: `Entry definitions must be unique, duplicated entries: [ab], all: [ab, ab]` | +| `$df->renameEach(rename_replace('_', ''))` producing an existing name - replaced it | throws `SchemaDefinitionNotUniqueException` | + +### 48) `flow-php/etl` - float values compare exactly + +| Before | After | +|-------------------------------------------------------------------------------------------------------------|-----------------------------------------| +| `$rows->unique()` / `diffLeft()` / `diffRight()` - floats sharing an integer part were equal (`1.5 == 1.9`) | exact: `1.5 != 1.9`, `0.1 + 0.2 != 0.3` | +| `rank()` / `dense_rank()` peers over a float column - same rule | exact | + +### 49) `flow-php/etl` - `Window` and window functions are immutable + +| Before | After | +|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------| +| `$window->partitionBy(...)` / `->orderBy(...)` / `->rowsBetween(...)` changed `$window` | return a copy - use the returned window | +| `$function->over($window)` changed `$function` | returns a copy | +| `window()->partitionBy(ref('a'))->partitions()` - `array` | `References`; `->all()` returns the array | +| `window()->partitionBy(ref('nope'))` - every row in one partition | throws `SchemaDefinitionNotFoundException` before the first row | +| `window()->orderBy(ref('nope'))` - threw per row | throws `SchemaDefinitionNotFoundException` before the first row | + +### 50) `flow-php/etl` - group and join key extraction reads the schema + +| Before | After | +|-----------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| +| `GroupBy::keyValues(Row $row)` | `keyValues(Row $row, Schema $input)` | +| `GroupBy::aggregatedRow(GroupKey $key, Aggregators $aggregators, EntryFactory $entryFactory)` | `aggregatedRow(GroupKey $key, Aggregators $aggregators, Schema $output)` | +| `KeyValues::ofRow(Row $row)` | `ofRow(Row $row, Schema $schema)` | +| `new KeyValues($refs, nullOnMissing: true)` | parameter removed - a missing key is `null` under a nullable column, throws under NOT NULL | +| `new HashBucketing(..., nullOnMissing: true)` | parameter removed | +| `join()` over a row without the join column - threw | the row is unmatched | + +### 51) `flow-php/etl` - a join producing a duplicate column throws `SchemaDefinitionNotUniqueException` + +| Before | After | +|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------| +| `$df->join($right, join_on(['id' => 'id']))`, both sides carry `name` - `JoinException` (`RuntimeException`): `Merged entries names must be unique, given: [id, name] + [name] try to use a different join prefix than: ""` | `SchemaDefinitionNotUniqueException` (`InvalidArgumentException`): `Entry definitions must be unique, duplicated entries: [name], all: [id, name, name]` | +| thrown when the first pair of rows was merged | thrown when the pipeline starts, before any row is read | +| `$rows->joinCross($right, '')` over shared columns - `InvalidArgumentException`: `... + [id, name]. Please consider using join prefix option` | `SchemaDefinitionNotUniqueException`: `Entry definitions must be unique, duplicated entries: [id, name], all: [id, name, id, name]` | +| `catch (JoinException $e)` around a join | `catch (SchemaDefinitionNotUniqueException $e)`, or give the right side a prefix: `join_on(['id' => 'id'], 'joined_')` | + +### 52) `flow-php/etl` - `Joiner` takes `JoinSide` and no `EntryFactory`, `Expression` drop helpers removed + +| Before | After | +|----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------| +| `Flow\ETL\Join\Expression::dropDuplicateLeftEntries()` / `dropDuplicateRightEntries()` | removed | +| `$joiner->join($left, $right, $nullLeftRow, $nullRightRow, $buildLeft)` | `$joiner->join(JoinSide::of($left, $nullLeftRow, $leftSchema), JoinSide::of($right, $nullRightRow, $rightSchema), $buildLeft)` | +| `new Joiner($expression, $type, $entryFactory, $batchSize)` | `new Joiner($expression, $type, $batchSize)` | + +### 53) `flow-php/etl` - a join against an empty side keeps that side's columns + +| Before | After | +|-------------------------------------------------------------------------------------------|----------------------------------------| +| `$df->join($emptyRight, $on, Join::left)` - right-hand columns dropped | kept, `null` | +| `$df->join($right, $on, Join::right)` with an empty left side - left-hand columns dropped | kept, `null` | +| `$rows->joinCross($empty)` / `$empty->joinCross($rows)` - the non-empty side's rows | zero rows carrying both sides' columns | + +### 54) `flow-php/etl` - a global `aggregate()` over zero rows returns one row + +| Before | After | +|--------------------------------------------------------------------------------------------------------------------|----------------------------------------------| +| `$df->aggregate(sum(ref('a')), count(ref('a')))` over zero rows, or after a `filter()` removed every row - no rows | one row, `['a_sum' => null, 'a_count' => 0]` | +| `collect()` / `collect_unique()` / `string_agg()` over zero rows - no rows | `[]` / `[]` / `''` | +| `$df->groupBy('k')->aggregate(...)` over zero rows - no rows | unchanged | + +### 55) `flow-php/etl` - `pivot()` takes its values + +Before: + +```php +$df->groupBy('date')->pivot(ref('product'))->aggregate(sum(ref('amount'))); +``` + +After: + +```php +$df->groupBy(['date'])->pivot(ref('product'), pivot_values('A', 'B'))->aggregate(sum(ref('amount'))); +// or +$df->groupBy(['date'])->pivot(ref('product'), discover_pivot_values())->aggregate(sum(ref('amount'))); +``` + +| Before | After | +|------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------| +| `GroupedDataFrame::pivot(Reference $ref)` | `pivot(Reference $ref, PivotValues $values)` | +| `GroupBy::pivot(Reference $ref)` | `GroupBy::pivot(Reference $ref, DeclaredPivotValues $values)` | +| one column per value found in the data | `pivot_values(...)`: one per declared value, `null` where no row carries it, undeclared values dropped | +| - | `discover_pivot_values()`: one per non-null value in the frame, sorted, at most `maxValues` (default `10_000`) | +| pivot value `0` or `'0'` - column `e00` | column `0` | +| pivot value `7` - column `e07` | column `7` | +| pivot value equal to a group-by column name - its column dropped | throws `InvalidArgumentException` | +| `GroupBy::pivotResult($rows, $context, $batchSize)` | removed - `(new PivotAggregation($batchSize))->aggregate($rows, $context, $groupBy)` | + +A custom extractor read by `discover_pivot_values()` must implement `Flow\ETL\Extractor\RewindableExtractor`. + +### 56) `flow-php/etl` - `unpack()` / `array_unpack()` take the `Schema` of the columns they produce + +Before: + +```php +$df->withEntry('row', ref('row')->unpack(['internal_id'])); +``` + +After: + +```php +$df->withEntry('row', ref('row')->unpack(schema(int_schema('id'), str_schema('name')))); +``` + +| Before | After | +|----------------------------------------------------------------|------------------------------------------------------| +| `ref('x')->unpack($skipKeys, $entryPrefix)` | `ref('x')->unpack(Schema $schema)` | +| `array_unpack($array, $skip_keys, $entry_prefix)` | `array_unpack($array, Schema $schema)` | +| every payload key except `$skipKeys` became a column | only the declared columns; other keys are dropped | +| column type inferred from each value | the declared type, nullable | +| a declared key missing from one payload - no entry in that row | `null` | +| `$entryPrefix` prepended to each key | removed - columns are named `.` | + +A custom `Flow\ETL\Function\ScalarFunction\UnpackResults` function must return a `StructureType` of its columns from +`returns()`. + +### 57) `flow-php/etl` - `on_each()` requires an operand that declares its element type + +| Before | After | +|------------------------------------------------------------------------|------------------------------------------------------------------------------------| +| `ref('a')->onEach(ref('element'))` with `a` = `[1, 'x']` -> `[1, 'x']` | throws `SchemaNotDerivableException` | +| - | `ref('a')->cast(type_list(type_string()))->onEach(ref('element'))` -> `['1', 'x']` | + +### 58) `flow-php/etl` - `isIn()` compares numbers loosely and refuses incomparable types + +| Before | After | +|--------------------------------------------------------------------------------------------|-------------------------------------------| +| `ref('a')->isIn(lit([1]))` with `a` = `'1'` - `false` | `true`, as `ref('a')->equals(lit(1))` | +| `ref('a')->isIn(lit([new DateTimeImmutable('2024-01-01')]))` with `a` a `string` - `false` | throws `Can't compare '(string == date)'` | +| `ref('a')->isIn(lit([1, 2]))` with `a` = `null` - `false` | `null`, `filter()` drops the row | + +### 59) `flow-php/etl` - `Calculator::divide()` always returns `float` + +| Before | After | +|-------------------------------------------|-------| +| `(new Calculator())->divide(4, 2)` - `2` | `2.0` | +| `ref('a')->divide(lit(2))` over `4` - `2` | `2.0` | + +### 60) `flow-php/etl` - `DataFrame::partitionBy()` removed, loaders partition, `Rows` carry no partitions + +| Before | After | +|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------| +| `$df->partitionBy('year')->write(to_csv($path))` | `$df->write(to_csv($path)->partitionBy(partition_by('year')))` | +| partitioned write - the partition column is written into the file body as well | path only; `partition_by('year')->writeColumns()` keeps it in the body | +| `$df->partitionBy('year')` without a partitioned write | `$df->repartition('year')` | +| `$df->dropPartitions()` | removed | +| `$df->dropPartitions(true)` | `$df->drop('year')` | +| `Rows::partitioned()` / `rows_partitioned()` | removed | +| `Rows::partitionBy()` / `partitions()` / `isPartitioned()` / `dropPartitions()` | removed | +| `Flow\ETL\Transformer\DropPartitionsTransformer` | removed | +| reading `year=2024/month=01/` - partition columns appended in path order (`year`, `month`) | appended in name order (`month`, `year`) | +| reading `year=__HIVE_DEFAULT_PARTITION__/` - `year` is the string `'__HIVE_DEFAULT_PARTITION__'` | `null`, and `year` is nullable | +| same read with `withSchema()` declaring `year` NOT NULL - the string | throws `InvalidArgumentException` | +| reading `year=abc/` with `withSchema()` declaring `year` as `integer` - `0` | throws `InvalidArgumentException` naming the column, file, type and value | +| `display()` / `printRows()` / `to_output()` printed a `Partitions:` footer | no footer | + +### 61) `flow-php/etl` - `filterPartitions()` compares the raw string partition value + +| Before | After | +|---------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------| +| `filterPartitions(ref('date')->equals(lit(new DateTimeImmutable('2024-01-01'))))` - matched `date=2024-01-01` | throws `Can't compare '(string == date)'`; use `lit('2024-01-01')`, or `from_csv($path)->partitionTypes(partition_types(date: type_date()))` | +| `filterPartitions(ref('active')->equals(lit(false)))` - matched `active=false` | throws `Can't compare '(string == boolean)'`; use `lit('false')`, or `partition_types(active: type_boolean())` | +| `filterPartitions(ref('date')->equals(lit('2024-01-01')))` - throws `Can't compare '(date == string)'` | matches `date=2024-01-01` | +| a `DateTimeImmutable` or `bool` literal over `files()` / `from_path_partitions()` - matched | matches nothing; compare a string literal | +| `new ScalarFunctionFilter($function, $entryFactory, $caster, $context)` | `new ScalarFunctionFilter($function, Schema $partitions, $context)` | + +### 62) `flow-php/etl` / `flow-php/types` - missing-column and type-mismatch messages changed + +| Before | After | +|------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------| +| `Schema definition for entry "x" not found` | `Schema definition for entry "x" not found.`, plus ` Did you mean one of: [a]?` when up to 3 column names are close | +| `$row->get('x')`: `Entry "x" does not exist. Did you mean one of the following? ["every", "column"]` | `Column "x" does not exist.`, plus ` Did you mean one of the following? ["a"]` when up to 3 column names are close | +| `Can't compare '(?integer > ?date)' due to data type mismatch.` | `Can't compare '(?integer > ?date)' due to data type mismatch - an explicit cast is required.` | + +### 63) `flow-php/etl` - pipeline and generator extractors and `Schema::fromPipeline()` removed + +| Before | After | +|-----------------------------------------------------------------|----------------------------------| +| `from_pipeline($pipeline)` / `new PipelineExtractor($pipeline)` | removed - `from_data_frame($df)` | +| `Flow\ETL\Extractor\GeneratorExtractor` | removed | +| `Schema::fromPipeline($pipeline, $context, $maxRows)` | removed - `$df->schema()` | + +### 64) `flow-php/etl` - `until()` stops at the first row failing its predicate + +| Before | After | +|-----------------------------------------------------------------------------------------------|----------| +| `$df->until(ref('v')->lessThan(lit(3)))` over `[1, 2, 5, 1, 2]` in one batch - `[1, 2, 1, 2]` | `[1, 2]` | + +### 65) `flow-php/etl` - `UnserializeTransformer` requires the payload schema + +| Before | After | +|-------------------------------------------------------------------------------------------|----------------------------------------------------------------------| +| `new UnserializeTransformer($source, $merge, $mergePrefix)` | `new UnserializeTransformer($source, $schema, $merge, $mergePrefix)` | +| payload missing, not a string, not unserializable or not one row - row returned unchanged | declared payload columns added as `null` | + +### 66) `flow-php/etl` - Floe reader drops `RowPadding`, `conform:` and `lenient:` + +| Before | After | +|-----------------------------------------------------------------|---------------------------------------------------------------| +| `Flow\Floe\RowPadding` | removed | +| `FloeStreamReader::rows($batchSize, $offset, $limit, $conform)` | `rows($batchSize, $offset, $limit)` | +| `FrameReader::frames(lenient: true)` | `frames()` - a truncated stream always throws `FloeException` | + +### 67) `flow-php/etl` / `flow-php/etl-adapter-parquet` - a Floe or Parquet glob checks every file against the first + +| Before | After | +|-----------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| +| `from_parquet($glob)` / `from_floe($glob)`, a later file with other columns or column types - rows kept that file's columns | throws `InferredSchemaException` naming both files - read with `->unionByName()` or `->withSchema(...)` | +| same read, a later file with the same columns in another order - rows kept that file's order | rows follow the first file's column order | +| - | `->unionByName()` reads every file under the merged schema; a column a file lacks is `null` | + +### 68) `flow-php/etl` and every package with DSL functions - documentation attributes leave the packages + +| Before | After | +|-------------------------------------------|-----------------------------------------------------| +| `Flow\ETL\Attribute\DocumentationDSL` | `Flow\Documentation\Attribute\DocumentationDSL` | +| `Flow\ETL\Attribute\DocumentationExample` | `Flow\Documentation\Attribute\DocumentationExample` | +| `Flow\ETL\Attribute\Module` | `Flow\Documentation\Attribute\Module` | +| `Flow\ETL\Attribute\Type` | `Flow\Documentation\Attribute\Type` | + +No installable package ships `Flow\Documentation\Attribute\*`: `ReflectionAttribute::newInstance()` on them throws +`Attribute class "..." not found`. Read them with `getName()` / `getArguments()`. + +### 69) `flow-php/etl` / `flow-php/types` - `TypeMerge` moved to `flow-php/types` as `TypeWidener` + +| Before | After | +|------------------------------------------------------|------------------------------------------------------| +| `Flow\ETL\Schema\Definition\TypeMerge` | `Flow\Types\Type\TypeWidener` | +| `merge($left, $right)` | `widen($left, $right)` | +| `mergeLists()` / `mergeMaps()` / `mergeStructures()` | `widenLists()` / `widenMaps()` / `widenStructures()` | + +### 70) `flow-php/types` - structure optional fields are declared inline with `structure_element()` + +| Before | After | +|----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------| +| `type_structure(['id' => type_integer()], ['nick' => type_string()])` | `type_structure(['id' => type_integer(), 'nick' => structure_element('nick', type_string(), optional: true)])` | +| `type_structure($elements, $optionalElements, true)` | `type_structure($elements, true)` - an array second argument throws `TypeError` | +| `new StructureType($elements, $optionalElements, $allowExtra)` | `StructureType::fromElements($elements, $allowExtra)` | +| `StructureType::elements()` - `array`, required fields only | `list`, optional fields included | +| `$structure->elements()['id']` | `$structure->element('id')?->type` | +| `StructureType::optionalElements()` | removed - read `StructureElement::$optional` | +| `normalize()` - `{"type": "structure", "elements": {...}, "optional_elements": {...}}` | `{"type": "structure_v2", "fields": [{"name": ..., "type": ..., "optional": ...}]}` | +| `type_from_array()` / `schema_from_json()` on a `structure` payload | throws `InvalidArgumentException: Unknown type 'structure'` | + +Regenerate any stored `schema_to_json()` output that contains a structure. + +### 71) `flow-php/types` - `time`, `date`, `string` and `Json` accept and refuse different values + +| Before | After | +|---------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------| +| `type_time()->isValid(new DateInterval('P1M'))` - `true` | `false` | +| `type_time()->cast(new DateInterval('P1M'))` - returned as-is | throws `CastingException`: `... Relative DateInterval (with months/years) can't be cast to time` | +| `type_time()->cast('12:34:56')` - throws `CastingException` | `DateInterval` of 12h 34m 56s | +| `type_time()->cast('12:34:56.5')` - throws `CastingException` | `DateInterval` with `f` = `0.5` | +| `type_union(type_time(), type_integer())->cast('12:34:56')` - `12` | `DateInterval` | +| `type_date()->isValid(new DateTimeImmutable('2024-01-02 00:00:00.5'))` - `true` | `false` | +| `Json::fromArray([1 => 'a'], asObject: true)` - `{"1":"a"}` | throws `InvalidArgumentException`: `All keys of a JSON object must be strings` | +| `type_string()->cast(new DateInterval('PT1H2M3S'))` - throws `CastingException` | `'01:02:03'`; `P1DT2H` -> `'26:00:00'`; a fraction adds `.uuuuuu` | + +### 72) `flow-php/types` - `StringTypeNarrower` types compact digit dates as `integer` and month-only dates as `string` + +| Before | After | +|------------------------------------------------------------|-----------| +| `(new StringTypeNarrower())->narrow('20240101')` -> `date` | `integer` | +| `(new StringTypeNarrower())->narrow('2024-01')` -> `date` | `string` | + +### 73) `flow-php/filesystem` - `Partition` takes a value and its type, encodes reserved characters and allows `null` + +| Before | After | +|------------------------------------------------------------------------------------------------|------------------------------------------------------------------------| +| `Partition::valueFromRow(Reference $ref, Row $row)` | `Partition::fromValue(string $name, Type $type, mixed $value): string` | +| `$partition->reference()` | removed | +| `Flow\ETL\Row\Entry\JsonEntry can't be used as a partition` | `Column "d" of type json can't be used as a partition` | +| `Partition::$value` - `string` | `?string` | +| `new Partition('path', 'a/b')` - throws `Partition value contains one of forbidden characters` | `->segment()` - `path=a%2Fb` | +| `new Partition('a/b', '1')` - throws `Partition name contains one of forbidden characters` | `->segment()` - `a%2Fb=1` | +| value `New York` written to `city=New York/` | `city=New%20York/` | +| `null` partition value on write - throws `Partition value can't be empty` | `year=__HIVE_DEFAULT_PARTITION__/` | +| `new Partitions(new Partition('a', '1'), new Partition('a', '2'))` | throws `InvalidArgumentException` | +| `Partition` / `Partitions` throw `Flow\ETL\Exception\InvalidArgumentException` | throw `Flow\Filesystem\Exception\InvalidArgumentException` | + +### 74) `flow-php/filesystem` - `SourceStream::readLines()` strips the separator and yields nothing for 0 bytes + +| Before | After | +|-----------------------------------------------------------------------------------------------------|--------------------| +| `memory_filesystem()` stream: each line kept its trailing `"\n"` | separator stripped | +| `memory_filesystem()` stream: `$separator` ignored | honoured | +| `memory_filesystem()` stream: 0 bytes yielded one `''` | yields nothing | +| `StringSourceStream` / `MemorySourceStream` dropped empty lines | yield them as `''` | +| `from_text()` over an empty `memory://` file - one empty row | no rows | +| `from_json_lines()` over an empty `memory://` file - threw | no rows | +| `from_csv()->withCharactersReadInLine($n)` on `memory://` - a line longer than `$n` split into rows | one row per line | + +A custom `SourceStream::readLines()` must strip the separator, yield nothing for a 0-byte stream and yield an empty +line as `''`. + +### 75) `flow-php/parquet` - FLOAT32 values read without `ext-arrow` are no longer rounded to 7 decimals + +| FLOAT32 value written | Before (read) | After (read) | +|-----------------------|---------------|-----------------------| +| `0.1` | `0.1` | `0.10000000149011612` | +| `18.52` | `18.5200005` | `18.520000457763672` | +| `1.0E-8` | `0.0` | `9.99999993922529E-9` | + +### 76) `flow-php/parquet` - `FlatColumn::decimal()` sizes `FIXED_LEN_BYTE_ARRAY` by the Parquet spec + +| `FlatColumn::decimal($name, $precision)` | Before (bytes) | After (bytes) | +|------------------------------------------|----------------|---------------| +| precision 7 | 3 | 4 | +| precision 12 | 5 | 6 | +| precision 19 | 8 | 9 | +| precision 24 | 10 | 11 | +| precision 36 | 15 | 16 | + +### 77) `flow-php/postgresql` - `Client` gains `describe()`, and `execute()` accepts `ConvertedParameters` + +| Before | After | +|----------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------| +| a custom `Flow\PostgreSql\Client\Client` implementation | adds `describe(Sql\|string $sql, array $parameters = []): array` returning `list` | +| `execute(Sql\|string $sql, array $parameters = []): int` | `execute(Sql\|string $sql, array\|ConvertedParameters $parameters = []): int` | + +A custom `Client::execute()` binds `ConvertedParameters::$values` as they are, without a converter. + +### 78) `flow-php/postgresql` - arrays parsed, `oid` as integer, `timetz` without offset, duplicates cast by name + +| Before | After | +|-----------------------------------------------------------|-------------------------------------------------------------------------------| +| `ResultCaster::cast()` returns `bool\|float\|int\|string` | returns `array\|bool\|float\|int\|string` | +| `_int4` value `'{1,NULL,3}'` | `[1, null, 3]` - every `_`-prefixed array type, elements cast by element type | +| `oid` value `'42'` | `42` | +| `timetz` value `'12:34:56+02'` | `'12:34:56'` | +| `SELECT 1::int8 AS a, 'x' AS a` - `a` is `0` | `'x'` | +| `SELECT 'x' AS a, 1::int8 AS a` - `a` is `'1'` | `1` | + +Applies to rows returned by `Client::fetch*()` and `Client::cursor()`. + +### 79) `flow-php/doctrine-dbal-bulk` - `Dialect` requires `maxBindParameters()` + +| Before | After | +|--------------------------------------------------------------|-----------------------------------------------------------------------------------------------------| +| a custom `Flow\Doctrine\Bulk\Dialect\Dialect` implementation | adds `public function maxBindParameters(): int` - the platform's bind-parameter limit, at least `1` | + +### 80) `flow-php/etl-adapter-csv`, `-json`, `-excel`, `-google-sheet` - a read without a schema infers one schema + +| | Before | After | +|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------| +| schema of a read without `withSchema()` | each yielded `Rows` typed from its own values | one schema for the whole read, inferred from a sample | +| nullability of an inferred column | nullable only where that `Rows` held a `null` | always nullable (`?integer`, `?string`, ...) | +| sample | - | first 20,480 rows over the first 10 files; Google Sheet: first 100 rows | +| value past the sample that the inferred type refuses | read, typed on its own | throws `Flow\ETL\Exception\SchemaMismatchException` | +| CSV/Excel file or Google Sheet whose header differs from the inferred one (header-only files too) | read with its own columns | throws `Flow\ETL\Exception\InferredSchemaException` | +| row key order | the row's own; a body column named like a partition is overwritten in place | the schema's: body columns (JSON: first seen across the sample), then partition columns | +| `flow file:schema` over CSV / Excel | every column `string`, `nullable: false` unless a value was empty | typed, every column `nullable: true` | +| infer from every row | - | `->inferSchema(infer_schema()->sampleSize(-1))` | +| infer from every file | - | `->inferSchema(infer_schema()->filesToSniff(-1))` | +| every column `?string` | - | `->inferSchema(infer_schema()->allStrings())` | +| files with different columns | - | `->inferSchema(infer_schema()->unionByName())` | +| no inference | - | `->withSchema($schema)` | + +On `from_google_sheet()`, `->inferSchema(infer_schema())` without `->sampleSize()` samples 20,480 rows, not 100. + +### 81) `flow-php/etl-adapter-csv` - cells are typed, not strings + +| Cell | Before | After | +|--------------------------------------|----------|-----------------------| +| `1` | `string` | `?integer` | +| `1.5` | `string` | `?float` | +| `true` / `false` | `string` | `?boolean` | +| `20240101` | `string` | `?integer` | +| `2024-01-01` / `2024-01-01 10:00:00` | `string` | `?date` / `?datetime` | +| uuid / JSON text | `string` | `?uuid` / `?json` | +| `01234`, `2024-01`, ` 12 ` (padded) | `string` | `?string` | + +Keep every column a string: `from_csv($path)->inferSchema(infer_schema()->allStrings())`. + +### 82) `flow-php/etl-adapter-doctrine` - `from_dbal_*()` type columns from the query + +| Driver | Column | Before | After | +|-------------------------------------|---------------------------------------------------------------------------------|---------------|-------------------------------------------------------------------------------------------------| +| `pgsql` | `NUMERIC` | `string` | `float` | +| `pgsql` | `DATE`, `TIMESTAMP`, `TIMESTAMPTZ` | `string` | `DateTimeImmutable` | +| `pgsql` | `TIME` | `string` | `DateInterval` | +| `pgsql` | `UUID` | `string` | `Flow\Types\Value\Uuid` | +| `pgsql` | `JSON`, `JSONB` | `string` | `Flow\Types\Value\Json` | +| `pgsql` | `XML` | `string` | `DOMDocument` | +| `pgsql` | any other type (`INTERVAL`, `INET`, `MONEY`, `OID`, arrays, ranges, enums, ...) | `string` | throws `SchemaNotDerivableException` | +| `mysqli` | `DECIMAL` | `string` | `float` | +| `mysqli` | `DATE`, `DATETIME`, `TIMESTAMP` | `string` | `DateTimeImmutable` | +| `mysqli` | `TIME` | `string` | `DateInterval` | +| `mysqli` | `JSON` | `string` | `Flow\Types\Value\Json` | +| `mysqli` | any other type (`BIT`, `GEOMETRY`, ...) | `string` | throws `SchemaNotDerivableException` | +| `sqlite3`, `pdo_sqlite` | `INTEGER`, `REAL` | `1`, `1.5` | `'1'`, `'1.5'` - every column is `?string` | +| `pdo_pgsql`, `pdo_mysql`, any other | every column | driver values | throws `SchemaNotDerivableException` - use the `pgsql` / `mysqli` driver or `->withSchema(...)` | + +Applies to `from_dbal_query()`, `from_dbal_queries()`, `from_dbal_limit_offset()`, `from_dbal_limit_offset_qb()` and +`from_dbal_key_set_qb()` without `->withSchema()`. A query the driver cannot describe (multi-statement, data-modifying +CTE, `INSERT ... RETURNING`) also throws. Declare `->withSchema(...)` to pick the types. + +### 83) `flow-php/etl-adapter-doctrine`, `-postgresql` - `withPageSize()` / `withFetchSize()` become `withBatchSize()` + +| Before | After | +|-------------------------------------------------------------------------------------------------------|----------------------------------------------| +| `from_pgsql_cursor(...)->withFetchSize(500)` | `from_pgsql_cursor(...)->withBatchSize(500)` | +| `from_pgsql_limit_offset(...)->withPageSize(500)` | `->withBatchSize(500)` | +| `from_pgsql_key_set(...)->withPageSize(500)` | `->withBatchSize(500)` | +| `from_dbal_limit_offset(...)->withPageSize(500)`, `from_dbal_limit_offset_qb(...)->withPageSize(500)` | `->withBatchSize(500)` | +| `from_dbal_key_set_qb(...)->withPageSize(500)` | `->withBatchSize(500)` | +| `Page size must be greater than 0, got 0` / `Fetch size must be greater than 0, got 0` | `Batch size must be greater than 0, got 0` | + +### 84) `flow-php/etl-adapter-excel` - text cells narrow to uuid, json and timezone, `to_excel()` writes date cells + +| | Before | After | +|-------------------------------------------------------------|----------------------------------------------------------|-----------------------------------------------------------------------------| +| text cell holding a uuid / JSON / timezone name | `string` | `?uuid` / `?json` / `?timezone` | +| any other text cell (`TRUE`, `12.9`, `2023-10-02`) | `string` | `?string` | +| blank cell under `withConvertEmptyToNull(false)` | `''` in that row | the whole column is `?string` (`1` reads as `'1'`) | +| `to_excel()` date / datetime value | text cell | date cell | +| `to_excel()->withDateFormat()` / `->withDateTimeFormat()` | PHP `date()` format, default `'Y-m-d'` / `'Y-m-d H:i:s'` | Excel number format, default `'yyyy-mm-dd'` / `'yyyy-mm-dd hh:mm:ss'` | +| `to_excel()` then `from_excel()`, date / datetime column | `string` | `?date` / `?datetime` | +| ZIP-signed file without an extension that is not a workbook | `ValueError: Invalid or uninitialized Zip object` | `Flow\ETL\Exception\InvalidArgumentException: Unsupported file format: n/a` | + +Before: + +```php +to_excel($path)->withDateFormat('Y-m-d')->withDateTimeFormat('Y-m-d H:i:s'); +``` + +After: + +```php +to_excel($path)->withDateFormat('yyyy-mm-dd')->withDateTimeFormat('yyyy-mm-dd hh:mm:ss'); +``` + +### 85) `flow-php/etl-adapter-excel` - requires `openspout/openspout` `~5.3.0` + +| Dependency | Before | After | +|-----------------------|--------|----------| +| `openspout/openspout` | `^5.2` | `~5.3.0` | + +### 86) `flow-php/etl-adapter-google-sheet` - `FORMATTED_VALUE` cells are typed, and `''` reads as `null` + +| | Before | After | +|--------------------------------------------|----------|---------------------------------------------------------------------------------------------------------| +| cell under `FORMATTED_VALUE` (the default) | `string` | typed as a CSV cell, see 81): `'1234'` -> `?integer`, `'TRUE'` -> `?boolean`, `'2024-01-01'` -> `?date` | +| `''` cell | `''` | `null`; keep `''` with `->withEmptyToNull(false)` | + +### 87) `flow-php/etl-adapter-http` - `response_body` and `request_body` hold the raw body text + +| Before | After | +|-----------------------------------------------------------------------------|--------------| +| `response_body` / `request_body` of a JSON or XML message - decoded `array` | `?string` | +| a JSON body that fails to decode - threw `RuntimeException` | kept as text | + +Decode in the pipeline: `->withEntry('response_body', ref('response_body')->jsonDecode())`. + +### 88) `flow-php/etl-adapter-json` - `schema_from_json_schema()` rejects a property with more than one non-null type + +| Before | After | +|------------------------------------------------------------------------|-----------------------------------------------------------| +| `anyOf` / `oneOf` of `string` and `integer` - `integer\|string` column | throws `Flow\ETL\Exception\UnsupportedUnionTypeException` | +| `enum: ["a", 1]` - `integer\|string` column | throws `UnsupportedUnionTypeException` | +| `type: ["string", "integer"]` - `integer\|string` column | throws `UnsupportedUnionTypeException` | +| `anyOf` of `string` and `null` - `?string` | unchanged | + +Give the property a single type in the JSON Schema, or declare the column by hand: `str_schema('v')`, +`json_schema('v')`. + +### 89) `flow-php/etl-adapter-json` - keys outside the sample are dropped, missing keys are `null` + +| | Before | After | +|--------------------------------------------------------|-----------------------------------------------------|-----------------------------------------------------------------------| +| key only a later file carries | kept in that file's rows | dropped; keep it with `->inferSchema(infer_schema()->unionByName())` | +| key first seen past the sample | kept in that row | dropped; keep it with `->inferSchema(infer_schema()->sampleSize(-1))` | +| key missing from a record | absent from that row | `null` | +| nested key every sampled record carries, missing later | read | throws `SchemaMismatchException` | +| nested key first seen past the sample | kept | dropped | +| 0-byte file, `from_json()` | throws `JsonMachine\Exception\SyntaxErrorException` | skipped, no rows | + +Applies to `from_json()` and `from_json_lines()` unless the row names one. + +### 90) `flow-php/etl-adapter-json` - `from_json_lines()` skips blank lines + +| Before | After | +|-----------------------------------------------------------------|----------------------------------------| +| an empty or whitespace-only line - threw `SyntaxErrorException` | skipped, in `schema()` and `extract()` | + +### 91) `flow-php/etl-adapter-parquet` - `to_parquet()` does not validate values against the Parquet schema + +| Before | After | +|---------------------------------------------------------------|----------------------------------------------------------------------| +| `to_parquet($path)` wrote with `Option::VALIDATE_DATA` `true` | `false` | +| - | `to_parquet($path)->withOptions(Options::default())` validates again | + +### 92) `flow-php/etl-adapter-postgresql` - `from_pgsql_*()` type columns from the query instead of reading strings + +| PostgreSQL column | Before | After | +|-----------------------------------------------------------------------|-------------------------|--------------------------------------| +| `numeric` | `string` | `float` | +| `date`, `timestamp`, `timestamptz` | `string` | `DateTimeImmutable` | +| `time` | `string` | `DateInterval` | +| `timetz` | `string` with offset | `DateInterval`, offset dropped | +| `uuid` | `string` | `Flow\Types\Value\Uuid` | +| `json`, `jsonb` | `string` | `Flow\Types\Value\Json` | +| `xml` | `string` | `DOMDocument` | +| `oid` | `string` | `int` | +| one-dimensional array (`int4[]`, `text[]`, ...) | `string` `'{1,NULL,3}'` | `[1, null, 3]`, `null` elements kept | +| multi-dimensional array | `string` | throws while hydrating | +| `record`, `point`, `line`, `lseg`, `box`, `path`, `polygon`, `circle` | `string` | throws `SchemaNotDerivableException` | + +Applies to `from_pgsql_cursor()`, `from_pgsql_limit_offset()` and `from_pgsql_key_set()` without `->withSchema()`. A +query that cannot run as a subquery (multi-statement, data-modifying CTE, `INSERT ... RETURNING`) also throws +`SchemaNotDerivableException`. Declare `->withSchema(...)` to pick the types. + +### 93) `flow-php/etl-adapter-postgresql` - `pgsql_table_to_flow_schema()` maps arrays, text-like types, `oid`, `timetz` + +| PostgreSQL column | Before | After | +|---------------------------------------------------------------------------------------------------------------|-------------------------------|------------------| +| `inet`, `cidr`, `macaddr`, `money`, `int4range`, `int8range`, `numrange`, `tsrange`, `tstzrange`, `daterange` | throws `TypeMappingException` | `string` | +| `oid` | throws `TypeMappingException` | `integer` | +| `timetz` (`time with time zone`) | throws `TypeMappingException` | `time` | +| `integer[]`, any array column | its element type, `integer` | `list` | + +Applies to `pgsql_table_to_flow_schema()` and `EntryTypesMap::toFlowType()`. + +| Before | After | +|---------------------------------------------------------------------------|---------| +| `TypeMappingException::ambiguousEntryType()` / `::unsupportedEntryType()` | removed | + +### 94) `flow-php/etl-adapter-postgresql` - `InsertQueryBuilder::build()` takes the client's converters + +| Before | After | +|-----------------------------------------------|---------------------------------------------------------------------------------------------| +| `$builder->build($values, $schema, $options)` | `$builder->build($values, $schema, $client->converters(), $options)` | +| returns `array{Sql, list}` | returns `array{Sql, ConvertedParameters}` - pass the second element to `$client->execute()` | + +### 95) `flow-php/etl-adapter-xml` - `from_xml()` keeps each element as the source wrote it + +| Before | After | +|------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------| +| `node` re-serialized with added indentation; comments dropped | the element's source text, whitespace and comments included | +| every ancestor namespace declared on `node` | only the ancestor namespaces the element uses | +| a truncated document returned the elements before the cut | throws `RuntimeException` | +| expat error messages | libxml error messages, e.g. `XML Error: Premature end of data in tag root line 1 at line 1` | +| `XMLParserExtractor::startElementHandler()` / `endElementHandler()` / `characterDataHandler()` | removed | + +### 96) `flow-php/cli` - `--schema-auto-cast` removed, `flow schema` ignores the row window + +| Before | After | +|---------------------------------------------------------------------------------------------------|------------------------------------------------| +| `--schema-auto-cast` on `file:analyze`, `file:convert`, `file:read`, `file:schema` | removed | +| `flow schema --input-file-offset=N` / `--input-file-limit=N` - schema of the selected rows | the file's schema; both options have no effect | + +### 97) `flow-php/cli` - command classes name themselves with `#[AsCommand]` + +| Before | After | +|----------------------------------------------------|-------------------------------------------------------------------------------------------------------| +| `new FileConvertCommand()` - named `file:read` | `file:convert` | +| `new FileRowsCountCommand()` - named `file:schema` | `file:rows:count` | +| `new PipelineRunCommand()` - named `run` | `pipeline:run` | +| `Flow\CLI\Command\*` classes carried no aliases | `run`, `read`, `schema`, `count`, `convert`, `analyze`, `format`, as the `flow` binary registers them | + +Registering the commands in your own console application: drop the `setName()` / `setAliases()` calls. + +### 98) `flow-php/flow-php-ext` - the `flow_php` extension must be reinstalled + +| Before | After | +|----------------------------|----------------------------------------| +| `flow_php` extension 0.1.0 | 0.3.0, required by this `flow-php/etl` | + +Reinstall it with the new release: `pie install flow-php/flow-php-ext`. + +### 99) `flow-php/etl-adapter-csv` - `withSeparator()`, `withEnclosure()` and `withEscape()` take a single byte + +| Before | After | +|-----------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------| +| `withSeparator('\|\|')` / `withEnclosure('\|\|')` / `withEscape('ab')` - PHP 8.3: first byte used; PHP 8.4+: `ValueError` on read | throws `Flow\ETL\Exception\InvalidArgumentException` | +| `withSeparator('')` / `withEnclosure('')` - PHP 8.3: `,` / `"` used; PHP 8.4+: `ValueError` on read | throws `Flow\ETL\Exception\InvalidArgumentException` | + +### 100) `flow-php/etl` - `array_expand()` nested in an expression gives rows, and is refused outside `withEntry()` + +| Before | After | +|---------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------| +| `withEntry('s', concat(ref('id'), ref('tags')->expand()))` - one row, list JSON-encoded: `a["x","y"]` | one row per element: `ax`, `ay` | +| `withEntry('v', ref('lists')->expand()->expand())` - one row per outer element, inner list JSON-encoded | throws `InvalidArgumentException` - expand one level per `withEntry()` | +| `array_expand()` in `filter()`, `until()`, `duplicateRow()`, `aggregate()`, `over()` | throws `InvalidArgumentException` at `schema()` / `run()` | +| `array_expand()` in an `onEach()` body | throws `InvalidArgumentException` when `onEach()` is called | + +### 101) `flow-php/array-dot` - `\` escapes every path grammar character, `array_dot_steps()` deprecated + +| Before | After | +|-----------------------------------------------------|-----------------------------------------------| +| `\?x`, `a\*b`, `a\\` read keys `\?x`, `a\*b`, `a\\` | read keys `?x`, `a*b`, `a\` | +| `?{a}` reads key `{a}` | throws `InvalidPathException` - `?\{a\}` | +| `x.*.a\.b` reads `a` -> `b` of each element | reads key `a.b` of each element | +| `x.{a\.b,c}` result keys `a__ESCAPED_DOT__b`, `c` | `a.b`, `c` | +| `x.{a?b}` result key `ab` | `a?b` | +| `array_dot_steps($path)` | deprecated - `Path::fromString($path)->steps` | + +### 102) `flow-php/array-dot` - `array_dot_set()` and `array_dot_rename()` change only the addressed key + +| Before | After | +|---------------------------------------------------------------------------------------------|---------------------------------| +| `array_dot_set(['a' => ['x' => 1]], 'a.y', 2)` -> `['a' => ['y' => 2]]` | `['a' => ['x' => 1, 'y' => 2]]` | +| `array_dot_set([5 => 'a', 7 => 'b'], '5', 'c')` -> `['a', 'b', 'c']` | `[5 => 'c', 7 => 'b']` | +| `array_dot_set([], '\{a\}', 1)` / `'?a'` write keys `\{a\}` / `?a` | write keys `{a}` / `a` | +| `array_dot_set($array, 'x.*', $value)` throws | sets every element of `x` | +| `array_dot_set($array, 'x.{a,b}', $value)` writes key `{a,b}` | throws `InvalidPathException` | +| `array_dot_rename(['{a}' => 1], '\{a\}', 'b')` -> warning, `['{a}' => 1, 'b' => null]` | `['b' => 1]` | +| `array_dot_rename(['a' => 1], '?missing', 'b')` -> warning, `['a' => 1, 'b' => null]` | `['a' => 1]` | +| `array_dot_rename($array, 'x.*', 'c')` / `'x.{a,b}'` -> warning, adds `c => null` under `x` | throws `InvalidPathException` | +| `array_dot_set(['x' => 5], 'x.*', 1)` throws `InvalidPathException` | `['x' => []]` | +| `array_dot_set([], 'x.*', 1)` throws `InvalidPathException` | `['x' => []]` | +| `array_dot_rename(['a' => 1, 'b' => 2], 'a', 'a')` -> `['b' => 2]` | `['a' => 1, 'b' => 2]` | + +### 103) `flow-php/etl` - `array_get_collection()` reads its keys as literal keys + +| Before | After | +|----------------------------------------------|-----------------------------------| +| key `a.b` reads `a` -> `b`, result key `a_b` | reads key `a.b`, result key `a.b` | +| key `k,l` throws `InvalidArgumentException` | reads key `k,l` | + +### 104) `flow-php/etl` - `array_expand()` over a null list gives no rows + +| Before | After | +|-----------------------------------------------|----------------------| +| `null` list throws `InvalidArgumentException` | no rows for that row | + +### 105) `flow-php/array-dot`, `flow-php/etl` - a nullsafe multimatch over an empty array reads `null` + +| Before | After | +|---------------------------------------------------------------------------------------------------------|------------------------------------| +| `array_dot_get([], '{?a}')` throws `InvalidPathException` | `['a' => null]` | +| `array_dot_get([], '{a}')` message `Path "{a}" does not exists ...` | `Path "a" does not exists ...` | +| `array_get_collection(ref('c'), ['id'])` over `[['name' => 'a'], []]` throws `InvalidArgumentException` | `[['id' => null], ['id' => null]]` | + +### 106) `flow-php/filesystem` - every filesystem matches glob patterns the same way + +Files: `data/flat.parquet`, `data/.hidden.parquet`, `data/.dir/x.parquet`, `data/date=2026-09-01/one.parquet`, +`data/id=1/date=2026-09-01/two.parquet` + +| pattern | local, before | memory / S3 / Azure, before | every filesystem, after | +|-------------------------|---------------|-----------------------------|-----------------------------------| +| `data/**.parquet` | `flat` | all five | `.hidden`, `flat` | +| `data/**/*.parquet` | all five | `.dir/x`, `one`, `two` | all five | +| `data/**` | `flat` | all five | all five | +| `data/*.parquet` | `flat` | `.hidden`, `flat` | `.hidden`, `flat` | +| `data/*/*.parquet` | `one` | `.dir/x`, `one` | `.dir/x`, `one` | +| `data/**/[!f]*.parquet` | `flat` | none | `.dir/x`, `.hidden`, `one`, `two` | +| `data/[a-g]*.parquet` | `flat` | none | `flat` | + +Recurse with `data/**/*.parquet`, not `data/**.parquet`. `webmozart/glob` is no longer installed with +`flow-php/filesystem` or `flow-php/etl`. + +### 107) `flow-php/parquet` - `ParquetEngine::openForWrite()` returns a `ParquetFileWriter` + +| Before | After | +|-------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------| +| `$engine->openForWrite($stream, $schema, $c, $o); $engine->writeBatch($rows); $engine->closeWrite();` | `$file = $engine->openForWrite($stream, $schema, $c, $o); $file->writeBatch($rows); $file->close();` | +| `ParquetEngine::closeWrite()`, `writeBatch()`, `writeRow()` | removed - on `ParquetFileWriter` | +| two `Writer`s sharing one engine overwrote each other | every `openForWrite()` returns an independent writer | +| `ArrowParquetEngine` left the destination stream open | `ParquetFileWriter::close()` closes it on every engine | + +### 108) `flow-php/etl-adapter-parquet` - path-only partition columns leave the file body + +| Before | After | +|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------| +| `partitionBy(partition_by('date'))` - file body carries an all-null `date` column | file body without `date` | +| `from_parquet()` types `date` from that body column, e.g. `datetime` | `string` - declare it: `from_parquet($path)->partitionTypes(partition_types(date: type_datetime()))` | + +--- + +## Upgrading from 0.42.x to 0.43.x + +### 1) `flow-php/etl` - `to_transformation()` expands a `Transformation` once per loader, not once per batch + +| Before | After | +|-----------------------------------------------------------------------------------|-------------------------| +| `to_transformation(limit(3), $loader)`, 6 batches → 6 rows loaded | 3 rows loaded | +| `to_transformation(add_row_index('n'), $loader)`, 6 batches → `n = [1,1,1,1,1,1]` | `n = [1,2,3,4,5,6]` | +| nested `DataFrame` span per batch | one nested span per run | + +Unchanged: any pipeline using `->collect()`, `drop()`, `select()`, `mask_columns()`, `batch_size()`, `batch_by()`. + +### 2) `flow-php/telemetry` - `Tracer::span()` no longer activates the span + +| Before | After | +|----------------------------------------------------|-------------------------------------------------------------------| +| `$tracer->span('x')` makes the span current | does not; `$tracer->activate($span): Scope` does | +| `$tracer->complete($span)` also detaches the scope | ends the span only | +| `span(..., SpanContext $parentContext)` | `span(..., Context $parent)` | +| `trace(..., SpanContext $parentContext)` | `trace(..., Context $parent)` | +| `Scope::detach()` always returns `0` | returns `Scope::DETACHED`, `Scope::INACTIVE` or `Scope::MISMATCH` | + +Call sites relying on implicit nesting still compile and silently produce siblings. Rewrite each one that needs +children: + +Before: + +```php +$span = $tracer->span('parent'); + +try { + // ... +} finally { + $tracer->complete($span); +} +``` + +After: + +```php +$span = $tracer->span('parent'); +$scope = $tracer->activate($span); + +try { + // ... +} finally { + $scope->detach(); + $tracer->complete($span); +} +``` + +### 3) `flow-php/etl` - window aggregates use the SQL default frame + +On `d = 2024-01-01, 2024-01-02, 2024-01-03, 2024-01-04` and `s = 100, 200, 300, 400`: + +| Before | After | +|-------------------------------------------------------------------------------|-----------------------------------------------| +| `sum(ref('s'))->over(window()->orderBy(ref('d')))` → `1000, 1000, 1000, 1000` | `100, 300, 600, 1000` | +| `average()`, `count()` over an ordered window - whole partition | rows up to the current row's peers | +| `window()->partitionBy(ref('dept'))` - whole partition | unchanged | +| empty frame | `sum()`/`average()` → `null`, `count()` → `0` | + +Restore the previous result: + +```php +sum(ref('s'))->over(window()->orderBy(ref('d'))->rowsBetween(unbounded_preceding(), unbounded_following())); +``` + +### 4) `flow-php/etl` - `count()` over a window is SQL `COUNT` + +On `s = 100, 100, 300` ordered by a distinct column: + +| Before | After | +|---------------------------------------------------------------------------|-------------------------------------------------| +| `count(ref('s'))` counts rows sharing the current row's value → `2, 2, 1` | counts non-null values in the frame → `1, 2, 3` | +| `count()` threw `Count WindowFunction function requires a reference.` | counts every row in the frame (`COUNT(*)`) | + +### 5) `flow-php/etl` - `partitionBy()` no longer sets `orderBy()` + +| Before | After | +|----------------------------------------------------------------------------------------------------------|---------------------------------------------------| +| `window()->orderBy(ref('date'))->partitionBy(ref('dept'))->order()` → `['dept']` | `['date']` | +| `window()->partitionBy(ref('dept'))->order()` → `['dept']` | `[]` | +| `rank()`/`dense_rank()`/`row_number()` over a `partitionBy()`-only window ranked by the partition column | throws `... requires to be ordered by one column` | + +Add the ordering explicitly: + +```php +rank()->over(window()->partitionBy(ref('dept'))->orderBy(ref('salary')->desc())); +``` + +### 6) `flow-php/etl` - `WindowFunction::apply()` receives a `WindowContext` + +| Before | After | +|----------------------------------------------------------|----------------------------------------------------------| +| `apply(Row $row, Rows $partition, FlowContext $context)` | `apply(WindowContext $window)` | +| `$row` | `$window->row()` | +| `$partition` | `$window->partition()` | +| `$context` | `$window->flowContext()` | +| - | `$window->frame()` - rows within the current row's frame | +| - | `$window->index()` - position in the ordered partition | +| `row_number()` on duplicate rows → `1, 1, 3` | `1, 2, 3` | + +Implementations must no longer sort; `$window->partition()` and `$window->frame()` are already ordered. + +### 7) `flow-php/postgresql` - query recording moved to `flow-php/symfony-postgresql-bundle` + +| Before | After | +|--------------------------------------------------|----------------------------------------------------------------------| +| `Flow\PostgreSql\Client\Debug\RecordingClient` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\ProfilerClient` | +| `Flow\PostgreSql\Client\Debug\QueryLog` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\QueryRecorder` | +| `Flow\PostgreSql\Client\Debug\QueryLogOptions` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\QueryRecorderOptions` | +| `Flow\PostgreSql\Client\Debug\RecordedQuery` | `Flow\Bridge\Symfony\PostgreSqlBundle\Profiler\RecordedQuery` | +| `QueryLogOptions::$maxParameters` | `QueryRecorderOptions::$maxRetainedParameters` | +| `QueryLogOptions::maxParameters()` | `QueryRecorderOptions::maxRetainedParameters()` | +| service `flow.postgresql.profiler.query_log` | `flow.postgresql.profiler.query_recorder` | +| config `flow_postgresql.profiler.max_parameters` | `flow_postgresql.profiler.max_retained_parameters` | + +Applies to `flow-php/postgresql` users only through the bundle; `Client\Telemetry` is unchanged. + +### 8) `flow-php/symfony-telemetry-bundle` - cache pools and PSR-18 clients that were silently skipped are now traced + +| Before | After | +|------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------| +| `cache.system`, `cache.validator`, `cache.serializer`, `cache.property_info`, `cache.app`, `cache.doctrine.*`, `cache.http_client.pool` - not traced | traced: `cache.*` spans and `flow.cache.hits` / `flow.cache.misses` | +| pool or client whose class is a `%parameter%` - not traced | traced | +| tag-aware pool whose class is a `%parameter%` - got the non-tag-aware decorator | gets `TagAwareTraceableCacheAdapter` | +| PSR-18 client behind an autoconfigured or abstract parent definition - container build failed with *"has a reference to an abstract definition"* | compiles; the client is traced | +| `instrumentation.cache.exclude_pools` entries for framework pools - had no effect | take effect | + +To keep the previous set of traced pools, exclude the framework's own: + +```yaml +flow_telemetry: + instrumentation: + cache: + exclude_pools: + - 'cache.system' + - 'cache.validator' + - 'cache.serializer' + - 'cache.property_info' + - '/^cache\.doctrine\..*/' + - 'cache.http_client.pool' +``` + +### 9) `flow-php/types` - a `Type` implementation's generic parameter is the value it represents + +| Before | After | +|----------------------------------|------------------------------------------| +| `ListType` | `ListType>` | +| `MapType` | `MapType>` | +| `StructureType` | `StructureType>` | +| `ClassStringType` | `ClassStringType>` | +| `ListType::element(): Type` | `Type>` | +| `MapType::key(): Type` | `Type>` | +| `MapType::value(): Type` | `Type>` | +| `type_string(): Type` | `type_string(): StringType` | + +Update `ListType`, `MapType`, `StructureType` and `ClassStringType` parameters in your own docblocks. + +--- + +## Upgrading from 0.41.x to 0.42.x + +### 1) `flow-php/symfony-telemetry-bundle` - messenger tracing simplified + +| Before (0.41) | After (0.42) | +|----------------------------------------------------------------------|--------------------------------------------------------------------| +| `instrumentation.messenger.trace`: `worker`/`handlers`/`both`/`none` | `instrumentation.messenger.trace`: `true`/`false` (default `true`) | +| `instrumentation.messenger.link`: `dispatcher`/`worker`/`both` | removed | +| `messenger.receive` worker-cycle span | removed | + +The worker-cycle span and the `trace: worker`/`both` modes are gone; a consumed message is a `process` span and a +produced one a `send` span. The `messenger:consume` worker loop is suppressed via +`instrumentation.console.exclude_commands` - which now **fully suppresses** matching commands (previously it only +skipped the console span) and defaults to `['messenger:consume']` - so transport-poll/idle-tick work does not surface +and the long-lived `messenger:consume` console span is dropped. Per-message handler traces are still recorded. Set +`console.exclude_commands: []` to trace the worker loop. + +### 2) `flow-php/symfony-telemetry-bundle` - cache span names unified to dotted lowercase + +| Before | After | +|-----------------------------------------------------------------------|----------------------------------------------------------| +| `Cache Commit {pool}` | `cache.commit` | +| `Cache Save {key} {pool}` | `cache.save` | +| `Cache SaveDeferred {key} {pool}` | `cache.save_deferred` | +| `Cache Delete {key} {pool}` | `cache.delete` | +| `Cache DeleteItem {key} {pool}` | `cache.delete_item` | +| `Cache DeleteItems {pool}` | `cache.delete_items` | +| `Cache Clear {pool}` | `cache.clear` | +| `Cache Prune {pool}` | `cache.prune` | +| `Cache Reset {pool}` | `cache.reset` | +| `Cache InvalidateTags {pool}` | `cache.invalidate_tags` | +| `cache.operation: saveDeferred/deleteItem/deleteItems/invalidateTags` | `save_deferred/delete_item/delete_items/invalidate_tags` | + +Cache spans now match the DBAL/messenger convention (dotted lowercase, low cardinality). The `{key}` and `{pool}` +that were baked into the span name move out of it - they were already available as the `cache.key` and `cache.pool` +attributes. Rename these series in dashboards and alerts, and update any filters on the camelCase `cache.operation` +values. + +### 3) `flow-php/telemetry` - `Sampler::shouldSample()` receives the parent `Context` + +| Before | After | +|--------------------------------------------|--------------------------------------------------------------------| +| `shouldSample(Span $span): SamplingResult` | `shouldSample(Context $parentContext, Span $span): SamplingResult` | +| `$sampler->shouldSample($span)` | `$sampler->shouldSample($context, $span)` | + +Custom `Sampler` implementations (including a `sampler: { type: service }` service in +`flow-php/symfony-telemetry-bundle`) must update the signature and forward `$parentContext` to any delegated sampler. + +### 4) `flow-php/telemetry` - `ResettableContextStorage` removed; `MemoryContextStorage::reset()` removed + +| Before | After | +|---------------------------------------------------|---------| +| `Flow\Telemetry\Context\ResettableContextStorage` | removed | +| `MemoryContextStorage::reset()` | removed | + +The context storage is no longer tagged `kernel.reset` in `flow-php/symfony-telemetry-bundle`; scope balance is +maintained by attach/detach alone. A custom `context_storage` service no longer needs a `reset()` method. + +### 5) `flow-php/symfony-telemetry-bundle` - messenger tracing middleware auto-injected into all buses + +The tracing middleware is now injected into every message bus automatically. If you previously added +`flow.telemetry.messenger.middleware` to a bus's `framework.messenger.buses.*.middleware` list by hand, remove it to +avoid duplicate spans. + +### 6) `flow-php/symfony-telemetry-bundle` - `http_kernel.exclude_paths` now suppresses the whole request + +| Before | After | +|---------------------------------------------------|---------------------------------------------------------| +| Excluded path only skips its own request span | Excluded path suppresses tracing for the entire request | +| DBAL/cache/`kernel.terminate` work still recorded | DBAL/cache/`kernel.terminate` work produces no spans | + +An excluded path now attaches the OpenTelemetry suppression key for the request's duration (through +`kernel.terminate`), so lower-level auto-instrumentation and terminate-phase database writes no longer emit orphan root +spans (e.g. the `/_wdt` toolbar fetch writing an audit row after its response). If you relied on those child spans being +recorded for an excluded path, remove the path from `exclude_paths`. + +### 7) `flow-php/postgresql`, `flow-php/symfony-postgresql-bundle` - `traceTransactions` bool replaced by + +`transactionSpans` mode + +| Before | After | +|----------------------------------------------------------------|--------------------------------------------------------------------------------| +| `postgresql_telemetry_options(traceTransactions: true)` | `postgresql_telemetry_options(transactionSpans: TransactionSpanMode::GROUPED)` | +| `postgresql_telemetry_options(traceTransactions: false)` | `postgresql_telemetry_options(transactionSpans: TransactionSpanMode::OFF)` | +| `PostgreSqlTelemetryOptions` 2nd arg `bool $traceTransactions` | `TransactionSpanMode $transactionSpans` | +| `$options->traceTransactions(false)` | `$options->transactionSpans(TransactionSpanMode::OFF)` | +| `$options->traceTransactions` (property) | `$options->transactionSpans` (`TransactionSpanMode`) | +| config `telemetry.trace_transactions: true` | config `telemetry.transaction_spans: grouped` | +| config `telemetry.trace_transactions: false` | config `telemetry.transaction_spans: off` | + +`TransactionSpanMode::PER_OPERATION` emits a short span per `BEGIN`/`COMMIT`/`ROLLBACK`; `GROUPED` (default) keeps the +single long-lived transaction span. + +The `db.client.operation.duration` metric now uses only low-cardinality dimensions: queries are tagged with +`db.system.name`, `db.namespace`, `db.operation.name`, `db.collection.name`; transactions with `db.system.name`, +`db.namespace`, `db.operation.name` (`begin`/`commit`/`rollback`), `db.transaction.nesting_level`. `db.query.text`, +`db.query.parameter.*`, `server.address` and `db.transaction.savepoint` are no longer metric dimensions. + +### 8) `flow-php/symfony-telemetry-bundle` - DBAL spans, metrics and config aligned with the PostgreSQL client + +| Before | After | +|------------------------------------------------------------------|--------------------------------------------------------------------------------------------| +| `doctrine.dbal.transaction.begin`/`.commit`/`.rollback` (always) | one `BEGIN TRANSACTION` span per transaction (`grouped`) | +| `doctrine.dbal.connection.exec`/`.query` span names | semconv `{db.operation.name} {db.collection.name}` (e.g. `SELECT users`) | +| `doctrine.dbal.statement.execute`/`.prepare` span names | semconv `{db.operation.name} {db.collection.name}` | +| config `instrumentation.dbal.log_sql` | removed - `db.query.text` is always recorded (bounded by `max_sql_length`) | +| - | config `instrumentation.dbal.transaction_spans`: `grouped` (default)/`per_operation`/`off` | +| - | config `instrumentation.dbal.collect_metrics` (default `true`) | +| - | config `instrumentation.dbal.include_parameters`/`max_parameters`/`max_parameter_length` | + +Query spans now carry `db.system.name`, `db.namespace`, `server.address`, `server.port`, `db.operation.name`, +`db.collection.name`, `db.response.returned_rows` and (on error) `db.response.status_code`, and the instrumentation +emits `db.client.operation.duration` and `db.client.response.returned_rows` metrics. If you matched DBAL spans by their +`doctrine.dbal.*` names, switch to the semantic names above. + +### 9) `flow-php/symfony-telemetry-bundle` - custom attribute keys moved out of reserved OTel namespaces + +| Before | After | +|------------------------------------|----------------------------------------| +| `controller` | `flow.symfony.controller` | +| `controller.argument` | `flow.symfony.controller.argument` | +| `code.namespace` + `code.function` | `code.function.name` (`Class::method`) | +| `command.name` | `flow.symfony.command.name` | +| `command.class` | `flow.symfony.command.class` | +| `process.signal` | `flow.symfony.command.signal` | +| `process.exit_code` | `process.exit.code` | +| `db.connection.name` | `flow.db.connection.name` | +| `db.transaction.nesting_level` | `flow.db.transaction.nesting_level` | +| `http.client.name` | `flow.http.client.name` | +| `log.channel` | `flow.log.channel` | + +### 10) `flow-php/symfony-telemetry-bundle` - HTTP spans aligned with the stable HTTP semconv + +| Before | After | +|-------------------------------------------------|----------------------------------------------| +| server span `url.full` | removed | +| server span `url.path` = URI incl. query | path only; query moves to `url.query` | +| - | server span `user_agent.original` | +| client span name `{method} {host}` | `{method}` | +| client span `server.port` only when non-default | always set (scheme default 80/443 filled in) | + +Applies to the HttpKernel server span and to the `http_client`/`psr18_client` client spans. + +### 11) `flow-php/symfony-telemetry-bundle` - messenger destination is the transport; `metrics_duration_unit` removed + +| Before | After | +|--------------------------------------------------------------------|----------------------------------------------------------| +| `messaging.destination.name` = short message class | transport name (consume side); absent on dispatch | +| `messaging.transport` | removed (folded into `messaging.destination.name`) | +| `messaging.consumer.group.name` (metric attribute) | removed | +| `messaging.message.class` | `flow.messenger.message.class` | +| `messaging.symfony.bus` | `flow.messenger.bus` | +| consume span `process {ShortClass}` | `process {transport}` | +| dispatch span `send {ShortClass}` | `send` | +| config `instrumentation.messenger.metrics_duration_unit`: `s`/`ms` | removed - `messaging.process.duration` is always seconds | +| `Instrumentation\Messenger\MessengerMetricDurationUnit` | removed | + +### 12) `flow-php/phpunit-telemetry-bridge` - `test.*` keys aligned with the OTel test registry; durations in seconds + +| Before | After | +|---------------------------------------------------|--------------------------------------------------------| +| `test.name` | `test.case.name` | +| `test.status` | `test.case.result.status` | +| `test.suite` | `test.suite.name` | +| - | `test.suite.run.status`: `success`/`failure`/`skipped` | +| `test.id` | `flow.phpunit.test.id` | +| `test.class` | `flow.phpunit.test.class` | +| `test.method` | `flow.phpunit.test.method` | +| `test.suite.test_count` | `flow.phpunit.suite.test_count` | +| `test.suite.is_root` | `flow.phpunit.suite.is_root` | +| `test.memory.peak_bytes` | `flow.phpunit.test.memory.peak` | +| `test.memory.delta_bytes` | `flow.phpunit.test.memory.delta` | +| `test.duration_ms` span attribute | removed (use the span duration) | +| `exception.message` span attribute | removed (message stays in the span status description) | +| `flow.phpunit.test.duration` unit `ms` | `s` (values rescaled) | +| `flow.phpunit.suite.duration` unit `ms` | `s` (values rescaled) | +| `flow.phpunit.test.memory.*` unit `bytes` | `By` | +| `flow.phpunit.test.count`/`suite.test_count` unit | `{test}` | +| - | resource attribute `telemetry.sdk.version` | + +### 13) `flow-php/filesystem`, `flow-php/etl`, `flow-php/postgresql` - flow-custom keys under `flow.*`; UCUM units + +| Before | After | +|------------------------------------------------------------|------------------------------------------------------| +| `path.uri`/`path.to` | `flow.filesystem.path.uri`/`.path.to` | +| `stream.type` | `flow.filesystem.stream.type` | +| `bytes.total_read`/`bytes.total_written` | `flow.filesystem.bytes.total_read`/`.total_written` | +| `filesystem.operation`/`filesystem.protocol` | `flow.filesystem.operation`/`.protocol` | +| spans `Read {file}`/`Write {file}` | `filesystem.read`/`filesystem.write` | +| `flow.filesystem.*.size` unit `bytes` | `By` | +| `flow.filesystem.*.operations` unit `operations` | `{operation}` | +| `dataframe.id`/`dataframe.name` | `flow.etl.dataframe.id`/`.dataframe.name` | +| `rows.total`/`rows.throughput.per_second` | `flow.etl.rows.total`/`.rows.throughput.per_second` | +| `memory.min.mb`/`memory.max.mb` | `flow.etl.memory.min`/`.memory.max` (values stay MB) | +| `loader.class`/`transformer.class` | `flow.etl.loader.class`/`.transformer.class` | +| `destination.uri`/`loading.rows` | `flow.etl.destination.uri`/`.loading.rows` | +| `transformation.input_rows`/`.output_rows` | `flow.etl.transformation.input_rows`/`.output_rows` | +| `join.type`/`scalar.function` | `flow.etl.join.type`/`.scalar.function` | +| spans `Cache Set {key}`/`Cache Delete {key}`/`Cache Clear` | `cache.set`/`cache.delete`/`cache.clear` | +| `flow.cache.hits`/`.misses` unit `operations` | `{operation}` | +| `flow.etl.rows.processed` unit `rows` | `{row}` | +| `flow.etl.rows.throughput` unit `rows/s/sec` | `{row}/s` | +| `db.transaction.savepoint` | `flow.db.transaction.savepoint` | +| `db.transaction.nesting_level` | `flow.db.transaction.nesting_level` | + +### 14) `flow-php/telemetry`, `flow-php/psr18-telemetry-bridge` - shared semconv constants; UCUM time units + +| Before | After | +|---------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------| +| official keys duplicated per package (`DbAttributes`, `PostgreSqlTelemetryAttributes`, inline strings) | `Flow\Telemetry\SemConvAttributes` / `SemConvMetrics` | +| `TimeUnit::SECONDS->value` = `'sec'` | `'s'` | +| `TimeUnit::MICROSECONDS->value` = `'µs'` | `'us'` | +| PSR-18 client span name `{method} {host}` | `{method}` | +| PSR-18 `server.port` only when non-default | always set (scheme default 80/443 filled in) | +| `FilesystemTelemetryAttributes::ATTR_BYTES_READ`/`ATTR_BYTES_WRITTEN`/`ATTR_PATH_FROM`/`ATTR_PATH_IS_PATTERN`/`ATTR_ERROR_TYPE` | removed | +| `PostgreSqlTelemetryAttributes::DB_QUERY_SUMMARY` + official-key constants | removed (officials via `SemConvAttributes`) | + +### 15) `flow-php/symfony-telemetry-bundle` - `runtime_mode` removed; terminate flushes, process end shuts down + +| Before | After | +|-----------------------------------------------------------------------------|---------------------------------------------| +| `flow_telemetry.runtime_mode: auto`/`classic`/`worker` | removed | +| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\RuntimeModeResolver` | removed | +| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\RuntimeMode` | removed | +| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\WorkerModeDetector` | removed | +| `Flow\Bridge\Symfony\TelemetryBundle\Runtime\EnvironmentWorkerModeDetector` | removed | +| shutdown on `kernel.terminate`/`console.terminate` (classic mode) | flush on terminate; shutdown at process end | + +Drop the `runtime_mode` key from `flow_telemetry` config and remove any `WorkerModeDetector` service overrides. + +### 16) `flow-php/telemetry` - `Telemetry::registerShutdownFunction()` holds a weak reference + +| Before | After | +|--------------------------------------------------|---------------------------------------------------------------| +| strong reference; instance kept alive until exit | weak reference; garbage-collected instances are not shut down | + +Keep the registered `Telemetry` instance referenced for as long as it should be shut down at process end. + +### 17) `flow-php/etl` - `SchemaValidator::isValid()` replaced by `validate(): ValidationContext` + +| Before | After | +|-----------------------------------------------|------------------------------------------------------------------| +| `SchemaValidator::isValid(...): bool` | `SchemaValidator::validate(...): ValidationContext` | +| `$validator->isValid($expected, $given)` | `$validator->validate($expected, $given)->isValid()` | +| `schema_validate(...): bool` | `schema_validate(...): ValidationContext` | +| `new SchemaValidationException($exp, $given)` | `new SchemaValidationException($exp, $given, ValidationContext)` | +| - | `SchemaValidationException::context(): ValidationContext` | + +Custom `SchemaValidator` implementations must return a `Flow\ETL\Schema\Validator\ValidationContext` +built from the missing, mismatched (`MismatchedDefinition`), and unexpected definitions they reject. + +### 18) `flow-php/filesystem` - `Partition` name and value forbid `{` and `}` + +| Before | After | +|----------------------------------|-----------------------------------| +| `new Partition('na{me', 'a}b')` | throws `InvalidArgumentException` | +| `partitionBy()` values with `{}` | throws `InvalidArgumentException` | + +`{name}` in a path is now a partition placeholder resolved from `partitionBy()` columns; strip braces from partition +values before partitioning. + +### 19) `flow-php/etl` - `Cache` stores only `Rows`; cache indexes are stored as `Rows` + +| Before | After | +|---------------------------------------------------|-----------------------------------------------| +| `Cache::get(string): Row\|Rows\|CacheIndex` | `Cache::get(string): Rows` | +| `Cache::set(string, Row\|Rows\|CacheIndex): void` | `Cache::set(string, Rows): void` | +| `$cache->set($id, $row)` | `$cache->set($id, rows($row))` | +| `$cache->set($id, $cacheIndex)` | `$cache->set($id, $cacheIndex->toRows())` | +| `$cache->get($id)` returning `CacheIndex` | `CacheIndex::fromRows($id, $cache->get($id))` | + +Custom `Cache` implementations must adopt the `Rows`-only signatures. + +### 20) `flow-php/etl` - cache and serialization use the Floe (`.floe`) binary format; datetime subclasses rejected + +| Before | After | +|----------------------------------------------------------------------------------------|------------------------------------------------------------------------| +| `config_builder()->serializer()` default `Base64Serializer(new NativePHPSerializer())` | `Flow\Floe\FloeSerializer` | +| `filesystem_cache(..., Serializer $serializer = new NativePHPSerializer())` | `filesystem_cache(..., Serializer $serializer = new FloeSerializer())` | +| `new FilesystemCache($filesystem, $serializer, $cacheDir)` | `new FilesystemCache($filesystem, $cacheDir, $serializer)` | +| caching or serializing a `DateTime`/`DateTimeImmutable` subclass (e.g. Carbon) | throws `Flow\Floe\Exception\FloeException` | + +Delete cache directories written by 0.41 - the old serialized payloads are unreadable. Convert +`DateTime`/`DateTimeImmutable` subclasses to `DateTime`/`DateTimeImmutable` before caching or serializing. + +A Floe file carries exactly one schema, and sort / join / group-by spill batches through it - a pipeline with a drifting +or schemaless source must declare column types at the source so nullable columns carry a concrete type +(`DataFrame::match($schema)` validates but does not retype values): + +```php +->read(from_array($data, schema(str_schema('email'), float_schema('discount', nullable: true)))) +``` + +### 21) `flow-php/symfony-telemetry-bundle` - `HttpKernelSpanSubscriber` takes a + +`RouteNamePathMap` instead of the router + +| Before | After | +|-----------------------------------------------------------|-------------------------------------------------------------------------| +| `new HttpKernelSpanSubscriber(..., router: $router, ...)` | `new HttpKernelSpanSubscriber(..., routePaths: $routeNamePathMap, ...)` | +| `?RouterInterface $router = null` | `?RouteNamePathMap $routePaths = null` | + +Applies only to direct construction; services wired by the bundle need no change. + +### 22) `flow-php/etl` - sort algorithm is an explicit choice; external sort is the default + +| Before | After | +|-----------------------------------------------------------------|---------------------------------------------------------------------------| +| `SortAlgorithms` enum | removed; `ConfigBuilder::sort(memory_sort()` / `external_sort())` | +| `SortAlgorithms::MEMORY_FALLBACK_EXTERNAL_SORT` (default) | removed; default is `external_sort()` | +| `SortAlgorithms::SQLITE_SORT` | removed | +| `SortAlgorithms::useMemory()` | removed | +| `ConfigBuilder::sortMemoryLimit(Unit $unit)` | removed | +| `SortConfigBuilder` | removed; `external_sort()` builder | +| `SortConfigBuilder::sortMemoryLimit(Unit $unit)` | removed | +| `SortConfig` | `MemorySortConfig` \| `ExternalSortConfig` (`Config::$sort`) | +| `SortConfig::$memoryLimit` | removed | +| `SortConfig::SORT_MAX_MEMORY_ENV` / `FLOW_SORT_MAX_MEMORY` env | removed | +| `new MemorySort(Unit $maximumMemory)` | removed; `sort(memory_sort())` | +| `MemorySort` throwing `Flow\ETL\Exception\OutOfMemoryException` | removed | +| `new ExternalSort($cache, $bucketsCount)` | removed; `sortBy()` pipelines `BucketingProcessor` + `MergeSortProcessor` | +| `ConfigBuilder::externalSortFilesystem(string)` | `external_sort()->filesystemProtocol(string)` | +| - | `external_sort()->runSize(int)` (default `10000`) | +| - | `external_sort()->bucketsCount(int)` (default `100`) | +| - | `external_sort()->batchSize(int)` (default `1000`) | +| - | `external_sort()->storage(BucketsStorage)` (default `FilesystemBuckets`) | + +To sort in memory, opt in explicitly: + +```php +data_frame(config_builder()->sort(memory_sort()))->read(...)->sortBy(ref('id'))->run(); +``` + +### 23) `flow-php/etl` - joins emit every matching right row and follow SQL semantics + +| Before | After | +|-----------------------------------------------------------------------|------------------------------------------------------------------------| +| inner/left/right join: first matching row per probe row | one output row per matching pair | +| left/left_anti join: left row dropped on hash collision without match | left row kept | +| duplicated right side rows collapsed into one | preserved | +| `DataFrame::join(..., Join::inner)`: duplicated join columns kept | duplicated join columns dropped (empty prefix) | +| `Equal` on object values (Uuid, etc.): always `false` | compared with `==` | +| `Flow\ETL\Processor\HashJoin\HashTable` | `Flow\ETL\Join\HashJoin\HashTable` | +| `Flow\ETL\Processor\HashJoin\Bucket` | removed | +| - | `Expression::comparison()`, `All::comparisons()`, `Any::comparisons()` | + +`Rows::joinRight()` emits matched rows in left-probe order and unmatched right rows last. + +### 24) `flow-php/etl` - join buckets storage is configurable, spills to disk by default + +| Before | After | +|-----------------------------------------------------------|-------------------------------------------------------------------------| +| `join()` holds right side in memory, keeps left row order | join buckets spilled to disk (Floe files), left row order not preserved | +| - | `ConfigBuilder::join(hash_join())` | +| - | `hash_join()->storage(BucketsStorage)` (default `FilesystemBuckets`) | +| - | `hash_join()->bucketsCount(int)` (default `64`) | +| - | `hash_join()->batchSize(int)` (default `1000`) | +| - | `Flow\ETL\Bucketing\ResidentBucketsStorage` | +| - | `Flow\ETL\Bucketing\Storage\MemoryBuckets` | + +To keep the right side in memory and preserve left row order: + +```php +data_frame(config_builder()->join(hash_join()->storage(new MemoryBuckets()))) + ->read(...) + ->join(...) + ->run(); +``` + +### 25) `flow-php/etl` - `Serializer` works on `Rows` and filesystem streams + +| Before | After | +|-------------------------------------------------------------------------------------------|---------------------------------------------------------------------------| +| `Serializer::serialize(object $serializable): string` | `Serializer::serialize(Rows $rows, DestinationStream $destination): void` | +| `Serializer::unserialize(string $serialized, array $classes): object` | `Serializer::unserialize(SourceStream $source): Rows` | +| `CompressingSerializer`/`NativePHPSerializer` throw `Flow\ETL\Exception\RuntimeException` | throw `Flow\Serializer\Exception\SerializationException` | +| - | `Flow\Floe\FloeSerializer` (the default) | + +### 26) `flow-php/etl` - `EntryFactory` API reshaped + +| Before | After | +|---------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------| +| `EntryFactory::create(string $entryName, mixed $value, Schema\|Definition\|null $schema = null)` | `EntryFactory::create(string $name, mixed $value, ?Type $type = null, ?Metadata $metadata = null)` | +| `EntryFactory::createAs(string $entryName, mixed $value, Type $type, ?Metadata $metadata = null)` | `EntryFactory::cast(string $name, mixed $value, Type $type, ?Metadata $metadata = null)` | +| - | `EntryFactory::fromDefinition(Definition $definition, mixed $value): Entry` | +| `to_entry($name, $data, EntryFactory $entryFactory)` | `to_entry($name, $data, EntryFactory $entryFactory = new EntryFactory())` | +| `ConfigBuilder::build(EntryFactory $entryFactory = new EntryFactory())` | `ConfigBuilder::build()` | + +### 27) `flow-php/etl` - `Encoder`/`Hydrator` contracts; hydrator configured on the context + +| Before | After | +|---------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------| +| - | `Flow\ETL\Row\Encoder`: `encode(list): list`, `decode(list): list` | +| - | `Flow\ETL\Row\Hydrator`: `cast(list, ?Schema): Rows`, `hydrate(list, ?Schema): Rows`, `dehydrate(Rows): list` | +| - | `Flow\ETL\Row\RawRowValues`, `Flow\ETL\Row\TypedRowValues` | +| - | `Flow\ETL\Row\PhpRowHydrator`, `NativeRowHydrator`, `AdaptiveRowHydrator` (the default) | +| - | `ConfigBuilder::hydrator(Hydrator)`, `Config::hydrator()`, `FlowContext::hydrator()` | +| `array_to_row($data, EntryFactory $entryFactory, ...)` | `array_to_row($data, Hydrator $hydrator = new AdaptiveRowHydrator(), ...)` | +| `array_to_rows($data, EntryFactory $entryFactory, ...)` | `array_to_rows($data, Hydrator $hydrator = new AdaptiveRowHydrator(), ...)` | + +`cast()` coerces untrusted values through the schema (`null` schema infers types); `hydrate()` instantiates trusted, +already-typed values and requires a schema. A custom extractor reading raw values (CSV, JSON, XML, Excel, Text, Google +Sheet, PostgreSQL, Doctrine) calls `$context->hydrator()->cast(...)`; a self-describing source (Parquet, Floe) +calls `$context->hydrator()->hydrate(...)`. + +Under a schema, entries emit in schema-definition order (was data-key order with missing columns appended); a schema +column missing from the source hydrates as a typed null (was a `StringEntry` null); a column absent from the schema is +dropped. When the `flow_php` extension is loaded, `AdaptiveRowHydrator` runs hydration natively - pin the pure-PHP +engine with `config_builder()->hydrator(new PhpRowHydrator())`. + +### 28) `flow-php/etl-adapter-csv`, `-json`, `-parquet`, `-xml`, `-excel`, `-doctrine`, `-postgresql`, `-seal`, `-text`, + +`-google-sheet` - per-format `Encoder`s replace normalizers + +| Before | After | +|------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------| +| `Flow\ETL\Adapter\CSV\RowsNormalizer`, `RowsNormalizer\EntryNormalizer`, `RowsNormalizer\ScalarCast` | `Flow\ETL\Adapter\CSV\CSVEncoder` | +| `Flow\ETL\Adapter\JSON\RowsNormalizer`, `RowsNormalizer\EntryNormalizer` | `Flow\ETL\Adapter\JSON\JSONEncoder` | +| `Flow\ETL\Adapter\Parquet\RowsNormalizer` | `Flow\ETL\Adapter\Parquet\ParquetEncoder` | +| `Flow\ETL\Adapter\XML\RowsNormalizer`, `RowsNormalizer\EntryNormalizer`, `RowsNormalizer\EntryNormalizer\PHPValueNormalizer` | `Flow\ETL\Adapter\XML\XMLEncoder` | +| `Flow\ETL\Adapter\Excel\RowsNormalizer\ExcelRowsNormalizer` | `Flow\ETL\Adapter\Excel\ExcelEncoder` | +| `Flow\ETL\Adapter\Doctrine\RowsNormalizer` | `Flow\ETL\Adapter\Doctrine\DbalEncoder` | +| `Flow\ETL\Adapter\Seal\RowsNormalizer`, `RowsNormalizer\EntryNormalizer` | `Flow\ETL\Adapter\Seal\SealEncoder` | +| - | `Flow\ETL\Adapter\Text\TextEncoder`, `Flow\ETL\Adapter\PostgreSql\PostgreSqlEncoder`, `Flow\ETL\Adapter\GoogleSheet\GoogleSheetEncoder` | +| `Flow\ETL\Adapter\Doctrine\TypesMap::flowRowTypes(Row)` | `TypesMap::flowSchemaTypes(Schema)` | +| `Flow\ETL\Adapter\PostgreSql\EntryTypesMap::mapEntry(Entry)` | `EntryTypesMap::map(string $column, Type $type, mixed $value)` | +| `InsertQueryBuilder::build(Rows, ...)` | `InsertQueryBuilder::build(array $values, Schema, ...)` | +| `UpdateQueryBuilder::build(Row, ...)`, `DeleteQueryBuilder::build(Row, ...)` | `build(array $value, Schema, ...)` | +| CSV/JSON/XML/Excel/Seal writers (no `dateFormat`) | `dateFormat = 'Y-m-d'` encoder constructor param | +| Excel `timeFormat = 'H:i:s'` | `timeFormat = '%H:%I:%S'` | +| - | `withDateFormat()` on `CSVLoader`, `JsonLoader`, `JsonLinesLoader`, `XMLLoader`, `ExcelLoader`, `SealLoader`; `SealLoader::withDateTimeFormat()` | + +Behavioural changes: CSV/JSON/XML/Excel/Seal `date` columns render with `dateFormat` (`Y-m-d`, was `dateTimeFormat`); +XML `date`/`time` columns render (was `InvalidArgumentException`); Seal `time` columns render as microseconds (was +`null`); Google Sheet `_spread_sheet_id`/`_sheet_name` are appended to the schema under `putInputIntoRows()` (were +dropped when a schema was set); Excel now appends `_input_file_uri` under `putInputIntoRows()`. + +### 29) `flow-php/etl` - null is a first-class `NullEntry`/`NullDefinition`; `from_null` metadata removed + +| Before | After | +|---------------------------------------------------------------------------|--------------------------------------------------------------------------------------| +| `null_entry($name)` → `StringEntry` (type `string`, `from_null` metadata) | `null_entry($name)` → `Flow\ETL\Row\Entry\NullEntry` (type `null`) | +| `null_schema($name)` → `StringDefinition` + `from_null` metadata | `null_schema($name)` → `Flow\ETL\Schema\Definition\NullDefinition` (always nullable) | +| `Flow\ETL\Row\Entry\StringEntry::fromNull($name, $metadata)` | `null_entry($name, $metadata)` | +| `new StringEntry($name, $value, $metadata, fromNull: true)` | `$fromNull` constructor flag removed | +| `Flow\ETL\Schema\Metadata::FROM_NULL` | removed | + +### 30) `flow-php/etl` - group by runs on bucketing storage; only referenced columns are spilled + +| Before | After | +|----------------------------------------------------------------------------|--------------------------------------------------------------------------------------| +| grouping state held in memory | rows partitioned into buckets through `BucketsStorage` (default `FilesystemBuckets`) | +| rows carry all columns into grouping | only `groupBy` refs + aggregator refs are spilled | +| STRICT schema mode throws for a missing aggregate column under `groupBy()` | row gets a `null` entry for it; aggregators skip `null` | +| group output follows input order (incidental) | follows hash-bucket order | +| - | `AggregatingFunction::references(): ?array` | +| - | `ConfigBuilder::groupBy(hash_group_by())` | +| - | `hash_group_by()->storage(BucketsStorage)` (default `FilesystemBuckets`) | +| - | `hash_group_by()->bucketsCount(int)` (default `64`) | +| - | `hash_group_by()->batchSize(int)` (default `1000`) | + +Custom aggregators must implement `references()` - return the references the aggregator reads, or +`null` when they cannot be statically enumerated (disables spill column pruning). + +### 31) `flow-php/etl` - `DataFrame::pivot()` removed; pivot is declared between `groupBy()` and `aggregate()` + +| Before | After | +|---------------------------------------------------|---------------------------------------------------| +| `->groupBy(...)->aggregate(...)->pivot(ref('x'))` | `->groupBy(...)->pivot(ref('x'))->aggregate(...)` | +| `DataFrame::pivot()` | removed; `GroupedDataFrame::pivot()` only | + +### 32) `flow-php/etl-adapter-csv`, `-excel`, `-json`, + +`-xml` - explicit schema no longer projects partition columns away + +| Before | After | +|------------------------------------------------------------------|------------------------------------------------------| +| partition columns undeclared in the schema are dropped from rows | force-added to rows as non-nullable `string` columns | + +Declare the partition column in the schema to control its type. + +### 33) `flow-php/etl` - `Schema` and `Schema\Definition` mutators return a new instance + +| Before | After | +|-----------------------------------------|------------------------------------------------------| +| `$schema->add(str_schema('x'));` | `$schema = $schema->add(str_schema('x'));` | +| `$schema->addMetadata('id', 'k', 'v');` | `$schema = $schema->addMetadata('id', 'k', 'v');` | +| `$definition->setMetadata($metadata);` | `$definition = $definition->setMetadata($metadata);` | + +Assign the result of every `Schema` mutator - `add`, `addAfter`, `addBefore`, `addMetadata`, `gracefulRemove`, +`insertAt`, `keep`, `makeNullable`, `merge`, `moveAfter`, `moveBefore`, `moveTo`, `prepend`, `remove`, `rename`, +`reorder`, `replace`, `setMetadata`, `sort` - and of `Definition::addMetadata()` / `Definition::setMetadata()`. +Discarding it is a silent no-op. diff --git a/documentation/upgrading.md b/documentation/upgrading.md index 6d9cec5d8e..3af04e966c 100644 --- a/documentation/upgrading.md +++ b/documentation/upgrading.md @@ -5,7 +5,49 @@ This document provides guidelines for upgrading between versions of Flow PHP. Please follow the instructions for your specific version to ensure a smooth upgrade process. -Upgrading from older versions: [0.41.x to 0.43.x](/documentation/upgrading-0.41-0.43.md) +Upgrading from older versions: [0.41.x to 0.44.x](/documentation/upgrading-0.41-0.44.md) + +--- + +## Upgrading from 0.48.x to 0.49.x + +### 1) `flow-php/postgresql` - `cte()` and `CTE` lose the `recursive` argument + +| Before | After | +|-----------------------------------------------------------|-------------------------------------------------------------------------------------------------| +| `cte($name, $query, $columns, $materialization, true)` | `with(cte($name, $query, $columns, $materialization))->recursive()` | +| `new CTE($name, $node, $columns, $materialization, true)` | `new CTE($name, $node, $columns, $materialization)` in `new WithClause([...], recursive: true)` | +| `CTE::recursive()` | removed. Use `WithClause::recursive()` | + +Before: + +```php +with(cte('tree', $query, recursive: true))->select(star())->from(table('tree')); +``` + +After: + +```php +with(cte('tree', $query))->recursive()->select(star())->from(table('tree')); +``` + +### 2) `flow-php/postgresql` - the DSL `returning()` / `returning_all()` and the static `ReturningClause` factories are removed + +| Before | After | +|--------------------------------------------------------|---------------------------------------------------------------------------| +| `returning('id', col('name'))` | removed. Use `->returning(col('id'), col('name'))` on the builder | +| `returning_all()` | removed. Use `->returningAll()` on the builder | +| `ReturningClause::all()`, `ReturningClause::columns()` | removed. Use `(new ReturningClause())->returning(...)` | +| `ReturningClause::toAstNodes($clause)` | removed. Use `$clause->toAst()->getReturningClause()?->getExprs()` | +| `ReturningClause::toAst()` gives the first `ResTarget` | gives a `Node` with the whole `ReturningClause`, aliases included | + +### 3) `flow-php/postgresql` - `->returning()` with no expressions throws + +| Before | After | +|---------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| +| `delete()->from('t')->returning()` wrote no RETURNING clause | throws `InvalidArgumentException`. Call `returningAll()` for `RETURNING *`, or remove the call | + +Applies to the delete, update, insert and merge builders. --- @@ -3910,1530 +3952,3 @@ Other protocols are unchanged (`memory://path`, `s3://bucket/key`). Windows driv | an extension built for another package version: `Class "...\RustBackend" not found` | `RuntimeException` `The loaded flow_php extension (, ABI ) does not match ...` | Rebuild or upgrade the extension with the package, or stop loading it. - ---- - -## Upgrading from 0.43.x to 0.44.x - -### 1) `flow-php/etl-adapter-json` - `to_json()`/`to_json_lines()` write list/map/structure/array entries as nested JSON - -| Before | After | -|-------------------------------------------------------------------|------------------------------------------------| -| `{"tags":"[1,2,3]"}` (escaped JSON string) | `{"tags":[1,2,3]}` | -| nested `DateTimeInterface`/`Uuid`/`UnitEnum` `json_encode`d as-is | datetime format / canonical string / case name | -| non-array value under a container type -> `""` | `null` | - -### 2) `flow-php/etl` - Floe inferred schema keeps the first batch's nullability - -| Before | After | -|--------------------------------------------------------------------|-------------------------------------------------| -| inferred schema - every column nullable | nullability taken from the first batch's values | -| `null` in batch >= 2 in a column non-nullable in batch 1 - written | throws | - -Columns that may only become null in later batches, declare the schema explicitly: -`to_floe($path)->withSchema($schema)`. - -### 3) `flow-php/types` - `detectType([])` returns `list` - -| Before | After | -|----------------------------------------------------------|--------------| -| `(new TypeDetector())->detectType([])` -> `array` | `list` | - -### 4) `flow-php/types` - `detectType()` returns different types for arrays - -| Before | After | -|----------------------------------------------------------------------------------------------------------|------------------------------------------------| -| `detectType([5 => 'a', 6 => []])` -> `map` | `array` | -| `detectType(['a', []])` -> `list` | `array` | -| `detectType([['id' => '1'], []])` -> `list` | `array` | -| `detectType([[1, 2], [null]])` -> `list>` | `list>` | -| `detectType([[], [1, 2]])` -> `list>` | `list>` | -| `detectType([1 => 1, 5 => 2.5])` -> `array` | `map` | -| `detectType(['name' => 'x', 'latlng' => [33, 65.5]])` -> `structure{name: string, latlng: array}` | `structure{name: string, latlng: list}` | - -### 5) `flow-php/types` - `cast()` refuses values it used to coerce, fabricate or wrap - -| Before | After | -|-----------------------------------------------------------------------------------------------------------------------------------------|---------------------------| -| `type_structure(['id' => type_integer(), 'name' => type_string()])->cast(['id' => 1])` -> `['id' => 1, 'name' => '']` | throws `CastingException` | -| same type, `->cast(['id' => 1, 'name' => null])` -> `['id' => 1, 'name' => '']` | throws `CastingException` | -| same type, `->cast([])`, `->cast(null)` -> `['id' => 0, 'name' => '']` | throws `CastingException` | -| `type_structure(['id' => type_integer()], ['name' => type_string()])->cast(['id' => 1, 'name' => null])` -> `['id' => 1, 'name' => '']` | throws `CastingException` | -| `type_list(type_string())->cast(null)` -> `['']` | throws `CastingException` | -| `type_structure(['id' => type_integer()])->cast('{"id":"1"}')` -> throws | `['id' => 1]` | -| `type_list(type_integer())->cast('["1","2"]')` -> throws | `[1, 2]` | -| `type_integer()->cast('abc')` -> `0` | throws `CastingException` | -| `type_integer()->cast('2024-01-01')` -> `2024` | throws `CastingException` | -| `type_integer()->cast([1, 2, 3])` -> `1` | throws `CastingException` | -| `type_integer()->cast(null)` -> `0` | throws `CastingException` | -| `type_integer()->cast('9223372036854775808')` -> `9223372036854775807`; also `'9223372036854775807.0'`, `type_positive_integer()` | throws `CastingException` | -| `type_integer()->cast(1e300)` -> `0` | throws `CastingException` | -| `type_integer()->cast(new DOMElement('e', 'abc'))` -> `0` | throws `CastingException` | -| `type_float()->cast('abc')` -> `0.0` | throws `CastingException` | -| `type_float()->cast([1])` -> `1.0` | throws `CastingException` | -| `type_float()->cast(null)` -> `0.0` | throws `CastingException` | -| `type_boolean()->cast('weird')` -> `true` | throws `CastingException` | -| `type_boolean()->cast('')` -> `false` | throws `CastingException` | -| `type_boolean()->cast([1, 2, 3])` / `->cast(new DateTimeImmutable())` -> `true` | throws `CastingException` | -| `type_boolean()->cast(null)` -> `false` | throws `CastingException` | -| `type_date()->cast('now')` / `type_datetime()->cast('now')` -> current time; also `''`, `'t'`, `'+12'`, `'yesterday'`, `'10:00:00'` | throws `CastingException` | -| `type_datetime()->cast('@1609459200')` -> `2021-01-01`; also `'2024-01'`, `'2024-001'`, and under `type_date()` | throws `CastingException` | -| `type_string()->cast(null)` / `type_scalar()->cast(null)` -> `''` | throws `CastingException` | -| `type_list(type_string())->cast([null])` -> `['']` | throws `CastingException` | -| `type_array()->cast('abc')` -> `['abc']` | throws `CastingException` | -| `type_list(type_integer())->cast(5)` -> `[5]` | throws `CastingException` | -| `type_array()->cast(null)` -> `[]` | throws `CastingException` | -| `type_integer()->cast(new DateTimeImmutable('@1700000000'))` -> `1700000000000000` | `1700000000` | -| `type_float()->cast(new DateTimeImmutable('@1700000000.5'))` -> `1700000000500000.0` | `1700000000.5` | -| `type_integer()->cast(new DateInterval('PT90S'))` -> `90000000` | `90` | -| `type_float()->cast(new DateInterval('PT90S'))` -> `90000000.0` | `90.0` | - -### 6) `flow-php/etl` - `FilesystemStreams` replaced by `FilesSink`, and a failed run discards its sink - -| Before | After | -|-----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| -| `Flow\ETL\Filesystem\FilesystemStreams` | `Flow\ETL\Filesystem\FilesSink` | -| `FilesystemStreams::FLOW_TMP_FILE_PREFIX` | `FilesSink::FLOW_TMP_FILE_PREFIX` | -| `new FilesystemStreams()` + `->setMode($mode)` | `new FilesSink($filesystem, $destination, $mode)` | -| `$streams->writeTo($filesystem, $path, $partitions)` | `$files->writeTo($partitions)` | -| `$streams->isOpen($path, $partitions)` | `$files->touched($partitions)` | -| `$streams->listOpenStreams($path)` | `$files->openStreams()` | -| `$streams->closeStreams($filesystem, $path)` | `$files->publish()` / `$files->abandon()` | -| `$streams->read()` / `->rm()` / `->exists()` / `count()` / `getIterator()` | removed - use the `Filesystem` directly | -| `saveMode()` on one `DataFrame` applied to every later `DataFrame` on the same `Config` | `saveMode()` is set on the sink and belongs to that sink alone | -| a failed run left its `DestinationStream` registered; the retry appended to it | the failed run's sink is discarded, the retry starts clean | -| an abandoned run left its `._flow_php_tmp.` file behind under `Overwrite` | the abandoned run removes it and never renames it over the destination | -| a failed run left its partial file at the destination under the other save modes | it removes any file it created; a destination it did not create is untouched | -| a run that threw or was abandoned never reached its loaders | loaders implementing `Flow\ETL\Loader\Discardable` receive `discard(FlowContext $context)` | -| `Config::filesystemStreams()` | removed | -| `new Config(..., FilesystemStreams $filesystemStreams, ...)` constructor parameter | removed | - -Build `Config` through `Config::builder()` / `Config::default()`. A `Loader` holding per-run state (an open stream, a -writer, a counter) implements `Discardable` and drops that state in `discard()`. - -### 7) `flow-php/etl` - `RetryLoader` no longer retries `InvalidLogicException` by default - -| Before | After | -|----------------------------------------------------------------------|-----------------------------------------------------------------| -| `new RetryLoader($loader)` default strategy `new AnyThrowable(3)` | `new AnyThrowableExcept([InvalidLogicException::class], 3)` | -| `write_with_retries($loader)` default strategy `new AnyThrowable(3)` | `new AnyThrowableExcept([InvalidLogicException::class], 3)` | -| an `InvalidLogicException` was attempted 4 times with delays between | attempted once, no delay | -| - | `retry_any_throwable_except([InvalidLogicException::class], 3)` | - -To keep retrying every throwable: - -```php -write_with_retries($loader, retry_any_throwable(3)); -``` - -### 8) `flow-php/etl` - operations inside a `Transformation` answer for the whole stream - -| Before | After | -|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `$df->sortBy(ref('id'))` inside a `Transformation` sorted each batch on its own | sorts the whole stream | -| `$df->aggregate(...)` / `groupBy()->aggregate()` / `pivot()` / window functions inside a `Transformation` answered per batch | answer for the whole stream | -| `$df->offset(2)` inside a `Transformation` lost rows | skips exactly the offset across the stream | -| `$df->cache($id)` inside a `Transformation` persisted one batch | persists the whole stream | -| `$df->batchBy(...)` / `batch_size(...)` cut chunks at the incoming batches | cut chunks over the stream | -| `$df->join(...)` inside a `Transformation` emitted rows in input order | emits rows grouped by key | -| `$df->partitionBy(...)` inside a `Transformation` | removed - `$df->repartition(...)` groups the whole stream by key, see 60) | -| a `Transformation` calling `$df->fetch()` / `count()` / `schema()` silently answered over an empty stream | throws `InvalidLogicException` | -| `write_with_retries($loader)` around `to_transformation(...)` (any wrapped step) or around a `to_branch(...)` armed with `withTransformation(...)`, at any nesting depth | throws `InvalidLogicException` at the first `load()`, use `to_transformation(..., write_with_retries($loader))` or `to_branch(..., write_with_retries($loader))->withTransformation(...)` | -| `Flow\ETL\Extractor\SwappableRowsExtractor` | removed | - -### 9) `flow-php/etl` - `to_branch()->withTransformation()` drives its `Transformation` once over the whole stream - -| Before | After | -|-----------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------| -| the `Transformation` ran on each filtered batch in its own `DataFrame` | one nested pipeline spans the stream | -| `$df->sortBy(...)` in a branch transformation sorted each batch alone | sorts the whole branch stream | -| `$df->aggregate(...)` / `limit()` / other `Processor`-backed operations answered per batch | answer once for the stream | -| a `Transformation` calling `$df->fetch()` / `count()` / `schema()` returned per-batch answers | throws `InvalidLogicException` | -| the wrapped loader received exactly one `load()` per outer batch | receives output as the transformation produces it; blocking operations deliver at `closure()` | -| telemetry `flow.etl.loading.rows` counted post-filter, post-transformation rows | counts the rows offered to the branch | - -### 10) `flow-php/etl-adapter-doctrine`, `-postgresql` - transactional loaders run `closure()` in a transaction - -| Before | After | -|-------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------| -| `to_dbal_transaction()` / `to_pgsql_transaction()` never called `closure()` on wrapped loaders | forwards `closure()` to every wrapped loader, inside one final transaction | -| blocking operations inside a wrapped `Transformation` answered per batch, each batch in its own transaction | answer for the whole stream, delivered at `closure()` in a single transaction | -| - | a failure during the final transaction rolls back the drained delivery and rethrows | -| `withIsolationLevel()` applied to per-batch transactions | applies to every transaction the wrapper opens | - -### 11) `flow-php/etl` - Floe on-disk format v2, existing `.floe` files must be rewritten - -| Before | After | -|-------------------------------------|-------------------------------------------------| -| header version byte `0x01` | `0x02` | -| uuid payload - 36 raw bytes | 4-byte little-endian length prefix + bytes | -| partition values stored in the file | not stored - read from the directory path only | -| reading a v1 file | throws `Floe does not support format version 1` | - -Files written by 0.43.x cannot be read. Regenerate them from their source, or export them with 0.43.x to another -format before upgrading. - -### 12) `flow-php/etl` - Floe rejects columns whose type is only known per value - -| Before | After | -|--------------------------------------------|------------------------------------------------------------| -| `list` element - written with a tag | `Floe does not support values of type "mixed"` | -| `union_schema()` column - written | `Floe does not support columns of type "integer\|string"` | -| `type_structure(..., allow_extra: true)` | `Floe does not support structures that allow extra values` | -| map key other than `integer`/`string` | `Floe does not support map keys of type "..."` | - -Declare an element type, or declare the column with `json_schema()` when the shape is dynamic. - -### 13) `flow-php/etl` - Floe validates every value against its column type - -| Before | After | -|--------------------------------------------------------------------------------------------------|------------------------------------------------------------| -| `'AB-1'` into an `integer` column - wrote `0` | throws `IncompatibleSchemaException` | -| `1.5` into an `integer` column - wrote `1` | throws | -| `1000` into a `string` column - raw `TypeError` | throws `IncompatibleSchemaException` | -| `int` into a `float` column - written | throws | -| `null` into a non-nullable column - written | throws | -| a batch without a NOT NULL session column - written | throws `IncompatibleSchemaException` | -| a batch without a nullable session column | unchanged, reads back as `null` | -| `IncompatibleSchemaException` - `column "x" (...) is not compatible with the session type (...)` | a `Missing Definitions:` / `Mismatched Definitions:` block | -| `floe_options(validate_data: false)` / `Options::withValidateData()` | removed | -| `floe_options($validate_data, $buffer_size, $codec)` | `floe_options($buffer_size, $codec)` | -| `new Options($validateData, $bufferSize, $codec)` | `new Options($bufferSize, $codec)` | - -### 14) `flow-php/etl` - aggregate result type no longer depends on the value - -| Before | After | -|---------------------------------------------------------------------|---------| -| `sum()` over a `float` column, whole total - `int` | `float` | -| `avg()`/`min()`/`max()` over a `float` column, whole result - `int` | `float` | -| `sum()` over an `integer` column - `int` | `float` | - -### 15) `flow-php/etl` - aggregates ignore a row missing the aggregated column - -| Before | After | -|---------------------------------------------------------------------------------------------------|--------------------------------------------| -| missing column under `$df->mode(execution_strict())` - `Sum error: Entry "amount" does not exist` | contributes nothing | -| missing column in the default mode - contributed nothing | unchanged | -| a column no row declares - contributed nothing | throws `SchemaDefinitionNotFoundException` | - -### 16) `flow-php/types` - `EnumType::isValid()` requires an object - -| Before | After | -|-------------------------------------------|-----------------------------------------------------| -| `type_enum(Suit::class)->isValid('Suit')` | `false` (was `true`) | -| `type_enum(Suit::class)->assert('Suit')` | throws `InvalidTypeException` (was raw `TypeError`) | -| `type_enum(Suit::class)->cast('Suit')` | throws `CastingException` (was raw `TypeError`) | - -### 17) `flow-php/etl` - filesystems are passed to sources and sinks, engine algorithms take a storage - -#### Filesystems - -| Before | After | -|----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------| -| `config_builder()->mount(aws_s3_filesystem(...))` + `from_csv(path('aws-s3://x.csv'))` | `from_csv(path('aws-s3://x.csv'), filesystem: aws_s3_filesystem(...))` | -| `config_builder()->unmount($fs)` | removed | -| `$config->fstab()` | removed - use `Flow\Filesystem\DSL\fstab()` for `file_copy()` / `file_move()` | -| `$context->filesystem($path)` | removed - pass `filesystem:` to the source or sink | -| `$context->streams()->list($path, $filter)` -> `SourceStream` | `(new Flow\Filesystem\FileListing($filesystem))->list($path, $filter)` -> `FileStatus` | -| `$context->streams()->writeTo($path, $partitions)` | `$files->writeTo($partitions)` on a sink-held `FilesSink` - see 6) | -| `$context->streams()->closeStreams($path)` in an extractor | removed | -| `new FilesystemStreams($filesystemTable)` | `new FilesSink($filesystem, $destination, $saveMode)` - see 6) | -| a custom file source reading through `FlowContext` | takes `Filesystem $filesystem = new NativeLocalFilesystem()` as its last constructor argument | -| `schema_from_json_schema($s)` resolved local `$ref`s through `fstab()` | takes a trailing `Filesystem $filesystem = new NativeLocalFilesystem()` | -| a `$ref` on `memory://` or `stdout://` | throws - pass the filesystem that serves it | -| `ChartJSLoader::withOutputPath($path)` / `::withTemplate($path)` | both take a trailing `Filesystem $filesystem = new NativeLocalFilesystem()`; `withOutputPath()` throws on a path with no extension | -| `FilePathArgument::getExisting($input, $config)` / `::getNotExisting(...)` (CLI) | `getExisting($input)` / `getNotExisting($input)`; the constructor takes the `Filesystem` | -| a custom `FileLoader` | implements `saveMode()` and `Discardable` - see 6) | -| a custom `Flow\Filesystem\Filesystem` implementation | adds `public function supports(Path $path): bool` - `return $this->mount()->supports($path);` | - -Two `memory_filesystem()` calls are two separate stores: pass the same instance to the writer and the reader. - -#### Filesystem telemetry - -| Before | After | -|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------| -| `withTelemetry()` traced every filesystem: `filesystem.read` / `filesystem.write` spans and filesystem metrics | filesystems are not traced - wrap the filesystem with `traceable_filesystem()` | -| `telemetry_options(filesystem: filesystem_telemetry_options(...))` | removed | -| `TelemetryOptions::filesystem()` / `->filesystem` | removed | -| the pipeline-start debug log's `fstab` field | a `spill` field naming the three spill storages | - -After: - -```php -$fs = traceable_filesystem( - aws_s3_filesystem($bucket, $client), - filesystem_telemetry_config($telemetry, $clock, filesystem_telemetry_options(trace_streams: true)), -); - -data_frame()->read(from_csv(path('aws-s3://x.csv'), filesystem: $fs))->run(); -``` - -#### Engine algorithms - -| Before | After | -|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------| -| `config_builder()->cacheFilesystem('s3')` | `config_builder()->cache($cache)` | -| `external_sort()->filesystemProtocol('file')` | `external_sort()->storage(new FilesystemBuckets($fs, $path))` | -| `hash_join()->filesystemProtocol(...)` / `hash_group_by()->filesystemProtocol(...)` | `->storage(BucketsStorage $storage)` | -| `CacheConfig::$filesystemMount` | removed | -| `SortAlgorithmBuilder::build(FilesystemTable, Path)` | `build(Path $spillRoot)` | -| `new ExternalSortConfig($bucketing, $runSize)` | `new ExternalSortConfig($bucketing, ?BucketsStorage $merge = null, $runSize)` - use named arguments | -| - | `external_sort()->mergeStorage($s)` - merged runs only | - -#### Per-operation algorithm overrides - -| Before | After | -|--------------------------------------|-------------------------------------------------------------------------------------| -| `$df->sortBy(ref('a'), ref('b'))` | `$df->sortBy([ref('a'), ref('b')])` | -| `$df->groupBy('a', 'b')` | `$df->groupBy(['a', 'b'])` | -| `$df->aggregate(sum(ref('a')), ...)` | `$df->aggregate([sum(ref('a')), ...])` | -| - | `$df->sortBy([ref('a')], external_sort()->storage(new MemoryBuckets()))` | -| - | `$df->groupBy(['a'], hash_group_by()->storage($s))` | -| - | `$df->join($right, $on, Join::left, hash_join()->storage($s))` | -| - | `$df->cache('report', cache: $psrCache)` + `from_cache('report', cache: $psrCache)` | - -`GroupedDataFrame::aggregate()` stays variadic: `$df->groupBy(['a'])->aggregate(sum(ref('b')))`. - -#### Save mode, the CLI and the HTTP bridge - -| Before | After | -|---------------------------------------------------------------------|--------------------------------------------------------------------------------------| -| `$df->saveMode(overwrite())` / `$df->mode(overwrite())` | `to_csv($path)->saveMode(overwrite())` - per sink | -| `$df->saveMode(overwrite())->write(write_with_retries(to_csv($p)))` | `$df->write(write_with_retries(to_csv($p)->saveMode(overwrite())))` | -| `to_text($path)` returned `Loader` | returns `TextLoader` | -| `LoaderFactory::get()` returned `Loader` | returns `Loader&FileLoader` | -| `flow read --config .flow.php aws-s3://bucket/x.csv` | `flow pipeline:run pipeline.php`, with the filesystem built inside the pipeline file | -| `new FlowBufferedResponse(..., filesystem: 'memory')` | `new FlowBufferedResponse(..., filesystem: new MemoryFilesystem())` | -| `new FlowStreamedResponse(..., filesystem: 'stdout')` | `new FlowStreamedResponse(..., filesystem: new StdOutFilesystem())` | -| `Output::loader(Path $path)` | `Output::loader(Path $path, Filesystem $filesystem)` | - -### 18) `flow-php/etl` - `equals()` / `notEquals()` return `null` when either side is `null` - -| Before | After | -|---------------------------------------------------------------------------------|-----------------------------------| -| `ref('a')->equals(ref('b'))`, both `null` - `true`, `filter()` keeps the row | `null`, `filter()` drops the row | -| `ref('a')->equals(lit(5))`, `a` is `null` - `false` | `null` | -| `ref('a')->notEquals(lit(5))`, `a` is `null` - `true`, `filter()` keeps the row | `null`, `filter()` drops the row | -| `ref('a')->same(ref('b'))` / `ref('a')->notSame(lit(5))` | unchanged - `null` same as `null` | - -To keep the old result, switch to `same()` / `notSame()`: - -Before: - -```php -$df->filter(ref('a')->equals(ref('b'))); -$df->filter(ref('a')->notEquals(lit(5))); -``` - -After: - -```php -$df->filter(ref('a')->same(ref('b'))); -$df->filter(ref('a')->notSame(lit(5))); -``` - -`same()` compares with `===`: over an integer column `ref('i')->same(lit(1.0))` is `false` where `equals()` is `true`. - -### 19) `flow-php/etl` - execution modes are removed, a function given bad data throws - -| Before | After | -|--------------------------------------------------------------------------------|-------------------------------------------------------------------------| -| default mode: a function given bad data returned `null` | throws - wrap it: `optional($function)` | -| `$df->mode(execution_lenient())` | removed - `optional($function)` per function | -| `$df->mode(execution_strict())` | removed - strict is the only behaviour | -| `coalesce($a, $b)` skipped a branch that threw | rethrows - `coalesce(optional($a), $b)` | -| `cast($v, type_integer())` over `null` returned `null` | throws - `cast($v, type_optional(type_integer()))` | -| `ref('l')->onEach($fn)` set an element whose `$fn` threw to `null` | throws - `ref('l')->onEach(optional($fn))` | -| `ref('s')->indexOf('a')` / `->indexOfLast('a')` over `null` - `false` | throws - `optional(ref('s')->indexOf('a'))` | -| `DataFrame::mode()` | removed | -| `Flow\ETL\Function\ExecutionMode`, `execution_lenient()`, `execution_strict()` | removed | -| `Flow\ETL\Function\Functions`, `FlowContext::functions()` | removed - a custom function throws instead of calling `invalidResult()` | - -Before: - -```php -data_frame()->read($extractor)->withEntry('payload', ref('json')->jsonDecode())->run(); -``` - -After: - -```php -data_frame()->read($extractor)->withEntry('payload', optional(ref('json')->jsonDecode()))->run(); -``` - -### 20) `flow-php/etl` - `not()`, `all()`, `any()` and `isType()` return `null` for a `null` operand - -| Before | After | -|-----------------------------------------------------------------------------------|-----------------------------------| -| `not(ref('a')->equals(lit(1)))`, `a` is `null` - `true`, `filter()` keeps the row | `null`, `filter()` drops the row | -| `all(ref('a')->equals(lit(1)), lit(true))`, `a` is `null` - `false` | `null` | -| `any(ref('a')->equals(lit(1)), lit(false))`, `a` is `null` - `false` | `null` | -| `ref('a')->isType(type_null())`, `a` is `null` - `true` | `null` - use `ref('a')->isNull()` | - -`isNull()`, `isNotNull()`, `same()`, `notSame()` and `exists()` still return `true` or `false`. - -### 21) `flow-php/etl` - expressions and steps are checked before the first row is read - -| Before | After | -|--------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------| -| `$df->filter(ref('id')->greaterThan(ref('created_at')))`, `integer` vs `datetime` - threw at the first row | throws at `schema()` / `run()`, before any row | -| the same failure under `onError(skip_rows_handler())` - handled per row by the error handler | throws; the error handler is not consulted | -| `ref('a')->plus(ref('d'))` (`integer` + `date`) over a source with no rows - no rows | throws `Cannot combine types "integer", "date" - an explicit cast is required.` | -| `$df->filter(ref('id'))` / `$df->until(ref('id'))` / `to_branch(ref('id'), $loader)` over an `integer` column - accepted | throws - compare explicitly: `ref('id')->notEquals(lit(0))` | -| `greatest(ref('i'), ref('s'))` / `least(...)` over `integer` and `string` - `'apple'` for `5`, `'apple'` | throws `InvalidTypeException` - cast one side | -| `coalesce(ref('i'), ref('s'))` / `when($c, ref('i'), ref('s'))` / `match_cases()` with `integer` and `string` arms - typed per value | `string` column | -| `coalesce(ref('i'), lit(true))` - typed per value | throws `InvalidTypeException` | -| `array_expand(ref('j'))` over a `json` column - typed per value | throws `SchemaNotDerivableException` | -| `ref('u')` / `select('u')` over a column declared `integer\|string` - one type per value | throws `UnsupportedUnionTypeException` - declare one type, e.g. `str_schema('u')` | - -Applies to frames read through `from_data_frame()` and to `to_transformation()` bodies: the inner error fails the -outer `run()` / `schema()`. - -### 22) `flow-php/etl` - functions return only values their column type can hold - -| Before | After | -|--------------------------------------------------------------------------------------|---------------------------------------| -| `ref('d')->toDateTime('Y-m-d')` over an unparseable string - `false` | `null` | -| `ref('j')->jsonDecode()` over scalar JSON (`'1'`) - `1` | throws - `cast()` scalar JSON | -| `ulid()` - `Symfony\Component\Uid\Ulid` | base32 `string` | -| `ref('x')->domElementParent()` of a root element - its `DOMDocument` | `null` | -| `ref('x')->xpath($path)` - every matched node, text and attribute nodes included | elements only, `null` when none match | -| `when($condition, $then, $else)` with `$else` of `0`, `false`, `''` or `[]` - `null` | `$else` | -| `round($v, 0)` / `ref('v')->round(0)` - `int` | `float` | - -### 23) `flow-php/etl` - function arguments that decide the output type take plain values - -| Before | After | -|-----------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------| -| `array_sort($a, $sort_function)` - `ScalarFunction\|Sort\|null` | `?Sort` | -| `array_reverse($a, $preserveKeys)` / `->arrayReverse($preserveKeys)` - `ScalarFunction\|bool` | `bool` | -| `->onEach($fn, $preserveKeys)` - `ScalarFunction\|bool` | `bool` | -| `regex()` / `regex_all()` / `->regex()` / `->regexAll()` `$flags` - `ScalarFunction\|int` | `int`; `PREG_OFFSET_CAPTURE`, `PREG_UNMATCHED_AS_NULL` (and `PREG_SET_ORDER` for `regex_all()`) throw at construction | -| `array_get($ref, $path)` / `->arrayGet($path)` - `ScalarFunction\|string` | `string`; a wildcard path throws - use `array_get_collection()` | -| `array_key_rename($ref, $path, $newName)` - `ScalarFunction\|string` | `string` | -| `uuid_v7()` / `uuid_v7(null)` | `uuid_v7($value)` - `$value` required | -| `->domElementNextSibling()` / `->domElementPreviousSibling()` - next / previous node, text included | next / previous element | -| `->domElementNextSibling(true)` / `->domElementPreviousSibling(true)` | argument removed | -| `ref('a')->isType('nope')` - threw at the first row | throws at construction | -| `new Round($value)` - `$precision` defaults to `0` | defaults to `2`, as `round()` | - -### 24) `flow-php/etl` - `cast()` accepts column types only - -| Before | After | -|----------------------------------------------------------------|---------------------------------------------------| -| `cast($v, 'json_pretty')` | removed - throws at construction; use `'json'` | -| `cast($v, 'object')` / `'mixed'` / `'callable'` / `'resource'` | throws `InvalidArgumentException` at construction | - -### 25) `flow-php/etl` - `EntryReference` is renamed to `UnresolvedReference` and is immutable - -| Before | After | -|---------------------------------------------------------|-----------------------------------------------| -| `Flow\ETL\Row\EntryReference` | renamed to `Flow\ETL\Row\UnresolvedReference` | -| `ref()` / `col()` / `entry()` return `EntryReference` | return `UnresolvedReference` | -| `$ref->as('b')` / `->asc()` / `->desc()` changed `$ref` | return a copy - use the returned reference | - -### 26) `flow-php/etl` - custom functions declare their output type and their children - -| Before | After | -|---------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------| -| `class MyFunction extends ScalarFunctionChain` | `final class MyFunction implements ScalarFunction` + `use ScalarFunctionChain;` | -| `ScalarFunction` - `eval()` | also `returns(): Type`, `children(): array`, `withChildren(array $children): static`, `resolved(): bool` | -| a custom `Reference` | implements the `ScalarFunction` members above - `Reference` extends `ScalarFunction` | -| `AggregatingFunction::result(EntryFactory $entryFactory): Entry` | removed - implement `outputName(): string`, `value(): mixed`, `returns(): Type` and the `FunctionTree` members | -| `WindowFunction` | adds `returns(): Type` and the `FunctionTree` members; `over()` returns a copy | -| `Flow\ETL\Function\ScalarFunction\ScalarResult`, `ScalarResult::from()` | removed - return the plain value, declare the type in `returns()` | -| `FrameAccumulator::value(): mixed` | `value(): float\|int\|null` | -| `Parameter::as*()` over a malformed value - `null` / `false` / `$default` | throws `InvalidArgumentException` | -| `Parameter::asBoolean(): bool` | `?bool` | -| `Parameter::asType()` | removed | - -Before: - -```php -final class Shout extends ScalarFunctionChain -{ - public function __construct(private readonly ScalarFunction $value) {} - - public function eval(Row $row, FlowContext $context): ?string - { - $value = (new Parameter($this->value))->asString($row, $context); - - return $value === null ? null : strtoupper($value); - } -} -``` - -After: - -```php -final class Shout implements ScalarFunction -{ - use ScalarFunctionChain; - use ResolvesFromChildren; - - public function __construct(private readonly ScalarFunction $value) {} - - public function eval(Row $row, FlowContext $context): ?string - { - $value = (new Parameter($this->value))->asString($row, $context); - - return $value === null ? null : strtoupper($value); - } - - public function returns(): Type - { - return type_optional(type_string()); - } - - public function children(): array - { - return [$this->value]; - } - - public function withChildren(array $children): static - { - return new self($children[0]); - } -} -``` - -### 27) `flow-php/etl` - user callbacks removed, `call()` takes a `ScalarFunction` and a return type - -| Before | After | -|----------------------------------------------------------------|--------------------------------------------------------------------------| -| `$df->map($callback)` | removed | -| `Flow\ETL\Transformer\CallbackRowTransformer` | removed | -| `to_callable($callable)` / `Flow\ETL\Loader\CallbackLoader` | removed - implement `Flow\ETL\Loader` | -| `Flow\ETL\Transformer\StyleConverter\ArrayKeyConverter` | removed | -| `call('strtoupper', [ref('name')])` | `call(lit('strtoupper'), type_string(), [ref('name')])` | -| `call($callable, $parameters, $return_type = null)` | `call(lit($callable), $return_type, $parameters)` - return type required | -| `->call($callable, $arguments, $refAlias, $returnType = null)` | `->call(lit($callable), $returnType, $arguments, $refAlias)` | -| `new CallUserFunc($callable, $parameters, $returnType)` | `new CallUserFunc($callable, $returnType, $parameters)` | -| `call(..., type_string())` column - `string` | `?string` | -| `lit($closure)`, or a `Closure` inside a `lit()` array | throws `InvalidArgumentException` | -| `(new TypeDetector())->detectType($closure)` | throws `Flow\Types\Exception\InvalidArgumentException` | - -Before: - -```php -$df->map(fn (Row $row): Row => $row->set(str_entry('name', $row->valueOf('name') ?? 'default'))); -``` - -After: - -```php -$df->withEntry('name', coalesce(ref('name'), lit('default'))); -``` - -Logic no scalar function expresses: implement `Flow\ETL\Transformer` and pass it to `$df->with($transformer)`. - -### 28) `flow-php/etl` - `Transformer` requires `bind()` - -| Before | After | -|------------------------------------------------------------------|---------------------------------------------------------| -| `Transformer::transform(Rows $rows, FlowContext $context): Rows` | plus `bind(Schema $input): Flow\ETL\Pipeline\BoundStep` | - -Return `new BoundStep($this, $output)`, where `$output` is the schema the step emits for `$input`. - -### 29) `flow-php/etl` - `Extractor` gains `schema()` and `withSchema()` - -| Before | After | -|----------------------------------------------------------------------|------------------------------------------------------------------| -| `Extractor` declares only `extract(FlowContext $context): Generator` | also `schema(): Schema` and `withSchema(Schema $schema): static` | -| a custom `Extractor` implementing only `extract()` | fatal at load | - -A source that cannot describe its output before reading throws -`Flow\ETL\Exception\SchemaNotDerivableException::extractor(self::class)` from `schema()`. - -### 30) `flow-php/etl` - `Entry` and the `*_entry()` functions are removed, a row is a name-keyed array under a `Schema` - -| Before | After | -|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| -| `Flow\ETL\Row\Entry` and the 18 `Flow\ETL\Row\Entry\*Entry` classes | removed | -| `Flow\ETL\Row\Entries`, `entries(...)` | removed | -| `row(Entry ...$entries)`, `Row::create(...)`, `Row::with(...)` | `row(array $values)` - `row(['id' => 1])` | -| `rows(Row ...$rows)`, `new Rows(Row ...$rows)` | `rows(Schema $schema, Row ...$rows)`, `new Rows(Schema $schema, Row ...$rows)` - see 31) | -| `$row->get('id')` returned an `Entry` | returns the value | -| `$row->valueOf('id')` | `$row->get('id')` | -| `$row->entries()` | `$row->values()` / `$row->names()` | -| `$row->schema()` | `$rows->schema()` | -| `$row->hash()` | `$row->hash($rows->schema())` | -| `$row->add()` / `set()` / `remove()` / `keep()` / `rename()` / `renameMany()` / `map()` / `merge()` / `isEqual()` / `sortEntries()` | removed | -| `$rows->entries()` | removed | -| `Metadata` per entry, free to differ row to row: `int_entry('id', 1, $metadata)` | one `Metadata` per column: `int_schema('id', metadata: $metadata)` | -| `compare_entries_by_name()` / `compare_entries_by_name_desc()` / `compare_entries_by_type()` / `compare_entries_by_type_desc()` / `compare_entries_by_type_and_name()` | removed - sort a schema: `$schema->sort(schema_sort_by_name())` | -| `Flow\ETL\Transformer\OrderEntries\{Comparator, CombinedComparator, NameComparator, TypeComparator, TypePriorities, Order}` | removed - implement `Flow\ETL\Schema\SortingStrategy::compare(Definition $left, Definition $right): int` | - -Before: - -```php -$rows = rows( - row(int_entry('id', 1), str_entry('name', 'Norbert')), - row(int_entry('id', 2), str_entry('name', null)), -); -``` - -After: - -```php -$rows = rows( - schema(int_schema('id'), str_schema('name', nullable: true)), - row(['id' => 1, 'name' => 'Norbert']), - row(['id' => 2, 'name' => null]), -); -``` - -| Before | Column in `schema(...)` | Value in `row([...])` | -|-------------------------------------------------------------|------------------------------------------|-----------------------------------------------------------------| -| `bool_entry('x', true)`, `boolean_entry('x', true)` | `bool_schema('x')` | `true` | -| `int_entry('x', 1)`, `integer_entry('x', 1)` | `int_schema('x')`, `integer_schema('x')` | `1` | -| `float_entry('x', 1.5)` | `float_schema('x')` | `1.5` | -| `float_entry('x', 1)`, `float_entry('x', '1.5')` | `float_schema('x')` | `type_float()->cast(1)`, `type_float()->cast('1.5')` | -| `str_entry('x', 'a')`, `string_entry('x', 'a')` | `str_schema('x')`, `string_schema('x')` | `'a'` | -| `null_entry('x')` | `null_schema('x')` | `null` | -| `datetime_entry('x', '2024-01-02 10:00:00')` | `datetime_schema('x')` | `type_datetime()->cast('2024-01-02 10:00:00')` | -| `date_entry('x', '2024-01-02')` | `date_schema('x')` | `type_date()->cast('2024-01-02')` | -| `time_entry('x', 'PT1H')` | `time_schema('x')` | `type_time()->cast('PT1H')` | -| `enum_entry('x', Suit::Hearts)` | `enum_schema('x', Suit::class)` | `Suit::Hearts` | -| `json_entry('x', ['a' => 1])`, `json_entry('x', '{"a":1}')` | `json_schema('x')` | `type_json()->cast(['a' => 1])`, `type_json()->cast('{"a":1}')` | -| `json_object_entry('x', [])` | `json_schema('x')` | `Flow\Types\Value\Json::fromArray([], asObject: true)` | -| `uuid_entry('x', $uuid)` | `uuid_schema('x')` | `type_uuid()->cast($uuid)` | -| `xml_entry('x', '')` | `xml_schema('x')` | `type_xml()->cast('')` | -| `xml_element_entry('x', '')` | `xml_element_schema('x')` | `type_xml_element()->cast('')` | -| `html_entry('x', $html)` | `html_schema('x')` | `type_html()->cast($html)` | -| `html_element_entry('x', $html)` | `html_element_schema('x')` | `type_html_element()->cast($html)` | -| `structure_entry('x', $value, $type)`, `struct_entry(...)` | `structure_schema('x', $type)` | `$value` | -| `list_entry('x', $value, $type)` | `list_schema('x', $type)` | `$value` | -| `map_entry('x', $value, $type)` | `map_schema('x', $type)` | `$value` | -| `to_entry('x', $value)` | the `*_schema('x')` matching `$value` | `$value` | -| any `*_entry('x', null)` | `*_schema('x', nullable: true)` | `null` | -| any `*_entry('x', $value, $metadata)` | `*_schema('x', metadata: $metadata)` | `$value` | - -### 31) `flow-php/etl` - `Rows` holds only rows that match its schema - -| Before | After | -|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| any row accepted | `new Rows($schema, ...$rows)` throws `Flow\ETL\Exception\SchemaMismatchException` on a wrong-typed value, a `null` in a NOT NULL column, an undeclared column or a missing NOT NULL column | -| - | `Flow\ETL\Exception\ColumnMismatchException`, the `getPrevious()` of `SchemaMismatchException` | -| a row without a declared nullable column | padded with `null` | -| `$rows->toArray()` - keys in each row's insertion order | schema order | -| `$rows->map()` / `flatMap()` / `each()` / `filter()` / `find()` / `findOne()` / `reduce()` / `sort($callable)` | removed - see below | -| `$rows->sortEntries()` / `$df->reorderEntries()` / `Flow\ETL\Transformer\OrderEntriesTransformer` | removed - `$df->select('a', 'b')` sets the column order | -| `Schema::isSame()` ignored column order | order-sensitive | -| `$rows->merge($other)`, `$other` with other columns or another column order - rows concatenated | throws `InvalidArgumentException`: `Cannot merge Rows with different schemas: [a: integer] and [a: string]` | -| declared NOT NULL column, empty cell or short line (`from_csv($p)->withSchema($schema)`) - `null` | throws `SchemaMismatchException` - declare the column `nullable: true` | -| `from_rows($a, $b)` - each batch kept its own columns | every batch carries every column of the combined schema, missing ones as `null` | -| `batches($extractor, $n)` / `batched_by(...)` over child batches of different shapes - mixed in one batch | later child batches matched to the first: missing nullable column padded, extra column throws `SchemaMismatchException` | - -Before: - -```php -$adults = $rows->filter(fn (Row $row): bool => $row->valueOf('age') >= 18); -``` - -After: - -```php -$adults = new Rows($rows->schema(), ...array_filter($rows->all(), fn (Row $row): bool => $row->get('age') >= 18)); -``` - -### 32) `flow-php/etl` / `flow-php/etl-adapter-excel` - `Entry` parameters become the value and its `Definition` - -| Before | After | -|----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------| -| `Definition::matches(Entry $entry)` | `matches(mixed $value)` - the caller checks the name | -| `Row\Comparator::equals(Row $row, Row $nextRow)` | `equals(Row $row, Row $nextRow, Schema $schema)` | -| `RenameEntryStrategy::rename(Row $row): Row` | `renames(Schema $schema): array` - `current_name => new_name`, `[]` when nothing is renamed | -| `CellStyler::style(Entry $entry, int $rowNumber, int $columnIndex, string $sheetName)` | `style(mixed $value, Definition $definition, int $rowNumber, int $columnIndex, string $sheetName)` | -| `new ASCIIValue($value)` - an `Entry` or a scalar/array | `new ASCIIValue(Type $type, mixed $value)` | -| `Dataset\Statistics\Column::__construct(Entry $entry)` | `__construct(Definition $definition, mixed $value)` | -| `Dataset\Statistics\Column::calculate(Entry $entry)` | `add(Definition $definition, mixed $value)` | -| `Dataset\Statistics\Columns::add(Entry $entry)` | `add(Definition $definition, mixed $value)` | -| `new NullRowBuilder(EntryFactory $entryFactory)` + `->collect($row)` | `new NullRowBuilder(Schema $schema)`, `collect()` removed | -| `new RowsBuffer(int $size)` | `new RowsBuffer(Schema $schema, int $size)` | -| `Rows::joinLeft($right, $on, $entryFactory)` / `Rows::joinRight($right, $on, $entryFactory)` | third argument removed | -| `Flow\ETL\Row\EntryFactory`, `Row\Entry\EntryInstantiator`, `Row\Entry\Instantiators` | removed | -| `EntryTypeResolver::fromDefinition()` | removed | - -### 33) `flow-php/etl` - `Config` options move to the extractors or are removed - -| Before | After | -|--------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------| -| `data_frame(config_builder()->putInputIntoRows())->read(from_csv($path))` | `data_frame()->read(from_csv($path)->withMetadataColumns(true))` | -| `ConfigBuilder::putInputIntoRows()` / `dontPutInputIntoRows()`, `Config::shouldPutInputIntoRows()` | removed | -| `config_builder()->extractorBatchSize(500)` | `from_csv($path)->withBatchSize(500)` - on each extractor | -| `Config::extractorBatchSize()` | removed - `$extractor->batchSize()` | -| `FlowContext::entryFactory()` | removed | -| `new Config(...)` with `$filesystemTable`, `$putInputIntoRows`, `$extractorBatchSize`, `$entryFactory` | those parameters removed, `HashRepartitionConfig $repartition` added - build through `Config::builder()` | - -`withMetadataColumns()` is on `from_csv()`, `from_json()`, `from_json_lines()`, `from_parquet()`, `from_text()`, -`from_xml()`, `from_excel()`, `from_google_sheet()`, `from_google_sheet_columns()` and `from_floe()`. - -### 34) `flow-php/etl` - extractors yield batches - -| Before | After | -|--------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| every extractor yielded one row per `Rows` | up to `batchSize()` rows per `Rows`: 100 by default, 1000 for `DbalLimitOffsetExtractor`, `DbalKeySetExtractor`, `PostgreSqlLimitOffsetExtractor`, `PostgreSqlKeySetExtractor` and `PostgreSqlCursorExtractor` | -| `Flow\ETL\Pipeline\Optimizer\BatchSizeOptimization` re-cut batches to 1000 rows before `DbalLoader` / `PostgreSqlLoader` | removed - `$df->batchSize(1000)` before `write()` | -| `new BatchExtractor($extractor, chunkSize: 10)` | `new BatchExtractor($extractor, batchSize: 10)` - a size below 1 throws | -| `$df->duplicateRow(...)` appended the copies after the batch's last row | each copy follows the row it duplicates | - -### 35) `flow-php/etl` - `LimitableExtractor` replaced by `LimitPushDown` - -| Before | After | -|--------------------------------------------------------------------------------|----------------------------------------------------------------------------------| -| `Flow\ETL\Extractor\LimitableExtractor` | `Flow\ETL\Extractor\LimitPushDown` | -| `Flow\ETL\Extractor\Limitable` (trait) | `Flow\ETL\Extractor\PushesLimit` | -| `changeLimit(int $limit): void` | `pushLimit(int $limit): void` - a second push can only lower the limit | -| `limit(): ?int` | `pushedLimit(): ?int` | -| `isLimited(): bool` | removed - `pushedLimit() !== null` | -| `incrementReturnedRows()` / `reachedLimit()` / `resetLimit()` | removed | -| a pushed limit replaced the `limit()` step, so the extractor had to stop at it | the `limit()` step stays; stopping early is optional | -| `new LimitReachedException($limit, $previous)` | `new LimitReachedException($limit, $rows, $previous)` - pass `previous:` by name | - -### 36) `flow-php/etl` - `ErrorHandler` answers per stage: extraction, transformation, loading - -| Before | After | -|---------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------| -| `ErrorHandler::throw(Throwable $error, Rows $rows): bool` | `onExtraction(ExtractionError $error): ExtractionAction` - `propagate` / `endSource` | -| `ErrorHandler::skipRows(Throwable $error, Rows $rows): bool` | `onTransformation(TransformationError $error): TransformationAction` - `propagate` / `skipBatch` | -| - | `onLoading(LoadingError $error): LoadingAction` - `propagate` / `skipLoader` | -| an extractor failure always propagated | goes to `onExtraction()`: `IgnoreError` and `SkipRows` end the source, `ThrowError` rethrows | -| `IgnoreError`: a failed transformation ran the remaining steps on the half-transformed batch | the batch is dropped | -| `SkipRows` / `skip_rows_handler()`: a failed transformation emitted the half-transformed batch | the batch is dropped | -| `SkipRows`: a failed loader was swallowed and the batch's remaining steps skipped | rethrown | -| `SkipRows`: a drain failure of `to_transformation()` / `to_branch()` at `closure()` was swallowed | rethrown | - -The error objects and the action enums are in `Flow\ETL\ErrorHandler`. - -Before: - -```php -final class SkipInvalidBatches implements ErrorHandler -{ - public function throw(Throwable $error, Rows $rows): bool - { - return !$error instanceof InvalidArgumentException; - } - - public function skipRows(Throwable $error, Rows $rows): bool - { - return true; - } -} -``` - -After: - -```php -final class SkipInvalidBatches implements ErrorHandler -{ - public function onExtraction(ExtractionError $error): ExtractionAction - { - return ExtractionAction::propagate; - } - - public function onTransformation(TransformationError $error): TransformationAction - { - return $error->cause instanceof InvalidArgumentException - ? TransformationAction::skipBatch - : TransformationAction::propagate; - } - - public function onLoading(LoadingError $error): LoadingAction - { - return $error->cause instanceof InvalidArgumentException - ? LoadingAction::skipLoader - : LoadingAction::propagate; - } -} -``` - -### 37) `flow-php/etl` - `Cache` gains `schema()` - -| Before | After | -|-----------------------------------------------------|------------------------------------------------------------------------------------------| -| - | `Cache::schema(string $key): Schema`, throws `KeyNotInCacheException` for an unknown key | -| a custom `Flow\ETL\Cache` implementation without it | fatal at load | - -### 38) `flow-php/etl` - data cached or serialized by 0.43.x cannot be read back - -| Before | After | -|-----------------------------------------------------------------------------------------------------------------|----------------| -| `Rows` stored by `FilesystemCache`, `PSRSimpleCache`, `ApcuCache`, `PSRCacheBuckets` or `serialize_to_string()` | cannot be read | - -Clear persistent caches and regenerate stored `serialize_to_string()` payloads after upgrading. - -### 39) `flow-php/etl` - in-memory sources infer one nullable schema from the first 20,480 rows - -| Before | After | -|--------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------| -| `df()->read(from_array([['id' => 1]]))->schema()` - `id: integer` | `id: ?integer`; the same for `from_memory()` and `from_sequence_*()` | -| `from_array()` / `from_memory()` / `from_sequence_*()` typed each row on its own | columns inferred from the first 20,480 rows | -| a row past the sample whose value the inferred type refuses - kept with its own type | throws `Flow\ETL\Exception\InferredSchemaException` | -| - | `from_array($rows)->inferSchema(infer_schema()->sampleSize(-1))` - infer from every row | -| `from_array([['a' => 1], ['a' => 2, 'b' => 'x']])` - first row has no `b` | first row `'b' => null` | -| `from_array([['a' => 'x'], ['a' => 1000]])` - second row `1000` | `'1000'` - values follow the widened column type | -| `from_array([['tz' => new DateTimeZone('Europe/Warsaw')]])` - `'Europe/Warsaw'`, column `string` | `DateTimeZone` object, column `?timezone` | -| `from_array([['s' => ['j' => null]]])` - `s: structure{j: null}` | `s: ?structure{j: ?string}` | -| `from_array([['o' => new stdClass()]])` - `InvalidArgumentException` on `schema()` and on read | `schema()` gives `o: ?string`; the read throws `SchemaMismatchException` | -| `from_array($generator)` - streamed | read in full into `/flow-php-source/` before the first row; an endless generator never returns | -| `from_array($generator)` read twice - `Cannot traverse an already closed generator` | replayed from the spill file | -| - | `from_array($generator, $schema)` or `->withSchema($schema)` - streamed, no spill, one read only | -| - | `from_array($generator, spillRoot: path($dir))` - default is the filesystem's system tmp dir | -| `from_memory($memory)` read again after `$memory->save()` - saw the new columns | keeps the schema of its first non-empty read; create a new `from_memory($memory)` | - -`InferredSchemaException` is not a `SchemaMismatchException`; the mismatch is its `getPrevious()`. - -### 40) `flow-php/etl` - `Hydrator`, `array_to_row()` and `array_to_rows()` require a `Schema` - -| Before | After | -|-----------------------------------------------------------|---------------------------------------------------------------------------------| -| `Hydrator::cast(array $batch, ?Schema $schema = null)` | removed - use `hydrate($batch, $schema)` | -| `Hydrator::hydrate(array $batch, ?Schema $schema = null)` | `hydrate(array $batch, Schema $schema)` | -| `array_to_row($data, $hydrator, $partitions, $schema)` | `array_to_row($data, $schema, $hydrator, $partitions)` | -| `array_to_rows($data, $hydrator, $partitions, $schema)` | `array_to_rows($data, $schema, $hydrator)` - put partition columns into `$data` | -| `array_to_rows($data)` | `df()->read(from_array($data))->fetch()` | - -### 41) `flow-php/etl` / `flow-php/types` - `autoCast()` removed - -| Before | After | -|--------------------------------------------|---------| -| `DataFrame::autoCast()` | removed | -| `Flow\ETL\Transformer\AutoCastTransformer` | removed | -| `Flow\Types\Type\AutoCaster` | removed | - -Before: - -```php -df()->read(from_csv($path))->autoCast()->fetch(); -df()->read(from_array([['id' => '1', 'total' => '10.5']]))->autoCast()->fetch(); -``` - -After: - -```php -df()->read(from_csv($path))->fetch(); -df()->read(from_array([['id' => '1', 'total' => '10.5']], schema(int_schema('id'), float_schema('total'))))->fetch(); -``` - -`from_array()` types by PHP value: numeric strings stay `?string` unless the schema is declared. - -### 42) `flow-php/etl` - a value a declared column refuses throws `SchemaMismatchException` - -| Before | After | -|-------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------| -| `from_csv($path)->withSchema($schema)`, `'n/a'` in a `datetime` column - throws `Flow\Types\Exception\CastingException` | throws `Flow\ETL\Exception\SchemaMismatchException`: `Rows do not match their schema: column "d" (row 1): could not convert 'n/a' (string) to datetime` | -| `getPrevious()` of the thrown exception | `Flow\ETL\Exception\ColumnMismatchException`, never the `CastingException` | -| `type_datetime()->cast('n/a')` - throws `CastingException` | unchanged | - -Replace `catch (CastingException $e)` around a read with `catch (SchemaMismatchException $e)`. - -### 43) `flow-php/etl` - `DataFrame::schema()` describes the plan without running it - -| Before | After | -|--------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------| -| `$df->schema()` ran the pipeline, loaders included, and merged every batch's schema | derived from the source's `schema()` through each step; no row is read, no loader runs | -| `$df->schema()` on a plan containing `joinEach()` - ran the pipeline | throws `DataDependentSchemaException` | -| `$df->schema()` over a source that cannot describe its schema, also through `from_data_frame($inner)` - ran the pipeline | throws `SchemaNotDerivableException` | -| `schema()` reported `dataFrameCompleted` / `dataFrameFailed` telemetry | reports neither | -| `$df->fetch()` returning no rows, `$df->void()->fetch()` - `Rows` with an empty schema | `Rows` carrying the plan's columns | - -### 44) `flow-php/etl` - `printSchema()` prints the plan's schema and takes no limit - -| Before | After | -|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------| -| `printSchema(?int $limit = 20, SchemaFormatter $formatter = new ASCIISchemaFormatter())` | `printSchema(SchemaFormatter $formatter = new ASCIISchemaFormatter())` | -| `$df->printSchema(20, $formatter)` | `$df->printSchema($formatter)` | -| ran the pipeline, loaders included, printing one schema per batch | prints `$df->schema()` once and runs nothing; throws where `schema()` throws | -| `$df->printSchema(); $df->fetch()` - at most 20 rows | every row | - -### 45) `flow-php/etl` - `display()` and `printRows()` render one table - -| Before | After | -|-------------------------------------------------------------|--------------------------------| -| `$df->display()` / `$df->printRows()` - one table per batch | one table | -| `$df->printRows(null)` streamed the frame batch by batch | collects the whole frame first | - -### 46) `flow-php/etl` - `select()` and `groupBy()` refuse an undeclared column - -| Before | After | -|----------------------------------------------------------------------|------------------------------------------------------------------------------------------------| -| `$df->select('missing')` - added a `missing` string column of `null` | throws `SchemaDefinitionNotFoundException`: `Schema definition for entry "missing" not found.` | -| `$df->groupBy(['missing'])` - grouped every row under a `null` key | throws `SchemaDefinitionNotFoundException` | - -### 47) `flow-php/etl` - renaming onto an existing column throws - -| Before | After | -|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------| -| `$df->rename('a_b', 'ab')` while `ab` exists - `a_b` replaced `ab` | throws `SchemaDefinitionNotUniqueException`: `Entry definitions must be unique, duplicated entries: [ab], all: [ab, ab]` | -| `$df->renameEach(rename_replace('_', ''))` producing an existing name - replaced it | throws `SchemaDefinitionNotUniqueException` | - -### 48) `flow-php/etl` - float values compare exactly - -| Before | After | -|-------------------------------------------------------------------------------------------------------------|-----------------------------------------| -| `$rows->unique()` / `diffLeft()` / `diffRight()` - floats sharing an integer part were equal (`1.5 == 1.9`) | exact: `1.5 != 1.9`, `0.1 + 0.2 != 0.3` | -| `rank()` / `dense_rank()` peers over a float column - same rule | exact | - -### 49) `flow-php/etl` - `Window` and window functions are immutable - -| Before | After | -|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------| -| `$window->partitionBy(...)` / `->orderBy(...)` / `->rowsBetween(...)` changed `$window` | return a copy - use the returned window | -| `$function->over($window)` changed `$function` | returns a copy | -| `window()->partitionBy(ref('a'))->partitions()` - `array` | `References`; `->all()` returns the array | -| `window()->partitionBy(ref('nope'))` - every row in one partition | throws `SchemaDefinitionNotFoundException` before the first row | -| `window()->orderBy(ref('nope'))` - threw per row | throws `SchemaDefinitionNotFoundException` before the first row | - -### 50) `flow-php/etl` - group and join key extraction reads the schema - -| Before | After | -|-----------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| -| `GroupBy::keyValues(Row $row)` | `keyValues(Row $row, Schema $input)` | -| `GroupBy::aggregatedRow(GroupKey $key, Aggregators $aggregators, EntryFactory $entryFactory)` | `aggregatedRow(GroupKey $key, Aggregators $aggregators, Schema $output)` | -| `KeyValues::ofRow(Row $row)` | `ofRow(Row $row, Schema $schema)` | -| `new KeyValues($refs, nullOnMissing: true)` | parameter removed - a missing key is `null` under a nullable column, throws under NOT NULL | -| `new HashBucketing(..., nullOnMissing: true)` | parameter removed | -| `join()` over a row without the join column - threw | the row is unmatched | - -### 51) `flow-php/etl` - a join producing a duplicate column throws `SchemaDefinitionNotUniqueException` - -| Before | After | -|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------| -| `$df->join($right, join_on(['id' => 'id']))`, both sides carry `name` - `JoinException` (`RuntimeException`): `Merged entries names must be unique, given: [id, name] + [name] try to use a different join prefix than: ""` | `SchemaDefinitionNotUniqueException` (`InvalidArgumentException`): `Entry definitions must be unique, duplicated entries: [name], all: [id, name, name]` | -| thrown when the first pair of rows was merged | thrown when the pipeline starts, before any row is read | -| `$rows->joinCross($right, '')` over shared columns - `InvalidArgumentException`: `... + [id, name]. Please consider using join prefix option` | `SchemaDefinitionNotUniqueException`: `Entry definitions must be unique, duplicated entries: [id, name], all: [id, name, id, name]` | -| `catch (JoinException $e)` around a join | `catch (SchemaDefinitionNotUniqueException $e)`, or give the right side a prefix: `join_on(['id' => 'id'], 'joined_')` | - -### 52) `flow-php/etl` - `Joiner` takes `JoinSide` and no `EntryFactory`, `Expression` drop helpers removed - -| Before | After | -|----------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------| -| `Flow\ETL\Join\Expression::dropDuplicateLeftEntries()` / `dropDuplicateRightEntries()` | removed | -| `$joiner->join($left, $right, $nullLeftRow, $nullRightRow, $buildLeft)` | `$joiner->join(JoinSide::of($left, $nullLeftRow, $leftSchema), JoinSide::of($right, $nullRightRow, $rightSchema), $buildLeft)` | -| `new Joiner($expression, $type, $entryFactory, $batchSize)` | `new Joiner($expression, $type, $batchSize)` | - -### 53) `flow-php/etl` - a join against an empty side keeps that side's columns - -| Before | After | -|-------------------------------------------------------------------------------------------|----------------------------------------| -| `$df->join($emptyRight, $on, Join::left)` - right-hand columns dropped | kept, `null` | -| `$df->join($right, $on, Join::right)` with an empty left side - left-hand columns dropped | kept, `null` | -| `$rows->joinCross($empty)` / `$empty->joinCross($rows)` - the non-empty side's rows | zero rows carrying both sides' columns | - -### 54) `flow-php/etl` - a global `aggregate()` over zero rows returns one row - -| Before | After | -|--------------------------------------------------------------------------------------------------------------------|----------------------------------------------| -| `$df->aggregate(sum(ref('a')), count(ref('a')))` over zero rows, or after a `filter()` removed every row - no rows | one row, `['a_sum' => null, 'a_count' => 0]` | -| `collect()` / `collect_unique()` / `string_agg()` over zero rows - no rows | `[]` / `[]` / `''` | -| `$df->groupBy('k')->aggregate(...)` over zero rows - no rows | unchanged | - -### 55) `flow-php/etl` - `pivot()` takes its values - -Before: - -```php -$df->groupBy('date')->pivot(ref('product'))->aggregate(sum(ref('amount'))); -``` - -After: - -```php -$df->groupBy(['date'])->pivot(ref('product'), pivot_values('A', 'B'))->aggregate(sum(ref('amount'))); -// or -$df->groupBy(['date'])->pivot(ref('product'), discover_pivot_values())->aggregate(sum(ref('amount'))); -``` - -| Before | After | -|------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------| -| `GroupedDataFrame::pivot(Reference $ref)` | `pivot(Reference $ref, PivotValues $values)` | -| `GroupBy::pivot(Reference $ref)` | `GroupBy::pivot(Reference $ref, DeclaredPivotValues $values)` | -| one column per value found in the data | `pivot_values(...)`: one per declared value, `null` where no row carries it, undeclared values dropped | -| - | `discover_pivot_values()`: one per non-null value in the frame, sorted, at most `maxValues` (default `10_000`) | -| pivot value `0` or `'0'` - column `e00` | column `0` | -| pivot value `7` - column `e07` | column `7` | -| pivot value equal to a group-by column name - its column dropped | throws `InvalidArgumentException` | -| `GroupBy::pivotResult($rows, $context, $batchSize)` | removed - `(new PivotAggregation($batchSize))->aggregate($rows, $context, $groupBy)` | - -A custom extractor read by `discover_pivot_values()` must implement `Flow\ETL\Extractor\RewindableExtractor`. - -### 56) `flow-php/etl` - `unpack()` / `array_unpack()` take the `Schema` of the columns they produce - -Before: - -```php -$df->withEntry('row', ref('row')->unpack(['internal_id'])); -``` - -After: - -```php -$df->withEntry('row', ref('row')->unpack(schema(int_schema('id'), str_schema('name')))); -``` - -| Before | After | -|----------------------------------------------------------------|------------------------------------------------------| -| `ref('x')->unpack($skipKeys, $entryPrefix)` | `ref('x')->unpack(Schema $schema)` | -| `array_unpack($array, $skip_keys, $entry_prefix)` | `array_unpack($array, Schema $schema)` | -| every payload key except `$skipKeys` became a column | only the declared columns; other keys are dropped | -| column type inferred from each value | the declared type, nullable | -| a declared key missing from one payload - no entry in that row | `null` | -| `$entryPrefix` prepended to each key | removed - columns are named `.` | - -A custom `Flow\ETL\Function\ScalarFunction\UnpackResults` function must return a `StructureType` of its columns from -`returns()`. - -### 57) `flow-php/etl` - `on_each()` requires an operand that declares its element type - -| Before | After | -|------------------------------------------------------------------------|------------------------------------------------------------------------------------| -| `ref('a')->onEach(ref('element'))` with `a` = `[1, 'x']` -> `[1, 'x']` | throws `SchemaNotDerivableException` | -| - | `ref('a')->cast(type_list(type_string()))->onEach(ref('element'))` -> `['1', 'x']` | - -### 58) `flow-php/etl` - `isIn()` compares numbers loosely and refuses incomparable types - -| Before | After | -|--------------------------------------------------------------------------------------------|-------------------------------------------| -| `ref('a')->isIn(lit([1]))` with `a` = `'1'` - `false` | `true`, as `ref('a')->equals(lit(1))` | -| `ref('a')->isIn(lit([new DateTimeImmutable('2024-01-01')]))` with `a` a `string` - `false` | throws `Can't compare '(string == date)'` | -| `ref('a')->isIn(lit([1, 2]))` with `a` = `null` - `false` | `null`, `filter()` drops the row | - -### 59) `flow-php/etl` - `Calculator::divide()` always returns `float` - -| Before | After | -|-------------------------------------------|-------| -| `(new Calculator())->divide(4, 2)` - `2` | `2.0` | -| `ref('a')->divide(lit(2))` over `4` - `2` | `2.0` | - -### 60) `flow-php/etl` - `DataFrame::partitionBy()` removed, loaders partition, `Rows` carry no partitions - -| Before | After | -|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------| -| `$df->partitionBy('year')->write(to_csv($path))` | `$df->write(to_csv($path)->partitionBy(partition_by('year')))` | -| partitioned write - the partition column is written into the file body as well | path only; `partition_by('year')->writeColumns()` keeps it in the body | -| `$df->partitionBy('year')` without a partitioned write | `$df->repartition('year')` | -| `$df->dropPartitions()` | removed | -| `$df->dropPartitions(true)` | `$df->drop('year')` | -| `Rows::partitioned()` / `rows_partitioned()` | removed | -| `Rows::partitionBy()` / `partitions()` / `isPartitioned()` / `dropPartitions()` | removed | -| `Flow\ETL\Transformer\DropPartitionsTransformer` | removed | -| reading `year=2024/month=01/` - partition columns appended in path order (`year`, `month`) | appended in name order (`month`, `year`) | -| reading `year=__HIVE_DEFAULT_PARTITION__/` - `year` is the string `'__HIVE_DEFAULT_PARTITION__'` | `null`, and `year` is nullable | -| same read with `withSchema()` declaring `year` NOT NULL - the string | throws `InvalidArgumentException` | -| reading `year=abc/` with `withSchema()` declaring `year` as `integer` - `0` | throws `InvalidArgumentException` naming the column, file, type and value | -| `display()` / `printRows()` / `to_output()` printed a `Partitions:` footer | no footer | - -### 61) `flow-php/etl` - `filterPartitions()` compares the raw string partition value - -| Before | After | -|---------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------| -| `filterPartitions(ref('date')->equals(lit(new DateTimeImmutable('2024-01-01'))))` - matched `date=2024-01-01` | throws `Can't compare '(string == date)'`; use `lit('2024-01-01')`, or `from_csv($path)->partitionTypes(partition_types(date: type_date()))` | -| `filterPartitions(ref('active')->equals(lit(false)))` - matched `active=false` | throws `Can't compare '(string == boolean)'`; use `lit('false')`, or `partition_types(active: type_boolean())` | -| `filterPartitions(ref('date')->equals(lit('2024-01-01')))` - throws `Can't compare '(date == string)'` | matches `date=2024-01-01` | -| a `DateTimeImmutable` or `bool` literal over `files()` / `from_path_partitions()` - matched | matches nothing; compare a string literal | -| `new ScalarFunctionFilter($function, $entryFactory, $caster, $context)` | `new ScalarFunctionFilter($function, Schema $partitions, $context)` | - -### 62) `flow-php/etl` / `flow-php/types` - missing-column and type-mismatch messages changed - -| Before | After | -|------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------| -| `Schema definition for entry "x" not found` | `Schema definition for entry "x" not found.`, plus ` Did you mean one of: [a]?` when up to 3 column names are close | -| `$row->get('x')`: `Entry "x" does not exist. Did you mean one of the following? ["every", "column"]` | `Column "x" does not exist.`, plus ` Did you mean one of the following? ["a"]` when up to 3 column names are close | -| `Can't compare '(?integer > ?date)' due to data type mismatch.` | `Can't compare '(?integer > ?date)' due to data type mismatch - an explicit cast is required.` | - -### 63) `flow-php/etl` - pipeline and generator extractors and `Schema::fromPipeline()` removed - -| Before | After | -|-----------------------------------------------------------------|----------------------------------| -| `from_pipeline($pipeline)` / `new PipelineExtractor($pipeline)` | removed - `from_data_frame($df)` | -| `Flow\ETL\Extractor\GeneratorExtractor` | removed | -| `Schema::fromPipeline($pipeline, $context, $maxRows)` | removed - `$df->schema()` | - -### 64) `flow-php/etl` - `until()` stops at the first row failing its predicate - -| Before | After | -|-----------------------------------------------------------------------------------------------|----------| -| `$df->until(ref('v')->lessThan(lit(3)))` over `[1, 2, 5, 1, 2]` in one batch - `[1, 2, 1, 2]` | `[1, 2]` | - -### 65) `flow-php/etl` - `UnserializeTransformer` requires the payload schema - -| Before | After | -|-------------------------------------------------------------------------------------------|----------------------------------------------------------------------| -| `new UnserializeTransformer($source, $merge, $mergePrefix)` | `new UnserializeTransformer($source, $schema, $merge, $mergePrefix)` | -| payload missing, not a string, not unserializable or not one row - row returned unchanged | declared payload columns added as `null` | - -### 66) `flow-php/etl` - Floe reader drops `RowPadding`, `conform:` and `lenient:` - -| Before | After | -|-----------------------------------------------------------------|---------------------------------------------------------------| -| `Flow\Floe\RowPadding` | removed | -| `FloeStreamReader::rows($batchSize, $offset, $limit, $conform)` | `rows($batchSize, $offset, $limit)` | -| `FrameReader::frames(lenient: true)` | `frames()` - a truncated stream always throws `FloeException` | - -### 67) `flow-php/etl` / `flow-php/etl-adapter-parquet` - a Floe or Parquet glob checks every file against the first - -| Before | After | -|-----------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| -| `from_parquet($glob)` / `from_floe($glob)`, a later file with other columns or column types - rows kept that file's columns | throws `InferredSchemaException` naming both files - read with `->unionByName()` or `->withSchema(...)` | -| same read, a later file with the same columns in another order - rows kept that file's order | rows follow the first file's column order | -| - | `->unionByName()` reads every file under the merged schema; a column a file lacks is `null` | - -### 68) `flow-php/etl` and every package with DSL functions - documentation attributes leave the packages - -| Before | After | -|-------------------------------------------|-----------------------------------------------------| -| `Flow\ETL\Attribute\DocumentationDSL` | `Flow\Documentation\Attribute\DocumentationDSL` | -| `Flow\ETL\Attribute\DocumentationExample` | `Flow\Documentation\Attribute\DocumentationExample` | -| `Flow\ETL\Attribute\Module` | `Flow\Documentation\Attribute\Module` | -| `Flow\ETL\Attribute\Type` | `Flow\Documentation\Attribute\Type` | - -No installable package ships `Flow\Documentation\Attribute\*`: `ReflectionAttribute::newInstance()` on them throws -`Attribute class "..." not found`. Read them with `getName()` / `getArguments()`. - -### 69) `flow-php/etl` / `flow-php/types` - `TypeMerge` moved to `flow-php/types` as `TypeWidener` - -| Before | After | -|------------------------------------------------------|------------------------------------------------------| -| `Flow\ETL\Schema\Definition\TypeMerge` | `Flow\Types\Type\TypeWidener` | -| `merge($left, $right)` | `widen($left, $right)` | -| `mergeLists()` / `mergeMaps()` / `mergeStructures()` | `widenLists()` / `widenMaps()` / `widenStructures()` | - -### 70) `flow-php/types` - structure optional fields are declared inline with `structure_element()` - -| Before | After | -|----------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------| -| `type_structure(['id' => type_integer()], ['nick' => type_string()])` | `type_structure(['id' => type_integer(), 'nick' => structure_element('nick', type_string(), optional: true)])` | -| `type_structure($elements, $optionalElements, true)` | `type_structure($elements, true)` - an array second argument throws `TypeError` | -| `new StructureType($elements, $optionalElements, $allowExtra)` | `StructureType::fromElements($elements, $allowExtra)` | -| `StructureType::elements()` - `array`, required fields only | `list`, optional fields included | -| `$structure->elements()['id']` | `$structure->element('id')?->type` | -| `StructureType::optionalElements()` | removed - read `StructureElement::$optional` | -| `normalize()` - `{"type": "structure", "elements": {...}, "optional_elements": {...}}` | `{"type": "structure_v2", "fields": [{"name": ..., "type": ..., "optional": ...}]}` | -| `type_from_array()` / `schema_from_json()` on a `structure` payload | throws `InvalidArgumentException: Unknown type 'structure'` | - -Regenerate any stored `schema_to_json()` output that contains a structure. - -### 71) `flow-php/types` - `time`, `date`, `string` and `Json` accept and refuse different values - -| Before | After | -|---------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------| -| `type_time()->isValid(new DateInterval('P1M'))` - `true` | `false` | -| `type_time()->cast(new DateInterval('P1M'))` - returned as-is | throws `CastingException`: `... Relative DateInterval (with months/years) can't be cast to time` | -| `type_time()->cast('12:34:56')` - throws `CastingException` | `DateInterval` of 12h 34m 56s | -| `type_time()->cast('12:34:56.5')` - throws `CastingException` | `DateInterval` with `f` = `0.5` | -| `type_union(type_time(), type_integer())->cast('12:34:56')` - `12` | `DateInterval` | -| `type_date()->isValid(new DateTimeImmutable('2024-01-02 00:00:00.5'))` - `true` | `false` | -| `Json::fromArray([1 => 'a'], asObject: true)` - `{"1":"a"}` | throws `InvalidArgumentException`: `All keys of a JSON object must be strings` | -| `type_string()->cast(new DateInterval('PT1H2M3S'))` - throws `CastingException` | `'01:02:03'`; `P1DT2H` -> `'26:00:00'`; a fraction adds `.uuuuuu` | - -### 72) `flow-php/types` - `StringTypeNarrower` types compact digit dates as `integer` and month-only dates as `string` - -| Before | After | -|------------------------------------------------------------|-----------| -| `(new StringTypeNarrower())->narrow('20240101')` -> `date` | `integer` | -| `(new StringTypeNarrower())->narrow('2024-01')` -> `date` | `string` | - -### 73) `flow-php/filesystem` - `Partition` takes a value and its type, encodes reserved characters and allows `null` - -| Before | After | -|------------------------------------------------------------------------------------------------|------------------------------------------------------------------------| -| `Partition::valueFromRow(Reference $ref, Row $row)` | `Partition::fromValue(string $name, Type $type, mixed $value): string` | -| `$partition->reference()` | removed | -| `Flow\ETL\Row\Entry\JsonEntry can't be used as a partition` | `Column "d" of type json can't be used as a partition` | -| `Partition::$value` - `string` | `?string` | -| `new Partition('path', 'a/b')` - throws `Partition value contains one of forbidden characters` | `->segment()` - `path=a%2Fb` | -| `new Partition('a/b', '1')` - throws `Partition name contains one of forbidden characters` | `->segment()` - `a%2Fb=1` | -| value `New York` written to `city=New York/` | `city=New%20York/` | -| `null` partition value on write - throws `Partition value can't be empty` | `year=__HIVE_DEFAULT_PARTITION__/` | -| `new Partitions(new Partition('a', '1'), new Partition('a', '2'))` | throws `InvalidArgumentException` | -| `Partition` / `Partitions` throw `Flow\ETL\Exception\InvalidArgumentException` | throw `Flow\Filesystem\Exception\InvalidArgumentException` | - -### 74) `flow-php/filesystem` - `SourceStream::readLines()` strips the separator and yields nothing for 0 bytes - -| Before | After | -|-----------------------------------------------------------------------------------------------------|--------------------| -| `memory_filesystem()` stream: each line kept its trailing `"\n"` | separator stripped | -| `memory_filesystem()` stream: `$separator` ignored | honoured | -| `memory_filesystem()` stream: 0 bytes yielded one `''` | yields nothing | -| `StringSourceStream` / `MemorySourceStream` dropped empty lines | yield them as `''` | -| `from_text()` over an empty `memory://` file - one empty row | no rows | -| `from_json_lines()` over an empty `memory://` file - threw | no rows | -| `from_csv()->withCharactersReadInLine($n)` on `memory://` - a line longer than `$n` split into rows | one row per line | - -A custom `SourceStream::readLines()` must strip the separator, yield nothing for a 0-byte stream and yield an empty -line as `''`. - -### 75) `flow-php/parquet` - FLOAT32 values read without `ext-arrow` are no longer rounded to 7 decimals - -| FLOAT32 value written | Before (read) | After (read) | -|-----------------------|---------------|-----------------------| -| `0.1` | `0.1` | `0.10000000149011612` | -| `18.52` | `18.5200005` | `18.520000457763672` | -| `1.0E-8` | `0.0` | `9.99999993922529E-9` | - -### 76) `flow-php/parquet` - `FlatColumn::decimal()` sizes `FIXED_LEN_BYTE_ARRAY` by the Parquet spec - -| `FlatColumn::decimal($name, $precision)` | Before (bytes) | After (bytes) | -|------------------------------------------|----------------|---------------| -| precision 7 | 3 | 4 | -| precision 12 | 5 | 6 | -| precision 19 | 8 | 9 | -| precision 24 | 10 | 11 | -| precision 36 | 15 | 16 | - -### 77) `flow-php/postgresql` - `Client` gains `describe()`, and `execute()` accepts `ConvertedParameters` - -| Before | After | -|----------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------| -| a custom `Flow\PostgreSql\Client\Client` implementation | adds `describe(Sql\|string $sql, array $parameters = []): array` returning `list` | -| `execute(Sql\|string $sql, array $parameters = []): int` | `execute(Sql\|string $sql, array\|ConvertedParameters $parameters = []): int` | - -A custom `Client::execute()` binds `ConvertedParameters::$values` as they are, without a converter. - -### 78) `flow-php/postgresql` - arrays parsed, `oid` as integer, `timetz` without offset, duplicates cast by name - -| Before | After | -|-----------------------------------------------------------|-------------------------------------------------------------------------------| -| `ResultCaster::cast()` returns `bool\|float\|int\|string` | returns `array\|bool\|float\|int\|string` | -| `_int4` value `'{1,NULL,3}'` | `[1, null, 3]` - every `_`-prefixed array type, elements cast by element type | -| `oid` value `'42'` | `42` | -| `timetz` value `'12:34:56+02'` | `'12:34:56'` | -| `SELECT 1::int8 AS a, 'x' AS a` - `a` is `0` | `'x'` | -| `SELECT 'x' AS a, 1::int8 AS a` - `a` is `'1'` | `1` | - -Applies to rows returned by `Client::fetch*()` and `Client::cursor()`. - -### 79) `flow-php/doctrine-dbal-bulk` - `Dialect` requires `maxBindParameters()` - -| Before | After | -|--------------------------------------------------------------|-----------------------------------------------------------------------------------------------------| -| a custom `Flow\Doctrine\Bulk\Dialect\Dialect` implementation | adds `public function maxBindParameters(): int` - the platform's bind-parameter limit, at least `1` | - -### 80) `flow-php/etl-adapter-csv`, `-json`, `-excel`, `-google-sheet` - a read without a schema infers one schema - -| | Before | After | -|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------| -| schema of a read without `withSchema()` | each yielded `Rows` typed from its own values | one schema for the whole read, inferred from a sample | -| nullability of an inferred column | nullable only where that `Rows` held a `null` | always nullable (`?integer`, `?string`, ...) | -| sample | - | first 20,480 rows over the first 10 files; Google Sheet: first 100 rows | -| value past the sample that the inferred type refuses | read, typed on its own | throws `Flow\ETL\Exception\SchemaMismatchException` | -| CSV/Excel file or Google Sheet whose header differs from the inferred one (header-only files too) | read with its own columns | throws `Flow\ETL\Exception\InferredSchemaException` | -| row key order | the row's own; a body column named like a partition is overwritten in place | the schema's: body columns (JSON: first seen across the sample), then partition columns | -| `flow file:schema` over CSV / Excel | every column `string`, `nullable: false` unless a value was empty | typed, every column `nullable: true` | -| infer from every row | - | `->inferSchema(infer_schema()->sampleSize(-1))` | -| infer from every file | - | `->inferSchema(infer_schema()->filesToSniff(-1))` | -| every column `?string` | - | `->inferSchema(infer_schema()->allStrings())` | -| files with different columns | - | `->inferSchema(infer_schema()->unionByName())` | -| no inference | - | `->withSchema($schema)` | - -On `from_google_sheet()`, `->inferSchema(infer_schema())` without `->sampleSize()` samples 20,480 rows, not 100. - -### 81) `flow-php/etl-adapter-csv` - cells are typed, not strings - -| Cell | Before | After | -|--------------------------------------|----------|-----------------------| -| `1` | `string` | `?integer` | -| `1.5` | `string` | `?float` | -| `true` / `false` | `string` | `?boolean` | -| `20240101` | `string` | `?integer` | -| `2024-01-01` / `2024-01-01 10:00:00` | `string` | `?date` / `?datetime` | -| uuid / JSON text | `string` | `?uuid` / `?json` | -| `01234`, `2024-01`, ` 12 ` (padded) | `string` | `?string` | - -Keep every column a string: `from_csv($path)->inferSchema(infer_schema()->allStrings())`. - -### 82) `flow-php/etl-adapter-doctrine` - `from_dbal_*()` type columns from the query - -| Driver | Column | Before | After | -|-------------------------------------|---------------------------------------------------------------------------------|---------------|-------------------------------------------------------------------------------------------------| -| `pgsql` | `NUMERIC` | `string` | `float` | -| `pgsql` | `DATE`, `TIMESTAMP`, `TIMESTAMPTZ` | `string` | `DateTimeImmutable` | -| `pgsql` | `TIME` | `string` | `DateInterval` | -| `pgsql` | `UUID` | `string` | `Flow\Types\Value\Uuid` | -| `pgsql` | `JSON`, `JSONB` | `string` | `Flow\Types\Value\Json` | -| `pgsql` | `XML` | `string` | `DOMDocument` | -| `pgsql` | any other type (`INTERVAL`, `INET`, `MONEY`, `OID`, arrays, ranges, enums, ...) | `string` | throws `SchemaNotDerivableException` | -| `mysqli` | `DECIMAL` | `string` | `float` | -| `mysqli` | `DATE`, `DATETIME`, `TIMESTAMP` | `string` | `DateTimeImmutable` | -| `mysqli` | `TIME` | `string` | `DateInterval` | -| `mysqli` | `JSON` | `string` | `Flow\Types\Value\Json` | -| `mysqli` | any other type (`BIT`, `GEOMETRY`, ...) | `string` | throws `SchemaNotDerivableException` | -| `sqlite3`, `pdo_sqlite` | `INTEGER`, `REAL` | `1`, `1.5` | `'1'`, `'1.5'` - every column is `?string` | -| `pdo_pgsql`, `pdo_mysql`, any other | every column | driver values | throws `SchemaNotDerivableException` - use the `pgsql` / `mysqli` driver or `->withSchema(...)` | - -Applies to `from_dbal_query()`, `from_dbal_queries()`, `from_dbal_limit_offset()`, `from_dbal_limit_offset_qb()` and -`from_dbal_key_set_qb()` without `->withSchema()`. A query the driver cannot describe (multi-statement, data-modifying -CTE, `INSERT ... RETURNING`) also throws. Declare `->withSchema(...)` to pick the types. - -### 83) `flow-php/etl-adapter-doctrine`, `-postgresql` - `withPageSize()` / `withFetchSize()` become `withBatchSize()` - -| Before | After | -|-------------------------------------------------------------------------------------------------------|----------------------------------------------| -| `from_pgsql_cursor(...)->withFetchSize(500)` | `from_pgsql_cursor(...)->withBatchSize(500)` | -| `from_pgsql_limit_offset(...)->withPageSize(500)` | `->withBatchSize(500)` | -| `from_pgsql_key_set(...)->withPageSize(500)` | `->withBatchSize(500)` | -| `from_dbal_limit_offset(...)->withPageSize(500)`, `from_dbal_limit_offset_qb(...)->withPageSize(500)` | `->withBatchSize(500)` | -| `from_dbal_key_set_qb(...)->withPageSize(500)` | `->withBatchSize(500)` | -| `Page size must be greater than 0, got 0` / `Fetch size must be greater than 0, got 0` | `Batch size must be greater than 0, got 0` | - -### 84) `flow-php/etl-adapter-excel` - text cells narrow to uuid, json and timezone, `to_excel()` writes date cells - -| | Before | After | -|-------------------------------------------------------------|----------------------------------------------------------|-----------------------------------------------------------------------------| -| text cell holding a uuid / JSON / timezone name | `string` | `?uuid` / `?json` / `?timezone` | -| any other text cell (`TRUE`, `12.9`, `2023-10-02`) | `string` | `?string` | -| blank cell under `withConvertEmptyToNull(false)` | `''` in that row | the whole column is `?string` (`1` reads as `'1'`) | -| `to_excel()` date / datetime value | text cell | date cell | -| `to_excel()->withDateFormat()` / `->withDateTimeFormat()` | PHP `date()` format, default `'Y-m-d'` / `'Y-m-d H:i:s'` | Excel number format, default `'yyyy-mm-dd'` / `'yyyy-mm-dd hh:mm:ss'` | -| `to_excel()` then `from_excel()`, date / datetime column | `string` | `?date` / `?datetime` | -| ZIP-signed file without an extension that is not a workbook | `ValueError: Invalid or uninitialized Zip object` | `Flow\ETL\Exception\InvalidArgumentException: Unsupported file format: n/a` | - -Before: - -```php -to_excel($path)->withDateFormat('Y-m-d')->withDateTimeFormat('Y-m-d H:i:s'); -``` - -After: - -```php -to_excel($path)->withDateFormat('yyyy-mm-dd')->withDateTimeFormat('yyyy-mm-dd hh:mm:ss'); -``` - -### 85) `flow-php/etl-adapter-excel` - requires `openspout/openspout` `~5.3.0` - -| Dependency | Before | After | -|-----------------------|--------|----------| -| `openspout/openspout` | `^5.2` | `~5.3.0` | - -### 86) `flow-php/etl-adapter-google-sheet` - `FORMATTED_VALUE` cells are typed, and `''` reads as `null` - -| | Before | After | -|--------------------------------------------|----------|---------------------------------------------------------------------------------------------------------| -| cell under `FORMATTED_VALUE` (the default) | `string` | typed as a CSV cell, see 81): `'1234'` -> `?integer`, `'TRUE'` -> `?boolean`, `'2024-01-01'` -> `?date` | -| `''` cell | `''` | `null`; keep `''` with `->withEmptyToNull(false)` | - -### 87) `flow-php/etl-adapter-http` - `response_body` and `request_body` hold the raw body text - -| Before | After | -|-----------------------------------------------------------------------------|--------------| -| `response_body` / `request_body` of a JSON or XML message - decoded `array` | `?string` | -| a JSON body that fails to decode - threw `RuntimeException` | kept as text | - -Decode in the pipeline: `->withEntry('response_body', ref('response_body')->jsonDecode())`. - -### 88) `flow-php/etl-adapter-json` - `schema_from_json_schema()` rejects a property with more than one non-null type - -| Before | After | -|------------------------------------------------------------------------|-----------------------------------------------------------| -| `anyOf` / `oneOf` of `string` and `integer` - `integer\|string` column | throws `Flow\ETL\Exception\UnsupportedUnionTypeException` | -| `enum: ["a", 1]` - `integer\|string` column | throws `UnsupportedUnionTypeException` | -| `type: ["string", "integer"]` - `integer\|string` column | throws `UnsupportedUnionTypeException` | -| `anyOf` of `string` and `null` - `?string` | unchanged | - -Give the property a single type in the JSON Schema, or declare the column by hand: `str_schema('v')`, -`json_schema('v')`. - -### 89) `flow-php/etl-adapter-json` - keys outside the sample are dropped, missing keys are `null` - -| | Before | After | -|--------------------------------------------------------|-----------------------------------------------------|-----------------------------------------------------------------------| -| key only a later file carries | kept in that file's rows | dropped; keep it with `->inferSchema(infer_schema()->unionByName())` | -| key first seen past the sample | kept in that row | dropped; keep it with `->inferSchema(infer_schema()->sampleSize(-1))` | -| key missing from a record | absent from that row | `null` | -| nested key every sampled record carries, missing later | read | throws `SchemaMismatchException` | -| nested key first seen past the sample | kept | dropped | -| 0-byte file, `from_json()` | throws `JsonMachine\Exception\SyntaxErrorException` | skipped, no rows | - -Applies to `from_json()` and `from_json_lines()` unless the row names one. - -### 90) `flow-php/etl-adapter-json` - `from_json_lines()` skips blank lines - -| Before | After | -|-----------------------------------------------------------------|----------------------------------------| -| an empty or whitespace-only line - threw `SyntaxErrorException` | skipped, in `schema()` and `extract()` | - -### 91) `flow-php/etl-adapter-parquet` - `to_parquet()` does not validate values against the Parquet schema - -| Before | After | -|---------------------------------------------------------------|----------------------------------------------------------------------| -| `to_parquet($path)` wrote with `Option::VALIDATE_DATA` `true` | `false` | -| - | `to_parquet($path)->withOptions(Options::default())` validates again | - -### 92) `flow-php/etl-adapter-postgresql` - `from_pgsql_*()` type columns from the query instead of reading strings - -| PostgreSQL column | Before | After | -|-----------------------------------------------------------------------|-------------------------|--------------------------------------| -| `numeric` | `string` | `float` | -| `date`, `timestamp`, `timestamptz` | `string` | `DateTimeImmutable` | -| `time` | `string` | `DateInterval` | -| `timetz` | `string` with offset | `DateInterval`, offset dropped | -| `uuid` | `string` | `Flow\Types\Value\Uuid` | -| `json`, `jsonb` | `string` | `Flow\Types\Value\Json` | -| `xml` | `string` | `DOMDocument` | -| `oid` | `string` | `int` | -| one-dimensional array (`int4[]`, `text[]`, ...) | `string` `'{1,NULL,3}'` | `[1, null, 3]`, `null` elements kept | -| multi-dimensional array | `string` | throws while hydrating | -| `record`, `point`, `line`, `lseg`, `box`, `path`, `polygon`, `circle` | `string` | throws `SchemaNotDerivableException` | - -Applies to `from_pgsql_cursor()`, `from_pgsql_limit_offset()` and `from_pgsql_key_set()` without `->withSchema()`. A -query that cannot run as a subquery (multi-statement, data-modifying CTE, `INSERT ... RETURNING`) also throws -`SchemaNotDerivableException`. Declare `->withSchema(...)` to pick the types. - -### 93) `flow-php/etl-adapter-postgresql` - `pgsql_table_to_flow_schema()` maps arrays, text-like types, `oid`, `timetz` - -| PostgreSQL column | Before | After | -|---------------------------------------------------------------------------------------------------------------|-------------------------------|------------------| -| `inet`, `cidr`, `macaddr`, `money`, `int4range`, `int8range`, `numrange`, `tsrange`, `tstzrange`, `daterange` | throws `TypeMappingException` | `string` | -| `oid` | throws `TypeMappingException` | `integer` | -| `timetz` (`time with time zone`) | throws `TypeMappingException` | `time` | -| `integer[]`, any array column | its element type, `integer` | `list` | - -Applies to `pgsql_table_to_flow_schema()` and `EntryTypesMap::toFlowType()`. - -| Before | After | -|---------------------------------------------------------------------------|---------| -| `TypeMappingException::ambiguousEntryType()` / `::unsupportedEntryType()` | removed | - -### 94) `flow-php/etl-adapter-postgresql` - `InsertQueryBuilder::build()` takes the client's converters - -| Before | After | -|-----------------------------------------------|---------------------------------------------------------------------------------------------| -| `$builder->build($values, $schema, $options)` | `$builder->build($values, $schema, $client->converters(), $options)` | -| returns `array{Sql, list}` | returns `array{Sql, ConvertedParameters}` - pass the second element to `$client->execute()` | - -### 95) `flow-php/etl-adapter-xml` - `from_xml()` keeps each element as the source wrote it - -| Before | After | -|------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------| -| `node` re-serialized with added indentation; comments dropped | the element's source text, whitespace and comments included | -| every ancestor namespace declared on `node` | only the ancestor namespaces the element uses | -| a truncated document returned the elements before the cut | throws `RuntimeException` | -| expat error messages | libxml error messages, e.g. `XML Error: Premature end of data in tag root line 1 at line 1` | -| `XMLParserExtractor::startElementHandler()` / `endElementHandler()` / `characterDataHandler()` | removed | - -### 96) `flow-php/cli` - `--schema-auto-cast` removed, `flow schema` ignores the row window - -| Before | After | -|---------------------------------------------------------------------------------------------------|------------------------------------------------| -| `--schema-auto-cast` on `file:analyze`, `file:convert`, `file:read`, `file:schema` | removed | -| `flow schema --input-file-offset=N` / `--input-file-limit=N` - schema of the selected rows | the file's schema; both options have no effect | - -### 97) `flow-php/cli` - command classes name themselves with `#[AsCommand]` - -| Before | After | -|----------------------------------------------------|-------------------------------------------------------------------------------------------------------| -| `new FileConvertCommand()` - named `file:read` | `file:convert` | -| `new FileRowsCountCommand()` - named `file:schema` | `file:rows:count` | -| `new PipelineRunCommand()` - named `run` | `pipeline:run` | -| `Flow\CLI\Command\*` classes carried no aliases | `run`, `read`, `schema`, `count`, `convert`, `analyze`, `format`, as the `flow` binary registers them | - -Registering the commands in your own console application: drop the `setName()` / `setAliases()` calls. - -### 98) `flow-php/flow-php-ext` - the `flow_php` extension must be reinstalled - -| Before | After | -|----------------------------|----------------------------------------| -| `flow_php` extension 0.1.0 | 0.3.0, required by this `flow-php/etl` | - -Reinstall it with the new release: `pie install flow-php/flow-php-ext`. - -### 99) `flow-php/etl-adapter-csv` - `withSeparator()`, `withEnclosure()` and `withEscape()` take a single byte - -| Before | After | -|-----------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------| -| `withSeparator('\|\|')` / `withEnclosure('\|\|')` / `withEscape('ab')` - PHP 8.3: first byte used; PHP 8.4+: `ValueError` on read | throws `Flow\ETL\Exception\InvalidArgumentException` | -| `withSeparator('')` / `withEnclosure('')` - PHP 8.3: `,` / `"` used; PHP 8.4+: `ValueError` on read | throws `Flow\ETL\Exception\InvalidArgumentException` | - -### 100) `flow-php/etl` - `array_expand()` nested in an expression gives rows, and is refused outside `withEntry()` - -| Before | After | -|---------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------| -| `withEntry('s', concat(ref('id'), ref('tags')->expand()))` - one row, list JSON-encoded: `a["x","y"]` | one row per element: `ax`, `ay` | -| `withEntry('v', ref('lists')->expand()->expand())` - one row per outer element, inner list JSON-encoded | throws `InvalidArgumentException` - expand one level per `withEntry()` | -| `array_expand()` in `filter()`, `until()`, `duplicateRow()`, `aggregate()`, `over()` | throws `InvalidArgumentException` at `schema()` / `run()` | -| `array_expand()` in an `onEach()` body | throws `InvalidArgumentException` when `onEach()` is called | - -### 101) `flow-php/array-dot` - `\` escapes every path grammar character, `array_dot_steps()` deprecated - -| Before | After | -|-----------------------------------------------------|-----------------------------------------------| -| `\?x`, `a\*b`, `a\\` read keys `\?x`, `a\*b`, `a\\` | read keys `?x`, `a*b`, `a\` | -| `?{a}` reads key `{a}` | throws `InvalidPathException` - `?\{a\}` | -| `x.*.a\.b` reads `a` -> `b` of each element | reads key `a.b` of each element | -| `x.{a\.b,c}` result keys `a__ESCAPED_DOT__b`, `c` | `a.b`, `c` | -| `x.{a?b}` result key `ab` | `a?b` | -| `array_dot_steps($path)` | deprecated - `Path::fromString($path)->steps` | - -### 102) `flow-php/array-dot` - `array_dot_set()` and `array_dot_rename()` change only the addressed key - -| Before | After | -|---------------------------------------------------------------------------------------------|---------------------------------| -| `array_dot_set(['a' => ['x' => 1]], 'a.y', 2)` -> `['a' => ['y' => 2]]` | `['a' => ['x' => 1, 'y' => 2]]` | -| `array_dot_set([5 => 'a', 7 => 'b'], '5', 'c')` -> `['a', 'b', 'c']` | `[5 => 'c', 7 => 'b']` | -| `array_dot_set([], '\{a\}', 1)` / `'?a'` write keys `\{a\}` / `?a` | write keys `{a}` / `a` | -| `array_dot_set($array, 'x.*', $value)` throws | sets every element of `x` | -| `array_dot_set($array, 'x.{a,b}', $value)` writes key `{a,b}` | throws `InvalidPathException` | -| `array_dot_rename(['{a}' => 1], '\{a\}', 'b')` -> warning, `['{a}' => 1, 'b' => null]` | `['b' => 1]` | -| `array_dot_rename(['a' => 1], '?missing', 'b')` -> warning, `['a' => 1, 'b' => null]` | `['a' => 1]` | -| `array_dot_rename($array, 'x.*', 'c')` / `'x.{a,b}'` -> warning, adds `c => null` under `x` | throws `InvalidPathException` | -| `array_dot_set(['x' => 5], 'x.*', 1)` throws `InvalidPathException` | `['x' => []]` | -| `array_dot_set([], 'x.*', 1)` throws `InvalidPathException` | `['x' => []]` | -| `array_dot_rename(['a' => 1, 'b' => 2], 'a', 'a')` -> `['b' => 2]` | `['a' => 1, 'b' => 2]` | - -### 103) `flow-php/etl` - `array_get_collection()` reads its keys as literal keys - -| Before | After | -|----------------------------------------------|-----------------------------------| -| key `a.b` reads `a` -> `b`, result key `a_b` | reads key `a.b`, result key `a.b` | -| key `k,l` throws `InvalidArgumentException` | reads key `k,l` | - -### 104) `flow-php/etl` - `array_expand()` over a null list gives no rows - -| Before | After | -|-----------------------------------------------|----------------------| -| `null` list throws `InvalidArgumentException` | no rows for that row | - -### 105) `flow-php/array-dot`, `flow-php/etl` - a nullsafe multimatch over an empty array reads `null` - -| Before | After | -|---------------------------------------------------------------------------------------------------------|------------------------------------| -| `array_dot_get([], '{?a}')` throws `InvalidPathException` | `['a' => null]` | -| `array_dot_get([], '{a}')` message `Path "{a}" does not exists ...` | `Path "a" does not exists ...` | -| `array_get_collection(ref('c'), ['id'])` over `[['name' => 'a'], []]` throws `InvalidArgumentException` | `[['id' => null], ['id' => null]]` | - -### 106) `flow-php/filesystem` - every filesystem matches glob patterns the same way - -Files: `data/flat.parquet`, `data/.hidden.parquet`, `data/.dir/x.parquet`, `data/date=2026-09-01/one.parquet`, -`data/id=1/date=2026-09-01/two.parquet` - -| pattern | local, before | memory / S3 / Azure, before | every filesystem, after | -|-------------------------|---------------|-----------------------------|-----------------------------------| -| `data/**.parquet` | `flat` | all five | `.hidden`, `flat` | -| `data/**/*.parquet` | all five | `.dir/x`, `one`, `two` | all five | -| `data/**` | `flat` | all five | all five | -| `data/*.parquet` | `flat` | `.hidden`, `flat` | `.hidden`, `flat` | -| `data/*/*.parquet` | `one` | `.dir/x`, `one` | `.dir/x`, `one` | -| `data/**/[!f]*.parquet` | `flat` | none | `.dir/x`, `.hidden`, `one`, `two` | -| `data/[a-g]*.parquet` | `flat` | none | `flat` | - -Recurse with `data/**/*.parquet`, not `data/**.parquet`. `webmozart/glob` is no longer installed with -`flow-php/filesystem` or `flow-php/etl`. - -### 107) `flow-php/parquet` - `ParquetEngine::openForWrite()` returns a `ParquetFileWriter` - -| Before | After | -|-------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------| -| `$engine->openForWrite($stream, $schema, $c, $o); $engine->writeBatch($rows); $engine->closeWrite();` | `$file = $engine->openForWrite($stream, $schema, $c, $o); $file->writeBatch($rows); $file->close();` | -| `ParquetEngine::closeWrite()`, `writeBatch()`, `writeRow()` | removed - on `ParquetFileWriter` | -| two `Writer`s sharing one engine overwrote each other | every `openForWrite()` returns an independent writer | -| `ArrowParquetEngine` left the destination stream open | `ParquetFileWriter::close()` closes it on every engine | - -### 108) `flow-php/etl-adapter-parquet` - path-only partition columns leave the file body - -| Before | After | -|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------| -| `partitionBy(partition_by('date'))` - file body carries an all-null `date` column | file body without `date` | -| `from_parquet()` types `date` from that body column, e.g. `datetime` | `string` - declare it: `from_parquet($path)->partitionTypes(partition_types(date: type_datetime()))` | diff --git a/src/lib/postgresql/src/Flow/PostgreSql/DSL/condition.php b/src/lib/postgresql/src/Flow/PostgreSql/DSL/condition.php index 02e4d4f246..ab448a7677 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/DSL/condition.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/DSL/condition.php @@ -19,6 +19,7 @@ use Flow\PostgreSql\QueryBuilder\Condition\ConditionBuilder; use Flow\PostgreSql\QueryBuilder\Condition\Exists; use Flow\PostgreSql\QueryBuilder\Condition\In; +use Flow\PostgreSql\QueryBuilder\Condition\InSubquery; use Flow\PostgreSql\QueryBuilder\Condition\IsDistinctFrom; use Flow\PostgreSql\QueryBuilder\Condition\IsNull; use Flow\PostgreSql\QueryBuilder\Condition\Like; @@ -142,13 +143,26 @@ function in_(string|Expression $expr, array $values): In if (count($values) === 1 && $values[0] instanceof Subquery) { throw new InvalidArgumentException( - 'in_() does not support subqueries; use any_($expr, ComparisonOperator::EQ, $subquery) for "IN (subquery)" semantics.', + 'in_() does not support subqueries; use in_subquery($expr, $subquery) for "IN (subquery)".', ); } return new In($expr instanceof Expression ? $expr : col($expr), $values); } +/** + * Create an IN (subquery) condition. + * + * Example: in_subquery(col('id'), select(col('user_id'))->from(table('orders'))) + */ +#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)] +function in_subquery(string|Expression $expr, SelectFinalStep $subquery): InSubquery +{ + return new InSubquery($expr instanceof Expression ? $expr : col($expr), new Node([ + 'select_stmt' => $subquery->toAst(), + ])); +} + /** * Create an IS NULL condition. */ diff --git a/src/lib/postgresql/src/Flow/PostgreSql/DSL/query.php b/src/lib/postgresql/src/Flow/PostgreSql/DSL/query.php index 0d151be0cb..dc5f58f8ee 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/DSL/query.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/DSL/query.php @@ -21,7 +21,6 @@ use Flow\PostgreSql\QueryBuilder\Clause\NullsPosition; use Flow\PostgreSql\QueryBuilder\Clause\OnConflictClause; use Flow\PostgreSql\QueryBuilder\Clause\OrderBy; -use Flow\PostgreSql\QueryBuilder\Clause\ReturningClause; use Flow\PostgreSql\QueryBuilder\Clause\SortDirection; use Flow\PostgreSql\QueryBuilder\Clause\WindowDefinition; use Flow\PostgreSql\QueryBuilder\Clause\WindowFrame; @@ -32,6 +31,7 @@ use Flow\PostgreSql\QueryBuilder\Cursor\DeclareCursorOptionsStep; use Flow\PostgreSql\QueryBuilder\Cursor\FetchCursorBuilder; use Flow\PostgreSql\QueryBuilder\Delete\DeleteBuilder; +use Flow\PostgreSql\QueryBuilder\Delete\DeleteFinalStep; use Flow\PostgreSql\QueryBuilder\Delete\DeleteFromStep; use Flow\PostgreSql\QueryBuilder\Exception\InvalidExpressionException; use Flow\PostgreSql\QueryBuilder\Expression\AggregateCall; @@ -46,6 +46,7 @@ use Flow\PostgreSql\QueryBuilder\Expression\Greatest; use Flow\PostgreSql\QueryBuilder\Expression\Least; use Flow\PostgreSql\QueryBuilder\Expression\Literal; +use Flow\PostgreSql\QueryBuilder\Expression\MergeActionFunction; use Flow\PostgreSql\QueryBuilder\Expression\NullIf; use Flow\PostgreSql\QueryBuilder\Expression\Parameter; use Flow\PostgreSql\QueryBuilder\Expression\RowExpression; @@ -58,10 +59,12 @@ use Flow\PostgreSql\QueryBuilder\Factory\CopyFactory; use Flow\PostgreSql\QueryBuilder\Insert\BulkInsert; use Flow\PostgreSql\QueryBuilder\Insert\InsertBuilder; +use Flow\PostgreSql\QueryBuilder\Insert\InsertFinalStep; use Flow\PostgreSql\QueryBuilder\Insert\InsertIntoStep; use Flow\PostgreSql\QueryBuilder\Listen\ListenBuilder; use Flow\PostgreSql\QueryBuilder\Listen\ListenFinalStep; use Flow\PostgreSql\QueryBuilder\Merge\MergeBuilder; +use Flow\PostgreSql\QueryBuilder\Merge\MergeFinalStep; use Flow\PostgreSql\QueryBuilder\Merge\MergeUsingStep; use Flow\PostgreSql\QueryBuilder\Notify\NotifyBuilder; use Flow\PostgreSql\QueryBuilder\Notify\NotifyFinalStep; @@ -94,6 +97,7 @@ use Flow\PostgreSql\QueryBuilder\Unlisten\UnlistenBuilder; use Flow\PostgreSql\QueryBuilder\Unlisten\UnlistenFinalStep; use Flow\PostgreSql\QueryBuilder\Update\UpdateBuilder; +use Flow\PostgreSql\QueryBuilder\Update\UpdateFinalStep; use Flow\PostgreSql\QueryBuilder\Update\UpdateTableStep; use Flow\PostgreSql\QueryBuilder\With\WithBuilder; use InvalidArgumentException; @@ -574,6 +578,17 @@ function current_date(): SQLValueFunctionExpression return SQLValueFunctionExpression::currentDate(); } +/** + * MERGE merge_action() function: 'INSERT', 'UPDATE' or 'DELETE' for each row in MERGE ... RETURNING. + * + * Example: merge('t')->using('s', 's')->on(...)->whenMatched()->thenDelete()->returning(merge_action()) + */ +#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)] +function merge_action(): MergeActionFunction +{ + return new MergeActionFunction(); +} + /** * SQL standard CURRENT_TIME function. * @@ -831,22 +846,18 @@ function desc(string|Expression $expr, NullsPosition $nulls = NullsPosition::DEF * Create a CTE (Common Table Expression). * * @param string $name CTE name - * @param SelectFinalStep $query CTE query + * @param DeleteFinalStep|InsertFinalStep|MergeFinalStep|SelectFinalStep|UpdateFinalStep $query CTE query * @param array $columnNames Column aliases (optional) * @param CTEMaterialization $materialization Materialization hint */ #[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)] function cte( string $name, - SelectFinalStep $query, + SelectFinalStep|InsertFinalStep|UpdateFinalStep|DeleteFinalStep|MergeFinalStep $query, array $columnNames = [], CTEMaterialization $materialization = CTEMaterialization::DEFAULT, - bool $recursive = false, ): CTE { - $node = new Node(); - $node->setSelectStmt($query->toAst()); - - return new CTE($name, $node, $columnNames, $materialization, $recursive); + return new CTE($name, $query, $columnNames, $materialization); } /** @@ -1014,28 +1025,6 @@ function conflict_constraint(string $name): ConflictTarget return ConflictTarget::constraint($name); } -/** - * Create a RETURNING clause. - * - * @param Expression|string ...$expressions Expressions to return - */ -#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)] -function returning(string|Expression ...$expressions): ReturningClause -{ - return new ReturningClause(array_map(static fn(string|Expression $e): Expression => $e instanceof Expression - ? $e - : col($e), array_values($expressions))); -} - -/** - * Create a RETURNING * clause. - */ -#[DocumentationDSL(module: Module::PG_QUERY, type: DSLType::HELPER)] -function returning_all(): ReturningClause -{ - return ReturningClause::all(); -} - /** * Create a BEGIN transaction builder. * diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/CTE.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/CTE.php index 0860cf1bea..add2a6b2b0 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/CTE.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/CTE.php @@ -5,26 +5,64 @@ namespace Flow\PostgreSql\QueryBuilder\Clause; use Flow\PostgreSql\Protobuf\AST\CommonTableExpr; +use Flow\PostgreSql\Protobuf\AST\CTECycleClause; +use Flow\PostgreSql\Protobuf\AST\CTESearchClause; +use Flow\PostgreSql\Protobuf\AST\DeleteStmt; +use Flow\PostgreSql\Protobuf\AST\InsertStmt; +use Flow\PostgreSql\Protobuf\AST\MergeStmt; use Flow\PostgreSql\Protobuf\AST\Node; use Flow\PostgreSql\Protobuf\AST\PBString; +use Flow\PostgreSql\Protobuf\AST\SelectStmt; +use Flow\PostgreSql\Protobuf\AST\UpdateStmt; use Flow\PostgreSql\QueryBuilder\Bridge\AstConvertible; +use Flow\PostgreSql\QueryBuilder\Delete\DeleteFinalStep; use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; +use Flow\PostgreSql\QueryBuilder\Expression\Expression; +use Flow\PostgreSql\QueryBuilder\Expression\Literal; +use Flow\PostgreSql\QueryBuilder\Insert\InsertFinalStep; +use Flow\PostgreSql\QueryBuilder\Merge\MergeFinalStep; +use Flow\PostgreSql\QueryBuilder\Select\SelectFinalStep; +use Flow\PostgreSql\QueryBuilder\Update\UpdateFinalStep; +use InvalidArgumentException; + +use function array_map; +use function array_values; /** * Represents a Common Table Expression (CTE). */ final readonly class CTE implements AstConvertible { + private Node $query; + /** * @param array $columnNames */ public function __construct( private string $name, - private Node $query, + SelectFinalStep|InsertFinalStep|UpdateFinalStep|DeleteFinalStep|MergeFinalStep|Node $query, private array $columnNames = [], private CTEMaterialization $materialization = CTEMaterialization::DEFAULT, - private bool $recursive = false, - ) {} + private ?CTESearchClause $search = null, + private ?CTECycleClause $cycle = null, + ) { + if ($query instanceof Node) { + $this->query = $query; + + return; + } + + $statement = $query->toAst(); + $node = new Node(); + + $this->query = match (true) { + $statement instanceof SelectStmt => $node->setSelectStmt($statement), + $statement instanceof InsertStmt => $node->setInsertStmt($statement), + $statement instanceof UpdateStmt => $node->setUpdateStmt($statement), + $statement instanceof DeleteStmt => $node->setDeleteStmt($statement), + $statement instanceof MergeStmt => $node->setMergeStmt($statement), + }; + } public static function fromAst(Node $node): static { @@ -62,10 +100,14 @@ public static function fromAst(Node $node): static $columnNames[] = $stringNode->getSval(); } - $materialization = CTEMaterialization::fromProtobuf($commonTableExpr->getCtematerialized()); - $recursive = $commonTableExpr->getCterecursive(); - - return new self($cteName, $cteQuery, $columnNames, $materialization, $recursive); + return new self( + $cteName, + $cteQuery, + $columnNames, + CTEMaterialization::fromProtobuf($commonTableExpr->getCtematerialized()), + $commonTableExpr->getSearchClause(), + $commonTableExpr->getCycleClause(), + ); } /** @@ -81,6 +123,34 @@ public function materialization(): CTEMaterialization return $this->materialization; } + /** + * @param list $columns + */ + public function cycle( + array $columns, + string $markColumn, + string $pathColumn, + ?Expression $markValue = null, + ?Expression $markDefault = null, + ): self { + if ($columns === []) { + throw new InvalidArgumentException('CYCLE needs at least one column'); + } + + if (($markValue === null) !== ($markDefault === null)) { + throw new InvalidArgumentException('CYCLE needs both a mark value and a mark default, or neither'); + } + + $cycle = new CTECycleClause(); + $cycle->setCycleColList(self::stringNodes($columns)); + $cycle->setCycleMarkColumn($markColumn); + $cycle->setCyclePathColumn($pathColumn); + $cycle->setCycleMarkValue(($markValue ?? Literal::bool(true))->toAst()); + $cycle->setCycleMarkDefault(($markDefault ?? Literal::bool(false))->toAst()); + + return new self($this->name, $this->query, $this->columnNames, $this->materialization, $this->search, $cycle); + } + public function materialized(): self { return new self( @@ -88,7 +158,8 @@ public function materialized(): self $this->query, $this->columnNames, CTEMaterialization::MATERIALIZED, - $this->recursive, + $this->search, + $this->cycle, ); } @@ -104,7 +175,8 @@ public function notMaterialized(): self $this->query, $this->columnNames, CTEMaterialization::NOT_MATERIALIZED, - $this->recursive, + $this->search, + $this->cycle, ); } @@ -113,9 +185,20 @@ public function query(): Node return $this->query; } - public function recursive(): bool + /** + * @param list $columns + */ + public function searchBreadthFirst(array $columns, string $sequenceColumn): self { - return $this->recursive; + return $this->search($columns, $sequenceColumn, true); + } + + /** + * @param list $columns + */ + public function searchDepthFirst(array $columns, string $sequenceColumn): self + { + return $this->search($columns, $sequenceColumn, false); } public function toAst(): Node @@ -124,22 +207,11 @@ public function toAst(): Node $commonTableExpr->setCtename($this->name); $commonTableExpr->setCtequery($this->query); $commonTableExpr->setCtematerialized($this->materialization->toProtobuf()); - $commonTableExpr->setCterecursive($this->recursive); + $commonTableExpr->setSearchClause($this->search); + $commonTableExpr->setCycleClause($this->cycle); if ($this->columnNames !== []) { - $aliasNodes = []; - - foreach ($this->columnNames as $columnName) { - $stringNode = new PBString(); - $stringNode->setSval($columnName); - - $node = new Node(); - $node->setString($stringNode); - - $aliasNodes[] = $node; - } - - $commonTableExpr->setAliascolnames($aliasNodes); + $commonTableExpr->setAliascolnames(self::stringNodes($this->columnNames)); } $node = new Node(); @@ -153,6 +225,36 @@ public function toAst(): Node */ public function withColumns(array $columns): self { - return new self($this->name, $this->query, $columns, $this->materialization, $this->recursive); + return new self($this->name, $this->query, $columns, $this->materialization, $this->search, $this->cycle); + } + + /** + * @param array $names + * + * @return list + */ + private static function stringNodes(array $names): array + { + return array_values(array_map( + static fn(string $name): Node => (new Node())->setString((new PBString())->setSval($name)), + $names, + )); + } + + /** + * @param list $columns + */ + private function search(array $columns, string $sequenceColumn, bool $breadthFirst): self + { + if ($columns === []) { + throw new InvalidArgumentException('SEARCH needs at least one column'); + } + + $search = new CTESearchClause(); + $search->setSearchColList(self::stringNodes($columns)); + $search->setSearchBreadthFirst($breadthFirst); + $search->setSearchSeqColumn($sequenceColumn); + + return new self($this->name, $this->query, $this->columnNames, $this->materialization, $search, $this->cycle); } } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/ReturningClause.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/ReturningClause.php index ed030f6e9e..ded60d4f63 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/ReturningClause.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Clause/ReturningClause.php @@ -6,78 +6,74 @@ use Flow\PostgreSql\Protobuf\AST\Node; use Flow\PostgreSql\Protobuf\AST\ResTarget; +use Flow\PostgreSql\Protobuf\AST\ReturningClause as ReturningClauseNode; +use Flow\PostgreSql\Protobuf\AST\ReturningOption; +use Flow\PostgreSql\Protobuf\AST\ReturningOptionKind; use Flow\PostgreSql\QueryBuilder\Bridge\AstConvertible; use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; -use Flow\PostgreSql\QueryBuilder\Expression\Column; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Expression\Expression; use Flow\PostgreSql\QueryBuilder\Expression\ExpressionFactory; -use Flow\PostgreSql\QueryBuilder\Expression\Star; +use InvalidArgumentException; + +use function array_values; -/** - * Represents a RETURNING clause for INSERT/UPDATE/DELETE statements. - */ final readonly class ReturningClause implements AstConvertible { /** * @param list $expressions */ public function __construct( - private array $expressions, + private array $expressions = [], + private ?string $oldAlias = null, + private ?string $newAlias = null, ) {} - public static function all(): self - { - return new self([Star::all()]); - } - - public static function columns(string ...$columns): self - { - $expressions = []; - - foreach ($columns as $column) { - $expressions[] = Column::name($column); - } - - return new self($expressions); - } - public static function fromAst(Node $node): static { - $resTarget = $node->getResTarget(); + $returningClause = $node->getReturningClause(); - if ($resTarget === null) { - throw InvalidAstException::unexpectedNodeType('ResTarget', 'unknown'); + if ($returningClause === null) { + throw InvalidAstException::unexpectedNodeType('ReturningClause', 'unknown'); } - $valNode = $resTarget->getVal(); + $expressions = []; - if ($valNode === null) { - throw InvalidAstException::missingRequiredField('val', 'ResTarget'); + foreach ($returningClause->getExprs() as $expressionNode) { + $expressions[] = ExpressionFactory::fromAst($expressionNode); } - $expression = ExpressionFactory::fromAst($valNode); + $oldAlias = null; + $newAlias = null; + + foreach ($returningClause->getOptions() as $optionNode) { + $option = $optionNode->getReturningOption(); + + if ($option === null) { + throw InvalidAstException::invalidFieldValue( + 'options', + 'ReturningClause', + 'Expected ReturningOption node', + ); + } + + if ($option->getOption() === ReturningOptionKind::RETURNING_OPTION_OLD) { + $oldAlias = $option->getValue(); + } elseif ($option->getOption() === ReturningOptionKind::RETURNING_OPTION_NEW) { + $newAlias = $option->getValue(); + } + } - return new self([$expression]); + return new self($expressions, $oldAlias, $newAlias); } - /** - * @return list - */ - public static function toAstNodes(self $clause): array + public function aliases(?string $old, ?string $new): self { - $nodes = []; - - foreach ($clause->expressions as $expr) { - $resTarget = new ResTarget(); - $resTarget->setVal($expr->toAst()); - - $node = new Node(); - $node->setResTarget($resTarget); - - $nodes[] = $node; + if ($old === null && $new === null) { + throw new InvalidArgumentException('returningAliases() needs an old or a new alias'); } - return $nodes; + return new self($this->expressions, $old, $new); } /** @@ -88,18 +84,58 @@ public function expressions(): array return $this->expressions; } + public function returning(Expression ...$expressions): self + { + if ($expressions === []) { + throw new InvalidArgumentException( + 'returning() needs at least one expression. Call returningAll() for RETURNING *.', + ); + } + + return new self(array_values($expressions), $this->oldAlias, $this->newAlias); + } + public function toAst(): Node { - if ($this->expressions === []) { - throw InvalidAstException::missingRequiredField('expressions', 'ReturningClause'); + $options = []; + + if ($this->oldAlias !== null) { + $options[] = self::optionNode(ReturningOptionKind::RETURNING_OPTION_OLD, $this->oldAlias); + } + + if ($this->newAlias !== null) { + $options[] = self::optionNode(ReturningOptionKind::RETURNING_OPTION_NEW, $this->newAlias); } - $resTarget = new ResTarget(); - $resTarget->setVal($this->expressions[0]->toAst()); + if ($this->expressions === [] && $options !== []) { + throw InvalidBuilderStateException::returningAliasesWithoutList($this->oldAlias, $this->newAlias); + } - $node = new Node(); - $node->setResTarget($resTarget); + $expressionNodes = []; - return $node; + foreach ($this->expressions as $expression) { + $expressionNode = $expression->toAst(); + + if ($expressionNode->getResTarget() === null) { + $expressionNode = (new Node())->setResTarget((new ResTarget())->setVal($expressionNode)); + } + + $expressionNodes[] = $expressionNode; + } + + $returningClause = new ReturningClauseNode(); + $returningClause->setExprs($expressionNodes); + $returningClause->setOptions($options); + + return (new Node())->setReturningClause($returningClause); + } + + private static function optionNode(int $kind, string $alias): Node + { + return (new Node())->setReturningOption( + (new ReturningOption()) + ->setOption($kind) + ->setValue($alias), + ); } } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/ConditionFactory.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/ConditionFactory.php index d03f0aa194..5d40981c5f 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/ConditionFactory.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/ConditionFactory.php @@ -71,7 +71,9 @@ public static function fromAst(Node $node): Condition return match ($subLinkType) { SubLinkType::EXISTS_SUBLINK => Exists::fromAst($node), - SubLinkType::ANY_SUBLINK => Any::fromAst($node), + SubLinkType::ANY_SUBLINK => $subLink->getOperName()->count() === 0 + ? InSubquery::fromAst($node) + : Any::fromAst($node), SubLinkType::ALL_SUBLINK => All::fromAst($node), SubLinkType::EXPR_SUBLINK => BooleanCondition::fromAst($node), default => throw UnsupportedNodeException::forNodeType('SubLink type: ' diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/InSubquery.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/InSubquery.php new file mode 100644 index 0000000000..f3be4606e8 --- /dev/null +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Condition/InSubquery.php @@ -0,0 +1,83 @@ +getSubLink(); + + if ($subLink === null) { + throw InvalidAstException::unexpectedNodeType('SubLink', 'unknown'); + } + + if ($subLink->getSubLinkType() !== SubLinkType::ANY_SUBLINK || $subLink->getOperName()->count() !== 0) { + throw InvalidAstException::invalidFieldValue( + 'sub_link_type', + 'SubLink', + 'Expected ANY_SUBLINK without oper_name for InSubquery condition', + ); + } + + $testexpr = $subLink->getTestexpr(); + + if ($testexpr === null) { + throw InvalidAstException::missingRequiredField('testexpr', 'SubLink'); + } + + $subselect = $subLink->getSubselect(); + + if ($subselect === null) { + throw InvalidAstException::missingRequiredField('subselect', 'SubLink'); + } + + return new self(ExpressionFactory::fromAst($testexpr), $subselect); + } + + public function and(Condition $other): AndCondition + { + return new AndCondition($this, $other); + } + + public function as(string $alias): AliasedExpression + { + return new AliasedExpression($this, $alias); + } + + public function not(): NotCondition + { + return new NotCondition($this); + } + + public function or(Condition $other): OrCondition + { + return new OrCondition($this, $other); + } + + public function toAst(): Node + { + $subLink = new SubLink([ + 'sub_link_type' => SubLinkType::ANY_SUBLINK, + 'testexpr' => $this->expression->toAst(), + 'subselect' => $this->subquery, + ]); + + return new Node(['sub_link' => $subLink]); + } +} diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteBuilder.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteBuilder.php index 2d1a8a238a..99b87f0ab8 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteBuilder.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteBuilder.php @@ -8,15 +8,13 @@ use Flow\PostgreSql\Protobuf\AST\DeleteStmt; use Flow\PostgreSql\Protobuf\AST\Node; use Flow\PostgreSql\Protobuf\AST\RangeVar; -use Flow\PostgreSql\Protobuf\AST\ResTarget; -use Flow\PostgreSql\Protobuf\AST\ReturningClause; use Flow\PostgreSql\QueryBuilder\AstDeparser; +use Flow\PostgreSql\QueryBuilder\Clause\ReturningClause; use Flow\PostgreSql\QueryBuilder\Clause\WithClause; use Flow\PostgreSql\QueryBuilder\Condition\Condition; use Flow\PostgreSql\QueryBuilder\Condition\ConditionFactory; use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; use Flow\PostgreSql\QueryBuilder\Expression\Expression; -use Flow\PostgreSql\QueryBuilder\Expression\ExpressionFactory; use Flow\PostgreSql\QueryBuilder\Expression\Star; use Flow\PostgreSql\QueryBuilder\QualifiedIdentifier; use Flow\PostgreSql\QueryBuilder\Table\AliasedTable; @@ -31,7 +29,6 @@ { /** * @param array $using - * @param array $returning */ private function __construct( private ?WithClause $with = null, @@ -40,7 +37,7 @@ private function __construct( private ?string $alias = null, private array $using = [], private ?Condition $where = null, - private array $returning = [], + private ?ReturningClause $returning = null, ) {} public static function create(): DeleteFromStep @@ -114,31 +111,7 @@ public static function fromAst(DeleteStmt $deleteStmt): static } } - $returningExpressions = []; $returningClause = $deleteStmt->getReturningClause(); - $returningList = $returningClause !== null ? $returningClause->getExprs() : []; - - if (count($returningList) > 0) { - foreach ($returningList as $resTargetNode) { - $resTarget = $resTargetNode->getResTarget(); - - if ($resTarget === null) { - throw InvalidAstException::invalidFieldValue( - 'returning_list', - 'DeleteStmt', - 'Expected ResTarget node', - ); - } - - $val = $resTarget->getVal(); - - if ($val === null) { - throw InvalidAstException::missingRequiredField('val', 'ResTarget'); - } - - $returningExpressions[] = ExpressionFactory::fromAst($val); - } - } return new self( with: $withClause, @@ -147,7 +120,9 @@ public static function fromAst(DeleteStmt $deleteStmt): static alias: $aliasName, using: $using, where: $whereCondition, - returning: $returningExpressions, + returning: $returningClause === null + ? null + : ReturningClause::fromAst((new Node())->setReturningClause($returningClause)), ); } @@ -187,11 +162,11 @@ public function returning(Expression ...$expressions): DeleteFinalStep alias: $this->alias, using: $this->using, where: $this->where, - returning: $expressions, + returning: ($this->returning ?? new ReturningClause())->returning(...$expressions), ); } - public function returningAll(): DeleteFinalStep + public function returningAliases(?string $old = null, ?string $new = null): DeleteReturningStep { return new self( with: $this->with, @@ -200,10 +175,15 @@ public function returningAll(): DeleteFinalStep alias: $this->alias, using: $this->using, where: $this->where, - returning: [Star::all()], + returning: ($this->returning ?? new ReturningClause())->aliases($old, $new), ); } + public function returningAll(): DeleteFinalStep + { + return $this->returning(Star::all()); + } + public function toSql(): string { return (new AstDeparser())->deparse($this->toAst()); @@ -258,22 +238,8 @@ public function toAst(): DeleteStmt $deleteStmt->setWhereClause($this->where->toAst()); } - if ($this->returning !== []) { - $returningNodes = []; - - foreach ($this->returning as $expression) { - $resTarget = new ResTarget(); - $resTarget->setVal($expression->toAst()); - - $resTargetNode = new Node(); - $resTargetNode->setResTarget($resTarget); - - $returningNodes[] = $resTargetNode; - } - - $returningClause = new ReturningClause(); - $returningClause->setExprs($returningNodes); - $deleteStmt->setReturningClause($returningClause); + if ($this->returning !== null) { + $deleteStmt->setReturningClause($this->returning->toAst()->getReturningClause()); } return $deleteStmt; diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteReturningStep.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteReturningStep.php index 1fb7822c9d..6a6eca6ffc 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteReturningStep.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Delete/DeleteReturningStep.php @@ -22,4 +22,6 @@ public function returning(Expression ...$expressions): DeleteFinalStep; * Add a RETURNING * clause to return all columns. */ public function returningAll(): DeleteFinalStep; + + public function returningAliases(?string $old = null, ?string $new = null): DeleteReturningStep; } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Exception/InvalidBuilderStateException.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Exception/InvalidBuilderStateException.php index 7b67847c70..8b099dcc84 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Exception/InvalidBuilderStateException.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Exception/InvalidBuilderStateException.php @@ -6,6 +6,7 @@ use InvalidArgumentException; +use function implode; use function sprintf; final class InvalidBuilderStateException extends InvalidArgumentException @@ -26,4 +27,29 @@ public static function noOperationSpecified(string $builderName): self { return new self(sprintf('No operation specified for %s', $builderName)); } + + public static function returningAliasesWithoutList(?string $oldAlias, ?string $newAlias): self + { + $aliases = []; + + if ($oldAlias !== null) { + $aliases[] = 'OLD AS ' . $oldAlias; + } + + if ($newAlias !== null) { + $aliases[] = 'NEW AS ' . $newAlias; + } + + return new self(sprintf('RETURNING WITH (%s) needs a RETURNING list. Call returning() or returningAll() after returningAliases().', implode( + ', ', + $aliases, + ))); + } + + public static function recursiveWithOnMerge(): self + { + return new self( + 'MERGE does not accept WITH RECURSIVE, because PostgreSQL rejects it. Remove recursive() from the WITH clause of this MERGE, or use INSERT, UPDATE or DELETE.', + ); + } } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/ExpressionFactory.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/ExpressionFactory.php index b7dbb5b12c..db82a449c9 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/ExpressionFactory.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/ExpressionFactory.php @@ -8,6 +8,8 @@ use Flow\PostgreSql\Protobuf\AST\FuncCall; use Flow\PostgreSql\Protobuf\AST\MinMaxOp; use Flow\PostgreSql\Protobuf\AST\Node; +use Flow\PostgreSql\Protobuf\AST\SubLinkType; +use Flow\PostgreSql\QueryBuilder\Condition\ConditionFactory; use Flow\PostgreSql\QueryBuilder\Exception\UnsupportedNodeException; use function count; @@ -99,8 +101,12 @@ public static function fromAst(Node $node): Expression return CaseExpression::fromAst($node); } - if ($node->getSubLink() !== null) { - return Subquery::fromAst($node); + $subLink = $node->getSubLink(); + + if ($subLink !== null) { + return $subLink->getSubLinkType() === SubLinkType::EXPR_SUBLINK + ? Subquery::fromAst($node) + : ConditionFactory::fromAst($node); } $minMaxExpr = $node->getMinMaxExpr(); @@ -125,6 +131,10 @@ public static function fromAst(Node $node): Expression return SQLValueFunctionExpression::fromAst($node); } + if ($node->getMergeSupportFunc() !== null) { + return MergeActionFunction::fromAst($node); + } + if ($node->getCollateClause() !== null) { return Collate::fromAst($node); } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/MergeActionFunction.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/MergeActionFunction.php new file mode 100644 index 0000000000..bb83db5c9a --- /dev/null +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Expression/MergeActionFunction.php @@ -0,0 +1,34 @@ +getMergeSupportFunc() === null) { + throw InvalidAstException::unexpectedNodeType('MergeSupportFunc', 'unknown'); + } + + return new self(); + } + + public function as(string $alias): AliasedExpression + { + return AliasedExpression::create($this, $alias); + } + + public function toAst(): Node + { + $node = new Node(); + $node->setMergeSupportFunc(new MergeSupportFunc()); + + return $node; + } +} diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertBuilder.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertBuilder.php index 55cea18dd7..22d17446b8 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertBuilder.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertBuilder.php @@ -10,11 +10,11 @@ use Flow\PostgreSql\Protobuf\AST\PBList; use Flow\PostgreSql\Protobuf\AST\RangeVar; use Flow\PostgreSql\Protobuf\AST\ResTarget; -use Flow\PostgreSql\Protobuf\AST\ReturningClause; use Flow\PostgreSql\Protobuf\AST\SelectStmt; use Flow\PostgreSql\QueryBuilder\AstDeparser; use Flow\PostgreSql\QueryBuilder\Clause\ConflictTarget; use Flow\PostgreSql\QueryBuilder\Clause\OnConflictClause; +use Flow\PostgreSql\QueryBuilder\Clause\ReturningClause; use Flow\PostgreSql\QueryBuilder\Clause\WithClause; use Flow\PostgreSql\QueryBuilder\Condition\Condition; use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; @@ -33,7 +33,6 @@ /** * @param list $columns * @param list> $valuesList - * @param list $returning */ private function __construct( private ?WithClause $with = null, @@ -45,8 +44,7 @@ private function __construct( private ?SelectFinalStep $selectQuery = null, private bool $defaultValues = false, private ?OnConflictClause $onConflict = null, - private array $returning = [], - private bool $returningAll = false, + private ?ReturningClause $returning = null, ) {} public static function create(): InsertIntoStep @@ -173,31 +171,7 @@ public function toAst(): SelectStmt $onConflict = OnConflictClause::fromAst($conflictNode); } - $returning = []; - $returningAll = false; - $insertReturningClause = $insertStmt->getReturningClause(); - $returningListNodes = $insertReturningClause !== null ? $insertReturningClause->getExprs() : []; - - foreach ($returningListNodes as $retNode) { - $resTarget = $retNode->getResTarget(); - - if ($resTarget !== null) { - $valNode = $resTarget->getVal(); - - if ($valNode !== null) { - $expr = ExpressionFactory::fromAst($valNode); - - if ($expr instanceof Star && !$expr->isQualified()) { - $returningAll = true; - $returning = []; - - break; - } - - $returning[] = $expr; - } - } - } + $returningClause = $insertStmt->getReturningClause(); $with = null; $withClauseNode = $insertStmt->getWithClause(); @@ -216,8 +190,9 @@ public function toAst(): SelectStmt $selectQuery, $defaultValues, $onConflict, - $returning, - $returningAll, + $returningClause === null + ? null + : ReturningClause::fromAst((new Node())->setReturningClause($returningClause)), ); } @@ -239,7 +214,6 @@ public function columns(string ...$columns): InsertValuesStep $this->defaultValues, $this->onConflict, $this->returning, - $this->returningAll, ); } @@ -256,7 +230,6 @@ public function defaultValues(): InsertOnConflictStep true, $this->onConflict, $this->returning, - $this->returningAll, ); } @@ -282,7 +255,6 @@ public function into(string|Table $table, ?string $alias = null): InsertColumnsS $this->defaultValues, $this->onConflict, $this->returning, - $this->returningAll, ); } @@ -299,7 +271,6 @@ public function onConflict(OnConflictClause $clause): InsertReturningStep $this->defaultValues, $clause, $this->returning, - $this->returningAll, ); } @@ -321,7 +292,6 @@ public function onConflictDoUpdate(ConflictTarget $target, array $updates): Inse $this->defaultValues, OnConflictClause::doUpdate($target, $updates), $this->returning, - $this->returningAll, ); } @@ -337,12 +307,11 @@ public function returning(Expression ...$expressions): InsertFinalStep $this->selectQuery, $this->defaultValues, $this->onConflict, - array_values([...$expressions]), - false, + ($this->returning ?? new ReturningClause())->returning(...$expressions), ); } - public function returningAll(): InsertFinalStep + public function returningAliases(?string $old = null, ?string $new = null): InsertReturningStep { return new self( $this->with, @@ -354,11 +323,15 @@ public function returningAll(): InsertFinalStep $this->selectQuery, $this->defaultValues, $this->onConflict, - [], - true, + ($this->returning ?? new ReturningClause())->aliases($old, $new), ); } + public function returningAll(): InsertFinalStep + { + return $this->returning(Star::all()); + } + public function select(SelectFinalStep $select): InsertOnConflictStep { return new self( @@ -372,7 +345,6 @@ public function select(SelectFinalStep $select): InsertOnConflictStep false, $this->onConflict, $this->returning, - $this->returningAll, ); } @@ -460,32 +432,8 @@ public function toAst(): InsertStmt } } - if ($this->returningAll) { - $resTarget = new ResTarget(); - $resTarget->setVal(Star::all()->toAst()); - - $retNode = new Node(); - $retNode->setResTarget($resTarget); - - $returningClause = new ReturningClause(); - $returningClause->setExprs([$retNode]); - $insertStmt->setReturningClause($returningClause); - } elseif ($this->returning !== []) { - $returningNodes = []; - - foreach ($this->returning as $expr) { - $resTarget = new ResTarget(); - $resTarget->setVal($expr->toAst()); - - $retNode = new Node(); - $retNode->setResTarget($resTarget); - - $returningNodes[] = $retNode; - } - - $returningClause = new ReturningClause(); - $returningClause->setExprs($returningNodes); - $insertStmt->setReturningClause($returningClause); + if ($this->returning !== null) { + $insertStmt->setReturningClause($this->returning->toAst()->getReturningClause()); } if ($this->with !== null) { @@ -513,7 +461,6 @@ public function values(Expression ...$values): InsertValuesStep $this->defaultValues, $this->onConflict, $this->returning, - $this->returningAll, ); } @@ -534,7 +481,6 @@ public function where(Condition $condition): InsertReturningStep $this->defaultValues, $this->onConflict->where($condition), $this->returning, - $this->returningAll, ); } } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertReturningStep.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertReturningStep.php index 876c55d744..d21d6b4174 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertReturningStep.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Insert/InsertReturningStep.php @@ -11,4 +11,6 @@ interface InsertReturningStep extends InsertFinalStep public function returning(Expression ...$expressions): InsertFinalStep; public function returningAll(): InsertFinalStep; + + public function returningAliases(?string $old = null, ?string $new = null): InsertReturningStep; } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeBuilder.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeBuilder.php index fd387f6213..9e81716d62 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeBuilder.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeBuilder.php @@ -12,9 +12,12 @@ use Flow\PostgreSql\Protobuf\AST\RangeVar; use Flow\PostgreSql\Protobuf\AST\ResTarget; use Flow\PostgreSql\QueryBuilder\AstDeparser; +use Flow\PostgreSql\QueryBuilder\Clause\ReturningClause; use Flow\PostgreSql\QueryBuilder\Clause\WithClause; use Flow\PostgreSql\QueryBuilder\Condition\Condition; use Flow\PostgreSql\QueryBuilder\Exception\InvalidExpressionException; +use Flow\PostgreSql\QueryBuilder\Expression\Expression; +use Flow\PostgreSql\QueryBuilder\Expression\Star; use Flow\PostgreSql\QueryBuilder\QualifiedIdentifier; use Flow\PostgreSql\QueryBuilder\Select\SelectFinalStep; @@ -37,6 +40,7 @@ private function __construct( private ?string $sourceAlias = null, private ?Condition $joinCondition = null, private array $whenClauses = [], + private ?ReturningClause $returning = null, ) {} public static function create(): MergeIntoStep @@ -62,6 +66,7 @@ public function addWhenClause(MergeWhenClauseData $clause): MergeWhenStep $this->sourceAlias, $this->joinCondition, [...$this->whenClauses, $clause], + $this->returning, ); } @@ -80,6 +85,7 @@ public function into(string $table, ?string $alias = null): MergeUsingStep $this->sourceAlias, $this->joinCondition, $this->whenClauses, + $this->returning, ); } @@ -96,9 +102,49 @@ public function on(Condition $condition): MergeWhenStep $this->sourceAlias, $condition, $this->whenClauses, + $this->returning, ); } + public function returning(Expression ...$expressions): MergeFinalStep + { + return new self( + $this->with, + $this->table, + $this->schema, + $this->tableAlias, + $this->sourceTable, + $this->sourceSchema, + $this->sourceSelect, + $this->sourceAlias, + $this->joinCondition, + $this->whenClauses, + ($this->returning ?? new ReturningClause())->returning(...$expressions), + ); + } + + public function returningAliases(?string $old = null, ?string $new = null): MergeWhenStep + { + return new self( + $this->with, + $this->table, + $this->schema, + $this->tableAlias, + $this->sourceTable, + $this->sourceSchema, + $this->sourceSelect, + $this->sourceAlias, + $this->joinCondition, + $this->whenClauses, + ($this->returning ?? new ReturningClause())->aliases($old, $new), + ); + } + + public function returningAll(): MergeFinalStep + { + return $this->returning(Star::all()); + } + public function toSql(): string { return (new AstDeparser())->deparse($this->toAst()); @@ -188,6 +234,10 @@ public function toAst(): MergeStmt $mergeStmt->setMergeWhenClauses($whenClauseNodes); + if ($this->returning !== null) { + $mergeStmt->setReturningClause($this->returning->toAst()->getReturningClause()); + } + if ($this->with !== null) { $withNode = $this->with->toAst(); $withClause = $withNode->getWithClause(); @@ -230,6 +280,7 @@ public function using(string|SelectFinalStep $source, string $alias): MergeOnSte $alias, $this->joinCondition, $this->whenClauses, + $this->returning, ); } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeWhenStep.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeWhenStep.php index 250612572d..2db30d9f77 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeWhenStep.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Merge/MergeWhenStep.php @@ -5,9 +5,16 @@ namespace Flow\PostgreSql\QueryBuilder\Merge; use Flow\PostgreSql\QueryBuilder\Condition\Condition; +use Flow\PostgreSql\QueryBuilder\Expression\Expression; interface MergeWhenStep extends MergeFinalStep { + public function returning(Expression ...$expressions): MergeFinalStep; + + public function returningAll(): MergeFinalStep; + + public function returningAliases(?string $old = null, ?string $new = null): MergeWhenStep; + public function whenMatched(): MergeWhenMatched; public function whenMatchedAnd(Condition $condition): MergeWhenMatched; diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateBuilder.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateBuilder.php index 89b77c7662..5f0501a97b 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateBuilder.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateBuilder.php @@ -8,9 +8,9 @@ use Flow\PostgreSql\Protobuf\AST\Node; use Flow\PostgreSql\Protobuf\AST\RangeVar; use Flow\PostgreSql\Protobuf\AST\ResTarget; -use Flow\PostgreSql\Protobuf\AST\ReturningClause; use Flow\PostgreSql\Protobuf\AST\UpdateStmt; use Flow\PostgreSql\QueryBuilder\AstDeparser; +use Flow\PostgreSql\QueryBuilder\Clause\ReturningClause; use Flow\PostgreSql\QueryBuilder\Clause\WithClause; use Flow\PostgreSql\QueryBuilder\Condition\Condition; use Flow\PostgreSql\QueryBuilder\Condition\ConditionFactory; @@ -38,7 +38,6 @@ /** * @param array $assignments * @param array $from - * @param array $returning */ private function __construct( private ?WithClause $with = null, @@ -48,7 +47,7 @@ private function __construct( private array $assignments = [], private array $from = [], private ?Condition $where = null, - private array $returning = [], + private ?ReturningClause $returning = null, ) {} public static function create(): UpdateTableStep @@ -140,17 +139,20 @@ public static function fromAst(UpdateStmt $updateStmt): static $where = ConditionFactory::fromAst($whereNode); } - $returning = []; $returningClause = $updateStmt->getReturningClause(); - $returningList = $returningClause !== null ? $returningClause->getExprs() : []; - if (count($returningList) > 0) { - foreach ($returningList as $returningNode) { - $returning[] = ExpressionFactory::fromAst($returningNode); - } - } - - return new self($with, $table, $schema, $alias, $assignments, $from, $where, $returning); + return new self( + $with, + $table, + $schema, + $alias, + $assignments, + $from, + $where, + $returningClause === null + ? null + : ReturningClause::fromAst((new Node())->setReturningClause($returningClause)), + ); } public static function with(WithClause $with): UpdateTableStep @@ -191,7 +193,21 @@ public function returning(Expression ...$expressions): UpdateFinalStep assignments: $this->assignments, from: $this->from, where: $this->where, - returning: $expressions, + returning: ($this->returning ?? new ReturningClause())->returning(...$expressions), + ); + } + + public function returningAliases(?string $old = null, ?string $new = null): UpdateReturningStep + { + return new self( + with: $this->with, + table: $this->table, + schema: $this->schema, + alias: $this->alias, + assignments: $this->assignments, + from: $this->from, + where: $this->where, + returning: ($this->returning ?? new ReturningClause())->aliases($old, $new), ); } @@ -288,22 +304,8 @@ public function toAst(): UpdateStmt $updateStmt->setWhereClause($this->where->toAst()); } - if ($this->returning !== []) { - $returningList = []; - - foreach ($this->returning as $expression) { - $resTarget = new ResTarget(); - $resTarget->setVal($expression->toAst()); - - $node = new Node(); - $node->setResTarget($resTarget); - - $returningList[] = $node; - } - - $returningClause = new ReturningClause(); - $returningClause->setExprs($returningList); - $updateStmt->setReturningClause($returningClause); + if ($this->returning !== null) { + $updateStmt->setReturningClause($this->returning->toAst()->getReturningClause()); } if ($this->with !== null) { diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateReturningStep.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateReturningStep.php index 5cc637563b..beeaa10282 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateReturningStep.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/Update/UpdateReturningStep.php @@ -22,4 +22,6 @@ public function returning(Expression ...$expressions): UpdateFinalStep; * Add RETURNING * clause. */ public function returningAll(): UpdateFinalStep; + + public function returningAliases(?string $old = null, ?string $new = null): UpdateReturningStep; } diff --git a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/With/WithBuilder.php b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/With/WithBuilder.php index 616e02ed34..d3641ed82e 100644 --- a/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/With/WithBuilder.php +++ b/src/lib/postgresql/src/Flow/PostgreSql/QueryBuilder/With/WithBuilder.php @@ -7,6 +7,7 @@ use Flow\PostgreSql\QueryBuilder\Clause\WithClause; use Flow\PostgreSql\QueryBuilder\Delete\DeleteBuilder; use Flow\PostgreSql\QueryBuilder\Delete\DeleteFromStep; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Expression\Expression; use Flow\PostgreSql\QueryBuilder\Insert\InsertBuilder; use Flow\PostgreSql\QueryBuilder\Insert\InsertIntoStep; @@ -44,6 +45,10 @@ public function insert(): InsertIntoStep */ public function merge(string $table, ?string $alias = null): MergeUsingStep { + if ($this->withClause->recursive()) { + throw InvalidBuilderStateException::recursiveWithOnMerge(); + } + return MergeBuilder::with($this->withClause)->into($table, $alias); } diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/DeleteDatabaseTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/DeleteDatabaseTest.php index bafd9160fc..4086977570 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/DeleteDatabaseTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/DeleteDatabaseTest.php @@ -4,9 +4,14 @@ namespace Flow\PostgreSql\Tests\Integration\QueryBuilder\Database; +use Flow\PostgreSql\Protobuf\AST\DeleteStmt; +use Flow\PostgreSql\QueryBuilder\Condition\ComparisonOperator; +use Flow\PostgreSql\QueryBuilder\Delete\DeleteBuilder; use Flow\PostgreSql\Tests\Integration\PostgreSqlTestCase; use function Flow\PostgreSql\DSL\agg_count; +use function Flow\PostgreSql\DSL\any_; +use function Flow\PostgreSql\DSL\asc; use function Flow\PostgreSql\DSL\col; use function Flow\PostgreSql\DSL\column; use function Flow\PostgreSql\DSL\column_type_integer; @@ -15,6 +20,7 @@ use function Flow\PostgreSql\DSL\column_type_timestamp; use function Flow\PostgreSql\DSL\column_type_varchar; use function Flow\PostgreSql\DSL\create; +use function Flow\PostgreSql\DSL\cte; use function Flow\PostgreSql\DSL\delete; use function Flow\PostgreSql\DSL\eq; use function Flow\PostgreSql\DSL\insert; @@ -22,8 +28,11 @@ use function Flow\PostgreSql\DSL\or_; use function Flow\PostgreSql\DSL\primary_key; use function Flow\PostgreSql\DSL\select; +use function Flow\PostgreSql\DSL\sql_parse; use function Flow\PostgreSql\DSL\star; use function Flow\PostgreSql\DSL\table; +use function Flow\PostgreSql\DSL\with; +use function Flow\Types\DSL\type_instance_of; final class DeleteDatabaseTest extends PostgreSqlTestCase { @@ -110,6 +119,94 @@ public function test_delete_all(): void static::assertSame(0, $row['cnt']); } + public function test_delete_rebuilt_from_parsed_in_subquery(): void + { + static::assertSame( + 2, + $this + ->pgsqlContext() + ->client() + ->execute( + DeleteBuilder::fromAst( + type_instance_of(DeleteStmt::class)->assert( + sql_parse( + 'DELETE FROM ' + . self::TABLE_LOGS + . ' WHERE id IN (SELECT log_id FROM ' + . self::TABLE_ARCHIVE + . ')', + )->raw()->getStmts()[0] + ->getStmt() + ?->getDeleteStmt(), + ), + )->toSql(), + ), + ); + static::assertSame( + [['id' => 3], ['id' => 4], ['id' => 5]], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + select(col('id')) + ->from(table(self::TABLE_LOGS)) + ->orderBy(asc(col('id'))) + ->toSql(), + ), + ); + } + + public function test_delete_with_data_modifying_cte_removes_parents_and_children(): void + { + static::assertSame( + 2, + $this + ->pgsqlContext() + ->client() + ->execute( + with( + cte( + 'removed_logs', + select(col('id'))->from(table(self::TABLE_LOGS))->where(eq(col('level'), literal('DEBUG'))), + ), + cte( + 'removed_archive', + delete() + ->from(self::TABLE_ARCHIVE) + ->where(any_( + col('log_id'), + ComparisonOperator::EQ, + select(col('id'))->from(table('removed_logs')), + )), + ), + ) + ->delete() + ->from(self::TABLE_LOGS) + ->where(any_(col('id'), ComparisonOperator::EQ, select(col('id'))->from(table('removed_logs')))) + ->toSql(), + ), + ); + static::assertSame( + [['id' => 1], ['id' => 4], ['id' => 5]], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + select(col('id')) + ->from(table(self::TABLE_LOGS)) + ->orderBy(asc(col('id'))) + ->toSql(), + ), + ); + static::assertSame( + [['log_id' => 1]], + $this + ->pgsqlContext() + ->client() + ->fetchAll(select(col('log_id'))->from(table(self::TABLE_ARCHIVE))->toSql()), + ); + } + public function test_delete_with_multiple_conditions(): void { $query = delete() diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/MergeDatabaseTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/MergeDatabaseTest.php index dc6654fc2d..320ef68e3b 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/MergeDatabaseTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/MergeDatabaseTest.php @@ -17,6 +17,7 @@ use function Flow\PostgreSql\DSL\insert; use function Flow\PostgreSql\DSL\literal; use function Flow\PostgreSql\DSL\merge; +use function Flow\PostgreSql\DSL\merge_action; use function Flow\PostgreSql\DSL\order_by; use function Flow\PostgreSql\DSL\primary_key; use function Flow\PostgreSql\DSL\select; @@ -100,6 +101,33 @@ protected function tearDown(): void parent::tearDown(); } + public function test_merge_returning_merge_action_per_row(): void + { + static::assertEqualsCanonicalizing( + [ + ['merge_action' => 'DELETE', 'id' => 1], + ['merge_action' => 'UPDATE', 'id' => 2], + ['merge_action' => 'INSERT', 'id' => 3], + ], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + merge(self::TABLE_TARGET, 't') + ->using(self::TABLE_SOURCE, 's') + ->on(eq(col('t.id'), col('s.id'))) + ->whenMatchedAnd(eq(col('s.id'), literal(1))) + ->thenDelete() + ->whenMatched() + ->thenUpdate(['value' => col('s.value')]) + ->whenNotMatched() + ->thenInsert(['id', 'name', 'value'], [col('s.id'), col('s.name'), col('s.value')]) + ->returning(merge_action(), col('s.id')) + ->toSql(), + ), + ); + } + public function test_merge_delete_matched(): void { $query = merge(self::TABLE_TARGET, 't') diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/SelectDatabaseTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/SelectDatabaseTest.php index 2f3af98aac..b2be13b681 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/SelectDatabaseTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/SelectDatabaseTest.php @@ -7,8 +7,10 @@ use Flow\PostgreSql\Tests\Integration\PostgreSqlTestCase; use function count; +use function Flow\PostgreSql\DSL\agg_count; use function Flow\PostgreSql\DSL\agg_sum; use function Flow\PostgreSql\DSL\and_; +use function Flow\PostgreSql\DSL\binary_expr; use function Flow\PostgreSql\DSL\col; use function Flow\PostgreSql\DSL\column; use function Flow\PostgreSql\DSL\column_type_decimal; @@ -17,11 +19,17 @@ use function Flow\PostgreSql\DSL\column_type_varchar; use function Flow\PostgreSql\DSL\create; use function Flow\PostgreSql\DSL\cte; +use function Flow\PostgreSql\DSL\delete; use function Flow\PostgreSql\DSL\desc; use function Flow\PostgreSql\DSL\eq; use function Flow\PostgreSql\DSL\gt; +use function Flow\PostgreSql\DSL\in_subquery; use function Flow\PostgreSql\DSL\insert; +use function Flow\PostgreSql\DSL\is_true; use function Flow\PostgreSql\DSL\literal; +use function Flow\PostgreSql\DSL\lt; +use function Flow\PostgreSql\DSL\merge; +use function Flow\PostgreSql\DSL\merge_action; use function Flow\PostgreSql\DSL\primary_key; use function Flow\PostgreSql\DSL\select; use function Flow\PostgreSql\DSL\star; @@ -137,6 +145,117 @@ public function test_select_specific_columns(): void static::assertArrayNotHasKey('id', $rows[0]); } + public function test_select_reads_returning_of_merge_cte(): void + { + static::assertSame( + [['merge_action' => 'DELETE', 'id' => 2]], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + with(cte('m', merge(self::TABLE_ORDERS, 'o') + ->using(self::TABLE_USERS, 'u') + ->on(eq(col('user_id', 'o'), col('id', 'u'))) + ->whenMatchedAnd(eq(col('status', 'o'), literal('pending'))) + ->thenDelete() + ->returning(merge_action(), col('id', 'o')))) + ->select(star()) + ->from(table('m')) + ->toSql(), + ), + ); + } + + public function test_select_reads_returning_of_data_modifying_cte(): void + { + static::assertSame( + [['id' => 2, 'user_id' => 1]], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + with(cte('removed_orders', delete() + ->from(self::TABLE_ORDERS) + ->where(eq(col('status'), literal('pending'))) + ->returning(col('id'), col('user_id')))) + ->select(col('id'), col('user_id')) + ->from(table('removed_orders')) + ->toSql(), + ), + ); + static::assertSame( + 3, + $this + ->pgsqlContext() + ->client() + ->fetchSingle(select(agg_count(star())->as('cnt'))->from(table(self::TABLE_ORDERS))->toSql())['cnt'], + ); + } + + public function test_select_from_cte_with_cycle_stops_on_cyclic_graph(): void + { + static::assertSame( + [['id' => 1, 'path' => ['(1)', '(2)', '(3)', '(1)']]], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + with( + cte( + 'edges', + select(literal(1)->as('id'), literal(2)->as('link')) + ->unionAll(select(literal(2), literal(3))) + ->unionAll(select(literal(3), literal(1))), + ), + cte( + 't', + select(col('id'), col('link')) + ->from(table('edges')) + ->where(eq(col('id'), literal(1))) + ->unionAll( + select(col('id', 'e'), col('link', 'e')) + ->from(table('edges')->as('e')) + ->join(table('t'), eq(col('id', 'e'), col('link', 't'))), + ), + ['id', 'link'], + )->cycle(['id'], 'is_cycle', 'path'), + ) + ->recursive() + ->select(col('id'), col('path')) + ->from(table('t')) + ->where(is_true(col('is_cycle'))) + ->toSql(), + ), + ); + } + + public function test_select_from_cte_with_search_breadth_first_orders_by_sequence_column(): void + { + static::assertSame( + [['n' => 3], ['n' => 2], ['n' => 1]], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + with(cte( + 't', + select(literal(1)) + ->unionAll( + select(binary_expr(col('n'), '+', literal(1))) + ->from(table('t')) + ->where(lt(col('n'), literal(3))), + ), + ['n'], + )->searchBreadthFirst(['n'], 'ord')) + ->recursive() + ->select(col('n')) + ->from(table('t')) + ->orderBy(desc(col('ord'))) + ->toSql(), + ), + ); + } + public function test_select_with_cte(): void { $cteQuery = select(col('user_id'), agg_sum(col('amount'))->as('total')) @@ -250,6 +369,27 @@ public function test_select_with_schema_qualified_table(): void static::assertSame('schema@example.com', $rows[0]['email']); } + public function test_select_with_in_subquery(): void + { + static::assertSame( + [['name' => 'John Doe']], + $this + ->pgsqlContext() + ->client() + ->fetchAll( + select(col('name')) + ->from(table(self::TABLE_USERS)) + ->where(in_subquery( + 'id', + select(col('user_id')) + ->from(table(self::TABLE_ORDERS)) + ->where(eq(col('status'), literal('pending'))), + )) + ->toSql(), + ), + ); + } + public function test_select_with_where(): void { $query = select(star())->from(table(self::TABLE_USERS))->where(eq(col('name'), literal('John Doe'))); diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/UpdateDatabaseTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/UpdateDatabaseTest.php index f342fb6c2b..b5d862b117 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/UpdateDatabaseTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Integration/QueryBuilder/Database/UpdateDatabaseTest.php @@ -173,6 +173,26 @@ public function test_update_with_returning(): void static::assertSame('80000.00', $row['salary']); } + public function test_update_with_returning_aliases_returns_old_and_new_values(): void + { + static::assertSame( + ['salary' => '50000.00', 'department_id' => 2], + $this + ->pgsqlContext() + ->client() + ->fetchSingle( + update() + ->update(self::TABLE_EMPLOYEES) + ->set('salary', literal(70000)) + ->set('department_id', literal(2)) + ->where(eq(col('name'), literal('Alice'))) + ->returningAliases(old: 'o', new: 'n') + ->returning(col('salary', 'o'), col('department_id', 'n')) + ->toSql(), + ), + ); + } + public function test_update_with_returning_all(): void { $query = update() diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/CTETest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/CTETest.php index 5fb1d97d4f..480d3cd888 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/CTETest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/CTETest.php @@ -5,16 +5,30 @@ namespace Flow\PostgreSql\Tests\Unit\QueryBuilder\Clause; use Flow\PostgreSql\Protobuf\AST\Node; -use Flow\PostgreSql\Protobuf\AST\SelectStmt; use Flow\PostgreSql\QueryBuilder\Clause\CTE; use Flow\PostgreSql\QueryBuilder\Clause\CTEMaterialization; +use InvalidArgumentException; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; +use function Flow\PostgreSql\DSL\col; +use function Flow\PostgreSql\DSL\cte; +use function Flow\PostgreSql\DSL\delete; +use function Flow\PostgreSql\DSL\eq; +use function Flow\PostgreSql\DSL\insert; +use function Flow\PostgreSql\DSL\literal; +use function Flow\PostgreSql\DSL\merge; +use function Flow\PostgreSql\DSL\select; +use function Flow\PostgreSql\DSL\star; +use function Flow\PostgreSql\DSL\table; +use function Flow\PostgreSql\DSL\update; +use function Flow\PostgreSql\DSL\with; + final class CTETest extends TestCase { public function test_converts_simple_cte_to_ast(): void { - $query = $this->createMockSelectNode(); + $query = (new Node())->setSelectStmt(select(star())->from(table('users'))->toAst()); $cte = new CTE('active_users', $query); $node = $cte->toAst(); @@ -23,13 +37,11 @@ public function test_converts_simple_cte_to_ast(): void static::assertNotNull($commonTableExpr); static::assertSame('active_users', $commonTableExpr->getCtename()); static::assertSame($query, $commonTableExpr->getCtequery()); - static::assertFalse($commonTableExpr->getCterecursive()); } public function test_converts_to_ast_with_column_names(): void { - $query = $this->createMockSelectNode(); - $cte = new CTE('totals', $query, ['category', 'amount']); + $cte = cte('totals', select(star())->from(table('users')), ['category', 'amount']); $node = $cte->toAst(); @@ -51,20 +63,217 @@ public function test_converts_to_ast_with_column_names(): void public function test_cte_getters(): void { - $query = $this->createMockSelectNode(); - $cte = new CTE('test_cte', $query, ['col1', 'col2'], CTEMaterialization::MATERIALIZED, true); + $query = (new Node())->setSelectStmt(select(star())->from(table('users'))->toAst()); + $cte = new CTE('test_cte', $query, ['col1', 'col2'], CTEMaterialization::MATERIALIZED); static::assertSame('test_cte', $cte->name()); static::assertSame($query, $cte->query()); static::assertSame(['col1', 'col2'], $cte->columnNames()); static::assertSame(CTEMaterialization::MATERIALIZED, $cte->materialization()); - static::assertTrue($cte->recursive()); + } + + public function test_cte_with_delete_body(): void + { + $cte = cte( + 'd', + delete() + ->from('children') + ->where(eq(col('parent_id'), literal(1))) + ->returning(col('id')), + ); + + static::assertNotNull($cte->query()->getDeleteStmt()); + static::assertSame( + 'WITH d AS (DELETE FROM children WHERE parent_id = 1 RETURNING id) SELECT id FROM d', + with($cte)->select(col('id'))->from(table('d'))->toSql(), + ); + } + + public function test_cte_with_insert_body(): void + { + $cte = cte( + 'i', + insert()->into('audit')->columns('id', 'note')->values(literal(1), literal('x'))->returning(col('id')), + ); + + static::assertNotNull($cte->query()->getInsertStmt()); + static::assertSame( + "WITH i AS (INSERT INTO audit (id, note) VALUES (1, 'x') RETURNING id) SELECT id FROM i", + with($cte)->select(col('id'))->from(table('i'))->toSql(), + ); + } + + public function test_cte_with_merge_body(): void + { + $cte = cte( + 'm', + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->whenMatched() + ->thenDelete(), + ); + + static::assertNotNull($cte->query()->getMergeStmt()); + static::assertSame( + 'WITH m AS (MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE) SELECT 1', + with($cte)->select(literal(1))->toSql(), + ); + } + + public function test_cte_with_select_body(): void + { + $cte = cte('s', select(col('id'))->from(table('parents'))); + + static::assertNotNull($cte->query()->getSelectStmt()); + static::assertSame( + 'WITH s AS (SELECT id FROM parents) SELECT id FROM s', + with($cte)->select(col('id'))->from(table('s'))->toSql(), + ); + } + + public function test_cte_with_update_body(): void + { + $cte = cte('u', update() + ->update('children') + ->set('parent_id', literal(2)) + ->where(eq(col('id'), literal(10))) + ->returning(col('id'), col('parent_id'))); + + static::assertNotNull($cte->query()->getUpdateStmt()); + static::assertSame( + 'WITH u AS (UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING id, parent_id) SELECT id, parent_id FROM u', + with($cte)->select(col('id'), col('parent_id'))->from(table('u'))->toSql(), + ); + } + + public function test_cycle_requires_at_least_one_column(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('CYCLE needs at least one column'); + + cte('t', select(star())->from(table('users')))->cycle([], 'is_cycle', 'path'); + } + + #[TestWith([true])] + #[TestWith([false])] + public function test_cycle_requires_both_mark_value_and_mark_default_or_neither(bool $withMarkValue): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('CYCLE needs both a mark value and a mark default, or neither'); + + cte('t', select(star())->from(table('users')))->cycle( + ['id'], + 'is_cycle', + 'path', + $withMarkValue ? literal('Y') : null, + $withMarkValue ? null : literal('N'), + ); + } + + public function test_cycle_sets_cycle_clause(): void + { + $cycleClause = cte('t', select(star())->from(table('users'))) + ->cycle(['id', 'link'], 'is_cycle', 'path', literal('Y'), literal('N')) + ->toAst() + ->getCommonTableExpr() + ?->getCycleClause(); + + static::assertNotNull($cycleClause); + static::assertCount(2, $cycleClause->getCycleColList()); + static::assertSame('id', $cycleClause->getCycleColList()[0]->getString()?->getSval()); + static::assertSame('link', $cycleClause->getCycleColList()[1]->getString()?->getSval()); + static::assertSame('is_cycle', $cycleClause->getCycleMarkColumn()); + static::assertSame('path', $cycleClause->getCyclePathColumn()); + static::assertSame( + literal('Y')->toAst()->serializeToString(), + $cycleClause->getCycleMarkValue()?->serializeToString(), + ); + static::assertSame( + literal('N')->toAst()->serializeToString(), + $cycleClause->getCycleMarkDefault()?->serializeToString(), + ); + } + + public function test_cycle_without_mark_values_uses_true_and_false(): void + { + $cycleClause = cte('t', select(star())->from(table('users'))) + ->cycle(['id'], 'is_cycle', 'path') + ->toAst() + ->getCommonTableExpr() + ?->getCycleClause(); + + static::assertNotNull($cycleClause); + static::assertSame( + literal(true)->toAst()->serializeToString(), + $cycleClause->getCycleMarkValue()?->serializeToString(), + ); + static::assertSame( + literal(false)->toAst()->serializeToString(), + $cycleClause->getCycleMarkDefault()?->serializeToString(), + ); + } + + public function test_materialization_and_columns_keep_search_and_cycle_clauses(): void + { + $cte = cte('t', select(star())->from(table('users'))) + ->searchDepthFirst(['id'], 'ord') + ->cycle(['id'], 'is_cycle', 'path'); + + foreach ([$cte->materialized(), $cte->notMaterialized(), $cte->withColumns(['id', 'link'])] as $copy) { + $commonTableExpr = $copy->toAst()->getCommonTableExpr(); + + static::assertSame('ord', $commonTableExpr?->getSearchClause()?->getSearchSeqColumn()); + static::assertSame('is_cycle', $commonTableExpr->getCycleClause()?->getCycleMarkColumn()); + } + } + + public function test_search_breadth_first_sets_search_clause(): void + { + $searchClause = cte('t', select(star())->from(table('users'))) + ->searchBreadthFirst(['n', 'm'], 'ord') + ->toAst() + ->getCommonTableExpr() + ?->getSearchClause(); + + static::assertNotNull($searchClause); + static::assertCount(2, $searchClause->getSearchColList()); + static::assertSame('n', $searchClause->getSearchColList()[0]->getString()?->getSval()); + static::assertSame('m', $searchClause->getSearchColList()[1]->getString()?->getSval()); + static::assertTrue($searchClause->getSearchBreadthFirst()); + static::assertSame('ord', $searchClause->getSearchSeqColumn()); + } + + public function test_search_depth_first_sets_search_clause(): void + { + $searchClause = cte('t', select(star())->from(table('users'))) + ->searchDepthFirst(['n'], 'ord') + ->toAst() + ->getCommonTableExpr() + ?->getSearchClause(); + + static::assertNotNull($searchClause); + static::assertCount(1, $searchClause->getSearchColList()); + static::assertSame('n', $searchClause->getSearchColList()[0]->getString()?->getSval()); + static::assertFalse($searchClause->getSearchBreadthFirst()); + static::assertSame('ord', $searchClause->getSearchSeqColumn()); + } + + #[TestWith([true])] + #[TestWith([false])] + public function test_search_requires_at_least_one_column(bool $breadthFirst): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('SEARCH needs at least one column'); + + $cte = cte('t', select(star())->from(table('users'))); + + $breadthFirst ? $cte->searchBreadthFirst([], 'ord') : $cte->searchDepthFirst([], 'ord'); } public function test_immutable_materialized_method(): void { - $query = $this->createMockSelectNode(); - $cte = new CTE('expensive', $query); + $cte = cte('expensive', select(star())->from(table('users'))); static::assertSame(CTEMaterialization::DEFAULT, $cte->materialization()); @@ -77,8 +286,7 @@ public function test_immutable_materialized_method(): void public function test_immutable_not_materialized_method(): void { - $query = $this->createMockSelectNode(); - $cte = new CTE('temp', $query); + $cte = cte('temp', select(star())->from(table('users'))); $notMaterialized = $cte->notMaterialized(); @@ -89,8 +297,7 @@ public function test_immutable_not_materialized_method(): void public function test_immutable_with_columns_method(): void { - $query = $this->createMockSelectNode(); - $cte = new CTE('users', $query); + $cte = cte('users', select(star())->from(table('users'))); static::assertSame([], $cte->columnNames()); @@ -101,22 +308,9 @@ public function test_immutable_with_columns_method(): void static::assertNotSame($cte, $withColumns); } - public function test_recursive_cte_to_ast(): void - { - $query = $this->createMockSelectNode(); - $cte = new CTE('hierarchy', $query, recursive: true); - - $node = $cte->toAst(); - - $commonTableExpr = $node->getCommonTableExpr(); - static::assertNotNull($commonTableExpr); - static::assertTrue($commonTableExpr->getCterecursive()); - } - public function test_roundtrip_conversion_simple(): void { - $query = $this->createMockSelectNode(); - $original = new CTE('test', $query); + $original = cte('test', select(star())->from(table('users'))); $node = $original->toAst(); $reconstructed = CTE::fromAst($node); @@ -124,13 +318,16 @@ public function test_roundtrip_conversion_simple(): void static::assertSame($original->name(), $reconstructed->name()); static::assertSame($original->columnNames(), $reconstructed->columnNames()); static::assertSame($original->materialization(), $reconstructed->materialization()); - static::assertSame($original->recursive(), $reconstructed->recursive()); } public function test_roundtrip_conversion_with_all_options(): void { - $query = $this->createMockSelectNode(); - $original = new CTE('complex', $query, ['a', 'b', 'c'], CTEMaterialization::MATERIALIZED, true); + $original = cte( + 'complex', + select(star())->from(table('users')), + ['a', 'b', 'c'], + CTEMaterialization::MATERIALIZED, + ); $node = $original->toAst(); $reconstructed = CTE::fromAst($node); @@ -138,15 +335,5 @@ public function test_roundtrip_conversion_with_all_options(): void static::assertSame($original->name(), $reconstructed->name()); static::assertSame($original->columnNames(), $reconstructed->columnNames()); static::assertSame($original->materialization(), $reconstructed->materialization()); - static::assertSame($original->recursive(), $reconstructed->recursive()); - } - - private function createMockSelectNode(): Node - { - $selectStmt = new SelectStmt(); - $node = new Node(); - $node->setSelectStmt($selectStmt); - - return $node; } } diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/ReturningClauseTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/ReturningClauseTest.php index 20a74e5d6a..1d9665a17b 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/ReturningClauseTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/ReturningClauseTest.php @@ -4,55 +4,100 @@ namespace Flow\PostgreSql\Tests\Unit\QueryBuilder\Clause; +use Flow\PostgreSql\Protobuf\AST\ReturningOptionKind; use Flow\PostgreSql\QueryBuilder\Clause\ReturningClause; -use Flow\PostgreSql\QueryBuilder\Expression\Column; -use Flow\PostgreSql\QueryBuilder\Expression\Star; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; +use function Flow\PostgreSql\DSL\col; +use function Flow\PostgreSql\DSL\literal; + final class ReturningClauseTest extends TestCase { - public function test_all(): void + public function test_aliases_without_alias_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('returningAliases() needs an old or a new alias'); + + (new ReturningClause())->aliases(null, null); + } + + public function test_aliases_without_expressions_throw_at_to_ast(): void { - $returning = ReturningClause::all(); + $this->expectException(InvalidBuilderStateException::class); + $this->expectExceptionMessage( + 'RETURNING WITH (OLD AS o, NEW AS n) needs a RETURNING list. Call returning() or returningAll() after returningAliases().', + ); - static::assertCount(1, $returning->expressions()); - static::assertInstanceOf(Star::class, $returning->expressions()[0]); + (new ReturningClause()) + ->aliases('o', 'n') + ->toAst(); } - public function test_columns(): void + public function test_from_ast_of_another_node_throws(): void { - $returning = ReturningClause::columns('id', 'created_at'); + $this->expectException(InvalidAstException::class); - static::assertCount(2, $returning->expressions()); - static::assertInstanceOf(Column::class, $returning->expressions()[0]); - static::assertInstanceOf(Column::class, $returning->expressions()[1]); + ReturningClause::fromAst(literal(1)->toAst()); } - public function test_single_column(): void + public function test_from_ast_reads_expressions_and_aliases(): void { - $returning = ReturningClause::columns('id'); + $clause = (new ReturningClause()) + ->aliases('o', 'n') + ->returning(col('id', 'o'), col('id', 'n')); - static::assertCount(1, $returning->expressions()); + static::assertEquals($clause, ReturningClause::fromAst($clause->toAst())); } - public function test_to_ast(): void + public function test_returning_without_expressions_throws(): void { - $returning = ReturningClause::columns('id'); - $node = $returning->toAst(); - $resTarget = $node->getResTarget(); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'returning() needs at least one expression. Call returningAll() for RETURNING *.', + ); - static::assertNotNull($resTarget); + (new ReturningClause())->returning(); } - public function test_to_ast_nodes(): void + public function test_to_ast_maps_expressions_and_aliases(): void { - $returning = ReturningClause::columns('id', 'name'); - $nodes = ReturningClause::toAstNodes($returning); + $returningClause = (new ReturningClause()) + ->aliases('o', 'n') + ->returning(col('id', 'o'), col('id', 'n')) + ->toAst() + ->getReturningClause(); + + static::assertNotNull($returningClause); + static::assertCount(2, $returningClause->getExprs()); + static::assertSame( + col('id', 'o')->toAst()->serializeToString(), + $returningClause->getExprs()[0]->getResTarget()?->getVal()?->serializeToString(), + ); + static::assertCount(2, $returningClause->getOptions()); + static::assertSame( + ReturningOptionKind::RETURNING_OPTION_OLD, + $returningClause->getOptions()[0]->getReturningOption()?->getOption(), + ); + static::assertSame('o', $returningClause->getOptions()[0]->getReturningOption()?->getValue()); + static::assertSame( + ReturningOptionKind::RETURNING_OPTION_NEW, + $returningClause->getOptions()[1]->getReturningOption()?->getOption(), + ); + static::assertSame('n', $returningClause->getOptions()[1]->getReturningOption()?->getValue()); + } - static::assertCount(2, $nodes); + public function test_to_ast_without_aliases_has_no_options(): void + { + $returningClause = (new ReturningClause()) + ->returning(col('id')) + ->toAst() + ->getReturningClause(); - foreach ($nodes as $node) { - static::assertNotNull($node->getResTarget()); - } + static::assertNotNull($returningClause); + static::assertCount(1, $returningClause->getExprs()); + static::assertCount(0, $returningClause->getOptions()); } } diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/WithClauseTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/WithClauseTest.php index 6b35f2e064..5db98a6459 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/WithClauseTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Clause/WithClauseTest.php @@ -97,7 +97,7 @@ public function test_immutable_add_method(): void public function test_recursive_with_clause_to_ast(): void { $query = $this->createMockSelectNode(); - $cte = new CTE('hierarchy', $query, recursive: true); + $cte = new CTE('hierarchy', $query); $withClause = new WithClause([$cte], recursive: true); $node = $withClause->toAst(); diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InSubqueryTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InSubqueryTest.php new file mode 100644 index 0000000000..b59e8ed873 --- /dev/null +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InSubqueryTest.php @@ -0,0 +1,74 @@ +expectException(InvalidAstException::class); + + InSubquery::fromAst(any_(col('id'), ComparisonOperator::EQ, select(col('id'))->from(table('d')))->toAst()); + } + + public function test_from_ast_of_another_sublink_throws(): void + { + $this->expectException(InvalidAstException::class); + + InSubquery::fromAst(exists(select(col('id'))->from(table('d')))->toAst()); + } + + public function test_from_ast_reads_to_ast_node(): void + { + $condition = in_subquery('id', select(col('id'))->from(table('d'))); + + static::assertEquals($condition, InSubquery::fromAst($condition->toAst())); + } + + public function test_in_subquery_to_sql(): void + { + static::assertSame( + 'SELECT id FROM parents WHERE id IN (SELECT id FROM d)', + select(col('id')) + ->from(table('parents')) + ->where(in_subquery('id', select(col('id'))->from(table('d')))) + ->toSql(), + ); + } + + public function test_not_in_subquery_to_sql(): void + { + static::assertSame( + 'SELECT id FROM parents WHERE NOT id IN (SELECT id FROM d)', + select(col('id')) + ->from(table('parents')) + ->where(not_(in_subquery('id', select(col('id'))->from(table('d'))))) + ->toSql(), + ); + } + + public function test_to_ast_writes_any_sublink_without_operator(): void + { + $subLink = in_subquery('id', select(col('id'))->from(table('d')))->toAst()->getSubLink(); + + static::assertNotNull($subLink); + static::assertSame(SubLinkType::ANY_SUBLINK, $subLink->getSubLinkType()); + static::assertCount(0, $subLink->getOperName()); + } +} diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InTest.php index ce573b34eb..727eb2906a 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Condition/InTest.php @@ -170,7 +170,9 @@ public function test_or_returns_or_condition(): void public function test_in_with_single_subquery_throws(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('in_() does not support subqueries'); + $this->expectExceptionMessage( + 'in_() does not support subqueries; use in_subquery($expr, $subquery) for "IN (subquery)".', + ); in_(col('y'), [sub_select(select(col('id'))->from(table('s')))]); } diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Delete/DeleteBuilderTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Delete/DeleteBuilderTest.php index 8f5deb34ca..9ec401247f 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Delete/DeleteBuilderTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Delete/DeleteBuilderTest.php @@ -18,11 +18,14 @@ use Flow\PostgreSql\QueryBuilder\Delete\DeleteBuilder; use Flow\PostgreSql\QueryBuilder\Delete\DeleteFinalStep; use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Expression\Column; use Flow\PostgreSql\QueryBuilder\Expression\FunctionCall; use Flow\PostgreSql\QueryBuilder\Expression\Literal; use Flow\PostgreSql\QueryBuilder\Table\Table; +use InvalidArgumentException; use LogicException; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use function assert; @@ -34,7 +37,9 @@ use function Flow\PostgreSql\DSL\literal; use function Flow\PostgreSql\DSL\param; use function Flow\PostgreSql\DSL\select; +use function Flow\PostgreSql\DSL\sql_parse; use function Flow\PostgreSql\DSL\table; +use function Flow\Types\DSL\type_instance_of; use function function_exists; final class DeleteBuilderTest extends TestCase @@ -814,6 +819,85 @@ public function test_to_ast_without_table_throws_exception(): void $builder->toAst(); } + #[TestWith([ + 'DELETE FROM parents WHERE id IN (SELECT id FROM d)', + 'DELETE FROM parents WHERE id IN (SELECT id FROM d)', + ])] + #[TestWith([ + 'WITH d AS (SELECT id FROM parents) DELETE FROM parents WHERE id IN (SELECT id FROM d)', + 'WITH d AS (SELECT id FROM parents) DELETE FROM parents WHERE id IN (SELECT id FROM d)', + ])] + #[TestWith([ + 'DELETE FROM parents WHERE id NOT IN (SELECT id FROM d)', + 'DELETE FROM parents WHERE NOT id IN (SELECT id FROM d)', + ])] + public function test_from_ast_round_trips_in_subquery(string $sql, string $deparsed): void + { + static::assertSame( + $deparsed, + DeleteBuilder::fromAst( + type_instance_of(DeleteStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getDeleteStmt(), + ), + )->toSql(), + ); + } + + public function test_from_ast_keeps_returning_aliases(): void + { + static::assertSame( + 'DELETE FROM children WHERE id = 10 RETURNING WITH (OLD AS o) o.id', + DeleteBuilder::fromAst( + type_instance_of(DeleteStmt::class)->assert( + sql_parse('DELETE FROM children WHERE id = 10 RETURNING WITH (OLD AS o) o.id')->raw()->getStmts()[0] + ->getStmt() + ?->getDeleteStmt(), + ), + )->toSql(), + ); + } + + public function test_returning_aliases_to_sql(): void + { + static::assertSame( + 'DELETE FROM children WHERE id = 10 RETURNING WITH (OLD AS o) o.id', + delete() + ->from('children') + ->where(eq(col('id'), literal(10))) + ->returningAliases(old: 'o') + ->returning(col('id', 'o')) + ->toSql(), + ); + } + + public function test_returning_aliases_without_alias_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('returningAliases() needs an old or a new alias'); + + delete()->from('children')->returningAliases(); + } + + public function test_returning_aliases_without_returning_list_throws(): void + { + $this->expectException(InvalidBuilderStateException::class); + $this->expectExceptionMessage( + 'RETURNING WITH (OLD AS o) needs a RETURNING list. Call returning() or returningAll() after returningAliases().', + ); + + delete()->from('children')->returningAliases(old: 'o')->toSql(); + } + + public function test_returning_without_expressions_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'returning() needs at least one expression. Call returningAll() for RETURNING *.', + ); + + delete()->from('children')->returning(); + } + private function deparse(DeleteStmt $deleteStmt): string { $parser = new Parser(); diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Expression/MergeActionFunctionTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Expression/MergeActionFunctionTest.php new file mode 100644 index 0000000000..1a911034dd --- /dev/null +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Expression/MergeActionFunctionTest.php @@ -0,0 +1,37 @@ +expectException(InvalidAstException::class); + + MergeActionFunction::fromAst(literal(1)->toAst()); + } + + public function test_from_ast_reads_merge_support_func_node(): void + { + static::assertEquals( + merge_action(), + MergeActionFunction::fromAst((new Node())->setMergeSupportFunc(new MergeSupportFunc())), + ); + } + + public function test_to_ast_gives_merge_support_func_node(): void + { + static::assertNotNull(merge_action()->toAst()->getMergeSupportFunc()); + } +} diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Insert/InsertBuilderTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Insert/InsertBuilderTest.php index 48461ba2d9..564f127dbf 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Insert/InsertBuilderTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Insert/InsertBuilderTest.php @@ -12,10 +12,13 @@ use Flow\PostgreSql\QueryBuilder\Clause\ConflictTarget; use Flow\PostgreSql\QueryBuilder\Clause\OnConflictClause; use Flow\PostgreSql\QueryBuilder\Condition\IsNull; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Expression\Column; use Flow\PostgreSql\QueryBuilder\Expression\FunctionCall; use Flow\PostgreSql\QueryBuilder\Expression\Literal; use Flow\PostgreSql\QueryBuilder\Insert\InsertBuilder; +use InvalidArgumentException; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use function count; @@ -26,7 +29,9 @@ use function Flow\PostgreSql\DSL\param; use function Flow\PostgreSql\DSL\parameters; use function Flow\PostgreSql\DSL\select; +use function Flow\PostgreSql\DSL\sql_parse; use function Flow\PostgreSql\DSL\table; +use function Flow\Types\DSL\type_instance_of; use function function_exists; final class InsertBuilderTest extends TestCase @@ -746,6 +751,84 @@ public function test_simple_insert_with_values_to_sql(): void ); } + #[TestWith([ + 'INSERT INTO audit (id) SELECT id FROM parents WHERE id IN (SELECT id FROM d)', + 'INSERT INTO audit (id) SELECT id FROM parents WHERE id IN (SELECT id FROM d)', + ])] + #[TestWith([ + 'INSERT INTO audit (id) SELECT id FROM parents WHERE id NOT IN (SELECT id FROM d)', + 'INSERT INTO audit (id) SELECT id FROM parents WHERE NOT id IN (SELECT id FROM d)', + ])] + public function test_from_ast_round_trips_in_subquery(string $sql, string $deparsed): void + { + static::assertSame( + $deparsed, + InsertBuilder::fromAst( + type_instance_of(InsertStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getInsertStmt(), + ), + )->toSql(), + ); + } + + public function test_from_ast_keeps_returning_aliases(): void + { + static::assertSame( + 'INSERT INTO children (id, parent_id) VALUES (12, 1) RETURNING WITH (NEW AS n) n.id', + InsertBuilder::fromAst( + type_instance_of(InsertStmt::class)->assert( + sql_parse( + 'INSERT INTO children (id, parent_id) VALUES (12, 1) RETURNING WITH (NEW AS n) n.id', + )->raw()->getStmts()[0] + ->getStmt() + ?->getInsertStmt(), + ), + )->toSql(), + ); + } + + public function test_returning_aliases_to_sql(): void + { + static::assertSame( + 'INSERT INTO children (id, parent_id) VALUES (12, 1) RETURNING WITH (NEW AS n) n.id', + insert() + ->into('children') + ->columns('id', 'parent_id') + ->values(literal(12), literal(1)) + ->returningAliases(new: 'n') + ->returning(col('id', 'n')) + ->toSql(), + ); + } + + public function test_returning_aliases_without_alias_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('returningAliases() needs an old or a new alias'); + + insert()->into('children')->columns('id')->values(literal(12))->returningAliases(); + } + + public function test_returning_aliases_without_returning_list_throws(): void + { + $this->expectException(InvalidBuilderStateException::class); + $this->expectExceptionMessage( + 'RETURNING WITH (NEW AS n) needs a RETURNING list. Call returning() or returningAll() after returningAliases().', + ); + + insert()->into('children')->columns('id')->values(literal(12))->returningAliases(new: 'n')->toSql(); + } + + public function test_returning_without_expressions_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'returning() needs at least one expression. Call returningAll() for RETURNING *.', + ); + + insert()->into('children')->columns('id')->values(literal(12))->returning(); + } + private function deparse(InsertStmt $insertStmt): string { $parser = new Parser(); diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Merge/MergeBuilderTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Merge/MergeBuilderTest.php index 3b1c3d6c7f..5046660cea 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Merge/MergeBuilderTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Merge/MergeBuilderTest.php @@ -14,6 +14,7 @@ use Flow\PostgreSql\QueryBuilder\Clause\WithClause; use Flow\PostgreSql\QueryBuilder\Condition\Comparison; use Flow\PostgreSql\QueryBuilder\Condition\ComparisonOperator; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Exception\InvalidExpressionException; use Flow\PostgreSql\QueryBuilder\Expression\Column; use Flow\PostgreSql\QueryBuilder\Expression\Literal; @@ -26,6 +27,7 @@ use Flow\PostgreSql\QueryBuilder\Merge\MergeWhenNotMatched; use Flow\PostgreSql\QueryBuilder\Select\SelectBuilder; use Flow\PostgreSql\QueryBuilder\Table\Table; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; use function assert; @@ -36,6 +38,7 @@ use function Flow\PostgreSql\DSL\gt; use function Flow\PostgreSql\DSL\literal; use function Flow\PostgreSql\DSL\merge; +use function Flow\PostgreSql\DSL\merge_action; use function Flow\PostgreSql\DSL\param; use function Flow\PostgreSql\DSL\select; use function Flow\PostgreSql\DSL\table; @@ -826,6 +829,89 @@ public function test_to_ast_without_when_clauses_throws_exception(): void $builder->toAst(); } + public function test_merge_returning_all_to_sql(): void + { + static::assertSame( + 'MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING *', + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->whenMatched() + ->thenDelete() + ->returningAll() + ->toSql(), + ); + } + + public function test_merge_returning_aliases_to_sql(): void + { + static::assertSame( + 'MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN UPDATE SET parent_id = 2 RETURNING WITH (OLD AS o, NEW AS n) o.parent_id, n.parent_id', + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->whenMatched() + ->thenUpdate(['parent_id' => literal(2)]) + ->returningAliases('o', 'n') + ->returning(col('parent_id', 'o'), col('parent_id', 'n')) + ->toSql(), + ); + } + + public function test_merge_returning_aliases_without_alias_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('returningAliases() needs an old or a new alias'); + + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->returningAliases(); + } + + public function test_merge_returning_aliases_without_returning_list_throws(): void + { + $this->expectException(InvalidBuilderStateException::class); + $this->expectExceptionMessage( + 'RETURNING WITH (OLD AS o) needs a RETURNING list. Call returning() or returningAll() after returningAliases().', + ); + + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->whenMatched() + ->thenDelete() + ->returningAliases(old: 'o') + ->toSql(); + } + + public function test_merge_returning_without_expressions_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'returning() needs at least one expression. Call returningAll() for RETURNING *.', + ); + + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->returning(); + } + + public function test_merge_returning_to_sql(): void + { + static::assertSame( + 'MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING merge_action() , c.id', + merge('children', 'c') + ->using('parents', 'p') + ->on(eq(col('parent_id', 'c'), col('id', 'p'))) + ->whenMatched() + ->thenDelete() + ->returning(merge_action(), col('id', 'c')) + ->toSql(), + ); + } + private function deparse(MergeStmt $mergeStmt): string { $parser = new Parser(); diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Select/SelectBuilderTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Select/SelectBuilderTest.php index 9218974b1e..3b98f7fbc7 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Select/SelectBuilderTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Select/SelectBuilderTest.php @@ -4,6 +4,7 @@ namespace Flow\PostgreSql\Tests\Unit\QueryBuilder\Select; +use Flow\PostgreSql\Protobuf\AST\Node; use Flow\PostgreSql\Protobuf\AST\SelectStmt; use Flow\PostgreSql\QueryBuilder\Clause\CTEMaterialization; use Flow\PostgreSql\QueryBuilder\Clause\NullsPosition; @@ -13,10 +14,13 @@ use Flow\PostgreSql\QueryBuilder\Condition\ComparisonOperator; use Flow\PostgreSql\QueryBuilder\Expression\AggregateCall; use Flow\PostgreSql\QueryBuilder\Expression\Column; +use Flow\PostgreSql\QueryBuilder\Expression\ExpressionFactory; use Flow\PostgreSql\QueryBuilder\Expression\Literal; +use Flow\PostgreSql\QueryBuilder\Expression\MergeActionFunction; use Flow\PostgreSql\QueryBuilder\Expression\Star; use Flow\PostgreSql\QueryBuilder\Select\SelectBuilder; use Flow\PostgreSql\QueryBuilder\Table\Table; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use function Flow\PostgreSql\DSL\agg_avg; @@ -1499,4 +1503,128 @@ public function test_from_ast_round_trips_nested_null_test_and_scalar_subquery() )->toSql(), ); } + + #[TestWith([ + 'WITH RECURSIVE t(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM t WHERE n < 3) SEARCH BREADTH FIRST BY n SET ord SELECT * FROM t', + ])] + #[TestWith([ + "WITH RECURSIVE t(id, link) AS (SELECT id, link FROM edges WHERE id = 1 UNION ALL SELECT e.id, e.link FROM edges e JOIN t ON e.id = t.link) CYCLE id SET is_cycle TO 'Y' DEFAULT 'N' USING path SELECT * FROM t", + ])] + #[TestWith([ + 'WITH RECURSIVE t(id, link) AS (SELECT id, link FROM edges WHERE id = 1 UNION ALL SELECT e.id, e.link FROM edges e JOIN t ON e.id = t.link) SEARCH DEPTH FIRST BY id SET ord CYCLE id SET is_cycle TO true DEFAULT false USING path SELECT * FROM t', + ])] + public function test_from_ast_round_trips_cte_search_and_cycle(string $sql): void + { + static::assertSame( + $sql, + SelectBuilder::fromAst( + type_instance_of(SelectStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getSelectStmt(), + ), + )->toSql(), + ); + } + + public function test_from_ast_fills_cycle_mark_defaults(): void + { + static::assertSame( + 'WITH RECURSIVE t(id, link) AS (SELECT id, link FROM edges WHERE id = 1 UNION ALL SELECT e.id, e.link FROM edges e JOIN t ON e.id = t.link) CYCLE id SET is_cycle TO true DEFAULT false USING path SELECT * FROM t', + SelectBuilder::fromAst( + type_instance_of(SelectStmt::class)->assert( + sql_parse( + 'WITH RECURSIVE t(id, link) AS (SELECT id, link FROM edges WHERE id = 1 UNION ALL SELECT e.id, e.link FROM edges e JOIN t ON e.id = t.link) CYCLE id SET is_cycle USING path SELECT * FROM t', + )->raw()->getStmts()[0] + ->getStmt() + ?->getSelectStmt(), + ), + )->toSql(), + ); + } + + #[TestWith(['WITH d AS (DELETE FROM children WHERE parent_id = 1 RETURNING id) SELECT id FROM d'])] + #[TestWith([ + 'WITH u AS (UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING id, parent_id) SELECT id, parent_id FROM u', + ])] + #[TestWith(["WITH i AS (INSERT INTO audit (id, note) VALUES (1, 'x') RETURNING id) SELECT id FROM i"])] + #[TestWith([ + 'WITH m AS (MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING c.id) SELECT id FROM m', + ])] + public function test_from_ast_round_trips_data_modifying_cte(string $sql): void + { + static::assertSame( + $sql, + SelectBuilder::fromAst( + type_instance_of(SelectStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getSelectStmt(), + ), + )->toSql(), + ); + } + + #[TestWith(['SELECT id IN (SELECT id FROM children) AS f FROM parents'])] + #[TestWith(['SELECT CASE WHEN id IN (SELECT parent_id FROM children) THEN 1 ELSE 0 END AS f FROM parents'])] + #[TestWith(['SELECT id = ANY (SELECT id FROM children) AS f FROM parents'])] + #[TestWith(['SELECT EXISTS (SELECT 1 FROM children) AS f FROM parents'])] + public function test_from_ast_round_trips_sublink_in_expression_position(string $sql): void + { + static::assertSame( + $sql, + SelectBuilder::fromAst( + type_instance_of(SelectStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getSelectStmt(), + ), + )->toSql(), + ); + } + + #[TestWith([ + 'SELECT id FROM parents WHERE id IN (SELECT id FROM d)', + 'SELECT id FROM parents WHERE id IN (SELECT id FROM d)', + ])] + #[TestWith([ + 'SELECT id FROM parents WHERE id NOT IN (SELECT id FROM d)', + 'SELECT id FROM parents WHERE NOT id IN (SELECT id FROM d)', + ])] + public function test_from_ast_round_trips_in_subquery(string $sql, string $deparsed): void + { + static::assertSame( + $deparsed, + SelectBuilder::fromAst( + type_instance_of(SelectStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getSelectStmt(), + ), + )->toSql(), + ); + } + + public function test_from_ast_round_trips_merge_cte_with_merge_action(): void + { + $selectStmt = type_instance_of(SelectStmt::class)->assert( + sql_parse( + 'WITH m AS (MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING merge_action(), c.id) SELECT * FROM m', + )->raw()->getStmts()[0] + ->getStmt() + ?->getSelectStmt(), + ); + + static::assertSame( + 'WITH m AS (MERGE INTO children c USING parents p ON c.parent_id = p.id WHEN MATCHED THEN DELETE RETURNING merge_action() , c.id) SELECT * FROM m', + SelectBuilder::fromAst($selectStmt)->toSql(), + ); + static::assertInstanceOf( + MergeActionFunction::class, + ExpressionFactory::fromAst( + type_instance_of(Node::class)->assert( + $selectStmt->getWithClause()?->getCtes()[0] + ?->getCommonTableExpr() + ?->getCtequery() + ?->getMergeStmt() + ?->getReturningClause() + ?->getExprs()[0] + ?->getResTarget() + ?->getVal(), + ), + ), + ); + } } diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Update/UpdateBuilderTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Update/UpdateBuilderTest.php index 24ff5be39f..df0d4f708a 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Update/UpdateBuilderTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/Update/UpdateBuilderTest.php @@ -15,6 +15,7 @@ use Flow\PostgreSql\QueryBuilder\Condition\Comparison; use Flow\PostgreSql\QueryBuilder\Condition\ComparisonOperator; use Flow\PostgreSql\QueryBuilder\Exception\InvalidAstException; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Exception\InvalidExpressionException; use Flow\PostgreSql\QueryBuilder\Expression\BinaryExpression; use Flow\PostgreSql\QueryBuilder\Expression\Column; @@ -23,6 +24,8 @@ use Flow\PostgreSql\QueryBuilder\Select\SelectBuilder; use Flow\PostgreSql\QueryBuilder\Table\Table; use Flow\PostgreSql\QueryBuilder\Update\UpdateBuilder; +use InvalidArgumentException; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\TestCase; use function count; @@ -32,9 +35,11 @@ use function Flow\PostgreSql\DSL\literal; use function Flow\PostgreSql\DSL\param; use function Flow\PostgreSql\DSL\select; +use function Flow\PostgreSql\DSL\sql_parse; use function Flow\PostgreSql\DSL\sub_select; use function Flow\PostgreSql\DSL\table; use function Flow\PostgreSql\DSL\update; +use function Flow\Types\DSL\type_instance_of; use function function_exists; final class UpdateBuilderTest extends TestCase @@ -784,6 +789,84 @@ public function test_update_with_with_clause(): void static::assertCount(1, $ctes); } + #[TestWith([ + "UPDATE parents SET name = 'x' WHERE id IN (SELECT id FROM d)", + "UPDATE parents SET name = 'x' WHERE id IN (SELECT id FROM d)", + ])] + #[TestWith([ + "UPDATE parents SET name = 'x' WHERE id NOT IN (SELECT id FROM d)", + "UPDATE parents SET name = 'x' WHERE NOT id IN (SELECT id FROM d)", + ])] + public function test_from_ast_round_trips_in_subquery(string $sql, string $deparsed): void + { + static::assertSame( + $deparsed, + UpdateBuilder::fromAst( + type_instance_of(UpdateStmt::class)->assert( + sql_parse($sql)->raw()->getStmts()[0]->getStmt()?->getUpdateStmt(), + ), + )->toSql(), + ); + } + + public function test_from_ast_keeps_returning_aliases(): void + { + static::assertSame( + 'UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING WITH (OLD AS o, NEW AS n) o.parent_id, n.parent_id', + UpdateBuilder::fromAst( + type_instance_of(UpdateStmt::class)->assert( + sql_parse( + 'UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING WITH (OLD AS o, NEW AS n) o.parent_id, n.parent_id', + )->raw()->getStmts()[0] + ->getStmt() + ?->getUpdateStmt(), + ), + )->toSql(), + ); + } + + public function test_returning_aliases_to_sql(): void + { + static::assertSame( + 'UPDATE children SET parent_id = 2 WHERE id = 10 RETURNING WITH (OLD AS o, NEW AS n) o.parent_id, n.parent_id', + update() + ->update('children') + ->set('parent_id', literal(2)) + ->where(eq(col('id'), literal(10))) + ->returningAliases(old: 'o', new: 'n') + ->returning(col('parent_id', 'o'), col('parent_id', 'n')) + ->toSql(), + ); + } + + public function test_returning_aliases_without_alias_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('returningAliases() needs an old or a new alias'); + + update()->update('children')->set('parent_id', literal(2))->returningAliases(); + } + + public function test_returning_aliases_without_returning_list_throws(): void + { + $this->expectException(InvalidBuilderStateException::class); + $this->expectExceptionMessage( + 'RETURNING WITH (OLD AS o, NEW AS n) needs a RETURNING list. Call returning() or returningAll() after returningAliases().', + ); + + update()->update('children')->set('parent_id', literal(2))->returningAliases('o', 'n')->toSql(); + } + + public function test_returning_without_expressions_throws(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'returning() needs at least one expression. Call returningAll() for RETURNING *.', + ); + + update()->update('children')->set('parent_id', literal(2))->returning(); + } + private function deparse(UpdateStmt $updateStmt): string { $parser = new Parser(); diff --git a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/With/WithBuilderTest.php b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/With/WithBuilderTest.php index ddb44bfac7..55ca0718fc 100644 --- a/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/With/WithBuilderTest.php +++ b/src/lib/postgresql/tests/Flow/PostgreSql/Tests/Unit/QueryBuilder/With/WithBuilderTest.php @@ -4,6 +4,7 @@ namespace Flow\PostgreSql\Tests\Unit\QueryBuilder\With; +use Flow\PostgreSql\QueryBuilder\Exception\InvalidBuilderStateException; use Flow\PostgreSql\QueryBuilder\Select\SelectFromStep; use PHPUnit\Framework\TestCase; @@ -15,6 +16,16 @@ final class WithBuilderTest extends TestCase { + public function test_merge_on_recursive_with_throws(): void + { + $this->expectException(InvalidBuilderStateException::class); + $this->expectExceptionMessage( + 'MERGE does not accept WITH RECURSIVE, because PostgreSQL rejects it. Remove recursive() from the WITH clause of this MERGE, or use INSERT, UPDATE or DELETE.', + ); + + with(cte('t', select(col('id'))->from(table('s'))))->recursive()->merge('target'); + } + public function test_select_returns_from_step_and_renders_with_clause(): void { $builder = with(cte('t', select(col('id'))->from(table('s'))))->select(col('id'));