joinoperator.c 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#include "executorimpl.h"
17 18 19 20
#include "function.h"
#include "os.h"
#include "querynodes.h"
#include "tcompare.h"
21
#include "tdatablock.h"
22
#include "thash.h"
23
#include "tmsg.h"
24 25
#include "ttypes.h"

26
static void         setJoinColumnInfo(SColumnInfo* pColumn, const SColumnNode* pColumnNode);
27
static SSDataBlock* doMergeJoin(struct SOperatorInfo* pOperator);
28
static void         destroyMergeJoinOperator(void* param);
29 30 31
static void         extractTimeCondition(SJoinOperatorInfo* pInfo, SOperatorInfo** pDownstream, int32_t numOfDownstream,
                                         SSortMergeJoinPhysiNode* pJoinNode);

S
slzhou 已提交
32 33
static void extractTimeCondition(SJoinOperatorInfo* pInfo, SOperatorInfo** pDownstream, int32_t numOfDownstream,
                                 SSortMergeJoinPhysiNode* pJoinNode) {
34 35 36
  SNode* pMergeCondition = pJoinNode->pMergeCondition;
  if (nodeType(pMergeCondition) == QUERY_NODE_OPERATOR) {
    SOperatorNode* pNode = (SOperatorNode*)pMergeCondition;
S
slzhou 已提交
37 38 39 40
    SColumnNode*   col1 = (SColumnNode*)pNode->pLeft;
    SColumnNode*   col2 = (SColumnNode*)pNode->pRight;
    SColumnNode*   leftTsCol = NULL;
    SColumnNode*   rightTsCol = NULL;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    if (col1->dataBlockId == pDownstream[0]->resultDataBlockId) {
      ASSERT(col2->dataBlockId == pDownstream[1]->resultDataBlockId);
      leftTsCol = col1;
      rightTsCol = col2;
    } else {
      ASSERT(col1->dataBlockId == pDownstream[1]->resultDataBlockId);
      ASSERT(col2->dataBlockId == pDownstream[0]->resultDataBlockId);
      leftTsCol = col2;
      rightTsCol = col1;
    }
    setJoinColumnInfo(&pInfo->leftCol, leftTsCol);
    setJoinColumnInfo(&pInfo->rightCol, rightTsCol);
  } else {
    ASSERT(false);
  }
}
57

58 59
SOperatorInfo* createMergeJoinOperatorInfo(SOperatorInfo** pDownstream, int32_t numOfDownstream,
                                           SSortMergeJoinPhysiNode* pJoinNode, SExecTaskInfo* pTaskInfo) {
60 61
  SJoinOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SJoinOperatorInfo));
  SOperatorInfo*     pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo));
H
Haojun Liao 已提交
62 63

  int32_t code = TSDB_CODE_SUCCESS;
64
  if (pOperator == NULL || pInfo == NULL) {
H
Haojun Liao 已提交
65
    code = TSDB_CODE_OUT_OF_MEMORY;
66 67 68
    goto _error;
  }

H
Haojun Liao 已提交
69
  int32_t      numOfCols = 0;
70
  SSDataBlock* pResBlock = createResDataBlock(pJoinNode->node.pOutputDataBlockDesc);
H
Haojun Liao 已提交
71
  SExprInfo*   pExprInfo = createExprInfo(pJoinNode->pTargets, NULL, &numOfCols);
72
  initResultSizeInfo(&pOperator->resultInfo, 4096);
73

74 75
  pInfo->pRes = pResBlock;
  pOperator->name = "MergeJoinOperator";
76
  pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN;
77 78 79 80 81 82
  pOperator->blocking = false;
  pOperator->status = OP_NOT_OPENED;
  pOperator->exprSupp.pExprInfo = pExprInfo;
  pOperator->exprSupp.numOfExprs = numOfCols;
  pOperator->info = pInfo;
  pOperator->pTaskInfo = pTaskInfo;
83

84
  extractTimeCondition(pInfo, pDownstream, numOfDownstream, pJoinNode);
85

