You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "github-code-scanning[bot] (via GitHub)" <gi...@apache.org> on 2023/03/20 07:24:57 UTC

[GitHub] [druid] github-code-scanning[bot] commented on a diff in pull request #13952: Limit the subquery results by memory usage

github-code-scanning[bot] commented on code in PR #13952:
URL: https://github.com/apache/druid/pull/13952#discussion_r1141713394


##########
server/src/main/java/org/apache/druid/server/InlineResultsCursor.java:
##########
@@ -0,0 +1,425 @@
+/*
+ * 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.server;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import org.apache.druid.frame.write.UnsupportedColumnTypeException;
+import org.apache.druid.java.util.common.DateTimes;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.query.BaseQuery;
+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.ColumnSelectorFactory;
+import org.apache.druid.segment.ColumnValueSelector;
+import org.apache.druid.segment.Cursor;
+import org.apache.druid.segment.DimensionDictionarySelector;
+import org.apache.druid.segment.DimensionSelector;
+import org.apache.druid.segment.DoubleColumnSelector;
+import org.apache.druid.segment.FloatColumnSelector;
+import org.apache.druid.segment.IdLookup;
+import org.apache.druid.segment.LongColumnSelector;
+import org.apache.druid.segment.ObjectColumnSelector;
+import org.apache.druid.segment.SimpleAscendingOffset;
+import org.apache.druid.segment.SimpleSettableOffset;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.segment.column.RowSignature;
+import org.apache.druid.segment.data.IndexedInts;
+import org.apache.druid.segment.data.Offset;
+import org.apache.druid.segment.data.RangeIndexedInts;
+import org.apache.druid.segment.serde.ComplexMetrics;
+import org.joda.time.DateTime;
+
+import javax.annotation.Nullable;
+import java.util.List;
+import java.util.Optional;
+
+public class InlineResultsCursor implements Cursor
+{
+
+  private final SimpleSettableOffset offset;
+  private final RowSignature rowSignature;
+  private final List<Object[]> results;
+
+
+  public InlineResultsCursor(List<Object[]> results, RowSignature rowSignature)
+  {
+    this.results = Preconditions.checkNotNull(results, "'results' cannot be null");
+    this.rowSignature = rowSignature;
+    this.offset = new SimpleAscendingOffset(results.size());
+  }
+
+  @Override
+  public ColumnSelectorFactory getColumnSelectorFactory()
+  {
+    return new ColumnSelectorFactory()
+    {
+      @Override
+      public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec)
+      {
+        return makeDimensionSelectorFor(dimensionSpec);
+      }
+
+      @Override
+      public ColumnValueSelector makeColumnValueSelector(String columnName)
+      {
+        int index = offset.getOffset();
+        return makeColumnValueSelectorFor(columnName, index);
+      }
+
+      @Nullable
+      @Override
+      public ColumnCapabilities getColumnCapabilities(String column)
+      {
+        return rowSignature.getColumnCapabilities(column);
+      }
+    };
+  }
+
+  @Override
+  public DateTime getTime()
+  {
+    return DateTimes.MIN;
+  }
+
+  @Override
+  public void advance()
+  {
+    offset.increment();
+    BaseQuery.checkInterrupted();
+  }
+
+  @Override
+  public void advanceUninterruptibly()
+  {
+    offset.increment();
+  }
+
+  @Override
+  public boolean isDone()
+  {
+    return !offset.withinBounds();
+  }
+
+  @Override
+  public boolean isDoneOrInterrupted()
+  {
+    return !offset.withinBounds() || Thread.currentThread().isInterrupted();
+  }
+
+  @Override
+  public void reset()
+  {
+    offset.reset();
+  }
+
+  private DimensionSelector makeDimensionSelectorFor(DimensionSpec dimensionSpec)
+  {
+    return dimensionSpec.decorate(makeUndecoratedDimensionSelectorFor(dimensionSpec.getDimension()));
+  }
+
+  private DimensionSelector makeUndecoratedDimensionSelectorFor(String dimensionSpec)
+  {
+    return new DimensionSelector()
+    {
+      @Override
+      public IndexedInts getRow()
+      {
+        Object stringValueObject = results.get(offset.getOffset())[rowSignature.indexOf(dimensionSpec)];
+        RangeIndexedInts rangeIndexedInts = new RangeIndexedInts();
+        if (stringValueObject instanceof String) {
+          rangeIndexedInts.setSize(1);
+        } else if (stringValueObject instanceof List) {
+          rangeIndexedInts.setSize(((List<?>) stringValueObject).size());
+        }
+        return rangeIndexedInts;
+      }
+
+      @Override
+      public ValueMatcher makeValueMatcher(@Nullable String value)
+      {
+        throw new UnsupportedOperationException();
+      }
+
+      @Override
+      public ValueMatcher makeValueMatcher(Predicate<String> predicate)
+      {
+        throw new UnsupportedOperationException();
+      }
+
+      @Override
+      public void inspectRuntimeShape(RuntimeShapeInspector inspector)
+      {
+
+      }
+
+      @Nullable
+      @Override
+      public Object getObject()
+      {
+        Object stringValueObject = results.get(offset.getOffset())[rowSignature.indexOf(dimensionSpec)];
+        return stringValueObject;
+      }
+
+      @Override
+      public Class<?> classOfObject()
+      {
+        Object stringValueObject = results.get(offset.getOffset())[rowSignature.indexOf(dimensionSpec)];
+        if (stringValueObject instanceof String) {
+          return String.class;
+        } else if (stringValueObject instanceof List) {
+          return List.class;
+        } else {
+          return Object.class;
+        }
+      }
+
+      @Override
+      public int getValueCardinality()
+      {
+        return DimensionDictionarySelector.CARDINALITY_UNKNOWN;
+      }
+
+      @Nullable
+      @Override
+      public String lookupName(int id)
+      {
+        Object stringValueObject = results.get(offset.getOffset())[rowSignature.indexOf(dimensionSpec)];
+        if (stringValueObject instanceof String) {
+          if (id == 0) {
+            return (String) stringValueObject;
+          } else {
+            throw new IndexOutOfBoundsException();
+          }
+        } else if (stringValueObject instanceof List) {
+          List<?> stringValueList = (List<?>) stringValueObject;
+          if (id < stringValueList.size()) {
+            return (String) stringValueList.get(id);
+          } else {
+            throw new IndexOutOfBoundsException();
+          }
+        } else {
+          throw new UnsupportedOperationException();
+        }
+      }
+
+      @Override
+      public boolean nameLookupPossibleInAdvance()
+      {
+        return false;
+      }
+
+      @Nullable
+      @Override
+      public IdLookup idLookup()
+      {
+        return null;
+      }
+    };
+  }
+
+  private ColumnValueSelector makeColumnValueSelectorFor(String columnName, int index)

Review Comment:
   ## Useless parameter
   
   The parameter 'index' is never used.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/4410)



-- 
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: commits-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org