diff --git a/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregator.java b/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregator.java index dbb1a85bbb5..6ce103e2af4 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregator.java +++ b/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregator.java @@ -20,7 +20,7 @@ package com.metamx.druid.aggregation; import com.google.common.collect.Lists; -import com.metamx.druid.processing.FloatMetricSelector; +import com.metamx.druid.processing.ObjectColumnSelector; import java.util.List; @@ -28,7 +28,7 @@ public class JavaScriptAggregator implements Aggregator { static interface ScriptAggregator { - public double aggregate(double current, FloatMetricSelector[] selectorList); + public double aggregate(double current, ObjectColumnSelector[] selectorList); public double combine(double a, double b); @@ -38,15 +38,15 @@ public class JavaScriptAggregator implements Aggregator } private final String name; - private final FloatMetricSelector[] selectorList; + private final ObjectColumnSelector[] selectorList; private final ScriptAggregator script; private volatile double current; - public JavaScriptAggregator(String name, List selectorList, ScriptAggregator script) + public JavaScriptAggregator(String name, List selectorList, ScriptAggregator script) { this.name = name; - this.selectorList = Lists.newArrayList(selectorList).toArray(new FloatMetricSelector[]{}); + this.selectorList = Lists.newArrayList(selectorList).toArray(new ObjectColumnSelector[]{}); this.script = script; this.current = script.reset(); diff --git a/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregatorFactory.java b/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregatorFactory.java index 9a594febb96..af68b26df42 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregatorFactory.java +++ b/common/src/main/java/com/metamx/druid/aggregation/JavaScriptAggregatorFactory.java @@ -21,18 +21,16 @@ package com.metamx.druid.aggregation; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Charsets; import com.google.common.base.Joiner; import com.google.common.collect.Lists; import com.google.common.primitives.Doubles; -import com.metamx.common.IAE; -import com.metamx.druid.processing.FloatMetricSelector; import com.metamx.druid.processing.ColumnSelectorFactory; - +import com.metamx.druid.processing.ObjectColumnSelector; import org.mozilla.javascript.Context; import org.mozilla.javascript.ContextAction; import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.Function; -import org.mozilla.javascript.Script; import org.mozilla.javascript.ScriptableObject; import javax.annotation.Nullable; @@ -76,16 +74,18 @@ public class JavaScriptAggregatorFactory implements AggregatorFactory } @Override - public Aggregator factorize(final ColumnSelectorFactory metricFactory) + public Aggregator factorize(final ColumnSelectorFactory columnFactory) { return new JavaScriptAggregator( name, Lists.transform( fieldNames, - new com.google.common.base.Function() + new com.google.common.base.Function() { @Override - public FloatMetricSelector apply(@Nullable String s) { return metricFactory.makeFloatMetricSelector(s); } + public ObjectColumnSelector apply(@Nullable String s) { + return columnFactory.makeObjectColumnSelector(s); + } } ), compiledScript @@ -93,17 +93,16 @@ public class JavaScriptAggregatorFactory implements AggregatorFactory } @Override - public BufferAggregator factorizeBuffered(final ColumnSelectorFactory metricFactory) + public BufferAggregator factorizeBuffered(final ColumnSelectorFactory columnSelectorFactory) { return new JavaScriptBufferAggregator( Lists.transform( fieldNames, - new com.google.common.base.Function() + new com.google.common.base.Function() { @Override - public FloatMetricSelector apply(@Nullable String s) - { - return metricFactory.makeFloatMetricSelector(s); + public ObjectColumnSelector apply(@Nullable String s) { + return columnSelectorFactory.makeObjectColumnSelector(s); } } ), @@ -182,8 +181,8 @@ public class JavaScriptAggregatorFactory implements AggregatorFactory { try { MessageDigest md = MessageDigest.getInstance("SHA-1"); - byte[] fieldNameBytes = Joiner.on(",").join(fieldNames).getBytes(); - byte[] sha1 = md.digest((fnAggregate+fnReset+fnCombine).getBytes()); + byte[] fieldNameBytes = Joiner.on(",").join(fieldNames).getBytes(Charsets.UTF_8); + byte[] sha1 = md.digest((fnAggregate+fnReset+fnCombine).getBytes(Charsets.UTF_8)); return ByteBuffer.allocate(1 + fieldNameBytes.length + sha1.length) .put(CACHE_TYPE_ID) @@ -242,7 +241,7 @@ public class JavaScriptAggregatorFactory implements AggregatorFactory return new JavaScriptAggregator.ScriptAggregator() { @Override - public double aggregate(final double current, final FloatMetricSelector[] selectorList) + public double aggregate(final double current, final ObjectColumnSelector[] selectorList) { Context cx = Context.getCurrentContext(); if(cx == null) cx = contextFactory.enterContext(); diff --git a/common/src/main/java/com/metamx/druid/aggregation/JavaScriptBufferAggregator.java b/common/src/main/java/com/metamx/druid/aggregation/JavaScriptBufferAggregator.java index 88f97b6a5dd..90cb171d972 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/JavaScriptBufferAggregator.java +++ b/common/src/main/java/com/metamx/druid/aggregation/JavaScriptBufferAggregator.java @@ -20,22 +20,24 @@ package com.metamx.druid.aggregation; import com.google.common.collect.Lists; +import com.metamx.druid.processing.ComplexMetricSelector; import com.metamx.druid.processing.FloatMetricSelector; +import com.metamx.druid.processing.ObjectColumnSelector; import java.nio.ByteBuffer; import java.util.List; public class JavaScriptBufferAggregator implements BufferAggregator { - private final FloatMetricSelector[] selectorList; + private final ObjectColumnSelector[] selectorList; private final JavaScriptAggregator.ScriptAggregator script; public JavaScriptBufferAggregator( - List selectorList, + List selectorList, JavaScriptAggregator.ScriptAggregator script ) { - this.selectorList = Lists.newArrayList(selectorList).toArray(new FloatMetricSelector[]{}); + this.selectorList = Lists.newArrayList(selectorList).toArray(new ObjectColumnSelector[]{}); this.script = script; } diff --git a/common/src/main/java/com/metamx/druid/processing/MetricSelectorUtils.java b/common/src/main/java/com/metamx/druid/processing/MetricSelectorUtils.java deleted file mode 100644 index 0db847b70d9..00000000000 --- a/common/src/main/java/com/metamx/druid/processing/MetricSelectorUtils.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.metamx.druid.processing; - -public class MetricSelectorUtils -{ -} diff --git a/common/src/test/java/com/metamx/druid/aggregation/JavaScriptAggregatorTest.java b/common/src/test/java/com/metamx/druid/aggregation/JavaScriptAggregatorTest.java index bb1a3a39075..98e4576a731 100644 --- a/common/src/test/java/com/metamx/druid/aggregation/JavaScriptAggregatorTest.java +++ b/common/src/test/java/com/metamx/druid/aggregation/JavaScriptAggregatorTest.java @@ -21,7 +21,7 @@ package com.metamx.druid.aggregation; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.metamx.druid.processing.FloatMetricSelector; +import com.metamx.druid.processing.ObjectColumnSelector; import org.junit.Assert; import org.junit.Test; @@ -77,7 +77,7 @@ public class JavaScriptAggregatorTest JavaScriptAggregator agg = new JavaScriptAggregator( "billy", - Arrays.asList(selector1, selector2), + Arrays.asList(MetricSelectorUtils.wrap(selector1), MetricSelectorUtils.wrap(selector2)), JavaScriptAggregatorFactory.compileScript(script.get("fnAggregate"), script.get("fnReset"), script.get("fnCombine")) @@ -113,7 +113,7 @@ public class JavaScriptAggregatorTest Map script = sumLogATimesBPlusTen; JavaScriptBufferAggregator agg = new JavaScriptBufferAggregator( - Arrays.asList(selector1, selector2), + Arrays.asList(MetricSelectorUtils.wrap(selector1), MetricSelectorUtils.wrap(selector2)), JavaScriptAggregatorFactory.compileScript(script.get("fnAggregate"), script.get("fnReset"), script.get("fnCombine")) @@ -164,7 +164,7 @@ public class JavaScriptAggregatorTest Map script = scriptDoubleSum; JavaScriptAggregator aggRhino = new JavaScriptAggregator( "billy", - Lists.asList(selector, new FloatMetricSelector[]{}), + Lists.asList(MetricSelectorUtils.wrap(selector), new ObjectColumnSelector[]{}), JavaScriptAggregatorFactory.compileScript(script.get("fnAggregate"), script.get("fnReset"), script.get("fnCombine")) diff --git a/common/src/test/java/com/metamx/druid/aggregation/MetricSelectorUtils.java b/common/src/test/java/com/metamx/druid/aggregation/MetricSelectorUtils.java new file mode 100644 index 00000000000..da49af820e2 --- /dev/null +++ b/common/src/test/java/com/metamx/druid/aggregation/MetricSelectorUtils.java @@ -0,0 +1,45 @@ +/* + * Druid - a distributed column store. + * Copyright (C) 2013 Metamarkets Group Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package com.metamx.druid.aggregation; + +import com.metamx.druid.processing.ComplexMetricSelector; +import com.metamx.druid.processing.FloatMetricSelector; +import com.metamx.druid.processing.ObjectColumnSelector; + +public class MetricSelectorUtils +{ + public static ObjectColumnSelector wrap(final FloatMetricSelector selector) + { + return new ObjectColumnSelector() + { + @Override + public Class classOfObject() + { + return Float.TYPE; + } + + @Override + public Float get() + { + return selector.get(); + } + }; + } +}