Skip to content

fix: skip exhausted search windows - #3587

Open
adityareddy-dev wants to merge 10 commits into
ClickHouse:mainfrom
adityareddy-dev:fix/skip-exhausted-search-pages
Open

adityareddy-dev wants to merge 10 commits into
ClickHouse:mainfrom
adityareddy-dev:fix/skip-exhausted-search-pages

Conversation

@adityareddy-dev

@adityareddy-dev adityareddy-dev commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A short search page triggered another query in the same time window. I now skip that query only when the effective query limits rule out truncation. Capped or unknown results keep paging within the window.

Fixes #1403.

How to reproduce

Starting point: A search spanning multiple time windows, with a page limit of 100 and only one matching row in the first window.

On main:

  1. Fetch the first page, then request the next page.
  2. The next query repeats the first window with OFFSET 1.

On this branch:

  1. Repeat the same search with each applicable overflow mode set to throw or its limits disabled.
  2. The next query starts the second window with OFFSET 0.
  3. When a result, read, timeout or other query limit can truncate the response, a nonempty short page keeps paging within the current window. Unknown settings use the same behavior.

How to test on Vercel preview

N/A, query pagination change with no visual changes.

References

#1403


Note

Medium Risk
Pagination behavior changes for search/time-windowed queries; incorrect truncation detection could skip real rows or issue extra queries, though logic is conservative when settings are unknown.

Overview
useOffsetPaginatedQuery no longer issues another offset query in the same time window when a page returns fewer rows than the page size and effective ClickHouse settings show results cannot be silently truncated (overflow modes throw or caps disabled, merged from server metadata, source querySettings, and team timeout).

In those cases pagination advances to the next time window at offset 0 instead of probing OFFSET n in an exhausted window. When caps or break overflow modes might hide more rows—or settings are unknown—it still offsets within the window until an empty page or a full page proves continuation is safe.

Each page now carries a computed canSkipShortPage flag used by getNextPageParam. Tests cover the overflow/limit matrix and related edge cases.

Reviewed by Cursor Bugbot for commit fae7739. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions github-actions Bot added external needs-vouch Author needs a maintainer to vouch for them labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Hi @adityareddy-dev, thanks for the pull request!

Before we review code from a first-time contributor, a maintainer vouches for them, and you're not on our list yet. There's nothing else to file and this PR stays open — a maintainer will look at it, usually within a day or two, and vouch for you right here if it's a genuine attempt at the problem.

To make that quick, check the PR description has:

  • The issue it addresses (ideally one labelled Accepting PR) and why the change is needed
  • Steps to reproduce the problem and verify the fix, starting from https://play.hyperdx.io or yarn app:dev:local connected to the demo server
  • Before/after screenshots if anything in the UI changed

More detail in our contributing guide.

@github-actions

github-actions Bot commented Nov 6, 2026 •

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.

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

No inline comments. 3 P3 folded at the bottom.

🔵 3 P3 — no action needed before merge
  • 🔵 P3 packages/app/src/hooks/useOffsetPaginatedQuery.tsx:275 — About 60 lines of truncation analysis are written inline in queryFn and rerun on every page → Move this into a pure exported helper, e.g. canQueryResultBeTruncated(serverSettings, querySettings, queryTimeout, requestedLimit), next to joinQuerySettings in common-utils/core/utils.ts, and unit-test it directly. The result depends only on connection, source and timeout, not on the page. Right now the overflow/limit matrix can only be checked through 60+ full renderHook runs, and the logic is not reusable by other paginators.
  • 🔵 P3 packages/app/src/hooks/useOffsetPaginatedQuery.tsx:279 — The .catch(() => undefined) on getSettings handles a case that cannot happen in production, and tests depend on it → clickhouseClient.query runs the same uncached-on-failure getMetadata(this).getSettings({ connectionId }) lookup in processClickhouseSettings (common-utils/src/clickhouse/index.ts:628, called from browser.ts:175 and node.ts:103) and does not catch errors. So if the settings lookup rejects, the search query rejects too, and the 'failed settings lookup' cases can only pass because query is mocked. Drop the catch and those test cases, or document that the catch only covers a transient first failure.
  • 🔵 P3 packages/app/src/hooks/useOffsetPaginatedQuery.tsx:282 — The empty-value filter on source querySettings repeats a check the schema already enforces → QuerySettingsSchema (common-utils/src/types.ts:2778) already requires setting and value to be min(1), and joinQuerySettings (core/utils.ts:1456) applies the same filter. Build the map with new Map(source?.querySettings?.map(({ setting, value }) => [setting, value])), or share one normalizer with joinQuerySettings, so the SQL SETTINGS clause and this check can't drift apart.

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.

