use std::ops::Neg;
use crate::interval_arithmetic::Interval;
use arrow::compute::SortOptions;
use arrow::datatypes::DataType;
#[derive(PartialEq, Debug, Clone, Copy, Default)]
pub enum SortProperties {
Ordered(SortOptions),
#[default]
Unordered,
Singleton,
}
impl SortProperties {
pub fn add(&self, rhs: &Self) -> Self {
match (self, rhs) {
(Self::Singleton, _) => *rhs,
(_, Self::Singleton) => *self,
(Self::Ordered(lhs), Self::Ordered(rhs))
if lhs.descending == rhs.descending
&& lhs.nulls_first == rhs.nulls_first =>
{
Self::Ordered(SortOptions {
descending: lhs.descending,
nulls_first: lhs.nulls_first,
})
}
_ => Self::Unordered,
}
}
pub fn sub(&self, rhs: &Self) -> Self {
match (self, rhs) {
(Self::Singleton, Self::Singleton) => Self::Singleton,
(Self::Singleton, Self::Ordered(rhs)) => Self::Ordered(SortOptions {
descending: !rhs.descending,
nulls_first: rhs.nulls_first,
}),
(_, Self::Singleton) => *self,
(Self::Ordered(lhs), Self::Ordered(rhs))
if lhs.descending != rhs.descending
&& lhs.nulls_first == rhs.nulls_first =>
{
Self::Ordered(SortOptions {
descending: lhs.descending,
nulls_first: lhs.nulls_first,
})
}
_ => Self::Unordered,
}
}
pub fn gt_or_gteq(&self, rhs: &Self) -> Self {
match (self, rhs) {
(Self::Singleton, Self::Ordered(rhs)) => Self::Ordered(SortOptions {
descending: !rhs.descending,
nulls_first: rhs.nulls_first,
}),
(_, Self::Singleton) => *self,
(Self::Ordered(lhs), Self::Ordered(rhs))
if lhs.descending != rhs.descending
&& lhs.nulls_first == rhs.nulls_first =>
{
*self
}
_ => Self::Unordered,
}
}
pub fn and(&self, rhs: &Self) -> Self {
self.kleene(rhs, |opt| opt.descending == opt.nulls_first)
}
pub fn or(&self, rhs: &Self) -> Self {
self.kleene(rhs, |opt| opt.descending != opt.nulls_first)
}
fn kleene(&self, rhs: &Self, preserves: fn(&SortOptions) -> bool) -> Self {
match (self, rhs) {
(Self::Singleton, Self::Singleton) => Self::Singleton,
(Self::Ordered(opt), Self::Singleton)
| (Self::Singleton, Self::Ordered(opt))
if preserves(opt) =>
{
Self::Ordered(*opt)
}
(Self::Ordered(lhs), Self::Ordered(rhs)) if lhs == rhs && preserves(lhs) => {
Self::Ordered(*lhs)
}
_ => Self::Unordered,
}
}
}
#[cfg(test)]
mod sort_properties_test {
use super::{SortOptions, SortProperties};
use rstest::rstest;
const fn ordered(descending: bool, nulls_first: bool) -> SortProperties {
SortProperties::Ordered(SortOptions {
descending,
nulls_first,
})
}
const ASC_NF: SortProperties = ordered(false, true);
const ASC_NL: SortProperties = ordered(false, false);
const DESC_NF: SortProperties = ordered(true, true);
const DESC_NL: SortProperties = ordered(true, false);
const UNORDERED: SortProperties = SortProperties::Unordered;
const SINGLETON: SortProperties = SortProperties::Singleton;
type BinOp = fn(&SortProperties, &SortProperties) -> SortProperties;
#[test]
fn ordering_propagation() {
let cases: &[(&str, BinOp, SortProperties, SortProperties, SortProperties)] = &[
(
"add: same direction is preserved",
SortProperties::add,
ASC_NF,
ASC_NF,
ASC_NF,
),
(
"add: nulls_last placement is preserved",
SortProperties::add,
ASC_NL,
ASC_NL,
ASC_NL,
),
(
"add: opposing directions are unordered",
SortProperties::add,
ASC_NF,
DESC_NF,
UNORDERED,
),
(
"add: literal with ordered",
SortProperties::add,
SINGLETON,
ASC_NF,
ASC_NF,
),
(
"add: two literals stay a literal",
SortProperties::add,
SINGLETON,
SINGLETON,
SINGLETON,
),
(
"and: ASC NULLS LAST is preserved",
SortProperties::and,
ASC_NL,
ASC_NL,
ASC_NL,
),
(
"and: DESC NULLS FIRST is preserved",
SortProperties::and,
DESC_NF,
DESC_NF,
DESC_NF,
),
(
"and: ASC NULLS FIRST is unordered",
SortProperties::and,
ASC_NF,
ASC_NF,
UNORDERED,
),
(
"and: DESC NULLS LAST is unordered",
SortProperties::and,
DESC_NL,
DESC_NL,
UNORDERED,
),
(
"and: mixed directions are unordered",
SortProperties::and,
ASC_NL,
DESC_NF,
UNORDERED,
),
(
"and: literal with ASC NULLS LAST",
SortProperties::and,
SINGLETON,
ASC_NL,
ASC_NL,
),
(
"and: literal with ASC NULLS FIRST is unordered",
SortProperties::and,
SINGLETON,
ASC_NF,
UNORDERED,
),
(
"and: two literals stay a literal",
SortProperties::and,
SINGLETON,
SINGLETON,
SINGLETON,
),
(
"or: ASC NULLS FIRST is preserved",
SortProperties::or,
ASC_NF,
ASC_NF,
ASC_NF,
),
(
"or: DESC NULLS LAST is preserved",
SortProperties::or,
DESC_NL,
DESC_NL,
DESC_NL,
),
(
"or: ASC NULLS LAST is unordered",
SortProperties::or,
ASC_NL,
ASC_NL,
UNORDERED,
),
(
"or: DESC NULLS FIRST is unordered",
SortProperties::or,
DESC_NF,
DESC_NF,
UNORDERED,
),
(
"or: mixed directions are unordered",
SortProperties::or,
ASC_NF,
DESC_NL,
UNORDERED,
),
(
"or: literal with ASC NULLS FIRST",
SortProperties::or,
SINGLETON,
ASC_NF,
ASC_NF,
),
(
"or: literal with ASC NULLS LAST is unordered",
SortProperties::or,
SINGLETON,
ASC_NL,
UNORDERED,
),
(
"or: two literals stay a literal",
SortProperties::or,
SINGLETON,
SINGLETON,
SINGLETON,
),
(
"sub: opposing directions are preserved",
SortProperties::sub,
ASC_NF,
DESC_NF,
ASC_NF,
),
(
"sub: result follows the left operand",
SortProperties::sub,
DESC_NF,
ASC_NF,
DESC_NF,
),
(
"sub: nulls_last placement is preserved",
SortProperties::sub,
ASC_NL,
DESC_NL,
ASC_NL,
),
(
"sub: same direction is unordered",
SortProperties::sub,
ASC_NF,
ASC_NF,
UNORDERED,
),
(
"sub: literal minus ordered flips the direction",
SortProperties::sub,
SINGLETON,
ASC_NF,
DESC_NF,
),
(
"sub: ordered minus literal keeps the direction",
SortProperties::sub,
ASC_NF,
SINGLETON,
ASC_NF,
),
(
"sub: two literals stay a literal",
SortProperties::sub,
SINGLETON,
SINGLETON,
SINGLETON,
),
(
"gt_or_gteq: opposing directions are preserved",
SortProperties::gt_or_gteq,
ASC_NF,
DESC_NF,
ASC_NF,
),
(
"gt_or_gteq: result follows the left operand",
SortProperties::gt_or_gteq,
DESC_NF,
ASC_NF,
DESC_NF,
),
(
"gt_or_gteq: nulls_last placement is preserved",
SortProperties::gt_or_gteq,
DESC_NL,
ASC_NL,
DESC_NL,
),
(
"gt_or_gteq: same direction is unordered",
SortProperties::gt_or_gteq,
ASC_NF,
ASC_NF,
UNORDERED,
),
(
"gt_or_gteq: literal on the left flips the direction",
SortProperties::gt_or_gteq,
SINGLETON,
ASC_NF,
DESC_NF,
),
(
"gt_or_gteq: literal on the right keeps the direction",
SortProperties::gt_or_gteq,
ASC_NF,
SINGLETON,
ASC_NF,
),
(
"gt_or_gteq: two literals stay a literal",
SortProperties::gt_or_gteq,
SINGLETON,
SINGLETON,
SINGLETON,
),
];
for &(name, op, lhs, rhs, expected) in cases {
assert_eq!(op(&lhs, &rhs), expected, "case: {name}");
}
for (lhs, rhs) in [
(ASC_NF, DESC_NL),
(ASC_NF, SINGLETON),
(ASC_NL, SINGLETON),
(ASC_NL, DESC_NF),
] {
assert_eq!(lhs.add(&rhs), rhs.add(&lhs), "add is commutative");
assert_eq!(lhs.and(&rhs), rhs.and(&lhs), "and is commutative");
assert_eq!(lhs.or(&rhs), rhs.or(&lhs), "or is commutative");
}
}
#[rstest]
#[case::add("add", SortProperties::add)]
#[case::sub("sub", SortProperties::sub)]
#[case::gt_or_gteq("gt_or_gteq", SortProperties::gt_or_gteq)]
#[case::and("and", SortProperties::and)]
#[case::or("or", SortProperties::or)]
fn conflicting_null_placement_is_never_ordered(
#[values(false, true)] l_descending: bool,
#[values(false, true)] r_descending: bool,
#[values(false, true)] l_nulls_first: bool,
#[case] op_name: &str,
#[case] op: BinOp,
) {
let lhs = ordered(l_descending, l_nulls_first);
let rhs = ordered(r_descending, !l_nulls_first);
assert_eq!(op(&lhs, &rhs), UNORDERED, "{op_name}: {lhs:?} and {rhs:?}");
}
}
impl Neg for SortProperties {
type Output = Self;
fn neg(mut self) -> Self::Output {
if let SortProperties::Ordered(SortOptions { descending, .. }) = &mut self {
*descending = !*descending;
}
self
}
}
#[derive(Debug, Clone)]
pub struct ExprProperties {
pub sort_properties: SortProperties,
pub range: Interval,
pub preserves_lex_ordering: bool,
pub strictly_order_preserving: bool,
}
impl ExprProperties {
pub fn new_unknown() -> Self {
Self {
sort_properties: SortProperties::default(),
range: Interval::make_unbounded(&DataType::Null).unwrap(),
preserves_lex_ordering: false,
strictly_order_preserving: false,
}
}
pub fn with_order(mut self, order: SortProperties) -> Self {
self.sort_properties = order;
self
}
pub fn with_range(mut self, range: Interval) -> Self {
self.range = range;
self
}
pub fn with_preserves_lex_ordering(mut self, preserves_lex_ordering: bool) -> Self {
self.preserves_lex_ordering = preserves_lex_ordering;
self
}
pub fn with_strictly_order_preserving(
mut self,
strictly_order_preserving: bool,
) -> Self {
self.strictly_order_preserving = strictly_order_preserving;
self
}
}