Search in sources :

Example 11 with BaseObjectColumnValueSelector

use of org.apache.druid.segment.BaseObjectColumnValueSelector in project druid by druid-io.

the class VirtualColumnsTest method testMakeSelectorsWithDotSupportBaseNameOnly.

@Test
public void testMakeSelectorsWithDotSupportBaseNameOnly() {
    final VirtualColumns virtualColumns = makeVirtualColumns();
    final BaseObjectColumnValueSelector objectSelector = virtualColumns.makeColumnValueSelector("foo", null);
    final DimensionSelector dimensionSelector = virtualColumns.makeDimensionSelector(new DefaultDimensionSpec("foo", "x"), null);
    final BaseFloatColumnValueSelector floatSelector = virtualColumns.makeColumnValueSelector("foo", null);
    final BaseLongColumnValueSelector longSelector = virtualColumns.makeColumnValueSelector("foo", null);
    Assert.assertEquals(-1L, objectSelector.getObject());
    Assert.assertEquals("-1", dimensionSelector.lookupName(dimensionSelector.getRow().get(0)));
    Assert.assertEquals(-1.0f, floatSelector.getFloat(), 0.0f);
    Assert.assertEquals(-1L, longSelector.getLong());
}
Also used : BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VirtualColumns(org.apache.druid.segment.VirtualColumns) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 12 with BaseObjectColumnValueSelector

use of org.apache.druid.segment.BaseObjectColumnValueSelector 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 BaseObjectColumnValueSelector[] 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 BaseObjectColumnValueSelector selector = selectorList[i];
                if (selector != null) {
                    final Object arg = selector.getObject();
                    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.
                        args[i + 1] = cx.newArray(scope, arrayToObjectArray(arg));
                    } else if (arg instanceof List) {
                        // Using toArray(Object[]), instead of just toArray(), because Arrays.asList()'s impl and similar List
                        // impls could clone the underlying array in toArray(), that could be not Object[], but e. g. String[].
                        args[i + 1] = cx.newArray(scope, ((List) arg).toArray(ObjectArrays.EMPTY_ARRAY));
                    } else {
                        args[i + 1] = Context.javaToJS(arg, scope);
                    }
                }
            }
            final Object res = fnAggregate.call(cx, scope, scope, args);
            return Context.toNumber(res);
        }

        private Object[] arrayToObjectArray(Object array) {
            int len = Array.getLength(array);
            final Object[] objectArray = new Object[len];
            for (int j = 0; j < len; j++) {
                objectArray[j] = Array.get(array, j);
            }
            return objectArray;
        }

        @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) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) ScriptableObject(org.mozilla.javascript.ScriptableObject) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

BaseObjectColumnValueSelector (org.apache.druid.segment.BaseObjectColumnValueSelector)12 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)6 Test (org.junit.Test)6 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)5 BaseFloatColumnValueSelector (org.apache.druid.segment.BaseFloatColumnValueSelector)4 BaseLongColumnValueSelector (org.apache.druid.segment.BaseLongColumnValueSelector)4 DimensionSelector (org.apache.druid.segment.DimensionSelector)4 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)3 ImmutableList (com.google.common.collect.ImmutableList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)2 DateTimes (org.apache.druid.java.util.common.DateTimes)2 IAE (org.apache.druid.java.util.common.IAE)2 Sequence (org.apache.druid.java.util.common.guava.Sequence)2 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)2 BucketExtractionFn (org.apache.druid.query.extraction.BucketExtractionFn)2 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)2 VirtualColumns (org.apache.druid.segment.VirtualColumns)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1