Skip to content

Commit 6937c9b

Browse files
committed
Convert string inputs to timestamp when fed to unix_timestamp function
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 23407cb commit 6937c9b

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunctions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2164,14 +2164,21 @@ public static Double transferUnixTimeStampFromDoubleInput(Double value) {
21642164
}
21652165
}
21662166

2167+
private static double getUnixTimestampAsDouble(ExprTimestampValue value) {
2168+
return value.timestampValue().getEpochSecond() + value.timestampValue().getNano() / 1E9;
2169+
}
2170+
21672171
private Double unixTimeStampOfImpl(ExprValue value) {
21682172
// Also, according to MySQL documentation:
21692173
// The date argument may be a DATE, DATETIME, or TIMESTAMP ...
21702174
switch ((ExprCoreType) value.type()) {
21712175
case DATE:
21722176
return value.dateValue().toEpochSecond(LocalTime.MIN, ZoneOffset.UTC) + 0d;
21732177
case TIMESTAMP:
2174-
return value.timestampValue().getEpochSecond() + value.timestampValue().getNano() / 1E9;
2178+
return getUnixTimestampAsDouble((ExprTimestampValue) value);
2179+
case STRING:
2180+
ExprTimestampValue timestampValue = new ExprTimestampValue(value.stringValue());
2181+
return getUnixTimestampAsDouble(timestampValue);
21752182
default:
21762183
// ... or a number in YYMMDD, YYMMDDhhmmss, YYYYMMDD, or YYYYMMDDhhmmss format.
21772184
// If the argument includes a time part, it may optionally include a fractional

docs/user/ppl/functions/datetime.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ The date argument may be a DATE, or TIMESTAMP string, or a number in YYMMDD, YYM
18991899
If argument is in invalid format or outside of range 1970-01-01 00:00:00 - 3001-01-18 23:59:59.999999 (0 to 32536771199.999999 epoch time), function returns NULL.
19001900
You can use `FROM_UNIXTIME`_ to do reverse conversion.
19011901

1902-
Argument type: <NONE>/DOUBLE/DATE/TIMESTAMP
1902+
Argument type: <NONE>/DOUBLE/DATE/TIMESTAMP/STRING
19031903

19041904
Return type: DOUBLE
19051905

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLDateTimeBuiltinFunctionIT.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ public void testUnixTimestamp() {
383383
verifyDataRows(actual, closeTo(1220249547.0, 3404817525.0, 1.220249547123456E9));
384384
}
385385

386+
@Test
387+
public void testUnixTimestampWithString() {
388+
JSONObject actual =
389+
executeQuery(
390+
String.format(
391+
"source=%s | head 1 | eval t = UNIX_TIMESTAMP('2008-09-01 06:12:27.123456') |"
392+
+ " fields t",
393+
TEST_INDEX_DATE_FORMATS));
394+
verifySchema(actual, schema("t", "double"));
395+
verifyDataRows(actual, closeTo(1.220249547123456E9));
396+
}
397+
386398
@Test
387399
public void testWeekAndWeekOfYear() {
388400
JSONObject actual =

0 commit comments

Comments
 (0)