Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ec646db

Browse files
committed
Create a 'type cache' that keeps track of the data needed for any particular
datatype by array_eq and array_cmp; use this to solve problems with memory leaks in array indexing support. The parser's equality_oper and ordering_oper routines also use the cache. Change the operator search algorithms to look for appropriate btree or hash index opclasses, instead of assuming operators named '<' or '=' have the right semantics. (ORDER BY ASC/DESC now also look at opclasses, instead of assuming '<' and '>' are the right things.) Add several more index opclasses so that there is no regression in functionality for base datatypes. initdb forced due to catalog additions.
1 parent d89578c commit ec646db

40 files changed

+964
-491
lines changed

src/backend/commands/analyze.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.58 2003/08/04 02:39:58 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.59 2003/08/17 19:58:04 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -390,7 +390,6 @@ examine_attribute(Relation onerel, int attnum)
390390
{
391391
Form_pg_attribute attr = onerel->rd_att->attrs[attnum - 1];
392392
Operator func_operator;
393-
Oid oprrest;
394393
HeapTuple typtuple;
395394
Oid eqopr = InvalidOid;
396395
Oid eqfunc = InvalidOid;
@@ -409,12 +408,8 @@ examine_attribute(Relation onerel, int attnum)
409408
func_operator = equality_oper(attr->atttypid, true);
410409
if (func_operator != NULL)
411410
{
412-
oprrest = ((Form_pg_operator) GETSTRUCT(func_operator))->oprrest;
413-
if (oprrest == F_EQSEL)
414-
{
415-
eqopr = oprid(func_operator);
416-
eqfunc = oprfuncid(func_operator);
417-
}
411+
eqopr = oprid(func_operator);
412+
eqfunc = oprfuncid(func_operator);
418413
ReleaseSysCache(func_operator);
419414
}
420415
if (!OidIsValid(eqfunc))
@@ -447,9 +442,7 @@ examine_attribute(Relation onerel, int attnum)
447442
func_operator = ordering_oper(attr->atttypid, true);
448443
if (func_operator != NULL)
449444
{
450-
oprrest = ((Form_pg_operator) GETSTRUCT(func_operator))->oprrest;
451-
if (oprrest == F_SCALARLTSEL)
452-
ltopr = oprid(func_operator);
445+
ltopr = oprid(func_operator);
453446
ReleaseSysCache(func_operator);
454447
}
455448
stats->ltopr = ltopr;

src/backend/commands/indexcmds.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.105 2003/08/04 02:39:58 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.106 2003/08/17 19:58:04 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -529,7 +529,8 @@ GetDefaultOpClass(Oid attrType, Oid accessMethodId)
529529
* than one exact match, then someone put bogus entries in pg_opclass.
530530
*
531531
* The initial search is done by namespace.c so that we only consider
532-
* opclasses visible in the current namespace search path.
532+
* opclasses visible in the current namespace search path. (See also
533+
* typcache.c, which applies the same logic, but over all opclasses.)
533534
*/
534535
for (opclass = OpclassGetCandidates(accessMethodId);
535536
opclass != NULL;

src/backend/commands/opclasscmds.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/commands/opclasscmds.c,v 1.17 2003/08/04 02:39:58 momjian Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/commands/opclasscmds.c,v 1.18 2003/08/17 19:58:04 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -261,7 +261,9 @@ DefineOpClass(CreateOpClassStmt *stmt)
261261

262262
/*
263263
* If we are creating a default opclass, check there isn't one
264-
* already. (XXX should we restrict this test to visible opclasses?)
264+
* already. (Note we do not restrict this test to visible opclasses;
265+
* this ensures that typcache.c can find unique solutions to its
266+
* questions.)
265267
*/
266268
if (stmt->isDefault)
267269
{

src/backend/nodes/copyfuncs.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.263 2003/08/08 21:41:43 momjian Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.264 2003/08/17 19:58:05 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1402,11 +1402,12 @@ _copyTypeName(TypeName *from)
14021402
return newnode;
14031403
}
14041404

1405-
static SortGroupBy *
1406-
_copySortGroupBy(SortGroupBy *from)
1405+
static SortBy *
1406+
_copySortBy(SortBy *from)
14071407
{
1408-
SortGroupBy *newnode = makeNode(SortGroupBy);
1408+
SortBy *newnode = makeNode(SortBy);
14091409

1410+
COPY_SCALAR_FIELD(sortby_kind);
14101411
COPY_NODE_FIELD(useOp);
14111412
COPY_NODE_FIELD(node);
14121413

@@ -2924,8 +2925,8 @@ copyObject(void *from)
29242925
case T_TypeCast:
29252926
retval = _copyTypeCast(from);
29262927
break;
2927-
case T_SortGroupBy:
2928-
retval = _copySortGroupBy(from);
2928+
case T_SortBy:
2929+
retval = _copySortBy(from);
29292930
break;
29302931
case T_RangeSubselect:
29312932
retval = _copyRangeSubselect(from);

src/backend/nodes/equalfuncs.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.207 2003/08/08 21:41:43 momjian Exp $
21+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.208 2003/08/17 19:58:05 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1480,8 +1480,9 @@ _equalTypeCast(TypeCast *a, TypeCast *b)
14801480
}
14811481

14821482
static bool
1483-
_equalSortGroupBy(SortGroupBy *a, SortGroupBy *b)
1483+
_equalSortBy(SortBy *a, SortBy *b)
14841484
{
1485+
COMPARE_SCALAR_FIELD(sortby_kind);
14851486
COMPARE_NODE_FIELD(useOp);
14861487
COMPARE_NODE_FIELD(node);
14871488

@@ -2045,8 +2046,8 @@ equal(void *a, void *b)
20452046
case T_TypeCast:
20462047
retval = _equalTypeCast(a, b);
20472048
break;
2048-
case T_SortGroupBy:
2049-
retval = _equalSortGroupBy(a, b);
2049+
case T_SortBy:
2050+
retval = _equalSortBy(a, b);
20502051
break;
20512052
case T_RangeSubselect:
20522053
retval = _equalRangeSubselect(a, b);

src/backend/optimizer/plan/createplan.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.154 2003/08/11 20:46:46 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.155 2003/08/17 19:58:05 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -629,8 +629,9 @@ create_unique_plan(Query *root, UniquePath *best_path)
629629

630630
tle = get_tle_by_resno(my_tlist, groupColIdx[groupColPos]);
631631
Assert(tle != NULL);
632-
sortList = addTargetToSortList(NULL, tle, sortList,
633-
my_tlist, NIL, false);
632+
sortList = addTargetToSortList(NULL, tle,
633+
sortList, my_tlist,
634+
SORTBY_ASC, NIL, false);
634635
}
635636
plan = (Plan *) make_sort_from_sortclauses(root, my_tlist,
636637
subplan, sortList);

src/backend/optimizer/plan/planner.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.159 2003/08/04 02:40:01 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.160 2003/08/17 19:58:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1327,7 +1327,9 @@ hash_safe_grouping(Query *parse)
13271327
Operator optup;
13281328
bool oprcanhash;
13291329

1330-
optup = equality_oper(tle->resdom->restype, false);
1330+
optup = equality_oper(tle->resdom->restype, true);
1331+
if (!optup)
1332+
return false;
13311333
oprcanhash = ((Form_pg_operator) GETSTRUCT(optup))->oprcanhash;
13321334
ReleaseSysCache(optup);
13331335
if (!oprcanhash)

src/backend/parser/gram.y

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.428 2003/08/04 02:40:01 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.429 2003/08/17 19:58:05 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -115,7 +115,7 @@ static void doNegateFloat(Value *v);
115115

116116
TypeName *typnam;
117117
DefElem *defelt;
118-
SortGroupBy *sortgroupby;
118+
SortBy *sortby;
119119
JoinExpr *jexpr;
120120
IndexElem *ielem;
121121
Alias *alias;
@@ -189,7 +189,7 @@ static void doNegateFloat(Value *v);
189189
database_name access_method_clause access_method attr_name
190190
index_name name function_name file_name
191191

192-
%type <list> func_name handler_name qual_Op qual_all_Op OptUseOp
192+
%type <list> func_name handler_name qual_Op qual_all_Op
193193
opt_class opt_validator
194194

195195
%type <range> qualified_name OptConstrFromTable
@@ -278,7 +278,7 @@ static void doNegateFloat(Value *v);
278278
%type <value> NumericOnly FloatOnly IntegerOnly
279279
%type <columnref> columnref
280280
%type <alias> alias_clause
281-
%type <sortgroupby> sortby
281+
%type <sortby> sortby
282282
%type <ielem> index_elem
283283
%type <node> table_ref
284284
%type <jexpr> joined_table
@@ -4577,21 +4577,34 @@ sortby_list:
45774577
| sortby_list ',' sortby { $$ = lappend($1, $3); }
45784578
;
45794579

4580-
sortby: a_expr OptUseOp
4580+
sortby: a_expr USING qual_all_Op
45814581
{
4582-
$$ = makeNode(SortGroupBy);
4582+
$$ = makeNode(SortBy);
45834583
$$->node = $1;
4584-
$$->useOp = $2;
4584+
$$->sortby_kind = SORTBY_USING;
4585+
$$->useOp = $3;
4586+
}
4587+
| a_expr ASC
4588+
{
4589+
$$ = makeNode(SortBy);
4590+
$$->node = $1;
4591+
$$->sortby_kind = SORTBY_ASC;
4592+
$$->useOp = NIL;
4593+
}
4594+
| a_expr DESC
4595+
{
4596+
$$ = makeNode(SortBy);
4597+
$$->node = $1;
4598+
$$->sortby_kind = SORTBY_DESC;
4599+
$$->useOp = NIL;
4600+
}
4601+
| a_expr
4602+
{
4603+
$$ = makeNode(SortBy);
4604+
$$->node = $1;
4605+
$$->sortby_kind = SORTBY_ASC; /* default */
4606+
$$->useOp = NIL;
45854607
}
4586-
;
4587-
4588-
OptUseOp: USING qual_all_Op { $$ = $2; }
4589-
| ASC
4590-
{ $$ = makeList1(makeString("<")); }
4591-
| DESC
4592-
{ $$ = makeList1(makeString(">")); }
4593-
| /*EMPTY*/
4594-
{ $$ = makeList1(makeString("<")); /*default*/ }
45954608
;
45964609

45974610

src/backend/parser/parse_clause.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.121 2003/08/07 19:20:22 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.122 2003/08/17 19:58:05 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1295,15 +1295,17 @@ transformSortClause(ParseState *pstate,
12951295

12961296
foreach(olitem, orderlist)
12971297
{
1298-
SortGroupBy *sortby = lfirst(olitem);
1298+
SortBy *sortby = lfirst(olitem);
12991299
TargetEntry *tle;
13001300

13011301
tle = findTargetlistEntry(pstate, sortby->node,
13021302
targetlist, ORDER_CLAUSE);
13031303

13041304
sortlist = addTargetToSortList(pstate, tle,
13051305
sortlist, targetlist,
1306-
sortby->useOp, resolveUnknown);
1306+
sortby->sortby_kind,
1307+
sortby->useOp,
1308+
resolveUnknown);
13071309
}
13081310

13091311
return sortlist;
@@ -1409,7 +1411,7 @@ transformDistinctClause(ParseState *pstate, List *distinctlist,
14091411
{
14101412
*sortClause = addTargetToSortList(pstate, tle,
14111413
*sortClause, targetlist,
1412-
NIL, true);
1414+
SORTBY_ASC, NIL, true);
14131415

14141416
/*
14151417
* Probably, the tle should always have been added at the
@@ -1457,7 +1459,8 @@ addAllTargetsToSortList(ParseState *pstate, List *sortlist,
14571459
if (!tle->resdom->resjunk)
14581460
sortlist = addTargetToSortList(pstate, tle,
14591461
sortlist, targetlist,
1460-
NIL, resolveUnknown);
1462+
SORTBY_ASC, NIL,
1463+
resolveUnknown);
14611464
}
14621465
return sortlist;
14631466
}
@@ -1478,7 +1481,8 @@ addAllTargetsToSortList(ParseState *pstate, List *sortlist,
14781481
List *
14791482
addTargetToSortList(ParseState *pstate, TargetEntry *tle,
14801483
List *sortlist, List *targetlist,
1481-
List *opname, bool resolveUnknown)
1484+
int sortby_kind, List *sortby_opname,
1485+
bool resolveUnknown)
14821486
{
14831487
/* avoid making duplicate sortlist entries */
14841488
if (!targetIsInSortList(tle, sortlist))
@@ -1499,13 +1503,25 @@ addTargetToSortList(ParseState *pstate, TargetEntry *tle,
14991503

15001504
sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
15011505

1502-
if (opname)
1503-
sortcl->sortop = compatible_oper_opid(opname,
1504-
restype,
1505-
restype,
1506-
false);
1507-
else
1508-
sortcl->sortop = ordering_oper_opid(restype);
1506+
switch (sortby_kind)
1507+
{
1508+
case SORTBY_ASC:
1509+
sortcl->sortop = ordering_oper_opid(restype);
1510+
break;
1511+
case SORTBY_DESC:
1512+
sortcl->sortop = reverse_ordering_oper_opid(restype);
1513+
break;
1514+
case SORTBY_USING:
1515+
Assert(sortby_opname != NIL);
1516+
sortcl->sortop = compatible_oper_opid(sortby_opname,
1517+
restype,
1518+
restype,
1519+
false);
1520+
break;
1521+
default:
1522+
elog(ERROR, "unrecognized sortby_kind: %d", sortby_kind);
1523+
break;
1524+
}
15091525

15101526
sortlist = lappend(sortlist, sortcl);
15111527
}

0 commit comments

Comments
 (0)