86 87
  if (pJoinNode->pOnConditions != NULL && pJoinNode->node.pConditions != NULL) {
    pInfo->pCondAfterMerge = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
H
Haojun Liao 已提交
88 89 90 91 92
    if (pInfo->pCondAfterMerge == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _error;
    }

93 94
    SLogicConditionNode* pLogicCond = (SLogicConditionNode*)(pInfo->pCondAfterMerge);
    pLogicCond->pParameterList = nodesMakeList();
H
Haojun Liao 已提交
95 96 97 98 99
    if (pLogicCond->pParameterList == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _error;
    }

100 101 102 103 104 105 106 107 108
    nodesListMakeAppend(&pLogicCond->pParameterList, nodesCloneNode(pJoinNode->pOnConditions));
    nodesListMakeAppend(&pLogicCond->pParameterList, nodesCloneNode(pJoinNode->node.pConditions));
    pLogicCond->condType = LOGIC_COND_TYPE_AND;
  } else if (pJoinNode->pOnConditions != NULL) {
    pInfo->pCondAfterMerge = nodesCloneNode(pJoinNode->pOnConditions);
  } else if (pJoinNode->node.pConditions != NULL) {
    pInfo->pCondAfterMerge = nodesCloneNode(pJoinNode->node.pConditions);
  } else {
    pInfo->pCondAfterMerge = NULL;
109 110
  }

111
  pInfo->inputOrder = TSDB_ORDER_ASC;
112
  if (pJoinNode->inputTsOrder == ORDER_ASC) {
113
    pInfo->inputOrder = TSDB_ORDER_ASC;
114
  } else if (pJoinNode->inputTsOrder == ORDER_DESC) {
115
    pInfo->inputOrder = TSDB_ORDER_DESC;
116 117
  }

118
  pOperator->fpSet =
119
      createOperatorFpSet(operatorDummyOpenFn, doMergeJoin, NULL, NULL, destroyMergeJoinOperator, NULL);
H
Haojun Liao 已提交
120
  code = appendDownstream(pOperator, pDownstream, numOfDownstream);
121 122 123 124 125 126
  if (code != TSDB_CODE_SUCCESS) {
    goto _error;
  }

  return pOperator;

127
_error:
H
Haojun Liao 已提交
128 129 130 131
  if (pInfo != NULL) {
    destroyMergeJoinOperator(pInfo);
  }

132
  taosMemoryFree(pOperator);
H
Haojun Liao 已提交
133
  pTaskInfo->code = code;
134 135 136 137 138 139 140 141 142 143 144
  return NULL;
}

void setJoinColumnInfo(SColumnInfo* pColumn, const SColumnNode* pColumnNode) {
  pColumn->slotId = pColumnNode->slotId;
  pColumn->type = pColumnNode->node.resType.type;
  pColumn->bytes = pColumnNode->node.resType.bytes;
  pColumn->precision = pColumnNode->node.resType.precision;
  pColumn->scale = pColumnNode->node.resType.scale;
}

145
void destroyMergeJoinOperator(void* param) {
146
  SJoinOperatorInfo* pJoinOperator = (SJoinOperatorInfo*)param;
147
  nodesDestroyNode(pJoinOperator->pCondAfterMerge);
148

S
shenglian zhou 已提交
149
  pJoinOperator->pRes = blockDataDestroy(pJoinOperator->pRes);
D
dapan1121 已提交
150
  taosMemoryFreeClear(param);
151
}
152 153

static void mergeJoinJoinLeftRight(struct SOperatorInfo* pOperator, SSDataBlock* pRes, int32_t currRow,
154 155
                                   SSDataBlock* pLeftBlock, int32_t leftPos, SSDataBlock* pRightBlock,
                                   int32_t rightPos) {
156 157 158 159 160 161 162 163 164 165 166 167
  SJoinOperatorInfo* pJoinInfo = pOperator->info;

  for (int32_t i = 0; i < pOperator->exprSupp.numOfExprs; ++i) {
    SColumnInfoData* pDst = taosArrayGet(pRes->pDataBlock, i);

    SExprInfo* pExprInfo = &pOperator->exprSupp.pExprInfo[i];

    int32_t blockId = pExprInfo->base.pParam[0].pCol->dataBlockId;
    int32_t slotId = pExprInfo->base.pParam[0].pCol->slotId;
    int32_t rowIndex = -1;

    SColumnInfoData* pSrc = NULL;
168
    if (pLeftBlock->info.blockId == blockId) {
169 170
      pSrc = taosArrayGet(pLeftBlock->pDataBlock, slotId);
      rowIndex = leftPos;
171
    } else {
172 173
      pSrc = taosArrayGet(pRightBlock->pDataBlock, slotId);
      rowIndex = rightPos;
174
    }
175

176 177 178 179 180 181 182 183
    if (colDataIsNull_s(pSrc, rowIndex)) {
      colDataAppendNULL(pDst, currRow);
    } else {
      char* p = colDataGetData(pSrc, rowIndex);
      colDataAppend(pDst, currRow, p, false);
    }
  }
}
184
typedef struct SRowLocation {
185 186
  SSDataBlock* pDataBlock;
  int32_t      pos;
187 188
} SRowLocation;

189
// pBlock[tsSlotId][startPos, endPos) == timestamp,
S
shenglian zhou 已提交
190
static int32_t mergeJoinGetBlockRowsEqualTs(SSDataBlock* pBlock, int16_t tsSlotId, int32_t startPos, int64_t timestamp,
191
                                            int32_t* pEndPos, SArray* rowLocations, SArray* createdBlocks) {
192 193
  int32_t numRows = pBlock->info.rows;
  ASSERT(startPos < numRows);
S
shenglian zhou 已提交
194
  SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, tsSlotId);
195 196

  int32_t i = startPos;
197
  for (; i < numRows; ++i) {
198
    char* pNextVal = colDataGetData(pCol, i);
199
    if (timestamp != *(int64_t*)pNextVal) {
200 201 202 203
      break;
    }
  }
  int32_t endPos = i;
S
shenglian zhou 已提交
204
  *pEndPos = endPos;
205

206 207 208 209
  if (endPos - startPos == 0) {
    return 0;
  }

210
  SSDataBlock* block = pBlock;
S
slzhou 已提交
211
  bool         createdNewBlock = false;
S
shenglian zhou 已提交
212
  if (endPos == numRows) {
S
slzhou 已提交
213
    block = blockDataExtractBlock(pBlock, startPos, endPos - startPos);
S
shenglian zhou 已提交
214
    taosArrayPush(createdBlocks, &block);
215
    createdNewBlock = true;
216
  }
217
  SRowLocation location = {0};
218 219
  for (int32_t j = startPos; j < endPos; ++j) {
    location.pDataBlock = block;
S
slzhou 已提交
220
    location.pos = (createdNewBlock ? j - startPos : j);
221
    taosArrayPush(rowLocations, &location);
222 223 224
  }
  return 0;
}
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
// whichChild == 0, left child of join; whichChild ==1, right child of join
static int32_t mergeJoinGetDownStreamRowsEqualTimeStamp(SOperatorInfo* pOperator, int32_t whichChild, int16_t tsSlotId,
                                                        SSDataBlock* startDataBlock, int32_t startPos,
                                                        int64_t timestamp, SArray* rowLocations,
                                                        SArray* createdBlocks) {
  ASSERT(whichChild == 0 || whichChild == 1);

  SJoinOperatorInfo* pJoinInfo = pOperator->info;
  int32_t            endPos = -1;
  SSDataBlock*       dataBlock = startDataBlock;
  mergeJoinGetBlockRowsEqualTs(dataBlock, tsSlotId, startPos, timestamp, &endPos, rowLocations, createdBlocks);
  while (endPos == dataBlock->info.rows) {
    SOperatorInfo* ds = pOperator->pDownstream[whichChild];
    dataBlock = ds->fpSet.getNextFn(ds);
    if (whichChild == 0) {
      pJoinInfo->leftPos = 0;
      pJoinInfo->pLeft = dataBlock;
    } else if (whichChild == 1) {
      pJoinInfo->rightPos = 0;
      pJoinInfo->pRight = dataBlock;
    }

    if (dataBlock == NULL) {
      setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED);
      endPos = -1;
      break;
    }
253

254 255 256 257 258 259 260 261 262
    mergeJoinGetBlockRowsEqualTs(dataBlock, tsSlotId, 0, timestamp, &endPos, rowLocations, createdBlocks);
  }
  if (endPos != -1) {
    if (whichChild == 0) {
      pJoinInfo->leftPos = endPos;
    } else if (whichChild == 1) {
      pJoinInfo->rightPos = endPos;
    }
  }
