fix(unified-logs): wrap facet LIMIT subqueries in parens for UNION ALL compatibility

ClickHouse rejects a bare LIMIT clause in the middle of a UNION ALL chain —
it treats LIMIT as an end-of-statement marker, so the next UNION ALL token
causes a parse error. Wrapping each facet SELECT in parentheses scopes the
LIMIT to that branch and satisfies the parser across all ClickHouse versions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jordi EnricandClaude Sonnet 4.6 committed 2026-05-14 18:27:35 +02:00
1 parent 5a067d0937
commit 7fc07f287b
1 file changed
+3 -5
@@ -187,9 +187,7 @@ const whereClause = (predicates: SafeLogSqlFragment[]): SafeLogSqlFragment =>
/**
* Calculates the chart bucketing level (minute/hour/day) given the date range.
*/
const calculateChartBucketing = (
search: SearchParamsType | Record<string, unknown>
): 'MINUTE' | 'HOUR' | 'DAY' => {
const calculateChartBucketing = (search: SearchParamsType | Record<string, unknown>): 'MINUTE' | 'HOUR' | 'DAY' => {
const dateRange = (search.date as Array<Date | string | number | null | undefined>) || []
const convertToMillis = (timestamp: Date | string | number | null | undefined) => {
@@ -317,11 +315,11 @@ export const getFacetCountQuery = ({
}
return safeSql`
SELECT ${lit(facet)} AS dimension, (${facetExpr}) AS value, count() AS count
(SELECT ${lit(facet)} AS dimension, (${facetExpr}) AS value, count() AS count
FROM logs
${whereClause(predicates)}
GROUP BY value
LIMIT ${lit(MAX_FACETS_QUANTITY)}
LIMIT ${lit(MAX_FACETS_QUANTITY)})
`
}