* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
*
* openGauss 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.
* ---------------------------------------------------------------------------------------
*
* vechashjoin.h
* Prototypes for vectorized hash join
*
* IDENTIFICATION
* src/include/vecexecutor/vechashjoin.h
*
* ---------------------------------------------------------------------------------------
*/
#ifndef VECHASHJOIN_H_
#define VECHASHJOIN_H_
#include "vecexecutor/vechashtable.h"
#include "vecexecutor/vecnodes.h"
#include "workload/workload.h"
#define HASH_BUILD 0
#define HASH_PROBE 1
#define HASH_END 2
typedef enum {
HASH_JOIN_INNER = 0,
HASH_JOIN_LEFT,
HASH_JOIN_RIGHT,
HASH_JOIN_SEMI,
HASH_JOIN_ANTI,
HASH_JOIN_RIGHT_SEMI,
HASH_JOIN_RIGHT_ANTI,
HASH_JOIN_LEFT_ANTI_FULL,
HASH_JOIN_RIGHT_ANTI_FULL,
HASH_JOIN_TYPE_NUM
} hashJoinType;
#define MEMORY_HASH 0
#define GRACE_HASH 1
#define PROBE_FETCH 0
#define PROBE_PARTITION_FILE 1
#define PROBE_DATA 2
#define PROBE_FINAL 3
#define PROBE_PREPARE_PAIR 4
extern VecHashJoinState* ExecInitVecHashJoin(VecHashJoin* node, EState* estate, int eflags);
extern VectorBatch* ExecVecHashJoin(VecHashJoinState* node);
extern void ExecEndVecHashJoin(VecHashJoinState* node);
extern void ExecReScanVecHashJoin(VecHashJoinState* node);
extern long ExecGetMemCostVecHash(VecHashJoin*);
extern void ExecEarlyFreeVecHashJoin(VecHashJoinState* node);
struct JoinStateLog {
int lastBuildIdx;
hashCell* lastCell;
bool restore;
};
struct ReCheckCellLoc {
hashCell* cell;
int resultIdx;
int oriIdx;
};
class HashJoinTbl : public hashBasedOperator {
public:
HashJoinTbl(VecHashJoinState* runtimeContext);
void Build();
VectorBatch* Probe();
void ResetNecessary();
public:
int m_outCols;
int* m_outKeyIdx;
int* m_outOKeyIdx;
Oid* m_outKeyCollation;
List* m_cache;
int m_probeStatus;
bool m_complicateJoinKey;
ScalarVector* m_cjVector;
bool m_outSimple;
bool m_innerSimple;
bool m_doProbeData;
VectorBatch* m_innerBatch;
VectorBatch* m_outerBatch;
VectorBatch* m_complicate_innerBatch;
VectorBatch* m_complicate_outerBatch;
VectorBatch* m_inQualBatch;
VectorBatch* m_outQualBatch;
VectorBatch* m_outRawBatch;
VectorBatch* m_result;
VecHashJoinState* m_runtime;
hashJoinType m_joinType;
int m_strategy;
JoinStateLog m_joinStateLog;
hashOpSource* m_probOpSource;
ReCheckCellLoc m_reCheckCell[BatchMaxSize];
bool m_match[BatchMaxSize];
int m_probeIdx;
bool m_nulleqmatch[BatchMaxSize];
hashFileSource* m_buildFileSource;
hashFileSource* m_probeFileSource;
bool* m_simpletype;
Oid* m_outerkeyType;
uint8* m_pLevel;
uint8 m_maxPLevel;
bool* m_isValid;
hashCell** cellPoint;
bool m_isWarning;
double m_build_time;
double m_probe_time;
private:
void SetJoinType();
void PrepareProbe();
template <bool complicateJoinKey, bool NeedCopy>
void buildHashTable(hashSource* source, int64 rownum);
template <bool complicateJoinKey>
void bindingFp();
void initFile(bool buildSide, VectorBatch* templateBatch, int fileNum);
VectorBatch* probeMemory();
VectorBatch* probeGrace();
VectorBatch* probeHashTable(hashSource* probSource);
template <bool complicateJoinKey>
void probePartition();
template <bool complicateJoinKey, bool buildside>
void RePartitionFileSource(hashFileSource* hashSource, int fileIdx);
void recordPartitionInfo(bool buildside, int fileIdx, int istart, int iend);
void preparePartition();
void initMemoryControl();
int calcSpillFile();
VectorBatch* endJoin();
VectorBatch* buildResult(VectorBatch* inBatch, VectorBatch* outBatch, bool checkqual);
bool* checkQual(VectorBatch* inBatch, VectorBatch* outBatch);
template <typename innerType, typename outerType, bool simpleType, bool nulleqnull>
void matchKey(ScalarVector* key, int nrows, int hashValKeyIdx, int key_num);
bool simpletype(Oid type);
void matchComplicateKey(VectorBatch* batch);
void DispatchKeyInnerFunction(int KeyIdx);
template <typename innerType>
void DispatchKeyOuterFunction(int KeyIdx);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* innerJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* leftJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* leftJoinWithQualT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* rightJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* rightJoinWithQualT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* semiJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* semiJoinWithQualT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* antiJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* antiJoinWithQualT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* rightSemiJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* rightSemiJoinWithQualT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* rightAntiJoinT(VectorBatch* batch);
template <bool complicateJoinKey, bool simpleKey>
VectorBatch* rightAntiJoinWithQualT(VectorBatch* batch);
void PushDownFilterIfNeed();
private:
void (HashJoinTbl::*m_funBuild[2])(VectorBatch* batch);
void CalcComplicateHashVal(VectorBatch* batch, List* hashKeys, bool inner);
bool HasEnoughMem(int nrows);
template <bool complicateJoinKey, bool simple>
void SaveToMemory(VectorBatch* batch);
template <bool complicateJoinKey>
void flushToDisk();
template <bool complicateJoinKey, bool buildSide>
void SaveToDisk(VectorBatch* batch);
VectorBatch* (HashJoinTbl::*m_probeFun[2])();
VectorBatch* (HashJoinTbl::*m_joinFun)(VectorBatch* batch);
VectorBatch* (HashJoinTbl::*m_joinFunArray[36])(VectorBatch* batch);
typedef void (HashJoinTbl::*pMatchKeyFunc)(ScalarVector* key, int nrows, int hashValKeyIdx, int key_num);
pMatchKeyFunc* m_matchKeyFunction;
};
#endif