Skip to content

Commit 60292c0

Browse files
docetewuchong
authored andcommitted
[FLINK-13280][table-planner-blink] Revert blink changes in DateTimeUtils, and keep it same as flink version
This closes apache#9124
1 parent f4e71f9 commit 60292c0

File tree

6 files changed

+254
-196
lines changed

6 files changed

+254
-196
lines changed

flink-table/flink-table-planner-blink/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java

+12-185
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.calcite.avatica.util;
1818

19-
import org.apache.flink.table.api.ValidationException;
20-
2119
import java.text.DateFormat;
2220
import java.text.NumberFormat;
2321
import java.text.ParsePosition;
@@ -69,9 +67,6 @@ private DateTimeUtils() {}
6967
/** The Java default time zone. */
7068
public static final TimeZone DEFAULT_ZONE = TimeZone.getDefault();
7169

72-
/** User's configured time zone*/
73-
private static volatile TimeZone userZone = UTC_ZONE;
74-
7570
/**
7671
* The number of milliseconds in a second.
7772
*/
@@ -95,19 +90,6 @@ private DateTimeUtils() {}
9590
*/
9691
public static final long MILLIS_PER_DAY = 86400000; // = 24 * 60 * 60 * 1000
9792

98-
/**
99-
* The number of microseconds in a day.
100-
*/
101-
public static final long MICROS_PER_DAY = 86400000000L; // = 24 * 60 * 60 * 1000 * 1000
102-
103-
/**
104-
* The number of seconds in a day.
105-
*
106-
* <p>This is the modulo 'mask' used when converting
107-
* TIMESTAMP values to DATE and TIME values.
108-
*/
109-
public static final long SECONDS_PER_DAY = 86400; // = 24 * 60 * 60
110-
11193
/**
11294
* Calendar set to the epoch (1970-01-01 00:00:00 UTC). Useful for
11395
* initializing other values. Calendars are not immutable, so be careful not
@@ -122,13 +104,6 @@ private DateTimeUtils() {}
122104

123105
//~ Methods ----------------------------------------------------------------
124106

125-
public static TimeZone getUserZone() {
126-
return userZone;
127-
}
128-
public static void setUserZone(TimeZone zone) {
129-
userZone = zone;
130-
}
131-
132107
/**
133108
* Parses a string using {@link SimpleDateFormat} and a given pattern. This
134109
* method parses a string at the specified parse position and if successful,
@@ -309,48 +284,15 @@ public static SimpleDateFormat newDateFormat(String format) {
309284
return sdf;
310285
}
311286

312-
/** Helper for CAST({timestamp} Or {date} or {time} AS VARCHAR(n)). */
313-
public static String unixDateTimeToString(Object o, TimeZone tz) {
314-
int offset = tz.getOffset(Calendar.ZONE_OFFSET);
315-
if (tz.useDaylightTime()) {
316-
offset = tz.getOffset(((java.util.Date) o).getTime());
317-
}
318-
if (o instanceof Date) {
319-
long time = ((Date) o).getTime();
320-
time = time + UTC_ZONE.getOffset(time);
321-
if (o instanceof java.sql.Date) {
322-
return unixDateToString((int) (time / MILLIS_PER_DAY) + offset);
323-
}
324-
if (o instanceof java.sql.Time) {
325-
return unixTimeToString(((int) (time % MILLIS_PER_DAY) + offset) % (int) MILLIS_PER_DAY);
326-
}
327-
if (o instanceof java.sql.Timestamp) {
328-
return unixTimestampToString(time + offset, 3);
329-
}
330-
}
331-
return o.toString();
332-
}
333-
334287
/** Helper for CAST({timestamp} AS VARCHAR(n)). */
335288
public static String unixTimestampToString(long timestamp) {
336289
return unixTimestampToString(timestamp, 0);
337290
}
338291

