Skip to content

Commit

Permalink
Merge branch 'fe-time-zone-function' of https://github.com/HangyuanLi…
Browse files Browse the repository at this point in the history
…u/incubator-doris into fe-time-zone-function

# Conflicts:
#	fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
#	fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java
  • Loading branch information
HangyuanLiu committed Aug 21, 2019
2 parents 6db4c68 + 98d6668 commit 12e3678
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
15 changes: 8 additions & 7 deletions fe/src/main/java/org/apache/doris/analysis/DateLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private DateLiteral() {
super();
}

public DateLiteral(Type type, boolean isMax) throws AnalysisException{
public DateLiteral(Type type, boolean isMax) throws AnalysisException {
super();
this.type = type;
if (type == Type.DATE) {
Expand All @@ -79,7 +79,7 @@ public DateLiteral(Type type, boolean isMax) throws AnalysisException{
analysisDone();
}

public DateLiteral(String s, Type type) throws AnalysisException{
public DateLiteral(String s, Type type) throws AnalysisException {
super();
init(s, type);
analysisDone();
Expand Down Expand Up @@ -117,7 +117,7 @@ public DateLiteral(DateLiteral other) {
type = other.type;
}

public static DateLiteral createMinValue(Type type) throws AnalysisException{
public static DateLiteral createMinValue(Type type) throws AnalysisException {
return new DateLiteral(type, false);
}

Expand Down Expand Up @@ -243,7 +243,7 @@ protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
if (targetType.isDateType()) {
return this;
} else if (targetType.isStringType()) {
return new StringLiteral(getStringValue());
return new StringLiteral(getStringValue());
}
Preconditions.checkState(false);
return this;
Expand All @@ -256,6 +256,7 @@ private long make_packed_datetime () {
long packed_datetime = ((ymd << 17) | hms) << 24 + microsecond;
return packed_datetime;
}

@Override
public void write(DataOutput out) throws IOException {
super.write(out);
Expand Down Expand Up @@ -307,7 +308,7 @@ public long unixTime(TimeZone timeZone) throws ParseException {
return dateFormat.parse(String.valueOf(getLongValue())).getTime();
}

public static DateLiteral dateParser(String date, String pattern) throws AnalysisException{
public static DateLiteral dateParser(String date, String pattern) throws AnalysisException {
LocalDateTime dateTime = formatBuilder(pattern).toFormatter().parseLocalDateTime(date);
DateLiteral dateLiteral = new DateLiteral(
dateTime.getYear(),
Expand All @@ -326,7 +327,7 @@ public static DateLiteral dateParser(String date, String pattern) throws Analysi

//Return the date stored in the dateliteral as pattern format.
//eg : "%Y-%m-%d" or "%Y-%m-%d %H:%i:%s"
public String dateFormat(String pattern) throws AnalysisException{
public String dateFormat(String pattern) throws AnalysisException {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
if (type == Type.DATE) {
builder.appendYear(4, 4).appendLiteral("-").
Expand All @@ -344,7 +345,7 @@ public String dateFormat(String pattern) throws AnalysisException{
.toString(formatBuilder(pattern).toFormatter());
}

private static DateTimeFormatterBuilder formatBuilder(String pattern) throws AnalysisException{
private static DateTimeFormatterBuilder formatBuilder(String pattern) throws AnalysisException {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
boolean escaped = false;
for (int i = 0; i < pattern.length(); i++) {
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ 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);
Expand Down
2 changes: 1 addition & 1 deletion fe/src/main/java/org/apache/doris/rewrite/FEFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ 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);
return new IntLiteral(((DateLiteral) arg).unixTime(TimeUtils.getTimeZone()) / 1000, Type.INT);
} catch (ParseException e) {
throw new AnalysisException(e.getLocalizedMessage());
}
Expand Down
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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 12e3678

Please sign in to comment.