* This method recursively walks down the quals of an expression subquery to see if it can be pulled up to a join * and constructs the pieces necessary to perform the pullup. * E.g. SELECT * FROM outer o WHERE o.a < (SELECT avg(i.x) FROM inner i WHERE o.b = i.y) * This extracts interesting pieces of the subquery so as to create SELECT i.y, avg(i.x) from inner i GROUP by i.y */
| 334 | * This extracts interesting pieces of the subquery so as to create SELECT i.y, avg(i.x) from inner i GROUP by i.y |
| 335 | */ |
| 336 | static void |
| 337 | SubqueryToJoinWalker(Node *node, ConvertSubqueryToJoinContext *context) |
| 338 | { |
| 339 | Assert(context); |
| 340 | Assert(context->safeToConvert); |
| 341 | |
| 342 | if (node == NULL) |
| 343 | { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | if (IsA(node, BoolExpr)) |
| 348 | { |
| 349 | |
| 350 | /** |
| 351 | * Be extremely conservative. If there are any outer vars under an or or a not expression, then give up. |
| 352 | */ |
| 353 | if (is_notclause(node) |
| 354 | || is_orclause(node)) |
| 355 | { |
| 356 | if (contain_vars_of_level_or_above(node, 1)) |
| 357 | { |
| 358 | context->safeToConvert = false; |
| 359 | return; |
| 360 | } |
| 361 | context->innerQual = make_and_qual(context->innerQual, node); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | Assert(is_andclause(node)); |
| 366 | |
| 367 | BoolExpr *bexp = (BoolExpr *) node; |
| 368 | ListCell *lc = NULL; |
| 369 | |
| 370 | foreach(lc, bexp->args) |
| 371 | { |
| 372 | Node *arg = (Node *) lfirst(lc); |
| 373 | |
| 374 | /** |
| 375 | * If there is an outer var anywhere in the boolean expression, walk recursively. |
| 376 | */ |
| 377 | if (contain_vars_of_level_or_above(arg, 1)) |
| 378 | { |
| 379 | SubqueryToJoinWalker(arg, context); |
| 380 | |
| 381 | if (!context->safeToConvert) |
| 382 | { |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | /** |
| 389 | * This qual should be part of the subquery's inner qual. |
| 390 | */ |
| 391 | context->innerQual = make_and_qual(context->innerQual, arg); |
| 392 | } |
| 393 | } |
no test coverage detected