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
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a60451c
add fe time zone
HangyuanLiu Aug 13, 2019
701d448
add fe time zone
HangyuanLiu Aug 14, 2019
194f5e9
add
HangyuanLiu Aug 14, 2019
37d7dfa
Merge remote-tracking branch 'upstream/master' into fe-time-zone-func…
HangyuanLiu Aug 14, 2019
5d85ff4
Merge branch 'fe-time-zone-function' of https://github.com/HangyuanLi…
HangyuanLiu Aug 14, 2019
b575d4c
add fe time zone
HangyuanLiu Aug 14, 2019
98ab140
add fe time zone
HangyuanLiu Aug 14, 2019
40c0dfe
add fe time zone
HangyuanLiu Aug 19, 2019
e5ac2fe
Merge branch 'fe-time-zone-function' of https://github.com/HangyuanLi…
HangyuanLiu Aug 19, 2019
01c8557
Merge branch 'fe-time-zone-function' of https://github.com/HangyuanLi…
HangyuanLiu Aug 19, 2019
58e856b
add
HangyuanLiu Aug 19, 2019
00f523d
add
HangyuanLiu Aug 19, 2019
3a376c0
add
HangyuanLiu Aug 19, 2019
4c486b9
add
HangyuanLiu Aug 19, 2019
6f12379
add
HangyuanLiu Aug 19, 2019
e4917d0
add
HangyuanLiu Aug 19, 2019
740b0bc
Merge branch 'fe-time-zone-function' of https://github.com/HangyuanLi…
HangyuanLiu Aug 19, 2019
05335df
refactor date literal
HangyuanLiu Aug 19, 2019
98d6668
Merge branch 'master' of https://github.com/apache/incubator-doris in…
HangyuanLiu Aug 19, 2019
c587d55
add
HangyuanLiu Aug 21, 2019
23e4fc1
Merge remote-tracking branch 'upstream/master' into fe-time-zone-func…
HangyuanLiu Aug 21, 2019
6db4c68
add
HangyuanLiu Aug 21, 2019
a4427f7
Merge branch 'fe-time-zone-function' of https://github.com/HangyuanLi…
HangyuanLiu Aug 21, 2019
266e74b
function name to camel
HangyuanLiu Aug 21, 2019
a3f5672
Merge branch 'master' of https://github.com/apache/incubator-doris in…
HangyuanLiu Aug 25, 2019
2412538
add master forward time zone support
HangyuanLiu Aug 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 323 additions & 40 deletions fe/src/main/java/org/apache/doris/analysis/DateLiteral.java

Large diffs are not rendered by default.

18 changes: 1 addition & 17 deletions fe/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,23 +536,7 @@ public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
LOG.warn("fn {} not exists", fnName.getFunction());
throw new AnalysisException(getFunctionNotFoundError(collectChildReturnTypes()));
}

if (fnName.getFunction().equals("from_unixtime")) {
// if has only one child, it has default time format: yyyy-MM-dd HH:mm:ss.SSSSSS
if (children.size() > 1) {
final StringLiteral formatExpr = (StringLiteral) children.get(1);
final String dateFormat1 = "yyyy-MM-dd HH:mm:ss";
final String dateFormat2 = "yyyy-MM-dd";
if (!formatExpr.getStringValue().equals(dateFormat1)
&& !formatExpr.getStringValue().equals(dateFormat2)) {
throw new AnalysisException(new StringBuilder("format does't support, try ")
.append("'").append(dateFormat1).append("'")
.append(" or ")
.append("'").append(dateFormat2).append("'.").toString());
}
}
}


