Search in sources :

Example 21 with ColumnValueSelector

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

the class TDigestSketchAggregatorFactory method makeAggregateCombiner.

@Override
public AggregateCombiner makeAggregateCombiner() {
    return new ObjectAggregateCombiner<MergingDigest>() {

        private MergingDigest combined = new MergingDigest(compression);

        @Override
        public void reset(final ColumnValueSelector selector) {
            combined = null;
            fold(selector);
        }

        @Override
        public void fold(final ColumnValueSelector selector) {
            MergingDigest other = (MergingDigest) selector.getObject();
            if (other == null) {
                return;
            }
            if (combined == null) {
                combined = new MergingDigest(compression);
            }
            combined.add(other);
        }

        @Nullable
        @Override
        public MergingDigest getObject() {
            return combined;
        }

        @Override
        public Class<MergingDigest> classOfObject() {
            return MergingDigest.class;
        }
    };
}
Also used : MergingDigest(com.tdunning.math.stats.MergingDigest) ObjectAggregateCombiner(org.apache.druid.query.aggregation.ObjectAggregateCombiner) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector)

Example 22 with ColumnValueSelector

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

the class ArrayOfDoublesSketchAggregatorFactory method makeAggregateCombiner.

@Override
public AggregateCombiner makeAggregateCombiner() {
    return new ObjectAggregateCombiner<ArrayOfDoublesSketch>() {

        private final ArrayOfDoublesUnion union = new ArrayOfDoublesSetOperationBuilder().setNominalEntries(nominalEntries).setNumberOfValues(numberOfValues).buildUnion();

        @Override
        public void reset(final ColumnValueSelector selector) {
            union.reset();
            fold(selector);
        }

        @Override
        public void fold(final ColumnValueSelector selector) {
            final ArrayOfDoublesSketch sketch = (ArrayOfDoublesSketch) selector.getObject();
            union.union(sketch);
        }

        @Override
        public ArrayOfDoublesSketch getObject() {
            return union.getResult();
        }

        @Override
        public Class<ArrayOfDoublesSketch> classOfObject() {
            return ArrayOfDoublesSketch.class;
        }
    };
}
Also used : ArrayOfDoublesUnion(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUnion) ArrayOfDoublesSetOperationBuilder(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSetOperationBuilder) ObjectAggregateCombiner(org.apache.druid.query.aggregation.ObjectAggregateCombiner) ArrayOfDoublesSketch(org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesSketch) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) NilColumnValueSelector(org.apache.druid.segment.NilColumnValueSelector) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) BaseDoubleColumnValueSelector(org.apache.druid.segment.BaseDoubleColumnValueSelector)

Example 23 with ColumnValueSelector

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

the class ExpressionSelectors method createBindings.

/**
 * Create {@link Expr.ObjectBinding} given a {@link ColumnSelectorFactory} and {@link ExpressionPlan} which
 * provides the set of identifiers which need a binding (list of required columns), and context of whether or not they
 * are used as array or scalar inputs
 */
