15 package com.cloudera.impala.analysis;
23 import com.cloudera.impala.thrift.TExprNode;
24 import com.cloudera.impala.thrift.TExprNodeType;
25 import com.google.common.base.Objects;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Lists;
46 this.description_ = description;
51 public String
toString() {
return description_; }
62 Preconditions.checkNotNull(e1);
64 Preconditions.checkArgument(op == Operator.BITNOT && e2 == null
65 || op != Operator.BITNOT && e2 != null);
66 if (e2 != null) children_.add(e2);
79 db.addBuiltin(ScalarFunction.createBuiltinOperator(
80 Operator.MULTIPLY.getName(), Lists.newArrayList(t, t), t));
81 db.addBuiltin(ScalarFunction.createBuiltinOperator(
82 Operator.ADD.getName(), Lists.newArrayList(t, t), t));
83 db.addBuiltin(ScalarFunction.createBuiltinOperator(
84 Operator.SUBTRACT.getName(), Lists.newArrayList(t, t), t));
86 db.addBuiltin(ScalarFunction.createBuiltinOperator(
87 Operator.DIVIDE.getName(),
90 db.addBuiltin(ScalarFunction.createBuiltinOperator(
91 Operator.DIVIDE.getName(),
97 db.addBuiltin(ScalarFunction.createBuiltinOperator(
98 Operator.INT_DIVIDE.getName(), Lists.newArrayList(t, t), t));
99 db.addBuiltin(ScalarFunction.createBuiltinOperator(
100 Operator.BITAND.getName(), Lists.newArrayList(t, t), t));
101 db.addBuiltin(ScalarFunction.createBuiltinOperator(
102 Operator.BITOR.getName(), Lists.newArrayList(t, t), t));
103 db.addBuiltin(ScalarFunction.createBuiltinOperator(
104 Operator.BITXOR.getName(), Lists.newArrayList(t, t), t));
105 db.addBuiltin(ScalarFunction.createBuiltinOperator(
106 Operator.BITNOT.getName(), Lists.newArrayList(t), t));
112 return Objects.toStringHelper(
this)
114 .addValue(super.debugString())
120 if (children_.size() == 1) {
121 return op_.toString() + getChild(0).toSql();
123 Preconditions.checkState(children_.size() == 2);
124 return getChild(0).toSql() +
" " +
op_.
toString() +
" " + getChild(1).toSql();
130 msg.node_type = TExprNodeType.FUNCTION_CALL;
140 Type t = getChild(childIdx).getType();
142 if (targetType.isDecimal() && !t.isNull()) {
143 Preconditions.checkState(t.isScalarType());
144 targetType = ((
ScalarType) t).getMinResolutionDecimal();
152 super.analyze(analyzer);
153 for (
Expr child: children_) {
156 String errMsg =
"Arithmetic operation requires numeric operands: " +
toSql();
158 errMsg =
"Subquery must return a single row: " + operand.toSql();
164 Type t0 = getChild(0).getType();
168 if (!t0.
isNull() && !t0.isIntegerType()) {
170 "types: " +
toSql());
175 Preconditions.checkNotNull(
fn_);
177 type_ = fn_.getReturnType();
181 Preconditions.checkState(children_.size() == 2);
183 t0 = getChild(0).getType();
184 Type t1 = getChild(1).getType();
186 String fnName = op_.getName();
193 type_ = TypesUtil.getArithmeticResultType(t0, t1,
op_);
203 if ((!t0.isNull() & !t0.isIntegerType()) ||
204 (!t1.isNull() && !t1.isIntegerType())) {
208 type_ = Type.getAssignmentCompatibleType(t0, t1);
217 Preconditions.checkState(
false,
218 "Unknown arithmetic operation " + op_.toString() +
" in: " + this.
toSql());
225 t0 = getChild(0).getType();
226 t1 = getChild(1).getType();
231 Preconditions.checkState(
false, String.format(
"No match " +
232 "for '%s' with operand types %s and %s",
toSql(), t0, t1));
234 Preconditions.checkState(type_.matchesType(fn_.getReturnType()));
static ArrayList< ScalarType > getIntegerTypes()
void analyze(Analyzer analyzer)
void castForFunctionCall(boolean ignoreWildcardDecimals)
void toThrift(TExprNode msg)
void convertNumericLiteralsFromDecimal(Analyzer analyzer)
ArithmeticExpr(ArithmeticExpr other)
boolean matchesType(Type t)
Operator(String description, String name)
Type[] collectChildReturnTypes()
void castChild(int childIdx, Type targetType)
static final ScalarType DOUBLE
static void initBuiltins(Db db)
static ArrayList< ScalarType > getNumericTypes()
static final ScalarType INT
final String description_
static final ScalarType DECIMAL
Function getBuiltinFunction(Analyzer analyzer, String name, Type[] argTypes, CompareMode mode)
ArithmeticExpr(Operator op, Expr e1, Expr e2)