trow.c 26.1 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
H
Hongze Cheng 已提交
14 15
 */

S
common  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
H
Hongze Cheng 已提交
17 18
#include "trow.h"

C
Cary Xu 已提交
19 20 21 22 23 24 25 26
const uint8_t tdVTypeByte[2][3] = {{
                                       // 2 bits
                                       TD_VTYPE_NORM_BYTE_II,
                                       TD_VTYPE_NONE_BYTE_II,
                                       TD_VTYPE_NULL_BYTE_II,
                                   },
                                   {
                                       // 1 bit
C
Cary Xu 已提交
27 28
                                       TD_VTYPE_NORM_BYTE_I,  // normal
                                       TD_VTYPE_NULL_BYTE_I,
C
Cary Xu 已提交
29
                                       TD_VTYPE_NULL_BYTE_I,  // padding
C
Cary Xu 已提交
30 31
                                   }

K
Kaili Xu 已提交
32 33
};

C
Cary Xu 已提交
34 35 36
// declaration
static uint8_t tdGetBitmapByte(uint8_t byte);

C
Cary Xu 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// static void dataColSetNEleNull(SDataCol *pCol, int nEle);

/**
 * @brief src2 data has more priority than src1
 *
 * @param target
 * @param src1
 * @param iter1
 * @param limit1
 * @param src2
 * @param iter2
 * @param limit2
 * @param tRows
 * @param update
 */
static void tdMergeTwoDataCols(SDataCols *target, SDataCols *src1, int *iter1, int limit1, SDataCols *src2, int *iter2,
                               int limit2, int tRows, bool update);

C
Cary Xu 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
// implementation
/**
 * @brief Compress bitmap bytes comprised of 2-bits to counterpart of 1-bit.
 * e.g.
 * TD_VTYPE_NORM 0x00U(00000000) to 00000000 Normal
 * TD_VTYPE_NULL 0x01U(00000001) to 00000001 Null
 * TD_VTYPE_NONE 0x02U(00000010) to 00000001 Null
 *
 * 00000000 0x00 0x00
 * 01000000 0x40 0x08
 * 10000000 0x80 0x08
 *  ...
 * @param byte
 * @return uint8_t
 */
static uint8_t tdGetMergedBitmapByte(uint8_t byte) {
  switch (byte) {
    case 0x00:
      return 0x00;
    case 0x40:
      return 0x08;
    case 0x80:
      return 0x08;
    case 0x10:
      return 0x04;
    case 0x50:
      return 0x0c;
    case 0x90:
      return 0x0c;
    case 0x20:
      return 0x04;
    case 0x60:
      return 0x0c;
    case 0xa0:
      return 0x0c;
    case 0x04:
      return 0x02;
    case 0x44:
      return 0x0a;
    case 0x84:
      return 0x0a;
    case 0x14:
      return 0x06;
    case 0x54:
      return 0x0e;
    case 0x94:
      return 0x0e;
    case 0x24:
      return 0x06;
    case 0x64:
      return 0x0e;
    case 0xa4:
      return 0x0e;
    case 0x08:
      return 0x02;
    case 0x48:
      return 0x0a;
    case 0x88:
      return 0x0a;
    case 0x18:
      return 0x06;
    case 0x58:
      return 0x0e;
    case 0x98:
      return 0x0e;
    case 0x28:
      return 0x06;
    case 0x68:
      return 0x0e;
    case 0xa8:
      return 0x0e;
    case 0x01:
      return 0x01;
    case 0x41:
      return 0x09;
    case 0x81:
      return 0x09;
    case 0x11:
      return 0x05;
    case 0x51:
      return 0x0d;
    case 0x91:
      return 0x0d;
    case 0x21:
      return 0x05;
    case 0x61:
      return 0x0d;
    case 0xa1:
      return 0x0d;
    case 0x05:
      return 0x03;
    case 0x45:
      return 0x0b;
    case 0x85:
      return 0x0b;
    case 0x15:
      return 0x07;
    case 0x55:
      return 0x0f;
    case 0x95:
      return 0x0f;
    case 0x25:
      return 0x07;
    case 0x65:
      return 0x0f;
    case 0xa5:
      return 0x0f;
    case 0x09:
      return 0x03;
    case 0x49:
      return 0x0b;
    case 0x89:
      return 0x0b;
    case 0x19:
      return 0x07;
    case 0x59:
      return 0x0f;
    case 0x99:
      return 0x0f;
    case 0x29:
      return 0x07;
    case 0x69:
      return 0x0f;
    case 0xa9:
      return 0x0f;
    case 0x02:
      return 0x01;
    case 0x42:
      return 0x09;
    case 0x82:
      return 0x09;
    case 0x12:
      return 0x05;
    case 0x52:
      return 0x0d;
    case 0x92:
      return 0x0d;
    case 0x22:
      return 0x05;
    case 0x62:
      return 0x0d;
    case 0xa2:
      return 0x0d;
    case 0x06:
      return 0x03;
    case 0x46:
      return 0x0b;
    case 0x86:
      return 0x0b;
    case 0x16:
      return 0x07;
    case 0x56:
      return 0x0f;
    case 0x96:
      return 0x0f;
    case 0x26:
      return 0x07;
    case 0x66:
      return 0x0f;
    case 0xa6:
      return 0x0f;
    case 0x0a:
      return 0x03;
    case 0x4a:
      return 0x0b;
    case 0x8a:
      return 0x0b;
    case 0x1a:
      return 0x07;
    case 0x5a:
      return 0x0f;
    case 0x9a:
      return 0x0f;
    case 0x2a:
      return 0x07;
    case 0x6a:
      return 0x0f;
    case 0xaa:
      return 0x0f;
    default:
      // make sure the bitmap area is set to 0 firstly
      ASSERT(0);
      return 0x0f;  // return NULL bitmap for exception
  }
}

