In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article describes "how PostgreSQL parses expressions." The relevant knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
SQL sample script.
Testdb=# select 1 from t_expr where id c2
< 3;一、数据结构 FmgrInfo 在函数通过fmgr调用前,该结构体持有系统目录(字典)信息,用于检索相关信息. 如果相同的函数将被调用多次,检索只需要完成一次即可,该结构体会缓存多次使用. /* * This struct holds the system-catalog information that must be looked up * before a function can be called through fmgr. If the same function is * to be called multiple times, the lookup need be done only once and the * info struct saved for re-use. * 在函数通过fmgr调用前,该结构体持有系统目录(字典)信息,用于检索相关信息. * 如果相同的函数将被调用多次,检索只需要完成一次即可,该结构体会缓存多次使用. * * Note that fn_expr really is parse-time-determined information about the * arguments, rather than about the function itself. But it's convenient * to store it here rather than in FunctionCallInfoData, where it might more * logically belong. * 注意,fn_expr实际上是关于参数的解析时确定的信息,而不是函数自身. * 但fn_expr在这里存储而不是FunctionCallInfoData中存储,因为从逻辑上来说,它就应该属于那. * * fn_extra is available for use by the called function; all other fields * should be treated as read-only after the struct is created. * fn_extra可用于被调用函数的使用;所有其他字段应该在结构体创建后被处理为只读. */typedef struct FmgrInfo{ //指向函数或者将被调用的处理器 PGFunction fn_addr; /* pointer to function or handler to be called */ //函数的oid Oid fn_oid; /* OID of function (NOT of handler, if any) */ //输入参数的个数,0..FUNC_MAX_ARGS short fn_nargs; /* number of input args (0..FUNC_MAX_ARGS) */ //函数是否严格(strict),输入NULL,输出NULL bool fn_strict; /* function is "strict" (NULL in =>NULL out) * / / whether the function returns the collection bool fn_retset; / * function returns a set * / / if track _ functions > this, collect statistics unsigned char fn_stats; / * collect stats if track_functions > this * / / extra space void * fn_extra used by handler / * extra space for use by handler * / / stores the memory context MemoryContext fn_mcxt; / * memory context to store fn_extra in * / / expression parsing tree of fn_extra, or NULL fmNodePtr fn_expr; / * expression parse tree for call, or NULL * /} FmgrInfo;typedef struct Node * fmNodePtr
FunctionCallInfoData
This structure stores the parameters actually passed to the fmgr-called function
/ * This struct is the data actually passed to an fmgr-called function. * this structure stores the parameters actually passed to the fmgr-called function * * The called function is expected to set isnull, and possibly resultinfo or * fields in whatever resultinfo points to. It should not change any other * fields. (In particular, scribbling on the argument arrays is a bad idea, * since some callers assume they can re-call with the same arguments.) * the called function expects to set the domain field pointed to by isnull and possibly resultinfo or resultinfo. * other fields should not be changed. * (in particular, scribbling on an array of parameters is a bad idea because some callers assume they can be called repeatedly with the same parameters) * / typedef struct FunctionCallInfoData {/ / points to the retrieval information of the call FmgrInfo * flinfo; / * ptr to lookup info used for this call * / / call context fmNodePtr context / * pass info about context of call * / / pass or return special information about the result fmNodePtr resultinfo; / * pass or return extra info about result * / / function collation Oid fncollation; / * collation for function to use * / # define FIELDNO_FUNCTIONCALLINFODATA_ISNULL 4 / / if the result is NULL, it must be set to T bool isnull / * function must set true if result is NULL * / / the number of parameters actually passed short nargs; / * # arguments actually passed * / # define FIELDNO_FUNCTIONCALLINFODATA_ARG 6 / / the argument passed to the function Datum arg[ FUNC _ MAX_ARGS] / * Arguments passed to function * / # define FIELDNO_FUNCTIONCALLINFODATA_ARGNULL 7 / / if arg[ I] is NULL, then the corresponding value is T bool MAX_ARGS [FUNC _ MAX_ARGS]; / * T if arg [I] is actually NULL * /} FunctionCallInfoData;/* * All functions that can be called directly by fmgr must have this signature. * (Other functions can be called by using a handler that does have this * signature.) * all functions can be called directly through fmgr, but must hold a signature. * (other functions can be called by using handler, also with this signature) * / typedef struct FunctionCallInfoData * FunctionCallInfo; II. Source code interpretation
ExecInterpExpr
The code snippets related to expression evaluation in ExecInterpExpr are as follows:
EEO_CASE (EEOP_FUNCEXPR) {FunctionCallInfo fcinfo = op- > d.func.fcinfodata; Datum d; fcinfo- > isnull = false; d = op- > d.func.fn_addr (fcinfo); * op- > resvalue = d; * op- > resnull = fcinfo- > isnull; EEO_NEXT ();}
If it is a function expression, the unified call parameter fcinfo is obtained from the step information of ExecInitFunc initialization, and then the actual function is called through the function pointer (for encapsulation) to evaluate the expression. Through unified parameters and unified return value, the realization is unified, which embodies the idea of polymorphism in object-oriented OO, which once again shows that OO is an idea and can be realized in procedural language.
Int4pl
The corresponding implementation function of the SQL sample script is int4pl. In fact, the code is as follows:
Datumint4pl (PG_FUNCTION_ARGS) {int32 arg1 = PG_GETARG_INT32 (0); int32 arg2 = PG_GETARG_INT32 (1); int32 result; if (unlikely (pg_add_s32_overflow (arg1, arg2, & result)) ereport (ERROR, (errcode (ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg ("integer out of range") PG_RETURN_INT32 (result);} / * If a + b overflows, return true, otherwise store the result of a + b into * * result. The content of * result is implementation defined in case of * overflow. * / static inline boolpg_add_s32_overflow (int32 a, int32 b, int32 * result) {# if defined (HAVE__BUILTIN_OP_OVERFLOW) return _ builtin_add_overflow (a, b, result); # else int64 res = (int64) a + (int64) b; if (res > PG_INT32_MAX | | res < PG_INT32_MIN) {* result = 0x5EED; / * to avoid spurious warnings * / return true } * result = (int32) res; return false;#endif}
The implementation of the function is relatively simple, the two numbers are simply added, if the overflow returns T, otherwise it returns F.
"how PostgreSQL parses expressions." This is the end of the introduction, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.