pub struct QueryPlanner { /* private fields */ }Expand description
Query planner that integrates statistics-based optimization
Implementations§
Source§impl QueryPlanner
impl QueryPlanner
Sourcepub fn new(engine: Arc<MVCCEngine>) -> Self
pub fn new(engine: Arc<MVCCEngine>) -> Self
Create a new query planner
Sourcepub fn invalidate_stats_cache(&self, table_name: &str)
pub fn invalidate_stats_cache(&self, table_name: &str)
Invalidate cached statistics for a table
Call this after ANALYZE to ensure fresh statistics are used.
Sourcepub fn clear_stats_cache(&self)
pub fn clear_stats_cache(&self)
Clear all cached statistics
Sourcepub fn get_table_stats(&self, table_name: &str) -> Option<TableStats>
pub fn get_table_stats(&self, table_name: &str) -> Option<TableStats>
Get or load statistics for a table
If cached stats are stale (older than TTL), they will be refreshed from the system tables.
Returns None if:
- No statistics have been collected (ANALYZE not run)
- Statistics have row_count == 0 (empty/invalid stats)
Sourcepub fn get_table_stats_with_fallback(&self, table: &dyn Table) -> TableStats
pub fn get_table_stats_with_fallback(&self, table: &dyn Table) -> TableStats
Get table statistics with fallback to runtime estimation
If ANALYZE hasn’t been run, computes basic statistics from the table. This ensures the optimizer always has some statistics to work with.
Sourcepub fn get_column_stats(
&self,
table_name: &str,
column_name: &str,
) -> Option<ColumnStatsCache>
pub fn get_column_stats( &self, table_name: &str, column_name: &str, ) -> Option<ColumnStatsCache>
Get column statistics
If cached stats are stale (older than TTL), they will be refreshed.
Sourcepub fn get_zone_maps(&self, table: &dyn Table) -> Option<Arc<TableZoneMap>>
pub fn get_zone_maps(&self, table: &dyn Table) -> Option<Arc<TableZoneMap>>
Get zone maps for a table (from table, not system tables) Uses Arc to avoid cloning on high QPS workloads
Sourcepub fn can_prune_entire_scan(
&self,
table: &dyn Table,
expr: &dyn Expression,
) -> bool
pub fn can_prune_entire_scan( &self, table: &dyn Table, expr: &dyn Expression, ) -> bool
Check if zone maps indicate that no rows can possibly match the expression
Returns true if the entire scan can be skipped (zone maps show no match possible). Returns false if:
- Zone maps are not available
- Some segments might match
- Expression cannot be evaluated against zone maps
This enables early exit optimization for range queries on ordered data.
Sourcepub fn stats_health(&self, table_name: &str) -> StatsHealth
pub fn stats_health(&self, table_name: &str) -> StatsHealth
Get overall health of statistics for a table
Sourcepub fn estimate_scan_rows(
&self,
table_name: &str,
predicate: Option<&Expression>,
) -> Option<u64>
pub fn estimate_scan_rows( &self, table_name: &str, predicate: Option<&Expression>, ) -> Option<u64>
Estimate the number of rows that will be returned by a scan with a predicate
This method uses table statistics and column statistics to estimate selectivity of predicates. It also applies cardinality feedback corrections if available from previous query executions.
§Arguments
table_name- Name of the table being scannedpredicate- Optional WHERE clause predicate
§Returns
Estimated number of rows, or None if stats are unavailable
Sourcepub fn estimate_with_feedback(
&self,
table_name: &str,
predicate: Option<&Expression>,
base_estimate: u64,
) -> u64
pub fn estimate_with_feedback( &self, table_name: &str, predicate: Option<&Expression>, base_estimate: u64, ) -> u64
Estimate row count with cardinality feedback correction
This method combines statistics-based estimation with learned corrections from previous query executions. When similar predicates have been executed before, the correction factor improves accuracy.
§Arguments
table_name- Name of the table being scannedpredicate- The WHERE clause predicate (for fingerprinting)base_estimate- Initial row count estimate from statistics
§Returns
Corrected row count estimate
Sourcepub fn record_feedback(
&self,
table_name: &str,
predicate: &Expression,
column_name: Option<String>,
estimated_rows: u64,
actual_rows: u64,
)
pub fn record_feedback( &self, table_name: &str, predicate: &Expression, column_name: Option<String>, estimated_rows: u64, actual_rows: u64, )
Record cardinality feedback after query execution
This method stores the difference between estimated and actual row counts, enabling future queries with similar predicates to benefit from the correction.
§Arguments
table_name- Name of the table that was scannedpredicate- The WHERE clause predicate (for fingerprinting)column_name- Optional column name for more specific feedbackestimated_rows- Row count estimate used during planningactual_rows- Actual row count observed during execution
Sourcepub fn get_feedback_correction(
&self,
table_name: &str,
predicate: &Expression,
) -> f64
pub fn get_feedback_correction( &self, table_name: &str, predicate: &Expression, ) -> f64
Get the correction factor for a predicate (for debugging/EXPLAIN)
Returns 1.0 if no feedback is available or if feedback is not yet reliable.
Source§impl QueryPlanner
impl QueryPlanner
Sourcepub fn plan_runtime_join(
&self,
left_rows: usize,
right_rows: usize,
has_equality_keys: bool,
) -> RuntimeJoinDecision
pub fn plan_runtime_join( &self, left_rows: usize, right_rows: usize, has_equality_keys: bool, ) -> RuntimeJoinDecision
Make a runtime join algorithm decision based on actual row counts
This is called during execution with the actual materialized row counts, enabling adaptive decisions that account for runtime conditions. Also consults the EdgeAwarePlanner for workload-learned hints.
§Arguments
left_rows- Actual row count from left sideright_rows- Actual row count from right sidehas_equality_keys- Whether join has equality conditions (a.x = b.x)
§Returns
Decision on which algorithm to use and whether to swap sides
Sourcepub fn plan_runtime_join_with_sort_info(
&self,
left_rows: usize,
right_rows: usize,
has_equality_keys: bool,
left_sorted: bool,
right_sorted: bool,
) -> RuntimeJoinDecision
pub fn plan_runtime_join_with_sort_info( &self, left_rows: usize, right_rows: usize, has_equality_keys: bool, left_sorted: bool, right_sorted: bool, ) -> RuntimeJoinDecision
Make a runtime join algorithm decision with sort information
Extended version that also considers whether inputs are pre-sorted, which enables merge join optimization.
§Arguments
left_rows- Actual row count from left sideright_rows- Actual row count from right sidehas_equality_keys- Whether join has equality conditions (a.x = b.x)left_sorted- Whether left input is sorted on join keysright_sorted- Whether right input is sorted on join keys