8516830 发表于 2016-11-21 08:02:36

PostgreSQL服务过程中的那些事二:Pg服务进程处理简单查询二:SQL解析为parsetree

话说查询“selectcname, comp from test1, test2 where test1.id=test2.id;” 发送到服务器端,走查询分支exec_simple_query,先调用start_xact_command初始化了事务管理相关对象和资源,接着调用pg_parse_query,通过Lex和Yacc对传入SQL语句进行词法语法解析,生成解析树。
 
1
下面是解析SQL语句的调用序列图。





Postgres服务进程简查之开始事务调用序列图
 
         上图红色方框中显示了解析SQL语句的方法调用过程,在raw_parser方法中对SQL语句进行词法、语法解析并返回解析树parsetree的列表,因为传入的SQL串(从客户端传入)可能有多个命令。
 
通过Lex和Yacc对传入SQL语句进行词法语法解析,主要是把SQL语句里的目前字段、DISTINCT子句、FROM子句、WHERE子句、GROUP BY子句、HAVING子句、ORDER BY子句等解析到各自对应的结构中,组织成各自的数据结构,最后组成解析树parsetree。分析完后对应SQL语句“selectcname, comp from test1, test2 where test1.id=test2.id;”的解析树结构如下图:



例子中SQL语句对应的解析树结构
pg输出的解析树如下:
2011-11-2306:57:39 HKT DETAIL:    
           {QUERY
           :commandType 1
           :querySource 0
           :canSetTag true
           :utilityStmt <>
           :resultRelation 0
           :intoClause <>
           :hasAggs false
           :hasWindowFuncs false
           :hasSubLinks false
           :hasDistinctOn false
           :hasRecursive false
           :hasModifyingCTE false
           :hasForUpdate false
           :cteList <>
           :rtable (
              {RTE
              :alias <>
              :eref
                 {ALIAS
                 :aliasname pg_type
                 :colnames ("typname" "typnamespace""typowner" "typlen" "typbyval" "t
                 yptype" "typcategory" "typispreferred""typisdefined" "typdelim" "typ
                 relid" "typelem" "typarray""typinput" "typoutput" "typreceive" "typs
                 end" "typmodin" "typmodout""typanalyze" "typalign" "typstorage" "typ
                 notnull" "typbasetype" "typtypmod""typndims" "typcollation" "typdefa
                 ultbin" "typdefault")
                 }
               :rtekind 0
              :relid 1247
              :relkind r
              :inh true
              :inFromCl true
              :requiredPerms 2
              :checkAsUser 0
              :selectedCols (b 6)
              :modifiedCols (b)
              }
           )
           :jointree
              {FROMEXPR
              :fromlist (
                 {RANGETBLREF
                 :rtindex 1
                 }
              )
              :quals
                 {OPEXPR
                 :opno 607
                 :opfuncid 184
                 :opresulttype 16
                 :opretset false
                 :opcollid 0
                 :inputcollid 0
                 :args (
                     {VAR
                     :varno 1
                     :varattno -2
                     :vartype 26
                     :vartypmod -1
                     :varcollid 0
                     :varlevelsup 0
                     :varnoold 1
                     :varoattno -2
                    :location 57
                     }
                     {RELABELTYPE
                     :arg
                        {CONST
                        :consttype 23
                        :consttypmod -1
                        :constcollid 0
                        :constlen 4
                        :constbyval true
                        :constisnull false
                        :location 63
                        :constvalue 4 [ 19 4 0 0 ]
                        }
                     :resulttype 26
                     :resulttypmod -1
                     :resultcollid 0
                     :relabelformat 2
                     :location -1
                     }
                 )
                 :location 61
                 }
              }
           :targetList (
              {TARGETENTRY
              :expr
                 {FUNCEXPR
                 :funcid 1081
                 :funcresulttype 25
                 :funcretset false
                 :funcformat 0
                 :funccollid 100
                 :inputcollid 0
                 :args (
                     {VAR
                     :varno 1
                     :varattno -2
                     :vartype 26
                     :vartypmod -1
                     :varcollid 0
                    :varlevelsup 0
                     :varnoold 1
                     :varoattno -2
                     :location 19
                     }
                     {CONST
                     :consttype 23
                     :consttypmod -1
                     :constcollid 0
                     :constlen 4
                    :constbyval true
                     :constisnull false
                     :location 23
                     :constvalue 4 [ 34 0 0 0 ]
                     }
                 )
                 :location 7
                 }
              :resno 1
              :resname typname
              :ressortgroupref 0
               :resorigtbl 0
              :resorigcol 0
              :resjunk false
              }
           )
           :returningList <>
           :groupClause <>
           :havingQual <>
           :windowClause <>
           :distinctClause <>
           :sortClause <>
           :limitOffset <>
           :limitCount <>
           :rowMarks <>
           :setOperations <>
           :constraintDeps <>
           }
 
         关于进行词法语法分析的Lex和Yacc请参考下面的资料,这些内容引自博客文章《一天之内不再畏惧lex&yacc之必备参考资料》,url是http://blog.sciencenet.cn/blog-419883-309595.html

[*] Lex andYACC primer/HOWTO http://tldp.org/HOWTO/Lex-YACC-HOWTO.html,短短20页,足够让你建立自信,一个上午足够了吧。
[*]A CompactGuide to Lex & Yacc http://epaperpress.com/lexandyacc/,修行再提高一步,一个下午搞定。
[*]创始人写的书你得看看吧,Lex and yacc, By John R. Levine, Tony Mason,Doug Brown ,不能下载,没关系,这里有:http://www.filefront.com/16046001/lex--yacc.pdf/,第五章Parsing SQL你不看一看?晚上吃完饭在教室里摆开架势三个小时敲定这短短30页的第5章,一气呵成,搞定。
有这三篇文档给你揣在兜里,我想你走路的时候应该“保劲”吧。




 
 
------------
转载请注明出处,来自博客:
blog.csdn.net/beiigang
beigang.iyunv.com
页: [1]
查看完整版本: PostgreSQL服务过程中的那些事二:Pg服务进程处理简单查询二:SQL解析为parsetree