Skip to content

Commit a82066f

Browse files
wt0530githubgxll
authored andcommitted
[fix][runtime] Fix interval value upper and lower limit verification
1 parent f499ddd commit a82066f

2 files changed

Lines changed: 86 additions & 60 deletions

File tree

runtime/src/main/java/io/dingodb/expr/runtime/op/BinaryIntervalOp.java

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,57 @@ public abstract class BinaryIntervalOp extends BinaryOp {
4040
}
4141

4242
public static IntervalType buildInterval(BigDecimal value0, IntervalType value1) {
43-
if (value1 instanceof IntervalYearType.IntervalYear) {
44-
return new IntervalYearType.IntervalYear(value0.multiply(new BigDecimal(12)),
45-
((IntervalYearType.IntervalYear) value1).elementType);
46-
} else if (value1 instanceof IntervalMonthType.IntervalMonth) {
47-
return new IntervalMonthType.IntervalMonth(value0, ((IntervalMonthType.IntervalMonth) value1).elementType);
48-
} else if (value1 instanceof IntervalDayType.IntervalDay) {
49-
return new IntervalDayType.IntervalDay(value0.multiply(new BigDecimal(24 * 60 * 60 * 1000)),
50-
((IntervalDayType.IntervalDay) value1).elementType
51-
);
52-
} else if (value1 instanceof IntervalWeekType.IntervalWeek) {
53-
return new IntervalWeekType.IntervalWeek(value0.multiply(new BigDecimal(60 * 60 * 1000)),
54-
((IntervalWeekType.IntervalWeek) value1).elementType
55-
);
56-
} else if (value1 instanceof IntervalHourType.IntervalHour) {
57-
return new IntervalHourType.IntervalHour(value0.multiply(new BigDecimal(60 * 60 * 1000)),
58-
((IntervalHourType.IntervalHour) value1).elementType
59-
);
60-
} else if (value1 instanceof IntervalMinuteType.IntervalMinute) {
61-
return new IntervalHourType.IntervalHour(value0.multiply(new BigDecimal(60 * 1000)),
62-
((IntervalMinuteType.IntervalMinute) value1).elementType
63-
);
64-
} else if (value1 instanceof IntervalSecondType.IntervalSecond) {
65-
return new IntervalSecondType.IntervalSecond(value0.multiply(new BigDecimal(1000)),
66-
((IntervalSecondType.IntervalSecond) value1).elementType
67-
);
43+
checkLegalRange(value0);
44+
try {
45+
if (value1 instanceof IntervalYearType.IntervalYear) {
46+
return new IntervalYearType.IntervalYear(new BigDecimal(Math.multiplyExact(value0.longValue(), 12)),
47+
((IntervalYearType.IntervalYear) value1).elementType);
48+
} else if (value1 instanceof IntervalMonthType.IntervalMonth) {
49+
return new IntervalMonthType.IntervalMonth(
50+
value0, ((IntervalMonthType.IntervalMonth) value1).elementType);
51+
} else if (value1 instanceof IntervalDayType.IntervalDay) {
52+
return new IntervalDayType.IntervalDay(
53+
new BigDecimal(Math.multiplyExact(value0.longValue(), 24 * 60 * 60 * 1000L)),
54+
((IntervalDayType.IntervalDay) value1).elementType
55+
);
56+
} else if (value1 instanceof IntervalWeekType.IntervalWeek) {
57+
return new IntervalWeekType.IntervalWeek(
58+
new BigDecimal(Math.multiplyExact(value0.longValue(), 60 * 60 * 1000L)),
59+
((IntervalWeekType.IntervalWeek) value1).elementType
60+
);
61+
} else if (value1 instanceof IntervalHourType.IntervalHour) {
62+
return new IntervalHourType.IntervalHour(
63+
new BigDecimal(Math.multiplyExact(value0.longValue(), 60 * 60 * 1000L)),
64+
((IntervalHourType.IntervalHour) value1).elementType
65+
);
66+
} else if (value1 instanceof IntervalMinuteType.IntervalMinute) {
67+
return new IntervalHourType.IntervalHour(
68+
new BigDecimal(Math.multiplyExact(value0.longValue(), 60 * 1000)),
69+
((IntervalMinuteType.IntervalMinute) value1).elementType
70+
);
71+
} else if (value1 instanceof IntervalSecondType.IntervalSecond) {
72+
return new IntervalSecondType.IntervalSecond(
73+
74+
75+
new BigDecimal(Math.multiplyExact(value0.longValue(), 1000)),
76+
((IntervalSecondType.IntervalSecond) value1).elementType
77+
);
78+
}
79+
} catch (ArithmeticException e) {
80+
return null;
6881
}
6982
return null;
7083
}
84+
85+
private static void checkLegalRange(BigDecimal value0) {
86+
if (value0.signum() == -1) {
87+
if (value0.compareTo(new BigDecimal(Long.MIN_VALUE / 1000)) < 0) {
88+
throw new RuntimeException("Interval parameter is smaller than the minimum range: -9223372036854775");
89+
}
90+
} else {
91+
if (value0.compareTo(new BigDecimal(Long.MAX_VALUE / 1000)) > 0) {
92+
throw new RuntimeException("Interval parameter is larger than the maximum range: 9223372036854775");
93+
}
94+
}
95+
}
7196
}

runtime/src/main/java/io/dingodb/expr/runtime/op/interval/MulOp.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.checkerframework.checker.nullness.qual.NonNull;
3232

3333
import java.math.BigDecimal;
34+
import java.math.RoundingMode;
3435
import java.sql.Date;
3536
import java.sql.Time;
3637
import java.sql.Timestamp;
@@ -76,142 +77,142 @@ static IntervalSecondType.IntervalSecond mul(String value0, IntervalSecondType.I
7677
}
7778

7879
static IntervalYearType.IntervalYear mul(Integer value0, IntervalYearType.IntervalYear value1) {
79-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
80+
BigDecimal decimal = new BigDecimal(value0);
8081
return (IntervalYearType.IntervalYear) buildInterval(decimal, value1);
8182
}
8283

8384
static IntervalMonthType.IntervalMonth mul(Integer value0, IntervalMonthType.IntervalMonth value1) {
84-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
85+
BigDecimal decimal = new BigDecimal(value0);
8586
return (IntervalMonthType.IntervalMonth) buildInterval(decimal, value1);
8687
}
8788

8889
static IntervalDayType.IntervalDay mul(Integer value0, IntervalDayType.IntervalDay value1) {
89-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
90+
BigDecimal decimal = new BigDecimal(value0);
9091
return (IntervalDayType.IntervalDay) buildInterval(decimal, value1);
9192
}
9293

9394
static IntervalWeekType.IntervalWeek mul(Integer value0, IntervalWeekType.IntervalWeek value1) {
94-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
95+
BigDecimal decimal = new BigDecimal(value0);
9596
return (IntervalWeekType.IntervalWeek) buildInterval(decimal, value1);
9697
}
9798

9899
static IntervalHourType.IntervalHour mul(Integer value0, IntervalHourType.IntervalHour value1) {
99-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
100+
BigDecimal decimal = new BigDecimal(value0);
100101
return (IntervalHourType.IntervalHour) buildInterval(decimal, value1);
101102
}
102103

103104
static IntervalMinuteType.IntervalMinute mul(Integer value0, IntervalMinuteType.IntervalMinute value1) {
104-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
105+
BigDecimal decimal = new BigDecimal(value0);
105106
return (IntervalMinuteType.IntervalMinute) buildInterval(decimal, value1);
106107
}
107108

108109
static IntervalSecondType.IntervalSecond mul(Integer value0, IntervalSecondType.IntervalSecond value1) {
109-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
110+
BigDecimal decimal = new BigDecimal(value0);
110111
return (IntervalSecondType.IntervalSecond) buildInterval(decimal, value1);
111112
}
112113

113114
static IntervalYearType.IntervalYear mul(Long value0, IntervalYearType.IntervalYear value1) {
114-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
115+
BigDecimal decimal = new BigDecimal(value0);
115116
return (IntervalYearType.IntervalYear) buildInterval(decimal, value1);
116117
}
117118

