Skip to content

Commit 0ddb029

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

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

lib/Db/Row2Mapper.php

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

3030
class Row2Mapper {
31+
private const MAX_DB_PARAMETERS = 65535;
32+
3133
use TTransactional;
3234

3335
private RowSleeveMapper $rowSleeveMapper;
@@ -193,6 +195,37 @@ public function findAll(array $showColumnIds, int $tableId, ?int $limit = null,
193195
* @throws InternalError
194196
*/
195197
private function getRows(array $rowIds, array $columnIds): array {
198+
if (empty($rowIds)) {
199+
return [];
200+
}
201+
202+
$columnTypesCount = count($this->columnsHelper->columns);
203+
if ($columnTypesCount === 0) {
204+
return [];
205+
}
206+
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);
216+
}
217+
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 {
196229
$qb = $this->db->getQueryBuilder();
197230

198231
$qbSqlForColumnTypes = null;

0 commit comments

Comments
 (0)