Skip to content

Commit

Permalink
Fixed issue #2312
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed May 21, 2015
1 parent 8130d91 commit ff6f06a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@
import com.orientechnologies.orient.core.exception.OQueryParsingException;
import com.orientechnologies.orient.core.id.OContextualRecordId;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.index.*;
import com.orientechnologies.orient.core.index.OCompositeIndexDefinition;
import com.orientechnologies.orient.core.index.OCompositeKey;
import com.orientechnologies.orient.core.index.OIndex;
import com.orientechnologies.orient.core.index.OIndexCursor;
import com.orientechnologies.orient.core.index.OIndexDefinition;
import com.orientechnologies.orient.core.index.OIndexEngineException;
import com.orientechnologies.orient.core.index.OIndexInternal;
import com.orientechnologies.orient.core.iterator.ORecordIteratorClass;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
Expand All @@ -55,11 +61,23 @@
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentHelper;
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.coll.OSQLFunctionDistinct;
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.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.OOrderBy;
import com.orientechnologies.orient.core.sql.parser.OOrderByItem;
import com.orientechnologies.orient.core.sql.query.OResultSet;
Expand Down Expand Up @@ -115,6 +133,7 @@ public class OCommandExecutorSQLSelect extends OCommandExecutorSQLResultsetAbstr
private Set<ORID> uniqueResult;
private boolean noCache = false;
private int tipLimitThreshold = OGlobalConfiguration.QUERY_LIMIT_THRESHOLD_TIP.getValueAsInteger();
private String NULL_VALUE = "null";

public OCommandExecutorSQLSelect() {

Expand Down Expand Up @@ -697,10 +716,9 @@ protected ORuntimeResult getProjectionGroup(final Object fieldValue) {
final long begin = System.currentTimeMillis();
try {

Object key = null;
if (groupedResult == null) {
Object key;
if (groupedResult == null)
groupedResult = new LinkedHashMap<Object, ORuntimeResult>();
}

if (fieldValue != null) {
if (fieldValue.getClass().isArray()) {
Expand All @@ -715,17 +733,18 @@ protected ORuntimeResult getProjectionGroup(final Object fieldValue) {
if (o != null) {
keyArray.append(o instanceof OIdentifiable ? ((OIdentifiable) o).getIdentity().toString() : o.toString());
} else {
keyArray.append("null");
keyArray.append(NULL_VALUE);
}
}

key = keyArray.toString();
} else
// LOOKUP FOR THE FIELD
{
} else {
// LOOKUP FOR THE FIELD
key = fieldValue;
}
}
} else
// USE NULL_VALUE THEN REPLACE WITH REAL NULL
key = NULL_VALUE;

ORuntimeResult group = groupedResult.get(key);
if (group == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package com.orientechnologies.orient.test.database.auto;

import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
Expand All @@ -34,6 +36,15 @@ public SQLSelectGroupByTest(@Optional String url) {
super(url);
}

@BeforeMethod
@Override
public void beforeMethod() throws Exception {
super.beforeMethod();

if (!database.getMetadata().getSchema().existsClass("Account"))
database.getMetadata().getSchema().createClass("Account");
}

@Test
public void queryGroupByBasic() {
List<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select location from Account group by location"))
Expand Down Expand Up @@ -86,4 +97,28 @@ public void queryGroupByAndOrderBy() {
last = d.field("location");
}
}

@Test
public void queryGroupByAndWithNulls() {
// INSERT WITH NO LOCATION (AS NULL)
database.command(new OCommandSQL("insert into Account set testNull = true")).execute();
database.command(new OCommandSQL("insert into Account set location = 'Rome'")).execute();
database.command(new OCommandSQL("insert into Account set location = 'Austin'")).execute();
database.command(new OCommandSQL("insert into Account set location = 'Austin'")).execute();

final List<ODocument> result = database.command(
new OSQLSynchQuery<ODocument>("select location, count(*) from Account group by location")).execute();

Assert.assertTrue(result.size() > 1);

boolean foundNullGroup = false;
for (ODocument d : result) {
if (d.field("location") == null) {
Assert.assertFalse(foundNullGroup);
foundNullGroup = true;
}
}

Assert.assertTrue(foundNullGroup);
}
}

0 comments on commit ff6f06a

Please sign in to comment.