Skip to content

Commit dc0aba8

Browse files
committed
Improve performance of mixed table aggregation (old hierarchy tables + new flat tables)
1 parent 748f22b commit dc0aba8

3 files changed

Lines changed: 641 additions & 27 deletions

File tree

core/ArchiveProcessor/Record.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public function getMultiplePeriodTransform(): ?callable
266266
* aggregates into the flat table when some periods do not
267267
* have the flat record yet.
268268
* Signature: function (DataTable $legacyHierarchy, DataTable $flatTable, ArchiveProcessor $archiveProcessor, Record $hierarchicalRecord): void
269+
* The callback is invoked once per legacy source period hierarchy table.
269270
*/
270271
public function setBuiltFromFlatRecord(
271272
string $flatRecordName,

core/ArchiveProcessor/RecordBuilder.php

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,18 +355,16 @@ protected function aggregateBuiltFromFlatRecordForNonDay(
355355
$hasLegacyFallbackData = false;
356356
$legacyReducerCallback = $hierarchicalRecord->getLegacyHierarchyToFlatReducerCallback();
357357
if (!empty($periodsWithoutFlatRecord) && is_callable($legacyReducerCallback)) {
358-
[$legacyHierarchicalTable, $hasLegacyFallbackData] = $this->aggregateDataTableFromBlobs(
358+
$hasLegacyFallbackData = $this->aggregateLegacyHierarchyPeriodsIntoFlatTable(
359359
$archiveProcessor,
360360
$hierarchicalRecord->getName(),
361+
$flatTable,
362+
$legacyReducerCallback,
363+
$hierarchicalRecord,
361364
$columnAggregationOps,
362365
$columnToRenameAfterAggregation,
363366
$periodsWithoutFlatRecord
364367
);
365-
366-
if ($hasLegacyFallbackData) {
367-
call_user_func($legacyReducerCallback, $legacyHierarchicalTable, $flatTable, $archiveProcessor, $hierarchicalRecord);
368-
}
369-
Common::destroy($legacyHierarchicalTable);
370368
}
371369

372370
if (!$hasFlatSourceData && !$hasLegacyFallbackData) {
@@ -405,6 +403,88 @@ function (Row $flatRow) use ($flatToHierarchyPathCallback, $archiveProcessor, $h
405403
return true;
406404
}
407405

406+
protected function aggregateLegacyHierarchyPeriodsIntoFlatTable(
407+
ArchiveProcessor $archiveProcessor,
408+
string $recordName,
409+
DataTable $flatTable,
410+
callable $legacyReducerCallback,
411+
Record $hierarchicalRecord,
412+
?array $columnsAggregationOperation,
413+
?array $columnsToRenameAfterAggregation,
414+
?array $periodsToInclude
415+
): bool {
416+
$currentPeriod = null;
417+
$currentPeriodRows = [];
418+
$hasRows = false;
419+
420+
foreach ($this->querySingleBlobRows($archiveProcessor, $recordName) as $archiveDataRow) {
421+
$period = $archiveDataRow['date1'] . ',' . $archiveDataRow['date2'];
422+
if ($periodsToInclude !== null && !isset($periodsToInclude[$period])) {
423+
continue;
424+
}
425+
426+
if ($currentPeriod !== null && $period !== $currentPeriod) {
427+
$hasRows = $this->reduceLegacyHierarchyPeriodRowsIntoFlatTable(
428+
$currentPeriodRows,
429+
$recordName,
430+
$flatTable,
431+
$legacyReducerCallback,
432+
$archiveProcessor,
433+
$hierarchicalRecord,
434+
$columnsAggregationOperation,
435+
$columnsToRenameAfterAggregation
436+
) || $hasRows;
437+
$currentPeriodRows = [];
438+
}
439+
440+
$currentPeriod = $period;
441+
$currentPeriodRows[] = $archiveDataRow;
442+
}
443+
444+
if (!empty($currentPeriodRows)) {
445+
$hasRows = $this->reduceLegacyHierarchyPeriodRowsIntoFlatTable(
446+
$currentPeriodRows,
447+
$recordName,
448+
$flatTable,
449+
$legacyReducerCallback,
450+
$archiveProcessor,
451+
$hierarchicalRecord,
452+
$columnsAggregationOperation,
453+
$columnsToRenameAfterAggregation
454+
) || $hasRows;
455+
}
456+
457+
return $hasRows;
458+
}
459+
460+
protected function reduceLegacyHierarchyPeriodRowsIntoFlatTable(
461+
array $periodRows,
462+
string $recordName,
463+
DataTable $flatTable,
464+
callable $legacyReducerCallback,
465+
ArchiveProcessor $archiveProcessor,
466+
Record $hierarchicalRecord,
467+
?array $columnsAggregationOperation,
468+
?array $columnsToRenameAfterAggregation
469+
): bool {
470+
[$legacyHierarchicalTable, $hasRows] = BlobTableAggregator::aggregateBlobRows(
471+
$periodRows,
472+
$recordName,
473+
$columnsAggregationOperation,
474+
function (DataTable $table) use ($archiveProcessor, $columnsToRenameAfterAggregation): void {
475+
$archiveProcessor->renameColumnsAfterAggregation($table, $columnsToRenameAfterAggregation);
476+
}
477+
);
478+
479+
if ($hasRows) {
480+
call_user_func($legacyReducerCallback, $legacyHierarchicalTable, $flatTable, $archiveProcessor, $hierarchicalRecord);
481+
}
482+
483+
Common::destroy($legacyHierarchicalTable);
484+
485+
return $hasRows;
486+
}
487+
408488
/**
409489
* Hook executed after the hierarchy table has been rebuilt from the flat table and before
410490
* the hierarchical blob record is serialized and inserted.

0 commit comments

Comments
 (0)