Skip to content

fix(common-utils): apply Map KV items rewrite to toString()-wrapped filter keys - #3826

Merged
karl-power merged 2 commits into
mainfrom
claude/adhoc-filter-kv-rewrite
Nov 25, 2026
Merged

karl-power merged 2 commits into
mainfrom
claude/adhoc-filter-kv-rewrite

Conversation

@karl-power

Copy link
Copy Markdown
Contributor

Summary

Ad hoc and dashboard dropdown filters on a Map key now use the KV items text index, like the same condition written in Lucene or in the SQL where.

  • rewriteSqlFilterWithKvItems now unwraps a single-argument toString(...) on the left of = / IN before matching Map['k']. Ad hoc and dropdown filters wrap every key in toString(), so the rewrite was skipped and ClickHouse decoded the whole Map for every row.
  • Dropping toString() does not change results: the rewrite only applies to maps with a registered KV items column, whose values are already strings.
  • Other operators, other functions, nested or multi-argument toString, and differently cased names are left unchanged.
  • On a customer dashboard, the same tile took 28.8 s and read 6.1 GiB with an ad hoc filter, and 4.3 s and 34 MiB with the equivalent Lucene filter.
  • $__filters in raw SQL tiles still skips the rewrite; that is a separate follow-up.

How to reproduce

Starting point: yarn dev, registered and logged in. The seeded otel_logs table has the LogAttributeItems column and idx_log_attr_items text index, and the dev app and API log their own activity to it, with keys such as LogAttributes['teamName'].

On main:

  1. Create a dashboard with a Number tile on the Logs source showing count.
  2. Click the filter icon ("Edit filters and variables") → "Add new filter", choose "Ad hoc keys and values", select Logs under "Data sources", and save.
  3. In the filter bar, click "+" next to the filter and add LogAttributes['teamName'], =, and your team name.
  4. Edit the tile, run the query, and open "Generated SQL" ("Apply filters" on). The WHERE clause contains toString(LogAttributes['teamName']) = '<team>'.

On this branch:

  1. Repeat the steps. The WHERE clause contains has(`LogAttributeItems`, concat('teamName', '=', '<team>')).
  2. Remove the condition, add a "SQL expression values" filter on LogAttributes['teamName'] for Logs, and select your team. The WHERE clause contains has(`LogAttributeItems`, concat('teamName', '=', '<team>')) instead of toString(LogAttributes['teamName']) IN ('<team>').

References

…ilter keys

Ad hoc and dashboard filters render Map keys as toString(Map['k']), which
the has(<items>, ...) rewrite skipped, so these filters decoded the whole
Map for every row instead of using the text index.
@karl-power
karl-power requested a review from a team as a code owner November 25, 2026 10:35
@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hyperdx-oss Ready Ready Preview Nov 25, 2026 10:39am UTC
hyperdx-storybook Ready Ready Preview Nov 25, 2026 10:39am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 89ac7ec

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@hyperdx/common-utils Patch
@hyperdx/app Patch
@hyperdx/api Patch
@hyperdx/otel-collector Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the review/tier-2 Low risk — AI review + quick human skim label Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔵 Tier 2 — Low Risk

Small, isolated change with no API route or data model modifications.

Why this tier:

  • Standard feature/fix — introduces new logic or modifies core functionality

Additional context: touches the query rendering engine lightly (21 lines, under the 150-line bar for Tier 4)

Review process: AI review + quick human skim (target: 5–15 min). Reviewer validates AI assessment and checks for domain-specific concerns.
SLA: Resolve within 4 business hours.

Stats
  • Production files changed: 1
  • Production lines changed: 21 (+ 165 in test files, excluded from tier calculation)
  • Branch: claude/adhoc-filter-kv-rewrite
  • Author: karl-power

To override this classification, remove the review/tier-2 label and apply a different review/tier-* label. Manual overrides are preserved on subsequent pushes.

@github-actions

Copy link
Copy Markdown
Contributor

Duplicate code analysis

No new duplicate code introduced by this PR.


jscpd compared this branch against origin/main
across app, api and common-utils. Only new clones are listed; the codebase
currently holds 107 in total (1.12%).
Run yarn dupes locally to reproduce, or yarn dupes:all for the full picture.

@github-actions

Copy link
Copy Markdown
Contributor

PR Review

✅ No issues found.

@github-actions

Copy link
Copy Markdown
Contributor

Deep Review

✅ No critical issues found.

This is a tightly-scoped change: a single unwrapToString() helper plus one call-site edit in rewriteSqlFilterWithKvItems, backed by comprehensive unit tests and two integration tests. All reviewers traced the behavior and found no correctness, security, or type-safety regression.

  • The toString-drop is sound: for maps with a registered KV items column the values are strings, so toString(Map['k']) is identity — the same invariant the pre-existing bare Map['k'] rewrite already relied on. The empty-value/absent-key bail at renderChartConfig.ts:506 still fires identically (toString('') = ''), and != / NOT IN / LIKE / regex / wrapper-on-right / nested / wrong-case / multi-arg cases are all guarded and tested as left-untouched.
  • No new SQL-injection surface: unwrapping only broadens which left-hand nodes match; keySql/valueSql are still derived from validated single_quote_string nodes via parser.exprToSQL, unchanged by this diff.
  • The changeset package list (@hyperdx/common-utils + @hyperdx/app) matches the repo's established convention for common-utils fixes; dependent packages are auto-bumped at release. Not a standards violation.
🔵 P3 nitpicks (1)
  • packages/common-utils/src/__tests__/renderChartConfig.test.ts:2124 — the new dashboard integration test drives only the included/IN selection, so the excluded-dropdown path (toString(key) NOT IN (...) left unrewritten) is verified at the unit level only, not end-to-end.
    • Fix: add an excluded set to the dashboard integration test asserting the NOT IN condition passes through unchanged.

Reviewers (7): correctness, testing, maintainability, project-standards, kieran-typescript, security, previous-comments.

Testing gaps:

  • Excluded-dropdown NOT IN left-untouched behavior is unit-covered but not exercised in the integration test.
  • No integration test confirms equivalence against a Map(String, LowCardinality(String)) column; soundness rests on the registration-time string-value invariant (pre-existing, not introduced by this diff).

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@github-actions

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 476 passed • 1 skipped • 1063s

Status Count
✅ Passed 476
❌ Failed 0
⚠️ Flaky 12
⏭️ Skipped 1

Tests ran across 4 shards in parallel.

View full report →

@karl-power
karl-power merged commit d628084 into main Nov 25, 2026
32 checks passed

This branch was successfully deployed

2 active deployments
Preview – hyperdx-oss — 89ac7ec3 Deployed Nov 25, 2026 by vercel[bot]
Preview – hyperdx-storybook — 89ac7ec3 Deployed Nov 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review/tier-2 Low risk — AI review + quick human skim

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants