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]; final ObjectColumnSelector selector = selectorList[i];
if (selector != null) { if (selector != null) {
final Object arg = selector.get(); 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 // 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. // may have other issues too. Let's just copy the array and wrap that.
final Object[] arrayAsObjectArray = new Object[Array.getLength(arg)]; final Object[] arrayAsObjectArray = new Object[Array.getLength(arg)];

View File

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

View File

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

View File

@ -184,12 +184,12 @@ public class JavaScriptAggregatorTest
@Test @Test
public void testAggregateStrings() 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( final JavaScriptAggregator agg = new JavaScriptAggregator(
"billy", "billy",
Collections.<ObjectColumnSelector>singletonList(ocs), Collections.<ObjectColumnSelector>singletonList(ocs),
JavaScriptAggregatorFactory.compileScript( 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("fnReset"),
scriptDoubleSum.get("fnCombine") scriptDoubleSum.get("fnCombine")
) )
@ -211,6 +211,11 @@ public class JavaScriptAggregatorTest
Assert.assertEquals(val, agg.get()); Assert.assertEquals(val, agg.get());
aggregate(ocs, agg); aggregate(ocs, agg);
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
aggregate(ocs, agg);
val += 2; val += 2;
Assert.assertEquals(val, agg.get()); Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get()); Assert.assertEquals(val, agg.get());