-
Notifications
You must be signed in to change notification settings - Fork 15
/
deparse.c
5078 lines (4467 loc) · 131 KB
/
deparse.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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* InfluxDB Foreign Data Wrapper for PostgreSQL
*
* Portions Copyright (c) 2018, TOSHIBA CORPORATION
*
* IDENTIFICATION
* deparse.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "influxdb_fdw.h"
#include "pgtime.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "nodes/plannodes.h"
#include "optimizer/clauses.h"
#include "optimizer/tlist.h"
#include "parser/parsetree.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
#include "utils/typcache.h"
#include "sys/types.h"
#include "regex.h"
#define QUOTE '"'
/* List of stable function with star argument of InfluxDB */
static const char *InfluxDBStableStarFunction[] = {
"influx_count_all",
"influx_mode_all",
"influx_max_all",
"influx_min_all",
"influx_sum_all",
"integral_all",
"mean_all",
"median_all",
"spread_all",
"stddev_all",
"first_all",
"last_all",
"percentile_all",
"sample_all",
"abs_all",
"acos_all",
"asin_all",
"atan_all",
"atan2_all",
"ceil_all",
"cos_all",
"cumulative_sum_all",
"derivative_all",
"difference_all",
"elapsed_all",
"exp_all",
"floor_all",
"ln_all",
"log_all",
"log2_all",
"log10_all",
"moving_average_all",
"non_negative_derivative_all",
"non_negative_difference_all",
"pow_all",
"round_all",
"sin_all",
"sqrt_all",
"tan_all",
"chande_momentum_oscillator_all",
"exponential_moving_average_all",
"double_exponential_moving_average_all",
"kaufmans_efficiency_ratio_all",
"kaufmans_adaptive_moving_average_all",
"triple_exponential_moving_average_all",
"triple_exponential_derivative_all",
"relative_strength_index_all",
NULL};
/* List of unique function without star argument of InfluxDB */
static const char *InfluxDBUniqueFunction[] = {
"bottom",
"percentile",
"top",
"cumulative_sum",
"derivative",
"difference",
"elapsed",
"log2",
"log10", /* Use for PostgreSQL old version */
"moving_average",
"non_negative_derivative",
"non_negative_difference",
"holt_winters",
"holt_winters_with_fit",
"chande_momentum_oscillator",
"exponential_moving_average",
"double_exponential_moving_average",
"kaufmans_efficiency_ratio",
"kaufmans_adaptive_moving_average",
"triple_exponential_moving_average",
"triple_exponential_derivative",
"relative_strength_index",
"influx_count",
"integral",
"spread",
"first",
"last",
"sample",
"influx_time",
"influx_fill_numeric",
"influx_fill_option",
NULL};
/* List of supported builtin function of InfluxDB */
static const char *InfluxDBSupportedBuiltinFunction[] = {
"now",
"sqrt",
"abs",
"acos",
"asin",
"atan",
"atan2",
"ceil",
"cos",
"exp",
"floor",
"ln",
"log",
"log10",
"pow",
"round",
"sin",
"tan",
NULL};
/*
* Global context for influxdb_foreign_expr_walker's search of an expression tree.
*/
typedef struct foreign_glob_cxt
{
PlannerInfo *root; /* global planner state */
RelOptInfo *foreignrel; /* the foreign relation we are planning for */
Relids relids; /* relids of base relations in the underlying
* scan */
Oid relid; /* relation oid */
unsigned int mixing_aggref_status; /* mixing_aggref_status contains
* information about whether
* expression includes both of
* aggregate and non-aggregate. */
bool for_tlist; /* whether evaluation for the expression of
* tlist */
bool is_inner_func; /* exist or not in inner exprs */
} foreign_glob_cxt;
/*
* Local (per-tree-level) context for influxdb_foreign_expr_walker's search.
* This is concerned with identifying collations used in the expression.
*/
typedef enum
{
FDW_COLLATE_NONE, /* expression is of a noncollatable type */
FDW_COLLATE_SAFE, /* collation derives from a foreign Var */
FDW_COLLATE_UNSAFE /* collation derives from something else */
} FDWCollateState;
typedef struct foreign_loc_cxt
{
Oid collation; /* OID of current collation, if any */
FDWCollateState state; /* state of current collation choice */
bool can_skip_cast; /* outer function can skip float8/numeric cast */
bool can_pushdown_stable; /* true if query contains stable
* function with star or regex */
bool can_pushdown_volatile; /* true if query contains volatile
* function */
bool influx_fill_enable; /* true if deparse subexpression inside
* influx_time() */
bool have_otherfunc_influx_time_tlist; /* true if having other functions than influx_time() in tlist. */
bool has_time_key; /* mark true if comparison with time key column */
bool has_sub_or_add_operator; /* mark true if expression has '-' or '+' operator */
bool is_comparison; /* mark true if has comparison */
} foreign_loc_cxt;
/* Type of operator for pattern matching */
typedef enum
{
UNKNOWN_OPERATOR = 0,
LIKE_OPERATOR, /* LIKE case senstive */
NOT_LIKE_OPERATOR, /* NOT LIKE case sensitive */
ILIKE_OPERATOR, /* LIKE case insensitive */
NOT_ILIKE_OPERATOR, /* NOT LIKE case insensitive */
REGEX_MATCH_CASE_SENSITIVE_OPERATOR,
REGEX_NOT_MATCH_CASE_SENSITIVE_OPERATOR,
REGEX_MATCH_CASE_INSENSITIVE_OPERATOR,
REGEX_NOT_MATCH_CASE_INSENSITIVE_OPERATOR
} PatternMatchingOperator;
/*
* Context for influxdb_deparse_expr
*/
typedef struct deparse_expr_cxt
{
PlannerInfo *root; /* global planner state */
RelOptInfo *foreignrel; /* the foreign relation we are planning for */
RelOptInfo *scanrel; /* the underlying scan relation. Same as
* foreignrel, when that represents a join or
* a base relation. */
StringInfo buf; /* output buffer to append to */
List **params_list; /* exprs that will become remote Params */
PatternMatchingOperator op_type; /* Type of operator for pattern matching */
bool is_tlist; /* deparse during target list exprs */
bool can_skip_cast; /* outer function can skip float8/numeric cast */
bool can_delete_directly; /* DELETE statement can pushdown
* directly */
bool has_bool_cmp; /* outer has bool comparison target */
FuncExpr *influx_fill_expr; /* Store the fill() function */
/*
* For comparison with time key column, if its data type is timestamp with time zone,
* need to convert to timestamp without time zone.
*/
bool convert_to_timestamp;
} deparse_expr_cxt;
typedef struct pull_func_clause_context
{
List *funclist;
} pull_func_clause_context;
/*
* Functions to determine whether an expression can be evaluated safely on
* remote server.
*/
static bool influxdb_foreign_expr_walker(Node *node,
foreign_glob_cxt *glob_cxt,
foreign_loc_cxt *outer_cxt);
/*
* Functions to construct string representation of a node tree.
*/
static void influxdb_deparse_expr(Expr *node, deparse_expr_cxt *context);
static void influxdb_deparse_var(Var *node, deparse_expr_cxt *context);
static void influxdb_deparse_const(Const *node, deparse_expr_cxt *context, int showtype);
static void influxdb_deparse_param(Param *node, deparse_expr_cxt *context);
static void influxdb_deparse_func_expr(FuncExpr *node, deparse_expr_cxt *context);
static void influxdb_deparse_fill_option(StringInfo buf, const char *val);
static void influxdb_deparse_op_expr(OpExpr *node, deparse_expr_cxt *context);
static void influxdb_deparse_operator_name(StringInfo buf, Form_pg_operator opform, PatternMatchingOperator *op_type);
static void influxdb_deparse_scalar_array_op_expr(ScalarArrayOpExpr *node,
deparse_expr_cxt *context);
static void influxdb_deparse_relabel_type(RelabelType *node, deparse_expr_cxt *context);
static void influxdb_deparse_bool_expr(BoolExpr *node, deparse_expr_cxt *context);
static void influxdb_deparse_null_test(NullTest *node, deparse_expr_cxt *context);
static void influxdb_deparse_array_expr(ArrayExpr *node, deparse_expr_cxt *context);
static void influxdb_deparse_coerce_via_io(CoerceViaIO * cio, deparse_expr_cxt *context);
static void influxdb_print_remote_param(int paramindex, Oid paramtype, int32 paramtypmod,
deparse_expr_cxt *context);
static void influxdb_print_remote_placeholder(Oid paramtype, int32 paramtypmod,
deparse_expr_cxt *context);
static void influxdb_deparse_relation(StringInfo buf, Relation rel);
static void influxdb_deparse_target_list(StringInfo buf, PlannerInfo *root, Index rtindex, Relation rel,
Bitmapset *attrs_used, List **retrieved_attrs);
static void influxdb_deparse_target_list_schemaless(StringInfo buf, Relation rel, Oid reloid,
Bitmapset *attrs_used,
List **retrieved_attrs,
bool all_fieldtag, List *slcols);
static void influxdb_deparse_slvar(Node *node, Var *var, Const *cnst, deparse_expr_cxt *context);
static void influxdb_deparse_column_ref(StringInfo buf, int varno, int varattno, Oid vartype, PlannerInfo *root, bool convert, bool *can_delete_directly);
static void influxdb_deparse_select(List *tlist, List **retrieved_attrs, deparse_expr_cxt *context);
static void influxdb_deparse_from_expr_for_rel(StringInfo buf, PlannerInfo *root,
RelOptInfo *foreignrel,
bool use_alias, List **params_list);
static void influxdb_deparse_from_expr(List *quals, deparse_expr_cxt *context);
static void influxdb_deparse_aggref(Aggref *node, deparse_expr_cxt *context);
static void influxdb_append_conditions(List *exprs, deparse_expr_cxt *context);
static void influxdb_append_group_by_clause(List *tlist, deparse_expr_cxt *context);
static void influxdb_append_order_by_clause(List *pathkeys, deparse_expr_cxt *context);
static Node *influxdb_deparse_sort_group_clause(Index ref, List *tlist,
deparse_expr_cxt *context);
static void influxdb_deparse_explicit_target_list(List *tlist, List **retrieved_attrs,
deparse_expr_cxt *context);
static Expr *influxdb_find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel);
static bool influxdb_contain_time_column(List *exprs, schemaless_info *pslinfo);
static bool influxdb_contain_time_key_column(Oid relid, List *exprs);
static bool influxdb_contain_time_expr(List *exprs);
static bool influxdb_contain_time_function(List *exprs);
static bool influxdb_contain_time_param(List *exprs);
static bool influxdb_contain_time_const(List *exprs);
static void influxdb_append_field_key(TupleDesc tupdesc, StringInfo buf, Index rtindex, PlannerInfo *root, bool first);
static void influxdb_append_limit_clause(deparse_expr_cxt *context);
static bool influxdb_is_string_type(Node *node, schemaless_info *pslinfo);
static char *influxdb_quote_identifier(const char *s, char q);
static bool influxdb_contain_functions_walker(Node *node, void *context);
bool influxdb_is_grouping_target(TargetEntry *tle, Query *query);
bool influxdb_is_builtin(Oid objectId);
bool influxdb_is_regex_argument(Const *node, char **extval);
char *influxdb_replace_function(char *in);
bool influxdb_is_star_func(Oid funcid, char *in);
static bool influxdb_is_unique_func(Oid funcid, char *in);
static bool influxdb_is_supported_builtin_func(Oid funcid, char *in);
static bool exist_in_function_list(char *funcname, const char **funclist);
static void add_backslash(StringInfo buf, const char *ptr, const char *regex_special);
static bool influxdb_last_percent_sign_check(const char *val);
static void influxdb_deparse_string_like_pattern(StringInfo buf, const char *val, PatternMatchingOperator op_type);
static void influxdb_deparse_string_regex_pattern(StringInfo buf, const char *val, PatternMatchingOperator op_type);
/*
* Local variables.
*/
static char *cur_opname = NULL;
/*
* Append remote name of specified foreign table to buf.
* Use value of table_name FDW option (if any) instead of relation's name.
* Similarly, schema_name FDW option overrides schema name.
*/
static void
influxdb_deparse_relation(StringInfo buf, Relation rel)
{
char *relname = influxdb_get_table_name(rel);
appendStringInfo(buf, "%s", influxdb_quote_identifier(relname, QUOTE));
}
static char *
influxdb_quote_identifier(const char *s, char q)
{
char *result = palloc(strlen(s) * 2 + 3);
char *r = result;
*r++ = q;
while (*s)
{
if (*s == q)
*r++ = *s;
*r++ = *s;
s++;
}
*r++ = q;
*r++ = '\0';
return result;
}
/*
* pull_func_clause_walker
*
* Recursively search for functions within a clause.
*/
static bool
influxdb_pull_func_clause_walker(Node *node, pull_func_clause_context * context)
{
if (node == NULL)
return false;
if (IsA(node, FuncExpr))
{
context->funclist = lappend(context->funclist, node);
return false;
}
return expression_tree_walker(node, influxdb_pull_func_clause_walker,
(void *) context);
}
/*
* pull_func_clause
*
* Pull out function from a clause and then add to target list
*/
List *
influxdb_pull_func_clause(Node *node)
{
pull_func_clause_context context;
context.funclist = NIL;
influxdb_pull_func_clause_walker(node, &context);
return context.funclist;
}
/*
* Returns true if given expr is safe to evaluate on the foreign server.
*/
bool
influxdb_is_foreign_expr(PlannerInfo *root,
RelOptInfo *baserel,
Expr *expr,
bool for_tlist)
{
foreign_glob_cxt glob_cxt;
foreign_loc_cxt loc_cxt;
InfluxDBFdwRelationInfo *fpinfo = (InfluxDBFdwRelationInfo *) (baserel->fdw_private);
/*
* Check that the expression consists of nodes that are safe to execute
* remotely.
*/
glob_cxt.root = root;
glob_cxt.foreignrel = baserel;
glob_cxt.relid = fpinfo->table->relid;
glob_cxt.mixing_aggref_status = INFLUXDB_TARGETS_MIXING_AGGREF_SAFE;
glob_cxt.for_tlist = for_tlist;
glob_cxt.is_inner_func = false;
/*
* For an upper relation, use relids from its underneath scan relation,
* because the upperrel's own relids currently aren't set to anything
* meaningful by the core code. For other relation, use their own relids.
*/
if (baserel->reloptkind == RELOPT_UPPER_REL)
glob_cxt.relids = fpinfo->outerrel->relids;
else
glob_cxt.relids = baserel->relids;
loc_cxt.collation = InvalidOid;
loc_cxt.state = FDW_COLLATE_NONE;
loc_cxt.can_skip_cast = false;
loc_cxt.influx_fill_enable = false;
loc_cxt.has_time_key = false;
loc_cxt.has_sub_or_add_operator = false;
loc_cxt.is_comparison = false;
if (!influxdb_foreign_expr_walker((Node *) expr, &glob_cxt, &loc_cxt))
return false;
/*
* If the expression has a valid collation that does not arise from a
* foreign var, the expression can not be sent over.
*/
if (loc_cxt.state == FDW_COLLATE_UNSAFE)
return false;
/* OK to evaluate on the remote server */
return true;
}
static bool
is_valid_type(Oid type)
{
switch (type)
{
case INT2OID:
case INT4OID:
case INT8OID:
case OIDOID:
case FLOAT4OID:
case FLOAT8OID:
case NUMERICOID:
case VARCHAROID:
case TEXTOID:
case TIMEOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
return true;
}
return false;
}
/*
* Check if expression is safe to execute remotely, and return true if so.
*
* In addition, *outer_cxt is updated with collation information.
*
* We must check that the expression contains only node types we can deparse,
* that all types/functions/operators are safe to send (which we approximate
* as being built-in), and that all collations used in the expression derive
* from Vars of the foreign table. Because of the latter, the logic is
* pretty close to assign_collations_walker() in parse_collate.c, though we
* can assume here that the given expression is valid.
*/
static bool
influxdb_foreign_expr_walker(Node *node,
foreign_glob_cxt *glob_cxt,
foreign_loc_cxt *outer_cxt)
{
bool check_type = true;
foreign_loc_cxt inner_cxt;
Oid collation;
FDWCollateState state;
HeapTuple tuple;
Form_pg_operator form;
char *cur_opname;
static bool is_time_column = false; /* Use static variable for save value
* from child node to parent node.
* Check column T_Var is time column? */
InfluxDBFdwRelationInfo *fpinfo =
(InfluxDBFdwRelationInfo *)(glob_cxt->foreignrel->fdw_private);
/* Need do nothing for empty subexpressions */
if (node == NULL)
return true;
/* Set up inner_cxt for possible recursion to child nodes */
inner_cxt.collation = InvalidOid;
inner_cxt.state = FDW_COLLATE_NONE;
inner_cxt.can_skip_cast = false;
inner_cxt.can_pushdown_stable = false;
inner_cxt.can_pushdown_volatile = false;
inner_cxt.influx_fill_enable = false;
inner_cxt.has_time_key = false;
inner_cxt.has_sub_or_add_operator = false;
inner_cxt.is_comparison = false;
switch (nodeTag(node))
{
case T_Var:
{
Var *var = (Var *) node;
/*
* If the Var is from the foreign table, we consider its
* collation (if any) safe to use. If it is from another
* table, we treat its collation the same way as we would a
* Param's collation, ie it's not safe for it to have a
* non-default collation.
*/
if (bms_is_member(var->varno, glob_cxt->relids) &&
var->varlevelsup == 0)
{
/* Var belongs to foreign table */
if (var->varattno < 0)
return false;
/* check column is time column? */
if (INFLUXDB_IS_TIME_TYPE(var->vartype))
{
is_time_column = true;
/*
* Does not pushdown comparison between substraction or addition of time column with interval and time key
* For example:
* time key = time column +/- interval
*/
if (outer_cxt->is_comparison && outer_cxt->has_sub_or_add_operator && outer_cxt->has_time_key)
return false;
}
/* Mark this target is field/tag */
glob_cxt->mixing_aggref_status |= INFLUXDB_TARGETS_MARK_COLUMN;
/* Else check the collation */
collation = var->varcollid;
state = OidIsValid(collation) ? FDW_COLLATE_SAFE : FDW_COLLATE_NONE;
}
else
{
/* Var belongs to some other table */
collation = var->varcollid;
if (collation == InvalidOid ||
collation == DEFAULT_COLLATION_OID)
{
/*
* It's noncollatable, or it's safe to combine with a
* collatable foreign Var, so set state to NONE.
*/
state = FDW_COLLATE_NONE;
}
else
{
/*
* Do not fail right away, since the Var might appear
* in a collation-insensitive context.
*/
state = FDW_COLLATE_UNSAFE;
}
}
}
break;
case T_Const:
{
char *type_name;
Const *c = (Const *) node;
if (c->consttype == INTERVALOID)
{
Interval *interval = DatumGetIntervalP(c->constvalue);
#if (PG_VERSION_NUM >= 150000)
struct pg_itm tm;
interval2itm(*interval, &tm);
#else
struct pg_tm tm;
fsec_t fsec;
interval2tm(*interval, &tm, &fsec);
#endif
/*
* Not pushdown interval with month or year because
* InfluxDB does not support month and year duration
*/
if (tm.tm_mon != 0 || tm.tm_year != 0)
{
return false;
}
}
/*
* Get type name based on the const value. If the type name is
* "influx_fill_enum", allow it to push down to remote by
* disable build in type check
*/
type_name = influxdb_get_data_type_name(c->consttype);
if (strcmp(type_name, "influx_fill_enum") == 0)
check_type = false;
/*
* If the constant has nondefault collation, either it's of a
* non-builtin type, or it reflects folding of a CollateExpr;
* either way, it's unsafe to send to the remote.
*/
if (c->constcollid != InvalidOid &&
c->constcollid != DEFAULT_COLLATION_OID)
return false;
/* Otherwise, we can consider that it doesn't set collation */
collation = InvalidOid;
state = FDW_COLLATE_NONE;
}
break;
case T_Param:
{
Param *p = (Param *) node;
if (!is_valid_type(p->paramtype))
return false;
if (INFLUXDB_IS_TIME_TYPE(p->paramtype))
{
/*
* Does not pushdown comparison between substraction or addition of Param with interval and time key
* For example:
* time key = Param +/- interval
*/
if (outer_cxt->is_comparison && outer_cxt->has_sub_or_add_operator && outer_cxt->has_time_key)
return false;
}
/*
* Collation rule is same as for Consts and non-foreign Vars.
*/
collation = p->paramcollid;
if (collation == InvalidOid ||
collation == DEFAULT_COLLATION_OID)
state = FDW_COLLATE_NONE;
else
state = FDW_COLLATE_UNSAFE;
}
break;
case T_FieldSelect: /* Allow pushdown FieldSelect to support
* accessing value of record of star and regex
* functions */
{
if (!(glob_cxt->foreignrel->reloptkind == RELOPT_BASEREL ||
glob_cxt->foreignrel->reloptkind == RELOPT_OTHER_MEMBER_REL))
return false;
collation = InvalidOid;
state = FDW_COLLATE_NONE;
check_type = false;
}
break;
case T_FuncExpr:
{
FuncExpr *fe = (FuncExpr *) node;
char *opername = NULL;
bool is_cast_func = false;
bool is_star_func = false;
bool can_pushdown_func = false;
bool is_regex = false;
/* get function name and schema */
tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(fe->funcid));
if (!HeapTupleIsValid(tuple))
{
elog(ERROR, "cache lookup failed for function %u", fe->funcid);
}
opername = pstrdup(((Form_pg_proc) GETSTRUCT(tuple))->proname.data);
ReleaseSysCache(tuple);
if (INFLUXDB_IS_TIME_TYPE(fe->funcresulttype))
{
if (outer_cxt->is_comparison)
{
if (strcmp(opername, "now") != 0) /* Does not support comparison with function except now()*/
{
return false;
}
else if (!outer_cxt->has_time_key) /* Does not support comparison between now() and not time key (tags/fields column or time expression) */
{
return false;
}
}
}
if (strcmp(opername, "float8") == 0 || strcmp(opername, "numeric") == 0)
{
is_cast_func = true;
}
/* pushed down to InfluxDB */
if (influxdb_is_star_func(fe->funcid, opername))
{
is_star_func = true;
outer_cxt->can_pushdown_stable = true;
}
if (influxdb_is_unique_func(fe->funcid, opername) ||
influxdb_is_supported_builtin_func(fe->funcid, opername))
{
can_pushdown_func = true;
inner_cxt.can_skip_cast = true;
outer_cxt->can_pushdown_volatile = true;
}
if (!(is_star_func || can_pushdown_func || is_cast_func))
return false;
/* fill() must be inside influx_time() */
if (strcmp(opername, "influx_fill_numeric") == 0 ||
strcmp(opername, "influx_fill_option") == 0)
{
if (outer_cxt->influx_fill_enable == false)
elog(ERROR, "influxdb_fdw: syntax error influx_fill_numeric() or influx_fill_option() must be embedded inside influx_time() function\n");
}
/* Accept type cast functions if outer is specific functions */
if (is_cast_func)
{
if (outer_cxt->can_skip_cast == false)
return false;
}
else
{
/*
* Nested function cannot be executed in non tlist
*/
if (!glob_cxt->for_tlist && glob_cxt->is_inner_func)
return false;
glob_cxt->is_inner_func = true;
}
/*
* Allow influx_fill_numeric/influx_fill_option() inside
* influx_time() function
*/
if (strcmp(opername, "influx_time") == 0)
{
inner_cxt.influx_fill_enable = true;
}
else
{
/* There is another function than influx_time in tlist */
outer_cxt->have_otherfunc_influx_time_tlist = true;
}
/*
* Recurse to input subexpressions.
*/
if (!influxdb_foreign_expr_walker((Node *) fe->args,
glob_cxt, &inner_cxt))
return false;
/*
* Force to restore the state after deparse subexpression if
* it has been change above
*/
inner_cxt.influx_fill_enable = false;
if (!is_cast_func)
glob_cxt->is_inner_func = false;
if (list_length(fe->args) > 0)
{
ListCell *funclc;
Node *firstArg;
funclc = list_head(fe->args);
firstArg = (Node *) lfirst(funclc);
if (IsA(firstArg, Const))
{
Const *arg = (Const *) firstArg;
char *extval;
if (arg->consttype == TEXTOID)
is_regex = influxdb_is_regex_argument(arg, &extval);
}
}
if (is_regex)
{
collation = InvalidOid;
state = FDW_COLLATE_NONE;
check_type = false;
outer_cxt->can_pushdown_stable = true;
}
else
{
/*
* If function's input collation is not derived from a
* foreign Var, it can't be sent to remote.
*/
if (fe->inputcollid == InvalidOid)
/* OK, inputs are all noncollatable */ ;
else if (inner_cxt.state != FDW_COLLATE_SAFE ||
fe->inputcollid != inner_cxt.collation)
return false;
/*
* Detect whether node is introducing a collation not
* derived from a foreign Var. (If so, we just mark it
* unsafe for now rather than immediately returning false,
* since the parent node might not care.)
*/
collation = fe->funccollid;
if (collation == InvalidOid)
state = FDW_COLLATE_NONE;
else if (inner_cxt.state == FDW_COLLATE_SAFE &&
collation == inner_cxt.collation)
state = FDW_COLLATE_SAFE;
else if (collation == DEFAULT_COLLATION_OID)
state = FDW_COLLATE_NONE;
else
state = FDW_COLLATE_UNSAFE;
}
}
break;
case T_OpExpr:
{
OpExpr *oe = (OpExpr *) node;
bool is_slvar = false;
bool is_param = false;
bool has_time_key = false;
bool has_time_column = false;
bool has_time_tags_or_fields_column = false;
if (influxdb_is_slvar_fetch(node, &(fpinfo->slinfo)))
is_slvar = true;
if (influxdb_is_param_fetch(node, &(fpinfo->slinfo)))
is_param = true;
/*
* Similarly, only built-in operators can be sent to remote.
* (If the operator is, surely its underlying function is
* too.)
*/
if (!influxdb_is_builtin(oe->opno) && !is_slvar && !is_param)
return false;
tuple = SearchSysCache1(OPEROID, ObjectIdGetDatum(oe->opno));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for operator %u", oe->opno);
form = (Form_pg_operator) GETSTRUCT(tuple);
/* opname is not a SQL identifier, so we should not quote it. */
cur_opname = pstrdup(NameStr(form->oprname));
ReleaseSysCache(tuple);
if (strcmp(cur_opname, "=") == 0 ||
strcmp(cur_opname, ">") == 0 ||
strcmp(cur_opname, "<") == 0 ||
strcmp(cur_opname, ">=") == 0 ||
strcmp(cur_opname, "<=") == 0 ||
strcmp(cur_opname, "!=") == 0 ||
strcmp(cur_opname, "<>") == 0)
{
inner_cxt.is_comparison = true;
}
/*
* Does not pushdown time comparison between interval vs interval
* For example:
* (time - time) vs interval constant
* (time - time) vs (time - time)
*/
if (inner_cxt.is_comparison &&
exprType((Node*) linitial(oe->args)) == INTERVALOID &&
exprType((Node*) lsecond(oe->args)) == INTERVALOID)
{
return false;
}
has_time_key = influxdb_contain_time_key_column(glob_cxt->relid, oe->args);
/*
* Does not pushdown time comparison between time expression vs not time key
* For example:
* time +/- interval vs tags/fields column
* time +/- interval vs time +/- interval
* time +/- interval vs function
*/
if (inner_cxt.is_comparison &&
!has_time_key &&
influxdb_contain_time_expr(oe->args))
{
return false;
}
/*
* Does not pushdown comparsion using !=, <> with time key column.
*/
if (strcmp(cur_opname, "!=") == 0 ||
strcmp(cur_opname, "<>") == 0)
{
if (has_time_key)
return false;
}
has_time_column = influxdb_contain_time_column(oe->args, &(fpinfo->slinfo));
has_time_tags_or_fields_column = (has_time_column && !has_time_key);
/* Does not pushdown time comparison between tags/fields vs function */
if (inner_cxt.is_comparison &&
has_time_tags_or_fields_column &&
influxdb_contain_time_function(oe->args))
{
return false;
}
if (strcmp(cur_opname, ">") == 0 ||
strcmp(cur_opname, "<") == 0 ||
strcmp(cur_opname, ">=") == 0 ||
strcmp(cur_opname, "<=") == 0 ||
strcmp(cur_opname, "=") == 0)
{
List *first = list_make1(linitial(oe->args));
List *second = list_make1(lsecond(oe->args));
bool has_both_time_colum = influxdb_contain_time_column(first, &(fpinfo->slinfo)) &&
influxdb_contain_time_column(second, &(fpinfo->slinfo));
/*
* Does not pushdown time comparsion using <, >, <=, >=, = between time key and time column
* For example:
* time key vs time key
* time key vs tags/fields column
*/
if (has_time_key && has_both_time_colum)
{
return false;
}
/* Handle the operators <, >, <=, >= */
if (strcmp(cur_opname, "=") != 0)
{
bool has_first_time_key = influxdb_contain_time_key_column(glob_cxt->relid, first);
bool has_second_time_key = influxdb_contain_time_key_column(glob_cxt->relid, second);
bool has_both_tags_or_fields_column = (has_both_time_colum && !has_first_time_key && !has_second_time_key);
/* Does not pushdown comparison between tags/fields column and time tags/field column */
if (has_both_tags_or_fields_column)
return false;
/*
* Does not pushdown comparison between tags/fields column and time constant or time param
* For example:
* tags/fields vs '2010-10-10 10:10:10'
*/
if (has_time_tags_or_fields_column &&
(influxdb_contain_time_const(oe->args) ||
influxdb_contain_time_param(oe->args)))
{
return false;
}
/*
* Cannot pushdown to InfluxDB if there is string comparison
* with: "<, >, <=, >=" operators.
*/
if (influxdb_is_string_type((Node *)linitial(oe->args), &(fpinfo->slinfo)))
{
return false;
}
}
}
/*
* Does not support pushdown time comparison between time key column and time column +/- interval or
* param +/- interval or function +/- interval except now() +/- interval.