118119
static IntervalMonthType.IntervalMonth mul(Long value0, IntervalMonthType.IntervalMonth value1) {
119-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
120+
BigDecimal decimal = new BigDecimal(value0);
120121
return (IntervalMonthType.IntervalMonth) buildInterval(decimal, value1);
121122
}
122123

123124
static IntervalDayType.IntervalDay mul(Long value0, IntervalDayType.IntervalDay value1) {
124-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
125+
BigDecimal decimal = new BigDecimal(value0);
125126
return (IntervalDayType.IntervalDay) buildInterval(decimal, value1);
126127
}
127128

128129
static IntervalWeekType.IntervalWeek mul(Long value0, IntervalWeekType.IntervalWeek value1) {
129-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
130+
BigDecimal decimal = new BigDecimal(value0);
130131
return (IntervalWeekType.IntervalWeek) buildInterval(decimal, value1);
131132
}
132133

133134
static IntervalHourType.IntervalHour mul(Long value0, IntervalHourType.IntervalHour value1) {
134-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
135+
BigDecimal decimal = new BigDecimal(value0);
135136
return (IntervalHourType.IntervalHour) buildInterval(decimal, value1);
136137
}
137138

138139
static IntervalMinuteType.IntervalMinute mul(Long value0, IntervalMinuteType.IntervalMinute value1) {
139-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
140+
BigDecimal decimal = new BigDecimal(value0);
140141
return (IntervalMinuteType.IntervalMinute) buildInterval(decimal, value1);
141142
}
142143

143144
static IntervalSecondType.IntervalSecond mul(Long value0, IntervalSecondType.IntervalSecond value1) {
144-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
145+
BigDecimal decimal = new BigDecimal(value0);
145146
return (IntervalSecondType.IntervalSecond) buildInterval(decimal, value1);
146147
}
147148

148149
static IntervalYearType.IntervalYear mul(Double value0, IntervalYearType.IntervalYear value1) {
149-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
150+
BigDecimal decimal = new BigDecimal(Math.round(value0));
150151
return (IntervalYearType.IntervalYear) buildInterval(decimal, value1);
151152
}
152153

153154
static IntervalMonthType.IntervalMonth mul(Double value0, IntervalMonthType.IntervalMonth value1) {
154-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
155+
BigDecimal decimal = new BigDecimal(Math.round(value0));
155156
return (IntervalMonthType.IntervalMonth) buildInterval(decimal, value1);
156157
}
157158

158159
static IntervalDayType.IntervalDay mul(Double value0, IntervalDayType.IntervalDay value1) {
159-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
160+
BigDecimal decimal = new BigDecimal(Math.round(value0));
160161
return (IntervalDayType.IntervalDay) buildInterval(decimal, value1);
161162
}
162163

163164
static IntervalWeekType.IntervalWeek mul(Double value0, IntervalWeekType.IntervalWeek value1) {
164-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
165+
BigDecimal decimal = new BigDecimal(Math.round(value0));
165166
return (IntervalWeekType.IntervalWeek) buildInterval(decimal, value1);
166167
}
167168

168169
static IntervalHourType.IntervalHour mul(Double value0, IntervalHourType.IntervalHour value1) {
169-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
170+
BigDecimal decimal = new BigDecimal(Math.round(value0));
170171
return (IntervalHourType.IntervalHour) buildInterval(decimal, value1);
171172
}
172173

173174
static IntervalMinuteType.IntervalMinute mul(Double value0, IntervalMinuteType.IntervalMinute value1) {
174-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
175+
BigDecimal decimal = new BigDecimal(Math.round(value0));
175176
return (IntervalMinuteType.IntervalMinute) buildInterval(decimal, value1);
176177
}
177178

178179
static IntervalSecondType.IntervalSecond mul(Double value0, IntervalSecondType.IntervalSecond value1) {
179-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
180+
BigDecimal decimal = new BigDecimal(Math.round(value0));
180181
return (IntervalSecondType.IntervalSecond) buildInterval(decimal, value1);
181182
}
182183

183184
static IntervalYearType.IntervalYear mul(Float value0, IntervalYearType.IntervalYear value1) {
184-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
185+
BigDecimal decimal = new BigDecimal(Math.round(value0));
185186
return (IntervalYearType.IntervalYear) buildInterval(decimal, value1);
186187
}
187188

