From 53687c69785c91a5942c4034f6b49d18fc005934 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Fri, 20 Nov 2026 10:53:52 -0600 Subject: [PATCH] fix: keyset pagination builds a key object for every row - doctrine and postgresql adapters compare raw key values per row - KeyValues built only for head, cursor, lookahead and errors - postgresql key names resolved once per page - streaming guide numbers updated Closes #2748 --- .../IO/Extractor/Pagination/KeySetPage.php | 46 ++++++++--- .../Extractor/Pagination/KeySetPageTest.php | 79 +++++++++++++++++++ .../IO/Extractor/Pagination/KeySetPage.php | 46 ++++++++--- .../Extractor/Pagination/KeySetPageTest.php | 66 ++++++++++++++++ .../stream-large-csv-download-in-symfony.md | 17 ++-- 5 files changed, 222 insertions(+), 32 deletions(-) diff --git a/src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/IO/Extractor/Pagination/KeySetPage.php b/src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/IO/Extractor/Pagination/KeySetPage.php index a465a7224..d3c0d654b 100644 --- a/src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/IO/Extractor/Pagination/KeySetPage.php +++ b/src/adapter/etl-adapter-doctrine/src/Flow/ETL/Adapter/Doctrine/IO/Extractor/Pagination/KeySetPage.php @@ -6,9 +6,10 @@ use Flow\ETL\Foundation\Exception\RuntimeException; -use function array_map; +use function array_key_exists; use function array_slice; use function count; +use function is_scalar; use function sprintf; final readonly class KeySetPage @@ -30,9 +31,28 @@ private function __construct( */ public static function of(KeySet $keySet, array $aliases, array $fetched, int $size, ?KeyValues $expectedHead): self { - $keys = array_map(static fn(array $row): KeyValues => KeyValues::of($keySet, $aliases, $row), $fetched); + $tieIndex = null; - if ($expectedHead !== null && ($keys === [] || !$keys[0]->equals($expectedHead))) { + foreach ($fetched as $index => $row) { + $tie = $index > 0; + + foreach ($aliases as $alias) { + if (!array_key_exists($alias, $row) || !is_scalar($row[$alias])) { + KeyValues::of($keySet, $aliases, $row); + } + + $tie = $tie && $row[$alias] === $fetched[$index - 1][$alias]; + } + + if ($tie && $tieIndex === null) { + $tieIndex = $index; + } + } + + if ( + $expectedHead !== null + && ($fetched === [] || !KeyValues::of($keySet, $aliases, $fetched[0])->equals($expectedHead)) + ) { throw new RuntimeException(sprintf( 'Keyset pagination expected the next page to start at the key %s, but it did not. Either the database ' . 'compares two different key values as equal (a case- or accent-insensitive collation, numeric scale ' @@ -42,19 +62,21 @@ public static function of(KeySet $keySet, array $aliases, array $fetched, int $s )); } - for ($index = 1, $count = count($keys); $index < $count; $index++) { - if ($keys[$index]->equals($keys[$index - 1])) { - throw new RuntimeException(sprintf( - 'Keyset pagination requires unique keys, but two rows share the key %s; add a unique column (for ' - . 'example the primary key) to the key set as its least significant key', - $keys[$index]->describe(), - )); - } + if ($tieIndex !== null) { + throw new RuntimeException(sprintf( + 'Keyset pagination requires unique keys, but two rows share the key %s; add a unique column (for ' + . 'example the primary key) to the key set as its least significant key', + KeyValues::of($keySet, $aliases, $fetched[$tieIndex])->describe(), + )); } return count($fetched) <= $size ? new self($fetched, null, null) - : new self(array_slice($fetched, 0, $size), $keys[$size - 1], $keys[$size]); + : new self( + array_slice($fetched, 0, $size), + KeyValues::of($keySet, $aliases, $fetched[$size - 1]), + KeyValues::of($keySet, $aliases, $fetched[$size]), + ); } public function isLast(): bool diff --git a/src/adapter/etl-adapter-doctrine/tests/Flow/ETL/Adapter/Doctrine/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php b/src/adapter/etl-adapter-doctrine/tests/Flow/ETL/Adapter/Doctrine/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php index 3ad65e111..3b4d87886 100644 --- a/src/adapter/etl-adapter-doctrine/tests/Flow/ETL/Adapter/Doctrine/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php +++ b/src/adapter/etl-adapter-doctrine/tests/Flow/ETL/Adapter/Doctrine/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php @@ -110,4 +110,83 @@ public function test_an_empty_page_after_a_lookahead_is_rejected(): void KeySetPage::of($keySet, ['key_id'], [], 2, KeyValues::of($keySet, ['key_id'], ['key_id' => 3])); } + + public function test_a_composite_key_differing_only_in_the_last_key_is_accepted(): void + { + $keySet = pagination_key_set(pagination_key_asc('a'), pagination_key_asc('b')); + $page = KeySetPage::of( + $keySet, + ['key_a', 'key_b'], + [['key_a' => 1, 'key_b' => 1], ['key_a' => 1, 'key_b' => 2], ['key_a' => 1, 'key_b' => 3]], + 2, + null, + ); + + static::assertSame([['key_a' => 1, 'key_b' => 1], ['key_a' => 1, 'key_b' => 2]], $page->rows); + static::assertEquals(KeyValues::of($keySet, ['key_a', 'key_b'], ['key_a' => 1, 'key_b' => 2]), $page->cursor); + static::assertEquals( + KeyValues::of($keySet, ['key_a', 'key_b'], ['key_a' => 1, 'key_b' => 3]), + $page->lookahead, + ); + } + + public function test_a_missing_key_column_in_a_middle_row_is_rejected(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Column "id" not found in result row for keyset pagination'); + + KeySetPage::of( + pagination_key_set(pagination_key_asc('id')), + ['key_id'], + [['key_id' => 1], ['other' => 2], ['key_id' => 3]], + 2, + null, + ); + } + + public function test_a_null_key_in_a_middle_row_is_rejected(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage( + 'NULL value found in column "id" for keyset pagination; key columns must be non-null', + ); + + KeySetPage::of( + pagination_key_set(pagination_key_asc('id')), + ['key_id'], + [['key_id' => 1], ['key_id' => null], ['key_id' => 3]], + 2, + null, + ); + } + + public function test_an_invalid_key_after_a_tie_is_rejected_before_the_tie(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage( + 'NULL value found in column "id" for keyset pagination; key columns must be non-null', + ); + + KeySetPage::of( + pagination_key_set(pagination_key_asc('id')), + ['key_id'], + [['key_id' => 1], ['key_id' => 1], ['key_id' => null]], + 2, + null, + ); + } + + public function test_an_unsupported_key_type_in_a_middle_row_is_rejected(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Unsupported value type "array" in column "id" for keyset pagination'); + + KeySetPage::of( + pagination_key_set(pagination_key_asc('id')), + ['key_id'], + [['key_id' => 1], ['key_id' => [2]], ['key_id' => 3]], + 2, + null, + ); + } } diff --git a/src/adapter/etl-adapter-postgresql/src/Flow/ETL/Adapter/PostgreSql/IO/Extractor/Pagination/KeySetPage.php b/src/adapter/etl-adapter-postgresql/src/Flow/ETL/Adapter/PostgreSql/IO/Extractor/Pagination/KeySetPage.php index 3463e6101..acbd97ad9 100644 --- a/src/adapter/etl-adapter-postgresql/src/Flow/ETL/Adapter/PostgreSql/IO/Extractor/Pagination/KeySetPage.php +++ b/src/adapter/etl-adapter-postgresql/src/Flow/ETL/Adapter/PostgreSql/IO/Extractor/Pagination/KeySetPage.php @@ -6,9 +6,11 @@ use Flow\ETL\Foundation\Exception\RuntimeException; +use function array_key_exists; use function array_map; use function array_slice; use function count; +use function is_scalar; use function sprintf; final readonly class KeySetPage @@ -29,9 +31,29 @@ private function __construct( */ public static function of(KeySet $keySet, array $fetched, int $size, ?KeyValues $expectedHead): self { - $keys = array_map(static fn(array $row): KeyValues => KeyValues::of($keySet, $row), $fetched); + $names = array_map(static fn(Key $key): string => $key->name(), $keySet->keys); + $tieIndex = null; - if ($expectedHead !== null && ($keys === [] || !$keys[0]->equals($expectedHead))) { + foreach ($fetched as $index => $row) { + $tie = $index > 0; + + foreach ($names as $name) { + if (!array_key_exists($name, $row) || !is_scalar($row[$name])) { + KeyValues::of($keySet, $row); + } + + $tie = $tie && $row[$name] === $fetched[$index - 1][$name]; + } + + if ($tie && $tieIndex === null) { + $tieIndex = $index; + } + } + + if ( + $expectedHead !== null + && ($fetched === [] || !KeyValues::of($keySet, $fetched[0])->equals($expectedHead)) + ) { throw new RuntimeException(sprintf( 'Keyset pagination expected the next page to start at the key %s, but it did not. Either the database ' . 'compares two different key values as equal (a case- or accent-insensitive collation, numeric scale ' @@ -41,19 +63,21 @@ public static function of(KeySet $keySet, array $fetched, int $size, ?KeyValues )); } - for ($index = 1, $count = count($keys); $index < $count; $index++) { - if ($keys[$index]->equals($keys[$index - 1])) { - throw new RuntimeException(sprintf( - 'Keyset pagination requires unique keys, but two rows share the key %s; add a unique column (for ' - . 'example the primary key) to the key set as its least significant key', - $keys[$index]->describe(), - )); - } + if ($tieIndex !== null) { + throw new RuntimeException(sprintf( + 'Keyset pagination requires unique keys, but two rows share the key %s; add a unique column (for ' + . 'example the primary key) to the key set as its least significant key', + KeyValues::of($keySet, $fetched[$tieIndex])->describe(), + )); } return count($fetched) <= $size ? new self($fetched, null, null) - : new self(array_slice($fetched, 0, $size), $keys[$size - 1], $keys[$size]); + : new self( + array_slice($fetched, 0, $size), + KeyValues::of($keySet, $fetched[$size - 1]), + KeyValues::of($keySet, $fetched[$size]), + ); } public function isLast(): bool diff --git a/src/adapter/etl-adapter-postgresql/tests/Flow/ETL/Adapter/PostgreSql/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php b/src/adapter/etl-adapter-postgresql/tests/Flow/ETL/Adapter/PostgreSql/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php index edc447836..412e236f5 100644 --- a/src/adapter/etl-adapter-postgresql/tests/Flow/ETL/Adapter/PostgreSql/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php +++ b/src/adapter/etl-adapter-postgresql/tests/Flow/ETL/Adapter/PostgreSql/Tests/Unit/IO/Extractor/Pagination/KeySetPageTest.php @@ -106,4 +106,70 @@ public function test_an_empty_page_after_a_lookahead_is_rejected(): void KeySetPage::of($keySet, [], 2, KeyValues::of($keySet, ['id' => 3])); } + + public function test_a_composite_key_differing_only_in_the_last_key_is_accepted(): void + { + $keySet = pgsql_pagination_key_set(pgsql_pagination_key_asc('a'), pgsql_pagination_key_asc('b')); + $page = KeySetPage::of($keySet, [['a' => 1, 'b' => 1], ['a' => 1, 'b' => 2], ['a' => 1, 'b' => 3]], 2, null); + + static::assertSame([['a' => 1, 'b' => 1], ['a' => 1, 'b' => 2]], $page->rows); + static::assertEquals(KeyValues::of($keySet, ['a' => 1, 'b' => 2]), $page->cursor); + static::assertEquals(KeyValues::of($keySet, ['a' => 1, 'b' => 3]), $page->lookahead); + } + + public function test_a_missing_key_column_in_a_middle_row_is_rejected(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Column "id" not found in result row for keyset pagination'); + + KeySetPage::of( + pgsql_pagination_key_set(pgsql_pagination_key_asc('id')), + [['id' => 1], ['other' => 2], ['id' => 3]], + 2, + null, + ); + } + + public function test_a_null_key_in_a_middle_row_is_rejected(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage( + 'NULL value found in column "id" for keyset pagination; key columns must be non-null', + ); + + KeySetPage::of( + pgsql_pagination_key_set(pgsql_pagination_key_asc('id')), + [['id' => 1], ['id' => null], ['id' => 3]], + 2, + null, + ); + } + + public function test_an_invalid_key_after_a_tie_is_rejected_before_the_tie(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage( + 'NULL value found in column "id" for keyset pagination; key columns must be non-null', + ); + + KeySetPage::of( + pgsql_pagination_key_set(pgsql_pagination_key_asc('id')), + [['id' => 1], ['id' => 1], ['id' => null]], + 2, + null, + ); + } + + public function test_an_unsupported_key_type_in_a_middle_row_is_rejected(): void + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Unsupported value type "array" in column "id" for keyset pagination'); + + KeySetPage::of( + pgsql_pagination_key_set(pgsql_pagination_key_asc('id')), + [['id' => 1], ['id' => [2]], ['id' => 3]], + 2, + null, + ); + } } diff --git a/web/landing/content/guides/stream-large-csv-download-in-symfony.md b/web/landing/content/guides/stream-large-csv-download-in-symfony.md index d30fcfd82..43d639af3 100644 --- a/web/landing/content/guides/stream-large-csv-download-in-symfony.md +++ b/web/landing/content/guides/stream-large-csv-download-in-symfony.md @@ -7,7 +7,7 @@ proof_label: 'A million orders as a download' proof_before: '91Mb' proof_after: '20Mb' published: '2026-11-18' -updated: '2026-11-18' +updated: '2026-11-20' --- # Stream a large CSV download in Symfony without running out of memory @@ -258,15 +258,14 @@ PHP 8.3, `memory_limit=128M`, a million orders in MySQL. Every controller masks | Format | `StreamedResponse` by hand | Flow | Flow with the `flow_php` extension | |---------|----------------------------|-------------|------------------------------------| -| CSV | 91Mb, 3.7s | 20Mb, 6.5s | 20Mb, 4.5s | -| JSON | 91Mb, 4.5s | 20Mb, 6.7s | 20Mb, 4.2s | -| XML | 91Mb, 4.9s | 20Mb, 7.0s | 20Mb, 5.3s | -| Parquet | - | 42Mb, 10.2s | 20Mb, 4.4s | +| CSV | 91Mb, 3.2s | 20Mb, 5.8s | 18Mb, 3.5s | +| JSON | 91Mb, 4.1s | 20Mb, 5.9s | 18Mb, 3.5s | +| XML | 91Mb, 4.5s | 20Mb, 5.9s | 20Mb, 4.5s | +| Parquet | - | 42Mb, 8.6s | 20Mb, 3.5s | -With the extension Flow is as fast as the controllers written by hand for JSON, and close to them for CSV and XML. Without it they are faster. +With the extension Flow is faster than the controller written by hand for JSON, as fast for XML, and close to it for CSV. Without it they are faster. But they are three different pieces of code, and their memory grows with the table. -The Flow controller is one piece of code with one line changed, and with half of the orders or with all of them it takes the same 20Mb. - -> Flow is slower here than it should be. The keyset reader does more work for every row than it needs to, and there is an open issue for it: [flow-php/flow#2748](https://github.com/flow-php/flow/issues/2748). +The Flow controller is one piece of code with one line changed. +With half of the orders or with all of them it never takes more than 20Mb. [flow_php](/documentation/components/extensions/flow-php-ext) is an optional PHP extension. The controller stays the same, without a single change.