From c73fda84b24c635b269fc549710cb3dff00b4899 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 19 Dec 2018 17:02:41 -0600 Subject: [PATCH 1/2] PG: Fix containedIn query on empty array --- spec/ParseQuery.spec.js | 24 +++++++++++++++++++ .../Postgres/PostgresStorageAdapter.js | 6 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index f3803b168f..e12988984d 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, 0); + }); + + 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..e9578285d1 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,10 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => { values.push(fieldName); patterns.push(`$${index}:name IS NULL`); index = index + 1; + } else { + patterns.push(`$${index}:name ${not} IN (null)`); + values.push(fieldName); + index += 1; } }; if (fieldValue.$in) { From bba5f714d181dc6f38b659b0c1794ed275cfacf1 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 19 Dec 2018 17:35:44 -0600 Subject: [PATCH 2/2] improve logic --- spec/ParseQuery.spec.js | 2 +- src/Adapters/Storage/Postgres/PostgresStorageAdapter.js | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index e12988984d..355689f6b7 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -247,7 +247,7 @@ describe('Parse.Query testing', () => { query.notContainedIn('value', []); const results = await query.find(); - equal(results.length, 0); + equal(results.length, 1); }); it('query containedIn on empty array', async () => { diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index e9578285d1..dcfd34b877 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -431,9 +431,12 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => { patterns.push(`$${index}:name IS NULL`); index = index + 1; } else { - patterns.push(`$${index}:name ${not} IN (null)`); - values.push(fieldName); - index += 1; + // Handle empty array + if (notIn) { + patterns.push('1 = 1'); // Return all values + } else { + patterns.push('1 = 2'); // Return no values + } } }; if (fieldValue.$in) {