Search in sources :

Example 26 with RowSignature

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

the class DefaultLimitSpec method makeComparator.

private Ordering<ResultRow> makeComparator(RowSignature rowSignature, boolean hasTimestamp, List<DimensionSpec> dimensions, List<AggregatorFactory> aggs, List<PostAggregator> postAggs, boolean sortByDimsFirst) {
    final Ordering<ResultRow> timeOrdering;
    if (hasTimestamp) {
        timeOrdering = new Ordering<ResultRow>() {

            @Override
            public int compare(ResultRow left, ResultRow right) {
                return Longs.compare(left.getLong(0), right.getLong(0));
            }
        };
    } else {
        timeOrdering = null;
    }
    Map<String, DimensionSpec> dimensionsMap = new HashMap<>();
    for (DimensionSpec spec : dimensions) {
        dimensionsMap.put(spec.getOutputName(), spec);
    }
    Map<String, AggregatorFactory> aggregatorsMap = new HashMap<>();
    for (final AggregatorFactory agg : aggs) {
        aggregatorsMap.put(agg.getName(), agg);
    }
    Map<String, PostAggregator> postAggregatorsMap = new HashMap<>();
    for (PostAggregator postAgg : postAggs) {
        postAggregatorsMap.put(postAgg.getName(), postAgg);
    }
    Ordering<ResultRow> ordering = null;
    for (OrderByColumnSpec columnSpec : columns) {
        String columnName = columnSpec.getDimension();
        Ordering<ResultRow> nextOrdering = null;
        final int columnIndex = rowSignature.indexOf(columnName);
        if (columnIndex >= 0) {
            if (postAggregatorsMap.containsKey(columnName)) {
                // noinspection unchecked
                nextOrdering = metricOrdering(columnIndex, postAggregatorsMap.get(columnName).getComparator());
            } else if (aggregatorsMap.containsKey(columnName)) {
                // noinspection unchecked
                nextOrdering = metricOrdering(columnIndex, aggregatorsMap.get(columnName).getComparator());
            } else if (dimensionsMap.containsKey(columnName)) {
                Optional<DimensionSpec> dimensionSpec = dimensions.stream().filter(ds -> ds.getOutputName().equals(columnName)).findFirst();
                if (!dimensionSpec.isPresent()) {
                    throw new ISE("Could not find the dimension spec for ordering column %s", columnName);
                }
                nextOrdering = dimensionOrdering(columnIndex, dimensionSpec.get().getOutputType(), columnSpec.getDimensionComparator());
            }
        }
        if (nextOrdering == null) {
            throw new ISE("Unknown column in order clause[%s]", columnSpec);
        }
        if (columnSpec.getDirection() == OrderByColumnSpec.Direction.DESCENDING) {
            nextOrdering = nextOrdering.reverse();
        }
        ordering = ordering == null ? nextOrdering : ordering.compound(nextOrdering);
    }
    if (ordering == null) {
        ordering = timeOrdering;
    } else if (timeOrdering != null) {
        ordering = sortByDimsFirst ? ordering.compound(timeOrdering) : timeOrdering.compound(ordering);
    }
    // noinspection unchecked
    return ordering != null ? ordering : (Ordering) Ordering.allEqual();
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) ComparableList(org.apache.druid.segment.data.ComparableList) DimensionHandlerUtils(org.apache.druid.segment.DimensionHandlerUtils) Rows(org.apache.druid.data.input.Rows) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) HashSet(java.util.HashSet) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) StringComparators(org.apache.druid.query.ordering.StringComparators) ComparableStringArray(org.apache.druid.segment.data.ComparableStringArray) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) Sequences(org.apache.druid.java.util.common.guava.Sequences) Nullable(javax.annotation.Nullable) Functions(com.google.common.base.Functions) Sequence(org.apache.druid.java.util.common.guava.Sequence) Longs(com.google.common.primitives.Longs) Function(com.google.common.base.Function) StringComparator(org.apache.druid.query.ordering.StringComparator) ResultRow(org.apache.druid.query.groupby.ResultRow) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) ValueType(org.apache.druid.segment.column.ValueType) Collectors(java.util.stream.Collectors) Granularities(org.apache.druid.java.util.common.granularity.Granularities) Objects(java.util.Objects) List(java.util.List) Ordering(com.google.common.collect.Ordering) NullHandling(org.apache.druid.common.config.NullHandling) RowSignature(org.apache.druid.segment.column.RowSignature) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) ColumnType(org.apache.druid.segment.column.ColumnType) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) TopNSequence(org.apache.druid.java.util.common.guava.TopNSequence) Collections(java.util.Collections) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) HashMap(java.util.HashMap) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) ISE(org.apache.druid.java.util.common.ISE)

Example 27 with RowSignature

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

the class FieldAccessPostAggregatorTest method testGetTypeBeforeDecorateNil.

@Test
public void testGetTypeBeforeDecorateNil() {
    FieldAccessPostAggregator postAgg = new FieldAccessPostAggregator("name", "column");
    RowSignature signature = RowSignature.builder().build();
    Assert.assertNull(postAgg.getType(signature));
}
Also used : RowSignature(org.apache.druid.segment.column.RowSignature) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 28 with RowSignature

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

the class BaseFilterTest method selectColumnValuesMatchingFilterUsingRowBasedColumnSelectorFactory.