339-
/**
340-
* Returns the formatted timestamp string
341-
* now, it is only for Timestamp Literal (TimestampString)
342-
* don't use it for scalar functions
343-
*/
344292
public static String unixTimestampToString(long timestamp, int precision) {
345293
final StringBuilder buf = new StringBuilder(17);
346-
347-
// Because unixTimeToString does't take offset into account
348-
// so, manually adjust the offset here
349-
long tzOffset = userZone.getOffset(timestamp);
350-
timestamp += tzOffset;
351-
352-
int date = (int) ((timestamp) / MILLIS_PER_DAY);
353-
int time = (int) ((timestamp) % MILLIS_PER_DAY);
294+
int date = (int) (timestamp / MILLIS_PER_DAY);
295+
int time = (int) (timestamp % MILLIS_PER_DAY);
354296
if (time < 0) {
355297
--date;
356298
time += MILLIS_PER_DAY;
@@ -485,7 +427,7 @@ public static StringBuilder number(StringBuilder buf, int v, int n) {
485427
}
486428

487429
public static int digitCount(int v) {
488-
for (int n = 1;; n++) {
430+
for (int n = 1; true; n++) {
489431
v /= 10;
490432
if (v == 0) {
491433
return n;
@@ -674,178 +616,63 @@ private static void fraction(StringBuilder buf, int scale, long ms) {
674616
}
675617
}
676618

677-
private static boolean isInteger(String s) {
678-
boolean isInt = s.length() > 0;
679-
for(int i = 0; i < s.length(); i++)
680-
{
681-
if(s.charAt(i) < '0' || s.charAt(i) > '9') {
682-
isInt = false;
683-
break;
684-
}
685-
}
686-
return isInt;
687-
}
688-
689-
private static boolean isLeapYear(int s) {
690-
return s % 400 == 0 || (s % 4 == 0 && s % 100 != 0);
691-
}
692-
693-
private static boolean isIllegalDate(int y, int m, int d) {
694-
int[] monthOf31Days = new int[]{1, 3, 5, 7, 8, 10, 12};
695-
if(y < 0 || y > 9999 || m < 1 || m > 12 || d < 1 || d > 31) {
696-
return false;
697-
}
698-
if(m == 2 && d > 28) {
699-
if(!(isLeapYear(y) && d == 29)) {
700-
return false;
701-
}
702-
}
703-
if(d == 31) {
704-
for(int i: monthOf31Days) {
705-
if(i == m) {
706-
return true;
707-
}
708-
}
709-
return false;
710-
}
711-
return true;
712-
}
713-
714-
public static Integer dateStringToUnixDate(String s) {
715-
// allow timestamp str to date, e.g. 2017-12-12 09:30:00.0
716-
int ws1 = s.indexOf(" ");
717-
if (ws1 > 0) {
718-
s = s.substring(0, ws1);
719-
}
619+
public static int dateStringToUnixDate(String s) {
720620
int hyphen1 = s.indexOf('-');
721621
int y;
722622
int m;
723623
int d;
724624
if (hyphen1 < 0) {
725-
if(!isInteger(s.trim())) {
726-
return null;
727-
}
728625
y = Integer.parseInt(s.trim());
729626
m = 1;
730627
d = 1;
731628
} else {
732-
if(!isInteger(s.substring(0, hyphen1).trim())) {
733-
return null;
734-
}
735629
y = Integer.parseInt(s.substring(0, hyphen1).trim());
736630
final int hyphen2 = s.indexOf('-', hyphen1 + 1);
737631
if (hyphen2 < 0) {
738-
if(!isInteger(s.substring(hyphen1 + 1).trim())) {
739-
return null;
740-
}
741632
m = Integer.parseInt(s.substring(hyphen1 + 1).trim());
742633
d = 1;
743634
} else {
744-
if(!isInteger(s.substring(hyphen1 + 1, hyphen2).trim())) {
745-
return null;
746-
}
747635
m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim());
748-
if(!isInteger(s.substring(hyphen2 + 1).trim())) {
749-
return null;
750-
}
751636
d = Integer.parseInt(s.substring(hyphen2 + 1).trim());
752637
}
753638
}
754-
if(!isIllegalDate(y, m, d)) {
755-
return null;
756-
}
757639
return ymdToUnixDate(y, m, d);
758640
}
759641

760-
public static Integer timeStringToUnixDate(String v) {
642+
public static int timeStringToUnixDate(String v) {
761643
return timeStringToUnixDate(v, 0);
762644
}
763645

764-
public static Integer timeStringToUnixDate(String v, int start) {
646+
public static int timeStringToUnixDate(String v, int start) {
765647
final int colon1 = v.indexOf(':', start);
766-
//timezone hh:mm:ss[.ssssss][[+|-]hh:mm:ss]
767-
//refer https://www.w3.org/TR/NOTE-datetime
768-
int timezoneHour;
769-
int timezoneMinute;
770648
int hour;
771649
int minute;
772650
int second;
773651
int milli;
774-
int operator = -1;
775-
int end = v.length();
776-
int timezone = v.indexOf('-', start);
777-
if (timezone < 0)
778-
{
779-
timezone = v.indexOf('+', start);
780-
operator = 1;
781-
}
782-
if (timezone < 0) {
783-
timezoneHour = 0;
784-
timezoneMinute = 0;
785-
} else {
786-
end = timezone;
787-
final int colon3 = v.indexOf(':', timezone);
788-
if (colon3 < 0) {
789-
if(!isInteger(v.substring(timezone + 1).trim())) {
790-
return null;
791-
}
792-
timezoneHour = Integer.parseInt(v.substring(timezone + 1).trim());
793-
timezoneMinute = 0;
794-
} else {
795-
if(!isInteger(v.substring(timezone + 1, colon3).trim())) {
796-
return null;
797-
}
798-
timezoneHour = Integer.parseInt(v.substring(timezone + 1, colon3).trim());
799-
if(!isInteger(v.substring(colon3 + 1).trim())) {
800-
return null;
801-
}
802-
timezoneMinute = Integer.parseInt(v.substring(colon3 + 1).trim());
803-
}
804-
}
805652
if (colon1 < 0) {
806-
if(!isInteger(v.substring(start, end).trim())) {
807-
return null;
808-
}
809-
hour = Integer.parseInt(v.substring(start, end).trim());
653+
hour = Integer.parseInt(v.trim());
810654
minute = 1;
811655
second = 1;
812656
milli = 0;
813657
} else {
814-
if(!isInteger(v.substring(start, colon1).trim())) {
815-
return null;
816-
}
817658
hour = Integer.parseInt(v.substring(start, colon1).trim());
818659
final int colon2 = v.indexOf(':', colon1 + 1);
819660
if (colon2 < 0) {
820-
if(!isInteger(v.substring(colon1 + 1, end).trim())) {
821-
return null;
822-
}
823-
minute = Integer.parseInt(v.substring(colon1 + 1, end).trim());
661+
minute = Integer.parseInt(v.substring(colon1 + 1).trim());
824662
second = 1;
825663
milli = 0;
826664
} else {
827-
if(!isInteger(v.substring(colon1 + 1, colon2).trim())) {
828-
return null;
829-
}
830665
minute = Integer.parseInt(v.substring(colon1 + 1, colon2).trim());
831666
int dot = v.indexOf('.', colon2);
832667
if (dot < 0) {
833-
if(!isInteger(v.substring(colon2 + 1, end).trim())) {
834-
return null;
835-
}
836-
second = Integer.parseInt(v.substring(colon2 + 1, end).trim());
668+
second = Integer.parseInt(v.substring(colon2 + 1).trim());
837669
milli = 0;
838670
} else {
839-
if(!isInteger(v.substring(colon2 + 1, dot).trim())) {
840-
return null;
841-
}
842671
second = Integer.parseInt(v.substring(colon2 + 1, dot).trim());
843-
milli = parseFraction(v.substring(dot + 1, end).trim(), 100);
672+
milli = parseFraction(v.substring(dot + 1).trim(), 100);
844673
}
845674
}
846675
}
847-
hour += operator * timezoneHour;
848-
minute += operator * timezoneMinute;
849676
return hour * (int) MILLIS_PER_HOUR
850677
+ minute * (int) MILLIS_PER_MINUTE
851678
+ second * (int) MILLIS_PER_SECOND
@@ -976,7 +803,7 @@ public static int unixTimeExtract(TimeUnitRange range, int time) {
976803
final int seconds = time / (int) MILLIS_PER_SECOND;
977804
return seconds % 60;
978805
default:
979-
throw new ValidationException("unit " + range + " can not be applied to time variable");
806+
throw new AssertionError(range);
980807
}
981808
}
982809

@@ -1133,7 +960,7 @@ public static int subtractMonths(int date0, int date1) {
1133960
// Start with an estimate.
1134961
// Since no month has more than 31 days, the estimate is <= the true value.
1135962
int m = (date0 - date1) / 31;
1136-
for (;;) {
963+
while (true) {
1137964
int date2 = addMonths(date1, m);
1138965
if (date2 >= date0) {
1139966
return m;

flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/codegen/calls/BuiltInMethods.scala

+5
Original file line numberDiff line numberDiff line change
@@ -485,4 +485,9 @@ object BuiltInMethods {
485485
"convertTz",
486486
classOf[String], classOf[String], classOf[String], classOf[String])
487487

488+
val STRING_TO_DATE = Types.lookupMethod(
489+
classOf[SqlDateTimeUtils], "dateStringToUnixDate", classOf[String])
490+
491+
val STRING_TO_TIME = Types.lookupMethod(
492+
classOf[SqlDateTimeUtils], "timeStringToUnixDate", classOf[String])
488493
}

flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/codegen/calls/ScalarOperatorGens.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ object ScalarOperatorGens {
892892
operand,
893893
resultNullable = true) {
894894
operandTerm =>
895-
s"${qualifyMethod(BuiltInMethod.STRING_TO_DATE.method)}($operandTerm.toString())"
895+
s"${qualifyMethod(BuiltInMethods.STRING_TO_DATE)}($operandTerm.toString())"
896896
}
897897

898898
// String -> Time
@@ -903,7 +903,7 @@ object ScalarOperatorGens {
903903
operand,
904904
resultNullable = true) {
905905
operandTerm =>
906-
s"${qualifyMethod(BuiltInMethod.STRING_TO_TIME.method)}($operandTerm.toString())"
906+
s"${qualifyMethod(BuiltInMethods.STRING_TO_TIME)}($operandTerm.toString())"
907907
}
908908

909909
// String -> Timestamp
@@ -2146,9 +2146,9 @@ object ScalarOperatorGens {
21462146
operandTerm: String): String =
21472147
targetType.getTypeRoot match {
21482148
case DATE =>
2149-
s"${qualifyMethod(BuiltInMethod.STRING_TO_DATE.method)}($operandTerm.toString())"
2149+
s"${qualifyMethod(BuiltInMethods.STRING_TO_DATE)}($operandTerm.toString())"
21502150
case TIME_WITHOUT_TIME_ZONE =>
2151-
s"${qualifyMethod(BuiltInMethod.STRING_TO_TIME.method)}($operandTerm.toString())"
2151+
s"${qualifyMethod(BuiltInMethods.STRING_TO_TIME)}($operandTerm.toString())"
21522152
case TIMESTAMP_WITHOUT_TIME_ZONE =>
21532153
s"""
21542154
|${qualifyMethod(BuiltInMethods.STRING_TO_TIMESTAMP)}($operandTerm.toString())

flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/codegen/calls/StringCallGen.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ object StringCallGen {
184184
// Date/Time & BinaryString Converting -- start
185185

186186
case TO_DATE if operands.size == 1 && isCharacterString(operands.head.resultType) =>
187-
methodGen(BuiltInMethod.STRING_TO_DATE.method)
187+
methodGen(BuiltInMethods.STRING_TO_DATE)
188188

189189
case TO_DATE if operands.size == 2 &&
190190
isCharacterString(operands.head.resultType) &&

0 commit comments

Comments
 (0)