Skip to content

Commit 00a3361

Browse files
committed
refactor: wrap UNION query in subquery for correct ORDER BY, LIMIT, and OFFSET application; update test documentation
1 parent e6af179 commit 00a3361

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,17 +1009,21 @@ public function find(array $queries = []): array
10091009
$offsetClause = isset($parsed['offset']) ? ' OFFSET {offset:UInt64}' : '';
10101010

10111011
// Query both tables with UNION ALL
1012+
// Wrap in subquery to ensure ORDER BY, LIMIT, OFFSET apply to the entire UNION result
10121013
$sql = "
1013-
SELECT {$selectColumns}
1014-
FROM {$fromTable}{$whereClause}
1015-
UNION ALL
1016-
SELECT {$selectColumns}
1017-
FROM {$fromCounterTable}{$whereClause}
1018-
{$orderClause}{$limitClause}{$offsetClause}
1014+
SELECT *
1015+
FROM (
1016+
SELECT {$selectColumns}
1017+
FROM {$fromTable}{$whereClause}
1018+
UNION ALL
1019+
SELECT {$selectColumns}
1020+
FROM {$fromCounterTable}{$whereClause}
1021+
){$orderClause}{$limitClause}{$offsetClause}
10191022
FORMAT JSON
10201023
";
10211024

10221025
$result = $this->query($sql, $parsed['params']);
1026+
10231027
return $this->parseResults($result);
10241028
}
10251029

@@ -1352,6 +1356,9 @@ private function parseResults(string $result): array
13521356
if ($key === 'tenant') {
13531357
// Parse tenant
13541358
$document[$key] = $value !== null ? (int) $value : null;
1359+
} elseif ($key === 'value') {
1360+
// Parse value as integer
1361+
$document[$key] = $value !== null ? (int) $value : null;
13551362
} elseif ($key === 'time') {
13561363
// Time comes as string in JSON format, convert to ISO 8601 if needed
13571364
$parsedTime = (string)$value;

tests/Usage/Adapter/ClickHouseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public function testMetricTenantOverridesAdapterTenantInBatch(): void
7979
}
8080

8181
/**
82-
* Test logBatch with explicit batch size parameter
83-
*/
82+
* Test logBatch with explicit batch size parameter
83+
*/
8484
public function testLogBatchWithBatchSize(): void
8585
{
8686
$metrics = [

0 commit comments

Comments
 (0)