jihoonson commented on a change in pull request #6396: Implement 
MapVirtualColumn.makeDimensionSelector properly
URL: https://github.com/apache/incubator-druid/pull/6396#discussion_r221338768
 
 

 ##########
 File path: 
extensions-contrib/virtual-columns/src/main/java/org/apache/druid/segment/StringTypeMapVirtualColumnDimensionSelector.java
 ##########
 @@ -0,0 +1,210 @@
+/*
+ * 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;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import org.apache.druid.query.filter.ValueMatcher;
+import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
+import org.apache.druid.segment.data.IndexedInts;
+import org.apache.druid.segment.data.SingleIndexedInt;
+import org.apache.druid.segment.data.ZeroIndexedInts;
+
+import javax.annotation.Nullable;
+import java.util.Objects;
+import java.util.stream.IntStream;
+
+/**
+ * {@link DimensionSelector} for String type {@link MapVirtualColumn}. The 
performance has not considered yet and so
+ * it may need to be improved later.
+ */
+final class StringTypeMapVirtualColumnDimensionSelector extends 
MapVirtualColumnDimensionSelector
+{
+  private final String subColumnName;
+
+  StringTypeMapVirtualColumnDimensionSelector(
+      DimensionSelector keySelector,
+      DimensionSelector valueSelector,
+      String subColumnName
+  )
+  {
+    super(keySelector, valueSelector);
+    this.subColumnName = Preconditions.checkNotNull(subColumnName, 
"subColumnName");
+  }
+
+  @Override
+  public IndexedInts getRow()
+  {
+    final DimensionSelector keySelector = getKeySelector();
+    final DimensionSelector valueSelector = getValueSelector();
+
+    final IndexedInts keyIndices = keySelector.getRow();
+    final IndexedInts valueIndices = valueSelector.getRow();
+
+    final int limit = Math.min(keyIndices.size(), valueIndices.size());
+    final int valueIndex = IntStream
+        .range(0, limit)
+        .filter(i -> 
subColumnName.equals(keySelector.lookupName(keyIndices.get(i)))) // 
subColumnName is never null
+        .findAny()
+        .orElse(-1);
+    if (valueIndex < 0) {
+      return ZeroIndexedInts.instance();
+    } else {
+      return new SingleIndexedInt(valueIndex);
+    }
+  }
+
+  @Override
+  public ValueMatcher makeValueMatcher(@Nullable String value)
+  {
+    return new ValueMatcher()
+    {
+      @Override
+      public boolean matches()
+      {
+        return Objects.equals(value, getObject());
+      }
+
+      @Override
+      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+      {
+        inspector.visit("keySelector", getKeySelector());
+        inspector.visit("valueSelector", getValueSelector());
+        inspector.visit("subColumnName", subColumnName);
+      }
+    };
+  }
+
+  @Override
+  public ValueMatcher makeValueMatcher(Predicate<String> predicate)
+  {
+    return new ValueMatcher()
+    {
+      @Override
+      public boolean matches()
+      {
+        return predicate.apply((String) getObject());
+      }
+
+      @Override
+      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+      {
+        inspector.visit("keySelector", getKeySelector());
+        inspector.visit("valueSelector", getValueSelector());
+        inspector.visit("subColumnName", subColumnName);
+      }
+    };
+  }
+
+  @Override
+  public int getValueCardinality()
+  {
+    // To get the value cardinarlity, we need to first check all keys and 
values to find valid pairs, and then find the
+    // number of distinct values among them.
+    return CARDINALITY_UNKNOWN;
+  }
+
+  @Nullable
+  @Override
+  public String lookupName(int id)
+  {
+    final DimensionSelector keySelector = getKeySelector();
+    final DimensionSelector valueSelector = getValueSelector();
+
+    final IndexedInts keyIndices = keySelector.getRow();
+    final IndexedInts valueIndices = valueSelector.getRow();
+
+    final int limit = Math.min(keyIndices.size(), valueIndices.size());
+    final int valueIndex = IntStream
+        .range(0, limit)
+        .filter(i -> 
subColumnName.equals(keySelector.lookupName(keyIndices.get(i)))) // 
subColumnName is never null
+        .findAny()
+        .orElse(-1);
+
+    if (valueIndex == id) {
+      return valueSelector.lookupName(id);
+    } else {
+      return null;
+    }
+  }
+
+  @Override
+  public boolean nameLookupPossibleInAdvance()
+  {
+    return false;
+  }
+
+  @Nullable
+  @Override
+  public IdLookup idLookup()
+  {
+    final DimensionSelector keySelector = getKeySelector();
+    final DimensionSelector valueSelector = getValueSelector();
+
+    final IdLookup valueLookup = valueSelector.idLookup();
+
+    if (valueLookup != null) {
+      final IndexedInts keyIndices = keySelector.getRow();
+      final IndexedInts valueIndices = valueSelector.getRow();
+
+      final int limit = Math.min(keyIndices.size(), valueIndices.size());
+      final int valueIndex = IntStream
+          .range(0, limit)
+          .filter(i -> 
subColumnName.equals(keySelector.lookupName(keyIndices.get(i)))) // 
subColumnName is never null
+          .findAny()
+          .orElse(-1);
 
 Review comment:
   Done.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to