Skip to content

Commit

Permalink
Make function to throw an exception on incorrect input.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Nov 1, 2022
1 parent 84a31c9 commit 557b0c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,9 @@ private ExprValue exprTime(ExprValue exprValue) {
*/
private ExprValue exprTimestamp(ExprValue exprValue) {
if (exprValue instanceof ExprStringValue) {
try {
return new ExprTimestampValue(exprValue.stringValue());
} catch (SemanticCheckException ignored) {
return ExprNullValue.of();
}
} else {
return new ExprTimestampValue(exprValue.timestampValue());
return new ExprTimestampValue(exprValue.stringValue());
}
return new ExprTimestampValue(exprValue.timestampValue());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.sql.expression.datetime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP;

import java.time.Instant;
Expand All @@ -17,13 +18,14 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.sql.data.model.ExprNullValue;
import org.opensearch.sql.data.model.ExprTimestampValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.ExpressionTestBase;
Expand All @@ -47,22 +49,23 @@ public void timestamp_one_arg_string() {
expr.valueOf(env).datetimeValue());
}

@Test
public void timestamp_one_arg_string_invalid_format() {
// Feb 30th
var expr = dsl.timestamp(DSL.literal("1984-02-30 12:20:42"));
assertEquals(TIMESTAMP, expr.type());
assertEquals(ExprNullValue.of(), expr.valueOf(env));

// 24:00:00
expr = dsl.timestamp(DSL.literal("1984-02-10 24:00:00"));
assertEquals(TIMESTAMP, expr.type());
assertEquals(ExprNullValue.of(), expr.valueOf(env));

// 2 digit year
expr = dsl.timestamp(DSL.literal("84-02-10 12:20:42"));
assertEquals(TIMESTAMP, expr.type());
assertEquals(ExprNullValue.of(), expr.valueOf(env));
/**
* Check that `TIMESTAMP` function throws an exception on incorrect string input.
* @param value A value.
* @param testName A test name.
*/
@ParameterizedTest(name = "{1}")
@CsvSource({
"1984-02-30 12:20:42, Feb 30th",
"1984-02-10 24:00:00, 24:00:00",
"84-02-10 12:20:42, 2 digit year"
})
public void timestamp_one_arg_string_invalid_format(String value, String testName) {
// exception thrown from ExprTimestampValue(String) CTOR
var exception = assertThrows(SemanticCheckException.class,
() -> dsl.timestamp(DSL.literal(value)).valueOf(env));
assertEquals(String.format("timestamp:%s in unsupported format, please "
+ "use yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]", value), exception.getMessage());
}

@Test
Expand Down

0 comments on commit 557b0c1

Please sign in to comment.