You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/01/17 16:07:16 UTC

[GitHub] [flink] dawidwys commented on a change in pull request #10878: [FLINK-15599][table] SQL client requires both legacy and blink planner to be on the classpath

dawidwys commented on a change in pull request #10878: [FLINK-15599][table] SQL client requires both legacy and blink planner to be on the classpath
URL: https://github.com/apache/flink/pull/10878#discussion_r368012192
 
 

 ##########
 File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/FunctionService.java
 ##########
 @@ -0,0 +1,184 @@
+/*
+ * 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.flink.table.functions;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.descriptors.ClassInstanceValidator;
+import org.apache.flink.table.descriptors.DescriptorProperties;
+import org.apache.flink.table.descriptors.FunctionDescriptor;
+import org.apache.flink.table.descriptors.FunctionDescriptorValidator;
+import org.apache.flink.table.descriptors.HierarchyDescriptorValidator;
+import org.apache.flink.table.descriptors.LiteralValueValidator;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Service for creating configured instances of {@link UserDefinedFunction} using a
+ * {@link FunctionDescriptor}.
+ */
+public class FunctionService {
+
+	private static final Logger LOG = LoggerFactory.getLogger(FunctionService.class);
+
+	/**
+	 * Creates a user-defined function with the given properties and the current thread's
+	 * context class loader.
+	 *
+	 * @param descriptor the descriptor that describes a function
+	 * @return the generated user-defined function
+	 */
+	public static UserDefinedFunction createFunction(FunctionDescriptor descriptor) {
+		return createFunction(descriptor, Thread.currentThread().getContextClassLoader());
+	}
+
+	/**
+	 * Creates a user-defined function with the given properties.
+	 *
+	 * @param descriptor the descriptor that describes a function
+	 * @param classLoader the class loader to load the function and its parameter's classes
+	 * @return the generated user-defined function
+	 */
+	public static UserDefinedFunction createFunction(
+			FunctionDescriptor descriptor,
+			ClassLoader classLoader) {
+		return createFunction(descriptor, classLoader, true);
+	}
+
+	/**
+	 * Creates a user-defined function with the given properties.
+	 *
+	 * @param descriptor the descriptor that describes a function
+	 * @param classLoader the class loader to load the function and its parameter's classes
+	 * @param performValidation whether or not the descriptor should be validated
+	 * @return the generated user-defined function
+	 */
+	public static UserDefinedFunction createFunction(
+			FunctionDescriptor descriptor,
+			ClassLoader classLoader,
+			boolean performValidation) {
+
+		DescriptorProperties properties = new DescriptorProperties(true);
+		properties.putProperties(descriptor.toProperties());
+
+		// validate
+		if (performValidation) {
+			new FunctionDescriptorValidator().validate(properties);
+		}
+
+		// instantiate
+		Tuple2<Class<Object>, Object> tuple2 = generateInstance(
+				HierarchyDescriptorValidator.EMPTY_PREFIX,
+				properties,
+				classLoader);
+
+		if (!UserDefinedFunction.class.isAssignableFrom(tuple2.f0)) {
+			throw new ValidationException(
+					String.format("Instantiated class '%s' is not a user-defined function.", tuple2.f0.getName()));
+		}
+		return (UserDefinedFunction) tuple2.f1;
+	}
+
+	/**
+	 * Recursively generate an instance of a class according the given properties.
+	 *
+	 * @param keyPrefix the prefix to fetch properties
+	 * @param descriptorProperties the descriptor properties that contains the class type information
+	 * @param classLoader the class loader to load the class
+	 * @param <T> type fo the generated instance
+	 * @return an instance of the class
+	 */
+	private static <T> Tuple2<Class<T>, T> generateInstance(
 
 Review comment:
   Do we need to return the class separately? Can't we just call the `.getClass` on the instance whenever necessary?
   
   If it is necessary can we at least have a specialized pojo for that rather than returning a Pojo? But I really believer we can just return the instance.

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


With regards,
Apache Git Services