You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2022/04/18 22:22:32 UTC

[GitHub] [druid] somu-imply commented on a diff in pull request #12439: Vectorize latest aggregator

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


##########
processing/src/main/java/org/apache/druid/query/aggregation/last/NumericLastVectorAggregator.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.aggregation.last;
+
+import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.query.aggregation.VectorAggregator;
+import org.apache.druid.segment.vector.VectorValueSelector;
+
+import javax.annotation.Nullable;
+import java.nio.ByteBuffer;
+
+public abstract class NumericLastVectorAggregator implements VectorAggregator
+{
+  final VectorValueSelector valueSelector;
+  private final boolean useDefault = NullHandling.replaceWithDefault();
+  private final VectorValueSelector timeSelector;
+  long lastTime;
+  boolean rhsNull;
+
+  public NumericLastVectorAggregator(VectorValueSelector timeSelector, VectorValueSelector valueSelector)
+  {
+    this.timeSelector = timeSelector;
+    this.valueSelector = valueSelector;
+    lastTime = Long.MIN_VALUE;
+    rhsNull = !useDefault;
+  }
+
+  @Override
+  public void aggregate(ByteBuffer buf, int position, int startRow, int endRow)
+  {
+    final long[] vector = timeSelector.getLongVector();
+    final boolean[] nulls = valueSelector.getNullVector();
+    boolean foundNull = false;
+
+    //check if nulls is null or not
+    if (nulls == null) {
+      foundNull = true;
+    }
+
+    //the time vector is already sorted so the last element would be the latest
+    //traverse the value vector from the back (for latest)
+
+    int index = 0;
+    if (!useDefault && foundNull == false) {
+      for (int i = endRow - 1; i >= startRow; i--) {
+        if (nulls[i] == false) {
+          index = i;
+          break;
+        }
+      }
+    } else {
+      index = endRow - 1;
+    }
+
+    //find the first non-null value
+    final long latestTime = vector[index];
+
+    if (latestTime > lastTime) {
+      lastTime = latestTime;
+      if (useDefault || (index >= startRow && index < endRow)) {
+        putValue(buf, position, index);
+        rhsNull = false;
+      } else {
+        rhsNull = true;
+      }
+    }
+  }
+
+  @Override
+  public void aggregate(
+      ByteBuffer buf,
+      int numRows,
+      int[] positions,
+      @Nullable int[] rows,
+      int positionOffset
+  )
+  {
+

Review Comment:
   Yes it is



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