Search in sources :

Example 21 with InDimFilter

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

the class MultiValuedDimensionTest method testGroupByWithDimFilterNullishResults.

@Test
public void testGroupByWithDimFilterNullishResults() {
    GroupByQuery query = GroupByQuery.builder().setDataSource("xx").setQuerySegmentSpec(new LegacySegmentSpec("1970/3000")).setGranularity(Granularities.ALL).setDimensions(new DefaultDimensionSpec("tags", "tags")).setAggregatorSpecs(new CountAggregatorFactory("count")).setDimFilter(new InDimFilter("product", ImmutableList.of("product_5", "product_6", "product_8"), null)).setContext(context).build();
    Sequence<ResultRow> result = helper.runQueryOnSegmentsObjs(ImmutableList.of(new QueryableIndexSegment(queryableIndexNullSampler, SegmentId.dummy("sid1")), new IncrementalIndexSegment(incrementalIndexNullSampler, SegmentId.dummy("sid2"))), query);
    List<ResultRow> expectedResults;
    // grouping input is filtered to [], null, [""]
    if (NullHandling.replaceWithDefault()) {
        // when sql compatible null handling is disabled, the inputs are effectively [], null, [null] and
        // are all grouped as null
        expectedResults = Collections.singletonList(GroupByQueryRunnerTestHelper.createExpectedRow(query, "1970-01-01T00:00:00.000Z", "tags", null, "count", 6L));
    } else {
        // with sql compatible null handling, null and [] = null, but [""] = ""
        expectedResults = ImmutableList.of(GroupByQueryRunnerTestHelper.createExpectedRow(query, "1970-01-01T00:00:00.000Z", "tags", null, "count", 4L), GroupByQueryRunnerTestHelper.createExpectedRow(query, "1970-01-01T00:00:00.000Z", "tags", "", "count", 2L));
    }
    TestHelper.assertExpectedObjects(expectedResults, result.toList(), "filter-nullish");
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) QueryableIndexSegment(org.apache.druid.segment.QueryableIndexSegment) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) IncrementalIndexSegment(org.apache.druid.segment.IncrementalIndexSegment) InDimFilter(org.apache.druid.query.filter.InDimFilter) LegacySegmentSpec(org.apache.druid.query.spec.LegacySegmentSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) GroupByQueryRunnerTest(org.apache.druid.query.groupby.GroupByQueryRunnerTest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 22 with InDimFilter

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

the class ArrayOverlapOperatorConversion method toDruidFilter.

@Nullable
@Override
public DimFilter toDruidFilter(final PlannerContext plannerContext, RowSignature rowSignature, @Nullable VirtualColumnRegistry virtualColumnRegistry, final RexNode rexNode) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final List<DruidExpression> druidExpressions = Expressions.toDruidExpressions(plannerContext, rowSignature, operands);
    if (druidExpressions == null) {
        return null;
    }
    // Converts array_overlaps() function into an OR of Selector filters if possible.
    final boolean leftSimpleExtractionExpr = druidExpressions.get(0).isSimpleExtraction();
    final boolean rightSimpleExtractionExpr = druidExpressions.get(1).isSimpleExtraction();
    final DruidExpression simpleExtractionExpr;
    final DruidExpression complexExpr;
    if (leftSimpleExtractionExpr ^ rightSimpleExtractionExpr) {
        if (leftSimpleExtractionExpr) {
            simpleExtractionExpr = druidExpressions.get(0);
            complexExpr = druidExpressions.get(1);
        } else {
            simpleExtractionExpr = druidExpressions.get(1);
            complexExpr = druidExpressions.get(0);
        }
    } else {
        return toExpressionFilter(plannerContext, getDruidFunctionName(), druidExpressions);
    }
    Expr expr = Parser.parse(complexExpr.getExpression(), plannerContext.getExprMacroTable());
    if (expr.isLiteral()) {
        // Evaluate the expression to take out the array elements.
        // We can safely pass null if the expression is literal.
        ExprEval<?> exprEval = expr.eval(InputBindings.nilBindings());
        String[] arrayElements = exprEval.asStringArray();
        if (arrayElements == null || arrayElements.length == 0) {
            // to create an empty array with no argument, we just return null.
            return null;
        } else if (arrayElements.length == 1) {
            return newSelectorDimFilter(simpleExtractionExpr.getSimpleExtraction(), arrayElements[0]);
        } else {
            return new InDimFilter(simpleExtractionExpr.getSimpleExtraction().getColumn(), Sets.newHashSet(arrayElements), simpleExtractionExpr.getSimpleExtraction().getExtractionFn(), null);
        }
    }
    return toExpressionFilter(plannerContext, getDruidFunctionName(), druidExpressions);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) Expr(org.apache.druid.math.expr.Expr) InDimFilter(org.apache.druid.query.filter.InDimFilter) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 23 with InDimFilter

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

the class ScanBenchmark method basicB.

