clintropolis commented on a change in pull request #11650:
URL: https://github.com/apache/druid/pull/11650#discussion_r708034339



##########
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:
       Yes, good catch :+1:
   
   There was another bug hiding this, which is that 
`VirtualColumns.getBitmapIndex` still calls 
`VirtualColumn.capabilities(columnName)` instead of 
`VirtualColumn.capabilities(inspector, columnName)`, and the former was not 
setting the flag on the artificial `ColumnCapabilities` it produces indicating 
that the column had bitmap indexes. So, it was always using a matcher instead 
of the bitmap index, which produced the correct behavior. We should probably at 
some point in the future figure out how to update 
`VirtualColumns.getBitmapIndex` to also have a `ColumnInspector` handy so it 
can use the better `capabilities` method, but I'll save that for another day.
   
   I've added a specialized `BitmapIndex` implementation to use the forwarded 
ids to take advantage of the smaller cardinality, and tests (the SQL no matches 
test would fail by picking up matches without these implementations (after the 
`capabilities` bug was fixed), and the like filter test. This involved a lot 
more refactoring than I planned...




-- 
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]

Reply via email to