PostgreSQL如何解析表达式.

67次阅读
没有评论

共计 4874 个字符,预计需要花费 13 分钟才能阅读完成。

本篇内容介绍了“PostgreSQL 如何解析表达式.”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

SQL 样例脚本.

testdb=# select 1+id,c2 from t_expr where id   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) */
 // 函数是否返回集合
 bool fn_retset; /* function returns a set */
 // 如 track_functions   this, 则收集统计信息
 unsigned char fn_stats; /* collect stats if track_functions   this */
 //handler 使用的额外空间
 void *fn_extra; /* extra space for use by handler */
 // 存储 fn_extra 的内存上下文
 MemoryContext fn_mcxt; /* memory context to store fn_extra in */
 // 表达式解析树, 或者为 NULL
 fmNodePtr fn_expr; /* expression parse tree for call, or NULL */
} FmgrInfo;
typedef struct Node *fmNodePtr;

FunctionCallInfoData
该结构体存储了实际传递给 fmgr-called 函数的参数

/*
 * This struct is the data actually passed to an fmgr-called function.
 *  该结构体存储了实际传递给 fmgr-called 函数的参数
 *
 * 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.)
 *  被调用的函数期望设置 isnull 以及可能的 resultinfo 或者 resultinfo 指向的域字段.
 *  不应该改变其他字段.
 * (特别的, 在参数数组上乱写是个坏主意, 因为某些调用者假定它们可以使用相同的参数重复调用)
 */
typedef struct FunctionCallInfoData
 // 指向该调用的检索信息
 FmgrInfo *flinfo; /* ptr to lookup info used for this call */
 // 调用上下文
 fmNodePtr context; /* pass info about context of call */
 // 传递或返回关于结果的特别信息
 fmNodePtr resultinfo; /* pass or return extra info about result */
 // 函数的 collation
 Oid fncollation; /* collation for function to use */
#define FIELDNO_FUNCTIONCALLINFODATA_ISNULL 4
 // 如结果为 NULL, 则必须设置为 T
 bool isnull; /* function must set true if result is NULL */
 // 实际传递的参数个数
 short nargs; /* # arguments actually passed */
#define FIELDNO_FUNCTIONCALLINFODATA_ARG 6
 // 传递给函数的参数
 Datum arg[FUNC_MAX_ARGS]; /* Arguments passed to function */
#define FIELDNO_FUNCTIONCALLINFODATA_ARGNULL 7
 // 如 arg[i]为 NULL, 则对应的值为 T
 bool argnull[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.)
 *  所有函数可以通过 fmgr 直接调用, 但必须持有签名.
 * (其他函数可通过使用 handler 的方式调用, 也有此签名)
 */
typedef struct FunctionCallInfoData *FunctionCallInfo;

二、源码解读

ExecInterpExpr
ExecInterpExpr 中与表达式求值相关的代码片段如下:

EEO_CASE(EEOP_FUNCEXPR)
 {
 FunctionCallInfo fcinfo = op- d.func.fcinfo_data;
 Datum d;
 fcinfo- isnull = false;
 d = op- d.func.fn_addr(fcinfo);
 *op- resvalue = d;
 *op- resnull = fcinfo- isnull;
 EEO_NEXT();
 }

如为函数表达式, 则从 ExecInitFunc 初始化的步骤信息中获取统一的调用参数 fcinfo, 然后通过函数指针 (用于封装) 调用实际的函数进行表达式求值. 通过统一的参数, 统一的返回值, 做到了实现的统一, 体现了面向对象 OO 中多态的思想, 这再次说明了 OO 是思想, 用过程性语言一样可以实现.

int4pl
SQL 样例脚本相应的实现函数是 int4pl, 其实现代码如下:

Datum
int4pl(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 bool
pg_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
}

函数实现相对比较简单, 两个数简单相加, 如溢出则返回 T, 否则返回 F.

“PostgreSQL 如何解析表达式.”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-24发表,共计4874字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)