Search in sources :

Example 1 with Filtration

use of io.druid.sql.calcite.filtration.Filtration in project druid by druid-io.

the class DruidQueryBuilder method toTopNQuery.

/**
   * Return this query as a TopN query, or null if this query is not compatible with TopN.
   *
   * @param dataSource         data source to query
   * @param sourceRowSignature row signature of the dataSource
   * @param context            query context
   * @param maxTopNLimit       maxTopNLimit from a PlannerConfig
   * @param useApproximateTopN from a PlannerConfig
   *
   * @return query or null
   */
public TopNQuery toTopNQuery(final DataSource dataSource, final RowSignature sourceRowSignature, final Map<String, Object> context, final int maxTopNLimit, final boolean useApproximateTopN) {
    // Must have GROUP BY one column, ORDER BY zero or one column, limit less than maxTopNLimit, and no HAVING.
    final boolean topNOk = grouping != null && grouping.getDimensions().size() == 1 && limitSpec != null && (limitSpec.getColumns().size() <= 1 && limitSpec.getLimit() <= maxTopNLimit) && having == null;
    if (!topNOk) {
        return null;
    }
    final DimensionSpec dimensionSpec = Iterables.getOnlyElement(grouping.getDimensions());
    final OrderByColumnSpec limitColumn;
    if (limitSpec.getColumns().isEmpty()) {
        limitColumn = new OrderByColumnSpec(dimensionSpec.getOutputName(), OrderByColumnSpec.Direction.ASCENDING, StringComparators.LEXICOGRAPHIC);
    } else {
        limitColumn = Iterables.getOnlyElement(limitSpec.getColumns());
    }
    final TopNMetricSpec topNMetricSpec;
    if (limitColumn.getDimension().equals(dimensionSpec.getOutputName())) {
        // DimensionTopNMetricSpec is exact; always return it even if allowApproximate is false.
        final DimensionTopNMetricSpec baseMetricSpec = new DimensionTopNMetricSpec(null, limitColumn.getDimensionComparator());
        topNMetricSpec = limitColumn.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? baseMetricSpec : new InvertedTopNMetricSpec(baseMetricSpec);
    } else if (useApproximateTopN) {
        // ORDER BY metric
        final NumericTopNMetricSpec baseMetricSpec = new NumericTopNMetricSpec(limitColumn.getDimension());
        topNMetricSpec = limitColumn.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? new InvertedTopNMetricSpec(baseMetricSpec) : baseMetricSpec;
    } else {
        return null;
    }
    final Filtration filtration = Filtration.create(filter).optimize(sourceRowSignature);
    return new TopNQuery(dataSource, VirtualColumns.EMPTY, Iterables.getOnlyElement(grouping.getDimensions()), topNMetricSpec, limitSpec.getLimit(), filtration.getQuerySegmentSpec(), filtration.getDimFilter(), Granularities.ALL, grouping.getAggregatorFactories(), grouping.getPostAggregators(), context);
}
Also used : OrderByColumnSpec(io.druid.query.groupby.orderby.OrderByColumnSpec) DimensionSpec(io.druid.query.dimension.DimensionSpec) DimensionTopNMetricSpec(io.druid.query.topn.DimensionTopNMetricSpec) InvertedTopNMetricSpec(io.druid.query.topn.InvertedTopNMetricSpec) Filtration(io.druid.sql.calcite.filtration.Filtration) NumericTopNMetricSpec(io.druid.query.topn.NumericTopNMetricSpec) TopNQuery(io.druid.query.topn.TopNQuery) TopNMetricSpec(io.druid.query.topn.TopNMetricSpec) NumericTopNMetricSpec(io.druid.query.topn.NumericTopNMetricSpec) DimensionTopNMetricSpec(io.druid.query.topn.DimensionTopNMetricSpec) InvertedTopNMetricSpec(io.druid.query.topn.InvertedTopNMetricSpec)

Example 2 with Filtration

use of io.druid.sql.calcite.filtration.Filtration in project druid by druid-io.

the class DruidQueryRel method explainTerms.

@Override
public RelWriter explainTerms(final RelWriter pw) {
    pw.item("dataSource", druidTable.getDataSource());
    if (queryBuilder != null) {
        final Filtration filtration = Filtration.create(queryBuilder.getFilter()).optimize(getSourceRowSignature());
        if (!filtration.getIntervals().equals(ImmutableList.of(Filtration.eternity()))) {
            pw.item("intervals", filtration.getIntervals());
        }
        if (filtration.getDimFilter() != null) {
            pw.item("filter", filtration.getDimFilter());
        }
        if (queryBuilder.getSelectProjection() != null) {
            pw.item("selectDimensions", queryBuilder.getSelectProjection().getDimensions());
            pw.item("selectMetrics", queryBuilder.getSelectProjection().getMetrics());
        }
        if (queryBuilder.getGrouping() != null) {
            pw.item("dimensions", queryBuilder.getGrouping().getDimensions());
            pw.item("aggregations", queryBuilder.getGrouping().getAggregations());
        }
        if (queryBuilder.getHaving() != null) {
            pw.item("having", queryBuilder.getHaving());
        }
        if (queryBuilder.getLimitSpec() != null) {
            pw.item("limitSpec", queryBuilder.getLimitSpec());
        }
    }
    return pw;
}
Also used : Filtration(io.druid.sql.calcite.filtration.Filtration)

Example 3 with Filtration

use of io.druid.sql.calcite.filtration.Filtration in project druid by druid-io.

the class DruidQueryBuilder method toTimeseriesQuery.

