MCPcopy Create free account
hub / github.com/apache/datafusion / calculate_selectivity

Function calculate_selectivity

datafusion/physical-expr/src/analysis.rs:304–343  ·  view source on GitHub ↗

This function calculates the filter predicate's selectivity by comparing the initial and pruned column boundaries. Selectivity is defined as the ratio of rows in a table that satisfy the filter's predicate.

(
    target_boundaries: &[ExprBoundaries],
    initial_boundaries: &[ExprBoundaries],
)

Source from the content-addressed store, hash-verified

302/// the initial and pruned column boundaries. Selectivity is defined as the
303/// ratio of rows in a table that satisfy the filter's predicate.
304fn calculate_selectivity(
305 target_boundaries: &[ExprBoundaries],
306 initial_boundaries: &[ExprBoundaries],
307) -> Result<f64> {
308 // Since the intervals are assumed uniform and the values
309 // are not correlated, we need to multiply the selectivities
310 // of multiple columns to get the overall selectivity.
311 if target_boundaries.len() != initial_boundaries.len() {
312 return Err(internal_datafusion_err!(
313 "The number of columns in the initial and target boundaries should be the same"
314 ));
315 }
316 let mut acc: f64 = 1.0;
317 for (initial, target) in initial_boundaries.iter().zip(target_boundaries) {
318 match (initial.interval.as_ref(), target.interval.as_ref()) {
319 (Some(initial_interval), Some(target_interval)) => {
320 if let Precision::Exact(distinct_count)
321 | Precision::Inexact(distinct_count) = target.distinct_count
322 && let Some(s) = singleton_selectivity(
323 initial_interval,
324 target_interval,
325 distinct_count,
326 )
327 {
328 acc *= s;
329 continue;
330 }
331 acc *= cardinality_ratio(initial_interval, target_interval);
332 }
333 (None, Some(_)) => {
334 return internal_err!(
335 "Initial boundary cannot be None while having a Some() target boundary"
336 );
337 }
338 _ => return Ok(0.0),
339 }
340 }
341
342 Ok(acc)
343}
344
345#[cfg(test)]
346mod tests {

Calls 5

singleton_selectivityFunction · 0.85
cardinality_ratioFunction · 0.85
lenMethod · 0.45
iterMethod · 0.45
as_refMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…