Skip to content

Commit

Permalink
add master forward time zone support
Browse files Browse the repository at this point in the history
  • Loading branch information
HangyuanLiu committed Aug 26, 2019
1 parent a3f5672 commit 9ac46ab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
47 changes: 35 additions & 12 deletions fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import java.io.DataInput;
Expand All @@ -53,13 +54,35 @@ public class DateLiteral extends LiteralExpr {
new DateLiteral(1900, 1, 1, 0, 0, 0);
private static final DateLiteral MAX_DATETIME =
new DateLiteral(9999, 12, 31, 23, 59, 59);

private static DateTimeFormatter DATE_TIME_FORMATTER = null;
private static DateTimeFormatter DATE_FORMATTER = null;
static {
try {
DATE_TIME_FORMATTER = formatBuilder("%Y-%m-%d %H:%i:%s").toFormatter();
DATE_FORMATTER = formatBuilder("%Y-%m-%d").toFormatter();
} catch (AnalysisException e) {
LOG.error("invalid date format", e);
System.exit(-1);
}
}

//Regex used to determine if the TIME field exists int date_format
private static final Pattern HAS_TIME_PART = Pattern.compile("^.*[HhIiklrSsT]+.*$");
//Date Literal persist type in meta
private enum DateLiteralType {
DATETIME,
DATE
}
private enum DateLiteralType {
DATETIME(0),
DATE(1);

private final int value;
private DateLiteralType(int value) {
this.value = value;
}

public int value() {
return value;
}
}

private DateLiteral() {
super();
Expand Down Expand Up @@ -131,9 +154,9 @@ private void init(String s, Type type) throws AnalysisException {
Preconditions.checkArgument(type.isDateType());
LocalDateTime dateTime;
if (type == Type.DATE) {
dateTime = formatBuilder("%Y-%m-%d").toFormatter().parseLocalDateTime(s);
dateTime = DATE_FORMATTER.parseLocalDateTime(s);
} else {
dateTime = formatBuilder("%Y-%m-%d %H:%i:%s").toFormatter().parseLocalDateTime(s);
dateTime = DATE_TIME_FORMATTER.parseLocalDateTime(s);
}
year = dateTime.getYear();
month = dateTime.getMonthOfYear();
Expand Down Expand Up @@ -267,9 +290,9 @@ public void write(DataOutput out) throws IOException {
super.write(out);
//set flag bit in meta, 0 is DATETIME and 1 is DATE
if (this.type == Type.DATETIME) {
out.writeShort(DateLiteralType.DATETIME.ordinal());
out.writeShort(DateLiteralType.DATETIME.value());
} else if (this.type == Type.DATE) {
out.writeShort(DateLiteralType.DATE.ordinal());
out.writeShort(DateLiteralType.DATE.value());
} else {
throw new IOException("Error date literal type : " + type);
}
Expand Down Expand Up @@ -301,9 +324,9 @@ public void readFields(DataInput in) throws IOException {
if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_60) {
short date_literal_type = in.readShort();
fromPackedDatetime(in.readLong());
if (date_literal_type == DateLiteralType.DATETIME.ordinal()) {
if (date_literal_type == DateLiteralType.DATETIME.value()) {
this.type = Type.DATETIME;
} else if (date_literal_type == DateLiteralType.DATE.ordinal()) {
} else if (date_literal_type == DateLiteralType.DATE.value()) {
this.type = Type.DATE;
} else {
throw new IOException("Error date literal type : " + type);
Expand Down Expand Up @@ -482,9 +505,9 @@ public DateLiteral plusDays(int day) throws AnalysisException {
LocalDateTime dateTime;
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
if (type == Type.DATE) {
dateTime = formatBuilder("%Y-%m-%d").toFormatter().parseLocalDateTime(getStringValue()).plusDays(day);
dateTime = DATE_FORMATTER.parseLocalDateTime(getStringValue()).plusDays(day);
} else {
dateTime = formatBuilder("%Y-%m-%d %H-%i-%s").toFormatter().parseLocalDateTime(getStringValue()).plusDays(day);
dateTime = DATE_TIME_FORMATTER.parseLocalDateTime(getStringValue()).plusDays(day);
}
DateLiteral dateLiteral = new DateLiteral(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
dateTime.getHourOfDay(), dateTime.getMinuteOfHour(), dateTime.getSecondOfMinute());
Expand Down
3 changes: 3 additions & 0 deletions fe/src/main/java/org/apache/doris/qe/ConnectProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) {
if (request.isSetUser_ip()) {
ctx.setRemoteIP(request.getUser_ip());
}
if (request.isSetTime_zone()) {
ctx.getSessionVariable().setTimeZone(request.getTime_zone());
}

ctx.setThreadLocalInfo();

Expand Down
1 change: 1 addition & 0 deletions fe/src/main/java/org/apache/doris/qe/MasterOpExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void forward() throws Exception {
params.setExecMemLimit(ctx.getSessionVariable().getMaxExecMemByte());
params.setQueryTimeout(ctx.getSessionVariable().getQueryTimeoutS());
params.setUser_ip(ctx.getRemoteIP());
params.setTime_zone(ctx.getSessionVariable().getTimeZone());

LOG.info("Forward statement {} to Master {}", ctx.getStmtId(), thriftAddress);

Expand Down
1 change: 1 addition & 0 deletions gensrc/thrift/FrontendService.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ struct TMasterOpRequest {
6: optional i64 execMemLimit
7: optional i32 queryTimeout
8: optional string user_ip
9: optional string time_zone
}

struct TColumnDefinition {
Expand Down

0 comments on commit 9ac46ab

Please sign in to comment.