Copilot commented on code in PR #16498:
URL: https://github.com/apache/pinot/pull/16498#discussion_r2251747069


##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "

Review Comment:
   The error message refers to 'MIN' but should refer to 'MIN2' to correctly 
identify the function being used.



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());

Review Comment:
   The error message shows 'min(dataColumn, 'dataType')' but should show 
'min2(dataColumn, 'dataType')' to correctly represent the function syntax.
   ```suggestion
                   "MIN2 expects the 2nd argument to be literal, got: %s. The 
function can be used as "
                       + "min2(dataColumn, 'dataType')", dataTypeExp.getType());
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MinStringAggregationFunction.java:
##########
@@ -0,0 +1,187 @@
+/**
+ * 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.pinot.core.query.aggregation.function;
+
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.CustomObject;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.common.ObjectSerDeUtils;
+import org.apache.pinot.core.query.aggregation.AggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import 
org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+
+
+public class MinStringAggregationFunction extends 
NullableSingleInputAggregationFunction<String, String> {
+
+  public MinStringAggregationFunction(ExpressionContext expression, boolean 
nullHandlingEnabled) {
+    super(expression, nullHandlingEnabled);
+  }
+
+  @Override
+  public AggregationFunctionType getType() {
+    return AggregationFunctionType.MIN2;
+  }
+
+  @Override
+  public AggregationResultHolder createAggregationResultHolder() {
+    return new ObjectAggregationResultHolder();
+  }
+
+  @Override
+  public GroupByResultHolder createGroupByResultHolder(int initialCapacity, 
int maxCapacity) {
+    return new ObjectGroupByResultHolder(initialCapacity, maxCapacity);
+  }
+
+  @Override
+  public void aggregate(int length, AggregationResultHolder 
aggregationResultHolder,
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    BlockValSet blockValSet = blockValSetMap.get(_expression);
+    String[] values = blockValSet.getStringValuesSV();
+
+    String min = foldNotNull(length, blockValSet, null, (accum, from, to) -> {
+      String innerMin = values[from];
+      for (int i = from + 1; i < to; i++) {
+        innerMin = innerMin.compareTo(values[i]) > 0 ? values[i] : innerMin;
+      }
+      return accum == null ? innerMin : innerMin.compareTo(accum) > 0 ? accum 
: innerMin;
+    });
+
+    if (min != null) {
+      String otherMax = aggregationResultHolder.getResult();
+      if (otherMax == null) {
+        // If the other min is null, we set the value directly

Review Comment:
   The comment refers to 'other min' but the variable is named 'otherMax'. This 
inconsistency should be corrected to match the variable name or vice versa.
   ```suggestion
           // If the other max is null, we set the value directly
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MIN: " + typeLiteral);
+            }
+            switch (dataType) {
+              case INT:
+              case LONG:
+              case FLOAT:
+              case DOUBLE:
+                return new MinAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              case STRING:
+                return new MinStringAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              default:
+                throw new IllegalArgumentException("Unsupported data type for 
MIN: " + dataType);

Review Comment:
   The error message refers to 'MIN' but should refer to 'MIN2' to correctly 
identify the function being used.



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MIN: " + typeLiteral);
+            }
+            switch (dataType) {
+              case INT:
+              case LONG:
+              case FLOAT:
+              case DOUBLE:
+                return new MinAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              case STRING:
+                return new MinStringAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              default:
+                throw new IllegalArgumentException("Unsupported data type for 
MIN: " + dataType);
+            }
+          }
           case MAX:
             return new MaxAggregationFunction(arguments, nullHandlingEnabled);
+          case MAX2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MAX expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "max(dataColumn, 'dataType')", dataTypeExp.getType());

Review Comment:
   The error message shows 'max(dataColumn, 'dataType')' but should show 
'max2(dataColumn, 'dataType')' to correctly represent the function syntax.



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MIN: " + typeLiteral);
+            }
+            switch (dataType) {
+              case INT:
+              case LONG:
+              case FLOAT:
+              case DOUBLE:
+                return new MinAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              case STRING:
+                return new MinStringAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              default:
+                throw new IllegalArgumentException("Unsupported data type for 
MIN: " + dataType);
+            }
+          }
           case MAX:
             return new MaxAggregationFunction(arguments, nullHandlingEnabled);
+          case MAX2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MAX expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "max(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MAX: " + typeLiteral);
+            }
+            switch (dataType) {
+              case INT:
+              case LONG:
+              case FLOAT:
+              case DOUBLE:
+                return new MaxAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              case STRING:
+                return new MaxStringAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              default:
+                throw new IllegalArgumentException("Unsupported data type for 
MAX: " + dataType);

Review Comment:
   The error message refers to 'MAX' but should refer to 'MAX2' to correctly 
identify the function being used.



##########
pinot-query-runtime/src/test/resources/queries/Aggregates.json:
##########
@@ -44,7 +44,8 @@
       {
         "psql": "4.2.7",
         "description": "aggregations on string column",
-        "sql": "SELECT count(string_col), count(distinct(string_col)), 
count(*) FROM {tbl}"
+        "sql": "SELECT min2(string_col, 'STRING'), max2(string_col, 'STRING'), 
count(string_col), count(distinct(string_col)), count(*) FROM {tbl}",

Review Comment:
   The string literal 'STRING' should be lowercase 'string' to match the 
DataType enum values used in the factory (which converts to uppercase 
internally).
   ```suggestion
           "sql": "SELECT min2(string_col, 'string'), max2(string_col, 
'string'), count(string_col), count(distinct(string_col)), count(*) FROM {tbl}",
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MIN: " + typeLiteral);
+            }
+            switch (dataType) {
+              case INT:
+              case LONG:
+              case FLOAT:
+              case DOUBLE:
+                return new MinAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              case STRING:
+                return new MinStringAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              default:
+                throw new IllegalArgumentException("Unsupported data type for 
MIN: " + dataType);
+            }
+          }
           case MAX:
             return new MaxAggregationFunction(arguments, nullHandlingEnabled);
+          case MAX2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MAX expects the 2nd argument to be literal, got: %s. The 
function can be used as "

Review Comment:
   The error message refers to 'MAX' but should refer to 'MAX2' to correctly 
identify the function being used.



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MIN: " + typeLiteral);

Review Comment:
   The error message refers to 'MIN' but should refer to 'MIN2' to correctly 
identify the function being used.
   ```suggestion
                 throw new IllegalArgumentException("Unsupported data type for 
MIN2: " + typeLiteral);
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java:
##########
@@ -214,8 +214,56 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new CountAggregationFunction(arguments, 
nullHandlingEnabled);
           case MIN:
             return new MinAggregationFunction(arguments, nullHandlingEnabled);
+          case MIN2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MIN expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MIN: " + typeLiteral);
+            }
+            switch (dataType) {
+              case INT:
+              case LONG:
+              case FLOAT:
+              case DOUBLE:
+                return new MinAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              case STRING:
+                return new MinStringAggregationFunction(firstArgument, 
nullHandlingEnabled);
+              default:
+                throw new IllegalArgumentException("Unsupported data type for 
MIN: " + dataType);
+            }
+          }
           case MAX:
             return new MaxAggregationFunction(arguments, nullHandlingEnabled);
+          case MAX2: {
+            ExpressionContext dataTypeExp = arguments.get(1);
+            Preconditions.checkArgument(dataTypeExp.getType() == 
ExpressionContext.Type.LITERAL,
+                "MAX expects the 2nd argument to be literal, got: %s. The 
function can be used as "
+                    + "max(dataColumn, 'dataType')", dataTypeExp.getType());
+            DataType dataType;
+            String typeLiteral = 
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+            try {
+              dataType = DataType.valueOf(typeLiteral);
+            } catch (IllegalArgumentException e) {
+              throw new IllegalArgumentException("Unsupported data type for 
MAX: " + typeLiteral);

Review Comment:
   The error message refers to 'MAX' but should refer to 'MAX2' to correctly 
identify the function being used.



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