Skip to content

Commit 15431c9

Browse files
authored
[Feature][Transform-SQL]Support sql transform to generate UUID (#7881)
1 parent 13bd46b commit 15431c9

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

Diff for: docs/en/transform-v2/sql-functions.md

+11
Original file line numberDiff line numberDiff line change
@@ -973,3 +973,14 @@ It is used to determine whether the condition is valid and return different valu
973973
Example:
974974

975975
case when c_string in ('c_string') then 1 else 0 end
976+
977+
### UUID
978+
979+
```UUID()```
980+
981+
Generate a uuid through java function.
982+
983+
Example:
984+
985+
select UUID() as seatunnel_uuid
986+

Diff for: docs/zh/transform-v2/sql-functions.md

+11
Original file line numberDiff line numberDiff line change
@@ -964,3 +964,14 @@ from
964964
示例:
965965

966966
case when c_string in ('c_string') then 1 else 0 end
967+
968+
### UUID
969+
970+
```UUID()```
971+
972+
通过java函数生成uuid
973+
974+
示例:
975+
976+
select UUID() as seatunnel_uuid
977+

Diff for: seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/sql_transform/func_string.conf

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ transform {
5454
sql {
5555
source_table_name = "fake"
5656
result_table_name = "fake1"
57-
query = "select ascii(c1) as c1_1, ascii(c2) as c2_1, bit_length(c4) as c4_1, length(c4) as c4_2, octet_length(c4) as c4_3, char(c5) as c5_1, concat(c1,id,'!') as c1_2, hextoraw(c6) as c6_1, rawtohex(c7) as c7_1, insert(name,2,2,'**') as name1, lower(name) as name2, upper(name) as name3, left(name, 3) as name4, right(name, 4) as name5, lpad(name, 10, '*') as name6, rpad(name, 10, '*') as name7, ltrim(c8, '*') as c8_1, rtrim(c8, '*') as c8_2, trim(c8, '*') as c8_3, regexp_replace(c9, 'w+', 'W', 'i') as c9_1, regexp_like(name, '[A-Z ]*', 'i') as name8, regexp_substr(c10, '\\d{4}') as c10_1, regexp_substr(c10, '(\\d{4})-(\\d{2})-(\\d{2})', 1, 1, null, 2) as c10_2, repeat(name||' ',3) as name9, replace(name,' ','_') as name10, soundex(name) as name11, name || space(3) as name12, substring(name, 1, 3) as name13, to_char(id) as id1, to_char(c11,'yyyy-MM-dd') as c11_1, translate(name, 'ing', 'ING') as name14, des_decrypt('1234567890', des_encrypt('1234567890', name)) as name15 from fake"
57+
query = "select ascii(c1) as c1_1, ascii(c2) as c2_1, bit_length(c4) as c4_1, length(c4) as c4_2, octet_length(c4) as c4_3, char(c5) as c5_1, concat(c1,id,'!') as c1_2, hextoraw(c6) as c6_1, rawtohex(c7) as c7_1, insert(name,2,2,'**') as name1, lower(name) as name2, upper(name) as name3, left(name, 3) as name4, right(name, 4) as name5, lpad(name, 10, '*') as name6, rpad(name, 10, '*') as name7, ltrim(c8, '*') as c8_1, rtrim(c8, '*') as c8_2, trim(c8, '*') as c8_3, regexp_replace(c9, 'w+', 'W', 'i') as c9_1, regexp_like(name, '[A-Z ]*', 'i') as name8, regexp_substr(c10, '\\d{4}') as c10_1, regexp_substr(c10, '(\\d{4})-(\\d{2})-(\\d{2})', 1, 1, null, 2) as c10_2, repeat(name||' ',3) as name9, replace(name,' ','_') as name10, soundex(name) as name11, name || space(3) as name12, substring(name, 1, 3) as name13, to_char(id) as id1, to_char(c11,'yyyy-MM-dd') as c11_1, translate(name, 'ing', 'ING') as name14, des_decrypt('1234567890', des_encrypt('1234567890', name)) as name15,UUID() as uuid from fake"
5858
}
5959
}
6060

@@ -286,6 +286,15 @@ sink {
286286
field_value = [
287287
{equals_to = "Joy Ding"}
288288
]
289+
},
290+
{
291+
field_name = uuid
292+
field_type = STRING
293+
field_value = [
294+
{
295+
rule_type = NOT_NULL
296+
}
297+
]
289298
}
290299
]
291300
}

Diff for: seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLFunction.java

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import java.util.List;
6262
import java.util.Map;
6363

64+
import static java.util.UUID.randomUUID;
65+
6466
public class ZetaSQLFunction {
6567
// ============================internal functions=====================
6668

@@ -171,6 +173,8 @@ public class ZetaSQLFunction {
171173
public static final String IFNULL = "IFNULL";
172174
public static final String NULLIF = "NULLIF";
173175

176+
public static final String UUID = "UUID";
177+
174178
private final SeaTunnelRowType inputRowType;
175179
private final ZetaSQLType zetaSQLType;
176180
private final ZetaSQLFilter zetaSQLFilter;
@@ -515,6 +519,8 @@ public Object executeFunctionExpr(String functionName, List<Object> args) {
515519
return SystemFunction.ifnull(args);
516520
case NULLIF:
517521
return SystemFunction.nullif(args);
522+
case UUID:
523+
return randomUUID().toString();
518524
default:
519525
for (ZetaUDF udf : udfList) {
520526
if (udf.functionName().equalsIgnoreCase(functionName)) {

Diff for: seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/sql/zeta/ZetaSQLType.java

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ private SeaTunnelDataType<?> getFunctionType(Function function) {
384384
case ZetaSQLFunction.MONTHNAME:
385385
case ZetaSQLFunction.FORMATDATETIME:
386386
case ZetaSQLFunction.FROM_UNIXTIME:
387+
case ZetaSQLFunction.UUID:
387388
return BasicType.STRING_TYPE;
388389
case ZetaSQLFunction.ASCII:
389390
case ZetaSQLFunction.LOCATE:

0 commit comments

Comments
 (0)