Skip to content

Refactor DateLiteral class in FE - #1644

Merged
morningman merged 26 commits into
apache:masterfrom
HangyuanLiu:fe-time-zone-function
Aug 27, 2019
Merged

Refactor DateLiteral class in FE#1644
morningman merged 26 commits into
apache:masterfrom
HangyuanLiu:fe-time-zone-function

Conversation

@HangyuanLiu

@HangyuanLiuHangyuanLiu commented Aug 15, 2019

Copy link
Copy Markdown
Contributor

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

@HangyuanLiuHangyuanLiu changed the title Fe time zone functionRefactor DateLiteral class in FEAug 19, 2019
…to fe-time-zone-function
Conflicts:
fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java
switch (type.getPrimitiveType()) {
case DATE:
return this.date.compareTo(TimeUtils.MIN_DATE) == 0;
return this.getStringValue().compareTo("1900-01-01") == 0;

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
privatestaticDateTimeFormatterBuilderFormatBuilder(Stringpattern) throwsAnalysisException{
privatestaticDateTimeFormatterBuilderformatBuilder(Stringpattern) throwsAnalysisException{

return hour;
}

public void setHour(long hour) {

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok

Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java
Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java Outdated
date = new Date(in.readLong());
if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_59) {
long packed_time = in.readLong();
microsecond = (packed_time % (1L << 24));

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
ContributorAuthor

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()

Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java
Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java
return dateLiteral;
}

public String dateFormat(String pattern) throws AnalysisException{

Copy link
Copy Markdown
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
Copy Markdown
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
Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java Outdated
Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java Outdated
Comment threadfe/src/main/java/org/apache/doris/analysis/DateLiteral.java Outdated
public void readFields(DataInput in) throws IOException {
super.readFields(in);
date = new Date(in.readLong());
if (Catalog.getCurrentCatalogJournalVersion() >= FeMetaVersion.VERSION_59) {

Copy link
Copy Markdown
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

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
Copy Markdown
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
Copy Markdown
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
HangyuanLiuforce-pushed the fe-time-zone-function branch 2 times, most recently from 9ac46ab to 63071d4CompareAugust 26, 2019 14:28
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
Copy Markdown
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.IOException;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;

Copy link
Copy Markdown
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?

}
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
Copy Markdown
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

@imayimay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@morningmanmorningman left a comment

Copy link
Copy Markdown
Contributor

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:masterAug 27, 2019
@imayimay 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 freeto 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

@HangyuanLiu@imay@morningman