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);
}
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();
}
}
};
}
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);
}
};
}
};
}
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);
}
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);
}
Aggregations