Spaces:
Build error
Build error
File size: 7,775 Bytes
84d2a97 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
use std::cmp::Reverse;
use itertools::Itertools;
use crate::index::field_index::CardinalityEstimation;
use crate::index::query_estimator::{
combine_min_should_estimations, combine_must_estimations, combine_should_estimations,
invert_estimation,
};
use crate::index::query_optimization::optimized_filter::{
OptimizedCondition, OptimizedFilter, OptimizedMinShould,
};
use crate::index::query_optimization::payload_provider::PayloadProvider;
use crate::index::struct_payload_index::StructPayloadIndex;
use crate::types::{Condition, Filter, MinShould};
impl StructPayloadIndex {
/// Converts user-provided filtering condition into optimized representation
///
/// Optimizations:
///
/// * Convert each condition into a checker function
/// * Use column index, avoid reading Payload, if possible
/// * Re-order operations using estimated cardinalities
///
/// ToDo: Add optimizations between clauses
///
/// # Arguments
///
/// * `filter` - original filter
/// * `payload_provider` - provides the payload storage
/// * `total` - total number of points in segment (used for cardinality estimation)
///
/// # Result
///
/// Optimized query + Cardinality estimation
pub fn optimize_filter<'a>(
&'a self,
filter: &'a Filter,
payload_provider: PayloadProvider,
total: usize,
) -> (OptimizedFilter<'a>, CardinalityEstimation) {
let mut filter_estimations: Vec<CardinalityEstimation> = vec![];
let optimized_filter = OptimizedFilter {
should: filter.should.as_ref().and_then(|conditions| {
if !conditions.is_empty() {
let (optimized_conditions, estimation) =
self.optimize_should(conditions, payload_provider.clone(), total);
filter_estimations.push(estimation);
Some(optimized_conditions)
} else {
None
}
}),
min_should: filter.min_should.as_ref().and_then(
|MinShould {
conditions,
min_count,
}| {
if !conditions.is_empty() {
let (optimized_conditions, estimation) = self.optimize_min_should(
conditions,
*min_count,
payload_provider.clone(),
total,
);
filter_estimations.push(estimation);
Some(OptimizedMinShould {
conditions: optimized_conditions,
min_count: *min_count,
})
} else {
None
}
},
),
must: filter.must.as_ref().and_then(|conditions| {
if !conditions.is_empty() {
let (optimized_conditions, estimation) =
self.optimize_must(conditions, payload_provider.clone(), total);
filter_estimations.push(estimation);
Some(optimized_conditions)
} else {
None
}
}),
must_not: filter.must_not.as_ref().and_then(|conditions| {
if !conditions.is_empty() {
let (optimized_conditions, estimation) =
self.optimize_must_not(conditions, payload_provider, total);
filter_estimations.push(estimation);
Some(optimized_conditions)
} else {
None
}
}),
};
(
optimized_filter,
combine_must_estimations(&filter_estimations, total),
)
}
fn convert_conditions<'a>(
&'a self,
conditions: &'a [Condition],
payload_provider: PayloadProvider,
total: usize,
) -> Vec<(OptimizedCondition<'a>, CardinalityEstimation)> {
conditions
.iter()
.map(|condition| match condition {
Condition::Filter(filter) => {
let (optimized_filter, estimation) =
self.optimize_filter(filter, payload_provider.clone(), total);
(OptimizedCondition::Filter(optimized_filter), estimation)
}
_ => {
let estimation = self.condition_cardinality(condition, None);
let condition_checker =
self.condition_converter(condition, payload_provider.clone());
(OptimizedCondition::Checker(condition_checker), estimation)
}
})
.collect()
}
fn optimize_should<'a>(
&'a self,
conditions: &'a [Condition],
payload_provider: PayloadProvider,
total: usize,
) -> (Vec<OptimizedCondition<'a>>, CardinalityEstimation) {
let mut converted = self.convert_conditions(conditions, payload_provider, total);
// More probable conditions first
converted.sort_by_key(|(_, estimation)| Reverse(estimation.exp));
let (conditions, estimations): (Vec<_>, Vec<_>) = converted.into_iter().unzip();
(conditions, combine_should_estimations(&estimations, total))
}
fn optimize_min_should<'a>(
&'a self,
conditions: &'a [Condition],
min_count: usize,
payload_provider: PayloadProvider,
total: usize,
) -> (Vec<OptimizedCondition<'a>>, CardinalityEstimation) {
let mut converted = self.convert_conditions(conditions, payload_provider, total);
// More probable conditions first if min_count < number of conditions
if min_count < conditions.len() / 2 {
converted.sort_by_key(|(_, estimation)| Reverse(estimation.exp));
} else {
// Less probable conditions first
converted.sort_by_key(|(_, estimation)| estimation.exp);
}
let (conditions, estimations): (Vec<_>, Vec<_>) = converted.into_iter().unzip();
(
conditions,
combine_min_should_estimations(&estimations, min_count, total),
)
}
fn optimize_must<'a>(
&'a self,
conditions: &'a [Condition],
payload_provider: PayloadProvider,
total: usize,
) -> (Vec<OptimizedCondition<'a>>, CardinalityEstimation) {
let mut converted = self.convert_conditions(conditions, payload_provider, total);
// Less probable conditions first
converted.sort_by_key(|(_, estimation)| estimation.exp);
let (conditions, estimations): (Vec<_>, Vec<_>) = converted.into_iter().unzip();
(conditions, combine_must_estimations(&estimations, total))
}
fn optimize_must_not<'a>(
&'a self,
conditions: &'a [Condition],
payload_provider: PayloadProvider,
total: usize,
) -> (Vec<OptimizedCondition<'a>>, CardinalityEstimation) {
let mut converted = self.convert_conditions(conditions, payload_provider, total);
// More probable conditions first, as it will be reverted
converted.sort_by_key(|(_, estimation)| estimation.exp);
let (conditions, estimations): (Vec<_>, Vec<_>) = converted.into_iter().unzip();
(
conditions,
combine_must_estimations(
&estimations
.into_iter()
.map(|estimation| invert_estimation(&estimation, total))
.collect_vec(),
total,
),
)
}
}
|