diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index f3803b168f..355689f6b7 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -238,6 +238,30 @@ describe('Parse.Query testing', () => { }); }); + it('query notContainedIn on empty array', async () => { + const object = new TestObject(); + object.set('value', 100); + await object.save(); + + const query = new Parse.Query(TestObject); + query.notContainedIn('value', []); + + const results = await query.find(); + equal(results.length, 1); + }); + + it('query containedIn on empty array', async () => { + const object = new TestObject(); + object.set('value', 100); + await object.save(); + + const query = new Parse.Query(TestObject); + query.containedIn('value', []); + + const results = await query.find(); + equal(results.length, 0); + }); + it('query with limit', function(done) { const baz = new TestObject({ foo: 'baz' }); const qux = new TestObject({ foo: 'qux' }); diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 8df3acc9f5..dcfd34b877 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -402,8 +402,8 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => { index = index + 1 + inPatterns.length; } else if (isInOrNin) { var createConstraint = (baseArray, notIn) => { + const not = notIn ? ' NOT ' : ''; if (baseArray.length > 0) { - const not = notIn ? ' NOT ' : ''; if (isArrayField) { patterns.push( `${not} array_contains($${index}:name, $${index + 1})` @@ -430,6 +430,13 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => { values.push(fieldName); patterns.push(`$${index}:name IS NULL`); index = index + 1; + } else { + // Handle empty array + if (notIn) { + patterns.push('1 = 1'); // Return all values + } else { + patterns.push('1 = 2'); // Return no values + } } }; if (fieldValue.$in) {