Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/content/querying/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ This is the equivalent of `WHERE <dimension_string> = '<dimension_value_string>'

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

### Column Comparison filter

The column comparison filter is similar to the selector filter, but instead compares dimensions to each other. For example:

``` json
"filter": { "type": "columnComparison", "dimensions": [<dimension_a>, <dimension_b>] }
```

This is the equivalent of `WHERE <dimension_a> = <dimension_b>`.

`dimensions` is list of [DimensionSpecs](./dimensionspecs.html), making it possible to apply an extraction function if needed.

### Regular expression filter

The regular expression filter is similar to the selector filter, but using regular expressions. It matches the specified dimension with the given pattern. The pattern can be any standard [Java regular expression](http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html).
Expand Down
1 change: 1 addition & 0 deletions docs/content/querying/multi-value-dimensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dimensions. Filters follow these rules on multi-value dimensions:

- Value filters (like "selector", "bound", and "in") match a row if any of the values of a multi-value dimension match
the filter.
- The Column Comparison filter will match a row if the dimensions have any overlap.
- Value filters that match `null` or `""` (empty string) will match empty cells in a multi-value dimension.
- Logical expression filters behave the same way they do on single-value dimensions: "and" matches a row if all
underlying filters match that row; "or" matches a row if any underlying filters match that row; "not" matches a row
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.filter;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet;
import io.druid.query.cache.CacheKeyBuilder;
import io.druid.query.dimension.DimensionSpec;
import io.druid.segment.filter.ColumnComparisonFilter;

import java.util.List;

/**
*/
public class ColumnComparisonDimFilter implements DimFilter
{
private static final Joiner COMMA_JOINER = Joiner.on(", ");

private final List<DimensionSpec> dimensions;

@JsonCreator
public ColumnComparisonDimFilter(
@JsonProperty("dimensions") List<DimensionSpec> dimensions
)
{
this.dimensions = Preconditions.checkNotNull(dimensions, "dimensions");
Preconditions.checkArgument(dimensions.size() >= 2, "dimensions must have a least 2 dimensions");
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(DimFilterUtils.COLUMN_COMPARISON_CACHE_ID)
// Since a = b is the same as b = a we can ignore the order here.
.appendCacheablesIgnoringOrder(dimensions)
.build();
}

@Override
public DimFilter optimize()
{
return this;
}

@Override
public Filter toFilter()
{
return new ColumnComparisonFilter(dimensions);
}

@JsonProperty
public List<DimensionSpec> getDimensions()
{
return dimensions;
}

@Override
public String toString()
{
return "ColumnComparisonDimFilter{" +
"dimensions=[" + COMMA_JOINER.join(dimensions) + "]" +
"}";
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ColumnComparisonDimFilter that = (ColumnComparisonDimFilter) o;

return dimensions.equals(that.dimensions);
}

@Override
public RangeSet<String> getDimensionRangeSet(String dimension)
{
return null;
}

@Override
public int hashCode()
{
return 31 * dimensions.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@JsonSubTypes.Type(name="or", value=OrDimFilter.class),
@JsonSubTypes.Type(name="not", value=NotDimFilter.class),
@JsonSubTypes.Type(name="selector", value=SelectorDimFilter.class),
@JsonSubTypes.Type(name="columnComparison", value=ColumnComparisonDimFilter.class),
@JsonSubTypes.Type(name="extraction", value=ExtractionDimFilter.class),
@JsonSubTypes.Type(name="regex", value=RegexDimFilter.class),
@JsonSubTypes.Type(name="search", value=SearchQueryDimFilter.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class DimFilterUtils
static final byte BOUND_CACHE_ID = 0xA;
static final byte INTERVAL_CACHE_ID = 0xB;
static final byte LIKE_CACHE_ID = 0xC;
static final byte COLUMN_COMPARISON_CACHE_ID = 0xD;
public static final byte STRING_SEPARATOR = (byte) 0xFF;

static byte[] computeCacheKey(byte cacheIdKey, List<DimFilter> filters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ public boolean matches()
}
};
}

@Override
public ValueGetter makeValueGetter(final FloatColumnSelector selector)
{
return new ValueGetter()
{
@Override
public String[] get()
{
return new String[]{ Float.toString(selector.get()) };
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,17 @@ public boolean matches()
}
};
}

@Override
public ValueGetter makeValueGetter(final LongColumnSelector selector)
{
return new ValueGetter()
{
@Override
public String[] get()
{
return new String[]{ Long.toString(selector.get()) };
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import io.druid.segment.DimensionSelector;
import io.druid.segment.data.IndexedInts;
import io.druid.segment.filter.BooleanValueMatcher;

public class StringValueMatcherColumnSelectorStrategy implements ValueMatcherColumnSelectorStrategy<DimensionSelector>
{
private static final String[] NULL_VALUE = new String[]{ null };
private static final ValueGetter NULL_VALUE_GETTER = new ValueGetter()
{
@Override
public String[] get()
{
return NULL_VALUE;
}
};

@Override
public ValueMatcher makeValueMatcher(final DimensionSelector selector, String value)
{
Expand All @@ -51,4 +62,30 @@ public ValueMatcher makeValueMatcher(
}
}

@Override
public ValueGetter makeValueGetter(final DimensionSelector selector)
{
if (selector.getValueCardinality() == 0) {
return NULL_VALUE_GETTER;
} else {
return new ValueGetter()
{
@Override
public String[] get()
{
final IndexedInts row = selector.getRow();
final int size = row.size();
if (size == 0) {
return NULL_VALUE;
} else {
String[] values = new String[size];
for (int i = 0; i < size; ++i) {
values[i] = Strings.emptyToNull(selector.lookupName(row.get(i)));
}
return values;
}
}
};
}
}
}
31 changes: 31 additions & 0 deletions processing/src/main/java/io/druid/query/filter/ValueGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.filter;

/**
*/
public interface ValueGetter
{
// It is not ideal that Long and Float values will get
// converted to strings. We should also add functions
// for these and modify ColumnComparisonFilter to handle
// comparing Long and Float columns to eachother.
public String[] get();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ public interface ValueMatcherColumnSelectorStrategy<ValueSelectorType extends Co
* @return A ValueMatcher that applies the predicate for this DimensionQueryHelper's value type from the predicateFactory
*/
ValueMatcher makeValueMatcher(ValueSelectorType selector, DruidPredicateFactory predicateFactory);

/**
* Create a ValueGetter.
*
* @param selector Column selector
* @return A ValueGetter that returns the value(s) of the selected column
*/
ValueGetter makeValueGetter(ValueSelectorType selector);
}
Loading