mod composite;
use super::{
CollectionBoundary, CollectionOrdering, NullPlacement, OrderCapability, OrderPartition,
ShadowBoundary, ShadowOrdering, TermRepetition, Unpushable, compare_collections, partition,
};
use crate::{FieldName, OrderDirection, OrderTerm, TermName, TermSubject};
struct Everything;
impl OrderCapability for Everything {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Lexicographic;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Bindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
struct Keyed;
impl OrderCapability for Keyed {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Unrepresentable;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Divergent;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Unbindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Divergent;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
struct Placeless;
impl OrderCapability for Placeless {
const NULLS: NullPlacement = NullPlacement::Unreachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Lexicographic;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Bindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
struct OtherRule;
impl OrderCapability for OtherRule {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Divergent;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Unbindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
struct Boundaryless;
impl OrderCapability for Boundaryless {
const NULLS: NullPlacement = NullPlacement::Reachable;
const REPEATED_TERMS: TermRepetition = TermRepetition::Admitted;
const COLLECTIONS: CollectionOrdering = CollectionOrdering::Lexicographic;
const COLLECTION_BOUNDARY: CollectionBoundary = CollectionBoundary::Unbindable;
const SHADOWS: ShadowOrdering = ShadowOrdering::Bytewise;
const SHADOW_BOUNDARY: ShadowBoundary = ShadowBoundary::Bindable;
}
fn array(name: &'static str, direction: OrderDirection) -> OrderTerm {
OrderTerm::array(TermName::new(name), FieldName::new(name), direction)
}
fn edge(name: &'static str, direction: OrderDirection) -> OrderTerm {
OrderTerm::edge(TermName::new(name), FieldName::new(name), direction)
}
fn term(name: &'static str, direction: OrderDirection) -> OrderTerm {
OrderTerm::new(TermName::new(name), FieldName::new(name), direction)
}
fn nullable(name: &'static str, direction: OrderDirection) -> OrderTerm {
OrderTerm::nullable(TermName::new(name), FieldName::new(name), direction)
}
fn plain() -> [OrderTerm; 3] {
[
term("a", OrderDirection::Asc),
term("b", OrderDirection::Desc),
term("id", OrderDirection::Asc),
]
}
#[test]
fn an_order_a_backend_can_express_is_whole() {
let terms = [
nullable("a", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
let partition = partition::<Everything>(&terms);
assert_eq!(partition, OrderPartition::Whole(&terms));
assert!(partition.is_whole());
assert_eq!(partition.pushable(), &terms);
assert!(partition.remainder().is_empty());
assert_eq!(partition.cause(), None);
}
#[test]
fn a_repeated_subject_splits_only_where_the_statement_is_keyed() {
let terms = [
term("a", OrderDirection::Asc),
term("a", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
assert_eq!(
partition::<Everything>(&terms),
OrderPartition::Whole(&terms)
);
assert_eq!(
partition::<Keyed>(&terms),
OrderPartition::Split {
pushable: &terms[..1],
remainder: &terms[1..],
cause: Unpushable::Repetition,
}
);
}
#[test]
fn a_nullable_term_splits_where_the_position_is_unreachable() {
let plain = plain();
assert_eq!(
partition::<Placeless>(&plain),
OrderPartition::Whole(&plain)
);
let terms = [
term("a", OrderDirection::Asc),
nullable("b", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
assert_eq!(
partition::<Placeless>(&terms),
OrderPartition::Split {
pushable: &terms[..1],
remainder: &terms[1..],
cause: Unpushable::NullPosition,
}
);
}
#[test]
fn the_split_takes_every_term_after_the_refused_one() {
let terms = [
term("a", OrderDirection::Asc),
term("a", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
let split = partition::<Keyed>(&terms);
assert_eq!(split.remainder().len(), 2);
assert_eq!(split.remainder()[1], terms[2]);
assert_eq!(
partition::<Keyed>(&terms[2..]),
OrderPartition::Whole(&terms[2..]),
"the tail is takeable on its own, and is still the caller's"
);
}
#[test]
fn a_refusal_at_the_first_term_leaves_nothing_for_the_statement() {
let terms = [nullable("a", OrderDirection::Asc)];
let partition = partition::<Placeless>(&terms);
assert!(partition.pushable().is_empty());
assert_eq!(partition.remainder(), &terms);
}
#[test]
fn an_empty_order_is_whole() {
assert_eq!(partition::<Keyed>(&[]), OrderPartition::Whole(&[]));
}
#[test]
fn a_cursord_query_with_a_remainder_is_refused() {
let terms = [
term("a", OrderDirection::Asc),
term("a", OrderDirection::Desc),
];
let refusal = partition::<Keyed>(&terms)
.paging::<Keyed>()
.expect_err("this order is not one `$sort` document");
assert_eq!(refusal.term(), TermName::new("a"));
assert_eq!(refusal.cause(), Unpushable::Repetition);
assert_eq!(
refusal.to_string(),
"a page's order lives entirely in the statement and `a` cannot be put in one: this backend's \
ordering holds one entry per subject and this one is named twice"
);
}
#[test]
fn a_whole_order_pages() {
let terms = plain();
assert_eq!(partition::<Keyed>(&terms).paging::<Keyed>(), Ok(&terms[..]));
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Row {
a: i64,
b: Option<i64>,
id: i64,
}
fn value(row: &Row, term: &OrderTerm) -> Option<i64> {
match term.column().as_ref() {
"a" => Some(row.a),
"b" => row.b,
_ => Some(row.id),
}
}
fn ids(rows: &[Row]) -> Vec<i64> {
rows.iter().map(|row| row.id).collect()
}
#[test]
fn the_remainder_applied_in_memory_produces_the_written_order() {
let terms = plain();
let partition = OrderPartition::Split {
pushable: &terms[..1],
remainder: &terms[1..],
cause: Unpushable::Repetition,
};
let mut rows = vec![
Row {
a: 1,
b: Some(5),
id: 3,
},
Row {
a: 1,
b: Some(9),
id: 1,
},
Row {
a: 1,
b: Some(5),
id: 2,
},
Row {
a: 2,
b: Some(1),
id: 4,
},
];
partition.apply(&mut rows, value);
assert_eq!(ids(&rows), [1, 2, 3, 4]);
}
#[test]
fn a_remainder_that_reopens_the_prefixs_column_does_not_take_over_the_order() {
let terms = [
term("a", OrderDirection::Asc),
term("a", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
let partition = OrderPartition::Split {
pushable: &terms[..1],
remainder: &terms[1..],
cause: Unpushable::Repetition,
};
let mut rows = vec![
Row {
a: 1,
b: None,
id: 3,
},
Row {
a: 0,
b: None,
id: 2,
},
Row {
a: 0,
b: None,
id: 1,
},
];
partition.apply(&mut rows, value);
assert_eq!(ids(&rows), [1, 2, 3]);
}
#[test]
fn the_completion_keeps_the_statements_own_sequence_where_the_remainder_ties() {
let terms = [
term("a", OrderDirection::Asc),
term("b", OrderDirection::Asc),
];
let partition = OrderPartition::Split {
pushable: &terms[..1],
remainder: &terms[1..],
cause: Unpushable::Repetition,
};
let mut rows: Vec<Row> = (1..=8)
.map(|id| Row {
a: 1,
b: Some(0),
id,
})
.collect();
partition.apply(&mut rows, value);
assert_eq!(ids(&rows), [1, 2, 3, 4, 5, 6, 7, 8]);
}
#[test]
fn a_missing_value_sorts_below_every_value_and_inverts_with_the_term() {
let ascending = [nullable("b", OrderDirection::Asc)];
let descending = [nullable("b", OrderDirection::Desc)];
let corpus = || {
vec![
Row {
a: 0,
b: Some(7),
id: 1,
},
Row {
a: 0,
b: None,
id: 2,
},
Row {
a: 0,
b: Some(3),
id: 3,
},
]
};
let mut rows = corpus();
OrderPartition::Split {
pushable: &[],
remainder: &ascending,
cause: Unpushable::NullPosition,
}
.apply(&mut rows, value);
assert_eq!(ids(&rows), [2, 3, 1]);
let mut rows = corpus();
OrderPartition::Split {
pushable: &[],
remainder: &descending,
cause: Unpushable::NullPosition,
}
.apply(&mut rows, value);
assert_eq!(ids(&rows), [1, 3, 2]);
}
#[test]
fn a_whole_partition_leaves_the_statements_own_answer_alone() {
let terms = plain();
let mut rows = vec![
Row {
a: 9,
b: None,
id: 1,
},
Row {
a: 1,
b: None,
id: 2,
},
];
OrderPartition::Whole(&terms).apply(&mut rows, value);
assert_eq!(ids(&rows), [1, 2]);
}
#[cfg(all(feature = "postgres", feature = "mongo"))]
#[test]
fn a_repeated_subject_is_postgres_ordering_and_not_mongodb_sorting() {
let terms = [
term("a", OrderDirection::Asc),
term("a", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
assert!(partition::<sqlx::Postgres>(&terms).is_whole());
assert_eq!(
partition::<crate::Mongo>(&terms).cause(),
Some(Unpushable::Repetition)
);
}
#[cfg(all(
feature = "sqlite",
feature = "postgres",
feature = "mysql",
feature = "mongo"
))]
#[test]
fn every_shipped_backend_can_place_a_missing_value() {
use crate::comparison::sql::NullOrdering;
let terms = [
nullable("a", OrderDirection::Desc),
term("id", OrderDirection::Asc),
];
assert!(partition::<sqlx::Sqlite>(&terms).is_whole());
assert!(partition::<sqlx::Postgres>(&terms).is_whole());
assert!(partition::<sqlx::MySql>(&terms).is_whole());
assert!(partition::<crate::Mongo>(&terms).is_whole());
assert_eq!(
NullPlacement::of(NullOrdering::Inexpressible),
NullPlacement::Unreachable,
"the projection is what would carry a fourth dialect's refusal into this frame"
);
}
#[test]
fn the_pin_is_what_a_live_postgres_answered() {
let mut text: Vec<(i32, Vec<&str>)> = vec![
(1, vec!["b"]),
(2, vec![]),
(3, vec!["a", "b"]),
(4, vec!["a"]),
(5, vec!["a", "a", "a"]),
(6, vec!["B"]),
(7, vec!["a", "b", "a"]),
(8, vec!["b", "a"]),
(9, vec![""]),
];
text.sort_by(|left, right| compare_collections(&left.1, &right.1).then(left.0.cmp(&right.0)));
assert_eq!(
text.iter().map(|row| row.0).collect::<Vec<_>>(),
[2, 9, 6, 4, 5, 3, 7, 1, 8],
"the empty array least, a prefix below what extends it, and the first differing element deciding"
);
let mut backwards = text.clone();
backwards
.sort_by(|left, right| compare_collections(&right.1, &left.1).then(left.0.cmp(&right.0)));
assert_eq!(
backwards.iter().map(|row| row.0).collect::<Vec<_>>(),
[8, 1, 7, 3, 5, 4, 6, 9, 2]
);
let mut numbers: Vec<(i32, Vec<i32>)> = vec![
(1, vec![2]),
(2, vec![10]),
(3, vec![1, 9]),
(4, vec![]),
(5, vec![1]),
(6, vec![1, 9, 0]),
(7, vec![2, 0]),
];
numbers.sort_by(|left, right| compare_collections(&left.1, &right.1).then(left.0.cmp(&right.0)));
assert_eq!(
numbers.iter().map(|row| row.0).collect::<Vec<_>>(),
[4, 5, 3, 6, 1, 7, 2],
"`{{10}}` after `{{2}}` is the element's own order and not the stored text's"
);
}
#[test]
fn a_null_element_is_the_one_postgres_rule_the_pin_does_not_hold() {
assert_eq!(
compare_collections([Some(1), None], [Some(1), Some(2)]),
core::cmp::Ordering::Less,
"in memory a missing value is BELOW every value, which PostgreSQL reverses inside an array"
);
}
#[test]
fn a_column_stored_collection_is_pushable_only_where_the_rule_agrees() {
let terms = [
array("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
assert_eq!(
partition::<Everything>(&terms),
OrderPartition::Whole(&terms)
);
assert_eq!(
partition::<OtherRule>(&terms),
OrderPartition::Split {
pushable: &[],
remainder: &terms,
cause: Unpushable::CollectionSemantics,
}
);
}
#[test]
fn an_edge_stored_collection_is_a_remainder_on_every_backend() {
let terms = [
term("a", OrderDirection::Asc),
edge("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
let split = OrderPartition::Split {
pushable: &terms[..1],
remainder: &terms[1..],
cause: Unpushable::CollectionTable,
};
assert_eq!(partition::<Everything>(&terms), split);
assert_eq!(partition::<OtherRule>(&terms), split);
assert_eq!(partition::<Keyed>(&terms), split);
}
#[test]
fn a_cursorless_query_completes_an_edge_stored_order_in_memory() {
#[derive(PartialEq, Eq)]
enum Value {
Scalar(i32),
Collection(Vec<&'static str>),
}
impl Ord for Value {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
match (self, other) {
(Self::Scalar(left), Self::Scalar(right)) => left.cmp(right),
(Self::Collection(left), Self::Collection(right)) => compare_collections(left, right),
(Self::Scalar(_), Self::Collection(_)) => core::cmp::Ordering::Less,
(Self::Collection(_), Self::Scalar(_)) => core::cmp::Ordering::Greater,
}
}
}
impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
let terms = [
term("a", OrderDirection::Asc),
edge("tags", OrderDirection::Asc),
];
let partition = partition::<Everything>(&terms);
assert_eq!(partition.cause(), Some(Unpushable::CollectionTable));
let mut rows: Vec<(u8, i32, Vec<&'static str>)> = vec![
(1, 1, vec!["a", "a"]),
(2, 1, vec![]),
(3, 1, vec!["a"]),
(4, 2, vec!["b"]),
(5, 2, vec!["a"]),
];
partition.apply(&mut rows, |row, term| {
Some(match term.subject() {
TermSubject::Value | TermSubject::Absent(_) => Value::Scalar(row.1),
TermSubject::Array | TermSubject::Edge | TermSubject::Shadow => {
Value::Collection(row.2.clone())
}
})
});
assert_eq!(
rows.iter().map(|row| row.0).collect::<Vec<_>>(),
[2, 3, 1, 5, 4],
"the prefix decides first and the collection breaks its ties, in the pin's order"
);
}
#[test]
fn a_page_over_an_edge_stored_collection_names_the_table() {
let terms = [
edge("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
let refusal = partition::<Everything>(&terms)
.paging::<Everything>()
.expect_err("a remainder is not pageable");
assert_eq!(refusal.term(), TermName::new("tags"));
assert_eq!(refusal.cause(), Unpushable::CollectionTable);
assert!(refusal.to_string().contains("kept in a table of its own"));
}
#[test]
fn a_page_over_a_pushable_collection_resumes_where_the_backend_binds_one() {
let terms = [
array("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
let partition = partition::<Everything>(&terms);
assert!(
partition.is_whole(),
"the statement carries this order whole"
);
assert_eq!(partition.paging::<Everything>(), Ok(&terms[..]));
}
#[test]
fn a_page_over_a_pushable_collection_is_refused_where_no_boundary_binds() {
let terms = [
array("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
let partition = partition::<Boundaryless>(&terms);
assert!(
partition.is_whole(),
"the statement carries this order whole"
);
let refusal = partition
.paging::<Boundaryless>()
.expect_err("this backend has no boundary to bind");
assert_eq!(refusal.term(), TermName::new("tags"));
assert_eq!(refusal.cause(), Unpushable::CollectionCursor);
assert!(refusal.to_string().contains("cannot bind one"));
}
#[test]
fn an_order_of_scalars_still_pages_whole() {
let terms = plain();
assert_eq!(
partition::<Everything>(&terms).paging::<Everything>(),
Ok(&terms[..])
);
}
#[cfg(all(
feature = "sqlite",
feature = "postgres",
feature = "mysql",
feature = "mongo"
))]
#[test]
fn only_postgres_orders_a_collection_the_framework_s_way() {
let terms = [
array("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
assert!(partition::<sqlx::Postgres>(&terms).is_whole());
for cause in [
partition::<sqlx::Sqlite>(&terms).cause(),
partition::<sqlx::MySql>(&terms).cause(),
partition::<crate::Mongo>(&terms).cause(),
] {
assert_eq!(cause, Some(Unpushable::CollectionSemantics));
}
let edged = [
edge("tags", OrderDirection::Asc),
term("id", OrderDirection::Asc),
];
for cause in [
partition::<sqlx::Postgres>(&edged).cause(),
partition::<sqlx::Sqlite>(&edged).cause(),
partition::<sqlx::MySql>(&edged).cause(),
partition::<crate::Mongo>(&edged).cause(),
] {
assert_eq!(cause, Some(Unpushable::CollectionTable));
}
}