Search in sources :

Example 21 with RegexDimFilter

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

the class DummyStringVirtualColumnTest method testGroupByWithRegexFilter.

private void testGroupByWithRegexFilter(List<Segment> segments, boolean enableRowBasedMethods, boolean enableColumnBasedMethods, boolean enableBitmaps, boolean disableValueMatchers) {
    GroupByQuery query = new GroupByQuery.Builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setGranularity(Granularities.ALL).setVirtualColumns(new DummyStringVirtualColumn(QueryRunnerTestHelper.MARKET_DIMENSION, VSTRING_DIM, enableRowBasedMethods, enableColumnBasedMethods, enableBitmaps, disableValueMatchers)).addDimension(VSTRING_DIM).setAggregatorSpecs(new CountAggregatorFactory(COUNT)).setInterval("2000/2030").addOrderByColumn(VSTRING_DIM).setDimFilter(new RegexDimFilter(VSTRING_DIM, "(spot)|(upfront)", null)).build();
    List<ResultRow> rows = groupByTestHelper.runQueryOnSegmentsObjs(segments, query).toList();
    List<ResultRow> expectedRows = Arrays.asList(GroupByQueryRunnerTestHelper.createExpectedRow(query, "2000", COUNT, 1674L, VSTRING_DIM, "spot"), GroupByQueryRunnerTestHelper.createExpectedRow(query, "2000", COUNT, 372L, VSTRING_DIM, "upfront"));
    TestHelper.assertExpectedObjects(expectedRows, rows, "failed");
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory)

Example 22 with RegexDimFilter

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

the class FilteredAggregatorTest method testAggregateWithExtractionFns.

@Test
public void testAggregateWithExtractionFns() {
    final float[] values = { 0.15f, 0.27f };
    TestFloatColumnSelector selector;
    FilteredAggregatorFactory factory;
    String extractionJsFn = "function(str) { return str + 'AARDVARK'; }";
    ExtractionFn extractionFn = new JavaScriptExtractionFn(extractionJsFn, false, JavaScriptConfig.getEnabledInstance());
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new SelectorDimFilter("dim", "aAARDVARK", extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new InDimFilter("dim", Arrays.asList("NOT-aAARDVARK", "FOOBAR", "aAARDVARK"), extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new BoundDimFilter("dim", "aAARDVARK", "aAARDVARK", false, false, true, extractionFn, StringComparators.ALPHANUMERIC));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new RegexDimFilter("dim", "aAARDVARK", extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new SearchQueryDimFilter("dim", new ContainsSearchQuerySpec("aAARDVARK", true), extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    String jsFn = "function(x) { return(x === 'aAARDVARK') }";
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new JavaScriptDimFilter("dim", jsFn, extractionFn, JavaScriptConfig.getEnabledInstance()));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
}
Also used : RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) ContainsSearchQuerySpec(org.apache.druid.query.search.ContainsSearchQuerySpec) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) InDimFilter(org.apache.druid.query.filter.InDimFilter) SearchQueryDimFilter(org.apache.druid.query.filter.SearchQueryDimFilter) JavaScriptDimFilter(org.apache.druid.query.filter.JavaScriptDimFilter) Test(org.junit.Test)

Example 23 with RegexDimFilter

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

the class ExpressionsTest method testRegexpLikeAsFilter.

@Test
public void testRegexpLikeAsFilter() {
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeInputRef("s"), testHelper.makeLiteral("f.")), Collections.emptyList(), new RegexDimFilter("s", "f.", null), true);
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeInputRef("s"), testHelper.makeLiteral("o")), Collections.emptyList(), // Column "s" contains an 'o', but not at the beginning, so we don't match
    new RegexDimFilter("s", "o", null), true);
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeInputRef("s"), testHelper.makeLiteral("x.")), Collections.emptyList(), new RegexDimFilter("s", "x.", null), false);
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeInputRef("s"), testHelper.makeLiteral("")), Collections.emptyList(), new RegexDimFilter("s", "", null), true);
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeInputRef("newliney"), testHelper.makeLiteral("^beep$")), Collections.emptyList(), new RegexDimFilter("newliney", "^beep$", null), false);
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeInputRef("newliney"), testHelper.makeLiteral("^beep\\nboop$")), Collections.emptyList(), new RegexDimFilter("newliney", "^beep\\nboop$", null), true);
    testHelper.testFilter(new RegexpLikeOperatorConversion().calciteOperator(), ImmutableList.of(testHelper.makeCall(SqlStdOperatorTable.CONCAT, testHelper.makeLiteral("Z"), testHelper.makeInputRef("s")), testHelper.makeLiteral("x(.)")), ImmutableList.of(new ExpressionVirtualColumn("v0", "concat('Z',\"s\")", ColumnType.STRING, TestExprMacroTable.INSTANCE)), new RegexDimFilter("v0", "x(.)", null), false);
}
Also used : RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) RegexpLikeOperatorConversion(org.apache.druid.sql.calcite.expression.builtin.RegexpLikeOperatorConversion) Test(org.junit.Test)

Example 24 with RegexDimFilter

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

the class RegexpLikeOperatorConversion method toDruidFilter.

@Nullable
@Override
public DimFilter toDruidFilter(final PlannerContext plannerContext, final RowSignature rowSignature, @Nullable final VirtualColumnRegistry virtualColumnRegistry, final RexNode rexNode) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final DruidExpression druidExpression = Expressions.toDruidExpression(plannerContext, rowSignature, operands.get(0));
    if (druidExpression == null) {
        return null;
    }
    final String pattern = RexLiteral.stringValue(operands.get(1));
    if (druidExpression.isSimpleExtraction()) {
        return new RegexDimFilter(druidExpression.getSimpleExtraction().getColumn(), pattern, druidExpression.getSimpleExtraction().getExtractionFn(), null);
    } else if (virtualColumnRegistry != null) {
        String v = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(druidExpression, operands.get(0).getType());
        return new RegexDimFilter(v, pattern, null, null);
    } else {
        return null;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Aggregations

RegexDimFilter (org.apache.druid.query.filter.RegexDimFilter)24 Test (org.junit.Test)18 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)12 SearchQueryDimFilter (org.apache.druid.query.filter.SearchQueryDimFilter)12 ContainsSearchQuerySpec (org.apache.druid.query.search.ContainsSearchQuerySpec)12 InDimFilter (org.apache.druid.query.filter.InDimFilter)11 JavaScriptDimFilter (org.apache.druid.query.filter.JavaScriptDimFilter)11 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)9 ArrayList (java.util.ArrayList)6 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)5 HashMap (java.util.HashMap)4 MapLookupExtractor (org.apache.druid.query.extraction.MapLookupExtractor)4 OrDimFilter (org.apache.druid.query.filter.OrDimFilter)4 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)4 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)3 ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)3 JavaScriptExtractionFn (org.apache.druid.query.extraction.JavaScriptExtractionFn)3 DimFilter (org.apache.druid.query.filter.DimFilter)3 LookupExtractor (org.apache.druid.query.lookup.LookupExtractor)3 PeriodGranularity (org.apache.druid.java.util.common.granularity.PeriodGranularity)2