-
Notifications
You must be signed in to change notification settings - Fork 105
/
mongo_query.c
545 lines (466 loc) · 15.5 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
/*-------------------------------------------------------------------------
*
* mongo_query.c
*
* Function definitions for sending queries to MongoDB. These functions assume
* that queries are sent through the official MongoDB C driver, and apply query
* optimizations to reduce the amount of data fetched from the driver.
*
* Copyright (c) 2012-2014 Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "mongo_fdw.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);
/*
* 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;
/* 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);
/*
* 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);
}
}
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)
{
List *equalityOperatorList = NIL;
List *comparisonOperatorList = NIL;
List *columnList = NIL;
ListCell *equalityOperatorCell = NULL;
ListCell *columnCell = NULL;
bson *queryDocument = NULL;
int documentStatus = BSON_OK;
queryDocument = bson_create();
bson_init(queryDocument);
/*
* 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;
List *argumentList = equalityOperator->args;
Var *column = (Var *) FindArgumentOfType(argumentList, T_Var);
Const *constant = (Const *) FindArgumentOfType(argumentList, T_Const);
columnId = column->varattno;
columnName = get_relid_attribute_name(relationId, columnId);
AppendConstantValue(queryDocument, columnName, constant);
}
/*
* 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;
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 */
bson_append_start_object(queryDocument, columnName);
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);
AppendConstantValue(queryDocument, mongoOperatorName, constant);
}
bson_append_finish_object(queryDocument);
}
documentStatus = bson_finish(queryDocument);
if (documentStatus != BSON_OK)
{
ereport(ERROR, (errmsg("could not create document for query"),
errhint("BSON error: %s", queryDocument->errstr)));
}
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;
}
/*
* 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)
{
Datum constantValue = constant->constvalue;
Oid constantTypeId = constant->consttype;
bool constantNull = constant->constisnull;
if (constantNull)
{
bson_append_null(queryDocument, keyName);
return;
}
switch(constantTypeId)
{
case INT2OID:
{
int16 value = DatumGetInt16(constantValue);
bson_append_int(queryDocument, keyName, (int) value);
break;
}
case INT4OID:
{
int32 value = DatumGetInt32(constantValue);
bson_append_int(queryDocument, keyName, value);
break;
}
case INT8OID:
{
int64 value = DatumGetInt64(constantValue);
bson_append_long(queryDocument, keyName, value);
break;
}
case FLOAT4OID:
{
float4 value = DatumGetFloat4(constantValue);
bson_append_double(queryDocument, keyName, (double) value);
break;
}
case FLOAT8OID:
{
float8 value = DatumGetFloat8(constantValue);
bson_append_double(queryDocument, keyName, value);
break;
}
case NUMERICOID:
{
Datum valueDatum = DirectFunctionCall1(numeric_float8, constantValue);
float8 value = DatumGetFloat8(valueDatum);
bson_append_double(queryDocument, keyName, value);
break;
}
case BOOLOID:
{
bool value = DatumGetBool(constantValue);
bson_append_int(queryDocument, keyName, (int) value);
break;
}
case BPCHAROID:
case VARCHAROID:
case TEXTOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
getTypeOutputInfo(constantTypeId, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, constantValue);
bson_append_string(queryDocument, keyName, outputString);
break;
}
case NAMEOID:
{
char *outputString = NULL;
Oid outputFunctionId = InvalidOid;
bool typeVarLength = false;
bson_oid_t bsonObjectId;
memset(bsonObjectId.bytes, 0, sizeof(bsonObjectId.bytes));
getTypeOutputInfo(constantTypeId, &outputFunctionId, &typeVarLength);
outputString = OidOutputFunctionCall(outputFunctionId, constantValue);
bson_oid_from_string(&bsonObjectId, outputString);
bson_append_oid(queryDocument, keyName, &bsonObjectId);
break;
}
case DATEOID:
{
Datum valueDatum = DirectFunctionCall1(date_timestamp, constantValue);
Timestamp valueTimestamp = DatumGetTimestamp(valueDatum);
int64 valueMicroSecs = valueTimestamp + POSTGRES_TO_UNIX_EPOCH_USECS;
int64 valueMilliSecs = valueMicroSecs / 1000;
bson_append_date(queryDocument, keyName, valueMilliSecs);
break;
}
case TIMESTAMPOID:
case TIMESTAMPTZOID:
{
Timestamp valueTimestamp = DatumGetTimestamp(constantValue);
int64 valueMicroSecs = valueTimestamp + POSTGRES_TO_UNIX_EPOCH_USECS;
int64 valueMilliSecs = valueMicroSecs / 1000;
bson_append_date(queryDocument, keyName, valueMilliSecs);
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", constantTypeId)));
break;
}
}
}
/*
* 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;
List *targetColumnList = baserel->reltargetlist;
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,
PVC_RECURSE_AGGREGATES,
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;
}