Skip to content

Commit 92ed929

Browse files
author
Thomas G. Lockhart
committed
Allow floating point constants for "def_arg" numeric arguments.
Used in the generic "CREATE xxx" parsing. Do some automatic type conversion for inserts from other columns. Previous trouble with "resjunk" regression test remains for now.
1 parent fa83887 commit 92ed929

File tree

7 files changed

+278
-132
lines changed

7 files changed

+278
-132
lines changed

src/backend/parser/gram.y

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.12 1998/05/09 23:22:15 thomas Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.13 1998/07/08 14:04:09 thomas Exp $
1414
*
1515
* HISTORY
1616
* AUTHOR DATE MAJOR EVENT
@@ -204,8 +204,7 @@ Oid param_type(int t); /* used in parse_expr.c */
204204
%type <ival> sub_type
205205
%type <list> OptCreateAs, CreateAsList
206206
%type <node> CreateAsElement
207-
%type <value> NumConst
208-
%type <value> IntegerOnly
207+
%type <value> NumericOnly, FloatOnly, IntegerOnly
209208
%type <attr> event_object, attr
210209
%type <sortgroupby> groupby
211210
%type <sortgroupby> sortby
@@ -1180,6 +1179,20 @@ OptSeqElem: CACHE IntegerOnly
11801179
}
11811180
;
11821181

