Skip to content

Commit

Permalink
[Feature][Transform-SQL]Support sql transform to generate UUID (#7881)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhilinli123 authored Oct 23, 2024
1 parent 13bd46b commit 15431c9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/en/transform-v2/sql-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -973,3 +973,14 @@ It is used to determine whether the condition is valid and return different valu
Example:

case when c_string in ('c_string') then 1 else 0 end

### UUID

```UUID()```

Generate a uuid through java function.

Example:

select UUID() as seatunnel_uuid

11 changes: 11 additions & 0 deletions docs/zh/transform-v2/sql-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,14 @@ from
示例:

case when c_string in ('c_string') then 1 else 0 end

### UUID

```UUID()```

通过java函数生成uuid

示例:

select UUID() as seatunnel_uuid

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ transform {
sql {
source_table_name = "fake"
result_table_name = "fake1"
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"
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"
}
}

Expand Down Expand Up @@ -286,6 +286,15 @@ sink {
field_value = [
{equals_to = "Joy Ding"}
]
},
{
field_name = uuid
field_type = STRING
field_value = [
{
rule_type = NOT_NULL
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import java.util.List;
import java.util.Map;

import static java.util.UUID.randomUUID;

public class ZetaSQLFunction {
// ============================internal functions=====================

Expand Down Expand Up @@ -171,6 +173,8 @@ public class ZetaSQLFunction {
public static final String IFNULL = "IFNULL";
public static final String NULLIF = "NULLIF";

public static final String UUID = "UUID";

private final SeaTunnelRowType inputRowType;
private final ZetaSQLType zetaSQLType;
private final ZetaSQLFilter zetaSQLFilter;
Expand Down Expand Up @@ -515,6 +519,8 @@ public Object executeFunctionExpr(String functionName, List<Object> args) {
return SystemFunction.ifnull(args);
case NULLIF:
return SystemFunction.nullif(args);
case UUID:
return randomUUID().toString();
default:
for (ZetaUDF udf : udfList) {
if (udf.functionName().equalsIgnoreCase(functionName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ private SeaTunnelDataType<?> getFunctionType(Function function) {
case ZetaSQLFunction.MONTHNAME:
case ZetaSQLFunction.FORMATDATETIME:
case ZetaSQLFunction.FROM_UNIXTIME:
case ZetaSQLFunction.UUID:
return BasicType.STRING_TYPE;
case ZetaSQLFunction.ASCII:
case ZetaSQLFunction.LOCATE:
Expand Down

0 comments on commit 15431c9

Please sign in to comment.