PostgreSQL 源码解读(19)- 查询语句#4(ParseTree详解)
解析树
二、数据结构1、SelectStmt
/*----------------------*SelectStatement**A"simple"SELECTisrepresentedintheoutputofgram.ybyasingle*SelectStmtnode;soisaVALUESconstruct.Aquerycontainingset*operators(UNION,INTERSECT,EXCEPT)isrepresentedbyatreeofSelectStmt*nodes,inwhichtheleafnodesarecomponentSELECTsandtheinternalnodes*representUNION,INTERSECT,orEXCEPToperators.Usingthesamenode*typeforbothleafandinternalnodesallowsgram.ytostickORDERBY,*LIMIT,etc,clausevaluesintoaSELECTstatementwithoutworrying*whetheritisasimpleorcompoundSELECT.*----------------------*/typedefenumSetOperation{SETOP_NONE=0,SETOP_UNION,SETOP_INTERSECT,SETOP_EXCEPT}SetOperation;typedefstructSelectStmt{NodeTagtype;/**Thesefieldsareusedonlyin"leaf"SelectStmts.*/List*distinctClause;/*NULL,listofDISTINCTONexprs,or*lcons(NIL,NIL)forall(SELECTDISTINCT)*/IntoClause*intoClause;/*targetforSELECTINTO*/List*targetList;/*thetargetlist(ofResTarget)*/List*fromClause;/*theFROMclause*/Node*whereClause;/*WHEREqualification*/List*groupClause;/*GROUPBYclauses*/Node*havingClause;/*HAVINGconditional-expression*/List*windowClause;/*WINDOWwindow_nameAS(...),...*//**Ina"leaf"noderepresentingaVALUESlist,theabovefieldsareall*null,andinsteadthisfieldisset.Notethattheelementsofthe*sublistsarejustexpressions,withoutResTargetdecoration.Alsonote*thatalistelementcanbeDEFAULT(representedasaSetToDefault*node),regardlessofthecontextoftheVALUESlist.It'suptoparse*analysistorejectthatwherenotvalid.*/List*valuesLists;/*untransformedlistofexpressionlists*//**Thesefieldsareusedinboth"leaf"SelectStmtsandupper-level*SelectStmts.*/List*sortClause;/*sortclause(alistofSortBy's)*/Node*limitOffset;/*#ofresulttuplestoskip*/Node*limitCount;/*#ofresulttuplestoreturn*/List*lockingClause;/*FORUPDATE(listofLockingClause's)*/WithClause*withClause;/*WITHclause*//**Thesefieldsareusedonlyinupper-levelSelectStmts.*/SetOperationop;/*typeofsetop*/boolall;/*ALLspecified?*/structSelectStmt*larg;/*leftchild*/structSelectStmt*rarg;/*rightchild*//*EventuallyaddfieldsforCORRESPONDINGspechere*/}SelectStmt;
2、RangeSubselect
/**RangeSubselect-subqueryappearinginaFROMclause*/typedefstructRangeSubselect{NodeTagtype;boollateral;/*doesithaveLATERALprefix?*/Node*subquery;/*theuntransformedsub-selectclause*/Alias*alias;/*tablealias&optionalcolumnaliases*/}RangeSubselect;
3、Alias
/**Alias-*specifiesanaliasforarangevariable;thealiasmightalso*specifyrenamingofcolumnswithinthetable.**Note:colnamesisalistofValuenodes(alwaysstrings).InAliasstructs*associatedwithRTEs,theremaybeentriescorrespondingtodropped*columns;thesearenormallyemptystrings("").Seeparsenodes.hforinfo.*/typedefstructAlias{NodeTagtype;char*aliasname;/*aliasedrelname(neverqualified)*/List*colnames;/*optionallistofcolumnaliases*/}Alias;
4、ResTarget
/**ResTarget-*resulttarget(usedintargetlistofpre-transformedparsetrees)**InaSELECTtargetlist,'name'isthecolumnlabelfroman*'ASColumnLabel'clause,orNULLiftherewasnone,and'val'isthe*valueexpressionitself.The'indirection'fieldisnotused.**INSERTusesResTargetinitstarget-column-nameslist.Here,'name'is*thenameofthedestinationcolumn,'indirection'storesanysubscripts*attachedtothedestination,and'val'isnotused.**InanUPDATEtargetlist,'name'isthenameofthedestinationcolumn,*'indirection'storesanysubscriptsattachedtothedestination,and*'val'istheexpressiontoassign.**SeeA_Indirectionformoreinfoaboutwhatcanappearin'indirection'.*/typedefstructResTarget{NodeTagtype;char*name;/*columnnameorNULL*/List*indirection;/*subscripts,fieldnames,and'*',orNIL*/Node*val;/*thevalueexpressiontocomputeorassign*/intlocation;/*tokenlocation,or-1ifunknown*/}ResTarget;
5、FromExpr
/*----------*FromExpr-representsaFROM...WHERE...construct**ThisisbothmoreflexiblethanaJoinExpr(itcanhaveanynumberof*children,includingzero)andlessso---wedon'tneedtodealwith*aliasesandsoon.Theoutputcolumnsetisimplicitlyjusttheunion*oftheoutputsofthechildren.*----------*/typedefstructFromExpr{NodeTagtype;List*fromlist;/*Listofjoinsubtrees*/Node*quals;/*qualifiersonjoin,ifany*/}FromExpr;
6、JoinExpr
/*----------*JoinExpr-forSQLJOINexpressions**isNatural,usingClause,andqualsareinterdependent.Theusercanwrite*onlyoneofNATURAL,USING(),orON()(thisisenforcedbythegrammar).*IfhewritesNATURALthenparseanalysisgeneratestheequivalentUSING()*list,andfromthatfillsin"quals"withtherightequalitycomparisons.*IfhewritesUSING()then"quals"isfilledwithequalitycomparisons.*IfhewritesON()thenonly"quals"isset.NotethatNATURAL/USING*arenotequivalenttoON()sincetheyalsoaffecttheoutputcolumnlist.**aliasisanAliasnoderepresentingtheASalias-clauseattachedtothe*joinexpression,orNULLifnoclause.NB:presenceorabsenceofthe*aliashasacriticalimpactonsemantics,becauseajoinwithanalias*restrictsvisibilityofthetables/columnsinsideit.**Duringparseanalysis,anRTEiscreatedfortheJoin,anditsindex*isfilledintortindex.ThisRTEispresentmainlysothatVarscan*becreatedthatrefertotheoutputsofthejoin.Theplannersometimes*generatesJoinExprsinternally;thesecanhavertindex=0ifthereare*nojoinaliasvariablesreferencingsuchjoins.*----------*/typedefstructJoinExpr{NodeTagtype;JoinTypejointype;/*typeofjoin*/boolisNatural;/*Naturaljoin?Willneedtoshapetable*/Node*larg;/*leftsubtree*/Node*rarg;/*rightsubtree*/List*usingClause;/*USINGclause,ifany(listofString)*/Node*quals;/*qualifiersonjoin,ifany*/Alias*alias;/*user-writtenaliasclause,ifany*/intrtindex;/*RTindexassignedforjoin,or0*/}JoinExpr;
7、RangeVar
/**RangeVar-rangevariable,usedinFROMclauses**Alsousedtorepresenttablenamesinutilitystatements;there,thealias*fieldisnotused,andinhtellswhethertoapplytheoperation*recursivelytochildtables.Insomecontextsitisalsousefultocarry*aTEMPtableindicationhere.*/typedefstructRangeVar{NodeTagtype;char*catalogname;/*thecatalog(database)name,orNULL*/char*schemaname;/*theschemaname,orNULL*/char*relname;/*therelation/sequencename*/boolinh;/*expandrelbyinheritance?recursivelyact*onchildren?*/charrelpersistence;/*seeRELPERSISTENCE_*inpg_class.h*/Alias*alias;/*tablealias&optionalcolumnaliases*/intlocation;/*tokenlocation,or-1ifunknown*/}RangeVar;
8、ColumnRef
/**ColumnRef-specifiesareferencetoacolumn,orpossiblyawholetuple**The"fields"listmustbenonempty.ItcancontainstringValuenodes*(representingnames)andA_Starnodes(representingoccurrenceofa'*').*Currently,A_Starmustappearonlyasthelastlistelement---thegrammar*isresponsibleforenforcingthis!**Note:anyarraysubscriptingorselectionoffieldsfromcompositecolumns*isrepresentedbyanA_IndirectionnodeabovetheColumnRef.However,*forsimplicityinthenormalcase,initialfieldselectionfromatable*nameisrepresentedwithinColumnRefandnotbyaddingA_Indirection.*/typedefstructColumnRef{NodeTagtype;List*fields;/*fieldnames(Valuestrings)orA_Star*/intlocation;/*tokenlocation,or-1ifunknown*/}ColumnRef;三、小结
1、解析树:通过跟踪分析源码,分析解析树Parsetree的结构;
2、其他数据结构:SelectSmt、RangeSubselect、JoinExpr等数据结构。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。