gianm commented on a change in pull request #11650: URL: https://github.com/apache/druid/pull/11650#discussion_r706235110
########## File path: processing/src/main/java/org/apache/druid/segment/virtual/ListFilteredVirtualColumn.java ########## @@ -0,0 +1,193 @@ +/* + * 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.virtual; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; +import org.apache.druid.query.cache.CacheKeyBuilder; +import org.apache.druid.query.dimension.DefaultDimensionSpec; +import org.apache.druid.query.dimension.DimensionSpec; +import org.apache.druid.query.dimension.ListFilteredDimensionSpec; +import org.apache.druid.segment.ColumnInspector; +import org.apache.druid.segment.ColumnSelector; +import org.apache.druid.segment.ColumnSelectorFactory; +import org.apache.druid.segment.ColumnValueSelector; +import org.apache.druid.segment.DimensionSelector; +import org.apache.druid.segment.VirtualColumn; +import org.apache.druid.segment.column.BitmapIndex; +import org.apache.druid.segment.column.ColumnCapabilities; +import org.apache.druid.segment.column.ColumnCapabilitiesImpl; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +/** + * {@link VirtualColumn} form of {@link ListFilteredDimensionSpec}, powered by + * {@link org.apache.druid.query.dimension.ForwardingFilteredDimensionSelector} + */ +public class ListFilteredVirtualColumn implements VirtualColumn +{ + private final String name; + private final DimensionSpec delegate; + private final Set<String> values; + private final boolean allowList; + + @JsonCreator + public ListFilteredVirtualColumn( + @JsonProperty("name") String name, + @JsonProperty("delegate") DimensionSpec delegate, + @JsonProperty("values") Set<String> values, + @JsonProperty("isAllowList") @Nullable Boolean isAllowList + ) + { + this.name = Preconditions.checkNotNull(name, "name"); + this.delegate = delegate; + this.values = values; + this.allowList = isAllowList == null ? true : isAllowList.booleanValue(); + } + + + @JsonProperty("name") + @Override + public String getOutputName() + { + return name; + } + + @JsonProperty + public Set<String> getValues() + { + return values; + } + + @JsonProperty("isAllowList") + public boolean isAllowList() + { + return allowList; + } + + @JsonProperty + public DimensionSpec getDelegate() + { + return delegate; + } + + @Override + public byte[] getCacheKey() + { + CacheKeyBuilder builder = new CacheKeyBuilder(VirtualColumnCacheHelper.CACHE_TYPE_ID_LIST_FILTERED) + .appendString(name) + .appendCacheable(delegate) + .appendStringsIgnoringOrder(values) + .appendBoolean(allowList); + return builder.build(); + } + + + @Override + public DimensionSelector makeDimensionSelector( + DimensionSpec dimensionSpec, + ColumnSelectorFactory factory + ) + { + if (allowList) { + return ListFilteredDimensionSpec.filterAllowList(values, factory.makeDimensionSelector(delegate)); + } else { + return ListFilteredDimensionSpec.filterDenyList(values, factory.makeDimensionSelector(delegate)); + } + } + + @Override + public ColumnValueSelector<?> makeColumnValueSelector( + String columnName, + ColumnSelectorFactory factory + ) + { + return makeDimensionSelector(DefaultDimensionSpec.of(columnName), factory); + } + + @Override + public ColumnCapabilities capabilities(String columnName) + { + return new ColumnCapabilitiesImpl().setType(delegate.getOutputType()); + } + + @Override + public ColumnCapabilities capabilities(ColumnInspector inspector, String columnName) + { + return inspector.getColumnCapabilities(delegate.getDimension()); + } + + @Override + public List<String> requiredColumns() + { + return Collections.singletonList(delegate.getDimension()); + } + + @Override + public boolean usesDotNotation() + { + return false; + } + + @Override + public BitmapIndex getBitmapIndex( + String columnName, + ColumnSelector selector + ) + { + return selector.getColumnHolder(delegate.getDimension()).getBitmapIndex(); Review comment: Doesn't this need to be decorated in some way, to avoid including the values we wanted to be filtered out? Like, if someone filters on this virtual column and names a value that isn't included, I think we wouldn't want that filter to match. In SQL `WHERE MV_CONTAINS(MV_FILTER_ONLY(col, ['x']), 'y')` should (a) use this virtual column; (b) use the index; but also (c) not match anything, even if `col` contains `y` sometimes. -- 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]
