executil.c 17.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#include "os.h"
H
Hongze Cheng 已提交
17
#include "tmsg.h"
18
#include "thash.h"
19

20 21
#include "executil.h"
#include "executorimpl.h"
H
Haojun Liao 已提交
22
#include "tcompression.h"
H
Haojun Liao 已提交
23 24 25 26 27 28 29
#include "tlosertree.h"

typedef struct SCompSupporter {
  STableQueryInfo **pTableQueryInfo;
  int32_t          *rowIndex;
  int32_t           order;
} SCompSupporter;
30

H
Haojun Liao 已提交
31
int32_t getRowNumForMultioutput(STaskAttr* pQueryAttr, bool topBottomQuery, bool stable) {
W
wpan 已提交
32 33
  if (pQueryAttr && (!stable)) {
    for (int16_t i = 0; i < pQueryAttr->numOfOutput; ++i) {
34 35 36
//      if (pQueryAttr->pExpr1[i].base. == FUNCTION_TOP || pQueryAttr->pExpr1[i].base.functionId == FUNCTION_BOTTOM) {
//        return (int32_t)pQueryAttr->pExpr1[i].base.param[0].i;
//      }
W
wpan 已提交
37 38 39 40 41 42
    }
  }

  return 1;
}

H
Haojun Liao 已提交
43
int32_t getOutputInterResultBufSize(STaskAttr* pQueryAttr) {
H
Haojun Liao 已提交
44 45
  int32_t size = 0;

H
Haojun Liao 已提交
46
  for (int32_t i = 0; i < pQueryAttr->numOfOutput; ++i) {
47
//    size += pQueryAttr->pExpr1[i].base.interBytes;
H
Haojun Liao 已提交
48 49
  }

H
Haojun Liao 已提交
50
  assert(size >= 0);
H
Haojun Liao 已提交
51 52 53
  return size;
}

54
int32_t initResultRowInfo(SResultRowInfo *pResultRowInfo, int32_t size) {
H
Haojun Liao 已提交
55
  pResultRowInfo->size     = 0;
56
  pResultRowInfo->curPos  = -1;
57
  pResultRowInfo->capacity = size;
58

wafwerar's avatar
wafwerar 已提交
59 60
  pResultRowInfo->pResult = taosMemoryCalloc(pResultRowInfo->capacity, POINTER_BYTES);
  pResultRowInfo->pPosition = taosMemoryCalloc(pResultRowInfo->capacity, sizeof(SResultRowPosition));
61
  if (pResultRowInfo->pResult == NULL || pResultRowInfo->pPosition == NULL) {
B
Bomin Zhang 已提交
62 63
    return TSDB_CODE_QRY_OUT_OF_MEMORY;
  }
H
Haojun Liao 已提交
64

65 66 67
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
68
void cleanupResultRowInfo(SResultRowInfo *pResultRowInfo) {
H
Haojun Liao 已提交
69
  if (pResultRowInfo == NULL) {
70 71
    return;
  }
H
Haojun Liao 已提交
72

H
Haojun Liao 已提交
73 74
  if (pResultRowInfo->capacity == 0) {
    assert(pResultRowInfo->pResult == NULL);
75 76
    return;
  }
77

D
dapan1121 已提交
78
  for(int32_t i = 0; i < pResultRowInfo->size; ++i) {
D
dapan1121 已提交
79
    if (pResultRowInfo->pResult[i]) {
wafwerar's avatar
wafwerar 已提交
80
      taosMemoryFreeClear(pResultRowInfo->pResult[i]->key);
81 82
    }
  }
83
  
wafwerar's avatar
wafwerar 已提交
84
  taosMemoryFreeClear(pResultRowInfo->pResult);
85 86
}

H
Haojun Liao 已提交
87
void resetResultRowInfo(STaskRuntimeEnv *pRuntimeEnv, SResultRowInfo *pResultRowInfo) {
H
Haojun Liao 已提交
88
  if (pResultRowInfo == NULL || pResultRowInfo->capacity == 0) {
89 90
    return;
  }
H
Haojun Liao 已提交
91

H
Haojun Liao 已提交
92 93
  for (int32_t i = 0; i < pResultRowInfo->size; ++i) {
    SResultRow *pWindowRes = pResultRowInfo->pResult[i];
94
    clearResultRow(pRuntimeEnv, pWindowRes);
H
Haojun Liao 已提交
95 96 97 98 99 100

    int32_t groupIndex = 0;
    int64_t uid = 0;

    SET_RES_WINDOW_KEY(pRuntimeEnv->keyBuf, &groupIndex, sizeof(groupIndex), uid);
    taosHashRemove(pRuntimeEnv->pResultRowHashTable, (const char *)pRuntimeEnv->keyBuf, GET_RES_WINDOW_KEY_LEN(sizeof(groupIndex)));
101
  }
102 103

  pResultRowInfo->size     = 0;
104
  pResultRowInfo->curPos  = -1;
105 106
}

H
Haojun Liao 已提交
107
int32_t numOfClosedResultRows(SResultRowInfo *pResultRowInfo) {
108
  int32_t i = 0;
H
Haojun Liao 已提交
109
  while (i < pResultRowInfo->size && pResultRowInfo->pResult[i]->closed) {
110 111 112 113 114 115
    ++i;
  }
  
  return i;
}

H
Haojun Liao 已提交
116
void closeAllResultRows(SResultRowInfo *pResultRowInfo) {
H
Haojun Liao 已提交
117
  assert(pResultRowInfo->size >= 0 && pResultRowInfo->capacity >= pResultRowInfo->size);
118
  
H
Haojun Liao 已提交
119
  for (int32_t i = 0; i < pResultRowInfo->size; ++i) {
120 121
    SResultRow* pRow = pResultRowInfo->pResult[i];
    if (pRow->closed) {
122 123 124
      continue;
    }
    
125
    pRow->closed = true;
126 127 128
  }
}

H
Haojun Liao 已提交
129
bool isResultRowClosed(SResultRowInfo *pResultRowInfo, int32_t slot) {
H
Haojun Liao 已提交
130
  return (getResultRow(pResultRowInfo, slot)->closed == true);
131 132
}

H
Haojun Liao 已提交
133
void closeResultRow(SResultRowInfo *pResultRowInfo, int32_t slot) {
H
Haojun Liao 已提交
134
  getResultRow(pResultRowInfo, slot)->closed = true;
135 136
}

137
void clearResultRow(STaskRuntimeEnv *pRuntimeEnv, SResultRow *pResultRow) {
H
Haojun Liao 已提交
138
  if (pResultRow == NULL) {
139 140
    return;
  }
H
Haojun Liao 已提交
141

142
  // the result does not put into the SDiskbasedBuf, ignore it.
H
Haojun Liao 已提交
143
  if (pResultRow->pageId >= 0) {
144
    SFilePage *page = getBufPage(pRuntimeEnv->pResultBuf, pResultRow->pageId);
H
Haojun Liao 已提交
145

H
Haojun Liao 已提交
146
    int16_t offset = 0;
H
Haojun Liao 已提交
147
    for (int32_t i = 0; i < pRuntimeEnv->pQueryAttr->numOfOutput; ++i) {
148
      struct SResultRowEntryInfo *pEntryInfo = NULL;//pResultRow->pEntryInfo[i];
149

150
      int16_t size = pRuntimeEnv->pQueryAttr->pExpr1[i].base.resSchema.bytes;
H
Haojun Liao 已提交
151
      char * s = getPosInResultPage(pRuntimeEnv->pQueryAttr, page, pResultRow->offset, offset);
152 153
      memset(s, 0, size);

H
Haojun Liao 已提交
154
      offset += size;
155
      cleanupResultRowEntry(pEntryInfo);
156
    }
157
  }
158

H
Haojun Liao 已提交
159 160
  pResultRow->numOfRows = 0;
  pResultRow->pageId = -1;
H
Haojun Liao 已提交
161
  pResultRow->offset = -1;
H
Haojun Liao 已提交
162
  pResultRow->closed = false;
163

wafwerar's avatar
wafwerar 已提交
164
  taosMemoryFreeClear(pResultRow->key);
D
dapan1121 已提交
165
  pResultRow->win = TSWINDOW_INITIALIZER;
166 167
}

H
Haojun Liao 已提交
168
// TODO refactor: use macro
H
Haojun Liao 已提交
169
SResultRowEntryInfo* getResultCell(const SResultRow* pRow, int32_t index, int32_t* offset) {
H
Haojun Liao 已提交
170
  assert(index >= 0 && offset != NULL);
H
Haojun Liao 已提交
171
  return (SResultRowEntryInfo*)((char*) pRow->pEntryInfo + offset[index]);
H
Haojun Liao 已提交
172 173
}

174 175 176 177 178 179 180 181
size_t getResultRowSize(SqlFunctionCtx* pCtx, int32_t numOfOutput) {
  int32_t rowSize = (numOfOutput * sizeof(SResultRowEntryInfo)) + sizeof(SResultRow);

  for(int32_t i = 0; i < numOfOutput; ++i) {
    rowSize += pCtx[i].resDataInfo.interBufSize;
  }

  return rowSize;
182 183
}

H
Haojun Liao 已提交
184
SResultRow* getNewResultRow(SResultRowPool* p) {
185 186 187 188 189 190
  if (p == NULL) {
    return NULL;
  }

  void* ptr = NULL;
  if (p->position.pos == 0) {
wafwerar's avatar
wafwerar 已提交
191
    ptr = taosMemoryCalloc(1, p->blockSize);
192 193 194 195 196 197
    taosArrayPush(p->pData, &ptr);

  } else {
    size_t last = taosArrayGetSize(p->pData);

    void** pBlock = taosArrayGet(p->pData, last - 1);
H
Haojun Liao 已提交
198
    ptr = ((char*) (*pBlock)) + p->elemSize * p->position.pos;
199 200 201
  }

  p->position.pos = (p->position.pos + 1)%p->numOfElemPerBlock;
202 203
  initResultRow(ptr);

204 205 206
  return ptr;
}

H
Haojun Liao 已提交
207 208 209 210 211 212 213 214
void cleanupGroupResInfo(SGroupResInfo* pGroupResInfo) {
  assert(pGroupResInfo != NULL);

  taosArrayDestroy(pGroupResInfo->pRows);
  pGroupResInfo->pRows     = NULL;
  pGroupResInfo->index     = 0;
}

H
Haojun Liao 已提交
215
void initGroupResInfo(SGroupResInfo* pGroupResInfo, SResultRowInfo* pResultInfo) {
H
Haojun Liao 已提交
216 217 218 219
  if (pGroupResInfo->pRows != NULL) {
    taosArrayDestroy(pGroupResInfo->pRows);
  }

220
  pGroupResInfo->pRows = taosArrayFromList(pResultInfo->pPosition, pResultInfo->size, sizeof(SResultRowPosition));
H
Haojun Liao 已提交
221
  pGroupResInfo->index = 0;
H
Haojun Liao 已提交
222 223 224
  assert(pGroupResInfo->index <= getNumOfTotalRes(pGroupResInfo));
}

H
Haojun Liao 已提交
225
bool hasRemainDataInCurrentGroup(SGroupResInfo* pGroupResInfo) {
H
Haojun Liao 已提交
226 227 228 229 230 231 232
  if (pGroupResInfo->pRows == NULL) {
    return false;
  }

  return pGroupResInfo->index < taosArrayGetSize(pGroupResInfo->pRows);
}

H
Haojun Liao 已提交
233 234 235 236 237 238 239 240
bool hasRemainData(SGroupResInfo* pGroupResInfo) {
  if (hasRemainDataInCurrentGroup(pGroupResInfo)) {
    return true;
  }

  return pGroupResInfo->currentGroup < pGroupResInfo->totalGroup;
}

H
Haojun Liao 已提交
241 242 243 244 245 246 247 248 249 250
bool incNextGroup(SGroupResInfo* pGroupResInfo) {
  return (++pGroupResInfo->currentGroup) < pGroupResInfo->totalGroup;
}

int32_t getNumOfTotalRes(SGroupResInfo* pGroupResInfo) {
  assert(pGroupResInfo != NULL);
  if (pGroupResInfo->pRows == 0) {
    return 0;
  }

H
Haojun Liao 已提交
251
  return (int32_t) taosArrayGetSize(pGroupResInfo->pRows);
H
Haojun Liao 已提交
252 253
}

H
Haojun Liao 已提交
254 255
static int64_t getNumOfResultWindowRes(STaskRuntimeEnv* pRuntimeEnv, SResultRow *pResultRow, int32_t* rowCellInfoOffset) {
  STaskAttr* pQueryAttr = pRuntimeEnv->pQueryAttr;
H
Haojun Liao 已提交
256

H
Haojun Liao 已提交
257
  for (int32_t j = 0; j < pQueryAttr->numOfOutput; ++j) {
258
    int32_t functionId = 0;//pQueryAttr->pExpr1[j].base.functionId;
H
Haojun Liao 已提交
259 260 261 262 263

    /*
     * ts, tag, tagprj function can not decide the output number of current query
     * the number of output result is decided by main output
     */
264
    if (functionId == FUNCTION_TS || functionId == FUNCTION_TAG || functionId == FUNCTION_TAGPRJ) {
H
Haojun Liao 已提交
265 266 267
      continue;
    }

268 269 270 271 272 273
//    SResultRowEntryInfo *pResultInfo = getResultCell(pResultRow, j, rowCellInfoOffset);
//    assert(pResultInfo != NULL);
//
//    if (pResultInfo->numOfRes > 0) {
//      return pResultInfo->numOfRes;
//    }
H
Haojun Liao 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  }

  return 0;
}

static int32_t tableResultComparFn(const void *pLeft, const void *pRight, void *param) {
  int32_t left  = *(int32_t *)pLeft;
  int32_t right = *(int32_t *)pRight;

  SCompSupporter *  supporter = (SCompSupporter *)param;

  int32_t leftPos  = supporter->rowIndex[left];
  int32_t rightPos = supporter->rowIndex[right];

  /* left source is exhausted */
  if (leftPos == -1) {
    return 1;
  }

  /* right source is exhausted*/
  if (rightPos == -1) {
    return -1;
  }

  STableQueryInfo** pList = supporter->pTableQueryInfo;
H
Haojun Liao 已提交
299 300
  SResultRow* pWindowRes1 = pList[left]->resInfo.pResult[leftPos];
//  SResultRow * pWindowRes1 = getResultRow(&(pList[left]->resInfo), leftPos);
H
Haojun Liao 已提交
301 302
  TSKEY leftTimestamp = pWindowRes1->win.skey;

H
Haojun Liao 已提交
303 304 305
//  SResultRowInfo *pWindowResInfo2 = &(pList[right]->resInfo);
//  SResultRow * pWindowRes2 = getResultRow(pWindowResInfo2, rightPos);
  SResultRow* pWindowRes2 = pList[right]->resInfo.pResult[rightPos];
H
Haojun Liao 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318
  TSKEY rightTimestamp = pWindowRes2->win.skey;

  if (leftTimestamp == rightTimestamp) {
    return 0;
  }

  if (supporter->order == TSDB_ORDER_ASC) {
    return (leftTimestamp > rightTimestamp)? 1:-1;
  } else {
    return (leftTimestamp < rightTimestamp)? 1:-1;
  }
}

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
int32_t tsAscOrder(const void* p1, const void* p2) {
  SResultRowCell* pc1 = (SResultRowCell*) p1;
  SResultRowCell* pc2 = (SResultRowCell*) p2;

  if (pc1->groupId == pc2->groupId) {
    if (pc1->pRow->win.skey == pc2->pRow->win.skey) {
      return 0;
    } else {
      return (pc1->pRow->win.skey < pc2->pRow->win.skey)? -1:1;
    }
  } else {
    return (pc1->groupId < pc2->groupId)? -1:1;
  }
}

int32_t tsDescOrder(const void* p1, const void* p2) {
  SResultRowCell* pc1 = (SResultRowCell*) p1;
  SResultRowCell* pc2 = (SResultRowCell*) p2;

  if (pc1->groupId == pc2->groupId) {
    if (pc1->pRow->win.skey == pc2->pRow->win.skey) {
      return 0;
    } else {
      return (pc1->pRow->win.skey < pc2->pRow->win.skey)? 1:-1;
    }
  } else {
    return (pc1->groupId < pc2->groupId)? -1:1;
  }
}

H
Haojun Liao 已提交
349
void orderTheResultRows(STaskRuntimeEnv* pRuntimeEnv) {
350 351 352 353 354 355 356 357 358 359
  __compar_fn_t  fn = NULL;
  if (pRuntimeEnv->pQueryAttr->order.order == TSDB_ORDER_ASC) {
    fn = tsAscOrder;
  } else {
    fn = tsDescOrder;
  }

  taosArraySort(pRuntimeEnv->pResultRowArrayList, fn);
}

H
Haojun Liao 已提交
360
static int32_t mergeIntoGroupResultImplRv(STaskRuntimeEnv *pRuntimeEnv, SGroupResInfo* pGroupResInfo, uint64_t groupId, int32_t* rowCellInfoOffset) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  if (!pGroupResInfo->ordered) {
    orderTheResultRows(pRuntimeEnv);
    pGroupResInfo->ordered = true;
  }

  if (pGroupResInfo->pRows == NULL) {
    pGroupResInfo->pRows = taosArrayInit(100, POINTER_BYTES);
  }

  size_t len = taosArrayGetSize(pRuntimeEnv->pResultRowArrayList);
  for(; pGroupResInfo->position < len; ++pGroupResInfo->position) {
    SResultRowCell* pResultRowCell = taosArrayGet(pRuntimeEnv->pResultRowArrayList, pGroupResInfo->position);
    if (pResultRowCell->groupId != groupId) {
      break;
    }

    int64_t num = getNumOfResultWindowRes(pRuntimeEnv, pResultRowCell->pRow, rowCellInfoOffset);
    if (num <= 0) {
      continue;
    }

    taosArrayPush(pGroupResInfo->pRows, &pResultRowCell->pRow);
    pResultRowCell->pRow->numOfRows = (uint32_t) num;
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
389
static UNUSED_FUNC int32_t mergeIntoGroupResultImpl(STaskRuntimeEnv *pRuntimeEnv, SGroupResInfo* pGroupResInfo, SArray *pTableList,
H
Haojun Liao 已提交
390
    int32_t* rowCellInfoOffset) {
H
Haojun Liao 已提交
391
  bool ascQuery = QUERY_IS_ASC_QUERY(pRuntimeEnv->pQueryAttr);
H
Haojun Liao 已提交
392 393 394 395

  int32_t code = TSDB_CODE_SUCCESS;

  int32_t *posList = NULL;
396
  SMultiwayMergeTreeInfo *pTree = NULL;
H
Haojun Liao 已提交
397 398 399 400 401 402 403
  STableQueryInfo **pTableQueryInfoList = NULL;

  size_t size = taosArrayGetSize(pTableList);
  if (pGroupResInfo->pRows == NULL) {
    pGroupResInfo->pRows = taosArrayInit(100, POINTER_BYTES);
  }

wafwerar's avatar
wafwerar 已提交
404 405
  posList = taosMemoryCalloc(size, sizeof(int32_t));
  pTableQueryInfoList = taosMemoryMalloc(POINTER_BYTES * size);
H
Haojun Liao 已提交
406 407

  if (pTableQueryInfoList == NULL || posList == NULL || pGroupResInfo->pRows == NULL || pGroupResInfo->pRows == NULL) {
408
//    qError("QInfo:%"PRIu64" failed alloc memory", GET_TASKID(pRuntimeEnv));
H
Haojun Liao 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    goto _end;
  }

  int32_t numOfTables = 0;
  for (int32_t i = 0; i < size; ++i) {
    STableQueryInfo *item = taosArrayGetP(pTableList, i);
    if (item->resInfo.size > 0) {
      pTableQueryInfoList[numOfTables++] = item;
    }
  }

  // there is no data in current group
  // no need to merge results since only one table in each group
  if (numOfTables == 0) {
    goto _end;
  }

H
Haojun Liao 已提交
427
  SCompSupporter cs = {pTableQueryInfoList, posList, pRuntimeEnv->pQueryAttr->order.order};
H
Haojun Liao 已提交
428

429
  int32_t ret = tMergeTreeCreate(&pTree, numOfTables, &cs, tableResultComparFn);
H
Haojun Liao 已提交
430 431 432 433 434 435 436 437 438
  if (ret != TSDB_CODE_SUCCESS) {
    code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    goto _end;
  }

  int64_t lastTimestamp = ascQuery? INT64_MIN:INT64_MAX;
  int64_t startt = taosGetTimestampMs();

  while (1) {
439
    int32_t tableIndex = tMergeTreeGetChosenIndex(pTree);
H
Haojun Liao 已提交
440 441 442 443

    SResultRowInfo *pWindowResInfo = &pTableQueryInfoList[tableIndex]->resInfo;
    SResultRow  *pWindowRes = getResultRow(pWindowResInfo, cs.rowIndex[tableIndex]);

H
Haojun Liao 已提交
444
    int64_t num = getNumOfResultWindowRes(pRuntimeEnv, pWindowRes, rowCellInfoOffset);
H
Haojun Liao 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    if (num <= 0) {
      cs.rowIndex[tableIndex] += 1;

      if (cs.rowIndex[tableIndex] >= pWindowResInfo->size) {
        cs.rowIndex[tableIndex] = -1;
        if (--numOfTables == 0) { // all input sources are exhausted
          break;
        }
      }
    } else {
      assert((pWindowRes->win.skey >= lastTimestamp && ascQuery) || (pWindowRes->win.skey <= lastTimestamp && !ascQuery));

      if (pWindowRes->win.skey != lastTimestamp) {
        taosArrayPush(pGroupResInfo->pRows, &pWindowRes);
        pWindowRes->numOfRows = (uint32_t) num;
      }

      lastTimestamp = pWindowRes->win.skey;

      // move to the next row of current entry
      if ((++cs.rowIndex[tableIndex]) >= pWindowResInfo->size) {
        cs.rowIndex[tableIndex] = -1;

        // all input sources are exhausted
        if ((--numOfTables) == 0) {
          break;
        }
      }
    }

H
Haojun Liao 已提交
475
    tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree));
H
Haojun Liao 已提交
476 477 478 479
  }

  int64_t endt = taosGetTimestampMs();

480
//  qDebug("QInfo:%"PRIx64" result merge completed for group:%d, elapsed time:%" PRId64 " ms", GET_TASKID(pRuntimeEnv),
481
//         pGroupResInfo->currentGroup, endt - startt);
H
Haojun Liao 已提交
482 483

  _end:
wafwerar's avatar
wafwerar 已提交
484 485 486
  taosMemoryFreeClear(pTableQueryInfoList);
  taosMemoryFreeClear(posList);
  taosMemoryFreeClear(pTree);
H
Haojun Liao 已提交
487 488 489 490

  return code;
}

H
Haojun Liao 已提交
491
int32_t mergeIntoGroupResult(SGroupResInfo* pGroupResInfo, STaskRuntimeEnv* pRuntimeEnv, int32_t* offset) {
H
Haojun Liao 已提交
492 493 494
  int64_t st = taosGetTimestampUs();

  while (pGroupResInfo->currentGroup < pGroupResInfo->totalGroup) {
495
    mergeIntoGroupResultImplRv(pRuntimeEnv, pGroupResInfo, pGroupResInfo->currentGroup, offset);
H
Haojun Liao 已提交
496 497 498 499 500 501

    // this group generates at least one result, return results
    if (taosArrayGetSize(pGroupResInfo->pRows) > 0) {
      break;
    }

502
//    qDebug("QInfo:%"PRIu64" no result in group %d, continue", GET_TASKID(pRuntimeEnv), pGroupResInfo->currentGroup);
H
Haojun Liao 已提交
503 504 505 506
    cleanupGroupResInfo(pGroupResInfo);
    incNextGroup(pGroupResInfo);
  }

507
//  int64_t elapsedTime = taosGetTimestampUs() - st;
508
//  qDebug("QInfo:%"PRIu64" merge res data into group, index:%d, total group:%d, elapsed time:%" PRId64 "us", GET_TASKID(pRuntimeEnv),
509
//         pGroupResInfo->currentGroup, pGroupResInfo->totalGroup, elapsedTime);
H
Haojun Liao 已提交
510 511 512

  return TSDB_CODE_SUCCESS;
}
H
Haojun Liao 已提交
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
//void blockDistInfoToBinary(STableBlockDist* pDist, struct SBufferWriter* bw) {
//  tbufWriteUint32(bw, pDist->numOfTables);
//  tbufWriteUint16(bw, pDist->numOfFiles);
//  tbufWriteUint64(bw, pDist->totalSize);
//  tbufWriteUint64(bw, pDist->totalRows);
//  tbufWriteInt32(bw, pDist->maxRows);
//  tbufWriteInt32(bw, pDist->minRows);
//  tbufWriteUint32(bw, pDist->numOfRowsInMemTable);
//  tbufWriteUint32(bw, pDist->numOfSmallBlocks);
//  tbufWriteUint64(bw, taosArrayGetSize(pDist->dataBlockInfos));
//
//  // compress the binary string
//  char* p = TARRAY_GET_START(pDist->dataBlockInfos);
//
//  // compress extra bytes
//  size_t x = taosArrayGetSize(pDist->dataBlockInfos) * pDist->dataBlockInfos->elemSize;
wafwerar's avatar
wafwerar 已提交
530
//  char* tmp = taosMemoryMalloc(x + 2);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
//
//  bool comp = false;
//  int32_t len = tsCompressString(p, (int32_t)x, 1, tmp, (int32_t)x, ONE_STAGE_COMP, NULL, 0);
//  if (len == -1 || len >= x) { // compress failed, do not compress this binary data
//    comp = false;
//    len = (int32_t)x;
//  } else {
//    comp = true;
//  }
//
//  tbufWriteUint8(bw, comp);
//  tbufWriteUint32(bw, len);
//  if (comp) {
//    tbufWriteBinary(bw, tmp, len);
//  } else {
//    tbufWriteBinary(bw, p, len);
//  }
wafwerar's avatar
wafwerar 已提交
548
//  taosMemoryFreeClear(tmp);
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
//}

//void blockDistInfoFromBinary(const char* data, int32_t len, STableBlockDist* pDist) {
//  SBufferReader br = tbufInitReader(data, len, false);
//
//  pDist->numOfTables = tbufReadUint32(&br);
//  pDist->numOfFiles  = tbufReadUint16(&br);
//  pDist->totalSize   = tbufReadUint64(&br);
//  pDist->totalRows   = tbufReadUint64(&br);
//  pDist->maxRows     = tbufReadInt32(&br);
//  pDist->minRows     = tbufReadInt32(&br);
//  pDist->numOfRowsInMemTable = tbufReadUint32(&br);
//  pDist->numOfSmallBlocks = tbufReadUint32(&br);
//  int64_t numSteps = tbufReadUint64(&br);
//
//  bool comp = tbufReadUint8(&br);
//  uint32_t compLen = tbufReadUint32(&br);
//
//  size_t originalLen = (size_t) (numSteps *sizeof(SFileBlockInfo));
//
//  char* outputBuf = NULL;
//  if (comp) {
wafwerar's avatar
wafwerar 已提交
571
//    outputBuf = taosMemoryMalloc(originalLen);
572 573 574 575 576 577 578 579 580 581 582 583 584
//
//    size_t actualLen = compLen;
//    const char* compStr = tbufReadBinary(&br, &actualLen);
//
//    int32_t orignalLen = tsDecompressString(compStr, compLen, 1, outputBuf,
//                                            (int32_t)originalLen , ONE_STAGE_COMP, NULL, 0);
//    assert(orignalLen == numSteps *sizeof(SFileBlockInfo));
//  } else {
//    outputBuf = (char*) tbufReadBinary(&br, &originalLen);
//  }
//
//  pDist->dataBlockInfos = taosArrayFromList(outputBuf, (uint32_t)numSteps, sizeof(SFileBlockInfo));
//  if (comp) {
wafwerar's avatar
wafwerar 已提交
585
//    taosMemoryFreeClear(outputBuf);
586 587
//  }
//}
H
Haojun Liao 已提交
588