Skip to content

Commit

Permalink
fixed issue #4911
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Sep 3, 2015
1 parent 127ce10 commit 7b4eeb7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
Expand Up @@ -19,6 +19,14 @@
*/ */
package com.orientechnologies.orient.core.sql; package com.orientechnologies.orient.core.sql;


import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import com.orientechnologies.common.collection.OMultiCollectionIterator; import com.orientechnologies.common.collection.OMultiCollectionIterator;
import com.orientechnologies.common.collection.OMultiValue; import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.collection.OSortedMultiIterator; import com.orientechnologies.common.collection.OSortedMultiIterator;
Expand Down Expand Up @@ -61,24 +69,32 @@
import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentHelper; import com.orientechnologies.orient.core.record.impl.ODocumentHelper;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper; import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.sql.filter.*; import com.orientechnologies.orient.core.sql.filter.OFilterOptimizer;
import com.orientechnologies.orient.core.sql.filter.OSQLFilter;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItem;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime; import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime;
import com.orientechnologies.orient.core.sql.functions.coll.OSQLFunctionDistinct; import com.orientechnologies.orient.core.sql.functions.coll.OSQLFunctionDistinct;
import com.orientechnologies.orient.core.sql.functions.misc.OSQLFunctionCount; import com.orientechnologies.orient.core.sql.functions.misc.OSQLFunctionCount;
import com.orientechnologies.orient.core.sql.operator.*; import com.orientechnologies.orient.core.sql.operator.OQueryOperator;
import com.orientechnologies.orient.core.sql.parser.*; import com.orientechnologies.orient.core.sql.operator.OQueryOperatorAnd;
import com.orientechnologies.orient.core.sql.operator.OQueryOperatorBetween;
import com.orientechnologies.orient.core.sql.operator.OQueryOperatorIn;
import com.orientechnologies.orient.core.sql.operator.OQueryOperatorMajor;
import com.orientechnologies.orient.core.sql.operator.OQueryOperatorMajorEquals;
import com.orientechnologies.orient.core.sql.operator.OQueryOperatorMinor;
import com.orientechnologies.orient.core.sql.operator.OQueryOperatorMinorEquals;
import com.orientechnologies.orient.core.sql.parser.OBinaryCondition;
import com.orientechnologies.orient.core.sql.parser.OOrderBy;
import com.orientechnologies.orient.core.sql.parser.OOrderByItem;
import com.orientechnologies.orient.core.sql.parser.OSelectStatement;
import com.orientechnologies.orient.core.sql.parser.OWhereClause;
import com.orientechnologies.orient.core.sql.query.OResultSet; import com.orientechnologies.orient.core.sql.query.OResultSet;
import com.orientechnologies.orient.core.sql.query.OSQLQuery; import com.orientechnologies.orient.core.sql.query.OSQLQuery;
import com.orientechnologies.orient.core.storage.OStorage.LOCKING_STRATEGY; import com.orientechnologies.orient.core.storage.OStorage.LOCKING_STRATEGY;


