15 package com.cloudera.impala.analysis;
17 import java.util.List;
26 import com.cloudera.impala.thrift.TCaseExpr;
27 import com.cloudera.impala.thrift.TExprNode;
28 import com.cloudera.impala.thrift.TExprNodeType;
29 import com.google.common.annotations.VisibleForTesting;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.Lists;
75 if (caseExpr != null) {
76 children_.add(caseExpr);
80 Preconditions.checkNotNull(whenClause.getWhenExpr());
81 children_.add(whenClause.getWhenExpr());
82 Preconditions.checkNotNull(whenClause.getThenExpr());
83 children_.add(whenClause.getThenExpr());
85 if (elseExpr != null) {
86 children_.add(elseExpr);
114 Expr encodedIsNull = null;
115 if (!decodeExpr.getChildren().isEmpty()) {
116 encoded = decodeExpr.getChild(childIdx++);
121 while (childIdx + 2 <= decodeExpr.getChildren().size()) {
122 Expr candidate = decodeExpr.getChild(childIdx++);
127 children_.add(encodedIsNull);
140 children_.add(decodeExpr.getChild(childIdx++));
144 if (childIdx < decodeExpr.getChildren().size()) {
146 children_.add(decodeExpr.getChild(childIdx));
162 if (t.isNull())
continue;
169 db.addBuiltin(ScalarFunction.createBuiltinOperator(
170 "case",
"", Lists.newArrayList(t), t));
172 db.addBuiltin(ScalarFunction.createBuiltinOperator(
173 "decode",
"", Lists.newArrayList(t), t));
179 if (!super.equals(obj))
return false;
193 StringBuilder output =
new StringBuilder(
"CASE");
196 output.append(
" " + children_.get(childIdx++).
toSql());
198 while (childIdx + 2 <= children_.size()) {
199 output.append(
" WHEN " + children_.get(childIdx++).
toSql());
200 output.append(
" THEN " + children_.get(childIdx++).
toSql());
203 output.append(
" ELSE " + children_.get(children_.size() - 1).toSql());
205 output.append(
" END");
206 return output.toString();
211 msg.node_type = TExprNodeType.CASE_EXPR;
218 super.analyze(analyzer);
225 FunctionCallExpr.validateScalarFnParams(decodeExpr_.getParams());
233 Type whenType = null;
235 Type returnType = null;
237 Expr lastCompatibleThenExpr = null;
238 Expr lastCompatibleWhenExpr = null;
239 int loopEnd = children_.size();
244 Expr caseExpr = null;
248 caseExpr = children_.get(0);
249 caseExpr.analyze(analyzer);
250 whenType = caseExpr.getType();
251 lastCompatibleWhenExpr = children_.get(0);
258 for (
int i = loopStart; i < loopEnd; i += 2) {
259 Expr whenExpr = children_.get(i);
263 whenType = analyzer.getCompatibleType(whenType,
264 lastCompatibleWhenExpr, whenExpr);
265 lastCompatibleWhenExpr = whenExpr;
271 Preconditions.checkState(
isCase());
273 " is not of type boolean and not castable to type boolean.");
280 Expr thenExpr = children_.get(i + 1);
281 returnType = analyzer.getCompatibleType(returnType,
282 lastCompatibleThenExpr, thenExpr);
283 lastCompatibleThenExpr = thenExpr;
286 Expr elseExpr = children_.get(children_.size() - 1);
287 returnType = analyzer.getCompatibleType(returnType,
288 lastCompatibleThenExpr, elseExpr);
298 if (!children_.get(0).type_.equals(whenType)) {
302 for (
int i = loopStart; i < loopEnd; i += 2) {
303 if (!children_.get(i).type_.equals(whenType)) {
309 for (
int i = loopStart + 1; i < children_.size(); i += 2) {
310 if (!children_.get(i).type_.equals(returnType)) {
316 if (!children_.get(children_.size() - 1).
type_.equals(returnType)) {
317 castChild(returnType, children_.size() - 1);
325 Preconditions.checkNotNull(
fn_);
FunctionCallExpr decodeExpr_
CaseExpr(Expr caseExpr, List< CaseWhenClause > whenClauses, Expr elseExpr)
static final ScalarType BOOLEAN
CaseExpr(FunctionCallExpr decodeExpr)
static boolean isImplicitlyCastable(Type t1, Type t2)
void analyze(Analyzer analyzer)
Function getBuiltinFunction(Analyzer analyzer, String name, Type[] argTypes, CompareMode mode)
static void initBuiltins(Db db)
void castChild(Type targetType, int childIndex)
boolean equals(Object obj)
void toThrift(TExprNode msg)
static ArrayList< ScalarType > getSupportedTypes()
void castChildCharsToStrings(Analyzer analyzer)