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



##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ArithmeticFunctions.java
##########
@@ -104,4 +106,46 @@ public static double ln(double a) {
   public static double sqrt(double a) {
     return Math.sqrt(a);
   }
+
+  @ScalarFunction
+  public static double sign(double a) {
+    return Math.signum(a);
+  }
+
+  @ScalarFunction
+  public static double power(double a, double b) {
+    return Math.pow(a, b);
+  }
+
+  @ScalarFunction
+  public static double log2(double a) {
+    return Math.log(a) / Math.log(2);
+  }
+
+  @ScalarFunction
+  public static double log10(double a) {
+    return Math.log10(a);
+  }
+
+  //TODO: The function should ideally be named 'round'
+  // but it is not possible because of existing DateTimeFunction with same 
name.
+  @ScalarFunction
+  public static double roundDecimal(double a, int b) {

Review comment:
       ^^

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/RoundDecimalTransformFunction.java
##########
@@ -0,0 +1,114 @@
+/**
+ * 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.operator.transform.function;
+
+import com.google.common.base.Preconditions;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+//TODO: The function should ideally be named 'round'
+// but it is not possible because of existing DateTimeFunction with same name.
+public class RoundDecimalTransformFunction extends BaseTransformFunction {
+  public static final String FUNCTION_NAME = "roundDecimal";
+  private TransformFunction _leftTransformFunction;
+  private TransformFunction _rightTransformFunction;
+  private int _scale;
+  private boolean _hasLiteralArg;

Review comment:
       (minor)
   ```suggestion
     private boolean _fixedScale;
   ```

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ArithmeticFunctions.java
##########
@@ -95,13 +97,55 @@ public static double exp(double a) {
     return Math.exp(a);
   }
 
-  @ScalarFunction
+  @ScalarFunction(names = {"ln", "log"})
   public static double ln(double a) {
     return Math.log(a);
   }
 
+  @ScalarFunction
+  public static double log2(double a) {
+    return Math.log(a) / Math.log(2);
+  }
+
+  @ScalarFunction
+  public static double log10(double a) {
+    return Math.log10(a);
+  }
+
   @ScalarFunction
   public static double sqrt(double a) {
     return Math.sqrt(a);
   }
+
+  @ScalarFunction
+  public static double sign(double a) {
+    return Math.signum(a);
+  }
+
+  @ScalarFunction

Review comment:
       To match presto
   ```suggestion
     @ScalarFunction(names = {"pow", "power"})
   ```

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/PowerTransformFunction.java
##########
@@ -0,0 +1,86 @@
+/**
+ * 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.operator.transform.function;
+
+import com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+
+
+public class PowerTransformFunction extends BaseTransformFunction {
+  public static final String FUNCTION_NAME = "power";
+  private TransformFunction _leftTransformFunction;
+  private TransformFunction _rightTransformFunction;
+  private double _exponent;
+  private boolean _hasLiteralArg;

Review comment:
       (minor)
   ```suggestion
     private boolean _fixedExponent;
   ```

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/RoundDecimalTransformFunction.java
##########
@@ -0,0 +1,114 @@
+/**
+ * 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.operator.transform.function;
+
+import com.google.common.base.Preconditions;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+//TODO: The function should ideally be named 'round'
+// but it is not possible because of existing DateTimeFunction with same name.
+public class RoundDecimalTransformFunction extends BaseTransformFunction {
+  public static final String FUNCTION_NAME = "roundDecimal";
+  private TransformFunction _leftTransformFunction;
+  private TransformFunction _rightTransformFunction;
+  private int _scale;
+  private boolean _hasLiteralArg;
+
+  @Override
+  public String getName() {
+    return FUNCTION_NAME;
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> 
dataSourceMap) {
+    int numArguments = arguments.size();
+    // Check that there are more than 2 arguments or no arguments
+    if (numArguments < 1 || numArguments > 2) {
+      throw new IllegalArgumentException(
+          "roundDecimal transform function supports either 1 or 2 arguments. 
Num arguments provided: " + numArguments);
+    }
+
+    _hasLiteralArg = false;
+    _leftTransformFunction = arguments.get(0);
+    if (numArguments > 1) {
+      _rightTransformFunction = arguments.get(1);
+      if (_rightTransformFunction instanceof LiteralTransformFunction) {
+        _scale = Integer.parseInt(((LiteralTransformFunction) 
_rightTransformFunction).getLiteral());
+        _hasLiteralArg = true;
+      }
+      Preconditions.checkArgument(
+          _rightTransformFunction.getResultMetadata().isSingleValue() && 
isIntegralResultDatatype(
+              _rightTransformFunction),
+          "Argument must be single-valued with type INT or LONG for transform 
function: %s", getName());
+    } else {
+      _rightTransformFunction = null;

Review comment:
       Can simplify the handling by setting `_scale = 0` and `_fixedScale = 
true`

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/ArithmeticFunctions.java
##########
@@ -104,4 +106,46 @@ public static double ln(double a) {
   public static double sqrt(double a) {
     return Math.sqrt(a);
   }
+
+  @ScalarFunction
+  public static double sign(double a) {
+    return Math.signum(a);
+  }
+
+  @ScalarFunction
+  public static double power(double a, double b) {
+    return Math.pow(a, b);
+  }
+
+  @ScalarFunction
+  public static double log2(double a) {
+    return Math.log(a) / Math.log(2);
+  }
+
+  @ScalarFunction
+  public static double log10(double a) {
+    return Math.log10(a);
+  }
+
+  //TODO: The function should ideally be named 'round'
+  // but it is not possible because of existing DateTimeFunction with same 
name.
+  @ScalarFunction
+  public static double roundDecimal(double a, int b) {
+    return BigDecimal.valueOf(a).setScale(b, 
RoundingMode.HALF_UP).doubleValue();
+  }
+
+  @ScalarFunction
+  public static double roundDecimal(double a) {
+    return BigDecimal.valueOf(a).setScale(0, 
RoundingMode.HALF_UP).doubleValue();

Review comment:
       ^^




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to