isEqual
TypeScript only
rel1.isEqual(rel2)Problem
Section titled “Problem”Check if two relations contain exactly the same tuples.
Example: I want to verify that my query transformation produces the same result as the expected output.
Description
Section titled “Description”The isEqual operator compares two relations for set equality. Two relations are equal if and only if they contain exactly the same tuples (regardless of order).
This is useful for:
- Testing query correctness
- Validating data transformations
- Implementing relational assertions
Requirements
Section titled “Requirements”Both relations should have the same heading (same attribute names) for a meaningful comparison.
Examples
Section titled “Examples”Equal relations
Section titled “Equal relations”const r1 = Bmg([ { a: 1, b: 2 }, { a: 3, b: 4 },])
const r2 = Bmg([ { a: 3, b: 4 }, // Different order { a: 1, b: 2 },])
r1.isEqual(r2)// => true (order doesn't matter)Unequal relations
Section titled “Unequal relations”const r1 = Bmg([ { a: 1, b: 2 }, { a: 3, b: 4 },])
const r2 = Bmg([ { a: 1, b: 2 }, { a: 5, b: 6 }, // Different tuple])
r1.isEqual(r2)// => falseTesting query results
Section titled “Testing query results”const expected = Bmg([ { sid: "S2", name: "Jones" }, { sid: "S3", name: "Blake" },])
const actual = suppliers .restrict({ city: "Paris" }) .project(['sid', 'name'])
if (!actual.isEqual(expected)) { throw new Error("Query result doesn't match expected")}Relational semantics
Section titled “Relational semantics”Because relations are sets:
- Order of tuples doesn’t matter
- Duplicate tuples are automatically removed
- Only the presence/absence of each unique tuple matters
// These are equal (duplicates are eliminated)const r1 = Bmg([{ a: 1 }, { a: 1 }, { a: 2 }])const r2 = Bmg([{ a: 1 }, { a: 2 }])
r1.isEqual(r2)// => true