somu-imply commented on code in PR #13268:
URL: https://github.com/apache/druid/pull/13268#discussion_r1036237797


##########
processing/src/main/java/org/apache/druid/segment/DimensionUnnestCursor.java:
##########
@@ -0,0 +1,394 @@
+/*
+ * 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.Predicate;
+import org.apache.druid.query.BaseQuery;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+import org.apache.druid.query.dimension.DimensionSpec;
+import org.apache.druid.query.filter.ValueMatcher;
+import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.apache.druid.segment.data.IndexedInts;
+import org.joda.time.DateTime;
+
+import javax.annotation.Nullable;
+import java.util.BitSet;
+import java.util.LinkedHashSet;
+
+/**
+ * The cursor to help unnest MVDs with dictionary encoding.
+ * Consider a segment has 2 rows
+ * ['a', 'b', 'c']
+ * ['d', 'c']
+ *
+ * Considering dictionary encoding, these are represented as
+ *
+ * 'a' -> 0
+ * 'b' -> 1
+ * 'c' -> 2
+ * 'd' -> 3
+ *
+ * The baseCursor points to the row of IndexedInts [0, 1, 2]
+ * while the unnestCursor with each call of advance() moves over individual 
elements.
+ *
+ * advance() -> 0 -> 'a'
+ * advance() -> 1 -> 'b'
+ * advance() -> 2 -> 'c'
+ * advance() -> 3 -> 'd' (advances base cursor first)
+ * advance() -> 2 -> 'c'
+ *
+ * Total 5 advance calls above
+ *
+ * The allowSet if available helps skip over elements which are not in the 
allowList by moving the cursor to
+ * the next available match. The hashSet is converted into a bitset (during 
initialization) for efficiency.
+ * If allowSet is ['c', 'd'] then the advance moves over to the next available 
match
+ *
+ * advance() -> 2 -> 'c'
+ * advance() -> 3 -> 'd' (advances base cursor first)
+ * advance() -> 2 -> 'c'
+ *
+ * Total 3 advance calls in this case
+ *
+ * The index reference points to the index of each row that the unnest cursor 
is accessing
+ * The indexedInts for each row are held in the indexedIntsForCurrentRow object
+ *
+ * The needInitialization flag sets up the initial values of 
indexedIntsForCurrentRow at the beginning of the segment
+ *
+ */
+public class DimensionUnnestCursor implements Cursor
+{
+  private final Cursor baseCursor;
+  private final DimensionSelector dimSelector;
+  private final String columnName;
+  private final String outputName;
+  private final LinkedHashSet<String> allowSet;
+  private final BitSet allowedBitSet;
+  private final ColumnSelectorFactory baseColumnSelectorFactory;
+  private int index;
+  private IndexedInts indexedIntsForCurrentRow;
+  private boolean needInitialization;
+  private SingleIndexInts indexIntsForRow;
+
+  public DimensionUnnestCursor(
+      Cursor cursor,
+      ColumnSelectorFactory baseColumnSelectorFactory,
+      String columnName,
+      String outputColumnName,
+      LinkedHashSet<String> allowSet
+  )
+  {
+    this.baseCursor = cursor;
+    this.baseColumnSelectorFactory = baseColumnSelectorFactory;
+    this.dimSelector = 
this.baseColumnSelectorFactory.makeDimensionSelector(DefaultDimensionSpec.of(columnName));
+    this.columnName = columnName;
+    this.index = 0;
+    this.outputName = outputColumnName;
+    this.needInitialization = true;
+    this.allowSet = allowSet;
+    this.allowedBitSet = new BitSet();
+  }
+
+  @Override
+  public ColumnSelectorFactory getColumnSelectorFactory()
+  {
+    return new ColumnSelectorFactory()
+    {
+      @Override
+      public DimensionSelector makeDimensionSelector(DimensionSpec 
dimensionSpec)
+      {
+        if (!outputName.equals(dimensionSpec.getDimension())) {
+          return 
baseColumnSelectorFactory.makeDimensionSelector(dimensionSpec);
+        }
+
+        return new DimensionSelector()
+        {
+          @Override
+          public IndexedInts getRow()
+          {
+            // This object reference has been created
+            // during the call to initialize and referenced henceforth
+            return indexIntsForRow;
+          }
+
+          @Override
+          public ValueMatcher makeValueMatcher(@Nullable String value)
+          {
+            final int idForLookup = idLookup().lookupId(value);
+            if (idForLookup < 0) {
+              return new ValueMatcher()
+              {
+                @Override
+                public boolean matches()
+                {
+                  return false;
+                }
+
+                @Override
+                public void inspectRuntimeShape(RuntimeShapeInspector 
inspector)
+                {
+
+                }
+              };
+            }
+
+            return new ValueMatcher()
+            {
+              @Override
+              public boolean matches()
+              {
+                return idForLookup == indexedIntsForCurrentRow.get(index);
+              }
+
+              @Override
+              public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+              {
+                dimSelector.inspectRuntimeShape(inspector);
+              }
+            };
+          }
+
+          @Override
+          public ValueMatcher makeValueMatcher(Predicate<String> predicate)
+          {
+            return DimensionSelectorUtils.makeValueMatcherGeneric(this, 
predicate);
+          }
+
+          @Override
+          public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+          {
+            dimSelector.inspectRuntimeShape(inspector);
+          }
+
+          @Nullable
+          @Override
+          public Object getObject()
+          {
+            if (allowedBitSet.isEmpty()) {
+              if (allowSet == null || allowSet.isEmpty()) {
+                return lookupName(indexedIntsForCurrentRow.get(index));
+              }
+            } else if (allowedBitSet.get(indexedIntsForCurrentRow.get(index))) 
{
+              return lookupName(indexedIntsForCurrentRow.get(index));
+            }
+            return null;
+          }
+
+          @Override
+          public Class<?> classOfObject()
+          {
+            return Object.class;
+          }
+
+          @Override
+          public int getValueCardinality()
+          {
+            if (!allowedBitSet.isEmpty()) {
+              return allowedBitSet.cardinality();
+            }
+            return dimSelector.getValueCardinality();
+          }
+
+          @Nullable
+          @Override
+          public String lookupName(int id)
+          {
+            return dimSelector.lookupName(id);
+          }
+
+          @Override
+          public boolean nameLookupPossibleInAdvance()
+          {
+            return dimSelector.nameLookupPossibleInAdvance();
+          }
+
+          @Nullable
+          @Override
+          public IdLookup idLookup()
+          {
+            return dimSelector.idLookup();
+          }
+        };
+      }
+
+      /*
+      This ideally should not be called. If called delegate using the 
makeDimensionSelector
+       */
+      @Override
+      public ColumnValueSelector makeColumnValueSelector(String columnName)
+      {
+        if (!outputName.equals(columnName)) {
+          return baseColumnSelectorFactory.makeColumnValueSelector(columnName);
+        }
+        return makeDimensionSelector(DefaultDimensionSpec.of(columnName));
+      }
+
+      @Nullable
+      @Override
+      public ColumnCapabilities getColumnCapabilities(String column)
+      {
+        if (!outputName.equals(columnName)) {
+          baseColumnSelectorFactory.getColumnCapabilities(column);
+        }
+        return baseColumnSelectorFactory.getColumnCapabilities(columnName);
+      }
+    };
+  }
+
+  @Override
+  public DateTime getTime()
+  {
+    return baseCursor.getTime();
+  }
+
+  @Override
+  public void advance()
+  {
+    advanceUninterruptibly();
+    BaseQuery.checkInterrupted();
+  }
+
+  @Override
+  public void advanceUninterruptibly()
+  {
+    do {
+      advanceAndUpdate();

Review Comment:
   If the base cursor does not have any data it does not come until this stage 
of unnest cursor creation as the base cursor is already in a `isDone==true` 
state. Additionally `UnnestStorageAdapter` before the cursor creation ensures 
that the base is non-null



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