Skip to content

Commit a9aa42a

Browse files
authored
PG: Fix containedIn query on empty array (parse-community#5254)
* PG: Fix containedIn query on empty array * improve logic
1 parent ed29144 commit a9aa42a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

spec/ParseQuery.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,30 @@ describe('Parse.Query testing', () => {
238238
});
239239
});
240240

241+
it('query notContainedIn on empty array', async () => {
242+
const object = new TestObject();
243+
object.set('value', 100);
244+
await object.save();
245+
246+
const query = new Parse.Query(TestObject);
247+
query.notContainedIn('value', []);
248+
249+
const results = await query.find();
250+
equal(results.length, 1);
251+
});
252+
253+
it('query containedIn on empty array', async () => {
254+
const object = new TestObject();
255+
object.set('value', 100);
256+
await object.save();
257+
258+
const query = new Parse.Query(TestObject);
259+
query.containedIn('value', []);
260+
261+
const results = await query.find();
262+
equal(results.length, 0);
263+
});
264+
241265
it('query with limit', function(done) {
242266
const baz = new TestObject({ foo: 'baz' });
243267
const qux = new TestObject({ foo: 'qux' });

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
402402
index = index + 1 + inPatterns.length;
403403
} else if (isInOrNin) {
404404
var createConstraint = (baseArray, notIn) => {
405+
const not = notIn ? ' NOT ' : '';
405406
if (baseArray.length > 0) {
406-
const not = notIn ? ' NOT ' : '';
407407
if (isArrayField) {
408408
patterns.push(
409409
`${not} array_contains($${index}:name, $${index + 1})`
@@ -430,6 +430,13 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
430430
values.push(fieldName);
431431
patterns.push(`$${index}:name IS NULL`);
432432
index = index + 1;
433+
} else {
434+
// Handle empty array
435+
if (notIn) {
436+
patterns.push('1 = 1'); // Return all values
437+
} else {
438+
patterns.push('1 = 2'); // Return no values
439+
}
433440
}
434441
};
435442
if (fieldValue.$in) {

0 commit comments

Comments
 (0)