Search in sources :

Example 1 with ObjectColumnSelector

use of io.druid.segment.ObjectColumnSelector in project druid by druid-io.

the class ApproximateHistogramFoldingAggregatorFactory method factorize.

@Override
public Aggregator factorize(ColumnSelectorFactory metricFactory) {
    ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(fieldName);
    if (selector == null) {
        // gracefully handle undefined metrics
        selector = new ObjectColumnSelector<ApproximateHistogram>() {

            @Override
            public Class<ApproximateHistogram> classOfObject() {
                return ApproximateHistogram.class;
            }

            @Override
            public ApproximateHistogram get() {
                return new ApproximateHistogram(0);
            }
        };
    }
    final Class cls = selector.classOfObject();
    if (cls.equals(Object.class) || ApproximateHistogram.class.isAssignableFrom(cls)) {
        return new ApproximateHistogramFoldingAggregator(selector, resolution, lowerLimit, upperLimit);
    }
    throw new IAE("Incompatible type for metric[%s], expected a ApproximateHistogram, got a %s", fieldName, cls);
}
Also used : IAE(io.druid.java.util.common.IAE) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector)

Example 2 with ObjectColumnSelector

use of io.druid.segment.ObjectColumnSelector in project druid by druid-io.

the class JavaScriptAggregatorFactory method compileScript.

@VisibleForTesting
static JavaScriptAggregator.ScriptAggregator compileScript(final String aggregate, final String reset, final String combine) {
    final ContextFactory contextFactory = ContextFactory.getGlobal();
    Context context = contextFactory.enterContext();
    context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
    final ScriptableObject scope = context.initStandardObjects();
    final Function fnAggregate = context.compileFunction(scope, aggregate, "aggregate", 1, null);
    final Function fnReset = context.compileFunction(scope, reset, "reset", 1, null);
    final Function fnCombine = context.compileFunction(scope, combine, "combine", 1, null);
    Context.exit();
    return new JavaScriptAggregator.ScriptAggregator() {

        @Override
        public double aggregate(final double current, final ObjectColumnSelector[] selectorList) {
            Context cx = Context.getCurrentContext();
            if (cx == null) {
                cx = contextFactory.enterContext();
                // Disable primitive wrapping- we want Java strings and primitives to behave like JS entities.
                cx.getWrapFactory().setJavaPrimitiveWrap(false);
            }
            final int size = selectorList.length;
            final Object[] args = new Object[size + 1];
            args[0] = current;
            for (int i = 0; i < size; i++) {
                final ObjectColumnSelector selector = selectorList[i];
                if (selector != null) {
                    final Object arg = selector.get();
                    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)];
                        for (int j = 0; j < Array.getLength(arg); j++) {
                            arrayAsObjectArray[j] = Array.get(arg, j);
                        }
                        args[i + 1] = cx.newArray(scope, arrayAsObjectArray);
                    } else {
                        args[i + 1] = Context.javaToJS(arg, scope);
                    }
                }
            }
            final Object res = fnAggregate.call(cx, scope, scope, args);
            return Context.toNumber(res);
        }

        @Override
        public double combine(final double a, final double b) {
            final Object res = contextFactory.call(new ContextAction() {

                @Override
                public Object run(final Context cx) {
                    return fnCombine.call(cx, scope, scope, new Object[] { a, b });
                }
            });
            return Context.toNumber(res);
        }

        @Override
        public double reset() {
            final Object res = contextFactory.call(new ContextAction() {

                @Override
                public Object run(final Context cx) {
                    return fnReset.call(cx, scope, scope, new Object[] {});
                }
            });
            return Context.toNumber(res);
        }

        @Override
        public void close() {
            if (Context.getCurrentContext() != null) {
                Context.exit();
            }
        }
    };
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with ObjectColumnSelector

use of io.druid.segment.ObjectColumnSelector in project druid by druid-io.

the class LongFirstAggregatorFactory method getCombiningFactory.

@Override
public AggregatorFactory getCombiningFactory() {
    return new LongFirstAggregatorFactory(name, name) {

        @Override
        public Aggregator factorize(ColumnSelectorFactory metricFactory) {
            final ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(name);
            return new LongFirstAggregator(name, null, null) {

                @Override
                public void aggregate() {
                    SerializablePair<Long, Long> pair = (SerializablePair<Long, Long>) selector.get();
                    if (pair.lhs < firstTime) {
                        firstTime = pair.lhs;
                        firstValue = pair.rhs;
                    }
                }
            };
        }

        @Override
        public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory) {
            final ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(name);
            return new LongFirstBufferAggregator(null, null) {

                @Override
                public void aggregate(ByteBuffer buf, int position) {
                    SerializablePair<Long, Long> pair = (SerializablePair<Long, Long>) selector.get();
                    long firstTime = buf.getLong(position);
                    if (pair.lhs < firstTime) {
                        buf.putLong(position, pair.lhs);
                        buf.putLong(position + Longs.BYTES, pair.rhs);
                    }
                }

                @Override
                public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    inspector.visit("selector", selector);
                }
            };
        }
    };
}
Also used : SerializablePair(io.druid.collections.SerializablePair) ColumnSelectorFactory(io.druid.segment.ColumnSelectorFactory) RuntimeShapeInspector(io.druid.query.monomorphicprocessing.RuntimeShapeInspector) ByteBuffer(java.nio.ByteBuffer) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector)

Example 4 with ObjectColumnSelector

use of io.druid.segment.ObjectColumnSelector in project druid by druid-io.

the class HyperUniquesAggregatorFactory method factorize.

@Override
public Aggregator factorize(ColumnSelectorFactory metricFactory) {
    ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(fieldName);
    if (selector == null) {
        return NoopAggregator.instance();
    }
    final Class classOfObject = selector.classOfObject();
    if (classOfObject.equals(Object.class) || HyperLogLogCollector.class.isAssignableFrom(classOfObject)) {
        return new HyperUniquesAggregator(selector);
    }
    throw new IAE("Incompatible type for metric[%s], expected a HyperUnique, got a %s", fieldName, classOfObject);
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) IAE(io.druid.java.util.common.IAE) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector)

Example 5 with ObjectColumnSelector

use of io.druid.segment.ObjectColumnSelector in project druid by druid-io.

the class HyperUniquesAggregatorFactory method factorizeBuffered.

@Override
public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory) {
    ObjectColumnSelector selector = metricFactory.makeObjectColumnSelector(fieldName);
    if (selector == null) {
        return NoopBufferAggregator.instance();
    }
    final Class classOfObject = selector.classOfObject();
    if (classOfObject.equals(Object.class) || HyperLogLogCollector.class.isAssignableFrom(classOfObject)) {
        return new HyperUniquesBufferAggregator(selector);
    }
    throw new IAE("Incompatible type for metric[%s], expected a HyperUnique, got a %s", fieldName, classOfObject);
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) IAE(io.druid.java.util.common.IAE) ObjectColumnSelector(io.druid.segment.ObjectColumnSelector)

Aggregations

ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)20 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)6 LongColumnSelector (io.druid.segment.LongColumnSelector)6 RuntimeShapeInspector (io.druid.query.monomorphicprocessing.RuntimeShapeInspector)5 ColumnSelectorFactory (io.druid.segment.ColumnSelectorFactory)5 DimensionSelector (io.druid.segment.DimensionSelector)5 SerializablePair (io.druid.collections.SerializablePair)4 IAE (io.druid.java.util.common.IAE)4 DimensionSpec (io.druid.query.dimension.DimensionSpec)4 Cursor (io.druid.segment.Cursor)4 FloatColumnSelector (io.druid.segment.FloatColumnSelector)4 ByteBuffer (java.nio.ByteBuffer)4 Test (org.junit.Test)4 ISE (io.druid.java.util.common.ISE)3 TestFloatColumnSelector (io.druid.segment.TestFloatColumnSelector)3 TestLongColumnSelector (io.druid.segment.TestLongColumnSelector)3 VirtualColumns (io.druid.segment.VirtualColumns)3 List (java.util.List)3 Map (java.util.Map)3 DateTime (org.joda.time.DateTime)3