Skip to content

Commit 2f6e974

Browse files
authored
[Improve] Improve error message when can not parse datetime value (#7181)
* [Improve] Improve error message when can not parse datetime value * update
1 parent 5611715 commit 2f6e974

File tree

6 files changed

+116
-6
lines changed

6 files changed

+116
-6
lines changed

seatunnel-common/src/main/java/org/apache/seatunnel/common/exception/CommonError.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,18 @@ public static SeaTunnelRuntimeException writeRowErrorWithFiledsCountNotMatch(
271271
return new SeaTunnelRuntimeException(
272272
WRITE_SEATUNNEL_ROW_ERROR_WITH_FILEDS_NOT_MATCH, params);
273273
}
274+
275+
public static SeaTunnelRuntimeException formatDateTimeError(String datetime, String field) {
276+
Map<String, String> params = new HashMap<>();
277+
params.put("datetime", datetime);
278+
params.put("field", field);
279+
return new SeaTunnelRuntimeException(CommonErrorCode.FORMAT_DATETIME_ERROR, params);
280+
}
281+
282+
public static SeaTunnelRuntimeException formatDateError(String date, String field) {
283+
Map<String, String> params = new HashMap<>();
284+
params.put("date", date);
285+
params.put("field", field);
286+
return new SeaTunnelRuntimeException(CommonErrorCode.FORMAT_DATE_ERROR, params);
287+
}
274288
}

seatunnel-common/src/main/java/org/apache/seatunnel/common/exception/CommonErrorCode.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ public enum CommonErrorCode implements SeaTunnelErrorCode {
7070

7171
WRITE_SEATUNNEL_ROW_ERROR_WITH_FILEDS_NOT_MATCH(
7272
"COMMON-31",
73-
"<connector>: The source has '<sourceFieldsNum>' fields, but the table of sink has '<sinkFieldsNum>' fields. Please check schema of sink table.");
73+
"<connector>: The source has '<sourceFieldsNum>' fields, but the table of sink has '<sinkFieldsNum>' fields. Please check schema of sink table."),
74+
FORMAT_DATE_ERROR(
75+
"COMMON-32",
76+
"The date format '<date>' of field '<field>' is not supported. Please check the date format."),
77+
FORMAT_DATETIME_ERROR(
78+
"COMMON-33",
79+
"The datetime format '<datetime>' of field '<field>' is not supported. Please check the datetime format."),
80+
;
7481

7582
private final String code;
7683
private final String description;

seatunnel-formats/seatunnel-format-json/src/main/java/org/apache/seatunnel/format/json/JsonToRowConverters.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ private LocalDate convertToLocalDate(JsonNode jsonNode, String fieldName) {
256256
dateFormatter = DateUtils.matchDateFormatter(dateStr);
257257
fieldFormatterMap.put(fieldName, dateFormatter);
258258
}
259+
if (dateFormatter == null) {
260+
throw CommonError.formatDateError(dateStr, fieldName);
261+
}
259262

260263
return dateFormatter.parse(jsonNode.asText()).query(TemporalQueries.localDate());
261264
}
@@ -272,6 +275,9 @@ private LocalDateTime convertToLocalDateTime(JsonNode jsonNode, String fieldName
272275
dateTimeFormatter = DateTimeUtils.matchDateTimeFormatter(datetimeStr);
273276
fieldFormatterMap.put(fieldName, dateTimeFormatter);
274277
}
278+
if (dateTimeFormatter == null) {
279+
throw CommonError.formatDateTimeError(datetimeStr, fieldName);
280+
}
275281

276282
TemporalAccessor parsedTimestamp = dateTimeFormatter.parse(datetimeStr);
277283
LocalTime localTime = parsedTimestamp.query(TemporalQueries.localTime());

seatunnel-formats/seatunnel-format-json/src/test/java/org/apache/seatunnel/format/json/JsonRowDataSerDeSchemaTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
import org.apache.seatunnel.common.utils.JsonUtils;
3838
import org.apache.seatunnel.format.json.exception.SeaTunnelJsonFormatException;
3939

40+
import org.junit.jupiter.api.Assertions;
4041
import org.junit.jupiter.api.Test;
4142

43+
import java.io.IOException;
4244
import java.math.BigDecimal;
4345
import java.sql.Timestamp;
4446
import java.time.LocalDate;
@@ -564,4 +566,39 @@ private void assertMapKeyType(
564566
Map<?, ?> keyMap = (Map<?, ?>) converter.convert(keyMapNode, fieldName);
565567
assertEquals(expect, keyMap.keySet().iterator().next());
566568
}
569+
570+
@Test
571+
public void testParseUnsupportedDateTimeFormat() throws IOException {
572+
SeaTunnelRowType rowType =
573+
new SeaTunnelRowType(
574+
new String[] {"date_field"},
575+
new SeaTunnelDataType<?>[] {LocalTimeType.LOCAL_DATE_TYPE});
576+
JsonDeserializationSchema deserializationSchema =
577+
new JsonDeserializationSchema(false, false, rowType);
578+
String content = "{\"date_field\":\"2022-092-24\"}";
579+
SeaTunnelRuntimeException exception =
580+
Assertions.assertThrows(
581+
SeaTunnelRuntimeException.class,
582+
() -> deserializationSchema.deserialize(content.getBytes()));
583+
Assertions.assertEquals(
584+
"ErrorCode:[COMMON-32], ErrorDescription:[The date format '2022-092-24' of field 'date_field' is not supported. Please check the date format.]",
585+
exception.getCause().getCause().getMessage());
586+
587+
SeaTunnelRowType rowType2 =
588+
new SeaTunnelRowType(
589+
new String[] {"timestamp_field"},
590+
new SeaTunnelDataType<?>[] {
591+
LocalTimeType.LOCAL_DATE_TIME_TYPE,
592+
});
593+
JsonDeserializationSchema deserializationSchema2 =
594+
new JsonDeserializationSchema(false, false, rowType2);
595+
String content2 = "{\"timestamp_field\": \"2022-09-24-22:45:00\"}";
596+
SeaTunnelRuntimeException exception2 =
597+
Assertions.assertThrows(
598+
SeaTunnelRuntimeException.class,
599+
() -> deserializationSchema2.deserialize(content2.getBytes()));
600+
Assertions.assertEquals(
601+
"ErrorCode:[COMMON-33], ErrorDescription:[The datetime format '2022-09-24-22:45:00' of field 'timestamp_field' is not supported. Please check the datetime format.]",
602+
exception2.getCause().getCause().getMessage());
603+
}
567604
}

