Skip to content

Commit 71556d4

Browse files
committed
Merge remote-tracking branch 'origin/1.8_release_3.10.x' into feat_1.8_merge3.10.x
# Conflicts: # core/src/main/java/com/dtstack/flink/sql/util/DateUtil.java # rdb/rdb-side/src/main/java/com/dtstack/flink/sql/side/rdb/async/RdbAsyncReqRow.java
2 parents d562266 + f9b0e57 commit 71556d4

File tree

6 files changed

+262
-74
lines changed

6 files changed

+262
-74
lines changed

core/src/main/java/com/dtstack/flink/sql/side/FieldInfo.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.flink.api.common.typeinfo.TypeInformation;
2424

2525
import java.io.Serializable;
26+
import java.util.Objects;
2627

2728
/**
2829
* Reason:
@@ -64,4 +65,24 @@ public TypeInformation getTypeInformation() {
6465
public void setTypeInformation(TypeInformation typeInformation) {
6566
this.typeInformation = typeInformation;
6667
}
68+
69+
@Override
70+
public boolean equals(Object o) {
71+
if (this == o) {
72+
return true;
73+
}
74+
75+
if (o == null || getClass() != o.getClass()) {
76+
return false;
77+
}
78+
79+
FieldInfo fieldInfo = (FieldInfo) o;
80+
return Objects.equals(table, fieldInfo.table) &&
81+
Objects.equals(fieldName, fieldInfo.fieldName);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return Objects.hash(table, fieldName);
87+
}
6788
}

core/src/main/java/com/dtstack/flink/sql/side/JoinNodeDealer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,7 @@ public JoinInfo dealJoinNode(SqlJoin joinNode, Set<String> sideTableSet,
190190
tableInfo.setJoinType(joinType);
191191
tableInfo.setCondition(joinNode.getCondition());
192192

193-
if(!needBuildTemp){
194-
return tableInfo;
195-
}
196-
197-
if(tableInfo.getLeftNode().getKind() != AS){
193+
if(tableInfo.getLeftNode().getKind() != AS && needBuildTemp){
198194
extractTemporaryQuery(tableInfo.getLeftNode(), tableInfo.getLeftTableAlias(), (SqlBasicCall) parentWhere,
199195
parentSelectList, queueInfo, joinFieldSet, tableRef);
200196
}else {

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

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@
2323
import java.sql.Timestamp;
2424
import java.text.ParseException;
2525
import java.text.SimpleDateFormat;
26+
import java.time.Instant;
2627
import java.time.LocalDate;
27-
import java.time.LocalDateTime;
2828
import java.time.LocalTime;
29-
import java.time.ZoneId;
30-
import java.time.format.DateTimeFormatter;
31-
import java.time.format.DateTimeParseException;
29+
import java.time.ZoneOffset;
3230
import java.util.Calendar;
3331
import java.util.Date;
3432
import java.util.Locale;
3533
import java.util.SimpleTimeZone;
34+
import java.util.TimeZone;
35+
import java.util.regex.Pattern;
36+
37+
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
3638

3739

3840
/**
@@ -45,9 +47,19 @@
4547
*/
4648
public class DateUtil {
4749

48-
static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
49-
static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
50-
static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
50+
static final String timeZone = "GMT+8";
51+
static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss";
52+
static final String dateFormat = "yyyy-MM-dd";
53+
static final String timeFormat = "HH:mm:ss";
54+
static final SimpleDateFormat datetimeFormatter = new SimpleDateFormat(datetimeFormat);
55+
static final SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
56+
static final SimpleDateFormat timeFormatter = new SimpleDateFormat(timeFormat);
57+
58+
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$");
59+
private static final Pattern DATE = Pattern.compile("^\\d{4}-(?:0[0-9]|1[0-2])-[0-9]{2}$");
60+
61+
private static final int MILLIS_PER_SECOND = 1000;
62+
5163

5264
public static java.sql.Date columnToDate(Object column) {
5365
if(column instanceof String) {
@@ -72,32 +84,23 @@ public static Date stringToDate(String strDate) {
7284
return null;
7385
}
7486
try {
75-
;
76-
return localDateTimetoDate(LocalDateTime.parse(strDate, DATE_TIME_FORMATTER));
77-
} catch (DateTimeParseException ignored) {
87+
return datetimeFormatter.parse(strDate);
88+
} catch (ParseException ignored) {
7889
}
7990

8091
try {
81-
return localDateTimetoDate(LocalDate.parse(strDate, DATE_FORMATTER).atStartOfDay());
82-
} catch (DateTimeParseException ignored) {
92+
return dateFormatter.parse(strDate);
93+
} catch (ParseException ignored) {
8394
}
8495

8596
try {
86-
return localDateTimetoDate(LocalDateTime.of(LocalDate.now(), LocalTime.parse(strDate, TIME_FORMATTER)));
87-
} catch (DateTimeParseException ignored) {
97+
return timeFormatter.parse(strDate);
98+
} catch (ParseException ignored) {
8899
}
89100

90101
throw new RuntimeException("can't parse date");
91102
}
92103

93-
public static Date localDateTimetoDate(LocalDateTime localDateTime){
94-
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
95-
}
96-
97-
public static LocalDateTime dateToLocalDateTime(Date date){
98-
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
99-
}
100-
101104
/**
102105
*
103106
*
@@ -127,9 +130,9 @@ public static long getTodayStart(long day) {
127130
* @return
128131
*/
129132
public static long getTodayStart(long day,String scope) {
130-
if("MS".equals(scope)){
133+
if(scope.equals("MS")){
131134
return getTodayStart(day)*1000;
132-
}else if("S".equals(scope)){
135+
}else if(scope.equals("S")){
133136
return getTodayStart(day);
134137
}else{
135138
return getTodayStart(day);
@@ -165,9 +168,9 @@ public static long getNextDayStart(long day) {
165168
* @return
166169
*/
167170
public static long getNextDayStart(long day,String scope) {
168-
if("MS".equals(scope)){
171+
if(scope.equals("MS")){
169172
return getNextDayStart(day)*1000;
170-
}else if("S".equals(scope)){
173+
}else if(scope.equals("S")){
171174
return getNextDayStart(day);
172175
}else{
173176
return getNextDayStart(day);
@@ -346,7 +349,7 @@ public static String get30DaysLaterByString(String day, String inFormat, String
346349
* @return String
347350
* @throws ParseException
348351
*/
349-
public static String getDateStrToFormat(String day, String inFormat, String outFormat) throws ParseException {
352+
public static String getDateStrTOFormat(String day, String inFormat, String outFormat) throws ParseException {
350353
SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
351354
Date date = sdf.parse(day);
352355
Calendar calendar = Calendar.getInstance();
@@ -355,7 +358,7 @@ public static String getDateStrToFormat(String day, String inFormat, String outF
355358
return dayBefore;
356359
}
357360

358-
public static long getDateMillToFormat(String day, String inFormat) throws ParseException {
361+
public static long getDateMillTOFormat(String day, String inFormat) throws ParseException {
359362
SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
360363
Date date = sdf.parse(day);
361364
Calendar calendar = Calendar.getInstance();
@@ -481,11 +484,11 @@ public static long getMillByDay(int severalDays,String condition) {
481484
if(condition==null){
482485
return getMillToDay(cal,dateT);
483486
}
484-
if("-".equals(condition)){
487+
if(condition.equals("-")){
485488
dateT = (cal.get(Calendar.DATE) - severalDays);
486489
return getMillToDay(cal,dateT);
487490
}
488-
if("+".equals(condition)){
491+
if(condition.equals("+")){
489492
dateT = (cal.get(Calendar.DATE) + severalDays);
490493
return getMillToDay(cal,dateT);
491494
}
@@ -501,11 +504,11 @@ public static long getStampByDay(int severalDays,String condition) {
501504
if(condition==null){
502505
return getStampToDay(cal,dateT);
503506
}
504-
if("-".equals(condition)){
507+
if(condition.equals("-")){
505508
dateT = (cal.get(Calendar.DATE) - severalDays);
506509
return getStampToDay(cal,dateT);
507510
}
508-
if("+".equals(condition)){
511+
if(condition.equals("+")){
509512
dateT = (cal.get(Calendar.DATE) + severalDays);
510513
return getStampToDay(cal,dateT);
511514
}
@@ -586,8 +589,8 @@ public static String getDate(Date date, String format) {
586589
*/
587590
public static long stringToLong(String day, String format) throws ParseException {
588591
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
589-
long date = dateFormat.parse(day).getTime();
590-
return date;
592+
long Date = dateFormat.parse(day).getTime();
593+
return Date;
591594
}
592595

593596
/**
@@ -599,8 +602,8 @@ public static long stringToLong(String day, String format) throws ParseException
599602
public static Date stringToDate(String day, String format) {
600603
try {
601604
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
602-
Date date = dateFormat.parse(day);
603-
return date;
605+
Date Date = dateFormat.parse(day);
606+
return Date;
604607
} catch (ParseException e) {
605608
return new Date();
606609
}
@@ -619,8 +622,8 @@ public static String longToString(long day, String format) throws ParseException
619622
day=day*1000;
620623
}
621624
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
622-
String date = dateFormat.format(day);
623-
return date;
625+
String Date = dateFormat.format(day);
626+
return Date;
624627
}
625628

626629
/**
@@ -774,12 +777,59 @@ public static java.sql.Timestamp columnToTimestamp(Object column) {
774777
}
775778

776779
public static String dateToString(Date date) {
777-
LocalDateTime localDateTime = dateToLocalDateTime(date);
778-
return localDateTime.format(DATE_FORMATTER);
780+
return dateFormatter.format(date);
779781
}
780782

781783
public static String timestampToString(Date date) {
782-
LocalDateTime localDateTime = dateToLocalDateTime(date);
783-
return localDateTime.format(DATE_TIME_FORMATTER);
784+
return datetimeFormatter.format(date);
784785
}
786+
787+
788+
public static Timestamp getTimestampFromStr(String timeStr) {
789+
if (DATETIME.matcher(timeStr).matches()) {
790+
Instant instant = Instant.from(ISO_INSTANT.parse(timeStr));
791+
return new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND);
792+
} else {
793+
java.sql.Date date = null;
794+
try {
795+
date = new java.sql.Date(datetimeFormatter.parse(timeStr).getTime());
796+
} catch (ParseException e) {
797+
throw new RuntimeException("getTimestampFromStr error data is " + timeStr);
798+
}
799+
return new Timestamp(date.getTime());
800+
}
801+
}
802+
803+
public static java.sql.Date getDateFromStr(String dateStr) {
804+
// 2020-01-01 format
805+
if (DATE.matcher(dateStr).matches()) {
806+
// convert from local date to instant
807+
Instant instant = LocalDate.parse(dateStr).atTime(LocalTime.of(0, 0, 0, 0)).toInstant(ZoneOffset.UTC);
808+
// calculate the timezone offset in millis
809+
int offset = TimeZone.getDefault().getOffset(instant.toEpochMilli());
810+
// need to remove the offset since time has no TZ component
811+
return new java.sql.Date(instant.toEpochMilli() - offset);
812+
} else if (DATETIME.matcher(dateStr).matches()) {
813+
// 2020-01-01T12:12:12Z format
814+
Instant instant = Instant.from(ISO_INSTANT.parse(dateStr));
815+
return new java.sql.Date(instant.toEpochMilli());
816+
} else {
817+
try {
818+
// 2020-01-01 12:12:12.0 format
819+
return new java.sql.Date(datetimeFormatter.parse(dateStr).getTime());
820+
} catch (ParseException e) {
821+
throw new RuntimeException("String convert to Date fail.");
822+
}
823+
}
824+
}
825+
826+
827+
public static String getStringFromTimestamp(Timestamp timestamp) {
828+
return datetimeFormatter.format(timestamp);
829+
}
830+
831+
public static String getStringFromDate(java.sql.Date date) {
832+
return dateFormatter.format(date);
833+
}
834+
785835
}

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* limitations under the License.
1717
*/
1818

19-
2019

2120
package com.dtstack.flink.sql.util;
2221

@@ -27,6 +26,16 @@
2726
import java.text.ParseException;
2827
import java.text.SimpleDateFormat;
2928

29+
30+
import java.time.Instant;
31+
import java.time.LocalDate;
32+
import java.time.LocalTime;
33+
import java.time.ZoneOffset;
34+
import java.util.TimeZone;
35+
import java.util.regex.Pattern;
36+
37+
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
38+
3039
/**
3140
* Convert val to specified numeric type
3241
* Date: 2017/4/21
@@ -35,7 +44,6 @@
3544
*/
3645

3746
public class MathUtil {
38-
3947
public static Long getLongVal(Object obj) {
4048
if (obj == null) {
4149
return null;
@@ -126,12 +134,12 @@ public static Double getDoubleVal(Object obj) {
126134
return Double.valueOf((String) obj);
127135
} else if (obj instanceof Float) {
128136
return ((Float) obj).doubleValue();
129-
} else if (obj instanceof Double){
137+
} else if (obj instanceof Double) {
130138
return (Double) obj;
131-
}else if (obj instanceof BigDecimal) {
139+
} else if (obj instanceof BigDecimal) {
132140
return ((BigDecimal) obj).doubleValue();
133-
}else if (obj instanceof Integer){
134-
return ((Integer)obj).doubleValue();
141+
} else if (obj instanceof Integer) {
142+
return ((Integer) obj).doubleValue();
135143
}
136144

137145
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Double.");
@@ -229,12 +237,7 @@ public static Date getDate(Object obj) {
229237
return null;
230238
}
231239
if (obj instanceof String) {
232-
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
233-
try {
234-
return new Date(format.parse((String) obj).getTime());
235-
} catch (ParseException e) {
236-
throw new RuntimeException("String convert to Date fail.");
237-
}
240+
return DateUtil.getDateFromStr((String) obj);
238241
} else if (obj instanceof Timestamp) {
239242
return new Date(((Timestamp) obj).getTime());
240243
} else if (obj instanceof Date) {
@@ -243,6 +246,8 @@ public static Date getDate(Object obj) {
243246
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date.");
244247
}
245248

249+
250+
246251
public static Timestamp getTimestamp(Object obj) {
247252
if (obj == null) {
248253
return null;
@@ -252,8 +257,9 @@ public static Timestamp getTimestamp(Object obj) {
252257
} else if (obj instanceof Date) {
253258
return new Timestamp(((Date) obj).getTime());
254259
} else if (obj instanceof String) {
255-
return new Timestamp(getDate(obj).getTime());
260+
return DateUtil.getTimestampFromStr(obj.toString());
256261
}
257262
throw new RuntimeException("not support type of " + obj.getClass() + " convert to Date.");
258263
}
264+
259265
}

0 commit comments

Comments
 (0)