private List<String> selectColumnValuesMatchingFilterUsingRowBasedColumnSelectorFactory(final DimFilter filter, final String selectColumn) {
    // Generate rowSignature
    final RowSignature.Builder rowSignatureBuilder = RowSignature.builder();
    for (String columnName : Iterables.concat(adapter.getAvailableDimensions(), adapter.getAvailableMetrics())) {
        rowSignatureBuilder.add(columnName, adapter.getColumnCapabilities(columnName).toColumnType());
    }
    // Perform test
    final SettableSupplier<InputRow> rowSupplier = new SettableSupplier<>();
    final ValueMatcher matcher = makeFilter(filter).makeMatcher(VIRTUAL_COLUMNS.wrap(RowBasedColumnSelectorFactory.create(RowAdapters.standardRow(), rowSupplier::get, rowSignatureBuilder.build(), false)));
    final List<String> values = new ArrayList<>();
    for (InputRow row : rows) {
        rowSupplier.set(row);
        if (matcher.matches()) {
            values.add((String) row.getRaw(selectColumn));
        }
    }
    return values;
}
Also used : SettableSupplier(org.apache.druid.common.guava.SettableSupplier) VectorValueMatcher(org.apache.druid.query.filter.vector.VectorValueMatcher) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) InputRow(org.apache.druid.data.input.InputRow) ArrayList(java.util.ArrayList) RowSignature(org.apache.druid.segment.column.RowSignature)

Example 29 with RowSignature

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

the class CalciteInsertDmlTest method testInsertWithPartitionedByAndLimitOffset.

@Test
public void testInsertWithPartitionedByAndLimitOffset() {
    RowSignature targetRowSignature = RowSignature.builder().add("__time", ColumnType.LONG).add("floor_m1", ColumnType.FLOAT).add("dim1", ColumnType.STRING).build();
    testInsertQuery().sql("INSERT INTO druid.dst SELECT __time, FLOOR(m1) as floor_m1, dim1 FROM foo LIMIT 10 OFFSET 20 PARTITIONED BY DAY").expectTarget("dst", targetRowSignature).expectResources(dataSourceRead("foo"), dataSourceWrite("dst")).expectQuery(newScanQueryBuilder().dataSource("foo").intervals(querySegmentSpec(Filtration.eternity())).columns("__time", "dim1", "v0").virtualColumns(expressionVirtualColumn("v0", "floor(\"m1\")", ColumnType.FLOAT)).limit(10).offset(20).context(queryContextWithGranularity(Granularities.DAY)).build()).verify();
}
Also used : RowSignature(org.apache.druid.segment.column.RowSignature) Test(org.junit.Test)

Example 30 with RowSignature

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

the class CalciteInsertDmlTest method testInsertWithClusteredBy.

@Test
public void testInsertWithClusteredBy() {
    // Test correctness of the query when only CLUSTERED BY clause is present
    RowSignature targetRowSignature = RowSignature.builder().add("__time", ColumnType.LONG).add("floor_m1", ColumnType.FLOAT).add("dim1", ColumnType.STRING).add("EXPR$3", ColumnType.DOUBLE).build();
    testInsertQuery().sql("INSERT INTO druid.dst " + "SELECT __time, FLOOR(m1) as floor_m1, dim1, CEIL(m2) FROM foo " + "PARTITIONED BY FLOOR(__time TO DAY) CLUSTERED BY 2, dim1 DESC, CEIL(m2)").expectTarget("dst", targetRowSignature).expectResources(dataSourceRead("foo"), dataSourceWrite("dst")).expectQuery(newScanQueryBuilder().dataSource("foo").intervals(querySegmentSpec(Filtration.eternity())).columns("__time", "dim1", "v0", "v1").virtualColumns(expressionVirtualColumn("v0", "floor(\"m1\")", ColumnType.FLOAT), expressionVirtualColumn("v1", "ceil(\"m2\")", ColumnType.DOUBLE)).orderBy(ImmutableList.of(new ScanQuery.OrderBy("v0", ScanQuery.Order.ASCENDING), new ScanQuery.OrderBy("dim1", ScanQuery.Order.DESCENDING), new ScanQuery.OrderBy("v1", ScanQuery.Order.ASCENDING))).context(queryContextWithGranularity(Granularities.DAY)).build()).verify();
}
Also used : RowSignature(org.apache.druid.segment.column.RowSignature) Test(org.junit.Test)

Aggregations

RowSignature (org.apache.druid.segment.column.RowSignature)46 ColumnType (org.apache.druid.segment.column.ColumnType)17 List (java.util.List)14 Test (org.junit.Test)13 Collectors (java.util.stream.Collectors)12 Nullable (javax.annotation.Nullable)11 PlannerContext (org.apache.druid.sql.calcite.planner.PlannerContext)11 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)10 Expressions (org.apache.druid.sql.calcite.expression.Expressions)10 Project (org.apache.calcite.rel.core.Project)9 RexLiteral (org.apache.calcite.rex.RexLiteral)9 RexNode (org.apache.calcite.rex.RexNode)9 SqlKind (org.apache.calcite.sql.SqlKind)9 ISE (org.apache.druid.java.util.common.ISE)9 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)9 ArrayList (java.util.ArrayList)8 StringUtils (org.apache.druid.java.util.common.StringUtils)8 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)8 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableSet (com.google.common.collect.ImmutableSet)6