Skip to content

Commit

Permalink
[Feature][Transforms] Support cast to bytes function of sql (apache#7284
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hailin0 authored Jul 30, 2024
1 parent ec1c319 commit b9acb57
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/en/transform-v2/sql-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ CALL FROM_UNIXTIME(1672502400, 'yyyy-MM-dd HH:mm:ss','UTC+6')

Converts a value to another data type.

Supported data types: STRING | VARCHAR, INT | INTEGER, LONG | BIGINT, BYTE, FLOAT, DOUBLE, DECIMAL(p,s), TIMESTAMP, DATE, TIME
Supported data types: STRING | VARCHAR, INT | INTEGER, LONG | BIGINT, BYTE, FLOAT, DOUBLE, DECIMAL(p,s), TIMESTAMP, DATE, TIME, BYTES

Example:

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/transform-v2/sql-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ CALL FROM_UNIXTIME(1672502400, 'yyyy-MM-dd HH:mm:ss','UTC+6')

将一个值转换为另一个数据类型。

支持的数据类型有:STRING | VARCHAR,INT | INTEGER,LONG | BIGINT,BYTE,FLOAT,DOUBLE,DECIMAL(p,s),TIMESTAMP,DATE,TIME
支持的数据类型有:STRING | VARCHAR,INT | INTEGER,LONG | BIGINT,BYTE,FLOAT,DOUBLE,DECIMAL(p,s),TIMESTAMP,DATE,TIME,BYTES

示例:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ transform {
Sql {
source_table_name = "fake"
result_table_name = "fake1"
query = "select cast(id as STRING) as id, cast(id as INT) as id2, cast(id as DOUBLE) as id3 , cast(c1 as double) as c1_1, cast(c1 as DECIMAL(10,2)) as c1_2, cast(c2 as DATE) as c2_1, coalesce(c3,'Unknown') c3_1, ifnull(c3,'Unknown') c3_2, ifnull(nullif(name,'Joy Ding'),'NULL') name1, nullif(name,'Joy Ding_') name2, cast(c4 as timestamp) as c4_1, cast(c4 as decimal(17,4)) as c4_2, cast(c5 as date) as c5, cast(c6 as time) as c6 from fake"
query = "select cast(id as STRING) as id, cast(id as INT) as id2, cast(id as DOUBLE) as id3 , cast(c1 as double) as c1_1, cast(c1 as DECIMAL(10,2)) as c1_2, cast(c2 as DATE) as c2_1, coalesce(c3,'Unknown') c3_1, ifnull(c3,'Unknown') c3_2, ifnull(nullif(name,'Joy Ding'),'NULL') name1, nullif(name,'Joy Ding_') name2, cast(c4 as timestamp) as c4_1, cast(c4 as decimal(17,4)) as c4_2, cast(c5 as date) as c5, cast(c6 as time) as c6, cast(name as bytes) as c7 from fake"
}
}

Expand Down Expand Up @@ -155,6 +155,15 @@ sink {
field_value = [
{equals_to = "23:51:09"}
]
},
{
field_name = "c7"
field_type = "bytes"
field_value = [
{
rule_type = NOT_NULL
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.seatunnel.api.table.type.DecimalType;
import org.apache.seatunnel.api.table.type.LocalTimeType;
import org.apache.seatunnel.api.table.type.MapType;
import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
import org.apache.seatunnel.api.table.type.SqlType;
Expand Down Expand Up @@ -69,6 +70,7 @@ public class ZetaSQLType {
public static final String BIGINT = "BIGINT";
public static final String LONG = "LONG";
public static final String BYTE = "BYTE";
public static final String BYTES = "BYTES";
public static final String DOUBLE = "DOUBLE";
public static final String FLOAT = "FLOAT";
public static final String TIMESTAMP = "TIMESTAMP";
Expand Down Expand Up @@ -311,6 +313,8 @@ private SeaTunnelDataType<?> getCastType(CastExpression castExpression) {
return BasicType.LONG_TYPE;
case BYTE:
return BasicType.BYTE_TYPE;
case BYTES:
return PrimitiveByteArrayType.INSTANCE;
case DOUBLE:
return BasicType.DOUBLE_TYPE;
case FLOAT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -98,6 +99,8 @@ public static Object castAs(List<Object> args) {
return Long.parseLong(v1.toString());
case "BYTE":
return Byte.parseByte(v1.toString());
case "BYTES":
return v1.toString().getBytes(StandardCharsets.UTF_8);
case "DOUBLE":
return Double.parseDouble(v1.toString());
case "FLOAT":
Expand Down

0 comments on commit b9acb57

Please sign in to comment.