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

Commit9511304

Browse files
committed
Rearrange the querytree representation of ORDER BY/GROUP BY/DISTINCT items
as per my recent proposal:1. Fold SortClause and GroupClause into a single node type SortGroupClause.We were already relying on them to be struct-equivalent, so using two nodetags wasn't accomplishing much except to get in the way of comparing itemswith equal().2. Add an "eqop" field to SortGroupClause to carry the associated equalityoperator. This is cheap for the parser to get at the same time it's lookingup the sort operator, and storing it eliminates the need for repeatednot-so-cheap lookups during planning. In future this will also let usrepresent GROUP/DISTINCT operations on datatypes that have hash opclassesbut no btree opclasses (ie, they have equality but no natural sort order).The previous representation simply didn't work for that, since its onlyindicator of comparison semantics was a sort operator.3. Add a hasDistinctOn boolean to struct Query to explicitly record whetherthe distinctClause came from DISTINCT or DISTINCT ON. This allows removingsome complicated and not 100% bulletproof code that attempted to figurethat out from the distinctClause alone.This patch doesn't in itself create any new capability, but it's necessaryinfrastructure for future attempts to use hash-based grouping for DISTINCTand UNION/INTERSECT/EXCEPT.
1 parent49f001d commit9511304

33 files changed

+747
-840
lines changed