seatunnel-formats/seatunnel-format-text/src/main/java/org/apache/seatunnel/format/text/TextDeserializationSchema.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
2626
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
2727
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
28+
import org.apache.seatunnel.common.exception.CommonError;
2829
import org.apache.seatunnel.common.exception.CommonErrorCode;
2930
import org.apache.seatunnel.common.utils.DateTimeUtils;
3031
import org.apache.seatunnel.common.utils.DateUtils;
@@ -289,6 +290,9 @@ private Object convert(
289290
dateFormatter = DateUtils.matchDateFormatter(field);
290291
fieldFormatterMap.put(fieldName, dateFormatter);
291292
}
293+
if (dateFormatter == null) {
294+
throw CommonError.formatDateError(field, fieldName);
295+
}
292296

293297
return dateFormatter.parse(field).query(TemporalQueries.localDate());
294298
case TIME:
@@ -300,6 +304,9 @@ private Object convert(
300304
dateTimeFormatter = DateTimeUtils.matchDateTimeFormatter(field);
301305
fieldFormatterMap.put(fieldName, dateTimeFormatter);
302306
}
307+
if (dateTimeFormatter == null) {
308+
throw CommonError.formatDateTimeError(field, fieldName);
309+
}
303310

304311
TemporalAccessor parsedTimestamp = dateTimeFormatter.parse(field);
305312
LocalTime localTime = parsedTimestamp.query(TemporalQueries.localTime());
@@ -320,11 +327,8 @@ private Object convert(
320327
}
321328
return new SeaTunnelRow(objects);
322329
default:
323-
throw new SeaTunnelTextFormatException(
324-
CommonErrorCode.UNSUPPORTED_DATA_TYPE,
325-
String.format(
326-
"SeaTunnel not support this data type [%s]",
327-
fieldType.getSqlType()));
330+
throw CommonError.unsupportedDataType(
331+
"SeaTunnel", fieldType.getSqlType().toString(), fieldName);
328332
}
329333
}
330334
}

seatunnel-formats/seatunnel-format-text/src/test/java/org/apache/seatunnel/format/text/TextFormatSchemaTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
2727
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
2828
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
29+
import org.apache.seatunnel.common.exception.SeaTunnelRuntimeException;
2930

3031
import org.junit.jupiter.api.Assertions;
3132
import org.junit.jupiter.api.BeforeEach;
@@ -145,4 +146,45 @@ public void testParse() throws IOException {
145146
Assertions.assertEquals(seaTunnelRow.getField(2), "tyrantlucifer");
146147
Assertions.assertEquals(data, content);
147148
}
149+
150+
@Test
151+
public void testParseUnsupportedDateTimeFormat() throws IOException {
152+
SeaTunnelRowType rowType =
153+
new SeaTunnelRowType(
154+
new String[] {"date_field"},
155+
new SeaTunnelDataType<?>[] {LocalTimeType.LOCAL_DATE_TYPE});
156+
TextDeserializationSchema deserializationSchema =
157+
TextDeserializationSchema.builder()
158+
.seaTunnelRowType(rowType)
159+
.delimiter("\u0001")
160+
.build();
161+
String content = "2022-092-24";
162+
SeaTunnelRuntimeException exception =
163+
Assertions.assertThrows(
164+
SeaTunnelRuntimeException.class,
165+
() -> deserializationSchema.deserialize(content.getBytes()));
166+
Assertions.assertEquals(
167+
"ErrorCode:[COMMON-32], ErrorDescription:[The date format '2022-092-24' of field 'date_field' is not supported. Please check the date format.]",
168+
exception.getMessage());
169+
170+
SeaTunnelRowType rowType2 =
171+
new SeaTunnelRowType(
172+
new String[] {"timestamp_field"},
173+
new SeaTunnelDataType<?>[] {
174+
LocalTimeType.LOCAL_DATE_TIME_TYPE,
175+
});
176+
TextDeserializationSchema deserializationSchema2 =
177+
TextDeserializationSchema.builder()
178+
.seaTunnelRowType(rowType2)
179+
.delimiter("\u0001")
180+
.build();
181+
String content2 = "2022-09-24-22:45:00";
182+
SeaTunnelRuntimeException exception2 =
183+
Assertions.assertThrows(
184+
SeaTunnelRuntimeException.class,
185+
() -> deserializationSchema2.deserialize(content2.getBytes()));
186+
Assertions.assertEquals(
187+
"ErrorCode:[COMMON-33], ErrorDescription:[The datetime format '2022-09-24-22:45:00' of field 'timestamp_field' is not supported. Please check the datetime format.]",
188+
exception2.getMessage());
189+
}
148190
}

0 commit comments

Comments
 (0)