Skip to content

PG: Fix containedIn query on empty array #5254

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

Merged
merged 2 commits into from
Dec 19, 2018
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
24 changes: 24 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
9 changes: 8 additions & 1 deletion src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})`
Expand All @@ -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) {
Expand Down