More consistent ObjectColumnSelectors. JS aggregator tests for nulls.

This commit is contained in:
Gian Merlino 2014-05-15 23:02:37 -07:00
parent cb028f490a
commit d389fbc961
4 changed files with 35 additions and 22 deletions

View File

@ -278,7 +278,7 @@ public class JavaScriptAggregatorFactory implements AggregatorFactory
final ObjectColumnSelector selector = selectorList[i];
if (selector != null) {
final Object arg = selector.get();
if (arg.getClass().isArray()) {
if (arg != null && arg.getClass().isArray()) {
// Context.javaToJS on an array sort of works, although it returns false for Array.isArray(...) and
// may have other issues too. Let's just copy the array and wrap that.
final Object[] arrayAsObjectArray = new Object[Array.getLength(arg)];

View File

@ -467,24 +467,30 @@ public class QueryableIndexStorageAdapter implements StorageAdapter
if (cachedColumnVals instanceof DictionaryEncodedColumn) {
final DictionaryEncodedColumn columnVals = (DictionaryEncodedColumn) cachedColumnVals;
if (columnVals.hasMultipleValues()) {
return new ObjectColumnSelector<String[]>()
return new ObjectColumnSelector<Object>()
{
@Override
public Class classOfObject()
{
return String[].class;
return Object.class;
}
@Override
public String[] get()
public Object get()
{
final IndexedInts multiValueRow = columnVals.getMultiValueRow(cursorOffset.getOffset());
if (multiValueRow.size() == 0) {
return null;
} else if (multiValueRow.size() == 1) {
return columnVals.lookupName(multiValueRow.get(1));
} else {
final String[] strings = new String[multiValueRow.size()];
for (int i = 0 ; i < multiValueRow.size() ; i++) {
strings[i] = columnVals.lookupName(multiValueRow.get(i));
}
return strings;
}
}
};
} else {
return new ObjectColumnSelector<String>()
@ -891,24 +897,30 @@ public class QueryableIndexStorageAdapter implements StorageAdapter
if (cachedColumnVals instanceof DictionaryEncodedColumn) {
final DictionaryEncodedColumn columnVals = (DictionaryEncodedColumn) cachedColumnVals;
if (columnVals.hasMultipleValues()) {
return new ObjectColumnSelector<String[]>()
return new ObjectColumnSelector<Object>()
{
@Override
public Class classOfObject()
{
return String[].class;
return Object.class;
}
@Override
public String[] get()
public Object get()
{
final IndexedInts multiValueRow = columnVals.getMultiValueRow(currRow);
if (multiValueRow.size() == 0) {
return null;
} else if (multiValueRow.size() == 1) {
return columnVals.lookupName(multiValueRow.get(1));
} else {
final String[] strings = new String[multiValueRow.size()];
for (int i = 0 ; i < multiValueRow.size() ; i++) {
strings[i] = columnVals.lookupName(multiValueRow.get(i));
}
return strings;
}
}
};
} else {
return new ObjectColumnSelector<String>()

View File

@ -408,11 +408,7 @@ public class IncrementalIndexStorageAdapter implements StorageAdapter
@Override
public Class classOfObject()
{
if (currEntry.getKey().getDims()[dimensionIndex].length > 1) {
return String[].class;
} else {
return String.class;
}
return Object.class;
}
@Override

View File

@ -184,12 +184,12 @@ public class JavaScriptAggregatorTest
@Test
public void testAggregateStrings()
{
final TestObjectColumnSelector ocs = new TestObjectColumnSelector("what", new String[]{"hey", "there"});
final TestObjectColumnSelector ocs = new TestObjectColumnSelector("what", null, new String[]{"hey", "there"});
final JavaScriptAggregator agg = new JavaScriptAggregator(
"billy",
Collections.<ObjectColumnSelector>singletonList(ocs),
JavaScriptAggregatorFactory.compileScript(
"function aggregate(current, a) { if (typeof a === 'string') { return current + 1; } else { return current + a.length; } }",
"function aggregate(current, a) { if (Array.isArray(a)) { return current + a.length; } else if (typeof a === 'string') { return current + 1; } else { return current; } }",
scriptDoubleSum.get("fnReset"),
scriptDoubleSum.get("fnCombine")
)
@ -211,6 +211,11 @@ public class JavaScriptAggregatorTest
Assert.assertEquals(val, agg.get());
aggregate(ocs, agg);
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
aggregate(ocs, agg);
val += 2;
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());