clintropolis commented on code in PR #15366: URL: https://github.com/apache/druid/pull/15366#discussion_r1408379246
########## processing/src/main/java/org/apache/druid/query/filter/ArrayContainsElementFilter.java: ########## @@ -0,0 +1,538 @@ +/* + * 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.Predicate; +import com.google.common.base.Predicates; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.RangeSet; +import org.apache.druid.error.InvalidInput; +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.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.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.TypeSignature; +import org.apache.druid.segment.column.TypeStrategy; +import org.apache.druid.segment.column.ValueType; +import org.apache.druid.segment.filter.Filters; +import org.apache.druid.segment.index.AllUnknownBitmapColumnIndex; +import org.apache.druid.segment.index.BitmapColumnIndex; +import org.apache.druid.segment.index.semantic.ArrayElementIndexes; +import org.apache.druid.segment.nested.StructuredData; +import org.apache.druid.segment.vector.VectorColumnSelectorFactory; + +import javax.annotation.Nullable; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Check to see if an array contains a specific element. This filter is not an exact replica of SQL ARRAY_CONTAINS + * or the native array_contains expression, which when given something like ARRAY_CONTAINS(arrayColumn, ARRAY[1,2,3]) + * will check that arrayColumn contains all elements of the match value. To model this functionality, use an + * {@link AndDimFilter} with an element filter for each element to match. + */ +public class ArrayContainsElementFilter extends AbstractOptimizableDimFilter implements Filter +{ + private final String column; + private final ColumnType elementMatchValueType; + @Nullable + private final Object elementMatchValue; + private final ExprEval<?> elementMatchValueEval; + + @Nullable + private final FilterTuning filterTuning; + private final DruidPredicateFactory predicateFactory; + + @JsonCreator + public ArrayContainsElementFilter( + @JsonProperty("column") String column, + @JsonProperty("elementMatchValueType") ColumnType elementMatchValueType, + @JsonProperty("elementMatchValue") @Nullable Object elementMatchValue, + @JsonProperty("filterTuning") @Nullable FilterTuning filterTuning + ) + { + if (column == null) { + throw InvalidInput.exception("Invalid array_contains filter, column cannot be null"); + } + this.column = column; + if (elementMatchValueType == null) { + throw InvalidInput.exception("Invalid array_contains filter on column [%s], elementMatchValueType cannot be null", column); + } + this.elementMatchValueType = elementMatchValueType; + this.elementMatchValue = elementMatchValue; + this.elementMatchValueEval = ExprEval.ofType(ExpressionType.fromColumnTypeStrict(elementMatchValueType), elementMatchValue); + this.filterTuning = filterTuning; + this.predicateFactory = new ArrayContainsPredicateFactory(elementMatchValueEval); + } + + @Override + public byte[] getCacheKey() + { + final TypeStrategy<Object> typeStrategy = elementMatchValueEval.type().getStrategy(); + final int size = typeStrategy.estimateSizeBytes(elementMatchValueEval.value()); + final ByteBuffer valueBuffer = ByteBuffer.allocate(size); + typeStrategy.write(valueBuffer, elementMatchValueEval.value(), size); Review Comment: oops, will change to use `NullableTypeStrategy`. I modeled this originally on the EqualityFilter because it shares a lot in common, but one difference is that the match value here can be null to check if an array contains null -- 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]
