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


##########
processing/src/main/java/org/apache/druid/segment/UnnestColumnValueSelectorCursor.java:
##########
@@ -0,0 +1,334 @@
+/*
+ * 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 org.apache.druid.java.util.common.UOE;
+import org.apache.druid.query.BaseQuery;
+import org.apache.druid.query.dimension.DimensionSpec;
+import org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.joda.time.DateTime;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.List;
+
+/**
+ * The cursor to help unnest MVDs without dictionary encoding and ARRAY type 
selectors.
+ *
+ * Consider a segment has 2 rows
+ * ['a', 'b', 'c']
+ * ['d', 'e']
+ *
+ * The baseCursor points to the row ['a', 'b', 'c']
+ * while the unnestCursor with each call of advance() moves over individual 
elements.
+ *
+ * unnestCursor.advance() -> 'a'
+ * unnestCursor.advance() -> 'b'
+ * unnestCursor.advance() -> 'c'
+ * unnestCursor.advance() -> 'd' (advances base cursor first)
+ * unnestCursor.advance() -> 'e'
+ *
+ *
+ * The allowSet if available helps skip over elements which are not in the 
allowList by moving the cursor to
+ * the next available match.
+ *
+ * The index reference points to the index of each row that the unnest cursor 
is accessing through currentVal
+ * The index ranges from 0 to the size of the list in each row which is held 
in the unnestListForCurrentRow
+ *
+ * The needInitialization flag sets up the initial values of 
unnestListForCurrentRow at the beginning of the segment
+ *
+ *
+ */
+public class UnnestColumnValueSelectorCursor implements Cursor
+{
+  private final Cursor baseCursor;
+  private final ColumnSelectorFactory baseColumSelectorFactory;
+  private final ColumnValueSelector columnValueSelector;
+  private final String columnName;
+  private final String outputName;
+  private final LinkedHashSet<String> allowSet;
+  private int index;
+  private Object currentVal;
+  private List<Object> unnestListForCurrentRow;
+  private boolean needInitialization;
+
+  public UnnestColumnValueSelectorCursor(
+      Cursor cursor,
+      ColumnSelectorFactory baseColumSelectorFactory,
+      String columnName,
+      String outputColumnName,
+      LinkedHashSet<String> allowSet
+  )
+  {
+    this.baseCursor = cursor;
+    this.baseColumSelectorFactory = baseColumSelectorFactory;
+    this.columnValueSelector = 
this.baseColumSelectorFactory.makeColumnValueSelector(columnName);
+    this.columnName = columnName;
+    this.index = 0;
+    this.outputName = outputColumnName;
+    this.needInitialization = true;
+    this.allowSet = allowSet;
+  }
+
+  @Override
+  public ColumnSelectorFactory getColumnSelectorFactory()
+  {
+    return new ColumnSelectorFactory()
+    {
+      @Override
+      public DimensionSelector makeDimensionSelector(DimensionSpec 
dimensionSpec)
+      {
+        if (!outputName.equals(dimensionSpec.getDimension())) {
+          return baseColumSelectorFactory.makeDimensionSelector(dimensionSpec);
+        }
+        throw new UOE("Unsupported dimension selector while using column value 
selector for column [%s]", outputName);
+      }
+
+      @Override
+      public ColumnValueSelector makeColumnValueSelector(String columnName)
+      {
+        if (!outputName.equals(columnName)) {
+          return baseColumSelectorFactory.makeColumnValueSelector(columnName);
+        }
+        return new ColumnValueSelector()
+        {
+          @Override
+          public double getDouble()
+          {
+            Object value = getObject();
+            if (value == null) {
+              return 0;
+            }
+            if (value instanceof Number) {
+              return ((Number) value).doubleValue();
+            }
+            throw new UOE("Cannot convert object to double");
+          }
+
+          @Override
+          public float getFloat()
+          {
+            Object value = getObject();
+            if (value == null) {
+              return 0;
+            }
+            if (value instanceof Number) {
+              return ((Number) value).floatValue();
+            }
+            throw new UOE("Cannot convert object to float");
+          }
+
+          @Override
+          public long getLong()
+          {
+            Object value = getObject();
+            if (value == null) {
+              return 0;
+            }
+            if (value instanceof Number) {
+              return ((Number) value).longValue();
+            }
+            throw new UOE("Cannot convert object to long");
+          }
+
+          @Override
+          public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+          {
+            columnValueSelector.inspectRuntimeShape(inspector);
+          }
+
+          @Override
+          public boolean isNull()
+          {
+            return getObject() == null;
+          }
+
+          @Nullable
+          @Override
+          public Object getObject()
+          {
+            if (!unnestListForCurrentRow.isEmpty()) {
+              if (allowSet == null || allowSet.isEmpty()) {
+                return unnestListForCurrentRow.get(index);
+              } else if (allowSet.contains((String) 
unnestListForCurrentRow.get(index))) {
+                return unnestListForCurrentRow.get(index);
+              }
+            }
+            return null;
+          }
+
+          @Override
+          public Class<?> classOfObject()
+          {
+            return Object.class;
+          }
+        };
+      }
+
+      @Nullable
+      @Override
+      public ColumnCapabilities getColumnCapabilities(String column)
+      {
+        if (!outputName.equals(column)) {
+          return baseColumSelectorFactory.getColumnCapabilities(column);
+        }
+        // This currently returns the same type as of the column to be unnested
+        // This is fine for STRING types

Review Comment:
   Thanks I'll do this in the followup PR. Will work on the runner tests too.



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