Skip to content

[CLI] Preserve TIMESTAMP fields in table CSV imports and report conversion failures - #19533

Open
DaZuiZui wants to merge 7 commits into
apache:masterfrom
DaZuiZui:codex/fix-table-csv-timestamp-fields
Open

DaZuiZui wants to merge 7 commits into
apache:masterfrom
DaZuiZui:codex/fix-table-csv-timestamp-fields

Conversation

@DaZuiZui

@DaZuiZui DaZuiZui commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #19532. Depends on #19540. Please merge the shared parser change first, then update this branch against master.

export-data writes formatted TIMESTAMP fields by default. Table CSV import now uses the primary time column's timestamp parser for those fields. With an empty destination table containing event_ts TIMESTAMP FIELD and v INT32 FIELD, importing a row with v=7 gives the following results at millisecond precision.

CSV value of event_ts Before (stored value, exit code) After (stored value, exit code)
2026-12-04T11:33:17.083+08:00 from default export NULL, 0 1796355197083, 0
1796355197083 from -tf timestamp 1796355197083, 0 1796355197083, 0
invalid NULL, 0 NULL, 1

Table CSV import

  • A partially failed import exits 1. For example, abc in an INT32 field previously produced exit code 0. It now produces 1, writes the valid fields and records the original row in .failed_0.
  • With -s source -fd failed, failed rows in source/a/data.csv and source/b/data.csv previously went to the same failed/data.csv.failed_0, so one file could overwrite the other. They now go to failed/a/data.csv.failed_0 and failed/b/data.csv.failed_0.
  • With header time,event_ts, an invalid primary time (bad,1000) or a short row (2) previously aborted the file. Now the importer skips that row, records it in .failed_0 and continues to write later rows such as 3,1000.
  • An invalid DATE (2026-02-30) or odd-length BLOB (0xABC) previously aborted the file. Now the invalid field is NULL, other valid fields in that row are written, and the original row appears in .failed_0. Later valid rows are imported.
  • -tz applies to offset-free primary times and TIMESTAMP fields. With a UTC JVM and -tz +08:00, a primary time of 1970-01-01T08:00:00 previously became 28800000 ms. It now becomes 0 ms. An explicit offset in the CSV takes precedence.

Timestamp output

Default and iso8601 formatting now put the fractional seconds before the complete offset. This affects export-data, getString on TIMESTAMP columns in the Java Session and JDBC APIs, and CLI timestamp display.

Instant and display zone Before After
2026-12-04T03:33:17.083Z, -05:00 2026-12-03T22:33:17-05:00.083 2026-12-03T22:33:17.083-05:00
1900-01-01T00:00:00.083Z, Europe/Paris 1900-01-01T00:09:21+00.083:09:21 1900-01-01T00:09:21.083+00:09:21

Custom export patterns retain their existing formatting. For example, -tf yyyy-MM-dd-HH:mm formats 1796355197083 in Asia/Shanghai as 2026-12-04-11:33.083.

Regression coverage

DataToolRegressionTest covers default-format CSV round trips at millisecond, microsecond and nanosecond precision, configured time zones, partial imports and failed-row paths. RpcUtilsTest covers the shared timestamp formatter and the custom export pattern above. The original table CSV round trip was also tested against a local IoTDB Edge instance.

Tested locally with the parser from #19540: all 120 CLI tests and all 10 RpcUtilsTest cases passed. With the current master parser, four of the 120 CLI cases fail as expected: trailing text in either a primary time or a TIMESTAMP field, a nanosecond value near Long.MIN_VALUE, and a local time in a DST zone.

The corresponding tree-model CSV import issue is tracked in #19537.

@JackieTien97 JackieTien97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 111 CLI tests passed. I verified the imported values against IoTDB Edge. The default millisecond export at +08:00 round-tripped, and an invalid field produced exit code 1 while preserving the other fields in its row.

I also confirmed a pre-existing exporter issue on the parent commit fba8ff7738. With -tz -05:00, the default CSV contains 2026-12-03T22:33:17-05:00.083. The fractional seconds follow the offset, so importing the generated file fails on the primary time column. Exporter formatting

