Skip to content

Commit 4f2efcd

Browse files
committed
[fix][runtime] Added divOp1Factory to standardize the scale of avg return types
1 parent f499ddd commit 4f2efcd

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

runtime/src/main/java/io/dingodb/expr/runtime/expr/Exprs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import io.dingodb.expr.runtime.op.aggregation.Sum0Agg;
5151
import io.dingodb.expr.runtime.op.aggregation.SumAgg;
5252
import io.dingodb.expr.runtime.op.arithmetic.AddOpFactory;
53+
import io.dingodb.expr.runtime.op.arithmetic.DivOp1Factory;
5354
import io.dingodb.expr.runtime.op.arithmetic.DivOpFactory;
5455
import io.dingodb.expr.runtime.op.arithmetic.MulOpFactory;
5556
import io.dingodb.expr.runtime.op.arithmetic.NegOpFactory;
@@ -197,6 +198,7 @@ public final class Exprs {
197198
public static final SubOpFactory SUB = SubOpFactory.INSTANCE;
198199
public static final MulOpFactory MUL = MulOpFactory.INSTANCE;
199200
public static final DivOpFactory DIV = DivOpFactory.INSTANCE;
201+
public static final DivOp1Factory DIV1 = DivOp1Factory.INSTANCE;
200202

201203
// Interval
202204
public static final io.dingodb.expr.runtime.op.interval.SubOpFactory INTERVAL_SUB =
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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.arithmetic;
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.math.RoundingMode;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
33+
public class DivOp1Factory extends DivOp {
34+
35+
public static final DivOp1Factory INSTANCE = new DivOp1Factory();
36+
@Serial
37+
private static final long serialVersionUID = -8087135512942367227L;
38+
39+
private final Map<Object, DivOp> opMap = new HashMap<>();
40+
41+
private DivOp1Factory() {
42+
super();
43+
opMap.put(keyOf(Types.FLOAT, Types.FLOAT), new DivOp1Factory.DivFloatFloat());
44+
opMap.put(keyOf(Types.DECIMAL, Types.DECIMAL), new DivOp1Factory.DivDecimalDecimal());
45+
opMap.put(keyOf(Types.INT, Types.INT), new DivOp1Factory.DivIntInt());
46+
opMap.put(keyOf(Types.LONG, Types.LONG), new DivOp1Factory.DivLongLong());
47+
opMap.put(keyOf(Types.DOUBLE, Types.DOUBLE), new DivOp1Factory.DivDoubleDouble());
48+
}
49+
50+
@Override
51+
public BinaryOp getOp(OpKey key) {
52+
return opMap.get(key);
53+
}
54+
55+
public static final class DivFloatFloat extends DivOp {
56+
57+
@Serial
58+
private static final long serialVersionUID = 3149920684645912128L;
59+
60+
@Override
61+
protected Float evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
62+
ExprConfig config) {
63+
return div((Float) value0, (Float) value1);
64+
}
65+
66+
@Override
67+
public Type getType() {
68+
return Types.FLOAT;
69+
}
70+
71+
@Override
72+
public OpKey getKey() {
73+
return keyOf(Types.FLOAT, Types.FLOAT);
74+
}
75+
}
76+
77+
public static final class DivDecimalDecimal extends DivOp {
78+
79+
@Serial
80+
private static final long serialVersionUID = 4481902942997870570L;
81+
82+
@Override
83+
protected BigDecimal evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
84+
ExprConfig config) {
85+
if (value0 instanceof BigDecimal && value1 instanceof BigDecimal) {
86+
BigDecimal value0Decimal = (BigDecimal) value0;
87+
BigDecimal value1Decimal = (BigDecimal) value1;
88+
int leftScale = value0Decimal.scale();
89+
90+
if (value1Decimal.compareTo(BigDecimal.ZERO) != 0) {
91+
return value0Decimal.divide(value1Decimal, leftScale, RoundingMode.HALF_UP);
92+
} else {
93+
return null;
94+
}
95+
} else {
96+
return null;
97+
}
98+
}
99+
100+
@Override
101+
public Type getType() {
102+
return Types.DECIMAL;
103+
}
104+
105+
@Override
106+
public OpKey getKey() {
107+
return keyOf(Types.DECIMAL, Types.DECIMAL);
108+
}
109+
}
110+
111+
public static final class DivIntInt extends DivOp {
112+
113+
@Serial
114+
private static final long serialVersionUID = -1044298367948649528L;
115+
116+
@Override
117+
protected Double evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
118+
ExprConfig config) {
119+
return div((Integer) value0, (Integer) value1);
120+
}
121+
122+
@Override
123+
public Type getType() {
124+
return Types.DOUBLE;
125+
}
126+
127+
@Override
128+
public OpKey getKey() {
129+
return keyOf(Types.INT, Types.INT);
130+
}
131+
}
132+
133+
public static final class DivLongLong extends DivOp {
134+
135+
136+
@Serial
137+
private static final long serialVersionUID = 6592631565770909715L;
138+
139+
@Override
140+
protected Double evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
141+
ExprConfig config) {
142+
return div((Long) value0, (Long) value1);
143+
}
144+
145+
@Override
146+
public Type getType() {
147+
return Types.DOUBLE;
148+
}
149+
150+
@Override
151+
public OpKey getKey() {
152+
return keyOf(Types.LONG, Types.LONG);
153+
}
154+
}
155+
156+
public static final class DivDoubleDouble extends DivOp {
157+
158+
159+
@Serial
160+
private static final long serialVersionUID = -2939341971102323838L;
161+
162+
@Override
163+
protected Double evalNonNullValue(@NonNull Object value0, @NonNull Object value1,
164+
ExprConfig config) {
165+
return div((Double) value0, (Double) value1);
166+
}
167+
168+
@Override
169+
public Type getType() {
170+
return Types.DOUBLE;
171+
}
172+
173+
@Override
174+
public OpKey getKey() {
175+
return keyOf(Types.DOUBLE, Types.DOUBLE);
176+
}
177+
}
178+
179+
}

0 commit comments

Comments
 (0)