From 884017d981c190e301eb85a732c741df8bfb1700 Mon Sep 17 00:00:00 2001 From: Navis Ryu Date: Fri, 29 Jul 2016 10:16:15 +0900 Subject: [PATCH] "all" type search query spec (#3300) * "all" type search query spec * addressed comments * added unit test --- .../search/search/AllSearchQuerySpec.java | 57 +++++++++++++++++++ .../query/search/search/SearchQuery.java | 3 +- .../query/search/search/SearchQuerySpec.java | 3 +- .../query/search/SearchQueryRunnerTest.java | 29 ++++++++++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 processing/src/main/java/io/druid/query/search/search/AllSearchQuerySpec.java diff --git a/processing/src/main/java/io/druid/query/search/search/AllSearchQuerySpec.java b/processing/src/main/java/io/druid/query/search/search/AllSearchQuerySpec.java new file mode 100644 index 00000000000..14350542921 --- /dev/null +++ b/processing/src/main/java/io/druid/query/search/search/AllSearchQuerySpec.java @@ -0,0 +1,57 @@ +/* + * Licensed to Metamarkets Group Inc. (Metamarkets) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Metamarkets licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.druid.query.search.search; + +/** + */ +public class AllSearchQuerySpec implements SearchQuerySpec +{ + private static final byte CACHE_TYPE_ID = 0x7f; + + @Override + public boolean accept(String dimVal) + { + return true; + } + + @Override + public byte[] getCacheKey() + { + return new byte[]{CACHE_TYPE_ID}; + } + + @Override + public boolean equals(Object object) + { + return object instanceof AllSearchQuerySpec; + } + + @Override + public int hashCode() + { + return CACHE_TYPE_ID; + } + + @Override + public String toString() + { + return "AllSearchQuerySpec{}"; + } +} diff --git a/processing/src/main/java/io/druid/query/search/search/SearchQuery.java b/processing/src/main/java/io/druid/query/search/search/SearchQuery.java index f3d1976c86f..00c8106483d 100644 --- a/processing/src/main/java/io/druid/query/search/search/SearchQuery.java +++ b/processing/src/main/java/io/druid/query/search/search/SearchQuery.java @@ -66,10 +66,9 @@ public class SearchQuery extends BaseQuery> this.granularity = granularity == null ? QueryGranularities.ALL : granularity; this.limit = (limit == 0) ? 1000 : limit; this.dimensions = dimensions; - this.querySpec = querySpec; + this.querySpec = querySpec == null ? new AllSearchQuerySpec() : querySpec; Preconditions.checkNotNull(querySegmentSpec, "Must specify an interval"); - Preconditions.checkNotNull(querySpec, "Must specify a query"); } @Override diff --git a/processing/src/main/java/io/druid/query/search/search/SearchQuerySpec.java b/processing/src/main/java/io/druid/query/search/search/SearchQuerySpec.java index 4d9fcfc9518..55c9c1a8551 100644 --- a/processing/src/main/java/io/druid/query/search/search/SearchQuerySpec.java +++ b/processing/src/main/java/io/druid/query/search/search/SearchQuerySpec.java @@ -29,7 +29,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; @JsonSubTypes.Type(name = "contains", value = ContainsSearchQuerySpec.class), @JsonSubTypes.Type(name = "insensitive_contains", value = InsensitiveContainsSearchQuerySpec.class), @JsonSubTypes.Type(name = "fragment", value = FragmentSearchQuerySpec.class), - @JsonSubTypes.Type(name = "regex", value = RegexSearchQuerySpec.class) + @JsonSubTypes.Type(name = "regex", value = RegexSearchQuerySpec.class), + @JsonSubTypes.Type(name = "all", value = AllSearchQuerySpec.class) }) public interface SearchQuerySpec { diff --git a/processing/src/test/java/io/druid/query/search/SearchQueryRunnerTest.java b/processing/src/test/java/io/druid/query/search/SearchQueryRunnerTest.java index f3cf3284d0f..9938c5f4233 100644 --- a/processing/src/test/java/io/druid/query/search/SearchQueryRunnerTest.java +++ b/processing/src/test/java/io/druid/query/search/SearchQueryRunnerTest.java @@ -550,6 +550,35 @@ public class SearchQueryRunnerTest ); } + @Test + public void testSearchAll() + { + List expectedHits = Lists.newLinkedList(); + expectedHits.add(new SearchHit(QueryRunnerTestHelper.marketDimension, "spot", 837)); + expectedHits.add(new SearchHit(QueryRunnerTestHelper.marketDimension, "total_market", 186)); + expectedHits.add(new SearchHit(QueryRunnerTestHelper.marketDimension, "upfront", 186)); + + checkSearchQuery( + Druids.newSearchQueryBuilder() + .dataSource(QueryRunnerTestHelper.dataSource) + .granularity(QueryRunnerTestHelper.allGran) + .intervals(QueryRunnerTestHelper.fullOnInterval) + .dimensions(QueryRunnerTestHelper.marketDimension) + .query("") + .build(), + expectedHits + ); + checkSearchQuery( + Druids.newSearchQueryBuilder() + .dataSource(QueryRunnerTestHelper.dataSource) + .granularity(QueryRunnerTestHelper.allGran) + .intervals(QueryRunnerTestHelper.fullOnInterval) + .dimensions(QueryRunnerTestHelper.marketDimension) + .build(), + expectedHits + ); + } + private void checkSearchQuery(Query searchQuery, List expectedResults) { checkSearchQuery(searchQuery, runner, expectedResults);