Skip to content

Commit 21e27cf

Browse files
committed
[31402][rdb-async]rdb async not support time type
1 parent f1626c7 commit 21e27cf

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

core/src/main/java/com/dtstack/flink/sql/util/DateUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class DateUtil {
5959

6060
private static final Pattern DATETIME = Pattern.compile("^\\d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3,9})?Z$");
6161
private static final Pattern DATE = Pattern.compile("^\\d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}$");
62+
private static final Pattern TIME = Pattern.compile("^\\d{2}:\\d{2}:\\d{2}(\\.\\d{3,9})?Z$");
6263
private static final int MILLIS_PER_SECOND = 1000;
6364

6465

@@ -832,4 +833,18 @@ public static java.sql.Date getDateFromStr(String dateStr) {
832833
return null == date ? null : new java.sql.Date(date.getTime());
833834
}
834835

836+
public static java.sql.Time getTimeFromStr(String dateStr) {
837+
if (TIME.matcher(dateStr).matches()) {
838+
dateStr = dateStr.substring(0,dateStr.length()-1);
839+
Instant instant = LocalTime.parse(dateStr).atDate(LocalDate.now()).toInstant(ZoneOffset.UTC);
840+
return new java.sql.Time(instant.toEpochMilli());
841+
} else if (DATETIME.matcher(dateStr).matches()) {
842+
Instant instant = Instant.from(ISO_INSTANT.parse(dateStr));
843+
return new java.sql.Time(instant.toEpochMilli());
844+
}
845+
Date date = stringToDate(dateStr);
846+
return null == date ? null : new java.sql.Time(date.getTime());
847+
}
848+
849+
835850
}

core/src/main/java/com/dtstack/flink/sql/util/MathUtil.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.math.BigDecimal;
2323
import java.math.BigInteger;
2424
import java.sql.Date;
25+
import java.sql.Time;
2526
import java.sql.Timestamp;
2627

2728
/**
@@ -234,7 +235,19 @@ public static Date getDate(Object obj) {
234235
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date.");
235236
}
236237

237-
238+
public static Time getTime(Object obj) {
239+
if (obj == null) {
240+
return null;
241+
}
242+
if (obj instanceof String) {
243+
return DateUtil.getTimeFromStr((String) obj);
244+
} else if (obj instanceof Timestamp) {
245+
return new Time(((Timestamp) obj).getTime());
246+
} else if (obj instanceof Time) {
247+
return (Time) obj;
248+
}
249+
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Time.");
250+
}
238251

239252
public static Timestamp getTimestamp(Object obj) {
240253
if (obj == null) {

rdb/rdb-side/src/main/java/com/dtstack/flink/sql/side/rdb/util/SwitchUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public static Object getTarget(Object obj, String targetType) {
7878
case "timestamp":
7979
case "datetime":
8080
return MathUtil.getTimestamp(obj);
81+
case "time":
82+
return MathUtil.getTime(obj);
8183
default:
8284
}
8385
return obj;

0 commit comments

Comments
 (0)