Skip to content

Commit bd9cea2

Browse files
committed
Change to ExpressionEvaluationException when fail parsing malformat date/time strings
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent f8f01f4 commit bd9cea2

18 files changed

Lines changed: 320 additions & 269 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/utils/datetime/DateTimeConversionUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.apache.calcite.avatica.util.TimeUnit;
1212
import org.opensearch.sql.data.model.*;
1313
import org.opensearch.sql.data.type.ExprCoreType;
14-
import org.opensearch.sql.exception.SemanticCheckException;
14+
import org.opensearch.sql.exception.ExpressionEvaluationException;
1515
import org.opensearch.sql.expression.function.FunctionProperties;
1616

1717
public final class DateTimeConversionUtils {
@@ -36,7 +36,7 @@ public static ExprTimestampValue forceConvertToTimestampValue(
3636
ExprValueUtils.timestampValue(timeValue.timestampValue(properties));
3737
case ExprStringValue stringValue -> new ExprTimestampValue(
3838
DateTimeParser.parse(stringValue.stringValue()));
39-
default -> throw new SemanticCheckException(
39+
default -> throw new ExpressionEvaluationException(
4040
String.format(
4141
"Cannot convert %s to timestamp, only STRING, DATE, TIME and TIMESTAMP are supported",
4242
value.type()));
@@ -63,8 +63,8 @@ public static ExprTimestampValue convertToTimestampValue(
6363
} else {
6464
try {
6565
return new ExprTimestampValue(value.timestampValue());
66-
} catch (SemanticCheckException e) {
67-
throw new SemanticCheckException(
66+
} catch (ExpressionEvaluationException e) {
67+
throw new ExpressionEvaluationException(
6868
String.format(
6969
"Cannot convert %s to timestamp, only STRING, DATE, TIME and TIMESTAMP are"
7070
+ " supported",
@@ -98,7 +98,7 @@ public static ExprDateValue convertToDateValue(ExprValue value, FunctionProperti
9898
return new ExprDateValue(value.stringValue());
9999
}
100100
default -> {
101-
throw new SemanticCheckException(
101+
throw new ExpressionEvaluationException(
102102
String.format(
103103
"Cannot convert %s to date, only STRING, DATE, TIME and TIMESTAMP are supported",
104104
value.type()));

core/src/main/java/org/opensearch/sql/data/model/ExprDateValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import lombok.RequiredArgsConstructor;
1717
import org.opensearch.sql.data.type.ExprCoreType;
1818
import org.opensearch.sql.data.type.ExprType;
19-
import org.opensearch.sql.exception.SemanticCheckException;
19+
import org.opensearch.sql.exception.ExpressionEvaluationException;
2020
import org.opensearch.sql.utils.DateTimeFormatters;
2121

2222
/** Expression Date Value. */
@@ -34,7 +34,7 @@ public ExprDateValue(String date) {
3434
try {
3535
this.date = LocalDate.parse(date, DateTimeFormatters.DATE_TIMESTAMP_FORMATTER);
3636
} catch (DateTimeParseException e) {
37-
throw new SemanticCheckException(
37+
throw new ExpressionEvaluationException(
3838
String.format("date:%s in unsupported format, please use 'yyyy-MM-dd'", date));
3939
}
4040
}

core/src/main/java/org/opensearch/sql/data/model/ExprStringValue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import lombok.RequiredArgsConstructor;
1414
import org.opensearch.sql.data.type.ExprCoreType;
1515
import org.opensearch.sql.data.type.ExprType;
16-
import org.opensearch.sql.exception.SemanticCheckException;
16+
import org.opensearch.sql.exception.ExpressionEvaluationException;
1717

1818
/** Expression String Value. */
1919
@RequiredArgsConstructor
@@ -39,7 +39,7 @@ public String stringValue() {
3939
public Instant timestampValue() {
4040
try {
4141
return new ExprTimestampValue(value).timestampValue();
42-
} catch (SemanticCheckException e) {
42+
} catch (ExpressionEvaluationException e) {
4343
return new ExprTimestampValue(
4444
LocalDateTime.of(new ExprDateValue(value).dateValue(), LocalTime.of(0, 0, 0)))
4545
.timestampValue();
@@ -50,7 +50,7 @@ public Instant timestampValue() {
5050
public LocalDate dateValue() {
5151
try {
5252
return new ExprTimestampValue(value).dateValue();
53-
} catch (SemanticCheckException e) {
53+
} catch (ExpressionEvaluationException e) {
5454
return new ExprDateValue(value).dateValue();
5555
}
5656
}
@@ -59,7 +59,7 @@ public LocalDate dateValue() {
5959
public LocalTime timeValue() {
6060
try {
6161
return new ExprTimestampValue(value).timeValue();
62-
} catch (SemanticCheckException e) {
62+
} catch (ExpressionEvaluationException e) {
6363
return new ExprTimeValue(value).timeValue();
6464
}
6565
}

core/src/main/java/org/opensearch/sql/data/model/ExprTimeValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import lombok.RequiredArgsConstructor;
1818
import org.opensearch.sql.data.type.ExprCoreType;
1919
import org.opensearch.sql.data.type.ExprType;
20-
import org.opensearch.sql.exception.SemanticCheckException;
20+
import org.opensearch.sql.exception.ExpressionEvaluationException;
2121
import org.opensearch.sql.expression.function.FunctionProperties;
2222
import org.opensearch.sql.utils.DateTimeFormatters;
2323

@@ -36,7 +36,7 @@ public ExprTimeValue(String time) {
3636
try {
3737
this.time = LocalTime.parse(time, DateTimeFormatters.TIME_TIMESTAMP_FORMATTER);
3838
} catch (DateTimeParseException e) {
39-
throw new SemanticCheckException(
39+
throw new ExpressionEvaluationException(
4040
String.format("time:%s in unsupported format, please use 'HH:mm:ss[.SSSSSSSSS]'", time));
4141
}
4242
}

core/src/main/java/org/opensearch/sql/data/model/ExprTimestampValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import lombok.RequiredArgsConstructor;
2020
import org.opensearch.sql.data.type.ExprCoreType;
2121
import org.opensearch.sql.data.type.ExprType;
22-
import org.opensearch.sql.exception.SemanticCheckException;
22+
import org.opensearch.sql.exception.ExpressionEvaluationException;
2323
import org.opensearch.sql.utils.DateTimeFormatters;
2424

2525
/** Expression Timestamp Value. */
@@ -39,7 +39,7 @@ public ExprTimestampValue(String timestamp) {
3939
LocalDateTime.parse(timestamp, DateTimeFormatters.DATE_TIMESTAMP_FORMATTER)
4040
.toInstant(ZoneOffset.UTC);
4141
} catch (DateTimeParseException e) {
42-
throw new SemanticCheckException(
42+
throw new ExpressionEvaluationException(
4343
String.format(
4444
"timestamp:%s in unsupported format, please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
4545
timestamp));

core/src/test/java/org/opensearch/sql/data/model/DateTimeValueTest.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.time.ZonedDateTime;
1919
import org.junit.jupiter.api.Test;
2020
import org.opensearch.sql.exception.ExpressionEvaluationException;
21-
import org.opensearch.sql.exception.SemanticCheckException;
2221
import org.opensearch.sql.expression.function.FunctionProperties;
2322

2423
public class DateTimeValueTest {
@@ -90,33 +89,34 @@ public void dateValueInterfaceTest() {
9089
assertEquals(
9190
ZonedDateTime.of(LocalDateTime.parse("2012-07-07T00:00:00"), ZoneOffset.UTC).toInstant(),
9291
dateValue.timestampValue());
93-
ExpressionEvaluationException exception =
92+
Throwable exception =
9493
assertThrows(ExpressionEvaluationException.class, () -> integerValue(1).dateValue());
9594
assertEquals("invalid to get dateValue from value of type INTEGER", exception.getMessage());
9695
}
9796

9897
@Test
9998
public void dateInUnsupportedFormat() {
100-
SemanticCheckException exception =
101-
assertThrows(SemanticCheckException.class, () -> new ExprDateValue("2020-07-07Z"));
99+
Throwable exception =
100+
assertThrows(ExpressionEvaluationException.class, () -> new ExprDateValue("2020-07-07Z"));
102101
assertEquals(
103102
"date:2020-07-07Z in unsupported format, please use 'yyyy-MM-dd'", exception.getMessage());
104103
}
105104

106105
@Test
107106
public void timeInUnsupportedFormat() {
108-
SemanticCheckException exception =
109-
assertThrows(SemanticCheckException.class, () -> new ExprTimeValue("01:01:0"));
107+
Throwable exception =
108+
assertThrows(ExpressionEvaluationException.class, () -> new ExprTimeValue("01:01:0"));
110109
assertEquals(
111110
"time:01:01:0 in unsupported format, please use 'HH:mm:ss[.SSSSSSSSS]'",
112111
exception.getMessage());
113112
}
114113

115114
@Test
116115
public void timestampInUnsupportedFormat() {
117-
SemanticCheckException exception =
116+
Throwable exception =
118117
assertThrows(
119-
SemanticCheckException.class, () -> new ExprTimestampValue("2020-07-07T01:01:01Z"));
118+
ExpressionEvaluationException.class,
119+
() -> new ExprTimestampValue("2020-07-07T01:01:01Z"));
120120
assertEquals(
121121
"timestamp:2020-07-07T01:01:01Z in unsupported format, "
122122
+ "please use 'yyyy-MM-dd HH:mm:ss[.SSSSSSSSS]'",
@@ -134,9 +134,9 @@ public void stringTimestampValue() {
134134
assertEquals(LocalTime.parse("19:44:00"), stringValue.timeValue());
135135
assertEquals("\"2020-08-17 19:44:00\"", stringValue.toString());
136136

137-
SemanticCheckException exception =
137+
Throwable exception =
138138
assertThrows(
139-
SemanticCheckException.class,
139+
ExpressionEvaluationException.class,
140140
() -> new ExprStringValue("2020-07-07T01:01:01Z").timestampValue());
141141
assertEquals(
142142
"date:2020-07-07T01:01:01Z in unsupported format, " + "please use 'yyyy-MM-dd'",
@@ -152,9 +152,10 @@ public void stringDateValue() {
152152
assertEquals(LocalDate.parse("2020-08-17"), stringValue.dateValue());
153153
assertEquals("\"2020-08-17\"", stringValue.toString());
154154

155-
SemanticCheckException exception =
155+
Throwable exception =
156156
assertThrows(
157-
SemanticCheckException.class, () -> new ExprStringValue("2020-07-07Z").dateValue());
157+
ExpressionEvaluationException.class,
158+
() -> new ExprStringValue("2020-07-07Z").dateValue());
158159
assertEquals(
159160
"date:2020-07-07Z in unsupported format, please use 'yyyy-MM-dd'", exception.getMessage());
160161
}
@@ -166,9 +167,9 @@ public void stringTimeValue() {
166167
assertEquals(LocalTime.parse("19:44:00"), stringValue.timeValue());
167168
assertEquals("\"19:44:00\"", stringValue.toString());
168169

169-
SemanticCheckException exception =
170+
Throwable exception =
170171
assertThrows(
171-
SemanticCheckException.class, () -> new ExprStringValue("01:01:0").timeValue());
172+
ExpressionEvaluationException.class, () -> new ExprStringValue("01:01:0").timeValue());
172173
assertEquals(
173174
"time:01:01:0 in unsupported format, please use 'HH:mm:ss[.SSSSSSSSS]'",
174175
exception.getMessage());
@@ -214,9 +215,9 @@ public void timestampWithVariableNanoPrecision() {
214215

215216
@Test
216217
public void timestampOverMaxNanoPrecision() {
217-
SemanticCheckException exception =
218+
Throwable exception =
218219
assertThrows(
219-
SemanticCheckException.class,
220+
ExpressionEvaluationException.class,
220221
() -> new ExprTimestampValue("2020-07-07 01:01:01.1234567890"));
221222
assertEquals(
222223
"timestamp:2020-07-07 01:01:01.1234567890 in unsupported format, please use "
@@ -226,8 +227,9 @@ public void timestampOverMaxNanoPrecision() {
226227

227228
@Test
228229
public void timeOverMaxNanoPrecision() {
229-
SemanticCheckException exception =
230-
assertThrows(SemanticCheckException.class, () -> new ExprTimeValue("01:01:01.1234567890"));
230+
Throwable exception =
231+
assertThrows(
232+
ExpressionEvaluationException.class, () -> new ExprTimeValue("01:01:01.1234567890"));
231233
assertEquals(
232234
"time:01:01:01.1234567890 in unsupported format, please use 'HH:mm:ss[.SSSSSSSSS]'",
233235
exception.getMessage());

core/src/test/java/org/opensearch/sql/expression/datetime/ConvertTZTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import org.junit.jupiter.api.Test;
1414
import org.opensearch.sql.data.model.ExprTimestampValue;
15-
import org.opensearch.sql.exception.SemanticCheckException;
15+
import org.opensearch.sql.exception.ExpressionEvaluationException;
1616
import org.opensearch.sql.expression.DSL;
1717
import org.opensearch.sql.expression.ExpressionTestBase;
1818
import org.opensearch.sql.expression.FunctionExpression;
@@ -27,7 +27,7 @@ public void invalidDate() {
2727
DSL.literal("+00:00"),
2828
DSL.literal("+00:00"));
2929
assertEquals(TIMESTAMP, expr.type());
30-
assertThrows(SemanticCheckException.class, expr::valueOf);
30+
assertThrows(ExpressionEvaluationException.class, expr::valueOf);
3131
}
3232

3333
@Test
@@ -156,7 +156,7 @@ public void invalidDateFeb30() {
156156
DSL.literal("+00:00"),
157157
DSL.literal("+00:00"));
158158
assertEquals(TIMESTAMP, expr.type());
159-
assertThrows(SemanticCheckException.class, expr::valueOf);
159+
assertThrows(ExpressionEvaluationException.class, expr::valueOf);
160160
}
161161

162162
@Test
@@ -167,7 +167,7 @@ public void invalidDateApril31() {
167167
DSL.literal("+00:00"),
168168
DSL.literal("+00:00"));
169169
assertEquals(TIMESTAMP, expr.type());
170-
assertThrows(SemanticCheckException.class, expr::valueOf);
170+
assertThrows(ExpressionEvaluationException.class, expr::valueOf);
171171
}
172172

173173
@Test
@@ -178,6 +178,6 @@ public void invalidMonth13() {
178178
DSL.literal("+00:00"),
179179
DSL.literal("+00:00"));
180180
assertEquals(TIMESTAMP, expr.type());
181-
assertThrows(SemanticCheckException.class, expr::valueOf);
181+
assertThrows(ExpressionEvaluationException.class, expr::valueOf);
182182
}
183183
}

0 commit comments

Comments
 (0)