[关闭]
@Spongcer 2015-04-10T06:50:15.000000Z 字数 1419 阅读 1562

PostgreSQL-pull_ands-pull_ors

PG pull_ands pull_ors


pull_ands() 和 pull_ors() 的就是把树状结构的 AND、OR 操作拉平.

before pull_ands(): A = 1 and B = 1 and (C = 1 and D = 1)
after pull_ands(): A = 1 and B = 1 and C = 1 and D = 1

  1. /*
  2. * pull_ands
  3. * Recursively flatten nested AND clauses into a single and-clause list.
  4. *
  5. * Input is the arglist of an AND clause.
  6. * Returns the rebuilt arglist (note original list structure is not touched).
  7. */
  8. static List *
  9. pull_ands(List *andlist)
  10. {
  11. List *out_list = NIL;
  12. ListCell *arg;
  13. foreach(arg, andlist)
  14. {
  15. Node *subexpr = (Node *) lfirst(arg);
  16. /*
  17. * Note: we can destructively concat the subexpression's arglist
  18. * because we know the recursive invocation of pull_ands will have
  19. * built a new arglist not shared with any other expr. Otherwise we'd
  20. * need a list_copy here.
  21. */
  22. if (and_clause(subexpr))
  23. out_list = list_concat(out_list,
  24. pull_ands(((BoolExpr *) subexpr)->args));
  25. else
  26. out_list = lappend(out_list, subexpr);
  27. }
  28. return out_list;
  29. }
  30. /*
  31. * pull_ors
  32. * Recursively flatten nested OR clauses into a single or-clause list.
  33. *
  34. * Input is the arglist of an OR clause.
  35. * Returns the rebuilt arglist (note original list structure is not touched).
  36. */
  37. static List *
  38. pull_ors(List *orlist)
  39. {
  40. List *out_list = NIL;
  41. ListCell *arg;
  42. foreach(arg, orlist)
  43. {
  44. Node *subexpr = (Node *) lfirst(arg);
  45. /*
  46. * Note: we can destructively concat the subexpression's arglist
  47. * because we know the recursive invocation of pull_ors will have
  48. * built a new arglist not shared with any other expr. Otherwise we'd
  49. * need a list_copy here.
  50. */
  51. if (or_clause(subexpr))
  52. out_list = list_concat(out_list,
  53. pull_ors(((BoolExpr *) subexpr)->args));
  54. else
  55. out_list = lappend(out_list, subexpr);
  56. }
  57. return out_list;
  58. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注