Repository navigation
Commit ea12707
authored
fix: apply Map KV text-index rewrite to SQL where and aggCondition (#2948)
Fixes #2927
## Why
The [Map direct read optimization](https://clickhouse.com/docs/clickstack/managing/performance-tuning#map-direct-read-optimization) docs promise the `Map['k'] = 'v'` → `has(ItemsCol, concat('k', '=', 'v'))` rewrite happens automatically for anything referencing `LogAttributes['key']` — saved queries, dashboards, and alerts included. In practice the rewrite was only wired to `filters[]` entries with `type: 'sql'` (#2405). Two SQL entry paths into the WHERE clause skipped it entirely:
1. **Top-level `chartConfig.where` with `whereLanguage: 'sql'`** — the search box, saved searches, and alerts built from them (`buildSearchChartConfig` maps search input to `where`/`whereLanguage`). This is the path measured in #2927 reading ~15× more rows than the equivalent Lucene query.
2. **`select[].aggCondition` with `aggConditionLanguage: 'sql'`** — the copy that `renderWhere` adds to the WHERE clause "to utilize index" was rendered verbatim, so it never actually utilized the text index.
`textIndexInfoLookup` was also only built when a `sql`-type filter was present, and was declared after the `where` block, so it couldn't have been used there anyway.
## What changed
In `renderWhere` (`packages/common-utils/src/core/renderChartConfig.ts`):
- Hoisted `textIndexInfoLookup` construction to the top of the function and widened its gate (`hasSqlFilter` → `hasSqlPredicate`) to cover: non-empty SQL `where`, `sql`-type filters, and SQL aggConditions on the aggCondition-to-WHERE path. Existing guards (database/table set, no subquery CTE) are unchanged, and the underlying metadata calls are cached, so this adds no meaningful per-query cost.
- Applied `rewriteSqlFilterWithKvItems` to `chartConfig.where` when the language resolves to `sql`.
- Applied it to the **WHERE-clause copy** of SQL aggConditions. The `aggFnIf(...)` copy in the SELECT clause is deliberately **not** rewritten: index-based granule pruning only happens in WHERE, and inside the aggregate the predicate is evaluated per-row, where `has(<ALIAS items col>, ...)` would recompute the whole `arrayMap` per row and be slower than a plain Map subscript. A code comment captures this so it isn't "fixed" later.
- The existing `filters[]` path is unchanged.
Version gating is inherited: the lookup is built by the same `buildTextIndexInfoLookup` → `supportsDirectReadMap` machinery, so pre-backport servers and ALIAS-vs-MATERIALIZED items columns behave exactly as documented.
**Known limitation (out of scope):** `sql_ast` filters are still rendered raw. The app only uses them for plain system-table columns, never Map subscripts, so there's no user-facing gap today.
## Testing
Added 5 tests to the existing `SQL filter KV items direct_read optimization` block in `renderChartConfig.test.ts`:
- SQL `where` `=` → `has(...)`, raw subscript gone
- SQL `where` multi-value `IN` → `hasAny(...)` on 26.5+
- SQL `where` unchanged when no KV items column exists
- SQL `where` unchanged on a pre-backport server with an ALIAS items column (version-gate fallback through the new path)
- SQL aggCondition: WHERE copy rewritten to `has(...)`, `countIf(...)` copy retains the raw Map subscript
Ran `make ci-lint` and `make ci-unit` — all green (common-utils 2053, app 709, api 383, cli type check).
## Relation to #2929
#2929 covers the `where` half of this with the same approach. This PR additionally covers the `aggCondition` gap, adds the version-gate and negative-case regression tests, and documents why the SELECT-clause aggCondition copy is intentionally excluded.1 parent 7e5ea5e commit ea12707
3 files changed
Lines changed: 197 additions & 23 deletions
File tree
- .changeset
- packages/common-utils/src
- __tests__
- core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1489 | 1489 | | |
1490 | 1490 | | |
1491 | 1491 | | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
| 1588 | + | |
| 1589 | + | |
| 1590 | + | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
| 1595 | + | |
| 1596 | + | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
1492 | 1625 | | |
1493 | 1626 | | |
1494 | 1627 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1167 | 1167 | | |
1168 | 1168 | | |
1169 | 1169 | | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
1170 | 1207 | | |
1171 | 1208 | | |
1172 | 1209 | | |
1173 | 1210 | | |
1174 | | - | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
1175 | 1218 | | |
1176 | 1219 | | |
1177 | 1220 | | |
| |||
1188 | 1231 | | |
1189 | 1232 | | |
1190 | 1233 | | |
1191 | | - | |
1192 | | - | |
1193 | | - | |
1194 | | - | |
1195 | | - | |
1196 | | - | |
| 1234 | + | |
1197 | 1235 | | |
1198 | 1236 | | |
1199 | 1237 | | |
1200 | 1238 | | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
1201 | 1246 | | |
1202 | | - | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
1203 | 1254 | | |
1204 | 1255 | | |
1205 | 1256 | | |
| |||
1217 | 1268 | | |
1218 | 1269 | | |
1219 | 1270 | | |
1220 | | - | |
1221 | | - | |
1222 | | - | |
1223 | | - | |
1224 | | - | |
1225 | | - | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
1231 | | - | |
1232 | | - | |
1233 | | - | |
1234 | | - | |
1235 | 1271 | | |
1236 | 1272 | | |
1237 | 1273 | | |
| |||
0 commit comments