【源码阅读】查询

 

总体流程

StmtExecutor.execute的过程总体分为三步:

● 分析hint

● analyze - 可能会遇到需要forward到master执行的情况;ShowStmt也可能转成SelectStmt

○ Query - analyzeAndGenerateQueryPlan

○ 其他Stmt直接调用对应的Stmt的analyze

● 执行 - handleQueryStmt或其他语句的执行

 

analyze阶段

SelectStmt.analyze

FromClause.analyze

核心在于使用Analyzer.resolveTableRef转化最初的TableRef,并对转化后的TableRef调用analyze

    public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
        TableRef leftTblRef = null;  // the one to the left of tblRef
        for (int i = 0; i < tablerefs.size(); ++i) {
            // Resolve and replace non-InlineViewRef table refs with a BaseTableRef or ViewRef.
            TableRef tblRef = tablerefs.get(i);
            tblRef = analyzer.resolveTableRef(tblRef);
            tablerefs.set(i, Preconditions.checkNotNull(tblRef));
            tblRef.setLeftTblRef(leftTblRef);
            if (tblRef instanceof InlineViewRef) {
                ((InlineViewRef) tblRef).setNeedToSql(needToSql);
            }
            tblRef.analyze(analyzer);
            leftTblRef = tblRef;
        }
    }
 

解析各个SelectListItem

    for (SelectListItem item : selectList.getItems()) {
        if (item.isStar()) {
            TableName tblName = item.getTblName();
            if (tblName == null) {
                expandStar(analyzer);
            } else {
                expandStar(analyzer, tblName);
            }
        } else {
            // Analyze the resultExpr before generating a label to ensure enforcement
            // of expr child and depth limits (toColumn() label may call toSql()).
            item.getExpr().analyze(analyzer);
            if (!(item.getExpr() instanceof CaseExpr)
                    && item.getExpr().contains(Predicates.instanceOf(Subquery.class))) {
                throw new AnalysisException("Subquery is not supported in the select list.");
            }
            Expr expr = rewriteQueryExprByMvColumnExpr(item.getExpr(), analyzer);
            resultExprs.add(expr);
            SlotRef aliasRef = new SlotRef(null, item.toColumnLabel());
            Expr existingAliasExpr = aliasSMap.get(aliasRef);
            if (existingAliasExpr != null && !existingAliasExpr.equals(item.getExpr())) {
                // If we have already seen this alias, it refers to more than one column and
                // therefore is ambiguous.
                ambiguousAliasList.add(aliasRef);
            }
            aliasSMap.put(aliasRef, item.getExpr().clone());
            colLabels.add(item.toColumnLabel());
        }
    }          

 

 

执行阶段

在StmtExecutor.handleQueryStmt中,如果没有开启缓存,直接调用StmtExecutor.sendResult

如果开启缓存

posted @ 2023-06-25 18:53  xutao_ustc  阅读(48)  评论(0)    收藏  举报