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

Commit

Permalink
Fixing date after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
FreCap committed Jul 1, 2021
1 parent 69b82eb commit 19bedb2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@

package com.amazon.opendistroforelasticsearch.sql.data.model;

import static com.amazon.opendistroforelasticsearch.sql.data.model.ExprTimestampValue.DATE_FORMATS_ALLOWED;

import com.amazon.opendistroforelasticsearch.sql.data.type.ExprCoreType;
import com.amazon.opendistroforelasticsearch.sql.data.type.ExprType;
import com.amazon.opendistroforelasticsearch.sql.data.utils.ExprDateFormatters;
import com.amazon.opendistroforelasticsearch.sql.exception.SemanticCheckException;
import com.google.common.base.Objects;
import java.time.Instant;
Expand All @@ -28,9 +27,7 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import lombok.RequiredArgsConstructor;

Expand All @@ -42,21 +39,13 @@ public class ExprDatetimeValue extends AbstractExprValue {
* Constructor with datetime string as input.
*/
public ExprDatetimeValue(String datetime) {
LocalDateTime localDateTime = null;

for (DateTimeFormatter format : DATE_FORMATS_ALLOWED) {
try {
localDateTime = LocalDateTime.parse(datetime, format);
} catch (DateTimeParseException ignored) {
// ignored
}
}
if (localDateTime == null) {
throw new SemanticCheckException(String.format(
"datetime:%s in unsupported format, please " + "use yyyy-MM-dd HH:mm:ss[.SSSSSS]",
datetime));
try {
this.datetime = LocalDateTime.parse(datetime,
ExprDateFormatters.TOLERANT_PARSER_DATE_TIME_FORMATTER);
} catch (DateTimeParseException e) {
throw new SemanticCheckException(String.format("datetime:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss[.SSSSSS]", datetime));
}
this.datetime = localDateTime;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ public void timestampInUnsupportedFormat() {
exception.getMessage());
}

@Test
public void datetimeInUnsupportedFormat() {
SemanticCheckException exception =
assertThrows(SemanticCheckException.class,
() -> new ExprDatetimeValue("2020-07-07T01:01:01Z"));
assertEquals(
"datetime:2020-07-07T01:01:01Z in unsupported format, "
+ "please use yyyy-MM-dd HH:mm:ss[.SSSSSS]",
exception.getMessage());
}

@Test
public void stringDateTimeValue() {
ExprValue stringValue = new ExprStringValue("2020-08-17 19:44:00");
Expand All @@ -142,9 +131,9 @@ public void stringDateTimeValue() {

SemanticCheckException exception =
assertThrows(SemanticCheckException.class,
() -> new ExprStringValue("2020-07-07T01:01:01Z").datetimeValue());
() -> new ExprStringValue("2020-07-07T01:01:01Z12345678").datetimeValue());
assertEquals(
"datetime:2020-07-07T01:01:01Z in unsupported format, "
"datetime:2020-07-07T01:01:01Z12345678 in unsupported format, "
+ "please use yyyy-MM-dd HH:mm:ss[.SSSSSS]",
exception.getMessage());
}
Expand Down Expand Up @@ -250,9 +239,9 @@ public void timestampOverMaxMicroPrecision() {
public void datetimeOverMaxMicroPrecision() {
SemanticCheckException exception =
assertThrows(SemanticCheckException.class,
() -> new ExprDatetimeValue("2020-07-07 01:01:01.1234567"));
() -> new ExprDatetimeValue("2020-07-07 01:01:01.1234567890"));
assertEquals(
"datetime:2020-07-07 01:01:01.1234567 in unsupported format, "
"datetime:2020-07-07 01:01:01.1234567890 in unsupported format, "
+ "please use yyyy-MM-dd HH:mm:ss[.SSSSSS]",
exception.getMessage());
}
Expand All @@ -261,9 +250,9 @@ public void datetimeOverMaxMicroPrecision() {
public void timeOverMaxMicroPrecision() {
SemanticCheckException exception =
assertThrows(SemanticCheckException.class,
() -> new ExprTimeValue("01:01:01.1234567"));
() -> new ExprTimeValue("01:01:01.1234567891"));
assertEquals(
"time:01:01:01.1234567 in unsupported format, please use HH:mm:ss[.SSSSSS]",
"time:01:01:01.1234567891 in unsupported format, please use HH:mm:ss[.SSSSSS]",
exception.getMessage());
}
}

0 comments on commit 19bedb2

Please sign in to comment.