Search in sources :

Example 16 with DimFilter

use of org.apache.druid.query.filter.DimFilter in project druid by druid-io.

the class DruidQuery method computeGrouping.

@Nonnull
private static Grouping computeGrouping(final PartialDruidQuery partialQuery, final PlannerContext plannerContext, final RowSignature rowSignature, final VirtualColumnRegistry virtualColumnRegistry, final RexBuilder rexBuilder, final boolean finalizeAggregations) {
    final Aggregate aggregate = Preconditions.checkNotNull(partialQuery.getAggregate(), "aggregate");
    final Project aggregateProject = partialQuery.getAggregateProject();
    final List<DimensionExpression> dimensions = computeDimensions(partialQuery, plannerContext, rowSignature, virtualColumnRegistry);
    final Subtotals subtotals = computeSubtotals(partialQuery, rowSignature);
    final List<Aggregation> aggregations = computeAggregations(partialQuery, plannerContext, rowSignature, virtualColumnRegistry, rexBuilder, finalizeAggregations);
    final RowSignature aggregateRowSignature = RowSignatures.fromRelDataType(ImmutableList.copyOf(Iterators.concat(dimensions.stream().map(DimensionExpression::getOutputName).iterator(), aggregations.stream().map(Aggregation::getOutputName).iterator())), aggregate.getRowType());
    final DimFilter havingFilter = computeHavingFilter(partialQuery, plannerContext, aggregateRowSignature);
    final Grouping grouping = Grouping.create(dimensions, subtotals, aggregations, havingFilter, aggregateRowSignature);
    if (aggregateProject == null) {
        return grouping;
    } else {
        return grouping.applyProject(plannerContext, aggregateProject);
    }
}
Also used : Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) Project(org.apache.calcite.rel.core.Project) DimensionExpression(org.apache.druid.sql.calcite.aggregation.DimensionExpression) Aggregate(org.apache.calcite.rel.core.Aggregate) RowSignature(org.apache.druid.segment.column.RowSignature) DimFilter(org.apache.druid.query.filter.DimFilter) Nonnull(javax.annotation.Nonnull)

Example 17 with DimFilter

use of org.apache.druid.query.filter.DimFilter in project druid by druid-io.

the class DruidQuery method fromPartialQuery.

public static DruidQuery fromPartialQuery(final PartialDruidQuery partialQuery, final DataSource dataSource, final RowSignature sourceRowSignature, final PlannerContext plannerContext, final RexBuilder rexBuilder, final boolean finalizeAggregations, @Nullable VirtualColumnRegistry virtualColumnRegistry) {
    final RelDataType outputRowType = partialQuery.leafRel().getRowType();
    if (virtualColumnRegistry == null) {
        virtualColumnRegistry = VirtualColumnRegistry.create(sourceRowSignature, plannerContext.getExprMacroTable());
    }
    // Now the fun begins.
    final DimFilter filter;
    final Projection selectProjection;
    final Grouping grouping;
    final Sorting sorting;
    if (partialQuery.getWhereFilter() != null) {
        filter = Preconditions.checkNotNull(computeWhereFilter(partialQuery, plannerContext, sourceRowSignature, virtualColumnRegistry));
    } else {
        filter = null;
    }
    // reflect select-project from partialQuery on its own.)
    if (partialQuery.getSelectProject() != null && partialQuery.getAggregate() == null) {
        selectProjection = Preconditions.checkNotNull(computeSelectProjection(partialQuery, plannerContext, computeOutputRowSignature(sourceRowSignature, null, null, null), virtualColumnRegistry));
    } else {
        selectProjection = null;
    }
    if (partialQuery.getAggregate() != null) {
        grouping = Preconditions.checkNotNull(computeGrouping(partialQuery, plannerContext, computeOutputRowSignature(sourceRowSignature, null, null, null), virtualColumnRegistry, rexBuilder, finalizeAggregations));
    } else {
        grouping = null;
    }
    if (partialQuery.getSort() != null) {
        sorting = Preconditions.checkNotNull(computeSorting(partialQuery, plannerContext, computeOutputRowSignature(sourceRowSignature, selectProjection, grouping, null), // When sorting follows grouping, virtual columns cannot be used
        partialQuery.getAggregate() != null ? null : virtualColumnRegistry));
    } else {
        sorting = null;
    }
    return new DruidQuery(dataSource, plannerContext, filter, selectProjection, grouping, sorting, sourceRowSignature, outputRowType, virtualColumnRegistry);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) DimFilter(org.apache.druid.query.filter.DimFilter)

Example 18 with DimFilter

use of org.apache.druid.query.filter.DimFilter in project druid by druid-io.

the class NativeQueryMaker method runQuery.

