PostgreSQL 的 target_list分析(四)

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:
根据 <PostgreSQL 数据库内核分析>200和201页的说法,

ResTarget 应该指向 ColumnRef 。这是如何实现的呢?

复制代码
target_list:                                
            target_el                { $$ = list_make1($1); }    
            | target_list ',' target_el                { $$ = lappend($1, $3); } 
        ;                        
                                
target_el:    a_expr AS ColLabel                            
                {                
                    $$ = makeNode(ResTarget);            
                    $$->name = $3;            
                    $$->indirection = NIL;            
                    $$->val = (Node *)$1;            
                    $$->location = @1;            
                }                
            /*                    
             * We support omitting AS only for column labels that aren't  
             * any known keyword.  There is an ambiguity against postfix
             * operators: is "a ! b" an infix expression, or a postfix 
             * expression and a column label?  We prefer to resolve this 
             * as an infix expression, which we accomplish by assigning  
             * IDENT a precedence higher than POSTFIXOP.                    
             */                    
    | a_expr IDENT                            
                {                
                    $$ = makeNode(ResTarget);            
                    $$->name = $2;            
                    $$->indirection = NIL;            
                    $$->val = (Node *)$1;            
                    $$->location = @1;            
                }                
    | a_expr                            
                {                
                    $$ = makeNode(ResTarget);            
                    $$->name = NULL;            
                    $$->indirection = NIL;            
                    $$->val = (Node *)$1;            
                    $$->location = @1; 
                }                
            | '*'                    
                {                
                    ColumnRef *n = makeNode(ColumnRef);            
                    n->fields = list_make1(makeNode(A_Star));            
                    n->location = @1;            
                                
                    $$ = makeNode(ResTarget);            
                    $$->name = NULL;            
                    $$->indirection = NIL;            
                    $$->val = (Node *)n;            
                    $$->location = @1;            
                }                
        ;                        
复制代码
 和

复制代码
a_expr:     c_expr        
                    { $$ = $1; }                    
            | a_expr TYPECAST Typename                        
                    { $$ = makeTypeCast($1, $3, @2); }                
            | a_expr COLLATE any_name                        
                    {                    
                    CollateClause *n = makeNode(CollateClause);                
                    n->arg = $1;                
                    n->collname = $3;                
                    n->location = @2;                
                    $$ = (Node *) n;                
                    }
...                    
复制代码

复制代码
c_expr:     columnref                               { $$ = $1; }
            | AexprConst                            { $$ = $1; }
            | PARAM opt_indirection                            
                {                        
                    ParamRef *p = makeNode(ParamRef);                    
                    p->number = $1;                    
                    p->location = @1;                    
                    if ($2)                    
                    {                    
                        A_Indirection *n = makeNode(A_Indirection);                
                        n->arg = (Node *) p;                
                        n->indirection = check_indirection($2, yyscanner);                
                        $$ = (Node *) n;                
                    }                    
                    else                    
                        $$ = (Node *) p;                
                }                        
            | '(' a_expr ')' opt_indirection                            
                {                        
                    if ($4)                    
                    {                    
                        A_Indirection *n = makeNode(A_Indirection);                
                        n->arg = $2;                
                        n->indirection = check_indirection($4, yyscanner);                
                        $$ = (Node *)n;                
                    }                    
                    else                    
                        $$ = $2;                
                }                        
            | case_expr                            
                { $$ = $1; }                        
            | func_expr                            
                { $$ = $1; }                        
            | select_with_parens            %prec UMINUS                
                {                        
                    SubLink *n = makeNode(SubLink);                    
                    n->subLinkType = EXPR_SUBLINK;                    
                    n->testexpr = NULL;                    
                    n->operName = NIL;                    
                    n->subselect = $1;                    
                    n->location = @1;                    
                    $$ = (Node *)n;                    
                }                        
            | EXISTS select_with_parens                            
                {                        
                    SubLink *n = makeNode(SubLink);                    
                    n->subLinkType = EXISTS_SUBLINK;                    
                    n->testexpr = NULL;                    
                    n->operName = NIL;                    
                    n->subselect = $2;                    
                    n->location = @1;                    
                    $$ = (Node *)n;                    
                }                        
            | ARRAY select_with_parens                            
                {                        
                    SubLink *n = makeNode(SubLink);                    
                    n->subLinkType = ARRAY_SUBLINK;                    
                    n->testexpr = NULL;                    
                    n->operName = NIL;                    
                    n->subselect = $2;                    
                    n->location = @1;                    
                    $$ = (Node *)n;                    
                }                        
            | ARRAY array_expr                            
                {                        
                    A_ArrayExpr *n = (A_ArrayExpr *) $2;                    
                    Assert(IsA(n, A_ArrayExpr));                    
                    /* point outermost A_ArrayExpr to the ARRAY keyword */                    
                    n->location = @1;                    
                    $$ = (Node *)n;                    
                }                        
            | row                            
                {                        
                    RowExpr *r = makeNode(RowExpr);                    
                    r->args = $1;                    
                    r->row_typeid = InvalidOid;    /* not analyzed yet */                
                    r->location = @1;                    
                    $$ = (Node *)r;                    
                }                        
        ;                                
