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

Commit 085e559

Browse files
committed
Change LIMIT/OFFSET to use int8
Dhanaraj M
1 parent 796de9c commit 085e559

File tree

8 files changed

+89
-27
lines changed

8 files changed

+89
-27
lines changed

src/backend/executor/nodeLimit.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/nodeLimit.c,v 1.25 2006/03/05 15:58:26 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/nodeLimit.c,v 1.26 2006/07/26 00:34:48 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -23,6 +23,7 @@
2323

2424
#include "executor/executor.h"
2525
#include "executor/nodeLimit.h"
26+
#include "catalog/pg_type.h"
2627

2728
static void recompute_limits(LimitState *node);
2829

@@ -226,14 +227,24 @@ recompute_limits(LimitState *node)
226227
{
227228
ExprContext *econtext = node->ps.ps_ExprContext;
228229
bool isNull;
229-
230+
Oid type;
231+
230232
if (node->limitOffset)
231233
{
232-
node->offset =
233-
DatumGetInt32(ExecEvalExprSwitchContext(node->limitOffset,
234+
type = ((Const *) node->limitOffset->expr)->consttype;
235+
236+
if (type == INT8OID)
237+
node->offset =
238+
DatumGetInt64(ExecEvalExprSwitchContext(node->limitOffset,
234239
econtext,
235240
&isNull,
236241
NULL));
242+
else
243+
node->offset = DatumGetInt32(ExecEvalExprSwitchContext(node->limitOffset,
244+
econtext,
245+
&isNull,
246+
NULL));
247+
237248
/* Interpret NULL offset as no offset */
238249
if (isNull)
239250
node->offset = 0;
@@ -249,11 +260,21 @@ recompute_limits(LimitState *node)
249260
if (node->limitCount)
250261
{
251262
node->noCount = false;
252-
node->count =
253-
DatumGetInt32(ExecEvalExprSwitchContext(node->limitCount,
263+
type = ((Const *) node->limitCount->expr)->consttype;
264+
265+
if (type == INT8OID)
266+
node->count =
267+
DatumGetInt64(ExecEvalExprSwitchContext(node->limitCount,
254268
econtext,
255269
&isNull,
256270
NULL));
271+
else
272+
node->count = DatumGetInt32(ExecEvalExprSwitchContext(node->limitCount,
273+
econtext,
274+
&isNull,
275+
NULL));
276+
277+
257278
/* Interpret NULL count as no count (LIMIT ALL) */
258279
if (isNull)
259280
node->noCount = true;

src/backend/optimizer/plan/createplan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.214 2006/07/14 14:52:20 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.215 2006/07/26 00:34:48 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -2863,7 +2863,7 @@ make_setop(SetOpCmd cmd, Plan *lefttree,
28632863
*/
28642864
Limit *
28652865
make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
2866-
int offset_est, int count_est)
2866+
int64 offset_est, int64 count_est)
28672867
{
28682868
Limit *node = makeNode(Limit);
28692869
Plan *plan = &node->plan;

src/backend/optimizer/plan/planner.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.203 2006/07/14 14:52:20 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.204 2006/07/26 00:34:48 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -59,7 +59,7 @@ static Plan *inheritance_planner(PlannerInfo *root);
5959
static Plan *grouping_planner(PlannerInfo *root, double tuple_fraction);
6060
static double preprocess_limit(PlannerInfo *root,
6161
double tuple_fraction,
62-
int *offset_est, int *count_est);
62+
int64 *offset_est, int64 *count_est);
6363
static bool choose_hashed_grouping(PlannerInfo *root, double tuple_fraction,
6464
Path *cheapest_path, Path *sorted_path,
6565
double dNumGroups, AggClauseCounts *agg_counts);
@@ -631,8 +631,8 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
631631
{
632632
Query *parse = root->parse;
633633
List *tlist = parse->targetList;
634-
int offset_est = 0;
635-
int count_est = 0;
634+
int64 offset_est = 0;
635+
int64 count_est = 0;
636636
Plan *result_plan;
637637
List *current_pathkeys;
638638
List *sort_pathkeys;
@@ -1080,7 +1080,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
10801080
*/
10811081
static double
10821082
preprocess_limit(PlannerInfo *root, double tuple_fraction,
1083-
int *offset_est, int *count_est)
1083+
int64 *offset_est, int64 *count_est)
10841084
{
10851085
Query *parse = root->parse;
10861086
Node *est;
@@ -1105,7 +1105,7 @@ preprocess_limit(PlannerInfo *root, double tuple_fraction,
11051105
}
11061106
else
11071107
{
1108-
*count_est = DatumGetInt32(((Const *) est)->constvalue);
1108+
*count_est = DatumGetInt64(((Const *) est)->constvalue);
11091109
if (*count_est <= 0)
11101110
*count_est = 1; /* force to at least 1 */
11111111
}
@@ -1128,7 +1128,8 @@ preprocess_limit(PlannerInfo *root, double tuple_fraction,
11281128
}
11291129
else
11301130
{
1131-
*offset_est = DatumGetInt32(((Const *) est)->constvalue);
1131+
*offset_est = DatumGetInt64(((Const *) est)->constvalue);
1132+
11321133
if (*offset_est < 0)
11331134
*offset_est = 0; /* less than 0 is same as 0 */
11341135
}

src/backend/parser/parse_clause.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.153 2006/07/14 14:52:21 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.154 2006/07/26 00:34:48 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1090,7 +1090,7 @@ transformLimitClause(ParseState *pstate, Node *clause,
10901090

10911091
qual = transformExpr(pstate, clause);
10921092

1093-
qual = coerce_to_integer(pstate, qual, constructName);
1093+
qual = coerce_to_integer64(pstate, qual, constructName);
10941094

10951095
/*
10961096
* LIMIT can't refer to any vars or aggregates of the current query; we

src/backend/parser/parse_coerce.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.141 2006/07/14 14:52:22 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_coerce.c,v 2.142 2006/07/26 00:34:48 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -821,7 +821,7 @@ coerce_to_boolean(ParseState *pstate, Node *node,
821821

822822
/* coerce_to_integer()
823823
* Coerce an argument of a construct that requires integer input
824-
* (LIMIT, OFFSET, etc). Also check that input is not a set.
824+
* Also check that input is not a set.
825825
*
826826
* Returns the possibly-transformed node tree.
827827
*
@@ -857,7 +857,45 @@ coerce_to_integer(ParseState *pstate, Node *node,
857857

858858
return node;
859859
}
860+
861+
/* coerce_to_integer64()
862+
* Coerce an argument of a construct that requires integer input
863+
* (LIMIT, OFFSET). Also check that input is not a set.
864+
*
865+
* Returns the possibly-transformed node tree.
866+
*
867+
* As with coerce_type, pstate may be NULL if no special unknown-Param
868+
* processing is wanted.
869+
*/
870+
Node *
871+
coerce_to_integer64(ParseState *pstate, Node *node,
872+
const char *constructName)
873+
{
874+
Oid inputTypeId = exprType(node);
875+
876+
if (inputTypeId != INT8OID)
877+
{
878+
node = coerce_to_target_type(pstate, node, inputTypeId,
879+
INT8OID, -1, COERCION_ASSIGNMENT,
880+
COERCE_IMPLICIT_CAST);
881+
if (node == NULL)
882+
ereport(ERROR,
883+
(errcode(ERRCODE_DATATYPE_MISMATCH),
884+
/* translator: first %s is name of a SQL construct, eg LIMIT */
885+
errmsg("argument of %s must be type integer, not type %s",
886+
constructName, format_type_be(inputTypeId))));
887+
}
860888

889+
if (expression_returns_set(node))
890+
ereport(ERROR,
891+
(errcode(ERRCODE_DATATYPE_MISMATCH),
892+
/* translator: %s is name of a SQL construct, eg LIMIT */
893+
errmsg("argument of %s must not return a set",
894+
constructName)));
895+
896+
return node;
897+
}
898+
861899

862900
/* select_common_type()
863901
* Determine the common supertype of a list of input expression types.

src/include/nodes/execnodes.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.153 2006/07/13 16:49:19 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.154 2006/07/26 00:34:48 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1325,11 +1325,11 @@ typedef struct LimitState
13251325
PlanState ps; /* its first field is NodeTag */
13261326
ExprState *limitOffset; /* OFFSET parameter, or NULL if none */
13271327
ExprState *limitCount; /* COUNT parameter, or NULL if none */
1328-
long offset; /* current OFFSET value */
1329-
long count; /* current COUNT, if any */
1328+
int64 offset; /* current OFFSET value */
1329+
int64 count; /* current COUNT, if any */
13301330
bool noCount; /* if true, ignore count */
13311331
LimitStateCond lstate; /* state machine status, as above */
1332-
long position; /* 1-based index of last tuple returned */
1332+
int64 position; /* 1-based index of last tuple returned */
13331333
TupleTableSlot *subSlot; /* tuple last obtained from subplan */
13341334
} LimitState;
13351335

src/include/optimizer/planmain.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.93 2006/07/01 18:38:33 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.94 2006/07/26 00:34:48 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -55,7 +55,7 @@ extern Material *make_material(Plan *lefttree);
5555
extern Plan *materialize_finished_plan(Plan *subplan);
5656
extern Unique *make_unique(Plan *lefttree, List *distinctList);
5757
extern Limit *make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
58-
int offset_est, int count_est);
58+
int64 offset_est, int64 count_est);
5959
extern SetOp *make_setop(SetOpCmd cmd, Plan *lefttree,
6060
List *distinctList, AttrNumber flagColIdx);
6161
extern Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);

src/include/parser/parse_coerce.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/parser/parse_coerce.h,v 1.63 2006/07/13 16:49:19 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/parser/parse_coerce.h,v 1.64 2006/07/26 00:34:48 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -57,7 +57,9 @@ extern Node *coerce_to_boolean(ParseState *pstate, Node *node,
5757
const char *constructName);
5858
extern Node *coerce_to_integer(ParseState *pstate, Node *node,
5959
const char *constructName);
60-
60+
extern Node *coerce_to_integer64(ParseState *pstate, Node *node,
61+
const char *constructName);
62+
6163
extern Oid select_common_type(List *typeids, const char *context);
6264
extern Node *coerce_to_common_type(ParseState *pstate, Node *node,
6365
Oid targetTypeId,

0 commit comments

Comments
 (0)