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

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

/**
Expand Down Expand Up @@ -97,7 +98,10 @@ public Date parse(String time) {
catch (ParseException ignored) {
}
}
throw new RuntimeException("TimeQualifier conversion error: <" + time + "> can't be parsed.");
throw new RuntimeException(
Comment thread
Abigael-JT marked this conversation as resolved.
Outdated
"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
22 changes: 19 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,6 +49,8 @@
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;
Expand All @@ -57,6 +59,7 @@

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 +75,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 RuntimeException ex) {
Comment thread
Abigael-JT marked this conversation as resolved.
Outdated
throw new RuntimeException(
"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()
);
}
}
5 changes: 4 additions & 1 deletion src/test/java/com/teragrep/pth10/EarliestLatestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ public void defaultFormatInvalidInputTest() { // MM/dd/yyyy:HH:mm:ss 2013-07-15
.performThrowingDPLTest(RuntimeException.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
6 changes: 3 additions & 3 deletions src/test/java/com/teragrep/pth10/relativeTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ 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 -> {
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