Skip to content

Commit

Permalink
fixed mistake in string timestamp casting, updated tests with fix, fi…
Browse files Browse the repository at this point in the history
…xed test formatting, cleaned tests

Signed-off-by: Matthew Wells <[email protected]>
  • Loading branch information
matthewryanwells committed Aug 16, 2023
1 parent 20798c1 commit 32e4ee7
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -36,7 +37,20 @@ public String stringValue() {

@Override
public Instant timestampValue() {
return new ExprTimestampValue(value).timestampValue();
try {
return new ExprTimestampValue(value).timestampValue();
} catch (SemanticCheckException e) {
try {
return new ExprTimestampValue(
LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0)))
.timestampValue();
} catch (SemanticCheckException exception) {
throw new SemanticCheckException(
String.format(
"timestamp:%s in unsupported format, please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
value));
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ private DefaultFunctionResolver day() {
}

/**
* DAYNAME(STRING/DATE/TIMESTAMP). return the name of the weekday for date, including Monday,
* Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
* DAYNAME(STRING/DATE/TIMESTAMP). return the name of the weekday for date, including <br>
* Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
*/
private DefaultFunctionResolver dayName() {
return define(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

class DateTimeFunctionTest extends ExpressionTestBase {

final List<FormatTester> FormatTesters =
final List<DateFormatTester> dateFormatTesters =
ImmutableList.of(
new TimestampFormatTester(
new DateFormatTester(
"1998-01-31 13:14:15.012345",
ImmutableList.of(
"%H",
Expand Down Expand Up @@ -112,21 +112,21 @@ class DateTimeFunctionTest extends ExpressionTestBase {
"2008-12-31",
ImmutableList.of("%v", "%V", "%u", "%U"),
ImmutableList.of("53", "52", "53", "52")),
new TimestampFormatTester(
new DateFormatTester(
"1998-01-31 13:14:15.012345",
ImmutableList.of("%Y-%m-%dT%TZ"),
ImmutableList.of("1998-01-31T13:14:15Z")),
new TimestampFormatTester(
new DateFormatTester(
"1998-01-31 13:14:15.012345",
ImmutableList.of("%Y-%m-%da %T a"),
ImmutableList.of("1998-01-31PM 13:14:15 PM")),
new TimestampFormatTester(
new DateFormatTester(
"1998-01-31 13:14:15.012345",
ImmutableList.of("%Y-%m-%db %T b"),
ImmutableList.of("1998-01-31b 13:14:15 b")));

@AllArgsConstructor
private class FormatTester {
private class DateFormatTester {
private final String date;
private final List<String> formatterList;
private final List<String> formattedList;
Expand All @@ -140,33 +140,8 @@ String getFormatted() {
return String.join(DELIMITER, formattedList);
}

FunctionExpression getFormatExpression() {
return null;
}
}

private class DateFormatTester extends FormatTester {
public DateFormatTester(String date, List<String> formatterList, List<String> formattedList) {
super(date, formatterList, formattedList);
}

FunctionExpression getFormatExpression() {
return DSL.date_format(
functionProperties,
DSL.timestamp(DSL.date(DSL.literal(super.date))),
DSL.literal(getFormatter()));
}
}

private class TimestampFormatTester extends FormatTester {
public TimestampFormatTester(
String date, List<String> formatterList, List<String> formattedList) {
super(date, formatterList, formattedList);
}

FunctionExpression getFormatExpression() {
return DSL.date_format(
functionProperties, DSL.literal(super.date), DSL.literal(getFormatter()));
FunctionExpression getDateFormatExpression() {
return DSL.date_format(functionProperties, DSL.literal(date), DSL.literal(getFormatter()));
}
}

Expand Down Expand Up @@ -1377,7 +1352,7 @@ public void year() {

@Test
public void date_format() {
FormatTesters.forEach(this::testFormat);
dateFormatTesters.forEach(this::testDateFormat);
String timestamp = "1998-01-31 13:14:15.012345";
String timestampFormat =
"%a %b %c %D %d %e %f %H %h %I %i %j %k %l %M " + "%m %p %r %S %s %T %% %P";
Expand All @@ -1391,8 +1366,8 @@ public void date_format() {
assertEquals(timestampFormatted, eval(expr).stringValue());
}

void testFormat(FormatTester dft) {
FunctionExpression expr = dft.getFormatExpression();
void testDateFormat(DateFormatTester dft) {
FunctionExpression expr = dft.getDateFormatExpression();
assertEquals(STRING, expr.type());
assertEquals(dft.getFormatted(), eval(expr).stringValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,10 @@ private void assertStringRepr(
if (v1.type() == v2.type()) {
assertEquals(String.format("%s(%s, %s)", function, v1, v2), functionExpression.toString());
} else {
var widerType = TIMESTAMP;
assertEquals(
String.format(
"%s(%s, %s)",
function, getExpectedStringRepr(widerType, v1), getExpectedStringRepr(widerType, v2)),
function, getExpectedStringRepr(TIMESTAMP, v1), getExpectedStringRepr(TIMESTAMP, v2)),
functionExpression.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,102 +236,102 @@ public void testDateAdd() throws IOException {

@Test
public void testDateTime() throws IOException {
JSONObject result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00+00:00', 'America/Los_Angeles') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
JSONObject result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00+00:00', 'America/Los_Angeles') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-24 21:30:00"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-24 21:30:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00+00:00', '+01:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00+00:00', '+01:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-25 06:30:00"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-25 06:30:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00-05:00', '+05:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00-05:00', '+05:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-25 15:30:00"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-25 15:30:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2004-02-28 23:00:00-10:00', '+10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2004-02-28 23:00:00-10:00', '+10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2004-02-29 19:00:00"));
verifySome(result.getJSONArray("datarows"), rows("2004-02-29 19:00:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2003-02-28 23:00:00-10:00', '+10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2003-02-28 23:00:00-10:00', '+10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2003-03-01 19:00:00"));
verifySome(result.getJSONArray("datarows"), rows("2003-03-01 19:00:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00+00:00', '+14:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-12-25 05:30:00+00:00', '+14:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-25 19:30:00"));
verifySome(result.getJSONArray("datarows"), rows("2008-12-25 19:30:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+10:00', '-10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+10:00', '-10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2007-12-31 06:00:00"));
verifySome(result.getJSONArray("datarows"), rows("2007-12-31 06:00:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+10:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2008-01-01 02:00:00"));
verifySome(result.getJSONArray("datarows"), rows("2008-01-01 02:00:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows("2008-01-01 02:00:00"));
verifySome(result.getJSONArray("datarows"), rows("2008-01-01 02:00:00"));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+15:00', '-12:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+15:00', '-12:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows(new Object[]{null}));
verifySome(result.getJSONArray("datarows"), rows(new Object[]{null}));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+10:00', '-14:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00+10:00', '-14:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows(new Object[]{null}));
verifySome(result.getJSONArray("datarows"), rows(new Object[]{null}));

result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00', '-14:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
result =
executeQuery(String.format(
"source=%s | eval f = DATETIME('2008-01-01 02:00:00', '-14:00') | fields f",
TEST_INDEX_DATE));
verifySchema(result,
schema("f", null, "timestamp"));
verifySome(result.getJSONArray("datarows"), rows(new Object[]{null}));
}
verifySome(result.getJSONArray("datarows"), rows(new Object[]{null}));
}

@Test
public void testDateSub() throws IOException {
Expand Down Expand Up @@ -1010,6 +1010,7 @@ public void testPeriodDiff() throws IOException {
verifySome(result.getJSONArray("datarows"), rows(11, -25));
}

@Test
public void testDateDiff() throws IOException {
var result = executeQuery(String.format("source=%s | eval"
+ " `'2000-01-02' - '2000-01-01'` = DATEDIFF(TIMESTAMP('2000-01-02 00:00:00'), TIMESTAMP('2000-01-01 23:59:59')),"
Expand Down

0 comments on commit 32e4ee7

Please sign in to comment.