|
| 1 | +/* ------------------------------------------------------------------------ |
| 2 | + * |
| 3 | + * utils.c |
| 4 | + * definitions of various support functions |
| 5 | + * |
| 6 | + * Copyright (c) 2016, Postgres Professional |
| 7 | + * |
| 8 | + * ------------------------------------------------------------------------ |
| 9 | + */ |
| 10 | +#include "utils.h" |
| 11 | +#include "nodes/nodeFuncs.h" |
| 12 | +#include "parser/parse_param.h" |
| 13 | +#include "utils/builtins.h" |
| 14 | +#include "rewrite/rewriteManip.h" |
| 15 | + |
| 16 | + |
| 17 | +static bool clause_contains_params_walker(Node *node, void *context); |
| 18 | + |
| 19 | +bool |
| 20 | +clause_contains_params(Node *clause) |
| 21 | +{ |
| 22 | + return expression_tree_walker(clause, |
| 23 | + clause_contains_params_walker, |
| 24 | + NULL); |
| 25 | +} |
| 26 | + |
| 27 | +static bool |
| 28 | +clause_contains_params_walker(Node *node, void *context) |
| 29 | +{ |
| 30 | + if (node == NULL) |
| 31 | + return false; |
| 32 | + if (IsA(node, Param)) |
| 33 | + return true; |
| 34 | + return expression_tree_walker(node, |
| 35 | + clause_contains_params_walker, |
| 36 | + context); |
| 37 | +} |
| 38 | + |
| 39 | +static Node * |
| 40 | +replace_child_var(Var *var, replace_rte_variables_context *context) |
| 41 | +{ |
| 42 | + ReplaceVarsContext *cxt = (ReplaceVarsContext *) context->callback_arg; |
| 43 | + Var *new_var; |
| 44 | + |
| 45 | + new_var = makeNode(Var); |
| 46 | + memcpy(new_var, var, sizeof(Var)); |
| 47 | + |
| 48 | + /* |
| 49 | + * Replace a partition's Var with a Var |
| 50 | + * pointing to the RuntimeAppend's results |
| 51 | + */ |
| 52 | + new_var->varno = cxt->parent->relid; |
| 53 | + new_var->location = -1; |
| 54 | + new_var->varnoold = 0; |
| 55 | + |
| 56 | + return (Node *) new_var; |
| 57 | +} |
| 58 | + |
| 59 | +Node * |
| 60 | +replace_child_vars_with_parent_var(Node *node, ReplaceVarsContext *context) |
| 61 | +{ |
| 62 | + return replace_rte_variables(node, context->child->relid, context->sublevels_up, |
| 63 | + replace_child_var, (void *) context, NULL); |
| 64 | +} |
| 65 | + |
| 66 | +/* |
| 67 | + * Sorts reltargetlist by Var's varattno (physical order) since |
| 68 | + * we can't use static build_path_tlist() for our custom nodes. |
| 69 | + * |
| 70 | + * See create_scan_plan & use_physical_tlist for more details. |
| 71 | + */ |
| 72 | +List * |
| 73 | +sort_rel_tlist(List *tlist) |
| 74 | +{ |
| 75 | + int i; |
| 76 | + int plain_tlist_size = list_length(tlist); |
| 77 | + Var **plain_tlist = palloc(plain_tlist_size * sizeof(Var *)); |
| 78 | + ListCell *tlist_cell; |
| 79 | + List *result = NIL; |
| 80 | + |
| 81 | + i = 0; |
| 82 | + foreach (tlist_cell, tlist) |
| 83 | + plain_tlist[i++] = lfirst(tlist_cell); |
| 84 | + |
| 85 | + qsort(plain_tlist, plain_tlist_size, sizeof(Var *), cmp_tlist_vars); |
| 86 | + |
| 87 | + for (i = 0; i < plain_tlist_size; i++) |
| 88 | + result = lappend(result, plain_tlist[i]); |
| 89 | + |
| 90 | + return result; |
| 91 | +} |
| 92 | + |
| 93 | +/* Compare Vars by varattno */ |
| 94 | +int |
| 95 | +cmp_tlist_vars(const void *a, const void *b) |
| 96 | +{ |
| 97 | + Var *v1 = *(Var **) a; |
| 98 | + Var *v2 = *(Var **) b; |
| 99 | + |
| 100 | + Assert(IsA(v1, Var) && IsA(v2, Var)); |
| 101 | + |
| 102 | + if (v1->varattno > v2->varattno) |
| 103 | + return 1; |
| 104 | + else if (v1->varattno < v2->varattno) |
| 105 | + return -1; |
| 106 | + else |
| 107 | + { |
| 108 | + /* XXX: I really doubt this case is ok */ |
| 109 | + Assert(v1->varattno != v2->varattno); |
| 110 | + return 0; |
| 111 | + } |
| 112 | +} |
0 commit comments