LakshSingla commented on code in PR #16230:
URL: https://github.com/apache/druid/pull/16230#discussion_r1567097904


##########
processing/src/main/java/org/apache/druid/query/aggregation/firstlast/FirstLastUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.firstlast;
+
+import org.apache.druid.query.aggregation.SerializablePairLongDouble;
+import org.apache.druid.query.aggregation.SerializablePairLongFloat;
+import org.apache.druid.query.aggregation.SerializablePairLongLong;
+import org.apache.druid.segment.BaseObjectColumnValueSelector;
+import org.apache.druid.segment.DimensionHandlerUtils;
+import org.apache.druid.segment.NilColumnValueSelector;
+import org.apache.druid.segment.column.ColumnCapabilities;
+import org.apache.druid.segment.column.ValueType;
+
+import javax.annotation.Nullable;
+
+public class FirstLastUtils
+{
+
+  /**
+   * Returns whether a given value selector *might* contain object assignable 
from pairClass (SerializablePairLong*).
+   */
+  public static boolean selectorNeedsFoldCheck(
+      final BaseObjectColumnValueSelector<?> valueSelector,
+      @Nullable final ColumnCapabilities valueSelectorCapabilities,
+      Class pairClass
+  )
+  {
+    if (valueSelectorCapabilities != null && 
!valueSelectorCapabilities.is(ValueType.COMPLEX)) {
+      // Known, non-complex type.
+      return false;
+    }
+
+    if (valueSelector instanceof NilColumnValueSelector) {
+      // Nil column, definitely no SerializablePairLongObject.
+      return false;
+    }
+
+    // Check if the selector class could possibly be of pairClass* (either a 
superclass or subclass).
+    final Class<?> clazz = valueSelector.classOfObject();
+    return clazz.isAssignableFrom(pairClass)
+           || pairClass.isAssignableFrom(clazz);
+  }
+
+  @Nullable
+  public static SerializablePairLongDouble readDoublePairFromVectorSelectors(
+      @Nullable boolean[] timeNullityVector,
+      long[] timeVector,
+      Object[] objectVector,
+      int index
+  )
+  {
+    final long time;
+    final Double value;
+
+    final Object object = objectVector[index];
+
+    if (object instanceof SerializablePairLongDouble) {
+      // We got a folded object, ignore timeSelector completely, the object 
has all the info it requires
+      final SerializablePairLongDouble pair = (SerializablePairLongDouble) 
object;
+      // if time == null, don't aggregate
+      if (pair.lhs == null) {
+        return null;
+      }
+      time = pair.lhs;
+      value = pair.rhs;
+    } else {
+      if (timeNullityVector != null && timeNullityVector[index]) {
+        // Donot aggregate pairs where time is unknown
+        return null;
+      }
+      time = timeVector[index];
+      value = DimensionHandlerUtils.convertObjectToDouble(object);
+    }
+    return new SerializablePairLongDouble(time, value);
+  }
+
+  @Nullable
+  public static SerializablePairLongFloat readFloatPairFromVectorSelectors(
+      @Nullable boolean[] timeNullityVector,
+      long[] timeVector,
+      Object[] objectVector,
+      int index
+  )
+  {
+    final long time;
+    final Float value;
+
+    final Object object = objectVector[index];
+
+    if (object instanceof SerializablePairLongFloat) {

Review Comment:
   If there's a Pair, then we ignore everything supplied by the timeSelector. 
All the information about the time is encoded in that pair. A pair could be 
something like: {lhs: time1, rhs: value1}, and the timeNullity vector could be 
[true]. We **need** to ignore whatever is present in the timeVector, and 
proceed with the information present inside the pair.
   
   Here's a PR https://github.com/apache/druid/pull/15939, where in the 
non-vectorised versions, this flow was broken - we checked the time's nullity 
first and then considered the pairs. That led to incorrect results  



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