263 264 265
  return 0;
}

266 267 268 269 270 271 272 273
static int32_t mergeJoinJoinDownstreamTsRanges(SOperatorInfo* pOperator, int64_t timestamp, SSDataBlock* pRes,
                                               int32_t* nRows) {
  SJoinOperatorInfo* pJoinInfo = pOperator->info;
  SArray*            leftRowLocations = taosArrayInit(8, sizeof(SRowLocation));
  SArray*            leftCreatedBlocks = taosArrayInit(8, POINTER_BYTES);

  SArray* rightRowLocations = taosArrayInit(8, sizeof(SRowLocation));
  SArray* rightCreatedBlocks = taosArrayInit(8, POINTER_BYTES);
274
  int32_t code = TSDB_CODE_SUCCESS;
275 276 277 278 279 280 281
  mergeJoinGetDownStreamRowsEqualTimeStamp(pOperator, 0, pJoinInfo->leftCol.slotId, pJoinInfo->pLeft,
                                           pJoinInfo->leftPos, timestamp, leftRowLocations, leftCreatedBlocks);
  mergeJoinGetDownStreamRowsEqualTimeStamp(pOperator, 1, pJoinInfo->rightCol.slotId, pJoinInfo->pRight,
                                           pJoinInfo->rightPos, timestamp, rightRowLocations, rightCreatedBlocks);

  size_t leftNumJoin = taosArrayGetSize(leftRowLocations);
  size_t rightNumJoin = taosArrayGetSize(rightRowLocations);
282 283
  code = blockDataEnsureCapacity(pRes, *nRows + leftNumJoin * rightNumJoin);
  if (code != TSDB_CODE_SUCCESS) {
H
Hongze Cheng 已提交
284 285
    qError("%s can not ensure block capacity for join. left: %zu, right: %zu", GET_TASKID(pOperator->pTaskInfo),
           leftNumJoin, rightNumJoin);
286 287
  }
  if (code == TSDB_CODE_SUCCESS) {
H
Hongze Cheng 已提交
288 289 290 291 292 293 294
    for (int32_t i = 0; i < leftNumJoin; ++i) {
      for (int32_t j = 0; j < rightNumJoin; ++j) {
        SRowLocation* leftRow = taosArrayGet(leftRowLocations, i);
        SRowLocation* rightRow = taosArrayGet(rightRowLocations, j);
        mergeJoinJoinLeftRight(pOperator, pRes, *nRows, leftRow->pDataBlock, leftRow->pos, rightRow->pDataBlock,
                               rightRow->pos);
        ++*nRows;
295
      }
H
Hongze Cheng 已提交
296
    }
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  }

  for (int i = 0; i < taosArrayGetSize(rightCreatedBlocks); ++i) {
    SSDataBlock* pBlock = taosArrayGetP(rightCreatedBlocks, i);
    blockDataDestroy(pBlock);
  }
  taosArrayDestroy(rightCreatedBlocks);
  taosArrayDestroy(rightRowLocations);
  for (int i = 0; i < taosArrayGetSize(leftCreatedBlocks); ++i) {
    SSDataBlock* pBlock = taosArrayGetP(leftCreatedBlocks, i);
    blockDataDestroy(pBlock);
  }
  taosArrayDestroy(leftCreatedBlocks);
  taosArrayDestroy(leftRowLocations);
  return TSDB_CODE_SUCCESS;
}

314 315
static bool mergeJoinGetNextTimestamp(SOperatorInfo* pOperator, int64_t* pLeftTs, int64_t* pRightTs) {
  SJoinOperatorInfo* pJoinInfo = pOperator->info;
316

317 318 319
  if (pJoinInfo->pLeft == NULL || pJoinInfo->leftPos >= pJoinInfo->pLeft->info.rows) {
    SOperatorInfo* ds1 = pOperator->pDownstream[0];
    pJoinInfo->pLeft = ds1->fpSet.getNextFn(ds1);
320

321 322 323 324
    pJoinInfo->leftPos = 0;
    if (pJoinInfo->pLeft == NULL) {
      setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED);
      return false;
325
    }
326
  }
