github-code-scanning[bot] commented on code in PR #13652:
URL: https://github.com/apache/druid/pull/13652#discussion_r1065445398
##########
processing/src/main/java/org/apache/druid/query/rowsandcols/column/DoubleArrayColumn.java:
##########
@@ -93,9 +97,25 @@
};
}
+ @Nullable
+ @SuppressWarnings("unchecked")
@Override
public <T> T as(Class<? extends T> clazz)
{
+ if (VectorCopier.class.equals(clazz)) {
+ return (T) (VectorCopier) (into, intoStart) -> {
+ for (int i = 0; i < vals.length; ++i) {
+ into[intoStart + i] = vals[i];
Review Comment:
## User-controlled data in arithmetic expression
This arithmetic expression depends on a [user-provided value](1),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](2),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](3),
potentially causing an overflow.
[Show more
details](https://github.com/apache/druid/security/code-scanning/3632)
##########
processing/src/main/java/org/apache/druid/query/rowsandcols/column/ConstantObjectColumn.java:
##########
@@ -95,9 +100,19 @@
};
}
+ @Nullable
+ @SuppressWarnings("unchecked")
@Override
public <T> T as(Class<? extends T> clazz)
{
+ if (VectorCopier.class.equals(clazz)) {
+ return (T) (VectorCopier) (into, intoStart) -> Arrays.fill(into,
intoStart, intoStart + numRows, obj);
Review Comment:
## User-controlled data in arithmetic expression
This arithmetic expression depends on a [user-provided value](1),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](2),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](3),
potentially causing an overflow.
[Show more
details](https://github.com/apache/druid/security/code-scanning/3630)
##########
processing/src/test/java/org/apache/druid/query/rowsandcols/semantic/CombinedSemanticInterfacesTest.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.rowsandcols.semantic;
+
+import com.google.common.collect.ImmutableMap;
+import org.apache.druid.query.aggregation.AggregatorFactory;
+import org.apache.druid.query.aggregation.LongSumAggregatorFactory;
+import org.apache.druid.query.operator.ColumnWithDirection;
+import org.apache.druid.query.operator.window.RowsAndColumnsHelper;
+import org.apache.druid.query.operator.window.WindowFrame;
+import org.apache.druid.query.rowsandcols.MapOfColumnsRowsAndColumns;
+import org.apache.druid.query.rowsandcols.RowsAndColumns;
+import org.apache.druid.query.rowsandcols.column.ColumnAccessor;
+import org.apache.druid.query.rowsandcols.column.IntArrayColumn;
+import org.apache.druid.query.rowsandcols.column.ObjectArrayColumn;
+import org.apache.druid.segment.column.ColumnType;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+/**
+ * Place where tests can live that are testing the interactions of multiple
semantic interfaces
+ */
+@SuppressWarnings("ConstantConditions")
+public class CombinedSemanticInterfacesTest extends SemanticTestBase
+{
+ public CombinedSemanticInterfacesTest(
+ String name,
+ Function<MapOfColumnsRowsAndColumns, RowsAndColumns> fn
+ )
+ {
+ super(name, fn);
+ }
+
+ /**
+ * Tests a relatively common series of operations for window functions:
partition -> aggregate -> sort
+ */
+ @Test
+ public void testPartitionAggregateAndSortTest()
+ {
+ RowsAndColumns rac = make(MapOfColumnsRowsAndColumns.fromMap(
+ ImmutableMap.of(
+ "sorted", new IntArrayColumn(new int[]{0, 0, 0, 1, 1, 2, 4, 4, 4}),
+ "unsorted", new IntArrayColumn(new int[]{3, 54, 21, 1, 5, 54, 2,
3, 92})
+ )
+ ));
+
+ ClusteredGroupPartitioner parter = rac.as(ClusteredGroupPartitioner.class);
+ if (parter == null) {
+ parter = new DefaultClusteredGroupPartitioner(rac);
+ }
+
+ final ArrayList<RowsAndColumns> partitioned =
parter.partitionOnBoundaries(Collections.singletonList("sorted"));
+ Assert.assertEquals(4, partitioned.size());
+
+ NaiveSortMaker.NaiveSorter sorter = null;
+ for (RowsAndColumns rowsAndColumns : partitioned) {
+ final FramedOnHeapAggregatable aggregatable =
FramedOnHeapAggregatable.fromRAC(rowsAndColumns);
+ final RowsAndColumns aggedRAC = aggregatable.aggregateAll(
+ WindowFrame.unbounded(),
+ new AggregatorFactory[]{new LongSumAggregatorFactory("sum",
"unsorted")}
+ );
+ if (sorter == null) {
+ sorter =
NaiveSortMaker.fromRAC(aggedRAC).make(ColumnWithDirection.ascending("unsorted"));
+ } else {
+ Assert.assertNull(sorter.moreData(aggedRAC));
+ }
+ }
+
+ final RowsAndColumns completed = sorter.complete();
Review Comment:
## Dereferenced variable may be null
Variable [sorter](1) may be null at this access because of [this](2)
assignment.
[Show more
details](https://github.com/apache/druid/security/code-scanning/3626)
##########
processing/src/main/java/org/apache/druid/query/rowsandcols/ArrayListRowsAndColumns.java:
##########
@@ -19,55 +19,123 @@
package org.apache.druid.query.rowsandcols;
+import it.unimi.dsi.fastutil.Arrays;
+import it.unimi.dsi.fastutil.ints.IntArrayList;
+import it.unimi.dsi.fastutil.ints.IntComparator;
+import it.unimi.dsi.fastutil.ints.IntList;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.query.operator.ColumnWithDirection;
import org.apache.druid.query.rowsandcols.column.Column;
import org.apache.druid.query.rowsandcols.column.ColumnAccessor;
+import org.apache.druid.query.rowsandcols.column.ColumnValueSwapper;
+import org.apache.druid.query.rowsandcols.column.DefaultVectorCopier;
+import org.apache.druid.query.rowsandcols.column.LimitedColumn;
+import org.apache.druid.query.rowsandcols.column.ObjectArrayColumn;
import org.apache.druid.query.rowsandcols.column.ObjectColumnAccessorBase;
+import org.apache.druid.query.rowsandcols.column.VectorCopier;
+import org.apache.druid.query.rowsandcols.semantic.AppendableRowsAndColumns;
+import org.apache.druid.query.rowsandcols.semantic.ClusteredGroupPartitioner;
+import
org.apache.druid.query.rowsandcols.semantic.DefaultClusteredGroupPartitioner;
+import org.apache.druid.query.rowsandcols.semantic.NaiveSortMaker;
import org.apache.druid.segment.RowAdapter;
import org.apache.druid.segment.column.ColumnType;
import org.apache.druid.segment.column.RowSignature;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.function.Function;
-public class ArrayListRowsAndColumns<RowType> implements RowsAndColumns
+/**
+ * ArrayListRowsAndColumns is a RowsAndColumns implementation that believes it
has all of its data on-heap.
+ * <p>
+ * It is an AppendableRowsAndColumns and, as with all RowsAndColumns, it is
not thread-safe for multiple writes.
+ * Under certain circumstances, concurrent reads from multiple threads can be
correct, but the code has to be follow
+ * a very strict ordering of code that ensures that reads only happen after
writes and once a value is read, it will
+ * never be overwritten.
+ * <p>
+ * Additionally, this object implements various of the semantic interfaces
directly to provide some degree
+ * of processing and memory optimization.
+ *
+ * @param <RowType>
+ */
+public class ArrayListRowsAndColumns<RowType> implements
AppendableRowsAndColumns
{
private final ArrayList<RowType> rows;
private final RowAdapter<RowType> rowAdapter;
private final RowSignature rowSignature;
+ private final Map<String, Column> extraColumns;
+ private final Set<String> columnNames;
+ private final int startOffset;
+ private final int endOffset;
+
public ArrayListRowsAndColumns(
ArrayList<RowType> rows,
RowAdapter<RowType> rowAdapter,
RowSignature rowSignature
)
+ {
+ this(
+ rows,
+ rowAdapter,
+ rowSignature,
+ new LinkedHashMap<>(),
+ new LinkedHashSet<>(rowSignature.getColumnNames()),
+ 0,
+ rows.size()
+ );
+ }
+
+ private ArrayListRowsAndColumns(
+ ArrayList<RowType> rows,
+ RowAdapter<RowType> rowAdapter,
+ RowSignature rowSignature,
+ Map<String, Column> extraColumns,
+ Set<String> columnNames,
+ int startOffset,
+ int endOffset
+ )
{
this.rows = rows;
this.rowAdapter = rowAdapter;
this.rowSignature = rowSignature;
+ this.extraColumns = extraColumns;
+ this.columnNames = columnNames;
+ this.startOffset = startOffset;
+ this.endOffset = endOffset;
}
@Override
public Collection<String> getColumnNames()
{
- return rowSignature.getColumnNames();
+ return columnNames;
}
@Override
public int numRows()
{
- return rows.size();
+ return endOffset - startOffset;
Review Comment:
## User-controlled data in arithmetic expression
This arithmetic expression depends on a [user-provided value](1),
potentially causing an underflow.
This arithmetic expression depends on a [user-provided value](2),
potentially causing an underflow.
This arithmetic expression depends on a [user-provided value](3),
potentially causing an underflow.
This arithmetic expression depends on a [user-provided value](4),
potentially causing an underflow.
This arithmetic expression depends on a [user-provided value](5),
potentially causing an underflow.
[Show more
details](https://github.com/apache/druid/security/code-scanning/3628)
##########
processing/src/main/java/org/apache/druid/query/rowsandcols/column/ObjectArrayColumn.java:
##########
@@ -41,6 +43,12 @@
this.comparator = comparator;
}
+ public Object[] getObjects()
Review Comment:
## Exposing internal representation
getObjects exposes the internal representation stored in field objects. The
value may be modified [after this call to getObjects](1).
[Show more
details](https://github.com/apache/druid/security/code-scanning/3627)
##########
processing/src/main/java/org/apache/druid/query/rowsandcols/column/IntArrayColumn.java:
##########
@@ -93,9 +97,25 @@
};
}
+ @Nullable
+ @SuppressWarnings("unchecked")
@Override
public <T> T as(Class<? extends T> clazz)
{
+ if (VectorCopier.class.equals(clazz)) {
+ return (T) (VectorCopier) (into, intoStart) -> {
+ for (int i = 0; i < vals.length; ++i) {
+ into[intoStart + i] = vals[i];
Review Comment:
## User-controlled data in arithmetic expression
This arithmetic expression depends on a [user-provided value](1),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](2),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](3),
potentially causing an overflow.
[Show more
details](https://github.com/apache/druid/security/code-scanning/3633)
##########
processing/src/main/java/org/apache/druid/query/rowsandcols/column/DefaultVectorCopier.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.rowsandcols.column;
+
+public class DefaultVectorCopier implements VectorCopier
+{
+ ColumnAccessor accessor;
+
+ public DefaultVectorCopier(ColumnAccessor accessor)
+ {
+ this.accessor = accessor;
+ }
+
+ @Override
+ public void copyInto(Object[] into, int intoStart)
+ {
+ final int numRows = accessor.numRows();
+ for (int i = 0; i < numRows; ++i) {
+ into[intoStart + i] = accessor.getObject(i);
Review Comment:
## User-controlled data in arithmetic expression
This arithmetic expression depends on a [user-provided value](1),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](2),
potentially causing an overflow.
This arithmetic expression depends on a [user-provided value](3),
potentially causing an overflow.
[Show more
details](https://github.com/apache/druid/security/code-scanning/3631)
--
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]