Hive谓词解析过程分析

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,则不会进行右边条件的计算。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/456187.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算术编码(Arithmetic Coding)源代码

Ian H. Witten、Radford M. Neal和John G. Cleary在1987年发表了一篇启发性的论文。论文中描述了一种基于整数运算的通用算术编码器&#xff0c;而且还给出了由计算错误导致的效率低下的分析。以下源代码来自于这篇论文&#xff1a;《基于算术编码的数据压缩》&#xff08;Arit…

pandas读写各种类型数据

read_X()通常是pandas模块下的&#xff0c;to_X()是dataframe的方法 CSV 读取 使用pandas.read_csv()方法&#xff0c;返回的是一个dataframe csv默认是以"&#xff0c;"分割的 csv文件内容 1、read_csv()默认以第一行数据作为标题 2、调用dataframe的head()方法…

python 类装饰器

1 装饰器无参数 class tracer: def __init__(self,func): self.calls 0 self.func func def __call__(self,*args): self.calls 1 print(call %s to %s %(self.calls, self.func.__name__)) self.func(*args) tracer def spam(a, b, c): print(a b c) …

【数据分析】使用pandas和numpy分析美国大选献金项目

1. 数据载入与总览 1.1 数据加载 #绘图工具 import matplotlib.pyplot as plt %matplotlib inline #数据处理工具 import numpy as np import pandas as pd from pandas import Series,DataFrame#数据路径自己指定&#xff0c;本案例数据路径就在当前文件夹下面子文件夹usa_e…

《容器技术系列》一1.4 Docker运行案例分析

本节书摘来华章计算机《容器技术系列》一书中的第1章 &#xff0c;第1.4节&#xff0c;孙宏亮 著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。 1.4 Docker运行案例分析 1.3节着重介绍了Docker架构中各个模块的功能&#xff0c;学完后我们可以对Docker的架构有一…

算术编码的原理与分析

转自&#xff1a;http://kulasuki115.blogcn.com/diary,201492702.shtml 前言 人类已进入信息时代&#xff0c;信息时代的重要特征是信息的数字化&#xff0c;人们越来越依靠计算机获取和利用信息&#xff0c;这就需要对信息的表示、存储、传输和处理等关键技术进行研究。我们…

3月22日AM

看了思维章节精讲视频课&#xff0c;并且总结了部分思维章节内容转载于:https://www.cnblogs.com/bgd140206102/p/6601440.html

阿里巴巴Dubbo实现的源码分析

Dubbo概述Dubbo是阿里巴巴开源出来的一个分布式服务框架&#xff0c;致力于提供高性能和透明化的RPC远程服务调用方案&#xff0c;以及作为SOA服务治理的方案。它的核心功能包括&#xff1a; remoting:远程通讯基础&#xff0c;提供对多种NIO框架抽象封装&#xff0c;包括“同步…

POJ 2106-Boolean Expressions,双栈运用类似表达式求值!

Boolean Expressions 首先声明此题后台可能极水&#xff08;毕竟这种数据不好造&#xff01;&#xff09;。昨天写了一天却总是找不到bug&#xff0c;讨论区各种数据都过了&#xff0c;甚至怀疑输入有问题&#xff0c;但看到gets也可以过&#xff0c;难道是思路错了&#xff1f…

H264 CAVLC 研究

目录 1 CAVLC概念 2 CAVLC原理 3 CAVLC编码流程 4 CAVLC解码流程 展开全部 1 CAVLC概念 2 CAVLC原理 3 CAVLC编码流程 4 CAVLC解码流程 收起 摘要纠错编辑摘要 CAVLC即基于上下文的自适应变长编码。H.264标准中使用CAVLC对4*4模块的亮度和色度残差数据进行编码。 CAVLC-CAVLC…

【MySQL 】学习笔记千行总结

/* Windows服务 */ -- 启动MySQLnet start mysql -- 创建Windows服务sc create mysql binPath mysqld_bin_path(注意&#xff1a;等号与值之间有空格)/* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码SHOW PROCESSLIST -- 显示哪些线程正在运行 SHOW VARIABLES…

CCCC 连续因子

题意&#xff1a; 一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7&#xff0c;其中5、6、7就是3个连续的数字。给定任一正整数N&#xff0c;要求编写程序求出最长连续因子的个数&#xff0c;并输出最小的连续因子序列。 输入格式&#xff1a; 输入在一行…

Mybatis怎么能看是否执行了sql语句

项目需要学习mybatis中&#xff0c;本来mybatis也不是什么新技术&#xff0c;无奈之前没接触过。 验证缓存机制时&#xff0c;需要能看到是否sql被执行了。这就需要增加日志的打印 配置如下 在pom中增加如下依赖&#xff1a; <dependency> <groupId>org.bgee.log4j…

定时备份 MySQL 并上传到七牛

定时备份 MySQL 并上传到七牛 多数应用场景下&#xff0c;我们需要对重要数据进行备份、并放置到一个安全的地方&#xff0c;以备不时之需。 常见的 MySQL 数据备份方式有&#xff0c;直接打包复制对应的数据库或表文件(物理备份)、mysqldump 全量逻辑备份、xtrabackup 增量逻辑…

vue_props div赋值props定义变量 templete获取

vue_props div赋值props定义变量 templete获取 <div id"app"> <add v-bind:btn"h"></add> </div> <script> var vm new Vue({ el: #app, data: { h: "hello" }, components: { "add": { …

H.264句法和语法总结 句法元素的分层结构

在 H.264 定义的码流中&#xff0c;句法元素被组织成有层次的结构&#xff0c;分别描述各个层次的信息&#xff0c;如下图所示 在H.264 中&#xff0c;句法元素共被组织成 序列、图像、片、宏块、子宏块五个层次。 在这样的结构中&#xff0c;每一层的头部和它的数据部分形成管…

instanceof 的运用

2019独角兽企业重金招聘Python工程师标准>>> Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出&#xff0c;这个对象是否是这个特定类或者是它的子类的一个实例。 用法&#xff1a; result object i…

R 脚本读取汇总 Excel 表格数据

主要用到了 xlsx 和 rJava 包&#xff0c;打开 Excel 文件&#xff0c;读取各表格数据&#xff0c;再写入到汇总表。 下图为处理前的原始数据表格&#xff1a; 下图为处理后的数据&#xff1a; 代码实现 安装&加载包的函数实现。installed.packages() 函数获取所有已安装…

[Grid Layout] Place grid items on a grid using grid-column and grid-row

It’s possible to position a grid item anywhere on a grid track. To do this, let’s specify some grid-template-columns and grid-template-rows, and to the grid items, we’ll pass grid-column and grid-row some numeric values. <!DOCTYPE html> <html l…

【大数据】最新大数据学习路线(完整详细版,含整套教程)

大数据学习路线 java(Java se,javaweb) Linux(shell,高并发架构,lucene,solr) Hadoop(Hadoop,HDFS,Mapreduce,yarn,hive,hbase,sqoop,zookeeper,flume) 机器学习(R,mahout) Storm(Storm,kafka,redis) Spark(scala,spark,spark core,spark sql,spark streaming,spark mllib,spa…