Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Support date and time functions: date_add, date_sub, day, dayname, dayofweek, dayofyear, from_days, hour, microsecond, minute, month, monthname, quarter, second, subdate, time_to_sec, to_days #746

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
db82e29
add functions day, month, quarter, year
Sep 9, 2020
e46da70
fix build error
Sep 9, 2020
88a878b
fix doctest error
Sep 9, 2020
63ea788
fix doctest build error
Sep 9, 2020
7551dc8
fix doctest
Sep 9, 2020
a673fe2
add dayofmonth()
Sep 9, 2020
7c70c33
add dayofyear()
Sep 9, 2020
d87f8f4
add dayofweek()
Sep 9, 2020
242403c
fix dayofweek logic & add unit test
Sep 9, 2020
be7051d
fix doctest for dayofweek()
Sep 9, 2020
6834c10
add dayname
Sep 9, 2020
ee02527
add monthname
Sep 10, 2020
8ab2f63
fix checkstyle build error
Sep 10, 2020
458c34a
fix build error
Sep 10, 2020
e42aac1
fix doctest for monthname
Sep 10, 2020
960783b
add hour()
Sep 10, 2020
4d355b2
add minute()
Sep 10, 2020
0881f24
add second
Sep 11, 2020
d199d54
add microsecond
Sep 14, 2020
c88a6dd
fix datetime & timestamp issue for microsecond
Sep 15, 2020
4dd0db0
add time_to_sec
Sep 15, 2020
988d177
add subdate & date_sub
Sep 15, 2020
9c1d96b
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
Sep 15, 2020
693211a
fix doctest error
Sep 15, 2020
36449f7
fix build error
Sep 15, 2020
048d5d6
add KeywordsCanBeId for dayofweek
Sep 15, 2020
c0593d9
add to_days
Sep 16, 2020
7ee0dbc
add from_days()
Sep 16, 2020
7764a0d
arrange by alphabetical order
Sep 17, 2020
18a6a84
add manual IT
Sep 17, 2020
7054a4b
add string input for date functions
Sep 17, 2020
7e4c726
fix microsecond
Sep 18, 2020
f0a16ce
update doc
Sep 18, 2020
b757335
add date_add
Sep 18, 2020
1a44c36
address PR comments
Sep 19, 2020
20acde3
update tests & doc
Sep 23, 2020
a51f2f7
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
Sep 23, 2020
42c7222
fix doc format
Sep 23, 2020
0f3894a
update tests for adddate
Sep 23, 2020
a73a8e9
move string conversion to ExprStringValue
Sep 24, 2020
ea9b190
add string type in doc
Sep 24, 2020
61ac4e5
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
Sep 24, 2020
dc70cd6
add test cases for datetime function in ExpeStringValue
Sep 28, 2020
d93f02b
Merge branch 'develop' of https://github.com/rupal-bq/sql into date-t…
Sep 28, 2020
c001615
removing implicit def for keyword in parser
Sep 29, 2020
104e6b9
add dayofweek
Sep 29, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ExprDatetimeValue extends AbstractExprValue {
private static final DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss");
.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSS]");
private final LocalDateTime datetime;

/**
Expand All @@ -43,7 +44,7 @@ public ExprDatetimeValue(String datetime) {
this.datetime = LocalDateTime.parse(datetime, formatter);
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss", datetime));
+ "use yyyy-MM-dd HH:mm:ss[.SSSSSS]", datetime));
}
}

Expand Down Expand Up @@ -80,7 +81,8 @@ public boolean equal(ExprValue other) {
@Override
public String value() {
return String.format("%s %s", DateTimeFormatter.ISO_DATE.format(datetime),
DateTimeFormatter.ISO_TIME.format(datetime));
DateTimeFormatter.ISO_TIME.format((datetime.getNano() == 0)
? datetime.truncatedTo(ChronoUnit.SECONDS) : datetime));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -43,6 +48,40 @@ public String stringValue() {
return value;
}

@Override
Copy link
Contributor

Choose a reason for hiding this comment

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

could you add the test cases for these functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added test cases for date-time functions in ExprStringValue

public LocalDateTime datetimeValue() {
try {
return new ExprDatetimeValue(value).datetimeValue();
} catch (SemanticCheckException e) {
try {
return new ExprDatetimeValue(
LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0)))
.datetimeValue();
} catch (SemanticCheckException exception) {
throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss[.SSSSSS]", value));
}
}
}

@Override
public LocalDate dateValue() {
try {
return new ExprDatetimeValue(value).dateValue();
} catch (SemanticCheckException e) {
return new ExprDateValue(value).dateValue();
}
}

@Override
public LocalTime timeValue() {
try {
return new ExprDatetimeValue(value).timeValue();
} catch (SemanticCheckException e) {
return new ExprTimeValue(value).timeValue();
}
}

@Override
public String toString() {
return String.format("\"%s\"", value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class ExprTimestampValue extends AbstractExprValue {
* todo. only support timestamp in format yyyy-MM-dd HH:mm:ss.
*/
private static final DateTimeFormatter FORMATTER = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSS]");
private static final DateTimeFormatter FORMATTER_WITNOUT_NANO = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss");
private final Instant timestamp;

Expand All @@ -55,14 +57,16 @@ public ExprTimestampValue(String timestamp) {
this.timestamp = LocalDateTime.parse(timestamp, FORMATTER).atZone(ZONE).toInstant();
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("timestamp:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss", timestamp));
+ "use yyyy-MM-dd HH:mm:ss[.SSSSSS]", timestamp));
}

}

@Override
public String value() {
return FORMATTER.withZone(ZONE).format(timestamp.truncatedTo(ChronoUnit.SECONDS));
return timestamp.getNano() == 0 ? FORMATTER_WITNOUT_NANO.withZone(ZONE)
.format(timestamp.truncatedTo(ChronoUnit.SECONDS))
: FORMATTER.withZone(ZONE).format(timestamp);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,97 @@ public FunctionExpression subtract(Expression... expressions) {
public FunctionExpression multiply(Expression... expressions) {
return function(BuiltinFunctionName.MULTIPLY, expressions);
}

public FunctionExpression adddate(Expression... expressions) {
return function(BuiltinFunctionName.ADDDATE, expressions);
}

public FunctionExpression date(Expression... expressions) {
return function(BuiltinFunctionName.DATE, expressions);
}

public FunctionExpression date_add(Expression... expressions) {
return function(BuiltinFunctionName.DATE_ADD, expressions);
}

public FunctionExpression date_sub(Expression... expressions) {
return function(BuiltinFunctionName.DATE_SUB, expressions);
}

public FunctionExpression day(Expression... expressions) {
return function(BuiltinFunctionName.DAY, expressions);
}

public FunctionExpression dayname(Expression... expressions) {
return function(BuiltinFunctionName.DAYNAME, expressions);
}

public FunctionExpression dayofmonth(Expression... expressions) {
return function(BuiltinFunctionName.DAYOFMONTH, expressions);
}

public FunctionExpression date(Expression... expressions) {
return function(BuiltinFunctionName.DATE, expressions);
public FunctionExpression dayofweek(Expression... expressions) {
return function(BuiltinFunctionName.DAYOFWEEK, expressions);
}

public FunctionExpression dayofyear(Expression... expressions) {
return function(BuiltinFunctionName.DAYOFYEAR, expressions);
}

public FunctionExpression from_days(Expression... expressions) {
return function(BuiltinFunctionName.FROM_DAYS, expressions);
}

public FunctionExpression hour(Expression... expressions) {
return function(BuiltinFunctionName.HOUR, expressions);
}

public FunctionExpression microsecond(Expression... expressions) {
return function(BuiltinFunctionName.MICROSECOND, expressions);
}

public FunctionExpression minute(Expression... expressions) {
return function(BuiltinFunctionName.MINUTE, expressions);
}

public FunctionExpression month(Expression... expressions) {
return function(BuiltinFunctionName.MONTH, expressions);
}

public FunctionExpression monthname(Expression... expressions) {
return function(BuiltinFunctionName.MONTHNAME, expressions);
}

public FunctionExpression quarter(Expression... expressions) {
return function(BuiltinFunctionName.QUARTER, expressions);
}

public FunctionExpression second(Expression... expressions) {
return function(BuiltinFunctionName.SECOND, expressions);
}

public FunctionExpression subdate(Expression... expressions) {
return function(BuiltinFunctionName.SUBDATE, expressions);
}

public FunctionExpression time(Expression... expressions) {
return function(BuiltinFunctionName.TIME, expressions);
}

public FunctionExpression time_to_sec(Expression... expressions) {
return function(BuiltinFunctionName.TIME_TO_SEC, expressions);
}

public FunctionExpression timestamp(Expression... expressions) {
return function(BuiltinFunctionName.TIMESTAMP, expressions);
}

public FunctionExpression adddate(Expression... expressions) {
return function(BuiltinFunctionName.ADDDATE, expressions);
public FunctionExpression to_days(Expression... expressions) {
return function(BuiltinFunctionName.TO_DAYS, expressions);
}

public FunctionExpression year(Expression... expressions) {
return function(BuiltinFunctionName.YEAR, expressions);
}

public FunctionExpression divide(Expression... expressions) {
Expand Down
Loading