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/05/25 08:26:43 UTC

[GitHub] [incubator-pinot] kishoreg commented on a change in pull request #5440: Add UDFs for String Transformation

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



##########
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":

Review comment:
       missing float

##########
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 can add another switch statement in init to set this based on the return type of the scalarfunction, see functionInvoker.getReturnType




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