复制代码
这个时候,距离ColumnRef 就已经不远了。

然后挑选最简单的情况:

复制代码
columnref:    ColId                                
                {                    
                    $$ = makeColumnRef($1, NIL, @1, yyscanner);                
                }                    
    | ColId indirection                                
                {                    
                    $$ = makeColumnRef($1, $2, @1, yyscanner);                
                }                    
    ;                                
复制代码
查了一下, 原来 makeColumnRef 就在 gram.y 里面:

复制代码
static Node *                                                                
makeColumnRef(char *colname, List *indirection, 
              int location, core_yyscan_t yyscanner)
{                                                                
    /*                                                            
     * Generate a ColumnRef node, with an A_Indirection node added if there 
     * is any subscripting in the specified indirection list.  However,
     * any field selection at the start of the indirection list must be 
     * transposed into the "fields" part of the ColumnRef node. 
     */                                                            
    ColumnRef  *c = makeNode(ColumnRef);                                                            
    int        nfields = 0;                                                    
    ListCell *l;                                                            
                                                                
    c->location = location; 
    foreach(l, indirection)
    {                                                            
        if (IsA(lfirst(l), A_Indices)) 
        {                                                        
            A_Indirection *i = makeNode(A_Indirection);
                                                                
            if (nfields == 0)                                                    
            {                                                    
                /* easy case - all indirection goes to A_Indirection */ 
                c->fields = list_make1(makeString(colname));                                                
                i->indirection = check_indirection(indirection, yyscanner);                                                
            }                                                    
            else                                                    
            {                                                    
                /* got to split the list in two */
                i->indirection = check_indirection(
                    list_copy_tail(indirection, nfields),yyscanner);                
                indirection = list_truncate(indirection, nfields);                                                
                c->fields = lcons(makeString(colname), indirection);                                                
            }                                                    
            i->arg = (Node *) c;                                                    
            return (Node *) i;                                                    
        }                                                        
        else if (IsA(lfirst(l), A_Star))                                                        
        {                                                        
            /* We only allow '*' at the end of a ColumnRef */ 
            if (lnext(l) != NULL)
                parser_yyerror("improper use of \"*\"");                                                
        }                                                        
        nfields++;                                                        
    }                                                            
    /* No subscripting, so all indirection gets added to field list */ 
    c->fields = lcons(makeString(colname), indirection);                                                            
    return (Node *) c;                                                            
}                                                                 
复制代码
这个

ColumnRef  *c = makeNode(ColumnRef);


c->fields = list_make1(makeString(colname)); 将 字段 赋予了 ColumnRef。

可以看出,语法分析是一层套着一层。而整个语法分析的目的,就是逐层地构建一个语法分析树形结构。


本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/09/10/2678905.html,如需转载请自行联系原作者
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
21天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
16 0
|
27天前
|
关系型数据库 MySQL 数据挖掘
轻松入门MySQL:利用MySQL时间函数优化产品销售数据统计与分析(9)
轻松入门MySQL:利用MySQL时间函数优化产品销售数据统计与分析(9)
|
2月前
|
SQL 存储 Oracle
mysql中Group By 分析
mysql中Group By 分析
22 0
|
2月前
|
Apache 索引
精进Hudi系列|Apache Hudi索引实现分析(五)之基于List的IndexFileFilter
精进Hudi系列|Apache Hudi索引实现分析(五)之基于List的IndexFileFilter
17 0
|
4月前
|
关系型数据库 MySQL Serverless
高顿教育:大数据抽数分析业务引入polardb mysql serverless
高顿教育通过使用polardb serverless形态进行数据汇总,然后统一进行数据同步到数仓,业务有明显高低峰期,灵活的弹性伸缩能力,大大降低了客户使用成本。
|
2月前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
98 0
|
21天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
87 1
|
27天前
|
缓存 关系型数据库 MySQL
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
|
29天前
|
SQL 关系型数据库 MySQL
【MySQL】慢SQL分析流程
【4月更文挑战第1天】【MySQL】慢SQL分析流程
|
2月前
|
缓存 网络协议 关系型数据库
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(系统底层优化篇)(二)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(系统底层优化篇)
34 0