public static Expr.ObjectBinding createBindings(ColumnSelectorFactory columnSelectorFactory, ExpressionPlan plan) {
    final List<String> columns = plan.getAnalysis().getRequiredBindingsList();
    final Map<String, Pair<ExpressionType, Supplier<Object>>> suppliers = new HashMap<>();
    for (String columnName : columns) {
        final ColumnCapabilities capabilities = columnSelectorFactory.getColumnCapabilities(columnName);
        final boolean multiVal = capabilities != null && capabilities.hasMultipleValues().isTrue();
        final Supplier<Object> supplier;
        final ExpressionType expressionType = ExpressionType.fromColumnType(capabilities);
        final boolean useObjectSupplierForMultiValueStringArray = capabilities != null && // multi-value rows, we can just use the dimension selector, which has the homogenization behavior built-in
        ((!capabilities.is(ValueType.STRING)) || (capabilities.is(ValueType.STRING) && !ExpressionProcessing.isHomogenizeNullMultiValueStringArrays() && !plan.is(ExpressionPlan.Trait.NEEDS_APPLIED))) && // expression has array output
        plan.is(ExpressionPlan.Trait.NON_SCALAR_OUTPUT);
        final boolean homogenizeNullMultiValueStringArrays = plan.is(ExpressionPlan.Trait.NEEDS_APPLIED) || ExpressionProcessing.isHomogenizeNullMultiValueStringArrays();
        if (capabilities == null || capabilities.isArray() || useObjectSupplierForMultiValueStringArray) {
            // Unknown type, array type, or output array uses an Object selector and see if that gives anything useful
            supplier = supplierFromObjectSelector(columnSelectorFactory.makeColumnValueSelector(columnName), homogenizeNullMultiValueStringArrays);
        } else if (capabilities.is(ValueType.FLOAT)) {
            ColumnValueSelector<?> selector = columnSelectorFactory.makeColumnValueSelector(columnName);
            supplier = makeNullableNumericSupplier(selector, selector::getFloat);
        } else if (capabilities.is(ValueType.LONG)) {
            ColumnValueSelector<?> selector = columnSelectorFactory.makeColumnValueSelector(columnName);
            supplier = makeNullableNumericSupplier(selector, selector::getLong);
        } else if (capabilities.is(ValueType.DOUBLE)) {
            ColumnValueSelector<?> selector = columnSelectorFactory.makeColumnValueSelector(columnName);
            supplier = makeNullableNumericSupplier(selector, selector::getDouble);
        } else if (capabilities.is(ValueType.STRING)) {
            supplier = supplierFromDimensionSelector(columnSelectorFactory.makeDimensionSelector(new DefaultDimensionSpec(columnName, columnName)), multiVal, homogenizeNullMultiValueStringArrays);
        } else {
            // complex type just pass straight through
            ColumnValueSelector<?> selector = columnSelectorFactory.makeColumnValueSelector(columnName);
            if (!(selector instanceof NilColumnValueSelector)) {
                supplier = selector::getObject;
            } else {
                supplier = null;
            }
        }
        if (supplier != null) {
            suppliers.put(columnName, new Pair<>(expressionType, supplier));
        }
    }
    if (suppliers.isEmpty()) {
        return InputBindings.nilBindings();
    } else if (suppliers.size() == 1 && columns.size() == 1) {
        // If there's only one column (and it has a supplier), we can skip the Map and just use that supplier when
        // asked for something.
        final String column = Iterables.getOnlyElement(suppliers.keySet());
        final Pair<ExpressionType, Supplier<Object>> supplier = Iterables.getOnlyElement(suppliers.values());
        return new Expr.ObjectBinding() {

            @Nullable
            @Override
            public Object get(String name) {
                // There's only one binding, and it must be the single column, so it can safely be ignored in production.
                assert column.equals(name);
                return supplier.rhs.get();
            }

            @Nullable
            @Override
            public ExpressionType getType(String name) {
                return supplier.lhs;
            }
        };
    } else {
        return InputBindings.withTypedSuppliers(suppliers);
    }
}
Also used : HashMap(java.util.HashMap) ColumnCapabilities(org.apache.druid.segment.column.ColumnCapabilities) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) NilColumnValueSelector(org.apache.druid.segment.NilColumnValueSelector) Expr(org.apache.druid.math.expr.Expr) ExpressionType(org.apache.druid.math.expr.ExpressionType) Nullable(javax.annotation.Nullable) Pair(org.apache.druid.java.util.common.Pair) NonnullPair(org.apache.druid.java.util.common.NonnullPair) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) NilColumnValueSelector(org.apache.druid.segment.NilColumnValueSelector) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector)

Example 24 with ColumnValueSelector

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

the class IncrementalIndexRowIterator method makeRowPointer.

private static RowPointer makeRowPointer(IncrementalIndex incrementalIndex, IncrementalIndexRowHolder rowHolder, RowNumCounter rowNumCounter) {
    ColumnSelectorFactory columnSelectorFactory = new IncrementalIndexColumnSelectorFactory(new IncrementalIndexStorageAdapter(incrementalIndex), VirtualColumns.EMPTY, false, rowHolder);
    ColumnValueSelector[] dimensionSelectors = incrementalIndex.getDimensions().stream().map(dim -> {
        ColumnValueSelector selectorWithUnsortedValues = columnSelectorFactory.makeColumnValueSelector(dim.getName());
        return dim.getIndexer().convertUnsortedValuesToSorted(selectorWithUnsortedValues);
    }).toArray(ColumnValueSelector[]::new);
    List<DimensionHandler> dimensionHandlers = incrementalIndex.getDimensions().stream().map(IncrementalIndex.DimensionDesc::getHandler).collect(Collectors.toList());
    ColumnValueSelector[] metricSelectors = incrementalIndex.getMetricNames().stream().map(columnSelectorFactory::makeColumnValueSelector).toArray(ColumnValueSelector[]::new);
    return new RowPointer(rowHolder, dimensionSelectors, dimensionHandlers, metricSelectors, incrementalIndex.getMetricNames(), rowNumCounter);
}
Also used : VirtualColumns(org.apache.druid.segment.VirtualColumns) Iterator(java.util.Iterator) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) IndexableAdapter(org.apache.druid.segment.IndexableAdapter) TimeAndDimsPointer(org.apache.druid.segment.TimeAndDimsPointer) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) List(java.util.List) DimensionHandler(org.apache.druid.segment.DimensionHandler) RowPointer(org.apache.druid.segment.RowPointer) RowNumCounter(org.apache.druid.segment.RowNumCounter) TransformableRowIterator(org.apache.druid.segment.TransformableRowIterator) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) DimensionHandler(org.apache.druid.segment.DimensionHandler) RowPointer(org.apache.druid.segment.RowPointer) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector)

