Skip to content

Commit a6e1c16

Browse files
committed
[fix][runtime] Max, min supports any type
1 parent 45e49cd commit a6e1c16

4 files changed

Lines changed: 620 additions & 2 deletions

File tree

runtime/src/main/java/io/dingodb/expr/runtime/op/mathematical/MaxFun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.sql.Time;
2626
import java.sql.Timestamp;
2727

28-
@Operators
28+
//@Operators
2929
abstract class MaxFun extends BinaryNumericOp {
3030
public static final String NAME = "MAX";
3131

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
/*
2+
* Copyright 2021 DataCanvas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.dingodb.expr.runtime.op.mathematical;
18+
19+
import io.dingodb.expr.common.type.Type;
20+
import io.dingodb.expr.common.type.Types;
21+
import io.dingodb.expr.runtime.ExprConfig;
22+
import io.dingodb.expr.runtime.op.BinaryOp;
23+
import io.dingodb.expr.runtime.op.OpKey;
24+
import org.checkerframework.checker.nullness.qual.NonNull;
25+
26+
import java.io.Serial;
27+
import java.math.BigDecimal;
28+
import java.sql.Date;
29+
import java.sql.Time;
30+
import java.sql.Timestamp;
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
35+
public class MaxFunFactory extends MaxFun {
36+
37+
private static final long serialVersionUID = -5055347917250761383L;
38+
39+
public static final MaxFunFactory INSTANCE = new MaxFunFactory();
40+
41+
private final Map<Object, MaxFun> opMap = new HashMap<>();
42+
43+
private MaxFunFactory() {
44+
super();
45+
opMap.put(keyOf(Types.FLOAT, Types.FLOAT), new MaxFloatFloat());
46+
opMap.put(keyOf(Types.DATE, Types.DATE), new MaxDateDate());
47+
opMap.put(keyOf(Types.TIME, Types.TIME), new MaxTimeTime());
48+
opMap.put(keyOf(Types.DECIMAL, Types.DECIMAL), new MaxDecimalDecimal());
49+
opMap.put(keyOf(Types.TIMESTAMP, Types.TIMESTAMP), new MaxTimestampTimestamp());
50+
opMap.put(keyOf(Types.INT, Types.INT), new MaxIntInt());
51+
opMap.put(keyOf(Types.BOOL, Types.BOOL), new MaxBoolBool());
52+
opMap.put(keyOf(Types.LONG, Types.LONG), new MaxLongLong());
53+
opMap.put(keyOf(Types.STRING, Types.STRING), new MaxStringString());
54+
opMap.put(keyOf(Types.DOUBLE, Types.DOUBLE), new MaxDoubleDouble());
55+
opMap.put(keyOf(Types.ANY, Types.ANY), new MaxAnyAny());
56+
}
57+
58+
@Override
59+
public BinaryOp getOp(OpKey key) {
60+
return opMap.get(key);
61+
}
62+
63+
public static final class MaxFloatFloat extends MaxFun {
64+
private static final long serialVersionUID = 2555474218069097492L;
65+
66+
@Override
67+
protected Float evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
68+
ExprConfig config) {
69+
return max((Float) value0, (Float) value1);
70+
}
71+
72+
@Override
73+
public Type getType() {
74+
return Types.FLOAT;
75+
}
76+
77+
@Override
78+
public OpKey getKey() {
79+
return keyOf(Types.FLOAT, Types.FLOAT);
80+
}
81+
}
82+
83+
public static final class MaxDateDate extends MaxFun {
84+
private static final long serialVersionUID = 7735469538485461203L;
85+
86+
@Override
87+
protected Date evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
88+
ExprConfig config) {
89+
return max((Date) value0, (Date) value1);
90+
}
91+
92+
@Override
93+
public Type getType() {
94+
return Types.DATE;
95+
}
96+
97+
@Override
98+
public OpKey getKey() {
99+
return keyOf(Types.DATE, Types.DATE);
100+
}
101+
}
102+
103+
public static final class MaxTimeTime extends MaxFun {
104+
private static final long serialVersionUID = 8336462365771873541L;
105+
106+
@Override
107+
protected Time evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
108+
ExprConfig config) {
109+
return max((Time) value0, (Time) value1);
110+
}
111+
112+
@Override
113+
public Type getType() {
114+
return Types.TIME;
115+
}
116+
117+
@Override
118+
public OpKey getKey() {
119+
return keyOf(Types.TIME, Types.TIME);
120+
}
121+
}
122+
123+
public static final class MaxDecimalDecimal extends MaxFun {
124+
private static final long serialVersionUID = -727474314233293589L;
125+
126+
@Override
127+
protected BigDecimal evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
128+
ExprConfig config) {
129+
return max((BigDecimal) value0, (BigDecimal) value1);
130+
}
131+
132+
@Override
133+
public Type getType() {
134+
return Types.DECIMAL;
135+
}
136+
137+
@Override
138+
public OpKey getKey() {
139+
return keyOf(Types.DECIMAL, Types.DECIMAL);
140+
}
141+
}
142+
143+
public static final class MaxTimestampTimestamp extends MaxFun {
144+
private static final long serialVersionUID = -2537421797606034469L;
145+
146+
@Override
147+
protected Timestamp evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
148+
ExprConfig config) {
149+
return max((Timestamp) value0, (Timestamp) value1);
150+
}
151+
152+
@Override
153+
public Type getType() {
154+
return Types.TIMESTAMP;
155+
}
156+
157+
@Override
158+
public OpKey getKey() {
159+
return keyOf(Types.TIMESTAMP, Types.TIMESTAMP);
160+
}
161+
}
162+
163+
public static final class MaxIntInt extends MaxFun {
164+
private static final long serialVersionUID = -1863224250123733868L;
165+
166+
@Override
167+
protected Integer evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
168+
ExprConfig config) {
169+
return max((Integer) value0, (Integer) value1);
170+
}
171+
172+
@Override
173+
public Type getType() {
174+
return Types.INT;
175+
}
176+
177+
@Override
178+
public OpKey getKey() {
179+
return keyOf(Types.INT, Types.INT);
180+
}
181+
}
182+
183+
public static final class MaxBoolBool extends MaxFun {
184+
private static final long serialVersionUID = 6855481395697267938L;
185+
186+
@Override
187+
protected Boolean evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
188+
ExprConfig config) {
189+
return max((Boolean) value0, (Boolean) value1);
190+
}
191+
192+
@Override
193+
public Type getType() {
194+
return Types.BOOL;
195+
}
196+
197+
@Override
198+
public OpKey getKey() {
199+
return keyOf(Types.BOOL, Types.BOOL);
200+
}
201+
}
202+
203+
public static final class MaxLongLong extends MaxFun {
204+
private static final long serialVersionUID = 1968842193821058281L;
205+
206+
@Override
207+
protected Long evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
208+
ExprConfig config) {
209+
return max((Long) value0, (Long) value1);
210+
}
211+
212+
@Override
213+
public Type getType() {
214+
return Types.LONG;
215+
}
216+
217+
@Override
218+
public OpKey getKey() {
219+
return keyOf(Types.LONG, Types.LONG);
220+
}
221+
}
222+
223+
public static final class MaxStringString extends MaxFun {
224+
private static final long serialVersionUID = 8529601134199613613L;
225+
226+
@Override
227+
protected String evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
228+
ExprConfig config) {
229+
return max((String) value0, (String) value1);
230+
}
231+
232+
@Override
233+
public Type getType() {
234+
return Types.STRING;
235+
}
236+
237+
@Override
238+
public OpKey getKey() {
239+
return keyOf(Types.STRING, Types.STRING);
240+
}
241+
}
242+
243+
public static final class MaxDoubleDouble extends MaxFun {
244+
private static final long serialVersionUID = -4919337984780906388L;
245+
246+
@Override
247+
protected Double evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
248+
ExprConfig config) {
249+
return max((Double) value0, (Double) value1);
250+
}
251+
252+
@Override
253+
public Type getType() {
254+
return Types.DOUBLE;
255+
}
256+
257+
@Override
258+
public OpKey getKey() {
259+
return keyOf(Types.DOUBLE, Types.DOUBLE);
260+
}
261+
}
262+
263+
public static final class MaxAnyAny extends MaxFun {
264+
265+
@Serial
266+
private static final long serialVersionUID = -5582716732694590377L;
267+
268+
@Override
269+
protected Object evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
270+
ExprConfig config) {
271+
if (value0 instanceof Integer && value1 instanceof Integer) {
272+
return max((Integer) value0, (Integer) value1);
273+
} else if (value0 instanceof BigDecimal && value1 instanceof BigDecimal) {
274+
return max((BigDecimal) value0, (BigDecimal) value1);
275+
} else if (value0 instanceof Long && value1 instanceof Long) {
276+
return max((Long) value0, (Long) value1);
277+
} else if (value0 instanceof Float && value1 instanceof Float) {
278+
return max((Float) value0, (Float) value1);
279+
} else if (value0 instanceof Double && value1 instanceof Double) {
280+
return max((Double) value0, (Double) value1);
281+
} else if (value0 instanceof String && value1 instanceof String) {
282+
return max((String) value0, (String) value1);
283+
} else if (value0 instanceof Boolean && value1 instanceof Boolean) {
284+
return max((Boolean) value0, (Boolean) value1);
285+
} else if (value0 instanceof Timestamp && value1 instanceof Timestamp) {
286+
return max((Timestamp) value0, (Timestamp) value1);
287+
} else if (value0 instanceof Date && value1 instanceof Date) {
288+
return max((Date) value0, (Date) value1);
289+
} else if (value0 instanceof Time && value1 instanceof Time) {
290+
return max((Time) value0, (Time) value1);
291+
} else if (value0.getClass() != value1.getClass() && value0 instanceof Number && value1 instanceof Number) {
292+
BigDecimal value0Decimal = new BigDecimal(value0.toString());
293+
BigDecimal value1Decimal = new BigDecimal(value1.toString());
294+
return max(value0Decimal, value1Decimal);
295+
} else {
296+
return max(value0.toString(), value1.toString());
297+
}
298+
}
299+
300+
@Override
301+
public Type getType() {
302+
return Types.ANY;
303+
}
304+
305+
@Override
306+
public OpKey getKey() {
307+
return keyOf(Types.ANY, Types.ANY);
308+
}
309+
}
310+
}

runtime/src/main/java/io/dingodb/expr/runtime/op/mathematical/MinFun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.sql.Time;
2626
import java.sql.Timestamp;
2727

28-
@Operators
28+
//@Operators
2929
abstract class MinFun extends BinaryNumericOp {
3030
public static final String NAME = "MIN";
3131

0 commit comments

Comments
 (0)