summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/prep/prepkeyset.c
blob: 180637d099ec6a7b49c9189eaef4f0b38c1714f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*-------------------------------------------------------------------------
 *
 * prepkeyset.c
 *	  Special preperation for keyset queries.
 *
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "optimizer/planmain.h"

/*
 * Node_Copy
 *		  a macro to simplify calling of copyObject on the specified field
 */
#define Node_Copy(from, newnode, field) newnode->field = copyObject(from->field)

bool		_use_keyset_query_optimizer = FALSE;

#ifdef ENABLE_KEY_SET_QUERY

static int	inspectOpNode(Expr *expr);
static int	inspectAndNode(Expr *expr);
static int	inspectOrNode(Expr *expr);
static int	TotalExpr;

/**********************************************************************
 *	 This routine transforms query trees with the following form:
 *		 SELECT a,b, ... FROM one_table WHERE
 *		  (v1 = const1 AND v2 = const2 [ vn = constn ]) OR
 *		  (v1 = const3 AND v2 = const4 [ vn = constn ]) OR
 *		  (v1 = const5 AND v2 = const6 [ vn = constn ]) OR
 *						   ...
 *		  [(v1 = constn AND v2 = constn [ vn = constn ])]
 *
 *							   into
 *
 *		 SELECT a,b, ... FROM one_table WHERE
 *		  (v1 = const1 AND v2 = const2 [ vn = constn ]) UNION
 *		 SELECT a,b, ... FROM one_table WHERE
 *		  (v1 = const3 AND v2 = const4 [ vn = constn ]) UNION
 *		 SELECT a,b, ... FROM one_table WHERE
 *		  (v1 = const5 AND v2 = const6 [ vn = constn ]) UNION
 *						   ...
 *		 SELECT a,b, ... FROM one_table WHERE
 *		  [(v1 = constn AND v2 = constn [ vn = constn ])]
 *
 *
 *	 To qualify for transformation the query must not be a sub select,
 *	 a HAVING, or a GROUP BY.	It must be a single table and have KSQO
 *	 set to 'on'.
 *
 *	 The primary use of this transformation is to avoid the exponrntial
 *	 memory consumption of cnfify() and to make use of index access
 *	 methods.
 *
 *		  daveh@insightdist.com   1998-08-31
 *
 *	 May want to also prune out duplicate terms.
 **********************************************************************/
void
transformKeySetQuery(Query *origNode)
{
	/* Qualify as a key set query candidate  */
	if (_use_keyset_query_optimizer == FALSE ||
		origNode->groupClause ||
		origNode->havingQual ||
		origNode->hasAggs ||
		origNode->utilityStmt ||
		origNode->unionClause ||
		origNode->unionall ||
		origNode->hasSubLinks ||
		origNode->commandType != CMD_SELECT)
		return;

	/* Qualify single table query	*/
	if (length(origNode->rtable) != 1)
		return;

	/* Sorry about the global, not worth passing around		*/
	/* 9 expressions seems like a good number.	More than 9 */
	/* and it starts to slow down quite a bit				*/
	TotalExpr = 0;
	/*************************/
	/* Qualify where clause */
	/*************************/
	if (!inspectOrNode((Expr *) origNode->jointree->quals) || TotalExpr < 9)
		return;

	/* Copy essential elements into a union node */
	while (((Expr *) origNode->jointree->quals)->opType == OR_EXPR)
	{
		Query	   *unionNode = makeNode(Query);
		List	   *qualargs = ((Expr *) origNode->jointree->quals)->args;

		unionNode->commandType = origNode->commandType;
		unionNode->resultRelation = origNode->resultRelation;
		unionNode->isPortal = origNode->isPortal;
		unionNode->isBinary = origNode->isBinary;

		Node_Copy(origNode, unionNode, distinctClause);
		Node_Copy(origNode, unionNode, sortClause);
		Node_Copy(origNode, unionNode, rtable);
		origNode->jointree->quals = NULL;		/* avoid unnecessary
												 * copying */
		Node_Copy(origNode, unionNode, jointree);
		Node_Copy(origNode, unionNode, targetList);

		/* Pull up Expr =  */
		unionNode->jointree->quals = lsecond(qualargs);

		/* Pull up balance of tree	*/
		origNode->jointree->quals = lfirst(qualargs);

		origNode->unionClause = lappend(origNode->unionClause, unionNode);
	}
	return;
}




static int
/**********************************************************************
 *	 Checks for 1 or more OR terms w/ 1 or more AND terms.
 *	 AND terms must be equal in size.
 *	 Returns the number of each AND term.
 **********************************************************************/
inspectOrNode(Expr *expr)
{
	int			rc;
	Expr	   *firstExpr,
			   *secondExpr;

	if (!(expr && nodeTag(expr) == T_Expr && expr->opType == OR_EXPR))
		return 0;

	firstExpr = lfirst(expr->args);
	secondExpr = lsecond(expr->args);
	if (nodeTag(firstExpr) != T_Expr || nodeTag(secondExpr) != T_Expr)
		return 0;

	if (firstExpr->opType == OR_EXPR && secondExpr->opType == AND_EXPR)
	{
		if ((rc = inspectOrNode(firstExpr)) == 0)
			return 0;

		return (rc == inspectAndNode(secondExpr)) ? rc : 0;
	}
	else if (firstExpr->opType == AND_EXPR && secondExpr->opType == AND_EXPR)
	{
		if ((rc = inspectAndNode(firstExpr)) == 0)
			return 0;

		return (rc == inspectAndNode(secondExpr)) ? rc : 0;

	}

	return 0;
}


static int
/**********************************************************************
 *	Check for one or more AND terms.   Each sub-term must be a T_Const
 *	T_Var expression.
 *	Returns the number of AND terms.
 **********************************************************************/
inspectAndNode(Expr *expr)
{
	int			rc;
	Expr	   *firstExpr,
			   *secondExpr;

	if (!(expr && nodeTag(expr) == T_Expr && expr->opType == AND_EXPR))
		return 0;

	firstExpr = lfirst(expr->args);
	secondExpr = lsecond(expr->args);
	if (nodeTag(firstExpr) != T_Expr || nodeTag(secondExpr) != T_Expr)
		return 0;

	if (firstExpr->opType == AND_EXPR &&
		secondExpr->opType == OP_EXPR && inspectOpNode(secondExpr))
	{
		rc = inspectAndNode(firstExpr);
		return ((rc) ? (rc + 1) : 0);	/* Add up the AND nodes */
	}
	else if (firstExpr->opType == OP_EXPR && inspectOpNode(firstExpr) &&
			 secondExpr->opType == OP_EXPR && inspectOpNode(secondExpr))
		return 1;

	return 0;
}


static int
/******************************************************************
 *	Return TRUE if T_Var = T_Const, else FALSE
 *	Actually it does not test for =.	Need to do this!
 ******************************************************************/
inspectOpNode(Expr *expr)
{
	Expr	   *firstExpr,
			   *secondExpr;

	if (nodeTag(expr) != T_Expr || expr->opType != OP_EXPR)
		return FALSE;

	TotalExpr++;

	firstExpr = lfirst(expr->args);
	secondExpr = lsecond(expr->args);
	return (firstExpr && secondExpr && nodeTag(firstExpr) == T_Var && nodeTag(secondExpr) == T_Const);
}

#endif	 /* ENABLE_KEY_SET_QUERY */