PDGGK opened a new pull request, #19289:
URL: https://github.com/apache/iotdb/pull/19289

   ## Description
   
   `Util.parseTime` in `library-udf` returns wrong values for the units `h` and 
`d` when `timestamp_precision` is `ns`. Both constants are multiplied as `int` 
and become `long` only at the last factor, so the product overflows first 
(`Util.java:303`, `:306`):
   
   | unit | returned | correct |
   |---|---|---|
   | `1h` | -694,967,296,000 | 3,600,000,000,000 |
   | `1d` | 500,654,080,000 | 86,400,000,000,000 |
   
   So `INTEGRAL` rejects `'unit'='1h'` as an unknown unit and returns 172.57 
times the correct value for `'unit'='1d'`, and `RESAMPLE` rejects 
`'every'='1h'`. The other library functions that read a duration through 
`parseTime` get the same values. I came across this while measuring `INTEGRAL` 
for the dev@ thread on its requirements.
   
   ### Change
   
   The first factor is now `long` (`60L * ...`, `24L * ...`), so the whole 
product is computed in `long`. The other units, and all units at `ms` and `us` 
precision, already fit in `int` before the `L` factor, so their values do not 
change.
   
   #17871 also changes `parseTime` and `UDAFIntegral`, but not these two 
constants. The negative-timestamp case of `INTEGRAL` that I mentioned on dev@ 
is handled there (`hasLastValue`), so it is not part of this PR.
   
   ### Verification
   
   - New `UtilParseTimeTest`: `1h`, `1d` and `2d` at `ns`, and every unit from 
`1ms` to `1d` compared across `ms`, `us` and `ns`. Both tests fail on master 
`c6e194d` with `expected:<3600000000000> but was:<-694967296000>` and pass with 
the change. All 21 tests in `library-udf` pass, and so does `spotless:check`.
   - On `apache/iotdb:2.0.11-standalone` with `timestamp_precision=ns`, with 
the `library-udf` jar built from v2.0.11 (its `parseTime` is the same as on 
master) without and with the change. A jar built from master bundles a newer 
TsFile than the 2.0.11 server, so I used the release sources. Data: `s1` = 1.0 
at time 0 and one day later.
   
   | query | v2.0.11 | with the change |
   |---|---|---|
   | `integral(s1, 'unit'='1s')` | 86400.0 | 86400.0 |
   | `integral(s1, 'unit'='1m')` | 1440.0 | 1440.0 |
   | `integral(s1, 'unit'='1h')` | `701: ... Unknown time unit input` | 24.0 |
   | `integral(s1, 'unit'='1d')` | 172.57424527530065 | 1.0 |
   | `integral(s1, 'unit'='2d')` | 86.28712263765033 | 0.5 |
   
   - `resample(s1, 'every'='1h')` on two points at 0 h and 2 h: v2.0.11 fails 
with `701: ... gap should be a time period whose unit is ms, s, m, h, d.`; with 
the change it returns the three rows at 0 h, 1 h and 2 h.
   
   <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.
   


-- 
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]

Reply via email to