Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfd19a35

Browse files
committed
Revise parse_coerce() to handle coercion of int and float
constants, not only string constants, at parse time. Get rid ofparser_typecast2(), which is bogus and redundant...
1 parent5a76a94 commitfd19a35

File tree

6 files changed

+114
-239
lines changed

6 files changed

+114
-239
lines changed

‎src/backend/parser/parse_coerce.c

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.21 1999/07/17 20:17:23 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.22 1999/08/05 02:33:53 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -35,74 +35,101 @@ coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId,
3535
int32atttypmod)
3636
{
3737
Node*result=NULL;
38-
TypetargetType;
39-
Oidinfunc;
40-
Datumval;
4138

42-
if (targetTypeId==InvalidOid)
39+
if (targetTypeId==InvalidOid||
40+
targetTypeId==inputTypeId)
41+
{
42+
/* no conversion needed */
4343
result=node;
44-
elseif (inputTypeId!=targetTypeId)
44+
}
45+
elseif (IS_BINARY_COMPATIBLE(inputTypeId,targetTypeId))
4546
{
46-
47-
/*
48-
* one of the known-good transparent conversions? then drop
49-
* through...
47+
/* no work if one of the known-good transparent conversions */
48+
result=node;
49+
}
50+
elseif (inputTypeId==UNKNOWNOID&&IsA(node,Const))
51+
{
52+
/* Input is a string constant with previously undetermined type.
53+
* Apply the target type's typinput function to it to produce
54+
* a constant of the target type.
55+
*
56+
* NOTE: this case cannot be folded together with the other
57+
* constant-input case, since the typinput function does not
58+
* necessarily behave the same as a type conversion function.
59+
* For example, int4's typinput function will reject "1.2",
60+
* whereas float-to-int type conversion will round to integer.
5061
*/
51-
if (IS_BINARY_COMPATIBLE(inputTypeId,targetTypeId))
52-
result=node;
62+
Const*con= (Const*)node;
63+
TypetargetType=typeidType(targetTypeId);
64+
char*val;
5365

66+
/* We know the source constant is really of type 'text' */
67+
val=textout((text*)con->constvalue);
68+
69+
/* now make a new const node */
70+
con=makeNode(Const);
71+
con->consttype=targetTypeId;
72+
con->constlen=typeLen(targetType);
73+
con->constvalue=stringTypeDatum(targetType,val,atttypmod);
74+
con->constisnull= false;
75+
con->constbyval=typeByVal(targetType);
76+
con->constisset= false;
77+
78+
pfree(val);
79+
80+
result= (Node*)con;
81+
}
82+
else
83+
{
5484
/*
55-
* if not unknown input type, try for explicit conversion using
56-
* functions...
85+
* Otherwise, find the appropriate type conversion function
86+
* (caller should have determined that there is one), and
87+
* generate an expression tree representing run-time
88+
* application of the conversion function.
5789
*/
58-
elseif (inputTypeId!=UNKNOWNOID)
59-
{
90+
FuncCall*n=makeNode(FuncCall);
91+
TypetargetType=typeidType(targetTypeId);
6092

61-
/*
62-
* We already know there is a function which will do this, so
63-
* let's use it
64-
*/
65-
FuncCall*n=makeNode(FuncCall);
93+
n->funcname=typeTypeName(targetType);
94+
n->args=lcons(node,NIL);
6695

67-
n->funcname=typeidTypeName(targetTypeId);
68-
n->args=lcons(node,NIL);
96+
result=transformExpr(pstate, (Node*)n,EXPR_COLUMN_FIRST);
6997

70-
result=transformExpr(pstate, (Node*)n,EXPR_COLUMN_FIRST);
71-
}
72-
else
98+
/*
99+
* If the input is a constant, apply the type conversion function
100+
* now instead of delaying to runtime. (This could someday be
101+
* done in a downstream constant-expression-simplifier, but we
102+
* can save cycles in the rewriter if we do it here.)
103+
*
104+
* XXX there are cases where we probably shouldn't do this,
105+
* such as coercing text 'now' to datetime? Need a way to
106+
* know whether type conversion function is cacheable...
107+
*/
108+
if (IsA(node,Const))
73109
{
74-
if (nodeTag(node)==T_Const)
75-
{
76-
Const*con= (Const*)node;
77-
78-
val= (Datum)textout((structvarlena*)con->constvalue);
79-
targetType=typeidType(targetTypeId);
80-
infunc=typeInfunc(targetType);
81-
con=makeNode(Const);
82-
con->consttype=targetTypeId;
83-
con->constlen=typeLen(targetType);
84-
85-
/*
86-
* Use "-1" for varchar() type. For char(), we need to pad
87-
* out the type with the proper number of spaces. This
88-
* was a major problem for DEFAULT string constants to
89-
* char() types.
90-
*/
91-
con->constvalue= (Datum)fmgr(infunc,
92-
val,
93-
typeTypElem(targetType),
94-
(targetTypeId!=BPCHAROID) ?-1 :atttypmod);
95-
con->constisnull= false;
96-
con->constbyval=typeByVal(targetType);
97-
con->constisset= false;
98-
result= (Node*)con;
99-
}
100-
else
101-
result=node;
110+
Const*con= (Const*)node;
111+
OidconvertFuncid;
112+
Datumval;
113+
114+
Assert(IsA(result,Expr)&&
115+
((Expr*)result)->opType==FUNC_EXPR);
116+
117+
/* Convert the given constant */
118+
convertFuncid= ((Func*) (((Expr*)result)->oper))->funcid;
119+
val= (Datum)fmgr(convertFuncid,con->constvalue);
120+
121+
/* now make a new const node */
122+
con=makeNode(Const);
123+
con->consttype=targetTypeId;
124+
con->constlen=typeLen(targetType);
125+
con->constvalue=val;
126+
con->constisnull= false;
127+
con->constbyval=typeByVal(targetType);
128+
con->constisset= false;
129+
130+
result= (Node*)con;
102131
}
103132
}
104-
else
105-
result=node;
106133

107134
returnresult;
108135
}

