15 package com.cloudera.impala.analysis;
17 import java.util.ArrayList;
18 import java.util.List;
26 import com.cloudera.impala.common.Reference;
27 import com.cloudera.impala.thrift.TExprNode;
28 import com.cloudera.impala.thrift.TExprNodeType;
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.Lists;
48 if (t.isNull())
continue;
55 String typeString = t.getPrimitiveType().
toString().toLowerCase();
58 db.addBuiltin(ScalarFunction.createBuiltin(
IN_ITERATE,
60 "impala::InPredicate::InIterate", null, null,
false));
63 "impala::InPredicate::NotInIterate", null, null,
false));
65 String prepareFn =
"impala::InPredicate::SetLookupPrepare_" + typeString;
66 String closeFn =
"impala::InPredicate::SetLookupClose_" + typeString;
70 "impala::InPredicate::InSetLookup", prepareFn, closeFn,
false));
73 "impala::InPredicate::NotInSetLookup", prepareFn, closeFn,
false));
81 children_.add(compareExpr);
82 children_.addAll(inList);
88 Preconditions.checkNotNull(compareExpr);
89 Preconditions.checkNotNull(subquery);
90 children_.add(compareExpr);
91 children_.add(subquery);
106 super.analyze(analyzer);
111 if (children_.size() != 2 || !(getChild(1) instanceof
Subquery)) {
125 ArrayList<Expr> subqueryExprs = subquery.getStatement().getResultExprs();
126 Expr compareExpr = children_.get(0);
127 Expr subqueryExpr = subqueryExprs.get(0);
128 analyzer.getCompatibleType(compareExpr.getType(), compareExpr, subqueryExpr);
130 Preconditions.checkState(getChildren().size() >= 2);
131 analyzer.castAllToCompatibleType(children_);
134 if (childType.isNull()) {
136 for (
int i = 0; i < children_.size(); ++i) {
144 boolean allConstant =
true;
145 for (
int i = 1; i < children_.size(); ++i) {
146 if (!children_.get(i).isConstant()) {
151 boolean useSetLookup = allConstant;
153 int setLookupThreshold = children_.get(0).
getType().isStringType() ? 6 : 2;
154 if (children_.size() - 1 < setLookupThreshold) useSetLookup =
false;
159 Type[] argTypes = {getChild(0).type_, getChild(1).type_};
167 Preconditions.checkNotNull(
fn_);
168 Preconditions.checkState(fn_.getReturnType().isBoolean());
173 Reference<SlotRef> slotRefRef =
new Reference<SlotRef>();
174 Reference<Integer> idxRef =
new Reference<Integer>();
176 && idxRef.getRef() == 0
177 && slotRefRef.getRef().getNumDistinctValues() > 0) {
179 / (double) slotRefRef.getRef().getNumDistinctValues();
189 Preconditions.checkState(!contains(
Subquery.class));
190 msg.node_type = TExprNodeType.FUNCTION_CALL;
195 StringBuilder strBuilder =
new StringBuilder();
196 String notStr = (
isNotIn_) ?
"NOT " :
"";
197 strBuilder.append(getChild(0).toSql() +
" " + notStr +
"IN ");
198 boolean hasSubquery = contains(
Subquery.class);
199 if (!hasSubquery) strBuilder.append(
"(");
200 for (
int i = 1; i < children_.size(); ++i) {
201 strBuilder.append(getChild(i).toSql());
202 strBuilder.append((i+1 != children_.size()) ?
", " :
"");
204 if (!hasSubquery) strBuilder.append(
")");
205 return strBuilder.toString();
214 return getChild(0).unwrapSlotRef(
true);
222 return new InPredicate(getChild(0), children_.subList(1, children_.size()),
InPredicate(Expr compareExpr, List< Expr > inList, boolean isNotIn)
void uncheckedCastChild(Type targetType, int childIndex)
boolean returnsScalarColumn()
static final String IN_ITERATE
void castForFunctionCall(boolean ignoreWildcardDecimals)
void toThrift(TExprNode msg)
void analyze(Analyzer analyzer)
static final ScalarType BOOLEAN
InPredicate(Expr compareExpr, Expr subquery, boolean isNotIn)
static final String IN_SET_LOOKUP
static final String NOT_IN_ITERATE
Function getBuiltinFunction(Analyzer analyzer, String name, Type[] argTypes, CompareMode mode)
static final String NOT_IN_SET_LOOKUP
boolean isSingleColumnPredicate(Reference< SlotRef > slotRefRef, Reference< Integer > idxRef)
static void initBuiltins(Db db)
InPredicate(InPredicate other)
static ArrayList< ScalarType > getSupportedTypes()
static double DEFAULT_SELECTIVITY