188189
static IntervalMonthType.IntervalMonth mul(Float value0, IntervalMonthType.IntervalMonth value1) {
189-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
190+
BigDecimal decimal = new BigDecimal(Math.round(value0));
190191
return (IntervalMonthType.IntervalMonth) buildInterval(decimal, value1);
191192
}
192193

193194
static IntervalDayType.IntervalDay mul(Float value0, IntervalDayType.IntervalDay value1) {
194-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
195+
BigDecimal decimal = new BigDecimal(Math.round(value0));
195196
return (IntervalDayType.IntervalDay) buildInterval(decimal, value1);
196197
}
197198

198199
static IntervalWeekType.IntervalWeek mul(Float value0, IntervalWeekType.IntervalWeek value1) {
199-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
200+
BigDecimal decimal = new BigDecimal(Math.round(value0));
200201
return (IntervalWeekType.IntervalWeek) buildInterval(decimal, value1);
201202
}
202203

203204
static IntervalHourType.IntervalHour mul(Float value0, IntervalHourType.IntervalHour value1) {
204-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
205+
BigDecimal decimal = new BigDecimal(Math.round(value0));
205206
return (IntervalHourType.IntervalHour) buildInterval(decimal, value1);
206207
}
207208

208209
static IntervalMinuteType.IntervalMinute mul(Float value0, IntervalMinuteType.IntervalMinute value1) {
209-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
210+
BigDecimal decimal = new BigDecimal(Math.round(value0));
210211
return (IntervalMinuteType.IntervalMinute) buildInterval(decimal, value1);
211212
}
212213

213214
static IntervalSecondType.IntervalSecond mul(Float value0, IntervalSecondType.IntervalSecond value1) {
214-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
215+
BigDecimal decimal = new BigDecimal(Math.round(value0));
215216
return (IntervalSecondType.IntervalSecond) buildInterval(decimal, value1);
216217
}
217218

@@ -251,37 +252,37 @@ static IntervalSecondType.IntervalSecond mul(Boolean value0, IntervalSecondType.
251252
}
252253

253254
static IntervalYearType.IntervalYear mul(BigDecimal value0, IntervalYearType.IntervalYear value1) {
254-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
255+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
255256
return (IntervalYearType.IntervalYear) buildInterval(decimal, value1);
256257
}
257258

258259
static IntervalMonthType.IntervalMonth mul(BigDecimal value0, IntervalMonthType.IntervalMonth value1) {
259-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
260+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
260261
return (IntervalMonthType.IntervalMonth) buildInterval(decimal, value1);
261262
}
262263

263264
static IntervalDayType.IntervalDay mul(BigDecimal value0, IntervalDayType.IntervalDay value1) {
264-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
265+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
265266
return (IntervalDayType.IntervalDay) buildInterval(decimal, value1);
266267
}
267268

268269
static IntervalWeekType.IntervalWeek mul(BigDecimal value0, IntervalWeekType.IntervalWeek value1) {
269-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
270+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
270271
return (IntervalWeekType.IntervalWeek) buildInterval(decimal, value1);
271272
}
272273

273274
static IntervalHourType.IntervalHour mul(BigDecimal value0, IntervalHourType.IntervalHour value1) {
274-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
275+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
275276
return (IntervalHourType.IntervalHour) buildInterval(decimal, value1);
276277
}
277278

278279
static IntervalMinuteType.IntervalMinute mul(BigDecimal value0, IntervalMinuteType.IntervalMinute value1) {
279-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
280+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
280281
return (IntervalMinuteType.IntervalMinute) buildInterval(decimal, value1);
281282
}
282283

283284
static IntervalSecondType.IntervalSecond mul(BigDecimal value0, IntervalSecondType.IntervalSecond value1) {
284-
BigDecimal decimal = new BigDecimal(Math.round(CastWithString.doubleCastWithStringCompat(value0.toString())));
285+
BigDecimal decimal = value0.setScale(0, RoundingMode.HALF_UP);
285286
return (IntervalSecondType.IntervalSecond) buildInterval(decimal, value1);
286287
}
287288

0 commit comments

Comments
 (0)