clintropolis commented on code in PR #14542: URL: https://github.com/apache/druid/pull/14542#discussion_r1264297460
########## processing/src/main/java/org/apache/druid/query/filter/NullFilter.java: ########## @@ -0,0 +1,315 @@ +/* + * 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.collect.ImmutableSet; +import com.google.common.collect.Range; +import com.google.common.collect.RangeSet; +import com.google.common.collect.TreeRangeSet; +import org.apache.druid.error.InvalidInput; +import org.apache.druid.java.util.common.IAE; +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.ColumnInspector; +import org.apache.druid.segment.ColumnProcessors; +import org.apache.druid.segment.ColumnSelector; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.apache.druid.segment.column.ColumnIndexSupplier; +import org.apache.druid.segment.column.TypeSignature; +import org.apache.druid.segment.column.ValueType; +import org.apache.druid.segment.filter.DimensionPredicateFilter; +import org.apache.druid.segment.filter.Filters; +import org.apache.druid.segment.index.BitmapColumnIndex; +import org.apache.druid.segment.index.semantic.NullValueIndex; +import org.apache.druid.segment.vector.VectorColumnSelectorFactory; + +import javax.annotation.Nullable; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +public class NullFilter extends AbstractOptimizableDimFilter implements Filter +{ + public static NullFilter forColumn(String column) + { + return new NullFilter(column, null, null); + } + + private final String column; + @Nullable + private final ExtractionFn extractionFn; + @Nullable + private final FilterTuning filterTuning; + + @JsonCreator + public NullFilter( + @JsonProperty("column") String column, + @JsonProperty("extractionFn") @Nullable ExtractionFn extractionFn, + @JsonProperty("filterTuning") @Nullable FilterTuning filterTuning + ) + { + if (column == null) { + throw InvalidInput.exception("Invalid null filter, column cannot be null"); + } + this.column = column; + // remove once SQL planner no longer uses extractionFn + this.extractionFn = extractionFn; + this.filterTuning = filterTuning; + } + + @JsonProperty + public String getColumn() + { + return column; + } + + @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 byte[] getCacheKey() + { + return new CacheKeyBuilder(DimFilterUtils.NULL_CACHE_ID) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(column) + .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, NullPredicateFactory.INSTANCE, extractionFn, filterTuning); + } + } + + @Nullable + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + RangeSet<String> retSet = TreeRangeSet.create(); + // Nulls are less than empty String in segments + retSet.add(Range.lessThan("")); + 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(true, selector); + } + final NullValueIndex nullValueIndex = indexSupplier.as(NullValueIndex.class); + if (nullValueIndex == null) { + return null; + } + return nullValueIndex.forNull(); Review Comment: renamed to `get` ########## processing/src/main/java/org/apache/druid/query/filter/RangeFilter.java: ########## @@ -0,0 +1,1025 @@ +/* + * 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.BoundType; +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.common.config.NullHandling; +import org.apache.druid.error.InvalidInput; +import org.apache.druid.java.util.common.IAE; +import org.apache.druid.java.util.common.StringUtils; +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.query.ordering.StringComparators; +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.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.DimensionPredicateFilter; +import org.apache.druid.segment.filter.Filters; +import org.apache.druid.segment.index.BitmapColumnIndex; +import org.apache.druid.segment.index.semantic.LexicographicalRangeIndex; +import org.apache.druid.segment.index.semantic.NumericRangeIndex; +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; + +public class RangeFilter extends AbstractOptimizableDimFilter implements Filter +{ + private final String column; + private final ColumnType matchValueType; + + @Nullable + private final Object upper; + + @Nullable + private final Object lower; + + private final ExprEval<?> upperEval; + private final ExprEval<?> lowerEval; + private final boolean lowerStrict; + private final boolean upperStrict; + @Nullable + private final ExtractionFn extractionFn; + @Nullable + private final FilterTuning filterTuning; + private final Supplier<Predicate<String>> stringPredicateSupplier; + private final Supplier<DruidLongPredicate> longPredicateSupplier; + private final Supplier<DruidFloatPredicate> floatPredicateSupplier; + private final Supplier<DruidDoublePredicate> doublePredicateSupplier; + + @JsonCreator + public RangeFilter( + @JsonProperty("column") String column, + @JsonProperty("matchValueType") ColumnType matchValueType, + @JsonProperty("lower") @Nullable Object lower, + @JsonProperty("upper") @Nullable Object upper, + @JsonProperty("lowerStrict") @Nullable Boolean lowerStrict, + @JsonProperty("upperStrict") @Nullable Boolean upperStrict, + @JsonProperty("extractionFn") @Nullable ExtractionFn extractionFn, + @JsonProperty("filterTuning") @Nullable FilterTuning filterTuning + ) + { + if (column == null) { + throw InvalidInput.exception("Invalid range filter, column cannot be null"); + } + this.column = column; + if (matchValueType == null) { + throw InvalidInput.exception("Invalid range filter on column [%s], matchValueType cannot be null", column); + } + this.matchValueType = matchValueType; + if (lower == null && upper == null) { + throw InvalidInput.exception( + "Invalid range filter on column [%s], lower and upper cannot be null at the same time", + column + ); + } + final ExpressionType expressionType = ExpressionType.fromColumnType(matchValueType); + this.upper = upper; + this.lower = lower; + this.upperEval = ExprEval.ofType(expressionType, upper); + this.lowerEval = ExprEval.ofType(expressionType, lower); + if (expressionType.isNumeric()) { + if (lower != null && lowerEval.value() == null) { + throw InvalidInput.exception( + "Invalid range filter on column [%s], lower bound [%s] cannot be parsed as specified match value type [%s]", + column, + lower, + expressionType + ); + } + if (upper != null && upperEval.value() == null) { + throw InvalidInput.exception( + "Invalid range filter on column [%s], upper bound [%s] cannot be parsed as specified match value type [%s]", + column, + upper, + expressionType + ); + } + } + this.lowerStrict = lowerStrict != null && lowerStrict; + this.upperStrict = upperStrict != null && upperStrict; + // remove once SQL planner no longer uses extractionFn + this.extractionFn = extractionFn; + this.filterTuning = filterTuning; + this.stringPredicateSupplier = makeStringPredicateSupplier(); + this.longPredicateSupplier = makeLongPredicateSupplier(); + this.floatPredicateSupplier = makeFloatPredicateSupplier(); + this.doublePredicateSupplier = makeDoublePredicateSupplier(); + } + + @JsonProperty + public String getColumn() + { + return column; + } + + @JsonProperty + public ColumnType getMatchValueType() + { + return matchValueType; + } + + @Nullable + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_NULL) + public Object getUpper() + { + return upper; + } + + @Nullable + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_NULL) + public Object getLower() + { + return lower; + } + + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_DEFAULT) + public boolean isLowerStrict() + { + return lowerStrict; + } + + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_DEFAULT) + public boolean isUpperStrict() + { + return upperStrict; + } + + public boolean hasLowerBound() + { + return lower != null; + } + + public boolean hasUpperBound() + { + return upper != null; + } + + @Nullable + @JsonProperty + @JsonInclude(JsonInclude.Include.NON_NULL) + public ExtractionFn getExtractionFn() + { + return extractionFn; + } + + @Nullable + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonProperty + public FilterTuning getFilterTuning() + { + return filterTuning; + } + + @Override + public byte[] getCacheKey() + { + final byte[] lowerBytes; + final byte[] upperBytes; + if (hasLowerBound()) { + final TypeStrategy<Object> typeStrategy = matchValueType.getStrategy(); + final int size = typeStrategy.estimateSizeBytes(lower); + final ByteBuffer valueBuffer = ByteBuffer.allocate(size); + typeStrategy.write(valueBuffer, lower, size); + lowerBytes = valueBuffer.array(); + } else { + lowerBytes = new byte[0]; + } + if (hasUpperBound()) { + final TypeStrategy<Object> typeStrategy = matchValueType.getStrategy(); + final int size = typeStrategy.estimateSizeBytes(upper); + final ByteBuffer valueBuffer = ByteBuffer.allocate(size); + typeStrategy.write(valueBuffer, upper, size); + upperBytes = valueBuffer.array(); + } else { + upperBytes = new byte[0]; + } + byte boundType = 0x1; + if (this.getLower() == null) { + boundType = 0x2; + } else if (this.getUpper() == null) { + boundType = 0x3; + } + + final byte lowerStrictByte = this.isLowerStrict() ? (byte) 1 : 0x0; + final byte upperStrictByte = this.isUpperStrict() ? (byte) 1 : 0x0; + + return new CacheKeyBuilder(DimFilterUtils.RANGE_CACHE_ID) + .appendByte(boundType) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(column) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendString(matchValueType.asTypeString()) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByteArray(upperBytes) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByteArray(lowerBytes) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByte(lowerStrictByte) + .appendByte(DimFilterUtils.STRING_SEPARATOR) + .appendByte(upperStrictByte) + .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 new DimensionPredicateFilter(column, getPredicateFactory(), extractionFn, filterTuning); + } + return this; + } + + @Override + public RangeSet<String> getDimensionRangeSet(String dimension) + { + // range partitioning converts stuff to strings.. so do that i guess Review Comment: changed comment -- 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]
