|
| 1 | +diff --git a/src/backend/optimizer/plan/Makefile b/src/backend/optimizer/plan/Makefile |
| 2 | +index 80ef162e484..84b847faaf9 100644 |
| 3 | +--- a/src/backend/optimizer/plan/Makefile |
| 4 | ++++ b/src/backend/optimizer/plan/Makefile |
| 5 | +@@ -20,6 +20,7 @@ OBJS = \ |
| 6 | + planmain.o \ |
| 7 | + planner.o \ |
| 8 | + setrefs.o \ |
| 9 | +- subselect.o |
| 10 | ++ subselect.o \ |
| 11 | ++ vscodehelper.o |
| 12 | + |
| 13 | + include $(top_srcdir)/src/backend/common.mk |
| 14 | +diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c |
| 15 | +index e17d31a5c3e..0bc057825d3 100644 |
| 16 | +--- a/src/backend/optimizer/plan/planmain.c |
| 17 | ++++ b/src/backend/optimizer/plan/planmain.c |
| 18 | +@@ -29,6 +29,7 @@ |
| 19 | + #include "optimizer/placeholder.h" |
| 20 | + #include "optimizer/planmain.h" |
| 21 | + |
| 22 | ++extern void vscode_test_helper(PlannerInfo *root); |
| 23 | + |
| 24 | + /* |
| 25 | + * query_planner |
| 26 | +@@ -284,5 +285,7 @@ query_planner(PlannerInfo *root, |
| 27 | + final_rel->cheapest_total_path->param_info != NULL) |
| 28 | + elog(ERROR, "failed to construct the join relation"); |
| 29 | + |
| 30 | ++ vscode_test_helper(root); |
| 31 | ++ |
| 32 | + return final_rel; |
| 33 | + } |
| 34 | +diff --git a/src/backend/optimizer/plan/vscodehelper.c b/src/backend/optimizer/plan/vscodehelper.c |
| 35 | +new file mode 100644 |
| 36 | +index 00000000000..7693688133b |
| 37 | +--- /dev/null |
| 38 | ++++ b/src/backend/optimizer/plan/vscodehelper.c |
| 39 | +@@ -0,0 +1,288 @@ |
| 40 | ++#include "postgres.h" |
| 41 | ++ |
| 42 | ++#include "nodes/bitmapset.h" |
| 43 | ++#include "nodes/pathnodes.h" |
| 44 | ++#include "utils/hsearch.h" |
| 45 | ++ |
| 46 | ++extern void vscode_test_helper(PlannerInfo *root); |
| 47 | ++extern void vscode_test_helper_unused(void *pointer); |
| 48 | ++ |
| 49 | ++#define ARRAY_SIZE 16 |
| 50 | ++#define UNUSED(x) ((void)x) |
| 51 | ++ |
| 52 | ++typedef struct TestStructure |
| 53 | ++{ |
| 54 | ++ int value; |
| 55 | ++} TestStructure; |
| 56 | ++ |
| 57 | ++typedef struct TestEmbeddedStructure |
| 58 | ++{ |
| 59 | ++ TestStructure value; |
| 60 | ++} TestEmbeddedStructure; |
| 61 | ++ |
| 62 | ++typedef struct TestPointerMember |
| 63 | ++{ |
| 64 | ++ TestStructure *value; |
| 65 | ++} TestPointerMember; |
| 66 | ++ |
| 67 | ++typedef struct TestFixedArray |
| 68 | ++{ |
| 69 | ++ int array[ARRAY_SIZE]; |
| 70 | ++} TestFixedArray; |
| 71 | ++ |
| 72 | ++typedef struct TestFlexibleArrayMember |
| 73 | ++{ |
| 74 | ++ int length; |
| 75 | ++ int array[FLEXIBLE_ARRAY_MEMBER]; |
| 76 | ++} TestFlexibleArrayMember; |
| 77 | ++ |
| 78 | ++typedef struct CustomListElement |
| 79 | ++{ |
| 80 | ++ int value; |
| 81 | ++} CustomListElement; |
| 82 | ++ |
| 83 | ++typedef struct CustomListVariable |
| 84 | ++{ |
| 85 | ++ List *value; |
| 86 | ++} CustomListVariable; |
| 87 | ++ |
| 88 | ++typedef struct TestHtabEntry |
| 89 | ++{ |
| 90 | ++ int key; |
| 91 | ++ int value; |
| 92 | ++} TestHtabEntry; |
| 93 | ++ |
| 94 | ++typedef struct SimpleHashEntry |
| 95 | ++{ |
| 96 | ++ uint32 status; |
| 97 | ++ uint32 hash; |
| 98 | ++ int key; |
| 99 | ++ int value; |
| 100 | ++} SimpleHashEntry; |
| 101 | ++ |
| 102 | ++typedef struct ArrayMember |
| 103 | ++{ |
| 104 | ++ int *array_field; |
| 105 | ++ int *array_expr; |
| 106 | ++ int length; |
| 107 | ++ int length2; |
| 108 | ++} ArrayMember; |
| 109 | ++ |
| 110 | ++typedef Expr *ExprAlias; |
| 111 | ++ |
| 112 | ++#define SH_PREFIX testhash |
| 113 | ++#define SH_ELEMENT_TYPE SimpleHashEntry |
| 114 | ++#define SH_KEY_TYPE int |
| 115 | ++#define SH_KEY key |
| 116 | ++#define SH_SCOPE static inline |
| 117 | ++#define SH_HASH_KEY(tb, key) ((uint32)(key)) |
| 118 | ++#define SH_EQUAL(tb, a, b) ((a) == (b)) |
| 119 | ++#define SH_STORE_HASH |
| 120 | ++#define SH_GET_HASH(tb, a) ((a)->hash) |
| 121 | ++#define SH_DECLARE |
| 122 | ++#define SH_DEFINE |
| 123 | ++#include "lib/simplehash.h" |
| 124 | ++ |
| 125 | ++static HTAB *create_htab(void); |
| 126 | ++static testhash_hash *create_simplehash(void); |
| 127 | ++static List *create_custom_list(void); |
| 128 | ++static ArrayMember *create_special_array_member(void); |
| 129 | ++static TestFlexibleArrayMember *create_flex_array_member(void); |
| 130 | ++ |
| 131 | ++static HTAB * |
| 132 | ++create_htab(void) |
| 133 | ++{ |
| 134 | ++ HASHCTL ctl; |
| 135 | ++ HTAB *htab; |
| 136 | ++ TestHtabEntry *entry; |
| 137 | ++ int key; |
| 138 | ++ |
| 139 | ++ ctl.keysize = sizeof(int); |
| 140 | ++ ctl.entrysize = sizeof(TestHtabEntry); |
| 141 | ++ htab = hash_create("Test VSCode HTAB", 8, &ctl, HASH_ELEM | HASH_BLOBS); |
| 142 | ++ |
| 143 | ++ key = 1; |
| 144 | ++ entry = (TestHtabEntry *)hash_search(htab, &key, HASH_ENTER, NULL); |
| 145 | ++ entry->value = 2; |
| 146 | ++ |
| 147 | ++ key = 10; |
| 148 | ++ entry = (TestHtabEntry *)hash_search(htab, &key, HASH_ENTER, NULL); |
| 149 | ++ entry->value = 4; |
| 150 | ++ |
| 151 | ++ key = 20; |
| 152 | ++ entry = (TestHtabEntry *)hash_search(htab, &key, HASH_ENTER, NULL); |
| 153 | ++ entry->value = 8; |
| 154 | ++ |
| 155 | ++ return htab; |
| 156 | ++} |
| 157 | ++ |
| 158 | ++static testhash_hash * |
| 159 | ++create_simplehash(void) |
| 160 | ++{ |
| 161 | ++ testhash_hash *hash; |
| 162 | ++ bool found; |
| 163 | ++ SimpleHashEntry *entry; |
| 164 | ++ |
| 165 | ++ hash = testhash_create(CurrentMemoryContext, 6, NULL); |
| 166 | ++ entry = testhash_insert(hash, 1, &found); |
| 167 | ++ entry->value = 2; |
| 168 | ++ entry = testhash_insert(hash, 10, &found); |
| 169 | ++ entry->value = 4; |
| 170 | ++ entry = testhash_insert(hash, 20, &found); |
| 171 | ++ entry->value = 8; |
| 172 | ++ |
| 173 | ++ return hash; |
| 174 | ++} |
| 175 | ++ |
| 176 | ++static List * |
| 177 | ++create_custom_list(void) |
| 178 | ++{ |
| 179 | ++ List *list = NIL; |
| 180 | ++ CustomListElement *element; |
| 181 | ++ |
| 182 | ++ element = palloc(sizeof(CustomListElement)); |
| 183 | ++ element->value = 1; |
| 184 | ++ list = lappend(list, element); |
| 185 | ++ |
| 186 | ++ element = palloc(sizeof(CustomListElement)); |
| 187 | ++ element->value = 2; |
| 188 | ++ list = lappend(list, element); |
| 189 | ++ |
| 190 | ++ element = palloc(sizeof(CustomListElement)); |
| 191 | ++ element->value = 3; |
| 192 | ++ list = lappend(list, element); |
| 193 | ++ |
| 194 | ++ return list; |
| 195 | ++} |
| 196 | ++ |
| 197 | ++static ArrayMember * |
| 198 | ++create_special_array_member(void) |
| 199 | ++{ |
| 200 | ++ ArrayMember *array = palloc(sizeof(ArrayMember)); |
| 201 | ++ array->length = 2; |
| 202 | ++ array->length2 = 2; |
| 203 | ++ array->array_field = palloc(sizeof(int) * array->length); |
| 204 | ++ array->array_expr = palloc(sizeof(int) * (array->length + array->length2)); |
| 205 | ++ array->array_field[0] = 1; |
| 206 | ++ array->array_field[1] = 2; |
| 207 | ++ array->array_expr[0] = 1; |
| 208 | ++ array->array_expr[1] = 2; |
| 209 | ++ array->array_expr[2] = 4; |
| 210 | ++ array->array_expr[3] = 8; |
| 211 | ++ return array; |
| 212 | ++} |
| 213 | ++ |
| 214 | ++static TestFlexibleArrayMember * |
| 215 | ++create_flex_array_member(void) |
| 216 | ++{ |
| 217 | ++ TestFlexibleArrayMember *member = palloc(offsetof(TestFlexibleArrayMember, array) + sizeof(int) * ARRAY_SIZE); |
| 218 | ++ member->length = ARRAY_SIZE; |
| 219 | ++ return member; |
| 220 | ++} |
| 221 | ++ |
| 222 | ++void |
| 223 | ++vscode_test_helper_unused(void *pointer) |
| 224 | ++{ |
| 225 | ++ /* This function is used to prevent symbol truncation in debug builds */ |
| 226 | ++ testhash_iterator iterator; |
| 227 | ++ testhash_hash *hash = (testhash_hash *)pointer; |
| 228 | ++ testhash_start_iterate(hash, &iterator); |
| 229 | ++ testhash_iterate(hash, &iterator); |
| 230 | ++} |
| 231 | ++ |
| 232 | ++void |
| 233 | ++vscode_test_helper(PlannerInfo *root) |
| 234 | ++{ |
| 235 | ++ /* Correct variable handling */ |
| 236 | ++ int i; |
| 237 | ++ int int_array[ARRAY_SIZE]; |
| 238 | ++ TestStructure structure_array[ARRAY_SIZE]; |
| 239 | ++ TestStructure *structure_pointer_array[ARRAY_SIZE]; |
| 240 | ++ TestStructure value_struct; |
| 241 | ++ TestStructure *pointer_struct; |
| 242 | ++ TestPointerMember pointer_member; |
| 243 | ++ TestEmbeddedStructure embedded_member; |
| 244 | ++ TestFixedArray fixed_size_array_member; |
| 245 | ++ |
| 246 | ++ /* Config file */ |
| 247 | ++ ArrayMember *array_member; |
| 248 | ++ ExprAlias expr_alias; |
| 249 | ++ CustomListVariable custom_list_variable; |
| 250 | ++ testhash_hash *simplehash; |
| 251 | ++ List *custom_list; |
| 252 | ++ HTAB *htab; |
| 253 | ++ TestFlexibleArrayMember *flexible_array_member; |
| 254 | ++ |
| 255 | ++ /* Node variables */ |
| 256 | ++ Bitmapset *bms; |
| 257 | ++ Node *node; |
| 258 | ++ Expr *expr; |
| 259 | ++ List *list; |
| 260 | ++ List *int_list; |
| 261 | ++ RestrictInfo *rinfo; |
| 262 | ++ EquivalenceClass *eclass; |
| 263 | ++ |
| 264 | ++ /* Protect against arbitrary query */ |
| 265 | ++ if (!( list_length(root->parse->rtable) == 3 |
| 266 | ++ && list_length(root->eq_classes) > 0 |
| 267 | ++ && list_length((root->simple_rel_array[1])->baserestrictinfo) > 0)) |
| 268 | ++ return; |
| 269 | ++ |
| 270 | ++ int_list = list_make4_int(1, 2, 4, 8); |
| 271 | ++ list = list_make3(root, root->parse, root->parse->rtable); |
| 272 | ++ custom_list = create_custom_list(); |
| 273 | ++ custom_list_variable.value = custom_list; |
| 274 | ++ node = (Node *) root; |
| 275 | ++ bms = bms_add_range(NULL, 5, 9); |
| 276 | ++ htab = create_htab(); |
| 277 | ++ simplehash = create_simplehash(); |
| 278 | ++ rinfo = (RestrictInfo *) linitial((root->simple_rel_array[1])->baserestrictinfo); |
| 279 | ++ expr = rinfo->clause; |
| 280 | ++ expr_alias = expr; |
| 281 | ++ eclass = (EquivalenceClass *) linitial(root->eq_classes); |
| 282 | ++ array_member = create_special_array_member(); |
| 283 | ++ flexible_array_member = create_flex_array_member(); |
| 284 | ++ |
| 285 | ++ i = 1; |
| 286 | ++ value_struct.value = 1; |
| 287 | ++ pointer_struct = palloc(sizeof(TestStructure)); |
| 288 | ++ pointer_struct->value = 1; |
| 289 | ++ embedded_member.value = value_struct; |
| 290 | ++ pointer_member.value = pointer_struct; |
| 291 | ++ embedded_member.value = value_struct; |
| 292 | ++ for (int j = 0; j < ARRAY_SIZE; j++) |
| 293 | ++ { |
| 294 | ++ fixed_size_array_member.array[j] = j + 1; |
| 295 | ++ int_array[j] = j + 1; |
| 296 | ++ structure_array[j].value = j + 1; |
| 297 | ++ structure_pointer_array[j] = palloc(sizeof(TestStructure)); |
| 298 | ++ structure_pointer_array[j]->value = j + 1; |
| 299 | ++ flexible_array_member->array[j] = j + 1; |
| 300 | ++ } |
| 301 | ++ |
| 302 | ++ UNUSED(bms); |
| 303 | ++ UNUSED(node); |
| 304 | ++ UNUSED(expr); |
| 305 | ++ UNUSED(list); |
| 306 | ++ UNUSED(int_list); |
| 307 | ++ UNUSED(htab); |
| 308 | ++ UNUSED(rinfo); |
| 309 | ++ UNUSED(eclass); |
| 310 | ++ UNUSED(array_member); |
| 311 | ++ UNUSED(expr_alias); |
| 312 | ++ UNUSED(simplehash); |
| 313 | ++ UNUSED(i); |
| 314 | ++ UNUSED(int_array); |
| 315 | ++ UNUSED(structure_array); |
| 316 | ++ UNUSED(structure_pointer_array); |
| 317 | ++ UNUSED(value_struct); |
| 318 | ++ UNUSED(pointer_struct); |
| 319 | ++ UNUSED(pointer_member); |
| 320 | ++ UNUSED(embedded_member); |
| 321 | ++ UNUSED(fixed_size_array_member); |
| 322 | ++ UNUSED(flexible_array_member); |
| 323 | ++ UNUSED(custom_list); |
| 324 | ++ UNUSED(custom_list_variable); |
| 325 | ++ |
| 326 | ++ return; |
| 327 | ++} |
| 328 | +\ No newline at end of file |
0 commit comments