1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-10 17:42:29 +03:00

Change parse-time representation of float literals (which include oversize

integers) to be strings instead of 'double'.  We convert from string form
to internal representation only after type resolution has determined the
correct type for the constant.  This eliminates loss-of-precision worries
and gets rid of the change in behavior seen at 17 digits with the
previous kluge.
This commit is contained in:
Tom Lane
2000-02-21 18:47:12 +00:00
parent ee97d103cc
commit 393f313227
12 changed files with 186 additions and 186 deletions

View File

@@ -11,7 +11,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.147 2000/02/20 02:14:58 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.148 2000/02/21 18:47:02 tgl Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
@@ -78,6 +78,7 @@ static Node *makeRowExpr(char *opr, List *largs, List *rargs);
static void mapTargetColumns(List *source, List *target);
static void param_type_init(Oid *typev, int nargs);
static Node *doNegate(Node *n);
static void doNegateFloat(Value *v);
/* old versions of flex define this as a macro */
#if defined(yywrap)
@@ -88,7 +89,6 @@ static Node *doNegate(Node *n);
%union
{
double dval;
int ival;
char chr;
char *str;
@@ -352,9 +352,8 @@ static Node *doNegate(Node *n);
UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
/* Special keywords, not in the query language - see the "lex" file */
%token <str> IDENT, SCONST, Op
%token <str> IDENT, FCONST, SCONST, Op
%token <ival> ICONST, PARAM
%token <dval> FCONST
/* these are not real. they are here so that they get generated as #define's*/
%token OP
@@ -1567,7 +1566,7 @@ FloatOnly: FCONST
| '-' FCONST
{
$$ = makeFloat($2);
$$->val.dval = - $$->val.dval;
doNegateFloat($$);
}
;
@@ -1722,16 +1721,11 @@ TriggerFuncArgs: TriggerFuncArg
TriggerFuncArg: ICONST
{
char *s = (char *) palloc (256);
char *s = (char *) palloc(64);
sprintf (s, "%d", $1);
$$ = s;
}
| FCONST
{
char *s = (char *) palloc (256);
sprintf (s, "%g", $1);
$$ = s;
}
| FCONST { $$ = $1; }
| Sconst { $$ = $1; }
| IDENT { $$ = $1; }
;
@@ -5183,7 +5177,7 @@ AexprConst: Iconst
{
A_Const *n = makeNode(A_Const);
n->val.type = T_Float;
n->val.val.dval = $1;
n->val.val.str = $1;
$$ = (Node *)n;
}
| Sconst
@@ -5621,7 +5615,8 @@ Oid param_type(int t)
* a few cycles throughout the parse and rewrite stages if we collapse
* the minus into the constant sooner rather than later...
*/
static Node *doNegate(Node *n)
static Node *
doNegate(Node *n)
{
if (IsA(n, A_Const))
{
@@ -5634,10 +5629,30 @@ static Node *doNegate(Node *n)
}
if (con->val.type == T_Float)
{
con->val.val.dval = -con->val.val.dval;
doNegateFloat(&con->val);
return n;
}
}
return makeA_Expr(OP, "-", NULL, n);
}
static void
doNegateFloat(Value *v)
{
char *oldval = v->val.str;
Assert(IsA(v, Float));
if (*oldval == '+')
oldval++;
if (*oldval == '-')
v->val.str = oldval; /* just strip the '-' */
else
{
char *newval = (char *) palloc(strlen(oldval) + 2);
*newval = '-';
strcpy(newval+1, oldval);
v->val.str = newval;
}
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.69 2000/02/20 21:32:10 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.70 2000/02/21 18:47:02 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -726,23 +726,19 @@ parser_typecast_constant(Value *expr, TypeName *typename)
switch (nodeTag(expr))
{
case T_String:
const_string = DatumGetPointer(expr->val.str);
break;
case T_Integer:
string_palloced = true;
const_string = int4out(expr->val.ival);
break;
case T_Float:
string_palloced = true;
const_string = float8out(&expr->val.dval);
case T_String:
const_string = expr->val.str;
break;
case T_Null:
isNull = true;
break;
default:
elog(ERROR,
"Cannot cast this expression to type '%s'",
elog(ERROR, "Cannot cast this expression to type '%s'",
typename->name);
}

View File

@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/scan.l,v 1.64 2000/02/19 04:17:25 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/scan.l,v 1.65 2000/02/21 18:47:02 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -324,7 +324,7 @@ other .
}
{param} {
yylval.ival = atoi((char*)&yytext[1]);
yylval.ival = atol((char*)&yytext[1]);
return PARAM;
}
@@ -332,46 +332,21 @@ other .
char* endptr;
errno = 0;
yylval.ival = strtol((char *)yytext,&endptr,10);
yylval.ival = strtol((char *)yytext, &endptr, 10);
if (*endptr != '\0' || errno == ERANGE)
{
errno = 0;
#if 0
yylval.dval = strtod(((char *)yytext),&endptr);
if (*endptr != '\0' || errno == ERANGE)
elog(ERROR,"Bad integer input '%s'",yytext);
CheckFloat8Val(yylval.dval);
elog(NOTICE,"Integer input '%s' is out of range; promoted to float", yytext);
return FCONST;
#endif
/* integer too large, treat it as a float */
yylval.str = pstrdup((char*)yytext);
return SCONST;
return FCONST;
}
return ICONST;
}
{decimal} {
char* endptr;
if (strlen((char *)yytext) <= 17)
{
errno = 0;
yylval.dval = strtod((char *)yytext,&endptr);
if (*endptr != '\0' || errno == ERANGE)
elog(ERROR,"Bad float input '%s'",yytext);
CheckFloat8Val(yylval.dval);
return FCONST;
}
yylval.str = pstrdup((char*)yytext);
return SCONST;
return FCONST;
}
{real} {
char* endptr;
errno = 0;
yylval.dval = strtod((char *)yytext,&endptr);
if (*endptr != '\0' || errno == ERANGE)
elog(ERROR,"Bad float input '%s'",yytext);
CheckFloat8Val(yylval.dval);
yylval.str = pstrdup((char*)yytext);
return FCONST;
}