Skip to content

Commit

Permalink
fix return type on square bracket filtering (new parser)
Browse files Browse the repository at this point in the history
now expressions like

SELECT out()[1, 2, 3] as foo …
SELECT out()[1-3] as foo …
SELECT out()[name = ‘foo’] as foo …

return a list as the value of foo.
The only exception is single number as a selector, eg.

SELECT out()[1] as foo …

in this case foo will be the single value, not a list
  • Loading branch information
luigidellaquila committed Sep 17, 2015
1 parent 27f2058 commit 0223753
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
Expand Up @@ -54,7 +54,7 @@ public Object execute(OIdentifiable iCurrentRecord, Object result, OCommandConte
return null;
}
if (!OMultiValue.isMultiValue(result)) {
return result;// is this correct?
return null;
}
Integer lFrom = from;
if (fromSelector != null) {
Expand Down Expand Up @@ -83,7 +83,7 @@ public Object execute(OIdentifiable iCurrentRecord, Object result, OCommandConte

lTo = Math.min(lTo, arrayResult.length);

return Arrays.copyOfRange(arrayResult, lFrom, lTo);
return Arrays.asList(Arrays.copyOfRange(arrayResult, lFrom, lTo));
}

}
Expand Down
Expand Up @@ -47,7 +47,7 @@ public Object execute(OIdentifiable iCurrentRecord, Object iResult, OCommandCont
}
result.add(OMultiValue.getValue(iResult, index));
}
return result.toArray();
return result;
}

}
Expand Down
Expand Up @@ -5,6 +5,10 @@
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class OModifier extends SimpleNode {
Expand Down Expand Up @@ -64,7 +68,7 @@ public Object execute(OIdentifiable iCurrentRecord, Object result, OCommandConte
} else if (arrayRange != null) {
return arrayRange.execute(iCurrentRecord, result, ctx);
} else if (condition != null) {
throw new UnsupportedOperationException("implement OModifier!");
return filterByCondition(iCurrentRecord, result, ctx);
} else if (arraySingleValues != null) {
return arraySingleValues.execute(iCurrentRecord, result, ctx);
} else {
Expand All @@ -76,5 +80,33 @@ public Object execute(OIdentifiable iCurrentRecord, Object result, OCommandConte
}
return result;
}

private Object filterByCondition(OIdentifiable iCurrentRecord, Object iResult, OCommandContext ctx) {
if(iResult==null){
return null;
}
List<Object> result = new ArrayList<Object>();
if(iResult.getClass().isArray()){
for(int i=0;i< Array.getLength(iResult); i++){
Object item = Array.get(iResult, i);
if(condition.evaluate(item, ctx)){
result.add(item);
}
}
return result;
}
if(iResult instanceof Iterable){
iResult = ((Iterable) iResult).iterator();
}
if(iResult instanceof Iterator){
while(((Iterator) iResult).hasNext()){
Object item = ((Iterator) iResult).next();
if(condition.evaluate(item, ctx)){
result.add(item);
}
}
}
return result;
}
}
/* JavaCC - OriginalChecksum=39c21495d02f9b5007b4a2d6915496e1 (do not edit this line) */
Expand Up @@ -6,6 +6,7 @@
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -36,6 +37,17 @@ public boolean evaluate(OIdentifiable currentRecord, OCommandContext ctx) {
return false;
}

public boolean evaluate(Object currentRecord, OCommandContext ctx) {
if (currentRecord instanceof OIdentifiable) {
return evaluate((OIdentifiable) currentRecord, ctx);
} else if (currentRecord instanceof Map) {
ODocument doc = new ODocument();
doc.fromMap((Map<String, Object>) currentRecord);
return evaluate(doc, ctx);
}
return false;
}

public List<OBooleanExpression> getSubBlocks() {
return subBlocks;
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -631,41 +632,40 @@ public void testArrayNumber() {
ODocument doc = (ODocument) result.get(0);
Object foo = doc.field("foo");
assertNotNull(foo);
assertFalse(foo.getClass().isArray());
// assertEquals(1, ((Object[])foo).length);
assertTrue(foo instanceof Vertex);
}


@Test
public void testArraySingleSelectors1() {
public void testArraySingleSelectors2() {
StringBuilder query = new StringBuilder();
query.append("match ");
query.append("{class:TriangleV, as: friend1, where: (uid = 0)}");
query.append("return friend1.out('TriangleE')[0-1] as foo" );
query.append("return friend1.out('TriangleE')[0,1] as foo" );

List<?> result = db.command(new OCommandSQL(query.toString())).execute();
assertEquals(1, result.size());
ODocument doc = (ODocument) result.get(0);
Object foo = doc.field("foo");
assertNotNull(foo);
assertTrue(foo.getClass().isArray());
assertEquals(1, ((Object[])foo).length);
assertTrue(foo instanceof List);
assertEquals(2, ((List)foo).size());
}


@Test
public void testArraySingleSelectors2() {
public void testArrayRangeSelectors1() {
StringBuilder query = new StringBuilder();
query.append("match ");
query.append("{class:TriangleV, as: friend1, where: (uid = 0)}");
query.append("return friend1.out('TriangleE')[0,1] as foo" );
query.append("return friend1.out('TriangleE')[0-1] as foo" );

List<?> result = db.command(new OCommandSQL(query.toString())).execute();
assertEquals(1, result.size());
ODocument doc = (ODocument) result.get(0);
Object foo = doc.field("foo");
assertNotNull(foo);
assertTrue(foo.getClass().isArray());
assertEquals(2, ((Object[])foo).length);
assertTrue(foo instanceof List);
assertEquals(1, ((List)foo).size());
}

@Test
Expand All @@ -680,8 +680,8 @@ public void testArrayRange2() {
ODocument doc = (ODocument) result.get(0);
Object foo = doc.field("foo");
assertNotNull(foo);
assertTrue(foo.getClass().isArray());
assertEquals(2, ((Object[])foo).length);
assertTrue(foo instanceof List);
assertEquals(2, ((List)foo).size());
}


Expand All @@ -697,8 +697,26 @@ public void testArrayRange3() {
ODocument doc = (ODocument) result.get(0);
Object foo = doc.field("foo");
assertNotNull(foo);
assertTrue(foo.getClass().isArray());
assertEquals(2, ((Object[])foo).length);
assertTrue(foo instanceof List);
assertEquals(2, ((List)foo).size());
}

@Test
public void testConditionInSquareBrackets() {
StringBuilder query = new StringBuilder();
query.append("match ");
query.append("{class:TriangleV, as: friend1, where: (uid = 0)}");
query.append("return friend1.out('TriangleE')[uid = 2] as foo" );

List<?> result = db.command(new OCommandSQL(query.toString())).execute();
assertEquals(1, result.size());
ODocument doc = (ODocument) result.get(0);
Object foo = doc.field("foo");
assertNotNull(foo);
assertTrue(foo instanceof List);
assertEquals(1, ((List) foo).size());
Vertex resultVertex = (Vertex) ((List)foo).get(0);
assertEquals(2, resultVertex.getProperty("uid"));
}

private long indexUsages(ODatabaseDocumentTx db) {
Expand Down

0 comments on commit 0223753

Please sign in to comment.