@adityareddy-dev
adityareddy-dev force-pushed the fix/skip-exhausted-search-pages branch from bcacd17 to b25c8cc Compare November 6, 2026 18:22
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

✅ No critical issues found. The core fix is sound: every uncertain path (getSettings throwing, empty-Map on insufficient privileges, non-numeric settings, any unmodeled overflow mode) conservatively resolves canSkipShortPage to false and keeps paging, and default throw-mode servers correctly skip the redundant offset query. The recommendations below are edge-case and maintainability items.

🟡 P2 — recommended

  • packages/app/src/hooks/useOffsetPaginatedQuery.tsx:160 — No-LIMIT builder queries skip the truncation check entirely (getNextPageParam requires requestedLimit > 0 and the queryFn block is gated by (config.limit?.limit ?? 0) > 0), so a timestamp-ordered query with no LIMIT advances to the next window right after the first nonempty page; on a server with result_overflow_mode=break and a nonzero max_result_rows, the truncated remainder of that window is silently dropped from results. Default throw servers are unaffected.
    • Fix: Consult the same non-truncating-settings check before advancing the window for no-LIMIT builder queries, or refuse the immediate advance when server settings are unknown or cap with break.
    • adversarial
  • packages/app/src/hooks/useOffsetPaginatedQuery.tsx:297 — The ~60-line overflow-mode truncation logic (limitsByMode/leafLimitsByMode, cannotTruncate, getSetting) is inlined in queryFn and only reachable end-to-end, and nothing documents that an unmodeled or newly shipped ClickHouse overflow mode must keep the result non-skippable — omitting a mode silently flips canSkipShortPage true and drops a real next page.
    • Fix: Extract to a pure, unit-tested helper (e.g. computeCanSkipShortPage(...)) and add a comment stating that unknown modes must default to not-skippable.
    • kieran-typescript, maintainability
🔵 P3 nitpicks (2)
  • packages/app/src/hooks/useOffsetPaginatedQuery.tsx:88 — canSkipShortPage is an optional TQueryFnData field that must be re-set on each paginating return; it is set at only two of several construction sites, so a future return path can omit it with no type error and silently disable the skip.
    • Fix: Make the field required with an explicit default, or set it through a single helper so no return site can forget it.
  • packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx:149 — The useSource mock is widened with as ReturnType<typeof useSource> instead of the suite's fromPartial convention, removing the type-checker's guard if the hook starts reading other source fields.
    • Fix: Use fromPartial({ data: ..., isLoading: false }) to match the existing partial-mock pattern.

Pre-existing (not introduced by this diff):

  • packages/app/src/hooks/useOffsetPaginatedQuery.tsx:184 — getNextPageParam derives windowing from the raw hook config while queryFn executes against optimizedConfig; because the MV optimizer rewrites timestampValueExpression but keeps orderBy, isTimestampExpressionInFirstOrderBy can disagree between the two. The window-stepping code is unchanged by this PR; flagged only as residual risk. correctness

Reviewers (11): correctness, adversarial, kieran-typescript, performance, reliability, testing, maintainability, project-standards, previous-comments, agent-native, learnings-researcher.

Testing gaps:

  • No test combines an MV-optimized config (differing timestampValueExpression) with the short-page skip, so the raw-vs-optimized windowing divergence is unverified.
  • No test covers a no-LIMIT builder query against a result_overflow_mode=break server with a nonzero max_result_rows (the window-drop edge above).
  • getSettings throwing a non-privileges error vs resolving an empty Map are not distinctly asserted to both yield canSkipShortPage=false.

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

