clintropolis commented on a change in pull request #9484: Harmonization and bug-fixing for selector and filter behavior on unknown types. URL: https://github.com/apache/druid/pull/9484#discussion_r389845742
########## File path: processing/src/main/java/org/apache/druid/segment/filter/ValueMatchers.java ########## @@ -0,0 +1,365 @@ +/* + * 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.segment.filter; + +import com.google.common.base.Predicate; +import org.apache.druid.common.config.NullHandling; +import org.apache.druid.query.filter.DruidDoublePredicate; +import org.apache.druid.query.filter.DruidFloatPredicate; +import org.apache.druid.query.filter.DruidLongPredicate; +import org.apache.druid.query.filter.DruidPredicateFactory; +import org.apache.druid.query.filter.ValueMatcher; +import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector; +import org.apache.druid.segment.BaseDoubleColumnValueSelector; +import org.apache.druid.segment.BaseFloatColumnValueSelector; +import org.apache.druid.segment.BaseLongColumnValueSelector; +import org.apache.druid.segment.BaseNullableColumnValueSelector; +import org.apache.druid.segment.DimensionDictionarySelector; +import org.apache.druid.segment.DimensionHandlerUtils; +import org.apache.druid.segment.DimensionSelector; + +import javax.annotation.Nullable; +import java.util.Objects; + +/** + * Utility methods for creating {@link ValueMatcher} instances. Mainly used by {@link ConstantValueMatcherFactory} + * and {@link PredicateValueMatcherFactory}. + */ +public class ValueMatchers +{ + private ValueMatchers() + { + // No instantiation. + } + + /** + * Creates a constant-based {@link ValueMatcher} for a string-typed selector. + * + * @param selector column selector + * @param value value to match + * @param hasMultipleValues whether the column selector *might* have multiple values + */ + public static ValueMatcher makeStringValueMatcher( + final DimensionSelector selector, + final String value, + final boolean hasMultipleValues + ) + { + final ValueMatcher booleanMatcher = toBooleanMatcherIfPossible( + selector, + hasMultipleValues, + s -> Objects.equals(s, NullHandling.emptyToNullIfNeeded(value)) + ); + + if (booleanMatcher != null) { + return booleanMatcher; + } else { + return selector.makeValueMatcher(value); + } + } + + /** + * Creates a predicate-based {@link ValueMatcher} for a string-typed selector. + * + * @param selector column selector + * @param predicateFactory predicate to match + * @param hasMultipleValues whether the column selector *might* have multiple values + */ + public static ValueMatcher makeStringValueMatcher( + final DimensionSelector selector, + final DruidPredicateFactory predicateFactory, + final boolean hasMultipleValues + ) + { + final Predicate<String> predicate = predicateFactory.makeStringPredicate(); + final ValueMatcher booleanMatcher = toBooleanMatcherIfPossible(selector, hasMultipleValues, predicate); + + if (booleanMatcher != null) { + return booleanMatcher; + } else { + return selector.makeValueMatcher(predicate); + } + } + + /** + * Creates a constant-based {@link ValueMatcher} for a float-typed selector. + * + * @param selector column selector + * @param value value to match + */ + public static ValueMatcher makeFloatValueMatcher( + final BaseFloatColumnValueSelector selector, + final String value + ) + { + final Float matchVal = DimensionHandlerUtils.convertObjectToFloat(value); + if (matchVal == null) { + return makeNumericNullValueMatcher(selector); + } + + // Use "floatToIntBits" to canonicalize NaN values. + final int matchValIntBits = Float.floatToIntBits(matchVal); + return new ValueMatcher() + { + @Override + public boolean matches() + { + if (selector.isNull()) { + return false; + } + return Float.floatToIntBits(selector.getFloat()) == matchValIntBits; + } + + @Override + public void inspectRuntimeShape(RuntimeShapeInspector inspector) + { + inspector.visit("selector", selector); + } + }; + } + + public static ValueMatcher makeLongValueMatcher(final BaseLongColumnValueSelector selector, final String value) + { + final Long matchVal = DimensionHandlerUtils.convertObjectToLong(value); + if (matchVal == null) { + return makeNumericNullValueMatcher(selector); + } + final long matchValLong = matchVal; + return new ValueMatcher() + { + @Override + public boolean matches() + { + if (selector.isNull()) { + return false; + } + return selector.getLong() == matchValLong; + } + + @Override + public void inspectRuntimeShape(RuntimeShapeInspector inspector) + { + inspector.visit("selector", selector); + } + }; + } + + public static ValueMatcher makeLongValueMatcher( Review comment: same nit re javadoc ---------------------------------------------------------------- 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] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
