15 package com.cloudera.impala.analysis;
17 import java.util.HashMap;
23 import com.cloudera.impala.thrift.TExprNode;
24 import com.cloudera.impala.thrift.TExprNodeType;
25 import com.google.common.base.Preconditions;
50 this.description_ = description;
59 private static Map<String, TimeUnit>
TIME_UNITS_MAP =
new HashMap<String, TimeUnit>();
62 TIME_UNITS_MAP.put(timeUnit.toString(), timeUnit);
63 TIME_UNITS_MAP.put(timeUnit.toString() +
"S", timeUnit);
69 private ArithmeticExpr.Operator
op_;
81 String timeUnitIdent) {
82 this.funcName_ = funcName.toLowerCase();
83 this.timeUnitIdent_ = timeUnitIdent;
84 this.intervalFirst_ =
false;
93 String timeUnitIdent,
boolean intervalFirst) {
95 this.funcName_ = null;
97 this.timeUnitIdent_ = timeUnitIdent;
98 this.intervalFirst_ = intervalFirst;
118 super.analyze(analyzer);
124 }
else if (
funcName_.equals(
"date_sub")) {
128 "' in timestamp arithmetic expression '" +
toSql() +
"'. " +
129 "Expected function name 'DATE_ADD' or 'DATE_SUB'.");
133 timeUnit_ = TIME_UNITS_MAP.get(timeUnitIdent_.toUpperCase());
136 "' in timestamp arithmetic expression '" +
toSql() +
"'.");
140 if (!getChild(0).
getType().isTimestamp() && !getChild(0).
getType().isNull()) {
142 "' of timestamp arithmetic expression '" +
toSql() +
"' returns type '" +
143 getChild(0).
getType().
toSql() +
"'. Expected type 'TIMESTAMP'.");
147 if (!getChild(1).
getType().isIntegerType() &&
148 !getChild(1).
getType().isNull()) {
150 "' of timestamp arithmetic expression '" +
toSql() +
"' returns type '" +
151 getChild(1).
getType().
toSql() +
"'. Expected an integer type.");
154 String funcOpName = String.format(
"%sS_%s", timeUnit_.toString(),
161 Preconditions.checkNotNull(
fn_);
162 Preconditions.checkState(fn_.getReturnType().isTimestamp());
163 type_ = fn_.getReturnType();
168 msg.node_type = TExprNodeType.FUNCTION_CALL;
173 public ArithmeticExpr.Operator
getOp() {
return op_; }
177 StringBuilder strBuilder =
new StringBuilder();
180 strBuilder.append(funcName_.toUpperCase() +
"(");
181 strBuilder.append(getChild(0).toSql() +
", ");
182 strBuilder.append(
"INTERVAL ");
183 strBuilder.append(getChild(1).toSql());
185 strBuilder.append(
")");
186 return strBuilder.toString();
190 strBuilder.append(
"INTERVAL ");
191 strBuilder.append(getChild(1).toSql() +
" ");
193 strBuilder.append(
" " + op_.toString() +
" ");
194 strBuilder.append(getChild(0).toSql());
197 strBuilder.append(getChild(0).toSql());
198 strBuilder.append(
" " + op_.toString() +
" ");
199 strBuilder.append(
"INTERVAL ");
200 strBuilder.append(getChild(1).toSql() +
" ");
203 return strBuilder.toString();
TimestampArithmeticExpr(String funcName, Expr e1, Expr e2, String timeUnitIdent)
final boolean intervalFirst_
TimeUnit(String description)
void castForFunctionCall(boolean ignoreWildcardDecimals)
void analyze(Analyzer analyzer)
TimestampArithmeticExpr(TimestampArithmeticExpr other)
String getTimeUnitIdent()
TimestampArithmeticExpr(ArithmeticExpr.Operator op, Expr e1, Expr e2, String timeUnitIdent, boolean intervalFirst)
final String description_
Type[] collectChildReturnTypes()
ArithmeticExpr.Operator getOp()
final String timeUnitIdent_
ArithmeticExpr.Operator op_
Function getBuiltinFunction(Analyzer analyzer, String name, Type[] argTypes, CompareMode mode)
static Map< String, TimeUnit > TIME_UNITS_MAP
void toThrift(TExprNode msg)