Skip to content

fix: Fix table sorting for formula, group by, and multi-series metric aliases - #3748

Open
pulpdrew wants to merge 1 commit into
mainfrom
drew/chart-explorer-table-order-quoting
Open

pulpdrew wants to merge 1 commit into
mainfrom
drew/chart-explorer-table-order-quoting

Conversation

@pulpdrew

@pulpdrew pulpdrew commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Problem: Clicking the header of a table chart column whose name is an alias in the SQL (for example "duration min") could send an invalid ORDER BY. This happened for formula columns, aliased group-bys, and multi-series metric queries. Before, only series aliases were quoted.

Fix:

  • Quoting: sort ids now quote every column that is an explicit alias in the final SELECT, using the shared quotedColumnName. That covers series aliases, formula columns, aliased group-bys, and every column of a composed metric query (more than one series, or any formula). Other columns keep their bare expression, because ClickHouse can't resolve a quoted auto-generated name like "count()".
  • Shared logic: added isComposedMetricChartConfig in common-utils and reused it in renderChartConfig. The group-by column matching in useResultColumnFilterKeys (click-to-filter) now also returns each entry's alias.

Screenshots or video

Before

Screen.Recording.2026-11-20.at.2.53.56.PM.mov

After

Screen.Recording.2026-11-20.at.2.55.47.PM.mov

How to test on Vercel preview

Follow the steps from the videos

References

  • Linear Issue: Closes HDX-6136
  • Related PRs:

@pulpdrew
pulpdrew requested a review from a team as a code owner November 20, 2026 19:46
@vercel

vercel Bot commented Nov 20, 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 20, 2026 7:49pm UTC
hyperdx-storybook Ready Ready Preview Nov 20, 2026 7:49pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 89ff875

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

This PR includes changesets to release 4 packages
Name Type
@hyperdx/app Patch
@hyperdx/common-utils 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-3 Standard — full human review required label Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🟡 Tier 3 — Standard

Introduces new logic, modifies core functionality, or touches areas with non-trivial risk.

Why this tier:

  • Cross-layer change: touches frontend (packages/app) + shared utils (packages/common-utils)

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

Review process: Full human review — logic, architecture, edge cases.
SLA: First-pass feedback within 1 business day.

Stats
  • Production files changed: 5
  • Production lines changed: 198 (+ 390 in test files, excluded from tier calculation)
  • Branch: drew/chart-explorer-table-order-quoting
  • Author: pulpdrew

To override this classification, remove the review/tier-3 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 108 in total (1.13%).
Run yarn dupes locally to reproduce, or yarn dupes:all for the full picture.

@github-actions

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

2 finding(s): 🔴 0 P0/P1 · 🟡 1 P2 · 🔵 1 P3

1 posted as inline comment(s) on the changed lines. 1 P3 folded at the bottom.

🔵 1 P3 — no action needed before merge
  • 🔵 P3 packages/app/tests/e2e/features/chart-explorer.spec.ts:728 — E2E sort-indicator check passes from DBTableChart's internal state, so it hides the round-trip failure → In ChartPreviewPanel, "duration min" DESC does not parse back into a SortingState (see orderByStringToSortingState in packages/app/src/utils.ts). The indicator still appears only because DBTableChart's local setSort state is used as a fallback, so this assertion would pass even if the controlled sort were broken. Add a unit test that round-trips a quoted id with a space through sortingStateToOrderByString and orderByStringToSortingState, or reload the page in the e2e before asserting the indicator.

Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

@github-actions

Copy link
Copy Markdown
Contributor

Deep Review

Scope: builder table-chart sort fix — getAliasedResultColumns (sorting.ts) now decides which result columns ORDER BY must quote (series aliases, formula columns, group-by aliases, and all composed-metric columns), replacing the old select-alias-only aliasMap in DBTableChart.tsx. Supporting refactors export getFormulaConfig, quotedColumnName, isComposedMetricChartConfig, and extract getBuilderGroupByColumns from the filter-key logic.

The fix is well-grounded: parseSelectItem derives aliases from chSqlToAliasMap, which only records an alias when a select/group-by item has an explicit AS (selectColumnsToAliasMap gates on column.as != null), so unaliased expressions correctly stay bare and re-evaluatable. The quoted set is a strict, more-correct superset of the prior behavior, and the change is covered by new unit tests (getAliasedResultColumns, getBuilderGroupByColumns, column-sort-id) plus a full-stack e2e test.

✅ No critical issues found.

🟡 P2 -- recommended

  • packages/app/src/components/DBTable/sorting.ts:314 -- the composed-metric branch quotes every result column including group-by passthroughs, which is correct only if renderMultiSeriesMetricChartConfig aliases every such column; this is unit-tested but no end-to-end test sorts a multi-series metric table by a group-by column to prove the emitted ORDER BY is valid.
    • Fix: add an e2e or integration test that sorts a multi-series metric table by a group-by column and asserts the query returns rows without a chart-error state.
🔵 P3 nitpicks (2)
  • .claude/skills/refactor-component/SKILL.md:181 -- the prettier --write → oxfmt doc edit is unrelated to the table-sorting fix and widens the PR's scope.
    • Fix: confirm the formatter-doc change belongs in this PR or split it into its own change.
  • packages/app/src/components/DBTable/sorting.ts:321 -- duplicate select aliases make ClickHouse suffix the second result column, which is matched by neither name nor the formula slice and is left bare (an unsortable non-expression id); this mirrors the prior behavior and is not a regression, but remains an untested edge of the new helper.
    • Fix: add a unit case for duplicate aliases documenting the intended quoting, or handle the suffixed column explicitly.

Reviewers (7): correctness, adversarial, kieran-typescript, testing, maintainability, api-contract, project-standards.

Testing gaps: multi-series metric table sorted by a group-by column is unit-covered but lacks an end-to-end assertion that the resulting ORDER BY query succeeds.

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 • 471 passed • 1 skipped • 1187s

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

Tests ran across 4 shards in parallel.

View full report →

This branch was successfully deployed

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

Labels

automerge review/tier-3 Standard — full human review required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant