Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DateLiteral class in FE #1644

Merged
merged 26 commits into from
Aug 27, 2019

Conversation

HangyuanLiu
Copy link
Contributor

@HangyuanLiu HangyuanLiu commented Aug 15, 2019

1、Add fe time zone function support
2、Refactor DateLiteral class in FE
#1583

@HangyuanLiu HangyuanLiu reopened this Aug 19, 2019
@HangyuanLiu HangyuanLiu changed the title Fe time zone function Refactor DateLiteral class in FE Aug 19, 2019
…to fe-time-zone-function

Conflicts:
	fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java
@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch from 3438f2f to 98d6668 Compare August 19, 2019 13:48
}

@Override
public boolean isMinValue() {
switch (type.getPrimitiveType()) {
case DATE:
return this.date.compareTo(TimeUtils.MIN_DATE) == 0;
return this.getStringValue().compareTo("1900-01-01") == 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think min value and max value can be a static final member of DateLiteral?So that you can just use "=" to check this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can create a static for min/max value. But we can't not use == to check if it is a minimal value. Because user can construct another object with minimal value string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

We can create a static for min/max value. But we can't not use == to check if it is a minimal value. Because user can construct another object with minimal value string

ok

.toString(FormatBuilder(pattern).toFormatter());
}

private static DateTimeFormatterBuilder FormatBuilder(String pattern) throws AnalysisException{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static DateTimeFormatterBuilder FormatBuilder(String pattern) throws AnalysisException{
private static DateTimeFormatterBuilder formatBuilder(String pattern) throws AnalysisException{

return hour;
}

public void setHour(long hour) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need these set or get functions? If not, it is better to remove them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

date = new Date(in.readLong());
if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_59) {
long packed_time = in.readLong();
microsecond = (packed_time % (1L << 24));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it a function

second = hms % (1 << 6);
minute = (hms >> 6) % (1 << 6);
hour = (hms >> 12);
this.type = Type.DATETIME;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why DATETIME? I think it may be DATE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dates read from the meta are all datetime types, and Doris explicitly specifies type DATE/DATETIME by setType()

return dateLiteral;
}

public String dateFormat(String pattern) throws AnalysisException{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add comments for this function. It's better to give some example that what function do

DateLiteral dateLiteral = (DateLiteral) date;

DateLiteral result = new DateLiteral(dateLiteral);
result.setDay(dateLiteral.getDay() + day.getLongValue());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about "2010-01-31" + 10

…tion

# Conflicts:
#	fe/src/main/java/org/apache/doris/common/FeMetaVersion.java
#	fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java
@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch from 12e3678 to a4427f7 Compare August 21, 2019 06:36
}

@Override
public void readFields(DataInput in) throws IOException {
super.readFields(in);
date = new Date(in.readLong());
if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_59) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERSION_59 is used by support strict mode, change to VERSION_60

@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch from 298dd35 to 58093b8 Compare August 21, 2019 14:27
return new DateLiteral(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth());
} else {
return new DateLiteral(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(),
dateTime.getHourOfDay(), dateTime.getMinuteOfHour(), dateTime.getSecondOfMinute());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move this logic to DateLiteral. And I think we can make formatter static which is thread-safe.

date = new Date(in.readLong());
if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_60) {
fromPackedDatetime(in.readLong());
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should persist type for later use. Or if we want to persist Date, we will have to change the format we use.

@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch from 58093b8 to 266e74b Compare August 25, 2019 03:18
@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch 2 times, most recently from 9ac46ab to 63071d4 Compare August 26, 2019 14:28
@@ -56,8 +49,13 @@
*/
@FEFunction(name = "timediff", argTypes = { "DATETIME", "DATETIME" }, returnType = "TIME")
public static FloatLiteral timeDiff(LiteralExpr first, LiteralExpr second) throws AnalysisException {
long timediff = (getTime(first) - getTime(second)) / 1000;
return new FloatLiteral((double)timediff, Type.TIME);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why "yyyy-MM-dd" other than "yyyy-MM-dd hh:mm:ss"

I think we can write a function getTime() for DateLiteral.


import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I see you use SimpleDateFormat and DateTimeFormatter.
Can we unify them to DateTimeFormatter?

throw new AnalysisException("unixtime should larger than zero");
}
Date date = new Date(unixTime.getLongValue() * 1000);
return new StringLiteral(dateFormat(date, "%Y-%m-%d %H:%i:%S"));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can create a constructor of DateLiteral with unix timestamp and timezone.
Then this logic can be included in DateLiteral

@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch from 63071d4 to 08bf2ec Compare August 27, 2019 04:14
@HangyuanLiu HangyuanLiu force-pushed the fe-time-zone-function branch from 08bf2ec to 2412538 Compare August 27, 2019 04:59
Copy link
Contributor

@imay imay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@morningman morningman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@morningman morningman merged commit 0c2e344 into apache:master Aug 27, 2019
@imay imay mentioned this pull request Sep 26, 2019
@HangyuanLiu HangyuanLiu deleted the fe-time-zone-function branch May 25, 2020 01:56
SWJTU-ZhangLei added a commit to SWJTU-ZhangLei/incubator-doris that referenced this pull request Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants