Dandandan commented on code in PR #6982:
URL: https://github.com/apache/arrow-datafusion/pull/6982#discussion_r1268656901


##########
datafusion/physical-expr/src/intervals/interval_aritmetic.rs:
##########
@@ -451,6 +452,93 @@ impl Interval {
         lower: IntervalBound::new(ScalarValue::Boolean(Some(true)), false),
         upper: IntervalBound::new(ScalarValue::Boolean(Some(true)), false),
     };
+
+    // Cardinality is the number of all points included by the interval, 
considering its bounds.
+    pub fn cardinality(&self) -> Result<u64> {
+        match self.get_datatype() {
+            Ok(data_type) if data_type.is_integer() => {
+                if let Some(diff) = 
self.upper.value.distance(&self.lower.value) {
+                    Ok(calculate_cardinality_based_on_bounds(
+                        self.lower.open,
+                        self.upper.open,
+                        diff as u64,
+                    ))
+                } else {
+                    Err(DataFusionError::Execution(format!(
+                        "Cardinality cannot be calculated for {:?}",
+                        self
+                    )))
+                }
+            }
+            // Since the floating-point numbers are ordered in the same order 
as their binary representation,
+            // we can consider their binary representations as "indices" and 
subtract them.
+            // 
https://stackoverflow.com/questions/8875064/how-many-distinct-floating-point-numbers-in-a-specific-range
+            Ok(data_type) if data_type.is_floating() => {
+                // If the minimum value is a negative number, we need to
+                // switch sides to ensure an unsigned result.
+                let (min, max) = if self.lower.value
+                    < ScalarValue::new_zero(&self.lower.value.get_datatype())?
+                {
+                    (self.upper.value.clone(), self.lower.value.clone())
+                } else {
+                    (self.lower.value.clone(), self.upper.value.clone())
+                };
+
+                match (min, max) {
+                    (
+                        ScalarValue::Float32(Some(lower)),
+                        ScalarValue::Float32(Some(upper)),
+                    ) => Ok(calculate_cardinality_based_on_bounds(
+                        self.lower.open,
+                        self.upper.open,
+                        (upper.to_bits().sub_checked(lower.to_bits()))? as u64,
+                    )),
+                    (
+                        ScalarValue::Float64(Some(lower)),
+                        ScalarValue::Float64(Some(upper)),
+                    ) => Ok(calculate_cardinality_based_on_bounds(
+                        self.lower.open,
+                        self.upper.open,
+                        upper.to_bits().sub_checked(lower.to_bits())?,
+                    )),
+                    _ => Err(DataFusionError::Execution(format!(
+                        "Cardinality cannot be calculated for the datatype 
{:?}",
+                        data_type
+                    ))),
+                }
+            }
+            // If the cardinality cannot be calculated anyway, give an error.
+            _ => Err(DataFusionError::Execution(format!(
+                "Cardinality cannot be calculated for {:?}",
+                self
+            ))),
+        }
+    }
+
+    /// The interval has any open bound(s), the function converts
+    /// them to closed bound(s) preserving the interval endpoints.
+    pub fn interval_with_closed_bounds(mut self) -> Interval {
+        if self.lower.open {
+            // Get next value
+            self.lower.value = self.lower.value.next_value::<true>();

Review Comment:
   maybe in this case `next_value_add` and `next_value_sub` would maybe be more 
clear than adding use of generics?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to