impala的sql解析与执行计划生成部分是由impala-frontend(java)实现的,监听端口是21000。用户通过beeswax接口beeswaxservice.query()提交一个请求,在impalad端的处理逻辑是由void impalaserver::query(queryhandle& query_handle, const query& query)这个函数(在impala-beeswax-server.cc中实现)完成的。
在impala中一条sql语句先后经历beeswaxservice.query->tclientrequest->texecrequest,最后把texecrequest交由impala-coordinator分发给多个backend处理。本文主要讲一条sql语句是怎么一步一步变成texecrequest的。
本文以下内容都以这样的一个sql为例说明:
select jobinfo.dt,user,max(taskinfo.finish_time-taskinfo.start_time),max(jobinfo.finish_time-jobinfo.submit_time)from taskinfo join jobinfo on jobinfo.jobid=taskinfo.jobidwhere jobinfo.job_status='success' and taskinfo.task_status='success'group by jobinfo.dt,user
通过调用status impalaserver::getexecrequest(const tclientrequest& request, texecrequest* result) 函数把tclientrequest转化成texecrequest
在这个函数里通过jni接口调用frontend.createexecrequest()生成texecrequest。首先调用analysiscontext.analyze(string stmt)分析提交的sql语句。
注释:analyzer对象是个存放这个sql所涉及到的所有信息(包含table, conjunct, slot,slotrefmap, eqjoinconjuncts等)的知识库,所有跟这个sql有关的东西都会存到analyzer对象里面。
1,sql的词法分析,语法分析analysiscontext.analyze(string stmt)会调用selectstmt.analyze()函数,这个函数就是对sql的analyze和向中央知识库analyzer register各种信息。
(1)处理这个sql所涉及到的table(即tablerefs),这些table是在from从句中提取出来的(包含关键字from, join, on/using)。注意join操作以及on/using条件是存储在参与join操作的右边的表的tableref中并分析的。依次analyze()每个tableref,向analyzer注册registerbasetableref(填充tupledescriptor)。如果对应的tableref涉及到join操作,还要analyzejoin()。在analyzejoin()时会向analyzer registerconjunct()填充analyzer的一些成员变量:conjuncts,tuplepredicates(tupleid与conjunct的映射),slotpredicates(slotid与conjunct的映射),eqjoinconjuncts。本例中on从句是一种binarypredicate,然后onclause.analyze(analyzer)会递归analyze这个on从句里的各种组件。
(2)处理select从句(包含关键字select, max(), avg()等聚集函数):分析这个sql都select了哪几项,每一项都是个expr类型的子类对象,把这几项填入resultexprs数组和collabels。然后把resultexprs里面的expr都递归analyze一下,要分析到树的最底层,向analyzer注册slotref等。
(3)分析where从句(关键字where),首先递归analyze从句中expr组成的树,然后向analyzer registerconjunct()填充analyzer的一些成员变量(同1,此外还要填充whereclauseconjuncts) 。
(4)处理sort相关信息(关键字order by)。先是解析aliases和ordinals,然后从order by后面的从句中提取expr填入orderingexprs,接着递归analyze从句中expr组成的树,最后创建sortinfo对象。
(5)处理aggregation相关信息(关键字group by, having, avg, max等)。首先递归分析group by从句里的expr,然后如果有having从句就像where从句一样,先是analyze having从句中expr组成的树,然后向analyzer registerconjunct()等。
(6)处理inlineview。
关于sql解析中所涉及到的各种数据结构表示如下:
至此词法分析,语法分析结束,有点像一个小的编译器。我们现在回到frontend.createexecrequest()函数中。调用完analysiscontext.analyze()之后,就开始填充texecrequest内的成员变量。
(1)如果是ddl命令(use, show tables, show databases, describe),那么调用createddlexecrequest();
(2)另外一种情况就是query或者dml命令,那么就得创建和填充tqueryexecrequest了。
2,根据sql语法树生成执行计划(plannode和planfragment的生成)下面就是用planner把sql解析出的语法树转换成plan fragments,后者能在各个backend被执行。
planner planner = new planner();
arraylistfragments =
planner.createplanfragments(analysisresult, request.queryoptions);
这个createplanfragments()函数是frontend最重要的函数:根据sql解析的结果和client传入的query options,生成执行计划。执行计划是用planfragment的数组表示的,最后会序列化到tqueryexecrequest.fragments然后传给backend的coordinator去调度执行。
下面进入planner.createplanfragments()函数看看执行计划是怎么生成的:
首先要搞清楚两个概念:plannode和planfragment。
plannode是sql解析出来的逻辑功能节点;planfragment是真正的执行计划节点。
2.1,创建plannodeplannode singlenodeplan =
createqueryplan(querystmt, analyzer, queryoptions.getdefault_order_by_limit());
(1)这个函数首先根据from从句中的第一个tableref创建一个plannode,一般为scannode(hdfsscannode或者hbasescannode)。这个scannode关联一个valuerange的数组(由多个cluster column取值区间组成)表示要读取的table的范围,还关联一个conjunct(where从句)。
(2)这个sql语句中tableref中剩下的其他table就需要建立hashjoinnode了。进入planner.createhashjoinnode()函数:首先为这个table建立scannode(同上),然后调用gethashlookupjoinconjuncts()获取两表或者多表join的eqjoinconjuncts和eqjoinpredicates,利用这两个条件创建hashjoinnode。每个hashjoinnode也是树状的,会有孩子节点,对于我们举例的两表join,孩子节点分别是两个表对应的scannode。(注意目前impala只支持一大一小两个表的join,默认是左大右小,是通过把右边的小表分发到每个节点的内存中分别于左边大表的一个区间进行join过滤实现的。)
(3)如果有group by从句,创建aggregationnode,并把刚才的hashjoinnode设为它的孩子。这里暂时不考虑distinct aggregation function。
(4)如果有order by… limit从句,创建sortnode。
这样createqueryplan()函数执行完毕,plannode组成的execution tree形成如下:
2.2,创建planfragment接下来就看impala backend节点数目有多少,如果只有一个节点,那么整棵执行树都在同一个impalad上执行;否则调用createplanfragments(singlenodeplan, ispartitioned, false, fragments)把plannode组成的执行树转换成planfragment组成的执行计划。
下面进入createplanfragments()这个函数:
这是一个递归函数,沿着plannode组成的执行树递归下去,分别创建对应的fragment。
(1)如果是scannode,创建一个planfragment(这个planfragment的root node是这个scannode,而且这个planfragment只包含一个plannode)。
(2)如果是hashjoinnode,并不是创建一个新的planfragment,而是修改leftchildfragment(是一个scannode)为以hashjoinnode作为root node的planfragment。因为对于hashjoinnode一般有两个scannode孩子,在处理hashjoinnode之前已经把这两个scannode变成了对应的planfragment。那么此时要得到hashjoinnode作为root node的planfragment是通过planner.createhashjoinfragment()函数完成的:首先把当前hashjoinnode作为hashjoinfragment的root node;然后把leftchildfragment中的root plannode(也就是参与join的两个表中左边的那个表对应的scannode)作为hashjoinnode的左孩子;通过调用planner.connectchildfragment()函数把hashjoinnode的右孩子设置为一个exchangenode(这个exchangenode表示一个1:n的数据流的receiver);同时把rightchildfragment(scannode作为root node)的destination设置为这个exchangenode。
(3)如果是aggregationnode,聚集操作很复杂了。以我们的例子来说明:如果这个aggregationnode不是distinct aggregation的2nd phase(因为本例中的aggregationnode的孩子是hashjoinnode而不是另外一个aggregationnode),首先把刚才生成的hashjoinnode作为root node对应的planfragment的root node设置为该aggregationnode,并把原来的root node(即hashjoinnode)设为新root node的孩子。然后通过planner.createparentfragment()创建一个包含exchangenode作为root node的新的planfragment。并把孩子planfragment的destination设置为这个exchangenode。然后在这个新的planfragment中创建一个新的aggregationnode作为新的root node并把刚才的exchangenode作为其孩子节点。
至此,createplanfragments()调用完成,生成的三个planfragment如下:
通过createplanfragments(singlenodeplan, ispartitioned, false, fragments)获取了所以执行计划planfragment组成的数组fragments,这个数组的最后一个元素就是根节点planfragment。然后就是调用planfragment.finalize()把这个执行计划finalize(递归finalize每个plannode)同时为每个planfragment指定 datastreamsink。
然后回到frontend.createexecrequest()函数中。执行完planner.createplanfragments()返回的arraylist就是完整的执行计划了。然后就是一次调用planfragment.tothrift()把它序列化到tqueryexecrequest。填充tqueryexecrequest的相关变量:dest_fragment_idx,per_node_scan_ranges,query_globals,result_set_metadata等。最后返回texecrequest型的对象给backend执行。
impala-backend(c++代码)拿到这个texecrequest对象,有coordinator在各个backend之间分发执行,这是下一篇文章的内容了。
吐槽:从中还是能够看到mapreduce的影子的。。。对于每个planfragment有个datastreamsink,会指向其他planfragment中的exchangenode,是个1对n的关系。。。所以分布式系统的瓶颈还是data shuffle,不管是mapreduce模型还是impala。这也说明其实tez/stinger initiative 对hive的优化还是很值得期待的。
参考文献:http://blog.csdn.net/wind5shy/article/details/8563355
原文地址:impala源代码分析(2)-sql解析与执行计划生成, 感谢原作者分享。
