Repository navigation
[CLI] Preserve TIMESTAMP fields in table CSV imports and report conversion failures #19533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
eaa4935
6b10760
3f9fd0b
a06d84f
4765249
d663b5b
87fe2ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,10 @@ | |
| import java.io.FileReader; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.time.DateTimeException; | ||
| import java.time.format.DateTimeParseException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
|
|
@@ -292,13 +296,7 @@ protected void importFromCsvFile(File file) { | |
| ioTPrinter.println(CliMessages.LOG_THE_FIRST_FIELD_OF_HEADER_MUST_BE_TIME_2D6FAABE); | ||
| return; | ||
| } | ||
| String failedFilePath; | ||
| if (failedFileDirectory == null) { | ||
| failedFilePath = file.getAbsolutePath() + ".failed"; | ||
| } else { | ||
| failedFilePath = failedFileDirectory + file.getName() + ".failed"; | ||
| } | ||
| if (writeData(headerNames, records.collect(Collectors.toList()), failedFilePath)) { | ||
| if (writeData(headerNames, records.collect(Collectors.toList()), failedCsvPath(file))) { | ||
| processSuccessFile(); | ||
| } | ||
| } catch (IOException e) { | ||
|
|
@@ -314,6 +312,16 @@ protected void importFromCsvFile(File file) { | |
| } | ||
| } | ||
|
|
||
| private static String failedCsvPath(File file) { | ||
| if (failedFileDirectory == null) { | ||
| return file.getAbsolutePath() + ".failed"; | ||
| } | ||
| Path source = file.toPath().toAbsolutePath().normalize(); | ||
| Path root = Path.of(targetPath).toAbsolutePath().normalize(); | ||
| Path relative = source.equals(root) ? source.getFileName() : root.relativize(source); | ||
| return Path.of(failedFileDirectory).resolve(relative) + ".failed"; | ||
| } | ||
|
|
||
| protected boolean writeData( | ||
| List<String> headerNames, List<CSVRecord> records, String failedFilePath) { | ||
| Map<String, TSDataType> headerTypeMap = new HashMap<>(); | ||
|
|
@@ -338,9 +346,30 @@ protected boolean writeData( | |
| List<ColumnCategory> columnCategorys = new LinkedList<>(columnCategory.values()); | ||
| Tablet tablet = new Tablet(table, headNames, columnTypes, columnCategorys, batchPointSize); | ||
| for (CSVRecord recordObj : records) { | ||
| if (recordObj.size() < headerNames.size()) { | ||
| ioTPrinter.println( | ||
| String.format( | ||
| CliMessages.LOG_CSV_RECORD_ARG_HAS_ARG_COLUMNS_EXPECTED_AT_LEAST_ARG_8678387E, | ||
| recordObj.getRecordNumber(), | ||
| recordObj.size(), | ||
| headerNames.size())); | ||
| failedRecords.add(recordObj.stream().collect(Collectors.toList())); | ||
| continue; | ||
| } | ||
| String timeValue = recordObj.get(0); | ||
| Long rowTimeStamp = parseTimestampField(timeValue); | ||
| if (rowTimeStamp == null) { | ||
| ioTPrinter.printf( | ||
| CliMessages.LOG_LINE_ARG_COLUMN_ARG_ARG_CAN_T_CONVERT_TO_ARG_ARG_F85541A2, | ||
| recordObj.getRecordNumber(), | ||
| headerNames.get(0), | ||
| timeValue, | ||
| TSDataType.TIMESTAMP); | ||
| failedRecords.add(recordObj.stream().collect(Collectors.toList())); | ||
| continue; | ||
| } | ||
| boolean isFail = false; | ||
| final int rowSize = tablet.getRowSize(); | ||
| long rowTimeStamp = parseTimestamp(recordObj.get(0)); | ||
| for (String headerName : headerNameMap.keySet()) { | ||
| String value = recordObj.get(headerNameMap.get(headerName)); | ||
| if (!"".equals(value)) { | ||
|
|
@@ -376,7 +405,7 @@ protected boolean writeData( | |
| } | ||
| type = headerTypeMap.get(headerName); | ||
| if (type != null) { | ||
| Object valueTrans = typeTrans(value, type); | ||
| Object valueTrans = parseField(value, type); | ||
| if (valueTrans == null) { | ||
| isFail = true; | ||
| ioTPrinter.printf( | ||
|
|
@@ -404,11 +433,47 @@ protected boolean writeData( | |
| } | ||
|
|
||
| if (!failedRecords.isEmpty()) { | ||
| markFileFailed(); | ||
| if (failedFileDirectory != null) { | ||
| try { | ||
| Files.createDirectories(Path.of(failedFilePath).toAbsolutePath().getParent()); | ||
| } catch (IOException e) { | ||
| ioTPrinter.println( | ||
| String.format( | ||
| CliMessages.EXCEPTION_CANNOT_DUMP_FAIL_RESULT_BECAUSE_ARG_5C6E82E0, | ||
| e.getMessage())); | ||
| return false; | ||
| } | ||
| } | ||
| writeFailedLinesFile(headerNames, failedFilePath, failedRecords); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static Object parseField(String value, TSDataType type) { | ||
| if (type == TSDataType.TIMESTAMP) { | ||
| return parseTimestampField(value); | ||
| } | ||
| // Each byte needs two hex digits, with or without the optional 0x prefix. | ||
| if (type == TSDataType.BLOB && value.length() % 2 != 0) { | ||
| return null; | ||
| } | ||
| try { | ||
| return typeTrans(value, type); | ||
| } catch (DateTimeParseException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static Long parseTimestampField(String value) { | ||
| try { | ||
| return parseTimestamp(value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Reject trailing characters after the UTC designator. Importing |
||
| } catch (DateTimeException | ArithmeticException e) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Preserve valid nanosecond timestamps near The nanosecond conversion multiplies the epoch second by one billion before adding the fractional part. The intermediate product overflows even though the final value fits in a |
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static void writeAndEmptyDataSet(Tablet tablet, int retryTime) { | ||
| executeWithRetry( | ||
| () -> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Call out the wider exit-status change in the PR description. This
return falseapplies to every failed record, not onlyTIMESTAMPfields. A CSV withabcin anINT32column, or a column whose type cannot be inferred, used to printImport completely!and exit0; it now exits1. I think the new behavior is correct and matchesImportSchemaTable, but it is a user-visible change for scripts, so please state in the description that any partially failed table CSV now produces a nonzero exit status.