abhishekagarwal87 commented on code in PR #13268:
URL: https://github.com/apache/druid/pull/13268#discussion_r1034886719


##########
processing/src/main/java/org/apache/druid/segment/UnnestStorageAdapter.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.collect.Lists;
+import org.apache.druid.java.util.common.granularity.Granularity;
+import org.apache.druid.java.util.common.guava.Sequence;
+import org.apache.druid.java.util.common.guava.Sequences;
+import org.apache.druid.query.QueryMetrics;
+import org.apache.druid.query.filter.Filter;
+import org.apache.druid.query.filter.InDimFilter;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.apache.druid.segment.data.Indexed;
+import org.apache.druid.segment.data.ListIndexed;
+import org.apache.druid.segment.filter.AndFilter;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.Objects;
+
+public class UnnestStorageAdapter implements StorageAdapter
+{
+  private final StorageAdapter baseAdapter;
+  private final String dimensionToUnnest;
+  private final String outputColumnName;
+  private final LinkedHashSet<String> allowSet;
+
+  public UnnestStorageAdapter(
+      final StorageAdapter baseAdapter,
+      final String dimension,
+      final String outputColumnName,
+      final LinkedHashSet<String> allowSet
+  )
+  {
+    this.baseAdapter = baseAdapter;
+    this.dimensionToUnnest = dimension;
+    this.outputColumnName = outputColumnName;
+    this.allowSet = allowSet;

Review Comment:
   what is special about allowSet that it gets its own variable? Is it just a 
filter or something more? 



##########
processing/src/main/java/org/apache/druid/query/UnnestDataSource.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.query;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.collect.ImmutableList;
+import org.apache.druid.java.util.common.IAE;
+import org.apache.druid.segment.SegmentReference;
+import org.apache.druid.segment.UnnestSegmentReference;
+import org.apache.druid.utils.JvmUtils;
+
+import javax.annotation.Nullable;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Function;
+
+public class UnnestDataSource implements DataSource
+{
+  private final DataSource base;
+  private final String column;
+  private final String outputName;
+  private final LinkedHashSet<String> allowList;
+
+  private UnnestDataSource(
+      DataSource dataSource,
+      String columnName,
+      String outputName,
+      LinkedHashSet<String> allowList
+  )
+  {
+    this.base = dataSource;
+    this.column = columnName;
+    this.outputName = outputName;
+    this.allowList = allowList;
+  }
+
+  @JsonCreator
+  public static UnnestDataSource create(
+      @JsonProperty("base") DataSource base,
+      @JsonProperty("column") String columnName,
+      @JsonProperty("outputName") String outputName,
+      @Nullable @JsonProperty("allowList") LinkedHashSet<String> allowList
+  )
+  {
+    return new UnnestDataSource(base, columnName, outputName, allowList);
+  }
+
+  @JsonProperty("base")
+  public DataSource getBase()
+  {
+    return base;
+  }
+
+  @JsonProperty("column")
+  public String getColumn()
+  {
+    return column;
+  }
+
+  @JsonProperty("outputName")
+  public String getOutputName()
+  {
+    return outputName;
+  }
+
+  @JsonProperty("allowList")
+  public LinkedHashSet<String> getAllowList()
+  {
+    return allowList;
+  }
+
+  @Override
+  public Set<String> getTableNames()
+  {
+    return base.getTableNames();
+  }
+
+  @Override
+  public List<DataSource> getChildren()
+  {
+    return ImmutableList.of(base);
+  }
+
+  @Override
+  public DataSource withChildren(List<DataSource> children)
+  {
+    if (children.size() != 1) {
+      throw new IAE("Expected [1] child, got [%d]", children.size());
+    }
+    return new UnnestDataSource(children.get(0), column, outputName, 
allowList);
+  }
+
+  @Override
+  public boolean isCacheable(boolean isBroker)
+  {
+    return base.isCacheable(isBroker);
+  }
+
+  @Override
+  public boolean isGlobal()
+  {
+    return base.isGlobal();
+  }
+
+  @Override
+  public boolean isConcrete()
+  {
+    return base.isConcrete();
+  }
+
+  @Override
+  public Function<SegmentReference, SegmentReference> createSegmentMapFunction(
+      Query query,
+      AtomicLong cpuTimeAccumulator
+  )
+  {
+    final Function<SegmentReference, SegmentReference> segmentMapFn = 
base.createSegmentMapFunction(
+        query,
+        cpuTimeAccumulator
+    );
+    return JvmUtils.safeAccumulateThreadCpuTime(
+        cpuTimeAccumulator,
+        () -> {
+          if (column == null) {
+            return segmentMapFn;
+          } else if (column.isEmpty()) {
+            return segmentMapFn;
+          } else {
+            return
+                segmentMapFn.andThen(
+                    baseSegment ->
+                        new UnnestSegmentReference(
+                            baseSegment,
+                            column,
+                            outputName,
+                            allowList
+                        )
+                );
+          }
+        }
+    );
+
+  }
+
+  @Override
+  public DataSource withUpdatedDataSource(DataSource newSource)
+  {
+    return new UnnestDataSource(newSource, column, outputName, allowList);
+  }
+
+  @Override
+  public byte[] getCacheKey()
+  {
+    return null;

Review Comment:
   how does caching work for this data source? 



##########
processing/src/main/java/org/apache/druid/segment/ColumnarValueUnnestCursor.java:
##########
@@ -0,0 +1,286 @@
+/*
+ * 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.query.BaseQuery;
+import org.apache.druid.query.dimension.DefaultDimensionSpec;
+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.LinkedHashSet;
+import java.util.List;
+
+public class ColumnarValueUnnestCursor implements Cursor

Review Comment:
   can you add some javadocs here about this class? 



##########
processing/src/main/java/org/apache/druid/segment/UnnestStorageAdapter.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.collect.Lists;
+import org.apache.druid.java.util.common.granularity.Granularity;
+import org.apache.druid.java.util.common.guava.Sequence;
+import org.apache.druid.java.util.common.guava.Sequences;
+import org.apache.druid.query.QueryMetrics;
+import org.apache.druid.query.filter.Filter;
+import org.apache.druid.query.filter.InDimFilter;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.apache.druid.segment.data.Indexed;
+import org.apache.druid.segment.data.ListIndexed;
+import org.apache.druid.segment.filter.AndFilter;
+import org.joda.time.DateTime;
+import org.joda.time.Interval;
+
+import javax.annotation.Nullable;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.Objects;
+
+public class UnnestStorageAdapter implements StorageAdapter
+{
+  private final StorageAdapter baseAdapter;
+  private final String dimensionToUnnest;
+  private final String outputColumnName;
+  private final LinkedHashSet<String> allowSet;
+
+  public UnnestStorageAdapter(
+      final StorageAdapter baseAdapter,
+      final String dimension,
+      final String outputColumnName,
+      final LinkedHashSet<String> allowSet
+  )
+  {
+    this.baseAdapter = baseAdapter;
+    this.dimensionToUnnest = dimension;
+    this.outputColumnName = outputColumnName;
+    this.allowSet = allowSet;
+  }
+
+  @Override
+  public Sequence<Cursor> makeCursors(
+      @Nullable Filter filter,
+      Interval interval,
+      VirtualColumns virtualColumns,
+      Granularity gran,
+      boolean descending,
+      @Nullable QueryMetrics<?> queryMetrics
+  )
+  {
+    Filter updatedFilter;
+    if (allowSet != null && !allowSet.isEmpty()) {
+      final InDimFilter allowListFilters;
+      allowListFilters = new InDimFilter(dimensionToUnnest, allowSet);
+      if (filter != null) {
+        updatedFilter = new AndFilter(Arrays.asList(filter, allowListFilters));
+      } else {
+        updatedFilter = allowListFilters;
+      }
+    } else {
+      updatedFilter = filter;
+    }
+    final Sequence<Cursor> baseCursorSequence = baseAdapter.makeCursors(
+        updatedFilter,
+        interval,
+        virtualColumns,
+        gran,
+        descending,
+        queryMetrics
+    );
+
+    return Sequences.map(
+        baseCursorSequence,
+        cursor -> {
+          Objects.requireNonNull(cursor);
+          Cursor retVal = cursor;
+          ColumnCapabilities capabilities = 
cursor.getColumnSelectorFactory().getColumnCapabilities(dimensionToUnnest);
+          if (capabilities.isDictionaryEncoded() == 
ColumnCapabilities.Capable.TRUE
+              && capabilities.areDictionaryValuesUnique() == 
ColumnCapabilities.Capable.TRUE) {
+            retVal = new DimensionUnnestCursor(retVal, 
retVal.getColumnSelectorFactory(), dimensionToUnnest, outputColumnName, 
allowSet);
+          } else {
+            retVal = new ColumnarValueUnnestCursor(retVal, 
retVal.getColumnSelectorFactory(), dimensionToUnnest, outputColumnName, 
allowSet);
+          }
+          return retVal;
+        }
+    );
+  }
+
+  @Override
+  public Interval getInterval()
+  {
+    return baseAdapter.getInterval();
+  }
+
+  @Override
+  public Indexed<String> getAvailableDimensions()
+  {
+    final LinkedHashSet<String> availableDimensions = new LinkedHashSet<>();
+
+    for (String dim : baseAdapter.getAvailableDimensions()) {
+      availableDimensions.add(dim);
+    }
+    availableDimensions.add(outputColumnName);
+    return new ListIndexed<>(Lists.newArrayList(availableDimensions));
+  }
+
+  @Override
+  public Iterable<String> getAvailableMetrics()
+  {
+    return baseAdapter.getAvailableMetrics();
+  }
+
+  @Override
+  public int getDimensionCardinality(String column)
+  {
+    if (outputColumnName.equals(dimensionToUnnest)) {
+      return baseAdapter.getDimensionCardinality(column);
+    }
+    return baseAdapter.getDimensionCardinality(dimensionToUnnest);
+  }
+
+  @Override
+  public DateTime getMinTime()
+  {
+    return baseAdapter.getMinTime();
+  }
+
+  @Override
+  public DateTime getMaxTime()
+  {
+    return baseAdapter.getMaxTime();
+  }
+
+  @Nullable
+  @Override
+  public Comparable getMinValue(String column)
+  {
+    if (outputColumnName.equals(dimensionToUnnest)) {
+      return baseAdapter.getMinValue(column);
+    }
+    return baseAdapter.getMinValue(dimensionToUnnest);
+  }
+
+  @Nullable
+  @Override
+  public Comparable getMaxValue(String column)
+  {
+    if (outputColumnName.equals(dimensionToUnnest)) {
+      return baseAdapter.getMaxValue(column);
+    }
+    return baseAdapter.getMaxValue(dimensionToUnnest);
+  }
+
+  @Nullable
+  @Override
+  public ColumnCapabilities getColumnCapabilities(String column)
+  {
+    if (outputColumnName.equals(dimensionToUnnest)) {
+      return baseAdapter.getColumnCapabilities(column);

Review Comment:
   should the returned set of column capabilities always have 
`hasMultipleValues` to `false`? 



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