1182+
NumericOnly: FloatOnly { $$ = $1; }
1183+
| IntegerOnly { $$ = $1; }
1184+
1185+
FloatOnly: FCONST
1186+
{
1187+
$$ = makeFloat($1);
1188+
}
1189+
| '-' FCONST
1190+
{
1191+
$$ = makeFloat($2);
1192+
$$->val.dval = - $$->val.dval;
1193+
}
1194+
;
1195+
11831196
IntegerOnly: Iconst
11841197
{
11851198
$$ = makeInteger($1);
@@ -1384,7 +1397,7 @@ def_elem: def_name '=' def_arg
13841397

13851398
def_arg: ColId { $$ = (Node *)makeString($1); }
13861399
| all_Op { $$ = (Node *)makeString($1); }
1387-
| NumConst { $$ = (Node *)$1; /* already a Value */ }
1400+
| NumericOnly { $$ = (Node *)$1; }
13881401
| Sconst { $$ = (Node *)makeString($1); }
13891402
| SETOF ColId
13901403
{
@@ -4442,10 +4455,6 @@ ParamNo: PARAM
44424455
}
44434456
;
44444457

4445-
NumConst: Iconst { $$ = makeInteger($1); }
4446-
| FCONST { $$ = makeFloat($1); }
4447-
;
4448-
44494458
Iconst: ICONST { $$ = $1; };
44504459
Sconst: SCONST { $$ = $1; };
44514460
UserId: IDENT { $$ = $1; };

src/backend/parser/parse_clause.c

Lines changed: 17 additions & 15 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_clause.c,v 1.18 1998/06/05 03:49:18 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.19 1998/07/08 14:04:10 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -198,29 +198,31 @@ find_targetlist_entry(ParseState *pstate, SortGroupBy *sortgroupby, List *tlist)
198198
List *p_target = tlist;
199199
TargetEntry *tent = makeNode(TargetEntry);
200200

201-
202201
if (sortgroupby->range) {
203-
Attr *missingTarget = (Attr *)makeNode(Attr);
204-
missingTarget->type = T_Attr;
202+
Attr *missingAttr = (Attr *)makeNode(Attr);
203+
missingAttr->type = T_Attr;
205204

206-
missingTarget->relname = palloc(strlen(sortgroupby->range) + 1);
207-
strcpy(missingTarget->relname, sortgroupby->range);
205+
missingAttr->relname = palloc(strlen(sortgroupby->range) + 1);
206+
strcpy(missingAttr->relname, sortgroupby->range);
208207

209-
missingTarget->attrs = lcons(makeString(sortgroupby->name), NIL);
208+
missingAttr->attrs = lcons(makeString(sortgroupby->name), NIL);
210209

211-
transformTargetId(pstate, (Node*)missingTarget, tent, sortgroupby->name, TRUE);
210+
tent = transformTargetIdent(pstate, (Node *)missingAttr, tent,
211+
&missingAttr->relname, NULL,
212+
missingAttr->relname, TRUE);
212213
}
213214
else {
214-
Ident *missingTarget = (Ident *)makeNode(Ident);
215-
missingTarget->type = T_Ident;
215+
Ident *missingIdent = (Ident *)makeNode(Ident);
216+
missingIdent->type = T_Ident;
216217

217-
missingTarget->name = palloc(strlen(sortgroupby->name) + 1);
218-
strcpy(missingTarget->name, sortgroupby->name);
218+
missingIdent->name = palloc(strlen(sortgroupby->name) + 1);
219+
strcpy(missingIdent->name, sortgroupby->name);
219220

220-
transformTargetId(pstate, (Node*)missingTarget, tent, sortgroupby->name, TRUE);
221+
tent = transformTargetIdent(pstate, (Node *)missingIdent, tent,
222+
&missingIdent->name, NULL,
223+
missingIdent->name, TRUE);
221224
}
222225

223-
224226
/* Add to the end of the target list */
225227
while (lnext(p_target) != NIL) {
226228
p_target = lnext(p_target);
@@ -457,7 +459,7 @@ transformUnionClause(List *unionClause, List *targetlist)
457459
Node *expr;
458460

459461
expr = ((TargetEntry *)lfirst(next_target))->expr;
460-
expr = coerce_target_expr(NULL, expr, itype, otype);
462+
expr = CoerceTargetExpr(NULL, expr, itype, otype);
461463
if (expr == NULL)
462464
{
463465
elog(ERROR,"Unable to transform %s to %s"

src/backend/parser/parse_coerce.c

Lines changed: 3 additions & 1 deletion
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.2 1998/05/29 14:00:20 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.3 1998/07/08 14:04:10 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -273,6 +273,7 @@ TypeCategory(Oid inType)
273273

274274
case (INT2OID):
275275
case (INT4OID):
276+
case (INT8OID):
276277
case (FLOAT4OID):
277278
case (FLOAT8OID):
278279
case (CASHOID):
@@ -387,6 +388,7 @@ PromoteTypeToNext(Oid inType)
387388
break;
388389

389390
case (INT4OID):
391+
case (INT8OID):
390392
case (FLOAT4OID):
391393
result = FLOAT8OID;
392394
break;

src/backend/parser/parse_expr.c

Lines changed: 5 additions & 1 deletion
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.30 1998/06/15 19:28:54 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.31 1998/07/08 14:04:10 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -312,6 +312,10 @@ transformExpr(ParseState *pstate, Node *expr, int precedence)
312312
case T_Expr:
313313
case T_Var:
314314
case T_Const:
315+
/* T_Param comes from implicit function calls in INSERT/VALUE statements.
316+
* - thomas 1998-06-11
317+
*/
318+
case T_Param:
315319
{
316320
result = (Node *) expr;
317321
break;

src/backend/parser/parse_func.c

Lines changed: 7 additions & 6 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_func.c,v 1.19 1998/06/15 19:28:55 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_func.c,v 1.20 1998/07/08 14:04:10 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -153,7 +153,6 @@ ParseFuncOrColumn(ParseState *pstate, char *funcname, List *fargs,
153153
Oid *true_oid_array;
154154
Node *retval;
155155
bool retset;
156-
bool exists;
157156
bool attisset = false;
158157
Oid toid = (Oid) 0;
159158
Expr *expr;
@@ -370,16 +369,18 @@ ParseFuncOrColumn(ParseState *pstate, char *funcname, List *fargs,
370369
rettype = toid;
371370
retset = true;
372371
true_oid_array = oid_array;
373-
exists = true;
374372
}
375373
else
376374
{
375+
bool exists;
376+
377377
exists = func_get_detail(funcname, nargs, oid_array, &funcid,
378378
&rettype, &retset, &true_oid_array);
379-
}
379+
if (!exists)
380+
elog(ERROR, "No such function '%s' with the specified attributes",
381+
funcname);
380382

381-
if (!exists)
382-
elog(ERROR, "No such attribute or function '%s'", funcname);
383+
}
383384

384385
/* got it */
385386
funcnode = makeNode(Func);

src/backend/parser/parse_relation.c

Lines changed: 28 additions & 8 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_relation.c,v 1.11 1998/02/26 04:33:34 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_relation.c,v 1.12 1998/07/08 14:04:11 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -20,6 +20,7 @@
2020
#include "catalog/pg_type.h"
2121
#include "nodes/makefuncs.h"
2222
#include "parser/parse_relation.h"
23+
#include "parser/parse_coerce.h"
2324
#include "utils/acl.h"
2425
#include "utils/builtins.h"
2526
#include "utils/lsyscache.h"
@@ -371,9 +372,8 @@ attnumTypeId(Relation rd, int attid)
371372
return (rd->rd_att->attrs[attid - 1]->atttypid);
372373
}
373374

374-
/*
375-
* handleTargetColname -
376-
* use column names from insert
375+
/* handleTargetColname()
376+
* Use column names from insert.
377377
*/
378378
void
379379
handleTargetColname(ParseState *pstate, char **resname,
@@ -395,9 +395,8 @@ handleTargetColname(ParseState *pstate, char **resname,
395395
checkTargetTypes(pstate, *resname, refname, colname);
396396
}
397397

398-
/*
399-
* checkTargetTypes -
400-
* checks value and target column types
398+
/* checkTargetTypes()
399+
* Checks value and target column types.
401400
*/
402401
static void
403402
checkTargetTypes(ParseState *pstate, char *target_colname,
@@ -432,6 +431,27 @@ checkTargetTypes(ParseState *pstate, char *target_colname,
432431
resdomno_target = attnameAttNum(pstate->p_target_relation, target_colname);
433432
attrtype_target = attnumTypeId(pstate->p_target_relation, resdomno_target);
434433

434+
#if FALSE
435+
if ((attrtype_id != attrtype_target)
436+
|| (get_atttypmod(rte->relid, resdomno_id) !=
437+
get_atttypmod(pstate->p_target_relation->rd_id, resdomno_target)))
438+
{
439+
if (can_coerce_type(1, &attrtype_id, &attrtype_target))
440+
{
441+
Node *expr = coerce_type(pstate, expr, attrtype_id, attrtype_target);
442+
443+
elog(ERROR, "Type %s(%d) can be coerced to match target column %s(%d)",
444+
colname, get_atttypmod(rte->relid, resdomno_id),
445+
target_colname, get_atttypmod(pstate->p_target_relation->rd_id, resdomno_target));
446+
}
447+
else
448+
{
449+
elog(ERROR, "Type or size of %s(%d) does not match target column %s(%d)",
450+
colname, get_atttypmod(rte->relid, resdomno_id),
451+
target_colname, get_atttypmod(pstate->p_target_relation->rd_id, resdomno_target));
452+
}
453+
}
454+
#else
435455
if (attrtype_id != attrtype_target)
436456
elog(ERROR, "Type of %s does not match target column %s",
437457
colname, target_colname);
@@ -446,5 +466,5 @@ checkTargetTypes(ParseState *pstate, char *target_colname,
446466
get_atttypmod(pstate->p_target_relation->rd_id, resdomno_target))
447467
elog(ERROR, "Length of %s is longer than length of target column %s",
448468
colname, target_colname);
449-
469+
#endif
450470
}

0 commit comments

Comments
 (0)