上一节,任务在Coordinator被解析,主要是生成了一个Statement,(基于Antlr),
---------------------------------------------------------------------------------接下来要做的是分析任务。
// analyze query
SubPlan subplan = analyzeQuery();
那么这个到底做了什么事情呢?函数代码如下:
private SubPlan analyzeQuery() {
try {
return doAnalyzeQuery();
} catch (StackOverflowError e) {
throw new RuntimeException("statement is too large (stack overflow during analysis)", e);
}
}
看来需要看return doAnalyzeQuery();到底做了什么呢?
===先预览下代码
private SubPlan doAnalyzeQuery() {
// time analysis phase
long analysisStart = System.nanoTime();
// analyze query
Analyzer analyzer = new Analyzer(stateMachine.getSession(), metadata, Optional.of(queryExplainer));
Analysis analysis = analyzer.analyze(statement);
PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
// plan query
LogicalPlanner logicalPlanner = new LogicalPlanner(stateMachine.getSession(), planOptimizers, idAllocator,
metadata, periodicImportManager, storageManager);
Plan plan = logicalPlanner.plan(analysis);
// fragment the plan
SubPlan subplan = new DistributedLogicalPlanner(metadata, idAllocator).createSubplans(plan, false);
stateMachine.recordAnalysisTime(analysisStart);
return subplan;
}
---下面认真分析!这货不认真分析,实在不行啊!!!
// time analysis phase
long analysisStart = System.nanoTime();---记住当前时间
然后是:
// analyze query
Analyzer analyzer = new Analyzer(stateMachine.getSession(), metadata, Optional.of(queryExplainer));
生成了一个分析器,
---然后开始解析
Analysis analysis = analyzer.analyze(statement);
具体过程如下:
statement的一个例子是:
> statement = "Query{queryBody=QuerySpecification{select=Select{distinct=false, selectItems=[*]}, from=[Table{abc}], where=null, groupBy=[], having=null, orderBy=[], limit=null}, orderBy=[]}"
这个前面说过,用 antlr分析过的,这里当作黑盒了!
--------------------------------------------------------------------------------------
public Analysis analyze(Statement statement) {
// 具体源码在这里
Analysis analysis = new Analysis();
StatementAnalyzer analyzer = new StatementAnalyzer(analysis, metadata, session, queryExplainer);
TupleDescriptor outputDescriptor = analyzer.process(statement, new AnalysisContext());
analysis.setOutputDescriptor(outputDescriptor);
return analysis;
}
Analysis的初始化过程先略去,有兴趣的自己看下。