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



##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/GenericTransformFunction.java
##########
@@ -0,0 +1,170 @@
+/**
+ * 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.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.FunctionInfo;
+import org.apache.pinot.common.function.FunctionInvoker;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public class GenericTransformFunction extends BaseTransformFunction {
+
+  private FunctionInfo _info;
+  FunctionInvoker _functionInvoker;
+  String _name;
+  Object[] _args;
+  List<Integer> _nonLiteralArgIndices;
+  List<FieldSpec.DataType> _nonLiteralArgType;
+  List<TransformFunction> _nonLiteralTransformFunction;
+  String[] _stringResult;
+
+  public GenericTransformFunction() {
+    _nonLiteralArgIndices = new ArrayList<>();
+    _nonLiteralArgType = new ArrayList<>();
+    _nonLiteralTransformFunction = new ArrayList<>();
+  }
+
+  @Override
+  public String getName() {
+    return _name;
+  }
+
+  public void setFunction(String functionName, FunctionInfo info)
+      throws Exception {
+    _name = functionName;
+    _info = info;
+    _functionInvoker = new FunctionInvoker(info);
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> 
dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 
_functionInvoker.getParameterTypes().length,
+        "The number of arguments are not same for scalar function and 
transform function: %s", getName());
+
+    _args = new Object[arguments.size()];
+    for (int i = 0; i < arguments.size(); i++) {
+      TransformFunction function = arguments.get(i);
+      if (function instanceof LiteralTransformFunction) {
+        String literal = ((LiteralTransformFunction) function).getLiteral();
+        Class paramType = _functionInvoker.getParameterTypes()[i];
+        switch (paramType.getTypeName()) {
+          case "java.lang.Integer":
+            _args[i] = Integer.parseInt(literal);
+            break;
+          case "java.lang.String":
+            _args[i] = literal;
+            break;
+          case "java.lang.Double":
+            _args[i] = Double.valueOf(literal);
+            break;
+          case "java.lang.Long":
+            _args[i] = Long.valueOf(literal);
+            break;
+          default:
+            throw new RuntimeException(
+                "Unsupported data type " + paramType.getTypeName() + "for 
transform function " + getName());
+        }
+      } else {
+        _nonLiteralArgIndices.add(i);
+        _nonLiteralTransformFunction.add(function);
+        Class paramType = _functionInvoker.getParameterTypes()[i];
+
+        switch (paramType.getTypeName()) {
+          case "java.lang.Integer":
+            _nonLiteralArgType.add(FieldSpec.DataType.INT);
+            break;
+          case "java.lang.String":
+            _nonLiteralArgType.add(FieldSpec.DataType.STRING);
+            break;
+          case "java.lang.Double":
+            _nonLiteralArgType.add(FieldSpec.DataType.DOUBLE);
+            break;
+          case "java.lang.Long":
+            _nonLiteralArgType.add(FieldSpec.DataType.LONG);
+            break;
+          default:
+            throw new RuntimeException(
+                "Unsupported data type " + paramType.getTypeName() + "for 
transform function " + getName());
+        }
+      }
+    }
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    return STRING_SV_NO_DICTIONARY_METADATA;

Review comment:
       You are right, we need to override methods for int, long, double, float. 
Shouldn't be hard, its mostly copy-paste. similar to _stringResultArray, create 
a resultArray for each type and instantiate them in the init

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/GenericTransformFunction.java
##########
@@ -0,0 +1,265 @@
+/**
+ * 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.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.FunctionInfo;
+import org.apache.pinot.common.function.FunctionInvoker;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public class GenericTransformFunction extends BaseTransformFunction {
+
+  private FunctionInfo _info;
+  FunctionInvoker _functionInvoker;
+  String _name;
+  Object[] _args;
+  List<Integer> _nonLiteralArgIndices;
+  List<FieldSpec.DataType> _nonLiteralArgType;
+  List<TransformFunction> _nonLiteralTransformFunction;
+  String[] _stringResult;
+  int[] _integerResult;
+  float[] _floatResult;
+  double[] _doubleResult;
+  long[] _longResult;
+
+  public GenericTransformFunction() {
+    _nonLiteralArgIndices = new ArrayList<>();
+    _nonLiteralArgType = new ArrayList<>();
+    _nonLiteralTransformFunction = new ArrayList<>();
+  }
+
+  @Override
+  public String getName() {
+    return _name;
+  }
+
+  public void setFunction(String functionName, FunctionInfo info)
+      throws Exception {
+    _name = functionName;
+    _info = info;
+    _functionInvoker = new FunctionInvoker(info);
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> 
dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 
_functionInvoker.getParameterTypes().length,
+        "The number of arguments are not same for scalar function and 
transform function: %s", getName());
+
+    _args = new Object[arguments.size()];
+    for (int i = 0; i < arguments.size(); i++) {
+      TransformFunction function = arguments.get(i);
+      if (function instanceof LiteralTransformFunction) {
+        String literal = ((LiteralTransformFunction) function).getLiteral();
+        Class paramType = _functionInvoker.getParameterTypes()[i];
+        switch (paramType.getTypeName()) {
+          case "java.lang.Integer":
+            _args[i] = Integer.parseInt(literal);
+            break;
+          case "java.lang.String":
+            _args[i] = literal;
+            break;
+          case "java.lang.Double":
+            _args[i] = Double.valueOf(literal);
+            break;
+          case "java.lang.Float":
+            _args[i] = Float.valueOf(literal);
+            break;
+          case "java.lang.Long":
+            _args[i] = Long.valueOf(literal);
+            break;
+          default:
+            throw new RuntimeException(
+                "Unsupported data type " + paramType.getTypeName() + "for 
transform function " + getName());
+        }
+      } else {
+        _nonLiteralArgIndices.add(i);
+        _nonLiteralTransformFunction.add(function);
+        Class paramType = _functionInvoker.getParameterTypes()[i];
+
+        switch (paramType.getTypeName()) {
+          case "java.lang.Integer":
+            _nonLiteralArgType.add(FieldSpec.DataType.INT);
+            break;
+          case "java.lang.String":
+            _nonLiteralArgType.add(FieldSpec.DataType.STRING);
+            break;
+          case "java.lang.Double":
+            _nonLiteralArgType.add(FieldSpec.DataType.DOUBLE);
+            break;
+          case "java.lang.Float":
+            _nonLiteralArgType.add(FieldSpec.DataType.FLOAT);
+            break;
+          case "java.lang.Long":
+            _nonLiteralArgType.add(FieldSpec.DataType.LONG);
+            break;
+          default:
+            throw new RuntimeException(
+                "Unsupported data type " + paramType.getTypeName() + "for 
transform function " + getName());
+        }
+      }
+    }
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    Class returnType = _functionInvoker.getReturnType();
+    switch(returnType.getTypeName()) {
+      case "java.lang.Integer": return INT_SV_NO_DICTIONARY_METADATA;
+      case "java.lang.Long": return LONG_SV_NO_DICTIONARY_METADATA;
+      case "java.lang.Float": return DOUBLE_SV_NO_DICTIONARY_METADATA;
+      case "java.lang.Double": return DOUBLE_SV_NO_DICTIONARY_METADATA;
+      case "java.lang.String": return STRING_SV_NO_DICTIONARY_METADATA;
+      default:
+        throw new RuntimeException("Unsupported data type " + 
returnType.getTypeName() + "for transform function " + getName());
+    }
+  }
+
+  @SuppressWarnings("Duplicates")
+  @Override
+  public int[] transformToIntValuesSV(ProjectionBlock projectionBlock) {
+    if (_integerResult == null) {
+      _integerResult = new int[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    transformValues(projectionBlock);
+    return _integerResult;
+  }
+
+  @SuppressWarnings("Duplicates")
+  @Override
+  public long[] transformToLongValuesSV(ProjectionBlock projectionBlock) {
+    if (_longResult == null) {
+      _longResult = new long[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    transformValues(projectionBlock);
+    return _longResult;
+  }
+
+  @SuppressWarnings("Duplicates")
+  @Override
+  public float[] transformToFloatValuesSV(ProjectionBlock projectionBlock) {
+    if (_floatResult == null) {
+      _floatResult = new float[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    transformValues(projectionBlock);
+    return _floatResult;
+  }
+
+  @SuppressWarnings("Duplicates")
+  @Override
+  public double[] transformToDoubleValuesSV(ProjectionBlock projectionBlock) {
+    if (_doubleResult == null) {
+      _doubleResult = new double[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    transformValues(projectionBlock);
+    return _doubleResult;
+  }
+
+  @SuppressWarnings("Duplicates")
+  @Override
+  public String[] transformToStringValuesSV(ProjectionBlock projectionBlock) {
+    if (_stringResult == null) {
+      _stringResult = new String[DocIdSetPlanNode.MAX_DOC_PER_CALL];
+    }
+    transformValues(projectionBlock);
+    return _stringResult;
+  }
+
+  private void transformValues(ProjectionBlock projectionBlock) {
+    int length = projectionBlock.getNumDocs();
+    int numNonLiteralArgs = _nonLiteralArgIndices.size();
+    Object[][] nonLiteralBlockValues = new Object[numNonLiteralArgs][];
+
+    transformNonLiteralArgsToValues(projectionBlock, numNonLiteralArgs, 
nonLiteralBlockValues);
+
+    //now invoke the actual function
+    for (int i = 0; i < length; i++) {
+      for (int k = 0; k < numNonLiteralArgs; k++) {
+        _args[_nonLiteralArgIndices.get(k)] = nonLiteralBlockValues[k][i];
+      }
+
+      Class returnType = _functionInvoker.getReturnType();

Review comment:
       it's better to duplicate this for every type event though this code 
looks better for readability.
   
   the switch case on every invocation will hurt perf

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/GenericTransformFunction.java
##########
@@ -0,0 +1,265 @@
+/**
+ * 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.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.FunctionInfo;
+import org.apache.pinot.common.function.FunctionInvoker;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public class GenericTransformFunction extends BaseTransformFunction {
+
+  private FunctionInfo _info;
+  FunctionInvoker _functionInvoker;
+  String _name;
+  Object[] _args;
+  List<Integer> _nonLiteralArgIndices;
+  List<FieldSpec.DataType> _nonLiteralArgType;
+  List<TransformFunction> _nonLiteralTransformFunction;
+  String[] _stringResult;
+  int[] _integerResult;
+  float[] _floatResult;
+  double[] _doubleResult;
+  long[] _longResult;
+
+  public GenericTransformFunction() {
+    _nonLiteralArgIndices = new ArrayList<>();
+    _nonLiteralArgType = new ArrayList<>();
+    _nonLiteralTransformFunction = new ArrayList<>();
+  }
+
+  @Override
+  public String getName() {
+    return _name;
+  }
+
+  public void setFunction(String functionName, FunctionInfo info)
+      throws Exception {
+    _name = functionName;
+    _info = info;
+    _functionInvoker = new FunctionInvoker(info);
+  }
+
+  @Override
+  public void init(List<TransformFunction> arguments, Map<String, DataSource> 
dataSourceMap) {
+    Preconditions.checkArgument(arguments.size() == 
_functionInvoker.getParameterTypes().length,
+        "The number of arguments are not same for scalar function and 
transform function: %s", getName());
+
+    _args = new Object[arguments.size()];
+    for (int i = 0; i < arguments.size(); i++) {
+      TransformFunction function = arguments.get(i);
+      if (function instanceof LiteralTransformFunction) {
+        String literal = ((LiteralTransformFunction) function).getLiteral();
+        Class paramType = _functionInvoker.getParameterTypes()[i];
+        switch (paramType.getTypeName()) {
+          case "java.lang.Integer":
+            _args[i] = Integer.parseInt(literal);
+            break;
+          case "java.lang.String":
+            _args[i] = literal;
+            break;
+          case "java.lang.Double":
+            _args[i] = Double.valueOf(literal);
+            break;
+          case "java.lang.Float":
+            _args[i] = Float.valueOf(literal);
+            break;
+          case "java.lang.Long":
+            _args[i] = Long.valueOf(literal);
+            break;
+          default:
+            throw new RuntimeException(
+                "Unsupported data type " + paramType.getTypeName() + "for 
transform function " + getName());
+        }
+      } else {
+        _nonLiteralArgIndices.add(i);
+        _nonLiteralTransformFunction.add(function);
+        Class paramType = _functionInvoker.getParameterTypes()[i];
+
+        switch (paramType.getTypeName()) {
+          case "java.lang.Integer":
+            _nonLiteralArgType.add(FieldSpec.DataType.INT);
+            break;
+          case "java.lang.String":
+            _nonLiteralArgType.add(FieldSpec.DataType.STRING);
+            break;
+          case "java.lang.Double":
+            _nonLiteralArgType.add(FieldSpec.DataType.DOUBLE);
+            break;
+          case "java.lang.Float":
+            _nonLiteralArgType.add(FieldSpec.DataType.FLOAT);
+            break;
+          case "java.lang.Long":
+            _nonLiteralArgType.add(FieldSpec.DataType.LONG);
+            break;
+          default:
+            throw new RuntimeException(
+                "Unsupported data type " + paramType.getTypeName() + "for 
transform function " + getName());
+        }
+      }
+    }
+  }
+
+  @Override
+  public TransformResultMetadata getResultMetadata() {
+    Class returnType = _functionInvoker.getReturnType();

Review comment:
       better to do this in init and save the metadata, Pinot might call this 
function multiple times

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/GenericTransformFunction.java
##########
@@ -0,0 +1,265 @@
+/**
+ * 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.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.function.FunctionInfo;
+import org.apache.pinot.common.function.FunctionInvoker;
+import org.apache.pinot.core.common.DataSource;
+import org.apache.pinot.core.operator.blocks.ProjectionBlock;
+import org.apache.pinot.core.operator.transform.TransformResultMetadata;
+import org.apache.pinot.core.plan.DocIdSetPlanNode;
+import org.apache.pinot.spi.data.FieldSpec;
+
+
+public class GenericTransformFunction extends BaseTransformFunction {
+
+  private FunctionInfo _info;
+  FunctionInvoker _functionInvoker;
+  String _name;
+  Object[] _args;
+  List<Integer> _nonLiteralArgIndices;
+  List<FieldSpec.DataType> _nonLiteralArgType;
+  List<TransformFunction> _nonLiteralTransformFunction;
+  String[] _stringResult;
+  int[] _integerResult;
+  float[] _floatResult;
+  double[] _doubleResult;
+  long[] _longResult;
+
+  public GenericTransformFunction() {
+    _nonLiteralArgIndices = new ArrayList<>();
+    _nonLiteralArgType = new ArrayList<>();
+    _nonLiteralTransformFunction = new ArrayList<>();
+  }
+
+  @Override
+  public String getName() {
+    return _name;
+  }
+
+  public void setFunction(String functionName, FunctionInfo info)

Review comment:
       should we move this to constructor @mayankshriv @Jackie-Jiang 

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/TransformFunctionFactory.java
##########
@@ -112,13 +115,24 @@ public static TransformFunction 
get(TransformExpressionTree expression, Map<Stri
     switch (expression.getExpressionType()) {
       case FUNCTION:
         String functionName = expression.getValue();
-        Class<? extends TransformFunction> transformFunctionClass = 
TRANSFORM_FUNCTION_MAP.get(functionName);
+        Class<? extends TransformFunction> transformFunctionClass;
+        FunctionInfo functionInfo = null;
+        if (FunctionRegistry.containsFunctionByName(functionName)) {
+          transformFunctionClass = GenericTransformFunction.class;
+          functionInfo = FunctionRegistry.getFunctionByName(functionName);

Review comment:
       its a wrapper for all functions in FunctionRegistry. what name do you 
suggest - ScalarTransformFunctionWrapper 




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