@Override
public Sequence<Object[]> runQuery(final DruidQuery druidQuery) {
    final Query<?> query = druidQuery.getQuery();
    if (plannerContext.getPlannerConfig().isRequireTimeCondition() && !(druidQuery.getDataSource() instanceof InlineDataSource)) {
        if (Intervals.ONLY_ETERNITY.equals(findBaseDataSourceIntervals(query))) {
            throw new CannotBuildQueryException("requireTimeCondition is enabled, all queries must include a filter condition on the __time column");
        }
    }
    int numFilters = plannerContext.getPlannerConfig().getMaxNumericInFilters();
    // Instead of IN(v1,v2,v3) user should specify IN('v1','v2','v3')
    if (numFilters != PlannerConfig.NUM_FILTER_NOT_USED) {
        if (query.getFilter() instanceof OrDimFilter) {
            OrDimFilter orDimFilter = (OrDimFilter) query.getFilter();
            int numBoundFilters = 0;
            for (DimFilter filter : orDimFilter.getFields()) {
                numBoundFilters += filter instanceof BoundDimFilter ? 1 : 0;
            }
            if (numBoundFilters > numFilters) {
                String dimension = ((BoundDimFilter) (orDimFilter.getFields().get(0))).getDimension();
                throw new UOE(StringUtils.format("The number of values in the IN clause for [%s] in query exceeds configured maxNumericFilter limit of [%s] for INs. Cast [%s] values of IN clause to String", dimension, numFilters, orDimFilter.getFields().size()));
            }
        }
    }
    final List<String> rowOrder;
    if (query instanceof TimeseriesQuery && !druidQuery.getGrouping().getDimensions().isEmpty()) {
        // Hack for timeseries queries: when generating them, DruidQuery.toTimeseriesQuery translates a dimension
        // based on a timestamp_floor expression into a 'granularity'. This is not reflected in the druidQuery's
        // output row signature, so we have to account for it here.
        // TODO: We can remove this once https://github.com/apache/druid/issues/9974 is done.
        final String timeDimension = Iterables.getOnlyElement(druidQuery.getGrouping().getDimensions()).getOutputName();
        rowOrder = druidQuery.getOutputRowSignature().getColumnNames().stream().map(f -> timeDimension.equals(f) ? ColumnHolder.TIME_COLUMN_NAME : f).collect(Collectors.toList());
    } else {
        rowOrder = druidQuery.getOutputRowSignature().getColumnNames();
    }
    final List<SqlTypeName> columnTypes = druidQuery.getOutputRowType().getFieldList().stream().map(f -> f.getType().getSqlTypeName()).collect(Collectors.toList());
    return execute(query, mapColumnList(rowOrder, fieldMapping), mapColumnList(columnTypes, fieldMapping));
}
Also used : Arrays(java.util.Arrays) DimensionHandlerUtils(org.apache.druid.segment.DimensionHandlerUtils) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) IAE(org.apache.druid.java.util.common.IAE) DateTimes(org.apache.druid.java.util.common.DateTimes) Sequence(org.apache.druid.java.util.common.guava.Sequence) Collection(java.util.Collection) StringUtils(org.apache.druid.java.util.common.StringUtils) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) ISE(org.apache.druid.java.util.common.ISE) UUID(java.util.UUID) PlannerConfig(org.apache.druid.sql.calcite.planner.PlannerConfig) Collectors(java.util.stream.Collectors) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) List(java.util.List) DimFilter(org.apache.druid.query.filter.DimFilter) Hook(org.apache.calcite.runtime.Hook) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) QueryLifecycleFactory(org.apache.druid.server.QueryLifecycleFactory) Iterables(com.google.common.collect.Iterables) ComparableList(org.apache.druid.segment.data.ComparableList) Intervals(org.apache.druid.java.util.common.Intervals) CannotBuildQueryException(org.apache.druid.sql.calcite.rel.CannotBuildQueryException) QueryLifecycle(org.apache.druid.server.QueryLifecycle) ArrayList(java.util.ArrayList) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult) Interval(org.joda.time.Interval) ColumnHolder(org.apache.druid.segment.column.ColumnHolder) Query(org.apache.druid.query.Query) Pair(org.apache.calcite.util.Pair) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) ComparableStringArray(org.apache.druid.segment.data.ComparableStringArray) UOE(org.apache.druid.java.util.common.UOE) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) Sequences(org.apache.druid.java.util.common.guava.Sequences) DruidQuery(org.apache.druid.sql.calcite.rel.DruidQuery) RelDataType(org.apache.calcite.rel.type.RelDataType) Access(org.apache.druid.server.security.Access) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) NlsString(org.apache.calcite.util.NlsString) InlineDataSource(org.apache.druid.query.InlineDataSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) QueryToolChest(org.apache.druid.query.QueryToolChest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Ints(com.google.common.primitives.Ints) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) NullHandling(org.apache.druid.common.config.NullHandling) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Evals(org.apache.druid.math.expr.Evals) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) UOE(org.apache.druid.java.util.common.UOE) NlsString(org.apache.calcite.util.NlsString) CannotBuildQueryException(org.apache.druid.sql.calcite.rel.CannotBuildQueryException) InlineDataSource(org.apache.druid.query.InlineDataSource) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter)

