Skip to content

Commit

Permalink
Fix SQL CONTAINS with lists of lists
Browse files Browse the repository at this point in the history
Resolves: #8653
  • Loading branch information
luigidellaquila committed Nov 27, 2018
1 parent 8a44448 commit 38a35b9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,27 @@ public Object jjtAccept(OrientSqlVisitor visitor, Object data) {
public boolean execute(Object left, Object right) {
if (left instanceof Collection) {
if (right instanceof Collection) {
if (((Collection) left).containsAll((Collection) right)) {
return true;
}

if (((Collection) right).size() == 1) {
Object item = ((Collection) right).iterator().next();
if (item instanceof OResult && ((OResult) item).getPropertyNames().size() == 1) {
Object propValue = ((OResult) item).getProperty(((OResult) item).getPropertyNames().iterator().next());
return ((Collection) left).contains(propValue);
if (((Collection) left).contains(propValue)) {
return true;
}
}
if (((Collection) left).contains(item)) {
return true;
}
if (item instanceof OResult) {
item = ((OResult) item).getElement().orElse(null);
}
if (item instanceof OIdentifiable && ((Collection) left).contains(item)) {
return true;
}
}

if (OMultiValue.contains(left, right)) {
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3851,4 +3851,43 @@ public void testOrderByWithCollate() {
}
}

@Test
public void testContainsEmptyCollection() {
String className = "testContainsEmptyCollection";

db.createClassIfNotExist(className);

db.command("INSERT INTO " + className + " content {\"name\": \"jack\", \"age\": 22}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"rose\", \"age\": 22, \"test\": [[]]}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"rose\", \"age\": 22, \"test\": [[1]]}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"pete\", \"age\": 22, \"test\": [{}]}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"david\", \"age\": 22, \"test\": [\"hello\"]}").close();

try (OResultSet result = db.query("select from " + className + " where test contains []")) {
Assert.assertTrue(result.hasNext());
result.next();
Assert.assertFalse(result.hasNext());
}
}


@Test
public void testContainsCollection() {
String className = "testContainsCollection";

db.createClassIfNotExist(className);

db.command("INSERT INTO " + className + " content {\"name\": \"jack\", \"age\": 22}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"rose\", \"age\": 22, \"test\": [[]]}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"rose\", \"age\": 22, \"test\": [[1]]}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"pete\", \"age\": 22, \"test\": [{}]}").close();
db.command("INSERT INTO " + className + " content {\"name\": \"david\", \"age\": 22, \"test\": [\"hello\"]}").close();

try (OResultSet result = db.query("select from " + className + " where test contains [1]")) {
Assert.assertTrue(result.hasNext());
result.next();
Assert.assertFalse(result.hasNext());
}
}

}

0 comments on commit 38a35b9

Please sign in to comment.