Example 25 with ColumnValueSelector

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

the class ExpressionSelectorsTest method test_long_bindings.

@Test
public void test_long_bindings() {
    final String columnName = "long3";
    for (StorageAdapter adapter : ADAPTERS) {
        Sequence<Cursor> cursorSequence = adapter.makeCursors(null, adapter.getInterval(), VirtualColumns.EMPTY, Granularities.ALL, false, null);
        List<Cursor> flatten = cursorSequence.toList();
        for (Cursor cursor : flatten) {
            ColumnSelectorFactory factory = cursor.getColumnSelectorFactory();
            // an assortment of plans
            ExpressionPlan plan = ExpressionPlanner.plan(adapter, Parser.parse("\"long3\"", TestExprMacroTable.INSTANCE));
            ExpressionPlan plan2 = ExpressionPlanner.plan(adapter, Parser.parse("\"long3\" + 3", TestExprMacroTable.INSTANCE));
            Expr.ObjectBinding bindings = ExpressionSelectors.createBindings(factory, plan);
            Expr.ObjectBinding bindings2 = ExpressionSelectors.createBindings(factory, plan2);
            ColumnValueSelector valueSelector = factory.makeColumnValueSelector(columnName);
            while (!cursor.isDone()) {
                Object bindingVal = bindings.get(columnName);
                Object bindingVal2 = bindings2.get(columnName);
                if (valueSelector.isNull()) {
                    Assert.assertNull(valueSelector.getObject());
                    Assert.assertNull(bindingVal);
                    Assert.assertNull(bindingVal2);
                } else {
                    Assert.assertEquals(valueSelector.getObject(), bindingVal);
                    Assert.assertEquals(valueSelector.getLong(), bindingVal);
                    Assert.assertEquals(valueSelector.getObject(), bindingVal2);
                    Assert.assertEquals(valueSelector.getLong(), bindingVal2);
                }
                cursor.advance();
            }
        }
    }
}
Also used : ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) Expr(org.apache.druid.math.expr.Expr) StorageAdapter(org.apache.druid.segment.StorageAdapter) IncrementalIndexStorageAdapter(org.apache.druid.segment.incremental.IncrementalIndexStorageAdapter) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) Cursor(org.apache.druid.segment.Cursor) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)32 Test (org.junit.Test)18 Cursor (org.apache.druid.segment.Cursor)13 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)12 ImmutableList (com.google.common.collect.ImmutableList)9 TestExprMacroTable (org.apache.druid.query.expression.TestExprMacroTable)9 ResultRow (org.apache.druid.query.groupby.ResultRow)9 VirtualColumns (org.apache.druid.segment.VirtualColumns)9 List (java.util.List)8 NullHandling (org.apache.druid.common.config.NullHandling)8 Granularities (org.apache.druid.java.util.common.granularity.Granularities)8 Sequence (org.apache.druid.java.util.common.guava.Sequence)8 Closer (org.apache.druid.java.util.common.io.Closer)8 QueryableIndex (org.apache.druid.segment.QueryableIndex)8 GeneratorSchemaInfo (org.apache.druid.segment.generator.GeneratorSchemaInfo)8 SegmentGenerator (org.apache.druid.segment.generator.SegmentGenerator)8 DataSegment (org.apache.druid.timeline.DataSegment)8 LinearShardSpec (org.apache.druid.timeline.partition.LinearShardSpec)8 TimeUnit (java.util.concurrent.TimeUnit)7 Expr (org.apache.druid.math.expr.Expr)7