Search in sources :

Example 31 with ExtractionFn

use of org.apache.druid.query.extraction.ExtractionFn in project druid by druid-io.

the class TopNQueryRunnerTest method testFullOnTopNLongTimeColumnWithExFn.

@Test
public void testFullOnTopNLongTimeColumnWithExFn() {
    String jsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension(new ExtractionDimensionSpec(ColumnHolder.TIME_COLUMN_NAME, "time_alias", jsExtractionFn)).metric("maxIndex").threshold(4).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).aggregators(Lists.newArrayList(Iterables.concat(commonAggregators, Lists.newArrayList(new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index"))))).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).build();
    List<Result<TopNResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(Arrays.<Map<String, Object>>asList(ImmutableMap.<String, Object>builder().put("time_alias", "super-1296345600000").put(QueryRunnerTestHelper.INDEX_METRIC, 5497.331253051758D).put("rows", 13L).put("addRowsIndexConstant", 5511.331253051758D).put("uniques", QueryRunnerTestHelper.UNIQUES_9).put("maxIndex", 1870.061029D).put("minIndex", 97.02391052246094D).build(), ImmutableMap.<String, Object>builder().put("time_alias", "super-1298678400000").put(QueryRunnerTestHelper.INDEX_METRIC, 6541.463027954102D).put("rows", 13L).put("addRowsIndexConstant", 6555.463027954102D).put("uniques", QueryRunnerTestHelper.UNIQUES_9).put("maxIndex", 1862.737933D).put("minIndex", 83.099365234375D).build(), ImmutableMap.<String, Object>builder().put("time_alias", "super-1301529600000").put(QueryRunnerTestHelper.INDEX_METRIC, 6814.467971801758D).put("rows", 13L).put("addRowsIndexConstant", 6828.467971801758D).put("uniques", QueryRunnerTestHelper.UNIQUES_9).put("maxIndex", 1734.27490234375D).put("minIndex", 93.39083862304688D).build(), ImmutableMap.<String, Object>builder().put("time_alias", "super-1294876800000").put(QueryRunnerTestHelper.INDEX_METRIC, 6077.949111938477D).put("rows", 13L).put("addRowsIndexConstant", 6091.949111938477D).put("uniques", QueryRunnerTestHelper.UNIQUES_9).put("maxIndex", 1689.0128173828125D).put("minIndex", 94.87471008300781D).build()))));
    assertExpectedResults(expectedResults, query);
}
Also used : DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) DoubleMinAggregatorFactory(org.apache.druid.query.aggregation.DoubleMinAggregatorFactory) Result(org.apache.druid.query.Result) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 32 with ExtractionFn

use of org.apache.druid.query.extraction.ExtractionFn in project druid by druid-io.

the class TopNQueryRunnerTest method testTopNWithEmptyStringProducingDimExtractionFn.

@Test
public /**
 * This test exists only to show what the current behavior is and not necessarily to define that this is
 * correct behavior.  In fact, the behavior when returning the empty string from a DimExtractionFn is, by
 * contract, undefined, so this can do anything.
 */
void testTopNWithEmptyStringProducingDimExtractionFn() {
    final ExtractionFn emptyStringDimExtraction = new DimExtractionFn() {

        @Override
        public byte[] getCacheKey() {
            return new byte[] { (byte) 0xFF };
        }

        @Override
        public String apply(String dimValue) {
            return "total_market".equals(dimValue) ? "" : dimValue;
        }

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

        @Override
        public ExtractionType getExtractionType() {
            return ExtractionType.MANY_TO_ONE;
        }
    };
    final TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).metric("rows").threshold(4).intervals(QueryRunnerTestHelper.FIRST_TO_THIRD).aggregators(commonAggregators).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).dimension(new ExtractionDimensionSpec(QueryRunnerTestHelper.MARKET_DIMENSION, QueryRunnerTestHelper.MARKET_DIMENSION, emptyStringDimExtraction)).build();
    List<Result<TopNResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-04-01T00:00:00.000Z"), new TopNResultValue(Arrays.asList(ImmutableMap.of(QueryRunnerTestHelper.MARKET_DIMENSION, "spot", "rows", 18L, "index", 2231.876812D, "addRowsIndexConstant", 2250.876812D, "uniques", QueryRunnerTestHelper.UNIQUES_9), QueryRunnerTestHelper.orderedMap(QueryRunnerTestHelper.MARKET_DIMENSION, "", "rows", 4L, "index", 5351.814783D, "addRowsIndexConstant", 5356.814783D, "uniques", QueryRunnerTestHelper.UNIQUES_2), ImmutableMap.of(QueryRunnerTestHelper.MARKET_DIMENSION, "upfront", "rows", 4L, "index", 4875.669692D, "addRowsIndexConstant", 4880.669692D, "uniques", QueryRunnerTestHelper.UNIQUES_2)))));
    assertExpectedResults(expectedResults, query);
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Result(org.apache.druid.query.Result) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 33 with ExtractionFn

use of org.apache.druid.query.extraction.ExtractionFn in project druid by druid-io.

the class SearchQueryRunnerTest method testSearchOnLongColumnWithExFn.

@Test
public void testSearchOnLongColumnWithExFn() {
    String jsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    SearchQuery searchQuery = Druids.newSearchQueryBuilder().dimensions(new ExtractionDimensionSpec(ColumnHolder.TIME_COLUMN_NAME, ColumnHolder.TIME_COLUMN_NAME, jsExtractionFn)).dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).query("1297123200000").build();
    List<SearchHit> expectedHits = new ArrayList<>();
    expectedHits.add(new SearchHit(ColumnHolder.TIME_COLUMN_NAME, "super-1297123200000", 13));
    checkSearchQuery(searchQuery, expectedHits);
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) ArrayList(java.util.ArrayList) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 34 with ExtractionFn

