Unverified Commit 98deaffd authored by Wink's avatar Wink Committed by GitHub

[Feature-3189][client] Optimize cdcsource and lineage (#3190)

parent 81689c18
......@@ -228,7 +228,7 @@ public class JobRefreshHandler {
.config(jobConfigInfo)
.build();
} catch (Exception e) {
log.error("Connect {} failed,{}", jobManagerHost, e.getMessage());
log.warn("Connect {} failed,{}", jobManagerHost, e.getMessage());
return builder.id(id).error(true).errorMsg(e.getMessage()).build();
}
}
......
......@@ -38,13 +38,12 @@ import org.apache.flink.types.Row;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import cn.hutool.core.collection.CollUtil;
public class SQLSinkBuilder extends AbstractSqlSinkBuilder implements Serializable {
public static final String KEY_WORD = "sql";
......@@ -58,7 +57,7 @@ public class SQLSinkBuilder extends AbstractSqlSinkBuilder implements Serializab
@Override
protected void initTypeConverterList() {
typeConverterList = CollUtil.newArrayList(
typeConverterList = Arrays.asList(
this::convertDateType,
this::convertTimestampType,
this::convertFloatType,
......
......@@ -34,12 +34,11 @@ import org.apache.flink.types.Row;
import java.io.Serializable;
import java.time.Instant;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import com.google.common.collect.Lists;
public class SQLCatalogSinkBuilder extends AbstractSqlSinkBuilder implements Serializable {
public static final String KEY_WORD = "sql-catalog";
......@@ -52,7 +51,7 @@ public class SQLCatalogSinkBuilder extends AbstractSqlSinkBuilder implements Ser
@Override
protected void initTypeConverterList() {
typeConverterList = Lists.newArrayList(
typeConverterList = Arrays.asList(
this::convertDateType,
this::convertTimestampType,
this::convertDecimalType,
......
......@@ -130,6 +130,9 @@ public class FlinkStatementUtil {
if (config.getSink().get("connector").equals("hudi") && (type.equals("TIMESTAMP"))) {
return "TIMESTAMP(3)";
}
if (config.getSink().get("connector").equals("doris") && (type.equals("TIME"))) {
return "STRING";
}
return type;
}
......
......@@ -31,7 +31,6 @@ import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.bridge.java.internal.StreamTableEnvironmentImpl;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.delegation.Planner;
import org.apache.flink.table.operations.ExplainOperation;
import org.apache.flink.table.operations.ModifyOperation;
......@@ -118,7 +117,7 @@ public abstract class AbstractCustomTableEnvironment
@Override
public List<LineageRel> getLineage(String statement) {
LineageContext lineageContext = new LineageContext((TableEnvironmentImpl) streamTableEnvironment);
return lineageContext.getLineage(statement);
LineageContext lineageContext = new LineageContext(this);
return lineageContext.analyzeLineage(statement);
}
}
/*
*
* 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.dinky.utils;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.util.SqlBasicVisitor;
import org.apache.flink.table.catalog.UnresolvedIdentifier;
import java.util.ArrayList;
import java.util.List;
public class FunctionVisitor extends SqlBasicVisitor<Void> {
private final List<UnresolvedIdentifier> functionList = new ArrayList<>();
@Override
public Void visit(SqlCall call) {
if (call instanceof SqlBasicCall && call.getOperator() instanceof SqlFunction) {
SqlFunction function = (SqlFunction) call.getOperator();
SqlIdentifier opName = function.getNameAsId();
functionList.add(UnresolvedIdentifier.of(opName.names));
}
return super.visit(call);
}
public List<UnresolvedIdentifier> getFunctionList() {
return functionList;
}
}
......@@ -19,26 +19,38 @@
package org.dinky.utils;
import org.dinky.data.model.FunctionResult;
import org.dinky.data.model.LineageRel;
import org.dinky.executor.CustomParser;
import org.dinky.executor.CustomTableEnvironment;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.metadata.RelColumnOrigin;
import org.apache.calcite.rel.metadata.RelMetadataQuery;
import org.apache.calcite.sql.SqlNode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import org.apache.flink.table.catalog.ContextResolvedFunction;
import org.apache.flink.table.catalog.FunctionCatalog;
import org.apache.flink.table.catalog.UnresolvedIdentifier;
import org.apache.flink.table.functions.FunctionIdentifier;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.SinkModifyOperation;
import org.apache.flink.table.planner.delegation.PlannerBase;
import org.apache.flink.table.planner.operations.PlannerQueryOperation;
import org.apache.flink.table.planner.plan.schema.TableSourceTable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* LineageContext
*
......@@ -46,13 +58,15 @@ import java.util.Set;
*/
public class LineageContext {
private final TableEnvironmentImpl tableEnv;
private static final Logger LOG = LoggerFactory.getLogger(LineageContext.class);
private final CustomTableEnvironment tableEnv;
public LineageContext(TableEnvironmentImpl tableEnv) {
public LineageContext(CustomTableEnvironment tableEnv) {
this.tableEnv = tableEnv;
}
public List<LineageRel> getLineage(String statement) {
public List<LineageRel> analyzeLineage(String statement) {
// 1. Generate original relNode tree
Tuple2<String, RelNode> parsed = parseStatement(statement);
String sinkTable = parsed.getField(0);
......@@ -131,4 +145,50 @@ public class LineageContext {
}
return resultList;
}
/**
* Analyze custom functions from SQL, does not contain system functions.
*
* @param singleSql the SQL statement to analyze
* @return custom functions set
*/
public Set<FunctionResult> analyzeFunction(String singleSql) {
LOG.info("Analyze function Sql: \n {}", singleSql);
CustomParser parser = (CustomParser) tableEnv.getParser();
// parsing sql and return the abstract syntax tree
SqlNode sqlNode = parser.parseSql(singleSql);
// validate the query
SqlNode validated = parser.validate(sqlNode);
// look for all functions
FunctionVisitor visitor = new FunctionVisitor();
validated.accept(visitor);
List<UnresolvedIdentifier> fullFunctionList = visitor.getFunctionList();
// filter custom functions
Set<FunctionResult> resultSet = new HashSet<>();
for (UnresolvedIdentifier unresolvedIdentifier : fullFunctionList) {
getFunctionCatalog()
.lookupFunction(unresolvedIdentifier)
.flatMap(ContextResolvedFunction::getIdentifier)
// the objectIdentifier of the built-in function is null
.flatMap(FunctionIdentifier::getIdentifier)
.ifPresent(identifier -> {
FunctionResult functionResult = new FunctionResult()
.setCatalogName(identifier.getCatalogName())
.setDatabase(identifier.getDatabaseName())
.setFunctionName(identifier.getObjectName());
LOG.debug("analyzed function: {}", functionResult);
resultSet.add(functionResult);
});
}
return resultSet;
}
private FunctionCatalog getFunctionCatalog() {
PlannerBase planner = (PlannerBase) tableEnv.getPlanner();
return planner.getFlinkContext().getFunctionCatalog();
}
}
......@@ -22,12 +22,11 @@ package org.dinky.utils;
import static org.junit.Assert.assertEquals;
import org.dinky.data.model.LineageRel;
import org.dinky.executor.CustomTableEnvironmentImpl;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.api.internal.TableEnvironmentImpl;
import java.util.List;
import java.util.stream.Collectors;
......@@ -43,7 +42,7 @@ import org.junit.Test;
*/
public class LineageContextTest {
private static TableEnvironmentImpl tableEnv;
private static CustomTableEnvironmentImpl tableEnv;
private static LineageContext context;
@BeforeClass
......@@ -52,8 +51,7 @@ public class LineageContextTest {
EnvironmentSettings settings =
EnvironmentSettings.newInstance().inStreamingMode().build();
tableEnv = (TableEnvironmentImpl) StreamTableEnvironment.create(env, settings);
tableEnv = CustomTableEnvironmentImpl.create(env, settings);
context = new LineageContext(tableEnv);
}
......@@ -80,7 +78,7 @@ public class LineageContextTest {
@Test
public void testGetLineage() {
List<LineageRel> actualList = context.getLineage("INSERT INTO TT select a||c A ,b||c B from ST");
List<LineageRel> actualList = context.analyzeLineage("INSERT INTO TT select a||c A ,b||c B from ST");
String[][] expectedArray = {
{"ST", "a", "TT", "A", "||(a, c)"},
{"ST", "c", "TT", "A", "||(a, c)"},
......
......@@ -19,6 +19,7 @@
package org.dinky.data.model;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -135,6 +136,10 @@ public class FlinkCDCConfig {
}
private boolean isSkip(String key) {
if (key.equals("url")) {
return !(sink.containsKey("connector")
&& Arrays.asList("jdbc", "clickhouse").contains(sink.get("connector")));
}
switch (key) {
case SINK_DB:
case AUTO_CREATE:
......
......@@ -169,7 +169,7 @@ public class DorisDriver extends AbstractJdbcDriver {
String propertiesStr = "\n PROPERTIES ( \"light_schema_change\" = \"true\" )";
String commentStr =
Asserts.isNullString(table.getComment()) ? "" : String.format("\n COMMENT '%s'", table.getComment());
Asserts.isNullString(table.getComment()) ? "" : String.format("\n COMMENT \"%s\"", table.getComment());
return MessageFormat.format(
"CREATE TABLE IF NOT EXISTS `{0}`.`{1}` (\n{2}\n) ENGINE=OLAP{3}{4}{5}{6}",
......@@ -214,11 +214,10 @@ public class DorisDriver extends AbstractJdbcDriver {
: String.format("%s NULL ", !column.isNullable() ? " NOT " : "");
return String.format(
" `%s` %s%s%s%s",
" `%s` %s%s%s",
column.getName(),
columnType,
defaultValue,
column.isAutoIncrement() ? " AUTO_INCREMENT " : "",
Asserts.isNotNullString(column.getComment())
? String.format(" COMMENT '%s'", column.getComment())
: "");
......
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