Skip to content

Commit b8f18e9

Browse files
committed
Fix: Large table causes sql error
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent da25bd7 commit b8f18e9

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

lib/Db/Row2Mapper.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use Throwable;
2929

3030
class Row2Mapper {
31-
private const ROWS_LOADING_CHUNK_SIZE = 5000;
31+
private const MAX_DB_PARAMETERS = 65535;
3232

3333
use TTransactional;
3434

@@ -195,16 +195,37 @@ public function findAll(array $showColumnIds, int $tableId, ?int $limit = null,
195195
* @throws InternalError
196196
*/
197197
private function getRows(array $rowIds, array $columnIds): array {
198-
if (count($rowIds) > self::ROWS_LOADING_CHUNK_SIZE) {
199-
$chunks = [];
200-
do {
201-
$chunkRowIds = array_splice($rowIds, 0, self::ROWS_LOADING_CHUNK_SIZE);
202-
$chunks[] = $this->getRows($chunkRowIds, $columnIds);
203-
} while (count($rowIds) > self::ROWS_LOADING_CHUNK_SIZE);
198+
if (empty($rowIds)) {
199+
return [];
200+
}
201+
202+
$columnTypesCount = count($this->columnsHelper->columns);
203+
if ($columnTypesCount === 0) {
204+
return [];
205+
}
204206

205-
return array_merge(...$chunks);
207+
$columnsCount = count($columnIds);
208+
$maxParamsPerType = floor(self::MAX_DB_PARAMETERS / $columnTypesCount) ;
209+
$calculatedChunkSize = (int)($maxParamsPerType - $columnsCount);
210+
$chunkSize = max(1, $calculatedChunkSize);
211+
212+
$rowIdChunks = array_chunk($rowIds, $chunkSize);
213+
$chunks = [];
214+
foreach ($rowIdChunks as $chunkedRowIds) {
215+
$chunks[] = $this->getRowsChunk($chunkedRowIds, $columnIds);
206216
}
207217

218+
return array_merge(...$chunks);
219+
}
220+
221+
/**
222+
* Builds and executes the UNION ALL query for a specific chunk of rows.
223+
* @param array $rowIds
224+
* @param array $columnIds
225+
* @return Row2[]
226+
* @throws InternalError
227+
*/
228+
private function getRowsChunk(array $rowIds, array $columnIds): array {
208229
$qb = $this->db->getQueryBuilder();
209230

210231
$qbSqlForColumnTypes = null;

0 commit comments

Comments
 (0)