alamb commented on code in PR #10117: URL: https://github.com/apache/datafusion/pull/10117#discussion_r1592934687
########## datafusion/expr/src/signature.rs: ########## @@ -346,13 +346,61 @@ impl Signature { } } -/// Monotonicity of the `ScalarFunctionExpr` with respect to its arguments. -/// Each element of this vector corresponds to an argument and indicates whether -/// the function's behavior is monotonic, or non-monotonic/unknown for that argument, namely: -/// - `None` signifies unknown monotonicity or non-monotonicity. -/// - `Some(true)` indicates that the function is monotonically increasing w.r.t. the argument in question. -/// - Some(false) indicates that the function is monotonically decreasing w.r.t. the argument in question. -pub type FuncMonotonicity = Vec<Option<bool>>; +#[derive(Debug, Clone)] +enum FuncMonotonicityPartial { + /// not monotonic or unknown monotonicity + None, + /// Increasing with respect to all of its arguments + Increasing, + /// Decreasing with respect to all of its arguments + Decreasing, + /// Each element of this vector corresponds to an argument and indicates whether + /// the function's behavior is monotonic, or non-monotonic/unknown for that argument, namely: + /// - `None` signifies unknown monotonicity or non-monotonicity. + /// - `Some(true)` indicates that the function is monotonically increasing w.r.t. the argument in question. + /// - Some(false) indicates that the function is monotonically decreasing w.r.t. the argument in question. + Mixed(Vec<Option<bool>>), +} + +/// Monotonicity of a function with respect to its arguments. +/// +/// A function is [monotonic] if it preserves the relative order of its inputs. +/// +/// [monotonic]: https://en.wikipedia.org/wiki/Monotonic_function +#[derive(Debug, Clone)] +pub struct FuncMonotonicity(FuncMonotonicityPartial); + +impl FuncMonotonicity { + pub fn new_none() -> Self { + Self(FuncMonotonicityPartial::None) + } + pub fn new_increasing() -> Self { + Self(FuncMonotonicityPartial::Increasing) + } + pub fn new_decreasing() -> Self { + Self(FuncMonotonicityPartial::Decreasing) + } + pub fn new_mixed(inner: Vec<Option<bool>>) -> Self { + Self(FuncMonotonicityPartial::Mixed(inner)) + } +} + +impl From<&FuncMonotonicity> for Vec<Option<bool>> { + fn from(val: &FuncMonotonicity) -> Self { + match &val.0 { + FuncMonotonicityPartial::None => vec![None], + FuncMonotonicityPartial::Increasing => vec![Some(true)], + FuncMonotonicityPartial::Decreasing => vec![Some(false)], + FuncMonotonicityPartial::Mixed(inner) => inner.to_vec(), + } + } +} + +impl PartialEq for FuncMonotonicity { + fn eq(&self, other: &Self) -> bool { + Into::<Vec<Option<bool>>>::into(self) == Into::<Vec<Option<bool>>>::into(other) Review Comment: Something like this perhaps: ```rust match (self, other) { (Self::None, None) => true, (Self::Increasing, Self::Increasing) => true, (Self::Decreasing, Self::Decreasing) => true, (Self::Partial(self_inner), Self::Partial(other_inner)) => self_inner == other_inner, _ => false } ``` ########## datafusion/expr/src/signature.rs: ########## @@ -346,13 +346,61 @@ impl Signature { } } -/// Monotonicity of the `ScalarFunctionExpr` with respect to its arguments. -/// Each element of this vector corresponds to an argument and indicates whether -/// the function's behavior is monotonic, or non-monotonic/unknown for that argument, namely: -/// - `None` signifies unknown monotonicity or non-monotonicity. -/// - `Some(true)` indicates that the function is monotonically increasing w.r.t. the argument in question. -/// - Some(false) indicates that the function is monotonically decreasing w.r.t. the argument in question. -pub type FuncMonotonicity = Vec<Option<bool>>; +#[derive(Debug, Clone)] +enum FuncMonotonicityPartial { + /// not monotonic or unknown monotonicity + None, + /// Increasing with respect to all of its arguments + Increasing, + /// Decreasing with respect to all of its arguments + Decreasing, + /// Each element of this vector corresponds to an argument and indicates whether + /// the function's behavior is monotonic, or non-monotonic/unknown for that argument, namely: + /// - `None` signifies unknown monotonicity or non-monotonicity. + /// - `Some(true)` indicates that the function is monotonically increasing w.r.t. the argument in question. + /// - Some(false) indicates that the function is monotonically decreasing w.r.t. the argument in question. + Mixed(Vec<Option<bool>>), +} + +/// Monotonicity of a function with respect to its arguments. +/// +/// A function is [monotonic] if it preserves the relative order of its inputs. +/// +/// [monotonic]: https://en.wikipedia.org/wiki/Monotonic_function +#[derive(Debug, Clone)] +pub struct FuncMonotonicity(FuncMonotonicityPartial); + +impl FuncMonotonicity { + pub fn new_none() -> Self { + Self(FuncMonotonicityPartial::None) + } + pub fn new_increasing() -> Self { + Self(FuncMonotonicityPartial::Increasing) + } + pub fn new_decreasing() -> Self { + Self(FuncMonotonicityPartial::Decreasing) + } + pub fn new_mixed(inner: Vec<Option<bool>>) -> Self { + Self(FuncMonotonicityPartial::Mixed(inner)) + } +} + +impl From<&FuncMonotonicity> for Vec<Option<bool>> { + fn from(val: &FuncMonotonicity) -> Self { + match &val.0 { + FuncMonotonicityPartial::None => vec![None], + FuncMonotonicityPartial::Increasing => vec![Some(true)], + FuncMonotonicityPartial::Decreasing => vec![Some(false)], + FuncMonotonicityPartial::Mixed(inner) => inner.to_vec(), + } + } +} Review Comment: I agree with @ozankabak it is confusing to have a way to convert back to `Vec<Option<bool>>` -- I think it would be easier to understand if all comparisons are done directly on `FuncMonotonicity` rather than converting to Vec<Option<Bool>> -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org