/**
C
Cary Xu 已提交
242
 * @brief Merge bitmap from 2 bits to 1 bit, and the memory buffer should be guaranteed by the invoker.
C
Cary Xu 已提交
243 244
 *
 * @param srcBitmap
C
Cary Xu 已提交
245
 * @param nBits
C
Cary Xu 已提交
246 247
 * @param dstBitmap
 */
C
Cary Xu 已提交
248
void tdMergeBitmap(uint8_t *srcBitmap, int32_t nBits, uint8_t *dstBitmap) {
C
Cary Xu 已提交
249
  int32_t i = 0, j = 0;
C
Cary Xu 已提交
250
  int32_t nBytes = TD_BITMAP_BYTES(nBits);
C
Cary Xu 已提交
251 252
  int32_t nRoundBytes = nBits / 4;
  int32_t nRemainderBits = nBits - nRoundBytes * 4;
C
Cary Xu 已提交
253

C
Cary Xu 已提交
254
  switch (nRemainderBits) {
C
Cary Xu 已提交
255 256 257 258
    case 0:
      // NOTHING TODO
      break;
    case 1: {
C
Cary Xu 已提交
259
      void *lastByte = POINTER_SHIFT(srcBitmap, nRoundBytes);
C
Cary Xu 已提交
260 261 262
      *(uint8_t *)lastByte &= 0xC0;
    } break;
    case 2: {
C
Cary Xu 已提交
263
      void *lastByte = POINTER_SHIFT(srcBitmap, nRoundBytes);
C
Cary Xu 已提交
264 265 266
      *(uint8_t *)lastByte &= 0xF0;
    } break;
    case 3: {
C
Cary Xu 已提交
267
      void *lastByte = POINTER_SHIFT(srcBitmap, nRoundBytes);
C
Cary Xu 已提交
268 269 270 271 272
      *(uint8_t *)lastByte &= 0xFC;
    } break;
    default:
      ASSERT(0);
  }
C
Cary Xu 已提交
273

C
Cary Xu 已提交
274
  if (nBytes > 0) {
C
Cary Xu 已提交
275 276 277
    dstBitmap[j] = (tdGetMergedBitmapByte(srcBitmap[i]) << 4);
  }

C
Cary Xu 已提交
278
  while ((++i) < nBytes) {
C
Cary Xu 已提交
279 280 281 282 283 284 285 286 287 288
    if ((i & 1) == 0) {
      dstBitmap[j] = (tdGetMergedBitmapByte(srcBitmap[i]) << 4);
    } else {
      dstBitmap[j] |= tdGetMergedBitmapByte(srcBitmap[i]);
      ++j;
    }
  }
}

static FORCE_INLINE void dataColSetNullAt(SDataCol *pCol, int index, bool setBitmap, int8_t bitmapMode) {
C
update  
Cary Xu 已提交
289 290 291 292 293 294 295 296 297
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff[index] = pCol->len;
    char *ptr = POINTER_SHIFT(pCol->pData, pCol->len);
    setVardataNull(ptr, pCol->type);
    pCol->len += varDataTLen(ptr);
  } else {
    setNull(POINTER_SHIFT(pCol->pData, TYPE_BYTES[pCol->type] * index), pCol->type, pCol->bytes);
    pCol->len += TYPE_BYTES[pCol->type];
  }
C
Cary Xu 已提交
298
  if (setBitmap) {
C
Cary Xu 已提交
299
    tdSetBitmapValType(pCol->pBitmap, index, TD_VTYPE_NONE, bitmapMode);
C
update  
Cary Xu 已提交
300 301 302
  }
}

C
Cary Xu 已提交
303 304 305 306 307 308 309 310 311 312 313 314
// static void dataColSetNEleNull(SDataCol *pCol, int nEle) {
//   if (IS_VAR_DATA_TYPE(pCol->type)) {
//     pCol->len = 0;
//     for (int i = 0; i < nEle; i++) {
//       dataColSetNullAt(pCol, i);
//     }
//   } else {
//     setNullN(pCol->pData, pCol->type, pCol->bytes, nEle);
//     pCol->len = TYPE_BYTES[pCol->type] * nEle;
//   }
// }

C
Cary Xu 已提交
315 316 317 318 319 320 321 322 323
/**
 * @brief Set bitmap area by byte preferentially and then by bit.
 *
 * @param pBitmap
 * @param nEle
 * @param valType
 * @param bitmapMode 0 for 2 bits, 1 for 1 bit
 * @return int32_t
 */
C
Cary Xu 已提交
324
int32_t tdSetBitmapValTypeN(void *pBitmap, int16_t nEle, TDRowValT valType, int8_t bitmapMode) {
C
Cary Xu 已提交
325
  TASSERT(valType < TD_VTYPE_MAX);
C
Cary Xu 已提交
326 327
  int32_t nBytes = (bitmapMode == 0 ? nEle / TD_VTYPE_PARTS : nEle / TD_VTYPE_PARTS_I);
  uint8_t vTypeByte = tdVTypeByte[bitmapMode][valType];
K
Kaili Xu 已提交
328
  for (int i = 0; i < nBytes; ++i) {
C
Cary Xu 已提交
329
    *(uint8_t *)pBitmap = vTypeByte;
K
Kaili Xu 已提交
330 331 332
    pBitmap = POINTER_SHIFT(pBitmap, 1);
  }

C
Cary Xu 已提交
333
  int32_t nLeft = nEle - nBytes * (bitmapMode == 0 ? TD_VTYPE_BITS : TD_VTYPE_BITS_I);
K
Kaili Xu 已提交
334
  for (int j = 0; j < nLeft; ++j) {
C
Cary Xu 已提交
335
    tdSetBitmapValType(pBitmap, j, valType, bitmapMode);
K
Kaili Xu 已提交
336
  }
C
Cary Xu 已提交
337
  return TSDB_CODE_SUCCESS;
K
Kaili Xu 已提交
338 339
}

C
Cary Xu 已提交
340 341 342 343 344 345 346
bool tdIsBitmapBlkNorm(const void *pBitmap, int32_t numOfBits, int8_t bitmapMode) {
  int32_t nBytes = (bitmapMode == 0 ? numOfBits / TD_VTYPE_PARTS : numOfBits / TD_VTYPE_PARTS_I);
  uint8_t vTypeByte = tdVTypeByte[bitmapMode][TD_VTYPE_NORM];
  for (int i = 0; i < nBytes; ++i) {
    if (*((uint8_t *)pBitmap) != vTypeByte) {
      return false;
    }
C
Cary Xu 已提交
347
    pBitmap = POINTER_SHIFT(pBitmap, i);
C
Cary Xu 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361
  }

  int32_t nLeft = numOfBits - nBytes * (bitmapMode == 0 ? TD_VTYPE_BITS : TD_VTYPE_BITS_I);

  for (int j = 0; j < nLeft; ++j) {
    uint8_t vType;
    tdGetBitmapValType(pBitmap, j, &vType, bitmapMode);
    if (vType != TD_VTYPE_NORM) {
      return false;
    }
  }
  return true;
}

C
Cary Xu 已提交
362
static FORCE_INLINE void dataColSetNoneAt(SDataCol *pCol, int index, bool setBitmap, int8_t bitmapMode) {
K
Kaili Xu 已提交
363 364 365 366 367 368 369 370 371
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff[index] = pCol->len;
    char *ptr = POINTER_SHIFT(pCol->pData, pCol->len);
    setVardataNull(ptr, pCol->type);
    pCol->len += varDataTLen(ptr);
  } else {
    setNull(POINTER_SHIFT(pCol->pData, TYPE_BYTES[pCol->type] * index), pCol->type, pCol->bytes);
    pCol->len += TYPE_BYTES[pCol->type];
  }
C
Cary Xu 已提交
372
  if (setBitmap) {
C
Cary Xu 已提交
373
    tdSetBitmapValType(pCol->pBitmap, index, TD_VTYPE_NONE, bitmapMode);
C
Cary Xu 已提交
374
  }
K
Kaili Xu 已提交
375 376
}

C
Cary Xu 已提交
377
static void dataColSetNEleNone(SDataCol *pCol, int nEle, int8_t bitmapMode) {
K
Kaili Xu 已提交
378 379 380
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->len = 0;
    for (int i = 0; i < nEle; ++i) {
C
Cary Xu 已提交
381
      dataColSetNoneAt(pCol, i, false, bitmapMode);
K
Kaili Xu 已提交
382 383 384 385 386 387
    }
  } else {
    setNullN(pCol->pData, pCol->type, pCol->bytes, nEle);
    pCol->len = TYPE_BYTES[pCol->type] * nEle;
  }
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
388
  tdSetBitmapValTypeN(pCol->pBitmap, nEle, TD_VTYPE_NONE, bitmapMode);
K
Kaili Xu 已提交
389 390 391
#endif
}

H
more  
Hongze Cheng 已提交
392
#if 0
H
more  
Hongze Cheng 已提交
393
void trbSetRowInfo(SRowBuilder *pRB, bool del, uint16_t sver) {
H
Hongze Cheng 已提交
394 395 396
  // TODO
}

H
more  
Hongze Cheng 已提交
397 398
void trbSetRowVersion(SRowBuilder *pRB, uint64_t ver) {
  // TODO
H
Hongze Cheng 已提交
399 400
}

H
more  
Hongze Cheng 已提交
401 402
void trbSetRowTS(SRowBuilder *pRB, TSKEY ts) {
  // TODO
H
Hongze Cheng 已提交
403 404
}

H
more  
Hongze Cheng 已提交
405 406 407
int trbWriteCol(SRowBuilder *pRB, void *pData, col_id_t cid) {
  // TODO
  return 0;
H
more  
Hongze Cheng 已提交
408
}
C
Cary Xu 已提交
409

C
Cary Xu 已提交
410
#endif
C
Cary Xu 已提交
411

C
Cary Xu 已提交
412
STSRow *tdRowDup(STSRow *row) {
wafwerar's avatar
wafwerar 已提交
413
  STSRow *trow = taosMemoryMalloc(TD_ROW_LEN(row));
C
Cary Xu 已提交
414 415
  if (trow == NULL) return NULL;

C
Cary Xu 已提交
416
  tdRowCpy(trow, row);
C
Cary Xu 已提交
417 418 419
  return trow;
}

C
Cary Xu 已提交
420
/**
C
Cary Xu 已提交
421 422 423 424 425 426 427
 * @brief
 *
 * @param pCol
 * @param valType
 * @param val
 * @param numOfRows
 * @param maxPoints
C
Cary Xu 已提交
428 429
 * @param bitmapMode default is 0(2 bits), otherwise 1(1 bit)
 * @param isMerge merge to current row
C
Cary Xu 已提交
430
 * @return int
C
Cary Xu 已提交
431
 */
C
Cary Xu 已提交
432
int tdAppendValToDataCol(SDataCol *pCol, TDRowValT valType, const void *val, int numOfRows, int maxPoints,
C
Cary Xu 已提交
433
                         int8_t bitmapMode, bool isMerge) {
C
Cary Xu 已提交
434
  TASSERT(pCol != NULL);
C
update  
Cary Xu 已提交
435

C
Cary Xu 已提交
436
  // Assume that the columns not specified during insert/upsert mean None.
K
Kaili Xu 已提交
437 438 439
  if (isAllRowsNone(pCol)) {
    if (tdValIsNone(valType)) {
      // all None value yet, just return
C
update  
Cary Xu 已提交
440 441 442
      return 0;
    }

C
Cary Xu 已提交
443
    if (tdAllocMemForCol(pCol, maxPoints) < 0) return -1;
C
update  
Cary Xu 已提交
444
    if (numOfRows > 0) {
K
Kaili Xu 已提交
445
      // Find the first not None value, fill all previous values as None
C
Cary Xu 已提交
446
      dataColSetNEleNone(pCol, numOfRows, bitmapMode);
C
update  
Cary Xu 已提交
447 448
    }
  }
C
Cary Xu 已提交
449 450
  const void *value = val;
  if (!tdValTypeIsNorm(valType) || !val) {
C
Cary Xu 已提交
451 452 453
    // TODO:
    // 1. back compatibility and easy to debug with codes of 2.0 to save NULL values.
    // 2. later on, considering further optimization, don't save Null/None for VarType.
C
Cary Xu 已提交
454
    value = getNullValue(pCol->type);
K
Kaili Xu 已提交
455
  }
C
Cary Xu 已提交
456 457 458 459 460
  if (!isMerge) {
    if (IS_VAR_DATA_TYPE(pCol->type)) {
      // set offset
      pCol->dataOff[numOfRows] = pCol->len;
      // Copy data
C
Cary Xu 已提交
461
      memcpy(POINTER_SHIFT(pCol->pData, pCol->len), value, varDataTLen(value));
C
Cary Xu 已提交
462
      // Update the length
C
Cary Xu 已提交
463
      pCol->len += varDataTLen(value);
C
Cary Xu 已提交
464 465
    } else {
      ASSERT(pCol->len == TYPE_BYTES[pCol->type] * numOfRows);
C
Cary Xu 已提交
466
      memcpy(POINTER_SHIFT(pCol->pData, pCol->len), value, pCol->bytes);
C
Cary Xu 已提交
467 468
      pCol->len += pCol->bytes;
    }
469
  } else if (!tdValTypeIsNone(valType)) {
C
Cary Xu 已提交
470 471 472 473 474 475
    if (IS_VAR_DATA_TYPE(pCol->type)) {
      // keep the last offset
      // discard the last var data
      int32_t lastVarLen = varDataTLen(POINTER_SHIFT(pCol->pData, pCol->dataOff[numOfRows]));
      pCol->len -= lastVarLen;
      // Copy data
C
Cary Xu 已提交
476
      memcpy(POINTER_SHIFT(pCol->pData, pCol->len), value, varDataTLen(value));
C
Cary Xu 已提交
477
      // Update the length
C
Cary Xu 已提交
478
      pCol->len += varDataTLen(value);
C
Cary Xu 已提交
479 480
    } else {
      ASSERT(pCol->len - TYPE_BYTES[pCol->type] == TYPE_BYTES[pCol->type] * numOfRows);
C
Cary Xu 已提交
481
      memcpy(POINTER_SHIFT(pCol->pData, pCol->len - TYPE_BYTES[pCol->type]), value, pCol->bytes);
C
Cary Xu 已提交
482
    }
C
update  
Cary Xu 已提交
483
  }
C
Cary Xu 已提交
484

K
Kaili Xu 已提交
485
#ifdef TD_SUPPORT_BITMAP
486 487 488
  if (!isMerge || !tdValTypeIsNone(valType)) {
    tdSetBitmapValType(pCol->pBitmap, numOfRows, valType, bitmapMode);
  }
K
Kaili Xu 已提交
489
#endif
C
update  
Cary Xu 已提交
490 491 492
  return 0;
}

K
Kaili Xu 已提交
493
// internal
C
Cary Xu 已提交
494 495
static int32_t tdAppendTpRowToDataCol(STSRow *pRow, STSchema *pSchema, SDataCols *pCols, bool isMerge) {
#if 0
C
Cary Xu 已提交
496
  ASSERT(pCols->numOfRows == 0 || dataColsKeyLast(pCols) < TD_ROW_KEY(pRow));
C
Cary Xu 已提交
497 498 499 500
#endif

  // Multi-Version rows with the same key and different versions supported
  ASSERT(pCols->numOfRows == 0 || dataColsKeyLast(pCols) <= TD_ROW_KEY(pRow));
C
Cary Xu 已提交
501

K
Kaili Xu 已提交
502 503 504 505 506
  int   rcol = 1;
  int   dcol = 1;
  void *pBitmap = tdGetBitmapAddrTp(pRow, pSchema->flen);

  SDataCol *pDataCol = &(pCols->cols[0]);
C
Cary Xu 已提交
507
  ASSERT(pDataCol->colId == PRIMARYKEY_TIMESTAMP_COL_ID);
C
Cary Xu 已提交
508 509
  tdAppendValToDataCol(pDataCol, TD_VTYPE_NORM, &pRow->ts, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                       isMerge);
C
Cary Xu 已提交
510 511

  while (dcol < pCols->numOfCols) {
K
Kaili Xu 已提交
512
    pDataCol = &(pCols->cols[dcol]);
C
Cary Xu 已提交
513
    if (rcol >= schemaNCols(pSchema)) {
C
Cary Xu 已提交
514 515
      tdAppendValToDataCol(pDataCol, TD_VTYPE_NULL, NULL, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                           isMerge);
K
Kaili Xu 已提交
516
      ++dcol;
C
Cary Xu 已提交
517 518 519 520
      continue;
    }

    STColumn *pRowCol = schemaColAt(pSchema, rcol);
C
update  
Cary Xu 已提交
521
    SCellVal  sVal = {0};
C
Cary Xu 已提交
522
    if (pRowCol->colId == pDataCol->colId) {
K
Kaili Xu 已提交
523 524
      if (tdGetTpRowValOfCol(&sVal, pRow, pBitmap, pRowCol->type, pRowCol->offset - sizeof(TSKEY), rcol - 1) < 0) {
        return terrno;
C
update  
Cary Xu 已提交
525
      }
C
Cary Xu 已提交
526 527
      tdAppendValToDataCol(pDataCol, sVal.valType, sVal.val, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                           isMerge);
K
Kaili Xu 已提交
528 529
      ++dcol;
      ++rcol;
C
Cary Xu 已提交
530
    } else if (pRowCol->colId < pDataCol->colId) {
K
Kaili Xu 已提交
531
      ++rcol;
C
Cary Xu 已提交
532
    } else {
C
Cary Xu 已提交
533 534
      tdAppendValToDataCol(pDataCol, TD_VTYPE_NULL, NULL, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                           isMerge);
K
Kaili Xu 已提交
535
      ++dcol;
C
Cary Xu 已提交
536 537
    }
  }
538
#if 0  
K
Kaili Xu 已提交
539
  ++pCols->numOfRows;
540
#endif
C
Cary Xu 已提交
541

K
Kaili Xu 已提交
542 543 544
  return TSDB_CODE_SUCCESS;
}
// internal
C
Cary Xu 已提交
545
static int32_t tdAppendKvRowToDataCol(STSRow *pRow, STSchema *pSchema, SDataCols *pCols, bool isMerge) {
C
Cary Xu 已提交
546
  ASSERT(pCols->numOfRows == 0 || dataColsKeyLast(pCols) < TD_ROW_KEY(pRow));
C
Cary Xu 已提交
547

C
update  
Cary Xu 已提交
548
  int   rcol = 0;
K
Kaili Xu 已提交
549
  int   dcol = 1;
C
Cary Xu 已提交
550
  int   tRowCols = tdRowGetNCols(pRow) - 1;  // the primary TS key not included in kvRowColIdx part
K
Kaili Xu 已提交
551
  int   tSchemaCols = schemaNCols(pSchema) - 1;
C
Cary Xu 已提交
552
  void *pBitmap = tdGetBitmapAddrKv(pRow, tdRowGetNCols(pRow));
C
Cary Xu 已提交
553

K
Kaili Xu 已提交
554
  SDataCol *pDataCol = &(pCols->cols[0]);
C
Cary Xu 已提交
555
  ASSERT(pDataCol->colId == PRIMARYKEY_TIMESTAMP_COL_ID);
C
Cary Xu 已提交
556 557
  tdAppendValToDataCol(pDataCol, TD_VTYPE_NORM, &pRow->ts, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                       isMerge);
K
Kaili Xu 已提交
558

C
Cary Xu 已提交
559
  while (dcol < pCols->numOfCols) {
K
Kaili Xu 已提交
560 561
    pDataCol = &(pCols->cols[dcol]);
    if (rcol >= tRowCols || rcol >= tSchemaCols) {
C
Cary Xu 已提交
562 563
      tdAppendValToDataCol(pDataCol, TD_VTYPE_NULL, NULL, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                           isMerge);
C
Cary Xu 已提交
564 565 566 567
      ++dcol;
      continue;
    }

K
Kaili Xu 已提交
568 569 570
    SKvRowIdx *pIdx = tdKvRowColIdxAt(pRow, rcol);
    int16_t    colIdx = -1;
    if (pIdx) {
C
bug fix  
Cary Xu 已提交
571
      colIdx = POINTER_DISTANCE(pIdx, TD_ROW_COL_IDX(pRow)) / sizeof(SKvRowIdx);
K
Kaili Xu 已提交
572
    }
C
bug fix  
Cary Xu 已提交
573
    TASSERT(colIdx >= 0);
K
Kaili Xu 已提交
574 575
    SCellVal sVal = {0};
    if (pIdx->colId == pDataCol->colId) {
C
Cary Xu 已提交
576
      if (tdGetKvRowValOfCol(&sVal, pRow, pBitmap, pIdx->offset, colIdx) < 0) {
K
Kaili Xu 已提交
577 578
        return terrno;
      }
C
Cary Xu 已提交
579 580
      tdAppendValToDataCol(pDataCol, sVal.valType, sVal.val, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                           isMerge);
C
Cary Xu 已提交
581 582
      ++dcol;
      ++rcol;
K
Kaili Xu 已提交
583
    } else if (pIdx->colId < pDataCol->colId) {
C
Cary Xu 已提交
584 585
      ++rcol;
    } else {
C
Cary Xu 已提交
586 587
      tdAppendValToDataCol(pDataCol, TD_VTYPE_NULL, NULL, pCols->numOfRows, pCols->maxPoints, pCols->bitmapMode,
                           isMerge);
C
Cary Xu 已提交
588 589 590
      ++dcol;
    }
  }
591
#if 0
K
Kaili Xu 已提交
592
  ++pCols->numOfRows;
593
#endif
K
Kaili Xu 已提交
594 595

  return TSDB_CODE_SUCCESS;
C
Cary Xu 已提交
596 597
}

C
update  
Cary Xu 已提交
598
/**
K
Kaili Xu 已提交
599 600 601 602 603
 * @brief exposed
 *
 * @param pRow
 * @param pSchema
 * @param pCols
C
update  
Cary Xu 已提交
604
 */
C
Cary Xu 已提交
605
int32_t tdAppendSTSRowToDataCol(STSRow *pRow, STSchema *pSchema, SDataCols *pCols, bool isMerge) {
C
Cary Xu 已提交
606
  if (TD_IS_TP_ROW(pRow)) {
C
Cary Xu 已提交
607
    return tdAppendTpRowToDataCol(pRow, pSchema, pCols, isMerge);
C
Cary Xu 已提交
608
  } else if (TD_IS_KV_ROW(pRow)) {
C
Cary Xu 已提交
609
    return tdAppendKvRowToDataCol(pRow, pSchema, pCols, isMerge);
C
Cary Xu 已提交
610 611 612
  } else {
    ASSERT(0);
  }
C
Cary Xu 已提交
613
  return TSDB_CODE_SUCCESS;
C
Cary Xu 已提交
614 615
}

C
Cary Xu 已提交
616 617 618 619 620 621 622 623 624 625 626 627
/**
 * @brief source data has more priority than target
 *
 * @param target
 * @param source
 * @param rowsToMerge
 * @param pOffset
 * @param update
 * @param maxVer
 * @return int
 */
int tdMergeDataCols(SDataCols *target, SDataCols *source, int rowsToMerge, int *pOffset, bool update,
C
Cary Xu 已提交
628
                    TDRowVerT maxVer) {
C
Cary Xu 已提交
629 630 631 632 633 634 635 636 637 638 639 640
  ASSERT(rowsToMerge > 0 && rowsToMerge <= source->numOfRows);
  ASSERT(target->numOfCols == source->numOfCols);
  int offset = 0;

  if (pOffset == NULL) {
    pOffset = &offset;
  }

  SDataCols *pTarget = NULL;

  if ((target->numOfRows == 0) || (dataColsKeyLast(target) < dataColsKeyAtRow(source, *pOffset))) {  // No overlap
    ASSERT(target->numOfRows + rowsToMerge <= target->maxPoints);
C
Cary Xu 已提交
641
    // TODO: filter the maxVer
C
Cary Xu 已提交
642 643 644
    TSKEY lastKey = TSKEY_INITIAL_VAL;
    for (int i = 0; i < rowsToMerge; ++i) {
      bool merge = false;
C
Cary Xu 已提交
645 646
      for (int j = 0; j < source->numOfCols; j++) {
        if (source->cols[j].len > 0 || target->cols[j].len > 0) {
C
Cary Xu 已提交
647
          SCellVal sVal = {0};
C
Cary Xu 已提交
648
          if (tdGetColDataOfRow(&sVal, source->cols + j, i + (*pOffset), source->bitmapMode) < 0) {
C
Cary Xu 已提交
649 650
            TASSERT(0);
          }
C
Cary Xu 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663

          if (j == 0) {
            if (lastKey == *(TSKEY *)sVal.val) {
              if (!update) {
                break;
              }
              merge = true;
            } else if (lastKey != TSKEY_INITIAL_VAL) {
              ++target->numOfRows;
            }

            lastKey = *(TSKEY *)sVal.val;
          }
C
Cary Xu 已提交
664 665 666
          if (i == 0) {
            (target->cols + j)->bitmap = (source->cols + j)->bitmap;
          }
C
Cary Xu 已提交
667

C
Cary Xu 已提交
668
          tdAppendValToDataCol(target->cols + j, sVal.valType, sVal.val, target->numOfRows, target->maxPoints,
C
Cary Xu 已提交
669
                               target->bitmapMode, merge);
C
Cary Xu 已提交
670 671
        }
      }
C
Cary Xu 已提交
672
    }
C
Cary Xu 已提交
673
    if (lastKey != TSKEY_INITIAL_VAL) {
C
Cary Xu 已提交
674
      ++target->numOfRows;
C
Cary Xu 已提交
675 676 677 678 679 680 681 682
    }
    (*pOffset) += rowsToMerge;
  } else {
    pTarget = tdDupDataCols(target, true);
    if (pTarget == NULL) goto _err;

    int iter1 = 0;
    tdMergeTwoDataCols(target, pTarget, &iter1, pTarget->numOfRows, source, pOffset, source->numOfRows,
C
Cary Xu 已提交
683
                       pTarget->numOfRows + rowsToMerge, update);
C
Cary Xu 已提交
684 685 686 687 688 689 690 691 692
  }

  tdFreeDataCols(pTarget);
  return 0;

_err:
  tdFreeDataCols(pTarget);
  return -1;
}
C
Cary Xu 已提交
693

C
Cary Xu 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706
static void tdAppendValToDataCols(SDataCols *target, SDataCols *src, int iter, bool isMerge) {
  for (int i = 0; i < src->numOfCols; ++i) {
    ASSERT(target->cols[i].type == src->cols[i].type);
    if (src->cols[i].len > 0 || target->cols[i].len > 0) {
      SCellVal sVal = {0};
      if (tdGetColDataOfRow(&sVal, src->cols + i, iter, src->bitmapMode) < 0) {
        TASSERT(0);
      }
      if (isMerge) {
        if (!tdValTypeIsNone(sVal.valType)) {
          tdAppendValToDataCol(&(target->cols[i]), sVal.valType, sVal.val, target->numOfRows, target->maxPoints,
                               target->bitmapMode, isMerge);
        } else {
707
          // Keep the origin value for None
C
Cary Xu 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
        }
      } else {
        tdAppendValToDataCol(&(target->cols[i]), sVal.valType, sVal.val, target->numOfRows, target->maxPoints,
                             target->bitmapMode, isMerge);
      }
    }
  }
}
/**
 * @brief src2 data has more priority than src1
 *
 * @param target
 * @param src1
 * @param iter1
 * @param limit1
 * @param src2
 * @param iter2
 * @param limit2
 * @param tRows
 * @param update
 */
C
Cary Xu 已提交
729
static void tdMergeTwoDataCols(SDataCols *target, SDataCols *src1, int *iter1, int limit1, SDataCols *src2, int *iter2,
C
Cary Xu 已提交
730
                               int limit2, int tRows, bool update) {
C
Cary Xu 已提交
731
  tdResetDataCols(target);
C
Cary Xu 已提交
732
  target->bitmapMode = src1->bitmapMode;
C
Cary Xu 已提交
733
  ASSERT(limit1 <= src1->numOfRows && limit2 <= src2->numOfRows);
C
Cary Xu 已提交
734
  int32_t nRows = 0;
C
Cary Xu 已提交
735

C
Cary Xu 已提交
736 737 738 739
  // TODO: filter the maxVer
  // TODO: handle the delete function
  TSKEY lastKey = TSKEY_INITIAL_VAL;
  while (nRows < tRows) {
C
Cary Xu 已提交
740 741 742
    if (*iter1 >= limit1 && *iter2 >= limit2) break;

    TSKEY key1 = (*iter1 >= limit1) ? INT64_MAX : dataColsKeyAt(src1, *iter1);
C
Cary Xu 已提交
743
    // TKEY  tkey1 = (*iter1 >= limit1) ? TKEY_NULL : dataColsTKeyAt(src1, *iter1);
C
Cary Xu 已提交
744
    TSKEY key2 = (*iter2 >= limit2) ? INT64_MAX : dataColsKeyAt(src2, *iter2);
C
Cary Xu 已提交
745
    // TKEY  tkey2 = (*iter2 >= limit2) ? TKEY_NULL : dataColsTKeyAt(src2, *iter2);
C
Cary Xu 已提交
746

C
Cary Xu 已提交
747 748 749 750 751 752 753 754 755
    // ASSERT(tkey1 == TKEY_NULL || (!TKEY_IS_DELETED(tkey1)));

    if (key1 <= key2) {
      // select key1 if not delete
      if (update && (lastKey == key1)) {
        tdAppendValToDataCols(target, src1, *iter1, true);
      } else if (lastKey != key1) {
        if (lastKey != TSKEY_INITIAL_VAL) {
          ++target->numOfRows;
C
Cary Xu 已提交
756
        }
C
Cary Xu 已提交
757
        tdAppendValToDataCols(target, src1, *iter1, false);
C
Cary Xu 已提交
758
      }
C
Cary Xu 已提交
759
      ++nRows;
C
Cary Xu 已提交
760
      ++(*iter1);
C
Cary Xu 已提交
761 762 763 764 765 766 767 768 769
      lastKey = key1;
    } else {
      // use key2 if not deleted
      // TODO: handle the delete function
      if (update && (lastKey == key2)) {
        tdAppendValToDataCols(target, src2, *iter2, true);
      } else if (lastKey != key2) {
        if (lastKey != TSKEY_INITIAL_VAL) {
          ++target->numOfRows;
C
Cary Xu 已提交
770
        }
C
Cary Xu 已提交
771
        tdAppendValToDataCols(target, src2, *iter2, false);
C
Cary Xu 已提交
772 773
      }

C
Cary Xu 已提交
774
      ++nRows;
C
Cary Xu 已提交
775
      ++(*iter2);
C
Cary Xu 已提交
776
      lastKey = key2;
C
Cary Xu 已提交
777 778
    }

C
Cary Xu 已提交
779 780
    ASSERT(target->numOfRows <= target->maxPoints - 1);
  }
C
Cary Xu 已提交
781
  if (lastKey != TSKEY_INITIAL_VAL) {
C
Cary Xu 已提交
782
    ++target->numOfRows;
C
Cary Xu 已提交
783 784 785
  }
}

C
Cary Xu 已提交
786
STSRow *mergeTwoRows(void *buffer, STSRow *row1, STSRow *row2, STSchema *pSchema1, STSchema *pSchema2) {
C
Cary Xu 已提交
787
#if 0
C
Cary Xu 已提交
788 789 790
  ASSERT(TD_ROW_KEY(row1) == TD_ROW_KEY(row2));
  ASSERT(schemaVersion(pSchema1) == TD_ROW_SVER(row1));
  ASSERT(schemaVersion(pSchema2) == TD_ROW_SVER(row2));
C
Cary Xu 已提交
791 792 793
  ASSERT(schemaVersion(pSchema1) >= schemaVersion(pSchema2));
#endif

C
Cary Xu 已提交
794
#if 0
C
Cary Xu 已提交
795 796 797 798 799
  SArray *stashRow = taosArrayInit(pSchema1->numOfCols, sizeof(SColInfo));
  if (stashRow == NULL) {
    return NULL;
  }

C
Cary Xu 已提交
800 801
  STSRow  pRow = buffer;
  STpRow dataRow = memRowDataBody(pRow);
C
Cary Xu 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
  memRowSetType(pRow, SMEM_ROW_DATA);
  dataRowSetVersion(dataRow, schemaVersion(pSchema1));  // use latest schema version
  dataRowSetLen(dataRow, (TDRowLenT)(TD_DATA_ROW_HEAD_SIZE + pSchema1->flen));

  TDRowLenT dataLen = 0, kvLen = TD_MEM_ROW_KV_HEAD_SIZE;

  int32_t  i = 0;  // row1
  int32_t  j = 0;  // row2
  int32_t  nCols1 = schemaNCols(pSchema1);
  int32_t  nCols2 = schemaNCols(pSchema2);
  SColInfo colInfo = {0};
  int32_t  kvIdx1 = 0, kvIdx2 = 0;

  while (i < nCols1) {
    STColumn *pCol = schemaColAt(pSchema1, i);
    void *    val1 = tdGetMemRowDataOfColEx(row1, pCol->colId, pCol->type, TD_DATA_ROW_HEAD_SIZE + pCol->offset, &kvIdx1);
    // if val1 != NULL, use val1;
    if (val1 != NULL && !isNull(val1, pCol->type)) {
      tdAppendColVal(dataRow, val1, pCol->type, pCol->offset);
      kvLen += tdGetColAppendLen(SMEM_ROW_KV, val1, pCol->type);
      setSColInfo(&colInfo, pCol->colId, pCol->type, val1);
      taosArrayPush(stashRow, &colInfo);
      ++i;  // next col
      continue;
    }

    void *val2 = NULL;
    while (j < nCols2) {
      STColumn *tCol = schemaColAt(pSchema2, j);
      if (tCol->colId < pCol->colId) {
        ++j;
        continue;
      }
      if (tCol->colId == pCol->colId) {
        val2 = tdGetMemRowDataOfColEx(row2, tCol->colId, tCol->type, TD_DATA_ROW_HEAD_SIZE + tCol->offset, &kvIdx2);
      } else if (tCol->colId > pCol->colId) {
        // set NULL
      }
      break;
    }  // end of while(j<nCols2)
    if (val2 == NULL) {
      val2 = (void *)getNullValue(pCol->type);
    }
    tdAppendColVal(dataRow, val2, pCol->type, pCol->offset);
    if (!isNull(val2, pCol->type)) {
      kvLen += tdGetColAppendLen(SMEM_ROW_KV, val2, pCol->type);
      setSColInfo(&colInfo, pCol->colId, pCol->type, val2);
      taosArrayPush(stashRow, &colInfo);
    }

    ++i;  // next col
  }

C
Cary Xu 已提交
855
  dataLen = TD_ROW_LEN(pRow);
C
Cary Xu 已提交
856 857 858 859

  if (kvLen < dataLen) {
    // scan stashRow and generate SKVRow
    memset(buffer, 0, sizeof(dataLen));
C
Cary Xu 已提交
860
    STSRow tRow = buffer;
C
Cary Xu 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874
    memRowSetType(tRow, SMEM_ROW_KV);
    SKVRow kvRow = (SKVRow)memRowKvBody(tRow);
    int16_t nKvNCols = (int16_t) taosArrayGetSize(stashRow);
    kvRowSetLen(kvRow, (TDRowLenT)(TD_KV_ROW_HEAD_SIZE + sizeof(SColIdx) * nKvNCols));
    kvRowSetNCols(kvRow, nKvNCols);
    memRowSetKvVersion(tRow, pSchema1->version);

    int32_t toffset = 0;
    int16_t k;
    for (k = 0; k < nKvNCols; ++k) {
      SColInfo *pColInfo = taosArrayGet(stashRow, k);
      tdAppendKvColVal(kvRow, pColInfo->colVal, true, pColInfo->colId, pColInfo->colType, toffset);
      toffset += sizeof(SColIdx);
    }
C
Cary Xu 已提交
875
    ASSERT(kvLen == TD_ROW_LEN(tRow));
C
Cary Xu 已提交
876 877 878
  }
  taosArrayDestroy(stashRow);
  return buffer;
C
Cary Xu 已提交
879
#endif
C
Cary Xu 已提交
880
  return NULL;
C
Cary Xu 已提交
881
}
C
Cary Xu 已提交
882 883 884 885 886 887

SDataCols *tdDupDataCols(SDataCols *pDataCols, bool keepData) {
  SDataCols *pRet = tdNewDataCols(pDataCols->maxCols, pDataCols->maxPoints);
  if (pRet == NULL) return NULL;

  pRet->numOfCols = pDataCols->numOfCols;
C
Cary Xu 已提交
888
  pRet->bitmapMode = pDataCols->bitmapMode;
C
Cary Xu 已提交
889 890 891
  pRet->sversion = pDataCols->sversion;
  if (keepData) pRet->numOfRows = pDataCols->numOfRows;

C
Cary Xu 已提交
892
  for (int i = 0; i < pDataCols->numOfCols; ++i) {
C
Cary Xu 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    pRet->cols[i].type = pDataCols->cols[i].type;
    pRet->cols[i].bitmap = pDataCols->cols[i].bitmap;
    pRet->cols[i].colId = pDataCols->cols[i].colId;
    pRet->cols[i].bytes = pDataCols->cols[i].bytes;
    pRet->cols[i].offset = pDataCols->cols[i].offset;

    if (keepData) {
      if (pDataCols->cols[i].len > 0) {
        if (tdAllocMemForCol(&pRet->cols[i], pRet->maxPoints) < 0) {
          tdFreeDataCols(pRet);
          return NULL;
        }
        pRet->cols[i].len = pDataCols->cols[i].len;
        memcpy(pRet->cols[i].pData, pDataCols->cols[i].pData, pDataCols->cols[i].len);
        if (IS_VAR_DATA_TYPE(pRet->cols[i].type)) {
          int dataOffSize = sizeof(VarDataOffsetT) * pDataCols->maxPoints;
          memcpy(pRet->cols[i].dataOff, pDataCols->cols[i].dataOff, dataOffSize);
        }
        if (!TD_COL_ROWS_NORM(pRet->cols + i)) {
C
Cary Xu 已提交
912
          memcpy(pRet->cols[i].pBitmap, pDataCols->cols[i].pBitmap, TD_BITMAP_BYTES(pDataCols->numOfRows));
C
Cary Xu 已提交
913 914 915 916 917 918 919
        }
      }
    }
  }

  return pRet;
}