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