use of org.apache.druid.query.extraction.ExtractionFn in project druid by druid-io.

the class TimeFilteringTest method testIntervalFilter.

@Test
public void testIntervalFilter() {
    assertFilterMatches(new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(Intervals.of("1970-01-01T00:00:00.001Z/1970-01-01T00:00:00.005Z")), null), ImmutableList.of("1", "2", "3", "4"));
    assertFilterMatches(new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Arrays.asList(Intervals.of("1970-01-01T00:00:00.000Z/1970-01-01T00:00:00.003Z"), Intervals.of("1970-01-01T00:00:00.004Z/1970-01-01T00:00:00.006Z")), null), ImmutableList.of("0", "1", "2", "4", "5"));
    assertFilterMatches(new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Arrays.asList(Intervals.of("1970-01-01T00:00:00.000Z/1970-01-01T00:00:00.001Z"), Intervals.of("1970-01-01T00:00:00.003Z/1970-01-01T00:00:00.006Z"), Intervals.of("1970-01-01T00:00:00.002Z/1970-01-01T00:00:00.005Z")), null), ImmutableList.of("0", "2", "3", "4", "5"));
    // increment timestamp by 2 hours
    String timeBoosterJsFn = "function(x) { return(x + 7200000) }";
    ExtractionFn exFn = new JavaScriptExtractionFn(timeBoosterJsFn, true, JavaScriptConfig.getEnabledInstance());
    assertFilterMatches(new IntervalDimFilter(ColumnHolder.TIME_COLUMN_NAME, Collections.singletonList(Intervals.of("1970-01-01T02:00:00.001Z/1970-01-01T02:00:00.005Z")), exFn), ImmutableList.of("1", "2", "3", "4"));
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) IntervalDimFilter(org.apache.druid.query.filter.IntervalDimFilter) Test(org.junit.Test)

Example 35 with ExtractionFn

use of org.apache.druid.query.extraction.ExtractionFn in project druid by druid-io.

the class BoundFilterTest method testMatchWithExtractionFn.

@Test
public void testMatchWithExtractionFn() {
    String extractionJsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn superFn = new JavaScriptExtractionFn(extractionJsFn, false, JavaScriptConfig.getEnabledInstance());
    String nullJsFn = "function(str) { return null; }";
    ExtractionFn makeNullFn = new JavaScriptExtractionFn(nullJsFn, false, JavaScriptConfig.getEnabledInstance());
    if (NullHandling.replaceWithDefault()) {
        assertFilterMatches(new BoundDimFilter("dim0", "", "", false, false, false, makeNullFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
    } else {
        assertFilterMatches(new BoundDimFilter("dim0", "", "", false, false, false, makeNullFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of());
    }
    assertFilterMatches(new BoundDimFilter("dim1", "super-ab", "super-abd", true, true, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("5"));
    assertFilterMatches(new BoundDimFilter("dim1", "super-0", "super-10", false, false, true, superFn, StringComparators.ALPHANUMERIC), ImmutableList.of("1", "2", "3"));
    assertFilterMatches(new BoundDimFilter("dim2", "super-", "super-zzzzzz", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
    if (NullHandling.replaceWithDefault()) {
        assertFilterMatches(new BoundDimFilter("dim2", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("1", "2", "5"));
        assertFilterMatches(new BoundDimFilter("dim2", "super-null", "super-null", false, false, false, superFn, StringComparators.NUMERIC), ImmutableList.of("1", "2", "5"));
    } else {
        assertFilterMatches(new BoundDimFilter("dim2", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("1", "5"));
        assertFilterMatches(new BoundDimFilter("dim2", "super-", "super-", false, false, false, superFn, StringComparators.NUMERIC), ImmutableList.of("2"));
        assertFilterMatches(new BoundDimFilter("dim2", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("1", "5"));
        assertFilterMatches(new BoundDimFilter("dim2", "super-", "super-", false, false, false, superFn, StringComparators.NUMERIC), ImmutableList.of("2"));
    }
    assertFilterMatches(new BoundDimFilter("dim3", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
    assertFilterMatches(new BoundDimFilter("dim4", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
    assertFilterMatches(new BoundDimFilter("dim4", "super-null", "super-null", false, false, false, superFn, StringComparators.NUMERIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) Test(org.junit.Test)

Aggregations

ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)40 Test (org.junit.Test)33 JavaScriptExtractionFn (org.apache.druid.query.extraction.JavaScriptExtractionFn)30 TimeFormatExtractionFn (org.apache.druid.query.extraction.TimeFormatExtractionFn)26 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)26 RegexDimExtractionFn (org.apache.druid.query.extraction.RegexDimExtractionFn)23 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)22 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)20 DimExtractionFn (org.apache.druid.query.extraction.DimExtractionFn)20 StringFormatExtractionFn (org.apache.druid.query.extraction.StringFormatExtractionFn)20 StrlenExtractionFn (org.apache.druid.query.extraction.StrlenExtractionFn)20 CascadeExtractionFn (org.apache.druid.query.extraction.CascadeExtractionFn)12 SearchQuerySpecDimExtractionFn (org.apache.druid.query.extraction.SearchQuerySpecDimExtractionFn)12 SubstringDimExtractionFn (org.apache.druid.query.extraction.SubstringDimExtractionFn)12 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)11 Result (org.apache.druid.query.Result)9 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)9 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)8 ArrayList (java.util.ArrayList)6 DoubleMaxAggregatorFactory (org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory)5