Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions src/main/java/com/teragrep/pth10/ast/DefaultTimeFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.util.Arrays;
import java.util.Date;

/**
Expand Down Expand Up @@ -88,7 +90,7 @@ public long getEpoch(String time) {
* @param time The human-readable time
* @return Date parsed from the given string
*/
public Date parse(String time) {
public Date parse(String time) throws DateTimeException {
// Try parsing all provided time formats in order
for (final String format : formats) {
try {
Expand All @@ -97,7 +99,10 @@ public Date parse(String time) {
catch (ParseException ignored) {
}
}
throw new RuntimeException("TimeQualifier conversion error: <" + time + "> can't be parsed.");
throw new DateTimeException(
"Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats: "
+ Arrays.toString(formats) + ")"
);
}

private Date parseDate(String time, String timeFormat) throws ParseException {
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/teragrep/pth10/ast/time/InstantTimestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@
import com.teragrep.pth10.ast.DefaultTimeFormat;
import com.teragrep.pth10.ast.TextString;
import com.teragrep.pth10.ast.UnquotedText;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Instant;
import java.util.Objects;

public final class InstantTimestamp implements DPLTimestamp {

private static final Logger LOGGER = LoggerFactory.getLogger(InstantTimestamp.class);
private final String value;
private final String timeformat;

Expand All @@ -72,19 +76,32 @@ public Instant instant() {
rv = relativeTimestamp.calculate(new Timestamp(System.currentTimeMillis()));
}
catch (NumberFormatException ne) {
rv = instantFromString(value, timeformat);
LOGGER.debug("Could not parse relative timestamp, trying default formats");
rv = instantFromString(value, timeformat, ne);
}

return rv;
}

// Uses defaultTimeFormat if timeformat is null and DPLTimeFormat if timeformat isn't null (which means that the
// timeformat= option was used).
private Instant instantFromString(final String value, final String timeFormatString) {
private Instant instantFromString(
final String value,
final String timeFormatString,
final NumberFormatException cause
) {
final String unquotedValue = new UnquotedText(new TextString(value)).read(); // erase the possible outer quotes
final Instant timevalue;
if (timeFormatString == null || timeFormatString.isEmpty()) {
timevalue = new DefaultTimeFormat().parse(unquotedValue).toInstant();
try {
timevalue = new DefaultTimeFormat().parse(unquotedValue).toInstant();
}
catch (final DateTimeException ex) {
throw new DateTimeException(
"Error parsing <" + unquotedValue + ">. " + ex.getMessage() + ". " + cause.getMessage() + ".",
cause
);
}
}
else {
// TODO: should be included in DPLTimeFormat
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/teragrep/pth10/DefaultTimeFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ void invalidTimeformat() {
.assertThrows(RuntimeException.class, () -> new DefaultTimeFormat().getEpoch(time));

Assertions
.assertEquals("TimeQualifier conversion error: <12/34/2020:10:25:40> can't be parsed.", rte.getMessage());
.assertEquals(
"Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats: [MM/dd/yyyy:HH:mm:ss, yyyy-MM-dd'T'HH:mm:ss.SSSXXX, yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ssXXX, yyyy-MM-dd'T'HH:mm:ss])",
rte.getMessage()
);
}
}
10 changes: 7 additions & 3 deletions src/test/java/com/teragrep/pth10/EarliestLatestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -354,11 +355,14 @@ public void defaultFormatTest() { // MM/dd/yyyy:HH:mm:ss 2013-07-15 10:01:50
)
public void defaultFormatInvalidInputTest() { // MM/dd/yyyy:HH:mm:ss 2013-07-15 10:01:50
String query = "(index=strawberry OR index=seagull) AND earliest=31/31/2014:00:00:00";
RuntimeException sqe = this.streamingTestUtil
.performThrowingDPLTest(RuntimeException.class, query, this.testFile, res -> {
DateTimeException sqe = this.streamingTestUtil
.performThrowingDPLTest(DateTimeException.class, query, this.testFile, res -> {
});
Assertions
.assertEquals("TimeQualifier conversion error: <31/31/2014:00:00:00> can't be parsed.", sqe.getMessage());
.assertEquals(
"Error parsing <31/31/2014:00:00:00>. Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats: [MM/dd/yyyy:HH:mm:ss, yyyy-MM-dd'T'HH:mm:ss.SSSXXX, yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ssXXX, yyyy-MM-dd'T'HH:mm:ss]). Unknown relative time modifier string [31/31/2014:00:00:00].",
sqe.getMessage()
);
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/teragrep/pth10/relativeTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ public void parseTimestampLatestRelativeTestWithPlus() {
)
public void parseTimestampLatestRelativeTestWithoutSign() {
String q = "index=cinnamon latest=3h ";
String expected = "TimeQualifier conversion error: <3h> can't be parsed.";
String expected = "Error parsing <3h>. Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats: [MM/dd/yyyy:HH:mm:ss, yyyy-MM-dd'T'HH:mm:ss.SSSXXX, yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ssXXX, yyyy-MM-dd'T'HH:mm:ss]). Unknown relative time modifier string [3h].";

RuntimeException exception = this.streamingTestUtil
.performThrowingDPLTest(RuntimeException.class, q, this.testFile, res -> {
DateTimeException exception = this.streamingTestUtil
.performThrowingDPLTest(DateTimeException.class, q, this.testFile, res -> {
});

Assertions.assertEquals(expected, exception.getMessage());
Expand Down Expand Up @@ -422,7 +422,7 @@ public void parseTimestampEarliestRelativeSnapToDayLatestNow() {
String expectedLatestString = String.valueOf(expectedLatest).substring(0, 7); // don't check last 2 numbers as the query takes some time and the "now" is different
String regex = "^.*_time >= from_unixtime\\(" + expectedEarliest + ".*_time < from_unixtime\\("
+ expectedLatestString + ".*$";
;

String result = this.streamingTestUtil.getCtx().getSparkQuery();
Assertions.assertTrue(result.matches(regex));
});
Expand All @@ -437,7 +437,7 @@ public void parseTimestampEarliestRelativeSnapToDayLatestNow() {
public void parseTimestampRelativeInvalidSnapToTimeUnitTest() {
// pth10 ticket #66 query: 'index=... sourcetype=... earliest=@-5h latest=@-3h'
String query = "index=cinnamon earliest=\"@-5h\" latest=\"@-3h\"";
String expected = "TimeQualifier conversion error: <@-5h> can't be parsed.";
String expected = "Error parsing <@-5h>. Check that the timestamp or the relative time value is in the correct format (Supported timestamp formats: [MM/dd/yyyy:HH:mm:ss, yyyy-MM-dd'T'HH:mm:ss.SSSXXX, yyyy-MM-dd'T'HH:mm:ss.SSS, yyyy-MM-dd'T'HH:mm:ssXXX, yyyy-MM-dd'T'HH:mm:ss]). Unknown relative time modifier string [@-5h].";

RuntimeException exception = this.streamingTestUtil
.performThrowingDPLTest(RuntimeException.class, query, this.testFile, res -> {
Expand Down