-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ArithmeticExpr.java
541 lines (490 loc) · 22.5 KB
/
ArithmeticExpr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// This file is made available under Elastic License 2.0.
// This file is based on code available under the Apache license here:
// https://github.com/apache/incubator-doris/blob/master/fe/fe-core/src/main/java/org/apache/doris/analysis/ArithmeticExpr.java
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.starrocks.analysis;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.starrocks.catalog.FunctionSet;
import com.starrocks.catalog.PrimitiveType;
import com.starrocks.catalog.ScalarFunction;
import com.starrocks.catalog.ScalarType;
import com.starrocks.catalog.Type;
import com.starrocks.common.AnalysisException;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.AstVisitor;
import com.starrocks.thrift.TExprNode;
import com.starrocks.thrift.TExprNodeType;
import com.starrocks.thrift.TExprOpcode;
import java.util.Arrays;
import java.util.Objects;
public class ArithmeticExpr extends Expr {
private final Operator op;
public enum OperatorPosition {
BINARY_INFIX,
UNARY_PREFIX,
UNARY_POSTFIX,
}
public ArithmeticExpr(Operator op, Expr e1, Expr e2) {
super();
this.op = op;
Preconditions.checkNotNull(e1);
children.add(e1);
Preconditions.checkArgument(
op == Operator.BITNOT && e2 == null || op != Operator.BITNOT && e2 != null);
if (e2 != null) {
children.add(e2);
}
}
/**
* Copy c'tor used in clone().
*/
protected ArithmeticExpr(ArithmeticExpr other) {
super(other);
this.op = other.op;
}
public static void initBuiltins(FunctionSet functionSet) {
for (Type t : Type.getNumericTypes()) {
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.MULTIPLY.getName(), Lists.newArrayList(t, t), t));
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.ADD.getName(), Lists.newArrayList(t, t), t));
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.SUBTRACT.getName(), Lists.newArrayList(t, t), t));
}
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.DIVIDE.getName(),
Lists.<Type>newArrayList(Type.DOUBLE, Type.DOUBLE),
Type.DOUBLE));
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.DIVIDE.getName(),
Lists.<Type>newArrayList(Type.DECIMALV2, Type.DECIMALV2),
Type.DECIMALV2));
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.DIVIDE.getName(),
Lists.<Type>newArrayList(Type.DECIMAL32, Type.DECIMAL32),
Type.DECIMAL32));
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.DIVIDE.getName(),
Lists.<Type>newArrayList(Type.DECIMAL64, Type.DECIMAL64),
Type.DECIMAL64));
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.DIVIDE.getName(),
Lists.<Type>newArrayList(Type.DECIMAL128, Type.DECIMAL128),
Type.DECIMAL128));
// MOD(), FACTORIAL(), BITAND(), BITOR(), BITXOR(), and BITNOT() are registered as
// builtins, see starrocks_functions.py
for (Type t : Type.getIntegerTypes()) {
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.INT_DIVIDE.getName(), Lists.newArrayList(t, t), t));
}
for (Type t : Arrays.asList(Type.DECIMAL32, Type.DECIMAL64, Type.DECIMAL128)) {
functionSet.addBuiltin(ScalarFunction.createBuiltinOperator(
Operator.INT_DIVIDE.getName(), Lists.newArrayList(t, t), Type.BIGINT));
}
}
// cast int128 into decimal128(38, 0).
// cast int64(including narrower integer types) into decimal64(18, 0).
// cast float32 into decimal64(18,6).
// cast float64 into decimal128(38,9).
// other types, throw an error to indicates an explicit cast is required
private static Type nonDecimalToDecimal(Type type) {
Preconditions.checkState(!type.isDecimalV3(), "Type of rhs may not be DecimalV3");
if (type.isLargeIntType()) {
return Type.DECIMAL128_INT;
} else if (type.isBigint()) {
return Type.DECIMAL64_INT;
} else if (type.isIntegerType() || type.isBoolean()) {
return Type.DECIMAL32_INT;
} else if (type.isDecimalV2()) {
return Type.DEFAULT_DECIMAL128;
} else if (type.isNull() || type.isFloatingPointType() || type.isStringType()) {
return Type.DECIMAL_ZERO;
} else {
Preconditions.checkState(false,
"Implicit casting for decimal arithmetic operations only support integer/float/boolean/null");
return Type.INVALID;
}
}
// For decimal addition, to avoid overflow, we adopt this scaling strategy:
// as much as possible to ensure correctness
// result precision is maximum integer part width + maximum fractional part width + 1
// This can be fully guaranteed correctness in case of sufficient precision
public static void getAddReturnTypeOfDecimal(TypeTriple triple, ScalarType lhsType, ScalarType rhsType) {
final int lhsPrecision = lhsType.getPrecision();
final int rhsPrecision = rhsType.getPrecision();
final int lhsScale = lhsType.getScalarScale();
final int rhsScale = rhsType.getScalarScale();
// decimal(p1, s1) + decimal(p2, s2)
// result type = decimal(max(p1 - s1, p2 - s2) + max(s1, s2), max(s1, s2)) + 1
int maxIntLength = Math.max(lhsPrecision - lhsScale, rhsPrecision - rhsScale);
int retPrecision = maxIntLength + Math.max(lhsScale, rhsScale) + 1;
int retScale = Math.max(lhsScale, rhsScale);
// precision
retPrecision = Math.min(retPrecision, 38);
PrimitiveType decimalType = PrimitiveType.getDecimalPrimitiveType(retPrecision);
decimalType = PrimitiveType.getWiderDecimalV3Type(decimalType, lhsType.getPrimitiveType());
decimalType = PrimitiveType.getWiderDecimalV3Type(decimalType, rhsType.getPrimitiveType());
triple.lhsTargetType = ScalarType.createDecimalV3Type(decimalType, retPrecision, lhsScale);
triple.rhsTargetType = ScalarType.createDecimalV3Type(decimalType, retPrecision, rhsScale);
triple.returnType = ScalarType.createDecimalV3Type(decimalType, retPrecision, retScale);
}
public static TypeTriple getReturnTypeOfDecimal(Operator op, ScalarType lhsType, ScalarType rhsType)
throws SemanticException {
Preconditions.checkState(lhsType.isDecimalV3() && rhsType.isDecimalV3(),
"Types of lhs and rhs must be DecimalV3");
final PrimitiveType lhsPtype = lhsType.getPrimitiveType();
final PrimitiveType rhsPtype = rhsType.getPrimitiveType();
final int lhsPrecision = lhsType.getPrecision();
final int rhsPrecision = rhsType.getPrecision();
final int lhsScale = lhsType.getScalarScale();
final int rhsScale = rhsType.getScalarScale();
// get the wider decimal type
PrimitiveType widerType = PrimitiveType.getWiderDecimalV3Type(lhsPtype, rhsPtype);
// compute arithmetic expr use decimal64 for both decimal32 and decimal64
widerType = PrimitiveType.getWiderDecimalV3Type(widerType, PrimitiveType.DECIMAL64);
int maxPrecision = PrimitiveType.getMaxPrecisionOfDecimal(widerType);
TypeTriple result = new TypeTriple();
result.lhsTargetType = ScalarType.createDecimalV3Type(widerType, maxPrecision, lhsScale);
result.rhsTargetType = ScalarType.createDecimalV3Type(widerType, maxPrecision, rhsScale);
int returnScale = 0;
int returnPrecision = 0;
switch (op) {
case ADD:
getAddReturnTypeOfDecimal(result, lhsType, rhsType);
return result;
case SUBTRACT:
case MOD:
returnScale = Math.max(lhsScale, rhsScale);
break;
case MULTIPLY:
returnScale = lhsScale + rhsScale;
returnPrecision = lhsPrecision + rhsPrecision;
final int maxDecimalPrecision = PrimitiveType.getMaxPrecisionOfDecimal(PrimitiveType.DECIMAL128);
if (returnPrecision <= maxDecimalPrecision) {
// returnPrecision <= 38, result never overflows, use the narrowest decimal type that can holds the result.
// for examples:
// decimal32(4,3) * decimal32(4,3) => decimal32(8,6);
// decimal64(15,3) * decimal32(9,4) => decimal128(24,7).
PrimitiveType commonPtype =
ScalarType.createDecimalV3NarrowestType(returnPrecision, returnScale).getPrimitiveType();
// a common type shall never be narrower than type of lhs and rhs
commonPtype = PrimitiveType.getWiderDecimalV3Type(commonPtype, lhsPtype);
commonPtype = PrimitiveType.getWiderDecimalV3Type(commonPtype, rhsPtype);
result.returnType = ScalarType.createDecimalV3Type(commonPtype, returnPrecision, returnScale);
result.lhsTargetType = ScalarType.createDecimalV3Type(commonPtype, lhsPrecision, lhsScale);
result.rhsTargetType = ScalarType.createDecimalV3Type(commonPtype, rhsPrecision, rhsScale);
return result;
} else if (returnScale <= maxDecimalPrecision) {
// returnPrecision > 38 and returnScale <= 38, the multiplication is computable but the result maybe
// overflow, so use decimal128 arithmetic and adopt maximum decimal precision(38) as precision of
// the result.
// for examples:
// decimal128(23,5) * decimal64(18,4) => decimal128(38, 9).
result.returnType =
ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, maxDecimalPrecision, returnScale);
result.lhsTargetType =
ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, lhsPrecision, lhsScale);
result.rhsTargetType =
ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, rhsPrecision, rhsScale);
return result;
} else {
// returnScale > 38, so it is cannot be represented as decimal.
throw new SemanticException(
String.format(
"Return scale(%d) exceeds maximum value(%d), please cast decimal type to low-precision one",
returnScale, maxDecimalPrecision));
}
case INT_DIVIDE:
case DIVIDE:
if (lhsScale <= 6) {
returnScale = lhsScale + 6;
} else if (lhsScale <= 12) {
returnScale = 12;
} else {
returnScale = lhsScale;
}
widerType = PrimitiveType.DECIMAL128;
maxPrecision = PrimitiveType.getMaxPrecisionOfDecimal(widerType);
result.lhsTargetType = ScalarType.createDecimalV3Type(widerType, maxPrecision, lhsScale);
result.rhsTargetType = ScalarType.createDecimalV3Type(widerType, maxPrecision, rhsScale);
int adjustedScale = returnScale + rhsScale;
if (adjustedScale > maxPrecision) {
throw new SemanticException(
String.format(
"Dividend fails to adjust scale to %d that exceeds maximum value(%d)",
adjustedScale,
maxPrecision));
}
break;
case BITAND:
case BITOR:
case BITXOR:
result.lhsTargetType = ScalarType.BIGINT;
result.rhsTargetType = ScalarType.BIGINT;
result.returnType = ScalarType.BIGINT;
return result;
default:
Preconditions.checkState(false, "DecimalV3 only support operators: +-*/%&|^");
}
result.returnType = op == Operator.INT_DIVIDE ? ScalarType.BIGINT :
ScalarType.createDecimalV3Type(widerType, maxPrecision, returnScale);
return result;
}
private void rewriteDecimalDecimalOperation() throws AnalysisException {
final Type lhsOriginType = getChild(0).type;
final Type rhsOriginType = getChild(1).type;
// if both of left child and right child are implict cast.
// It means ArithmeticExpr has been applied rewriteDecimalDecimalOperation.
// so we don't have to rewrite again.
// TODO:
if (getChild(0).isImplicitCast() && getChild(1).isImplicitCast()) {
return;
}
Type lhsTargetType = lhsOriginType;
Type rhsTargetType = rhsOriginType;
if (!lhsTargetType.isDecimalV3()) {
lhsTargetType = nonDecimalToDecimal(lhsTargetType);
}
if (!rhsTargetType.isDecimalV3()) {
rhsTargetType = nonDecimalToDecimal(rhsTargetType);
}
TypeTriple triple = getReturnTypeOfDecimal(op, (ScalarType) lhsTargetType, (ScalarType) rhsTargetType);
if (!triple.lhsTargetType.equals(lhsOriginType)) {
Preconditions.checkState(triple.lhsTargetType.isValid());
castChild(triple.lhsTargetType, 0);
}
if (!triple.rhsTargetType.equals(rhsOriginType)) {
Preconditions.checkState(triple.rhsTargetType.isValid());
castChild(triple.rhsTargetType, 1);
}
type = triple.returnType;
}
private void rewriteDecimalFloatingPointOperation() throws AnalysisException {
Type lhsType = getChild(0).type;
Type rhsType = getChild(1).type;
Type resultType = Type.DOUBLE;
if (!resultType.equals(lhsType)) {
castChild(resultType, 0);
}
if (!resultType.equals(rhsType)) {
castChild(resultType, 1);
}
this.type = resultType;
}
private boolean hasFloatingPointOrStringType() {
Type lhsType = getChild(0).type;
Type rhsType = getChild(1).type;
return lhsType.isFloatingPointType() || lhsType.isStringType() || rhsType.isFloatingPointType() ||
rhsType.isStringType();
}
private boolean resultTypeIsBigInt() {
switch (op) {
case BITAND:
case BITOR:
case BITXOR:
case BITNOT:
case INT_DIVIDE:
return true;
default:
return false;
}
}
public void rewriteDecimalOperation() throws AnalysisException {
if (hasFloatingPointOrStringType() && !resultTypeIsBigInt()) {
rewriteDecimalFloatingPointOperation();
} else {
rewriteDecimalDecimalOperation();
}
}
@Override
public String toString() {
return toSql();
}
@Override
public Expr clone() {
return new ArithmeticExpr(this);
}
@Override
public String toSqlImpl() {
if (children.size() == 1) {
return op.toString() + " " + getChild(0).toSql();
} else {
return getChild(0).toSql() + " " + op.toString() + " " + getChild(1).toSql();
}
}
@Override
public String toDigestImpl() {
if (children.size() == 1) {
return op.toString() + " " + getChild(0).toDigest();
} else {
return getChild(0).toDigest() + " " + op.toString() + " " + getChild(1).toDigest();
}
}
@Override
protected String explainImpl() {
if (children.size() == 1) {
return op.toString() + " " + getChild(0).explain();
} else {
return getChild(0).explain() + " " + op.toString() + " " + getChild(1).explain();
}
}
@Override
protected void toThrift(TExprNode msg) {
msg.node_type = TExprNodeType.ARITHMETIC_EXPR;
msg.setOpcode(op.getOpcode());
msg.setOutput_column(outputColumn);
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) {
return false;
}
return ((ArithmeticExpr) obj).opcode == opcode;
}
public static Type getCommonType(Type t1, Type t2) {
PrimitiveType pt1 = t1.getNumResultType().getPrimitiveType();
PrimitiveType pt2 = t2.getNumResultType().getPrimitiveType();
if (pt1 == PrimitiveType.DOUBLE || pt2 == PrimitiveType.DOUBLE) {
return Type.DOUBLE;
} else if (pt1.isDecimalV3Type() || pt2.isDecimalV3Type()) {
return ScalarType.getAssigmentCompatibleTypeOfDecimalV3((ScalarType) t1, (ScalarType) t2);
} else if (pt1 == PrimitiveType.DECIMALV2 || pt2 == PrimitiveType.DECIMALV2) {
return Type.DECIMALV2;
} else if (pt1 == PrimitiveType.LARGEINT || pt2 == PrimitiveType.LARGEINT) {
return Type.LARGEINT;
} else if (pt1 == PrimitiveType.BIGINT || pt2 == PrimitiveType.BIGINT) {
return Type.BIGINT;
} else if ((PrimitiveType.TINYINT.ordinal() <= pt1.ordinal() &&
pt1.ordinal() <= PrimitiveType.INT.ordinal()) &&
(PrimitiveType.TINYINT.ordinal() <= pt2.ordinal() &&
pt2.ordinal() <= PrimitiveType.INT.ordinal())) {
return (pt1.ordinal() > pt2.ordinal()) ? t1 : t2;
} else if (PrimitiveType.TINYINT.ordinal() <= pt1.ordinal() &&
pt1.ordinal() <= PrimitiveType.INT.ordinal()) {
// when t2 is INVALID TYPE:
return t1;
} else if (PrimitiveType.TINYINT.ordinal() <= pt2.ordinal() &&
pt2.ordinal() <= PrimitiveType.INT.ordinal()) {
// when t1 is INVALID TYPE:
return t2;
} else {
return Type.INVALID;
}
}
public static Type getBiggerType(Type t) {
switch (t.getNumResultType().getPrimitiveType()) {
case TINYINT:
return Type.SMALLINT;
case SMALLINT:
return Type.INT;
case INT:
case BIGINT:
return Type.BIGINT;
case LARGEINT:
return Type.LARGEINT;
case DOUBLE:
return Type.DOUBLE;
case DECIMALV2:
return Type.DECIMALV2;
case DECIMAL32:
case DECIMAL64:
case DECIMAL128:
return t;
default:
return Type.INVALID;
}
}
@Override
public void analyzeImpl(Analyzer analyzer) throws AnalysisException {
}
@Override
public int hashCode() {
return 31 * super.hashCode() + Objects.hashCode(op);
}
@Override
public boolean isNullable() {
if (op == Operator.DIVIDE || op == Operator.INT_DIVIDE || op == Operator.MOD) {
return true;
}
return children.stream().anyMatch(e -> e.isNullable() || e.getType().isDecimalV3());
}
/**
* Below function is added by new analyzer
*/
@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) throws SemanticException {
return visitor.visitArithmeticExpr(this, context);
}
public Operator getOp() {
return op;
}
public enum Operator {
MULTIPLY("*", "multiply", OperatorPosition.BINARY_INFIX, TExprOpcode.MULTIPLY, true),
DIVIDE("/", "divide", OperatorPosition.BINARY_INFIX, TExprOpcode.DIVIDE, true),
MOD("%", "mod", OperatorPosition.BINARY_INFIX, TExprOpcode.MOD, false),
INT_DIVIDE("DIV", "int_divide", OperatorPosition.BINARY_INFIX, TExprOpcode.INT_DIVIDE, true),
ADD("+", "add", OperatorPosition.BINARY_INFIX, TExprOpcode.ADD, true),
SUBTRACT("-", "subtract", OperatorPosition.BINARY_INFIX, TExprOpcode.SUBTRACT, true),
BITAND("&", "bitand", OperatorPosition.BINARY_INFIX, TExprOpcode.BITAND, false),
BITOR("|", "bitor", OperatorPosition.BINARY_INFIX, TExprOpcode.BITOR, false),
BITXOR("^", "bitxor", OperatorPosition.BINARY_INFIX, TExprOpcode.BITXOR, false),
BITNOT("~", "bitnot", OperatorPosition.UNARY_PREFIX, TExprOpcode.BITNOT, false),
FACTORIAL("!", "factorial", OperatorPosition.UNARY_POSTFIX, TExprOpcode.FACTORIAL, true);
private final String description;
private final String name;
private final OperatorPosition pos;
private final TExprOpcode opcode;
private final boolean monotonic;
Operator(String description, String name, OperatorPosition pos, TExprOpcode opcode, boolean monotonic) {
this.description = description;
this.name = name;
this.pos = pos;
this.opcode = opcode;
this.monotonic = monotonic;
}
@Override
public String toString() {
return description;
}
public String getName() {
return name;
}
public OperatorPosition getPos() {
return pos;
}
public TExprOpcode getOpcode() {
return opcode;
}
public boolean isBinary() {
return pos == OperatorPosition.BINARY_INFIX;
}
public boolean isMonotonic() {
return monotonic;
}
}
public static class TypeTriple {
public ScalarType returnType;
public ScalarType lhsTargetType;
public ScalarType rhsTargetType;
}
@Override
public boolean isSelfMonotonic() {
return op.isMonotonic();
}
}