clintropolis commented on code in PR #15366: URL: https://github.com/apache/druid/pull/15366#discussion_r1408371598
########## 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); + return new CacheKeyBuilder(DimFilterUtils.ARRAY_CONTAINS_CACHE_ID) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(column) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(elementMatchValueType.asTypeString()) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByteArray(valueBuffer.array()) + .build(); + } + + @Override + public DimFilter optimize() + { + return this; + } + + @Override + public Filter toFilter() + { + return this; + } + + @JsonProperty + public String getColumn() + { + return column; + } + + @JsonProperty + public ColumnType getElementMatchValueType() + { + return elementMatchValueType; + } + + @JsonProperty + public Object getElementMatchValue() + { + return elementMatchValue; + } + + @Nullable + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_NULL) + public FilterTuning getFilterTuning() + { + return filterTuning; + } + + @Override + public String toString() + { + DimFilter.DimFilterToStringBuilder bob = + new DimFilter.DimFilterToStringBuilder().append("array_contains_element(") + .appendDimension(column, null) + .append(", ") + .append( + elementMatchValueType.isArray() + ? Arrays.deepToString(elementMatchValueEval.asArray()) + : elementMatchValueEval.value() + ) + .append(")"); + if (!ColumnType.STRING.equals(elementMatchValueType)) { + bob.append(" (" + elementMatchValueType.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; + } + ArrayContainsElementFilter that = (ArrayContainsElementFilter) o; + if (!column.equals(that.column)) { + return false; + } + if (!Objects.equals(elementMatchValueType, that.elementMatchValueType)) { + return false; + } + if (!Objects.equals(filterTuning, that.filterTuning)) { + return false; + } + if (elementMatchValueType.isArray()) { + return Arrays.deepEquals(elementMatchValueEval.asArray(), that.elementMatchValueEval.asArray()); + } else { + return Objects.equals(elementMatchValueEval.value(), that.elementMatchValueEval.value()); + } + } + + @Override + public int hashCode() + { + return Objects.hash(column, elementMatchValueType, elementMatchValueEval.value(), filterTuning); + } + + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + return null; + } + + @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 new AllUnknownBitmapColumnIndex(selector); + } + final ArrayElementIndexes elementIndexes = indexSupplier.as(ArrayElementIndexes.class); + if (elementIndexes != null) { + return elementIndexes.containsValue(elementMatchValueEval.value(), elementMatchValueType); + } + + if (elementMatchValueEval.valueOrDefault() != null && selector.getColumnCapabilities(column) != null && !selector.getColumnCapabilities(column).isArray()) { + // column is not an array, behave like a normal equality filter + return EqualityFilter.getEqualityIndex(column, elementMatchValueEval, elementMatchValueType, selector); Review Comment: this is covered by `testScalarColumnContains` https://github.com/apache/druid/pull/15366/files#diff-7e731568ff302f962b2d5458d9bc63cfd95cb6856d2c4c210735c067741e9f9aR392 -- 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]
