-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1936 from b-slim/between_range_with_predicat
adding Upper/Lower Bound Filter
- Loading branch information
Showing
8 changed files
with
503 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
processing/src/main/java/io/druid/query/filter/BoundDimFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* 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.Preconditions; | ||
import com.metamx.common.StringUtils; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class BoundDimFilter implements DimFilter | ||
{ | ||
private final String dimension; | ||
private final String upper; | ||
private final String lower; | ||
private final boolean lowerStrict; | ||
private final boolean upperStrict; | ||
private final boolean alphaNumeric; | ||
|
||
@JsonCreator | ||
public BoundDimFilter( | ||
@JsonProperty("dimension") String dimension, | ||
@JsonProperty("lower") String lower, | ||
@JsonProperty("upper") String upper, | ||
@JsonProperty("lowerStrict") Boolean lowerStrict, | ||
@JsonProperty("upperStrict") Boolean upperStrict, | ||
@JsonProperty("alphaNumeric") Boolean alphaNumeric | ||
) | ||
{ | ||
this.dimension = Preconditions.checkNotNull(dimension, "dimension can not be null"); | ||
Preconditions.checkState((lower != null) || (upper != null), "lower and upper can not be null at the same time"); | ||
this.upper = upper; | ||
this.lower = lower; | ||
this.lowerStrict = (lowerStrict == null) ? false : lowerStrict; | ||
this.upperStrict = (upperStrict == null) ? false : upperStrict; | ||
this.alphaNumeric = (alphaNumeric == null) ? false : alphaNumeric; | ||
} | ||
|
||
@JsonProperty | ||
public String getDimension() | ||
{ | ||
return dimension; | ||
} | ||
|
||
@JsonProperty | ||
public String getUpper() | ||
{ | ||
return upper; | ||
} | ||
|
||
@JsonProperty | ||
public String getLower() | ||
{ | ||
return lower; | ||
} | ||
|
||
@JsonProperty | ||
public boolean isLowerStrict() | ||
{ | ||
return lowerStrict; | ||
} | ||
|
||
@JsonProperty | ||
public boolean isUpperStrict() | ||
{ | ||
return upperStrict; | ||
} | ||
|
||
@JsonProperty | ||
public boolean isAlphaNumeric() | ||
{ | ||
return alphaNumeric; | ||
} | ||
|
||
@Override | ||
public byte[] getCacheKey() | ||
{ | ||
byte[] dimensionBytes = StringUtils.toUtf8(this.getDimension()); | ||
byte[] lowerBytes = this.getLower() == null ? new byte[0] : StringUtils.toUtf8(this.getLower()); | ||
byte[] upperBytes = this.getUpper() == null ? new byte[0] : StringUtils.toUtf8(this.getUpper()); | ||
byte boundType = 0x1; | ||
if (this.getLower() == null) { | ||
boundType = 0x2; | ||
} else if (this.getUpper() == null) { | ||
boundType = 0x3; | ||
} | ||
|
||
byte lowerStrictByte = (this.isLowerStrict() == false) ? 0x0 : (byte) 1; | ||
byte upperStrictByte = (this.isUpperStrict() == false) ? 0x0 : (byte) 1; | ||
byte AlphaNumericByte = (this.isAlphaNumeric() == false) ? 0x0 : (byte) 1; | ||
|
||
ByteBuffer boundCacheBuffer = ByteBuffer.allocate( | ||
8 | ||
+ dimensionBytes.length | ||
+ upperBytes.length | ||
+ lowerBytes.length | ||
); | ||
boundCacheBuffer.put(DimFilterCacheHelper.BOUND_CACHE_ID) | ||
.put(boundType) | ||
.put(upperStrictByte) | ||
.put(lowerStrictByte) | ||
.put(AlphaNumericByte) | ||
.put(DimFilterCacheHelper.STRING_SEPARATOR) | ||
.put(dimensionBytes) | ||
.put(DimFilterCacheHelper.STRING_SEPARATOR) | ||
.put(upperBytes) | ||
.put(DimFilterCacheHelper.STRING_SEPARATOR) | ||
.put(lowerBytes); | ||
return boundCacheBuffer.array(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) | ||
{ | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof BoundDimFilter)) { | ||
return false; | ||
} | ||
|
||
BoundDimFilter that = (BoundDimFilter) o; | ||
|
||
if (isLowerStrict() != that.isLowerStrict()) { | ||
return false; | ||
} | ||
if (isUpperStrict() != that.isUpperStrict()) { | ||
return false; | ||
} | ||
if (isAlphaNumeric() != that.isAlphaNumeric()) { | ||
return false; | ||
} | ||
if (!getDimension().equals(that.getDimension())) { | ||
return false; | ||
} | ||
if (getUpper() != null ? !getUpper().equals(that.getUpper()) : that.getUpper() != null) { | ||
return false; | ||
} | ||
return !(getLower() != null ? !getLower().equals(that.getLower()) : that.getLower() != null); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
int result = getDimension().hashCode(); | ||
result = 31 * result + (getUpper() != null ? getUpper().hashCode() : 0); | ||
result = 31 * result + (getLower() != null ? getLower().hashCode() : 0); | ||
result = 31 * result + (isLowerStrict() ? 1 : 0); | ||
result = 31 * result + (isUpperStrict() ? 1 : 0); | ||
result = 31 * result + (isAlphaNumeric() ? 1 : 0); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
processing/src/main/java/io/druid/segment/filter/BoundFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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.segment.filter; | ||
|
||
import com.google.common.base.Predicate; | ||
import io.druid.query.filter.BoundDimFilter; | ||
import io.druid.query.topn.AlphaNumericTopNMetricSpec; | ||
import io.druid.query.topn.LexicographicTopNMetricSpec; | ||
|
||
import java.util.Comparator; | ||
|
||
public class BoundFilter extends DimensionPredicateFilter | ||
{ | ||
|
||
public BoundFilter(final BoundDimFilter boundDimFilter) | ||
{ | ||
super( | ||
boundDimFilter.getDimension(), new Predicate<String>() | ||
{ | ||
@Override | ||
public boolean apply(String input) | ||
{ | ||
if (input == null) { | ||
return false; | ||
} | ||
Comparator<String> comparator; | ||
if (boundDimFilter.isAlphaNumeric()) { | ||
comparator = new AlphaNumericTopNMetricSpec(null).getComparator(null, null); | ||
} else { | ||
comparator = new LexicographicTopNMetricSpec(null).getComparator(null, null); | ||
} | ||
|
||
int lowerComparing = 1; | ||
int upperComparing = 1; | ||
if (boundDimFilter.getLower() != null) { | ||
lowerComparing = comparator.compare(input, boundDimFilter.getLower()); | ||
} | ||
if (boundDimFilter.getUpper() != null) { | ||
upperComparing = comparator.compare(boundDimFilter.getUpper(), input); | ||
} | ||
if (boundDimFilter.isLowerStrict() && boundDimFilter.isUpperStrict()) { | ||
return ((lowerComparing > 0)) && ( upperComparing > 0); | ||
} else if (boundDimFilter.isLowerStrict()) { | ||
return (lowerComparing > 0) && (upperComparing >= 0); | ||
} else if (boundDimFilter.isUpperStrict()) { | ||
return (lowerComparing >= 0) && (upperComparing > 0); | ||
} | ||
return (lowerComparing >= 0) && (upperComparing >= 0); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.