metesynnada commented on code in PR #5322: URL: https://github.com/apache/arrow-datafusion/pull/5322#discussion_r1119901310
########## datafusion/physical-expr/src/intervals/interval_aritmetic.rs: ########## @@ -0,0 +1,533 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +//http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Interval arithmetic library + +use std::borrow::Borrow; +use std::fmt; +use std::fmt::{Display, Formatter}; + +use arrow::compute::{cast_with_options, CastOptions}; +use arrow::datatypes::DataType; +use datafusion_common::{DataFusionError, Result, ScalarValue}; +use datafusion_expr::Operator; + +use crate::aggregate::min_max::{max, min}; + +/// This type represents an interval, which is used to calculate reliable +/// bounds for expressions. Currently, we only support addition and +/// subtraction, but more capabilities will be added in the future. +/// Upper/lower bounds having NULL values indicate an unbounded side. For +/// example; [10, 20], [10, ∞], [-∞, 100] and [-∞, ∞] are all valid intervals. +#[derive(Debug, PartialEq, Clone, Eq, Hash)] +pub struct Interval { + pub lower: ScalarValue, + pub upper: ScalarValue, +} + +impl Default for Interval { + fn default() -> Self { + Interval { + lower: ScalarValue::Null, + upper: ScalarValue::Null, + } + } +} + +impl Display for Interval { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "Interval [{}, {}]", self.lower, self.upper) + } +} + +impl Interval { + pub(crate) fn cast_to( + &self, + data_type: &DataType, + cast_options: &CastOptions, + ) -> Result<Interval> { + Ok(Interval { + lower: cast_scalar_value(&self.lower, data_type, cast_options)?, + upper: cast_scalar_value(&self.upper, data_type, cast_options)?, + }) + } + + pub(crate) fn get_datatype(&self) -> DataType { + self.lower.get_datatype() + } + + /// Decide if this interval is certainly greater than, possibly greater than, + /// or can't be greater than `other` by returning [true, true], + /// [false, true] or [false, false] respectively. + pub(crate) fn gt(&self, other: &Interval) -> Interval { Review Comment: Revamped as well, maybe you want to check it again. -- 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]
