@@ -349,6 +349,20 @@ private function getCounterTableName(): string
349349 return $ tableName ;
350350 }
351351
352+ /**
353+ * Build a fully qualified table reference with database, escaping, and optional FINAL clause.
354+ *
355+ * @param string $tableName The table name (with namespace already applied)
356+ * @param bool $useFinal Whether to append FINAL clause (defaults to adapter's useFinal setting)
357+ * @return string Fully qualified table reference
358+ */
359+ private function buildTableReference (string $ tableName , ?bool $ useFinal = null ): string
360+ {
361+ $ useFinal = $ useFinal ?? $ this ->useFinal ;
362+ $ escapedTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ tableName );
363+ return $ escapedTable . ($ useFinal ? ' FINAL ' : '' );
364+ }
365+
352366 /**
353367 * Log a query execution for debugging purposes.
354368 *
@@ -1064,13 +1078,9 @@ private function resolveTenantFromMetric(array $metricData): ?int
10641078 */
10651079 public function find (array $ queries = []): array
10661080 {
1067- $ tableName = $ this ->getTableName ();
1068- $ counterTableName = $ this ->getCounterTableName ();
1069- $ escapedTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ tableName );
1070- $ escapedCounterTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ counterTableName );
1071- // FINAL on both tables (SummingMergeTree and ReplacingMergeTree)
1072- $ fromTable = $ escapedTable . ($ this ->useFinal ? ' FINAL ' : '' );
1073- $ fromCounterTable = $ escapedCounterTable . ($ this ->useFinal ? ' FINAL ' : '' );
1081+ // Get table references with FINAL clause
1082+ $ fromTable = $ this ->buildTableReference ($ this ->getTableName ());
1083+ $ fromCounterTable = $ this ->buildTableReference ($ this ->getCounterTableName ());
10741084
10751085 // Parse queries
10761086 $ parsed = $ this ->parseQueries ($ queries );
@@ -1122,13 +1132,9 @@ public function find(array $queries = []): array
11221132 */
11231133 public function count (array $ queries = []): int
11241134 {
1125- $ tableName = $ this ->getTableName ();
1126- $ counterTableName = $ this ->getCounterTableName ();
1127- $ escapedTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ tableName );
1128- $ escapedCounterTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ counterTableName );
1129- // FINAL on both tables (SummingMergeTree and ReplacingMergeTree)
1130- $ fromTable = $ escapedTable . ($ this ->useFinal ? ' FINAL ' : '' );
1131- $ fromCounterTable = $ escapedCounterTable . ($ this ->useFinal ? ' FINAL ' : '' );
1135+ // Get table references with FINAL clause
1136+ $ fromTable = $ this ->buildTableReference ($ this ->getTableName ());
1137+ $ fromCounterTable = $ this ->buildTableReference ($ this ->getCounterTableName ());
11321138
11331139 // Parse queries - we only need filters and params
11341140 $ parsed = $ this ->parseQueries ($ queries );
@@ -1604,13 +1610,9 @@ public function getBetweenDates(string $metric, string $startDate, string $endDa
16041610 */
16051611 public function sum (array $ queries = [], string $ attribute = 'value ' ): int
16061612 {
1607- $ tableName = $ this ->getTableName ();
1608- $ counterTableName = $ this ->getCounterTableName ();
1609- $ escapedTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ tableName );
1610- $ escapedCounterTable = $ this ->escapeIdentifier ($ this ->database ) . '. ' . $ this ->escapeIdentifier ($ counterTableName );
1611- // FINAL on both tables (SummingMergeTree and ReplacingMergeTree)
1612- $ fromTable = $ escapedTable . ($ this ->useFinal ? ' FINAL ' : '' );
1613- $ fromCounterTable = $ escapedCounterTable . ($ this ->useFinal ? ' FINAL ' : '' );
1613+ // Get table references with FINAL clause
1614+ $ fromTable = $ this ->buildTableReference ($ this ->getTableName ());
1615+ $ fromCounterTable = $ this ->buildTableReference ($ this ->getCounterTableName ());
16141616
16151617 // Validate attribute name
16161618 $ this ->validateAttributeName ($ attribute );
0 commit comments