Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public class SqlParameters extends AbstractParameters {
*/
private String sql;

/**
* sql source
* SCRIPT: inline sql text
* FILE: sql from resource center file
*/
private String sqlSource;

/**
* sql resource file path in resource center
*/
private String sqlResource;

/**
* sql type
* 0 query
Expand Down Expand Up @@ -139,6 +151,22 @@ public void setSql(String sql) {
this.sql = sql;
}

public String getSqlSource() {
return sqlSource;
}

public void setSqlSource(String sqlSource) {
this.sqlSource = sqlSource;
}

public String getSqlResource() {
return sqlResource;
}

public void setSqlResource(String sqlResource) {
this.sqlResource = sqlResource;
}

public int getSqlType() {
return sqlType;
}
Expand Down Expand Up @@ -213,12 +241,25 @@ public void setGroupId(int groupId) {

@Override
public boolean checkParameters() {
return datasource != 0 && StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(sql);
if (datasource == 0 || StringUtils.isEmpty(type)) {
return false;
}
// support both inline sql and sql from resource file
if (StringUtils.isNotEmpty(sql)) {
return true;
}
return StringUtils.isNotEmpty(sqlResource);
}

@Override
public List<ResourceInfo> getResourceFilesList() {
return new ArrayList<>();
List<ResourceInfo> resourceFiles = new ArrayList<>();
if (StringUtils.isNotEmpty(sqlResource)) {
ResourceInfo resourceInfo = new ResourceInfo();
resourceInfo.setResourceName(sqlResource);
resourceFiles.add(resourceInfo);
}
return resourceFiles;
}

public void dealOutParam(String result) {
Expand Down Expand Up @@ -272,6 +313,8 @@ public String toString() {
+ "type='" + type + '\''
+ ", datasource=" + datasource
+ ", sql='" + sql + '\''
+ ", sqlSource='" + sqlSource + '\''
+ ", sqlResource='" + sqlResource + '\''
+ ", sqlType=" + sqlType
+ ", sendEmail=" + sendEmail
+ ", displayRows=" + displayRows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ public void testSqlParameters() {
sqlParameters.setLocalParams(properties);
sqlParameters.dealOutParam(sqlResult);
Assertions.assertNotNull(sqlParameters.getVarPool().get(0));

// resource files list should contain sqlResource when it is set
sqlParameters.setSql(null);
sqlParameters.setSqlResource("/sql/demo.sql");
Assertions.assertFalse(CollectionUtils.isEmpty(sqlParameters.getResourceFilesList()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@
import org.apache.dolphinscheduler.plugin.task.api.parameters.AbstractParameters;
import org.apache.dolphinscheduler.plugin.task.api.parameters.SqlParameters;
import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
import org.apache.dolphinscheduler.plugin.task.api.resource.ResourceContext;
import org.apache.dolphinscheduler.spi.datasource.BaseConnectionParam;
import org.apache.dolphinscheduler.spi.enums.DbType;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -128,6 +134,8 @@ public void handle(TaskCallBack taskCallBack) throws TaskException {
sqlParameters.getLimit());
try {

ensureSqlContent();

// get datasource
baseConnectionParam = (BaseConnectionParam) DataSourceUtils.buildConnectionParams(dbType,
sqlTaskExecutionContext.getConnectionParams());
Expand Down Expand Up @@ -402,7 +410,30 @@ private void printReplacedSql(String content, String formatSql, String rgex, Map
.append(")");
}
}
log.info("Sql Params are {}", logPrint);

private void ensureSqlContent() {
if (StringUtils.isNotEmpty(sqlParameters.getSql())) {
return;
}
if (StringUtils.isEmpty(sqlParameters.getSqlResource())) {
return;
}
String resourcePathInStorage = sqlParameters.getSqlResource();
try {
ResourceContext resourceContext = taskExecutionContext.getResourceContext();
ResourceContext.ResourceItem resourceItem =
resourceContext.getResourceItem(resourcePathInStorage);
String localPath = resourceItem.getResourceAbsolutePathInLocal();
log.info("Load sql content from resource file: {}", resourcePathInStorage);
String sqlContent = new String(
Files.readAllBytes(Paths.get(localPath)),
StandardCharsets.UTF_8);
sqlParameters.setSql(sqlContent);
} catch (IOException e) {
log.error("Read sql content from resource file {} error", resourcePathInStorage, e);
throw new TaskException(String.format("Read sql content from resource file %s error", resourcePathInStorage),
e);
}
}

/**
Expand All @@ -420,19 +451,22 @@ private SqlBinds getSqlAndSqlParamsMap(String sql) {

Map<String, Property> paramsMap = taskExecutionContext.getPrepareParamsMap();

// spell SQL according to the final user-defined variable
if (paramsMap == null) {
sqlBuilder.append(sql);
return new SqlBinds(sqlBuilder.toString(), sqlParamsMap);
}
Map<String, String> placeholderParamsMap = paramsMap == null
? Collections.emptyMap()
: ParameterUtils.convert(paramsMap);

if (StringUtils.isNotEmpty(sqlParameters.getTitle())) {
String title = ParameterUtils.convertParameterPlaceholders(sqlParameters.getTitle(),
ParameterUtils.convert(paramsMap));
String title = ParameterUtils.convertParameterPlaceholders(sqlParameters.getTitle(), placeholderParamsMap);
log.info("SQL title : {}", title);
sqlParameters.setTitle(title);
}

// spell SQL according to the final user-defined variable
if (paramsMap == null) {
sqlBuilder.append(sql);
return new SqlBinds(sqlBuilder.toString(), sqlParamsMap);
}

// special characters need to be escaped, ${} needs to be escaped
setSqlParamsMap(sql, sqlParamsMap, paramsMap, taskExecutionContext.getTaskInstanceId());
// Replace the original value in sql !{...} ,Does not participate in precompilation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,75 @@

package org.apache.dolphinscheduler.plugin.task.sql;

import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
import org.apache.dolphinscheduler.plugin.task.api.parameters.SqlParameters;
import org.apache.dolphinscheduler.plugin.task.api.resource.ResourceContext;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

class SqlTaskTest {

@Test
void testSqlLoadedFromResourceFileWhenSqlIsEmpty(@TempDir Path tempDir) throws Exception {
Path sqlFile = tempDir.resolve("test.sql");
String sqlContent = "SELECT 1";
Files.write(sqlFile, sqlContent.getBytes(StandardCharsets.UTF_8));

SqlParameters sqlParameters = new SqlParameters();
sqlParameters.setType("MYSQL");
sqlParameters.setDatasource(1);
sqlParameters.setSql(null);
sqlParameters.setSqlResource("/sql/test.sql");

TaskExecutionContext taskExecutionContext = new TaskExecutionContext();
taskExecutionContext.setTaskParams(JSONUtils.toJsonString(sqlParameters));
taskExecutionContext.setScheduleTime(System.currentTimeMillis());

ResourceContext resourceContext = new ResourceContext();
resourceContext.addResourceItem(ResourceContext.ResourceItem.builder()
.resourceAbsolutePathInStorage(sqlParameters.getSqlResource())
.resourceAbsolutePathInLocal(sqlFile.toString())
.build());
taskExecutionContext.setResourceContext(resourceContext);

SqlTask sqlTask = new SqlTask(taskExecutionContext);

Method ensureSqlContent = SqlTask.class.getDeclaredMethod("ensureSqlContent");
ensureSqlContent.setAccessible(true);
ensureSqlContent.invoke(sqlTask);

SqlParameters loadedParameters = (SqlParameters) sqlTask.getParameters();
Assertions.assertEquals(sqlContent, loadedParameters.getSql());
}
}

/*
* 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.plugin.task.sql;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
4 changes: 4 additions & 0 deletions dolphinscheduler-ui/src/locales/en_US/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,14 @@ export default {
sql_type_query: 'Query',
sql_type_non_query: 'Non Query',
sql_statement: 'SQL Statement',
sql_source: 'SQL Source',
sql_source_script: 'Script',
sql_source_file: 'Resource file',
pre_sql_statement: 'Pre SQL Statement',
post_sql_statement: 'Post SQL Statement',
sql_input_placeholder: 'Please enter non-query sql.',
sql_empty_tips: 'The sql can not be empty.',
sql_resource_file: 'SQL Resource File',
procedure_method: 'SQL Statement',
procedure_method_tips: 'Please enter the procedure script',
procedure_method_snippet:
Expand Down
4 changes: 4 additions & 0 deletions dolphinscheduler-ui/src/locales/zh_CN/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,14 @@ export default {
sql_type_query: '查询',
sql_type_non_query: '非查询',
sql_statement: 'SQL语句',
sql_source: 'SQL来源',
sql_source_script: '脚本',
sql_source_file: '资源文件',
pre_sql_statement: '前置SQL语句',
post_sql_statement: '后置SQL语句',
sql_input_placeholder: '请输入非查询SQL语句',
sql_empty_tips: '语句不能为空',
sql_resource_file: 'SQL资源文件',
procedure_method: 'SQL语句',
procedure_method_tips: '请输入存储脚本',
procedure_method_snippet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import type { IJsonItem } from '../types'
export function useSql(model: { [field: string]: any }): IJsonItem[] {
const { t } = useI18n()
const hiveSpan = computed(() => (model.type === 'HIVE' ? 24 : 0))
const showScriptEditor = computed(
() => model.sqlSource === 'SCRIPT' || !model.sqlSource
)

return [
{
Expand All @@ -34,10 +37,27 @@ export function useSql(model: { [field: string]: any }): IJsonItem[] {
},
span: hiveSpan
},
{
type: 'radio',
field: 'sqlSource',
name: t('project.node.sql_source'),
options: [
{
label: t('project.node.sql_source_script'),
value: 'SCRIPT'
},
{
label: t('project.node.sql_source_file'),
value: 'FILE'
}
],
span: 24
},
{
type: 'editor',
field: 'sql',
name: t('project.node.sql_statement'),
if: showScriptEditor,
validate: {
trigger: ['input', 'trigger'],
required: true,
Expand All @@ -47,6 +67,22 @@ export function useSql(model: { [field: string]: any }): IJsonItem[] {
language: 'sql'
}
},
{
type: 'tree-select',
field: 'sqlResource',
name: t('project.node.sql_resource_file'),
span: 24,
if: () => model.sqlSource === 'FILE',
props: {
placeholder: t('project.node.resources_tips'),
keyField: 'fullName',
labelField: 'name',
disabledField: 'disable'
},
slots: {
// 这里占位,真正的资源数据从全局资源 store 中获取,保持与 use-resources 一致行为
}
},
...useCustomParams({
model,
field: 'localParams',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function useSql({
type: 'MYSQL',
displayRows: 10,
sql: '',
sqlSource: 'SCRIPT',
sqlResource: '',
sqlType: '0',
preStatements: [],
postStatements: [],
Expand Down
Loading