Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix indexed column EqFilter condition for is null operator #431

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading