tdatablock.c 43.4 KB
Newer Older
S
Shengliang Guan 已提交
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/>.
 */

#define _DEFAULT_SOURCE
H
Haojun Liao 已提交
17
#include "tdatablock.h"
S
compare  
Shengliang Guan 已提交
18
#include "tcompare.h"
19
#include "tglobal.h"
20
#include "tlog.h"
21

L
Liu Jicong 已提交
22
int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) {
H
Haojun Liao 已提交
23 24
  pEp->port = 0;
  strcpy(pEp->fqdn, ep);
25

L
Liu Jicong 已提交
26
  char* temp = strchr(pEp->fqdn, ':');
27 28
  if (temp) {
    *temp = 0;
L
Liu Jicong 已提交
29
    pEp->port = atoi(temp + 1);
30 31
  }

H
Haojun Liao 已提交
32
  if (pEp->port == 0) {
S
Shengliang Guan 已提交
33
    pEp->port = tsServerPort;
34 35 36 37 38 39
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
40
void addEpIntoEpSet(SEpSet* pEpSet, const char* fqdn, uint16_t port) {
H
Haojun Liao 已提交
41 42 43 44 45 46 47 48 49 50
  if (pEpSet == NULL || fqdn == NULL || strlen(fqdn) == 0) {
    return;
  }

  int32_t index = pEpSet->numOfEps;
  tstrncpy(pEpSet->eps[index].fqdn, fqdn, tListLen(pEpSet->eps[index].fqdn));
  pEpSet->eps[index].port = port;
  pEpSet->numOfEps += 1;
}

L
Liu Jicong 已提交
51
bool isEpsetEqual(const SEpSet* s1, const SEpSet* s2) {
52 53 54 55 56
  if (s1->numOfEps != s2->numOfEps || s1->inUse != s2->inUse) {
    return false;
  }

  for (int32_t i = 0; i < s1->numOfEps; i++) {
L
Liu Jicong 已提交
57
    if (s1->eps[i].port != s2->eps[i].port || strncmp(s1->eps[i].fqdn, s2->eps[i].fqdn, TSDB_FQDN_LEN) != 0)
58 59 60 61 62
      return false;
  }
  return true;
}

L
Liu Jicong 已提交
63
void updateEpSet_s(SCorEpSet* pEpSet, SEpSet* pNewEpSet) {
64 65 66 67 68
  taosCorBeginWrite(&pEpSet->version);
  pEpSet->epSet = *pNewEpSet;
  taosCorEndWrite(&pEpSet->version);
}

L
Liu Jicong 已提交
69
SEpSet getEpSet_s(SCorEpSet* pEpSet) {
70 71 72 73 74 75 76 77
  SEpSet ep = {0};
  taosCorBeginRead(&pEpSet->version);
  ep = pEpSet->epSet;
  taosCorEndRead(&pEpSet->version);

  return ep;
}

78
int32_t colDataGetLength(const SColumnInfoData* pColumnInfoData, int32_t numOfRows) {
H
Haojun Liao 已提交
79 80 81 82 83 84 85 86
  ASSERT(pColumnInfoData != NULL);
  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
    return pColumnInfoData->varmeta.length;
  } else {
    return pColumnInfoData->info.bytes * numOfRows;
  }
}

H
Haojun Liao 已提交
87 88 89 90 91 92 93 94
int32_t colDataGetFullLength(const SColumnInfoData* pColumnInfoData, int32_t numOfRows) {
  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
    return pColumnInfoData->varmeta.length + sizeof(int32_t) * numOfRows;
  } else {
    return pColumnInfoData->info.bytes * numOfRows + BitmapLen(numOfRows);
  }
}

H
Haojun Liao 已提交
95 96 97 98
void colDataTrim(SColumnInfoData* pColumnInfoData) {
  // TODO
}

99 100 101 102
int32_t colDataAppend(SColumnInfoData* pColumnInfoData, uint32_t currentRow, const char* pData, bool isNull) {
  ASSERT(pColumnInfoData != NULL);

  if (isNull) {
H
Haojun Liao 已提交
103 104
    // There is a placehold for each NULL value of binary or nchar type.
    if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
L
Liu Jicong 已提交
105
      pColumnInfoData->varmeta.offset[currentRow] = -1;  // it is a null value of VAR type.
H
Haojun Liao 已提交
106 107 108 109
    } else {
      colDataSetNull_f(pColumnInfoData->nullbitmap, currentRow);
    }

H
Haojun Liao 已提交
110
    pColumnInfoData->hasNull = true;
111 112 113 114 115
    return 0;
  }

  int32_t type = pColumnInfoData->info.type;
  if (IS_VAR_DATA_TYPE(type)) {
116 117 118 119 120 121 122 123 124 125 126 127 128
    int32_t dataLen = varDataTLen(pData);
    if(type == TSDB_DATA_TYPE_JSON) {
      if(*pData == TSDB_DATA_TYPE_NULL) {
        dataLen = 0;
      }else if(*pData == TSDB_DATA_TYPE_NCHAR) {
        dataLen = varDataTLen(pData+CHAR_BYTES);
      }else if(*pData == TSDB_DATA_TYPE_BIGINT || *pData == TSDB_DATA_TYPE_DOUBLE) {
        dataLen = LONG_BYTES;
      }else if(*pData == TSDB_DATA_TYPE_BOOL) {
        dataLen = CHAR_BYTES;
      }
      dataLen += CHAR_BYTES;
    }
H
Haojun Liao 已提交
129
    SVarColAttr* pAttr = &pColumnInfoData->varmeta;
130
    if (pAttr->allocLen < pAttr->length + dataLen) {
H
Haojun Liao 已提交
131 132 133 134 135
      uint32_t newSize = pAttr->allocLen;
      if (newSize == 0) {
        newSize = 8;
      }

136
      while (newSize < pAttr->length + dataLen) {
H
Haojun Liao 已提交
137 138 139
        newSize = newSize * 1.5;
      }

wafwerar's avatar
wafwerar 已提交
140
      char* buf = taosMemoryRealloc(pColumnInfoData->pData, newSize);
H
Haojun Liao 已提交
141
      if (buf == NULL) {
H
Haojun Liao 已提交
142
        return TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
143 144 145 146 147 148 149 150 151
      }

      pColumnInfoData->pData = buf;
      pAttr->allocLen = newSize;
    }

    uint32_t len = pColumnInfoData->varmeta.length;
    pColumnInfoData->varmeta.offset[currentRow] = len;

152 153
    memcpy(pColumnInfoData->pData + len, pData, dataLen);
    pColumnInfoData->varmeta.length += dataLen;
154
  } else {
wmmhello's avatar
wmmhello 已提交
155
    memcpy(pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow, pData, pColumnInfoData->info.bytes);
156 157 158 159 160
  }

  return 0;
}

L
Liu Jicong 已提交
161 162
static void doBitmapMerge(SColumnInfoData* pColumnInfoData, int32_t numOfRow1, const SColumnInfoData* pSource,
                          int32_t numOfRow2) {
wmmhello's avatar
wmmhello 已提交
163 164
  if (numOfRow2 <= 0) return;

H
Haojun Liao 已提交
165 166 167
  uint32_t total = numOfRow1 + numOfRow2;

  if (BitmapLen(numOfRow1) < BitmapLen(total)) {
wafwerar's avatar
wafwerar 已提交
168
    char*    tmp = taosMemoryRealloc(pColumnInfoData->nullbitmap, BitmapLen(total));
H
Haojun Liao 已提交
169 170 171 172 173 174 175 176 177 178
    uint32_t extend = BitmapLen(total) - BitmapLen(numOfRow1);
    memset(tmp + BitmapLen(numOfRow1), 0, extend);
    pColumnInfoData->nullbitmap = tmp;
  }

  uint32_t remindBits = BitPos(numOfRow1);
  uint32_t shiftBits = 8 - remindBits;

  if (remindBits == 0) {  // no need to shift bits of bitmap
    memcpy(pColumnInfoData->nullbitmap + BitmapLen(numOfRow1), pSource->nullbitmap, BitmapLen(numOfRow2));
wmmhello's avatar
wmmhello 已提交
179 180
    return;
  }
H
Haojun Liao 已提交
181

wmmhello's avatar
wmmhello 已提交
182 183
  uint8_t* p = (uint8_t*)pSource->nullbitmap;
  pColumnInfoData->nullbitmap[BitmapLen(numOfRow1) - 1] |= (p[0] >> remindBits);  // copy remind bits
H
Haojun Liao 已提交
184

wmmhello's avatar
wmmhello 已提交
185 186 187
  if (BitmapLen(numOfRow1) == BitmapLen(total)) {
    return;
  }
H
Haojun Liao 已提交
188

wmmhello's avatar
wmmhello 已提交
189 190
  int32_t len = BitmapLen(numOfRow2);
  int32_t i = 0;
H
Haojun Liao 已提交
191

wmmhello's avatar
wmmhello 已提交
192 193 194 195 196
  uint8_t* start = (uint8_t*)&pColumnInfoData->nullbitmap[BitmapLen(numOfRow1)];
  int32_t overCount = BitmapLen(total) - BitmapLen(numOfRow1);
  while (i < len) { // size limit of pSource->nullbitmap
    if (i >= 1) {
      start[i - 1] |= (p[i] >> remindBits); //copy remind bits
H
Haojun Liao 已提交
197
    }
wmmhello's avatar
wmmhello 已提交
198 199 200 201 202 203 204

    if (i >= overCount) { // size limit of pColumnInfoData->nullbitmap
      return;
    }

    start[i] |= (p[i] << shiftBits);  //copy shift bits
    i += 1;
H
Haojun Liao 已提交
205 206 207
  }
}

L
Liu Jicong 已提交
208 209
int32_t colDataMergeCol(SColumnInfoData* pColumnInfoData, uint32_t numOfRow1, const SColumnInfoData* pSource,
                        uint32_t numOfRow2) {
H
Haojun Liao 已提交
210 211 212 213 214 215
  ASSERT(pColumnInfoData != NULL && pSource != NULL && pColumnInfoData->info.type == pSource->info.type);

  if (numOfRow2 == 0) {
    return numOfRow1;
  }

wmmhello's avatar
wmmhello 已提交
216 217 218
  if (pSource->hasNull) {
    pColumnInfoData->hasNull = pSource->hasNull;
  }
219

H
Haojun Liao 已提交
220 221
  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
    // Handle the bitmap
wafwerar's avatar
wafwerar 已提交
222
    char* p = taosMemoryRealloc(pColumnInfoData->varmeta.offset, sizeof(int32_t) * (numOfRow1 + numOfRow2));
H
Haojun Liao 已提交
223 224 225 226
    if (p == NULL) {
      // TODO
    }

L
Liu Jicong 已提交
227 228
    pColumnInfoData->varmeta.offset = (int32_t*)p;
    for (int32_t i = 0; i < numOfRow2; ++i) {
229 230
      pColumnInfoData->varmeta.offset[i + numOfRow1] = pSource->varmeta.offset[i] + pColumnInfoData->varmeta.length;
    }
H
Haojun Liao 已提交
231

232
    // copy data
H
Haojun Liao 已提交
233 234 235
    uint32_t len = pSource->varmeta.length;
    uint32_t oldLen = pColumnInfoData->varmeta.length;
    if (pColumnInfoData->varmeta.allocLen < len + oldLen) {
wafwerar's avatar
wafwerar 已提交
236
      char* tmp = taosMemoryRealloc(pColumnInfoData->pData, len + oldLen);
H
Haojun Liao 已提交
237 238 239 240 241 242 243 244
      if (tmp == NULL) {
        return TSDB_CODE_VND_OUT_OF_MEMORY;
      }

      pColumnInfoData->pData = tmp;
      pColumnInfoData->varmeta.allocLen = len + oldLen;
    }

245 246
    memcpy(pColumnInfoData->pData + oldLen, pSource->pData, len);
    pColumnInfoData->varmeta.length = len + oldLen;
H
Haojun Liao 已提交
247 248 249 250
  } else {
    doBitmapMerge(pColumnInfoData, numOfRow1, pSource, numOfRow2);

    int32_t newSize = (numOfRow1 + numOfRow2) * pColumnInfoData->info.bytes;
wafwerar's avatar
wafwerar 已提交
251
    char*   tmp = taosMemoryRealloc(pColumnInfoData->pData, newSize);
H
Haojun Liao 已提交
252 253 254 255 256 257 258 259 260 261 262 263
    if (tmp == NULL) {
      return TSDB_CODE_VND_OUT_OF_MEMORY;
    }

    pColumnInfoData->pData = tmp;
    int32_t offset = pColumnInfoData->info.bytes * numOfRow1;
    memcpy(pColumnInfoData->pData + offset, pSource->pData, pSource->info.bytes * numOfRow2);
  }

  return numOfRow1 + numOfRow2;
}

264 265 266 267 268 269 270 271
int32_t colDataAssign(SColumnInfoData* pColumnInfoData, const SColumnInfoData* pSource, int32_t numOfRows) {
  ASSERT(pColumnInfoData != NULL && pSource != NULL && pColumnInfoData->info.type == pSource->info.type);
  if (numOfRows == 0) {
    return numOfRows;
  }

  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
    // Handle the bitmap
wafwerar's avatar
wafwerar 已提交
272
    char* p = taosMemoryRealloc(pColumnInfoData->varmeta.offset, sizeof(int32_t) * numOfRows);
273 274 275 276
    if (p == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

L
Liu Jicong 已提交
277
    pColumnInfoData->varmeta.offset = (int32_t*)p;
278 279 280
    memcpy(pColumnInfoData->varmeta.offset, pSource->varmeta.offset, sizeof(int32_t) * numOfRows);

    if (pColumnInfoData->varmeta.allocLen < pSource->varmeta.length) {
wafwerar's avatar
wafwerar 已提交
281
      char* tmp = taosMemoryRealloc(pColumnInfoData->pData, pSource->varmeta.length);
282 283 284 285 286 287 288 289 290 291 292
      if (tmp == NULL) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }

      pColumnInfoData->pData = tmp;
      pColumnInfoData->varmeta.allocLen = pSource->varmeta.length;
    }

    memcpy(pColumnInfoData->pData, pSource->pData, pSource->varmeta.length);
    pColumnInfoData->varmeta.length = pSource->varmeta.length;
  } else {
wafwerar's avatar
wafwerar 已提交
293
    char* tmp = taosMemoryRealloc(pColumnInfoData->nullbitmap, BitmapLen(numOfRows));
294 295 296 297 298 299 300 301
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

    pColumnInfoData->nullbitmap = tmp;
    memcpy(pColumnInfoData->nullbitmap, pSource->nullbitmap, BitmapLen(numOfRows));

    int32_t newSize = numOfRows * pColumnInfoData->info.bytes;
wafwerar's avatar
wafwerar 已提交
302
    tmp = taosMemoryRealloc(pColumnInfoData->pData, newSize);
303 304 305 306 307 308 309 310 311 312 313
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

    pColumnInfoData->pData = tmp;
    memcpy(pColumnInfoData->pData, pSource->pData, pSource->info.bytes * numOfRows);
  }

  return 0;
}

H
Haojun Liao 已提交
314
size_t blockDataGetNumOfCols(const SSDataBlock* pBlock) {
H
Haojun Liao 已提交
315
  ASSERT(pBlock && pBlock->info.numOfCols == taosArrayGetSize(pBlock->pDataBlock));
316 317 318
  return pBlock->info.numOfCols;
}

L
Liu Jicong 已提交
319
size_t blockDataGetNumOfRows(const SSDataBlock* pBlock) { return pBlock->info.rows; }
320

H
Haojun Liao 已提交
321
int32_t blockDataUpdateTsWindow(SSDataBlock* pDataBlock) {
322 323 324 325 326 327 328 329 330 331 332 333 334
  if (pDataBlock == NULL || pDataBlock->info.rows <= 0) {
    return 0;
  }

  if (pDataBlock->info.numOfCols <= 0) {
    return -1;
  }

  SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, 0);
  if (pColInfoData->info.type != TSDB_DATA_TYPE_TIMESTAMP) {
    return 0;
  }

L
Liu Jicong 已提交
335 336
  pDataBlock->info.window.skey = *(TSKEY*)colDataGetData(pColInfoData, 0);
  pDataBlock->info.window.ekey = *(TSKEY*)colDataGetData(pColInfoData, (pDataBlock->info.rows - 1));
337 338 339
  return 0;
}

340
// if pIndexMap = NULL, merger one column by on column
341
int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc, SArray* pIndexMap) {
wmmhello's avatar
wmmhello 已提交
342
  assert(pSrc != NULL && pDest != NULL);
H
Haojun Liao 已提交
343

wmmhello's avatar
wmmhello 已提交
344
  int32_t numOfCols = pDest->info.numOfCols;
L
Liu Jicong 已提交
345
  for (int32_t i = 0; i < numOfCols; ++i) {
346 347 348 349
    int32_t mapIndex = i;
    if(pIndexMap) {
      mapIndex = *(int32_t*)taosArrayGet(pIndexMap, i);
    }
H
Haojun Liao 已提交
350
    SColumnInfoData* pCol2 = taosArrayGet(pDest->pDataBlock, i);
351
    SColumnInfoData* pCol1 = taosArrayGet(pSrc->pDataBlock, mapIndex);
H
Haojun Liao 已提交
352

353 354
    uint32_t oldLen = colDataGetLength(pCol2, pDest->info.rows);
    uint32_t newLen = colDataGetLength(pCol1, pSrc->info.rows);
H
Haojun Liao 已提交
355 356

    int32_t newSize = oldLen + newLen;
wafwerar's avatar
wafwerar 已提交
357
    char*   tmp = taosMemoryRealloc(pCol2->pData, newSize);
H
Haojun Liao 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    if (tmp != NULL) {
      pCol2->pData = tmp;
      colDataMergeCol(pCol2, pDest->info.rows, pCol1, pSrc->info.rows);
    } else {
      return TSDB_CODE_VND_OUT_OF_MEMORY;
    }
  }

  pDest->info.rows += pSrc->info.rows;
  return TSDB_CODE_SUCCESS;
}

size_t blockDataGetSize(const SSDataBlock* pBlock) {
  assert(pBlock != NULL);

L
Liu Jicong 已提交
373
  size_t  total = 0;
H
Haojun Liao 已提交
374
  int32_t numOfCols = pBlock->info.numOfCols;
H
Haojun Liao 已提交
375

L
Liu Jicong 已提交
376
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
377
    SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
H
Haojun Liao 已提交
378
    total += colDataGetFullLength(pColInfoData, pBlock->info.rows);
H
Haojun Liao 已提交
379 380 381 382 383 384 385
  }

  return total;
}

// the number of tuples can be fit in one page.
// Actual data rows pluses the corresponding meta data must fit in one memory buffer of the given page size.
L
Liu Jicong 已提交
386 387
int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startIndex, int32_t* stopIndex,
                           int32_t pageSize) {
H
Haojun Liao 已提交
388 389 390 391 392
  ASSERT(pBlock != NULL && stopIndex != NULL);

  int32_t numOfCols = pBlock->info.numOfCols;
  int32_t numOfRows = pBlock->info.rows;

H
Haojun Liao 已提交
393 394
  int32_t bitmapChar = 1;

L
Liu Jicong 已提交
395
  size_t headerSize = sizeof(int32_t);
396
  size_t colHeaderSize = sizeof(int32_t) * numOfCols;
L
Liu Jicong 已提交
397
  size_t payloadSize = pageSize - (headerSize + colHeaderSize);
398

H
Haojun Liao 已提交
399
  // TODO speedup by checking if the whole page can fit in firstly.
H
Haojun Liao 已提交
400
  if (!hasVarCol) {
L
Liu Jicong 已提交
401
    size_t  rowSize = blockDataGetRowSize(pBlock);
402 403
    int32_t capacity = payloadSize / (rowSize + numOfCols * bitmapChar / 8.0);
    ASSERT(capacity > 0);
404

405
    *stopIndex = startIndex + capacity - 1;
H
Haojun Liao 已提交
406 407 408
    if (*stopIndex >= numOfRows) {
      *stopIndex = numOfRows - 1;
    }
409

H
Haojun Liao 已提交
410
    return TSDB_CODE_SUCCESS;
411
  }
wmmhello's avatar
wmmhello 已提交
412 413 414 415 416 417
  // iterate the rows that can be fit in this buffer page
  int32_t size = (headerSize + colHeaderSize);
  for (int32_t j = startIndex; j < numOfRows; ++j) {
    for (int32_t i = 0; i < numOfCols; ++i) {
      SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, i);
      if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
418
        if (pColInfoData->varmeta.offset[j] != -1) {
wmmhello's avatar
wmmhello 已提交
419 420
          char* p = colDataGetData(pColInfoData, j);
          size += varDataTLen(p);
H
Haojun Liao 已提交
421 422
        }

wmmhello's avatar
wmmhello 已提交
423
        size += sizeof(pColInfoData->varmeta.offset[0]);
424
      } else {
wmmhello's avatar
wmmhello 已提交
425
        size += pColInfoData->info.bytes;
H
Haojun Liao 已提交
426

wmmhello's avatar
wmmhello 已提交
427 428 429
        if (((j - startIndex) & 0x07) == 0) {
          size += 1;  // the space for null bitmap
        }
H
Haojun Liao 已提交
430 431 432
      }
    }

wmmhello's avatar
wmmhello 已提交
433
    if (size > pageSize) {  // pageSize must be able to hold one row
wmmhello's avatar
wmmhello 已提交
434
      *stopIndex = j - 1;
wmmhello's avatar
wmmhello 已提交
435
      ASSERT(*stopIndex >= startIndex);
wmmhello's avatar
wmmhello 已提交
436 437 438

      return TSDB_CODE_SUCCESS;
    }
H
Haojun Liao 已提交
439
  }
wmmhello's avatar
wmmhello 已提交
440 441 442 443 444

  // all fit in
  *stopIndex = numOfRows - 1;
  return TSDB_CODE_SUCCESS;

H
Haojun Liao 已提交
445 446
}

447 448 449 450 451
SSDataBlock* blockDataExtractBlock(SSDataBlock* pBlock, int32_t startIndex, int32_t rowCount) {
  if (pBlock == NULL || startIndex < 0 || rowCount > pBlock->info.rows || rowCount + startIndex > pBlock->info.rows) {
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
452
  SSDataBlock* pDst = taosMemoryCalloc(1, sizeof(SSDataBlock));
453 454 455 456
  if (pDst == NULL) {
    return NULL;
  }

457 458 459 460 461
  pDst->info = pBlock->info;

  pDst->info.rows = 0;
  pDst->pDataBlock = taosArrayInit(pBlock->info.numOfCols, sizeof(SColumnInfoData));

L
Liu Jicong 已提交
462 463
  for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) {
    SColumnInfoData  colInfo = {0};
464 465 466 467 468
    SColumnInfoData* pSrcCol = taosArrayGet(pBlock->pDataBlock, i);
    colInfo.info = pSrcCol->info;

    if (IS_VAR_DATA_TYPE(pSrcCol->info.type)) {
      SVarColAttr* pAttr = &colInfo.varmeta;
wafwerar's avatar
wafwerar 已提交
469
      pAttr->offset = taosMemoryCalloc(rowCount, sizeof(int32_t));
470
    } else {
wafwerar's avatar
wafwerar 已提交
471 472
      colInfo.nullbitmap = taosMemoryCalloc(1, BitmapLen(rowCount));
      colInfo.pData = taosMemoryCalloc(rowCount, colInfo.info.bytes);
473 474 475 476 477 478 479 480 481 482
    }

    taosArrayPush(pDst->pDataBlock, &colInfo);
  }

  for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) {
    SColumnInfoData* pColData = taosArrayGet(pBlock->pDataBlock, i);
    SColumnInfoData* pDstCol = taosArrayGet(pDst->pDataBlock, i);

    for (int32_t j = startIndex; j < (startIndex + rowCount); ++j) {
L
Liu Jicong 已提交
483
      bool  isNull = colDataIsNull(pColData, pBlock->info.rows, j, pBlock->pBlockAgg);
484
      char* p = colDataGetData(pColData, j);
485 486 487 488 489 490 491 492 493

      colDataAppend(pDstCol, j - startIndex, p, isNull);
    }
  }

  pDst->info.rows = rowCount;
  return pDst;
}

H
Haojun Liao 已提交
494 495
/**
 *
496 497 498 499 500
 * +------------------+---------------------------------------------+
 * |the number of rows|                    column #1                |
 * |    (4 bytes)     |------------+-----------------------+--------+
 * |                  | null bitmap| column length(4bytes) | values |
 * +------------------+------------+-----------------------+--------+
H
Haojun Liao 已提交
501 502 503 504
 * @param buf
 * @param pBlock
 * @return
 */
505
int32_t blockDataToBuf(char* buf, const SSDataBlock* pBlock) {
H
Haojun Liao 已提交
506 507 508
  ASSERT(pBlock != NULL);

  // write the number of rows
L
Liu Jicong 已提交
509
  *(uint32_t*)buf = pBlock->info.rows;
H
Haojun Liao 已提交
510 511 512 513 514 515

  int32_t numOfCols = pBlock->info.numOfCols;
  int32_t numOfRows = pBlock->info.rows;

  char* pStart = buf + sizeof(uint32_t);

L
Liu Jicong 已提交
516
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
517 518 519 520 521 522 523 524 525
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
      memcpy(pStart, pCol->varmeta.offset, numOfRows * sizeof(int32_t));
      pStart += numOfRows * sizeof(int32_t);
    } else {
      memcpy(pStart, pCol->nullbitmap, BitmapLen(numOfRows));
      pStart += BitmapLen(pBlock->info.rows);
    }

526
    uint32_t dataSize = colDataGetLength(pCol, numOfRows);
527

L
Liu Jicong 已提交
528
    *(int32_t*)pStart = dataSize;
529 530
    pStart += sizeof(int32_t);

H
Haojun Liao 已提交
531 532 533 534 535 536 537
    memcpy(pStart, pCol->pData, dataSize);
    pStart += dataSize;
  }

  return 0;
}

538
int32_t blockDataFromBuf(SSDataBlock* pBlock, const char* buf) {
L
Liu Jicong 已提交
539
  pBlock->info.rows = *(int32_t*)buf;
540

L
Liu Jicong 已提交
541
  int32_t     numOfCols = pBlock->info.numOfCols;
542 543
  const char* pStart = buf + sizeof(uint32_t);

L
Liu Jicong 已提交
544
  for (int32_t i = 0; i < numOfCols; ++i) {
545 546 547
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);

    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
H
Haojun Liao 已提交
548
      size_t metaSize = pBlock->info.rows * sizeof(int32_t);
549
      char* tmp = taosMemoryRealloc(pCol->varmeta.offset, metaSize);    // preview calloc is too small
wmmhello's avatar
wmmhello 已提交
550 551 552 553
      if (tmp == NULL) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      pCol->varmeta.offset = (int32_t*)tmp;
554 555 556 557 558 559 560
      memcpy(pCol->varmeta.offset, pStart, metaSize);
      pStart += metaSize;
    } else {
      memcpy(pCol->nullbitmap, pStart, BitmapLen(pBlock->info.rows));
      pStart += BitmapLen(pBlock->info.rows);
    }

L
Liu Jicong 已提交
561
    int32_t colLength = *(int32_t*)pStart;
562 563
    pStart += sizeof(int32_t);

564 565
    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
      if (pCol->varmeta.allocLen < colLength) {
wafwerar's avatar
wafwerar 已提交
566
        char* tmp = taosMemoryRealloc(pCol->pData, colLength);
567 568 569 570 571 572 573 574 575 576
        if (tmp == NULL) {
          return TSDB_CODE_OUT_OF_MEMORY;
        }

        pCol->pData = tmp;
        pCol->varmeta.allocLen = colLength;
      }

      pCol->varmeta.length = colLength;
      ASSERT(pCol->varmeta.length <= pCol->varmeta.allocLen);
577 578 579 580 581
    }

    memcpy(pCol->pData, pStart, colLength);
    pStart += colLength;
  }
582 583

  return TSDB_CODE_SUCCESS;
584 585
}

H
Haojun Liao 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
int32_t blockDataFromBuf1(SSDataBlock* pBlock, const char* buf, size_t capacity) {
  pBlock->info.rows = *(int32_t*)buf;

  int32_t     numOfCols = pBlock->info.numOfCols;
  const char* pStart = buf + sizeof(uint32_t);

  for (int32_t i = 0; i < numOfCols; ++i) {
    SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, i);

    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
      size_t metaSize = capacity * sizeof(int32_t);
      memcpy(pCol->varmeta.offset, pStart, metaSize);
      pStart += metaSize;
    } else {
      memcpy(pCol->nullbitmap, pStart, BitmapLen(capacity));
      pStart += BitmapLen(capacity);
    }

    int32_t colLength = *(int32_t*)pStart;
    pStart += sizeof(int32_t);

    if (IS_VAR_DATA_TYPE(pCol->info.type)) {
      if (pCol->varmeta.allocLen < colLength) {
        char* tmp = taosMemoryRealloc(pCol->pData, colLength);
        if (tmp == NULL) {
          return TSDB_CODE_OUT_OF_MEMORY;
        }

        pCol->pData = tmp;
        pCol->varmeta.allocLen = colLength;
      }

      pCol->varmeta.length = colLength;
      ASSERT(pCol->varmeta.length <= pCol->varmeta.allocLen);
    }

    memcpy(pCol->pData, pStart, colLength);
623
    pStart += pCol->info.bytes * capacity;
H
Haojun Liao 已提交
624 625 626 627 628
  }

  return TSDB_CODE_SUCCESS;
}

629
size_t blockDataGetRowSize(SSDataBlock* pBlock) {
H
Haojun Liao 已提交
630
  ASSERT(pBlock != NULL);
631 632
  if (pBlock->info.rowSize == 0) {
    size_t rowSize = 0;
H
Haojun Liao 已提交
633

L
Liu Jicong 已提交
634 635 636 637 638
    size_t numOfCols = pBlock->info.numOfCols;
    for (int32_t i = 0; i < numOfCols; ++i) {
      SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, i);
      rowSize += pColInfo->info.bytes;
    }
639 640

    pBlock->info.rowSize = rowSize;
H
Haojun Liao 已提交
641 642
  }

643
  return pBlock->info.rowSize;
H
Haojun Liao 已提交
644 645
}

H
Haojun Liao 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
/**
 * @refitem blockDataToBuf for the meta size
 *
 * @param pBlock
 * @return
 */
size_t blockDataGetSerialMetaSize(const SSDataBlock* pBlock) {
  return sizeof(int32_t) + pBlock->info.numOfCols * sizeof(int32_t);
}

double blockDataGetSerialRowSize(const SSDataBlock* pBlock) {
  ASSERT(pBlock != NULL);
  double rowSize = 0;

  size_t numOfCols = pBlock->info.numOfCols;
L
Liu Jicong 已提交
661
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
662 663 664 665 666 667
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, i);
    rowSize += pColInfo->info.bytes;

    if (IS_VAR_DATA_TYPE(pColInfo->info.type)) {
      rowSize += sizeof(int32_t);
    } else {
L
Liu Jicong 已提交
668
      rowSize += 1 / 8.0;  // one bit for each record
H
Haojun Liao 已提交
669 670 671 672 673 674
    }
  }

  return rowSize;
}

H
Haojun Liao 已提交
675
typedef struct SSDataBlockSortHelper {
L
Liu Jicong 已提交
676 677
  SArray*      orderInfo;  // SArray<SBlockOrderInfo>
  SSDataBlock* pDataBlock;
H
Haojun Liao 已提交
678 679 680
} SSDataBlockSortHelper;

int32_t dataBlockCompar(const void* p1, const void* p2, const void* param) {
L
Liu Jicong 已提交
681
  const SSDataBlockSortHelper* pHelper = (const SSDataBlockSortHelper*)param;
H
Haojun Liao 已提交
682 683 684

  SSDataBlock* pDataBlock = pHelper->pDataBlock;

L
Liu Jicong 已提交
685 686
  int32_t left = *(int32_t*)p1;
  int32_t right = *(int32_t*)p2;
H
Haojun Liao 已提交
687 688

  SArray* pInfo = pHelper->orderInfo;
689

L
Liu Jicong 已提交
690
  for (int32_t i = 0; i < pInfo->size; ++i) {
H
Haojun Liao 已提交
691
    SBlockOrderInfo* pOrder = TARRAY_GET_ELEM(pInfo, i);
L
Liu Jicong 已提交
692
    SColumnInfoData* pColInfoData = pOrder->pColData;  // TARRAY_GET_ELEM(pDataBlock->pDataBlock, pOrder->colIndex);
H
Haojun Liao 已提交
693

694
    if (pColInfoData->hasNull) {
L
Liu Jicong 已提交
695
      bool leftNull = colDataIsNull(pColInfoData, pDataBlock->info.rows, left, pDataBlock->pBlockAgg);
H
Haojun Liao 已提交
696
      bool rightNull = colDataIsNull(pColInfoData, pDataBlock->info.rows, right, pDataBlock->pBlockAgg);
697
      if (leftNull && rightNull) {
L
Liu Jicong 已提交
698
        continue;  // continue to next slot
699
      }
H
Haojun Liao 已提交
700

701
      if (rightNull) {
H
Haojun Liao 已提交
702
        return pOrder->nullFirst ? 1 : -1;
703
      }
H
Haojun Liao 已提交
704

705
      if (leftNull) {
H
Haojun Liao 已提交
706
        return pOrder->nullFirst ? -1 : 1;
707
      }
H
Haojun Liao 已提交
708 709
    }

L
Liu Jicong 已提交
710
    void* left1 = colDataGetData(pColInfoData, left);
711
    void* right1 = colDataGetData(pColInfoData, right);
H
Haojun Liao 已提交
712

713
    __compar_fn_t fn = getKeyComparFunc(pColInfoData->info.type, pOrder->order);
714

715 716 717 718 719
    int ret = fn(left1, right1);
    if (ret == 0) {
      continue;
    } else {
      return ret;
H
Haojun Liao 已提交
720 721 722 723 724 725
    }
  }

  return 0;
}

L
Liu Jicong 已提交
726 727
static int32_t doAssignOneTuple(SColumnInfoData* pDstCols, int32_t numOfRows, const SSDataBlock* pSrcBlock,
                                int32_t tupleIndex) {
H
Haojun Liao 已提交
728
  int32_t code = 0;
H
Haojun Liao 已提交
729 730 731 732 733 734
  int32_t numOfCols = pSrcBlock->info.numOfCols;

  for (int32_t i = 0; i < numOfCols; ++i) {
    SColumnInfoData* pDst = &pDstCols[i];
    SColumnInfoData* pSrc = taosArrayGet(pSrcBlock->pDataBlock, i);

H
Haojun Liao 已提交
735
    if (pSrc->hasNull && colDataIsNull(pSrc, pSrcBlock->info.rows, tupleIndex, pSrcBlock->pBlockAgg)) {
H
Haojun Liao 已提交
736 737 738 739
      code = colDataAppend(pDst, numOfRows, NULL, true);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
H
Haojun Liao 已提交
740
    } else {
741
      char* p = colDataGetData(pSrc, tupleIndex);
H
Haojun Liao 已提交
742 743 744 745
      code = colDataAppend(pDst, numOfRows, p, false);
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }
H
Haojun Liao 已提交
746 747
    }
  }
748 749

  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
750 751
}

H
Haojun Liao 已提交
752
static int32_t blockDataAssign(SColumnInfoData* pCols, const SSDataBlock* pDataBlock, int32_t* index) {
H
Haojun Liao 已提交
753
#if 0
H
Haojun Liao 已提交
754
  for (int32_t i = 0; i < pDataBlock->info.rows; ++i) {
H
Haojun Liao 已提交
755 756 757 758
    int32_t code = doAssignOneTuple(pCols, i, pDataBlock, index[i]);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
H
Haojun Liao 已提交
759
  }
H
Haojun Liao 已提交
760
#else
L
Liu Jicong 已提交
761
  for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) {
H
Haojun Liao 已提交
762 763 764 765
    SColumnInfoData* pDst = &pCols[i];
    SColumnInfoData* pSrc = taosArrayGet(pDataBlock->pDataBlock, i);

    if (IS_VAR_DATA_TYPE(pSrc->info.type)) {
L
Liu Jicong 已提交
766 767
      memcpy(pDst->pData, pSrc->pData, pSrc->varmeta.length);
      pDst->varmeta.length = pSrc->varmeta.length;
H
Haojun Liao 已提交
768

L
Liu Jicong 已提交
769 770 771
      for (int32_t j = 0; j < pDataBlock->info.rows; ++j) {
        pDst->varmeta.offset[j] = pSrc->varmeta.offset[index[j]];
      }
H
Haojun Liao 已提交
772
    } else {
wmmhello's avatar
wmmhello 已提交
773 774 775 776
      for (int32_t j = 0; j < pDataBlock->info.rows; ++j) {
        if (colDataIsNull_f(pSrc->nullbitmap, index[j])) {
          colDataSetNull_f(pDst->nullbitmap, j);
          continue;
H
Haojun Liao 已提交
777
        }
wmmhello's avatar
wmmhello 已提交
778
        memcpy(pDst->pData + j * pDst->info.bytes, pSrc->pData + index[j] * pDst->info.bytes, pDst->info.bytes);
H
Haojun Liao 已提交
779 780 781 782
      }
    }
  }
#endif
H
Haojun Liao 已提交
783
  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
784 785 786 787 788 789
}

static SColumnInfoData* createHelpColInfoData(const SSDataBlock* pDataBlock) {
  int32_t rows = pDataBlock->info.rows;
  int32_t numOfCols = pDataBlock->info.numOfCols;

wafwerar's avatar
wafwerar 已提交
790
  SColumnInfoData* pCols = taosMemoryCalloc(numOfCols, sizeof(SColumnInfoData));
H
Haojun Liao 已提交
791 792 793 794
  if (pCols == NULL) {
    return NULL;
  }

L
Liu Jicong 已提交
795
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
796 797 798 799
    SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, i);
    pCols[i].info = pColInfoData->info;

    if (IS_VAR_DATA_TYPE(pCols[i].info.type)) {
wafwerar's avatar
wafwerar 已提交
800 801
      pCols[i].varmeta.offset = taosMemoryCalloc(rows, sizeof(int32_t));
      pCols[i].pData = taosMemoryCalloc(1, pColInfoData->varmeta.length);
H
Haojun Liao 已提交
802 803 804

      pCols[i].varmeta.length = pColInfoData->varmeta.length;
      pCols[i].varmeta.allocLen = pCols[i].varmeta.length;
H
Haojun Liao 已提交
805
    } else {
wafwerar's avatar
wafwerar 已提交
806 807
      pCols[i].nullbitmap = taosMemoryCalloc(1, BitmapLen(rows));
      pCols[i].pData = taosMemoryCalloc(rows, pCols[i].info.bytes);
H
Haojun Liao 已提交
808 809 810 811 812 813
    }
  }

  return pCols;
}

H
Haojun Liao 已提交
814
static void copyBackToBlock(SSDataBlock* pDataBlock, SColumnInfoData* pCols) {
H
Haojun Liao 已提交
815 816
  int32_t numOfCols = pDataBlock->info.numOfCols;

L
Liu Jicong 已提交
817
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
818 819 820 821
    SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, i);
    pColInfoData->info = pCols[i].info;

    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
wafwerar's avatar
wafwerar 已提交
822
      taosMemoryFreeClear(pColInfoData->varmeta.offset);
H
Haojun Liao 已提交
823 824
      pColInfoData->varmeta = pCols[i].varmeta;
    } else {
wafwerar's avatar
wafwerar 已提交
825
      taosMemoryFreeClear(pColInfoData->nullbitmap);
H
Haojun Liao 已提交
826 827 828
      pColInfoData->nullbitmap = pCols[i].nullbitmap;
    }

wafwerar's avatar
wafwerar 已提交
829
    taosMemoryFreeClear(pColInfoData->pData);
H
Haojun Liao 已提交
830 831 832
    pColInfoData->pData = pCols[i].pData;
  }

wafwerar's avatar
wafwerar 已提交
833
  taosMemoryFreeClear(pCols);
H
Haojun Liao 已提交
834 835 836
}

static int32_t* createTupleIndex(size_t rows) {
wafwerar's avatar
wafwerar 已提交
837
  int32_t* index = taosMemoryCalloc(rows, sizeof(int32_t));
H
Haojun Liao 已提交
838 839 840 841
  if (index == NULL) {
    return NULL;
  }

L
Liu Jicong 已提交
842
  for (int32_t i = 0; i < rows; ++i) {
H
Haojun Liao 已提交
843 844 845 846 847 848
    index[i] = i;
  }

  return index;
}

wafwerar's avatar
wafwerar 已提交
849
static void destroyTupleIndex(int32_t* index) { taosMemoryFreeClear(index); }
H
Haojun Liao 已提交
850

H
Haojun Liao 已提交
851
int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) {
H
Haojun Liao 已提交
852 853 854 855 856 857 858
  ASSERT(pDataBlock != NULL && pOrderInfo != NULL);
  if (pDataBlock->info.rows <= 1) {
    return TSDB_CODE_SUCCESS;
  }

  // Allocate the additional buffer.
  uint32_t rows = pDataBlock->info.rows;
H
Haojun Liao 已提交
859 860 861 862 863 864 865

  bool sortColumnHasNull = false;
  bool varTypeSort = false;

  for (int32_t i = 0; i < taosArrayGetSize(pOrderInfo); ++i) {
    SBlockOrderInfo* pInfo = taosArrayGet(pOrderInfo, i);

H
Haojun Liao 已提交
866
    SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, pInfo->slotId);
H
Haojun Liao 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    if (pColInfoData->hasNull) {
      sortColumnHasNull = true;
    }

    if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
      varTypeSort = true;
    }
  }

  if (taosArrayGetSize(pOrderInfo) == 1 && (!sortColumnHasNull)) {
    if (pDataBlock->info.numOfCols == 1) {
      if (!varTypeSort) {
        SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, 0);
        SBlockOrderInfo* pOrder = taosArrayGet(pOrderInfo, 0);

        int64_t p0 = taosGetTimestampUs();

884
        __compar_fn_t fn = getKeyComparFunc(pColInfoData->info.type, pOrder->order);
H
Haojun Liao 已提交
885 886 887
        qsort(pColInfoData->pData, pDataBlock->info.rows, pColInfoData->info.bytes, fn);

        int64_t p1 = taosGetTimestampUs();
888
        uDebug("blockDataSort easy cost:%" PRId64 ", rows:%d\n", p1 - p0, pDataBlock->info.rows);
H
Haojun Liao 已提交
889 890 891 892 893 894 895 896

        return TSDB_CODE_SUCCESS;
      } else {  // var data type
      }
    } else if (pDataBlock->info.numOfCols == 2) {
    }
  }

H
Haojun Liao 已提交
897 898 899 900 901 902
  int32_t* index = createTupleIndex(rows);
  if (index == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return terrno;
  }

903 904
  int64_t p0 = taosGetTimestampUs();

H
Haojun Liao 已提交
905
  SSDataBlockSortHelper helper = {.pDataBlock = pDataBlock, .orderInfo = pOrderInfo};
L
Liu Jicong 已提交
906
  for (int32_t i = 0; i < taosArrayGetSize(helper.orderInfo); ++i) {
H
Haojun Liao 已提交
907
    struct SBlockOrderInfo* pInfo = taosArrayGet(helper.orderInfo, i);
H
Haojun Liao 已提交
908
    pInfo->pColData = taosArrayGet(pDataBlock->pDataBlock, pInfo->slotId);
H
Haojun Liao 已提交
909 910
  }

H
Haojun Liao 已提交
911 912
  taosqsort(index, rows, sizeof(int32_t), &helper, dataBlockCompar);

913 914
  int64_t p1 = taosGetTimestampUs();

H
Haojun Liao 已提交
915 916
  SColumnInfoData* pCols = createHelpColInfoData(pDataBlock);
  if (pCols == NULL) {
917
    destroyTupleIndex(index);
H
Haojun Liao 已提交
918 919 920 921
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return terrno;
  }

922 923
  int64_t p2 = taosGetTimestampUs();

wmmhello's avatar
wmmhello 已提交
924
  blockDataAssign(pCols, pDataBlock, index);
H
Haojun Liao 已提交
925

926
  int64_t p3 = taosGetTimestampUs();
H
Haojun Liao 已提交
927 928

  copyBackToBlock(pDataBlock, pCols);
929 930
  int64_t p4 = taosGetTimestampUs();

931
  uDebug("blockDataSort complex sort:%" PRId64 ", create:%" PRId64 ", assign:%" PRId64 ", copyback:%" PRId64 ", rows:%d\n", p1 - p0, p2 - p1,
L
Liu Jicong 已提交
932
         p3 - p2, p4 - p3, rows);
H
Haojun Liao 已提交
933
  destroyTupleIndex(index);
H
Haojun Liao 已提交
934 935

  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
936
}
937

H
Haojun Liao 已提交
938 939
typedef struct SHelper {
  int32_t index;
L
Liu Jicong 已提交
940 941 942 943 944
  union {
    char*   pData;
    int64_t i64;
    double  d64;
  };
H
Haojun Liao 已提交
945 946 947 948 949 950
} SHelper;

SHelper* createTupleIndex_rv(int32_t numOfRows, SArray* pOrderInfo, SSDataBlock* pBlock) {
  int32_t sortValLengthPerRow = 0;
  int32_t numOfCols = taosArrayGetSize(pOrderInfo);

L
Liu Jicong 已提交
951
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
952
    SBlockOrderInfo* pInfo = taosArrayGet(pOrderInfo, i);
H
Haojun Liao 已提交
953
    SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, pInfo->slotId);
H
Haojun Liao 已提交
954 955 956 957 958 959
    pInfo->pColData = pColInfo;
    sortValLengthPerRow += pColInfo->info.bytes;
  }

  size_t len = sortValLengthPerRow * pBlock->info.rows;

wafwerar's avatar
wafwerar 已提交
960 961
  char*    buf = taosMemoryCalloc(1, len);
  SHelper* phelper = taosMemoryCalloc(numOfRows, sizeof(SHelper));
L
Liu Jicong 已提交
962
  for (int32_t i = 0; i < numOfRows; ++i) {
H
Haojun Liao 已提交
963 964 965 966 967
    phelper[i].index = i;
    phelper[i].pData = buf + sortValLengthPerRow * i;
  }

  int32_t offset = 0;
L
Liu Jicong 已提交
968
  for (int32_t i = 0; i < numOfCols; ++i) {
H
Haojun Liao 已提交
969
    SBlockOrderInfo* pInfo = taosArrayGet(pOrderInfo, i);
L
Liu Jicong 已提交
970 971 972 973
    for (int32_t j = 0; j < numOfRows; ++j) {
      phelper[j].i64 = *(int32_t*)pInfo->pColData->pData + pInfo->pColData->info.bytes * j;
      //      memcpy(phelper[j].pData + offset, pInfo->pColData->pData + pInfo->pColData->info.bytes * j,
      //      pInfo->pColData->info.bytes);
H
Haojun Liao 已提交
974 975 976 977 978 979 980 981 982
    }

    offset += pInfo->pColData->info.bytes;
  }

  return phelper;
}

int32_t dataBlockCompar_rv(const void* p1, const void* p2, const void* param) {
L
Liu Jicong 已提交
983
  const SSDataBlockSortHelper* pHelper = (const SSDataBlockSortHelper*)param;
H
Haojun Liao 已提交
984

L
Liu Jicong 已提交
985
  //  SSDataBlock* pDataBlock = pHelper->pDataBlock;
H
Haojun Liao 已提交
986

L
Liu Jicong 已提交
987 988
  SHelper* left = (SHelper*)p1;
  SHelper* right = (SHelper*)p2;
H
Haojun Liao 已提交
989 990 991 992

  SArray* pInfo = pHelper->orderInfo;

  int32_t offset = 0;
L
Liu Jicong 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  //  for(int32_t i = 0; i < pInfo->size; ++i) {
  //    SBlockOrderInfo* pOrder = TARRAY_GET_ELEM(pInfo, 0);
  //    SColumnInfoData* pColInfoData = pOrder->pColData;//TARRAY_GET_ELEM(pDataBlock->pDataBlock, pOrder->colIndex);

  //    if (pColInfoData->hasNull) {
  //      bool leftNull  = colDataIsNull(pColInfoData, pDataBlock->info.rows, left, pDataBlock->pBlockAgg);
  //      bool rightNull = colDataIsNull(pColInfoData, pDataBlock->info.rows, right, pDataBlock->pBlockAgg);
  //      if (leftNull && rightNull) {
  //        continue; // continue to next slot
  //      }
  //
  //      if (rightNull) {
  //        return pHelper->nullFirst? 1:-1;
  //      }
  //
  //      if (leftNull) {
  //        return pHelper->nullFirst? -1:1;
  //      }
  //    }

  //    void* left1  = colDataGetData(pColInfoData, left);
  //    void* right1 = colDataGetData(pColInfoData, right);

  //    switch(pColInfoData->info.type) {
  //      case TSDB_DATA_TYPE_INT: {
  int32_t leftx = *(int32_t*)left->pData;    //*(int32_t*)(left->pData + offset);
  int32_t rightx = *(int32_t*)right->pData;  //*(int32_t*)(right->pData + offset);

  //        offset += pColInfoData->info.bytes;
  if (leftx == rightx) {
    //          break;
    return 0;
  } else {
    //          if (pOrder->order == TSDB_ORDER_ASC) {
    return (leftx < rightx) ? -1 : 1;
    //          } else {
    //            return (leftx < rightx)? 1:-1;
    //          }
  }
  //      }
  //      default:
  //        assert(0);
  //    }
  //  }
H
Haojun Liao 已提交
1037 1038 1039 1040

  return 0;
}

L
Liu Jicong 已提交
1041
int32_t varColSort(SColumnInfoData* pColumnInfoData, SBlockOrderInfo* pOrder) { return 0; }
H
Haojun Liao 已提交
1042 1043

int32_t blockDataSort_rv(SSDataBlock* pDataBlock, SArray* pOrderInfo, bool nullFirst) {
L
Liu Jicong 已提交
1044
  // Allocate the additional buffer.
H
Haojun Liao 已提交
1045 1046
  int64_t p0 = taosGetTimestampUs();

H
Haojun Liao 已提交
1047
  SSDataBlockSortHelper helper = {.pDataBlock = pDataBlock, .orderInfo = pOrderInfo};
H
Haojun Liao 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

  uint32_t rows = pDataBlock->info.rows;
  SHelper* index = createTupleIndex_rv(rows, helper.orderInfo, pDataBlock);
  if (index == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return terrno;
  }

  taosqsort(index, rows, sizeof(SHelper), &helper, dataBlockCompar_rv);

L
Liu Jicong 已提交
1058
  int64_t          p1 = taosGetTimestampUs();
H
Haojun Liao 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
  SColumnInfoData* pCols = createHelpColInfoData(pDataBlock);
  if (pCols == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return terrno;
  }

  int64_t p2 = taosGetTimestampUs();

  //  int32_t code = blockDataAssign(pCols, pDataBlock, index);
  //  if (code != TSDB_CODE_SUCCESS) {
  //    terrno = code;
  //    return code;
  //  }

  int64_t p3 = taosGetTimestampUs();

  copyBackToBlock(pDataBlock, pCols);
  int64_t p4 = taosGetTimestampUs();

L
Liu Jicong 已提交
1078 1079
  printf("sort:%" PRId64 ", create:%" PRId64 ", assign:%" PRId64 ", copyback:%" PRId64 ", rows:%d\n", p1 - p0, p2 - p1,
         p3 - p2, p4 - p3, rows);
H
Haojun Liao 已提交
1080
  //  destroyTupleIndex(index);
1081
  return 0;
H
Haojun Liao 已提交
1082 1083
}

1084
void blockDataCleanup(SSDataBlock* pDataBlock) {
1085
  pDataBlock->info.rows = 0;
1086 1087 1088
  for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) {
    SColumnInfoData* p = taosArrayGet(pDataBlock->pDataBlock, i);
    colInfoDataCleanup(p, pDataBlock->info.capacity);
1089 1090 1091
  }
}

1092 1093
int32_t colInfoDataEnsureCapacity(SColumnInfoData* pColumn, size_t existRows, uint32_t numOfRows) {
  if (0 == numOfRows || numOfRows <= existRows) {
X
Xiaoyu Wang 已提交
1094 1095 1096
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
1097
  if (IS_VAR_DATA_TYPE(pColumn->info.type)) {
wafwerar's avatar
wafwerar 已提交
1098
    char* tmp = taosMemoryRealloc(pColumn->varmeta.offset, sizeof(int32_t) * numOfRows);
D
dapan1121 已提交
1099 1100 1101
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
1102

D
dapan1121 已提交
1103
    pColumn->varmeta.offset = (int32_t*)tmp;
1104
    memset(&pColumn->varmeta.offset[existRows], 0, sizeof(int32_t) * (numOfRows - existRows));
D
dapan1121 已提交
1105
  } else {
wafwerar's avatar
wafwerar 已提交
1106
    char* tmp = taosMemoryRealloc(pColumn->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
1107 1108 1109
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
1110

1111
    int32_t oldLen = BitmapLen(existRows);
D
dapan1121 已提交
1112
    pColumn->nullbitmap = tmp;
1113
    memset(&pColumn->nullbitmap[oldLen], 0, BitmapLen(numOfRows) - oldLen);
1114 1115 1116 1117 1118

    if (pColumn->info.type == TSDB_DATA_TYPE_NULL) {
      return TSDB_CODE_SUCCESS;
    }

X
Xiaoyu Wang 已提交
1119
    assert(pColumn->info.bytes);
wafwerar's avatar
wafwerar 已提交
1120
    tmp = taosMemoryRealloc(pColumn->pData, numOfRows * pColumn->info.bytes);
D
dapan1121 已提交
1121 1122 1123
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
1124

D
dapan1121 已提交
1125 1126 1127 1128 1129 1130
    pColumn->pData = tmp;
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1131
void colInfoDataCleanup(SColumnInfoData* pColumn, uint32_t numOfRows) {
1132 1133 1134 1135 1136 1137 1138
  if (IS_VAR_DATA_TYPE(pColumn->info.type)) {
    pColumn->varmeta.length = 0;
  } else {
    memset(pColumn->nullbitmap, 0, BitmapLen(numOfRows));
  }
}

D
dapan1121 已提交
1139 1140
int32_t blockDataEnsureCapacity(SSDataBlock* pDataBlock, uint32_t numOfRows) {
  int32_t code = 0;
H
Haojun Liao 已提交
1141 1142 1143
  if (numOfRows == 0) {
    return TSDB_CODE_SUCCESS;
  }
L
Liu Jicong 已提交
1144

1145 1146
  pDataBlock->info.capacity = numOfRows;

L
Liu Jicong 已提交
1147
  for (int32_t i = 0; i < pDataBlock->info.numOfCols; ++i) {
D
dapan1121 已提交
1148
    SColumnInfoData* p = taosArrayGet(pDataBlock->pDataBlock, i);
1149
    code = colInfoDataEnsureCapacity(p, pDataBlock->info.rows, numOfRows);
D
dapan1121 已提交
1150 1151
    if (code) {
      return code;
1152 1153
    }
  }
H
Haojun Liao 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162

  return TSDB_CODE_SUCCESS;
}

void* blockDataDestroy(SSDataBlock* pBlock) {
  if (pBlock == NULL) {
    return NULL;
  }

L
Liu Jicong 已提交
1163
  blockDestroyInner(pBlock);
wafwerar's avatar
wafwerar 已提交
1164
  taosMemoryFreeClear(pBlock);
H
Haojun Liao 已提交
1165
  return NULL;
D
dapan1121 已提交
1166
}
1167

1168
SSDataBlock* createOneDataBlock(const SSDataBlock* pDataBlock, bool copyData) {
1169 1170 1171
  if(pDataBlock == NULL){
    return NULL;
  }
1172

1173 1174
  int32_t numOfCols = pDataBlock->info.numOfCols;

wafwerar's avatar
wafwerar 已提交
1175
  SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
1176
  pBlock->pDataBlock = taosArrayInit(numOfCols, sizeof(SColumnInfoData));
1177

1178
  pBlock->info.numOfCols = numOfCols;
1179
  pBlock->info.hasVarCol = pDataBlock->info.hasVarCol;
1180
  pBlock->info.rowSize   = pDataBlock->info.rows;
1181

L
Liu Jicong 已提交
1182
  for (int32_t i = 0; i < numOfCols; ++i) {
1183 1184 1185 1186 1187 1188
    SColumnInfoData  colInfo = {0};
    SColumnInfoData* p = taosArrayGet(pDataBlock->pDataBlock, i);
    colInfo.info = p->info;
    taosArrayPush(pBlock->pDataBlock, &colInfo);
  }

1189 1190 1191 1192 1193
  if (copyData) {
    for (int32_t i = 0; i < numOfCols; ++i) {
      SColumnInfoData* pDst = taosArrayGet(pBlock->pDataBlock, i);
      SColumnInfoData* pSrc = taosArrayGet(pDataBlock->pDataBlock, i);

1194
      int32_t code = colInfoDataEnsureCapacity(pDst, 0, pDataBlock->info.rows);
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
      if (code != TSDB_CODE_SUCCESS) {
        return NULL;
      }

      colDataAssign(pDst, pSrc, pDataBlock->info.rows);
    }

    pBlock->info.rows = pDataBlock->info.rows;
    pBlock->info.capacity = pDataBlock->info.rows;
  }

1206 1207 1208 1209
  return pBlock;
}

size_t blockDataGetCapacityInRow(const SSDataBlock* pBlock, size_t pageSize) {
H
Haojun Liao 已提交
1210
  return (int32_t) ((pageSize - blockDataGetSerialMetaSize(pBlock))/ blockDataGetSerialRowSize(pBlock));
1211
}
H
Haojun Liao 已提交
1212

1213 1214
void colDataDestroy(SColumnInfoData* pColData) {
  if (IS_VAR_DATA_TYPE(pColData->info.type)) {
H
Haojun Liao 已提交
1215
    taosMemoryFree(pColData->varmeta.offset);
1216
  } else {
H
Haojun Liao 已提交
1217
    taosMemoryFree(pColData->nullbitmap);
1218 1219
  }

H
Haojun Liao 已提交
1220
  taosMemoryFree(pColData->pData);
1221 1222
}

H
Haojun Liao 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

static void doShiftBitmap(char* nullBitmap, size_t n, size_t total) {
  int32_t len = BitmapLen(total);

  int32_t newLen = BitmapLen(total - n);
  if (n%8 == 0) {
    memmove(nullBitmap, nullBitmap + n/8, newLen);
  } else {
    int32_t tail = n % 8;
    int32_t i = 0;

    uint8_t* p = (uint8_t*) nullBitmap;
    while(i < len) {
      uint8_t v = p[i];

      p[i] = 0;
      p[i] = (v << tail);

      if (i < len - 1) {
        uint8_t next = p[i + 1];
        p[i] |= (next >> (8 - tail));
      }

      i += 1;
    }
  }
}

static void colDataTrimFirstNRows(SColumnInfoData* pColInfoData, size_t n, size_t total) {
  if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) {
    memmove(pColInfoData->varmeta.offset, &pColInfoData->varmeta.offset[n], (total - n));
    memset(&pColInfoData->varmeta.offset[total - n - 1], 0, n);
  } else {
    int32_t bytes = pColInfoData->info.bytes;
    memmove(pColInfoData->pData, ((char*)pColInfoData->pData + n * bytes), (total - n) * bytes);
    doShiftBitmap(pColInfoData->nullbitmap, n, total);
  }
}

int32_t blockDataTrimFirstNRows(SSDataBlock *pBlock, size_t n) {
  if (n == 0) {
    return TSDB_CODE_SUCCESS;
  }

  if (pBlock->info.rows <= n) {
    blockDataCleanup(pBlock);
  } else {
    for(int32_t i = 0; i < pBlock->info.numOfCols; ++i) {
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i);
      colDataTrimFirstNRows(pColInfoData, n, pBlock->info.rows);
    }

    pBlock->info.rows -= n;
  }
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
int32_t tEncodeDataBlock(void** buf, const SSDataBlock* pBlock) {
  int64_t tbUid = pBlock->info.uid;
  int16_t numOfCols = pBlock->info.numOfCols;
  int16_t hasVarCol = pBlock->info.hasVarCol;
  int32_t rows = pBlock->info.rows;
  int32_t sz = taosArrayGetSize(pBlock->pDataBlock);

  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, tbUid);
  tlen += taosEncodeFixedI16(buf, numOfCols);
  tlen += taosEncodeFixedI16(buf, hasVarCol);
  tlen += taosEncodeFixedI32(buf, rows);
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    SColumnInfoData* pColData = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, i);
    tlen += taosEncodeFixedI16(buf, pColData->info.colId);
    tlen += taosEncodeFixedI16(buf, pColData->info.type);
    tlen += taosEncodeFixedI32(buf, pColData->info.bytes);

    if (IS_VAR_DATA_TYPE(pColData->info.type)) {
      tlen += taosEncodeBinary(buf, pColData->varmeta.offset, sizeof(int32_t) * rows);
    } else {
      tlen += taosEncodeBinary(buf, pColData->nullbitmap, BitmapLen(rows));
    }

    int32_t len = colDataGetLength(pColData, rows);
X
Xiaoyu Wang 已提交
1306
    tlen += taosEncodeFixedI32(buf, len);
H
Haojun Liao 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341

    tlen += taosEncodeBinary(buf, pColData->pData, len);
  }
  return tlen;
}