327

328 329 330
  if (pJoinInfo->pRight == NULL || pJoinInfo->rightPos >= pJoinInfo->pRight->info.rows) {
    SOperatorInfo* ds2 = pOperator->pDownstream[1];
    pJoinInfo->pRight = ds2->fpSet.getNextFn(ds2);
331

332 333 334 335
    pJoinInfo->rightPos = 0;
    if (pJoinInfo->pRight == NULL) {
      setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED);
      return false;
336
    }
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  }
  // only the timestamp match support for ordinary table
  SColumnInfoData* pLeftCol = taosArrayGet(pJoinInfo->pLeft->pDataBlock, pJoinInfo->leftCol.slotId);
  char*            pLeftVal = colDataGetData(pLeftCol, pJoinInfo->leftPos);
  *pLeftTs = *(int64_t*)pLeftVal;

  SColumnInfoData* pRightCol = taosArrayGet(pJoinInfo->pRight->pDataBlock, pJoinInfo->rightCol.slotId);
  char*            pRightVal = colDataGetData(pRightCol, pJoinInfo->rightPos);
  *pRightTs = *(int64_t*)pRightVal;

  ASSERT(pLeftCol->info.type == TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT(pRightCol->info.type == TSDB_DATA_TYPE_TIMESTAMP);
  return true;
}

static void doMergeJoinImpl(struct SOperatorInfo* pOperator, SSDataBlock* pRes) {
  SJoinOperatorInfo* pJoinInfo = pOperator->info;
354

355
  int32_t nrows = pRes->info.rows;
356

357
  bool asc = (pJoinInfo->inputOrder == TSDB_ORDER_ASC) ? true : false;
358

359 360 361
  while (1) {
    int64_t leftTs = 0;
    int64_t rightTs = 0;
362
    bool    hasNextTs = mergeJoinGetNextTimestamp(pOperator, &leftTs, &rightTs);
363 364 365
    if (!hasNextTs) {
      break;
    }
366

367
    if (leftTs == rightTs) {
368
      mergeJoinJoinDownstreamTsRanges(pOperator, leftTs, pRes, &nrows);
369
    } else if (asc && leftTs < rightTs || !asc && leftTs > rightTs) {
370 371 372 373 374
      pJoinInfo->leftPos += 1;

      if (pJoinInfo->leftPos >= pJoinInfo->pLeft->info.rows) {
        continue;
      }
375
    } else if (asc && leftTs > rightTs || !asc && leftTs < rightTs) {
376 377 378 379 380 381 382 383 384 385 386 387
      pJoinInfo->rightPos += 1;
      if (pJoinInfo->rightPos >= pJoinInfo->pRight->info.rows) {
        continue;
      }
    }

    // the pDataBlock are always the same one, no need to call this again
    pRes->info.rows = nrows;
    if (pRes->info.rows >= pOperator->resultInfo.threshold) {
      break;
    }
  }
388 389 390 391
}

SSDataBlock* doMergeJoin(struct SOperatorInfo* pOperator) {
  SJoinOperatorInfo* pJoinInfo = pOperator->info;
392

393 394 395 396 397 398 399 400 401 402
  SSDataBlock* pRes = pJoinInfo->pRes;
  blockDataCleanup(pRes);
  blockDataEnsureCapacity(pRes, 4096);
  while (true) {
    int32_t numOfRowsBefore = pRes->info.rows;
    doMergeJoinImpl(pOperator, pRes);
    int32_t numOfNewRows = pRes->info.rows - numOfRowsBefore;
    if (numOfNewRows == 0) {
      break;
    }
403
    if (pJoinInfo->pCondAfterMerge != NULL) {
H
Haojun Liao 已提交
404
      doFilter(pJoinInfo->pCondAfterMerge, pRes, NULL, NULL);
405
    }
406 407 408 409
    if (pRes->info.rows >= pOperator->resultInfo.threshold) {
      break;
    }
  }
410 411
  return (pRes->info.rows > 0) ? pRes : NULL;
}