morningman commented on a change in pull request #1644: Refactor DateLiteral
class in FE
URL: https://github.com/apache/incubator-doris/pull/1644#discussion_r315625125
##########
File path: fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
##########
@@ -187,18 +222,266 @@ protected Expr uncheckedCastTo(Type targetType) throws
AnalysisException {
@Override
public void write(DataOutput out) throws IOException {
super.write(out);
- out.writeLong(date.getTime());
+ long ymd = ((year * 13 + month) << 5) | day;
+ long hms = (hour << 12) | (minute << 6) | second;
+ long packed_datetime = ((ymd << 17) | hms) << 24 + microsecond;
+ out.writeLong(packed_datetime);
}
@Override
public void readFields(DataInput in) throws IOException {
super.readFields(in);
- date = new Date(in.readLong());
+ if (Catalog.getCurrentCatalogJournalVersion() >=
FeMetaVersion.VERSION_59) {
+ long packed_time = in.readLong();
+ microsecond = (packed_time % (1L << 24));
+ long ymdhms = (packed_time >> 24);
+ long ymd = ymdhms >> 17;
+ long hms = ymdhms % (1 << 17);
+
+ day = ymd % (1 << 5);
+ long ym = ymd >> 5;
+ month = ym % 13;
+ year = ym / 13;
+ year %= 10000;
+ second = hms % (1 << 6);
+ minute = (hms >> 6) % (1 << 6);
+ hour = (hms >> 12);
+ this.type = Type.DATETIME;
+ } else {
+ Date date = new Date(in.readLong());
+ String date_str = TimeUtils.format(date, Type.DATETIME);
+ try {
+ init(date_str, Type.DATETIME);
+ } catch (AnalysisException ex) {
+ throw new IOException(ex.getMessage());
+ }
+ }
}
public static DateLiteral read(DataInput in) throws IOException {
DateLiteral literal = new DateLiteral();
literal.readFields(in);
return literal;
}
+
+ public long unixTime(TimeZone timeZone) throws ParseException {
+ SimpleDateFormat dateFormat;
+ if (type == Type.DATE) {
+ dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ } else {
+ dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ }
+ dateFormat.setTimeZone(timeZone);
+ return dateFormat.parse(getStringValue()).getTime();
+ }
+
+ public static DateLiteral dateParser(String date, String pattern) throws
AnalysisException{
+ DateLiteral dateLiteral = new DateLiteral();
+ if(HAS_TIME_PART.matcher(pattern).matches()) {
+ dateLiteral.setType(Type.DATETIME);
+ } else {
+ dateLiteral.setType(Type.DATE);
+ }
+
+ LocalDateTime dateTime =
FormatBuilder(pattern).toFormatter().parseLocalDateTime(date);
+ dateLiteral.setYear(dateTime.getYear());
+ dateLiteral.setMonth(dateTime.getMonthOfYear());
+ dateLiteral.setDay(dateTime.getDayOfMonth());
+ dateLiteral.setHour(dateTime.getHourOfDay());
+ dateLiteral.setMinute(dateTime.getMinuteOfHour());
+ dateLiteral.setSecond(dateTime.getSecondOfMinute());
+ return dateLiteral;
+ }
+
+ public String dateFormat(String pattern) throws AnalysisException{
+ DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
+ if (type == Type.DATE) {
+ builder.appendYear(4, 4).appendLiteral("-").
+
appendMonthOfYear(2).appendLiteral("-").appendDayOfMonth(2);
+ } else {
+ builder.appendYear(4, 4).appendLiteral("-").
+ appendMonthOfYear(2).appendLiteral("-")
+ .appendDayOfMonth(2).appendLiteral(" ")
+ .appendHourOfDay(2).appendLiteral(":")
+ .appendMinuteOfHour(2).appendLiteral(":")
+ .appendSecondOfMinute(2);
+ }
+
+ return builder.toFormatter().parseLocalDateTime(getStringValue())
+ .toString(FormatBuilder(pattern).toFormatter());
+ }
+
+ private static DateTimeFormatterBuilder FormatBuilder(String pattern)
throws AnalysisException{
+ DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
+ boolean escaped = false;
+ for (int i = 0; i < pattern.length(); i++) {
+ char character = pattern.charAt(i);
+ if (escaped) {
+ switch (character) {
+ case 'a': // %a Abbreviated weekday name (Sun..Sat)
+ builder.appendDayOfWeekShortText();
+ break;
+ case 'b': // %b Abbreviated month name (Jan..Dec)
+ builder.appendMonthOfYearShortText();
+ break;
+ case 'c': // %c Month, numeric (0..12)
+ builder.appendMonthOfYear(1);
+ break;
+ case 'd': // %d Day of the month, numeric (00..31)
+ builder.appendDayOfMonth(2);
+ break;
+ case 'e': // %e Day of the month, numeric (0..31)
+ builder.appendDayOfMonth(1);
+ break;
+ case 'H': // %H Hour (00..23)
+ builder.appendHourOfDay(2);
+ break;
+ case 'h': // %h Hour (01..12)
+ case 'I': // %I Hour (01..12)
+ builder.appendClockhourOfHalfday(2);
+ break;
+ case 'i': // %i Minutes, numeric (00..59)
+ builder.appendMinuteOfHour(2);
+ break;
+ case 'j': // %j Day of year (001..366)
+ builder.appendDayOfYear(3);
+ break;
+ case 'k': // %k Hour (0..23)
+ builder.appendHourOfDay(1);
+ break;
+ case 'l': // %l Hour (1..12)
+ builder.appendClockhourOfHalfday(1);
+ break;
+ case 'M': // %M Month name (January..December)
+ builder.appendMonthOfYearText();
+ break;
+ case 'm': // %m Month, numeric (00..12)
+ builder.appendMonthOfYear(2);
+ break;
+ case 'p': // %p AM or PM
+ builder.appendHalfdayOfDayText();
+ break;
+ case 'r': // %r Time, 12-hour (hh:mm:ss followed by AM or
PM)
+ builder.appendClockhourOfHalfday(2)
+ .appendLiteral(':')
+ .appendMinuteOfHour(2)
+ .appendLiteral(':')
+ .appendSecondOfMinute(2)
+ .appendLiteral(' ')
+ .appendHalfdayOfDayText();
+ break;
+ case 'S': // %S Seconds (00..59)
+ case 's': // %s Seconds (00..59)
+ builder.appendSecondOfMinute(2);
+ break;
+ case 'T': // %T Time, 24-hour (hh:mm:ss)
+ builder.appendHourOfDay(2)
+ .appendLiteral(':')
+ .appendMinuteOfHour(2)
+ .appendLiteral(':')
+ .appendSecondOfMinute(2);
+ break;
+ case 'v': // %v Week (01..53), where Monday is the first
day of the week; used with %x
+ builder.appendWeekOfWeekyear(2);
+ break;
+ case 'x': // %x Year for the week, where Monday is the
first day of the week, numeric, four digits; used with %v
+ builder.appendWeekyear(4, 4);
+ break;
+ case 'W': // %W Weekday name (Sunday..Saturday)
+ builder.appendDayOfWeekText();
+ break;
+ case 'Y': // %Y Year, numeric, four digits
+ builder.appendYear(4, 4);
+ break;
+ case 'y': // %y Year, numeric (two digits)
+ builder.appendTwoDigitYear(2020);
+ break;
+ case 'f': // %f Microseconds (000000..999999)
+ case 'w': // %w Day of the week (0=Sunday..6=Saturday)
+ case 'U': // %U Week (00..53), where Sunday is the first
day of the week
+ case 'u': // %u Week (00..53), where Monday is the first
day of the week
+ case 'V': // %V Week (01..53), where Sunday is the first
day of the week; used with %X
+ case 'X': // %X Year for the week where Sunday is the
first day of the week, numeric, four digits; used with %V
+ case 'D': // %D Day of the month with English suffix (0th,
1st, 2nd, 3rd, …)
+ throw new AnalysisException(String.format("%%%s not
supported in date format string", character));
+ case '%': // %% A literal "%" character
+ builder.appendLiteral('%');
+ break;
+ default: // %<x> The literal character represented by <x>
+ builder.appendLiteral(character);
+ break;
+ }
+ escaped = false;
+ } else if (character == '%') {
+ escaped = true;
+ } else {
+ builder.appendLiteral(character);
+ }
+ }
+ return builder;
+ }
+
+ public long getHour() {
+ return hour;
+ }
+
+ public void setHour(long hour) {
Review comment:
Do we really need these `set` or `get` functions? If not, it is better to
remove them.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]