if (fn.getFunctionName().getFunction().equals("time_diff")) {
fn.getReturnType().getPrimitiveType().setTimeType();
return;
Expand Down
2 changes: 1 addition & 1 deletion fe/src/main/java/org/apache/doris/common/FeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ public class FeConstants {

// general model
// Current meta data version. Use this version to write journals and image
public static int meta_version = FeMetaVersion.VERSION_58;
public static int meta_version = FeMetaVersion.VERSION_59;
}
2 changes: 2 additions & 0 deletions fe/src/main/java/org/apache/doris/common/FeMetaVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ public final class FeMetaVersion {
public static final int VERSION_57 = 57;
// broker load support function, persist origin stmt in broker load
public static final int VERSION_58 = 58;
// date literal
public static final int VERSION_59 = 59;
}
17 changes: 16 additions & 1 deletion fe/src/main/java/org/apache/doris/common/util/TimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.doris.common.DdlException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.ErrorReport;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.VariableMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -116,6 +118,16 @@ public static synchronized String getCurrentFormatTime() {
return DATETIME_FORMAT.format(new Date());
}

public static TimeZone getTimeZone() {
String timezone;
if (ConnectContext.get() != null) {
timezone = ConnectContext.get().getSessionVariable().getTimeZone();
} else {
timezone = VariableMgr.getGlobalSessionVariable().getTimeZone();
}
return TimeZone.getTimeZone(ZoneId.of(timezone, timeZoneAliasMap));
}

public static String longToTimeString(long timeStamp, SimpleDateFormat dateFormat) {
if (timeStamp <= 0L) {
return "N/A";
Expand All @@ -124,7 +136,10 @@ public static String longToTimeString(long timeStamp, SimpleDateFormat dateForma
}

public static synchronized String longToTimeString(long timeStamp) {
return longToTimeString(timeStamp, DATETIME_FORMAT);
TimeZone timeZone = getTimeZone();
SimpleDateFormat dateFormatTimeZone = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatTimeZone.setTimeZone(timeZone);
return longToTimeString(timeStamp, dateFormatTimeZone);
}

public static synchronized Date getTimeAsDate(String timeString) {
Expand Down
399 changes: 33 additions & 366 deletions fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java

Large diffs are not rendered by default.

20 changes: 19 additions & 1 deletion fe/src/test/java/org/apache/doris/catalog/PartitionKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@
import java.util.List;
import java.util.TimeZone;

import org.apache.doris.common.FeConstants;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "org.apache.log4j.*", "javax.management.*" })
@PrepareForTest(Catalog.class)
public class PartitionKeyTest {

private static List<Column> allColumns;
Expand All @@ -43,6 +53,8 @@ public class PartitionKeyTest {
private static Column largeInt;
private static Column date;
private static Column datetime;

private Catalog catalog;

@BeforeClass
public static void setUp() {
Expand Down Expand Up @@ -143,6 +155,12 @@ public void compareTest() throws AnalysisException {

@Test
public void testSerialization() throws Exception {
catalog = EasyMock.createMock(Catalog.class);
PowerMock.mockStatic(Catalog.class);
EasyMock.expect(Catalog.getInstance()).andReturn(catalog).anyTimes();
EasyMock.expect(Catalog.getCurrentCatalogJournalVersion()).andReturn(FeConstants.meta_version).anyTimes();
PowerMock.replay(Catalog.class);

// 1. Write objects to file
File file = new File("./keyRangePartition");
file.createNewFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ public void testDateTrans() throws AnalysisException {

DateLiteral datetime = new DateLiteral("2015-03-01 12:00:00", ScalarType.DATETIME);
Assert.assertEquals(20150301120000L, datetime.getRealValue());

Assert.assertEquals("2015-03-01", TimeUtils.format(date.getValue(), date.getType()));
Assert.assertEquals("2015-03-01 12:00:00", TimeUtils.format(datetime.getValue(), datetime.getType()));
}

}
70 changes: 30 additions & 40 deletions fe/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.TimeZone;

import static org.junit.Assert.fail;

/*
Expand Down Expand Up @@ -65,48 +63,49 @@ public void dateDiffTest() throws AnalysisException {
expectedResult = new IntLiteral(-30);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void dateAddTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.dateAdd(new StringLiteral("2018-08-08"), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
DateLiteral actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateAdd(new StringLiteral("2018-08-08"), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07", Type.DATE);
actualResult = FEFunctions.dateAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void addDateTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.addDate(new StringLiteral("2018-08-08"), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
DateLiteral actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.addDate(new StringLiteral("2018-08-08"), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07", Type.DATE);
actualResult = FEFunctions.addDate(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME);
Assert.assertEquals(expectedResult, actualResult);

}

@Test
public void daysAddTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.daysAdd(new StringLiteral("2018-08-08"), new IntLiteral(1));
DateLiteral actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE);
System.out.println(actualResult.getStringValue());
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.daysAdd(new StringLiteral("2018-08-08"), new IntLiteral(-1));
actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-07", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void fromUnixTimeTest() throws AnalysisException {
StringLiteral actualResult = FEFunctions.fromUnixTime(new IntLiteral(100000));
StringLiteral expectedResult = new StringLiteral("1970-01-02 11:46:40");
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.fromUnixTime(new IntLiteral(100000), new StringLiteral("yyyy-MM-dd"));
actualResult = FEFunctions.fromUnixTime(new IntLiteral(100000), new StringLiteral("%Y-%m-%d"));
expectedResult = new StringLiteral("1970-01-02");
Assert.assertEquals(expectedResult, actualResult);

Expand All @@ -125,11 +124,6 @@ public void fromUnixTimeTestException() throws AnalysisException {
@Test
public void dateFormatUtilTest() {
try {
Assert.assertEquals("19670102,196701,196701,0101", FEFunctions.dateFormat(new DateLiteral("1967-01-02 13:04:05", Type.DATETIME), new StringLiteral("%Y%m%d,%X%V,%x%v,%U%u")).getStringValue());
Assert.assertEquals("19960105,199553,199601,0001", FEFunctions.dateFormat(new DateLiteral("1996-01-05 13:04:05", Type.DATETIME), new StringLiteral("%Y%m%d,%X%V,%x%v,%U%u")).getStringValue());

Assert.assertEquals("2017-01-01,01,00", FEFunctions.dateFormat(new DateLiteral("2017-01-01 13:04:05", Type.DATETIME), new StringLiteral("%Y-%m-%d,%U,%u")).getStringValue());
Assert.assertEquals("201753,201752,5352", FEFunctions.dateFormat(new DateLiteral("2017-12-31 13:04:05", Type.DATETIME),new StringLiteral("%X%V,%x%v,%U%u")).getStringValue());

DateLiteral testDate = new DateLiteral("2001-01-09 13:04:05", Type.DATETIME);
Assert.assertEquals("Tue", FEFunctions.dateFormat(testDate, new StringLiteral("%a")).getStringValue());
Expand All @@ -153,24 +147,20 @@ public void dateFormatUtilTest() {
Assert.assertEquals("13:04:05", FEFunctions.dateFormat(testDate, new StringLiteral("%T")).getStringValue());
Assert.assertEquals("02", FEFunctions.dateFormat(testDate, new StringLiteral("%v")).getStringValue());
Assert.assertEquals("Tuesday", FEFunctions.dateFormat(testDate, new StringLiteral("%W")).getStringValue());
Assert.assertEquals("2", FEFunctions.dateFormat(testDate, new StringLiteral("%w")).getStringValue());
Assert.assertEquals("2001", FEFunctions.dateFormat(testDate, new StringLiteral("%Y")).getStringValue());
Assert.assertEquals("01", FEFunctions.dateFormat(testDate, new StringLiteral("%y")).getStringValue());
Assert.assertEquals("%", FEFunctions.dateFormat(testDate, new StringLiteral("%%")).getStringValue());
Assert.assertEquals("foo", FEFunctions.dateFormat(testDate, new StringLiteral("foo")).getStringValue());
Assert.assertEquals("g", FEFunctions.dateFormat(testDate, new StringLiteral("%g")).getStringValue());
Assert.assertEquals("4", FEFunctions.dateFormat(testDate, new StringLiteral("%4")).getStringValue());
Assert.assertEquals("2001 02" ,FEFunctions.dateFormat(testDate, new StringLiteral("%x %v")).getStringValue());
Assert.assertEquals("9th" ,FEFunctions.dateFormat(testDate, new StringLiteral("%D")).getStringValue());
} catch (AnalysisException e) {
e.printStackTrace();
}
}

@Test
public void dateParseTest() {
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
TimeZone.setDefault(tz);
try {
Assert.assertEquals("2013-05-10", FEFunctions.dateParse(new StringLiteral("2013,05,10"), new StringLiteral("%Y,%m,%d")).getStringValue());
Assert.assertEquals("2013-05-17 00:35:10", FEFunctions.dateParse(new StringLiteral("2013-05-17 12:35:10"), new StringLiteral("%Y-%m-%d %h:%i:%s")).getStringValue());
Expand Down Expand Up @@ -229,44 +219,44 @@ public void dateParseTest() {

@Test
public void dateSubTest() throws AnalysisException {
DateLiteral actualResult = FEFunctions.dateSub(new StringLiteral("2018-08-08"), new IntLiteral(1));
DateLiteral actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1));
DateLiteral expectedResult = new DateLiteral("2018-08-07", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.dateSub(new StringLiteral("2018-08-08"), new IntLiteral(-1));
actualResult = FEFunctions.dateSub(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1));
expectedResult = new DateLiteral("2018-08-09", Type.DATE);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void yearTest() throws AnalysisException {
IntLiteral actualResult = FEFunctions.year(new StringLiteral("2018-08-08"));
IntLiteral expectedResult = new IntLiteral(2018);
IntLiteral actualResult = FEFunctions.year(new DateLiteral("2018-08-08", Type.DATE));
IntLiteral expectedResult = new IntLiteral(2018, Type.INT);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.year(new StringLiteral("1970-01-02 11:46:40"));
expectedResult = new IntLiteral(1970);
actualResult = FEFunctions.year(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
expectedResult = new IntLiteral(1970, Type.INT);
Assert.assertEquals(expectedResult, actualResult);
}
@Test
public void monthTest() throws AnalysisException {
IntLiteral actualResult = FEFunctions.month(new StringLiteral("2018-08-08"));
IntLiteral expectedResult = new IntLiteral(8);
IntLiteral actualResult = FEFunctions.month(new DateLiteral("2018-08-08", Type.DATE));
IntLiteral expectedResult = new IntLiteral(8, Type.INT);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.month(new StringLiteral("1970-01-02 11:46:40"));
expectedResult = new IntLiteral(1);
actualResult = FEFunctions.month(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
expectedResult = new IntLiteral(1, Type.INT);
Assert.assertEquals(expectedResult, actualResult);
}

@Test
public void dayTest() throws AnalysisException {
IntLiteral actualResult = FEFunctions.day(new StringLiteral("2018-08-08"));
IntLiteral expectedResult = new IntLiteral(8);
IntLiteral actualResult = FEFunctions.day(new DateLiteral("2018-08-08", Type.DATE));
IntLiteral expectedResult = new IntLiteral(8, Type.INT);
Assert.assertEquals(expectedResult, actualResult);

actualResult = FEFunctions.day(new StringLiteral("1970-01-02 11:46:40"));
expectedResult = new IntLiteral(2);
actualResult = FEFunctions.day(new DateLiteral("1970-01-02 11:46:40", Type.DATETIME));
expectedResult = new IntLiteral(2, Type.INT);
Assert.assertEquals(expectedResult, actualResult);
}

Expand Down