PostgreSQL 的 语法分析的理解(二)

对于PostgreSQL的语法分析,修改其 gram.y后,

opt_distinct:                                
            DISTINCT        { $$ = list_make1(NIL); }        
            | DISTINCT ON '(' expr_list ')'    { $$ = $4; }        
            | ALL            { $$ = NIL; }        
            | /*EMPTY*/      { $$ = NIL; }

 

simple_select:                                        
            SELECT    opt_distinct    target_list                            
            into_clause     from_clause     where_clause 
            group_clause     having_clause    window_clause 
                {                        
                    SelectStmt *n = makeNode(SelectStmt);                    
                    n->distinctClause = $2;                    
                    n->targetList = $3;                    
                    n->intoClause = $4;                    
                    n->fromClause = $5;                    
                    n->whereClause = $6;                    
                    n->groupClause = $7;                    
                    n->havingClause = $8;                    
                    n->windowClause = $9;                    
                    $$ = (Node *)n;                    
                }                        
            | values_clause   { $$ = $1; }
            | TABLE relation_expr                            
                {                        
                    /* same as SELECT * FROM relation_expr */                    
                    ColumnRef *cr = makeNode(ColumnRef);                    
                    ResTarget *rt = makeNode(ResTarget);                    
                    SelectStmt *n = makeNode(SelectStmt);                    
                                        
                    cr->fields = list_make1(makeNode(A_Star));                    
                    cr->location = -1;                    
                                        
                    rt->name = NULL;                    
                    rt->indirection = NIL;                    
                    rt->val = (Node *)cr;                    
                    rt->location = -1;                    
                                        
                    n->targetList = list_make1(rt);                    
                    n->fromClause = list_make1($2);                    
                    $$ = (Node *)n;                    
                }                        
            | select_clause UNION opt_all select_clause                            
                {                        
                    $$ = makeSetOp(SETOP_UNION, $3, $1, $4);                    
                }                        
            | select_clause INTERSECT opt_all select_clause 
                {                        
                    $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);                    
                }                        
            | select_clause EXCEPT opt_all select_clause   
                {                        
                    $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);                    
                }                        
        ;                             

 

-->


opt_distinct: DISTINCT { $$ = list_make1(NIL);
fprintf(stderr,"In opt_distinct\n");
} | DISTINCT ON '(' expr_list ')' { $$ = $4; } | ALL { $$ = NIL; } | /*EMPTY*/ { $$ = NIL; }

-->


simple_select: SELECT opt_distinct target_list into_clause from_clause where_clause group_clause having_clause window_clause { SelectStmt *n = makeNode(SelectStmt); n->distinctClause = $2; n->targetList = $3; n->intoClause = $4; n->fromClause = $5; n->whereClause = $6; n->groupClause = $7; n->havingClause = $8; n->windowClause = $9; $$ = (Node *)n;
fprintf(stderr,"In simple select\n“); } | values_clause { $$ = $1; } | TABLE relation_expr { /* same as SELECT * FROM relation_expr */ ColumnRef *cr = makeNode(ColumnRef); ResTarget *rt = makeNode(ResTarget); SelectStmt *n = makeNode(SelectStmt); cr->fields = list_make1(makeNode(A_Star)); cr->location = -1; rt->name = NULL; rt->indirection = NIL; rt->val = (Node *)cr; rt->location = -1; n->targetList = list_make1(rt); n->fromClause = list_make1($2); $$ = (Node *)n; } | select_clause UNION opt_all select_clause { $$ = makeSetOp(SETOP_UNION, $3, $1, $4); } | select_clause INTERSECT opt_all select_clause { $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4); } | select_clause EXCEPT opt_all select_clause { $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4); } ;

 在psql执行查询:select  distinct id from a6;

在后台会显示:

In opt_distinct  

In simple select

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值