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



##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,81 @@
+/**
+ * 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 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) {

Review comment:
       rename to bytesToBigDecimalHex?

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,81 @@
+/**
+ * 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 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();

Review comment:
       whats is happening inside number.toString? is it possible for us to 
generate the hex string without having to create big integer and big decimal?

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AggregationFunctionFactory.java
##########
@@ -117,6 +117,18 @@ public static AggregationFunction 
getAggregationFunction(FunctionContext functio
             return new MaxAggregationFunction(firstArgument);
           case SUM:
             return new SumAggregationFunction(firstArgument);
+          case SUMPRECISION:
+            int numArguments = arguments.size();
+            if (numArguments == 3) {

Review comment:
       its probably better to move this logic into SumPrecission(arguments) 
constructor. You can still keep the other two constructors

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/DataTypeConversionFunctions.java
##########
@@ -0,0 +1,81 @@
+/**
+ * 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 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) {

Review comment:
       while you are there can you also add base64encode decode?

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/common/ObjectSerDeUtils.java
##########
@@ -776,31 +781,30 @@ public IdSet deserialize(ByteBuffer byteBuffer) {
     }
   };
 
+  public static final ObjectSerDe<BigDecimal> BIGDECIMAL_SER_DE = new 
ObjectSerDe<BigDecimal>() {
+
+    @Override
+    public byte[] serialize(BigDecimal value) {
+      return DataTypeConversionFunctions.bigDecimalToBytes(value);
+    }
+
+    @Override
+    public BigDecimal deserialize(byte[] bytes) {
+      return new 
BigDecimal(DataTypeConversionFunctions.bytesToBigDecimal(bytes));
+    }
+
+    @Override
+    public BigDecimal deserialize(ByteBuffer byteBuffer) {
+      byte[] bytes = new byte[byteBuffer.remaining()];
+      byteBuffer.get(bytes);
+      return deserialize(bytes);
+    }
+  };
+
   // NOTE: DO NOT change the order, it has to be the same order as the 
ObjectType
   //@formatter:off
-  private static final ObjectSerDe[] SER_DES = {
-      STRING_SER_DE,
-      LONG_SER_DE,
-      DOUBLE_SER_DE,
-      DOUBLE_ARRAY_LIST_SER_DE,
-      AVG_PAIR_SER_DE,
-      MIN_MAX_RANGE_PAIR_SER_DE,
-      HYPER_LOG_LOG_SER_DE,
-      QUANTILE_DIGEST_SER_DE,
-      MAP_SER_DE,
-      INT_SET_SER_DE,
-      TDIGEST_SER_DE,
-      DISTINCT_TABLE_SER_DE,
-      DATA_SKETCH_SER_DE,
-      GEOMETRY_SER_DE,
-      ROARING_BITMAP_SER_DE,
-      LONG_SET_SER_DE,
-      FLOAT_SET_SER_DE,
-      DOUBLE_SET_SER_DE,
-      STRING_SET_SER_DE,
-      BYTES_SET_SER_DE,
-      ID_SET_SER_DE
-  };
+  private static final ObjectSerDe[] SER_DES =

Review comment:
       check your IDE pref to honor @formatter:off annotation

##########
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.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, 
Integer precision) {
+    super(expression);
+    _mathContext = new MathContext(precision);
+  }
+
+  public SumWithPrecisionAggregationFunction(ExpressionContext expression, 
Integer precision, Integer scale) {
+    super(expression);
+    _mathContext = new MathContext(precision);
+    _scale = scale;
+  }
+
+  public SumWithPrecisionAggregationFunction(ExpressionContext expression) {
+    super(expression);
+  }
+
+  @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));
+  }
+
+  @Override
+  public void aggregateGroupBySV(int length, int[] groupKeyArray, 
GroupByResultHolder groupByResultHolder,
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    byte[][] valueArray = blockValSetMap.get(_expression).getBytesValuesSV();
+    for (int i = 0; i < length; i++) {
+      int groupKey = groupKeyArray[i];
+      BigDecimal groupByResultValue = getDefaultResult(groupByResultHolder, 
groupKey);
+      BigDecimal value = new 
BigDecimal(DataTypeConversionFunctions.bytesToBigDecimal(valueArray[i]));
+      groupByResultValue = groupByResultValue.add(value, _mathContext);
+      groupByResultHolder.setValueForKey(groupKey, 
setScale(groupByResultValue));
+    }
+  }
+
+  @Override
+  public void aggregateGroupByMV(int length, int[][] groupKeysArray, 
GroupByResultHolder groupByResultHolder,
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    byte[][] valueArray = blockValSetMap.get(_expression).getBytesValuesSV();
+    for (int i = 0; i < length; i++) {
+      byte[] value = valueArray[i];
+      for (int groupKey : groupKeysArray[i]) {
+        BigDecimal groupByResultValue = getDefaultResult(groupByResultHolder, 
groupKey);
+        BigDecimal valueBigDecimal = new 
BigDecimal(DataTypeConversionFunctions.bytesToBigDecimal(value));
+        groupByResultValue = groupByResultValue.add(valueBigDecimal, 
_mathContext);
+        groupByResultHolder.setValueForKey(groupKey, 
setScale(groupByResultValue));
+      }
+    }
+  }
+
+  @Override
+  public BigDecimal extractAggregationResult(AggregationResultHolder 
aggregationResultHolder) {
+    return getDefaultResult(aggregationResultHolder);
+  }
+
+  @Override
+  public BigDecimal extractGroupByResult(GroupByResultHolder 
groupByResultHolder, int groupKey) {
+    return getDefaultResult(groupByResultHolder, groupKey);
+  }
+
+  @Override
+  public BigDecimal merge(BigDecimal intermediateResult1, BigDecimal 
intermediateResult2) {
+    try {
+      return setScale(intermediateResult1.add(intermediateResult2, 
_mathContext));
+    } catch (Exception e) {
+      throw new RuntimeException("Caught Exception while merging results in 
sum with precision function", e);
+    }
+  }
+
+  @Override
+  public boolean isIntermediateResultComparable() {
+    return true;
+  }
+
+  @Override
+  public DataSchema.ColumnDataType getIntermediateResultColumnType() {
+    return DataSchema.ColumnDataType.OBJECT;
+  }
+
+  @Override
+  public DataSchema.ColumnDataType getFinalResultColumnType() {
+    return DataSchema.ColumnDataType.STRING;
+  }
+
+  @Override
+  public BigDecimal extractFinalResult(BigDecimal intermediateResult) {
+    return intermediateResult;
+  }
+
+  public BigDecimal getDefaultResult(AggregationResultHolder 
aggregationResultHolder) {
+    BigDecimal result = aggregationResultHolder.getResult();
+    if (result == null) {
+      result = new BigDecimal(0, _mathContext);
+      aggregationResultHolder.setValue(result);
+    }
+    result = setScale(result);
+    return result;
+  }
+
+  public BigDecimal getDefaultResult(GroupByResultHolder groupByResultHolder, 
int groupKey) {
+    BigDecimal result = groupByResultHolder.getResult(groupKey);
+    if (result == null) {
+      result = new BigDecimal(0, _mathContext);
+      groupByResultHolder.setValueForKey(groupKey, result);
+    }
+    result = setScale(result);
+    return result;
+  }
+
+  private BigDecimal setScale(BigDecimal value) {

Review comment:
       this method is bit confusing, what is it trying to do. add some javadocs 
and maybe rename the function if possible

##########
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.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, 
Integer precision) {

Review comment:
       if users dont use scale, will the results vary depending on the order of 
segment execution?




----------------------------------------------------------------
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:
us...@infra.apache.org



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

Reply via email to