/**
   * Return this query as a Timeseries query, or null if this query is not compatible with Timeseries.
   *
   * @param dataSource         data source to query
   * @param sourceRowSignature row signature of the dataSource
   * @param context            query context
   *
   * @return query or null
   */
public TimeseriesQuery toTimeseriesQuery(final DataSource dataSource, final RowSignature sourceRowSignature, final Map<String, Object> context) {
    if (grouping == null || having != null) {
        return null;
    }
    final Granularity queryGranularity;
    final List<DimensionSpec> dimensions = grouping.getDimensions();
    if (dimensions.isEmpty()) {
        queryGranularity = Granularities.ALL;
    } else if (dimensions.size() == 1) {
        final DimensionSpec dimensionSpec = Iterables.getOnlyElement(dimensions);
        final Granularity gran = ExtractionFns.toQueryGranularity(dimensionSpec.getExtractionFn());
        if (gran == null || !dimensionSpec.getDimension().equals(Column.TIME_COLUMN_NAME)) {
            // Timeseries only applies if the single dimension is granular __time.
            return null;
        }
        // Timeseries only applies if sort is null, or if the first sort field is the time dimension.
        final boolean sortingOnTime = limitSpec == null || limitSpec.getColumns().isEmpty() || (limitSpec.getLimit() == Integer.MAX_VALUE && limitSpec.getColumns().get(0).getDimension().equals(dimensionSpec.getOutputName()));
        if (sortingOnTime) {
            queryGranularity = gran;
        } else {
            return null;
        }
    } else {
        return null;
    }
    final Filtration filtration = Filtration.create(filter).optimize(sourceRowSignature);
    final boolean descending;
    if (limitSpec != null && !limitSpec.getColumns().isEmpty()) {
        descending = limitSpec.getColumns().get(0).getDirection() == OrderByColumnSpec.Direction.DESCENDING;
    } else {
        descending = false;
    }
    final Map<String, Object> theContext = Maps.newHashMap();
    theContext.put("skipEmptyBuckets", true);
    theContext.putAll(context);
    return new TimeseriesQuery(dataSource, filtration.getQuerySegmentSpec(), descending, VirtualColumns.EMPTY, filtration.getDimFilter(), queryGranularity, grouping.getAggregatorFactories(), grouping.getPostAggregators(), theContext);
}
Also used : DimensionSpec(io.druid.query.dimension.DimensionSpec) Filtration(io.druid.sql.calcite.filtration.Filtration) TimeseriesQuery(io.druid.query.timeseries.TimeseriesQuery) Granularity(io.druid.java.util.common.granularity.Granularity)

Example 4 with Filtration

use of io.druid.sql.calcite.filtration.Filtration in project druid by druid-io.

the class DruidQueryBuilder method toSelectQuery.

/**
   * Return this query as a Select query, or null if this query is not compatible with Select.
   *
   * @param dataSource         data source to query
   * @param sourceRowSignature row signature of the dataSource
   * @param context            query context
   *
   * @return query or null
   */
public SelectQuery toSelectQuery(final DataSource dataSource, final RowSignature sourceRowSignature, final Map<String, Object> context) {
    if (grouping != null) {
        return null;
    }
    final Filtration filtration = Filtration.create(filter).optimize(sourceRowSignature);
    final boolean descending;
    if (limitSpec != null) {
        // Safe to assume limitSpec has zero or one entry; DruidSelectSortRule wouldn't push in anything else.
        if (limitSpec.getColumns().size() > 0) {
            final OrderByColumnSpec orderBy = Iterables.getOnlyElement(limitSpec.getColumns());
            if (!orderBy.getDimension().equals(Column.TIME_COLUMN_NAME)) {
                throw new ISE("WTF?! Got select with non-time orderBy[%s]", orderBy);
            }
            descending = orderBy.getDirection() == OrderByColumnSpec.Direction.DESCENDING;
        } else {
            descending = false;
        }
    } else {
        descending = false;
    }
    return new SelectQuery(dataSource, filtration.getQuerySegmentSpec(), descending, filtration.getDimFilter(), Granularities.ALL, selectProjection != null ? selectProjection.getDimensions() : ImmutableList.<DimensionSpec>of(), selectProjection != null ? selectProjection.getMetrics() : ImmutableList.<String>of(), null, new PagingSpec(null, 0), /* dummy -- will be replaced */
    context);
}
Also used : OrderByColumnSpec(io.druid.query.groupby.orderby.OrderByColumnSpec) SelectQuery(io.druid.query.select.SelectQuery) DimensionSpec(io.druid.query.dimension.DimensionSpec) Filtration(io.druid.sql.calcite.filtration.Filtration) PagingSpec(io.druid.query.select.PagingSpec) ISE(io.druid.java.util.common.ISE)

Aggregations

Filtration (io.druid.sql.calcite.filtration.Filtration)4 DimensionSpec (io.druid.query.dimension.DimensionSpec)3 OrderByColumnSpec (io.druid.query.groupby.orderby.OrderByColumnSpec)2 ISE (io.druid.java.util.common.ISE)1 Granularity (io.druid.java.util.common.granularity.Granularity)1 PagingSpec (io.druid.query.select.PagingSpec)1 SelectQuery (io.druid.query.select.SelectQuery)1 TimeseriesQuery (io.druid.query.timeseries.TimeseriesQuery)1 DimensionTopNMetricSpec (io.druid.query.topn.DimensionTopNMetricSpec)1 InvertedTopNMetricSpec (io.druid.query.topn.InvertedTopNMetricSpec)1 NumericTopNMetricSpec (io.druid.query.topn.NumericTopNMetricSpec)1 TopNMetricSpec (io.druid.query.topn.TopNMetricSpec)1 TopNQuery (io.druid.query.topn.TopNQuery)1