private static Druids.ScanQueryBuilder basicB(final GeneratorSchemaInfo basicSchema) {
    final QuerySegmentSpec intervalSpec = new MultipleIntervalSegmentSpec(Collections.singletonList(basicSchema.getDataInterval()));
    List<String> dimHyperUniqueFilterVals = new ArrayList<>();
    int numResults = (int) (100000 * 0.1);
    int step = 100000 / numResults;
    for (int i = 0; i < 100001 && dimHyperUniqueFilterVals.size() < numResults; i += step) {
        dimHyperUniqueFilterVals.add(String.valueOf(i));
    }
    DimFilter filter = new InDimFilter("dimHyperUnique", dimHyperUniqueFilterVals, null);
    return Druids.newScanQueryBuilder().filters(filter).dataSource("blah").intervals(intervalSpec).order(ordering);
}
Also used : ArrayList(java.util.ArrayList) InDimFilter(org.apache.druid.query.filter.InDimFilter) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) InDimFilter(org.apache.druid.query.filter.InDimFilter)

Example 24 with InDimFilter

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

the class SearchBenchmark method basicD.

private static SearchQueryBuilder basicD(final GeneratorSchemaInfo basicSchema) {
    final QuerySegmentSpec intervalSpec = new MultipleIntervalSegmentSpec(Collections.singletonList(basicSchema.getDataInterval()));
    final List<String> dimUniformFilterVals = new ArrayList<>();
    final int resultNum = (int) (100000 * 0.1);
    final int step = 100000 / resultNum;
    for (int i = 1; i < 100001 && dimUniformFilterVals.size() < resultNum; i += step) {
        dimUniformFilterVals.add(String.valueOf(i));
    }
    final String dimName = "dimUniform";
    final List<DimFilter> dimFilters = new ArrayList<>();
    dimFilters.add(new InDimFilter(dimName, dimUniformFilterVals, null));
    dimFilters.add(new SelectorDimFilter(dimName, "3", null));
    dimFilters.add(new BoundDimFilter(dimName, "100", "10000", true, true, true, null, null));
    return Druids.newSearchQueryBuilder().dataSource("blah").granularity(Granularities.ALL).intervals(intervalSpec).query("").dimensions(Collections.singletonList("dimUniform")).filters(new AndDimFilter(dimFilters));
}
Also used : BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) ArrayList(java.util.ArrayList) InDimFilter(org.apache.druid.query.filter.InDimFilter) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) InDimFilter(org.apache.druid.query.filter.InDimFilter)

Example 25 with InDimFilter

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

the class SearchBenchmark method basicB.

private static SearchQueryBuilder basicB(final GeneratorSchemaInfo basicSchema) {
    final QuerySegmentSpec intervalSpec = new MultipleIntervalSegmentSpec(Collections.singletonList(basicSchema.getDataInterval()));
    final List<String> dimUniformFilterVals = new ArrayList<>();
    int resultNum = (int) (100000 * 0.1);
    int step = 100000 / resultNum;
    for (int i = 1; i < 100001 && dimUniformFilterVals.size() < resultNum; i += step) {
        dimUniformFilterVals.add(String.valueOf(i));
    }
    List<String> dimHyperUniqueFilterVals = new ArrayList<>();
    resultNum = (int) (100000 * 0.1);
    step = 100000 / resultNum;
    for (int i = 0; i < 100001 && dimHyperUniqueFilterVals.size() < resultNum; i += step) {
        dimHyperUniqueFilterVals.add(String.valueOf(i));
    }
    final List<DimFilter> dimFilters = new ArrayList<>();
    dimFilters.add(new InDimFilter("dimUniform", dimUniformFilterVals, null));
    dimFilters.add(new InDimFilter("dimHyperUnique", dimHyperUniqueFilterVals, null));
    return Druids.newSearchQueryBuilder().dataSource("blah").granularity(Granularities.ALL).intervals(intervalSpec).query("").dimensions(Lists.newArrayList("dimUniform", "dimHyperUnique")).filters(new AndDimFilter(dimFilters));
}
Also used : AndDimFilter(org.apache.druid.query.filter.AndDimFilter) ArrayList(java.util.ArrayList) InDimFilter(org.apache.druid.query.filter.InDimFilter) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) InDimFilter(org.apache.druid.query.filter.InDimFilter)

Aggregations

InDimFilter (org.apache.druid.query.filter.InDimFilter)58 Test (org.junit.Test)43 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)39 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)24 Filter (org.apache.druid.query.filter.Filter)18 FalseFilter (org.apache.druid.segment.filter.FalseFilter)17 OrFilter (org.apache.druid.segment.filter.OrFilter)17 SelectorFilter (org.apache.druid.segment.filter.SelectorFilter)17 ArrayList (java.util.ArrayList)16 ExpressionDimFilter (org.apache.druid.query.filter.ExpressionDimFilter)16 AndFilter (org.apache.druid.segment.filter.AndFilter)16 BoundFilter (org.apache.druid.segment.filter.BoundFilter)16 JoinFilterPreAnalysis (org.apache.druid.segment.join.filter.JoinFilterPreAnalysis)16 JoinFilterSplit (org.apache.druid.segment.join.filter.JoinFilterSplit)16 DimFilter (org.apache.druid.query.filter.DimFilter)14 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)13 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)12 RegexDimFilter (org.apache.druid.query.filter.RegexDimFilter)12 SearchQueryDimFilter (org.apache.druid.query.filter.SearchQueryDimFilter)12 ContainsSearchQuerySpec (org.apache.druid.query.search.ContainsSearchQuerySpec)12