-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature-15837] [api] Auto create workflow while import sql script
- Loading branch information
Showing
24 changed files
with
833 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 140 additions & 91 deletions
231
.../main/java/org/apache/dolphinscheduler/api/service/impl/ProcessDefinitionServiceImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
...scheduler-api/src/main/java/org/apache/dolphinscheduler/api/task/SqlTaskParseContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task; | ||
|
||
import org.apache.dolphinscheduler.dao.entity.DataSource; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class SqlTaskParseContext { | ||
|
||
private Map<String, Object> processProperties; | ||
|
||
private Map<String, Object> hints; | ||
|
||
private String sql; | ||
|
||
private String taskName; | ||
|
||
private DataSource dataSource; | ||
|
||
public <T> T hint(String key) { | ||
Object o = hints.get(key); | ||
if (Objects.nonNull(o)) { | ||
return (T) o; | ||
} | ||
o = processProperties.get(key); | ||
if (Objects.nonNull(o)) { | ||
return (T) o; | ||
} | ||
return null; | ||
} | ||
|
||
public <T> T hintOrDefault(String key, T defaultValue) { | ||
T hint = hint(key); | ||
if (Objects.nonNull(hint)) { | ||
return hint; | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...nscheduler-api/src/main/java/org/apache/dolphinscheduler/api/task/SqlTaskParsePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task; | ||
|
||
public interface SqlTaskParsePlugin { | ||
|
||
String name(); | ||
|
||
SqlTaskParseResult parse(SqlTaskParseContext context); | ||
} |
23 changes: 23 additions & 0 deletions
23
...ler-api/src/main/java/org/apache/dolphinscheduler/api/task/SqlTaskParsePluginFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task; | ||
|
||
public interface SqlTaskParsePluginFactory { | ||
|
||
SqlTaskParsePlugin build(); | ||
} |
62 changes: 62 additions & 0 deletions
62
...uler-api/src/main/java/org/apache/dolphinscheduler/api/task/SqlTaskParsePluginLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
public class SqlTaskParsePluginLoader { | ||
|
||
private SqlTaskParsePluginLoader() { | ||
} | ||
|
||
private static final Map<String, SqlTaskParsePlugin> PLUGIN_MAP; | ||
|
||
static { | ||
List<SqlTaskParsePlugin> plugins = load(); | ||
PLUGIN_MAP = ImmutableMap | ||
.copyOf(plugins.stream().collect(Collectors.toMap(SqlTaskParsePlugin::name, plugin -> plugin))); | ||
} | ||
|
||
private static List<SqlTaskParsePlugin> load() { | ||
List<SqlTaskParsePlugin> plugins = new ArrayList<>(); | ||
for (SqlTaskParsePluginFactory sqlTaskParsePluginFactory : ServiceLoader | ||
.load(SqlTaskParsePluginFactory.class)) { | ||
SqlTaskParsePlugin plugin = sqlTaskParsePluginFactory.build(); | ||
plugins.add(plugin); | ||
} | ||
return plugins; | ||
} | ||
|
||
public static List<SqlTaskParsePlugin> allSqlTaskParsePlugins() { | ||
return new ArrayList<>(PLUGIN_MAP.values()); | ||
} | ||
|
||
public static SqlTaskParsePlugin getSqlTaskParsePlugin(String pluginName) { | ||
return PLUGIN_MAP.get(pluginName); | ||
} | ||
|
||
public static SqlTaskParsePlugin defaultSqlTaskParsePlugin() { | ||
return PLUGIN_MAP.get("hint"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...nscheduler-api/src/main/java/org/apache/dolphinscheduler/api/task/SqlTaskParseResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task; | ||
|
||
import java.util.Set; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class SqlTaskParseResult { | ||
|
||
private Set<String> upstreamSet; | ||
|
||
private Set<String> downstreamSet; | ||
} |
101 changes: 101 additions & 0 deletions
101
...api/src/main/java/org/apache/dolphinscheduler/api/task/druid/DruidSqlTaskParsePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task.druid; | ||
|
||
import org.apache.dolphinscheduler.api.task.SqlTaskParseContext; | ||
import org.apache.dolphinscheduler.api.task.SqlTaskParsePlugin; | ||
import org.apache.dolphinscheduler.api.task.SqlTaskParseResult; | ||
import org.apache.dolphinscheduler.dao.entity.DataSource; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import com.alibaba.druid.DbType; | ||
import com.alibaba.druid.sql.SQLUtils; | ||
import com.alibaba.druid.sql.ast.SQLStatement; | ||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor; | ||
import com.alibaba.druid.stat.TableStat; | ||
|
||
public class DruidSqlTaskParsePlugin implements SqlTaskParsePlugin { | ||
|
||
@Override | ||
public String name() { | ||
return "druid parser"; | ||
} | ||
|
||
@Override | ||
public SqlTaskParseResult parse(SqlTaskParseContext context) { | ||
DbType dbType = getDbType(context.getDataSource()); | ||
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(context.getSql(), dbType); | ||
Set<String> upstreamList = new HashSet<>(); | ||
Set<String> downstreamList = new HashSet<>(); | ||
for (SQLStatement sqlStatement : sqlStatements) { | ||
SchemaStatVisitor schemaStatVisitor = SQLUtils.createSchemaStatVisitor(dbType); | ||
sqlStatement.accept(schemaStatVisitor); | ||
|
||
Map<TableStat.Name, TableStat> tables = schemaStatVisitor.getTables(); | ||
for (Map.Entry<TableStat.Name, TableStat> table : tables.entrySet()) { | ||
if (table.getValue().getSelectCount() > 0) { | ||
upstreamList.add(table.getKey().getName()); | ||
} else { | ||
downstreamList.add(table.getKey().getName()); | ||
} | ||
} | ||
} | ||
|
||
SqlTaskParseResult result = new SqlTaskParseResult(); | ||
result.setUpstreamSet(upstreamList); | ||
result.setDownstreamSet(downstreamList); | ||
return result; | ||
} | ||
|
||
private DbType getDbType(DataSource dataSource) { | ||
switch (dataSource.getType()) { | ||
case MYSQL: | ||
return DbType.mysql; | ||
case ORACLE: | ||
return DbType.oracle; | ||
case SQLSERVER: | ||
return DbType.sqlserver; | ||
case POSTGRESQL: | ||
return DbType.postgresql; | ||
case PRESTO: | ||
return DbType.presto; | ||
case CLICKHOUSE: | ||
return DbType.clickhouse; | ||
case TRINO: | ||
return DbType.trino; | ||
case DB2: | ||
return DbType.db2; | ||
case OCEANBASE: | ||
return DbType.oceanbase; | ||
case STARROCKS: | ||
return DbType.starrocks; | ||
case H2: | ||
return DbType.h2; | ||
case HIVE: | ||
case SPARK: | ||
case KYUUBI: | ||
return DbType.hive; | ||
default: | ||
return DbType.other; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../main/java/org/apache/dolphinscheduler/api/task/druid/DruidSqlTaskParsePluginFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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.dolphinscheduler.api.task.druid; | ||
|
||
import org.apache.dolphinscheduler.api.task.SqlTaskParsePlugin; | ||
import org.apache.dolphinscheduler.api.task.SqlTaskParsePluginFactory; | ||
|
||
import com.google.auto.service.AutoService; | ||
|
||
@AutoService(SqlTaskParsePluginFactory.class) | ||
public class DruidSqlTaskParsePluginFactory implements SqlTaskParsePluginFactory { | ||
|
||
@Override | ||
public SqlTaskParsePlugin build() { | ||
return new DruidSqlTaskParsePlugin(); | ||
} | ||
} |
Oops, something went wrong.