控制算子----BitmapAnd和BitmapOr

引言

上一篇文章中介绍了Result算子,Result算子是最基本的算子之一。而今天要介绍的BitmapAnd算子和BitmapOr算子也很重要。

代码位置

BitmapAnd

src\gausskernel\runtime\executor\nodeBitmapAnd.cpp

BitmapOr

src\gausskernel\runtime\executor\nodeBitmapOr.cpp

功能作用

有关Bitmap(位图)的内容我在之前的文章:扫描算子—-nodeBitmapScan.cpp解析中介绍过了,可以点击此处跳转阅读。

BitmapAnd和BitmapOr顾名思义,就是对位图做与运算和或运算。如下图图解所示:

  • BitmapAnd

BitmapAnd

  • BitmapOr

BitmapOr

两者功能相似,这里只对BitmapAnd算子的代码做简要分析。

MultiExecBitmapAnd

MultiExecBitmapAnd函数是BitmapAnd算子中的关键函数,其代码如下:

 
  1. //代码清单1
  2. Node* MultiExecBitmapAnd(BitmapAndState* node)
  3. {
  4. PlanState** bitmapplans;
  5. int nplans;
  6. int i;
  7. TIDBitmap* result = NULL;
  8. /* 表示结点开始执行 */
  9. if (node->ps.instrument) {
  10. InstrStartNode(node->ps.instrument);
  11. }
  12. bitmapplans = node->bitmapplans;
  13. nplans = node->nplans;
  14. /* 遍历所有位图进行与操作 */
  15. for (i = 0; i < nplans; i++) {
  16. PlanState* subnode = bitmapplans[i];
  17. subnode->hbktScanSlot.currSlot = node->ps.hbktScanSlot.currSlot;
  18. TIDBitmap* subresult = NULL;
  19. /* 获取位图 */
  20. subresult = (TIDBitmap*)MultiExecProcNode(subnode);
  21. if (subresult == NULL || !IsA(subresult, TIDBitmap))
  22. ereport(ERROR,
  23. (errcode(ERRCODE_UNRECOGNIZED_NODE_TYPE),
  24. errmodule(MOD_EXECUTOR),
  25. errmsg("unrecognized result from subplan for BitmapAnd.")));
  26. if (result == NULL) {
  27. result = subresult; /* 初始化result为第一个子计划中的位图 */
  28. } else {
  29. /* result和subresult其中有一个是分区表索引(非全局索引),则把result设为分区表索引 */
  30. if (tbm_is_global(result) != tbm_is_global(subresult)) {
  31. tbm_set_global(result, false);
  32. }
  33. /* result和subresult取交集 */
  34. tbm_intersect(result, subresult);
  35. tbm_free(subresult);
  36. }
  37. /* 如果result为空,则可以提前退出循环,因为result将不会再发生改变 */
  38. if (tbm_is_empty(result)) {
  39. break;
  40. }
  41. }
  42. if (result == NULL) {
  43. ereport(ERROR,
  44. (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
  45. errmodule(MOD_EXECUTOR),
  46. errmsg("BitmapAnd doesn't support zero inputs")));
  47. }
  48. /* 表示结束此结点的执行 */
  49. if (node->ps.instrument) {
  50. InstrStopNode(node->ps.instrument, 0 /* XXX */);
  51. }
  52. return (Node*)result;
  53. }

输入参数

MultiExecBitmapAnd函数的输入参数为BitmapAndState类型的结构体指针,结构体定义如下:

 
  1. typedef struct BitmapAndState {
  2. PlanState ps; /* 父结点 */
  3. PlanState** bitmapplans; /* 需要与操作的位图 */
  4. int nplans; /* 需要与操作的位图数量 */
  5. } BitmapAndState;

执行过程

执行过程较为简单,具体直接见代码清单1中的注释。

小结

这篇文章里我介绍了BitmapAnd和BitmapAnd算子,这两个算子十分重要,主要是对BitmapIndexScan算子中返回的数据页位图进行与操作和或操作,得到最终的位图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值