Search in sources :

Example 11 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class DruidQuery method toScanQuery.

/**
 * Return this query as a Scan query, or null if this query is not compatible with Scan.
 *
 * @return query or null
 */
@Nullable
private ScanQuery toScanQuery(final QueryFeatureInspector queryFeatureInspector) {
    if (grouping != null) {
        // Scan cannot GROUP BY.
        return null;
    }
    if (outputRowSignature.size() == 0) {
        // Should never do a scan query without any columns that we're interested in. This is probably a planner bug.
        throw new ISE("Cannot convert to Scan query without any columns.");
    }
    final Pair<DataSource, Filtration> dataSourceFiltrationPair = getFiltration(dataSource, filter, virtualColumnRegistry);
    final DataSource newDataSource = dataSourceFiltrationPair.lhs;
    final Filtration filtration = dataSourceFiltrationPair.rhs;
    final List<ScanQuery.OrderBy> orderByColumns;
    long scanOffset = 0L;
    long scanLimit = 0L;
    if (sorting != null) {
        scanOffset = sorting.getOffsetLimit().getOffset();
        if (sorting.getOffsetLimit().hasLimit()) {
            final long limit = sorting.getOffsetLimit().getLimit();
            if (limit == 0) {
                // Can't handle zero limit (the Scan query engine would treat it as unlimited).
                return null;
            }
            scanLimit = limit;
        }
        orderByColumns = sorting.getOrderBys().stream().map(orderBy -> new ScanQuery.OrderBy(orderBy.getDimension(), orderBy.getDirection() == OrderByColumnSpec.Direction.DESCENDING ? ScanQuery.Order.DESCENDING : ScanQuery.Order.ASCENDING)).collect(Collectors.toList());
    } else {
        orderByColumns = Collections.emptyList();
    }
    if (!queryFeatureInspector.feature(QueryFeature.SCAN_CAN_ORDER_BY_NON_TIME) && (orderByColumns.size() > 1 || orderByColumns.stream().anyMatch(orderBy -> !orderBy.getColumnName().equals(ColumnHolder.TIME_COLUMN_NAME)))) {
        // Cannot handle this ordering.
        // Scan cannot ORDER BY non-time columns.
        plannerContext.setPlanningError("SQL query requires order by non-time column %s that is not supported.", orderByColumns);
        return null;
    }
    // Compute the list of columns to select, sorted and deduped.
    final SortedSet<String> scanColumns = new TreeSet<>(outputRowSignature.getColumnNames());
    orderByColumns.forEach(column -> scanColumns.add(column.getColumnName()));
    return new ScanQuery(newDataSource, filtration.getQuerySegmentSpec(), getVirtualColumns(true), ScanQuery.ResultFormat.RESULT_FORMAT_COMPACTED_LIST, 0, scanOffset, scanLimit, null, orderByColumns, filtration.getDimFilter(), ImmutableList.copyOf(scanColumns), false, ImmutableSortedMap.copyOf(plannerContext.getQueryContext()));
}
Also used : GroupByRules(org.apache.druid.sql.calcite.rule.GroupByRules) SortedSet(java.util.SortedSet) RowSignatures(org.apache.druid.sql.calcite.table.RowSignatures) OffsetLimit(org.apache.druid.sql.calcite.planner.OffsetLimit) Pair(org.apache.druid.java.util.common.Pair) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) DimFilterHavingSpec(org.apache.druid.query.groupby.having.DimFilterHavingSpec) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) QueryFeatureInspector(org.apache.druid.sql.calcite.run.QueryFeatureInspector) SqlKind(org.apache.calcite.sql.SqlKind) DimensionExpression(org.apache.druid.sql.calcite.aggregation.DimensionExpression) DataSource(org.apache.druid.query.DataSource) TopNMetricSpec(org.apache.druid.query.topn.TopNMetricSpec) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) ValueType(org.apache.druid.segment.column.ValueType) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) RexInputRef(org.apache.calcite.rex.RexInputRef) QueryDataSource(org.apache.druid.query.QueryDataSource) List(java.util.List) DimFilter(org.apache.druid.query.filter.DimFilter) Sort(org.apache.calcite.rel.core.Sort) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) Optional(java.util.Optional) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) Expressions(org.apache.druid.sql.calcite.expression.Expressions) Project(org.apache.calcite.rel.core.Project) Iterables(com.google.common.collect.Iterables) Granularity(org.apache.druid.java.util.common.granularity.Granularity) HashMap(java.util.HashMap) DimensionTopNMetricSpec(org.apache.druid.query.topn.DimensionTopNMetricSpec) Filter(org.apache.calcite.rel.core.Filter) QueryFeature(org.apache.druid.sql.calcite.run.QueryFeature) Iterators(com.google.common.collect.Iterators) ScanQuery(org.apache.druid.query.scan.ScanQuery) TopNQuery(org.apache.druid.query.topn.TopNQuery) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) HashSet(java.util.HashSet) ColumnHolder(org.apache.druid.segment.column.ColumnHolder) ImmutableList(com.google.common.collect.ImmutableList) Query(org.apache.druid.query.Query) StringComparators(org.apache.druid.query.ordering.StringComparators) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) JoinDataSource(org.apache.druid.query.JoinDataSource) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) Nonnull(javax.annotation.Nonnull) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) Filtration(org.apache.druid.sql.calcite.filtration.Filtration) RelDataType(org.apache.calcite.rel.type.RelDataType) VirtualColumns(org.apache.druid.segment.VirtualColumns) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) StringComparator(org.apache.druid.query.ordering.StringComparator) RexBuilder(org.apache.calcite.rex.RexBuilder) VirtualColumn(org.apache.druid.segment.VirtualColumn) Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) Ints(com.google.common.primitives.Ints) Aggregate(org.apache.calcite.rel.core.Aggregate) Granularities(org.apache.druid.java.util.common.granularity.Granularities) IntList(it.unimi.dsi.fastutil.ints.IntList) Types(org.apache.druid.segment.column.Types) RowSignature(org.apache.druid.segment.column.RowSignature) ColumnType(org.apache.druid.segment.column.ColumnType) Preconditions(com.google.common.base.Preconditions) AggregateCall(org.apache.calcite.rel.core.AggregateCall) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Collections(java.util.Collections) Filtration(org.apache.druid.sql.calcite.filtration.Filtration) ScanQuery(org.apache.druid.query.scan.ScanQuery) DataSource(org.apache.druid.query.DataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) JoinDataSource(org.apache.druid.query.JoinDataSource) TreeSet(java.util.TreeSet) ISE(org.apache.druid.java.util.common.ISE) Nullable(javax.annotation.Nullable)

Example 12 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class UnifiedIndexerAppenderatorsManagerTest method test_getBundle_unknownDataSource.

@Test
public void test_getBundle_unknownDataSource() {
    final ScanQuery query = Druids.newScanQueryBuilder().dataSource("unknown").intervals(new MultipleIntervalSegmentSpec(Intervals.ONLY_ETERNITY)).build();
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("Could not find segment walker for datasource");
    manager.getBundle(query);
}
Also used : ScanQuery(org.apache.druid.query.scan.ScanQuery) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 13 with ScanQuery

use of org.apache.druid.query.scan.ScanQuery in project druid by druid-io.

the class CalciteJoinQueryTest method testLeftJoinSubqueryWithNullKeyFilter.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testLeftJoinSubqueryWithNullKeyFilter(Map<String, Object> queryContext) throws Exception {
    // Cannot vectorize due to 'concat' expression.
    cannotVectorize();
    ScanQuery nullCompatibleModePlan = newScanQueryBuilder().dataSource(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(GroupByQuery.builder().setDataSource(new LookupDataSource("lookyloo")).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "concat(\"k\",'')", ColumnType.STRING)).setDimensions(new DefaultDimensionSpec("v0", "d0")).build()), "j0.", equalsCondition(makeColumnExpression("dim1"), makeColumnExpression("j0.d0")), JoinType.INNER)).intervals(querySegmentSpec(Filtration.eternity())).columns("dim1", "j0.d0").context(queryContext).build();
    ScanQuery nonNullCompatibleModePlan = newScanQueryBuilder().dataSource(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(GroupByQuery.builder().setDataSource(new LookupDataSource("lookyloo")).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "concat(\"k\",'')", ColumnType.STRING)).setDimensions(new DefaultDimensionSpec("v0", "d0")).build()), "j0.", equalsCondition(makeColumnExpression("dim1"), makeColumnExpression("j0.d0")), JoinType.LEFT)).intervals(querySegmentSpec(Filtration.eternity())).columns("dim1", "j0.d0").filters(new NotDimFilter(new SelectorDimFilter("j0.d0", null, null))).context(queryContext).build();
    boolean isJoinFilterRewriteEnabled = queryContext.getOrDefault(JOIN_FILTER_REWRITE_ENABLE_KEY, true).toString().equals("true");
    testQuery("SELECT dim1, l1.k\n" + "FROM foo\n" + "LEFT JOIN (select k || '' as k from lookup.lookyloo group by 1) l1 ON foo.dim1 = l1.k\n" + "WHERE l1.k IS NOT NULL\n", queryContext, ImmutableList.of(NullHandling.sqlCompatible() ? nullCompatibleModePlan : nonNullCompatibleModePlan), NullHandling.sqlCompatible() || !isJoinFilterRewriteEnabled ? ImmutableList.of(new Object[] { "abc", "abc" }) : ImmutableList.of(new Object[] { "10.1", "" }, // this result is incorrect. TODO : fix this result when the JoinFilterAnalyzer bug is fixed
    new Object[] { "2", "" }, new Object[] { "1", "" }, new Object[] { "def", "" }, new Object[] { "abc", "abc" }));
}
Also used : NotDimFilter(org.apache.druid.query.filter.NotDimFilter) GlobalTableDataSource(org.apache.druid.query.GlobalTableDataSource) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) LookupDataSource(org.apache.druid.query.LookupDataSource) ScanQuery(org.apache.druid.query.scan.ScanQuery) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

ScanQuery (org.apache.druid.query.scan.ScanQuery)13 Test (org.junit.Test)7 QueryDataSource (org.apache.druid.query.QueryDataSource)5 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)3 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 SegmentDescriptor (org.apache.druid.query.SegmentDescriptor)2 TableDataSource (org.apache.druid.query.TableDataSource)2 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)2 ScanResultValue (org.apache.druid.query.scan.ScanResultValue)2 MultipleSpecificSegmentSpec (org.apache.druid.query.spec.MultipleSpecificSegmentSpec)2 TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)2 TopNQuery (org.apache.druid.query.topn.TopNQuery)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 Iterables (com.google.common.collect.Iterables)1 Iterators (com.google.common.collect.Iterators)1 Ints (com.google.common.primitives.Ints)1