Skip to content

Commit

Permalink
Fixed issue #5043
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Oct 1, 2015
1 parent 04d9579 commit e4742c7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 37 deletions.
Expand Up @@ -26,6 +26,7 @@
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.iterator.OLazyWrapperIterator;
import com.orientechnologies.orient.core.record.impl.ODocument;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand All @@ -34,6 +35,7 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

/**
* Iterator that allow to iterate against multiple collection of elements.
Expand Down Expand Up @@ -176,7 +178,7 @@ public boolean supportsFastContains() {
final Object o = sources.get(i);

if (o != null) {
if (o instanceof Collection<?> || o instanceof ORidBag) {
if (o instanceof Set<?> || o instanceof ORidBag) {
// OK
} else if (o instanceof OLazyWrapperIterator) {
if (!((OLazyWrapperIterator) o).canUseMultiValueDirectly())
Expand Down Expand Up @@ -220,7 +222,7 @@ protected boolean getNextPartial() {
Object next = sourcesIterator.next();
if (next != null) {

if (next instanceof Iterable<?>)
if (!(next instanceof ODocument) && next instanceof Iterable<?>)
next = ((Iterable) next).iterator();

if (next instanceof OAutoConvertToRecord)
Expand Down
Expand Up @@ -2368,7 +2368,7 @@ private void applyExpand() {
if (r != null) {
if (r instanceof OIdentifiable) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) r);
} else if (OMultiValue.isMultiValue(r)) {
} else if (r instanceof Iterator || OMultiValue.isMultiValue(r)) {
for (Object o : OMultiValue.getMultiValueIterable(r)) {
((Collection<OIdentifiable>) tempResult).add((OIdentifiable) o);
}
Expand Down
Expand Up @@ -27,8 +27,8 @@
import java.util.Set;

/**
* This operator can work inline. Returns
* the DIFFERENCE between the collections received as parameters. Works also with no collection values.
* This operator can work inline. Returns the DIFFERENCE between the collections received as parameters. Works also with no
* collection values.
*
* @author Luca Garulli (l.garulli--at--orientechnologies.com)
*
Expand All @@ -51,13 +51,13 @@ public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurren

boolean first = true;
for (Object iParameter : iParams) {
if(first) {
if (first) {
if (iParameter instanceof Collection<?>) {
result.addAll((Collection<Object>) iParameter);
} else {
result.add(iParameter);
}
}else{
} else {
if (iParameter instanceof Collection<?>) {
result.removeAll((Collection<Object>) iParameter);
} else {
Expand All @@ -72,17 +72,7 @@ public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurren

}

@Override
public Set<Object> getResult() {
return super.getResult();
}

public String getSyntax() {
return "difference(<field>, <field> [, <field]*)";
}

public boolean aggregateResults() {
return false;
}

}
Expand Up @@ -19,20 +19,21 @@
*/
package com.orientechnologies.orient.core.sql.functions.coll;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.util.OSupportsContains;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.util.OSupportsContains;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable;

/**
* This operator can work as aggregate or inline. If only one argument is passed than aggregates, otherwise executes, and returns,
Expand Down Expand Up @@ -75,7 +76,7 @@ public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object i
}

// IN-LINE MODE (STATELESS)
Iterator result = OMultiValue.getMultiValueIterator(value);
Iterator iterator = OMultiValue.getMultiValueIterator(value);

for (int i = 1; i < iParams.length; ++i) {
value = iParams[i];
Expand All @@ -84,23 +85,25 @@ public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object i
value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);

if (value != null) {
intersectWith(result, value);
} else
result = new ArrayList().iterator();
value = intersectWith(iterator, value);
iterator = OMultiValue.getMultiValueIterator(value);
} else {
return new ArrayList().iterator();
}
}

return result;
return iterator;
}

@Override
public Object getResult() {
return OMultiValue.toSet(context);
}

static Iterator intersectWith(final Iterator current, Object value) {
static Collection intersectWith(final Iterator current, Object value) {
final HashSet tempSet = new HashSet();

if (value instanceof Iterator && (!(value instanceof OSupportsContains) || !((OSupportsContains) value).supportsFastContains()))
if (!(value instanceof Set) && (!(value instanceof OSupportsContains) || !((OSupportsContains) value).supportsFastContains()))
value = OMultiValue.toSet(value);

for (Iterator it = current; it.hasNext();) {
Expand All @@ -117,7 +120,7 @@ static Iterator intersectWith(final Iterator current, Object value) {
}
}

return tempSet.iterator();
return tempSet;
}

public String getSyntax() {
Expand Down
Expand Up @@ -23,9 +23,9 @@

/**
* Abstract class for multi-value based function implementations.
*
*
* @author Luca Garulli (l.garulli--at--orientechnologies.com)
*
*
*/
public abstract class OSQLFunctionMultiValueAbstract<T> extends OSQLFunctionConfigurableAbstract {

Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Set;
import java.util.Collection;

public class GraphIntersectLightweightEdges extends GraphNoTxAbstractTest {
private final int TOT = 1000;
Expand Down Expand Up @@ -85,17 +85,24 @@ public void testIntersect() {

OLogManager.instance().info(this, "Intersecting...");

final Iterable<OrientVertex> result = graph.command(new OCommandSQL("select intersect( out() ) from [?,?]")).execute(
Iterable<OrientVertex> result = graph.command(new OCommandSQL("select intersect( out() ) from [?,?]")).execute(
root1.getIdentity(), root2.getIdentity());

OLogManager.instance().info(this, "Intersecting done");

Assert.assertTrue(result.iterator().hasNext());
OrientVertex o = result.iterator().next();

final Set set = o.getRecord().field("intersect");
final Collection set = o.getRecord().field("intersect");

Assert.assertEquals(set.iterator().next(), common);

result = graph.command(
new OCommandSQL(
"select expand($c) let $a=(select from V limit 20), $b=(select from V skip 10 limit 10), $c=intersect( $a, $b )"))
.execute();

Assert.assertTrue(result.iterator().hasNext());
}

@BeforeClass
Expand Down

0 comments on commit e4742c7

Please sign in to comment.