Unverified Commit 7950c5f9 authored by Licho's avatar Licho Committed by GitHub

refactor: asome trivial modified. (#2081)

* refactor: package uri wrong, and trivial optimization * refactor: a little trivial change * Spotless Apply * refactor Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * refactor: map null to empty Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply * fix: Mistakenly deleted Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * fix Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> * Spotless Apply --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent e5070787
...@@ -116,8 +116,8 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -116,8 +116,8 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
List<ModifyOperation> modifyOperations = List<ModifyOperation> modifyOperations =
operations.stream() operations.stream()
.filter(operation -> operation instanceof ModifyOperation) .filter(ModifyOperation.class::isInstance)
.map(operation -> (ModifyOperation) operation) .map(ModifyOperation.class::cast)
.collect(Collectors.toList()); .collect(Collectors.toList());
StreamGraph streamGraph = transOperatoinsToStreamGraph(modifyOperations); StreamGraph streamGraph = transOperatoinsToStreamGraph(modifyOperations);
...@@ -180,26 +180,26 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment { ...@@ -180,26 +180,26 @@ public class CustomTableEnvironmentImpl extends AbstractCustomTableEnvironment {
} }
Operation operation = operations.get(0); Operation operation = operations.get(0);
SqlExplainResult record = new SqlExplainResult(); SqlExplainResult data = new SqlExplainResult();
record.setParseTrue(true); data.setParseTrue(true);
record.setExplainTrue(true); data.setExplainTrue(true);
if (operation instanceof ModifyOperation) { if (operation instanceof ModifyOperation) {
record.setType("Modify DML"); data.setType("Modify DML");
} else if (operation instanceof ExplainOperation) { } else if (operation instanceof ExplainOperation) {
record.setType("Explain DML"); data.setType("Explain DML");
} else if (operation instanceof QueryOperation) { } else if (operation instanceof QueryOperation) {
record.setType("Query DML"); data.setType("Query DML");
} else { } else {
record.setExplain(operation.asSummaryString()); data.setExplain(operation.asSummaryString());
record.setType("DDL"); data.setType("DDL");
// record.setExplain("DDL statement needn't comment。"); // data.setExplain("DDL statement needn't comment。");
return record; return data;
} }
record.setExplain(getPlanner().explain(operations, ExplainFormat.TEXT, extraDetails)); data.setExplain(getPlanner().explain(operations, ExplainFormat.TEXT, extraDetails));
return record; return data;
} }
public boolean parseAndLoadConfiguration( public boolean parseAndLoadConfiguration(
......
...@@ -34,6 +34,7 @@ import org.apache.flink.table.api.internal.TableEnvironmentInternal; ...@@ -34,6 +34,7 @@ import org.apache.flink.table.api.internal.TableEnvironmentInternal;
import org.apache.flink.table.delegation.Planner; import org.apache.flink.table.delegation.Planner;
import org.apache.flink.types.Row; import org.apache.flink.types.Row;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -67,7 +68,7 @@ public interface CustomTableEnvironment ...@@ -67,7 +68,7 @@ public interface CustomTableEnvironment
Configuration getRootConfiguration(); Configuration getRootConfiguration();
default List<LineageRel> getLineage(String statement) { default List<LineageRel> getLineage(String statement) {
return null; return Collections.emptyList();
} }
<T> void createTemporaryView(String s, DataStream<Row> dataStream, List<String> columnNameList); <T> void createTemporaryView(String s, DataStream<Row> dataStream, List<String> columnNameList);
......
...@@ -34,6 +34,8 @@ public class SqlUtil { ...@@ -34,6 +34,8 @@ public class SqlUtil {
private static final String SEMICOLON = ";"; private static final String SEMICOLON = ";";
private SqlUtil() {}
public static String[] getStatements(String sql) { public static String[] getStatements(String sql) {
return getStatements(sql, SystemConfiguration.getInstances().getSqlSeparator()); return getStatements(sql, SystemConfiguration.getInstances().getSqlSeparator());
} }
......
...@@ -53,6 +53,10 @@ import org.apache.flink.runtime.rest.messages.JobPlanInfo; ...@@ -53,6 +53,10 @@ import org.apache.flink.runtime.rest.messages.JobPlanInfo;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
...@@ -66,6 +70,7 @@ import cn.hutool.core.util.StrUtil; ...@@ -66,6 +70,7 @@ import cn.hutool.core.util.StrUtil;
* @since 2021/6/22 * @since 2021/6/22
*/ */
public class Explainer { public class Explainer {
private static final Logger logger = LoggerFactory.getLogger(Explainer.class);
private Executor executor; private Executor executor;
private boolean useStatementSet; private boolean useStatementSet;
...@@ -213,7 +218,7 @@ public class Explainer { ...@@ -213,7 +218,7 @@ public class Explainer {
record.setIndex(index++); record.setIndex(index++);
sqlExplainRecords.add(record); sqlExplainRecords.add(record);
} }
if (correct && jobParam.getTrans().size() > 0) { if (correct && !jobParam.getTrans().isEmpty()) {
if (useStatementSet) { if (useStatementSet) {
SqlExplainResult record = new SqlExplainResult(); SqlExplainResult record = new SqlExplainResult();
List<String> inserts = new ArrayList<>(); List<String> inserts = new ArrayList<>();
...@@ -222,7 +227,7 @@ public class Explainer { ...@@ -222,7 +227,7 @@ public class Explainer {
inserts.add(item.getValue()); inserts.add(item.getValue());
} }
} }
if (inserts.size() > 0) { if (!inserts.isEmpty()) {
String sqlSet = String.join(";\r\n ", inserts); String sqlSet = String.join(";\r\n ", inserts);
try { try {
record.setExplain(executor.explainStatementSet(inserts)); record.setExplain(executor.explainStatementSet(inserts));
...@@ -304,43 +309,39 @@ public class Explainer { ...@@ -304,43 +309,39 @@ public class Explainer {
public ObjectNode getStreamGraph(String statement) { public ObjectNode getStreamGraph(String statement) {
JobParam jobParam = pretreatStatements(SqlUtil.getStatements(statement, sqlSeparator)); JobParam jobParam = pretreatStatements(SqlUtil.getStatements(statement, sqlSeparator));
if (jobParam.getDdl().size() > 0) { jobParam.getDdl().forEach(statementParam -> executor.executeSql(statementParam.getValue()));
for (StatementParam statementParam : jobParam.getDdl()) {
executor.executeSql(statementParam.getValue()); if (!jobParam.getTrans().isEmpty()) {
}
}
if (jobParam.getTrans().size() > 0) {
return executor.getStreamGraph(jobParam.getTransStatement()); return executor.getStreamGraph(jobParam.getTransStatement());
} else if (jobParam.getExecute().size() > 0) { }
List<String> datastreamPlans = new ArrayList<>();
for (StatementParam item : jobParam.getExecute()) { if (!jobParam.getExecute().isEmpty()) {
datastreamPlans.add(item.getValue()); List<String> datastreamPlans =
} jobParam.getExecute().stream()
.map(StatementParam::getValue)
.collect(Collectors.toList());
return executor.getStreamGraphFromDataStream(datastreamPlans); return executor.getStreamGraphFromDataStream(datastreamPlans);
} else {
return mapper.createObjectNode();
} }
return mapper.createObjectNode();
} }
public JobPlanInfo getJobPlanInfo(String statement) { public JobPlanInfo getJobPlanInfo(String statement) {
JobParam jobParam = pretreatStatements(SqlUtil.getStatements(statement, sqlSeparator)); JobParam jobParam = pretreatStatements(SqlUtil.getStatements(statement, sqlSeparator));
if (jobParam.getDdl().size() > 0) { jobParam.getDdl().forEach(statementParam -> executor.executeSql(statementParam.getValue()));
for (StatementParam statementParam : jobParam.getDdl()) {
executor.executeSql(statementParam.getValue()); if (!jobParam.getTrans().isEmpty()) {
}
}
if (jobParam.getTrans().size() > 0) {
return executor.getJobPlanInfo(jobParam.getTransStatement()); return executor.getJobPlanInfo(jobParam.getTransStatement());
} else if (jobParam.getExecute().size() > 0) { }
List<String> datastreamPlans = new ArrayList<>();
for (StatementParam item : jobParam.getExecute()) { if (!jobParam.getExecute().isEmpty()) {
datastreamPlans.add(item.getValue()); List<String> datastreamPlans =
} jobParam.getExecute().stream()
.map(StatementParam::getValue)
.collect(Collectors.toList());
return executor.getJobPlanInfoFromDataStream(datastreamPlans); return executor.getJobPlanInfoFromDataStream(datastreamPlans);
} else {
throw new RuntimeException(
"Creating job plan fails because this job doesn't contain an insert statement.");
} }
throw new RuntimeException(
"Creating job plan fails because this job doesn't contain an insert statement.");
} }
public List<LineageRel> getLineage(String statement) { public List<LineageRel> getLineage(String statement) {
...@@ -353,15 +354,12 @@ public class Explainer { ...@@ -353,15 +354,12 @@ public class Explainer {
useStatementSet, useStatementSet,
1, 1,
executor.getTableConfig().getConfiguration().toMap()); executor.getTableConfig().getConfiguration().toMap());
JobManager jm = JobManager.buildPlanMode(jobConfig); this.initialize(JobManager.buildPlanMode(jobConfig), jobConfig, statement);
this.initialize(jm, jobConfig, statement);
String[] sqls = SqlUtil.getStatements(statement, sqlSeparator);
List<LineageRel> lineageRelList = new ArrayList<>(); List<LineageRel> lineageRelList = new ArrayList<>();
for (String item : sqls) { for (String item : SqlUtil.getStatements(statement, sqlSeparator)) {
String sql = "";
try { try {
sql = FlinkInterceptor.pretreatStatement(executor, item); String sql = FlinkInterceptor.pretreatStatement(executor, item);
if (Asserts.isNullString(sql)) { if (Asserts.isNullString(sql)) {
continue; continue;
} }
...@@ -373,8 +371,8 @@ public class Explainer { ...@@ -373,8 +371,8 @@ public class Explainer {
executor.executeSql(sql); executor.executeSql(sql);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error(e.getMessage());
break; return lineageRelList;
} }
} }
return lineageRelList; return lineageRelList;
......
...@@ -21,8 +21,8 @@ package org.dinky.job; ...@@ -21,8 +21,8 @@ package org.dinky.job;
import org.dinky.function.data.model.UDF; import org.dinky.function.data.model.UDF;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* JobParam * JobParam
...@@ -87,11 +87,7 @@ public class JobParam { ...@@ -87,11 +87,7 @@ public class JobParam {
} }
public List<String> getTransStatement() { public List<String> getTransStatement() {
List<String> statementList = new ArrayList<>(); return trans.stream().map(StatementParam::getValue).collect(Collectors.toList());
for (StatementParam statementParam : trans) {
statementList.add(statementParam.getValue());
}
return statementList;
} }
public void setTrans(List<StatementParam> trans) { public void setTrans(List<StatementParam> trans) {
......
...@@ -238,9 +238,11 @@ public abstract class Executor { ...@@ -238,9 +238,11 @@ public abstract class Executor {
reset.invoke(UserGroupInformation.class); reset.invoke(UserGroupInformation.class);
log.info("Reset kerberos authentication..."); log.info("Reset kerberos authentication...");
} }
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { } catch (NoSuchMethodException
throw new RuntimeException(e); | IllegalAccessException
} catch (IOException e) { | InvocationTargetException
| IOException e) {
logger.error("Reset kerberos authentication error.", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
...@@ -284,8 +286,7 @@ public abstract class Executor { ...@@ -284,8 +286,7 @@ public abstract class Executor {
"Kerberos [{}] authentication success.", "Kerberos [{}] authentication success.",
UserGroupInformation.getLoginUser().getUserName()); UserGroupInformation.getLoginUser().getUserName());
} catch (IOException e) { } catch (IOException e) {
log.error("Kerberos authentication failed."); log.error("Kerberos authentication failed. ", e);
e.printStackTrace();
} }
} }
...@@ -302,14 +303,7 @@ public abstract class Executor { ...@@ -302,14 +303,7 @@ public abstract class Executor {
if (udfPyFilePath == null || udfPyFilePath.length == 0) { if (udfPyFilePath == null || udfPyFilePath.length == 0) {
return; return;
} }
// if (MapUtil.isNotEmpty(setConfig)) {
// setConfig.put(PythonOptions.PYTHON_FILES.key(), String.join(",",
// udfPyFilePath));
// setConfig.put(PythonOptions.PYTHON_CLIENT_EXECUTABLE.key(), executable);
// Configuration configuration =
// Configuration.fromMap(executorSetting.getConfig());
// environment.getConfig().configure(configuration, null);
// }
Configuration configuration = tableEnvironment.getConfig().getConfiguration(); Configuration configuration = tableEnvironment.getConfig().getConfiguration();
configuration.setString(PythonOptions.PYTHON_FILES, String.join(",", udfPyFilePath)); configuration.setString(PythonOptions.PYTHON_FILES, String.join(",", udfPyFilePath));
configuration.setString(PythonOptions.PYTHON_CLIENT_EXECUTABLE, executable); configuration.setString(PythonOptions.PYTHON_CLIENT_EXECUTABLE, executable);
...@@ -346,7 +340,7 @@ public abstract class Executor { ...@@ -346,7 +340,7 @@ public abstract class Executor {
try { try {
objectNode = (ObjectNode) mapper.readTree(json); objectNode = (ObjectNode) mapper.readTree(json);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
e.printStackTrace(); logger.error("Get stream graph json node error.", e);
} }
return objectNode; return objectNode;
...@@ -357,11 +351,8 @@ public abstract class Executor { ...@@ -357,11 +351,8 @@ public abstract class Executor {
} }
public ObjectNode getStreamGraphFromDataStream(List<String> statements) { public ObjectNode getStreamGraphFromDataStream(List<String> statements) {
for (String statement : statements) { statements.forEach(this::executeSql);
executeSql(statement); return getStreamGraphJsonNode(getStreamGraph());
}
StreamGraph streamGraph = getStreamGraph();
return getStreamGraphJsonNode(streamGraph);
} }
public JobPlanInfo getJobPlanInfo(List<String> statements) { public JobPlanInfo getJobPlanInfo(List<String> statements) {
...@@ -369,9 +360,7 @@ public abstract class Executor { ...@@ -369,9 +360,7 @@ public abstract class Executor {
} }
public JobPlanInfo getJobPlanInfoFromDataStream(List<String> statements) { public JobPlanInfo getJobPlanInfoFromDataStream(List<String> statements) {
for (String statement : statements) { statements.forEach(this::executeSql);
executeSql(statement);
}
StreamGraph streamGraph = getStreamGraph(); StreamGraph streamGraph = getStreamGraph();
return new JobPlanInfo(JsonPlanGenerator.generatePlan(streamGraph.getJobGraph())); return new JobPlanInfo(JsonPlanGenerator.generatePlan(streamGraph.getJobGraph()));
} }
...@@ -382,17 +371,13 @@ public abstract class Executor { ...@@ -382,17 +371,13 @@ public abstract class Executor {
public TableResult executeStatementSet(List<String> statements) { public TableResult executeStatementSet(List<String> statements) {
StatementSet statementSet = tableEnvironment.createStatementSet(); StatementSet statementSet = tableEnvironment.createStatementSet();
for (String item : statements) { statements.forEach(statementSet::addInsertSql);
statementSet.addInsertSql(item);
}
return statementSet.execute(); return statementSet.execute();
} }
public String explainStatementSet(List<String> statements) { public String explainStatementSet(List<String> statements) {
StatementSet statementSet = tableEnvironment.createStatementSet(); StatementSet statementSet = tableEnvironment.createStatementSet();
for (String item : statements) { statements.forEach(statementSet::addInsertSql);
statementSet.addInsertSql(item);
}
return statementSet.explain(); return statementSet.explain();
} }
......
...@@ -41,7 +41,6 @@ import java.util.Calendar; ...@@ -41,7 +41,6 @@ import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -185,10 +184,6 @@ public final class SqlManager { ...@@ -185,10 +184,6 @@ public final class SqlManager {
rows); rows);
} }
public Iterator getSqlFragmentsIterator() {
return sqlFragments.entrySet().iterator();
}
public Table getSqlFragmentsTable(CustomTableEnvironmentImpl environment) { public Table getSqlFragmentsTable(CustomTableEnvironmentImpl environment) {
List<String> keys = new ArrayList<>(sqlFragments.keySet()); List<String> keys = new ArrayList<>(sqlFragments.keySet());
return environment.fromValues(keys); return environment.fromValues(keys);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.dinky.parser; package org.dinky.parser;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -33,6 +34,7 @@ import org.slf4j.LoggerFactory; ...@@ -33,6 +34,7 @@ import org.slf4j.LoggerFactory;
* @since 2021/6/14 16:49 * @since 2021/6/14 16:49
*/ */
public class SingleSqlParserFactory { public class SingleSqlParserFactory {
private SingleSqlParserFactory() {}
protected static final Logger logger = LoggerFactory.getLogger(SingleSqlParserFactory.class); protected static final Logger logger = LoggerFactory.getLogger(SingleSqlParserFactory.class);
...@@ -61,7 +63,7 @@ public class SingleSqlParserFactory { ...@@ -61,7 +63,7 @@ public class SingleSqlParserFactory {
if (tmp == null) { if (tmp == null) {
logger.error("sql: {} illegal.", sql); logger.error("sql: {} illegal.", sql);
return null; return Collections.emptyMap();
} }
return tmp.splitSql2Segment(); return tmp.splitSql2Segment();
......
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