mirror of https://github.com/apache/druid.git
More consistent ObjectColumnSelectors. JS aggregator tests for nulls.
This commit is contained in:
parent
cb028f490a
commit
d389fbc961
|
@ -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)];
|
||||
|
|
|
@ -467,23 +467,29 @@ 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());
|
||||
final String[] strings = new String[multiValueRow.size()];
|
||||
for (int i = 0 ; i < multiValueRow.size() ; i++) {
|
||||
strings[i] = columnVals.lookupName(multiValueRow.get(i));
|
||||
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;
|
||||
}
|
||||
return strings;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
|
@ -891,23 +897,29 @@ 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);
|
||||
final String[] strings = new String[multiValueRow.size()];
|
||||
for (int i = 0 ; i < multiValueRow.size() ; i++) {
|
||||
strings[i] = columnVals.lookupName(multiValueRow.get(i));
|
||||
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;
|
||||
}
|
||||
return strings;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue