github-code-scanning[bot] commented on code in PR #14542: URL: https://github.com/apache/druid/pull/14542#discussion_r1255314859
########## processing/src/main/java/org/apache/druid/query/filter/EqualityFilter.java: ########## @@ -0,0 +1,537 @@ +/* + * 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.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Range; +import com.google.common.collect.RangeSet; +import com.google.common.collect.TreeRangeSet; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.math.expr.ExprEval; +import org.apache.druid.math.expr.ExpressionType; +import org.apache.druid.query.cache.CacheKeyBuilder; +import org.apache.druid.query.extraction.ExtractionFn; +import org.apache.druid.query.filter.vector.VectorValueMatcher; +import org.apache.druid.query.filter.vector.VectorValueMatcherColumnProcessorFactory; +import org.apache.druid.segment.BaseDoubleColumnValueSelector; +import org.apache.druid.segment.BaseFloatColumnValueSelector; +import org.apache.druid.segment.BaseLongColumnValueSelector; +import org.apache.druid.segment.BaseObjectColumnValueSelector; +import org.apache.druid.segment.ColumnInspector; +import org.apache.druid.segment.ColumnProcessorFactory; +import org.apache.druid.segment.ColumnProcessors; +import org.apache.druid.segment.ColumnSelector; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.apache.druid.segment.DimensionSelector; +import org.apache.druid.segment.column.ColumnCapabilities; +import org.apache.druid.segment.column.ColumnIndexSupplier; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.TypeStrategy; +import org.apache.druid.segment.filter.DimensionPredicateFilter; +import org.apache.druid.segment.filter.Filters; +import org.apache.druid.segment.filter.PredicateValueMatcherFactory; +import org.apache.druid.segment.filter.ValueMatchers; +import org.apache.druid.segment.index.BitmapColumnIndex; +import org.apache.druid.segment.index.StringValueSetIndex; +import org.apache.druid.segment.vector.VectorColumnSelectorFactory; + +import javax.annotation.Nullable; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +public class EqualityFilter extends AbstractOptimizableDimFilter implements Filter +{ + private final String column; + private final ColumnType matchValueType; + private final Object matchValue; + @Nullable + private final ExtractionFn extractionFn; + @Nullable + private final FilterTuning filterTuning; + private final DruidPredicateFactory predicateFactory; + + @JsonCreator + public EqualityFilter( + @JsonProperty("column") String column, + @JsonProperty("matchValueType") ColumnType matchValueType, + @JsonProperty("matchValue") Object matchValue, + @JsonProperty("extractionFn") @Nullable ExtractionFn extractionFn, + @JsonProperty("filterTuning") @Nullable FilterTuning filterTuning + ) + { + Preconditions.checkArgument(column != null, "column must not be null"); + Preconditions.checkArgument(matchValue != null, "value must not be null"); + + this.column = column; + this.matchValueType = matchValueType; + this.matchValue = matchValue; + this.extractionFn = extractionFn; + this.filterTuning = filterTuning; + this.predicateFactory = new EqualityPredicateFactory(matchValue, matchValueType); + } + + @Override + public byte[] getCacheKey() + { + final TypeStrategy<Object> typeStrategy = matchValueType.getStrategy(); + final int size = typeStrategy.estimateSizeBytes(matchValue); + final ByteBuffer valueBuffer = ByteBuffer.allocate(size); + typeStrategy.write(valueBuffer, matchValue, size); + return new CacheKeyBuilder(DimFilterUtils.EQUALS_CACHE_ID) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(column) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(matchValueType.asTypeString()) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByteArray(valueBuffer.array()) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByteArray(extractionFn == null ? new byte[0] : extractionFn.getCacheKey()) + .build(); + } + + @Override + public DimFilter optimize() + { + return this; + } + + @Override + public Filter toFilter() + { + if (extractionFn == null) { + return this; + } else { + return new DimensionPredicateFilter(column, predicateFactory, extractionFn, filterTuning); + } + } + + @JsonProperty + public String getColumn() + { + return column; + } + + @JsonProperty + public ColumnType getMatchValueType() + { + return matchValueType; + } + + @JsonProperty + public Object getMatchValue() + { + return matchValue; + } + + @Nullable + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_NULL) + public ExtractionFn getExtractionFn() + { + return extractionFn; + } + + @Nullable + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_NULL) + public FilterTuning getFilterTuning() + { + return filterTuning; + } + + @Override + public String toString() + { + DimFilter.DimFilterToStringBuilder bob = new DimFilter.DimFilterToStringBuilder().appendDimension( + column, + extractionFn + ) + .append(" = ") + .append(matchValue); + + if (!ColumnType.STRING.equals(matchValueType)) { + bob.append(" (" + matchValueType.asTypeString() + ")"); + } + return bob.appendFilterTuning(filterTuning).build(); + } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + boolean valuesMatch = false; + EqualityFilter that = (EqualityFilter) o; + if (matchValue instanceof Object[] && that.matchValue instanceof Object[]) { + valuesMatch = Arrays.deepEquals((Object[]) matchValue, (Object[]) that.matchValue); + } else { + valuesMatch = Objects.equals(matchValue, that.matchValue); + } + return column.equals(that.column) && + Objects.equals(matchValueType, that.matchValueType) && + valuesMatch && + Objects.equals(extractionFn, that.extractionFn) && + Objects.equals(filterTuning, that.filterTuning); + } + + @Override + public int hashCode() + { + return Objects.hash(column, matchValueType, matchValue, extractionFn, filterTuning); + } + + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + if (!Objects.equals(getColumn(), dimension) || getExtractionFn() != null) { + return null; + } + RangeSet<String> retSet = TreeRangeSet.create(); + // todo (clint): this is lame.. but matches how range partitioning currently works i think + retSet.add(Range.singleton(String.valueOf(matchValue))); + return retSet; + } + + @Nullable + @Override + public BitmapColumnIndex getBitmapColumnIndex(ColumnIndexSelector selector) + { + if (!Filters.checkFilterTuningUseIndex(column, selector, filterTuning)) { + return null; + } + + final ColumnIndexSupplier indexSupplier = selector.getIndexSupplier(column); + if (indexSupplier == null) { + return Filters.makeNullIndex(false, selector); + } + + // todo (clint): do it for reals, i think we can do better than string value set ... + + final StringValueSetIndex valueSetIndex = indexSupplier.as(StringValueSetIndex.class); + if (valueSetIndex == null) { + // column exists, but has no index + return null; + } + return valueSetIndex.forValue(String.valueOf(matchValue)); + } + + @Override + public ValueMatcher makeMatcher(ColumnSelectorFactory factory) + { + return ColumnProcessors.makeProcessor( + column, + new TypedConstantValueMatcherFactory(matchValue, matchValueType), + factory + ); + } + + @Override + public VectorValueMatcher makeVectorMatcher(VectorColumnSelectorFactory factory) + { + final ColumnCapabilities capabilities = factory.getColumnCapabilities(column); + + if (matchValueType.isPrimitive() && (capabilities == null || capabilities.isPrimitive())) { + return ColumnProcessors.makeVectorProcessor( + column, + VectorValueMatcherColumnProcessorFactory.instance(), + factory + ).makeMatcher(matchValue, matchValueType); + } + return ColumnProcessors.makeVectorProcessor( + column, + VectorValueMatcherColumnProcessorFactory.instance(), + factory + ).makeMatcher(new EqualityPredicateFactory(matchValue, matchValueType)); + } + + @Override + public boolean supportsSelectivityEstimation(ColumnSelector columnSelector, ColumnIndexSelector indexSelector) + { + return Filters.supportsSelectivityEstimation(this, column, columnSelector, indexSelector); + } + + @Override + public boolean canVectorizeMatcher(ColumnInspector inspector) + { + return true; + } + + @Override + public Set<String> getRequiredColumns() + { + return ImmutableSet.of(column); + } + + @Override + public boolean supportsRequiredColumnRewrite() + { + return true; + } + + @Override + public Filter rewriteRequiredColumns(Map<String, String> columnRewrites) + { + String rewriteDimensionTo = columnRewrites.get(column); + + if (rewriteDimensionTo == null) { + throw new IAE( + "Received a non-applicable rewrite: %s, filter's dimension: %s", + columnRewrites, + columnRewrites + ); + } + + return new EqualityFilter( + rewriteDimensionTo, + matchValueType, + matchValue, + extractionFn, + filterTuning + ); + } + + private static class EqualityPredicateFactory implements DruidPredicateFactory + { + private final ExprEval<?> matchValue; + private final ColumnType matchValueType; + + private final Object initLock = new Object(); + + private volatile DruidLongPredicate longPredicate; + private volatile DruidFloatPredicate floatPredicate; + private volatile DruidDoublePredicate doublePredicate; + + public EqualityPredicateFactory(Object matchValue, ColumnType matchValueType) + { + this.matchValue = ExprEval.ofType(ExpressionType.fromColumnType(matchValueType), matchValue); + this.matchValueType = matchValueType; + } + + @Override + public Predicate<String> makeStringPredicate() + { + return Predicates.equalTo(matchValue.castTo(ExpressionType.STRING).asString()); + } + + @Override + public DruidLongPredicate makeLongPredicate() + { + initLongPredicate(); + return longPredicate; + } + + @Override + public DruidFloatPredicate makeFloatPredicate() + { + initFloatPredicate(); + return floatPredicate; + } + + @Override + public DruidDoublePredicate makeDoublePredicate() + { + initDoublePredicate(); + return doublePredicate; + } + + @Override + public Predicate<Object[]> makeArrayPredicate() + { + final Object[] arrayValue = matchValue.asArray(); + return input -> Arrays.deepEquals(input, arrayValue); + } + + @Override + public Predicate<Object> makeObjectPredicate() + { + return Predicates.equalTo(matchValue.valueOrDefault()); + } + + private void initLongPredicate() + { + if (longPredicate != null) { + return; + } + synchronized (initLock) { + if (longPredicate != null) { + return; + } + if (matchValue == null) { + longPredicate = DruidLongPredicate.MATCH_NULL_ONLY; + return; + } + final Long valueAsLong = (Long) matchValue.castTo(ExpressionType.LONG).valueOrDefault(); + + if (valueAsLong == null) { + longPredicate = DruidLongPredicate.ALWAYS_FALSE; + } else { + // store the primitive, so we don't unbox for every comparison + final long unboxedLong = valueAsLong; + longPredicate = input -> input == unboxedLong; + } + } + } + + private void initFloatPredicate() + { + if (floatPredicate != null) { + return; + } + synchronized (initLock) { + if (floatPredicate != null) { + return; + } + + if (matchValue == null) { + floatPredicate = DruidFloatPredicate.MATCH_NULL_ONLY; + return; + } + final Float valueAsFloat = ((Number) matchValue.castTo(ExpressionType.DOUBLE).valueOrDefault()).floatValue(); Review Comment: ## Boxed variable is never null The variable 'valueAsFloat' is only assigned values of primitive type and is never 'null', but it is declared with the boxed type 'Float'. [Show more details](https://github.com/apache/druid/security/code-scanning/5229) -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
