Merge pull request #331 from metamx/js-aggregator-missing-column

JavaScriptAggregatorFactory: Handle missing columns by passing down null args
This commit is contained in:
fjy 2013-12-16 21:21:55 -08:00
commit 9fc0e18962
2 changed files with 39 additions and 3 deletions

View File

@ -265,9 +265,11 @@ public class JavaScriptAggregatorFactory implements AggregatorFactory
final Object[] args = new Object[size + 1]; final Object[] args = new Object[size + 1];
args[0] = current; args[0] = current;
int i = 0; for (int i = 0 ; i < size ; i++) {
while (i < size) { final ObjectColumnSelector selector = selectorList[i];
args[i + 1] = selectorList[i++].get(); if (selector != null) {
args[i + 1] = selector.get();
}
} }
final Object res = fnAggregate.call(cx, scope, scope, args); final Object res = fnAggregate.call(cx, scope, scope, args);

View File

@ -27,6 +27,7 @@ import org.junit.Test;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Map; import java.util.Map;
public class JavaScriptAggregatorTest public class JavaScriptAggregatorTest
@ -141,6 +142,39 @@ public class JavaScriptAggregatorTest
Assert.assertEquals(val, agg.get(buf, position)); Assert.assertEquals(val, agg.get(buf, position));
} }
@Test
public void testAggregateMissingColumn()
{
Map<String, String> script = scriptDoubleSum;
JavaScriptAggregator agg = new JavaScriptAggregator(
"billy",
Collections.<ObjectColumnSelector>singletonList(null),
JavaScriptAggregatorFactory.compileScript(script.get("fnAggregate"),
script.get("fnReset"),
script.get("fnCombine"))
);
final double val = 0;
Assert.assertEquals("billy", agg.getName());
agg.reset();
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
agg.aggregate();
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
agg.aggregate();
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
Assert.assertEquals(val, agg.get());
}
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
final LoopingFloatColumnSelector selector = new LoopingFloatColumnSelector(new float[]{42.12f, 9f}); final LoopingFloatColumnSelector selector = new LoopingFloatColumnSelector(new float[]{42.12f, 9f});