|
10 | 10 | *
|
11 | 11 | *
|
12 | 12 | * IDENTIFICATION
|
13 |
| - * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.101 2000/11/16 22:30:24 tgl Exp $ |
| 13 | + * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.102 2000/12/23 18:49:41 tgl Exp $ |
14 | 14 | *
|
15 | 15 | *-------------------------------------------------------------------------
|
16 | 16 | */
|
@@ -1733,6 +1733,49 @@ make_limit(List *tlist, Plan *lefttree,
|
1733 | 1733 |
|
1734 | 1734 | copy_plan_costsize(plan,lefttree);
|
1735 | 1735 |
|
| 1736 | +/* |
| 1737 | + * If offset/count are constants, adjust the output rows count and costs |
| 1738 | + * accordingly. This is only a cosmetic issue if we are at top level, |
| 1739 | + * but if we are building a subquery then it's important to report |
| 1740 | + * correct info to the outer planner. |
| 1741 | + */ |
| 1742 | +if (limitOffset&&IsA(limitOffset,Const)) |
| 1743 | +{ |
| 1744 | +Const*limito= (Const*)limitOffset; |
| 1745 | +int32offset=DatumGetInt32(limito->constvalue); |
| 1746 | + |
| 1747 | +if (!limito->constisnull&&offset>0) |
| 1748 | +{ |
| 1749 | +if (offset>plan->plan_rows) |
| 1750 | +offset= (int32)plan->plan_rows; |
| 1751 | +if (plan->plan_rows>0) |
| 1752 | +plan->startup_cost+= |
| 1753 | +(plan->total_cost-plan->startup_cost) |
| 1754 | +* ((double)offset) /plan->plan_rows; |
| 1755 | +plan->plan_rows-=offset; |
| 1756 | +if (plan->plan_rows<1) |
| 1757 | +plan->plan_rows=1; |
| 1758 | +} |
| 1759 | +} |
| 1760 | +if (limitCount&&IsA(limitCount,Const)) |
| 1761 | +{ |
| 1762 | +Const*limitc= (Const*)limitCount; |
| 1763 | +int32count=DatumGetInt32(limitc->constvalue); |
| 1764 | + |
| 1765 | +if (!limitc->constisnull&&count >=0) |
| 1766 | +{ |
| 1767 | +if (count>plan->plan_rows) |
| 1768 | +count= (int32)plan->plan_rows; |
| 1769 | +if (plan->plan_rows>0) |
| 1770 | +plan->total_cost=plan->startup_cost+ |
| 1771 | +(plan->total_cost-plan->startup_cost) |
| 1772 | +* ((double)count) /plan->plan_rows; |
| 1773 | +plan->plan_rows=count; |
| 1774 | +if (plan->plan_rows<1) |
| 1775 | +plan->plan_rows=1; |
| 1776 | +} |
| 1777 | +} |
| 1778 | + |
1736 | 1779 | plan->state= (EState*)NULL;
|
1737 | 1780 | plan->targetlist=tlist;
|
1738 | 1781 | plan->qual=NIL;
|
|