Example 19 with DimFilter

use of org.apache.druid.query.filter.DimFilter in project druid by druid-io.

the class DruidQueryTest method test_filtration_intervalsInBothFilters.

@Test
public void test_filtration_intervalsInBothFilters() {
    DataSource dataSource = join(JoinType.INNER, filterWithInterval);
    DataSource expectedDataSource = join(JoinType.INNER, selectorFilter);
    DimFilter queryFilter = new AndDimFilter(otherFilter, new BoundDimFilter("__time", "150", "250", false, true, null, null, StringComparators.NUMERIC));
    Pair<DataSource, Filtration> pair = DruidQuery.getFiltration(dataSource, queryFilter, VirtualColumnRegistry.create(RowSignature.empty(), TestExprMacroTable.INSTANCE));
    verify(pair, expectedDataSource, otherFilter, Intervals.utc(150, 200));
}
Also used : BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) Filtration(org.apache.druid.sql.calcite.filtration.Filtration) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) DataSource(org.apache.druid.query.DataSource) TableDataSource(org.apache.druid.query.TableDataSource) JoinDataSource(org.apache.druid.query.JoinDataSource) Test(org.junit.Test)

Example 20 with DimFilter

use of org.apache.druid.query.filter.DimFilter in project druid by druid-io.

the class ServerManagerTest method testGetQueryRunnerForSegmentsForUnknownQueryThrowingException.

@Test
public void testGetQueryRunnerForSegmentsForUnknownQueryThrowingException() {
    final Interval interval = Intervals.of("P1d/2011-04-01");
    final List<SegmentDescriptor> descriptors = Collections.singletonList(new SegmentDescriptor(interval, "1", 0));
    expectedException.expect(QueryUnsupportedException.class);
    expectedException.expectMessage("Unknown query type");
    serverManager.getQueryRunnerForSegments(new BaseQuery<Object>(new TableDataSource("test"), new MultipleSpecificSegmentSpec(descriptors), false, new HashMap<>()) {

        @Override
        public boolean hasFilters() {
            return false;
        }

        @Override
        public DimFilter getFilter() {
            return null;
        }

        @Override
        public String getType() {
            return null;
        }

        @Override
        public Query<Object> withOverriddenContext(Map<String, Object> contextOverride) {
            return null;
        }

        @Override
        public Query<Object> withQuerySegmentSpec(QuerySegmentSpec spec) {
            return null;
        }

        @Override
        public Query<Object> withDataSource(DataSource dataSource) {
            return null;
        }
    }, descriptors);
}
Also used : MultipleSpecificSegmentSpec(org.apache.druid.query.spec.MultipleSpecificSegmentSpec) BaseQuery(org.apache.druid.query.BaseQuery) Query(org.apache.druid.query.Query) SearchQuery(org.apache.druid.query.search.SearchQuery) HashMap(java.util.HashMap) DataSource(org.apache.druid.query.DataSource) TableDataSource(org.apache.druid.query.TableDataSource) TableDataSource(org.apache.druid.query.TableDataSource) SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) DimFilter(org.apache.druid.query.filter.DimFilter) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

DimFilter (org.apache.druid.query.filter.DimFilter)57 AndDimFilter (org.apache.druid.query.filter.AndDimFilter)31 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)30 ArrayList (java.util.ArrayList)29 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)29 OrDimFilter (org.apache.druid.query.filter.OrDimFilter)27 Test (org.junit.Test)21 InDimFilter (org.apache.druid.query.filter.InDimFilter)18 List (java.util.List)14 HashMap (java.util.HashMap)12 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)12 ExtractionDimFilter (org.apache.druid.query.filter.ExtractionDimFilter)11 NotDimFilter (org.apache.druid.query.filter.NotDimFilter)11 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)9 RegexDimFilter (org.apache.druid.query.filter.RegexDimFilter)9 ISE (org.apache.druid.java.util.common.ISE)7 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)7 JavaScriptDimFilter (org.apache.druid.query.filter.JavaScriptDimFilter)7 SearchQueryDimFilter (org.apache.druid.query.filter.SearchQueryDimFilter)7 Interval (org.joda.time.Interval)7