import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/** /**
* Executes the SQL SELECT statement. the parse() method compiles the query and builds the meta information needed by the execute(). * Executes the SQL SELECT statement. the parse() method compiles the query and builds the meta information needed by the execute().
* If the query contains the ORDER BY clause, the results are temporary collected internally, then ordered and finally returned all * If the query contains the ORDER BY clause, the results are temporary collected internally, then ordered and finally returned all
Expand Down Expand Up @@ -210,6 +226,10 @@ private static Object getIndexKey(final OIndexDefinition indexDefinition, Object
} }
} }


public boolean hasGroupBy() {
return groupByFields != null && groupByFields.size() > 0;
}

@Override @Override
protected boolean isUseCache() { protected boolean isUseCache() {
return !noCache && request.isUseCache(); return !noCache && request.isUseCache();
Expand Down
Expand Up @@ -163,7 +163,8 @@ protected void executeTest() throws Exception {


final Iterable<OrientVertex> explain = graph.command(new OCommandSQL("explain select from " + e.getIdentity())).execute(); final Iterable<OrientVertex> explain = graph.command(new OCommandSQL("explain select from " + e.getIdentity())).execute();


System.out.println("explain select from " + e.getIdentity() + " -> " + ((ODocument) explain.iterator().next().getRecord()).field("servers")); System.out.println("explain select from " + e.getIdentity() + " -> "
+ ((ODocument) explain.iterator().next().getRecord()).field("servers"));


result = graph.command(new OCommandSQL("select from " + e.getIdentity())).execute(); result = graph.command(new OCommandSQL("select from " + e.getIdentity())).execute();


Expand Down Expand Up @@ -292,6 +293,53 @@ protected void executeTest() throws Exception {
} }
} }


// TEST DISTRIBUTED QUERY AGAINST ALL 3 DATABASES TO TEST AGGREGATION + GROUP BY
for (int server = 0; server < vertices.length; ++server) {
OrientGraphFactory f = new OrientGraphFactory("plocal:target/server" + server + "/databases/" + getDatabaseName());
OrientGraphNoTx g = f.getNoTx();
try {

Iterable<OrientVertex> result = g.command(new OCommandSQL("select name, count(*) from Client group by name")).execute();

int count = 0;
for (OrientVertex v : result) {
System.out.println("select name, count(*) from Client group by name -> " + v.getRecord());

Assert.assertEquals(((Number) v.getProperty("count")).intValue(), 1);

count++;
}

Assert.assertEquals("Returned wrong vertices count on server " + server, vertices.length, count);
} finally {
g.shutdown();
}
}

// TEST DISTRIBUTED QUERY AGAINST ALL 3 DATABASES TO TEST AGGREGATION + ADDITIONAL FIELD
for (int server = 0; server < vertices.length; ++server) {
OrientGraphFactory f = new OrientGraphFactory("plocal:target/server" + server + "/databases/" + getDatabaseName());
OrientGraphNoTx g = f.getNoTx();
try {

Iterable<OrientVertex> result = g.command(new OCommandSQL("select name, count(*) from Client")).execute();

int count = 0;
for (OrientVertex v : result) {
System.out.println("select name, count(*) from Client -> " + v.getRecord());

Assert.assertEquals(((Number) v.getProperty("count")).intValue(), vertices.length);
Assert.assertNotNull(v.getProperty("name"));

count++;
}

Assert.assertEquals("Returned wrong vertices count on server " + server, 1, count);
} finally {
g.shutdown();
}
}

// TEST DISTRIBUTED DELETE WITH DIRECT COMMAND AND SQL // TEST DISTRIBUTED DELETE WITH DIRECT COMMAND AND SQL
OrientGraphFactory f = new OrientGraphFactory("plocal:target/server" + 0 + "/databases/" + getDatabaseName()); OrientGraphFactory f = new OrientGraphFactory("plocal:target/server" + 0 + "/databases/" + getDatabaseName());
OrientGraphNoTx g = f.getNoTx(); OrientGraphNoTx g = f.getNoTx();
Expand Down
Expand Up @@ -286,8 +286,8 @@ public Object command(final OCommandRequestText iCommand) {
final OCommandExecutorSQLSelect select = exec instanceof OCommandExecutorSQLSelect ? (OCommandExecutorSQLSelect) exec final OCommandExecutorSQLSelect select = exec instanceof OCommandExecutorSQLSelect ? (OCommandExecutorSQLSelect) exec
: null; : null;


if (select != null && select.isAnyFunctionAggregates()) { if (select != null && select.isAnyFunctionAggregates() && !select.hasGroupBy()) {
result = mergeResultByAggegation(select, results); result = mergeResultByAggregation(select, results);
} else { } else {
// MIX & FILTER RESULT SET AVOIDING DUPLICATES // MIX & FILTER RESULT SET AVOIDING DUPLICATES
// TODO: ONCE OPTIMIZED (SEE ABOVE) AVOID TO FILTER HERE // TODO: ONCE OPTIMIZED (SEE ABOVE) AVOID TO FILTER HERE
Expand Down Expand Up @@ -392,7 +392,7 @@ protected Map<String, Object> executeOnServers(final OCommandRequestText iComman
return results; return results;
} }


protected Object mergeResultByAggegation(final OCommandExecutorSQLSelect select, final Map<String, Object> iResults) { protected Object mergeResultByAggregation(final OCommandExecutorSQLSelect select, final Map<String, Object> iResults) {
final List<Object> list = new ArrayList<Object>(); final List<Object> list = new ArrayList<Object>();
final ODocument doc = new ODocument(); final ODocument doc = new ODocument();
list.add(doc); list.add(doc);
Expand All @@ -418,7 +418,7 @@ protected Object mergeResultByAggegation(final OCommandExecutorSQLSelect select,
for (Map.Entry<String, Object> p : proj.entrySet()) { for (Map.Entry<String, Object> p : proj.entrySet()) {
// WRITE THE FIELD AS IS // WRITE THE FIELD AS IS
if (!(p.getValue() instanceof OSQLFunctionRuntime)) if (!(p.getValue() instanceof OSQLFunctionRuntime))
d.field(p.getKey(), p.getValue()); doc.field(p.getKey(), ((ODocument) r).field(p.getKey()));
} }
} }
} }
Expand Down

0 comments on commit 7b4eeb7

Please sign in to comment.