‎src/backend/catalog/dependency.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/dependency.c,v 1.76 2008/06/14 18:04:33 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/dependency.c,v 1.77 2008/08/02 21:31:59 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1335,7 +1335,7 @@ find_expr_references_walker(Node *node,
13351335
}
13361336
return false;
13371337
}
1338-
if (IsA(node,Const))
1338+
elseif (IsA(node,Const))
13391339
{
13401340
Const*con= (Const*)node;
13411341
Oidobjoid;
@@ -1408,84 +1408,84 @@ find_expr_references_walker(Node *node,
14081408
}
14091409
return false;
14101410
}
1411-
if (IsA(node,Param))
1411+
elseif (IsA(node,Param))
14121412
{
14131413
Param*param= (Param*)node;
14141414

14151415
/* A parameter must depend on the parameter's datatype */
14161416
add_object_address(OCLASS_TYPE,param->paramtype,0,
14171417
context->addrs);
14181418
}
1419-
if (IsA(node,FuncExpr))
1419+
elseif (IsA(node,FuncExpr))
14201420
{
14211421
FuncExpr*funcexpr= (FuncExpr*)node;
14221422

14231423
add_object_address(OCLASS_PROC,funcexpr->funcid,0,
14241424
context->addrs);
14251425
/* fall through to examine arguments */
14261426
}
1427-
if (IsA(node,OpExpr))
1427+
elseif (IsA(node,OpExpr))
14281428
{
14291429
OpExpr*opexpr= (OpExpr*)node;
14301430

14311431
add_object_address(OCLASS_OPERATOR,opexpr->opno,0,
14321432
context->addrs);
14331433
/* fall through to examine arguments */
14341434
}
1435-
if (IsA(node,DistinctExpr))
1435+
elseif (IsA(node,DistinctExpr))
14361436
{
14371437
DistinctExpr*distinctexpr= (DistinctExpr*)node;
14381438

14391439
add_object_address(OCLASS_OPERATOR,distinctexpr->opno,0,
14401440
context->addrs);
14411441
/* fall through to examine arguments */
14421442
}
1443-
if (IsA(node,ScalarArrayOpExpr))
1443+
elseif (IsA(node,ScalarArrayOpExpr))
14441444
{
14451445
ScalarArrayOpExpr*opexpr= (ScalarArrayOpExpr*)node;
14461446

14471447
add_object_address(OCLASS_OPERATOR,opexpr->opno,0,
14481448
context->addrs);
14491449
/* fall through to examine arguments */
14501450
}
1451-
if (IsA(node,NullIfExpr))
1451+
elseif (IsA(node,NullIfExpr))
14521452
{
14531453
NullIfExpr*nullifexpr= (NullIfExpr*)node;
14541454

14551455
add_object_address(OCLASS_OPERATOR,nullifexpr->opno,0,
14561456
context->addrs);
14571457
/* fall through to examine arguments */
14581458
}
1459-
if (IsA(node,Aggref))
1459+
elseif (IsA(node,Aggref))
14601460
{
14611461
Aggref*aggref= (Aggref*)node;
14621462

14631463
add_object_address(OCLASS_PROC,aggref->aggfnoid,0,
14641464
context->addrs);
14651465
/* fall through to examine arguments */
14661466
}
1467-
if (is_subplan(node))
1467+
elseif (is_subplan(node))
14681468
{
14691469
/* Extra work needed here if we ever need this case */
14701470
elog(ERROR,"already-planned subqueries not supported");
14711471
}
1472-
if (IsA(node,RelabelType))
1472+
elseif (IsA(node,RelabelType))
14731473
{
14741474
RelabelType*relab= (RelabelType*)node;
14751475

14761476
/* since there is no function dependency, need to depend on type */
14771477
add_object_address(OCLASS_TYPE,relab->resulttype,0,
14781478
context->addrs);
14791479
}
1480-
if (IsA(node,CoerceViaIO))
1480+
elseif (IsA(node,CoerceViaIO))
14811481
{
14821482
CoerceViaIO*iocoerce= (CoerceViaIO*)node;
14831483

14841484
/* since there is no exposed function, need to depend on type */
14851485
add_object_address(OCLASS_TYPE,iocoerce->resulttype,0,
14861486
context->addrs);
14871487
}
1488-
if (IsA(node,ArrayCoerceExpr))
1488+
elseif (IsA(node,ArrayCoerceExpr))
14891489
{
14901490
ArrayCoerceExpr*acoerce= (ArrayCoerceExpr*)node;
14911491

@@ -1496,22 +1496,22 @@ find_expr_references_walker(Node *node,
14961496
context->addrs);
14971497
/* fall through to examine arguments */
14981498
}
1499-
if (IsA(node,ConvertRowtypeExpr))
1499+
elseif (IsA(node,ConvertRowtypeExpr))
15001500
{
15011501
ConvertRowtypeExpr*cvt= (ConvertRowtypeExpr*)node;
15021502

15031503
/* since there is no function dependency, need to depend on type */
15041504
add_object_address(OCLASS_TYPE,cvt->resulttype,0,
15051505
context->addrs);
15061506
}
1507-
if (IsA(node,RowExpr))
1507+
elseif (IsA(node,RowExpr))
15081508
{
15091509
RowExpr*rowexpr= (RowExpr*)node;
15101510

15111511
add_object_address(OCLASS_TYPE,rowexpr->row_typeid,0,
15121512
context->addrs);
15131513
}
1514-
if (IsA(node,RowCompareExpr))
1514+
elseif (IsA(node,RowCompareExpr))
15151515
{
15161516
RowCompareExpr*rcexpr= (RowCompareExpr*)node;
15171517
ListCell*l;
@@ -1528,14 +1528,25 @@ find_expr_references_walker(Node *node,
15281528
}
15291529
/* fall through to examine arguments */
15301530
}
1531-
if (IsA(node,CoerceToDomain))
1531+
elseif (IsA(node,CoerceToDomain))
15321532
{
15331533
CoerceToDomain*cd= (CoerceToDomain*)node;
15341534

15351535
add_object_address(OCLASS_TYPE,cd->resulttype,0,
15361536
context->addrs);
15371537
}
1538-
if (IsA(node,Query))
1538+
elseif (IsA(node,SortGroupClause))
1539+
{
1540+
SortGroupClause*sgc= (SortGroupClause*)node;
1541+
1542+
add_object_address(OCLASS_OPERATOR,sgc->eqop,0,
1543+
context->addrs);
1544+
if (OidIsValid(sgc->sortop))
1545+
add_object_address(OCLASS_OPERATOR,sgc->sortop,0,
1546+
context->addrs);
1547+
return false;
1548+
}
1549+
elseif (IsA(node,Query))
15391550
{
15401551
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
15411552
Query*query= (Query*)node;
@@ -1572,6 +1583,11 @@ find_expr_references_walker(Node *node,
15721583
}
15731584
}
15741585

