forked from EnterpriseDB/mongo_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_query.c
742 lines (653 loc) · 20.7 KB
/
mongo_query.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*-------------------------------------------------------------------------
*
* mongo_query.c
* Foreign-data wrapper for remote MongoDB servers
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
*
* Portions Copyright (c) 2004-2014, EnterpriseDB Corporation.
*
* Portions Copyright (c) 2012–2014 Citus Data, Inc.
*
* IDENTIFICATION
* mongo_query.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "mongo_wrapper.h"
#ifdef META_DRIVER
#include "mongoc.h"
#else
#include "mongo.h"
#endif
#include <bson.h>
#include <json.h>
#include <bits.h>
#include "mongo_fdw.h"
#include "mongo_query.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/relation.h"
#include "optimizer/var.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/lsyscache.h"
#include "utils/numeric.h"
#include "utils/timestamp.h"
/* Local functions forward declarations */
static Expr * FindArgumentOfType(List *argumentList, NodeTag argumentType);
static char * MongoOperatorName(const char *operatorName);
static List * EqualityOperatorList(List *operatorList);
static List * UniqueColumnList(List *operatorList);
static List * ColumnOperatorList(Var *column, List *operatorList);
static void AppendConstantValue(BSON *queryDocument, const char *keyName,
Const *constant);
static void AppendParamValue(BSON *queryDocument, const char *keyName,
Param *paramNode, ForeignScanState *scanStateNode);
/*
* ApplicableOpExpressionList walks over all filter clauses that relate to this
* foreign table, and chooses applicable clauses that we know we can translate
* into Mongo queries. Currently, these clauses include comparison expressions
* that have a column and a constant as arguments. For example, "o_orderdate >=
* date '1994-01-01' + interval '1' year" is an applicable expression.
*/
List *
ApplicableOpExpressionList(RelOptInfo *baserel)
{
List *opExpressionList = NIL;
List *restrictInfoList = baserel->baserestrictinfo;
ListCell *restrictInfoCell = NULL;
foreach(restrictInfoCell, restrictInfoList)
{
RestrictInfo *restrictInfo = (RestrictInfo *) lfirst(restrictInfoCell);
Expr *expression = restrictInfo->clause;
NodeTag expressionType = 0;
OpExpr *opExpression = NULL;
char *operatorName = NULL;
char *mongoOperatorName = NULL;
List *argumentList = NIL;
Var *column = NULL;
Const *constant = NULL;
bool equalsOperator = false;
bool constantIsArray = false;
Param *paramNode = NULL;
/* we only support operator expressions */
expressionType = nodeTag(expression);
if (expressionType != T_OpExpr)
{
continue;
}
opExpression = (OpExpr *) expression;
operatorName = get_opname(opExpression->opno);
/* we only support =, <, >, <=, >=, and <> operators */
if (strncmp(operatorName, EQUALITY_OPERATOR_NAME, NAMEDATALEN) == 0)
{
equalsOperator = true;
}
mongoOperatorName = MongoOperatorName(operatorName);
if (!equalsOperator && mongoOperatorName == NULL)
{
continue;
}
/*
* We only support simple binary operators that compare a column against
* a constant. If the expression is a tree, we don't recurse into it.
*/
argumentList = opExpression->args;
column = (Var *) FindArgumentOfType(argumentList, T_Var);
constant = (Const *) FindArgumentOfType(argumentList, T_Const);
paramNode = (Param *) FindArgumentOfType(argumentList, T_Param);
/*
* We don't push down operators where the constant is an array, since
* conditional operators for arrays in MongoDB aren't properly defined.
* For example, {similar_products : [ "B0009S4IJW", "6301964144" ]}
* finds results that are equal to the array, but {similar_products:
* {$gte: [ "B0009S4IJW", "6301964144" ]}} returns an empty set.
*/
if (constant != NULL)
{
Oid constantArrayTypeId = get_element_type(constant->consttype);
if (constantArrayTypeId != InvalidOid)
{
constantIsArray = true;
}
}
if (column != NULL && constant != NULL && !constantIsArray)
{
opExpressionList = lappend(opExpressionList, opExpression);
}
if (column != NULL && paramNode != NULL)
{
opExpressionList = lappend(opExpressionList, opExpression);
}
}
return opExpressionList;
}
/*
* FindArgumentOfType walks over the given argument list, looks for an argument
* with the given type, and returns the argument if it is found.
*/
static Expr *
FindArgumentOfType(List *argumentList, NodeTag argumentType)
{
Expr *foundArgument = NULL;
ListCell *argumentCell = NULL;
foreach(argumentCell, argumentList)
{
Expr *argument = (Expr *) lfirst(argumentCell);
if (nodeTag(argument) == argumentType)
{
foundArgument = argument;
break;
}
}
return foundArgument;
}
/*
* QueryDocument takes in the applicable operator expressions for a relation and
* converts these expressions into equivalent queries in MongoDB. For now, this
* function can only transform simple comparison expressions, and returns these
* transformed expressions in a BSON document. For example, simple expressions
* "l_shipdate >= date '1994-01-01' AND l_shipdate < date '1995-01-01'" become
* "l_shipdate: { $gte: new Date(757382400000), $lt: new Date(788918400000) }".
*/
BSON *
QueryDocument(Oid relationId, List *opExpressionList, ForeignScanState *scanStateNode)
{
List *equalityOperatorList = NIL;
List *comparisonOperatorList = NIL;
List *columnList = NIL;
ListCell *equalityOperatorCell = NULL;
ListCell *columnCell = NULL;
BSON *queryDocument = NULL;
queryDocument = BsonCreate();
/*
* We distinguish between equality expressions and others since we need to
* insert the latter (<, >, <=, >=, <>) as separate sub-documents into the
* BSON query object.
*/
equalityOperatorList = EqualityOperatorList(opExpressionList);
comparisonOperatorList = list_difference(opExpressionList, equalityOperatorList);
/* append equality expressions to the query */
foreach(equalityOperatorCell, equalityOperatorList)
{
OpExpr *equalityOperator = (OpExpr *) lfirst(equalityOperatorCell);
Oid columnId = InvalidOid;
char *columnName = NULL;
Const *constant = NULL;
Param *paramNode = NULL;
List *argumentList = equalityOperator->args;
Var *column = (Var *) FindArgumentOfType(argumentList, T_Var);
constant = (Const *) FindArgumentOfType(argumentList, T_Const);
paramNode = (Param *) FindArgumentOfType(argumentList, T_Param);
columnId = column->varattno;
columnName = get_relid_attribute_name(relationId, columnId);
if (constant != NULL)
AppendConstantValue(queryDocument, columnName, constant);
else
AppendParamValue(queryDocument, columnName, paramNode, scanStateNode);
}
/*
* For comparison expressions, we need to group them by their columns and
* append all expressions that correspond to a column as one sub-document.
* Otherwise, even when we have two expressions to define the upper- and
* lower-bound of a range, Mongo uses only one of these expressions during
* an index search.
*/
columnList = UniqueColumnList(comparisonOperatorList);
/* append comparison expressions, grouped by columns, to the query */
foreach(columnCell, columnList)
{
Var *column = (Var *) lfirst(columnCell);
Oid columnId = InvalidOid;
char *columnName = NULL;
List *columnOperatorList = NIL;
ListCell *columnOperatorCell = NULL;
BSON r;
columnId = column->varattno;
columnName = get_relid_attribute_name(relationId, columnId);
/* find all expressions that correspond to the column */
columnOperatorList = ColumnOperatorList(column, comparisonOperatorList);
/* for comparison expressions, start a sub-document */
BsonAppendStartObject(queryDocument, columnName, &r);
foreach(columnOperatorCell, columnOperatorList)
{
OpExpr *columnOperator = (OpExpr *) lfirst(columnOperatorCell);
char *operatorName = NULL;
char *mongoOperatorName = NULL;
List *argumentList = columnOperator->args;
Const *constant = (Const *) FindArgumentOfType(argumentList, T_Const);
operatorName = get_opname(columnOperator->opno);
mongoOperatorName = MongoOperatorName(operatorName);
#ifdef META_DRIVER
AppendConstantValue(&r, mongoOperatorName, constant);
#else
AppendConstantValue(queryDocument, mongoOperatorName, constant);
#endif
}
BsonAppendFinishObject(queryDocument, &r);
}
if (!BsonFinish(queryDocument))
{
#ifdef META_DRIVER
ereport(ERROR, (errmsg("could not create document for query"),
errhint("BSON flags: %d", queryDocument->flags)));
#else
ereport(ERROR, (errmsg("could not create document for query"),
errhint("BSON error: %d", queryDocument->err)));
#endif
}
return queryDocument;
}
/*
* MongoOperatorName takes in the given PostgreSQL comparison operator name, and
* returns its equivalent in MongoDB.
*/
static char *
MongoOperatorName(const char *operatorName)
{
const char *mongoOperatorName = NULL;
const int32 nameCount = 5;
static const char *nameMappings[][2] = { { "<", "$lt" },
{ ">", "$gt" },
{ "<=", "$lte" },
{ ">=", "$gte" },
{ "<>", "$ne" } };
int32 nameIndex = 0;
for (nameIndex = 0; nameIndex < nameCount; nameIndex++)
{
const char *pgOperatorName = nameMappings[nameIndex][0];
if (strncmp(pgOperatorName, operatorName, NAMEDATALEN) == 0)
{
mongoOperatorName = nameMappings[nameIndex][1];
break;
}
}
return (char *) mongoOperatorName;
}
/*
* EqualityOperatorList finds the equality (=) operators in the given list, and
* returns these operators in a new list.
*/
static List *
EqualityOperatorList(List *operatorList)
{
List *equalityOperatorList = NIL;
ListCell *operatorCell = NULL;
foreach(operatorCell, operatorList)
{
OpExpr *operator = (OpExpr *) lfirst(operatorCell);
char *operatorName = NULL;
operatorName = get_opname(operator->opno);
if (strncmp(operatorName, EQUALITY_OPERATOR_NAME, NAMEDATALEN) == 0)
{
equalityOperatorList = lappend(equalityOperatorList, operator);
}
}
return equalityOperatorList;
}
/*
* UniqueColumnList walks over the given operator list, and extracts the column
* argument in each operator. The function then de-duplicates extracted columns,
* and returns them in a new list.
*/
static List *
UniqueColumnList(List *operatorList)
{
List *uniqueColumnList = NIL;
ListCell *operatorCell = NULL;
foreach(operatorCell, operatorList)
{
OpExpr *operator = (OpExpr *) lfirst(operatorCell);
List *argumentList = operator->args;
Var *column = (Var *) FindArgumentOfType(argumentList, T_Var);
/* list membership is determined via column's equal() function */
uniqueColumnList = list_append_unique(uniqueColumnList, column);
}
return uniqueColumnList;
}
/*
* ColumnOperatorList finds all expressions that correspond to the given column,
* and returns them in a new list.
*/
static List *
ColumnOperatorList(Var *column, List *operatorList)
{
List *columnOperatorList = NIL;
ListCell *operatorCell = NULL;
foreach(operatorCell, operatorList)
{
OpExpr *operator = (OpExpr *) lfirst(operatorCell);
List *argumentList = operator->args;
Var *foundColumn = (Var *) FindArgumentOfType(argumentList, T_Var);
if (equal(column, foundColumn))
{
columnOperatorList = lappend(columnOperatorList, operator);
}
}
return columnOperatorList;
}
static void
AppendParamValue(BSON *queryDocument, const char *keyName, Param *paramNode,
ForeignScanState *scanStateNode)
{
ExprState *param_expr;
Datum param_value;
bool isNull;
ExprContext *econtext;
if (scanStateNode == NULL)
return;
econtext = scanStateNode->ss.ps.ps_ExprContext;
/* Prepare for parameter expression evaluation */
param_expr = ExecInitExpr((Expr *) paramNode, (PlanState *)scanStateNode);
/* Evaluate the parameter expression */
#if PG_VERSION_NUM >= 100000
param_value = ExecEvalExpr(param_expr, econtext, &isNull);
#else
param_value = ExecEvalExpr(param_expr, econtext, &isNull, NULL);
#endif
AppenMongoValue(queryDocument, keyName, param_value, isNull,
paramNode->paramtype);
}
/*
* AppendConstantValue appends to the query document the key name and constant
* value. The function translates the constant value from its PostgreSQL type to
* its MongoDB equivalent.
*/
static void
AppendConstantValue(BSON *queryDocument, const char *keyName, Const *constant)
{
if (constant->constisnull)
{
BsonAppendNull(queryDocument, keyName);
return;
}
AppenMongoValue(queryDocument, keyName, constant->constvalue, false, constant->consttype);
}
bool
AppenMongoValue(BSON *queryDocument, const char *keyName, Datum value, bool isnull, Oid id)
{
bool status = false;
if (isnull)
{
status = BsonAppendNull(queryDocument, keyName);
return status;
}
switch(id)
{
case INT2OID:
{
int16 valueInt = DatumGetInt16(value);
status = BsonAppendInt32(queryDocument, keyName, (int) valueInt);
break;
}
case INT4OID:
{
int32 valueInt = DatumGetInt32(value);
status = BsonAppendInt32(queryDocument, keyName, valueInt);
break;
}
case INT8OID:
{
int64 valueLong = DatumGetInt64(value);
status = BsonAppendInt64(queryDocument, keyName, valueLong);
break;
}
case FLOAT4OID:
{
float4 valueFloat = DatumGetFloat4(value);
status = BsonAppendDouble(queryDocument, keyName, (double) valueFloat);
break;
}
case FLOAT8OID:
{
float8 valueFloat = DatumGetFloat8(value);
status = BsonAppendDouble(queryDocument, keyName, valueFloat);
break;
}
case NUMERICOID:
{
Datum valueDatum = DirectFunctionCall1(numeric_float8, value);
float8 valueFloat = DatumGetFloat8(valueDatum);
status = BsonAppendDouble(queryDocument, keyName, valueFloat);
break;
}
case BOOLOID:
{
bool valueBool = DatumGetBool(value);
status = BsonAppendBool(queryDocument, keyName, (int) valueBool);
break;
}
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(id, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
status = BsonAppendUTF8(queryDocument, keyName, outputString);
break;
}
case BYTEAOID:
{
int len;
char *data;
char *result = DatumGetPointer(value);
if (VARATT_IS_1B(result)) {
len = VARSIZE_1B(result) - VARHDRSZ_SHORT;
data = VARDATA_1B(result);
} else {
len = VARSIZE_4B(result) - VARHDRSZ;
data = VARDATA_4B(result);
}
#ifdef META_DRIVER
if (strcmp(keyName, "_id") == 0)
{
bson_oid_t oid;
bson_oid_init_from_data(&oid, (const uint8_t *)data);
status = BsonAppendOid(queryDocument, keyName, &oid);
}
else
{
status = BsonAppendBinary(queryDocument, keyName, data, len);
}
#else
status = BsonAppendBinary(queryDocument, keyName, data, len);
#endif
break;
}
case NAMEOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
bson_oid_t bsonObjectId;
memset(bsonObjectId.bytes, 0, sizeof(bsonObjectId.bytes));
getTypeOutputInfo(id, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
BsonOidFromString(&bsonObjectId, outputString);
status = BsonAppendOid(queryDocument, keyName, &bsonObjectId);
break;
}
case DATEOID:
{
Datum valueDatum = DirectFunctionCall1(date_timestamp, value);
Timestamp valueTimestamp = DatumGetTimestamp(valueDatum);
int64 valueMicroSecs = valueTimestamp + POSTGRES_TO_UNIX_EPOCH_USECS;
int64 valueMilliSecs = valueMicroSecs / 1000;
status = BsonAppendDate(queryDocument, keyName, valueMilliSecs);
break;
}
case TIMESTAMPOID:
case TIMESTAMPTZOID:
{
Timestamp valueTimestamp = DatumGetTimestamp(value);
int64 valueMicroSecs = valueTimestamp + POSTGRES_TO_UNIX_EPOCH_USECS;
int64 valueMilliSecs = valueMicroSecs / 1000;
status = BsonAppendDate(queryDocument, keyName, valueMilliSecs);
break;
}
case NUMERICARRAY_OID:
{
ArrayType *array;
Oid elmtype;
int16 elmlen;
bool elmbyval;
char elmalign;
int num_elems;
Datum *elem_values;
bool *elem_nulls;
int i;
BSON t;
array = DatumGetArrayTypeP(value);
elmtype = ARR_ELEMTYPE(array);
get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
deconstruct_array(array, elmtype, elmlen, elmbyval, elmalign, &elem_values, &elem_nulls, &num_elems);
BsonAppendStartArray(queryDocument, keyName, &t);
for (i = 0; i < num_elems; i++)
{
Datum valueDatum;
float8 valueFloat;
if (elem_nulls[i])
continue;
valueDatum = DirectFunctionCall1(numeric_float8, elem_values[i]);
valueFloat = DatumGetFloat8(valueDatum);
#ifdef META_DRIVER
status = BsonAppendDouble(&t, keyName, valueFloat);
#else
status = BsonAppendDouble(queryDocument, keyName, valueFloat);
#endif
}
BsonAppendFinishArray(queryDocument, &t);
pfree(elem_values);
pfree(elem_nulls);
break;
}
case TEXTARRAYOID:
{
ArrayType *array;
Oid elmtype;
int16 elmlen;
bool elmbyval;
char elmalign;
int num_elems;
Datum *elem_values;
bool *elem_nulls;
int i;
BSON t;
array = DatumGetArrayTypeP(value);
elmtype = ARR_ELEMTYPE(array);
get_typlenbyvalalign(elmtype, &elmlen, &elmbyval, &elmalign);
deconstruct_array(array, elmtype, elmlen, elmbyval, elmalign, &elem_values, &elem_nulls, &num_elems);
BsonAppendStartArray(queryDocument, keyName, &t);
for (i = 0; i < num_elems; i++)
{
char *valueString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
if (elem_nulls[i])
continue;
getTypeOutputInfo(TEXTOID, &outputFunctionId, &typeVarLength);
valueString = OidOutputFunctionCall(outputFunctionId, elem_values[i]);
status = BsonAppendUTF8(queryDocument, keyName, valueString);
}
BsonAppendFinishArray(queryDocument, &t);
pfree(elem_values);
pfree(elem_nulls);
break;
}
case JSONOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
struct json_object *o;
bool typeVarLength = false;
getTypeOutputInfo(id, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, value);
o = JsonTokenerPrase(outputString);
if (is_error(o))
{
elog(WARNING, "cannot parse the document");
status = 0;
break;
}
status = JsonToBsonAppendElement(queryDocument, keyName, o);
break;
}
default:
{
/*
* We currently error out on other data types. Some types such as
* byte arrays are easy to add, but they need testing. Other types
* such as money or inet, do not have equivalents in MongoDB.
*/
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("cannot convert constant value to BSON value"),
errhint("Constant value data type: %u", id)));
break;
}
}
return status;
}
/*
* ColumnList takes in the planner's information about this foreign table. The
* function then finds all columns needed for query execution, including those
* used in projections, joins, and filter clauses, de-duplicates these columns,
* and returns them in a new list.
*/
List *
ColumnList(RelOptInfo *baserel)
{
List *columnList = NIL;
List *neededColumnList = NIL;
AttrNumber columnIndex = 1;
AttrNumber columnCount = baserel->max_attr;
#if PG_VERSION_NUM >= 90600
List *targetColumnList = baserel->reltarget->exprs;
#else
List *targetColumnList = baserel->reltargetlist;
#endif
List *restrictInfoList = baserel->baserestrictinfo;
ListCell *restrictInfoCell = NULL;
/* first add the columns used in joins and projections */
neededColumnList = list_copy(targetColumnList);
/* then walk over all restriction clauses, and pull up any used columns */
foreach(restrictInfoCell, restrictInfoList)
{
RestrictInfo *restrictInfo = (RestrictInfo *) lfirst(restrictInfoCell);
Node *restrictClause = (Node *) restrictInfo->clause;
List *clauseColumnList = NIL;
/* recursively pull up any columns used in the restriction clause */
clauseColumnList = pull_var_clause(restrictClause,
#if PG_VERSION_NUM < 90600
PVC_RECURSE_AGGREGATES,
#endif
PVC_RECURSE_PLACEHOLDERS);
neededColumnList = list_union(neededColumnList, clauseColumnList);
}
/* walk over all column definitions, and de-duplicate column list */
for (columnIndex = 1; columnIndex <= columnCount; columnIndex++)
{
ListCell *neededColumnCell = NULL;
Var *column = NULL;
/* look for this column in the needed column list */
foreach(neededColumnCell, neededColumnList)
{
Var *neededColumn = (Var *) lfirst(neededColumnCell);
if (neededColumn->varattno == columnIndex)
{
column = neededColumn;
break;
}
}
if (column != NULL)
{
columnList = lappend(columnList, column);
}
}
return columnList;
}