clintropolis commented on code in PR #16039: URL: https://github.com/apache/druid/pull/16039#discussion_r1533197382
########## processing/src/main/java/org/apache/druid/query/filter/TypedInFilter.java: ########## @@ -0,0 +1,664 @@ +/* + * 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.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Range; +import com.google.common.collect.RangeSet; +import com.google.common.collect.Sets; +import com.google.common.collect.TreeRangeSet; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; +import it.unimi.dsi.fastutil.doubles.DoubleOpenHashSet; +import it.unimi.dsi.fastutil.floats.FloatOpenHashSet; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; +import it.unimi.dsi.fastutil.objects.ObjectAVLTreeSet; +import org.apache.druid.common.config.NullHandling; +import org.apache.druid.error.InvalidInput; +import org.apache.druid.java.util.common.ByteBufferUtils; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.math.expr.Evals; +import org.apache.druid.query.cache.CacheKeyBuilder; +import org.apache.druid.query.filter.vector.VectorValueMatcher; +import org.apache.druid.query.filter.vector.VectorValueMatcherColumnProcessorFactory; +import org.apache.druid.segment.ColumnInspector; +import org.apache.druid.segment.ColumnProcessors; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.apache.druid.segment.DimensionHandlerUtils; +import org.apache.druid.segment.column.ColumnIndexSupplier; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.NullableTypeStrategy; +import org.apache.druid.segment.column.ValueType; +import org.apache.druid.segment.filter.Filters; +import org.apache.druid.segment.index.BitmapColumnIndex; +import org.apache.druid.segment.index.semantic.Utf8ValueSetIndexes; +import org.apache.druid.segment.index.semantic.ValueSetIndexes; +import org.apache.druid.segment.vector.VectorColumnSelectorFactory; + +import javax.annotation.Nullable; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.SortedSet; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class TypedInFilter extends AbstractOptimizableDimFilter implements Filter +{ + private final String column; + private final ColumnType matchValueType; + @Nullable + private final List<?> unsortedValues; + private final Supplier<List<?>> lazyMatchValues; + @Nullable + private final Supplier<SortedSet<ByteBuffer>> lazyMatchValueBytes; + @Nullable + private final FilterTuning filterTuning; + private final Supplier<DruidPredicateFactory> predicateFactorySupplier; + + @JsonIgnore + private final Supplier<byte[]> cacheKeySupplier; + + /** + * Creates a new filter. + * + * @param column column to search + * @param values set of values to match. This collection may be reused to avoid copying a big collection. + * Therefore, callers should <b>not</b> modify the collection after it is passed to this + * constructor. + * @param matchValueType type of values contained in set + * @param filterTuning optional tuning + */ + @JsonCreator + public TypedInFilter( + @JsonProperty("column") String column, + @JsonProperty("matchValueType") ColumnType matchValueType, + @JsonProperty("values") @Nullable List<?> values, + @JsonProperty("sortedValues") @Nullable List<?> sortedValues, + @JsonProperty("filterTuning") @Nullable FilterTuning filterTuning + ) + { + if (NullHandling.replaceWithDefault()) { + throw InvalidInput.exception( + "Invalid IN filter, typed in filter only supports SQL compatible null handling mode, set druid.generic.useDefaultValue=false to use this filter" + ); + } + this.column = column; + if (column == null) { + throw InvalidInput.exception("Invalid IN filter, column cannot be null"); + } + this.filterTuning = filterTuning; + this.matchValueType = matchValueType; + if (matchValueType == null) { + throw InvalidInput.exception("Invalid IN filter on column [%s], matchValueType cannot be null", column); + } + // one of sorted or not sorted + if (values == null && sortedValues == null) { + throw InvalidInput.exception( + "Invalid IN filter on column [%s], exactly one of values or sortedValues must be non-null", + column + ); + } + if (sortedValues != null) { + this.unsortedValues = null; + this.lazyMatchValues = () -> sortedValues; + } else { + this.unsortedValues = values; + this.lazyMatchValues = Suppliers.memoize(() -> sortValues(unsortedValues, matchValueType)); + } + if (matchValueType.is(ValueType.STRING)) { + this.lazyMatchValueBytes = Suppliers.memoize(() -> { + final SortedSet<ByteBuffer> matchValueBytes = new ObjectAVLTreeSet<>(ByteBufferUtils.utf8Comparator()); + for (Object s : lazyMatchValues.get()) { + matchValueBytes.add(StringUtils.toUtf8ByteBuffer(Evals.asString(s))); + } + return matchValueBytes; + }); + } else { + this.lazyMatchValueBytes = null; + } + + this.predicateFactorySupplier = Suppliers.memoize( + () -> new InFilterDruidPredicateFactory(lazyMatchValues.get(), matchValueType) + ); + this.cacheKeySupplier = Suppliers.memoize(this::computeCacheKey); + } + + @JsonProperty + public String getColumn() + { + return column; + } + + @JsonProperty + public List<?> getSortedValues() + { + return lazyMatchValues.get(); + } + + @JsonProperty + public ColumnType getMatchValueType() + { + return matchValueType; + } + + @Nullable + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonProperty + public FilterTuning getFilterTuning() + { + return filterTuning; + } + + @Override + public byte[] getCacheKey() + { + return cacheKeySupplier.get(); + } + + @Override + public DimFilter optimize(final boolean mayIncludeUnknown) + { + final List<?> matchValues = lazyMatchValues.get(); + if (matchValues.isEmpty()) { + return FalseDimFilter.instance(); + } else if (matchValues.size() == 1) { + if (matchValues.get(0) == null) { + return NullFilter.forColumn(column); + } + return new EqualityFilter( + column, + matchValueType, + matchValues.iterator().next(), + filterTuning + ); + } + return this; + } + + @Override + public Filter toFilter() + { + return this; + } + + @Nullable + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + if (!Objects.equals(getColumn(), dimension)) { + return null; + } + RangeSet<String> retSet = TreeRangeSet.create(); + for (Object value : lazyMatchValues.get()) { + String valueEquivalent = NullHandling.nullToEmptyIfNeeded(Evals.asString(value)); Review Comment: stale code from when i was originally aspiring to support both modes, will remove -- 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]
