where col1 = 100 and abs(col2) > 0在Hive中的處理過程
where過濾條件稱為謂詞predicate。
以上where過濾條件在經過Hive的語法解析后,生成如下的語法樹:
TOK_WHERE AND = TOK_TABLE_OR_COL c1 100 > TOK_FUNCTION ABS TOK_TABLE_OR_COLc2 0
有了語法樹之后,最終的目的是生成predicate每個節點對應的ExprNodeDesc,即描述對應的節點:
public Map<ASTNode, ExprNodeDesc> genAllExprNodeDesc(ASTNode expr, RowResolver input,TypeCheckCtx tcCtx) throws SemanticException {...Map<ASTNode, ExprNodeDesc> nodeOutputs =TypeCheckProcFactory.genExprNode(expr, tcCtx);...
生成的過程是對上述語法樹的一個深度優先遍歷的過程,Hive中大量對樹的遍歷的代碼,在遍歷過程中根據指定的規則或對語法樹進行修改,或輸出相應的結果。
Hive中有一個默認的深度優先遍歷的實現DefaultGraphWalker。
這個遍歷器的實現部分代碼如下:
public void walk(Node nd) throws SemanticException { // Push the node in the stackopStack.push(nd);// While there are still nodes to dispatch...while (!opStack.empty()) {Node node = opStack.peek();if (node.getChildren() == null ||getDispatchedList().containsAll(node.getChildren())) {// Dispatch current nodeif (!getDispatchedList().contains(node)) {dispatch(node, opStack);opQueue.add(node);}opStack.pop();continue;}// Add a single child and restart the loopfor (Node childNode : node.getChildren()) {if (!getDispatchedList().contains(childNode)) {opStack.push(childNode);break;}}} // end while}
先將當前節點放到待處理的棧opStack中,然后從opStack取節點出來,如果取出來的節點沒有Children,或者Children已經全部處理完畢,才對當前節點進行處理(dispatch),如果當前節點有Children且還沒有處理完,則將當前節點的Children放到棧頂,然后重新從棧中取節點進行處理。這是很基礎的深度優先遍歷的實現。
那在遍歷的過程中,如何針對不同的節點進行不同的處理呢?
在遍歷之前,先預置一些針對不同的節點不同規則的處理器,然后在遍歷過程中,通過分發器Dispatcher選擇最合適的處理器進行處理。
生成ExprNodeDesc的遍歷中一共先預置了8個規則Rule,每個規則對應一個處理器NodeProcessor:
Map<Rule, NodeProcessor> opRules = new LinkedHashMap<Rule, NodeProcessor>();opRules.put(new RuleRegExp("R1", HiveParser.TOK_NULL + "%"),tf.getNullExprProcessor());opRules.put(new RuleRegExp("R2", HiveParser.Number + "%|" +HiveParser.TinyintLiteral + "%|" +HiveParser.SmallintLiteral + "%|" +HiveParser.BigintLiteral + "%|" +HiveParser.DecimalLiteral + "%"),tf.getNumExprProcessor());opRules.put(new RuleRegExp("R3", HiveParser.Identifier + "%|"+ HiveParser.StringLiteral + "%|" + HiveParser.TOK_CHARSETLITERAL + "%|"+ HiveParser.TOK_STRINGLITERALSEQUENCE + "%|"+ "%|" + HiveParser.KW_IF + "%|" + HiveParser.KW_CASE + "%|"+ HiveParser.KW_WHEN + "%|" + HiveParser.KW_IN + "%|"+ HiveParser.KW_ARRAY + "%|" + HiveParser.KW_MAP + "%|"+ HiveParser.KW_STRUCT + "%|" + HiveParser.KW_EXISTS + "%|"+ HiveParser.KW_GROUPING + "%|"+ HiveParser.TOK_SUBQUERY_OP_NOTIN + "%"),tf.getStrExprProcessor());opRules.put(new RuleRegExp("R4", HiveParser.KW_TRUE + "%|"+ HiveParser.KW_FALSE + "%"), tf.getBoolExprProcessor());opRules.put(new RuleRegExp("R5", HiveParser.TOK_DATELITERAL + "%|"+ HiveParser.TOK_TIMESTAMPLITERAL + "%"), tf.getDateTimeExprProcessor());opRules.put(new RuleRegExp("R6",HiveParser.TOK_INTERVAL_YEAR_MONTH_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_DAY_TIME_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_YEAR_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_MONTH_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_DAY_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_HOUR_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_MINUTE_LITERAL + "%|"+ HiveParser.TOK_INTERVAL_SECOND_LITERAL + "%"), tf.getIntervalExprProcessor());opRules.put(new RuleRegExp("R7", HiveParser.TOK_TABLE_OR_COL + "%"),tf.getColumnExprProcessor());opRules.put(new RuleRegExp("R8", HiveParser.TOK_SUBQUERY_OP + "%"),tf.getSubQueryExprProcessor());
這里使用的分發器Dispatcher是DefaultRuleDispatcher,DefaultRuleDispatcher選擇處理器的邏輯如下:
// find the firing rule// find the rule from the stack specifiedRule rule = null;int minCost = Integer.MAX_VALUE;for (Rule r : procRules.keySet()) {int cost = r.cost(ndStack);if ((cost >= 0) && (cost <= minCost)) {minCost = cost;rule = r;}}NodeProcessor proc;if (rule == null) {proc = defaultProc;} else {proc = procRules.get(rule);}// Do nothing in case proc is nullif (proc != null) {// Call the process functionreturn proc.process(nd, ndStack, procCtx, nodeOutputs);} else {return null;}
遍歷所有的規則Rule,調用每個規則的cost方法計算cost,找其中cost最小的規則對應的處理器,如果沒有找到,則使用默認處理器,如果沒有設置默認處理器,則不做任何事情。
那么每個規則的cost是如何計算的?
-- 沒太看懂==|| (后續再理理)
WHERE條件語法樹每個節點對應的處理器如下:
TOK_WHERE AND --> TypeCheckProcFactory.DefaultExprProcessor= --> TypeCheckProcFactory.DefaultExprProcessorTOK_TABLE_OR_COL --> TypeCheckProcFactory.ColumnExprProcessorc1 --> TypeCheckProcFactory.StrExprProcessor100 --> TypeCheckProcFactory.NumExprProcessor> --> TypeCheckProcFactory.DefaultExprProcessorTOK_FUNCTION --> TypeCheckProcFactory.DefaultExprProcessorABS --> TypeCheckProcFactory.StrExprProcessorTOK_TABLE_OR_COL --> TypeCheckProcFactory.ColumnExprProcessorc2 --> TypeCheckProcFactory.StrExprProcessor0 --> TypeCheckProcFactory.NumExprProcessorTypeCheckProcFactory.StrExprProcessor 生成ExprNodeConstantDesc
TypeCheckProcFactory.ColumnExprProcessor 處理column,生成ExprNodeColumnDesc
TypeCheckProcFactory.NumExprProcessor生成ExprNodeConstantDesc
TypeCheckProcFactory.DefaultExprProcessor生成ExprNodeGenericFuncDesc
在深度優先遍歷完WHERE語法樹后,每個節點都會生成一個ExprNodeDesc,但是其實除了最頂層的AND節點生成的ExprNodeDesc有用,其他的節點生成的都是中間結果,最終都會包含在AND節點生成的ExprNodeDesc中。所以在遍歷WHERE樹后,通過AND節點生成的ExprNodeDesc構造FilterDesc:
new FilterDesc(genExprNodeDesc(condn, inputRR, useCaching), false)
有了FilterDesc后,就能夠構造出FilterOperator了,然后再將生成的FilterOperator加入到Operator樹中:
Operator<T> ret = get((Class<T>) conf.getClass());
ret.setConf(conf);
至此,where過濾條件對應的FilterOperator構造完畢。
接下來仔細看下AND生成的ExprNodeDesc,它其實是一個ExprNodeGenericFuncDesc:
// genericUDF是GenericUDFOPAnd,就是對應AND操作符private GenericUDF genericUDF;// AND是一個二元操作符,children里存的是對應的操作符// 根據WHERE語法樹,可以知道children[0]肯定又是一個ExprNodeGenericFuncDesc,而且是一個=函 // 數,而children[1]也是一個肯定又是一個ExprNodeGenericFuncDesc,而且是一個>函數,以此類 // 推,每個ExprNodeGenericFuncDesc都有對應的childrenprivate List<ExprNodeDesc> chidren;// UDF的名字,這里是andprivate transient String funcText;/*** This class uses a writableObjectInspector rather than a TypeInfo to store* the canonical type information for this NodeDesc.*/private transient ObjectInspector writableObjectInspector;
每個ExprNodeDesc都對應有一個ExprNodeEvaluator,來對每個ExprNodeDesc進行實際的計算。看下ExprNodeEvaluator類的基本方法:
public abstract class ExprNodeEvaluator<T extends ExprNodeDesc> {// 對應的ExprNodeDescprotected final T expr;// 在經過這個Evaluator計算后,它的輸出值該如何解析的ObjectInspectorprotected ObjectInspector outputOI;.../*** Initialize should be called once and only once. Return the ObjectInspector* for the return value, given the rowInspector.* 初始化方法,傳入一個ObjectInspector,即傳入的數據應該如何解析的ObjectInspector* 而需要返回經過這個Evaluator計算后的輸出值的解析ObjectInspector*/public abstract ObjectInspector initialize(ObjectInspector rowInspector) throws HiveException;// evaluate方法,調用來對row數據進行解析public Object evaluate(Object row) throws HiveException {return evaluate(row, -1);}/*** Evaluate the expression given the row. This method should use the* rowInspector passed in from initialize to inspect the row object. The* return value will be inspected by the return value of initialize.* If this evaluator is referenced by others, store it for them*/protected Object evaluate(Object row, int version) throws HiveException {if (version < 0 || version != this.version) {this.version = version;return evaluation = _evaluate(row, version);}return evaluation;}// 由各個子類實現的方法的_evaluate方法,結合上面的evaluate方法,這里實際使用了設計模式的模板 // 方法模式protected abstract Object _evaluate(Object row, int version) throws HiveException;...
}
通過ExprNodeEvaluatorFactory獲取到每個ExprNodeDesc對應的ExprNodeEvaluator:
public static ExprNodeEvaluator get(ExprNodeDesc desc) throws HiveException {// Constant nodeif (desc instanceof ExprNodeConstantDesc) {return new ExprNodeConstantEvaluator((ExprNodeConstantDesc) desc);}// Column-reference node, e.g. a column in the input rowif (desc instanceof ExprNodeColumnDesc) {return new ExprNodeColumnEvaluator((ExprNodeColumnDesc) desc);}// Generic Function node, e.g. CASE, an operator or a UDF nodeif (desc instanceof ExprNodeGenericFuncDesc) {return new ExprNodeGenericFuncEvaluator((ExprNodeGenericFuncDesc) desc);}// Field node, e.g. get a.myfield1 from aif (desc instanceof ExprNodeFieldDesc) {return new ExprNodeFieldEvaluator((ExprNodeFieldDesc) desc);}throw new RuntimeException("Cannot find ExprNodeEvaluator for the exprNodeDesc = " + desc);}
看下FilterOperator中如何使用ExprNodeEvaluator對數據進行過濾的。
首先在FilterOperator的initializeOp方法中,獲取到ExprNodeEvaluator:
conditionEvaluator = ExprNodeEvaluatorFactory.get(conf.getPredicate());
然后在process方法中,調用initialize方法后,調用eveluate方法獲取到整個where過濾的結果:
conditionInspector = (PrimitiveObjectInspector) conditionEvaluator.initialize(rowInspector);
...
Object condition = conditionEvaluator.evaluate(row);
...
Boolean ret = (Boolean) conditionInspector.getPrimitiveJavaObject(condition);// 如果結果是true,則forward到下一個operator繼續處理
if (Boolean.TRUE.equals(ret)) {forward(row, rowInspector);
}
再來看下GenericUDFOPAnd的evaluate方法實現:
@Overridepublic Object evaluate(DeferredObject[] arguments) throws HiveException {boolean bool_a0 = false, bool_a1 = false;Object a0 = arguments[0].get();if (a0 != null) {bool_a0 = boi0.get(a0);if (bool_a0 == false) {result.set(false);return result;}}Object a1 = arguments[1].get();if (a1 != null) {bool_a1 = boi1.get(a1);if (bool_a1 == false) {result.set(false);return result;}}if ((a0 != null && bool_a0 == true) && (a1 != null && bool_a1 == true)) {result.set(true);return result;}return null;}
從以上代碼知道,在進行AND的計算時,如果左邊條件返回false,則不會進行右邊條件的計算,所以AND的順序其實是影響實際的效率的。類似的還有OR也是一樣的,如果左邊條件返回true,則不會進行右邊條件的計算。