1586+
/* query_tree_walker ignores ORDER BY etc, but we need those opers */
1587+
find_expr_references_walker((Node*)query->sortClause,context);
1588+
find_expr_references_walker((Node*)query->groupClause,context);
1589+
find_expr_references_walker((Node*)query->distinctClause,context);
1590+
15751591
/* Examine substructure of query */
15761592
context->rtables=lcons(query->rtable,context->rtables);
15771593
result=query_tree_walker(query,

‎src/backend/commands/analyze.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.123 2008/07/01 10:33:09 heikki Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.124 2008/08/02 21:31:59 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1490,40 +1490,28 @@ static bool
14901490
std_typanalyze(VacAttrStats*stats)
14911491
{
14921492
Form_pg_attributeattr=stats->attr;
1493-
Operatorfunc_operator;
1494-
Oideqopr=InvalidOid;
1495-
Oideqfunc=InvalidOid;
1496-
Oidltopr=InvalidOid;
1493+
Oidltopr;
1494+
Oideqopr;
14971495
StdAnalyzeData*mystats;
14981496

14991497
/* If the attstattarget column is negative, use the default value */
15001498
/* NB: it is okay to scribble on stats->attr since it's a copy */
15011499
if (attr->attstattarget<0)
15021500
attr->attstattarget=default_statistics_target;
15031501

1502+
/* Look for default "<" and "=" operators for column's type */
1503+
get_sort_group_operators(attr->atttypid,
1504+
false, false, false,
1505+
&ltopr,&eqopr,NULL);
1506+
15041507
/* If column has no "=" operator, we can't do much of anything */
1505-
func_operator=equality_oper(attr->atttypid, true);
1506-
if (func_operator!=NULL)
1507-
{
1508-
eqopr=oprid(func_operator);
1509-
eqfunc=oprfuncid(func_operator);
1510-
ReleaseSysCache(func_operator);
1511-
}
1512-
if (!OidIsValid(eqfunc))
1508+
if (!OidIsValid(eqopr))
15131509
return false;
15141510

1515-
/* Is there a "<" operator with suitable semantics? */
1516-
func_operator=ordering_oper(attr->atttypid, true);
1517-
if (func_operator!=NULL)
1518-
{
1519-
ltopr=oprid(func_operator);
1520-
ReleaseSysCache(func_operator);
1521-
}
1522-
15231511
/* Save the operator info for compute_stats routines */
15241512
mystats= (StdAnalyzeData*)palloc(sizeof(StdAnalyzeData));
15251513
mystats->eqopr=eqopr;
1526-
mystats->eqfunc=eqfunc;
1514+
mystats->eqfunc=get_opcode(eqopr);
15271515
mystats->ltopr=ltopr;
15281516
stats->extra_data=mystats;
15291517

‎src/backend/executor/nodeAgg.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
* Portions Copyright (c) 1994, Regents of the University of California
6262
*
6363
* IDENTIFICATION
64-
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.158 2008/05/12 00:00:49 alvherre Exp $
64+
* $PostgreSQL: pgsql/src/backend/executor/nodeAgg.c,v 1.159 2008/08/02 21:31:59 tgl Exp $
6565
*
6666
*-------------------------------------------------------------------------
6767
*/
@@ -1496,7 +1496,8 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
14961496

14971497
if (aggref->aggdistinct)
14981498
{
1499-
Oideq_function;
1499+
Oidlt_opr;
1500+
Oideq_opr;
15001501

15011502
/* We don't implement DISTINCT aggs in the HASHED case */
15021503
Assert(node->aggstrategy!=AGG_HASHED);
@@ -1524,9 +1525,11 @@ ExecInitAgg(Agg *node, EState *estate, int eflags)
15241525
* record it in the Aggref node ... or at latest, do it in the
15251526
* planner.
15261527
*/
1527-
eq_function=equality_oper_funcid(inputTypes[0]);
1528-
fmgr_info(eq_function,&(peraggstate->equalfn));
1529-
peraggstate->sortOperator=ordering_oper_opid(inputTypes[0]);
1528+
get_sort_group_operators(inputTypes[0],
1529+
true, true, false,
1530+
&lt_opr,&eq_opr,NULL);
1531+
fmgr_info(get_opcode(eq_opr),&(peraggstate->equalfn));
1532+
peraggstate->sortOperator=lt_opr;
15301533
peraggstate->sortstate=NULL;
15311534
}
15321535

‎src/backend/nodes/copyfuncs.c

Lines changed: 8 additions & 21 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-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.395 2008/07/16 01:30:22 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.396 2008/08/02 21:31:59 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1542,24 +1542,13 @@ _copyFkConstraint(FkConstraint *from)
15421542
returnnewnode;
15431543
}
15441544

1545-
staticSortClause*
1546-
_copySortClause(SortClause*from)
1545+
staticSortGroupClause*
1546+
_copySortGroupClause(SortGroupClause*from)
15471547
{
1548-
SortClause*newnode=makeNode(SortClause);
1549-
1550-
COPY_SCALAR_FIELD(tleSortGroupRef);
1551-
COPY_SCALAR_FIELD(sortop);
1552-
COPY_SCALAR_FIELD(nulls_first);
1553-
1554-
returnnewnode;
1555-
}
1556-
1557-
staticGroupClause*
1558-
_copyGroupClause(GroupClause*from)
1559-
{
1560-
GroupClause*newnode=makeNode(GroupClause);
1548+
SortGroupClause*newnode=makeNode(SortGroupClause);
15611549

15621550
COPY_SCALAR_FIELD(tleSortGroupRef);
1551+
COPY_SCALAR_FIELD(eqop);
15631552
COPY_SCALAR_FIELD(sortop);
15641553
COPY_SCALAR_FIELD(nulls_first);
15651554

@@ -1861,6 +1850,7 @@ _copyQuery(Query *from)
18611850
COPY_NODE_FIELD(intoClause);
18621851
COPY_SCALAR_FIELD(hasAggs);
18631852
COPY_SCALAR_FIELD(hasSubLinks);
1853+
COPY_SCALAR_FIELD(hasDistinctOn);
18641854
COPY_NODE_FIELD(rtable);
18651855
COPY_NODE_FIELD(jointree);
18661856
COPY_NODE_FIELD(targetList);
@@ -3586,11 +3576,8 @@ copyObject(void *from)
35863576
caseT_RangeTblEntry:
35873577
retval=_copyRangeTblEntry(from);
35883578
break;
3589-
caseT_SortClause:
3590-
retval=_copySortClause(from);
3591-
break;
3592-
caseT_GroupClause:
3593-
retval=_copyGroupClause(from);
3579+
caseT_SortGroupClause:
3580+
retval=_copySortGroupClause(from);
35943581
break;
35953582
caseT_RowMarkClause:
35963583
retval=_copyRowMarkClause(from);

‎src/backend/nodes/equalfuncs.c

Lines changed: 6 additions & 8 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-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.324 2008/07/16 01:30:22 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.325 2008/08/02 21:31:59 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -756,6 +756,7 @@ _equalQuery(Query *a, Query *b)
756756
COMPARE_NODE_FIELD(intoClause);
757757
COMPARE_SCALAR_FIELD(hasAggs);
758758
COMPARE_SCALAR_FIELD(hasSubLinks);
759+
COMPARE_SCALAR_FIELD(hasDistinctOn);
759760
COMPARE_NODE_FIELD(rtable);
760761
COMPARE_NODE_FIELD(jointree);
761762
COMPARE_NODE_FIELD(targetList);
@@ -1885,9 +1886,10 @@ _equalRangeTblEntry(RangeTblEntry *a, RangeTblEntry *b)
18851886
}
18861887

18871888
staticbool
1888-
_equalSortClause(SortClause*a,SortClause*b)
1889+
_equalSortGroupClause(SortGroupClause*a,SortGroupClause*b)
18891890
{
18901891
COMPARE_SCALAR_FIELD(tleSortGroupRef);
1892+
COMPARE_SCALAR_FIELD(eqop);
18911893
COMPARE_SCALAR_FIELD(sortop);
18921894
COMPARE_SCALAR_FIELD(nulls_first);
18931895

@@ -2515,12 +2517,8 @@ equal(void *a, void *b)
25152517
caseT_RangeTblEntry:
25162518
retval=_equalRangeTblEntry(a,b);
25172519
break;
2518-
caseT_SortClause:
2519-
retval=_equalSortClause(a,b);
2520-
break;
2521-
caseT_GroupClause:
2522-
/* GroupClause is equivalent to SortClause */
2523-
retval=_equalSortClause(a,b);
2520+
caseT_SortGroupClause:
2521+
retval=_equalSortGroupClause(a,b);
25242522
break;
25252523
caseT_RowMarkClause:
25262524
retval=_equalRowMarkClause(a,b);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp