You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2020/09/30 02:35:59 UTC

[GitHub] [incubator-pinot] Jackie-Jiang commented on a change in pull request #6053: Add support for Decimal with Precision Sum aggregation

Jackie-Jiang commented on a change in pull request #6053:
URL: https://github.com/apache/incubator-pinot/pull/6053#discussion_r497201010



##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }
+    System.arraycopy(value, 0, bigDecimalBytesArray, 4, value.length);
+    return bigDecimalBytesArray;
+  }
+
+  @ScalarFunction
+  public static String bytesToBigDecimal(byte[] bytes) {
+    int scale = 0;
+    for (int i = 0; i < 4; i++) {

Review comment:
       Similarly here

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }

Review comment:
       For better performance, avoid the for loop
   ```suggestion
       bigDecimalBytesArray[0] = (byte) scale >>> 24;
       bigDecimalBytesArray[1] = (byte) scale >>> 16;
       bigDecimalBytesArray[2] = (byte) scale >>> 8;
       bigDecimalBytesArray[3] = (byte) scale;
   ```

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }
+    System.arraycopy(value, 0, bigDecimalBytesArray, 4, value.length);
+    return bigDecimalBytesArray;
+  }
+
+  @ScalarFunction
+  public static String bytesToBigDecimal(byte[] bytes) {
+    int scale = 0;
+    for (int i = 0; i < 4; i++) {
+      scale += (((int) bytes[i]) << (8 * (3 - i)));
+    }
+    byte[] vals = new byte[bytes.length - 4];
+    System.arraycopy(bytes, 4, vals, 0, vals.length);
+    BigInteger unscaled = new BigInteger(vals);
+    BigDecimal number = new BigDecimal(unscaled, scale);
+    return number.toString();
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalFromString(String bigDecimal) {
+    return bigDecimalToBytes(new BigDecimal(bigDecimal));
+  }
+
+  @ScalarFunction
+  public static byte[] hexToBytes(String hex) {
+    int len = hex.length();

Review comment:
       You may use `BytesUtils.toBytes(hex)`

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }
+    System.arraycopy(value, 0, bigDecimalBytesArray, 4, value.length);
+    return bigDecimalBytesArray;
+  }
+
+  @ScalarFunction
+  public static String bytesToBigDecimal(byte[] bytes) {
+    int scale = 0;
+    for (int i = 0; i < 4; i++) {
+      scale += (((int) bytes[i]) << (8 * (3 - i)));
+    }
+    byte[] vals = new byte[bytes.length - 4];
+    System.arraycopy(bytes, 4, vals, 0, vals.length);
+    BigInteger unscaled = new BigInteger(vals);
+    BigDecimal number = new BigDecimal(unscaled, scale);
+    return number.toString();
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalFromString(String bigDecimal) {
+    return bigDecimalToBytes(new BigDecimal(bigDecimal));
+  }
+
+  @ScalarFunction
+  public static byte[] hexToBytes(String hex) {
+    int len = hex.length();
+    byte[] data = new byte[len / 2];
+    for (int i = 0; i < len; i += 2) {
+      data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
+    }
+    return data;
+  }
+
+  @ScalarFunction
+  public static String bytesToHex(byte[] bytes) {
+    StringBuilder sb = new StringBuilder();

Review comment:
       `BytesUtils.toHexString(bytes)`

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/SumWithPrecisionAggregationFunction.java
##########
@@ -0,0 +1,174 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.MathContext;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.AggregationFunctionType;
+import org.apache.pinot.common.function.scalar.DataTypeConversionFunctions;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+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.core.query.request.context.ExpressionContext;
+
+
+public class SumWithPrecisionAggregationFunction extends BaseSingleInputAggregationFunction<BigDecimal, BigDecimal> {
+  MathContext _mathContext = new MathContext(0);
+  Integer _scale = null;
+
+  public SumWithPrecisionAggregationFunction(ExpressionContext expression, List<ExpressionContext> arguments) {
+    super(expression);
+    int numArguments = arguments.size();
+
+    if (numArguments == 3) {
+      Integer precision = Integer.parseInt(arguments.get(1).getLiteral());
+      _scale = Integer.parseInt(arguments.get(2).getLiteral());
+      _mathContext = new MathContext(precision);
+    } else if (numArguments == 2) {
+      Integer precision = Integer.parseInt(arguments.get(1).getLiteral());
+      _mathContext = new MathContext(precision);
+    }
+  }
+
+  @Override
+  public AggregationFunctionType getType() {
+    return AggregationFunctionType.SUMPRECISION;
+  }
+
+  @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) {
+    byte[][] valueArray = blockValSetMap.get(_expression).getBytesValuesSV();

Review comment:
       We might also want to support string type as the BigDecimal values?

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/SumWithPrecisionAggregationFunction.java
##########
@@ -0,0 +1,174 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.MathContext;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.AggregationFunctionType;
+import org.apache.pinot.common.function.scalar.DataTypeConversionFunctions;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+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.core.query.request.context.ExpressionContext;
+
+
+public class SumWithPrecisionAggregationFunction extends BaseSingleInputAggregationFunction<BigDecimal, BigDecimal> {
+  MathContext _mathContext = new MathContext(0);
+  Integer _scale = null;
+
+  public SumWithPrecisionAggregationFunction(ExpressionContext expression, List<ExpressionContext> arguments) {
+    super(expression);
+    int numArguments = arguments.size();
+
+    if (numArguments == 3) {
+      Integer precision = Integer.parseInt(arguments.get(1).getLiteral());
+      _scale = Integer.parseInt(arguments.get(2).getLiteral());
+      _mathContext = new MathContext(precision);
+    } else if (numArguments == 2) {
+      Integer precision = Integer.parseInt(arguments.get(1).getLiteral());
+      _mathContext = new MathContext(precision);
+    }
+  }
+
+  @Override
+  public AggregationFunctionType getType() {
+    return AggregationFunctionType.SUMPRECISION;
+  }
+
+  @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) {
+    byte[][] valueArray = blockValSetMap.get(_expression).getBytesValuesSV();
+    BigDecimal sumValue = getDefaultResult(aggregationResultHolder);
+    for (int i = 0; i < length; i++) {
+      BigDecimal value = new BigDecimal(DataTypeConversionFunctions.bytesToBigDecimal(valueArray[i]));
+      sumValue = sumValue.add(value, _mathContext);
+    }
+    aggregationResultHolder.setValue(setScale(sumValue));

Review comment:
       To get the consistent result, we should either set the scale at the end (in `extractFinalResult`) or for each value. I think setting it in `extractFinalResult` should give better performance.

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }
+    System.arraycopy(value, 0, bigDecimalBytesArray, 4, value.length);
+    return bigDecimalBytesArray;
+  }
+
+  @ScalarFunction
+  public static String bytesToBigDecimal(byte[] bytes) {
+    int scale = 0;
+    for (int i = 0; i < 4; i++) {
+      scale += (((int) bytes[i]) << (8 * (3 - i)));
+    }
+    byte[] vals = new byte[bytes.length - 4];
+    System.arraycopy(bytes, 4, vals, 0, vals.length);
+    BigInteger unscaled = new BigInteger(vals);
+    BigDecimal number = new BigDecimal(unscaled, scale);
+    return number.toString();
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalFromString(String bigDecimal) {
+    return bigDecimalToBytes(new BigDecimal(bigDecimal));
+  }
+
+  @ScalarFunction
+  public static byte[] hexToBytes(String hex) {
+    int len = hex.length();
+    byte[] data = new byte[len / 2];
+    for (int i = 0; i < len; i += 2) {
+      data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
+    }
+    return data;
+  }
+
+  @ScalarFunction
+  public static String bytesToHex(byte[] bytes) {
+    StringBuilder sb = new StringBuilder();
+    for (byte b : bytes) {
+      sb.append(String.format("%02X ", b));
+    }
+
+    return sb.toString();
+  }
+
+  @ScalarFunction
+  public static String base64Encode(String input) {
+    return Base64.getEncoder().encodeToString(input.getBytes());
+  }
+
+  @ScalarFunction
+  public static String base64Decode(String input) {
+    return new String(Base64.getDecoder().decode(input.getBytes()));
+  }

Review comment:
       ```suggestion
     public static byte[] base64Decode(String input) {
       return Base64.getDecoder().decode(input);
     }
   ```

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {

Review comment:
       Recommend passing in `String` instead of `BigDecimal` for symmetry with `bytesToBigDecimal()`. Also, scaler transform function won't work on `BigDecimal` objects.

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }
+    System.arraycopy(value, 0, bigDecimalBytesArray, 4, value.length);
+    return bigDecimalBytesArray;
+  }
+
+  @ScalarFunction
+  public static String bytesToBigDecimal(byte[] bytes) {
+    int scale = 0;
+    for (int i = 0; i < 4; i++) {
+      scale += (((int) bytes[i]) << (8 * (3 - i)));
+    }
+    byte[] vals = new byte[bytes.length - 4];
+    System.arraycopy(bytes, 4, vals, 0, vals.length);
+    BigInteger unscaled = new BigInteger(vals);
+    BigDecimal number = new BigDecimal(unscaled, scale);
+    return number.toString();
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalFromString(String bigDecimal) {
+    return bigDecimalToBytes(new BigDecimal(bigDecimal));
+  }
+
+  @ScalarFunction
+  public static byte[] hexToBytes(String hex) {
+    int len = hex.length();
+    byte[] data = new byte[len / 2];
+    for (int i = 0; i < len; i += 2) {
+      data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
+    }
+    return data;
+  }
+
+  @ScalarFunction
+  public static String bytesToHex(byte[] bytes) {
+    StringBuilder sb = new StringBuilder();
+    for (byte b : bytes) {
+      sb.append(String.format("%02X ", b));
+    }
+
+    return sb.toString();
+  }
+
+  @ScalarFunction
+  public static String base64Encode(String input) {
+    return Base64.getEncoder().encodeToString(input.getBytes());
+  }

Review comment:
       ```suggestion
     public static byte[] base64Encode(byte[] bytes) {
       return Base64.getEncoder().encodeToString(bytes);
     }
   ```

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/SumWithPrecisionAggregationFunction.java
##########
@@ -0,0 +1,174 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.MathContext;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.AggregationFunctionType;
+import org.apache.pinot.common.function.scalar.DataTypeConversionFunctions;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+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.core.query.request.context.ExpressionContext;
+
+
+public class SumWithPrecisionAggregationFunction extends BaseSingleInputAggregationFunction<BigDecimal, BigDecimal> {
+  MathContext _mathContext = new MathContext(0);
+  Integer _scale = null;
+
+  public SumWithPrecisionAggregationFunction(ExpressionContext expression, List<ExpressionContext> arguments) {
+    super(expression);
+    int numArguments = arguments.size();
+
+    if (numArguments == 3) {
+      Integer precision = Integer.parseInt(arguments.get(1).getLiteral());
+      _scale = Integer.parseInt(arguments.get(2).getLiteral());
+      _mathContext = new MathContext(precision);
+    } else if (numArguments == 2) {
+      Integer precision = Integer.parseInt(arguments.get(1).getLiteral());
+      _mathContext = new MathContext(precision);
+    }
+  }
+
+  @Override
+  public AggregationFunctionType getType() {
+    return AggregationFunctionType.SUMPRECISION;
+  }
+
+  @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) {
+    byte[][] valueArray = blockValSetMap.get(_expression).getBytesValuesSV();
+    BigDecimal sumValue = getDefaultResult(aggregationResultHolder);
+    for (int i = 0; i < length; i++) {
+      BigDecimal value = new BigDecimal(DataTypeConversionFunctions.bytesToBigDecimal(valueArray[i]));

Review comment:
       Suggest adding a util class for `BigDecimal` (the scalar function can also call the util class). Here we should avoid converting `BigDecimal` to `String` and back again

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/SumWithPrecisionAggregationFunction.java
##########
@@ -0,0 +1,174 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.MathContext;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.AggregationFunctionType;
+import org.apache.pinot.common.function.scalar.DataTypeConversionFunctions;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+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.core.query.request.context.ExpressionContext;
+
+
+public class SumWithPrecisionAggregationFunction extends BaseSingleInputAggregationFunction<BigDecimal, BigDecimal> {

Review comment:
       Please add some javadoc on the arguments expected for the function.
   Also recommend renaming it to `SumPrecisionAggregationFunction` to match the function name

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/SumWithPrecisionAggregationFunction.java
##########
@@ -0,0 +1,174 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.MathContext;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.AggregationFunctionType;
+import org.apache.pinot.common.function.scalar.DataTypeConversionFunctions;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+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.core.query.request.context.ExpressionContext;
+
+
+public class SumWithPrecisionAggregationFunction extends BaseSingleInputAggregationFunction<BigDecimal, BigDecimal> {
+  MathContext _mathContext = new MathContext(0);

Review comment:
       Make these 2 variables `private final` because the aggregation function can be shared among multiple segments, and needs to be stateless.

##########
File path: pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,92 @@
+/**
+ * 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.common.function.scalar;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Base64;
+import org.apache.pinot.spi.annotations.ScalarFunction;
+
+
+public class DataTypeConversionFunctions {
+  private DataTypeConversionFunctions() {
+
+  }
+
+  @ScalarFunction
+  public static byte[] bigDecimalToBytes(BigDecimal number) {
+    int scale = number.scale();
+    BigInteger unscaled = number.unscaledValue();
+    byte[] value = unscaled.toByteArray();
+    byte[] bigDecimalBytesArray = new byte[value.length + 4];
+    for (int i = 0; i < 4; i++) {
+      bigDecimalBytesArray[i] = (byte) (scale >>> (8 * (3 - i)));
+    }

Review comment:
       Actually, after another thought, I don't think we need to use 4 bytes for the scale. Scale larger than 256 doesn't make lots of sense.

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/SumWithPrecisionAggregationFunction.java
##########
@@ -0,0 +1,174 @@
+/**
+ * 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.math.BigDecimal;
+import java.math.MathContext;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.AggregationFunctionType;
+import org.apache.pinot.common.function.scalar.DataTypeConversionFunctions;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.BlockValSet;
+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.core.query.request.context.ExpressionContext;
+
+
+public class SumWithPrecisionAggregationFunction extends BaseSingleInputAggregationFunction<BigDecimal, BigDecimal> {
+  MathContext _mathContext = new MathContext(0);
+  Integer _scale = null;
+
+  public SumWithPrecisionAggregationFunction(ExpressionContext expression, List<ExpressionContext> arguments) {
+    super(expression);

Review comment:
       ```suggestion
     public SumWithPrecisionAggregationFunction(List<ExpressionContext> arguments) {
       super(arguments.get(0));
   ```




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org