join_physical_operator.h 1.8 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by WangYunlai on 2021/6/10.
//

#pragma once

#include "sql/parser/parse.h"
#include "sql/operator/physical_operator.h"

/**
 * 最简单的两表(称为左表、右表)join算子
 * 依次遍历左表的每一行,然后关联右表的每一行
 */
羽飞's avatar
羽飞 已提交
24 25
class NestedLoopJoinPhysicalOperator : public PhysicalOperator
{
羽飞's avatar
羽飞 已提交
26 27 28 29
public:
  NestedLoopJoinPhysicalOperator();
  virtual ~NestedLoopJoinPhysicalOperator() = default;

L
Longda Feng 已提交
30 31 32 33 34
  PhysicalOperatorType type() const override
  {
    return PhysicalOperatorType::NESTED_LOOP_JOIN;
  }

羽飞's avatar
羽飞 已提交
35
  RC open(Trx *trx) override;
羽飞's avatar
羽飞 已提交
36 37 38 39 40 41 42
  RC next() override;
  RC close() override;
  Tuple *current_tuple() override;

private:
  RC left_next();   //! 左表遍历下一条数据
  RC right_next();  //! 右表遍历下一条数据,如果上一轮结束了就重新开始新的一轮
L
Longda Feng 已提交
43

羽飞's avatar
羽飞 已提交
44
private:
羽飞's avatar
羽飞 已提交
45 46
  Trx *trx_ = nullptr;

羽飞's avatar
羽飞 已提交
47 48 49 50 51 52 53 54 55
  //! 左表右表的真实对象是在PhysicalOperator::children_中,这里是为了写的时候更简单
  PhysicalOperator *left_ = nullptr;
  PhysicalOperator *right_ = nullptr;
  Tuple *left_tuple_ = nullptr;
  Tuple *right_tuple_ = nullptr;
  JoinedTuple joined_tuple_;  //! 当前关联的左右两个tuple
  bool round_done_ = true;    //! 右表遍历的一轮是否结束
  bool right_closed_ = true;  //! 右表算子是否已经关闭
};