Unverified Commit 65ded4ca authored by Licho's avatar Licho Committed by GitHub

feat: compatiable office create function sql syntax, and fix some bug (#1896)

* fix: python udf template error * fix: when DinkyException, not process exception * fix: compatible office sql syntax * Spotless Apply * fix: invoke toUDF twice * Spotless Apply * doc: addition some python document. * add unit test 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 d70b621a
...@@ -60,6 +60,7 @@ public class UdfClassLoaderAspect { ...@@ -60,6 +60,7 @@ public class UdfClassLoaderAspect {
if (!(e instanceof DinkyException)) { if (!(e instanceof DinkyException)) {
throw new DinkyException(e); throw new DinkyException(e);
} }
throw (DinkyException) e;
} finally { } finally {
if (proceed instanceof JobResult) { if (proceed instanceof JobResult) {
ClassLoader lastContextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader lastContextClassLoader = Thread.currentThread().getContextClassLoader();
......
...@@ -66,8 +66,17 @@ public class StudioController { ...@@ -66,8 +66,17 @@ public class StudioController {
/** 执行Sql */ /** 执行Sql */
@PostMapping("/executeSql") @PostMapping("/executeSql")
public Result<JobResult> executeSql(@RequestBody StudioExecuteDTO studioExecuteDTO) { public Result<JobResult> executeSql(@RequestBody StudioExecuteDTO studioExecuteDTO) {
try {
JobResult jobResult = studioService.executeSql(studioExecuteDTO); JobResult jobResult = studioService.executeSql(studioExecuteDTO);
return Result.succeed(jobResult, "执行成功"); return Result.succeed(jobResult, "execute successful");
} catch (Exception ex) {
JobResult jobResult = new JobResult();
jobResult.setJobConfig(studioExecuteDTO.getJobConfig());
jobResult.setSuccess(false);
jobResult.setStatement(studioExecuteDTO.getStatement());
jobResult.setError(ex.toString());
return Result.failed(jobResult, "execute failed");
}
} }
/** 解释Sql */ /** 解释Sql */
......
...@@ -139,7 +139,7 @@ public class Explainer { ...@@ -139,7 +139,7 @@ public class Explainer {
} else { } else {
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);
} }
ddl.add(new StatementParam(statement, operationType)); ddl.add(new StatementParam(statement, operationType));
statementList.add(statement); statementList.add(statement);
......
...@@ -74,7 +74,7 @@ import groovy.lang.GroovyClassLoader; ...@@ -74,7 +74,7 @@ import groovy.lang.GroovyClassLoader;
public class UDFUtil { public class UDFUtil {
public static final String FUNCTION_SQL_REGEX = public static final String FUNCTION_SQL_REGEX =
"create\\s+.*function\\s+(.*)\\s+as\\s+'(.*)'(\\s+language (.*))?"; "^CREATE\\s+(?:(?:TEMPORARY|TEMPORARY\\s+SYSTEM)\\s+)?FUNCTION\\s+(?:IF\\s+NOT\\s+EXISTS\\s+)?(\\S+)\\s+AS\\s+'(\\S+)'\\s*(?:LANGUAGE\\s+(?:JAVA|SCALA|PYTHON)\\s+)?(?:USING\\s+JAR\\s+'(\\S+)'\\s*(?:,\\s*JAR\\s+'(\\S+)'\\s*)*)?";
public static final String SESSION = "SESSION"; public static final String SESSION = "SESSION";
public static final String YARN = "YARN"; public static final String YARN = "YARN";
...@@ -328,6 +328,13 @@ public class UDFUtil { ...@@ -328,6 +328,13 @@ public class UDFUtil {
List<String> groups = CollUtil.removeEmpty(ReUtil.getAllGroups(pattern, statement)); List<String> groups = CollUtil.removeEmpty(ReUtil.getAllGroups(pattern, statement));
String udfName = groups.get(1); String udfName = groups.get(1);
String className = groups.get(2); String className = groups.get(2);
if (groups.size() > 3) {
// if statement contains using jar, using these jars, not to lookup ClassLoaderUtil
// pool
return null;
}
if (ClassLoaderUtil.isPresent(className)) { if (ClassLoaderUtil.isPresent(className)) {
// 获取已经加载在java的类,对应的包路径 // 获取已经加载在java的类,对应的包路径
try { try {
......
/*
*
* 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.function.util;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.function.Function;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
class UDFUtilTest {
@Test
void isUdfStatement() {
Pattern pattern = Pattern.compile(UDFUtil.FUNCTION_SQL_REGEX, Pattern.CASE_INSENSITIVE);
Function<String, Boolean> c = (s) -> UDFUtil.isUdfStatement(pattern, s);
assertTrue(c.apply("create function a as 'abc'"));
assertTrue(c.apply("create function a as 'abc' language python"));
assertTrue(c.apply("create function a as 'abc' using jar 'path'"));
assertTrue(c.apply("create function a as 'abc' using jar 'path', jar 'path/2'"));
assertTrue(
c.apply("create tempOrary function a as 'abc' using jar 'path', jar 'path/2'"));
assertTrue(
c.apply(
"create tempOrary system function a as 'abc' using jar 'path', jar 'path/2'"));
assertTrue(
c.apply(
"create tempOrary system function a as 'abc' using jar 'path',jar 'path/2'"));
assertFalse(
c.apply(
" create tempOrary system function a as 'abc' using jar 'path', jar 'path/2'"));
assertFalse(
c.apply(
"create tempOrary system function a as abc using jar 'path', jar 'path/2'"));
}
}
...@@ -5,7 +5,7 @@ title: Python UDF ...@@ -5,7 +5,7 @@ title: Python UDF
--- ---
## 环境准备 ## 环境准备
1. 需要把 `${FLINK_HOME}/opt/flink-python_${scala-version}-${flink-version}.jar` 放入到 1. 需要把 `${FLINK_HOME}/opt/flink-python_${scala-version}-${flink-version}.jar` 放入到
`${FLINK_HOME}/lib/` 和 `${Dinky_HOME}/plugins/` 下面。如下: `${FLINK_HOME}/lib/` 和 `${DINKY_HOME}/plugins/` 下面。如下:
![flink_python_intro.png](http://www.aiwenmo.com/dinky/docs/zh-CN/udf_develop/how_to/flink_python_intro.png) ![flink_python_intro.png](http://www.aiwenmo.com/dinky/docs/zh-CN/udf_develop/how_to/flink_python_intro.png)
2. 在 Dinky 安装目录 `conf/application.yml` 下,配置 python 执行环境。例如 :/usr/bin/python3.6 2. 在 Dinky 安装目录 `conf/application.yml` 下,配置 python 执行环境。例如 :/usr/bin/python3.6
...@@ -58,7 +58,14 @@ flink-conf.yml ...@@ -58,7 +58,14 @@ flink-conf.yml
python.client.executable: /root/miniconda3/bin/python3.8 python.client.executable: /root/miniconda3/bin/python3.8
python.executable: /root/miniconda3/bin/python3.8 python.executable: /root/miniconda3/bin/python3.8
``` ```
> 这里需要注意的是,`flink-conf.yml` 得配置 `python` 的执行路径,`python` 环境必须包含 `apache-flink` ,支持 `python3.6 - python3.8`
或在sql中配置
```sql
set 'python.client.executable'='/root/miniconda3/bin/python3.8';
set 'python.executable'='/root/miniconda3/bin/python3.8';
```
> 这里需要注意的是,`flink-conf.yml` 得配置 `python` 的执行路径,`python` 环境必须包含 `apache-flink`和`pyflink`, 注意根据flink版本安装相应版本的包
> >
> `python.executable` 是 NodeManager 环境的 python 执行路径 > `python.executable` 是 NodeManager 环境的 python 执行路径
> >
......
...@@ -830,7 +830,7 @@ CREATE TABLE `dinky_udf_template` ( ...@@ -830,7 +830,7 @@ CREATE TABLE `dinky_udf_template` (
INSERT INTO `dinky_udf_template` VALUES (1, 'java_udf', 'Java', 'UDF', '${(package==\'\')?string(\'\',\'package \'+package+\';\')}\n\nimport org.apache.flink.table.functions.ScalarFunction;\n\npublic class ${className} extends ScalarFunction {\n public String eval(String s) {\n return null;\n }\n}', NULL, '2022-10-19 09:17:37', '2022-10-25 17:45:57'); INSERT INTO `dinky_udf_template` VALUES (1, 'java_udf', 'Java', 'UDF', '${(package==\'\')?string(\'\',\'package \'+package+\';\')}\n\nimport org.apache.flink.table.functions.ScalarFunction;\n\npublic class ${className} extends ScalarFunction {\n public String eval(String s) {\n return null;\n }\n}', NULL, '2022-10-19 09:17:37', '2022-10-25 17:45:57');
INSERT INTO `dinky_udf_template` VALUES (2, 'java_udtf', 'Java', 'UDTF', '${(package==\'\')?string(\'\',\'package \'+package+\';\')}\n\nimport org.apache.flink.table.functions.ScalarFunction;\n\n@FunctionHint(output = @DataTypeHint(\"ROW<word STRING, length INT>\"))\npublic static class ${className} extends TableFunction<Row> {\n\n public void eval(String str) {\n for (String s : str.split(\" \")) {\n // use collect(...) to emit a row\n collect(Row.of(s, s.length()));\n }\n }\n}', NULL, '2022-10-19 09:22:58', '2022-10-25 17:49:30'); INSERT INTO `dinky_udf_template` VALUES (2, 'java_udtf', 'Java', 'UDTF', '${(package==\'\')?string(\'\',\'package \'+package+\';\')}\n\nimport org.apache.flink.table.functions.ScalarFunction;\n\n@FunctionHint(output = @DataTypeHint(\"ROW<word STRING, length INT>\"))\npublic static class ${className} extends TableFunction<Row> {\n\n public void eval(String str) {\n for (String s : str.split(\" \")) {\n // use collect(...) to emit a row\n collect(Row.of(s, s.length()));\n }\n }\n}', NULL, '2022-10-19 09:22:58', '2022-10-25 17:49:30');
INSERT INTO `dinky_udf_template` VALUES (3, 'scala_udf', 'Scala', 'UDF', '${(package==\'\')?string(\'\',\'package \'+package+\';\')}\n\nimport org.apache.flink.table.api._\nimport org.apache.flink.table.functions.ScalarFunction\n\n// 定义可参数化的函数逻辑\nclass ${className} extends ScalarFunction {\n def eval(s: String, begin: Integer, end: Integer): String = {\n \"this is scala\"\n }\n}', NULL, '2022-10-25 09:21:32', '2022-10-25 17:49:46'); INSERT INTO `dinky_udf_template` VALUES (3, 'scala_udf', 'Scala', 'UDF', '${(package==\'\')?string(\'\',\'package \'+package+\';\')}\n\nimport org.apache.flink.table.api._\nimport org.apache.flink.table.functions.ScalarFunction\n\n// 定义可参数化的函数逻辑\nclass ${className} extends ScalarFunction {\n def eval(s: String, begin: Integer, end: Integer): String = {\n \"this is scala\"\n }\n}', NULL, '2022-10-25 09:21:32', '2022-10-25 17:49:46');
INSERT INTO `dinky_udf_template` VALUES (4, 'python_udf_1', 'Python', 'UDF', 'from pyflink.table import ScalarFunction, DataTypes\nfrom pyflink.table.udf import udf\n\nclass ${className}(ScalarFunction):\n def __init__(self):\n pass\n\n def eval(self, variable):\n return str(variable)\n\n\n${attr!\'f\'} = udf(HashCode(), result_type=DataTypes.STRING())', NULL, '2022-10-25 09:23:07', '2022-10-25 09:34:01'); INSERT INTO `dinky_udf_template` VALUES (4, 'python_udf_1', 'Python', 'UDF', 'from pyflink.table import ScalarFunction, DataTypes\nfrom pyflink.table.udf import udf\n\nclass ${className}(ScalarFunction):\n def __init__(self):\n pass\n\n def eval(self, variable):\n return str(variable)\n\n\n${attr!\'f\'} = udf(${className}(), result_type=DataTypes.STRING())', NULL, '2022-10-25 09:23:07', '2022-10-25 09:34:01');
INSERT INTO `dinky_udf_template` VALUES (5, 'python_udf_2', 'Python', 'UDF', 'from pyflink.table import DataTypes\nfrom pyflink.table.udf import udf\n\n@udf(result_type=DataTypes.STRING())\ndef ${className}(variable1:str):\n return \'\'', NULL, '2022-10-25 09:25:13', '2022-10-25 09:34:47'); INSERT INTO `dinky_udf_template` VALUES (5, 'python_udf_2', 'Python', 'UDF', 'from pyflink.table import DataTypes\nfrom pyflink.table.udf import udf\n\n@udf(result_type=DataTypes.STRING())\ndef ${className}(variable1:str):\n return \'\'', NULL, '2022-10-25 09:25:13', '2022-10-25 09:34:47');
-- ---------------------------- -- ----------------------------
......
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