From e4917d0168446e72a12b1cc2c4a8d76168edb9c2 Mon Sep 17 00:00:00 2001 From: HangyuanLiu <460660596@qq.com> Date: Mon, 19 Aug 2019 21:18:29 +0800 Subject: [PATCH 1/2] add --- .../org/apache/doris/analysis/DateLiteral.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java b/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java index f3525c686f4affc..c04fb2592ecfab7 100644 --- a/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java +++ b/fe/src/main/java/org/apache/doris/analysis/DateLiteral.java @@ -58,15 +58,15 @@ public DateLiteral(Type type, boolean isMax) throws AnalysisException{ this.type = type; if (type == Type.DATE) { if (isMax) { - init("1900-01-01", Type.DATE); - } else { init("9999-12-31", Type.DATE); + } else { + init("1900-01-01", Type.DATE); } } else { if (isMax) { - init("1900-01-01 00:00:00", Type.DATETIME); - } else { init("9999-12-31 23:59:59", Type.DATETIME); + } else { + init("1900-01-01 00:00:00", Type.DATETIME); } } analysisDone(); @@ -107,7 +107,7 @@ private void init(String s, Type type) throws AnalysisException { if (type == Type.DATE) { dateTime = FormatBuilder("%Y-%m-%d").toFormatter().parseLocalDateTime(s); } else { - dateTime = FormatBuilder("%Y-%m-%d %H-%i-%s").toFormatter().parseLocalDateTime(s); + dateTime = FormatBuilder("%Y-%m-%d %H:%i:%s").toFormatter().parseLocalDateTime(s); } year = dateTime.getYear(); month = dateTime.getMonthOfYear(); @@ -179,7 +179,6 @@ public String toSqlImpl() { @Override public String getStringValue() { - //return TimeUtils.format(date, type); if (type == Type.DATE) { return String.format("%04d", year) + "-" + String.format("%02d", month) + "-" + @@ -268,7 +267,12 @@ public static DateLiteral read(DataInput in) throws IOException { } public long unixTime(TimeZone timeZone) throws ParseException { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + 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); long timestamp = dateFormat.parse(getStringValue()).getTime(); return timestamp; From 05335dffefb265846466835fb7ee3163d5fbca63 Mon Sep 17 00:00:00 2001 From: HangyuanLiu <460660596@qq.com> Date: Mon, 19 Aug 2019 21:20:58 +0800 Subject: [PATCH 2/2] refactor date literal --- .../doris/catalog/RangePartitionInfo.java | 1 + .../org/apache/doris/rewrite/FEFunctions.java | 5 +++-- .../doris/catalog/PartitionKeyTest.java | 20 ++++++++++++++++++- .../doris/common/util/TimeUtilsTest.java | 3 --- .../apache/doris/rewrite/FEFunctionsTest.java | 5 +++-- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/fe/src/main/java/org/apache/doris/catalog/RangePartitionInfo.java b/fe/src/main/java/org/apache/doris/catalog/RangePartitionInfo.java index 404ed8c84e93dc3..f4fbb27aa26abcb 100644 --- a/fe/src/main/java/org/apache/doris/catalog/RangePartitionInfo.java +++ b/fe/src/main/java/org/apache/doris/catalog/RangePartitionInfo.java @@ -178,6 +178,7 @@ public Range handleNewSinglePartitionDesc(SingleRangePartitionDesc Range range = null; try { range = checkAndCreateRange(desc); + System.out.println(range.toString()); idToRange.put(partitionId, range); } catch (IllegalArgumentException e) { // Range.closedOpen may throw this if (lower > upper) diff --git a/fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java b/fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java index ef1ac7eb953c9b5..85a95099834a3d5 100644 --- a/fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java +++ b/fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java @@ -78,7 +78,7 @@ public static DateLiteral dateAdd(LiteralExpr date, LiteralExpr day) throws Anal DateLiteral dateLiteral = (DateLiteral) date; DateLiteral result = new DateLiteral(dateLiteral); - result.setDay(dateLiteral.getDay() - 1); + result.setDay(dateLiteral.getDay() + day.getLongValue()); return result; } @@ -126,7 +126,8 @@ public static IntLiteral day(LiteralExpr arg) throws AnalysisException { @FEFunction(name = "unix_timestamp", argTypes = { "DATETIME" }, returnType = "INT") public static IntLiteral unix_timestamp(LiteralExpr arg) throws AnalysisException { try { - return new IntLiteral(((DateLiteral) arg).unixTime(TimeUtils.getTimeZone()), Type.INT); + System.out.println("unix " + ((DateLiteral) arg).unixTime(TimeUtils.getTimeZone())); + return new IntLiteral(((DateLiteral) arg).unixTime(TimeUtils.getTimeZone()) / 1000, Type.INT); } catch (ParseException e) { throw new AnalysisException(e.getLocalizedMessage()); } diff --git a/fe/src/test/java/org/apache/doris/catalog/PartitionKeyTest.java b/fe/src/test/java/org/apache/doris/catalog/PartitionKeyTest.java index ad1702c4b89097d..d6f2f2dc912f1af 100644 --- a/fe/src/test/java/org/apache/doris/catalog/PartitionKeyTest.java +++ b/fe/src/test/java/org/apache/doris/catalog/PartitionKeyTest.java @@ -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 allColumns; @@ -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() { @@ -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(); diff --git a/fe/src/test/java/org/apache/doris/common/util/TimeUtilsTest.java b/fe/src/test/java/org/apache/doris/common/util/TimeUtilsTest.java index 49e86f36c686bd5..1cbabecb8d9b380 100644 --- a/fe/src/test/java/org/apache/doris/common/util/TimeUtilsTest.java +++ b/fe/src/test/java/org/apache/doris/common/util/TimeUtilsTest.java @@ -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())); } } diff --git a/fe/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java b/fe/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java index f84333207de56bf..d5bfde952c35125 100644 --- a/fe/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java +++ b/fe/src/test/java/org/apache/doris/rewrite/FEFunctionsTest.java @@ -90,11 +90,12 @@ public void addDateTest() throws AnalysisException { @Test public void daysAddTest() throws AnalysisException { DateLiteral actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(1)); - DateLiteral expectedResult = new DateLiteral("2018-08-09 00:00:00", Type.DATETIME); + DateLiteral expectedResult = new DateLiteral("2018-08-09", Type.DATE); + System.out.println(actualResult.getStringValue()); Assert.assertEquals(expectedResult, actualResult); actualResult = FEFunctions.daysAdd(new DateLiteral("2018-08-08", Type.DATE), new IntLiteral(-1)); - expectedResult = new DateLiteral("2018-08-07 00:00:00", Type.DATETIME); + expectedResult = new DateLiteral("2018-08-07", Type.DATE); Assert.assertEquals(expectedResult, actualResult); }