Commit e7fd245
authored
[fix](constant folding) Reject overflowing from_second and from_millisecond literals (#67037)
### What problem does this PR solve?
Related PR: #28685
Problem Summary:
`from_second` and `from_millisecond` widen their argument to
microseconds **before** the range check runs, so the multiplication can
wrap and land back inside the accepted range.
`fromSecond` computes `second.getValue() * 1000 * 1000` and hands the
result to `fromMicroSecond(long, int)`, whose guard is `microSecond < 0
|| microSecond > 253402271999999999L`. The guard therefore only ever
sees the wrapped product:
| Expression | Product | Wraps to | Folds to |
|-----------------------------------------|----------------------------------------|----------|-----------------------------------|
| `from_second(18446744073710)` | `18446744073710 * 1000000` | `448384` | `1970-01-01 00:00:00.448384` |
| `from_millisecond(18446744073709552)` | `18446744073709552 * 1000` | `384` | `1970-01-01 00:00:00.000384` |
Both wrapped values sit inside `[0, 253402271999999999]`, so the guard passes.
`from_microsecond` passes its argument through unmultiplied and is
unaffected, which is why only the two widening wrappers are wrong.
These functions are registered for FE constant folding, so this is the
path a literal argument takes. The BE divides rather than multiplies
(`from_unixtime(value / Impl::ratio, ...)`) and rejects the same value,
so today the same expression errors over a column but folds to a bogus
datetime over a literal. #28685 fixed the BE side of this family in 2023
and did not touch the FE fold path.
### What is changed and how does it work?
The widening now goes through a small `toMicroSecond` helper that uses
`Math.multiplyExact` and converts the resulting `ArithmeticException`
into the same `AnalysisException` the range check already raises. An
overflowing literal now reports "out of range" instead of folding.
In-range arguments are unaffected, and negative arguments are still
rejected by the existing guard — both covered by the new tests.
### Release note
Fix `from_second` and `from_millisecond` returning a wrong datetime
instead of an out-of-range error for a literal argument large enough to
overflow when converted to microseconds.1 parent 955f720 commit e7fd245
2 files changed
Lines changed: 65 additions & 2 deletions
File tree
- fe/fe-core/src
- main/java/org/apache/doris/nereids/trees/expressions/functions/executable
- test/java/org/apache/doris/nereids/trees/expressions/functions/executable
Lines changed: 21 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1092 | 1092 | | |
1093 | 1093 | | |
1094 | 1094 | | |
1095 | | - | |
| 1095 | + | |
| 1096 | + | |
1096 | 1097 | | |
1097 | 1098 | | |
1098 | 1099 | | |
1099 | 1100 | | |
1100 | | - | |
| 1101 | + | |
| 1102 | + | |
1101 | 1103 | | |
1102 | 1104 | | |
1103 | 1105 | | |
| |||
1117 | 1119 | | |
1118 | 1120 | | |
1119 | 1121 | | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
1120 | 1139 | | |
1121 | 1140 | | |
1122 | 1141 | | |
| |||
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
212 | 256 | | |
0 commit comments