JackieTien97 opened a new pull request, #19284:
URL: https://github.com/apache/iotdb/pull/19284
## Description
Fixes #19283.
With `timestamp_precision=us`, `DateTimeUtils.getInstantWithPrecision`
converted a datetime before `1970-01-01T00:00:00Z` that has a fractional second
to a timestamp 999,999 us too large. For a negative instant with a fractional
part, the `us` branch follows `Instant.toEpochMilli()`: it multiplies
`epochSecond + 1` and must then subtract that extra second, but it subtracted
`1` instead of `1_000_000` (one second in microseconds).
`convertDatetimeStrToLong` ends in this method, so datetime literals in tree-
and table-model SQL and `CAST(<string> AS TIMESTAMP)` were all affected.
The fix subtracts `1_000_000L`. Whole seconds, instants from 1970 onwards,
and the `ms`/`ns` branches do not change. The method keeps the `epochSecond +
1` form, so the multiplication cannot overflow near `Long.MIN_VALUE`, and a
value outside the `long` range still throws `ArithmeticException`.
### Example
1C1D with `timestamp_precision=us`, CLI with `TZ=UTC`:
```sql
INSERT INTO root.us_test.d1(time, s1) VALUES (1969-12-31T23:59:59.5+00:00,
1);
INSERT INTO root.us_test.d1(time, s1) VALUES (1970-01-01T00:00:00.5+00:00,
2);
INSERT INTO root.us_test.d2(time, s1) VALUES (-500000, 3);
SELECT s1 FROM root.us_test.d1;
SELECT s1 FROM root.us_test.d2 WHERE time = 1969-12-31T23:59:59.5+00:00;
```
Before, the pre-1970 row was stored 1 us before the 1970 row, and the
`WHERE` query found nothing:
```
+---------------------------+------------------+
| Time|root.us_test.d1.s1|
+---------------------------+------------------+
|1970-01-01T00:00:00.499999Z| 1|
|1970-01-01T00:00:00.500000Z| 2|
+---------------------------+------------------+
+----+------------------+
|Time|root.us_test.d2.s1|
+----+------------------+
+----+------------------+
Empty set.
```
After:
```
+---------------------------+------------------+
| Time|root.us_test.d1.s1|
+---------------------------+------------------+
|1969-12-31T23:59:59.500000Z| 1|
|1970-01-01T00:00:00.500000Z| 2|
+---------------------------+------------------+
+---------------------------+------------------+
| Time|root.us_test.d2.s1|
+---------------------------+------------------+
|1969-12-31T23:59:59.500000Z| 3|
+---------------------------+------------------+
```
The table model behaves the same way: `INSERT INTO t (time, device_id, s)
VALUES (1969-12-31T23:59:59.5+00:00, 'd1', 1)` stored `499999` and now stores
`-500000`; `CAST('1969-12-31T23:59:59.5Z' AS TIMESTAMP)` returned `499999` and
now returns `-500000`.
### Tests
`DateTimeUtilsTest.getInstantWithPrecisionTest` did not call
`getInstantWithPrecision`; its body was a copy of
`convertDurationStrToLongTest3`. It now checks the `ms`/`us`/`ns` branches at
and after the epoch, whole seconds before it, the largest `us` value, and the
`ArithmeticException` one microsecond later. Two new tests cover fractional
seconds before the epoch, directly and through `convertDatetimeStrToLong` (with
`Z`, without an offset, and in a UTC+8 session). Without the fix both fail with
`expected:<-500000> but was:<499999>`.
<hr>
This PR has:
- [x] been self-reviewed.
- [x] added unit tests or modified existing tests to cover new code paths,
ensuring the threshold
for code coverage.
- [x] been tested in a test IoTDB cluster.
<hr>
##### Key changed/added classes (or packages if there are too many classes)
in this PR
- `iotdb-core/node-commons`:
`org.apache.iotdb.commons.queryengine.utils.DateTimeUtils`
- `iotdb-core/datanode` (test): `org.apache.iotdb.db.utils.DateTimeUtilsTest`
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]