ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use super::SqlFlagsComparison;
use crate::FlagsFilter;

/// The crossing keeps the operator set exactly as it stands — seven slots in, seven out.
#[test]
fn a_crossing_carries_every_slot_across() {
  let filter = FlagsFilter::new()
    .with_eq(1u32)
    .with_has_all(2u32)
    .with_has_any(4u32)
    .with_has_none(8u32)
    .with_in_([1u32, 2]);

  let crossed = SqlFlagsComparison::<u32, ()>::try_from_filter_with(filter, Ok::<_, ()>).unwrap();

  assert_eq!(crossed.eq(), Some(&1));
  assert_eq!(crossed.has_all(), Some(&2));
  assert_eq!(crossed.has_any(), Some(&4));
  assert_eq!(crossed.has_none(), Some(&8));
  assert_eq!(crossed.in_().unwrap(), [1, 2]);
  assert_eq!(crossed.conjuncts(), 5);
}

#[cfg(feature = "sqlite")]
mod sqlite {
  use core::{cell::RefCell, convert::Infallible};

  use super::SqlFlagsComparison;
  use crate::{
    FlagsFilter,
    comparison::{
      TryFromFilter,
      sql::tests::{Probe, render, threaded},
    },
  };

  /// **THE MASK ALGEBRA, rendered.** `hasAll` compares the masked column against the mask itself;
  /// `hasNone` compares it against zero; `hasAny` is that negated — this family's one spelling of
  /// negation, so the positive and the negative differ only in the wrap.
  ///
  /// The PARENTHESIS around `col & ?` is the load-bearing character: `&` binds looser than `=` in
  /// MySQL and tighter in PostgreSQL, so an unparenthesised subject would be two different questions
  /// in two dialects.
  #[test]
  fn each_mask_word_renders_one_bitwise_shape() {
    for (word, filter, sql) in [
      (
        "hasAll",
        FlagsFilter::new().with_has_all(3i64),
        "(\"k\" & ?) = ?",
      ),
      (
        "hasAny",
        FlagsFilter::new().with_has_any(3i64),
        "NOT ( (\"k\" & ?) = 0 )",
      ),
      (
        "hasNone",
        FlagsFilter::new().with_has_none(3i64),
        "(\"k\" & ?) = 0",
      ),
    ] {
      let crossed = SqlFlagsComparison::<i64, sqlx::Sqlite>::try_from_filter(filter).unwrap();
      assert_eq!(render::<sqlx::Sqlite, _>("k", &crossed).0, sql, "{word}");
    }
  }

  /// **`hasAll` binds its mask TWICE**, which is the whole difference between it and the other two:
  /// the mask is both the operand and the answer it is compared against.
  #[test]
  fn the_masks_are_bound_once_each_and_twice_for_has_all() {
    for (word, filter, binds) in [
      ("hasAll", FlagsFilter::new().with_has_all(3i64), vec![3, 3]),
      ("hasAny", FlagsFilter::new().with_has_any(3i64), vec![3]),
      ("hasNone", FlagsFilter::new().with_has_none(3i64), vec![3]),
    ] {
      let bound = RefCell::new(Vec::new());
      let crossed =
        SqlFlagsComparison::<Probe<'_>, sqlx::Sqlite>::try_from_filter_with(filter, |value| {
          Ok::<_, Infallible>(Probe {
            value,
            bound: &bound,
          })
        })
        .unwrap();

      threaded::<sqlx::Sqlite, _>("k", &crossed);
      assert_eq!(*bound.borrow(), binds, "{word}");
    }
  }

  /// The seven words together, in slot order, joined by the accumulator's ` AND ` — the whole
  /// lowering a document's worth of conjoined words produces.
  #[test]
  fn the_seven_words_lower_as_one_conjunction() {
    let crossed = SqlFlagsComparison::<i64, sqlx::Sqlite>::try_from_filter(
      FlagsFilter::new()
        .with_eq(1i64)
        .with_ne(2i64)
        .with_in_([3i64, 4])
        .with_not_in([5i64])
        .with_has_all(6i64)
        .with_has_any(7i64)
        .with_has_none(8i64),
    )
    .unwrap();

    assert_eq!(crossed.conjuncts(), 7);
    assert_eq!(
      threaded::<sqlx::Sqlite, _>("k", &crossed),
      "SELECT \"id\" FROM \"t\" WHERE \"k\" = ? AND NOT ( \"k\" = ? ) AND \"k\" IN (?, ?) \
       AND NOT ( \"k\" IN (?) ) AND (\"k\" & ?) = ? AND NOT ( (\"k\" & ?) = 0 ) \
       AND (\"k\" & ?) = 0"
    );
  }

  /// **`hasAll` is not a rewrite of the other two**, and the vacuous mask is why: at `m = ∅` the
  /// three questions answer TRUE, FALSE and TRUE, which no single rewrite reproduces. The SQL says
  /// so — `(col & 0) = 0` is true of every row and its negation of none.
  #[test]
  fn the_empty_mask_renders_the_three_vacuous_answers() {
    for (word, filter, sql) in [
      (
        "hasAll(∅) — true of every row",
        FlagsFilter::new().with_has_all(0i64),
        "(\"k\" & ?) = ?",
      ),
      (
        "hasAny(∅) — true of none",
        FlagsFilter::new().with_has_any(0i64),
        "NOT ( (\"k\" & ?) = 0 )",
      ),
      (
        "hasNone(∅) — true of every row",
        FlagsFilter::new().with_has_none(0i64),
        "(\"k\" & ?) = 0",
      ),
    ] {
      let crossed = SqlFlagsComparison::<i64, sqlx::Sqlite>::try_from_filter(filter).unwrap();
      assert_eq!(render::<sqlx::Sqlite, _>("k", &crossed).0, sql, "{word}");
    }
  }
}