Copilot commented on code in PR #16498:
URL: https://github.com/apache/pinot/pull/16498#discussion_r2251631770
##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/AggregationFunctionType.java:
##########
@@ -52,7 +52,13 @@ public enum AggregationFunctionType {
COUNT("count"),
// TODO: min/max only supports NUMERIC in Pinot, where Calcite supports
COMPARABLE_ORDERED
MIN("min", SqlTypeName.DOUBLE, SqlTypeName.DOUBLE),
+ /// An alternative to MIN to support other types
+ MIN2("min2", ReturnTypes.ARG0,
+ OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.CHARACTER),
SqlTypeName.OTHER),
MAX("max", SqlTypeName.DOUBLE, SqlTypeName.DOUBLE),
+ /// An alternative to MAX to support other types
Review Comment:
Use standard Java documentation comments with /** */ instead of /// style
comments for consistency with the Apache license header and other documentation
in the file.
##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/AggregationFunctionType.java:
##########
@@ -52,7 +52,13 @@ public enum AggregationFunctionType {
COUNT("count"),
// TODO: min/max only supports NUMERIC in Pinot, where Calcite supports
COMPARABLE_ORDERED
MIN("min", SqlTypeName.DOUBLE, SqlTypeName.DOUBLE),
+ /// An alternative to MIN to support other types
Review Comment:
Use standard Java documentation comments with /** */ instead of /// style
comments for consistency with the Apache license header and other documentation
in the file.
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MaxStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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 MaxStringAggregationFunction extends
NullableSingleInputAggregationFunction<String, String> {
+
+ public MaxStringAggregationFunction(ExpressionContext expression, boolean
nullHandlingEnabled) {
+ super(expression, nullHandlingEnabled);
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.MAX2;
+ }
+
+ @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 max = foldNotNull(length, blockValSet, null, (accum, from, to) -> {
+ String innerMax = values[from];
+ for (int i = from + 1; i < to; i++) {
+ innerMax = innerMax.compareTo(values[i]) < 0 ? values[i] : innerMax;
+ }
+ return accum == null ? innerMax : innerMax.compareTo(accum) < 0 ? accum
: innerMax;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, max);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String max) {
Review Comment:
Remove extra space between 'String' and 'max' parameter name.
##########
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 2rd argument to be literal, got: %s. The
function can be used as "
+ + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+ DataType dataType;
+ try {
+ String upperCase =
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+ dataType = DataType.valueOf(upperCase);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Unsupported data type for
MIN: " + upperCaseFunctionName);
+ }
+ 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 2rd argument to be literal, got: %s. The
function can be used as "
Review Comment:
Change '2rd' to '2nd' for correct ordinal number formatting.
##########
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 2rd argument to be literal, got: %s. The
function can be used as "
+ + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+ DataType dataType;
+ try {
+ String upperCase =
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+ dataType = DataType.valueOf(upperCase);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Unsupported data type for
MIN: " + upperCaseFunctionName);
Review Comment:
The error message uses 'upperCaseFunctionName' which should be
'dataTypeExp.getLiteral().getStringValue()' to show the actual invalid data
type value that was provided.
```suggestion
throw new IllegalArgumentException("Unsupported data type for
MIN: " + dataTypeExp.getLiteral().getStringValue());
```
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MinStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, min);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String min) {
+ if (min != null) {
+ if (_nullHandlingEnabled) {
+ String otherMin = aggregationResultHolder.getResult();
+ if (otherMin == null) {
+ // If the other min is null, we set the value directly
+ aggregationResultHolder.setValue(min);
+ } else {
+ // Compare and set the minimum value
+ aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
+ }
+ } else {
+ String otherMin = aggregationResultHolder.getResult();
+ aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
+ }
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray,
GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ BlockValSet blockValSet = blockValSetMap.get(_expression);
+ String[] valueArray = blockValSet.getStringValuesSV();
+
+ if (_nullHandlingEnabled) {
+ forEachNotNull(length, blockValSet, (from, to) -> {
+ for (int i = from; i < to; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ String result = groupByResultHolder.getResult(groupKey);
+ if (result == null || value.compareTo(result) < 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ });
+ } else {
+ for (int i = 0; i < length; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ if (value.compareTo(groupByResultHolder.getResult(groupKey)) < 0) {
Review Comment:
Potential NullPointerException when groupByResultHolder.getResult(groupKey)
returns null and value.compareTo() is called on a null reference. This should
check for null result before comparison, similar to the null handling enabled
branch above.
```suggestion
String result = groupByResultHolder.getResult(groupKey);
if (result == null || value.compareTo(result) < 0) {
```
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MinStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, min);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String min) {
+ if (min != null) {
+ if (_nullHandlingEnabled) {
+ String otherMin = aggregationResultHolder.getResult();
+ if (otherMin == null) {
+ // If the other min is null, we set the value directly
+ aggregationResultHolder.setValue(min);
+ } else {
+ // Compare and set the minimum value
+ aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
+ }
+ } else {
+ String otherMin = aggregationResultHolder.getResult();
+ aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
+ }
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray,
GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ BlockValSet blockValSet = blockValSetMap.get(_expression);
+ String[] valueArray = blockValSet.getStringValuesSV();
+
+ if (_nullHandlingEnabled) {
+ forEachNotNull(length, blockValSet, (from, to) -> {
+ for (int i = from; i < to; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ String result = groupByResultHolder.getResult(groupKey);
+ if (result == null || value.compareTo(result) < 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ });
+ } else {
+ for (int i = 0; i < length; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ if (value.compareTo(groupByResultHolder.getResult(groupKey)) < 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray,
GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ BlockValSet blockValSet = blockValSetMap.get(_expression);
+ String[] valueArray = blockValSet.getStringValuesSV();
+
+ if (_nullHandlingEnabled) {
+ forEachNotNull(length, blockValSet, (from, to) -> {
+ for (int i = from; i < to; i++) {
+ String value = valueArray[i];
+ for (int groupKey : groupKeysArray[i]) {
+ String result = groupByResultHolder.getResult(groupKey);
+ if (result == null || value.compareTo(result) < 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ }
+ });
+ } else {
+ for (int i = 0; i < length; i++) {
+ String value = valueArray[i];
+ for (int groupKey : groupKeysArray[i]) {
+ if (value.compareTo(groupByResultHolder.getResult(groupKey)) < 0) {
Review Comment:
Potential NullPointerException when groupByResultHolder.getResult(groupKey)
returns null and value.compareTo() is called on a null reference. This should
check for null result before comparison, similar to the null handling enabled
branch above.
```suggestion
String result = groupByResultHolder.getResult(groupKey);
if (result == null || value.compareTo(result) < 0) {
```
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MinStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, min);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String min) {
+ if (min != null) {
+ if (_nullHandlingEnabled) {
+ String otherMin = aggregationResultHolder.getResult();
+ if (otherMin == null) {
+ // If the other min is null, we set the value directly
+ aggregationResultHolder.setValue(min);
+ } else {
+ // Compare and set the minimum value
+ aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
+ }
+ } else {
+ String otherMin = aggregationResultHolder.getResult();
+ aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
Review Comment:
Potential NullPointerException when otherMin is null in the non-null
handling branch. The code should check if otherMin is null before calling
compareTo, similar to the null handling enabled branch above.
```suggestion
if (otherMin == null) {
aggregationResultHolder.setValue(min);
} else {
aggregationResultHolder.setValue(min.compareTo(otherMin) > 0 ?
otherMin : min);
}
```
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MaxStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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 MaxStringAggregationFunction extends
NullableSingleInputAggregationFunction<String, String> {
+
+ public MaxStringAggregationFunction(ExpressionContext expression, boolean
nullHandlingEnabled) {
+ super(expression, nullHandlingEnabled);
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.MAX2;
+ }
+
+ @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 max = foldNotNull(length, blockValSet, null, (accum, from, to) -> {
+ String innerMax = values[from];
+ for (int i = from + 1; i < to; i++) {
+ innerMax = innerMax.compareTo(values[i]) < 0 ? values[i] : innerMax;
+ }
+ return accum == null ? innerMax : innerMax.compareTo(accum) < 0 ? accum
: innerMax;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, max);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String max) {
+ if (max != null) {
+ if (_nullHandlingEnabled) {
+ String otherMax = aggregationResultHolder.getResult();
+ if (otherMax == null) {
+ // If the other max is null, we set the value directly
+ aggregationResultHolder.setValue(max);
+ } else {
+ // Compare and set the maximum value
+ aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
+ }
+ } else {
+ String otherMax = aggregationResultHolder.getResult();
+ aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
+ }
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray,
GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ BlockValSet blockValSet = blockValSetMap.get(_expression);
+ String[] valueArray = blockValSet.getStringValuesSV();
+
+ if (_nullHandlingEnabled) {
+ forEachNotNull(length, blockValSet, (from, to) -> {
+ for (int i = from; i < to; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ String result = groupByResultHolder.getResult(groupKey);
+ if (result == null || value.compareTo(result) > 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ });
+ } else {
+ for (int i = 0; i < length; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ if (value.compareTo(groupByResultHolder.getResult(groupKey)) > 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void aggregateGroupByMV(int length, int[][] groupKeysArray,
GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ BlockValSet blockValSet = blockValSetMap.get(_expression);
+ String[] valueArray = blockValSet.getStringValuesSV();
+
+ if (_nullHandlingEnabled) {
+ forEachNotNull(length, blockValSet, (from, to) -> {
+ for (int i = from; i < to; i++) {
+ String value = valueArray[i];
+ for (int groupKey : groupKeysArray[i]) {
+ String result = groupByResultHolder.getResult(groupKey);
+ if (result == null || value.compareTo(result) > 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ }
+ });
+ } else {
+ for (int i = 0; i < length; i++) {
+ String value = valueArray[i];
+ for (int groupKey : groupKeysArray[i]) {
+ if (value.compareTo(groupByResultHolder.getResult(groupKey)) > 0) {
Review Comment:
Potential NullPointerException when groupByResultHolder.getResult(groupKey)
returns null and value.compareTo() is called on a null reference. This should
check for null result before comparison, similar to the null handling enabled
branch above.
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MaxStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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 MaxStringAggregationFunction extends
NullableSingleInputAggregationFunction<String, String> {
+
+ public MaxStringAggregationFunction(ExpressionContext expression, boolean
nullHandlingEnabled) {
+ super(expression, nullHandlingEnabled);
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.MAX2;
+ }
+
+ @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 max = foldNotNull(length, blockValSet, null, (accum, from, to) -> {
+ String innerMax = values[from];
+ for (int i = from + 1; i < to; i++) {
+ innerMax = innerMax.compareTo(values[i]) < 0 ? values[i] : innerMax;
+ }
+ return accum == null ? innerMax : innerMax.compareTo(accum) < 0 ? accum
: innerMax;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, max);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String max) {
+ if (max != null) {
+ if (_nullHandlingEnabled) {
+ String otherMax = aggregationResultHolder.getResult();
+ if (otherMax == null) {
+ // If the other max is null, we set the value directly
+ aggregationResultHolder.setValue(max);
+ } else {
+ // Compare and set the maximum value
+ aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
+ }
+ } else {
+ String otherMax = aggregationResultHolder.getResult();
+ aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
+ }
+ }
+ }
+
+ @Override
+ public void aggregateGroupBySV(int length, int[] groupKeyArray,
GroupByResultHolder groupByResultHolder,
+ Map<ExpressionContext, BlockValSet> blockValSetMap) {
+ BlockValSet blockValSet = blockValSetMap.get(_expression);
+ String[] valueArray = blockValSet.getStringValuesSV();
+
+ if (_nullHandlingEnabled) {
+ forEachNotNull(length, blockValSet, (from, to) -> {
+ for (int i = from; i < to; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ String result = groupByResultHolder.getResult(groupKey);
+ if (result == null || value.compareTo(result) > 0) {
+ groupByResultHolder.setValueForKey(groupKey, value);
+ }
+ }
+ });
+ } else {
+ for (int i = 0; i < length; i++) {
+ String value = valueArray[i];
+ int groupKey = groupKeyArray[i];
+ if (value.compareTo(groupByResultHolder.getResult(groupKey)) > 0) {
Review Comment:
Potential NullPointerException when groupByResultHolder.getResult(groupKey)
returns null and value.compareTo() is called on a null reference. This should
check for null result before comparison, similar to the null handling enabled
branch above.
```suggestion
String result = groupByResultHolder.getResult(groupKey);
if (result == null || value.compareTo(result) > 0) {
```
##########
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 2rd argument to be literal, got: %s. The
function can be used as "
+ + "min(dataColumn, 'dataType')", dataTypeExp.getType());
+ DataType dataType;
+ try {
+ String upperCase =
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+ dataType = DataType.valueOf(upperCase);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Unsupported data type for
MIN: " + upperCaseFunctionName);
+ }
+ 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 2rd argument to be literal, got: %s. The
function can be used as "
+ + "max(dataColumn, 'dataType')", dataTypeExp.getType());
+ DataType dataType;
+ try {
+ String upperCase =
dataTypeExp.getLiteral().getStringValue().toUpperCase();
+ dataType = DataType.valueOf(upperCase);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Unsupported data type for
MAX: " + upperCaseFunctionName);
Review Comment:
The error message uses 'upperCaseFunctionName' which should be
'dataTypeExp.getLiteral().getStringValue()' to show the actual invalid data
type value that was provided.
##########
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 2rd argument to be literal, got: %s. The
function can be used as "
Review Comment:
Change '2rd' to '2nd' for correct ordinal number formatting.
##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/MaxStringAggregationFunction.java:
##########
@@ -0,0 +1,202 @@
+/**
+ * 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 MaxStringAggregationFunction extends
NullableSingleInputAggregationFunction<String, String> {
+
+ public MaxStringAggregationFunction(ExpressionContext expression, boolean
nullHandlingEnabled) {
+ super(expression, nullHandlingEnabled);
+ }
+
+ @Override
+ public AggregationFunctionType getType() {
+ return AggregationFunctionType.MAX2;
+ }
+
+ @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 max = foldNotNull(length, blockValSet, null, (accum, from, to) -> {
+ String innerMax = values[from];
+ for (int i = from + 1; i < to; i++) {
+ innerMax = innerMax.compareTo(values[i]) < 0 ? values[i] : innerMax;
+ }
+ return accum == null ? innerMax : innerMax.compareTo(accum) < 0 ? accum
: innerMax;
+ });
+
+ updateAggregationResultHolder(aggregationResultHolder, max);
+ }
+
+ protected void updateAggregationResultHolder(AggregationResultHolder
aggregationResultHolder, String max) {
+ if (max != null) {
+ if (_nullHandlingEnabled) {
+ String otherMax = aggregationResultHolder.getResult();
+ if (otherMax == null) {
+ // If the other max is null, we set the value directly
+ aggregationResultHolder.setValue(max);
+ } else {
+ // Compare and set the maximum value
+ aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
+ }
+ } else {
+ String otherMax = aggregationResultHolder.getResult();
+ aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
Review Comment:
Potential NullPointerException when otherMax is null in the non-null
handling branch. The code should check if otherMax is null before calling
compareTo, similar to the null handling enabled branch above.
```suggestion
if (otherMax == null) {
aggregationResultHolder.setValue(max);
} else {
aggregationResultHolder.setValue(max.compareTo(otherMax) < 0 ?
otherMax : max);
}
```
--
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]