feat(query-engine): SQL Top-K via CountMinSketchWithHeap#389
Merged
milindsrivastava1997 merged 4 commits intoJun 6, 2026
Merged
Conversation
… flags (#391) * split enable_topk into limiting vs formatting flags * add PromQL topk pipeline and Prometheus wire format tests * formatting
ccbec5e to
11bc4c9
Compare
milindsrivastava1997
approved these changes
Jun 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Adds an end-to-end SQL execution path for approximate top-k queries by event count, aligned with the existing PromQL
topk(k, ...)implementation. Queries of the form:are now recognized as top-k queries during SQL planning, mapped to
Statistic::Topk, and executed usingCountMinSketchWithHeap. Instead of materializing all group-by keys and applyingORDER BY/LIMITas a post-processing step, the engine retrieves candidate keys directly from the sketch heap, estimates counts from the CMS, sorts them, and returns the top-k results.Changes by Area
Precompute (
accumulator_factory.rs)CmsWithHeapAccumulatorUpdaterforCountMinSketchWithHeap.CmsAccumulatorUpdater, which treated the sketch as a plain CMS and could not enumerate heap candidates.count_eventssupport (defaulttrue), allowing COUNT-style workloads to update the sketch with a constant weight of1.0per observation rather than using the sample value as the weight.cms_heap_params()to construct heap-backed sketches from planner-generated parameters.depth,width,heapsize) and legacy naming (row_num,col_num,heap_size), with defaults of3,1024, and32.SQL Engine (
sql.rs)Added
detect_sql_topk()to identify SQL top-k patterns and promote them toStatistic::Topk.Detection requires:
COUNT(...)GROUP BYORDER BY <aggregate_alias> DESCLIMIT kExtracts and stores
kinquery_kwargsfor downstream execution.Context generation uses
Statistic::Topkand emptygrouping_labels, matching the intended sketch layout where the GROUP BY column is represented as the sketch's aggregated dimension rather than a storage partition key.Updated
handle_query_sql()so top-k queries execute through the sketch heap path with:enable_topk_limiting = trueenable_topk_formatting = falseSQL top-k queries bypass
SqlPostProcessing, since ordering and truncation are now performed directly by the query pipeline.Added unit tests covering:
Query Pipeline (
mod.rs)Split the previous
enable_topkflag into two independent controls:enable_topk_limitingenable_topk_formattingenable_topk_limitingsorts top-k candidates and truncates results tok.enable_topk_formattingpreserves the existing PromQL behavior of prepending metric names to labels.Heap candidate collection no longer truncates during enumeration.
The full candidate set is collected first, then sorted and truncated within the pipeline, ensuring consistent ordering behavior across execution paths.
Call Sites
PromQL
enable_topk_limiting = trueenable_topk_formatting = trueSQL Top-K
enable_topk_limiting = trueenable_topk_formatting = falseSQL (non-top-k) / Elastic
enable_topk_limiting = falseenable_topk_formatting = falseTests and Supporting Changes
engine_factories.rsto accommodate the newexecute_query_pipelinesignature.