1use std::cmp::Ordering;
21
22use super::binary::{IndexPermutation, QuadSlot, TermReader};
23use super::encoded_term::{EncodedTerm, RDF_TYPE};
24use super::pattern::EncodedTriplePattern;
25use super::str_hash::StrHash;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29pub enum CompareOp {
30 Eq,
32 Ne,
34 Lt,
36 Le,
38 Gt,
40 Ge,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Hash)]
50pub struct SlotComparison {
51 pub slot: QuadSlot,
53 pub op: CompareOp,
55 pub term: EncodedTerm,
57}
58
59#[derive(Debug, Clone)]
66pub struct ComparisonMask {
67 permutation: IndexPermutation,
69 checks: Vec<(usize, Vec<(CompareOp, EncodedTerm)>)>,
73 empty: bool,
75}
76
77impl Default for ComparisonMask {
78 fn default() -> Self {
79 Self {
80 permutation: IndexPermutation::Spog,
81 checks: Vec::new(),
82 empty: false,
83 }
84 }
85}
86
87impl ComparisonMask {
88 pub fn compile(
97 pattern: &EncodedTriplePattern,
98 permutation: IndexPermutation,
99 comparisons: &[SlotComparison],
100 ) -> Self {
101 let order = permutation.slot_order();
102 let mut checks: Vec<(usize, Vec<(CompareOp, EncodedTerm)>)> = Vec::new();
103 let mut empty = false;
104 for comparison in comparisons {
105 if comparison.slot == QuadSlot::Graph {
108 continue;
109 }
110 let Some(index) = order.iter().position(|slot| *slot == comparison.slot) else {
111 if permutation == IndexPermutation::Tsg
114 && comparison.slot == QuadSlot::Predicate
115 && decide(&RDF_TYPE, comparison.op, &comparison.term) == Some(false)
116 {
117 empty = true;
118 break;
119 }
120 continue;
121 };
122 if let Some(bound) = pattern.bound_slot(comparison.slot) {
123 match decide(bound, comparison.op, &comparison.term) {
124 Some(false) => {
125 empty = true;
126 break;
127 }
128 Some(true) | None => continue,
131 }
132 }
133 if let Some((_, ops)) = checks.iter_mut().find(|(slot, _)| *slot == index) {
134 ops.push((comparison.op, comparison.term.clone()));
135 } else {
136 checks.push((index, vec![(comparison.op, comparison.term.clone())]));
137 }
138 }
139 checks.sort_by_key(|(index, _)| *index);
140 Self {
141 permutation,
142 checks,
143 empty,
144 }
145 }
146
147 pub fn is_empty(&self) -> bool {
149 self.empty
150 }
151
152 pub fn is_trivial(&self) -> bool {
154 self.empty || self.checks.is_empty()
155 }
156
157 pub fn keeps(&self, logical: &[u8], seg_starts: &[usize; 4]) -> bool {
165 if self.empty {
166 return false;
167 }
168 if self.checks.is_empty() {
169 return true;
170 }
171 let order_len = self.permutation.slot_order().len();
172 for (index, group) in &self.checks {
173 let end = if index + 1 < order_len {
174 seg_starts[index + 1]
175 } else {
176 logical.len()
177 };
178 let segment = &logical[seg_starts[*index]..end];
179 let mut cursor = segment;
180 let term = match cursor.read_term() {
181 Ok(term) => term,
182 Err(_) => return true,
183 };
184 for (op, target) in group {
185 if decide(&term, *op, target) == Some(false) {
186 return false;
187 }
188 }
189 }
190 true
191 }
192}
193
194pub fn decide_slot_comparison(
200 term: &EncodedTerm,
201 op: CompareOp,
202 target: &EncodedTerm,
203) -> Option<bool> {
204 decide(term, op, target)
205}
206
207#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209enum Truth {
210 True,
211 False,
212 Type,
213 Unknown,
214}
215
216#[derive(Debug, Clone, Copy, PartialEq, Eq)]
219enum Cmp {
220 Ordered(Ordering),
221 Type,
222 Unknown,
223}
224
225fn decide(term: &EncodedTerm, op: CompareOp, target: &EncodedTerm) -> Option<bool> {
227 if term == target
235 && !matches!(
236 term,
237 EncodedTerm::FloatLiteral(_) | EncodedTerm::DoubleLiteral(_)
238 )
239 {
240 return Some(matches!(op, CompareOp::Eq | CompareOp::Le | CompareOp::Ge));
241 }
242 match op {
243 CompareOp::Eq => match term_eq(term, target) {
244 Truth::True => Some(true),
245 Truth::False | Truth::Type => Some(false),
246 Truth::Unknown => None,
247 },
248 CompareOp::Ne => match term_eq(term, target) {
249 Truth::True => Some(false),
250 Truth::False => Some(true),
251 Truth::Type => Some(false),
253 Truth::Unknown => None,
254 },
255 CompareOp::Lt | CompareOp::Le | CompareOp::Gt | CompareOp::Ge => {
256 match term_cmp(term, target) {
257 Cmp::Ordered(ordering) => Some(matches!(
258 (op, ordering),
259 (CompareOp::Lt, Ordering::Less)
260 | (CompareOp::Le, Ordering::Less | Ordering::Equal)
261 | (CompareOp::Gt, Ordering::Greater)
262 | (CompareOp::Ge, Ordering::Greater | Ordering::Equal)
263 )),
264 Cmp::Type => Some(false),
265 Cmp::Unknown => None,
266 }
267 }
268 }
269}
270
271fn is_numeric(term: &EncodedTerm) -> bool {
272 matches!(
273 term,
274 EncodedTerm::IntegerLiteral(_)
275 | EncodedTerm::DecimalLiteral(_)
276 | EncodedTerm::FloatLiteral(_)
277 | EncodedTerm::DoubleLiteral(_)
278 )
279}
280
281fn is_string(term: &EncodedTerm) -> bool {
282 matches!(
283 term,
284 EncodedTerm::SmallStringLiteral(_) | EncodedTerm::BigStringLiteral { .. }
285 )
286}
287
288fn is_lang_string(term: &EncodedTerm) -> bool {
289 matches!(
290 term,
291 EncodedTerm::SmallSmallLangStringLiteral { .. }
292 | EncodedTerm::SmallBigLangStringLiteral { .. }
293 | EncodedTerm::BigSmallLangStringLiteral { .. }
294 | EncodedTerm::BigBigLangStringLiteral { .. }
295 )
296}
297
298fn is_opaque(term: &EncodedTerm) -> bool {
299 matches!(
300 term,
301 EncodedTerm::SmallTypedLiteral { .. } | EncodedTerm::BigTypedLiteral { .. }
302 )
303}
304
305fn is_geometry(term: &EncodedTerm) -> bool {
306 matches!(
307 term,
308 EncodedTerm::LiteralPoint { .. }
309 | EncodedTerm::LiteralGeometry { .. }
310 | EncodedTerm::BlobLiteral { .. }
311 )
312}
313
314fn is_non_literal_term(term: &EncodedTerm) -> bool {
316 matches!(
317 term,
318 EncodedTerm::NamedNode { .. }
319 | EncodedTerm::NumericalBlankNode { .. }
320 | EncodedTerm::SmallBlankNode(_)
321 | EncodedTerm::BigBlankNode { .. }
322 | EncodedTerm::DefaultGraph
323 )
324}
325
326fn string_content_eq(a: &EncodedTerm, b: &EncodedTerm) -> bool {
331 match (a, b) {
332 (EncodedTerm::SmallStringLiteral(a), EncodedTerm::SmallStringLiteral(b)) => a == b,
333 (
334 EncodedTerm::BigStringLiteral { value_id: a },
335 EncodedTerm::BigStringLiteral { value_id: b },
336 ) => a == b,
337 (EncodedTerm::SmallStringLiteral(a), EncodedTerm::BigStringLiteral { value_id: b }) => {
338 &StrHash::new(a.as_str()) == b
339 }
340 (EncodedTerm::BigStringLiteral { value_id: a }, EncodedTerm::SmallStringLiteral(b)) => {
341 a == &StrHash::new(b.as_str())
342 }
343 _ => false,
344 }
345}
346
347fn term_eq(a: &EncodedTerm, b: &EncodedTerm) -> Truth {
358 if is_numeric(a) || is_numeric(b) {
359 if !(is_numeric(a) && is_numeric(b)) {
360 return if is_opaque(a) || is_opaque(b) {
364 Truth::Type
365 } else {
366 Truth::False
367 };
368 }
369 return numeric_eq(a, b);
370 }
371 if is_string(a) || is_string(b) {
372 if is_string(a) && is_string(b) {
373 return truth(string_content_eq(a, b));
374 }
375 return if is_opaque(a) || is_opaque(b) {
378 Truth::Type
379 } else {
380 Truth::False
381 };
382 }
383 if is_lang_string(a) || is_lang_string(b) {
384 return truth(a == b);
387 }
388 if is_opaque(a) || is_opaque(b) {
389 if is_opaque(a) && is_opaque(b) {
392 return Truth::Unknown;
393 }
394 let other = if is_opaque(a) { b } else { a };
406 return if is_non_literal_term(other) || is_geometry(other) {
407 Truth::False
408 } else {
409 Truth::Type
410 };
411 }
412 if is_geometry(a) || is_geometry(b) {
413 return truth(a == b);
414 }
415 match (a, b) {
417 (EncodedTerm::BooleanLiteral(x), EncodedTerm::BooleanLiteral(y)) => truth(x == y),
418 (EncodedTerm::DateTimeLiteral(x), EncodedTerm::DateTimeLiteral(y)) => truth(x == y),
419 (EncodedTerm::TimeLiteral(x), EncodedTerm::TimeLiteral(y)) => truth(x == y),
420 (EncodedTerm::DateLiteral(x), EncodedTerm::DateLiteral(y)) => {
421 match x.partial_cmp(y) {
424 Some(Ordering::Equal) => Truth::True,
425 Some(_) => Truth::False,
426 None => Truth::Type,
427 }
428 }
429 (EncodedTerm::GYearMonthLiteral(x), EncodedTerm::GYearMonthLiteral(y)) => truth(x == y),
430 (EncodedTerm::GYearLiteral(x), EncodedTerm::GYearLiteral(y)) => truth(x == y),
431 (EncodedTerm::GMonthDayLiteral(x), EncodedTerm::GMonthDayLiteral(y)) => truth(x == y),
432 (EncodedTerm::GDayLiteral(x), EncodedTerm::GDayLiteral(y)) => truth(x == y),
433 (EncodedTerm::GMonthLiteral(x), EncodedTerm::GMonthLiteral(y)) => truth(x == y),
434 (
437 EncodedTerm::DurationLiteral(_)
438 | EncodedTerm::YearMonthDurationLiteral(_)
439 | EncodedTerm::DayTimeDurationLiteral(_),
440 EncodedTerm::DurationLiteral(_)
441 | EncodedTerm::YearMonthDurationLiteral(_)
442 | EncodedTerm::DayTimeDurationLiteral(_),
443 ) => Truth::Unknown,
444 (EncodedTerm::NamedNode { .. }, EncodedTerm::NamedNode { .. }) => truth(a == b),
445 (EncodedTerm::NumericalBlankNode { .. }, EncodedTerm::NumericalBlankNode { .. }) => {
446 truth(a == b)
447 }
448 (EncodedTerm::SmallBlankNode(_), EncodedTerm::SmallBlankNode(_)) => truth(a == b),
449 (EncodedTerm::BigBlankNode { .. }, EncodedTerm::BigBlankNode { .. }) => truth(a == b),
450 (EncodedTerm::DefaultGraph, EncodedTerm::DefaultGraph) => Truth::True,
451 (EncodedTerm::BooleanLiteral(_), _)
452 | (_, EncodedTerm::BooleanLiteral(_))
453 | (EncodedTerm::DateTimeLiteral(_), _)
454 | (_, EncodedTerm::DateTimeLiteral(_))
455 | (EncodedTerm::TimeLiteral(_), _)
456 | (_, EncodedTerm::TimeLiteral(_))
457 | (EncodedTerm::DateLiteral(_), _)
458 | (_, EncodedTerm::DateLiteral(_))
459 | (EncodedTerm::GYearMonthLiteral(_), _)
460 | (_, EncodedTerm::GYearMonthLiteral(_))
461 | (EncodedTerm::GYearLiteral(_), _)
462 | (_, EncodedTerm::GYearLiteral(_))
463 | (EncodedTerm::GMonthDayLiteral(_), _)
464 | (_, EncodedTerm::GMonthDayLiteral(_))
465 | (EncodedTerm::GDayLiteral(_), _)
466 | (_, EncodedTerm::GDayLiteral(_))
467 | (EncodedTerm::GMonthLiteral(_), _)
468 | (_, EncodedTerm::GMonthLiteral(_))
469 | (EncodedTerm::DefaultGraph, EncodedTerm::NamedNode { .. })
470 | (EncodedTerm::NamedNode { .. }, EncodedTerm::DefaultGraph)
471 | (EncodedTerm::DefaultGraph, EncodedTerm::NumericalBlankNode { .. })
472 | (EncodedTerm::NumericalBlankNode { .. }, EncodedTerm::DefaultGraph)
473 | (EncodedTerm::DefaultGraph, EncodedTerm::SmallBlankNode(_))
474 | (EncodedTerm::SmallBlankNode(_), EncodedTerm::DefaultGraph)
475 | (EncodedTerm::DefaultGraph, EncodedTerm::BigBlankNode { .. })
476 | (EncodedTerm::BigBlankNode { .. }, EncodedTerm::DefaultGraph) => Truth::False,
477 _ => Truth::Unknown,
478 }
479}
480
481fn truth(value: bool) -> Truth {
482 if value { Truth::True } else { Truth::False }
483}
484
485fn numeric_eq(a: &EncodedTerm, b: &EncodedTerm) -> Truth {
486 let equal = match (a, b) {
488 (EncodedTerm::FloatLiteral(x), EncodedTerm::FloatLiteral(y)) => x == y,
489 (EncodedTerm::FloatLiteral(x), EncodedTerm::DoubleLiteral(y)) => {
490 oxsdatatypes::Double::from(*x) == *y
491 }
492 (EncodedTerm::FloatLiteral(x), EncodedTerm::IntegerLiteral(y)) => {
493 *x == oxsdatatypes::Float::from(*y)
494 }
495 (EncodedTerm::FloatLiteral(x), EncodedTerm::DecimalLiteral(y)) => {
496 *x == oxsdatatypes::Float::from(*y)
497 }
498 (EncodedTerm::DoubleLiteral(x), EncodedTerm::FloatLiteral(y)) => {
499 *x == oxsdatatypes::Double::from(*y)
500 }
501 (EncodedTerm::DoubleLiteral(x), EncodedTerm::DoubleLiteral(y)) => x == y,
502 (EncodedTerm::DoubleLiteral(x), EncodedTerm::IntegerLiteral(y)) => {
503 *x == oxsdatatypes::Double::from(*y)
504 }
505 (EncodedTerm::DoubleLiteral(x), EncodedTerm::DecimalLiteral(y)) => {
506 *x == oxsdatatypes::Double::from(*y)
507 }
508 (EncodedTerm::IntegerLiteral(x), EncodedTerm::FloatLiteral(y)) => {
509 oxsdatatypes::Float::from(*x) == *y
510 }
511 (EncodedTerm::IntegerLiteral(x), EncodedTerm::DoubleLiteral(y)) => {
512 oxsdatatypes::Double::from(*x) == *y
513 }
514 (EncodedTerm::IntegerLiteral(x), EncodedTerm::IntegerLiteral(y)) => x == y,
515 (EncodedTerm::IntegerLiteral(x), EncodedTerm::DecimalLiteral(y)) => {
516 oxsdatatypes::Decimal::from(*x) == *y
517 }
518 (EncodedTerm::DecimalLiteral(x), EncodedTerm::FloatLiteral(y)) => {
519 oxsdatatypes::Float::from(*x) == *y
520 }
521 (EncodedTerm::DecimalLiteral(x), EncodedTerm::DoubleLiteral(y)) => {
522 oxsdatatypes::Double::from(*x) == *y
523 }
524 (EncodedTerm::DecimalLiteral(x), EncodedTerm::IntegerLiteral(y)) => {
525 *x == oxsdatatypes::Decimal::from(*y)
526 }
527 (EncodedTerm::DecimalLiteral(x), EncodedTerm::DecimalLiteral(y)) => x == y,
528 _ => return Truth::Unknown,
529 };
530 truth(equal)
531}
532
533fn term_cmp(a: &EncodedTerm, b: &EncodedTerm) -> Cmp {
534 if is_numeric(a) && is_numeric(b) {
535 return numeric_cmp(a, b);
536 }
537 if is_numeric(a) || is_numeric(b) {
538 return if is_opaque(a) || is_opaque(b) {
539 Cmp::Unknown
540 } else {
541 Cmp::Type
542 };
543 }
544 if is_string(a) && is_string(b) {
545 return match (a, b) {
546 (EncodedTerm::SmallStringLiteral(x), EncodedTerm::SmallStringLiteral(y)) => {
547 match x.as_str().partial_cmp(y.as_str()) {
548 Some(ordering) => Cmp::Ordered(ordering),
549 None => Cmp::Type,
550 }
551 }
552 _ => Cmp::Unknown,
554 };
555 }
556 if is_string(a) || is_string(b) {
557 return if is_opaque(a) || is_opaque(b) {
558 Cmp::Unknown
559 } else {
560 Cmp::Type
561 };
562 }
563 if is_opaque(a) || is_opaque(b) || is_geometry(a) || is_geometry(b) {
564 return Cmp::Unknown;
567 }
568 match (a, b) {
569 (EncodedTerm::DateTimeLiteral(x), EncodedTerm::DateTimeLiteral(y)) => {
570 cmp_opt(x.partial_cmp(y))
571 }
572 (EncodedTerm::DateLiteral(x), EncodedTerm::DateLiteral(y)) => cmp_opt(x.partial_cmp(y)),
573 (EncodedTerm::TimeLiteral(_), _) | (_, EncodedTerm::TimeLiteral(_)) => Cmp::Type,
576 (
577 EncodedTerm::GYearLiteral(_)
578 | EncodedTerm::GYearMonthLiteral(_)
579 | EncodedTerm::GMonthDayLiteral(_)
580 | EncodedTerm::GDayLiteral(_)
581 | EncodedTerm::GMonthLiteral(_)
582 | EncodedTerm::DurationLiteral(_)
583 | EncodedTerm::YearMonthDurationLiteral(_)
584 | EncodedTerm::DayTimeDurationLiteral(_),
585 _,
586 )
587 | (
588 _,
589 EncodedTerm::GYearLiteral(_)
590 | EncodedTerm::GYearMonthLiteral(_)
591 | EncodedTerm::GMonthDayLiteral(_)
592 | EncodedTerm::GDayLiteral(_)
593 | EncodedTerm::GMonthLiteral(_)
594 | EncodedTerm::DurationLiteral(_)
595 | EncodedTerm::YearMonthDurationLiteral(_)
596 | EncodedTerm::DayTimeDurationLiteral(_),
597 ) => Cmp::Unknown,
598 (EncodedTerm::BooleanLiteral(_), _)
600 | (_, EncodedTerm::BooleanLiteral(_))
601 | (EncodedTerm::NamedNode { .. }, _)
602 | (_, EncodedTerm::NamedNode { .. })
603 | (EncodedTerm::NumericalBlankNode { .. }, _)
604 | (_, EncodedTerm::NumericalBlankNode { .. })
605 | (EncodedTerm::SmallBlankNode(_), _)
606 | (_, EncodedTerm::SmallBlankNode(_))
607 | (EncodedTerm::BigBlankNode { .. }, _)
608 | (_, EncodedTerm::BigBlankNode { .. })
609 | (EncodedTerm::DefaultGraph, _)
610 | (_, EncodedTerm::DefaultGraph)
611 | (EncodedTerm::SmallSmallLangStringLiteral { .. }, _)
612 | (EncodedTerm::SmallBigLangStringLiteral { .. }, _)
613 | (EncodedTerm::BigSmallLangStringLiteral { .. }, _)
614 | (EncodedTerm::BigBigLangStringLiteral { .. }, _)
615 | (_, EncodedTerm::SmallSmallLangStringLiteral { .. })
616 | (_, EncodedTerm::SmallBigLangStringLiteral { .. })
617 | (_, EncodedTerm::BigSmallLangStringLiteral { .. })
618 | (_, EncodedTerm::BigBigLangStringLiteral { .. }) => Cmp::Type,
619 _ => Cmp::Unknown,
620 }
621}
622
623fn cmp_opt(ordering: Option<Ordering>) -> Cmp {
624 match ordering {
625 Some(ordering) => Cmp::Ordered(ordering),
626 None => Cmp::Type,
628 }
629}
630
631fn numeric_cmp(a: &EncodedTerm, b: &EncodedTerm) -> Cmp {
632 let ordering = match (a, b) {
634 (EncodedTerm::FloatLiteral(x), EncodedTerm::FloatLiteral(y)) => x.partial_cmp(y),
635 (EncodedTerm::FloatLiteral(x), EncodedTerm::DoubleLiteral(y)) => {
636 oxsdatatypes::Double::from(*x).partial_cmp(y)
637 }
638 (EncodedTerm::FloatLiteral(x), EncodedTerm::IntegerLiteral(y)) => {
639 x.partial_cmp(&oxsdatatypes::Float::from(*y))
640 }
641 (EncodedTerm::FloatLiteral(x), EncodedTerm::DecimalLiteral(y)) => {
642 x.partial_cmp(&oxsdatatypes::Float::from(*y))
643 }
644 (EncodedTerm::DoubleLiteral(x), EncodedTerm::FloatLiteral(y)) => {
645 x.partial_cmp(&oxsdatatypes::Double::from(*y))
646 }
647 (EncodedTerm::DoubleLiteral(x), EncodedTerm::DoubleLiteral(y)) => x.partial_cmp(y),
648 (EncodedTerm::DoubleLiteral(x), EncodedTerm::IntegerLiteral(y)) => {
649 x.partial_cmp(&oxsdatatypes::Double::from(*y))
650 }
651 (EncodedTerm::DoubleLiteral(x), EncodedTerm::DecimalLiteral(y)) => {
652 x.partial_cmp(&oxsdatatypes::Double::from(*y))
653 }
654 (EncodedTerm::IntegerLiteral(x), EncodedTerm::FloatLiteral(y)) => {
655 oxsdatatypes::Float::from(*x).partial_cmp(y)
656 }
657 (EncodedTerm::IntegerLiteral(x), EncodedTerm::DoubleLiteral(y)) => {
658 oxsdatatypes::Double::from(*x).partial_cmp(y)
659 }
660 (EncodedTerm::IntegerLiteral(x), EncodedTerm::IntegerLiteral(y)) => x.partial_cmp(y),
661 (EncodedTerm::IntegerLiteral(x), EncodedTerm::DecimalLiteral(y)) => {
662 oxsdatatypes::Decimal::from(*x).partial_cmp(y)
663 }
664 (EncodedTerm::DecimalLiteral(x), EncodedTerm::FloatLiteral(y)) => {
665 oxsdatatypes::Float::from(*x).partial_cmp(y)
666 }
667 (EncodedTerm::DecimalLiteral(x), EncodedTerm::DoubleLiteral(y)) => {
668 oxsdatatypes::Double::from(*x).partial_cmp(y)
669 }
670 (EncodedTerm::DecimalLiteral(x), EncodedTerm::IntegerLiteral(y)) => {
671 x.partial_cmp(&oxsdatatypes::Decimal::from(*y))
672 }
673 (EncodedTerm::DecimalLiteral(x), EncodedTerm::DecimalLiteral(y)) => x.partial_cmp(y),
674 _ => return Cmp::Unknown,
675 };
676 cmp_opt(ordering)
677}
678
679#[cfg(test)]
680mod tests {
681 use super::*;
682 use crate::Literal;
683 use crate::NamedNode;
684 use crate::encode::encoded_term::EncodedQuad;
685 use crate::encode::pattern::EncodedTriplePattern;
686
687 fn int(value: i64) -> EncodedTerm {
688 EncodedTerm::from(&Literal::from(value))
689 }
690
691 fn lit(value: &str) -> EncodedTerm {
692 EncodedTerm::from(&Literal::new_simple_literal(value))
693 }
694
695 fn comparison(slot: QuadSlot, op: CompareOp, term: EncodedTerm) -> SlotComparison {
696 SlotComparison { slot, op, term }
697 }
698
699 fn decide_term(term: &EncodedTerm, op: CompareOp, target: &EncodedTerm) -> Option<bool> {
704 decide(term, op, target)
705 }
706
707 #[test]
708 fn numeric_range_is_decided() {
709 assert_eq!(
710 decide_term(&int(1967), CompareOp::Ge, &int(1967)),
711 Some(true)
712 );
713 assert_eq!(
714 decide_term(&int(1966), CompareOp::Ge, &int(1967)),
715 Some(false)
716 );
717 assert_eq!(
718 decide_term(&int(1971), CompareOp::Le, &int(1971)),
719 Some(true)
720 );
721 assert_eq!(
722 decide_term(&int(1972), CompareOp::Le, &int(1971)),
723 Some(false)
724 );
725 assert_eq!(
727 decide_term(
728 &EncodedTerm::from(&Literal::from(1967.0f64)),
729 CompareOp::Eq,
730 &int(1967)
731 ),
732 Some(true)
733 );
734 }
735
736 #[test]
742 fn identical_non_float_terms_are_ordered_equal() {
743 let time = EncodedTerm::from(&Literal::new_typed_literal(
744 "13:00:00",
745 NamedNode::new_unchecked("http://www.w3.org/2001/XMLSchema#time"),
746 ));
747 let boolean = EncodedTerm::from(&Literal::from(true));
748 let iri = EncodedTerm::from(&NamedNode::new_unchecked("http://ex/iri"));
749 let bnode = EncodedTerm::from(&crate::BlankNode::new("b").unwrap());
750 let lang = EncodedTerm::from(&Literal::new_language_tagged_literal("x", "en").unwrap());
751 for term in [&time, &boolean, &iri, &bnode, &lang] {
752 assert_eq!(decide_term(term, CompareOp::Le, term), Some(true));
753 assert_eq!(decide_term(term, CompareOp::Ge, term), Some(true));
754 assert_eq!(decide_term(term, CompareOp::Lt, term), Some(false));
755 assert_eq!(decide_term(term, CompareOp::Gt, term), Some(false));
756 assert_eq!(decide_term(term, CompareOp::Eq, term), Some(true));
757 assert_eq!(decide_term(term, CompareOp::Ne, term), Some(false));
758 }
759
760 let positive_zero = EncodedTerm::from(&Literal::from(0.0f64));
763 let negative_zero = EncodedTerm::from(&Literal::from(-0.0f64));
764 assert!(positive_zero != negative_zero);
765 assert_eq!(
766 decide_term(&positive_zero, CompareOp::Le, &negative_zero),
767 Some(true)
768 );
769 assert_eq!(
770 decide_term(&positive_zero, CompareOp::Ge, &negative_zero),
771 Some(true)
772 );
773 let nan = EncodedTerm::from(&Literal::from(f64::NAN));
775 assert_eq!(decide_term(&nan, CompareOp::Le, &nan), Some(false));
776 assert_eq!(decide_term(&nan, CompareOp::Ge, &nan), Some(false));
777 }
778
779 #[test]
780 fn type_mismatch_drops_but_undecidable_keeps() {
781 assert_eq!(decide_term(&lit("x"), CompareOp::Lt, &int(1)), Some(false));
783 let big = lit("aaaaaaaaaaaaaaaa");
785 assert!(matches!(big, EncodedTerm::BigStringLiteral { .. }));
786 assert_eq!(decide_term(&big, CompareOp::Lt, &lit("b")), None);
787 assert_eq!(decide_term(&big, CompareOp::Eq, &big), Some(true));
789 assert_eq!(
790 decide_term(&big, CompareOp::Eq, &lit("bbbbbbbbbbbbbbbb")),
791 Some(false)
792 );
793 assert_eq!(decide_term(&big, CompareOp::Eq, &lit("short")), Some(false));
795 }
796
797 #[test]
798 fn ne_inverts_definite_equality_but_not_type_errors() {
799 assert_eq!(decide_term(&int(1), CompareOp::Ne, &int(2)), Some(true));
800 assert_eq!(decide_term(&int(1), CompareOp::Ne, &int(1)), Some(false));
801 let opaque = EncodedTerm::from(&Literal::new_typed_literal(
803 "x",
804 NamedNode::new_unchecked("http://example.org/things#myType"),
805 ));
806 assert_eq!(decide_term(&opaque, CompareOp::Ne, &int(1)), Some(false));
807 }
808
809 #[test]
813 fn ne_keeps_opaque_typed_vs_iri_and_blank_node() {
814 let opaque = EncodedTerm::from(&Literal::new_typed_literal(
815 "x",
816 NamedNode::new_unchecked("http://example.org/things#myType"),
817 ));
818 let iri = EncodedTerm::from(&NamedNode::new_unchecked("http://ex/iri"));
819 let bnode = EncodedTerm::from(&crate::BlankNode::new("b").unwrap());
820
821 for other in [&iri, &bnode] {
822 assert_eq!(decide_term(&opaque, CompareOp::Ne, other), Some(true));
823 assert_eq!(decide_term(other, CompareOp::Ne, &opaque), Some(true));
824 assert_eq!(decide_term(&opaque, CompareOp::Eq, other), Some(false));
825 assert_eq!(decide_term(other, CompareOp::Eq, &opaque), Some(false));
826 }
827 }
828
829 #[test]
830 fn bound_slot_comparison_folds_to_empty() {
831 let pattern = EncodedTriplePattern {
832 subject: None,
833 predicate: Some(EncodedTerm::from(&NamedNode::new_unchecked("http://ex/p"))),
834 object: None,
835 graph_name: None,
836 };
837 let mask = ComparisonMask::compile(
839 &pattern,
840 IndexPermutation::Posg,
841 &[comparison(
842 QuadSlot::Predicate,
843 CompareOp::Eq,
844 EncodedTerm::from(&NamedNode::new_unchecked("http://ex/q")),
845 )],
846 );
847 assert!(mask.is_empty());
848 }
849
850 #[test]
851 fn empty_mask_is_the_identity() {
852 let pattern = EncodedTriplePattern::default();
853 let mask = ComparisonMask::compile(&pattern, IndexPermutation::Spog, &[]);
854 assert!(mask.is_trivial());
855 assert!(mask.keeps(&[], &[0; 4]));
856 }
857
858 #[test]
859 fn full_key_mask_keeps_only_matching_rows() {
860 use crate::encode::binary::{IndexPermutation, encode_index_key};
861
862 let make = |year: i64| {
863 EncodedQuad::new(
864 EncodedTerm::from(&NamedNode::new_unchecked("http://ex/s")),
865 EncodedTerm::from(&NamedNode::new_unchecked("http://ex/year")),
866 int(year),
867 EncodedTerm::DefaultGraph,
868 )
869 };
870 let pattern = EncodedTriplePattern {
871 subject: None,
872 predicate: Some(EncodedTerm::from(&NamedNode::new_unchecked(
873 "http://ex/year",
874 ))),
875 object: None,
876 graph_name: None,
877 };
878 let mask = ComparisonMask::compile(
879 &pattern,
880 IndexPermutation::Posg,
881 &[comparison(QuadSlot::Object, CompareOp::Ge, int(1967))],
882 );
883 for (year, expected) in [(1966, false), (1967, true), (1971, true), (1972, true)] {
884 let mut key = encode_index_key(IndexPermutation::Posg, &make(year));
885 key.extend_from_slice(&[0u8; 8]);
888 let row = crate::encode::key_matcher::KeyRow::from_versioned_key(
889 &key,
890 IndexPermutation::Posg,
891 )
892 .expect("key owned by encode_index_key");
893 assert_eq!(
894 mask.keeps(row.logical(), &row.seg_starts),
895 expected,
896 "year {year}"
897 );
898 }
899 }
900}