@adityareddy-dev
adityareddy-dev force-pushed the fix/skip-exhausted-search-pages branch from ab7139d to 9263c98 Compare November 6, 2026 18:57
@adityareddy-dev

Copy link
Copy Markdown
Contributor Author

Done.

@adityareddy-dev
adityareddy-dev force-pushed the fix/skip-exhausted-search-pages branch from e508541 to bdac94d Compare November 6, 2026 20:53
@vercel

vercel Bot commented Nov 6, 2026

Copy link
Copy Markdown

@adityareddy-dev is attempting to deploy a commit to the ClickHouse Team on Vercel.

A member of the Team first needs to authorize it.

@adityareddy-dev
adityareddy-dev force-pushed the fix/skip-exhausted-search-pages branch from 2a44a78 to 7b0c09f Compare November 6, 2026 21:29
@adityareddy-dev
adityareddy-dev force-pushed the fix/skip-exhausted-search-pages branch from 7b0c09f to aa6623c Compare November 6, 2026 21:30
@adityareddy-dev

Copy link
Copy Markdown
Contributor Author

Fixed.

@adityareddy-dev
adityareddy-dev force-pushed the fix/skip-exhausted-search-pages branch from ea17b69 to 01081a8 Compare November 6, 2026 21:49
});
const sourceSettings = new Map(
source?.querySettings
?.filter(({ setting, value }) => setting.length && value.length)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — Empty-setting filter copies joinQuerySettings and can drift from the SQL actually sent

The setting.length && value.length filter repeats the one in joinQuerySettings (packages/common-utils/src/core/utils.ts:1393), which decides which source settings reach the SQL SETTINGS clause. The inference here is only correct if both filters stay identical. Fix: export a small filterQuerySettings (or querySettingsToMap) helper from common-utils and use it in both places.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

source?.querySettings,
);

let canSkipShortPage = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — The check for whether query limits can truncate a page is about 60 lines of logic written inline in the queryFn, and it repeats the empty-setting filter from joinQuerySettings

Move this logic into a pure helper next to joinQuerySettings in packages/common-utils/src/core/utils.ts (for example canResultBeTruncated(serverSettings, querySettings, queryTimeout, limit)). The helper should reuse that file's setting.length && value.length filter (utils.ts:1393) so the SETTINGS rendering and this check can't drift apart. Then unit-test it directly instead of through roughly 80 hook renders.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

@adityareddy-dev
adityareddy-dev requested a review from a team as a code owner November 10, 2026 08:26
@changeset-bot

changeset-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fae7739

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

This PR includes changesets to release 3 packages
Name Type
@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

?.filter(({ setting, value }) => setting.length && value.length)
.map(({ setting, value }) => [setting, value]),
);
// SQL SETTINGS override the timeout injected by the client, then server defaults.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — The hook keeps its own copy of how ClickHouse settings take effect, in two places

Move the settings check (lines 275-336) into a pure helper in packages/common-utils/src/core/utils.ts, next to joinQuerySettings. It would take the server settings, source settings, queryTimeout and the requested limit, and return a boolean. Today the inline code copies two pieces of logic. (1) The empty-pair filter at lines 281-283 repeats joinQuerySettings (core/utils.ts:1421). If the copies drift, the decision will be based on settings that differ from the SETTINGS clause actually sent. (2) The max_execution_time precedence (source setting, then the client timeout, then the server value) restates processClickhouseSettings (packages/common-utils/src/clickhouse/index.ts:609-614). A setting added to the client defaults later will not be counted here. Unit-test the helper directly instead of rendering the whole hook for each of the ~90 settings combinations.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

@adityareddy-dev

Copy link
Copy Markdown
Contributor Author

@karl-power can you vouch for me on this one?

…arch-pages

# Conflicts:
#	packages/app/src/hooks/__tests__/useOffsetPaginatedQuery.test.tsx

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external needs-vouch Author needs a maintainer to vouch for them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Partial Results in Paginated Search Invokes Repetitive Search

1 participant