void* tDecodeDataBlock(const void* buf, SSDataBlock* pBlock) {
  int32_t sz;

  buf = taosDecodeFixedI64(buf, &pBlock->info.uid);
  buf = taosDecodeFixedI16(buf, &pBlock->info.numOfCols);
  buf = taosDecodeFixedI16(buf, &pBlock->info.hasVarCol);
  buf = taosDecodeFixedI32(buf, &pBlock->info.rows);
  buf = taosDecodeFixedI32(buf, &sz);
  pBlock->pDataBlock = taosArrayInit(sz, sizeof(SColumnInfoData));
  for (int32_t i = 0; i < sz; i++) {
    SColumnInfoData data = {0};
    buf = taosDecodeFixedI16(buf, &data.info.colId);
    buf = taosDecodeFixedI16(buf, &data.info.type);
    buf = taosDecodeFixedI32(buf, &data.info.bytes);

    if (IS_VAR_DATA_TYPE(data.info.type)) {
      buf = taosDecodeBinary(buf, (void**)&data.varmeta.offset, pBlock->info.rows * sizeof(int32_t));
      data.varmeta.length = pBlock->info.rows * sizeof(int32_t);
      data.varmeta.allocLen = data.varmeta.length;
    } else {
      buf = taosDecodeBinary(buf, (void**)&data.nullbitmap, BitmapLen(pBlock->info.rows));
    }

    int32_t len = 0;
    buf = taosDecodeFixedI32(buf, &len);
    buf = taosDecodeBinary(buf, (void**)&data.pData, len);
    taosArrayPush(pBlock->pDataBlock, &data);
  }
  return (void*)buf;
L
Liu Jicong 已提交
1342
}
L
Liu Jicong 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

int32_t tEncodeDataBlocks(void** buf, const SArray* blocks) {
  int32_t tlen = 0;
  int32_t sz = taosArrayGetSize(blocks);
  tlen += taosEncodeFixedI32(buf, sz);

  for (int32_t i = 0; i < sz; i++) {
    SSDataBlock* pBlock = taosArrayGet(blocks, i);
    tlen += tEncodeDataBlock(buf, pBlock);
  }

  return tlen;
}

L
Liu Jicong 已提交
1357
void* tDecodeDataBlocks(const void* buf, SArray** blocks) {
L
Liu Jicong 已提交
1358 1359
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
L
Liu Jicong 已提交
1360 1361

  *blocks = taosArrayInit(sz, sizeof(SSDataBlock));
L
Liu Jicong 已提交
1362 1363 1364
  for (int32_t i = 0; i < sz; i++) {
    SSDataBlock pBlock = {0};
    buf = tDecodeDataBlock(buf, &pBlock);
L
Liu Jicong 已提交
1365
    taosArrayPush(*blocks, &pBlock);
L
Liu Jicong 已提交
1366 1367 1368
  }
  return (void*)buf;
}
L
Liu Jicong 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

static char* formatTimestamp(char* buf, int64_t val, int precision) {
  time_t  tt;
  int32_t ms = 0;
  if (precision == TSDB_TIME_PRECISION_NANO) {
    tt = (time_t)(val / 1000000000);
    ms = val % 1000000000;
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
    tt = (time_t)(val / 1000000);
    ms = val % 1000000;
  } else {
    tt = (time_t)(val / 1000);
    ms = val % 1000;
  }

  /* comment out as it make testcases like select_with_tags.sim fail.
    but in windows, this may cause the call to localtime crash if tt < 0,
    need to find a better solution.
    if (tt < 0) {
      tt = 0;
    }
    */

#ifdef WINDOWS
  if (tt < 0) tt = 0;
#endif
  if (tt <= 0 && ms < 0) {
    tt--;
    if (precision == TSDB_TIME_PRECISION_NANO) {
      ms += 1000000000;
    } else if (precision == TSDB_TIME_PRECISION_MICRO) {
      ms += 1000000;
    } else {
      ms += 1000;
    }
  }

1406
  struct tm* ptm = taosLocalTime(&tt, NULL);
L
Liu Jicong 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  size_t     pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm);

  if (precision == TSDB_TIME_PRECISION_NANO) {
    sprintf(buf + pos, ".%09d", ms);
  } else if (precision == TSDB_TIME_PRECISION_MICRO) {
    sprintf(buf + pos, ".%06d", ms);
  } else {
    sprintf(buf + pos, ".%03d", ms);
  }

  return buf;
}
H
Haojun Liao 已提交
1419

L
Liu Jicong 已提交
1420
void blockDebugShowData(const SArray* dataBlocks) {
L
Liu Jicong 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
  char    pBuf[128];
  int32_t sz = taosArrayGetSize(dataBlocks);
  for (int32_t i = 0; i < sz; i++) {
    SSDataBlock* pDataBlock = taosArrayGet(dataBlocks, i);
    int32_t      colNum = pDataBlock->info.numOfCols;
    int32_t      rows = pDataBlock->info.rows;
    for (int32_t j = 0; j < rows; j++) {
      printf("|");
      for (int32_t k = 0; k < colNum; k++) {
        SColumnInfoData* pColInfoData = taosArrayGet(pDataBlock->pDataBlock, k);
        void*            var = POINTER_SHIFT(pColInfoData->pData, j * pColInfoData->info.bytes);
        switch (pColInfoData->info.type) {
          case TSDB_DATA_TYPE_TIMESTAMP:
            formatTimestamp(pBuf, *(uint64_t*)var, TSDB_TIME_PRECISION_MILLI);
            printf(" %25s |", pBuf);
            break;
          case TSDB_DATA_TYPE_INT:
            printf(" %15d |", *(int32_t*)var);
            break;
L
Liu Jicong 已提交
1440 1441 1442
          case TSDB_DATA_TYPE_UINT:
            printf(" %15u |", *(uint32_t*)var);
            break;
L
Liu Jicong 已提交
1443 1444 1445
          case TSDB_DATA_TYPE_BIGINT:
            printf(" %15ld |", *(int64_t*)var);
            break;
L
Liu Jicong 已提交
1446 1447 1448
          case TSDB_DATA_TYPE_UBIGINT:
            printf(" %15lu |", *(uint64_t*)var);
            break;
L
Liu Jicong 已提交
1449 1450 1451 1452 1453 1454 1455
        }
      }
      printf("\n");
    }
  }
}