‎src/backend/parser/parse_expr.c

Lines changed: 10 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.55 1999/07/19 00:26:19 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.56 1999/08/05 02:33:53 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -631,16 +631,16 @@ exprTypmod(Node *expr)
631631
return-1;
632632
}
633633

634+
/*
635+
* Produce an appropriate Const node from a constant value produced
636+
* by the parser and an explicit type name to cast to.
637+
*/
634638
staticNode*
635639
parser_typecast(Value*expr,TypeName*typename,int32atttypmod)
636640
{
637-
/* check for passing non-ints */
638641
Const*adt;
639642
Datumlcp;
640643
Typetp;
641-
chartype_string[NAMEDATALEN];
642-
int32len;
643-
char*cp=NULL;
644644
char*const_string=NULL;
645645
boolstring_palloced= false;
646646

@@ -659,180 +659,30 @@ parser_typecast(Value *expr, TypeName *typename, int32 atttypmod)
659659
break;
660660
default:
661661
elog(ERROR,
662-
"parser_typecast: cannot cast this expression to type '%s'",
662+
"parser_typecast: cannot cast this expression to type '%s'",
663663
typename->name);
664664
}
665665

666666
if (typename->arrayBounds!=NIL)
667667
{
668+
chartype_string[NAMEDATALEN+2];
669+
668670
sprintf(type_string,"_%s",typename->name);
669671
tp= (Type)typenameType(type_string);
670672
}
671673
else
672674
tp= (Type)typenameType(typename->name);
673675

