Skip to content

Commit

Permalink
Add "like" filter. (apache#3642)
Browse files Browse the repository at this point in the history
* Add "like" filter.

* Addressed some PR comments.

* Slight simplifications to LikeFilter.

* Additional simplifications.

* Fix comment in LikeFilter.

* Clarify comment in LikeFilter.

* Simplify LikeMatcher a bit.

* No use going through the optimized path if prefix is empty.

* Add more tests.
  • Loading branch information
gianm authored and fundead committed Dec 7, 2016
1 parent e336ffd commit 1ab0f05
Show file tree
Hide file tree
Showing 10 changed files with 1,076 additions and 9 deletions.
251 changes: 251 additions & 0 deletions benchmarks/src/main/java/io/druid/benchmark/LikeFilterBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
/*
* 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.benchmark;

import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.metamx.collections.bitmap.BitmapFactory;
import com.metamx.collections.bitmap.ImmutableBitmap;
import com.metamx.collections.bitmap.MutableBitmap;
import com.metamx.collections.bitmap.RoaringBitmapFactory;
import com.metamx.collections.spatial.ImmutableRTree;
import io.druid.query.filter.BitmapIndexSelector;
import io.druid.query.filter.BoundDimFilter;
import io.druid.query.filter.Filter;
import io.druid.query.filter.LikeDimFilter;
import io.druid.query.filter.RegexDimFilter;
import io.druid.query.filter.SelectorDimFilter;
import io.druid.query.ordering.StringComparators;
import io.druid.segment.column.BitmapIndex;
import io.druid.segment.data.BitmapSerdeFactory;
import io.druid.segment.data.GenericIndexed;
import io.druid.segment.data.Indexed;
import io.druid.segment.data.RoaringBitmapSerdeFactory;
import io.druid.segment.serde.BitmapIndexColumnPartSupplier;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
@Fork(value = 1)
@Warmup(iterations = 10)
@Measurement(iterations = 10)
public class LikeFilterBenchmark
{
private static final int START_INT = 1_000_000;
private static final int END_INT = 9_999_999;

private static final Filter SELECTOR_EQUALS = new SelectorDimFilter(
"foo",
"1000000",
null
).toFilter();

private static final Filter LIKE_EQUALS = new LikeDimFilter(
"foo",
"1000000",
null,
null
).toFilter();

private static final Filter BOUND_PREFIX = new BoundDimFilter(
"foo",
"50",
"50\uffff",
false,
false,
null,
null,
StringComparators.LEXICOGRAPHIC
).toFilter();

private static final Filter REGEX_PREFIX = new RegexDimFilter(
"foo",
"^50.*",
null
).toFilter();

private static final Filter LIKE_PREFIX = new LikeDimFilter(
"foo",
"50%",
null,
null
).toFilter();

// cardinality, the dictionary will contain evenly spaced integers
@Param({"1000", "100000", "1000000"})
int cardinality;

int step;

// selector will contain a cardinality number of bitmaps; each one contains a single int: 0
BitmapIndexSelector selector;

@Setup
public void setup() throws IOException
{
step = (END_INT - START_INT) / cardinality;
final BitmapFactory bitmapFactory = new RoaringBitmapFactory();
final BitmapSerdeFactory serdeFactory = new RoaringBitmapSerdeFactory(null);
final List<Integer> ints = generateInts();
final GenericIndexed<String> dictionary = GenericIndexed.fromIterable(
FluentIterable.from(ints)
.transform(
new Function<Integer, String>()
{
@Override
public String apply(Integer i)
{
return i.toString();
}
}
),
GenericIndexed.STRING_STRATEGY
);
final BitmapIndex bitmapIndex = new BitmapIndexColumnPartSupplier(
bitmapFactory,
GenericIndexed.fromIterable(
FluentIterable.from(ints)
.transform(
new Function<Integer, ImmutableBitmap>()
{
@Override
public ImmutableBitmap apply(Integer i)
{
final MutableBitmap mutableBitmap = bitmapFactory.makeEmptyMutableBitmap();
mutableBitmap.add((i - START_INT) / step);
return bitmapFactory.makeImmutableBitmap(mutableBitmap);
}
}
),
serdeFactory.getObjectStrategy()
),
dictionary
).get();
selector = new BitmapIndexSelector()
{
@Override
public Indexed<String> getDimensionValues(String dimension)
{
return dictionary;
}

@Override
public int getNumRows()
{
throw new UnsupportedOperationException();
}

@Override
public BitmapFactory getBitmapFactory()
{
return bitmapFactory;
}

@Override
public ImmutableBitmap getBitmapIndex(String dimension, String value)
{
return bitmapIndex.getBitmap(bitmapIndex.getIndex(value));
}

@Override
public BitmapIndex getBitmapIndex(String dimension)
{
return bitmapIndex;
}

@Override
public ImmutableRTree getSpatialIndex(String dimension)
{
throw new UnsupportedOperationException();
}
};
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void matchLikeEquals(Blackhole blackhole)
{
final ImmutableBitmap bitmapIndex = LIKE_EQUALS.getBitmapIndex(selector);
blackhole.consume(bitmapIndex);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void matchSelectorEquals(Blackhole blackhole)
{
final ImmutableBitmap bitmapIndex = SELECTOR_EQUALS.getBitmapIndex(selector);
blackhole.consume(bitmapIndex);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void matchLikePrefix(Blackhole blackhole)
{
final ImmutableBitmap bitmapIndex = LIKE_PREFIX.getBitmapIndex(selector);
blackhole.consume(bitmapIndex);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void matchBoundPrefix(Blackhole blackhole)
{
final ImmutableBitmap bitmapIndex = BOUND_PREFIX.getBitmapIndex(selector);
blackhole.consume(bitmapIndex);
}

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void matchRegexPrefix(Blackhole blackhole)
{
final ImmutableBitmap bitmapIndex = REGEX_PREFIX.getBitmapIndex(selector);
blackhole.consume(bitmapIndex);
}

private List<Integer> generateInts()
{
final List<Integer> ints = new ArrayList<>(cardinality);

for (int i = 0; i < cardinality; i++) {
ints.add(START_INT + step * i);
}

return ints;
}
}
30 changes: 28 additions & 2 deletions docs/content/querying/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,36 @@ The grammar for a IN filter is as follows:

The IN filter supports the use of extraction functions, see [Filtering with Extraction Functions](#filtering-with-extraction-functions) for details.

### Like filter

Like filters can be used for basic wildcard searches. They are equivalent to the SQL LIKE operator. Special characters
supported are "%" (matches any number of characters) and "\_" (matches any one character).

|property|type|description|required?|
|--------|-----------|---------|---------|
|type|String|This should always be "like".|yes|
|dimension|String|The dimension to filter on|yes|
|pattern|String|LIKE pattern, such as "foo%" or "___bar".|yes|
|escape|String|An escape character that can be used to escape special characters.|no|
|extractionFn|[Extraction function](#filtering-with-extraction-functions)| Extraction function to apply to the dimension|no|

Like filters support the use of extraction functions, see [Filtering with Extraction Functions](#filtering-with-extraction-functions) for details.

This Like filter expresses the condition `last_name LIKE "D%"` (i.e. last_name starts with "D").

```json
{
"type": "like",
"dimension": "last_name",
"pattern": "D%"
}
```

### Bound filter

The Bound filter can be used to filter by comparing dimension values to an upper value and/or a lower value.
Bound filters can be used to filter on ranges of dimension values. It can be used for comparison filtering like
greater than, less than, greater than or equal to, less than or equal to, and "between" (if both "lower" and
"upper" are set).

|property|type|description|required?|
|--------|-----------|---------|---------|
Expand All @@ -218,7 +244,7 @@ The Bound filter can be used to filter by comparing dimension values to an upper
|ordering|String|Specifies the sorting order to use when comparing values against the bound. Can be one of the following values: "lexicographic", "alphanumeric", "numeric", "strlen". See [Sorting Orders](./sorting-orders.html) for more details.|no, default: "lexicographic"|
|extractionFn|[Extraction function](#filtering-with-extraction-functions)| Extraction function to apply to the dimension|no|

The bound filter supports the use of extraction functions, see [Filtering with Extraction Functions](#filtering-with-extraction-functions) for details.
Bound filters support the use of extraction functions, see [Filtering with Extraction Functions](#filtering-with-extraction-functions) for details.

The following bound filter expresses the condition `21 <= age <= 31`:
```json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
@JsonSubTypes.Type(name="spatial", value=SpatialDimFilter.class),
@JsonSubTypes.Type(name="in", value=InDimFilter.class),
@JsonSubTypes.Type(name="bound", value=BoundDimFilter.class),
@JsonSubTypes.Type(name="interval", value=IntervalDimFilter.class)
@JsonSubTypes.Type(name="interval", value=IntervalDimFilter.class),
@JsonSubTypes.Type(name="like", value=LikeDimFilter.class)
})
public interface DimFilter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class DimFilterUtils
static final byte IN_CACHE_ID = 0x9;
static final byte BOUND_CACHE_ID = 0xA;
static final byte INTERVAL_CACHE_ID = 0xB;
static final byte LIKE_CACHE_ID = 0xC;
public static final byte STRING_SEPARATOR = (byte) 0xFF;

static byte[] computeCacheKey(byte cacheIdKey, List<DimFilter> filters)
Expand Down
Loading

0 comments on commit 1ab0f05

Please sign in to comment.