suneet-s commented on a change in pull request #9800: URL: https://github.com/apache/druid/pull/9800#discussion_r420311705
########## File path: processing/src/main/java/org/apache/druid/query/filter/FalseDimFilter.java ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 org.apache.druid.query.filter; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.google.common.collect.ImmutableRangeSet; +import com.google.common.collect.RangeSet; +import org.apache.druid.query.cache.CacheKeyBuilder; +import org.apache.druid.segment.filter.FalseFilter; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Set; + +public class FalseDimFilter implements DimFilter +{ + private static final FalseDimFilter INSTANCE = new FalseDimFilter(); + + @JsonCreator + public static FalseDimFilter instance() + { + return INSTANCE; + } + + private FalseDimFilter() + { + } + + @Override + public DimFilter optimize() + { + return this; + } + + @Override + public Filter toFilter() + { + return FalseFilter.instance(); + } + + @Nullable + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + return ImmutableRangeSet.of(); + } + + @Override + public Set<String> getRequiredColumns() + { + return Collections.emptySet(); + } + + @Override + public byte[] getCacheKey() + { + return new CacheKeyBuilder(DimFilterUtils.FALSE_CACHE_ID).build(); Review comment: nit: should this be a static instance ########## File path: processing/src/main/java/org/apache/druid/query/filter/InDimFilter.java ########## @@ -73,9 +77,14 @@ private final Supplier<DruidFloatPredicate> floatPredicateSupplier; private final Supplier<DruidDoublePredicate> doublePredicateSupplier; + @JsonIgnore + private byte[] cacheKey; + @JsonCreator public InDimFilter( @JsonProperty("dimension") String dimension, + // This 'values' collection instance can be reused if possible to avoid copying a big collection. + // Callers should _not_ modify the collection after it is passed to this constructor. @JsonProperty("values") Collection<String> values, Review comment: Can we pass in a `Set<String>` instead of `Collection<String>` so jackson can do the de-dupe for us? ########## File path: processing/src/main/java/org/apache/druid/query/filter/AndDimFilter.java ########## @@ -72,8 +73,20 @@ public AndDimFilter(DimFilter... fields) @Override public DimFilter optimize() { - List<DimFilter> elements = DimFilters.optimize(fields); - return elements.size() == 1 ? elements.get(0) : new AndDimFilter(elements); + List<DimFilter> elements = DimFilters.optimize(fields) + .stream() + .filter(filter -> !(filter instanceof TrueDimFilter)) + .collect(Collectors.toList()); + if (elements.isEmpty()) { + // All elements were TrueDimFilter after optimization + return TrueDimFilter.instance(); + } else if (elements.size() == 1) { + return elements.get(0); Review comment: Looks like this code is hit by other tests in druid-processing, but I'm not sure which ones ########## File path: processing/src/main/java/org/apache/druid/query/filter/FalseDimFilter.java ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 org.apache.druid.query.filter; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.google.common.collect.ImmutableRangeSet; +import com.google.common.collect.RangeSet; +import org.apache.druid.query.cache.CacheKeyBuilder; +import org.apache.druid.segment.filter.FalseFilter; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.Set; + +public class FalseDimFilter implements DimFilter +{ + private static final FalseDimFilter INSTANCE = new FalseDimFilter(); + + @JsonCreator + public static FalseDimFilter instance() + { + return INSTANCE; + } + + private FalseDimFilter() + { + } + + @Override + public DimFilter optimize() + { + return this; + } + + @Override + public Filter toFilter() + { + return FalseFilter.instance(); + } + + @Nullable + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + return ImmutableRangeSet.of(); + } + + @Override + public Set<String> getRequiredColumns() + { + return Collections.emptySet(); + } + + @Override + public byte[] getCacheKey() + { + return new CacheKeyBuilder(DimFilterUtils.FALSE_CACHE_ID).build(); + } + + @Override + public int hashCode() + { + return DimFilterUtils.FALSE_CACHE_ID; + } + + @Override + public boolean equals(Object o) + { + return o == this; + } Review comment: EqualsVerifierTest for this? ########## File path: processing/src/main/java/org/apache/druid/query/filter/InDimFilter.java ########## @@ -132,31 +145,40 @@ public FilterTuning getFilterTuning() @Override public byte[] getCacheKey() { Review comment: Can multiple threads call `getCacheKey` at the same time? What happens if 2 threads try to build sortedValues? ########## File path: processing/src/main/java/org/apache/druid/query/filter/InDimFilter.java ########## @@ -132,31 +145,40 @@ public FilterTuning getFilterTuning() @Override public byte[] getCacheKey() { - boolean hasNull = false; - for (String value : values) { - if (value == null) { - hasNull = true; - break; + if (cacheKey == null) { + final List<String> sortedValues = new ArrayList<>(values); Review comment: Does this negate your comment on line 96? We'd be copying all the values here to a list so that we can sort the list. ########## File path: processing/src/main/java/org/apache/druid/query/filter/InDimFilter.java ########## @@ -132,31 +145,40 @@ public FilterTuning getFilterTuning() @Override public byte[] getCacheKey() { - boolean hasNull = false; - for (String value : values) { - if (value == null) { - hasNull = true; - break; + if (cacheKey == null) { + final List<String> sortedValues = new ArrayList<>(values); + sortedValues.sort(Comparator.nullsFirst(Ordering.natural())); + final Hasher hasher = Hashing.sha256().newHasher(); Review comment: Why did you choose a sha256 hasher? Is it fast to construct a new hasher? or to perform a sha256 hash? I wrote a test here which fails to produce the same cacheKey even though the values are sorted ``` @Test public void testCacheKey() { final InDimFilter dimFilter1 = new InDimFilter("dim", ImmutableList.of("v1", "v2", "v3"), null); final InDimFilter dimFilter2 = new InDimFilter("dim", ImmutableList.of("v3", "v2", "v1"), null); Assert.assertEquals(dimFilter1.getCacheKey(), dimFilter2.getCacheKey()); } ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
