Unverified Commit 21c1dec9 authored by Licho's avatar Licho Committed by GitHub

feat: add official `add jar` sql syntax, and rename custom `add jar` to `add customJar` (#1927)

* feat: add official add jar sql syntax * Spotless Apply * refact Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * refactor: add log add reduce code Signed-off-by: 's avatarlicho <lecho.sun@gmail.com> * Spotless Apply * fix: unit test Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * feat: add configure combination initialize Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply * fix: adapter 1.13 * spotless * fix: configure import * Spotless Apply * fix: flink 1.14 adaptically * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Signed-off-by: 's avatarlicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent c9f0ba4f
...@@ -70,6 +70,7 @@ import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram; ...@@ -70,6 +70,7 @@ import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.planner.utils.ExecutorUtils; import org.apache.flink.table.planner.utils.ExecutorUtils;
import org.apache.flink.table.typeutils.FieldInfoUtils; import org.apache.flink.table.typeutils.FieldInfoUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -81,6 +82,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; ...@@ -81,6 +82,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import cn.hutool.core.util.ReflectUtil;
/** /**
* 定制TableEnvironmentImpl * 定制TableEnvironmentImpl
* *
...@@ -331,6 +334,20 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -331,6 +334,20 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
return false; return false;
} }
@Override
public Configuration getRootConfiguration() {
Method method =
ReflectUtil.getMethod(
this.getStreamExecutionEnvironment().getClass(), "getConfiguration");
ReflectUtil.setAccessible(method);
try {
Object object = method.invoke(this.getStreamExecutionEnvironment());
return (Configuration) object;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private void callSet( private void callSet(
SetOperation setOperation, SetOperation setOperation,
StreamExecutionEnvironment environment, StreamExecutionEnvironment environment,
......
...@@ -69,6 +69,7 @@ import org.apache.flink.table.planner.delegation.DefaultExecutor; ...@@ -69,6 +69,7 @@ import org.apache.flink.table.planner.delegation.DefaultExecutor;
import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram; import org.apache.flink.table.planner.plan.optimize.program.FlinkChainedProgram;
import org.apache.flink.table.typeutils.FieldInfoUtils; import org.apache.flink.table.typeutils.FieldInfoUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -80,6 +81,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; ...@@ -80,6 +81,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import cn.hutool.core.util.ReflectUtil;
/** /**
* 定制TableEnvironmentImpl * 定制TableEnvironmentImpl
* *
...@@ -184,6 +187,20 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -184,6 +187,20 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
classLoader); classLoader);
} }
@Override
public Configuration getRootConfiguration() {
Method method =
ReflectUtil.getMethod(
this.getStreamExecutionEnvironment().getClass(), "getConfiguration");
ReflectUtil.setAccessible(method);
try {
Object object = method.invoke(this.getStreamExecutionEnvironment());
return (Configuration) object;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
private static Executor lookupExecutor( private static Executor lookupExecutor(
ClassLoader classLoader, ClassLoader classLoader,
String executorIdentifier, String executorIdentifier,
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.dinky.executor; package org.dinky.executor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.TableEnvironment; import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
...@@ -51,4 +52,9 @@ public abstract class AbstractCustomTableEnvironment ...@@ -51,4 +52,9 @@ public abstract class AbstractCustomTableEnvironment
public Planner getPlanner() { public Planner getPlanner() {
return ((StreamTableEnvironmentImpl) streamTableEnvironment).getPlanner(); return ((StreamTableEnvironmentImpl) streamTableEnvironment).getPlanner();
} }
@Override
public Configuration getRootConfiguration() {
return (Configuration) this.getConfig().getRootConfiguration();
}
} }
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.dinky.executor; package org.dinky.executor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.TableEnvironment; import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
...@@ -71,4 +72,9 @@ public abstract class AbstractCustomTableEnvironment ...@@ -71,4 +72,9 @@ public abstract class AbstractCustomTableEnvironment
ReflectUtil.setFieldValue( ReflectUtil.setFieldValue(
getPlanner(), "extendedOperationExecutor", extendedOperationExecutor); getPlanner(), "extendedOperationExecutor", extendedOperationExecutor);
} }
@Override
public Configuration getRootConfiguration() {
return (Configuration) this.getConfig().getRootConfiguration();
}
} }
...@@ -22,6 +22,7 @@ package org.dinky.executor; ...@@ -22,6 +22,7 @@ package org.dinky.executor;
import org.dinky.model.LineageRel; import org.dinky.model.LineageRel;
import org.dinky.result.SqlExplainResult; import org.dinky.result.SqlExplainResult;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.jobgraph.JobGraph; import org.apache.flink.runtime.jobgraph.JobGraph;
import org.apache.flink.runtime.rest.messages.JobPlanInfo; import org.apache.flink.runtime.rest.messages.JobPlanInfo;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
...@@ -61,6 +62,8 @@ public interface CustomTableEnvironment ...@@ -61,6 +62,8 @@ public interface CustomTableEnvironment
Planner getPlanner(); Planner getPlanner();
Configuration getRootConfiguration();
default List<LineageRel> getLineage(String statement) { default List<LineageRel> getLineage(String statement) {
return null; return null;
} }
......
...@@ -23,6 +23,7 @@ import org.dinky.assertion.Asserts; ...@@ -23,6 +23,7 @@ import org.dinky.assertion.Asserts;
import org.dinky.constant.FlinkSQLConstant; import org.dinky.constant.FlinkSQLConstant;
import org.dinky.context.DinkyClassLoaderContextHolder; import org.dinky.context.DinkyClassLoaderContextHolder;
import org.dinky.context.JarPathContextHolder; import org.dinky.context.JarPathContextHolder;
import org.dinky.executor.CustomTableEnvironment;
import org.dinky.executor.Executor; import org.dinky.executor.Executor;
import org.dinky.explainer.watchTable.WatchStatementExplainer; import org.dinky.explainer.watchTable.WatchStatementExplainer;
import org.dinky.function.data.model.UDF; import org.dinky.function.data.model.UDF;
...@@ -45,6 +46,8 @@ import org.dinky.utils.LogUtil; ...@@ -45,6 +46,8 @@ import org.dinky.utils.LogUtil;
import org.dinky.utils.SqlUtil; import org.dinky.utils.SqlUtil;
import org.dinky.utils.URLUtils; import org.dinky.utils.URLUtils;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.runtime.rest.messages.JobPlanInfo; import org.apache.flink.runtime.rest.messages.JobPlanInfo;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -113,6 +116,11 @@ public class Explainer { ...@@ -113,6 +116,11 @@ public class Explainer {
.forEach(JarPathContextHolder::addOtherPlugins); .forEach(JarPathContextHolder::addOtherPlugins);
DinkyClassLoaderContextHolder.get() DinkyClassLoaderContextHolder.get()
.addURL(URLUtils.getURLs(JarPathContextHolder.getOtherPluginsFiles())); .addURL(URLUtils.getURLs(JarPathContextHolder.getOtherPluginsFiles()));
} else if (operationType.equals(SqlType.ADD_JAR)) {
Configuration combinationConfig = getCombinationConfig();
FileSystem.initialize(combinationConfig, null);
ddl.add(new StatementParam(statement, operationType));
statementList.add(statement);
} else if (operationType.equals(SqlType.INSERT) } else if (operationType.equals(SqlType.INSERT)
|| operationType.equals(SqlType.SELECT) || operationType.equals(SqlType.SELECT)
|| operationType.equals(SqlType.SHOW) || operationType.equals(SqlType.SHOW)
...@@ -148,6 +156,16 @@ public class Explainer { ...@@ -148,6 +156,16 @@ public class Explainer {
return new JobParam(statementList, ddl, trans, execute, CollUtil.removeNull(udfList)); return new JobParam(statementList, ddl, trans, execute, CollUtil.removeNull(udfList));
} }
private Configuration getCombinationConfig() {
CustomTableEnvironment cte = executor.getCustomTableEnvironment();
Configuration rootConfig = cte.getRootConfiguration();
Configuration config = cte.getConfig().getConfiguration();
Configuration combinationConfig = new Configuration();
combinationConfig.addAll(rootConfig);
combinationConfig.addAll(config);
return combinationConfig;
}
public List<UDF> parseUDFFromStatements(String[] statements) { public List<UDF> parseUDFFromStatements(String[] statements) {
List<UDF> udfList = new ArrayList<>(); List<UDF> udfList = new ArrayList<>();
for (String statement : statements) { for (String statement : statements) {
...@@ -156,7 +174,7 @@ public class Explainer { ...@@ -156,7 +174,7 @@ public class Explainer {
} }
UDF udf = UDFUtil.toUDF(statement); UDF udf = UDFUtil.toUDF(statement);
if (Asserts.isNotNull(udf)) { if (Asserts.isNotNull(udf)) {
udfList.add(UDFUtil.toUDF(statement)); udfList.add(udf);
} }
} }
return udfList; return udfList;
......
...@@ -56,8 +56,8 @@ public enum SqlType { ...@@ -56,8 +56,8 @@ public enum SqlType {
RESET("RESET", "^RESET.*"), RESET("RESET", "^RESET.*"),
EXECUTE("EXECUTE", "^EXECUTE.*"), EXECUTE("EXECUTE", "^EXECUTE.*"),
ADD_JAR("ADD_JAR", "^ADD\\s+JAR\\s+\\S+"),
ADD("ADD", "^ADD.*"), ADD("ADD", "^ADD\\s+CUSTOMJAR\\s+\\S+"),
WATCH("WATCH", "^WATCH.*"), WATCH("WATCH", "^WATCH.*"),
......
...@@ -36,7 +36,7 @@ import cn.hutool.core.util.StrUtil; ...@@ -36,7 +36,7 @@ import cn.hutool.core.util.StrUtil;
/** @since 0.7.0 */ /** @since 0.7.0 */
public class AddJarSqlParser { public class AddJarSqlParser {
private static final String ADD_JAR = "(add\\s+jar)\\s+'(.*.jar)'"; private static final String ADD_JAR = "(add\\s+customjar)\\s+'(.*.jar)'";
private static final Pattern ADD_JAR_PATTERN = private static final Pattern ADD_JAR_PATTERN =
Pattern.compile(ADD_JAR, Pattern.CASE_INSENSITIVE); Pattern.compile(ADD_JAR, Pattern.CASE_INSENSITIVE);
......
...@@ -30,7 +30,7 @@ import org.apache.flink.table.api.TableResult; ...@@ -30,7 +30,7 @@ import org.apache.flink.table.api.TableResult;
/** @since 0.7.0 */ /** @since 0.7.0 */
public class AddJarOperation extends AbstractOperation implements Operation { public class AddJarOperation extends AbstractOperation implements Operation {
private static final String KEY_WORD = "ADD JAR"; private static final String KEY_WORD = "ADD CUSTOMJAR";
public AddJarOperation(String statement) { public AddJarOperation(String statement) {
super(statement); super(statement);
......
...@@ -52,7 +52,8 @@ class SqlTypeTest { ...@@ -52,7 +52,8 @@ class SqlTypeTest {
test("SET...", SqlType.SET, true); test("SET...", SqlType.SET, true);
test("RESET...", SqlType.RESET, true); test("RESET...", SqlType.RESET, true);
test("EXECUTE...", SqlType.EXECUTE, true); test("EXECUTE...", SqlType.EXECUTE, true);
test("ADD...", SqlType.ADD, true); test("ADD jar ...", SqlType.ADD_JAR, true);
test("ADD customjar ...", SqlType.ADD, true);
test("WATCH...", SqlType.WATCH, true); test("WATCH...", SqlType.WATCH, true);
String sql = String sql =
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
package org.dinky.function.pool; package org.dinky.function.pool;
import org.dinky.function.data.model.UDF; import org.dinky.function.data.model.UDF;
import org.dinky.process.exception.DinkyException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -49,9 +48,8 @@ public class UdfCodePool { ...@@ -49,9 +48,8 @@ public class UdfCodePool {
public static UDF getUDF(String className) { public static UDF getUDF(String className) {
UDF udf = CODE_POOL.get(className); UDF udf = CODE_POOL.get(className);
if (udf == null) { if (udf == null) {
String error = StrUtil.format("class: {} is not exists!", className); String error = StrUtil.format("class: {} is not exists!,maybe for add jar", className);
log.error(error); log.warn(error);
throw new DinkyException(error);
} }
return udf; return udf;
} }
......
...@@ -352,6 +352,7 @@ public class UDFUtil { ...@@ -352,6 +352,7 @@ public class UDFUtil {
} }
UDF udf = UdfCodePool.getUDF(className); UDF udf = UdfCodePool.getUDF(className);
if (udf != null) {
return UDF.builder() return UDF.builder()
.name(udfName) .name(udfName)
.className(className) .className(className)
...@@ -359,6 +360,7 @@ public class UDFUtil { ...@@ -359,6 +360,7 @@ public class UDFUtil {
.functionLanguage(udf.getFunctionLanguage()) .functionLanguage(udf.getFunctionLanguage())
.build(); .build();
} }
}
return null; return null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment