本篇内容主要讲解“怎么实现PostgreSQL中的类型转换”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么实现PostgreSQL中的类型转换”吧!
解析表达式,涉及不同数据类型时:
1.如有相应类型的Operator定义(pg_operator),则尝试进行类型转换,否则报错;
2.如有相应类型的转换规则,转换为目标类型后解析,否则报错.
一、数据结构
Form_pg_operator
pg_operator中的定义,代码会其中的定义转换为FormData_pg_operator结构体
CATALOG(pg_operator,2617,OperatorRelationId)
{
Oid oid;
NameData oprname;
Oid oprnamespace BKI_DEFAULT(PGNSP);
Oid oprowner BKI_DEFAULT(PGUID);
char oprkind BKI_DEFAULT(b);
bool oprcanmerge BKI_DEFAULT(f);
bool oprcanhash BKI_DEFAULT(f);
Oid oprleft BKI_LOOKUP(pg_type);
Oid oprright BKI_LOOKUP(pg_type);
Oid oprresult BKI_LOOKUP(pg_type);
Oid oprcom BKI_DEFAULT(0) BKI_LOOKUP(pg_operator);
Oid oprnegate BKI_DEFAULT(0) BKI_LOOKUP(pg_operator);
regproc oprcode BKI_LOOKUP(pg_proc);
regproc oprrest BKI_DEFAULT(-) BKI_LOOKUP(pg_proc);
regproc oprjoin BKI_DEFAULT(-) BKI_LOOKUP(pg_proc);
} FormData_pg_operator;
typedef FormData_pg_operator *Form_pg_operator;
二、源码解读
coerce_type函数实现具体的类型转换.
Node *
coerce_type(ParseState *pstate, Node *node,
Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod,
CoercionContext ccontext, CoercionForm cformat, int location)
{
Node *result;//结果Node
CoercionPathType pathtype;
Oid funcId;
if (targetTypeId == inputTypeId ||
node == NULL)
{
//不需要转换
return node;
}
if (targetTypeId == ANYOID ||
targetTypeId == ANYELEMENTOID ||
targetTypeId == ANYNONARRAYOID)
{
//目标类型可以为任意类型(ANYXXX)
return node;
}
if (targetTypeId == ANYARRAYOID ||
targetTypeId == ANYENUMOID ||
targetTypeId == ANYRANGEOID)
{
if (inputTypeId != UNKNOWNOID)
{
//获取基本类型
Oid baseTypeId = getBaseType(inputTypeId);
if (baseTypeId != inputTypeId)
{
RelabelType *r = makeRelabelType((Expr *) node,
baseTypeId, -1,
InvalidOid,
cformat);
r->location = location;
return (Node *) r;
}
return node;
}
}
if (inputTypeId == UNKNOWNOID && IsA(node, Const))
{
//---------------- 输入类型为unknown并且是常量
Const *con = (Const *) node;//常量
Const *newcon = makeNode(Const);//转换后逇常量
Oid baseTypeId;//基本类型Oid
int32 baseTypeMod;//基本typmode
int32 inputTypeMod;//输入typmode
Type baseType;//基本类型
ParseCallbackState pcbstate;//解析回调函数
baseTypeMod = targetTypeMod;
baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
if (baseTypeId == INTERVALOID)
inputTypeMod = baseTypeMod;
else
inputTypeMod = -1;
baseType = typeidType(baseTypeId);
//构造输出常量Const
newcon->consttype = baseTypeId;
newcon->consttypmod = inputTypeMod;
newcon->constcollid = typeTypeCollation(baseType);
newcon->constlen = typeLen(baseType);
newcon->constbyval = typeByVal(baseType);
newcon->constisnull = con->constisnull;
//使用原始位置
newcon->location = con->location;
//如果报错,则指向该常量文本
setup_parser_errposition_callback(&pcbstate, pstate, con->location);
if (!con->constisnull)
newcon->constvalue = stringTypeDatum(baseType,
DatumGetCString(con->constvalue),
inputTypeMod);
else
newcon->constvalue = stringTypeDatum(baseType,
NULL,
inputTypeMod);
//如为可变长度值,则强制其为非扩展格式.
if (!con->constisnull && newcon->constlen == -1)
newcon->constvalue =
PointerGetDatum(PG_DETOAST_DATUM(newcon->constvalue));
#ifdef RANDOMIZE_ALLOCATED_MEMORY
if (!con->constisnull && !newcon->constbyval)
{
Datum val2;
val2 = stringTypeDatum(baseType,
DatumGetCString(con->constvalue),
inputTypeMod);
if (newcon->constlen == -1)
val2 = PointerGetDatum(PG_DETOAST_DATUM(val2));
if (!datumIsEqual(newcon->constvalue, val2, false, newcon->constlen))
elog(WARNING, "type %s has unstable input conversion for \"%s\"",
typeTypeName(baseType), DatumGetCString(con->constvalue));
}
#endif
cancel_parser_errposition_callback(&pcbstate);
//结果Node
result = (Node *) newcon;
if (baseTypeId != targetTypeId)
result = coerce_to_domain(result,
baseTypeId, baseTypeMod,
targetTypeId,
ccontext, cformat, location,
false);
ReleaseSysCache(baseType);
//返回
return result;
}
if (IsA(node, Param) &&
pstate != NULL && pstate->p_coerce_param_hook != NULL)
{
result = pstate->p_coerce_param_hook(pstate,
(Param *) node,
targetTypeId,
targetTypeMod,
location);
if (result)
return result;
}
if (IsA(node, CollateExpr))
{
CollateExpr *coll = (CollateExpr *) node;
CollateExpr *newcoll = makeNode(CollateExpr);
newcoll->arg = (Expr *)
coerce_type(pstate, (Node *) coll->arg,
inputTypeId, targetTypeId, targetTypeMod,
ccontext, cformat, location);
newcoll->collOid = coll->collOid;
newcoll->location = coll->location;
return (Node *) newcoll;
}
pathtype = find_coercion_pathway(targetTypeId, inputTypeId, ccontext,
&funcId);
if (pathtype != COERCION_PATH_NONE)
{
if (pathtype != COERCION_PATH_RELABELTYPE)
{
Oid baseTypeId;
int32 baseTypeMod;
baseTypeMod = targetTypeMod;
baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
result = build_coercion_expression(node, pathtype, funcId,
baseTypeId, baseTypeMod,
ccontext, cformat, location);
if (targetTypeId != baseTypeId)
result = coerce_to_domain(result, baseTypeId, baseTypeMod,
targetTypeId,
ccontext, cformat, location,
true);
}
else
{
result = coerce_to_domain(node, InvalidOid, -1, targetTypeId,
ccontext, cformat, location,
false);
if (result == node)
{
RelabelType *r = makeRelabelType((Expr *) result,
targetTypeId, -1,
InvalidOid,
cformat);
r->location = location;
result = (Node *) r;
}
}
return result;
}
if (inputTypeId == RECORDOID &&
ISCOMPLEX(targetTypeId))
{
return coerce_record_to_complex(pstate, node, targetTypeId,
ccontext, cformat, location);
}
if (targetTypeId == RECORDOID &&
ISCOMPLEX(inputTypeId))
{
return node;
}
#ifdef NOT_USED
if (inputTypeId == RECORDARRAYOID &&
is_complex_array(targetTypeId))
{
}
#endif
if (targetTypeId == RECORDARRAYOID &&
is_complex_array(inputTypeId))
{
return node;
}
if (typeInheritsFrom(inputTypeId, targetTypeId)
|| typeIsOfTypedTable(inputTypeId, targetTypeId))
{
Oid baseTypeId = getBaseType(inputTypeId);
ConvertRowtypeExpr *r = makeNode(ConvertRowtypeExpr);
if (baseTypeId != inputTypeId)
{
RelabelType *rt = makeRelabelType((Expr *) node,
baseTypeId, -1,
InvalidOid,
COERCE_IMPLICIT_CAST);
rt->location = location;
node = (Node *) rt;
}
r->arg = (Expr *) node;
r->resulttype = targetTypeId;
r->convertformat = cformat;
r->location = location;
return (Node *) r;
}
elog(ERROR, "failed to find conversion function from %s to %s",
format_type_be(inputTypeId), format_type_be(targetTypeId));
return NULL;
}
Datum
stringTypeDatum(Type tp, char *string, int32 atttypmod)
{
Form_pg_type typform = (Form_pg_type) GETSTRUCT(tp);
Oid typinput = typform->typinput;
Oid typioparam = getTypeIOParam(tp);
//调用函数进行转换
return OidInputFunctionCall(typinput, string, typioparam, atttypmod);
}
Datum
OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
{
FmgrInfo flinfo;
//构造函数调用参数
fmgr_info(functionId, &flinfo);
return InputFunctionCall(&flinfo, str, typioparam, typmod);
}
Datum
InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
{
LOCAL_FCINFO(fcinfo, 3);
Datum result;
if (str == NULL && flinfo->fn_strict)
return (Datum) 0;
InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
fcinfo->args[0].value = CStringGetDatum(str);
fcinfo->args[0].isnull = false;
fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
fcinfo->args[1].isnull = false;
fcinfo->args[2].value = Int32GetDatum(typmod);
fcinfo->args[2].isnull = false;
//调用函数
result = FunctionCallInvoke(fcinfo);
if (str == NULL)
{
if (!fcinfo->isnull)
elog(ERROR, "input function %u returned non-NULL",
flinfo->fn_oid);
}
else
{
if (fcinfo->isnull)
elog(ERROR, "input function %u returned NULL",
flinfo->fn_oid);
}
return result;
}
Datum
int4in(PG_FUNCTION_ARGS)
{
char *num = PG_GETARG_CSTRING(0);
PG_RETURN_INT32(pg_strtoint32(num));
}
int32
pg_strtoint32(const char *s)
{
const char *ptr = s;
int32 tmp = 0;
bool neg = false;
while (likely(*ptr) && isspace((unsigned char) *ptr))
ptr++;
if (*ptr == '-')
{
ptr++;
neg = true;
}
else if (*ptr == '+')
ptr++;
if (unlikely(!isdigit((unsigned char) *ptr)))
goto invalid_syntax;
while (*ptr && isdigit((unsigned char) *ptr))//如'123',-1->-12->-123
{
int8 digit = (*ptr++ - '0');//获取数字
if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||//tmp*10
unlikely(pg_sub_s32_overflow(tmp, digit, &tmp)))//tmp - digit
goto out_of_range;
}
while (*ptr != '\0' && isspace((unsigned char) *ptr))
ptr++;
if (unlikely(*ptr != '\0'))
goto invalid_syntax;
if (!neg)
{
if (unlikely(tmp == PG_INT32_MIN))
goto out_of_range;
tmp = -tmp;//符号取反,-123->123
}
return tmp;
out_of_range:
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type %s",
s, "integer")));
invalid_syntax:
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"integer", s)));
return 0;
}
三、跟踪分析
SQL脚本
testdb=# select * from t_conv where id = '1';
跟踪分析
(gdb) b coerce_type
Breakpoint 1 at 0x6055f0: file parse_coerce.c, line 164.
(gdb) c
Continuing.
Breakpoint 1, coerce_type (pstate=0x2c89e30, node=0x2c8a678, inputTypeId=705, targetTypeId=23, targetTypeMod=-1,
ccontext=COERCION_IMPLICIT, cformat=COERCE_IMPLICIT_CAST, location=-1) at parse_coerce.c:164
164 if (targetTypeId == inputTypeId ||
(gdb)
输入参数,输入类型ID为705(unknown),目标类型为23(int4)
(gdb) p *pstate
$1 = {parentParseState = 0x0, p_sourcetext = 0x2c88e08 "select * from t_conv where id = '1';", p_rtable = 0x2c8a2a0,
p_joinexprs = 0x0, p_joinlist = 0x2c8a3a8, p_namespace = 0x2c8a328, p_lateral_active = false, p_ctenamespace = 0x0,
p_future_ctes = 0x0, p_parent_cte = 0x0, p_target_relation = 0x0, p_target_rangetblentry = 0x0, p_is_insert = false,
p_windowdefs = 0x0, p_expr_kind = EXPR_KIND_WHERE, p_next_resno = 2, p_multiassign_exprs = 0x0, p_locking_clause = 0x0,
p_locked_from_parent = false, p_resolve_unknowns = true, p_queryEnv = 0x0, p_hasAggs = false, p_hasWindowFuncs = false,
p_hasTargetSRFs = false, p_hasSubLinks = false, p_hasModifyingCTE = false, p_last_srf = 0x0, p_pre_columnref_hook = 0x0,
p_post_columnref_hook = 0x0, p_paramref_hook = 0x0, p_coerce_param_hook = 0x0, p_ref_hook_state = 0x0}
(gdb) p *node
$2 = {type = T_Const}
(gdb) p *(Const *)node
$3 = {xpr = {type = T_Const}, consttype = 705, consttypmod = -1, constcollid = 0, constlen = -2, constvalue = 46701384,
constisnull = false, constbyval = false, location = 32}
(gdb) x/1cb 46701384
0x2c89b48: 49 '1'
(gdb)
进入常量转换分支
(gdb) n
170 if (targetTypeId == ANYOID ||
(gdb)
171 targetTypeId == ANYELEMENTOID ||
(gdb)
188 if (targetTypeId == ANYARRAYOID ||
(gdb)
189 targetTypeId == ANYENUMOID ||
(gdb)
225 if (inputTypeId == UNKNOWNOID && IsA(node, Const))
(gdb)
244 Const *con = (Const *) node;
(gdb)
245 Const *newcon = makeNode(Const);
(gdb)
260 baseTypeMod = targetTypeMod;
(gdb) p *con
$4 = {xpr = {type = T_Const}, consttype = 705, consttypmod = -1, constcollid = 0, constlen = -2, constvalue = 46701384,
constisnull = false, constbyval = false, location = 32}
(gdb) n
261 baseTypeId = getBaseTypeAndTypmod(targetTypeId, &baseTypeMod);
(gdb)
272 if (baseTypeId == INTERVALOID)
(gdb)
275 inputTypeMod = -1;
(gdb)
277 baseType = typeidType(baseTypeId);
(gdb)
279 newcon->consttype = baseTypeId;
(gdb)
280 newcon->consttypmod = inputTypeMod;
(gdb)
281 newcon->constcollid = typeTypeCollation(baseType);
(gdb)
282 newcon->constlen = typeLen(baseType);
(gdb)
283 newcon->constbyval = typeByVal(baseType);
(gdb)
284 newcon->constisnull = con->constisnull;
(gdb)
291 newcon->location = con->location;
(gdb)
297 setup_parser_errposition_callback(&pcbstate, pstate, con->location);
(gdb) p *newcon
$5 = {xpr = {type = T_Const}, consttype = 23, consttypmod = -1, constcollid = 0, constlen = 4, constvalue = 0,
constisnull = false, constbyval = true, location = 32}
(gdb)
完成转换
(gdb) n
303 if (!con->constisnull)
(gdb)
305 DatumGetCString(con->constvalue),
(gdb)
304 newcon->constvalue = stringTypeDatum(baseType,
(gdb)
317 if (!con->constisnull && newcon->constlen == -1)
(gdb)
349 cancel_parser_errposition_callback(&pcbstate);
(gdb)
351 result = (Node *) newcon;
(gdb) p *newcon
$6 = {xpr = {type = T_Const}, consttype = 23, consttypmod = -1, constcollid = 0, constlen = 4, constvalue = 1,
constisnull = false, constbyval = true, location = 32}
(gdb) n
354 if (baseTypeId != targetTypeId)
(gdb)
361 ReleaseSysCache(baseType);
(gdb)
363 return result;
(gdb)
转换的实现
跟踪InputFunctionCall
(gdb) b InputFunctionCall
Breakpoint 2 at 0xa6fabe: file fmgr.c, line 1533.
(gdb) c
Continuing.
Breakpoint 2, InputFunctionCall (flinfo=0x7ffd8b1da1f0, str=0x2c89b48 "1", typioparam=23, typmod=-1) at fmgr.c:1533
1533 LOCAL_FCINFO(fcinfo, 3);
(gdb) n
1536 if (str == NULL && flinfo->fn_strict)
(gdb)
1539 InitFunctionCallInfoData(*fcinfo, flinfo, 3, InvalidOid, NULL, NULL);
(gdb)
1541 fcinfo->args[0].value = CStringGetDatum(str);
(gdb)
1542 fcinfo->args[0].isnull = false;
(gdb)
1543 fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
(gdb)
1544 fcinfo->args[1].isnull = false;
(gdb)
1545 fcinfo->args[2].value = Int32GetDatum(typmod);
(gdb)
1546 fcinfo->args[2].isnull = false;
(gdb)
1548 result = FunctionCallInvoke(fcinfo);
(gdb) p *fcinfo
$7 = {flinfo = 0x7ffd8b1da1f0, context = 0x0, resultinfo = 0x0, fncollation = 0, isnull = false, nargs = 3,
args = 0x7ffd8b1da180}
(gdb) p *fcinfo->flinfo
$8 = {fn_addr = 0x9694fc <int4in>, fn_oid = 42, fn_nargs = 1, fn_strict = true, fn_retset = false, fn_stats = 2 '\002',
fn_extra = 0x0, fn_mcxt = 0x2c88cf0, fn_expr = 0x0}
(gdb)
实现函数是int4in->pg_strtoint32
(gdb) b pg_strtoint32
Breakpoint 3 at 0x9b8b45: file numutils.c, line 201.
(gdb) c
Continuing.
Breakpoint 3, pg_strtoint32 (s=0x2c89b48 "123") at numutils.c:201
201 const char *ptr = s;
(gdb) n
202 int32 tmp = 0;
(gdb)
203 bool neg = false;
(gdb)
206 while (likely(*ptr) && isspace((unsigned char) *ptr))
(gdb)
210 if (*ptr == '-')
(gdb)
215 else if (*ptr == '+')
(gdb)
219 if (unlikely(!isdigit((unsigned char) *ptr)))
(gdb)
223 while (*ptr && isdigit((unsigned char) *ptr))
(gdb)
225 int8 digit = (*ptr++ - '0');
(gdb)
227 if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
(gdb) p digit
$9 = 1 '\001'
(gdb) n
228 unlikely(pg_sub_s32_overflow(tmp, digit, &tmp)))
(gdb) p tmp
$10 = 0
(gdb) n
227 if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
(gdb) p tmp
$11 = -1
(gdb) n
223 while (*ptr && isdigit((unsigned char) *ptr))
(gdb) p tmp
$12 = -1
(gdb) n
225 int8 digit = (*ptr++ - '0');
(gdb)
227 if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
(gdb) p digit
$13 = 2 '\002'
(gdb) n
228 unlikely(pg_sub_s32_overflow(tmp, digit, &tmp)))
(gdb)
227 if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
(gdb) p tmp
$14 = -12
(gdb) n
223 while (*ptr && isdigit((unsigned char) *ptr))
(gdb)
225 int8 digit = (*ptr++ - '0');
(gdb)
227 if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
(gdb) p digit
$15 = 3 '\003'
(gdb) n
228 unlikely(pg_sub_s32_overflow(tmp, digit, &tmp)))
(gdb)
227 if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
(gdb) p tmp
$16 = -123
(gdb) n
223 while (*ptr && isdigit((unsigned char) *ptr))
(gdb)
233 while (*ptr != '\0' && isspace((unsigned char) *ptr))
(gdb)
236 if (unlikely(*ptr != '\0'))
(gdb)
239 if (!neg)
(gdb)
242 if (unlikely(tmp == PG_INT32_MIN))
(gdb) p tmp
$17 = -123
(gdb) n
244 tmp = -tmp;
(gdb)
247 return tmp;
到此,相信大家对“怎么实现PostgreSQL中的类型转换”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!