if (type != null) {
Object valueTrans = typeTrans(value, type);
Object valueTrans =
type == TSDataType.TIMESTAMP ? parseTimestampField(value) : typeTrans(value, type);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Honor the import time zone for fields without an offset. With the JVM in UTC and -tz +08:00, importing 1970-01-01T08:00:00 stored 28800000 ms and exited 0. The expected epoch value is 0. Table import reads -tz into timeZoneID, but this parser uses zoneId, which still holds the JVM default.

Region zones also need the offset for the date in the field. In a December run with America/New_York as the JVM zone, 2026-07-01T12:00:00 was stored as 17:00Z. The July offset puts it at 16:00Z. Please pass the configured ZoneId through to the parser and resolve offset-free values using the input date. Offset lookup


private static Long parseTimestampField(String value) {
try {
return parseTimestamp(value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reject trailing characters after the UTC designator. Importing 2026-12-04T03:33:17.083Zjunk stored 1796355197083 and exited 0 without producing a failed-row file. The shared parser truncates the string at the first Z, so the exception handler never sees the malformed suffix. Please validate the entire field and cover this case with assertions for exit code 1 and the row in .failed_0. UTC normalization

private static Long parseTimestampField(String value) {
try {
return parseTimestamp(value);
} catch (DateTimeException | ArithmeticException e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Preserve valid nanosecond timestamps near Long.MIN_VALUE. The default CSV export of -9223372036854775807 is 1677-09-21T00:12:43.145224193Z. Importing that CSV with -tp ns left the field NULL and exited 1. The numeric export of the same value round-tripped.

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 long. Please use the negative-instant adjustment already present in the microsecond branch and cover this value in a round-trip test. Precision conversion

@JackieTien97 JackieTien97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few additional points not covered by the earlier review. I re-ran DataToolRegressionTest on this head (47/47 pass) and confirmed both new tests fail when ImportDataTable.java is reverted to 7c6774e0fe.

if (!failedRecords.isEmpty()) {
markFileFailed();
writeFailedLinesFile(headerNames, failedFilePath, failedRecords);
return false;

Copy link
Copy Markdown
Contributor

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 false applies to every failed record, not only TIMESTAMP fields. A CSV with abc in an INT32 column, or a column whose type cannot be inferred, used to print Import completely! and exit 0; it now exits 1. I think the new behavior is correct and matches ImportSchemaTable, 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.

type = headerTypeMap.get(headerName);
if (type != null) {
Object valueTrans = typeTrans(value, type);
Object valueTrans =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] The tree-model importer has the same round-trip gap. ExportDataTree writes non-time columns with iterator.getString, which formats TIMESTAMP values through RpcUtils.formatDatetime (ISO offset format by default). ImportDataTree and AbstractDataTool.writeDataAlignedByDevice still send TIMESTAMP fields to typeTrans, i.e. Long.parseLong, and they also do not mark the file as failed when failedRecords is non-empty. Consider moving the TIMESTAMP handling into AbstractDataTool.typeTrans so both dialects share it and this call site needs no special case, or track the tree-model fix in a follow-up issue linked from this PR.

long failures = loadFileFailedNum.sum();
long successes = loadFileSuccessfulNum.sum();
ImportDataScanTool.putToQueue(csv.toString());
assertFalse(AbstractImportData.init(new ImportDataTable()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Assert that the valid data is still written. The description says valid fields are kept when a field conversion fails, but this test only checks the counters and the failed-row file. Please capture session.insert (as in testTableCsvTimestampFieldRoundTrip) and assert that row 1,1000 was inserted, so a regression that drops the whole file would be caught.

}

@Test
public void testTableCsvTimestampFieldRoundTrip() throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Cover a non-millisecond precision. The fix relies on parseTimestamp applying timestampPrecision to formatted values, but the round trip only runs with ms. Besides the ns edge case raised earlier, please add a regular us round trip (export with timestampPrecision = "us", import the default-format CSV, and assert the original microsecond value).

When a directory import uses -fd, retain each source directory in the failure path so same-named CSV files can be retried independently. Keep the existing flat name for a single input file.

Report invalid dates and odd-length hex fields as row failures while inserting valid fields and continuing the batch.
@DaZuiZui
DaZuiZui requested a review from JackieTien97 December 4, 2026 07:55
@DaZuiZui

DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Please hold off on the review for a moment; I still have some issues to address.

@DaZuiZui

DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @JackieTien97. I added the round-trip test and opened #19537 for the tree model. Could you take another look when you have a chance?

@JackieTien97 JackieTien97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. All earlier comments are addressed, and on d663b5bfe0 the 120 CLI tests, RpcUtilsTest, DateTimeUtilsTest, StatementGeneratorTest and the cast tests pass locally. Three things are still needed before this can be merged.

1. Move the server-side parser change to a separate PR. See the inline comment on DateTimeUtils. This PR should then contain only CLI changes and state that it depends on the new PR.

2. Update the PR description. It still lists only ImportDataTable and DataToolRegressionTest, but the branch now changes several user-visible behaviors. Per the repository's PR guidelines, please describe each one with a before/after example:

  • Partially failed table CSV imports exit 1 instead of 0 (already described).
  • With -fd and a directory source, failed-row files keep the source's relative subdirectories instead of being written flat, where same-named files overwrote each other.
  • An invalid primary time, a record with too few columns, an invalid DATE, or an odd-length BLOB now sends only that row to the failed-row file. Previously the whole file was aborted.
  • -tz now applies to offset-free values in table CSV imports. Previously they used the JVM default zone.
  • export-data output for negative offsets and second-precision historical offsets changes, for example from 2026-12-03T22:33:17-05:00.083 to 2026-12-03T22:33:17.083-05:00. The same RpcUtils path also changes getString on TIMESTAMP columns in the Java Session, JDBC and CLI display, so please call that out as well.

3. Fix the -tf custom-format regression in RpcUtils. See the inline comment.

@DaZuiZui

DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

thank review~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Table CSV import loses TIMESTAMP fields from the default export and exits successfully

2 participants