|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#13166] A row with NO VALUE satisfies a negation-carrying operator — the |
| 5 | + * six cells, on this matcher, stated directly rather than through a `$not`. |
| 6 | + * |
| 7 | + * ## The ruling this file enforces |
| 8 | + * |
| 9 | + * `$ne` / `$nin` / `$notContains` MATCH a row whose field has no value. That is |
| 10 | + * the INCLUDE direction: #5146 made `$not` NULL-safe, #5298 option A extended it |
| 11 | + * to the non-negated negative operators, and the 2026-08-10 reversal that would |
| 12 | + * have taken SQL's three-valued answer as the common denominator was WITHDRAWN |
| 13 | + * the same day once its cross-backend cost had been measured. Ten other surfaces |
| 14 | + * already answer this way — `formula` (`matches-filter-not-null-safe.test.ts`), |
| 15 | + * the four SQL compilers via `nullSafeNegative` / `nullValueSatisfiesOperator`, |
| 16 | + * and `driver-mongodb`, which passes `$nin` through and compiles `$notContains` |
| 17 | + * to `{ $not: { $regex } }`, both of which match a missing or null field. |
| 18 | + * |
| 19 | + * ## Why the table has SIX cells and not three |
| 20 | + * |
| 21 | + * "No value" has two readings that this matcher reaches through DIFFERENT code, |
| 22 | + * so a three-cell table cannot see the difference between them: |
| 23 | + * |
| 24 | + * - `name: null` — the shape a SQL NULL round-trips into a record. |
| 25 | + * - the key absent — the shape a partial write leaves. |
| 26 | + * |
| 27 | + * Collapsing them is how the divergence stayed invisible: two INDEPENDENT causes |
| 28 | + * produced it, each reachable from only one of the two readings, so a fixture |
| 29 | + * carrying one reading measures at most one of them. Keep both columns. |
| 30 | + * |
| 31 | + * ⚠️ Do not collapse this into a single case, and do not add `$exists` to it. |
| 32 | + * `$exists` is the neighbouring cell (#13195): this package's live mingo path |
| 33 | + * and `driver-mongodb` still read it as key-presence rather than has-value, so |
| 34 | + * it is a different, still-open divergence with a different backend list. |
| 35 | + * |
| 36 | + * The complementary POSITIVE operators are asserted beside each negative one, so |
| 37 | + * a matcher that started answering "every row" to everything cannot pass this |
| 38 | + * file: the ruling is that a no-value row joins the negative answer, not that |
| 39 | + * predicates stop discriminating. |
| 40 | + */ |
| 41 | + |
| 42 | +import { describe, it, expect } from 'vitest'; |
| 43 | + |
| 44 | +import { match } from './memory-matcher.js'; |
| 45 | + |
| 46 | +/** `name` present but null — how a SQL NULL round-trips into a record. */ |
| 47 | +const NULLED: Array<Record<string, unknown>> = [ |
| 48 | + { id: '1', name: 'alpha-one' }, |
| 49 | + { id: '2', name: 'beta' }, |
| 50 | + { id: '3', name: null }, |
| 51 | +]; |
| 52 | + |
| 53 | +/** The same rows with `name` ABSENT — the shape a partial write leaves. */ |
| 54 | +const MISSING: Array<Record<string, unknown>> = [ |
| 55 | + { id: '1', name: 'alpha-one' }, |
| 56 | + { id: '2', name: 'beta' }, |
| 57 | + { id: '3' }, |
| 58 | +]; |
| 59 | + |
| 60 | +const ids = (rows: Array<Record<string, unknown>>, filter: unknown): string[] => |
| 61 | + rows.filter((r) => match(r, filter)).map((r) => String(r.id)); |
| 62 | + |
| 63 | +/** The ruling's answer: row 2 has a different value, row 3 has none. */ |
| 64 | +const NO_VALUE_INCLUDED = ['2', '3']; |
| 65 | + |
| 66 | +describe('[#13166] no-value rows and the negation-carrying operators', () => { |
| 67 | + describe('the six cells — three operators x two readings of "no value"', () => { |
| 68 | + const OPERATORS: Array<[name: string, filter: unknown]> = [ |
| 69 | + ['$ne', { name: { $ne: 'alpha-one' } }], |
| 70 | + ['$nin', { name: { $nin: ['alpha-one'] } }], |
| 71 | + ['$notContains', { name: { $notContains: 'one' } }], |
| 72 | + ]; |
| 73 | + |
| 74 | + for (const [op, filter] of OPERATORS) { |
| 75 | + it(`${op}: a null value satisfies it`, () => { |
| 76 | + expect(ids(NULLED, filter)).toEqual(NO_VALUE_INCLUDED); |
| 77 | + }); |
| 78 | + |
| 79 | + it(`${op}: an absent key satisfies it`, () => { |
| 80 | + expect(ids(MISSING, filter)).toEqual(NO_VALUE_INCLUDED); |
| 81 | + }); |
| 82 | + |
| 83 | + it(`${op}: both readings of "no value" answer alike`, () => { |
| 84 | + expect(ids(MISSING, filter)).toEqual(ids(NULLED, filter)); |
| 85 | + }); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + describe('the operators still discriminate — this is not "match everything"', () => { |
| 90 | + it('each negation excludes the row that DOES carry the comparand', () => { |
| 91 | + expect(ids(NULLED, { name: { $ne: 'alpha-one' } })).not.toContain('1'); |
| 92 | + expect(ids(NULLED, { name: { $nin: ['alpha-one'] } })).not.toContain('1'); |
| 93 | + expect(ids(NULLED, { name: { $notContains: 'one' } })).not.toContain('1'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('the positive twins keep answering the complement over the VALUED rows', () => { |
| 97 | + // A no-value row is in NEITHER answer for the positive operators: the |
| 98 | + // ruling moved the negative cells only. |
| 99 | + expect(ids(NULLED, { name: { $eq: 'alpha-one' } })).toEqual(['1']); |
| 100 | + expect(ids(MISSING, { name: { $in: ['alpha-one'] } })).toEqual(['1']); |
| 101 | + expect(ids(NULLED, { name: { $contains: 'one' } })).toEqual(['1']); |
| 102 | + expect(ids(MISSING, { name: { $contains: 'one' } })).toEqual(['1']); |
| 103 | + }); |
| 104 | + |
| 105 | + it('a present non-string value still fails $notContains on the type test', () => { |
| 106 | + // Out of scope for this card, and stated so a later reader does not read |
| 107 | + // the fix above as "any non-string satisfies the negation". Only the |
| 108 | + // no-value readings moved; a value that is there and is not a string |
| 109 | + // keeps the answer it had. |
| 110 | + expect(ids([{ id: '9', name: 42 }], { name: { $notContains: 'one' } })).toEqual([]); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('cross-operator agreement, the invariant that outlives the fixture', () => { |
| 115 | + it('$nin answers exactly what $ne answers — it is the list form of it', () => { |
| 116 | + // `formula`'s suite states the same identity over its own fixture. `$ne` |
| 117 | + // is the operator ENROLLED in `FILTER_LOGIC_CASES`, so this is the link |
| 118 | + // between the enrolled cell and the two that are not enrolled yet. |
| 119 | + for (const rows of [NULLED, MISSING]) { |
| 120 | + expect(ids(rows, { name: { $nin: ['alpha-one'] } })) |
| 121 | + .toEqual(ids(rows, { name: { $ne: 'alpha-one' } })); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it('$contains and $notContains partition the VALUED rows and both keep the no-value row out of the positive side', () => { |
| 126 | + for (const rows of [NULLED, MISSING]) { |
| 127 | + const inside = ids(rows, { name: { $contains: 'one' } }); |
| 128 | + const outside = ids(rows, { name: { $notContains: 'one' } }); |
| 129 | + expect(inside).toEqual(['1']); |
| 130 | + expect(outside).toEqual(NO_VALUE_INCLUDED); |
| 131 | + expect(inside.filter((id) => outside.includes(id))).toEqual([]); |
| 132 | + expect([...inside, ...outside].sort()).toEqual(['1', '2', '3']); |
| 133 | + } |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments