Skip to content

Commit 671ffe7

Browse files
committed
fix: MV column not under aggregate - use subquery alias for toStartOfDay
1 parent 64833d6 commit 671ffe7

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -973,20 +973,21 @@ public function setup(): void
973973
$escapedDailyMv = $this->escapeIdentifier($this->database) . '.' . $this->escapeIdentifier($dailyMvName);
974974

975975
$tenantSelect = $this->sharedTables ? 'tenant' : "'' as tenant";
976-
$groupByClause = $this->sharedTables ? 'metric, tenant' : 'metric';
976+
$groupByClause = $this->sharedTables ? 'metric, tenant, day' : 'metric, day';
977977

978978
$dailySelect = $this->sharedTables
979-
? "generateUUIDv4() as id, metric, {$tenantSelect}, sum(value) as value, 'event' as type, toStartOfDay(time) as time, '{}' as tags"
980-
: "generateUUIDv4() as id, metric, sum(value) as value, 'event' as type, toStartOfDay(time) as time, '{}' as tags";
979+
? "generateUUIDv4() as id, metric, {$tenantSelect}, sum(value) as value, 'event' as type, day as time, '{}' as tags"
980+
: "generateUUIDv4() as id, metric, sum(value) as value, 'event' as type, day as time, '{}' as tags";
981981

982982
$createDailyMvSql = "
983983
CREATE MATERIALIZED VIEW IF NOT EXISTS {$escapedDailyMv}
984984
TO {$escapedDailyTable}
985985
AS SELECT
986+
toStartOfDay(time) as day,
986987
{$dailySelect}
987988
FROM {$escapedDatabaseAndTable}
988989
WHERE type = 'event'
989-
GROUP BY {$groupByClause}, toStartOfDay(time)
990+
GROUP BY {$groupByClause}
990991
";
991992

992993
$this->query($createDailyMvSql);

0 commit comments

Comments
 (0)