Files
greptimedb/src/query/src/optimizer.rs
T
dennis zhuang 27182bdd69 perf(query): fill TopK heaps early over filtered region scans (#10515)
* perf(query): let a TopK over a filtered scan publish its dynamic filter early

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* perf(query): share the global TopK conditions and flush filters per batch

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(query): pin unsplit filter output at batch size 1

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* perf(query): balance rows when assigning ranges in time order

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* perf(query): leave TopK on other sort keys unchanged

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* perf(query): spread ranges of unknown size and test the scan wiring

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(query): share TopK test helpers and merge assignment cases

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(query): leave fetch-limited filters alone under a TopK

A FilterExec with a fetch keeps the first rows that pass, so changing
when the TopK threshold arrives or the order ranges are read changes
which rows it keeps. Both rules now stop at such a filter.

Also use replace_children instead of the deprecated with_new_children,
and document the coalescer behavior that batch size 1 relies on.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(query): rewrite only TopKs bounded by a SortPreservingMergeExec

A partitioned TopK keeps up to fetch rows per partition, which equals
the global result only because the merge above it takes the first rows
of all partitions. Match from that merge, through projections, and
require the TopK's fetch to cover the merge's, so a TopK whose
per-partition rows are part of the result is never rewritten.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* docs(query): state what the global TopK fetch cap does and does not bound

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* perf(query): keep per-partition TopK heaps when no range can be skipped

With no more ranges than partitions each partition reads at most one
range, so the TopK threshold leaves no later range to skip and one heap
only adds a single consumer. filtered_scan_below_topk now requires more
ranges than partitions; both callers pass the partition count they plan
for.

The small sqlness tables in order_by and windowed_sort fall under the
guard, so a new windowed_sort case with six files covers the rewrite
end to end.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(perf): add a query regression case for time-index TopK over a selective filter

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(sqlness): say what the one-heap windowed_sort case pins

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
2026-11-19 15:55:49 +00:00

60 lines
1.9 KiB
Rust

// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pub mod const_normalization;
pub mod constant_term;
pub mod count_nest_aggr;
pub mod count_wildcard;
pub mod enforce_sorting;
pub(crate) mod expr_depth;
pub mod global_limit;
pub mod global_topk;
pub(crate) mod infer_aggregate_input_bounds;
pub(crate) mod insert_assignment;
pub(crate) mod json_schema_concretize;
pub(crate) mod json_type_concretize;
pub(crate) mod merge_scan_projection;
pub mod parallelize_scan;
pub mod promql_tsid_narrow_join;
pub mod remove_duplicate;
pub mod scan_hint;
pub mod series_frame_output;
pub(crate) mod sst_manifest_aggregate;
pub mod string_normalization;
#[cfg(test)]
pub(crate) mod test_util;
pub mod topk_filter_batch;
pub mod transcribe_atat;
pub mod type_conversion;
pub mod windowed_sort;
use datafusion_common::Result;
use datafusion_common::config::ConfigOptions;
use datafusion_expr::LogicalPlan;
use crate::QueryEngineContext;
/// [`ExtensionAnalyzerRule`]s transform [`LogicalPlan`]s in some way to make
/// the plan valid prior to the rest of the DataFusion optimization process.
/// It's an extension of datafusion [`AnalyzerRule`]s but accepts [`QueryEngineContext`] as the second parameter.
pub trait ExtensionAnalyzerRule {
/// Rewrite `plan`
fn analyze(
&self,
plan: LogicalPlan,
ctx: &QueryEngineContext,
config: &ConfigOptions,
) -> Result<LogicalPlan>;
}