Skip to content

Commit

Permalink
fix indexed column eqfilter conditioning for is null operator
Browse files Browse the repository at this point in the history
  • Loading branch information
andriizavoiko committed Nov 27, 2024
1 parent 8f84e85 commit 063e9be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/tests/simple-queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,5 +502,16 @@ describe('Simple queries', () => {
members VARCHAR[]
);`);
expectQueryError(() => none(`INSERT INTO example (members) VALUES (ARRAY[])`), /cannot determine type of empty array/);
})
});

it('should select row with null value condition for indexed column', async () => {
none(`
CREATE TABLE "tableA" ("id" SERIAL NOT NULL, "reference" INTEGER);
CREATE INDEX "IDX_1" ON "tableA" ("reference");
INSERT INTO "tableA"(id, reference) values (1, null);
`);
// it is important that EqFilter hasItem gets called, so two same conditions are applied to fall both into best and sorted conditions
const got = many(`SELECT "tableA"."id" FROM "tableA" WHERE ("tableA"."id" IN (1, 2) AND "tableA"."reference" IS NULL) AND ("tableA"."id" IN (1, 2) AND "tableA"."reference" IS NULL)`);
expect(got).toHaveLength(1);
});
});
7 changes: 6 additions & 1 deletion src/transforms/eq-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ export class EqFilter extends FilterBase {
}

hasItem(item: Row, t: _Transaction) {
const isEq = this.op === 'eq';
const val = this.onValue.get(item, t);

if (this.matchNull && nullIsh(val)) {
return isEq;
}
if (nullIsh(val)) {
return false;
}
const eq = this.onValue.type.equals(val, this.equalsCst);
if (nullIsh(eq)) {
return false;
}
return this.op === 'eq' ? !!eq : !eq;
return isEq ? !!eq : !eq;
}

constructor(private onValue: IValue
Expand Down

0 comments on commit 063e9be

Please sign in to comment.