674-
len=typeLen(tp);
675-
676-
cp=stringTypeString(tp,const_string,atttypmod);
677-
678-
if (!typeByVal(tp))
679-
lcp=PointerGetDatum(cp);
680-
else
681-
{
682-
switch (len)
683-
{
684-
case1:
685-
lcp=Int8GetDatum(cp);
686-
break;
687-
case2:
688-
lcp=Int16GetDatum(cp);
689-
break;
690-
case4:
691-
lcp=Int32GetDatum(cp);
692-
break;
693-
default:
694-
lcp=PointerGetDatum(cp);
695-
break;
696-
}
697-
}
698-
699-
adt=makeConst(typeTypeId(tp),
700-
len,
701-
(Datum)lcp,
702-
false,
703-
typeByVal(tp),
704-
false,/* not a set */
705-
true/* is cast */ );
706-
707-
if (string_palloced)
708-
pfree(const_string);
709-
710-
return (Node*)adt;
711-
}
712-
713-
714-
/* parser_typecast2()
715-
* Convert (only) constants to specified type.
716-
*/
717-
Node*
718-
parser_typecast2(Node*expr,OidexprType,Typetp,int32atttypmod)
719-
{
720-
/* check for passing non-ints */
721-
Const*adt;
722-
Datumlcp;
723-
int32len=typeLen(tp);
724-
char*cp=NULL;
725-
726-
char*const_string=NULL;
727-
boolstring_palloced= false;
728-
729-
Assert(IsA(expr,Const));
730-
731-
switch (exprType)
732-
{
733-
case0:/* NULL */
734-
break;
735-
caseINT4OID:/* int4 */
736-
const_string= (char*)palloc(256);
737-
string_palloced= true;
738-
sprintf(const_string,"%d",
739-
(int) ((Const*)expr)->constvalue);
740-
break;
741-
caseNAMEOID:/* name */
742-
const_string= (char*)palloc(256);
743-
string_palloced= true;
744-
sprintf(const_string,"%s",
745-
(char*) ((Const*)expr)->constvalue);
746-
break;
747-
caseCHAROID:/* char */
748-
const_string= (char*)palloc(256);
749-
string_palloced= true;
750-
sprintf(const_string,"%c",
751-
(char) ((Const*)expr)->constvalue);
752-
break;
753-
caseFLOAT4OID:/* float4 */
754-
{
755-
float32floatVal=DatumGetFloat32(((Const*)expr)->constvalue);
756-
757-
const_string= (char*)palloc(256);
758-
string_palloced= true;
759-
sprintf(const_string,"%f",*floatVal);
760-
break;
761-
}
762-
caseFLOAT8OID:/* float8 */
763-
{
764-
float64floatVal=DatumGetFloat64(((Const*)expr)->constvalue);
765-
766-
const_string= (char*)palloc(256);
767-
string_palloced= true;
768-
sprintf(const_string,"%f",*floatVal);
769-
break;
770-
}
771-
caseCASHOID:/* money */
772-
const_string= (char*)palloc(256);
773-
string_palloced= true;
774-
sprintf(const_string,"%ld",
775-
(long) ((Const*)expr)->constvalue);
776-
break;
777-
caseTEXTOID:/* text */
778-
const_string=DatumGetPointer(((Const*)expr)->constvalue);
779-
const_string= (char*)textout((structvarlena*)const_string);
780-
break;
781-
caseUNKNOWNOID:/* unknown */
782-
const_string=DatumGetPointer(((Const*)expr)->constvalue);
783-
const_string= (char*)textout((structvarlena*)const_string);
784-
break;
785-
default:
786-
elog(ERROR,"unknown type %u",exprType);
787-
}
788-
789-
if (!exprType)
790-
{
791-
adt=makeConst(typeTypeId(tp),
792-
(Size)0,
793-
(Datum)NULL,
794-
true,/* isnull */
795-
false,/* was omitted */
796-
false,/* not a set */
797-
true/* is cast */ );
798-
return (Node*)adt;
799-
}
800-
801-
cp=stringTypeString(tp,const_string,atttypmod);
802-
803-
if (!typeByVal(tp))
804-
lcp=PointerGetDatum(cp);
805-
else
806-
{
807-
switch (len)
808-
{
809-
case1:
810-
lcp=Int8GetDatum(cp);
811-
break;
812-
case2:
813-
lcp=Int16GetDatum(cp);
814-
break;
815-
case4:
816-
lcp=Int32GetDatum(cp);
817-
break;
818-
default:
819-
lcp=PointerGetDatum(cp);
820-
break;
821-
}
822-
}
676+
lcp=stringTypeDatum(tp,const_string,atttypmod);
823677

824678
adt=makeConst(typeTypeId(tp),
825-
(Size)len,
679+
typeLen(tp),
826680
(Datum)lcp,
827681
false,
828682
typeByVal(tp),
829683
false,/* not a set */
830684
true/* is cast */ );
831685

832-
/*
833-
* printf("adt %s : %u %d %d\n",CString(expr),typeTypeId(tp) ,
834-
* len,cp);
835-
*/
836686
if (string_palloced)
837687
pfree(const_string);
838688

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp