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

Method cardinality

datafusion/expr-common/src/interval_arithmetic.rs:908–948  ·  view source on GitHub ↗

Returns the cardinality of this interval, which is the number of all distinct points inside it. This function returns `None` if: - The interval is unbounded from either side, or - Cardinality calculations for the datatype in question is not implemented yet, or - An overflow occurs during the calculation: This case can only arise when the calculated cardinality does not fit in an `u64`.

(&self)

Source from the content-addressed store, hash-verified

906 /// - An overflow occurs during the calculation: This case can only arise
907 /// when the calculated cardinality does not fit in an `u64`.
908 pub fn cardinality(&self) -> Option<u64> {
909 let data_type = self.data_type();
910 if data_type.is_integer()
911 || matches!(
912 data_type,
913 DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)
914 )
915 {
916 self.upper.distance(&self.lower).map(|diff| diff as u64)
917 } else if data_type.is_floating() {
918 // Negative numbers are sorted in the reverse order. To
919 // always have a positive difference after the subtraction,
920 // we perform following transformation:
921 match (&self.lower, &self.upper) {
922 // Exploit IEEE 754 ordering properties to calculate the correct
923 // cardinality in all cases (including subnormals).
924 (
925 ScalarValue::Float32(Some(lower)),
926 ScalarValue::Float32(Some(upper)),
927 ) => {
928 let lower_bits = map_floating_point_order!(lower.to_bits(), u32);
929 let upper_bits = map_floating_point_order!(upper.to_bits(), u32);
930 Some((upper_bits - lower_bits) as u64)
931 }
932 (
933 ScalarValue::Float64(Some(lower)),
934 ScalarValue::Float64(Some(upper)),
935 ) => {
936 let lower_bits = map_floating_point_order!(lower.to_bits(), u64);
937 let upper_bits = map_floating_point_order!(upper.to_bits(), u64);
938 let count = upper_bits - lower_bits;
939 (count != u64::MAX).then_some(count)
940 }
941 _ => None,
942 }
943 } else {
944 // Cardinality calculations are not implemented for this data type yet:
945 None
946 }
947 .map(|result| result + 1)
948 }
949
950 /// Reflects an [`Interval`] around the point zero.
951 ///

Callers 3

cardinality_ratioFunction · 0.80
max_distinct_countFunction · 0.80

Calls 4

is_integerMethod · 0.80
distanceMethod · 0.80
data_typeMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected