• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #5034

24 Apr 2026 11:25AM UTC coverage: 73.058%. Remained the same
#5034

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1336 of 1975 new or added lines in 48 files covered. (67.65%)

14149 existing lines in 164 files now uncovered.

275896 of 377640 relevant lines covered (73.06%)

132944440.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#define _DEFAULT_SOURCE
17
#include "tdataformat.h"
18
#include "decimal.h"
19
#include "tRealloc.h"
20
#include "tdatablock.h"
21
#include "tlog.h"
22

23
enum {
24
  BLOB_ROW_KV = 0,
25
  BLOB_ROW_TUPLE = 1,
26
};
27

28
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData);
29
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward);
30

31
static int32_t tRowMergeWithBlobImpl(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob, SBlobSet *pDstBlobSet,
32
                                     int32_t iStart, int32_t iEnd, int8_t flag);
33

34
static int32_t tRowBuildTupleWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
35
                                       SRow **ppRow, SBlobSet *pSrcBlobSet, SBlobSet *pDstBlobSet);
36

37
static int32_t tRowBuildKVRowWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
38
                                       SRow **ppRow, SBlobSet *pSrcBlob, SBlobSet *pDstBlobSet);
39
// ================================
40
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson);
41

42
// SRow ========================================================================
43
#define KV_FLG_LIT ((uint8_t)0x10)
44
#define KV_FLG_MID ((uint8_t)0x20)
45
#define KV_FLG_BIG ((uint8_t)0x40)
46

47
#define BIT_FLG_NONE  ((uint8_t)0x0)
48
#define BIT_FLG_NULL  ((uint8_t)0x1)
49
#define BIT_FLG_VALUE ((uint8_t)0x2)
50

51
#pragma pack(push, 1)
52
typedef struct {
53
  int16_t nCol;
54
  uint8_t idx[];  // uint8_t * | uint16_t * | uint32_t *
55
} SKVIdx;
56
#pragma pack(pop)
57

58
#define ROW_SET_BITMAP(PB, FLAG, IDX, VAL)      \
59
  do {                                          \
60
    switch (FLAG) {                             \
61
      case (HAS_NULL | HAS_NONE):               \
62
        SET_BIT1(PB, IDX, VAL);                 \
63
        break;                                  \
64
      case (HAS_VALUE | HAS_NONE):              \
65
        SET_BIT1(PB, IDX, (VAL) ? (VAL)-1 : 0); \
66
        break;                                  \
67
      case (HAS_VALUE | HAS_NULL):              \
68
        SET_BIT1(PB, IDX, (VAL)-1);             \
69
        break;                                  \
70
      case (HAS_VALUE | HAS_NULL | HAS_NONE):   \
71
        SET_BIT2(PB, IDX, VAL);                 \
72
        break;                                  \
73
      default:                                  \
74
        break;                                  \
75
    }                                           \
76
  } while (0)
77

78
static int32_t tPutPrimaryKeyIndex(uint8_t *p, const SPrimaryKeyIndex *index) {
253,574,346✔
79
  int32_t n = 0;
253,574,346✔
80
  n += tPutI8(p ? p + n : p, index->type);
253,574,346✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
253,585,896✔
82
  return n;
253,583,838✔
83
}
84

85
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
2,147,483,647✔
86
  int32_t n = 0;
2,147,483,647✔
87
  n += tGetI8(p + n, &index->type);
2,147,483,647✔
88
  n += tGetU32v(p + n, &index->offset);
2,147,483,647✔
89
  return n;
2,147,483,647✔
90
}
91

92
static FORCE_INLINE int32_t tRowBuildScanAddNone(SRowBuildScanInfo *sinfo, const STColumn *pTColumn) {
93
  if ((pTColumn->flags & COL_IS_KEY)) return TSDB_CODE_PAR_PRIMARY_KEY_IS_NONE;
2,147,483,647✔
94
  sinfo->numOfNone++;
2,147,483,647✔
95
  return 0;
2,147,483,647✔
96
}
97

98
static FORCE_INLINE int32_t tRowBuildScanAddNull(SRowBuildScanInfo *sinfo, const STColumn *pTColumn) {
99
  if ((pTColumn->flags & COL_IS_KEY)) return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,147,483,647✔
100
  sinfo->numOfNull++;
2,147,483,647✔
101
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
2,147,483,647✔
102
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
2,147,483,647✔
103
  return 0;
2,147,483,647✔
104
}
105

106
static FORCE_INLINE void tRowBuildScanAddValue(SRowBuildScanInfo *sinfo, SColVal *colVal, const STColumn *pTColumn) {
107
  bool isPK = ((pTColumn->flags & COL_IS_KEY) != 0);
2,147,483,647✔
108

109
  if (isPK) {
2,147,483,647✔
110
    sinfo->tupleIndices[sinfo->numOfPKs].type = colVal->value.type;
84,529,542✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
84,529,500✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
84,529,836✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
84,530,634✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
84,527,946✔
115
    sinfo->numOfPKs++;
84,527,988✔
116
  }
117

118
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
2,147,483,647✔
119
  if (IS_VAR_DATA_TYPE(colVal->value.type)) {
2,147,483,647✔
120
    if (sinfo->hasBlob && IS_STR_DATA_BLOB(colVal->value.type)) {
2,147,483,647✔
121
      sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)     // size
53,350,344✔
122
                             + BSE_SEQUECE_SIZE;                     // value
26,675,368✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
53,350,344✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
26,675,368✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
53,350,344✔
126
    } else {
127
      sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)  // size
2,147,483,647✔
128
                             + colVal->value.nData;               // value
2,147,483,647✔
129

130
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
2,147,483,647✔
131
                              + tPutU32v(NULL, colVal->value.nData)  // size
2,147,483,647✔
132
                              + colVal->value.nData;                 // value
2,147,483,647✔
133
    }
134
  } else {
135
    sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)              // colId
2,147,483,647✔
136
                            + tDataTypes[colVal->value.type].bytes;  // value
2,147,483,647✔
137
  }
138
  sinfo->numOfValue++;
2,147,483,647✔
139
}
2,147,483,647✔
140

141
static int32_t tRowBuildScan(SArray *colVals, const STSchema *schema, SRowBuildScanInfo *sinfo) {
2,147,483,647✔
142
  int32_t  code = 0;
2,147,483,647✔
143
  int32_t  colValIndex = 1;
2,147,483,647✔
144
  int32_t  numOfColVals = TARRAY_SIZE(colVals);
2,147,483,647✔
145
  SColVal *colValArray = (SColVal *)TARRAY_DATA(colVals);
2,147,483,647✔
146

147
  if (!(numOfColVals > 0)) {
2,147,483,647✔
148
    return TSDB_CODE_INVALID_PARA;
×
149
  }
150
  if (!(colValArray[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
2,147,483,647✔
151
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
152
  }
153
  if (!(colValArray[0].value.type == TSDB_DATA_TYPE_TIMESTAMP)) {
2,147,483,647✔
154
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
155
  }
156

157
  *sinfo = (SRowBuildScanInfo){.tupleFixedSize = schema->flen, .hasBlob = sinfo->hasBlob, .scanType = sinfo->scanType};
2,147,483,647✔
158

159
  // loop scan
160
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
161
    for (;;) {
162
      if (colValIndex >= numOfColVals) {
2,147,483,647✔
163
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
221,024✔
164
        break;
110,512✔
165
      }
166

167
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
168
        if (!(colValArray[colValIndex].value.type == schema->columns[i].type)) {
2,147,483,647✔
169
          code = TSDB_CODE_INVALID_PARA;
×
170
          goto _exit;
×
171
        }
172

173
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {
2,147,483,647✔
174
          tRowBuildScanAddValue(sinfo, &colValArray[colValIndex], schema->columns + i);
2,147,483,647✔
175
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {
2,147,483,647✔
176
          if ((code = tRowBuildScanAddNull(sinfo, schema->columns + i))) goto _exit;
2,147,483,647✔
177
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {
2,147,483,647✔
178
          if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
2,147,483,647✔
179
        }
180

181
        colValIndex++;
2,147,483,647✔
182
        break;
2,147,483,647✔
183
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {
2,095,436✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
89,664✔
185
        break;
44,832✔
186
      } else {  // skip useless value
187
        colValIndex++;
2,050,604✔
188
      }
189
    }
190
  }
191

192
  if (sinfo->numOfNone) {
2,147,483,647✔
193
    sinfo->flag |= HAS_NONE;
2,147,483,647✔
194
  }
195
  if (sinfo->numOfNull) {
2,147,483,647✔
196
    sinfo->flag |= HAS_NULL;
2,122,356,346✔
197
  }
198
  if (sinfo->numOfValue) {
2,147,483,647✔
199
    sinfo->flag |= HAS_VALUE;
2,147,483,647✔
200
  }
201

202
  // Tuple
203
  sinfo->tupleFlag = sinfo->flag;
2,147,483,647✔
204
  switch (sinfo->flag) {
2,147,483,647✔
205
    case HAS_NONE:
2,147,483,647✔
206
    case HAS_NULL:
207
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
208
      sinfo->tupleFixedSize = 0;
2,147,483,647✔
209
      break;
2,147,483,647✔
210
    case HAS_VALUE:
2,147,483,647✔
211
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
212
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
213
      break;
2,147,483,647✔
214
    case (HAS_NONE | HAS_NULL):
2,570,193✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,570,193✔
216
      sinfo->tupleFixedSize = 0;
2,570,826✔
217
      break;
2,570,826✔
218
    case (HAS_NONE | HAS_VALUE):
2,147,483,647✔
219
    case (HAS_NULL | HAS_VALUE):
220
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,147,483,647✔
221
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
222
      break;
2,147,483,647✔
223
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
1,186,206,756✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,186,206,756✔
225
      sinfo->tupleFixedSize = schema->flen;
1,186,208,022✔
226
      break;
1,049,250,573✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
84,525,762✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
84,530,172✔
231
  }
232
  sinfo->tupleRowSize = sizeof(SRow)              // SRow
2,147,483,647✔
233
                        + sinfo->tuplePKSize      // primary keys
2,147,483,647✔
234
                        + sinfo->tupleBitmapSize  // bitmap
2,147,483,647✔
235
                        + sinfo->tupleFixedSize   // fixed part
2,147,483,647✔
236
                        + sinfo->tupleVarSize;    // var part
2,147,483,647✔
237

238
  // Key-Value
239
  if (sinfo->kvMaxOffset <= UINT8_MAX) {
2,147,483,647✔
240
    sinfo->kvFlag = (KV_FLG_LIT | sinfo->flag);
2,147,483,647✔
241
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint8_t);
2,147,483,647✔
242
  } else if (sinfo->kvMaxOffset <= UINT16_MAX) {
47,892,406✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
47,827,308✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
47,829,096✔
245
  } else {
246
    sinfo->kvFlag = (KV_FLG_BIG | sinfo->flag);
65,300✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint32_t);
65,300✔
248
  }
249
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
250
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
84,528,156✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
84,530,508✔
252
  }
253
  sinfo->kvRowSize = sizeof(SRow)             // SRow
2,147,483,647✔
254
                     + sinfo->kvPKSize        // primary keys
2,147,483,647✔
255
                     + sinfo->kvIndexSize     // index array
2,147,483,647✔
256
                     + sinfo->kvPayloadSize;  // payload
2,147,483,647✔
257

258
_exit:
2,147,483,647✔
259
  return code;
2,147,483,647✔
260
}
261

262
static int32_t tRowBuildTupleRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
2,147,483,647✔
263
                                 SRow **ppRow) {
264
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
2,147,483,647✔
265
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
2,147,483,647✔
266
  if (*ppRow == NULL) {
2,147,483,647✔
267
    return terrno;
×
268
  }
269
  (*ppRow)->flag = sinfo->tupleFlag;
2,147,483,647✔
270
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
2,147,483,647✔
271
  (*ppRow)->sver = schema->version;
2,147,483,647✔
272
  (*ppRow)->len = sinfo->tupleRowSize;
2,147,483,647✔
273
  (*ppRow)->ts = VALUE_GET_TRIVIAL_DATUM(&colValArray[0].value);
2,147,483,647✔
274

275
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
2,147,483,647✔
276
    return 0;
2,147,483,647✔
277
  }
278

279
  uint8_t *primaryKeys = (*ppRow)->data;
2,147,483,647✔
280
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
2,147,483,647✔
281
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
2,147,483,647✔
282
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
2,147,483,647✔
283

284
  // primary keys
285
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
286
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
78,362,020✔
287
  }
288

289
  // bitmap + fixed + varlen
290
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
2,147,483,647✔
291
  int32_t colValIndex = 1;
2,147,483,647✔
292
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
293
    for (;;) {
294
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
295
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
15,048✔
296
        break;
15,048✔
297
      }
298

299
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
300
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
301
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_VALUE);
2,147,483,647✔
302

303
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647✔
304
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
2,147,483,647✔
305
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
2,147,483,647✔
306
            if (colValArray[colValIndex].value.nData) {
2,147,483,647✔
307
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
2,147,483,647✔
308
              varlen += colValArray[colValIndex].value.nData;
2,147,483,647✔
309
            }
310
          } else {
311
            (void)memcpy(fixed + schema->columns[i].offset,
2,147,483,647✔
312
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,147,483,647✔
313
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647✔
314
          }
315
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
816,485,144✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
762,077,846✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
54,979,580✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
54,978,353✔
319
        }
320

321
        colValIndex++;
2,147,483,647✔
322
        break;
2,147,483,647✔
323
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
144✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
144✔
325
        break;
144✔
326
      } else {
327
        colValIndex++;
×
328
      }
329
    }
330
  }
331

332
  return 0;
2,147,483,647✔
333
}
334

335
static int32_t addEmptyItemToBlobSet(SBlobSet *pBlobSet, int8_t type, uint64_t *pSeq) {
2,712,756✔
336
  SBlobItem item = {.type = type, .len = 0};
2,712,756✔
337
  uint64_t  seq = 0;
2,712,756✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
2,712,756✔
339
  if (code != 0) return code;
2,712,756✔
340

341
  if (pSeq != NULL) {
2,712,756✔
342
    *pSeq = seq;
×
343
  }
344
  return 0;
2,712,756✔
345
}
346
static int32_t tRowBuildTupleWithBlob(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
555,579✔
347
                                      SRow **ppRow, SBlobSet *pBlobSet) {
348
  int32_t  code = 0;
555,579✔
349
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
555,579✔
350
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
555,579✔
351
  if (*ppRow == NULL) {
555,579✔
352
    return terrno;
×
353
  }
354
  (*ppRow)->flag = sinfo->tupleFlag;
555,579✔
355
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
555,579✔
356
  (*ppRow)->sver = schema->version;
555,579✔
357
  (*ppRow)->len = sinfo->tupleRowSize;
555,579✔
358
  (*ppRow)->ts = colValArray[0].value.val;
555,579✔
359

360
  if (((*ppRow)->flag) == 0) {
555,579✔
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
555,579✔
364
    code = addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL);
550,652✔
365
    return code;
550,652✔
366
  }
367

368
  uint8_t *primaryKeys = (*ppRow)->data;
4,927✔
369
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
4,927✔
370
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
4,927✔
371
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
4,927✔
372
  uint8_t *start = varlen;
4,927✔
373

374
  // primary keys
375
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
4,927✔
376
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
×
377
  }
378

379
  // bitmap + fixed + varlen
380
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
4,927✔
381
  int32_t colValIndex = 1;
4,927✔
382
  int8_t  firstBlobCol = 1;
4,927✔
383
  for (int32_t i = 1; i < schema->numOfCols; i++) {
94,920✔
384
    for (;;) {
×
385
      if (colValIndex >= numOfColVals) {  // NONE
89,993✔
386
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
387
        break;
×
388
      }
389

390
      int8_t hasBlob = 0;
89,993✔
391
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
89,993✔
392
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
89,993✔
393
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_VALUE);
79,808✔
394

395
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
79,808✔
396
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
22,062✔
397
              hasBlob = 1;
4,442✔
398
            }
399
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
22,062✔
400
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
44,124✔
401
            if (colValArray[colValIndex].value.nData) {
22,062✔
402
              if (hasBlob == 0) {
22,062✔
403
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
17,620✔
404
                varlen += colValArray[colValIndex].value.nData;
17,620✔
405
              } else {
406
                uint64_t  seq = 0;
4,442✔
407
                SBlobItem item = {.seqOffsetInRow = varlen - (*ppRow)->data,
4,442✔
408
                                  .data = colValArray[colValIndex].value.pData,
4,442✔
409
                                  .len = colValArray[colValIndex].value.nData,
4,442✔
410
                                  .type = TSDB_DATA_BLOB_VALUE};
411
                code = tBlobSetPush(pBlobSet, &item, &seq, firstBlobCol);
4,442✔
412
                if (firstBlobCol == 1) {
4,442✔
413
                  firstBlobCol = 0;
4,442✔
414
                }
415
                if (code != 0) return code;
4,442✔
416
                varlen += tPutU64(varlen, seq);
8,884✔
417
              }
418
            } else {
419
              if (hasBlob) TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
420
            }
421
          } else {
422
            (void)memcpy(fixed + schema->columns[i].offset,
57,746✔
423
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
57,746✔
424
                         tDataTypes[schema->columns[i].type].bytes);
57,746✔
425
          }
426
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
10,185✔
427
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
485✔
428
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
485✔
429
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
485✔
430
          }
431
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
9,700✔
432
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
9,700✔
433
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
434
          }
435
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
9,700✔
436
        }
437

438
        colValIndex++;
89,993✔
439
        break;
89,993✔
440
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
441
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
442
        break;
×
443
      } else {
444
        colValIndex++;
×
445
      }
446
    }
447
  }
448

449
  if (((*ppRow)->flag) == 0) {
4,927✔
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
4,927✔
454
}
455

456
static int32_t tRowBuildTupleWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
×
457
                                       SRow **ppRow, SBlobSet *pSrcBlobSet, SBlobSet *pDstBlobSet) {
458
  int32_t  code = 0;
×
459
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
×
460
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
×
461
  if (*ppRow == NULL) {
×
462
    return terrno;
×
463
  }
464
  (*ppRow)->flag = sinfo->tupleFlag;
×
465
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
×
466
  (*ppRow)->sver = schema->version;
×
467
  (*ppRow)->len = sinfo->tupleRowSize;
×
468
  (*ppRow)->ts = colValArray[0].value.val;
×
469

470
  if (((*ppRow)->flag) == 0) {
×
471
    return TSDB_CODE_INVALID_PARA;
×
472
  }
473
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
×
474
    code = addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
475
    return code;
×
476
  }
477

478
  uint8_t *primaryKeys = (*ppRow)->data;
×
479
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
×
480
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
×
481
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
×
482
  uint8_t *start = varlen;
×
483

484
  // primary keys
485
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
×
486
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
×
487
  }
488

489
  // bitmap + fixed + varlen
490
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
×
491
  int32_t colValIndex = 1;
×
492
  for (int32_t i = 1; i < schema->numOfCols; i++) {
×
493
    for (;;) {
×
494
      if (colValIndex >= numOfColVals) {  // NONE
×
495
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
496
        break;
×
497
      }
498

499
      int8_t hasBlob = 0;
×
500
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
×
501
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
×
502
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_VALUE);
×
503

504
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
505
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
506
              hasBlob = 1;
×
507
            }
508
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
×
509
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
×
510
            if (colValArray[colValIndex].value.nData) {
×
511
              if (hasBlob == 0) {
×
512
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
×
513
                varlen += colValArray[colValIndex].value.nData;
×
514
              } else {
515
                uint64_t seq = 0;
×
516
                if (tGetU64(colValArray[colValIndex].value.pData, &seq) < 0) {
×
517
                  TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
518
                }
519
                SBlobItem item = {0};
×
520

521
                code = tBlobSetGet(pSrcBlobSet, seq, &item);
×
522
                TAOS_CHECK_RETURN(code);
×
523

524
                code = tBlobSetPush(pDstBlobSet, &item, &seq, 0);
×
525
                TAOS_CHECK_RETURN(code);
×
526
                varlen += tPutU64(varlen, seq);
×
527
              }
528
            } else {
529
              if (hasBlob) TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
530
            }
531
          } else {
532
            (void)memcpy(fixed + schema->columns[i].offset,
×
533
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
×
534
                         tDataTypes[schema->columns[i].type].bytes);
×
535
          }
536
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
537
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
×
538
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
539
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
540
          }
541
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
542
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
543
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
544
          }
545
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
546
        }
547

548
        colValIndex++;
×
549
        break;
×
550
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
551
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
552
        break;
×
553
      } else {
554
        colValIndex++;
×
555
      }
556
    }
557
  }
558

559
  if (((*ppRow)->flag) == 0) {
×
560
    return TSDB_CODE_INVALID_PARA;
×
561
  }
562

563
  return 0;
×
564
}
565
static FORCE_INLINE void tRowBuildKVRowSetIndex(uint8_t flag, SKVIdx *indices, uint32_t offset) {
566
  if (flag & KV_FLG_LIT) {
2,147,483,647✔
567
    ((uint8_t *)indices->idx)[indices->nCol] = (uint8_t)offset;
2,147,483,647✔
568
  } else if (flag & KV_FLG_MID) {
780,501,405✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
780,501,405✔
570
  } else {
571
    ((uint32_t *)indices->idx)[indices->nCol] = (uint32_t)offset;
×
572
  }
573
  indices->nCol++;
2,147,483,647✔
574
}
2,147,483,647✔
575

576
static int32_t tRowBuildKVRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema, SRow **ppRow) {
2,147,483,647✔
577
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
2,147,483,647✔
578

579
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
2,147,483,647✔
580
  if (*ppRow == NULL) {
2,147,483,647✔
581
    return terrno;
×
582
  }
583
  (*ppRow)->flag = sinfo->kvFlag;
2,147,483,647✔
584
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
2,147,483,647✔
585
  (*ppRow)->sver = schema->version;
2,147,483,647✔
586
  (*ppRow)->len = sinfo->kvRowSize;
2,147,483,647✔
587
  (*ppRow)->ts = VALUE_GET_TRIVIAL_DATUM(&colValArray[0].value);
2,147,483,647✔
588

589
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
2,147,483,647✔
590
    return TSDB_CODE_INVALID_PARA;
×
591
  }
592

593
  uint8_t *primaryKeys = (*ppRow)->data;
2,147,483,647✔
594
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
2,147,483,647✔
595
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
2,147,483,647✔
596
  uint32_t payloadSize = 0;
2,147,483,647✔
597

598
  // primary keys
599
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
600
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
6,165,884✔
601
  }
602

603
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
2,147,483,647✔
604
  int32_t colValIndex = 1;
2,147,483,647✔
605
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
606
    for (;;) {
607
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
608
        break;
40,248✔
609
      }
610

611
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
612
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
613
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647✔
614
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647✔
615
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,147,483,647✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
2,147,483,647✔
617
            if (colValArray[colValIndex].value.nData > 0) {
2,147,483,647✔
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
2,147,483,647✔
619
                           colValArray[colValIndex].value.nData);
2,147,483,647✔
620
              payloadSize += colValArray[colValIndex].value.nData;
2,147,483,647✔
621
            }
622
          } else {
623
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,147,483,647✔
624
            (void)memcpy(payload + payloadSize,
2,147,483,647✔
625
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,147,483,647✔
626
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647✔
627
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,147,483,647✔
628
          }
629
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
2,147,483,647✔
630
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647✔
631
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
2,147,483,647✔
632
        }
633

634
        colValIndex++;
2,147,483,647✔
635
        break;
2,147,483,647✔
636
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
44,688✔
637
        break;
44,688✔
638
      } else {
639
        colValIndex++;
×
640
      }
641
    }
642
  }
643
  return 0;
2,147,483,647✔
644
}
645

646
static int32_t tRowBuildKVRowWithBlob(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
26,672,462✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
26,672,462✔
649

650
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
26,672,462✔
651
  if (*ppRow == NULL) {
26,672,462✔
652
    return terrno;
×
653
  }
654
  (*ppRow)->flag = sinfo->kvFlag;
26,672,462✔
655
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
26,672,462✔
656
  (*ppRow)->sver = schema->version;
26,672,462✔
657
  (*ppRow)->len = sinfo->kvRowSize;
26,672,462✔
658
  (*ppRow)->ts = colValArray[0].value.val;
26,672,462✔
659

660
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
26,672,462✔
661
    return TSDB_CODE_INVALID_PARA;
×
662
  }
663

664
  if (((*ppRow)->flag) == 0) {
26,672,462✔
665
    return TSDB_CODE_INVALID_PARA;
×
666
  }
667

668
  uint8_t *primaryKeys = (*ppRow)->data;
26,672,462✔
669
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
26,672,462✔
670
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
26,672,462✔
671
  uint32_t payloadSize = 0;
26,672,462✔
672

673
  // primary keys
674
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
26,672,462✔
675
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
×
676
  }
677

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
26,672,462✔
679
  int32_t colValIndex = 1;
26,672,462✔
680
  int8_t  firstBlobCol = 1;
26,672,462✔
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
60,174,712✔
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
33,502,250✔
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
33,502,250✔
687

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
33,502,250✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
33,502,250✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
30,096,014✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
30,096,014✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
27,364,774✔
693
              hasBlob = 1;
26,670,926✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
27,364,774✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
27,364,774✔
697
            if (colValArray[colValIndex].value.nData > 0) {
27,364,774✔
698
              if (hasBlob == 0) {
25,212,965✔
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
693,848✔
700
                             colValArray[colValIndex].value.nData);
693,848✔
701
                payloadSize += colValArray[colValIndex].value.nData;
693,848✔
702
              } else {
703
                uint64_t  seq = 0;
24,519,117✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
24,519,117✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
24,519,117✔
706
                                  .data = colValArray[colValIndex].value.pData,
24,519,117✔
707
                                  .len = colValArray[colValIndex].value.nData,
24,519,117✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
24,519,117✔
710
                if (code != 0) return code;
24,519,117✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
49,038,234✔
712
              }
713
            } else {
714
              if (hasBlob) {
2,151,809✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
2,151,809✔
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,731,240✔
720
            (void)memcpy(payload + payloadSize,
2,732,024✔
721
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,731,240✔
722
                         tDataTypes[schema->columns[i].type].bytes);
2,731,240✔
723
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,731,240✔
724
          }
725
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
3,406,236✔
726
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
1,536✔
727
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
1,536✔
728
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
1,536✔
729
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
1,536✔
730
          }
731
        }
732
        colValIndex++;
33,502,250✔
733
        break;
33,502,250✔
734
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
735
        break;
×
736
      } else {
737
        colValIndex++;
×
738
      }
739
    }
740
  }
741

742
  if (((*ppRow)->flag) == 0) {
26,672,462✔
743
    return TSDB_CODE_INVALID_PARA;
×
744
  }
745
  return 0;
26,672,462✔
746
}
747

748
static int32_t tRowBuildKVRowWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
×
749
                                       SRow **ppRow, SBlobSet *pSrcBlobSet, SBlobSet *pDstBlobSet) {
750
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
×
751

752
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
×
753
  if (*ppRow == NULL) {
×
754
    return terrno;
×
755
  }
756
  (*ppRow)->flag = sinfo->kvFlag;
×
757
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
×
758
  (*ppRow)->sver = schema->version;
×
759
  (*ppRow)->len = sinfo->kvRowSize;
×
760
  (*ppRow)->ts = colValArray[0].value.val;
×
761

762
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
×
763
    return TSDB_CODE_INVALID_PARA;
×
764
  }
765

766
  if (((*ppRow)->flag) == 0) {
×
767
    return TSDB_CODE_INVALID_PARA;
×
768
  }
769

770
  uint8_t *primaryKeys = (*ppRow)->data;
×
771
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
×
772
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
×
773
  uint32_t payloadSize = 0;
×
774

775
  // primary keys
776
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
×
777
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
×
778
  }
779

780
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
×
781
  int32_t colValIndex = 1;
×
782
  for (int32_t i = 1; i < schema->numOfCols; i++) {
×
783
    for (;;) {
×
784
      if (colValIndex >= numOfColVals) {  // NONE
×
785
        break;
×
786
      }
787
      uint8_t hasBlob = 0;
×
788

789
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
×
790
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
×
791
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
792
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
793
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
794
              hasBlob = 1;
×
795
            }
796
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
797
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
×
798
            if (colValArray[colValIndex].value.nData > 0) {
×
799
              if (hasBlob == 0) {
×
800
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
×
801
                             colValArray[colValIndex].value.nData);
×
802
                payloadSize += colValArray[colValIndex].value.nData;
×
803
              } else {
804
                uint64_t seq = 0;
×
805
                if (tGetU64(colValArray[colValIndex].value.pData, &seq) < 0) {
×
806
                  TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
807
                }
808
                SBlobItem item = {0};
×
809

810
                int32_t code = tBlobSetGet(pSrcBlobSet, seq, &item);
×
811
                TAOS_CHECK_RETURN(code);
×
812

813
                code = tBlobSetPush(pDstBlobSet, &item, &seq, 0);
×
814
                TAOS_CHECK_RETURN(code);
×
815

816
                payloadSize += tPutU64(payload + payloadSize, seq);
×
817
              }
818
            } else {
819
              if (hasBlob) {
×
820
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
821
              }
822
            }
823
          } else {
824
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
825
            (void)memcpy(payload + payloadSize,
×
826
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
×
827
                         tDataTypes[schema->columns[i].type].bytes);
×
828
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
×
829
          }
830
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
831
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
832
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
×
833
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
834
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
835
          }
836
        }
837
        colValIndex++;
×
838
        break;
×
839
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
840
        break;
×
841
      } else {
842
        colValIndex++;
×
843
      }
844
    }
845
  }
846

847
  if (((*ppRow)->flag) == 0) {
×
848
    return TSDB_CODE_INVALID_PARA;
×
849
  }
850
  return 0;
×
851
}
852
int32_t tRowBuild(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SRowBuildScanInfo *sinfo) {
2,147,483,647✔
853
  int32_t code;
854
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
2,147,483,647✔
855
  if (code) return code;
2,147,483,647✔
856

857
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
2,147,483,647✔
858
    code = tRowBuildTupleRow(aColVal, sinfo, pTSchema, ppRow);
2,147,483,647✔
859
  } else {
860
    code = tRowBuildKVRow(aColVal, sinfo, pTSchema, ppRow);
2,147,483,647✔
861
  }
862
  return code;
2,147,483,647✔
863
}
864

865
int32_t tBlobRowCreate(int64_t cap, int8_t type, SBlobSet **ppBlobRow) {
×
866
  int32_t    lino = 0;
×
867
  int32_t    code = 0;
×
868
  SBlobSet *p = taosMemCalloc(1, sizeof(SBlobSet));
×
869
  if (p == NULL) {
×
870
    return terrno;
×
871
  }
872

873
  p->type = type;
×
874
  p->pSeqTable = taosArrayInit(128, sizeof(SBlobValue));
×
875
  if (p->pSeqTable == NULL) {
×
876
    TAOS_CHECK_EXIT(terrno);
×
877
  }
878

879
  p->data = taosMemCalloc(1024, cap * sizeof(uint8_t));
×
880
  if (p->data == NULL) {
×
881
    TAOS_CHECK_EXIT(terrno);
×
882
  }
883

884
  p->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
×
885
  if (p->pSeqToffset == NULL) {
×
886
    TAOS_CHECK_EXIT(terrno);
×
887
  }
888

889
  p->pSet = taosArrayInit(4, sizeof(int32_t));
×
890
  if (p->pSet == NULL) {
×
891
    TAOS_CHECK_EXIT(terrno);
×
892
  }
893

894
  p->cap = cap;
×
895
  p->len = 0;
×
896
  p->seq = 1;
×
897

898
  *ppBlobRow = p;
×
899
_exit:
×
900
  if (code != 0) {
×
901
    taosHashCleanup(p->pSeqToffset);
×
902
    taosArrayDestroy(p->pSeqTable);
×
903
    taosArrayDestroy(p->pSet);
×
904

905
    taosMemoryFree(p->data);
×
906
    taosMemoryFree(p);
×
907
  }
908
  uDebug("create blob row %p", p);
×
909
  return code;
×
910
}
911
int32_t tBlobRowPush(SBlobSet *pBlobRow, SBlobItem *pItem, uint64_t *seq, int8_t nextRow) {
×
912
  if (pBlobRow == NULL || pItem == NULL || seq == NULL) {
×
913
    return TSDB_CODE_INVALID_PARA;
×
914
  }
915
  uTrace("push blob %p, seqOffsetInRow %d, dataLen %d, nextRow %d", pBlobRow, pItem->seqOffsetInRow, pItem->len,
×
916
         nextRow);
917
  int32_t  lino = 0;
×
918
  int32_t  code = 0;
×
919
  uint64_t offset;
920
  uint32_t len = pItem->len;
×
921
  uint8_t *data = pItem->data;
×
922
  int32_t  dataOffset = pItem->seqOffsetInRow;
×
923

924
  uint64_t tlen = pBlobRow->len + len;
×
925
  if (tlen > pBlobRow->cap) {
×
926
    int64_t cap = pBlobRow->cap;
×
927
    // opt later
928
    do {
929
      cap = cap * 2;
×
930
    } while (tlen > cap);
×
931

932
    uint8_t *data = taosMemRealloc(pBlobRow->data, cap);
×
933
    if (data == NULL) {
×
934
      return terrno;
×
935
    }
936
    pBlobRow->data = data;
×
937
    pBlobRow->cap = cap;
×
938
  }
939
  if (len > 0) {
×
940
    (void)memcpy(pBlobRow->data + pBlobRow->len, data, len);
×
941
  }
942

943
  offset = pBlobRow->len;
×
944
  pBlobRow->len += len;
×
945

946
  pBlobRow->seq++;
×
947
  *seq = pBlobRow->seq;
×
948

949
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow};
×
950
  if (taosArrayPush(pBlobRow->pSeqTable, &value) == NULL) {
×
951
    TAOS_CHECK_EXIT(terrno);
×
952
  }
953

954
  int32_t sz = taosArrayGetSize(pBlobRow->pSeqTable);
×
955
  code = taosHashPut(pBlobRow->pSeqToffset, seq, sizeof(int64_t), &sz, sizeof(int32_t));
×
956
  if (code != 0) {
×
957
    TAOS_CHECK_EXIT(code);
×
958
  }
959

960
_exit:
×
961
  return code;
×
962
}
963
int32_t tBlobRowUpdate(SBlobSet *pBlobRow, uint64_t seq, SBlobItem *pItem) {
×
964
  int32_t code = 0;
×
965
  return code;
×
966
  if (pBlobRow == NULL || pItem == NULL) {
967
    return TSDB_CODE_INVALID_PARA;
968
  }
969
  if (pBlobRow->pSeqToffset == NULL || pBlobRow->pSeqTable == NULL) {
970
    return TSDB_CODE_INVALID_PARA;
971
  }
972

973
  return code;
974
}
975
int32_t tBlobRowGet(SBlobSet *pBlobRow, uint64_t seq, SBlobItem *pItem) {
×
976
  if (pBlobRow == NULL || pItem == NULL) {
×
977
    return TSDB_CODE_INVALID_PARA;
×
978
  }
979
  if (pBlobRow->pSeqToffset == NULL || pBlobRow->pSeqTable == NULL) {
×
980
    return TSDB_CODE_INVALID_PARA;
×
981
  }
982

983
  int32_t code = 0;
×
984
  int32_t len = 0;
×
985
  int32_t dataOffset = 0;
×
986
  int8_t  nextRow = 0;
×
987

988
  int32_t *offset = (int32_t *)taosHashGet(pBlobRow->pSeqToffset, &seq, sizeof(int64_t));
×
989
  if (offset == NULL) {
×
990
    return TSDB_CODE_INVALID_PARA;
×
991
  }
992

993
  SBlobValue *value = taosArrayGet(pBlobRow->pSeqTable, *offset - 1);
×
994
  if (value == NULL) {
×
995
    return TSDB_CODE_INVALID_PARA;
×
996
  }
997

998
  len = value->len;
×
999
  dataOffset = value->dataOffset;
×
1000
  nextRow = value->nextRow;
×
1001

1002
  pItem->seqOffsetInRow = dataOffset + pBlobRow->data - (uint8_t *)pBlobRow;
×
1003
  pItem->len = len;
×
1004
  pItem->data = pBlobRow->data + value->offset;
×
1005

1006
  return code;
×
1007
}
1008

1009
int32_t tBlobRowSize(SBlobSet *pBlobRow) {
×
1010
  if (pBlobRow == NULL) return 0;
×
1011
  return taosArrayGetSize(pBlobRow->pSeqTable);
×
1012
}
1013
int32_t tBlobRowEnd(SBlobSet *pBlobRow) {
×
1014
  if (pBlobRow == NULL) return 0;
×
1015
  int32_t sz = taosArrayGetSize(pBlobRow->pSeqTable);
×
1016
  if (taosArrayPush(pBlobRow->pSet, &sz) == NULL) {
×
1017
    return terrno;
×
1018
  }
1019
  return 0;
×
1020
}
1021
int32_t tBlobRowRebuild(SBlobSet *p, int32_t sRow, int32_t nrow, SBlobSet **pDst) {
×
1022
  int32_t code = 0;
×
1023

1024
  code = tBlobRowCreate(p->cap, p->type, pDst);
×
1025
  if (code != 0) {
×
1026
    return code;
×
1027
  }
1028

1029
  SBlobSet *pBlobRow = *pDst;
×
1030
  if (p->pSeqToffset == NULL || p->pSeqTable == NULL || p->data == NULL) {
×
1031
    return TSDB_CODE_INVALID_PARA;
×
1032
  }
1033

1034
  uint64_t seq = 0;
×
1035
  int32_t  count = 0;
×
1036
  for (int32_t i = sRow; i < taosArrayGetSize(p->pSeqTable); i++) {
×
1037
    SBlobValue *pValue = taosArrayGet(p->pSeqTable, i);
×
1038
    uint8_t    *data = p->data + pValue->offset;
×
1039
    int32_t     len = pValue->len;
×
1040

1041
    SBlobItem item = {.data = data, .len = len, .seqOffsetInRow = pValue->dataOffset};
×
1042

1043
    code = tBlobRowPush(pBlobRow, &item, &seq, pValue->nextRow);
×
1044
    count++;
×
1045
    if (count >= nrow) {
×
1046
      break;
×
1047
    }
1048
  }
1049

1050
  return code;
×
1051
}
1052

1053
int32_t tBlobRowDestroy(SBlobSet *pBlobRow) {
×
1054
  if (pBlobRow == NULL) return 0;
×
1055
  int32_t code = 0;
×
1056
  uTrace("destroy blob row, seqTable size %p", pBlobRow);
×
1057
  taosMemoryFree(pBlobRow->data);
×
1058
  taosArrayDestroy(pBlobRow->pSeqTable);
×
1059
  taosHashCleanup(pBlobRow->pSeqToffset);
×
1060
  taosArrayDestroy(pBlobRow->pSet);
×
1061
  taosMemoryFree(pBlobRow);
×
1062
  return code;
×
1063
}
1064
int32_t tBlobRowClear(SBlobSet *pBlobRow) {
×
1065
  if (pBlobRow == NULL) return 0;
×
1066
  int32_t code = 0;
×
1067
  uTrace("clear blob row, seqTable size %p", pBlobRow);
×
1068
  taosArrayClear(pBlobRow->pSeqTable);
×
1069
  taosHashClear(pBlobRow->pSeqToffset);
×
1070
  pBlobRow->len = 0;
×
1071
  pBlobRow->seq = 1;
×
1072
  return code;
×
1073
}
1074

1075
int32_t tRowBuildWithBlob(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SBlobSet *pBlobSet,
27,228,041✔
1076
                          SRowBuildScanInfo *sinfo) {
1077
  int32_t code;
1078

1079
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
27,228,041✔
1080
  if (code) return code;
27,228,041✔
1081

1082
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
27,228,041✔
1083
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
555,579✔
1084
  } else {
1085
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
26,672,462✔
1086
  }
1087

1088
  return code;
27,228,041✔
1089
}
1090

1091
int32_t tRowBuildWithBlob2(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SBlobSet *pSrcBlobSet,
×
1092
                           SBlobSet *pDstBlobSet, SRowBuildScanInfo *sinfo) {
1093
  int32_t code;
1094

1095
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
×
1096
  if (code) return code;
×
1097

1098
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
×
1099
    code = tRowBuildTupleWithBlob2(aColVal, sinfo, pTSchema, ppRow, pSrcBlobSet, pDstBlobSet);
×
1100
  } else {
1101
    code = tRowBuildKVRowWithBlob2(aColVal, sinfo, pTSchema, ppRow, pSrcBlobSet, pDstBlobSet);
×
1102
  }
1103

1104
  return code;
×
1105
}
1106

1107
int32_t tBlobSetCreate(int64_t cap, int8_t type, SBlobSet **ppBlobSet) {
50,093✔
1108
  int32_t    lino = 0;
50,093✔
1109
  int32_t    code = 0;
50,093✔
1110
  SBlobSet  *p = taosMemCalloc(1, sizeof(SBlobSet));
50,093✔
1111
  if (p == NULL) {
50,093✔
1112
    return terrno;
×
1113
  }
1114

1115
  p->type = type;
50,093✔
1116
  p->pSeqTable = taosArrayInit(128, sizeof(SBlobValue));
50,093✔
1117
  if (p->pSeqTable == NULL) {
50,093✔
1118
    TAOS_CHECK_EXIT(terrno);
×
1119
  }
1120

1121
  p->data = taosMemCalloc(1024, cap * sizeof(uint8_t));
50,093✔
1122
  if (p->data == NULL) {
50,093✔
1123
    TAOS_CHECK_EXIT(terrno);
×
1124
  }
1125

1126
  p->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
50,093✔
1127
  if (p->pSeqToffset == NULL) {
50,093✔
1128
    TAOS_CHECK_EXIT(terrno);
×
1129
  }
1130

1131
  p->pSet = taosArrayInit(4, sizeof(int32_t));
50,093✔
1132
  if (p->pSet == NULL) {
50,093✔
1133
    TAOS_CHECK_EXIT(terrno);
×
1134
  }
1135

1136
  p->cap = cap;
50,093✔
1137
  p->len = 0;
50,093✔
1138
  p->seq = 1;
50,093✔
1139

1140
  *ppBlobSet = p;
50,093✔
1141
_exit:
50,093✔
1142
  if (code != 0) {
50,093✔
1143
    taosHashCleanup(p->pSeqToffset);
×
1144
    taosArrayDestroy(p->pSeqTable);
×
1145
    taosArrayDestroy(p->pSet);
×
1146

1147
    taosMemoryFree(p->data);
×
1148
    taosMemoryFree(p);
×
1149
  }
1150
  uDebug("create blob row %p", p);
50,093✔
1151
  return code;
50,093✔
1152
}
1153
int32_t tBlobSetPush(SBlobSet *pBlobSet, SBlobItem *pItem, uint64_t *seq, int8_t nextRow) {
27,236,434✔
1154
  if (pBlobSet == NULL || pItem == NULL || seq == NULL) {
27,236,434✔
1155
    return TSDB_CODE_INVALID_PARA;
×
1156
  }
1157
  uTrace("push blob %p, seqOffsetInRow %d, dataLen %d, nextRow %d", pBlobSet, pItem->seqOffsetInRow, pItem->len,
27,236,434✔
1158
         nextRow);
1159
  int32_t  lino = 0;
27,236,434✔
1160
  int32_t  code = 0;
27,236,434✔
1161
  uint64_t offset;
1162
  uint32_t len = pItem->len;
27,236,434✔
1163
  uint8_t *data = pItem->data;
27,236,434✔
1164
  int32_t  dataOffset = pItem->seqOffsetInRow;
27,236,434✔
1165

1166
  uint64_t tlen = pBlobSet->len + len;
27,236,434✔
1167
  if (tlen > pBlobSet->cap) {
27,236,434✔
1168
    int64_t cap = pBlobSet->cap;
22,696✔
1169
    // opt later
1170
    do {
1171
      cap = cap * 2;
22,696✔
1172
    } while (tlen > cap);
22,696✔
1173

1174
    uint8_t *data = taosMemRealloc(pBlobSet->data, cap);
22,696✔
1175
    if (data == NULL) {
22,696✔
1176
      return terrno;
×
1177
    }
1178
    pBlobSet->data = data;
22,696✔
1179
    pBlobSet->cap = cap;
22,696✔
1180
  }
1181
  if (len > 0) {
27,236,434✔
1182
    (void)memcpy(pBlobSet->data + pBlobSet->len, data, len);
24,523,678✔
1183
  }
1184

1185
  offset = pBlobSet->len;
27,236,434✔
1186
  pBlobSet->len += len;
27,236,434✔
1187

1188
  pBlobSet->seq++;
27,236,434✔
1189
  *seq = pBlobSet->seq;
27,236,434✔
1190

1191
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow, .type = pItem->type};
27,236,434✔
1192
  if (taosArrayPush(pBlobSet->pSeqTable, &value) == NULL) {
54,472,868✔
1193
    TAOS_CHECK_EXIT(terrno);
×
1194
  }
1195

1196
  int32_t sz = taosArrayGetSize(pBlobSet->pSeqTable);
27,236,434✔
1197
  code = taosHashPut(pBlobSet->pSeqToffset, seq, sizeof(int64_t), &sz, sizeof(int32_t));
27,236,434✔
1198
  if (code != 0) {
27,236,434✔
1199
    TAOS_CHECK_EXIT(code);
×
1200
  }
1201

1202
_exit:
27,236,434✔
1203
  return code;
27,236,434✔
1204
}
1205

1206
void tBlobSetSwap(SBlobSet *p1, SBlobSet *p2) {
815✔
1207
  SBlobSet t = {0};
815✔
1208

1209
  memcpy(&t, p1, sizeof(SBlobSet));
815✔
1210

1211
  memcpy(p1, p2, sizeof(SBlobSet));
815✔
1212
  memcpy(p2, &t, sizeof(SBlobSet));
815✔
1213
}
815✔
1214

1215
int32_t tBlobSetUpdate(SBlobSet *pBlobSet, uint64_t seq, SBlobItem *pItem) {
×
1216
  int32_t code = 0;
×
1217
  return code;
×
1218
  if (pBlobSet == NULL || pItem == NULL) {
1219
    return TSDB_CODE_INVALID_PARA;
1220
  }
1221
  if (pBlobSet->pSeqToffset == NULL || pBlobSet->pSeqTable == NULL) {
1222
    return TSDB_CODE_INVALID_PARA;
1223
  }
1224

1225
  return code;
1226
}
1227
int32_t tBlobSetGet(SBlobSet *pBlobSet, uint64_t seq, SBlobItem *pItem) {
×
1228
  if (pBlobSet == NULL || pItem == NULL) {
×
1229
    return TSDB_CODE_INVALID_PARA;
×
1230
  }
1231
  if (pBlobSet->pSeqToffset == NULL || pBlobSet->pSeqTable == NULL) {
×
1232
    return TSDB_CODE_INVALID_PARA;
×
1233
  }
1234

1235
  int32_t code = 0;
×
1236
  int32_t len = 0;
×
1237
  int32_t dataOffset = 0;
×
1238
  int8_t  nextRow = 0;
×
1239

1240
  int32_t *offset = (int32_t *)taosHashGet(pBlobSet->pSeqToffset, &seq, sizeof(int64_t));
×
1241
  if (offset == NULL) {
×
1242
    return TSDB_CODE_INVALID_PARA;
×
1243
  }
1244

1245
  SBlobValue *value = taosArrayGet(pBlobSet->pSeqTable, *offset - 1);
×
1246
  if (value == NULL) {
×
1247
    return TSDB_CODE_INVALID_PARA;
×
1248
  }
1249

1250
  len = value->len;
×
1251
  dataOffset = value->dataOffset;
×
1252
  nextRow = value->nextRow;
×
1253

1254
  pItem->seqOffsetInRow = value->dataOffset;
×
1255
  pItem->len = len;
×
1256
  pItem->data = pBlobSet->data + value->offset;
×
1257
  pItem->type = value->type;
×
1258

1259
  return code;
×
1260
}
1261

1262
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
1,448,541,866✔
1263
  if (pBlobSet == NULL) return 0;
1,448,541,866✔
1264
  return taosArrayGetSize(pBlobSet->pSeqTable);
139,349✔
1265
}
1266

1267
void tBlobSetDestroy(SBlobSet *pBlobSet) {
15,057,811✔
1268
  if (pBlobSet == NULL) return;
15,057,811✔
1269
  uTrace("destroy blob row, seqTable size %p", pBlobSet);
94,129✔
1270
  taosMemoryFree(pBlobSet->data);
94,129✔
1271
  taosArrayDestroy(pBlobSet->pSeqTable);
99,132✔
1272
  taosHashCleanup(pBlobSet->pSeqToffset);
99,132✔
1273
  taosArrayDestroy(pBlobSet->pSet);
99,132✔
1274
  taosMemoryFree(pBlobSet);
99,132✔
1275
}
1276
int32_t tBlobSetClear(SBlobSet *pBlobSet) {
×
1277
  if (pBlobSet == NULL) return 0;
×
1278
  int32_t code = 0;
×
1279
  uTrace("clear blob row, seqTable size %p", pBlobSet);
×
1280
  taosArrayClear(pBlobSet->pSeqTable);
×
1281
  taosHashClear(pBlobSet->pSeqToffset);
×
1282
  pBlobSet->len = 0;
×
1283
  pBlobSet->seq = 1;
×
1284
  return code;
×
1285
}
1286

1287
static int32_t tBindInfoCompare(const void *p1, const void *p2, const void *param) {
7,252✔
1288
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
7,252✔
1289
    return -1;
1,372✔
1290
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
5,880✔
1291
    return 1;
5,880✔
1292
  }
1293
  return 0;
×
1294
}
1295

1296
/* build rows to `rowArray` from bind
1297
 * `infos` is the bind information array
1298
 * `numOfInfos` is the number of bind information
1299
 * `infoSorted` is whether the bind information is sorted by column id
1300
 * `pTSchema` is the schema of the table
1301
 * `rowArray` is the array to store the rows
1302
 * `pOrdered` is the pointer to store ordered
1303
 * `pDupTs` is the pointer to store duplicateTs
1304
 */
1305
int32_t tRowBuildFromBind(SBindInfo *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
1,977,049✔
1306
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
1307
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
1,977,049✔
1308
    return TSDB_CODE_INVALID_PARA;
×
1309
  }
1310

1311
  if (!infoSorted) {
1,984,866✔
1312
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1313
  }
1314

1315
  int32_t code = 0;
1,984,866✔
1316
  int32_t numOfRows = infos[0].bind->num;
1,984,866✔
1317
  SArray *colValArray;
1318
  SColVal colVal;
1,956,903✔
1319

1320
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,986,747✔
1321
    return terrno;
×
1322
  }
1323

1324
  SRowKey rowKey, lastRowKey;
1,956,591✔
1325
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
20,033,399✔
1326
    taosArrayClear(colValArray);
18,377,163✔
1327

1328
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
95,797,306✔
1329
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
77,248,741✔
1330
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
1,312✔
1331
      } else {
1332
        SValue value = {
77,258,934✔
1333
            .type = infos[iInfo].type,
77,259,204✔
1334
        };
1335
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
77,262,630✔
1336
          value.nData = infos[iInfo].bind->length[iRow];
4,950,107✔
1337
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
4,967,325✔
1338
            code = TSDB_CODE_INVALID_PARA;
×
1339
            goto _exit;
×
1340
          }
1341
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
4,968,525✔
1342
        } else {
1343
          valueSetDatum(&value, infos[iInfo].type,
72,337,150✔
1344
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
72,333,076✔
1345
                        infos[iInfo].bind->buffer_length);
72,335,863✔
1346
        }
1347
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
78,056,625✔
1348
      }
1349
      if (taosArrayPush(colValArray, &colVal) == NULL) {
77,481,631✔
1350
        code = terrno;
×
1351
        goto _exit;
×
1352
      }
1353
    }
1354

1355
    SRow             *row;
4,034,718✔
1356
    SRowBuildScanInfo sinfo = {0};
18,518,442✔
1357
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
18,518,086✔
1358
      goto _exit;
×
1359
    }
1360

1361
    if ((taosArrayPush(rowArray, &row)) == NULL) {
18,036,151✔
1362
      code = terrno;
×
1363
      goto _exit;
×
1364
    }
1365

1366
    if (pOrdered && pDupTs) {
18,036,151✔
1367
      tRowGetKey(row, &rowKey);
36,129,798✔
1368
      if (iRow == 0) {
18,075,123✔
1369
        *pOrdered = true;
1,986,643✔
1370
        *pDupTs = false;
1,987,045✔
1371
      } else {
1372
        if (*pOrdered) {
16,088,480✔
1373
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
16,134,896✔
1374
          *pOrdered = (res >= 0);
16,134,896✔
1375
          if (!*pDupTs) {
16,134,992✔
1376
            *pDupTs = (res == 0);
16,077,729✔
1377
          }
1378
        }
1379
      }
1380
      lastRowKey = rowKey;
18,074,877✔
1381
    }
1382
  }
1383

1384
_exit:
1,965,016✔
1385
  taosArrayDestroy(colValArray);
1,964,781✔
1386
  return code;
1,985,376✔
1387
}
1388

1389
int32_t tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
2,147,483,647✔
1390
  if (!(iCol < pTSchema->numOfCols)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
1391
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
1392

1393
  STColumn *pTColumn = pTSchema->columns + iCol;
2,147,483,647✔
1394

1395
  if (iCol == 0) {
2,147,483,647✔
1396
    pColVal->cid = pTColumn->colId;
2,147,483,647✔
1397
    pColVal->value.type = pTColumn->type;
2,147,483,647✔
1398
    pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
1399
    VALUE_SET_TRIVIAL_DATUM(&pColVal->value, pRow->ts);
2,147,483,647✔
1400
    return 0;
2,147,483,647✔
1401
  }
1402

1403
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1404
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
20,003,002✔
1405
    return 0;
20,003,002✔
1406
  }
1407

1408
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1409
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
577,689,307✔
1410
    return 0;
577,708,884✔
1411
  }
1412

1413
  SPrimaryKeyIndex index;
2,147,483,647✔
1414
  uint8_t         *data = pRow->data;
2,147,483,647✔
1415
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
1416
    data += tGetPrimaryKeyIndex(data, &index);
2,147,483,647✔
1417
  }
1418

1419
  if (pRow->flag >> 4) {  // KV Row
2,147,483,647✔
1420
    SKVIdx  *pIdx = (SKVIdx *)data;
2,147,483,647✔
1421
    uint8_t *pv = NULL;
2,147,483,647✔
1422

1423
    if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
1424
      pv = pIdx->idx + pIdx->nCol;
2,147,483,647✔
1425
    } else if (pRow->flag & KV_FLG_MID) {
2,147,483,647✔
1426
      pv = pIdx->idx + (pIdx->nCol << 1);
2,147,483,647✔
1427
    } else {
1428
      pv = pIdx->idx + (pIdx->nCol << 2);
×
1429
    }
1430

1431
    int16_t lidx = 0;
2,147,483,647✔
1432
    int16_t ridx = pIdx->nCol - 1;
2,147,483,647✔
1433
    while (lidx <= ridx) {
2,147,483,647✔
1434
      int16_t  mid = (lidx + ridx) >> 1;
2,147,483,647✔
1435
      uint8_t *pData = NULL;
2,147,483,647✔
1436
      if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
1437
        pData = pv + ((uint8_t *)pIdx->idx)[mid];
2,147,483,647✔
1438
      } else if (pRow->flag & KV_FLG_MID) {
2,147,483,647✔
1439
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
2,147,483,647✔
1440
      } else {
1441
        pData = pv + ((uint32_t *)pIdx->idx)[mid];
×
1442
      }
1443

1444
      int16_t cid;
2,147,483,647✔
1445
      pData += tGetI16v(pData, &cid);
2,147,483,647✔
1446

1447
      if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
1448
        if (cid < 0) {
2,147,483,647✔
1449
          *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
2,147,483,647✔
1450
        } else {
1451
          pColVal->cid = pTColumn->colId;
2,147,483,647✔
1452
          pColVal->value.type = pTColumn->type;
2,147,483,647✔
1453
          pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
1454

1455
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
1456
            int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
2,147,483,647✔
1457

1458
            pData += tGetU32v(pData, &pColVal->value.nData);
2,147,483,647✔
1459
            if (pColVal->value.nData > 0) {
2,147,483,647✔
1460
              pColVal->value.pData = pData;
2,147,483,647✔
1461
            } else {
1462
              pColVal->value.pData = NULL;
11,185,862✔
1463
            }
1464
            if (isBlob == 1) {
2,147,483,647✔
1465
              pData += BSE_SEQUECE_SIZE;  // skip seq
33,241,495✔
1466
            }
1467
          } else {
1468
            valueSetDatum(&pColVal->value, pTColumn->type, pData, pTColumn->bytes);
2,147,483,647✔
1469
          }
1470
        }
1471
        return 0;
2,147,483,647✔
1472
      } else if (TABS(cid) < pTColumn->colId) {
2,147,483,647✔
1473
        lidx = mid + 1;
2,147,483,647✔
1474
      } else {
1475
        ridx = mid - 1;
2,147,483,647✔
1476
      }
1477
    }
1478

1479
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
2,147,483,647✔
1480
  } else {  // Tuple Row
1481
    uint8_t *bitmap = data;
2,147,483,647✔
1482
    uint8_t *fixed;
1483
    uint8_t *varlen;
1484
    uint8_t  bit;
1485

1486
    if (pRow->flag == HAS_VALUE) {
2,147,483,647✔
1487
      fixed = bitmap;
2,147,483,647✔
1488
      bit = BIT_FLG_VALUE;
2,147,483,647✔
1489
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
2,147,483,647✔
1490
      fixed = BIT2_SIZE(pTSchema->numOfCols - 1) + bitmap;
×
1491
      bit = GET_BIT2(bitmap, iCol - 1);
×
1492
    } else {
1493
      fixed = BIT1_SIZE(pTSchema->numOfCols - 1) + bitmap;
2,147,483,647✔
1494
      bit = GET_BIT1(bitmap, iCol - 1);
2,147,483,647✔
1495

1496
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
2,147,483,647✔
1497
        if (bit) bit++;
10,893,732✔
1498
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
2,147,483,647✔
1499
        bit++;
2,147,483,647✔
1500
      }
1501
    }
1502
    varlen = fixed + pTSchema->flen;
2,147,483,647✔
1503

1504
    if (bit == BIT_FLG_NONE) {
2,147,483,647✔
1505
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,752,677✔
1506
      return 0;
1,752,677✔
1507
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1508
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,943,781,256✔
1509
      return 0;
1,943,782,979✔
1510
    }
1511

1512
    pColVal->cid = pTColumn->colId;
2,147,483,647✔
1513
    pColVal->value.type = pTColumn->type;
2,147,483,647✔
1514
    pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
1515
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
1516
      int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
2,147,483,647✔
1517
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
2,147,483,647✔
1518
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
2,147,483,647✔
1519
      // TODO(yhDeng): support tuple
1520
      //  if (isBlob) pColVal->value.pData += sizeof(uint64_t);  // skip seq
1521
      //  }
1522
    } else {
1523
      valueSetDatum(&pColVal->value, pTColumn->type, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
2,147,483,647✔
1524
    }
1525
  }
1526

1527
  return 0;
2,147,483,647✔
1528
}
1529

1530
void tRowDestroy(SRow *pRow) {
2,147,483,647✔
1531
  if (pRow) taosMemoryFree(pRow);
2,147,483,647✔
1532
}
2,147,483,647✔
1533

1534
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
302,118,551✔
1535
  SRowKey key1, key2;
302,090,724✔
1536
  tRowGetKey(*(SRow **)p1, &key1);
605,406,526✔
1537
  tRowGetKey(*(SRow **)p2, &key2);
606,539,088✔
1538
  return tRowKeyCompare(&key1, &key2);
303,426,605✔
1539
}
1540
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
37,111,587✔
1541

1542
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
91,613,244✔
1543
  SColVal* pColVal = tRowIterNext(iter);
91,613,244✔
1544
  while (pColVal != NULL && pColVal->cid < targetCid) {
119,150,995✔
1545
    pColVal = tRowIterNext(iter);
27,536,301✔
1546
  }
1547
  return pColVal;
91,614,738✔
1548
}
1549

1550
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, ERowMergeStrategy strategy) {
5,501,489✔
1551
  int32_t code = 0;
5,501,489✔
1552

1553
  int32_t    nRow = iEnd - iStart;
5,501,489✔
1554
  SRowIter **aIter = NULL;
5,501,489✔
1555
  SArray    *aColVal = NULL;
5,501,489✔
1556
  SRow      *pRow = NULL;
5,501,489✔
1557
  uint8_t    hasBlob = 0;
5,501,523✔
1558
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
5,501,523✔
1559
  if (aIter == NULL) {
5,501,453✔
1560
    code = terrno;
×
1561
    goto _exit;
×
1562
  }
1563

1564
  for (int32_t i = 0; i < nRow; i++) {
42,614,120✔
1565
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
37,112,477✔
1566

1567
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
37,112,197✔
1568
    if (code) goto _exit;
37,112,667✔
1569
  }
1570

1571
  // merge
1572
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
5,501,643✔
1573
  if (aColVal == NULL) {
5,501,259✔
1574
    code = terrno;
×
1575
    goto _exit;
×
1576
  }
1577

1578
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
74,911,745✔
1579
    int32_t targetCid = pTSchema->columns[iCol].colId;
69,408,844✔
1580
    SColVal *pColVal = NULL;
69,408,710✔
1581

1582
    switch (strategy) {
69,408,710✔
1583
      case KEEP_CONSISTENCY:
4,672,498✔
1584
        if (nRow > 0)
4,672,498✔
1585
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
4,672,498✔
1586
        break;
4,672,498✔
1587

1588
      default:  // default using PREFER_NON_NULL strategy
64,736,212✔
1589
      case PREFER_NON_NULL:
1590
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
106,033,906✔
1591
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
86,940,916✔
1592

1593
          if (COL_VAL_IS_VALUE(pColValT)) {
86,942,104✔
1594
            pColVal = pColValT;
45,644,466✔
1595
            break;
45,644,466✔
1596
          } else if (pColVal == NULL) {
41,297,694✔
1597
            pColVal = pColValT;
20,922,448✔
1598
          }
1599
        }
1600
        break;
64,737,456✔
1601
    }
1602

1603
    if (pColVal) {
69,409,954✔
1604
      if (taosArrayPush(aColVal, pColVal) == NULL) {
69,409,920✔
1605
        code = terrno;
×
1606
        goto _exit;
×
1607
      }
1608
    }
1609
  }
1610

1611
  // build
1612
  SRowBuildScanInfo sinfo = {0};
5,500,423✔
1613
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
5,501,485✔
1614

1615
  if (code) goto _exit;
5,501,163✔
1616

1617
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
5,501,163✔
1618
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
5,501,255✔
1619
    code = terrno;
×
1620
    goto _exit;
×
1621
  }
1622

1623
_exit:
5,501,471✔
1624
  if (aIter) {
5,501,459✔
1625
    for (int32_t i = 0; i < nRow; i++) {
42,614,106✔
1626
      tRowIterClose(&aIter[i]);
37,112,591✔
1627
    }
1628
    taosMemoryFree(aIter);
5,501,515✔
1629
  }
1630
  if (aColVal) taosArrayDestroy(aColVal);
5,501,465✔
1631
  if (code) tRowDestroy(pRow);
5,501,433✔
1632
  return code;
5,501,433✔
1633
}
1634
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
8,150✔
1635
  int32_t code = 0;
8,150✔
1636
  int32_t lino = 0;
8,150✔
1637
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
16,300✔
1638
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
8,150✔
1639
    code = addEmptyItemToBlobSet(pDst, type, NULL);
8,150✔
1640
    TAOS_CHECK_GOTO(code, &lino, _error);
8,150✔
1641
  } else {
1642
    uint64_t seq = 0;
×
1643
    if (tGetU64(pVal->value.pData, &seq) < 0) {
×
1644
      uError("tBlobSetTransferTo: invalid blob value, seq %p", pVal->value.pData);
×
1645
      return TSDB_CODE_INVALID_PARA;
×
1646
    }
1647

1648
    SBlobItem item = {0};
×
1649
    code = tBlobSetGet(pSrc, seq, &item);
×
1650
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1651

1652
    code = tBlobSetPush(pDst, &item, &seq, 1);
×
1653
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1654
    if (tPutU64(pVal->value.pData, seq) < 0) {
×
1655
      uError("tBlobSetTransferTo: put seq to colVal failed");
×
1656
      return TSDB_CODE_INVALID_PARA;
×
1657
    }
1658
  }
1659

1660
_error:
8,150✔
1661
  return code;
8,150✔
1662
}
1663

1664
static int32_t tRowRebuildBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob) {
815✔
1665
  int32_t code = 0;
815✔
1666
  int32_t   nRow = TARRAY_SIZE(aRowP);
815✔
1667
  int32_t   lino = 0;
815✔
1668
  SBlobSet *pTempBlob = NULL;
815✔
1669

1670
  SArray *aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
815✔
1671
  if (aColVal == NULL) {
815✔
1672
    TAOS_CHECK_RETURN(code = terrno);
×
1673
  }
1674

1675
  SRowIter **aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
815✔
1676
  if (aIter == NULL) {
815✔
1677
    TAOS_CHECK_GOTO(terrno, &lino, _error);
×
1678
  }
1679

1680
  for (int32_t i = 0; i < nRow; i++) {
8,965✔
1681
    SRow *pRowT = taosArrayGetP(aRowP, 0 + i);
8,150✔
1682
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
8,150✔
1683
    TAOS_CHECK_GOTO(code, &lino, _error);
8,150✔
1684
  }
1685

1686
  for (int32_t i = 0; i < nRow; i++) {
8,965✔
1687
    SColVal *pColVal = tRowIterNext(aIter[i]);
8,150✔
1688
    do {
1689
      if (COL_VAL_IS_VALUE(pColVal) || COL_VAL_IS_NULL(pColVal)) {
16,300✔
1690
        if (IS_STR_DATA_BLOB(pColVal->value.type)) {
16,300✔
1691
          if (taosArrayPush(aColVal, pColVal) == NULL) {
8,150✔
1692
            code = terrno;
×
1693
            TAOS_CHECK_GOTO(code, &lino, _error);
×
1694
          }
1695
          break;
8,150✔
1696
        }
1697
      }
1698
    } while ((pColVal = tRowIterNext(aIter[i])) != NULL);
8,150✔
1699
  }
1700

1701
  code = tBlobSetCreate(pBlob->cap, pBlob->type, &pTempBlob);
815✔
1702
  TAOS_CHECK_GOTO(code, &lino, _error);
815✔
1703

1704
  for (int32_t i = 0; i < taosArrayGetSize(aColVal); i++) {
8,965✔
1705
    uint64_t seq = 0;
8,150✔
1706
    SColVal *pVal = taosArrayGet(aColVal, i);
8,150✔
1707

1708
    code = tBlobSetTransferTo(pBlob, pTempBlob, pVal);
8,150✔
1709
    TAOS_CHECK_GOTO(code, &lino, _error);
8,150✔
1710
  }
1711

1712
  tBlobSetSwap(pBlob, pTempBlob);
815✔
1713

1714
_error:
815✔
1715
  if (code != 0) {
815✔
1716
    uError("tRowRebuildBlob failed at line %d, code %d", code, lino);
×
1717
  }
1718

1719
  if (aIter) {
815✔
1720
    for (int32_t i = 0; i < nRow; i++) {
8,965✔
1721
      tRowIterClose(&aIter[i]);
8,150✔
1722
    }
1723
    taosMemoryFree(aIter);
815✔
1724
  }
1725
  if (aColVal) {
815✔
1726
    taosArrayDestroy(aColVal);
815✔
1727
  }
1728
  tBlobSetDestroy(pTempBlob);
815✔
1729
  return code;
815✔
1730
}
1731

1732
static int32_t tRowMergeAndRebuildBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob) {
×
1733
  int32_t code = 0;
×
1734
  int32_t lino = 0;
×
1735

1736
  SBlobSet *pTempBlobSet = NULL;
×
1737
  int32_t   size = taosArrayGetSize(aRowP);
×
1738
  if (size <= 1) {
×
1739
    return code;
×
1740
  }
1741
  int32_t colBlobIdx = 0;
×
1742
  for (int32_t i = 0; i < pTSchema->numOfCols; i++) {
×
1743
    if (IS_STR_DATA_BLOB(pTSchema->columns[i].type)) {
×
1744
      colBlobIdx = i;
×
1745
      break;
×
1746
    }
1747
  }
1748

1749
  code = tBlobSetCreate(pBlob->cap, pBlob->type, &pTempBlobSet);
×
1750
  TAOS_CHECK_GOTO(code, &lino, _error);
×
1751

1752
  int32_t iStart = 0;
×
1753
  while (iStart < aRowP->size) {
×
1754
    SRowKey key1;
×
1755
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
×
1756

1757
    tRowGetKey(row1, &key1);
×
1758

1759
    int32_t iEnd = iStart + 1;
×
1760
    while (iEnd < aRowP->size) {
×
1761
      SRowKey key2;
×
1762
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
×
1763
      tRowGetKey(row2, &key2);
×
1764

1765
      if (tRowKeyCompare(&key1, &key2) != 0) break;
×
1766

1767
      iEnd++;
×
1768
    }
1769

1770
    if (iEnd - iStart > 1) {
×
1771
      code = tRowMergeWithBlobImpl(aRowP, pTSchema, pBlob, pTempBlobSet, iStart, iEnd, 0);
×
1772
      TAOS_CHECK_GOTO(code, &lino, _error);
×
1773
    } else {
1774
      SColVal colVal = {0};
×
1775
      code = tRowGet(row1, pTSchema, colBlobIdx, &colVal);
×
1776
      TAOS_CHECK_GOTO(code, &lino, _error);
×
1777

1778
      code = tBlobSetTransferTo(pBlob, pTempBlobSet, &colVal);
×
1779
      TAOS_CHECK_GOTO(code, &lino, _error);
×
1780
    }
1781
    // the array is also changing, so the iStart just ++ instead of iEnd
1782
    iStart++;
×
1783
  }
1784

1785
_error:
×
1786
  if (pBlob && pTempBlobSet) {
×
1787
    tBlobSetSwap(pBlob, pTempBlobSet);
×
1788
  }
1789

1790
  tBlobSetDestroy(pTempBlobSet);
×
1791
  return code;
×
1792
}
1793

1794
static int32_t tRowMergeWithBlobImpl(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob, SBlobSet *pDstBlob,
×
1795
                                     int32_t iStart, int32_t iEnd, int8_t flag) {
1796
  int32_t code = 0;
×
1797
  int32_t    lino = 0;
×
1798
  int32_t    nRow = iEnd - iStart;
×
1799
  SRowIter **aIter = NULL;
×
1800
  SArray    *aColVal = NULL;
×
1801
  SRow      *pRow = NULL;
×
1802

1803
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
×
1804
  if (aColVal == NULL) {
×
1805
    code = terrno;
×
1806
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1807
  }
1808

1809
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
×
1810
  if (aIter == NULL) {
×
1811
    code = terrno;
×
1812
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1813
  }
1814

1815
  for (int32_t i = 0; i < nRow; i++) {
×
1816
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
×
1817
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
×
1818
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1819
  }
1820
  // merge
1821

1822
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
×
1823
    SColVal  *pColVal = NULL;
×
1824
    STColumn *pCol = pTSchema->columns + iCol;
×
1825

1826
    for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
×
1827
      SColVal *pColValT = tRowIterNext(aIter[iRow]);
×
1828
      while (pColValT->cid < pTSchema->columns[iCol].colId) {
×
1829
        pColValT = tRowIterNext(aIter[iRow]);
×
1830
      }
1831
      // todo: take strategy according to the flag
1832
      if (COL_VAL_IS_VALUE(pColValT)) {
×
1833
        pColVal = pColValT;
×
1834
        break;
×
1835
      } else if (COL_VAL_IS_NULL(pColValT)) {
×
1836
        if (pColVal == NULL) {
×
1837
          pColVal = pColValT;
×
1838
        }
1839
      }
1840
    }
1841

1842
    if (pColVal) {
×
1843
      if (taosArrayPush(aColVal, pColVal) == NULL) {
×
1844
        code = terrno;
×
1845
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
1846
      }
1847
    }
1848
  }
1849

1850
  // build
1851

1852
  SRowBuildScanInfo sinfo = {.hasBlob = 1};
×
1853
  code = tRowBuildWithBlob2(aColVal, pTSchema, &pRow, pBlob, pDstBlob, &sinfo);
×
1854
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1855

1856
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
×
1857
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
×
1858
    code = terrno;
×
1859
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1860
  }
1861

1862
_exit:
×
1863
  if (aIter) {
×
1864
    for (int32_t i = 0; i < nRow; i++) {
×
1865
      tRowIterClose(&aIter[i]);
×
1866
    }
1867
    taosMemoryFree(aIter);
×
1868
  }
1869
  if (aColVal) taosArrayDestroy(aColVal);
×
1870
  if (code) tRowDestroy(pRow);
×
1871

1872
  return code;
×
1873
}
1874

1875
int32_t tRowSort(SArray *aRowP) {
1,648,591✔
1876
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
1,648,591✔
1877
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
1,642,287✔
1878
  if (code != TSDB_CODE_SUCCESS) {
1,642,287✔
1879
    uError("taosArrayMSort failed caused by %d", code);
×
1880
  }
1881
  return code;
1,642,287✔
1882
}
1883

1884
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, ERowMergeStrategy strategy) {
1,797,269✔
1885
  int32_t code = 0;
1,797,269✔
1886

1887
  int32_t iStart = 0;
1,797,269✔
1888
  while (iStart < aRowP->size) {
23,963,539✔
1889
    SRowKey key1;
22,148,122✔
1890
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
22,166,160✔
1891

1892
    tRowGetKey(row1, &key1);
44,318,088✔
1893

1894
    int32_t iEnd = iStart + 1;
22,161,582✔
1895
    while (iEnd < aRowP->size) {
53,770,334✔
1896
      SRowKey key2;
51,956,649✔
1897
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
51,972,915✔
1898
      tRowGetKey(row2, &key2);
103,941,072✔
1899

1900
      if (tRowKeyCompare(&key1, &key2) != 0) break;
51,980,567✔
1901

1902
      iEnd++;
31,612,652✔
1903
    }
1904

1905
    if (iEnd - iStart > 1) {
22,165,232✔
1906
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
5,501,353✔
1907
      if (code) return code;
5,501,421✔
1908
    }
1909

1910
    // the array is also changing, so the iStart just ++ instead of iEnd
1911
    iStart++;
22,165,300✔
1912
  }
1913

1914
  return code;
1,797,297✔
1915
}
1916

1917
int32_t tRowSortWithBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlobSet) {
815✔
1918
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
815✔
1919
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
815✔
1920
  if (code != TSDB_CODE_SUCCESS) {
815✔
1921
    uError("taosArrayMSort failed caused by %d", code);
×
1922
    return code;
×
1923
  }
1924
  return code;
815✔
1925
}
1926

1927
int8_t tRowNeedDoMerge(SArray *aRowP) {
815✔
1928
  if (aRowP == NULL || aRowP->size <= 1) return 0;
815✔
1929

1930
  int8_t  doMerge = 0;
815✔
1931
  int32_t iStart = 0;
815✔
1932
  while (iStart < aRowP->size) {
8,965✔
1933
    SRowKey key1;
8,150✔
1934
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
8,150✔
1935

1936
    tRowGetKey(row1, &key1);
16,300✔
1937

1938
    int32_t iEnd = iStart + 1;
8,150✔
1939
    while (iEnd < aRowP->size) {
8,150✔
1940
      SRowKey key2;
7,335✔
1941
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
7,335✔
1942
      tRowGetKey(row2, &key2);
14,670✔
1943

1944
      if (tRowKeyCompare(&key1, &key2) != 0) break;
7,335✔
1945

1946
      iEnd++;
×
1947
    }
1948

1949
    if (iEnd - iStart > 1) {
8,150✔
1950
      doMerge = 1;
×
1951
      break;
×
1952
    }
1953
    iStart++;
8,150✔
1954
  }
1955
  return doMerge;
815✔
1956
}
1957

1958
int32_t tRowMergeWithBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlobSet, int8_t flag) {
815✔
1959
  int32_t code = 0;
815✔
1960

1961
  int8_t doMerge = tRowNeedDoMerge(aRowP);
815✔
1962
  if (!doMerge) {
815✔
1963
    code = tRowRebuildBlob(aRowP, pTSchema, pBlobSet);
815✔
1964
  } else {
1965
    code = tRowRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1966
    TAOS_CHECK_RETURN(code);
×
1967

1968
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1969
  }
1970
  return code;
815✔
1971
}
1972

1973
// SRowIter ========================================
1974
struct SRowIter {
1975
  SRow     *pRow;
1976
  STSchema *pTSchema;
1977

1978
  int32_t iTColumn;
1979
  union {
1980
    struct {  // kv
1981
      int32_t iCol;
1982
      SKVIdx *pIdx;
1983
    };
1984
    struct {  // tuple
1985
      uint8_t *pb;
1986
      uint8_t *pf;
1987
    };
1988
  };
1989
  uint8_t *pv;
1990
  SColVal  cv;
1991
};
1992

1993
int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
42,825,908✔
1994
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
42,825,908✔
1995

1996
  int32_t code = 0;
42,826,184✔
1997

1998
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
42,826,184✔
1999
  if (pIter == NULL) {
42,825,916✔
2000
    code = terrno;
×
2001
    goto _exit;
×
2002
  }
2003

2004
  pIter->pRow = pRow;
42,825,916✔
2005
  pIter->pTSchema = pTSchema;
42,825,074✔
2006
  pIter->iTColumn = 0;
42,825,920✔
2007

2008
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
42,825,138✔
2009

2010
  uint8_t         *data = pRow->data;
42,438,501✔
2011
  SPrimaryKeyIndex index;
42,428,421✔
2012
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
45,296,479✔
2013
    data += tGetPrimaryKeyIndex(data, &index);
2,857,930✔
2014
  }
2015

2016
  if (pRow->flag >> 4) {
42,440,069✔
2017
    pIter->iCol = 0;
7,686,984✔
2018
    pIter->pIdx = (SKVIdx *)data;
7,686,984✔
2019
    if (pRow->flag & KV_FLG_LIT) {
7,686,984✔
2020
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
7,686,984✔
2021
    } else if (pRow->flag & KV_FLG_MID) {
×
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
×
2023
    } else {
2024
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
2025
    }
2026
  } else {
2027
    switch (pRow->flag) {
34,753,099✔
2028
      case (HAS_NULL | HAS_NONE):
3,005✔
2029
        pIter->pb = data;
3,005✔
2030
        break;
3,005✔
2031
      case HAS_VALUE:
31,777,525✔
2032
        pIter->pf = data;
31,777,525✔
2033
        pIter->pv = pIter->pf + pTSchema->flen;
31,777,561✔
2034
        break;
31,778,559✔
2035
      case (HAS_VALUE | HAS_NONE):
2,971,813✔
2036
      case (HAS_VALUE | HAS_NULL):
2037
        pIter->pb = data;
2,971,813✔
2038
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
2,971,813✔
2039
        pIter->pv = pIter->pf + pTSchema->flen;
2,971,813✔
2040
        break;
2,971,813✔
2041
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2042
        pIter->pb = data;
×
2043
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2044
        pIter->pv = pIter->pf + pTSchema->flen;
×
2045
        break;
×
2046
      default:
×
2047
        break;
×
2048
    }
2049
  }
2050

2051
_exit:
42,827,096✔
2052
  if (code) {
42,826,188✔
2053
    *ppIter = NULL;
×
2054
  } else {
2055
    *ppIter = pIter;
42,826,188✔
2056
  }
2057
  return code;
42,826,156✔
2058
}
2059

2060
void tRowIterClose(SRowIter **ppIter) {
42,825,478✔
2061
  SRowIter *pIter = *ppIter;
42,825,478✔
2062
  if (pIter) {
42,825,494✔
2063
    taosMemoryFree(pIter);
42,825,500✔
2064
  }
2065
  *ppIter = NULL;
42,826,270✔
2066
}
42,826,292✔
2067

2068
SColVal *tRowIterNext(SRowIter *pIter) {
217,577,506✔
2069
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
217,577,506✔
2070
    return NULL;
4,754,575✔
2071
  }
2072

2073
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
212,842,651✔
2074

2075
  // timestamp
2076
  if (0 == pIter->iTColumn) {
212,868,917✔
2077
    pIter->cv.cid = pTColumn->colId;
14,944,403✔
2078
    pIter->cv.value.type = pTColumn->type;
14,945,121✔
2079
    pIter->cv.flag = CV_FLAG_VALUE;
14,944,367✔
2080
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
14,944,203✔
2081
    goto _exit;
14,945,153✔
2082
  }
2083

2084
  if (pIter->pRow->flag == HAS_NONE) {
197,912,916✔
2085
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
5,648,283✔
2086
    goto _exit;
5,648,283✔
2087
  }
2088

2089
  if (pIter->pRow->flag == HAS_NULL) {
192,265,257✔
2090
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
369,442✔
2091
    goto _exit;
369,442✔
2092
  }
2093

2094
  if (pIter->pRow->flag >> 4) {  // KV
191,898,331✔
2095
    if (pIter->iCol < pIter->pIdx->nCol) {
154,494,258✔
2096
      uint8_t *pData;
2097

2098
      if (pIter->pRow->flag & KV_FLG_LIT) {
69,764,153✔
2099
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
69,766,235✔
2100
      } else if (pIter->pRow->flag & KV_FLG_MID) {
×
2101
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
×
2102
      } else {
2103
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
2104
      }
2105

2106
      int16_t cid;
69,764,847✔
2107
      pData += tGetI16v(pData, &cid);
69,761,377✔
2108

2109
      if (TABS(cid) == pTColumn->colId) {
69,761,377✔
2110
        if (cid < 0) {
68,382,693✔
2111
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,619,760✔
2112
        } else {
2113
          pIter->cv.cid = pTColumn->colId;
64,762,933✔
2114
          pIter->cv.value.type = pTColumn->type;
64,761,545✔
2115
          pIter->cv.flag = CV_FLAG_VALUE;
64,761,545✔
2116

2117
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
64,760,851✔
2118
            pData += tGetU32v(pData, &pIter->cv.value.nData);
25,106,664✔
2119
            if (pIter->cv.value.nData > 0) {
12,555,414✔
2120
              pIter->cv.value.pData = pData;
12,553,959✔
2121
            } else {
2122
              pIter->cv.value.pData = NULL;
1,455✔
2123
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2124
              //   pIter->cv.value.pData = pData;
2125
              // }
2126
            }
2127
          } else {
2128
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
52,212,377✔
2129
          }
2130
        }
2131

2132
        pIter->iCol++;
68,392,409✔
2133
        goto _exit;
68,387,551✔
2134
      } else if (TABS(cid) > pTColumn->colId) {
1,378,684✔
2135
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,378,684✔
2136
        goto _exit;
1,378,684✔
2137
      } else {
2138
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2139
        goto _exit;
×
2140
      }
2141
    } else {
2142
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
84,744,679✔
2143
      goto _exit;
84,742,597✔
2144
    }
2145
  } else {  // Tuple
2146
    uint8_t bv = BIT_FLG_VALUE;
37,386,891✔
2147
    if (pIter->pb) {
37,386,891✔
2148
      switch (pIter->pRow->flag) {
15,727,763✔
2149
        case (HAS_NULL | HAS_NONE):
27,217✔
2150
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
27,217✔
2151
          break;
27,217✔
2152
        case (HAS_VALUE | HAS_NONE):
9,569,690✔
2153
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
9,569,690✔
2154
          if (bv) bv++;
9,569,690✔
2155
          break;
9,569,690✔
2156
        case (HAS_VALUE | HAS_NULL):
6,130,856✔
2157
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
6,130,856✔
2158
          break;
6,130,856✔
2159
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2160
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2161
          break;
×
2162
        default:
×
2163
          break;
×
2164
      }
2165

2166
      if (bv == BIT_FLG_NONE) {
15,727,763✔
2167
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
984,227✔
2168
        goto _exit;
984,227✔
2169
      } else if (bv == BIT_FLG_NULL) {
14,743,536✔
2170
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,083,812✔
2171
        goto _exit;
1,083,812✔
2172
      }
2173
    }
2174

2175
    pIter->cv.cid = pTColumn->colId;
35,319,278✔
2176
    pIter->cv.value.type = pTColumn->type;
35,319,630✔
2177
    pIter->cv.flag = CV_FLAG_VALUE;
35,318,782✔
2178
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
39,580,921✔
2179
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
4,262,304✔
2180
      pData += tGetU32v(pData, &pIter->cv.value.nData);
8,524,242✔
2181
      if (pIter->cv.value.nData > 0) {
4,262,443✔
2182
        pIter->cv.value.pData = pData;
4,259,939✔
2183
      } else {
2184
        pIter->cv.value.pData = NULL;
2,516✔
2185
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2186
        //   pIter->cv.value.pData = pData;
2187
        // }
2188
      }
2189
    } else {
2190
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
31,056,211✔
2191
    }
2192
    goto _exit;
35,317,956✔
2193
  }
2194

2195
_exit:
212,855,623✔
2196
  pIter->iTColumn++;
212,855,623✔
2197
  return &pIter->cv;
212,829,115✔
2198
}
2199

2200
static int32_t tRowNoneUpsertColData(SColData *aColData, int32_t nColData, int32_t flag) {
790,449✔
2201
  int32_t code = 0;
790,449✔
2202

2203
  if (flag) return code;
790,449✔
2204

2205
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
6,313,726✔
2206
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
5,796,715✔
2207
    if (code) return code;
5,796,715✔
2208
  }
2209

2210
  return code;
517,011✔
2211
}
2212
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
62,657,655✔
2213
  int32_t code = 0;
62,657,655✔
2214

2215
  int32_t   iColData = 0;
62,657,655✔
2216
  SColData *pColData = &aColData[iColData];
62,657,655✔
2217
  int32_t   iTColumn = 1;
62,657,655✔
2218
  STColumn *pTColumn = &pSchema->columns[iTColumn];
62,657,655✔
2219

2220
  while (pColData) {
412,640,550✔
2221
    if (pTColumn) {
349,997,909✔
2222
      if (pTColumn->colId == pColData->cid) {  // NULL
349,997,909✔
2223
        if (flag == 0) {
350,189,055✔
2224
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
350,189,055✔
2225
        } else {
2226
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2227
        }
2228
        if (code) goto _exit;
339,678,795✔
2229

2230
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
339,678,795✔
2231
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
339,743,005✔
2232
      } else if (pTColumn->colId > pColData->cid) {  // NONE
×
2233
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2234
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2235
      } else {
2236
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
×
2237
      }
2238
    } else {  // NONE
2239
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2240
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2241
    }
2242
  }
2243

2244
_exit:
62,642,641✔
2245
  return code;
62,642,641✔
2246
}
2247
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
2,147,483,647✔
2248
                                      int32_t flag) {
2249
  int32_t code = 0;
2,147,483,647✔
2250

2251
  int32_t   iColData = 0;
2,147,483,647✔
2252
  SColData *pColData = &aColData[iColData];
2,147,483,647✔
2253
  int32_t   iTColumn = 1;
2,147,483,647✔
2254
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
2,147,483,647✔
2255

2256
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
2,147,483,647✔
2257
  SPrimaryKeyIndex index;
2,147,483,647✔
2258
  uint8_t         *data = pRow->data;
2,147,483,647✔
2259
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
2260
    data += tGetPrimaryKeyIndex(data, &index);
69,053,718✔
2261
  }
2262

2263
  switch (pRow->flag) {
2,147,483,647✔
2264
    case HAS_VALUE:
2,147,483,647✔
2265
      pf = data;  // TODO: fix here
2,147,483,647✔
2266
      pv = pf + pTSchema->flen;
2,147,483,647✔
2267
      break;
2,147,483,647✔
2268
    case (HAS_NULL | HAS_NONE):
185,078✔
2269
      pb = data;
185,078✔
2270
      break;
185,078✔
2271
    case (HAS_VALUE | HAS_NONE):
484,238,850✔
2272
    case (HAS_VALUE | HAS_NULL):
2273
      pb = data;
484,238,850✔
2274
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
484,238,850✔
2275
      pv = pf + pTSchema->flen;
484,236,846✔
2276
      break;
484,238,490✔
2277
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2278
      pb = data;
×
2279
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2280
      pv = pf + pTSchema->flen;
×
2281
      break;
×
2282
    default:
×
2283
      return TSDB_CODE_INVALID_DATA_FMT;
×
2284
  }
2285

2286
  while (pColData) {
2,147,483,647✔
2287
    if (pTColumn) {
2,147,483,647✔
2288
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2289
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647✔
2290
          return TSDB_CODE_INVALID_PARA;
×
2291
        }
2292
        if (pb) {
2,147,483,647✔
2293
          uint8_t bv;
2294
          switch (pRow->flag) {
2,099,434,673✔
2295
            case (HAS_NULL | HAS_NONE):
3,656,490✔
2296
              bv = GET_BIT1(pb, iTColumn - 1);
3,656,490✔
2297
              break;
3,656,490✔
2298
            case (HAS_VALUE | HAS_NONE):
64,638,991✔
2299
              bv = GET_BIT1(pb, iTColumn - 1);
64,638,991✔
2300
              if (bv) bv++;
64,652,431✔
2301
              break;
64,652,431✔
2302
            case (HAS_VALUE | HAS_NULL):
2,031,228,115✔
2303
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
2,031,228,115✔
2304
              break;
2,031,310,680✔
2305
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
34,174✔
2306
              bv = GET_BIT2(pb, iTColumn - 1);
34,174✔
2307
              break;
×
2308
            default:
×
2309
              return TSDB_CODE_INVALID_DATA_FMT;
×
2310
          }
2311

2312
          if (bv == BIT_FLG_NONE) {
2,099,619,601✔
2313
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
8,142,226✔
2314
              goto _exit;
×
2315
            goto _continue;
8,141,666✔
2316
          } else if (bv == BIT_FLG_NULL) {
2,091,477,375✔
2317
            if (flag == 0) {
574,650,784✔
2318
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
574,346,499✔
2319
            } else {
2320
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
304,285✔
2321
            }
2322
            if (code) goto _exit;
574,634,680✔
2323
            goto _continue;
574,634,680✔
2324
          }
2325
        }
2326

2327
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
2328
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
2,147,483,647✔
2329
          uint32_t nData;
2,147,483,647✔
2330
          pData += tGetU32v(pData, &nData);
2,147,483,647✔
2331
          if (flag == 0) {
2,147,483,647✔
2332
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2333
          } else {
2334
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
500,793,407✔
2335
          }
2336
          if (code) goto _exit;
2,147,483,647✔
2337
        } else {
2338
          if (flag == 0) {
2,147,483,647✔
2339
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2340
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
2341
          } else {
2342
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2343
                                                                          TYPE_BYTES[pColData->type], flag > 0);
1,358,519,029✔
2344
          }
2345
          if (code) goto _exit;
2,147,483,647✔
2346
        }
2347

2348
      _continue:
2,147,483,647✔
2349
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2350
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2351
      } else if (pTColumn->colId > pColData->cid) {  // NONE
68,130,200✔
2352
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2353
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2354
      } else {
2355
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
68,112,434✔
2356
      }
2357
    } else {
2358
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
37,562,948✔
2359
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
37,562,948✔
2360
    }
2361
  }
2362

2363
_exit:
2,147,483,647✔
2364
  return code;
2,147,483,647✔
2365
}
2366
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
1,885,103,484✔
2367
  int32_t code = 0;
1,885,103,484✔
2368

2369
  uint8_t  *pv = NULL;
1,885,103,484✔
2370
  int32_t   iColData = 0;
1,885,103,484✔
2371
  SColData *pColData = &aColData[iColData];
1,885,103,484✔
2372
  int32_t   iTColumn = 1;
1,885,182,627✔
2373
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,885,182,627✔
2374
  int32_t   iCol = 0;
1,885,189,240✔
2375

2376
  // primary keys
2377
  uint8_t         *data = pRow->data;
1,885,189,240✔
2378
  SPrimaryKeyIndex index;
1,885,191,652✔
2379
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,890,975,084✔
2380
    data += tGetPrimaryKeyIndex(data, &index);
5,783,177✔
2381
  }
2382

2383
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,885,178,330✔
2384
  if (pRow->flag & KV_FLG_LIT) {
1,885,178,330✔
2385
    pv = pKVIdx->idx + pKVIdx->nCol;
1,873,569,552✔
2386
  } else if (pRow->flag & KV_FLG_MID) {
11,609,374✔
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
11,602,992✔
2388
  } else if (pRow->flag & KV_FLG_BIG) {
×
2389
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2390
  } else {
2391
    return TSDB_CODE_INVALID_PARA;
×
2392
  }
2393

2394
  while (pColData) {
2,147,483,647✔
2395
    if (pTColumn) {
2,147,483,647✔
2396
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2397
        while (iCol < pKVIdx->nCol) {
2,147,483,647✔
2398
          uint8_t *pData;
2399
          if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
2400
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
2,147,483,647✔
2401
          } else if (pRow->flag & KV_FLG_MID) {
315,936,616✔
2402
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
316,053,195✔
2403
          } else if (pRow->flag & KV_FLG_BIG) {
×
2404
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
×
2405
          } else {
2406
            return TSDB_CODE_INVALID_DATA_FMT;
×
2407
          }
2408

2409
          int16_t cid;
2,147,483,647✔
2410
          pData += tGetI16v(pData, &cid);
2,147,483,647✔
2411

2412
          if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
2413
            if (cid < 0) {
2,147,483,647✔
2414
              if (flag == 0) {
2,147,483,647✔
2415
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,147,483,647✔
2416
              } else {
2417
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
25,624✔
2418
              }
2419
              if (code) goto _exit;
2,147,483,647✔
2420
            } else {
2421
              uint32_t nData;
2,147,483,647✔
2422
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
2423
                pData += tGetU32v(pData, &nData);
1,019,347,012✔
2424
              } else {
2425
                nData = 0;
2,147,483,647✔
2426
              }
2427
              if (flag == 0) {
2,147,483,647✔
2428
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2429
              } else {
2430
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
4,579,044✔
2431
              }
2432
              if (code) goto _exit;
2,147,483,647✔
2433
            }
2434
            iCol++;
2,147,483,647✔
2435
            goto _continue;
2,147,483,647✔
2436
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
2,147,483,647✔
2437
            break;
2,147,483,647✔
2438
          } else {
2439
            iCol++;
4,677,000✔
2440
          }
2441
        }
2442

2443
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
2,147,483,647✔
2444

2445
      _continue:
2,147,483,647✔
2446
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2447
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2448
      } else if (pTColumn->colId > pColData->cid) {
4,677,000✔
2449
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2450
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2451
      } else {
2452
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
4,677,000✔
2453
      }
2454
    } else {
2455
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
8,369✔
2456
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
8,369✔
2457
    }
2458
  }
2459

2460
_exit:
1,885,982,086✔
2461
  return code;
1,885,142,936✔
2462
}
2463
/* flag > 0: forward update
2464
 * flag == 0: append
2465
 * flag < 0: backward update
2466
 */
2467
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
2,147,483,647✔
2468
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2469
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2470

2471
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
2472
    return tRowNoneUpsertColData(aColData, nColData, flag);
790,449✔
2473
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2474
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
62,657,163✔
2475
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2476
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,885,148,057✔
2477
  } else {  // TUPLE row
2478
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
2,147,483,647✔
2479
  }
2480
}
2481

2482
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
1,906,799,246✔
2483
  key->numOfPKs = row->numOfPKs;
1,906,799,246✔
2484

2485
  if (key->numOfPKs == 0) {
1,907,295,040✔
2486
    return;
×
2487
  }
2488

2489
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
1,901,355,154✔
2490

2491
  uint8_t *data = row->data;
1,907,563,476✔
2492

2493
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
2494
    data += tGetPrimaryKeyIndex(data, &indices[i]);
1,907,323,366✔
2495
  }
2496

2497
  // primary keys
2498
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
2499
    key->pks[i].type = indices[i].type;
1,907,280,254✔
2500

2501
    uint8_t *tdata = data + indices[i].offset;
1,907,389,200✔
2502
    if (row->flag >> 4) {
1,907,335,255✔
2503
      tdata += tGetI16v(tdata, NULL);
1,275,226,270✔
2504
    }
2505

2506
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
1,907,175,040✔
2507
      key->pks[i].pData = tdata;
490,077,081✔
2508
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
981,125,855✔
2509
    } else {
2510
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
1,417,066,514✔
2511
    }
2512
  }
2513
}
2514

2515
#define T_COMPARE_SCALAR_VALUE(TYPE, V1, V2)    \
2516
  do {                                          \
2517
    if (*(TYPE *)(V1) < *(TYPE *)(V2)) {        \
2518
      return -1;                                \
2519
    } else if (*(TYPE *)(V1) > *(TYPE *)(V2)) { \
2520
      return 1;                                 \
2521
    } else {                                    \
2522
      return 0;                                 \
2523
    }                                           \
2524
  } while (0)
2525

2526
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
1,247,295,914✔
2527
  switch (tv1->type) {
1,247,295,914✔
2528
    case TSDB_DATA_TYPE_BOOL:
×
2529
    case TSDB_DATA_TYPE_TINYINT:
2530
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_SMALLINT:
×
2532
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2533
    case TSDB_DATA_TYPE_INT:
545,413,759✔
2534
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
545,413,759✔
2535
    case TSDB_DATA_TYPE_BIGINT:
149,262,867✔
2536
    case TSDB_DATA_TYPE_TIMESTAMP:
2537
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
149,262,867✔
2538
    case TSDB_DATA_TYPE_FLOAT:
×
2539
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_DOUBLE:
×
2541
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_UTINYINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_USMALLINT:
×
2545
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2546
    case TSDB_DATA_TYPE_UINT:
148,781,902✔
2547
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
148,781,902✔
2548
    case TSDB_DATA_TYPE_UBIGINT:
147,617,202✔
2549
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
147,617,202✔
2550
    case TSDB_DATA_TYPE_GEOMETRY:
256,231,345✔
2551
    case TSDB_DATA_TYPE_BINARY: {
2552
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
256,231,345✔
2553
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
256,187,836✔
2554
    }
2555
    case TSDB_DATA_TYPE_NCHAR: {
×
2556
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2557
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2558
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2559
    }
2560
    case TSDB_DATA_TYPE_VARBINARY: {
×
2561
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2562
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2563
    }
2564
    default:
×
2565
      break;
×
2566
  }
2567

2568
  return 0;
×
2569
}
2570

2571
// NOTE:
2572
// set key->numOfPKs to 0 as the smallest key with ts
2573
// set key->numOfPKs to (TD_MAX_PK_COLS + 1) as the largest key with ts
2574
FORCE_INLINE int32_t tRowKeyCompare(const SRowKey *key1, const SRowKey *key2) {
2,147,483,647✔
2575
  if (key1->ts < key2->ts) {
2,147,483,647✔
2576
    return -1;
2,147,483,647✔
2577
  } else if (key1->ts > key2->ts) {
2,147,483,647✔
2578
    return 1;
2,147,483,647✔
2579
  }
2580

2581
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2582
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2583
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
1,247,452,309✔
2584
      if (ret) return ret;
1,247,256,384✔
2585
    }
2586
  } else if (key1->numOfPKs < key2->numOfPKs) {
5,482✔
2587
    return -1;
5,482✔
2588
  } else {
2589
    return 1;
×
2590
  }
2591

2592
  return 0;
2,147,483,647✔
2593
}
2594

2595
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
2,147,483,647✔
2596
  pDst->ts = pSrc->ts;
2,147,483,647✔
2597
  pDst->numOfPKs = pSrc->numOfPKs;
2,147,483,647✔
2598

2599
  if (pSrc->numOfPKs > 0) {
2,147,483,647✔
2600
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
1,502,033,382✔
2601
      SValue *pVal = &pDst->pks[i];
751,020,819✔
2602
      pVal->type = pSrc->pks[i].type;
751,048,998✔
2603

2604
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
751,132,065✔
2605
    }
2606
  }
2607
}
2,147,483,647✔
2608

2609
// STag ========================================
2610
static int tTagValCmprFn(const void *p1, const void *p2) {
2,147,483,647✔
2611
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
2,147,483,647✔
2612
    return -1;
2,147,483,647✔
2613
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
2,147,483,647✔
2614
    return 1;
2,147,483,647✔
2615
  }
2616

2617
  return 0;
2,147,483,647✔
2618
}
2619
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
4,621,492✔
2620
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
4,621,492✔
2621
}
2622

2623
#ifdef TD_DEBUG_PRINT_TAG
2624
static void debugPrintTagVal(int8_t type, const void *val, int32_t vlen, const char *tag, int32_t ln) {
2625
  switch (type) {
2626
    case TSDB_DATA_TYPE_VARBINARY:
2627
    case TSDB_DATA_TYPE_JSON:
2628
    case TSDB_DATA_TYPE_VARCHAR:
2629
    case TSDB_DATA_TYPE_NCHAR:
2630
    case TSDB_DATA_TYPE_GEOMETRY: {
2631
      char tmpVal[32] = {0};
2632
      tstrncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
2633
      printf("%s:%d type:%d vlen:%d, val:\"%s\"\n", tag, ln, (int32_t)type, vlen, tmpVal);
2634
    } break;
2635
    case TSDB_DATA_TYPE_FLOAT:
2636
      printf("%s:%d type:%d vlen:%d, val:%f\n", tag, ln, (int32_t)type, vlen, *(float *)val);
2637
      break;
2638
    case TSDB_DATA_TYPE_DOUBLE:
2639
      printf("%s:%d type:%d vlen:%d, val:%lf\n", tag, ln, (int32_t)type, vlen, *(double *)val);
2640
      break;
2641
    case TSDB_DATA_TYPE_BOOL:
2642
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
2643
      break;
2644
    case TSDB_DATA_TYPE_TINYINT:
2645
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
2646
      break;
2647
    case TSDB_DATA_TYPE_SMALLINT:
2648
      printf("%s:%d type:%d vlen:%d, val:%" PRIi16 "\n", tag, ln, (int32_t)type, vlen, *(int16_t *)val);
2649
      break;
2650
    case TSDB_DATA_TYPE_INT:
2651
      printf("%s:%d type:%d vlen:%d, val:%" PRIi32 "\n", tag, ln, (int32_t)type, vlen, *(int32_t *)val);
2652
      break;
2653
    case TSDB_DATA_TYPE_BIGINT:
2654
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
2655
      break;
2656
    case TSDB_DATA_TYPE_TIMESTAMP:
2657
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
2658
      break;
2659
    case TSDB_DATA_TYPE_UTINYINT:
2660
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
2661
      break;
2662
    case TSDB_DATA_TYPE_USMALLINT:
2663
      printf("%s:%d type:%d vlen:%d, val:%" PRIu16 "\n", tag, ln, (int32_t)type, vlen, *(uint16_t *)val);
2664
      break;
2665
    case TSDB_DATA_TYPE_UINT:
2666
      printf("%s:%d type:%d vlen:%d, val:%" PRIu32 "\n", tag, ln, (int32_t)type, vlen, *(uint32_t *)val);
2667
      break;
2668
    case TSDB_DATA_TYPE_UBIGINT:
2669
      printf("%s:%d type:%d vlen:%d, val:%" PRIu64 "\n", tag, ln, (int32_t)type, vlen, *(uint64_t *)val);
2670
      break;
2671
    case TSDB_DATA_TYPE_NULL:
2672
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
2673
      break;
2674
    default:
2675
      break;
2676
  }
2677
}
2678

2679
void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
2680
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
2681
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
2682
  uint8_t *p = NULL;
2683
  int16_t  offset = 0;
2684

2685
  if (isLarge) {
2686
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
2687
  } else {
2688
    p = (uint8_t *)&pTag->idx[pTag->nTag];
2689
  }
2690
  printf("%s:%d >>> STAG === %s:%s, len: %d, nTag: %d, sver:%d\n", tag, ln, isJson ? "json" : "normal",
2691
         isLarge ? "large" : "small", (int32_t)pTag->len, (int32_t)pTag->nTag, pTag->ver);
2692
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
2693
    if (isLarge) {
2694
      offset = ((int16_t *)pTag->idx)[n];
2695
    } else {
2696
      offset = pTag->idx[n];
2697
    }
2698
    STagVal tagVal = {0};
2699
    if (isJson) {
2700
      tagVal.pKey = (char *)POINTER_SHIFT(p, offset);
2701
    } else {
2702
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
2703
    }
2704
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
2705
    tGetTagVal(p + offset, &tagVal, isJson);
2706
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
2707
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
2708
    } else {
2709
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
2710
    }
2711
  }
2712
  printf("\n");
2713
}
2714
#endif
2715

2716
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
387,978,898✔
2717
  int32_t n = 0;
387,978,898✔
2718

2719
  // key
2720
  if (isJson) {
387,978,898✔
2721
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,670,664✔
2722
  } else {
2723
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
773,273,989✔
2724
  }
2725

2726
  // type
2727
  n += tPutI8(p ? p + n : p, pTagVal->type);
387,965,755✔
2728

2729
  // value
2730
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
388,009,780✔
2731
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
330,789,969✔
2732
  } else {
2733
    p = p ? p + n : p;
222,621,931✔
2734
    n += tDataTypes[pTagVal->type].bytes;
222,624,712✔
2735
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
222,574,202✔
2736
  }
2737

2738
  return n;
387,935,229✔
2739
}
2740
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
2,147,483,647✔
2741
  int32_t n = 0;
2,147,483,647✔
2742

2743
  // key
2744
  if (isJson) {
2,147,483,647✔
2745
    n += tGetCStr(p + n, &pTagVal->pKey);
10,457,286✔
2746
  } else {
2747
    n += tGetI16v(p + n, &pTagVal->cid);
2,147,483,647✔
2748
  }
2749

2750
  // type
2751
  n += tGetI8(p + n, &pTagVal->type);
2,147,483,647✔
2752

2753
  // value
2754
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
2,147,483,647✔
2755
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
2,147,483,647✔
2756
  } else {
2757
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
2,147,483,647✔
2758
    n += tDataTypes[pTagVal->type].bytes;
2,147,483,647✔
2759
  }
2760

2761
  return n;
2,147,483,647✔
2762
}
2763

2764
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
11,419,124✔
2765

2766
bool tTagIsJsonNull(void *data) {
1,415,471✔
2767
  STag  *pTag = (STag *)data;
1,415,471✔
2768
  int8_t isJson = tTagIsJson(pTag);
1,415,471✔
2769
  if (!isJson) return false;
1,415,053✔
2770
  return ((STag *)data)->nTag == 0;
799,447✔
2771
}
2772

2773
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
80,835,628✔
2774
  int32_t  code = 0;
80,835,628✔
2775
  uint8_t *p = NULL;
80,835,628✔
2776
  int16_t  n = 0;
80,835,628✔
2777
  int16_t  nTag = taosArrayGetSize(pArray);
80,835,628✔
2778
  int32_t  szTag = 0;
80,837,360✔
2779
  int8_t   isLarge = 0;
80,837,360✔
2780

2781
  // sort
2782
  if (isJson) {
80,837,360✔
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
434,791✔
2784
  } else {
2785
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
80,402,569✔
2786
  }
2787

2788
  // get size
2789
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
274,787,459✔
2790
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
193,994,697✔
2791
  }
2792
  if (szTag <= INT8_MAX) {
80,792,762✔
2793
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
72,064,402✔
2794
  } else {
2795
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
8,728,360✔
2796
    isLarge = 1;
8,728,360✔
2797
  }
2798

2799
  // build tag
2800
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
80,792,762✔
2801
  if ((*ppTag) == NULL) {
80,834,613✔
2802
    code = terrno;
×
2803
    goto _err;
×
2804
  }
2805
  (*ppTag)->flags = 0;
80,836,653✔
2806
  if (isJson) {
80,848,862✔
2807
    (*ppTag)->flags |= TD_TAG_JSON;
434,791✔
2808
  }
2809
  if (isLarge) {
80,848,862✔
2810
    (*ppTag)->flags |= TD_TAG_LARGE;
8,728,495✔
2811
  }
2812
  (*ppTag)->len = szTag;
80,848,862✔
2813
  (*ppTag)->nTag = nTag;
80,836,403✔
2814
  (*ppTag)->ver = version;
80,824,278✔
2815

2816
  if (isLarge) {
80,801,824✔
2817
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
8,728,495✔
2818
  } else {
2819
    p = (uint8_t *)&(*ppTag)->idx[nTag];
72,073,329✔
2820
  }
2821
  n = 0;
80,820,982✔
2822
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
274,814,912✔
2823
    if (isLarge) {
193,976,598✔
2824
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
50,964,488✔
2825
    } else {
2826
      (*ppTag)->idx[iTag] = n;
143,012,110✔
2827
    }
2828
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
193,959,635✔
2829
  }
2830
#ifdef TD_DEBUG_PRINT_TAG
2831
  debugPrintSTag(*ppTag, __func__, __LINE__);
2832
#endif
2833

2834
  return code;
80,838,314✔
2835

2836
_err:
×
2837
  return code;
×
2838
}
2839

2840
void tTagFree(STag *pTag) {
1,179,625,528✔
2841
  if (pTag) taosMemoryFree(pTag);
1,179,625,528✔
2842
}
1,179,625,528✔
2843

2844
char *tTagValToData(const STagVal *value, bool isJson) {
658,897,643✔
2845
  if (!value) {
658,897,643✔
2846
    return NULL;
×
2847
  }
2848

2849
  char  *data = NULL;
658,897,643✔
2850
  int8_t typeBytes = 0;
658,897,643✔
2851
  if (isJson) {
658,897,643✔
2852
    typeBytes = CHAR_BYTES;
2,354,576✔
2853
  }
2854

2855
  if (IS_VAR_DATA_TYPE(value->type)) {
658,897,643✔
2856
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
242,418,907✔
2857
    if (data == NULL) {
242,194,497✔
2858
      return NULL;
×
2859
    }
2860

2861
    if (isJson) {
242,194,497✔
2862
      *data = value->type;
1,782,046✔
2863
    }
2864

2865
    varDataLen(data + typeBytes) = value->nData;
242,194,497✔
2866
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
242,218,537✔
2867
  } else {
2868
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
416,661,421✔
2869
  }
2870

2871
  return data;
658,962,953✔
2872
}
2873

2874
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
2,147,483,647✔
2875
  if (!pTag || !pTagVal) {
2,147,483,647✔
2876
    return false;
×
2877
  }
2878

2879
  int16_t  lidx = 0;
2,147,483,647✔
2880
  int16_t  ridx = pTag->nTag - 1;
2,147,483,647✔
2881
  int16_t  midx;
2882
  uint8_t *p;
2883
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
2,147,483,647✔
2884
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
2,147,483,647✔
2885
  int16_t  offset;
2886
  STagVal  tv;
2,147,483,647✔
2887
  int      c;
2888

2889
  if (isLarge) {
2,147,483,647✔
2890
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
487,309,712✔
2891
  } else {
2892
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,760,098,119✔
2893
  }
2894

2895
  pTagVal->type = TSDB_DATA_TYPE_NULL;
2,147,483,647✔
2896
  pTagVal->pData = NULL;
2,147,483,647✔
2897
  pTagVal->nData = 0;
2,147,483,647✔
2898
  while (lidx <= ridx) {
2,147,483,647✔
2899
    midx = (lidx + ridx) / 2;
2,147,483,647✔
2900
    if (isLarge) {
2,147,483,647✔
2901
      offset = ((int16_t *)pTag->idx)[midx];
1,665,462,150✔
2902
    } else {
2903
      offset = pTag->idx[midx];
2,147,483,647✔
2904
    }
2905

2906
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
2,147,483,647✔
2907
    if (isJson) {
2,147,483,647✔
2908
      c = tTagValJsonCmprFn(pTagVal, &tv);
4,064,382✔
2909
    } else {
2910
      c = tTagValCmprFn(pTagVal, &tv);
2,147,483,647✔
2911
    }
2912

2913
    if (c < 0) {
2,147,483,647✔
2914
      ridx = midx - 1;
1,937,210,725✔
2915
    } else if (c > 0) {
2,147,483,647✔
2916
      lidx = midx + 1;
2,147,483,647✔
2917
    } else {
2918
      (void)memcpy(pTagVal, &tv, sizeof(tv));
2,147,483,647✔
2919
      return true;
2,147,483,647✔
2920
    }
2921
  }
2922
  return false;
60,205,353✔
2923
}
2924

2925
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
297,861,434✔
2926
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
595,766,078✔
2927
}
2928

2929
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
1,938,940,590✔
2930

2931
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
975,568✔
2932
  int32_t  code = 0;
975,568✔
2933
  uint8_t *p = NULL;
975,568✔
2934
  STagVal  tv = {0};
975,568✔
2935
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
975,568✔
2936
  int16_t  offset = 0;
975,568✔
2937

2938
  if (isLarge) {
975,568✔
2939
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,284✔
2940
  } else {
2941
    p = (uint8_t *)&pTag->idx[pTag->nTag];
971,284✔
2942
  }
2943

2944
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
975,568✔
2945
  if (*ppArray == NULL) {
975,568✔
2946
    code = terrno;
×
2947
    goto _err;
×
2948
  }
2949

2950
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,479,265✔
2951
    if (isLarge) {
1,503,697✔
2952
      offset = ((int16_t *)pTag->idx)[iTag];
5,730✔
2953
    } else {
2954
      offset = pTag->idx[iTag];
1,497,967✔
2955
    }
2956
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,503,697✔
2957
    if (taosArrayPush(*ppArray, &tv) == NULL) {
3,007,394✔
2958
      code = terrno;
×
2959
      goto _err;
×
2960
    }
2961
  }
2962

2963
  return code;
975,568✔
2964

2965
_err:
×
2966
  return code;
×
2967
}
2968

2969

2970
void destroyTagVal(void *pTag) {
13,305,540✔
2971
  STagVal* pTagVal = (STagVal*)pTag;
13,305,540✔
2972
  if (pTagVal && IS_VAR_DATA_TYPE(pTagVal->type)) {
13,305,540✔
2973
    taosMemoryFree(pTagVal->pData);
5,804,652✔
2974
  }
2975
}
13,305,540✔
2976

2977
// STSchema ========================================
2978
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
1,149,604,858✔
2979
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
1,149,604,858✔
2980
  if (pTSchema == NULL) {
1,149,481,382✔
2981
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2982
    return NULL;
×
2983
  }
2984

2985
  pTSchema->numOfCols = numOfCols;
1,149,481,382✔
2986
  pTSchema->version = version;
1,149,519,613✔
2987

2988
  // timestamp column
2989
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,149,601,519✔
2990
    terrno = TSDB_CODE_INVALID_PARA;
×
2991
    taosMemoryFree(pTSchema);
×
2992
    return NULL;
×
2993
  }
2994
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,149,600,703✔
2995
    terrno = TSDB_CODE_INVALID_PARA;
×
2996
    taosMemoryFree(pTSchema);
×
2997
    return NULL;
×
2998
  }
2999
  pTSchema->columns[0].colId = aSchema[0].colId;
1,149,464,978✔
3000
  pTSchema->columns[0].type = aSchema[0].type;
1,149,719,282✔
3001
  pTSchema->columns[0].flags = aSchema[0].flags;
1,149,799,436✔
3002
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
1,149,700,578✔
3003
  pTSchema->columns[0].offset = -1;
1,149,705,509✔
3004

3005
  // other columns
3006
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
2,147,483,647✔
3007
    SSchema  *pSchema = &aSchema[iCol];
2,147,483,647✔
3008
    STColumn *pTColumn = &pTSchema->columns[iCol];
2,147,483,647✔
3009

3010
    pTColumn->colId = pSchema->colId;
2,147,483,647✔
3011
    pTColumn->type = pSchema->type;
2,147,483,647✔
3012
    pTColumn->flags = pSchema->flags;
2,147,483,647✔
3013
    pTColumn->offset = pTSchema->flen;
2,147,483,647✔
3014

3015
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
2,147,483,647✔
3016
      pTColumn->bytes = pSchema->bytes;
2,147,483,647✔
3017
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
2,147,483,647✔
3018
    } else {
3019
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
2,147,483,647✔
3020
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
2,147,483,647✔
3021
    }
3022

3023
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
2,147,483,647✔
3024
  }
3025

3026
#if 1  // todo : remove this
3027
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
1,149,972,130✔
3028
#endif
3029

3030
  return pTSchema;
1,149,874,916✔
3031
}
3032

3033
static int32_t tTColumnCompare(const void *p1, const void *p2) {
190,855,724✔
3034
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
190,855,724✔
3035
    return -1;
31,817,090✔
3036
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
159,065,427✔
3037
    return 1;
113,187,901✔
3038
  }
3039

3040
  return 0;
45,903,632✔
3041
}
3042

3043
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid) {
45,907,754✔
3044
  STColumn tcol = {
45,907,754✔
3045
      .colId = cid,
3046
  };
3047

3048
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
45,907,754✔
3049
}
3050

3051
// SColData ========================================
3052
void tColDataDestroy(void *ph) {
1,614,383,536✔
3053
  if (ph) {
1,614,383,536✔
3054
    SColData *pColData = (SColData *)ph;
1,614,458,925✔
3055

3056
    tFree(pColData->pBitMap);
1,614,458,925✔
3057
    tFree(pColData->aOffset);
1,614,500,981✔
3058
    tFree(pColData->pData);
1,614,471,329✔
3059
  }
3060
}
1,614,408,275✔
3061

3062
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,696,915,128✔
3063
  pColData->cid = cid;
1,696,915,128✔
3064
  pColData->type = type;
1,696,982,151✔
3065
  pColData->cflag = cflag;
1,697,017,590✔
3066
  tColDataClear(pColData);
1,697,093,907✔
3067
}
1,697,040,766✔
3068

3069
void tColDataClear(SColData *pColData) {
2,147,483,647✔
3070
  pColData->numOfNone = 0;
2,147,483,647✔
3071
  pColData->numOfNull = 0;
2,147,483,647✔
3072
  pColData->numOfValue = 0;
2,147,483,647✔
3073
  pColData->nVal = 0;
2,147,483,647✔
3074
  pColData->flag = 0;
2,147,483,647✔
3075
  pColData->nData = 0;
2,147,483,647✔
3076
}
2,147,483,647✔
3077

3078
void tColDataDeepClear(SColData *pColData) {
45,547,151✔
3079
  pColData->pBitMap = NULL;
45,547,151✔
3080
  pColData->aOffset = NULL;
45,554,108✔
3081
  pColData->pData = NULL;
45,551,855✔
3082

3083
  tColDataClear(pColData);
45,553,026✔
3084
}
45,553,126✔
3085

3086
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
3087
  int32_t  code = 0;
2,147,483,647✔
3088
  uint32_t cvtNData = sizeof(uint64_t);
2,147,483,647✔
3089
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3090
    // TODO
3091
    if (IS_STR_DATA_BLOB(pColData->type)) {
2,147,483,647✔
3092
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
72,114,736✔
3093
      if (code) goto _exit;
32,236,551✔
3094
      pColData->aOffset[pColData->nVal] = pColData->nData;
32,236,551✔
3095

3096
      if (nData) {
32,236,551✔
3097
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
29,048,445✔
3098
        if (code) goto _exit;
29,048,445✔
3099
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
29,048,445✔
3100
        pColData->nData += BSE_SEQUECE_SIZE;
29,048,445✔
3101
      } else {
3102
        // uint64_t zero = 0;
3103
        // (void)memcpy(pColData->pData + pColData->nData, &zero, BSE_SEQUECE_SIZE);
3104
        // pColData->nData += BSE_SEQUECE_SIZE;
3105
      }
3106

3107
    } else {
3108
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
2,147,483,647✔
3109
      if (code) goto _exit;
2,147,483,647✔
3110
      pColData->aOffset[pColData->nVal] = pColData->nData;
2,147,483,647✔
3111

3112
      if (nData) {
2,147,483,647✔
3113
        code = tRealloc(&pColData->pData, pColData->nData + nData);
2,147,483,647✔
3114
        if (code) goto _exit;
2,147,483,647✔
3115
        (void)memcpy(pColData->pData + pColData->nData, pData, nData);
2,147,483,647✔
3116
        pColData->nData += nData;
2,147,483,647✔
3117
      }
3118
    }
3119
  } else {
3120
    if (!(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal)) {
2,147,483,647✔
3121
      return TSDB_CODE_INVALID_PARA;
×
3122
    }
3123
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
2,147,483,647✔
3124
    if (code) goto _exit;
2,147,483,647✔
3125
    if (pData) {
2,147,483,647✔
3126
      (void)memcpy(pColData->pData + pColData->nData, pData, TYPE_BYTES[pColData->type]);
2,147,483,647✔
3127
    } else {
3128
      memset(pColData->pData + pColData->nData, 0, TYPE_BYTES[pColData->type]);
1,715,179,596✔
3129
    }
3130
    pColData->nData += tDataTypes[pColData->type].bytes;
2,147,483,647✔
3131
  }
3132
  pColData->nVal++;
2,147,483,647✔
3133

3134
_exit:
2,147,483,647✔
3135
  return code;
2,147,483,647✔
3136
}
3137
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
693,458,520✔
3138
  pColData->flag = HAS_VALUE;
693,555,154✔
3139
  pColData->numOfValue++;
693,576,962✔
3140
  return tColDataPutValue(pColData, pData, nData);
693,576,556✔
3141
}
3142
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
18,755,460✔
3143
  pColData->flag = HAS_NONE;
18,755,460✔
3144
  pColData->numOfNone++;
18,755,460✔
3145
  pColData->nVal++;
18,755,460✔
3146
  return 0;
18,755,460✔
3147
}
3148
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
33,536,434✔
3149
  pColData->flag = HAS_NULL;
33,561,140✔
3150
  pColData->numOfNull++;
33,562,313✔
3151
  pColData->nVal++;
33,562,194✔
3152
  return 0;
33,562,319✔
3153
}
3154
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
999,702✔
3155
  int32_t code = 0;
999,702✔
3156

3157
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
999,702✔
3158
  code = tRealloc(&pColData->pBitMap, nBit);
999,702✔
3159
  if (code) return code;
999,702✔
3160

3161
  memset(pColData->pBitMap, 0, nBit);
999,702✔
3162
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
999,702✔
3163

3164
  pColData->flag |= HAS_VALUE;
999,702✔
3165
  pColData->numOfValue++;
999,702✔
3166

3167
  if (pColData->nVal) {
999,702✔
3168
    if (IS_VAR_DATA_TYPE(pColData->type)) {
999,702✔
3169
      if (IS_STR_DATA_BLOB(pColData->type)) {
38,018✔
3170
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3171
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3172
        if (code) return code;
×
3173
        memset(pColData->aOffset, 0, nOffset);
×
3174

3175
      } else {
3176
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
38,018✔
3177
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
38,018✔
3178
        if (code) return code;
38,018✔
3179
        memset(pColData->aOffset, 0, nOffset);
38,018✔
3180
      }
3181
    } else {
3182
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
961,684✔
3183
      code = tRealloc(&pColData->pData, pColData->nData);
961,684✔
3184
      if (code) return code;
961,684✔
3185
      memset(pColData->pData, 0, pColData->nData);
961,684✔
3186
    }
3187
  }
3188

3189
  return tColDataPutValue(pColData, pData, nData);
999,702✔
3190
}
3191
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3192
  pColData->nVal++;
2,147,483,647✔
3193
  pColData->numOfNone++;
2,147,483,647✔
3194
  return 0;
2,147,483,647✔
3195
}
3196
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
68,852✔
3197
  int32_t code = 0;
91,640✔
3198

3199
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
91,640✔
3200
  code = tRealloc(&pColData->pBitMap, nBit);
91,640✔
3201
  if (code) return code;
91,640✔
3202

3203
  memset(pColData->pBitMap, 0, nBit);
91,640✔
3204
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
91,640✔
3205

3206
  pColData->flag |= HAS_NULL;
91,640✔
3207
  pColData->numOfNull++;
91,640✔
3208
  pColData->nVal++;
91,640✔
3209

3210
  return code;
91,640✔
3211
}
3212
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,813,156✔
3213
  int32_t code = 0;
4,813,593✔
3214

3215
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
4,813,593✔
3216
  code = tRealloc(&pColData->pBitMap, nBit);
4,813,593✔
3217
  if (code) return code;
4,813,593✔
3218

3219
  memset(pColData->pBitMap, 0, nBit);
4,813,593✔
3220
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,813,593✔
3221

3222
  pColData->flag |= HAS_VALUE;
4,813,593✔
3223
  pColData->numOfValue++;
4,813,593✔
3224

3225
  if (pColData->nVal) {
4,813,593✔
3226
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,239,197✔
3227
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,425,604✔
3228
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,425,604✔
3229
      if (code) return code;
1,425,604✔
3230
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,425,604✔
3231
        memset(pColData->aOffset, 0, nOffset);
1,419,501✔
3232
      }
3233
    } else {
3234
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,387,989✔
3235
      code = tRealloc(&pColData->pData, pColData->nData);
3,387,989✔
3236
      if (code) return code;
3,387,989✔
3237
      memset(pColData->pData, 0, pColData->nData);
3,387,989✔
3238
    }
3239
  }
3240

3241
  return tColDataPutValue(pColData, pData, nData);
4,813,593✔
3242
}
3243
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
18,796✔
3244
  int32_t code = 0;
18,796✔
3245

3246
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
18,796✔
3247
  code = tRealloc(&pColData->pBitMap, nBit);
18,796✔
3248
  if (code) return code;
18,796✔
3249

3250
  memset(pColData->pBitMap, 255, nBit);
18,796✔
3251
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
18,796✔
3252

3253
  pColData->flag |= HAS_NONE;
18,796✔
3254
  pColData->numOfNone++;
18,796✔
3255
  pColData->nVal++;
18,796✔
3256

3257
  return code;
18,796✔
3258
}
3259
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3260
  pColData->nVal++;
2,147,483,647✔
3261
  pColData->numOfNull++;
2,147,483,647✔
3262
  return 0;
2,147,483,647✔
3263
}
3264
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3265
  int32_t code = 0;
×
3266

3267
  pColData->flag |= HAS_VALUE;
×
3268
  pColData->numOfValue++;
×
3269

3270
  uint8_t *pBitMap = NULL;
×
3271
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3272
  if (code) return code;
×
3273

3274
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3275
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3276
  }
3277
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3278

3279
  tFree(pColData->pBitMap);
×
3280
  pColData->pBitMap = pBitMap;
×
3281

3282
  if (pColData->nVal) {
×
3283
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3284
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3285
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3286
      if (code) return code;
×
3287
      memset(pColData->aOffset, 0, nOffset);
×
3288
    } else {
3289
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3290
      code = tRealloc(&pColData->pData, pColData->nData);
×
3291
      if (code) return code;
×
3292
      memset(pColData->pData, 0, pColData->nData);
×
3293
    }
3294
  }
3295

3296
  return tColDataPutValue(pColData, pData, nData);
×
3297
}
3298
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
795,048✔
3299
  int32_t code = 0;
795,048✔
3300

3301
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
795,048✔
3302
  if (code) return code;
795,048✔
3303

3304
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
795,048✔
3305
  pColData->numOfNone++;
795,048✔
3306
  pColData->nVal++;
795,048✔
3307

3308
  return code;
795,048✔
3309
}
3310
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
146,856✔
3311
  int32_t code = 0;
146,856✔
3312

3313
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
146,856✔
3314
  if (code) return code;
146,856✔
3315

3316
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
146,856✔
3317
  pColData->numOfNull++;
146,856✔
3318
  pColData->nVal++;
146,856✔
3319

3320
  return code;
146,856✔
3321
}
3322
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3323
  pColData->numOfValue++;
2,147,483,647✔
3324
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3325
}
3326
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
10,019,724✔
3327
  int32_t code = 0;
10,019,724✔
3328

3329
  pColData->flag |= HAS_NONE;
10,019,724✔
3330
  pColData->numOfNone++;
10,019,724✔
3331

3332
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
10,019,724✔
3333
  code = tRealloc(&pColData->pBitMap, nBit);
10,019,724✔
3334
  if (code) return code;
10,019,724✔
3335

3336
  memset(pColData->pBitMap, 255, nBit);
10,019,724✔
3337
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
10,019,724✔
3338

3339
  return tColDataPutValue(pColData, NULL, 0);
10,019,091✔
3340
}
3341
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
23,007,702✔
3342
  int32_t code = 0;
23,639,061✔
3343

3344
  pColData->flag |= HAS_NULL;
23,639,061✔
3345
  pColData->numOfNull++;
23,639,532✔
3346

3347
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
23,639,532✔
3348
  code = tRealloc(&pColData->pBitMap, nBit);
23,639,532✔
3349
  if (code) return code;
23,637,953✔
3350

3351
  memset(pColData->pBitMap, 255, nBit);
23,637,953✔
3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
23,638,586✔
3353

3354
  return tColDataPutValue(pColData, NULL, 0);
23,639,628✔
3355
}
3356
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
339,559,192✔
3357
  int32_t code = 0;
339,559,832✔
3358

3359
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
339,559,832✔
3360
  if (code) return code;
339,564,681✔
3361

3362
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
339,564,681✔
3363
  pColData->numOfValue++;
339,564,681✔
3364

3365
  return tColDataPutValue(pColData, pData, nData);
339,561,643✔
3366
}
3367
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
869,148,895✔
3368
  int32_t code = 0;
869,148,895✔
3369

3370
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
869,148,895✔
3371
  if (code) return code;
869,150,015✔
3372

3373
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
869,150,015✔
3374
  pColData->numOfNone++;
869,156,344✔
3375

3376
  return tColDataPutValue(pColData, NULL, 0);
869,158,986✔
3377
}
3378
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
101,671✔
3379
  int32_t code = 0;
101,671✔
3380

3381
  pColData->flag |= HAS_NULL;
101,671✔
3382
  pColData->numOfNull++;
101,671✔
3383

3384
  uint8_t *pBitMap = NULL;
101,671✔
3385
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
101,671✔
3386
  if (code) return code;
101,671✔
3387

3388
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,433,195✔
3389
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2,331,524✔
3390
  }
3391
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
101,671✔
3392

3393
  tFree(pColData->pBitMap);
101,671✔
3394
  pColData->pBitMap = pBitMap;
101,671✔
3395

3396
  return tColDataPutValue(pColData, NULL, 0);
101,671✔
3397
}
3398
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3399
  int32_t code = 0;
2,147,483,647✔
3400

3401
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
2,147,483,647✔
3402
  if (code) return code;
2,147,483,647✔
3403
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
2,147,483,647✔
3404
  pColData->numOfValue++;
2,147,483,647✔
3405

3406
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3407
}
3408
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,123,958✔
3409
  int32_t code = 0;
2,123,958✔
3410

3411
  pColData->flag |= HAS_NONE;
2,123,958✔
3412
  pColData->numOfNone++;
2,123,958✔
3413

3414
  uint8_t *pBitMap = NULL;
2,123,958✔
3415
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
2,123,958✔
3416
  if (code) return code;
2,123,958✔
3417

3418
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
183,466,710✔
3419
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
181,342,752✔
3420
  }
3421
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
2,123,958✔
3422

3423
  tFree(pColData->pBitMap);
2,123,958✔
3424
  pColData->pBitMap = pBitMap;
2,123,958✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
2,123,958✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,461,264,137✔
3429
  int32_t code = 0;
1,462,589,672✔
3430

3431
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,462,589,672✔
3432
  if (code) return code;
1,462,625,612✔
3433
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,462,625,612✔
3434
  pColData->numOfNull++;
1,462,731,179✔
3435

3436
  return tColDataPutValue(pColData, NULL, 0);
1,462,683,875✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,123,652✔
3439
  int32_t code = 0;
3,240,124✔
3440

3441
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
3,240,124✔
3442
  if (code) return code;
3,240,124✔
3443
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
3,240,124✔
3444
  pColData->numOfValue++;
3,240,124✔
3445

3446
  return tColDataPutValue(pColData, pData, nData);
3,240,124✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
334,150,694✔
3449
  int32_t code = 0;
334,150,694✔
3450

3451
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
334,150,694✔
3452
  if (code) return code;
334,150,694✔
3453
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
334,150,694✔
3454
  pColData->numOfNone++;
334,150,694✔
3455

3456
  return tColDataPutValue(pColData, NULL, 0);
334,150,694✔
3457
}
3458
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
382,097✔
3459
  int32_t code = 0;
382,097✔
3460

3461
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
382,097✔
3462
  if (code) return code;
382,097✔
3463
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
382,097✔
3464
  pColData->numOfNull++;
382,097✔
3465

3466
  return tColDataPutValue(pColData, NULL, 0);
382,097✔
3467
}
3468
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
3469
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
3470
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
3471
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
3472
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
3473
    {tColDataAppendValue40, tColDataAppendValue41, tColDataAppendValue42},  // HAS_VALUE
3474
    {tColDataAppendValue50, tColDataAppendValue51, tColDataAppendValue52},  // HAS_VALUE|HAS_NONE
3475
    {tColDataAppendValue60, tColDataAppendValue61, tColDataAppendValue62},  // HAS_VALUE|HAS_NULL
3476
    {tColDataAppendValue70, tColDataAppendValue71, tColDataAppendValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
3477

3478
    //       VALUE                  NONE                     NULL
3479
};
3480

3481
static FORCE_INLINE int32_t tColDataPutValueBlob(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
3482
  int32_t  code = 0;
119✔
3483
  uint8_t  buf[sizeof(uint64_t) + 1] = {0};
×
3484
  uint64_t seq = 0;
119✔
3485

3486
  int32_t offset = 0;
119✔
3487
  if (IS_STR_DATA_BLOB(pColData->type)) {
119✔
3488
    code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
119✔
3489
    if (code) goto _exit;
119✔
3490
    pColData->aOffset[pColData->nVal] = pColData->nData;
119✔
3491
    if (nData) {
119✔
3492
      SBlobItem item = {.seqOffsetInRow = seq, .len = nData, .data = pData, .type = TSDB_DATA_BLOB_VALUE};
119✔
3493
      code = tBlobSetPush(pArg, &item, &seq, 0);
119✔
3494
      if (code != 0) return code;
119✔
3495

3496
      offset = tPutU64(buf, seq);
119✔
3497
      code = tRealloc(&pColData->pData, pColData->nData + offset);
119✔
3498
      if (code != 0) return code;
119✔
3499
      memcpy(pColData->pData + pColData->nData, buf, offset);
119✔
3500
      pColData->nData += offset;
119✔
3501
    } else {
3502
      int8_t type = pData ? TSDB_DATA_BLOB_EMPTY_VALUE : TSDB_DATA_BLOB_NULL_VALUE;
×
3503
      code = addEmptyItemToBlobSet(pArg, type, NULL);
×
3504
    }
3505
  }
3506
  pColData->nVal++;
119✔
3507

3508
_exit:
119✔
3509
  return code;
119✔
3510
}
3511

3512
static FORCE_INLINE int32_t tColDataAppendValueBlob00(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
81✔
3513
  pColData->flag = HAS_VALUE;
81✔
3514
  pColData->numOfValue++;
81✔
3515
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
81✔
3516
}
3517
static FORCE_INLINE int32_t tColDataAppendValueBlob01(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3518
  pColData->flag = HAS_NONE;
×
3519
  pColData->numOfNone++;
×
3520
  pColData->nVal++;
×
3521
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3522
}
3523
static FORCE_INLINE int32_t tColDataAppendValueBlob02(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
124✔
3524
  pColData->flag = HAS_NULL;
124✔
3525
  pColData->numOfNull++;
124✔
3526
  pColData->nVal++;
124✔
3527
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
124✔
3528
}
3529
static FORCE_INLINE int32_t tColDataAppendValueBlob10(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3530
  int32_t code = 0;
×
3531

3532
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3533
  code = tRealloc(&pColData->pBitMap, nBit);
×
3534
  if (code) return code;
×
3535

3536
  memset(pColData->pBitMap, 0, nBit);
×
3537
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3538

3539
  pColData->flag |= HAS_VALUE;
×
3540
  pColData->numOfValue++;
×
3541

3542
  if (pColData->nVal) {
×
3543
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3544
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3545
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3546
      if (code) return code;
×
3547
      memset(pColData->aOffset, 0, nOffset);
×
3548
    } else {
3549
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3550
      code = tRealloc(&pColData->pData, pColData->nData);
×
3551
      if (code) return code;
×
3552
      memset(pColData->pData, 0, pColData->nData);
×
3553
    }
3554
  }
3555

3556
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3557
}
3558
static FORCE_INLINE int32_t tColDataAppendValueBlob11(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3559
  pColData->nVal++;
×
3560
  pColData->numOfNone++;
×
3561
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3562
}
3563
static FORCE_INLINE int32_t tColDataAppendValueBlob12(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3564
  int32_t code = 0;
×
3565

3566
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3567
  code = tRealloc(&pColData->pBitMap, nBit);
×
3568
  if (code) return code;
×
3569

3570
  memset(pColData->pBitMap, 0, nBit);
×
3571
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3572

3573
  pColData->flag |= HAS_NULL;
×
3574
  pColData->numOfNull++;
×
3575
  pColData->nVal++;
×
3576
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3577
}
3578
static FORCE_INLINE int32_t tColDataAppendValueBlob20(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3579
  int32_t code = 0;
×
3580

3581
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3582
  code = tRealloc(&pColData->pBitMap, nBit);
×
3583
  if (code) return code;
×
3584

3585
  memset(pColData->pBitMap, 0, nBit);
×
3586
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3587

3588
  pColData->flag |= HAS_VALUE;
×
3589
  pColData->numOfValue++;
×
3590

3591
  if (pColData->nVal) {
×
3592
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3593
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3594
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3595
      if (code) return code;
×
3596
      memset(pColData->aOffset, 0, nOffset);
×
3597
    } else {
3598
      return TSDB_CODE_INVALID_MSG;
×
3599
    }
3600
  }
3601

3602
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3603
}
3604
static FORCE_INLINE int32_t tColDataAppendValueBlob21(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3605
  int32_t code = 0;
×
3606

3607
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3608
  code = tRealloc(&pColData->pBitMap, nBit);
×
3609
  if (code) return code;
×
3610

3611
  memset(pColData->pBitMap, 255, nBit);
×
3612
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3613

3614
  pColData->flag |= HAS_NONE;
×
3615
  pColData->numOfNone++;
×
3616
  pColData->nVal++;
×
3617
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3618
}
3619
static FORCE_INLINE int32_t tColDataAppendValueBlob22(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3620
  pColData->nVal++;
×
3621
  pColData->numOfNull++;
×
3622
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3623
}
3624
static FORCE_INLINE int32_t tColDataAppendValueBlob30(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3625
  int32_t code = 0;
×
3626

3627
  pColData->flag |= HAS_VALUE;
×
3628
  pColData->numOfValue++;
×
3629

3630
  uint8_t *pBitMap = NULL;
×
3631
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3632
  if (code) return code;
×
3633

3634
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3635
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3636
  }
3637
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3638

3639
  tFree(pColData->pBitMap);
×
3640
  pColData->pBitMap = pBitMap;
×
3641

3642
  if (pColData->nVal) {
×
3643
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3644
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3645
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3646
      if (code) return code;
×
3647
      memset(pColData->aOffset, 0, nOffset);
×
3648
    } else {
3649
      return TSDB_CODE_INVALID_MSG;
×
3650
    }
3651
  }
3652

3653
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3654
}
3655
static FORCE_INLINE int32_t tColDataAppendValueBlob31(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3656
  int32_t code = 0;
×
3657

3658
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3659
  if (code) return code;
×
3660

3661
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3662
  pColData->numOfNone++;
×
3663
  pColData->nVal++;
×
3664

3665
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3666
}
3667
static FORCE_INLINE int32_t tColDataAppendValueBlob32(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3668
  int32_t code = 0;
×
3669

3670
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3671
  if (code) return code;
×
3672

3673
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3674
  pColData->numOfNull++;
×
3675
  pColData->nVal++;
×
3676
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3677
}
3678
static FORCE_INLINE int32_t tColDataAppendValueBlob40(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
38✔
3679
  pColData->numOfValue++;
38✔
3680
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
38✔
3681
}
3682
static FORCE_INLINE int32_t tColDataAppendValueBlob41(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3683
  int32_t code = 0;
×
3684

3685
  pColData->flag |= HAS_NONE;
×
3686
  pColData->numOfNone++;
×
3687

3688
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3689
  code = tRealloc(&pColData->pBitMap, nBit);
×
3690
  if (code) return code;
×
3691

3692
  memset(pColData->pBitMap, 255, nBit);
×
3693
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3694

3695
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3696
}
3697
static FORCE_INLINE int32_t tColDataAppendValueBlob42(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3698
  int32_t code = 0;
×
3699

3700
  pColData->flag |= HAS_NULL;
×
3701
  pColData->numOfNull++;
×
3702

3703
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3704
  code = tRealloc(&pColData->pBitMap, nBit);
×
3705
  if (code) return code;
×
3706

3707
  memset(pColData->pBitMap, 255, nBit);
×
3708
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3709

3710
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3711
}
3712
static FORCE_INLINE int32_t tColDataAppendValueBlob50(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3713
  int32_t code = 0;
×
3714

3715
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3716
  if (code) return code;
×
3717

3718
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3719
  pColData->numOfValue++;
×
3720

3721
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3722
}
3723
static FORCE_INLINE int32_t tColDataAppendValueBlob51(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3724
  int32_t code = 0;
×
3725

3726
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3727
  if (code) return code;
×
3728

3729
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3730
  pColData->numOfNone++;
×
3731

3732
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3733
}
3734
static FORCE_INLINE int32_t tColDataAppendValueBlob52(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3735
  int32_t code = 0;
×
3736

3737
  pColData->flag |= HAS_NULL;
×
3738
  pColData->numOfNull++;
×
3739

3740
  uint8_t *pBitMap = NULL;
×
3741
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3742
  if (code) return code;
×
3743

3744
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3745
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
×
3746
  }
3747
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
×
3748

3749
  tFree(pColData->pBitMap);
×
3750
  pColData->pBitMap = pBitMap;
×
3751

3752
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3753
}
3754
static FORCE_INLINE int32_t tColDataAppendValueBlob60(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3755
  int32_t code = 0;
×
3756

3757
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3758
  if (code) return code;
×
3759
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3760
  pColData->numOfValue++;
×
3761

3762
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3763
}
3764
static FORCE_INLINE int32_t tColDataAppendValueBlob61(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3765
  int32_t code = 0;
×
3766

3767
  pColData->flag |= HAS_NONE;
×
3768
  pColData->numOfNone++;
×
3769

3770
  uint8_t *pBitMap = NULL;
×
3771
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3772
  if (code) return code;
×
3773

3774
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3775
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
×
3776
  }
3777
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
×
3778

3779
  tFree(pColData->pBitMap);
×
3780
  pColData->pBitMap = pBitMap;
×
3781

3782
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3783
}
3784
static FORCE_INLINE int32_t tColDataAppendValueBlob62(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3785
  int32_t code = 0;
×
3786

3787
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3788
  if (code) return code;
×
3789
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3790
  pColData->numOfNull++;
×
3791

3792
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3793
}
3794
static FORCE_INLINE int32_t tColDataAppendValueBlob70(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3795
  int32_t code = 0;
×
3796

3797
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3798
  if (code) return code;
×
3799
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
×
3800
  pColData->numOfValue++;
×
3801

3802
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3803
}
3804
static FORCE_INLINE int32_t tColDataAppendValueBlob71(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3805
  int32_t code = 0;
×
3806

3807
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3808
  if (code) return code;
×
3809
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
×
3810
  pColData->numOfNone++;
×
3811

3812
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3813
}
3814
static FORCE_INLINE int32_t tColDataAppendValueBlob72(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3815
  int32_t code = 0;
×
3816

3817
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3818
  if (code) return code;
×
3819
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
×
3820
  pColData->numOfNull++;
×
3821

3822
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3823
}
3824

3825
static int32_t (*tColDataAppendValueBlobImpl[8][3])(void *pDst, SColData *pColData, uint8_t *pData, uint32_t nData) = {
3826
    {tColDataAppendValueBlob00, tColDataAppendValueBlob01, tColDataAppendValueBlob02},  // 0
3827
    {tColDataAppendValueBlob10, tColDataAppendValueBlob11, tColDataAppendValueBlob12},  // HAS_NONE
3828
    {tColDataAppendValueBlob20, tColDataAppendValueBlob21, tColDataAppendValueBlob22},  // HAS_NULL
3829
    {tColDataAppendValueBlob30, tColDataAppendValueBlob31, tColDataAppendValueBlob32},  // HAS_NULL|HAS_NONE
3830
    {tColDataAppendValueBlob40, tColDataAppendValueBlob41, tColDataAppendValueBlob42},  // HAS_VALUE
3831
    {tColDataAppendValueBlob50, tColDataAppendValueBlob51, tColDataAppendValueBlob52},  // HAS_VALUE|HAS_NONE
3832
    {tColDataAppendValueBlob60, tColDataAppendValueBlob61, tColDataAppendValueBlob62},  // HAS_VALUE|HAS_NULL
3833
    {tColDataAppendValueBlob70, tColDataAppendValueBlob71, tColDataAppendValueBlob72}   // HAS_VALUE|HAS_NULL|HAS_NONE
3834
    //       VALUE                  NONE                     NULL
3835
};
3836
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
2,147,483,647✔
3837
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) {
2,147,483,647✔
3838
    return TSDB_CODE_INVALID_PARA;
×
3839
  }
3840
  return tColDataAppendValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
3841
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData);
2,147,483,647✔
3842
}
3843

3844
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,224✔
3845
  pColData->numOfNone--;
3,224✔
3846
  pColData->nVal--;
3,224✔
3847
  if (pColData->numOfNone) {
3,224✔
3848
    return tColDataAppendValue10(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
3,224✔
3851
    return tColDataAppendValue00(pColData, pData, nData);
3,224✔
3852
  }
3853
}
3854
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
26,148✔
3855
  pColData->numOfNone--;
26,148✔
3856
  pColData->nVal--;
26,148✔
3857
  if (pColData->numOfNone) {
26,148✔
3858
    return tColDataAppendValue12(pColData, pData, nData);
22,788✔
3859
  } else {
3860
    pColData->flag = 0;
3,360✔
3861
    return tColDataAppendValue02(pColData, pData, nData);
3,360✔
3862
  }
3863
  return 0;
3864
}
3865
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
94,487✔
3866
  if (forward) {
94,487✔
3867
    pColData->numOfNull--;
94,487✔
3868
    pColData->nVal--;
94,487✔
3869
    if (pColData->numOfNull) {
93,847✔
3870
      return tColDataAppendValue20(pColData, pData, nData);
437✔
3871
    } else {
3872
      pColData->flag = 0;
94,050✔
3873
      return tColDataAppendValue00(pColData, pData, nData);
94,050✔
3874
    }
3875
  }
3876
  return 0;
×
3877
}
3878
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3879
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3880
    pColData->numOfNone--;
×
3881
    pColData->nVal--;
×
3882
    if (pColData->numOfNone) {
×
3883
      return tColDataAppendValue30(pColData, pData, nData);
×
3884
    } else {
3885
      pColData->flag = HAS_NULL;
×
3886
      return tColDataAppendValue20(pColData, pData, nData);
×
3887
    }
3888
  } else if (forward) {  // NULL ==> VALUE
×
3889
    pColData->numOfNull--;
×
3890
    pColData->nVal--;
×
3891
    if (pColData->numOfNull) {
×
3892
      return tColDataAppendValue30(pColData, pData, nData);
×
3893
    } else {
3894
      pColData->flag = HAS_NONE;
×
3895
      return tColDataAppendValue10(pColData, pData, nData);
×
3896
    }
3897
  }
3898
  return 0;
×
3899
}
3900
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
330,232✔
3901
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
330,232✔
3902
    pColData->numOfNone--;
330,232✔
3903
    pColData->numOfNull++;
330,232✔
3904
    if (pColData->numOfNone) {
330,232✔
3905
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
319,032✔
3906
    } else {
3907
      pColData->flag = HAS_NULL;
11,200✔
3908
    }
3909
  }
3910
  return 0;
330,232✔
3911
}
3912
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3913
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3914
    pColData->nVal--;
2,147,483,647✔
3915
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3916
      pColData->nData = pColData->aOffset[pColData->nVal];
1,752,996,418✔
3917
    } else {
3918
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3919
    }
3920
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3921
  }
3922
  return 0;
×
3923
}
3924
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
629,357✔
3925
  if (forward) {  // VALUE ==> NULL
629,357✔
3926
    pColData->numOfValue--;
629,357✔
3927
    pColData->nVal--;
629,357✔
3928
    if (pColData->numOfValue) {
629,357✔
3929
      if (IS_VAR_DATA_TYPE(pColData->type)) {
608,011✔
3930
        pColData->nData = pColData->aOffset[pColData->nVal];
177,338✔
3931
      } else {
3932
        pColData->nData -= TYPE_BYTES[pColData->type];
430,673✔
3933
      }
3934
      return tColDataAppendValue42(pColData, pData, nData);
608,011✔
3935
    } else {
3936
      pColData->flag = 0;
21,346✔
3937
      pColData->nData = 0;
21,346✔
3938
      return tColDataAppendValue02(pColData, pData, nData);
21,346✔
3939
    }
3940
  }
3941
  return 0;
×
3942
}
3943
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,867,533✔
3944
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
1,867,533✔
3945
    pColData->numOfNone--;
126,600✔
3946
    pColData->nVal--;
126,600✔
3947
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
126,600✔
3948
      pColData->nData -= TYPE_BYTES[pColData->type];
125,967✔
3949
    }
3950
    if (pColData->numOfNone) {
127,233✔
3951
      return tColDataAppendValue50(pColData, pData, nData);
×
3952
    } else {
3953
      pColData->flag = HAS_VALUE;
126,600✔
3954
      return tColDataAppendValue40(pColData, pData, nData);
126,600✔
3955
    }
3956
  } else if (forward) {  // VALUE ==> VALUE
1,740,933✔
3957
    pColData->nVal--;
1,740,933✔
3958
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,740,933✔
3959
      pColData->nData = pColData->aOffset[pColData->nVal];
248,580✔
3960
    } else {
3961
      pColData->nData -= TYPE_BYTES[pColData->type];
1,492,353✔
3962
    }
3963
    return tColDataPutValue(pColData, pData, nData);
1,740,933✔
3964
  }
3965
  return 0;
×
3966
}
3967
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
23,348✔
3968
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
23,348✔
3969
    pColData->numOfNone--;
23,348✔
3970
    pColData->nVal--;
23,348✔
3971
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
23,348✔
3972
      pColData->nData -= TYPE_BYTES[pColData->type];
15,752✔
3973
    }
3974
    if (pColData->numOfNone) {
23,348✔
3975
      return tColDataAppendValue52(pColData, pData, nData);
×
3976
    } else {
3977
      pColData->flag = HAS_VALUE;
23,348✔
3978
      return tColDataAppendValue42(pColData, pData, nData);
23,348✔
3979
    }
3980
  } else if (forward) {  // VALUE ==> NULL
×
3981
    pColData->numOfValue--;
×
3982
    pColData->nVal--;
×
3983
    if (pColData->numOfValue) {
×
3984
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3985
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3986
      } else {
3987
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3988
      }
3989
      return tColDataAppendValue52(pColData, pData, nData);
×
3990
    } else {
3991
      pColData->flag = HAS_NONE;
×
3992
      pColData->nData = 0;
×
3993
      return tColDataAppendValue12(pColData, pData, nData);
×
3994
    }
3995
  }
3996
  return 0;
×
3997
}
3998
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,251,526✔
3999
  if (forward) {
3,251,526✔
4000
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,251,526✔
4001
      pColData->numOfNull--;
2,291,809✔
4002
      pColData->nVal--;
2,291,169✔
4003
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
2,291,169✔
4004
        pColData->nData -= TYPE_BYTES[pColData->type];
1,894,455✔
4005
      }
4006
      if (pColData->numOfNull) {
2,292,442✔
4007
        return tColDataAppendValue60(pColData, pData, nData);
2,328✔
4008
      } else {
4009
        pColData->flag = HAS_VALUE;
2,286,942✔
4010
        return tColDataAppendValue40(pColData, pData, nData);
2,288,841✔
4011
      }
4012
    } else {  // VALUE ==> VALUE
4013
      pColData->nVal--;
959,717✔
4014
      if (IS_VAR_DATA_TYPE(pColData->type)) {
959,717✔
4015
        pColData->nData = pColData->aOffset[pColData->nVal];
119,084✔
4016
      } else {
4017
        pColData->nData -= TYPE_BYTES[pColData->type];
840,633✔
4018
      }
4019
      return tColDataPutValue(pColData, pData, nData);
959,717✔
4020
    }
4021
  }
4022
  return 0;
×
4023
}
4024
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,509,794✔
4025
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,509,794✔
4026
    pColData->numOfValue--;
951,663✔
4027
    pColData->nVal--;
951,663✔
4028
    if (pColData->numOfValue) {
951,663✔
4029
      if (IS_VAR_DATA_TYPE(pColData->type)) {
951,663✔
4030
        pColData->nData = pColData->aOffset[pColData->nVal];
101,849✔
4031
      } else {
4032
        pColData->nData -= TYPE_BYTES[pColData->type];
849,814✔
4033
      }
4034
      return tColDataAppendValue62(pColData, pData, nData);
951,663✔
4035
    } else {
4036
      pColData->flag = HAS_NULL;
×
4037
      pColData->nData = 0;
×
4038
      return tColDataAppendValue20(pColData, pData, nData);
×
4039
    }
4040
  }
4041
  return 0;
558,131✔
4042
}
4043
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
320,696✔
4044
  int32_t code = 0;
320,696✔
4045

4046
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
320,696✔
4047
  if (bv == 0) {  // NONE ==> VALUE
320,696✔
4048
    pColData->numOfNone--;
75,960✔
4049
    pColData->nVal--;
75,960✔
4050
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
75,960✔
4051
      pColData->nData -= TYPE_BYTES[pColData->type];
75,960✔
4052
    }
4053
    if (pColData->numOfNone) {
75,960✔
4054
      return tColDataAppendValue70(pColData, pData, nData);
75,960✔
4055
    } else {
4056
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4057
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
×
4058
      }
4059
      pColData->flag = (HAS_VALUE | HAS_NULL);
×
4060
      return tColDataAppendValue60(pColData, pData, nData);
×
4061
    }
4062
  } else if (bv == 1) {  // NULL ==> VALUE
244,736✔
4063
    if (forward) {
41,152✔
4064
      pColData->numOfNull--;
41,152✔
4065
      pColData->nVal--;
41,152✔
4066
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
41,152✔
4067
        pColData->nData -= TYPE_BYTES[pColData->type];
41,152✔
4068
      }
4069
      if (pColData->numOfNull) {
41,152✔
4070
        return tColDataAppendValue70(pColData, pData, nData);
40,512✔
4071
      } else {
4072
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
1,920✔
4073
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,280✔
4074
        }
4075
        pColData->flag = (HAS_VALUE | HAS_NONE);
640✔
4076
        return tColDataAppendValue50(pColData, pData, nData);
640✔
4077
      }
4078
    }
4079
  } else if (bv == 2) {  // VALUE ==> VALUE
203,584✔
4080
    if (forward) {
203,584✔
4081
      pColData->nVal--;
203,584✔
4082
      if (IS_VAR_DATA_TYPE(pColData->type)) {
203,584✔
4083
        pColData->nData = pColData->aOffset[pColData->nVal];
50,640✔
4084
      } else {
4085
        pColData->nData -= TYPE_BYTES[pColData->type];
152,944✔
4086
      }
4087
      return tColDataPutValue(pColData, pData, nData);
203,584✔
4088
    }
4089
  } else {
4090
    return TSDB_CODE_INVALID_PARA;
×
4091
  }
4092
  return 0;
×
4093
}
4094
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
373,872✔
4095
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
373,872✔
4096
  if (bv == 0) {  // NONE ==> NULL
373,872✔
4097
    pColData->numOfNone--;
373,872✔
4098
    pColData->nVal--;
373,872✔
4099
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
373,872✔
4100
      pColData->nData -= TYPE_BYTES[pColData->type];
232,488✔
4101
    }
4102
    if (pColData->numOfNone) {
373,872✔
4103
      return tColDataAppendValue72(pColData, pData, nData);
×
4104
    } else {
4105
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
11,747,152✔
4106
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
11,373,280✔
4107
      }
4108
      pColData->flag = (HAS_VALUE | HAS_NULL);
373,872✔
4109
      return tColDataAppendValue62(pColData, pData, nData);
373,872✔
4110
    }
4111
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4112
    pColData->numOfValue--;
×
4113
    pColData->nVal--;
×
4114
    if (pColData->numOfValue) {
×
4115
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4116
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4117
      } else {
4118
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4119
      }
4120
      return tColDataAppendValue72(pColData, pData, nData);
×
4121
    } else {
4122
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4123
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4124
      }
4125
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4126
      pColData->nData = 0;
×
4127
      return tColDataAppendValue32(pColData, pData, nData);
×
4128
    }
4129
  }
4130
  return 0;
×
4131
}
4132
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,900,203✔
4133
  return 0;
2,900,203✔
4134
}
4135
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4136
    {NULL, NULL, NULL},                                                     // 0
4137
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4138
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4139
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4140
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4141
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4142
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4143
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4144

4145
    //    VALUE             NONE        NULL
4146
};
4147
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward) {
2,147,483,647✔
4148
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
4149
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
4150

4151
  if (tColDataUpdateValueImpl[pColData->flag][pColVal->flag] == NULL) return 0;
2,147,483,647✔
4152

4153
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
4154
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData, forward);
2,147,483,647✔
4155
}
4156

4157
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
1,117,503,953✔
4158
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,117,503,953✔
4159
}
1,117,780,070✔
4160
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,643,635,924✔
4161
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,643,635,924✔
4162
}
1,643,632,915✔
4163
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
6,271,301✔
4164
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4165
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
6,271,301✔
4166
    case 0:
3,183,442✔
4167
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,183,442✔
4168
      break;
3,184,075✔
4169
    case 1:
3,087,859✔
4170
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
3,087,859✔
4171
      break;
3,087,859✔
4172
    default:
×
4173
      break;
×
4174
  }
4175
}
6,271,934✔
4176
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4177
  SValue value = {.type = pColData->type};
2,147,483,647✔
4178
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4179
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4180
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4181
    } else {
4182
      value.nData = pColData->nData - pColData->aOffset[iVal];
232,514,456✔
4183
    }
4184
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4185
  } else {
4186
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4187
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4188
  }
4189
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4190
}
2,147,483,647✔
4191
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
953,741,948✔
4192
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4193
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
953,741,948✔
4194
    case 0:
449,414,147✔
4195
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
449,414,147✔
4196
      break;
449,460,140✔
4197
    case 1:
504,963,740✔
4198
      tColDataGetValue4(pColData, iVal, pColVal);
4199
      break;
504,742,209✔
4200
    default:
×
4201
      break;
×
4202
  }
4203
}
954,202,349✔
4204
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4205
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4206
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4207
    case 0:
2,147,483,647✔
4208
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,147,483,647✔
4209
      break;
2,147,483,647✔
4210
    case 1:
2,147,483,647✔
4211
      tColDataGetValue4(pColData, iVal, pColVal);
4212
      break;
2,147,483,647✔
4213
    default:
×
4214
      break;
×
4215
  }
4216
}
2,147,483,647✔
4217
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
21,582,346✔
4218
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4219
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
21,582,346✔
4220
    case 0:
3,818,840✔
4221
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,818,840✔
4222
      break;
3,818,840✔
4223
    case 1:
4,588,296✔
4224
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
4,588,296✔
4225
      break;
4,588,296✔
4226
    case 2:
13,175,843✔
4227
      tColDataGetValue4(pColData, iVal, pColVal);
4228
      break;
13,175,210✔
4229
    default:
×
4230
      break;
×
4231
  }
4232
}
21,582,346✔
4233
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4234
    NULL,               // 0
4235
    tColDataGetValue1,  // HAS_NONE
4236
    tColDataGetValue2,  // HAS_NULL
4237
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4238
    tColDataGetValue4,  // HAS_VALUE
4239
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4240
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4241
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4242
};
4243
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4244
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4245
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4246
    return TSDB_CODE_INVALID_PARA;
606✔
4247
  }
4248
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4249
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4250
}
4251

4252
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
2,147,483,647✔
4253
  switch (pColData->flag) {
2,147,483,647✔
4254
    case HAS_NONE:
×
4255
      return 0;
×
4256
    case HAS_NULL:
×
4257
      return 1;
×
4258
    case (HAS_NULL | HAS_NONE):
6,245,178✔
4259
      return GET_BIT1(pColData->pBitMap, iVal);
6,245,178✔
4260
    case HAS_VALUE:
×
4261
      return 2;
×
4262
    case (HAS_VALUE | HAS_NONE):
2,147,483,647✔
4263
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
2,147,483,647✔
4264
    case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
4265
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
2,147,483,647✔
4266
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
92,513,404✔
4267
      return GET_BIT2(pColData->pBitMap, iVal);
92,513,404✔
4268
    default:
×
4269
      return 0;
×
4270
  }
4271
}
4272

4273
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
15,276,620✔
4274
  int32_t code = 0;
15,276,620✔
4275

4276
  *pColData = *pColDataFrom;
15,276,620✔
4277

4278
  // bitmap
4279
  switch (pColData->flag) {
15,276,977✔
4280
    case (HAS_NULL | HAS_NONE):
61,298✔
4281
    case (HAS_VALUE | HAS_NONE):
4282
    case (HAS_VALUE | HAS_NULL):
4283
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
61,298✔
4284
      if (pColData->pBitMap == NULL) {
61,298✔
4285
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4286
        goto _exit;
×
4287
      }
4288
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
61,298✔
4289
      break;
61,298✔
4290
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
4291
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
4292
      if (pColData->pBitMap == NULL) {
×
4293
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4294
        goto _exit;
×
4295
      }
4296
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
4297
      break;
×
4298
    default:
15,215,322✔
4299
      pColData->pBitMap = NULL;
15,215,322✔
4300
      break;
15,214,965✔
4301
  }
4302

4303
  // offset
4304
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
15,276,263✔
4305
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
77,380✔
4306
    if (pColData->aOffset == NULL) {
77,380✔
4307
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4308
      goto _exit;
×
4309
    }
4310
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
77,380✔
4311
  } else {
4312
    pColData->aOffset = NULL;
15,198,526✔
4313
  }
4314

4315
  // value
4316
  if (pColData->nData) {
15,277,334✔
4317
    pColData->pData = xMalloc(arg, pColData->nData);
15,237,532✔
4318
    if (pColData->pData == NULL) {
15,237,889✔
4319
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4320
      goto _exit;
×
4321
    }
4322

4323
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
15,237,532✔
4324
  } else {
4325
    pColData->pData = NULL;
39,445✔
4326
  }
4327

4328
_exit:
15,275,896✔
4329
  return code;
15,275,896✔
4330
}
4331

4332
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
573,871,474✔
4333
  int32_t code;
4334
  SBuffer local;
573,865,751✔
4335

4336
  if (!(colData->nVal > 0)) {
573,893,261✔
4337
    return TSDB_CODE_INVALID_PARA;
×
4338
  }
4339

4340
  (*info) = (SColDataCompressInfo){
573,892,959✔
4341
      .cmprAlg = info->cmprAlg,
573,899,958✔
4342
      .columnFlag = colData->cflag,
573,900,065✔
4343
      .flag = colData->flag,
573,886,412✔
4344
      .dataType = colData->type,
573,896,551✔
4345
      .columnId = colData->cid,
573,901,917✔
4346
      .numOfData = colData->nVal,
573,897,858✔
4347
  };
4348

4349
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
573,876,457✔
4350
    return 0;
19,153,615✔
4351
  }
4352

4353
  tBufferInit(&local);
4354
  if (assist == NULL) {
554,736,777✔
4355
    assist = &local;
×
4356
  }
4357

4358
  // bitmap
4359
  if (colData->flag != HAS_VALUE) {
554,736,777✔
4360
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
32,579,295✔
4361
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,825,797✔
4362
    } else {
4363
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
30,753,455✔
4364
    }
4365

4366
    SCompressInfo cinfo = {
32,579,252✔
4367
        .dataType = TSDB_DATA_TYPE_TINYINT,
4368
        .cmprAlg = info->cmprAlg,
32,579,281✔
4369
        .originalSize = info->bitmapOriginalSize,
32,578,872✔
4370
    };
4371

4372
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
32,579,281✔
4373
    if (code) {
32,579,295✔
4374
      tBufferDestroy(&local);
4375
      return code;
×
4376
    }
4377

4378
    info->bitmapCompressedSize = cinfo.compressedSize;
32,579,295✔
4379
  }
4380

4381
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
554,679,265✔
4382
    tBufferDestroy(&local);
4383
    return 0;
99,151✔
4384
  }
4385

4386
  // offset
4387
  if (IS_VAR_DATA_TYPE(colData->type)) {
554,606,234✔
4388
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
88,563,468✔
4389

4390
    SCompressInfo cinfo = {
88,595,151✔
4391
        .dataType = TSDB_DATA_TYPE_INT,
4392
        .cmprAlg = info->cmprAlg,
88,595,284✔
4393
        .originalSize = info->offsetOriginalSize,
88,589,043✔
4394
    };
4395

4396
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
88,592,078✔
4397
    if (code) {
88,597,143✔
4398
      tBufferDestroy(&local);
4399
      return code;
×
4400
    }
4401

4402
    info->offsetCompressedSize = cinfo.compressedSize;
88,597,143✔
4403
  }
4404

4405
  // data
4406
  if (colData->nData > 0) {
554,639,736✔
4407
    info->dataOriginalSize = colData->nData;
554,632,795✔
4408

4409
    SCompressInfo cinfo = {
554,634,459✔
4410
        .dataType = colData->type,
554,628,956✔
4411
        .cmprAlg = info->cmprAlg,
554,632,606✔
4412
        .originalSize = info->dataOriginalSize,
554,628,502✔
4413
    };
4414

4415
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
554,609,250✔
4416
    if (code) {
554,593,615✔
4417
      tBufferDestroy(&local);
4418
      return code;
×
4419
    }
4420

4421
    info->dataCompressedSize = cinfo.compressedSize;
554,593,615✔
4422
  }
4423

4424
  tBufferDestroy(&local);
4425
  return 0;
554,605,535✔
4426
}
4427

4428
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
954,510,888✔
4429
  int32_t  code;
4430
  SBuffer  local;
954,505,743✔
4431
  uint8_t *data = (uint8_t *)input;
954,583,044✔
4432

4433
  tBufferInit(&local);
4434
  if (assist == NULL) {
954,597,098✔
4435
    assist = &local;
×
4436
  }
4437

4438
  tColDataClear(colData);
954,597,098✔
4439
  colData->cid = info->columnId;
954,572,519✔
4440
  colData->type = info->dataType;
954,605,323✔
4441
  colData->cflag = info->columnFlag;
954,581,084✔
4442
  colData->nVal = info->numOfData;
954,596,995✔
4443
  colData->flag = info->flag;
954,527,897✔
4444

4445
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
954,568,425✔
4446
    goto _exit;
83,525,157✔
4447
  }
4448

4449
  // bitmap
4450
  if (info->bitmapOriginalSize > 0) {
871,039,153✔
4451
    SCompressInfo cinfo = {
135,869,663✔
4452
        .dataType = TSDB_DATA_TYPE_TINYINT,
4453
        .cmprAlg = info->cmprAlg,
135,871,139✔
4454
        .originalSize = info->bitmapOriginalSize,
135,867,406✔
4455
        .compressedSize = info->bitmapCompressedSize,
135,853,598✔
4456
    };
4457

4458
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
135,867,620✔
4459
    if (code) {
135,837,458✔
4460
      tBufferDestroy(&local);
4461
      return code;
×
4462
    }
4463

4464
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
135,837,458✔
4465
    if (code) {
135,839,610✔
4466
      tBufferDestroy(&local);
4467
      return code;
×
4468
    }
4469

4470
    data += cinfo.compressedSize;
135,839,610✔
4471
  }
4472

4473
  if (info->flag == (HAS_NONE | HAS_NULL)) {
871,051,101✔
4474
    goto _exit;
191,799✔
4475
  }
4476

4477
  // offset
4478
  if (info->offsetOriginalSize > 0) {
870,838,157✔
4479
    SCompressInfo cinfo = {
188,085,425✔
4480
        .cmprAlg = info->cmprAlg,
188,074,116✔
4481
        .dataType = TSDB_DATA_TYPE_INT,
4482
        .originalSize = info->offsetOriginalSize,
188,087,636✔
4483
        .compressedSize = info->offsetCompressedSize,
188,084,019✔
4484
    };
4485

4486
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
188,085,419✔
4487
    if (code) {
188,073,943✔
4488
      tBufferDestroy(&local);
4489
      return code;
×
4490
    }
4491

4492
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
188,073,943✔
4493
    if (code) {
188,076,392✔
4494
      tBufferDestroy(&local);
4495
      return code;
×
4496
    }
4497

4498
    data += cinfo.compressedSize;
188,076,392✔
4499
  }
4500

4501
  // data
4502
  if (info->dataOriginalSize > 0) {
870,821,461✔
4503
    colData->nData = info->dataOriginalSize;
870,813,867✔
4504

4505
    SCompressInfo cinfo = {
870,828,327✔
4506
        .cmprAlg = info->cmprAlg,
1,741,644,036✔
4507
        .dataType = colData->type,
870,817,367✔
4508
        .originalSize = info->dataOriginalSize,
870,822,526✔
4509
        .compressedSize = info->dataCompressedSize,
870,836,708✔
4510
    };
4511

4512
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
870,835,061✔
4513
    if (code) {
870,685,019✔
4514
      tBufferDestroy(&local);
4515
      return code;
×
4516
    }
4517

4518
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
870,685,019✔
4519
    if (code) {
870,795,242✔
4520
      tBufferDestroy(&local);
4521
      return code;
×
4522
    }
4523

4524
    data += cinfo.compressedSize;
870,795,242✔
4525
  }
4526

4527
_exit:
954,605,730✔
4528
  switch (colData->flag) {
954,599,093✔
4529
    case HAS_NONE:
45,913,250✔
4530
      colData->numOfNone = colData->nVal;
45,913,250✔
4531
      break;
45,913,250✔
4532
    case HAS_NULL:
37,594,214✔
4533
      colData->numOfNull = colData->nVal;
37,594,214✔
4534
      break;
37,610,379✔
4535
    case HAS_VALUE:
735,144,544✔
4536
      colData->numOfValue = colData->nVal;
735,144,544✔
4537
      break;
735,206,298✔
4538
    default:
135,863,489✔
4539
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4540
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4541
        if (bitValue == 0) {
2,147,483,647✔
4542
          colData->numOfNone++;
474,150,499✔
4543
        } else if (bitValue == 1) {
2,147,483,647✔
4544
          colData->numOfNull++;
2,147,483,647✔
4545
        } else {
4546
          colData->numOfValue++;
2,147,483,647✔
4547
        }
4548
      }
4549
  }
4550
  tBufferDestroy(&local);
4551
  return 0;
954,529,151✔
4552
}
4553

4554
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
237,956✔
4555
                                    char *data) {
4556
  int32_t code = 0;
237,956✔
4557
  if (data == NULL) {
237,956✔
4558
    if (pColData->cflag & COL_IS_KEY) {
6,520✔
4559
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4560
    } else {
4561
      for (int32_t i = 0; i < nRows; ++i) {
14,346✔
4562
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
7,826✔
4563
      }
4564
    }
4565
    goto _exit;
6,520✔
4566
  }
4567

4568
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
231,436✔
4569
    if (!IS_STR_DATA_BLOB(type)) {
44,391✔
4570
      for (int32_t i = 0; i < nRows; ++i) {
117,724✔
4571
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
73,333✔
4572
        if (offset == -1) {
73,333✔
4573
          if (pColData->cflag & COL_IS_KEY) {
×
4574
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4575
            goto _exit;
×
4576
          }
4577
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4578
            goto _exit;
×
4579
          }
4580
        } else {
4581
          if (varDataTLen(data + offset) > bytes || (type == TSDB_DATA_TYPE_NCHAR && varDataLen(data + offset) % TSDB_NCHAR_SIZE != 0)) {
73,333✔
4582
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4583
                   bytes);
4584
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4585
            goto _exit;
×
4586
          }
4587
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
146,666✔
4588
                                                                        varDataLen(data + offset));
73,333✔
4589
        }
4590
      }
4591
    } else {
4592
      for (int32_t i = 0; i < nRows; ++i) {
×
4593
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4594
        if (offset == -1) {
×
4595
          if (pColData->cflag & COL_IS_KEY) {
×
4596
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4597
            goto _exit;
×
4598
          }
4599
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4600
            goto _exit;
×
4601
          }
4602
        } else {
4603
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4604
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4605
                   (int)blobDataTLen(data + offset), bytes);
4606
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4607
            goto _exit;
×
4608
          }
4609
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4610
                                                                        blobDataLen(data + offset));
×
4611
        }
4612
      }
4613
    }
4614
  } else {  // fixed-length data type
4615
    bool allValue = true;
187,045✔
4616
    bool allNull = true;
187,045✔
4617
    for (int32_t i = 0; i < nRows; ++i) {
507,908✔
4618
      if (!BMIsNull(lengthOrbitmap, i)) {
320,863✔
4619
        allNull = false;
243,187✔
4620
      } else {
4621
        allValue = false;
77,676✔
4622
      }
4623
    }
4624
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
187,045✔
4625
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4626
      goto _exit;
×
4627
    }
4628

4629
    if (allValue) {
187,045✔
4630
      // optimize (todo)
4631
      for (int32_t i = 0; i < nRows; ++i) {
390,936✔
4632
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
238,646✔
4633
      }
4634
    } else if (allNull) {
34,755✔
4635
      // optimize (todo)
4636
      for (int32_t i = 0; i < nRows; ++i) {
93,564✔
4637
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
62,058✔
4638
        if (code) goto _exit;
62,058✔
4639
      }
4640
    } else {
4641
      for (int32_t i = 0; i < nRows; ++i) {
23,408✔
4642
        if (BMIsNull(lengthOrbitmap, i)) {
20,159✔
4643
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
15,618✔
4644
          if (code) goto _exit;
15,618✔
4645
        } else {
4646
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
4,541✔
4647
        }
4648
      }
4649
    }
4650
  }
4651

4652
_exit:
3,249✔
4653
  return code;
237,956✔
4654
}
4655
int32_t tColDataAddValueByDataBlockWithBlob(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows,
×
4656
                                            char *lengthOrbitmap, char *data, void *pBlobSet) {
4657
  int32_t code = 0;
×
4658
  if (data == NULL) {
×
4659
    if (pColData->cflag & COL_IS_KEY) {
×
4660
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4661
    } else {
4662
      for (int32_t i = 0; i < nRows; ++i) {
×
4663
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4664
      }
4665
    }
4666
    goto _exit;
×
4667
  }
4668

4669
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
×
4670
    if (!IS_STR_DATA_BLOB(type)) {
×
4671
      for (int32_t i = 0; i < nRows; ++i) {
×
4672
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4673
        if (offset == -1) {
×
4674
          if (pColData->cflag & COL_IS_KEY) {
×
4675
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4676
            goto _exit;
×
4677
          }
4678
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4679
            goto _exit;
×
4680
          }
4681
        } else {
4682
          if (varDataTLen(data + offset) > bytes) {
×
4683
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4684
                   bytes);
4685
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4686
            goto _exit;
×
4687
          }
4688
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
×
4689
                                                                        varDataLen(data + offset));
×
4690
        }
4691
      }
4692
    } else {
4693
      for (int32_t i = 0; i < nRows; ++i) {
×
4694
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4695
        if (offset == -1) {
×
4696
          if (pColData->cflag & COL_IS_KEY) {
×
4697
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4698
            goto _exit;
×
4699
          }
4700
          if ((code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0))) {
×
4701
            goto _exit;
×
4702
          }
4703
        } else {
4704
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4705
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4706
                   (int)blobDataTLen(data + offset), bytes);
4707
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4708
            goto _exit;
×
4709
          }
4710
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](
×
4711
              pBlobSet, pColData, (uint8_t *)blobDataVal(data + offset), blobDataLen(data + offset));
×
4712
        }
4713
      }
4714
    }
4715
  } else {  // fixed-length data type
4716
    bool allValue = true;
×
4717
    bool allNull = true;
×
4718
    for (int32_t i = 0; i < nRows; ++i) {
×
4719
      if (!BMIsNull(lengthOrbitmap, i)) {
×
4720
        allNull = false;
×
4721
      } else {
4722
        allValue = false;
×
4723
      }
4724
    }
4725
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
×
4726
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4727
      goto _exit;
×
4728
    }
4729

4730
    if (allValue) {
×
4731
      // optimize (todo)
4732
      for (int32_t i = 0; i < nRows; ++i) {
×
4733
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4734
      }
4735
    } else if (allNull) {
×
4736
      // optimize (todo)
4737
      for (int32_t i = 0; i < nRows; ++i) {
×
4738
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4739
        if (code) goto _exit;
×
4740
      }
4741
    } else {
4742
      for (int32_t i = 0; i < nRows; ++i) {
×
4743
        if (BMIsNull(lengthOrbitmap, i)) {
×
4744
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4745
          if (code) goto _exit;
×
4746
        } else {
4747
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4748
        }
4749
      }
4750
    }
4751
  }
4752

4753
_exit:
×
4754
  return code;
×
4755
}
4756

4757
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
22,248,199✔
4758
                               checkWKBGeometryFn cgeos) {
4759
  int32_t code = 0;
22,248,199✔
4760

4761
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
22,248,199✔
4762
    if (!(pColData->type == pBind->buffer_type)) {
22,249,618✔
4763
      return TSDB_CODE_INVALID_PARA;
×
4764
    }
4765
  }
4766

4767
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
22,253,674✔
4768
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
28,592✔
4769
      code = igeos();
1,706✔
4770
      if (code) {
1,706✔
4771
        return code;
×
4772
      }
4773
    }
4774
    for (int32_t i = 0; i < pBind->num; ++i) {
1,372,530✔
4775
      if (pBind->is_null && pBind->is_null[i]) {
1,352,178✔
4776
        if (pColData->cflag & COL_IS_KEY) {
659,316✔
4777
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,396✔
4778
          goto _exit;
1,396✔
4779
        }
4780
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
657,920✔
4781
        if (code) goto _exit;
657,920✔
4782
      } else if (pBind->length[i] > buffMaxLen) {
692,862✔
4783
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4784
      } else {
4785
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
692,862✔
4786
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
3,010✔
4787
          if (code) {
3,010✔
4788
            uError("stmt col[%d] bind geometry wrong format", i);
202✔
4789
            goto _exit;
202✔
4790
          }
4791
        }
4792
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,366,460✔
4793
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
692,660✔
4794
      }
4795
    }
4796
  } else {  // fixed-length data type
4797
    bool allValue;
4798
    bool allNull;
4799
    if (pBind->is_null) {
22,225,190✔
4800
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
14,823,250✔
4801
      allNull = (same && pBind->is_null[0] != 0);
14,819,929✔
4802
      allValue = (same && pBind->is_null[0] == 0);
14,818,529✔
4803
    } else {
4804
      allNull = false;
7,404,507✔
4805
      allValue = true;
7,404,507✔
4806
    }
4807

4808
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
22,225,226✔
4809
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,792✔
4810
      goto _exit;
2,792✔
4811
    }
4812

4813
    if (allValue) {
22,220,998✔
4814
      // optimize (todo)
4815
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4816
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4817
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4818
      }
4819
    } else if (allNull) {
25,674✔
4820
      // optimize (todo)
4821
      for (int32_t i = 0; i < pBind->num; ++i) {
1,280✔
4822
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
640✔
4823
        if (code) goto _exit;
640✔
4824
      }
4825
    } else {
4826
      for (int32_t i = 0; i < pBind->num; ++i) {
8,569,182✔
4827
        if (pBind->is_null[i]) {
8,544,148✔
4828
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,777,354✔
4829
          if (code) goto _exit;
3,777,354✔
4830
        } else {
4831
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,533,588✔
4832
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,766,794✔
4833
        }
4834
      }
4835
    }
4836
  }
4837

4838
_exit:
9,994✔
4839
  return code;
22,257,659✔
4840
}
4841

4842
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
1,136,194,300✔
4843
  int32_t code = 0;
1,136,194,300✔
4844

4845
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,136,194,300✔
4846
    if (!(pColData->type == pBind->buffer_type)) {
1,114,053,653✔
4847
      return TSDB_CODE_INVALID_PARA;
×
4848
    }
4849
  }
4850

4851
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
1,288,310,236✔
4852
    uint8_t *buf = pBind->buffer;
131,062,223✔
4853
    for (int32_t i = 0; i < pBind->num; ++i) {
257,469,976✔
4854
      if (pBind->is_null && pBind->is_null[i]) {
129,232,189✔
4855
        if (pColData->cflag & COL_IS_KEY) {
6,352,912✔
4856
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4857
          goto _exit;
×
4858
        }
4859
        if (pBind->is_null[i] == 1) {
6,353,115✔
4860
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
6,350,378✔
4861
          if (code) goto _exit;
6,351,287✔
4862
        } else {
4863
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4864
          if (code) goto _exit;
×
4865
        }
4866
      } else if (pBind->length[i] > buffMaxLen) {
122,800,221✔
4867
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4868
      } else {
4869
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
122,929,029✔
4870
        buf += pBind->length[i];
122,569,761✔
4871
      }
4872
    }
4873
  } else {  // fixed-length data type
4874
    bool allValue;
4875
    bool allNull;
4876
    bool allNone;
4877
    if (pBind->is_null) {
1,042,784,664✔
4878
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
31,659,390✔
4879
      allNull = (same && pBind->is_null[0] == 1);
31,690,253✔
4880
      allNone = (same && pBind->is_null[0] > 1);
31,673,372✔
4881
      allValue = (same && pBind->is_null[0] == 0);
31,679,956✔
4882
    } else {
4883
      allNull = false;
995,632,065✔
4884
      allNone = false;
995,632,065✔
4885
      allValue = true;
995,632,065✔
4886
    }
4887

4888
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
1,027,288,172✔
4889
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4890
      goto _exit;
×
4891
    }
4892

4893
    uint8_t *buf = pBind->buffer;
1,033,945,658✔
4894

4895
    if (allValue) {
1,037,014,626✔
4896
      // optimize (todo)
4897
      for (int32_t i = 0; i < pBind->num; ++i) {
1,992,535,636✔
4898
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
1,001,448,096✔
4899
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
1,010,337,695✔
4900
          *val = 1;
×
4901
        }
4902
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
1,005,229,038✔
4903
      }
4904
    } else if (allNull) {
32,704,566✔
4905
      // optimize (todo)
4906
      for (int32_t i = 0; i < pBind->num; ++i) {
63,343,361✔
4907
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
31,669,650✔
4908
        if (code) goto _exit;
31,671,895✔
4909
      }
4910
    } else if (allNone) {
1,033,100✔
4911
      // optimize (todo)
4912
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4913
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4914
        if (code) goto _exit;
×
4915
      }
4916
    } else {
4917
      for (int32_t i = 0; i < pBind->num; ++i) {
1,033,100✔
4918
        if (pBind->is_null[i]) {
×
4919
          if (pBind->is_null[i] == 1) {
×
4920
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4921
            if (code) goto _exit;
×
4922
          } else {
4923
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4924
            if (code) goto _exit;
×
4925
          }
4926
        } else {
4927
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
4928
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
4929
            *val = 1;
×
4930
          }
4931

4932
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
×
4933
        }
4934
      }
4935
    }
4936
  }
4937

4938
_exit:
21,604,347✔
4939
  return code;
1,146,515,195✔
4940
}
4941

4942
int32_t tColDataAddValueByBind2WithGeos(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
1,067,280✔
4943
                                        initGeosFn igeos, checkWKBGeometryFn cgeos) {
4944
  int32_t code = 0;
1,067,280✔
4945

4946
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,067,280✔
4947
    if (!(pColData->type == pBind->buffer_type)) {
1,081,716✔
4948
      return TSDB_CODE_INVALID_PARA;
×
4949
    }
4950
  }
4951

4952
  if (pColData->type != TSDB_DATA_TYPE_GEOMETRY) {
1,082,076✔
4953
    return TSDB_CODE_INVALID_OPTION;
×
4954
  }
4955

4956
  code = igeos();
1,081,536✔
4957
  if (code) {
1,079,556✔
4958
    return code;
×
4959
  }
4960

4961
  uint8_t *buf = pBind->buffer;
1,079,556✔
4962
  for (int32_t i = 0; i < pBind->num; ++i) {
2,162,880✔
4963
    if (pBind->is_null && pBind->is_null[i]) {
1,081,452✔
4964
      if (pColData->cflag & COL_IS_KEY) {
428✔
4965
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4966
        goto _exit;
×
4967
      }
4968
      if (pBind->is_null[i] == 1) {
428✔
4969
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
428✔
4970
        if (code) goto _exit;
428✔
4971
      } else {
4972
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4973
        if (code) goto _exit;
×
4974
      }
4975
    } else if (pBind->length[i] > buffMaxLen) {
1,081,600✔
4976
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4977
    } else {
4978
      code = cgeos(buf, pBind->length[i]);
1,081,780✔
4979
      if (code) {
1,077,712✔
4980
        uError("stmt2 col[%d] bind geometry with wrong format", i);
×
4981
        goto _exit;
×
4982
      }
4983
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
1,077,712✔
4984
      buf += pBind->length[i];
1,081,492✔
4985
    }
4986
  }
4987

4988
_exit:
1,081,536✔
4989
  return code;
1,081,536✔
4990
}
4991

4992
int32_t tColDataAddValueByBind2WithBlob(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
243✔
4993
                                        SBlobSet *pBlobSet) {
4994
  int32_t code = 0;
243✔
4995

4996
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
243✔
4997
    if (!(pColData->type == pBind->buffer_type)) {
119✔
4998
      return TSDB_CODE_INVALID_PARA;
×
4999
    }
5000
  }
5001

5002
  if (IS_STR_DATA_BLOB(pColData->type)) {  // var-length data type
243✔
5003
    uint8_t *buf = pBind->buffer;
243✔
5004
    for (int32_t i = 0; i < pBind->num; ++i) {
486✔
5005
      if (pBind->is_null && pBind->is_null[i]) {
243✔
5006
        if (pColData->cflag & COL_IS_KEY) {
124✔
5007
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5008
          goto _exit;
×
5009
        }
5010
        if (pBind->is_null[i] == 1) {
124✔
5011
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0);
124✔
5012
          if (code) goto _exit;
124✔
5013
        } else {
5014
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NONE](pBlobSet, pColData, NULL, 0);
×
5015
          if (code) goto _exit;
×
5016
        }
5017
      } else if (pBind->length[i] > buffMaxLen) {
119✔
5018
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5019
      } else {
5020
        code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](pBlobSet, pColData, buf, pBind->length[i]);
119✔
5021
        buf += pBind->length[i];
119✔
5022
      }
5023
    }
5024
  }
5025
_exit:
243✔
5026
  return code;
243✔
5027
}
5028

5029
int32_t tColDataAddValueByBind2WithDecimal(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
1,853✔
5030
                                           uint8_t precision, uint8_t scale) {
5031
  int32_t code = 0;
1,853✔
5032

5033
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,853✔
5034
    if (!(pColData->type == pBind->buffer_type)) {
1,297✔
5035
      return TSDB_CODE_INVALID_PARA;
×
5036
    }
5037
  }
5038

5039
  if (!IS_DECIMAL_TYPE(pColData->type)) {
1,853✔
5040
    return TSDB_CODE_INVALID_OPTION;
×
5041
  }
5042

5043
  uint8_t *buf = pBind->buffer;
1,853✔
5044
  for (int32_t i = 0; i < pBind->num; ++i) {
4,882✔
5045
    if (pBind->is_null && pBind->is_null[i]) {
3,029✔
5046
      if (pColData->cflag & COL_IS_KEY) {
556✔
5047
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5048
        goto _exit;
×
5049
      }
5050
      if (pBind->is_null[i] == 1) {
556✔
5051
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
164✔
5052
        if (code) goto _exit;
164✔
5053
      } else {
5054
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
392✔
5055
        if (code) goto _exit;
392✔
5056
      }
5057
    } else {
5058
      if (pColData->type == TSDB_DATA_TYPE_DECIMAL64) {
2,473✔
5059
        Decimal64 dec = {0};
1,215✔
5060
        int32_t   code = decimal64FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
1,215✔
5061
        buf += pBind->length[i];
1,215✔
5062
        if (TSDB_CODE_SUCCESS != code) {
1,215✔
5063
          return code;
×
5064
        }
5065
        int64_t tmp = DECIMAL64_GET_VALUE(&dec);
1,215✔
5066
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)&tmp,
1,215✔
5067
                                                                      TYPE_BYTES[pColData->type]);
1,215✔
5068
      } else if (pColData->type == TSDB_DATA_TYPE_DECIMAL) {
1,258✔
5069
        Decimal128 dec = {0};
1,258✔
5070
        int32_t    code = decimal128FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
1,258✔
5071
        buf += pBind->length[i];
1,258✔
5072
        if (TSDB_CODE_SUCCESS != code) {
1,258✔
5073
          return code;
×
5074
        }
5075
        uint8_t *pV = taosMemCalloc(1, sizeof(Decimal128));
1,258✔
5076
        if (!pV) return terrno;
1,258✔
5077
        memcpy(pV, &dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
1,258✔
5078
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pV, TYPE_BYTES[pColData->type]);
1,258✔
5079
        taosMemoryFree(pV);
1,258✔
5080
      }
5081
    }
5082
  }
5083

5084
_exit:
1,853✔
5085
  return code;
1,853✔
5086
}
5087
/* build rows to `rowArray` from bind
5088
 * `infos` is the bind information array
5089
 * `numOfInfos` is the number of bind information
5090
 * `infoSorted` is whether the bind information is sorted by column id
5091
 * `pTSchema` is the schema of the table
5092
 * `rowArray` is the array to store the rows
5093
 * `pOrdered` is the pointer to store ordered
5094
 * `pDupTs` is the pointer to store duplicateTs
5095
 */
5096
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
1,160,553✔
5097
                           const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray, bool *pOrdered,
5098
                           bool *pDupTs) {
5099
  if (infos == NULL || numOfInfos <= 0 || pTSchema == NULL || numOfInfos > pTSchema->numOfCols || rowArray == NULL) {
1,160,553✔
5100
    return TSDB_CODE_INVALID_PARA;
×
5101
  }
5102
  int8_t hasBlob = schemaHasBlob(pTSchema);
1,161,179✔
5103
  if (!infoSorted) {
1,161,067✔
5104
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
1,372✔
5105
  }
5106

5107
  int32_t code = 0;
1,161,067✔
5108
  int32_t numOfRows = -1;
1,161,067✔
5109
  SArray *colValArray, *bufArray;
5110
  SColVal colVal;
322,697✔
5111
  int32_t numOfFixedValue = 0;
1,161,025✔
5112
  int32_t lino = 0;
1,161,025✔
5113
  bool    hasDecimal128 = false;
1,161,025✔
5114

5115
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,161,025✔
5116
    return terrno;
×
5117
  }
5118
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
1,161,005✔
5119
    taosArrayDestroy(colValArray);
×
5120
    return terrno;
×
5121
  }
5122
  for (int i = 0; i < numOfInfos; ++i) {
10,021,957✔
5123
    if (parsedCols) {
8,859,775✔
5124
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
28,224✔
5125
      if (pParsedVal) {
28,224✔
5126
        continue;
5,880✔
5127
      }
5128
    }
5129
    if (numOfRows == -1) {
8,853,895✔
5130
      numOfRows = infos[i].bind->num;
1,160,951✔
5131
    }
5132

5133
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
17,709,105✔
5134
      taosArrayDestroy(colValArray);
×
5135
      taosArrayDestroy(bufArray);
×
5136
      return terrno;
×
5137
    }
5138
  }
5139

5140
  SRowKey rowKey, lastRowKey;
323,630✔
5141
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
14,539,999✔
5142
    taosArrayClear(colValArray);
13,385,213✔
5143
    numOfFixedValue = 0;
13,384,797✔
5144

5145
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
74,359,817✔
5146
      if (parsedCols) {
60,890,725✔
5147
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
30,576✔
5148
        if (pParsedVal) {
30,576✔
5149
          numOfFixedValue++;
19,363✔
5150
          colVal = *pParsedVal;
19,363✔
5151

5152
          if (taosArrayPush(colValArray, &colVal) == NULL) {
7,056✔
5153
            code = terrno;
×
5154
            TAOS_CHECK_GOTO(code, &lino, _exit);
392✔
5155
          }
5156
          continue;
7,056✔
5157
        }
5158
      }
5159

5160
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
60,871,362✔
5161
        if (infos[iInfo].bind->is_null[iRow] == 1) {
980✔
5162
          if (iInfo == 0) {
588✔
5163
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
196✔
5164
            TAOS_CHECK_GOTO(code, &lino, _exit);
196✔
5165
          }
5166
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
392✔
5167
        } else {
5168
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
392✔
5169
        }
5170
      } else {
5171
        SValue value = {
60,879,679✔
5172
            .type = infos[iInfo].type,
60,877,293✔
5173
        };
5174
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
60,886,001✔
5175
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
3,801,377✔
UNCOV
5176
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5177
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5178
            value.nData = length;
×
5179
            if (value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE) > TSDB_MAX_BLOB_LEN) {
×
5180
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5181
              uError("stmt2 bind col:%d, row:%d length:%d  greater than blob maximum length: %d", iInfo, iRow,
×
5182
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), TSDB_MAX_BLOB_LEN);
5183
              goto _exit;
196✔
5184
            }
5185
            value.pData = *data;
×
5186
            *data += length;
×
5187
          } else {
5188
            int32_t   length = infos[iInfo].bind->length[iRow];
3,888,782✔
5189
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
3,889,200✔
5190
            value.nData = length;
3,889,762✔
5191
            if (value.nData + VARSTR_HEADER_SIZE > infos[iInfo].bytes) {
3,889,762✔
5192
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5193
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum length: %d", iInfo, iRow,
×
5194
                     value.nData + (uint32_t)(VARSTR_HEADER_SIZE), infos[iInfo].bytes);
5195
              goto _exit;
×
5196
            }
5197
            value.pData = *data;
3,889,436✔
5198
            *data += length;
3,888,772✔
5199
          }
5200
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5201
        } else {
5202
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
57,035,842✔
5203
            if (!pSchemaExt) {
784✔
5204
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5205
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5206
              goto _exit;
×
5207
            }
5208
            uint8_t precision = 0, scale = 0;
784✔
5209
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
784✔
5210
            Decimal128 dec = {0};
784✔
5211
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
784✔
5212
            int32_t    length = infos[iInfo].bind->length[iRow];
784✔
5213
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
784✔
5214
            *data += length;
784✔
5215
            hasDecimal128 = true;
784✔
5216
            TAOS_CHECK_GOTO(code, &lino, _exit);
784✔
5217

5218
            // precision check
5219
            // scale auto fit
5220

5221
            code = decimal128ToDataVal(&dec, &value);
784✔
5222
            TAOS_CHECK_GOTO(code, &lino, _exit);
784✔
5223

5224
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
57,039,843✔
5225
            if (!pSchemaExt) {
980✔
5226
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5227
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5228
              goto _exit;
196✔
5229
            }
5230
            uint8_t precision = 0, scale = 0;
980✔
5231
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
980✔
5232
            Decimal64 dec = {0};
980✔
5233
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
980✔
5234
            int32_t   length = infos[iInfo].bind->length[iRow];
980✔
5235
            code = decimal64FromStr(*(char **)data, length, precision, scale, &dec);
980✔
5236
            *data += length;
980✔
5237
            TAOS_CHECK_GOTO(code, &lino, _exit);
980✔
5238

5239
            code = decimal64ToDataVal(&dec, &value);
784✔
5240
            TAOS_CHECK_GOTO(code, &lino, _exit);
784✔
5241

5242
          } else {
5243
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
57,035,785✔
5244
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
57,033,160✔
5245
              *val = 1;
×
5246
            }
5247
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
57,033,160✔
5248
          }
5249
        }
5250
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
60,998,630✔
5251
      }
5252
      if (taosArrayPush(colValArray, &colVal) == NULL) {
60,967,964✔
5253
        code = terrno;
×
5254
        goto _exit;
×
5255
      }
5256
    }
5257

5258
    SRow *row;
2,241,128✔
5259

5260
    if (hasBlob == 0) {
13,439,869✔
5261
      SRowBuildScanInfo sinfo = {0};
13,378,977✔
5262
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
13,378,060✔
5263
      TAOS_CHECK_GOTO(code, &lino, _exit);
13,366,783✔
5264
    } else {
5265
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
61,556✔
5266
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
61,556✔
5267
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5268
    }
5269

5270
    if ((taosArrayPush(rowArray, &row)) == NULL) {
13,362,054✔
5271
      code = terrno;
×
5272
      goto _exit;
×
5273
    }
5274

5275
    // fix decimal memory leak
5276
    if (hasDecimal128) {
13,362,054✔
5277
      int32_t num = taosArrayGetSize(colValArray);
784✔
5278
      for (int32_t i = 0; i < num; ++i) {
3,136✔
5279
        SColVal *pCol = taosArrayGet(colValArray, i);
2,352✔
5280
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
2,352✔
5281
          taosMemoryFreeClear(pCol->value.pData);
784✔
5282
        }
5283
      }
5284
      hasDecimal128 = false;
784✔
5285
    }
5286

5287
    if (pOrdered && pDupTs) {
13,362,054✔
5288
      tRowGetKey(row, &rowKey);
26,743,345✔
5289
      if (iRow == 0) {
13,380,746✔
5290
        *pOrdered = true;
1,160,321✔
5291
        *pDupTs = false;
1,160,321✔
5292
      } else {
5293
        if (*pOrdered) {
12,220,425✔
5294
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
12,216,512✔
5295
          *pOrdered = (res >= 0);
12,216,512✔
5296
          if (!*pDupTs) {
12,218,092✔
5297
            *pDupTs = (res == 0);
12,217,242✔
5298
          }
5299
        }
5300
      }
5301
      lastRowKey = rowKey;
13,378,994✔
5302
    }
5303
  }
5304
_exit:
1,155,047✔
5305
  if (code != 0) {
1,155,094✔
5306
    if (hasDecimal128) {
392✔
5307
      int32_t num = taosArrayGetSize(colValArray);
×
5308
      for (int32_t i = 0; i < num; ++i) {
×
5309
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5310
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5311
          taosMemoryFreeClear(pCol->value.pData);
×
5312
        }
5313
      }
5314
    }
5315
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
392✔
5316
  }
5317
  taosArrayDestroy(colValArray);
1,155,094✔
5318
  taosArrayDestroy(bufArray);
1,161,179✔
5319
  return code;
1,161,627✔
5320
}
5321
/* build rows to `rowArray` from bind
5322
 * `infos` is the bind information array
5323
 * `numOfInfos` is the number of bind information
5324
 * `infoSorted` is whether the bind information is sorted by column id
5325
 * `pTSchema` is the schema of the table
5326
 * `rowArray` is the array to store the rows
5327
 * `pOrdered` is the pointer to store ordered
5328
 * `pDupTs` is the pointer to store duplicateTs
5329
 */
5330
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
196✔
5331
                                   const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray,
5332
                                   bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5333
  if (infos == NULL || numOfInfos <= 0 || pTSchema == NULL || numOfInfos > pTSchema->numOfCols || rowArray == NULL) {
196✔
5334
    return TSDB_CODE_INVALID_PARA;
×
5335
  }
5336
  int8_t hasBlob = schemaHasBlob(pTSchema);
196✔
5337
  if (!infoSorted) {
196✔
5338
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5339
  }
5340

5341
  int32_t code = 0;
196✔
5342
  int32_t numOfRows = -1;
196✔
5343
  SArray *colValArray, *bufArray;
5344
  SColVal colVal;
×
5345
  int32_t numOfFixedValue = 0;
196✔
5346
  int32_t lino = 0;
196✔
5347
  bool    hasDecimal128 = false;
196✔
5348

5349
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
196✔
5350
    return terrno;
×
5351
  }
5352
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
196✔
5353
    taosArrayDestroy(colValArray);
×
5354
    return terrno;
×
5355
  }
5356
  for (int i = 0; i < numOfInfos; ++i) {
784✔
5357
    if (parsedCols) {
588✔
5358
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
×
5359
      if (pParsedVal) {
×
5360
        continue;
×
5361
      }
5362
    }
5363
    if (numOfRows == -1) {
588✔
5364
      numOfRows = infos[i].bind->num;
196✔
5365
    }
5366

5367
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
1,176✔
5368
      taosArrayDestroy(colValArray);
×
5369
      taosArrayDestroy(bufArray);
×
5370
      return terrno;
×
5371
    }
5372
  }
5373

5374
  SRowKey rowKey, lastRowKey;
×
5375
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
784✔
5376
    taosArrayClear(colValArray);
588✔
5377
    numOfFixedValue = 0;
588✔
5378

5379
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
2,352✔
5380
      if (parsedCols) {
1,764✔
5381
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
×
5382
        if (pParsedVal) {
×
5383
          numOfFixedValue++;
×
5384
          colVal = *pParsedVal;
×
5385

5386
          if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5387
            code = terrno;
×
5388
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5389
          }
5390
          continue;
×
5391
        }
5392
      }
5393

5394
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
1,764✔
5395
        if (infos[iInfo].bind->is_null[iRow] == 1) {
392✔
5396
          if (iInfo == 0) {
392✔
5397
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5398
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5399
          }
5400
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
392✔
5401
        } else {
5402
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5403
        }
5404
      } else {
5405
        SValue value = {
1,372✔
5406
            .type = infos[iInfo].type,
1,372✔
5407
        };
5408
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
1,372✔
5409
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
392✔
5410
            int32_t   length = infos[iInfo].bind->length[iRow];
392✔
5411
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
392✔
5412
            value.nData = length;
392✔
5413
            if (value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE) > TSDB_MAX_BLOB_LEN) {
392✔
5414
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5415
              uError("stmt2 bind col:%d, row:%d length:%d  greater than blob maximum length: %d", iInfo, iRow,
×
5416
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), TSDB_MAX_BLOB_LEN);
5417
              goto _exit;
×
5418
            }
5419
            value.pData = *data;
392✔
5420
            *data += length;
392✔
5421
          } else {
5422
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5423
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5424
            value.nData = length;
×
5425
            if (value.nData + VARSTR_HEADER_SIZE > infos[iInfo].bytes) {
×
5426
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5427
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum length: %d", iInfo, iRow,
×
5428
                     value.nData + (uint32_t)(VARSTR_HEADER_SIZE), infos[iInfo].bytes);
5429
              goto _exit;
×
5430
            }
5431
            value.pData = *data;
×
5432
            *data += length;
×
5433
          }
5434
        } else {
5435
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
980✔
5436
            if (!pSchemaExt) {
392✔
5437
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5438
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5439
              goto _exit;
×
5440
            }
5441
            uint8_t precision = 0, scale = 0;
392✔
5442
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
392✔
5443
            Decimal128 dec = {0};
392✔
5444
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
392✔
5445
            int32_t    length = infos[iInfo].bind->length[iRow];
392✔
5446
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
392✔
5447
            *data += length;
392✔
5448
            hasDecimal128 = true;
392✔
5449
            TAOS_CHECK_GOTO(code, &lino, _exit);
392✔
5450

5451
            code = decimal128ToDataVal(&dec, &value);
392✔
5452
            TAOS_CHECK_GOTO(code, &lino, _exit);
392✔
5453

5454
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
588✔
5455
            if (!pSchemaExt) {
×
5456
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5457
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5458
              goto _exit;
×
5459
            }
5460
            uint8_t precision = 0, scale = 0;
×
5461
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5462
            Decimal64 dec = {0};
×
5463
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5464
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5465
            code = decimal64FromStr(*(char **)data, length, precision, scale, &dec);
×
5466
            *data += length;
×
5467
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5468

5469
            code = decimal64ToDataVal(&dec, &value);
×
5470
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5471

5472
          } else {
5473
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
588✔
5474
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
588✔
5475
              *val = 1;
×
5476
            }
5477
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
588✔
5478
          }
5479
        }
5480
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
1,372✔
5481
      }
5482
      if (taosArrayPush(colValArray, &colVal) == NULL) {
1,764✔
5483
        code = terrno;
×
5484
        goto _exit;
×
5485
      }
5486
    }
5487

5488
    SRow *row;
×
5489

5490
    if (hasBlob == 0) {
588✔
5491
      SRowBuildScanInfo sinfo = {0};
×
5492
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
×
5493
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5494
    } else {
5495
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
588✔
5496
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, pBlobSet, &sinfo);
588✔
5497
      TAOS_CHECK_GOTO(code, &lino, _exit);
588✔
5498
    }
5499

5500
    if ((taosArrayPush(rowArray, &row)) == NULL) {
588✔
5501
      code = terrno;
×
5502
      goto _exit;
×
5503
    }
5504

5505
    // fix decimal memory leak
5506
    if (hasDecimal128) {
588✔
5507
      int32_t num = taosArrayGetSize(colValArray);
392✔
5508
      for (int32_t i = 0; i < num; ++i) {
1,568✔
5509
        SColVal *pCol = taosArrayGet(colValArray, i);
1,176✔
5510
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
1,176✔
5511
          taosMemoryFreeClear(pCol->value.pData);
392✔
5512
        }
5513
      }
5514
      hasDecimal128 = false;
392✔
5515
    }
5516

5517
    if (pOrdered && pDupTs) {
588✔
5518
      tRowGetKey(row, &rowKey);
1,176✔
5519
      if (iRow == 0) {
588✔
5520
        *pOrdered = true;
196✔
5521
        *pDupTs = false;
196✔
5522
      } else {
5523
        if (*pOrdered) {
392✔
5524
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
392✔
5525
          *pOrdered = (res >= 0);
392✔
5526
          if (!*pDupTs) {
392✔
5527
            *pDupTs = (res == 0);
392✔
5528
          }
5529
        }
5530
      }
5531
      lastRowKey = rowKey;
588✔
5532
    }
5533
  }
5534
_exit:
196✔
5535
  if (code != 0) {
196✔
5536
    if (hasDecimal128) {
×
5537
      int32_t num = taosArrayGetSize(colValArray);
×
5538
      for (int32_t i = 0; i < num; ++i) {
×
5539
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5540
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5541
          taosMemoryFreeClear(pCol->value.pData);
×
5542
        }
5543
      }
5544
    }
5545
    uError("tRowBuildFromBind2WithBlob failed at line %d, ErrCode=0x%x", lino, code);
×
5546
  }
5547
  taosArrayDestroy(colValArray);
196✔
5548
  taosArrayDestroy(bufArray);
196✔
5549
  return code;
196✔
5550
}
5551

5552
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
45,668,765✔
5553
  int32_t code = TSDB_CODE_SUCCESS;
45,668,765✔
5554

5555
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
45,668,765✔
5556
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,758,630✔
5557
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
5,179,172✔
5558
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
10,937,802✔
5559
    if (iToRow == 0) {
5,758,630✔
5560
      pToColData->aOffset[iToRow] = 0;
16,158✔
5561
    }
5562

5563
    if (iToRow < pToColData->nVal - 1) {
5,758,630✔
5564
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,744,766✔
5565
    }
5566

5567
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,758,630✔
5568
                 nData);
5569
  } else {
5570
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
39,910,135✔
5571
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
39,910,135✔
5572
  }
5573
  return code;
45,668,765✔
5574
}
5575

5576
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
47,309,825✔
5577
                                        int32_t iToRow) {
5578
  int32_t code = TSDB_CODE_SUCCESS;
47,309,825✔
5579
  int     bit_val = 0;
47,309,825✔
5580

5581
  switch (pFromColData->flag) {
47,309,825✔
5582
    case HAS_NONE: {
×
5583
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5584
    } break;
×
5585
    case HAS_NULL: {
1,641,060✔
5586
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,641,060✔
5587
    } break;
1,641,060✔
5588
    case (HAS_NULL | HAS_NONE): {
×
5589
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5590
      if (0 == bit_val)
×
5591
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5592
      else
5593
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5594
    } break;
×
5595
    case HAS_VALUE: {
5,184,276✔
5596
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
5,184,276✔
5597
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
5,184,276✔
5598
    } break;
5,184,276✔
5599
    case (HAS_VALUE | HAS_NONE): {
×
5600
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5601
      if (0 == bit_val)
×
5602
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5603
      else
5604
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5605
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5606
    } break;
×
5607
    case (HAS_VALUE | HAS_NULL): {
40,484,489✔
5608
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
40,484,489✔
5609
      if (0 == bit_val)
40,484,489✔
5610
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
18,277,827✔
5611
      else
5612
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
22,206,662✔
5613
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
40,484,489✔
5614
    } break;
40,484,489✔
5615
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5616
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5617
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5618
    } break;
×
5619
    default:
×
5620
      return -1;
×
5621
  }
5622

5623
  return code;
47,309,825✔
5624
}
5625

5626
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
2,977,200✔
5627
                               int32_t nColData) {
5628
  int32_t code = TSDB_CODE_SUCCESS;
2,977,200✔
5629

5630
  for (int32_t i = 0; i < nColData; i++) {
50,287,025✔
5631
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
47,309,825✔
5632
    if (code != TSDB_CODE_SUCCESS) {
47,309,825✔
5633
      return code;
×
5634
    }
5635
  }
5636

5637
  return code;
2,977,200✔
5638
}
5639

5640
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
2,977,200✔
5641
  int32_t code = TSDB_CODE_SUCCESS;
2,977,200✔
5642

5643
  for (int32_t i = 0; i < nColData; i++) {
50,287,025✔
5644
    SColVal cv = {0};
47,309,825✔
5645
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
47,309,825✔
5646
    if (code != TSDB_CODE_SUCCESS) {
47,309,825✔
5647
      return code;
×
5648
    }
5649
    code = tColDataAppendValue(&aToColData[i], &cv);
47,309,825✔
5650
    if (code != TSDB_CODE_SUCCESS) {
47,309,825✔
5651
      return code;
×
5652
    }
5653
  }
5654

5655
  return code;
2,977,200✔
5656
}
5657

5658
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
2,147,483,647✔
5659
  SColVal cv;
2,147,483,647✔
5660

5661
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
2,147,483,647✔
5662
  key->numOfPKs = 0;
2,147,483,647✔
5663

5664
  for (int i = 1; i < nColData; i++) {
2,147,483,647✔
5665
    if (aColData[i].cflag & COL_IS_KEY) {
2,147,483,647✔
5666
      tColDataGetValue4(&aColData[i], iRow, &cv);
873,534✔
5667
      key->pks[key->numOfPKs++] = cv.value;
873,534✔
5668
    } else {
5669
      break;
2,147,483,647✔
5670
    }
5671
  }
5672
}
2,147,483,647✔
5673

5674
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
335,675✔
5675
  SColData *aDstColData = NULL;
335,675✔
5676
  int32_t   i = start, j = mid + 1, k = 0;
335,675✔
5677
  SRowKey   keyi, keyj;
334,695✔
5678

5679
  if (end > start) {
335,675✔
5680
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
335,675✔
5681
    if (aDstColData == NULL) {
335,675✔
5682
      return terrno;
×
5683
    }
5684
    for (int c = 0; c < nColData; ++c) {
5,605,501✔
5685
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
5,269,826✔
5686
    }
5687
  }
5688

5689
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
335,675✔
5690
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
335,675✔
5691
  while (i <= mid && j <= end) {
1,825,494✔
5692
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,489,819✔
5693
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
11,330✔
5694
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
11,330✔
5695
    } else {
5696
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,478,489✔
5697
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,478,489✔
5698
    }
5699
  }
5700

5701
  while (i <= mid) {
1,814,633✔
5702
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,478,958✔
5703
  }
5704

5705
  while (j <= end) {
344,098✔
5706
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
8,423✔
5707
  }
5708

5709
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,312,875✔
5710
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
2,977,200✔
5711
  }
5712

5713
  if (aDstColData) {
335,675✔
5714
    for (int32_t i = 0; i < nColData; i++) {
5,605,501✔
5715
      tColDataDestroy(&aDstColData[i]);
5,269,826✔
5716
    }
5717
    taosMemoryFree(aDstColData);
335,675✔
5718
  }
5719

5720
  return TSDB_CODE_SUCCESS;
335,675✔
5721
}
5722

5723
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
674,197✔
5724
  int32_t ret = TSDB_CODE_SUCCESS;
674,197✔
5725
  int32_t mid;
5726

5727
  if (start >= end) {
674,197✔
5728
    return TSDB_CODE_SUCCESS;
338,522✔
5729
  }
5730

5731
  mid = (start + end) / 2;
335,675✔
5732

5733
  ret = tColDataMergeSort(aColData, start, mid, nColData);
335,675✔
5734
  if (ret != TSDB_CODE_SUCCESS) {
335,675✔
5735
    return ret;
×
5736
  }
5737

5738
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
335,675✔
5739
  if (ret != TSDB_CODE_SUCCESS) {
335,675✔
5740
    return ret;
×
5741
  }
5742

5743
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
335,675✔
5744
}
5745

5746
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
2,847✔
5747
  int32_t nVal = aColData[0].nVal;
2,847✔
5748

5749
  if (nVal < 2) return TSDB_CODE_SUCCESS;
2,847✔
5750

5751
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
2,847✔
5752
}
5753

5754
static int32_t tColDataMerge(SArray **colArr) {
1,185✔
5755
  int32_t code = 0;
1,185✔
5756
  SArray *src = *colArr;
1,185✔
5757
  SArray *dst = NULL;
1,185✔
5758

5759
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
1,185✔
5760
  if (dst == NULL) {
1,185✔
5761
    return terrno;
×
5762
  }
5763

5764
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
4,544✔
5765
    SColData *srcCol = taosArrayGet(src, i);
3,359✔
5766

5767
    SColData *dstCol = taosArrayReserve(dst, 1);
3,359✔
5768
    if (dstCol == NULL) {
3,359✔
5769
      code = terrno;
×
5770
      goto _exit;
×
5771
    }
5772
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
3,359✔
5773
  }
5774

5775
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
1,185✔
5776
  SRowKey lastKey;
989✔
5777
  for (int32_t i = 0; i < numRows; i++) {
4,339✔
5778
    SRowKey key;
1,978✔
5779
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
3,154✔
5780

5781
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
5,123✔
5782
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
5,132✔
5783
        SColData *srcCol = taosArrayGet(src, j);
3,751✔
5784
        SColData *dstCol = taosArrayGet(dst, j);
3,751✔
5785

5786
        SColVal cv;
2,967✔
5787
        code = tColDataGetValue(srcCol, i, &cv);
3,751✔
5788
        if (code != TSDB_CODE_SUCCESS) {
3,751✔
5789
          goto _exit;
×
5790
        }
5791
        code = tColDataAppendValue(dstCol, &cv);
3,751✔
5792
        if (code) {
3,751✔
5793
          goto _exit;
×
5794
        }
5795
      }
5796
      lastKey = key;
1,381✔
5797
    } else {  // update existing row
5798
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
6,308✔
5799
        SColData *srcCol = taosArrayGet(src, j);
4,535✔
5800
        SColData *dstCol = taosArrayGet(dst, j);
4,535✔
5801

5802
        SColVal cv;
2,967✔
5803
        code = tColDataGetValue(srcCol, i, &cv);
4,535✔
5804
        if (code != TSDB_CODE_SUCCESS) {
4,535✔
5805
          goto _exit;
×
5806
        }
5807
        code = tColDataUpdateValue(dstCol, &cv, true);
4,535✔
5808
        if (code) {
4,535✔
5809
          goto _exit;
×
5810
        }
5811
      }
5812
    }
5813
  }
5814

5815
_exit:
1,185✔
5816
  if (code) {
1,185✔
5817
    taosArrayDestroyEx(dst, tColDataDestroy);
×
5818
  } else {
5819
    taosArrayDestroyEx(src, tColDataDestroy);
1,185✔
5820
    *colArr = dst;
1,185✔
5821
  }
5822
  return code;
1,185✔
5823
}
5824

5825
int32_t tColDataSortMerge(SArray **arr) {
7,592,143✔
5826
  SArray   *colDataArr = *arr;
7,592,143✔
5827
  int32_t   nColData = TARRAY_SIZE(colDataArr);
7,594,400✔
5828
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
7,594,023✔
5829

5830
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
7,592,551✔
5831
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5832
  }
5833
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
7,592,192✔
5834
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5835
  }
5836
  if (!(aColData[0].flag == HAS_VALUE)) {
7,591,869✔
5837
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5838
  }
5839

5840
  if (aColData[0].nVal <= 1) goto _exit;
7,591,456✔
5841

5842
  int8_t doSort = 0;
7,538,272✔
5843
  int8_t doMerge = 0;
7,538,272✔
5844
  // scan -------
5845
  SRowKey lastKey;
7,497,202✔
5846
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
7,538,649✔
5847
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
2,147,483,647✔
5848
    SRowKey key;
2,147,483,647✔
5849
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
2,147,483,647✔
5850

5851
    int32_t c = tRowKeyCompare(&lastKey, &key);
2,147,483,647✔
5852
    if (c < 0) {
2,147,483,647✔
5853
      lastKey = key;
2,147,483,647✔
5854
      continue;
2,147,483,647✔
5855
    } else if (c > 0) {
949,658✔
5856
      doSort = 1;
2,847✔
5857
      break;
2,847✔
5858
    } else {
5859
      doMerge = 1;
946,891✔
5860
    }
5861
  }
5862

5863
  // sort -------
5864
  if (doSort) {
6,061,309✔
5865
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
2,847✔
5866
  }
5867

5868
  if ((doMerge != 1) && (doSort == 1)) {
6,061,509✔
5869
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
2,847✔
5870
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
337,542✔
5871
      SRowKey key;
334,695✔
5872
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
334,891✔
5873

5874
      int32_t c = tRowKeyCompare(&lastKey, &key);
334,891✔
5875
      if (c == 0) {
334,891✔
5876
        doMerge = 1;
196✔
5877
        break;
196✔
5878
      }
5879
      lastKey = key;
334,695✔
5880
    }
5881
  }
5882

5883
  // merge -------
5884
  if (doMerge) {
7,540,897✔
5885
    int32_t code = tColDataMerge(arr);
1,185✔
5886
    if (code) return code;
1,185✔
5887
  }
5888

5889
_exit:
7,592,256✔
5890
  return 0;
7,593,381✔
5891
}
5892

5893
int32_t tColDataSortMergeWithBlob(SArray **arr, SBlobSet *pBlob) {
×
5894
  SArray   *colDataArr = *arr;
×
5895
  int32_t   nColData = TARRAY_SIZE(colDataArr);
×
5896
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
×
5897

5898
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
×
5899
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5900
  }
5901
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
5902
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5903
  }
5904
  if (!(aColData[0].flag == HAS_VALUE)) {
×
5905
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5906
  }
5907

5908
  if (aColData[0].nVal <= 1) goto _exit;
×
5909

5910
  int8_t doSort = 0;
×
5911
  int8_t doMerge = 0;
×
5912
  // scan -------
5913
  SRowKey lastKey;
×
5914
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5915
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5916
    SRowKey key;
×
5917
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5918

5919
    int32_t c = tRowKeyCompare(&lastKey, &key);
×
5920
    if (c < 0) {
×
5921
      lastKey = key;
×
5922
      continue;
×
5923
    } else if (c > 0) {
×
5924
      doSort = 1;
×
5925
      break;
×
5926
    } else {
5927
      doMerge = 1;
×
5928
    }
5929
  }
5930
  if (doMerge || doSort) {
×
5931
    return TSDB_CODE_BLOB_NOT_SUPPORT;
×
5932
  }
5933
  // sort -------
5934
  if (doSort) {
×
5935
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
×
5936
  }
5937

5938
  if ((doMerge != 1) && (doSort == 1)) {
×
5939
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5940
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5941
      SRowKey key;
×
5942
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5943

5944
      int32_t c = tRowKeyCompare(&lastKey, &key);
×
5945
      if (c == 0) {
×
5946
        doMerge = 1;
×
5947
        break;
×
5948
      }
5949
      lastKey = key;
×
5950
    }
5951
  }
5952

5953
  // merge -------
5954
  if (doMerge) {
×
5955
    int32_t code = tColDataMerge(arr);
×
5956
    if (code) return code;
×
5957
  }
5958

5959
_exit:
×
5960
  return 0;
×
5961
}
5962

5963
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
46,905,387✔
5964
  int32_t code = 0;
46,905,387✔
5965

5966
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
93,822,924✔
5967
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
93,833,799✔
5968
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
93,824,570✔
5969
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
93,817,233✔
5970

5971
  // bitmap
5972
  switch (pColData->flag) {
46,908,925✔
5973
    case (HAS_NULL | HAS_NONE):
182,760✔
5974
    case (HAS_VALUE | HAS_NONE):
5975
    case (HAS_VALUE | HAS_NULL):
5976
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
182,760✔
5977
      if (code) return code;
182,734✔
5978
      break;
182,734✔
5979
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5980
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5981
      if (code) return code;
×
5982
      break;
×
5983
    default:
46,722,455✔
5984
      break;
46,722,455✔
5985
  }
5986

5987
  // value
5988
  if (pColData->flag & HAS_VALUE) {
46,905,189✔
5989
    if (IS_VAR_DATA_TYPE(pColData->type)) {
46,828,744✔
5990
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
240,984✔
5991
      if (code) return code;
233,166✔
5992

5993
      code = tEncodeI32v(pEncoder, pColData->nData);
233,166✔
5994
      if (code) return code;
233,235✔
5995

5996
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
233,235✔
5997
      if (code) return code;
233,169✔
5998
    } else {
5999
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
46,590,250✔
6000
      if (code) return code;
46,577,401✔
6001
    }
6002
  }
6003

6004
  return code;
46,896,034✔
6005
}
6006

6007
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
33,021,516✔
6008
  int32_t code = 0;
33,021,516✔
6009

6010
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
66,048,060✔
6011
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
66,052,374✔
6012
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
66,047,743✔
6013
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
66,050,609✔
6014

6015
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
33,028,696✔
6016
    return TSDB_CODE_INVALID_PARA;
×
6017
  }
6018

6019
  // bitmap
6020
  switch (pColData->flag) {
33,021,556✔
6021
    case (HAS_NULL | HAS_NONE):
69,066✔
6022
    case (HAS_VALUE | HAS_NONE):
6023
    case (HAS_VALUE | HAS_NULL):
6024
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
69,066✔
6025
      if (code) return code;
69,066✔
6026
      break;
69,066✔
6027
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
6028
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
6029
      if (code) return code;
×
6030
      break;
×
6031
    default:
32,952,847✔
6032
      break;
32,952,847✔
6033
  }
6034

6035
  // value
6036
  if (pColData->flag & HAS_VALUE) {
33,021,913✔
6037
    if (IS_VAR_DATA_TYPE(pColData->type)) {
32,978,738✔
6038
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
129,576✔
6039
      if (code) return code;
130,300✔
6040

6041
      code = tDecodeI32v(pDecoder, &pColData->nData);
130,300✔
6042
      if (code) return code;
130,300✔
6043

6044
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
130,300✔
6045
      if (code) return code;
130,300✔
6046
    } else {
6047
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
32,847,020✔
6048
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
32,846,269✔
6049
      if (code) return code;
32,849,142✔
6050
    }
6051
  }
6052
  pColData->cflag = 0;
33,026,901✔
6053

6054
  return code;
33,026,197✔
6055
}
6056

6057
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
46,901,810✔
6058
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
46,901,810✔
6059
  if (code) return code;
46,895,625✔
6060
  return tEncodeI8(pEncoder, pColData->cflag);
93,800,675✔
6061
}
6062

6063
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
33,022,270✔
6064
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
33,022,270✔
6065
  if (code) return code;
33,024,769✔
6066

6067
  code = tDecodeI8(pDecoder, &pColData->cflag);
33,024,769✔
6068
  return code;
33,027,625✔
6069
}
6070

6071
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
46,889,337✔
6072
  if (version == 0) {
46,889,337✔
6073
    return tEncodeColDataVersion0(pEncoder, pColData);
×
6074
  } else if (version == 1) {
46,889,337✔
6075
    return tEncodeColDataVersion1(pEncoder, pColData);
×
6076
  } else if (version == 2) {
46,889,337✔
6077
    int32_t posStart = pEncoder->pos;
46,893,011✔
6078
    pEncoder->pos += INT_BYTES;
46,894,121✔
6079
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
46,911,218✔
6080
    if (code) return code;
46,902,483✔
6081
    int32_t posEnd = pEncoder->pos;
46,902,483✔
6082
    int32_t pos = posEnd - posStart;
46,898,250✔
6083
    pEncoder->pos = posStart;
46,898,250✔
6084
    code = tEncodeI32(pEncoder, pos);
46,913,951✔
6085
    pEncoder->pos = posEnd;
46,913,951✔
6086
    return code;
46,920,017✔
6087
  } else {
UNCOV
6088
    return TSDB_CODE_INVALID_PARA;
×
6089
  }
6090
}
6091

6092
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
48,297,452✔
6093
  if (version == 0) {
48,297,452✔
6094
    return tDecodeColDataVersion0(pDecoder, pColData);
×
6095
  } else if (version == 1) {
48,297,452✔
6096
    return tDecodeColDataVersion1(pDecoder, pColData);
×
6097
  } else if (version == 2) {
48,297,452✔
6098
    if (jump) {
48,301,022✔
6099
      int32_t len = 0;
15,276,977✔
6100
      int32_t code = tDecodeI32(pDecoder, &len);
15,276,620✔
6101
      if (code) return code;
15,276,620✔
6102
      pDecoder->pos += (len - INT_BYTES);
15,276,620✔
6103
      return TSDB_CODE_SUCCESS;
15,276,977✔
6104
    } else {
6105
      pDecoder->pos += INT_BYTES;
33,024,045✔
6106
      return tDecodeColDataVersion1(pDecoder, pColData);
33,025,473✔
6107
    }    
6108
  } else {
6109
    return TSDB_CODE_INVALID_PARA;
×
6110
  }
6111
}
6112

6113
int32_t tEncodeRow(SEncoder *pEncoder, SRow *pRow) { return tEncodeFixed(pEncoder, pRow, pRow->len); }
2,147,483,647✔
6114

6115
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
2,147,483,647✔
6116
  if (ppRow == NULL) {
2,147,483,647✔
6117
    return TSDB_CODE_INVALID_PARA;
×
6118
  }
6119

6120
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
2,147,483,647✔
6121
    return TSDB_CODE_OUT_OF_RANGE;
×
6122
  }
6123

6124
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
2,147,483,647✔
6125
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
2,147,483,647✔
6126
}
6127

6128
int32_t tEncodeBlobSet(SEncoder *pEncoder, SBlobSet *pRow) {
98,556✔
6129
  int32_t code = 0;
98,556✔
6130
  int32_t lino = 0;
98,556✔
6131
  uint8_t compressed = 0;
98,556✔
6132
  int32_t compressSize = 0;
98,556✔
6133
  char   *p = NULL;
98,556✔
6134

6135
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
197,112✔
6136
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
197,112✔
6137

6138
  int32_t nSeq = taosArrayGetSize(pRow->pSeqTable);
98,556✔
6139
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nSeq));
98,556✔
6140

6141
  // if (pRow->type) {
6142
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
98,556✔
6143
  while (pIter) {
54,555,124✔
6144
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
54,456,568✔
6145
    int32_t  idx = *(int32_t *)pIter;
54,456,568✔
6146
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
54,456,568✔
6147
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
54,456,568✔
6148

6149
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
54,456,568✔
6150
  }
6151
  //}
6152
  for (int32_t i = 0; i < nSeq; i++) {
54,555,124✔
6153
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
54,456,568✔
6154
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
108,913,136✔
6155
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
108,913,136✔
6156
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
108,913,136✔
6157
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
108,913,136✔
6158
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
108,913,136✔
6159
  }
6160

6161
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
197,112✔
6162

6163
_exit:
98,556✔
6164
  return code;
98,556✔
6165
}
6166

6167
int32_t tDecodeBlobSet(SDecoder *pDecoder, SBlobSet **pBlobSet) {
49,039✔
6168
  int32_t    code = 0;
49,039✔
6169
  int32_t    lino = 0;
49,039✔
6170
  int32_t    nSeq = 0;
49,039✔
6171
  uint8_t    compressd = 0;
49,039✔
6172
  int32_t    compressSize = 0;
49,039✔
6173
  SBlobSet  *pBlob = taosMemCalloc(1, sizeof(SBlobSet));
49,039✔
6174
  if (pBlob == NULL) {
49,039✔
6175
    TAOS_CHECK_EXIT(terrno);
×
6176
  }
6177
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pBlob->type));
98,078✔
6178
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pBlob->len));
98,078✔
6179

6180
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
49,039✔
6181

6182
  // if (pBlob->type) {
6183
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
49,039✔
6184
  if (pBlob->pSeqToffset == NULL) {
49,039✔
6185
    TAOS_CHECK_EXIT(terrno);
×
6186
  }
6187
  for (int32_t i = 0; i < nSeq; i++) {
27,276,681✔
6188
    uint64_t seq;
27,227,642✔
6189
    int32_t  idx;
27,227,642✔
6190
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
27,227,642✔
6191
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
27,227,642✔
6192

6193
    code = taosHashPut(pBlob->pSeqToffset, &seq, sizeof(seq), &idx, sizeof(idx));
27,227,642✔
6194
    TAOS_CHECK_EXIT(code);
27,227,642✔
6195
  }
6196
  //}
6197

6198
  pBlob->pSeqTable = taosArrayInit(nSeq, sizeof(SBlobValue));
49,039✔
6199
  if (pBlob->pSeqTable == NULL) {
49,039✔
6200
    TAOS_CHECK_EXIT(terrno);
×
6201
  }
6202

6203
  for (int32_t i = 0; i < nSeq; i++) {
27,276,681✔
6204
    SBlobValue value;
27,227,642✔
6205
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
27,227,642✔
6206
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
27,227,642✔
6207
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
27,227,642✔
6208
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
27,227,642✔
6209
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
27,227,642✔
6210
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
54,455,284✔
6211
      TAOS_CHECK_EXIT(terrno);
×
6212
    }
6213
  }
6214

6215
  pBlob->data = taosMemCalloc(1, pBlob->len);
49,039✔
6216
  if (pBlob->data == NULL) {
49,039✔
6217
    TAOS_CHECK_EXIT(terrno);
×
6218
  }
6219

6220
  TAOS_CHECK_EXIT(tDecodeFixed(pDecoder, pBlob->data, pBlob->len));
49,039✔
6221
  *pBlobSet = pBlob;
49,039✔
6222

6223
  uTrace("decode blob len:%d", (int32_t)(pBlob->len));
49,039✔
6224

6225
_exit:
49,039✔
6226
  if (code != 0) {
49,039✔
6227
    if (pBlob != NULL) {
×
6228
      taosMemoryFree(pBlob->data);
×
6229
      taosArrayDestroy(pBlob->pSeqTable);
×
6230
      taosMemoryFree(pBlob);
×
6231
    }
6232
  }
6233
  return code;
49,039✔
6234
}
6235

6236
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
6237
  do {                                       \
6238
    (SUM) += (VAL);                          \
6239
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
6240
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
6241
  } while (0)
6242

6243
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
8,784,523✔
6244
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,784,523✔
6245
  int16_t *numOfNull = &pAggs->numOfNull;
8,784,523✔
6246
  *sum = 0;
8,784,523✔
6247
  *max = 0;
8,784,523✔
6248
  *min = 1;
8,784,523✔
6249
  *numOfNull = 0;
8,784,523✔
6250

6251
  int8_t val;
6252
  if (HAS_VALUE == pColData->flag) {
8,784,523✔
6253
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6254
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,147,483,647✔
6255
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6256
    }
6257
  } else {
6258
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6259
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6260
        case 0:
×
6261
        case 1:
6262
          (*numOfNull)++;
×
6263
          break;
×
6264
        case 2:
×
6265
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
×
6266
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
6267
          break;
×
6268
        default:
×
6269
          break;
×
6270
      }
6271
    }
6272
  }
6273
}
8,862,859✔
6274

6275
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,798,538✔
6276
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,798,538✔
6277
  int16_t *numOfNull = &pAggs->numOfNull;
8,798,538✔
6278
  *sum = 0;
8,798,538✔
6279
  *max = INT8_MIN;
8,799,189✔
6280
  *min = INT8_MAX;
8,798,538✔
6281
  *numOfNull = 0;
8,798,538✔
6282

6283
  int8_t val;
6284
  if (HAS_VALUE == pColData->flag) {
8,797,887✔
6285
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6286
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6287
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6288
    }
6289
  } else {
6290
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,270,269✔
6291
      switch (tColDataGetBitValue(pColData, iVal)) {
1,269,000✔
6292
        case 0:
1,002,087✔
6293
        case 1:
6294
          (*numOfNull)++;
1,002,087✔
6295
          break;
1,002,087✔
6296
        case 2:
266,913✔
6297
          val = ((int8_t *)pColData->pData)[iVal];
266,913✔
6298
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
266,913✔
6299
          break;
266,913✔
6300
        default:
×
6301
          break;
×
6302
      }
6303
    }
6304
  }
6305
}
8,840,096✔
6306

6307
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,785,653✔
6308
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,785,653✔
6309
  int16_t *numOfNull = &pAggs->numOfNull;
8,784,351✔
6310
  *sum = 0;
8,784,351✔
6311
  *max = INT16_MIN;
8,784,351✔
6312
  *min = INT16_MAX;
8,783,700✔
6313
  *numOfNull = 0;
8,783,700✔
6314

6315
  int16_t val;
6316
  if (HAS_VALUE == pColData->flag) {
8,783,700✔
6317
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6318
      val = ((int16_t *)pColData->pData)[iVal];
2,147,483,647✔
6319
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6320
    }
6321
  } else {
6322
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6323
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6324
        case 0:
×
6325
        case 1:
6326
          (*numOfNull)++;
×
6327
          break;
×
6328
        case 2:
×
6329
          val = ((int16_t *)pColData->pData)[iVal];
×
6330
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
6331
          break;
×
6332
        default:
×
6333
          break;
×
6334
      }
6335
    }
6336
  }
6337
}
8,815,567✔
6338

6339
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
28,786,100✔
6340
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
28,786,100✔
6341
  int16_t *numOfNull = &pAggs->numOfNull;
28,788,893✔
6342
  *sum = 0;
28,787,171✔
6343
  *max = INT32_MIN;
28,787,171✔
6344
  *min = INT32_MAX;
28,787,528✔
6345
  *numOfNull = 0;
28,787,948✔
6346

6347
  int32_t val;
6348
  if (HAS_VALUE == pColData->flag) {
28,788,242✔
6349
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6350
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6351
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6352
    }
6353
  } else {
6354
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6355
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6356
        case 0:
293,924,746✔
6357
        case 1:
6358
          (*numOfNull)++;
293,924,746✔
6359
          break;
293,924,746✔
6360
        case 2:
2,147,483,647✔
6361
          val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6362
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6363
          break;
2,147,483,647✔
6364
        default:
×
6365
          break;
×
6366
      }
6367
    }
6368
  }
6369
}
26,854,228✔
6370

6371
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
70,646,918✔
6372
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
70,646,918✔
6373
  int16_t *numOfNull = &pAggs->numOfNull;
70,647,111✔
6374
  *sum = 0;
70,647,111✔
6375
  *max = INT64_MIN;
70,647,111✔
6376
  *min = INT64_MAX;
70,647,111✔
6377
  *numOfNull = 0;
70,647,304✔
6378

6379
  int64_t val;
6380
  if (HAS_VALUE == pColData->flag) {
70,647,304✔
6381
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6382
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6383
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6384
    }
6385
  } else {
6386
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6387
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6388
        case 0:
324,415,164✔
6389
        case 1:
6390
          (*numOfNull)++;
324,415,164✔
6391
          break;
324,415,164✔
6392
        case 2:
2,147,483,647✔
6393
          val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6394
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6395
          break;
2,147,483,647✔
6396
        default:
×
6397
          break;
×
6398
      }
6399
    }
6400
  }
6401
}
45,914,927✔
6402

6403
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
30,910,802✔
6404
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
30,910,802✔
6405
  int16_t *numOfNull = &pAggs->numOfNull;
30,910,802✔
6406
  *(double *)sum = 0;
30,910,802✔
6407
  *(double *)max = -FLT_MAX;
30,910,802✔
6408
  *(double *)min = FLT_MAX;
30,910,802✔
6409
  *numOfNull = 0;
30,910,802✔
6410

6411
  float val;
6412
  if (HAS_VALUE == pColData->flag) {
30,910,151✔
6413
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6414
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6415
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6416
    }
6417
  } else {
6418
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
11,022,881✔
6419
      switch (tColDataGetBitValue(pColData, iVal)) {
11,006,338✔
6420
        case 0:
1,956,180✔
6421
        case 1:
6422
          (*numOfNull)++;
1,956,180✔
6423
          break;
1,956,180✔
6424
        case 2:
9,050,158✔
6425
          val = ((float *)pColData->pData)[iVal];
9,050,158✔
6426
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
9,050,158✔
6427
          break;
9,050,158✔
6428
        default:
×
6429
          break;
×
6430
      }
6431
    }
6432
  }
6433
}
31,359,568✔
6434

6435
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
16,164,694✔
6436
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
16,164,694✔
6437
  int16_t *numOfNull = &pAggs->numOfNull;
16,166,122✔
6438
  *(double *)sum = 0;
16,164,043✔
6439
  *(double *)max = -DBL_MAX;
16,165,408✔
6440
  *(double *)min = DBL_MAX;
16,165,765✔
6441
  *numOfNull = 0;
16,165,398✔
6442

6443
  double val;
6444
  if (HAS_VALUE == pColData->flag) {
16,165,755✔
6445
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6446
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6447
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6448
    }
6449
  } else {
6450
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,270,269✔
6451
      switch (tColDataGetBitValue(pColData, iVal)) {
1,269,000✔
6452
        case 0:
1,004,202✔
6453
        case 1:
6454
          (*numOfNull)++;
1,004,202✔
6455
          break;
1,004,202✔
6456
        case 2:
264,798✔
6457
          val = ((double *)pColData->pData)[iVal];
264,798✔
6458
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
264,798✔
6459
          break;
264,798✔
6460
        default:
×
6461
          break;
×
6462
      }
6463
    }
6464
  }
6465
}
16,592,742✔
6466

6467
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,424,505✔
6468
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,424,505✔
6469
  int16_t *numOfNull = &pAggs->numOfNull;
8,425,156✔
6470
  *(uint64_t *)sum = 0;
8,425,156✔
6471
  *(uint64_t *)max = 0;
8,426,458✔
6472
  *(uint64_t *)min = UINT8_MAX;
8,425,807✔
6473
  *numOfNull = 0;
8,425,807✔
6474

6475
  uint8_t val;
6476
  if (HAS_VALUE == pColData->flag) {
8,425,807✔
6477
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6478
      val = ((uint8_t *)pColData->pData)[iVal];
2,147,483,647✔
6479
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6480
    }
6481
  } else {
6482
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6483
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6484
        case 0:
×
6485
        case 1:
6486
          (*numOfNull)++;
×
6487
          break;
×
6488
        case 2:
×
6489
          val = ((uint8_t *)pColData->pData)[iVal];
×
6490
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6491
          break;
×
6492
        default:
×
6493
          break;
×
6494
      }
6495
    }
6496
  }
6497
}
8,473,305✔
6498

6499
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,424,505✔
6500
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,424,505✔
6501
  int16_t *numOfNull = &pAggs->numOfNull;
8,425,807✔
6502
  *(uint64_t *)sum = 0;
8,424,505✔
6503
  *(uint64_t *)max = 0;
8,425,156✔
6504
  *(uint64_t *)min = UINT16_MAX;
8,426,458✔
6505
  *numOfNull = 0;
8,427,109✔
6506

6507
  uint16_t val;
6508
  if (HAS_VALUE == pColData->flag) {
8,425,156✔
6509
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6510
      val = ((uint16_t *)pColData->pData)[iVal];
2,147,483,647✔
6511
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6512
    }
6513
  } else {
6514
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6515
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6516
        case 0:
×
6517
        case 1:
6518
          (*numOfNull)++;
×
6519
          break;
×
6520
        case 2:
×
6521
          val = ((uint16_t *)pColData->pData)[iVal];
×
6522
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6523
          break;
×
6524
        default:
×
6525
          break;
×
6526
      }
6527
    }
6528
  }
6529
}
8,450,759✔
6530

6531
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,427,109✔
6532
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,427,109✔
6533
  int16_t *numOfNull = &pAggs->numOfNull;
8,426,458✔
6534
  *(uint64_t *)sum = 0;
8,425,807✔
6535
  *(uint64_t *)max = 0;
8,425,807✔
6536
  *(uint64_t *)min = UINT32_MAX;
8,425,807✔
6537
  *numOfNull = 0;
8,425,807✔
6538

6539
  uint32_t val;
6540
  if (HAS_VALUE == pColData->flag) {
8,425,807✔
6541
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6542
      val = ((uint32_t *)pColData->pData)[iVal];
2,147,483,647✔
6543
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6544
    }
6545
  } else {
6546
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6547
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6548
        case 0:
×
6549
        case 1:
6550
          (*numOfNull)++;
×
6551
          break;
×
6552
        case 2:
×
6553
          val = ((uint32_t *)pColData->pData)[iVal];
×
6554
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6555
          break;
×
6556
        default:
×
6557
          break;
×
6558
      }
6559
    }
6560
  }
6561
}
8,321,033✔
6562

6563
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,427,109✔
6564
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,427,109✔
6565
  int16_t *numOfNull = &pAggs->numOfNull;
8,427,109✔
6566
  *(uint64_t *)sum = 0;
8,427,109✔
6567
  *(uint64_t *)max = 0;
8,427,109✔
6568
  *(uint64_t *)min = UINT64_MAX;
8,427,109✔
6569
  *numOfNull = 0;
8,427,109✔
6570

6571
  uint64_t val;
6572
  if (HAS_VALUE == pColData->flag) {
8,427,109✔
6573
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6574
      val = ((uint64_t *)pColData->pData)[iVal];
2,147,483,647✔
6575
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6576
    }
6577
  } else {
6578
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6579
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6580
        case 0:
×
6581
        case 1:
6582
          (*numOfNull)++;
×
6583
          break;
×
6584
        case 2:
×
6585
          val = ((uint64_t *)pColData->pData)[iVal];
×
6586
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6587
          break;
×
6588
        default:
×
6589
          break;
×
6590
      }
6591
    }
6592
  }
6593
}
8,055,463✔
6594

6595
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
31,872,072✔
6596
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
31,872,072✔
6597
  int16_t *numOfNull = &pAggs->numOfNull;
31,872,646✔
6598
  *(uint64_t *)sum = 0;
31,872,072✔
6599
  *(uint64_t *)max = 0;
31,872,072✔
6600
  *(uint64_t *)min = 0;
31,872,072✔
6601
  *numOfNull = 0;
31,871,498✔
6602

6603
  switch (pColData->flag) {
31,871,421✔
6604
    case HAS_NONE:
×
6605
    case HAS_NULL:
6606
    case (HAS_NONE | HAS_NULL):
6607
      *numOfNull = pColData->nVal;
×
6608
      break;
×
6609
    case HAS_VALUE:
31,864,984✔
6610
      *numOfNull = 0;
31,864,984✔
6611
      break;
31,865,061✔
6612
    case (HAS_VALUE | HAS_NULL):
3,825✔
6613
    case (HAS_VALUE | HAS_NONE):
6614
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
8,940,825✔
6615
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
8,937,000✔
6616
          (*numOfNull)++;
1,163,907✔
6617
        }
6618
      }
6619
      break;
3,825✔
6620
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6621
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6622
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6623
          (*numOfNull)++;
×
6624
        }
6625
      }
6626
      break;
×
6627
    default:
×
6628
      break;
×
6629
  }
6630
}
31,868,886✔
6631

6632
#define CALC_DECIMAL_SUM_MAX_MIN(TYPE, pSumOp, pCompOp, pColData, pSum, pMax, pMin)                   \
6633
  do {                                                                                                \
6634
    if (decimal128AddCheckOverflow((Decimal *)pSum, pVal, DECIMAL_WORD_NUM(TYPE))) *pOverflow = true; \
6635
    pSumOp->add(pSum, pVal, DECIMAL_WORD_NUM(TYPE));                                                  \
6636
    if (pCompOp->gt(pVal, pMax, DECIMAL_WORD_NUM(TYPE))) {                                            \
6637
      *(pMax) = *pVal;                                                                                \
6638
    }                                                                                                 \
6639
    if (pCompOp->lt(pVal, pMin, DECIMAL_WORD_NUM(TYPE))) {                                            \
6640
      *(pMin) = *pVal;                                                                                \
6641
    }                                                                                                 \
6642
  } while (0)
6643

6644
static FORCE_INLINE void tColDataCalcSMADecimal64Type(SColData *pColData, SColumnDataAgg *pAggs) {
18,699✔
6645
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum;
18,699✔
6646
  Decimal64  *pMax = (Decimal64 *)pAggs->decimal128Max, *pMin = (Decimal64 *)pAggs->decimal128Min;
18,699✔
6647
  uint8_t    *pOverflow = &pAggs->overflow;
18,699✔
6648
  *pSum = DECIMAL128_ZERO;
18,699✔
6649
  *pMax = DECIMAL64_MIN;
18,699✔
6650
  *pMin = DECIMAL64_MAX;
18,699✔
6651
  pAggs->numOfNull = 0;
18,699✔
6652
  pAggs->colId |= DECIMAL_AGG_FLAG;
18,699✔
6653

6654
  Decimal64         *pVal = NULL;
18,699✔
6655
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
18,699✔
6656
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
18,699✔
6657
  if (HAS_VALUE == pColData->flag) {
18,699✔
6658
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
159,378✔
6659
      pVal = ((Decimal64 *)pColData->pData) + iVal;
157,800✔
6660
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
157,800✔
6661
    }
6662
  } else {
6663
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
12,877,521✔
6664
      switch (tColDataGetBitValue(pColData, iVal)) {
12,860,400✔
6665
        case 0:
567,711✔
6666
        case 1:
6667
          pAggs->numOfNull++;
567,711✔
6668
          break;
567,711✔
6669
        case 2:
12,292,689✔
6670
          pVal = ((Decimal64 *)pColData->pData) + iVal;
12,292,689✔
6671
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
12,292,689✔
6672
          break;
12,292,689✔
6673
        default:
×
6674
          break;
×
6675
      }
6676
    }
6677
  }
6678
}
18,699✔
6679

6680
static FORCE_INLINE void tColDataCalcSMADecimal128Type(SColData *pColData, SColumnDataAgg *pAggs) {
46,677✔
6681
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum, *pMax = (Decimal128 *)pAggs->decimal128Max,
46,677✔
6682
             *pMin = (Decimal128 *)pAggs->decimal128Min;
46,677✔
6683
  uint8_t *pOverflow = &pAggs->overflow;
46,677✔
6684
  *pSum = DECIMAL128_ZERO;
46,677✔
6685
  *pMax = DECIMAL128_MIN;
46,677✔
6686
  *pMin = DECIMAL128_MAX;
46,677✔
6687
  pAggs->numOfNull = 0;
46,677✔
6688
  pAggs->colId |= DECIMAL_AGG_FLAG;
46,677✔
6689

6690
  Decimal128        *pVal = NULL;
46,677✔
6691
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
46,677✔
6692
  if (HAS_VALUE == pColData->flag) {
46,677✔
6693
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
239,067✔
6694
      pVal = ((Decimal128 *)pColData->pData) + iVal;
236,700✔
6695
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
236,700✔
6696
    }
6697
  } else {
6698
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
40,803,810✔
6699
      switch (tColDataGetBitValue(pColData, iVal)) {
40,759,500✔
6700
        case 0:
1,803,510✔
6701
        case 1:
6702
          pAggs->numOfNull++;
1,803,510✔
6703
          break;
1,803,510✔
6704
        case 2:
38,955,990✔
6705
          pVal = ((Decimal128 *)pColData->pData) + iVal;
38,955,990✔
6706
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
38,955,990✔
6707
          break;
38,955,990✔
6708
        default:
×
6709
          break;
×
6710
      }
6711
    }
6712
  }
6713
}
46,677✔
6714

6715
void (*tColDataCalcSMA[])(SColData *pColData, SColumnDataAgg *pAggs) = {
6716
    NULL,
6717
    tColDataCalcSMABool,            // TSDB_DATA_TYPE_BOOL
6718
    tColDataCalcSMATinyInt,         // TSDB_DATA_TYPE_TINYINT
6719
    tColDataCalcSMATinySmallInt,    // TSDB_DATA_TYPE_SMALLINT
6720
    tColDataCalcSMAInt,             // TSDB_DATA_TYPE_INT
6721
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_BIGINT
6722
    tColDataCalcSMAFloat,           // TSDB_DATA_TYPE_FLOAT
6723
    tColDataCalcSMADouble,          // TSDB_DATA_TYPE_DOUBLE
6724
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARCHAR
6725
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_TIMESTAMP
6726
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_NCHAR
6727
    tColDataCalcSMAUTinyInt,        // TSDB_DATA_TYPE_UTINYINT
6728
    tColDataCalcSMATinyUSmallInt,   // TSDB_DATA_TYPE_USMALLINT
6729
    tColDataCalcSMAUInt,            // TSDB_DATA_TYPE_UINT
6730
    tColDataCalcSMAUBigInt,         // TSDB_DATA_TYPE_UBIGINT
6731
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_JSON
6732
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARBINARY
6733
    tColDataCalcSMADecimal128Type,  // TSDB_DATA_TYPE_DECIMAL
6734
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_BLOB
6735
    NULL,                           // TSDB_DATA_TYPE_MEDIUMBLOB
6736
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_GEOMETRY
6737
    tColDataCalcSMADecimal64Type,   // TSDB_DATA_TYPE_DECIMAL64
6738
};
6739

6740
// SValueColumn ================================
6741
int32_t tValueColumnInit(SValueColumn *valCol) {
1,106,714,779✔
6742
  valCol->type = TSDB_DATA_TYPE_NULL;
1,106,714,779✔
6743
  valCol->numOfValues = 0;
1,106,743,998✔
6744
  tBufferInit(&valCol->data);
1,106,745,869✔
6745
  tBufferInit(&valCol->offsets);
1,106,754,451✔
6746
  return 0;
1,106,818,824✔
6747
}
6748

6749
void tValueColumnDestroy(SValueColumn *valCol) {
2,147,483,647✔
6750
  valCol->type = TSDB_DATA_TYPE_NULL;
2,147,483,647✔
6751
  valCol->numOfValues = 0;
2,147,483,647✔
6752
  tBufferDestroy(&valCol->data);
2,147,483,647✔
6753
  tBufferDestroy(&valCol->offsets);
2,147,483,647✔
6754
  return;
2,147,483,647✔
6755
}
6756

6757
void tValueColumnClear(SValueColumn *valCol) {
1,545,472,837✔
6758
  valCol->type = TSDB_DATA_TYPE_NULL;
1,545,472,837✔
6759
  valCol->numOfValues = 0;
1,545,549,008✔
6760
  tBufferClear(&valCol->data);
1,545,555,716✔
6761
  tBufferClear(&valCol->offsets);
1,545,589,186✔
6762
  return;
1,545,573,724✔
6763
}
6764

6765
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
1,896,905✔
6766
  int32_t code;
6767

6768
  if (valCol->numOfValues == 0) {
1,896,905✔
6769
    valCol->type = value->type;
1,025,268✔
6770
  }
6771

6772
  if (!(value->type == valCol->type)) {
1,897,604✔
6773
    return TSDB_CODE_INVALID_PARA;
×
6774
  }
6775

6776
  if (IS_VAR_DATA_TYPE(value->type)) {
1,896,905✔
6777
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
1,652,348✔
6778
      return code;
×
6779
    }
6780
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
1,652,348✔
6781
      return code;
×
6782
    }
6783
  } else {
6784
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
1,071,430✔
6785
    if (code) return code;
1,071,430✔
6786
  }
6787
  valCol->numOfValues++;
1,897,604✔
6788

6789
  return 0;
1,897,604✔
6790
}
6791

6792
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
61,506,672✔
6793
  int32_t code;
6794

6795
  if (idx < 0 || idx >= valCol->numOfValues) {
61,506,672✔
6796
    return TSDB_CODE_OUT_OF_RANGE;
×
6797
  }
6798

6799
  if (IS_VAR_DATA_TYPE(valCol->type)) {
61,506,840✔
6800
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
1,869,354✔
6801
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
1,870,137✔
6802
    int32_t  oldDataSize = nextOffset - offsets[idx];
1,870,137✔
6803
    int32_t  bytesAdded = value->nData - oldDataSize;
1,870,137✔
6804

6805
    if (bytesAdded != 0) {
1,870,137✔
6806
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
42,924✔
6807
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
21,462✔
6808
              tBufferGetSize(&valCol->data) - nextOffset);
21,462✔
6809
      valCol->data.size += bytesAdded;
21,462✔
6810

6811
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
21,462✔
6812
        offsets[i] += bytesAdded;
×
6813
      }
6814
    }
6815
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
1,870,137✔
6816
  } else {
6817
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
59,637,486✔
6818
                        tDataTypes[valCol->type].bytes);
59,636,703✔
6819
  }
6820
  return 0;
6821
}
6822

6823
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
320,569,114✔
6824
  if (idx < 0 || idx >= valCol->numOfValues) {
320,569,114✔
6825
    return TSDB_CODE_OUT_OF_RANGE;
×
6826
  }
6827

6828
  value->type = valCol->type;
320,593,475✔
6829
  if (IS_VAR_DATA_TYPE(value->type)) {
401,432,866✔
6830
    int32_t       offset, nextOffset;
80,831,223✔
6831
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
80,834,787✔
6832

6833
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
80,835,450✔
6834
    if (idx == valCol->numOfValues - 1) {
80,835,438✔
6835
      nextOffset = tBufferGetSize(&valCol->data);
46,036,953✔
6836
    } else {
6837
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
34,804,692✔
6838
    }
6839
    value->nData = nextOffset - offset;
80,838,162✔
6840
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
80,832,027✔
6841
  } else {
6842
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
239,746,894✔
6843
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
479,516,304✔
6844
  }
6845
  return 0;
320,586,830✔
6846
}
6847

6848
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,024,574✔
6849
  int32_t code;
6850

6851
  if (!(valCol->numOfValues > 0)) {
1,024,574✔
6852
    return TSDB_CODE_INVALID_PARA;
×
6853
  }
6854

6855
  (*info) = (SValueColumnCompressInfo){
1,025,268✔
6856
      .cmprAlg = info->cmprAlg,
1,025,268✔
6857
      .type = valCol->type,
1,025,268✔
6858
  };
6859

6860
  // offset
6861
  if (IS_VAR_DATA_TYPE(valCol->type)) {
1,025,268✔
6862
    SCompressInfo cinfo = {
379,766✔
6863
        .cmprAlg = info->cmprAlg,
379,766✔
6864
        .dataType = TSDB_DATA_TYPE_INT,
6865
        .originalSize = valCol->offsets.size,
379,766✔
6866
    };
6867

6868
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
379,766✔
6869
    if (code) return code;
380,460✔
6870

6871
    info->offsetOriginalSize = cinfo.originalSize;
380,460✔
6872
    info->offsetCompressedSize = cinfo.compressedSize;
380,460✔
6873
  }
6874

6875
  // data
6876
  SCompressInfo cinfo = {
1,025,268✔
6877
      .cmprAlg = info->cmprAlg,
2,050,498✔
6878
      .dataType = valCol->type,
1,025,268✔
6879
      .originalSize = valCol->data.size,
1,025,268✔
6880
  };
6881

6882
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
1,024,574✔
6883
  if (code) return code;
1,025,268✔
6884

6885
  info->dataOriginalSize = cinfo.originalSize;
1,025,268✔
6886
  info->dataCompressedSize = cinfo.compressedSize;
1,025,268✔
6887

6888
  return 0;
1,024,574✔
6889
}
6890

6891
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
145,682,828✔
6892
                               SBuffer *assist) {
6893
  int32_t code;
6894

6895
  tValueColumnClear(valCol);
145,682,828✔
6896
  valCol->type = info->type;
145,713,755✔
6897
  // offset
6898
  if (IS_VAR_DATA_TYPE(valCol->type)) {
209,345,041✔
6899
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
63,622,223✔
6900

6901
    SCompressInfo cinfo = {
63,624,320✔
6902
        .dataType = TSDB_DATA_TYPE_INT,
6903
        .cmprAlg = info->cmprAlg,
63,624,332✔
6904
        .originalSize = info->offsetOriginalSize,
63,625,718✔
6905
        .compressedSize = info->offsetCompressedSize,
63,622,982✔
6906
    };
6907

6908
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
63,629,141✔
6909
    if (code) {
63,638,879✔
6910
      return code;
×
6911
    }
6912
  } else {
6913
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
82,076,334✔
6914
  }
6915

6916
  // data
6917
  SCompressInfo cinfo = {
145,723,445✔
6918
      .dataType = valCol->type,
145,720,697✔
6919
      .cmprAlg = info->cmprAlg,
145,714,526✔
6920
      .originalSize = info->dataOriginalSize,
145,717,286✔
6921
      .compressedSize = info->dataCompressedSize,
145,712,405✔
6922
  };
6923

6924
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
145,708,331✔
6925
  if (code) {
145,727,591✔
6926
    return code;
×
6927
  }
6928

6929
  return 0;
145,727,591✔
6930
}
6931

6932
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
1,024,574✔
6933
  int32_t code;
6934
  uint8_t fmtVer = 0;
1,024,574✔
6935

6936
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
2,049,148✔
6937
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
2,049,842✔
6938
  if ((code = tBufferPutI8(buffer, info->type))) return code;
2,049,842✔
6939
  if (IS_VAR_DATA_TYPE(info->type)) {
1,024,574✔
6940
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
760,226✔
6941
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
760,226✔
6942
  }
6943
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
2,049,148✔
6944
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
2,049,148✔
6945

6946
  return 0;
1,024,574✔
6947
}
6948

6949
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
145,707,644✔
6950
  int32_t code;
6951
  uint8_t fmtVer;
145,707,644✔
6952

6953
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
145,711,079✔
6954
  if (fmtVer == 0) {
145,719,347✔
6955
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
145,719,347✔
6956
    if ((code = tBufferGetI8(reader, &info->type))) return code;
145,724,168✔
6957
    if (IS_VAR_DATA_TYPE(info->type)) {
145,724,144✔
6958
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
63,630,623✔
6959
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
63,632,684✔
6960
    } else {
6961
      info->offsetOriginalSize = 0;
82,082,541✔
6962
      info->offsetCompressedSize = 0;
82,084,602✔
6963
    }
6964
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
145,715,177✔
6965
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
145,724,156✔
6966
  } else {
6967
    return TSDB_CODE_INVALID_PARA;
×
6968
  }
6969

6970
  return 0;
145,732,412✔
6971
}
6972

6973
int32_t tCompressData(void          *input,       // input
1,001,844,890✔
6974
                      SCompressInfo *info,        // compress info
6975
                      void          *output,      // output
6976
                      int32_t        outputSize,  // output size
6977
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6978
) {
6979
  int32_t extraSizeNeeded;
6980
  int32_t code;
6981

6982
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
1,001,844,890✔
6983
  if (!(outputSize >= extraSizeNeeded)) {
1,001,905,339✔
6984
    return TSDB_CODE_INVALID_PARA;
×
6985
  }
6986

6987
  if (info->cmprAlg == NO_COMPRESSION) {
1,001,905,339✔
6988
    (void)memcpy(output, input, info->originalSize);
35,860✔
6989
    info->compressedSize = info->originalSize;
35,860✔
6990
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
1,125,122,595✔
6991
    SBuffer local;
123,258,431✔
6992

6993
    tBufferInit(&local);
6994
    if (buffer == NULL) {
123,282,664✔
6995
      buffer = &local;
×
6996
    }
6997

6998
    if (info->cmprAlg == TWO_STAGE_COMP) {
123,282,664✔
6999
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
123,110,636✔
7000
      if (code) {
123,105,201✔
7001
        tBufferDestroy(&local);
7002
        return code;
×
7003
      }
7004
    }
7005

7006
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
369,831,468✔
7007
        input,                                                   // input
7008
        info->originalSize,                                      // input size
7009
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
123,278,880✔
7010
        output,                                                  // output
7011
        outputSize,                                              // output size
7012
        info->cmprAlg,                                           // compression algorithm
123,275,864✔
7013
        buffer->data,                                            // buffer
7014
        buffer->capacity                                         // buffer size
123,278,690✔
7015
    );
7016
    if (info->compressedSize < 0) {
123,281,836✔
7017
      tBufferDestroy(&local);
7018
      return TSDB_CODE_COMPRESS_ERROR;
×
7019
    }
7020

7021
    tBufferDestroy(&local);
7022
  } else {
7023
    DEFINE_VAR(info->cmprAlg)
878,595,172✔
7024
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
878,574,278✔
7025
      (void)memcpy(output, input, info->originalSize);
7,320✔
7026
      info->compressedSize = info->originalSize;
7,320✔
7027
      return 0;
7,320✔
7028
    }
7029
    SBuffer local;
878,556,844✔
7030

7031
    tBufferInit(&local);
7032
    if (buffer == NULL) {
878,584,590✔
7033
      buffer = &local;
×
7034
    }
7035
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
878,584,590✔
7036

7037
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
2,147,483,647✔
7038
        input,                                                      // input
7039
        info->originalSize,                                         // input size
7040
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
878,584,680✔
7041
        output,                                                     // output
7042
        outputSize,                                                 // output size
7043
        info->cmprAlg,                                              // compression algorithm
7044
        buffer->data,                                               // buffer
7045
        buffer->capacity                                            // buffer size
878,570,094✔
7046
    );
7047
    if (info->compressedSize < 0) {
878,581,644✔
7048
      tBufferDestroy(&local);
7049
      return TSDB_CODE_COMPRESS_ERROR;
×
7050
    }
7051

7052
    tBufferDestroy(&local);
7053
    // new col compress
7054
  }
7055

7056
  return 0;
1,001,852,396✔
7057
}
7058

7059
int32_t tDecompressData(void                *input,       // input
2,147,483,647✔
7060
                        const SCompressInfo *info,        // compress info
7061
                        void                *output,      // output
7062
                        int32_t              outputSize,  // output size
7063
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
7064
) {
7065
  int32_t code;
7066

7067
  if (!(outputSize >= info->originalSize)) {
2,147,483,647✔
7068
    return TSDB_CODE_INVALID_PARA;
×
7069
  }
7070

7071
  if (info->cmprAlg == NO_COMPRESSION) {
2,147,483,647✔
7072
    if (!(info->compressedSize == info->originalSize)) {
16,854✔
7073
      return TSDB_CODE_INVALID_PARA;
×
7074
    }
7075
    (void)memcpy(output, input, info->compressedSize);
16,854✔
7076
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
7077
    SBuffer local;
2,019,369,464✔
7078

7079
    tBufferInit(&local);
7080
    if (buffer == NULL) {
2,019,582,009✔
7081
      buffer = &local;
×
7082
    }
7083

7084
    if (info->cmprAlg == TWO_STAGE_COMP) {
2,019,582,009✔
7085
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,018,455,514✔
7086
      if (code) {
2,018,312,946✔
7087
        tBufferDestroy(&local);
7088
        return code;
×
7089
      }
7090
    }
7091

7092
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,147,483,647✔
7093
        input,                                                  // input
7094
        info->compressedSize,                                   // inputSize
2,019,562,683✔
7095
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,019,590,233✔
7096
        output,                                                 // output
7097
        outputSize,                                             // output size
7098
        info->cmprAlg,                                          // compression algorithm
2,019,614,887✔
7099
        buffer->data,                                           // helper buffer
7100
        buffer->capacity                                        // extra buffer size
2,019,524,588✔
7101
    );
7102
    if (decompressedSize < 0) {
2,019,418,928✔
7103
      tBufferDestroy(&local);
7104
      return TSDB_CODE_COMPRESS_ERROR;
×
7105
    }
7106

7107
    if (!(decompressedSize == info->originalSize)) {
2,019,418,928✔
7108
      return TSDB_CODE_COMPRESS_ERROR;
×
7109
    }
7110
    tBufferDestroy(&local);
7111
  } else {
7112
    DEFINE_VAR(info->cmprAlg);
2,140,649,026✔
7113
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
2,140,692,748✔
7114
      (void)memcpy(output, input, info->compressedSize);
12,810✔
7115
      return 0;
12,810✔
7116
    }
7117
    SBuffer local;
2,140,669,403✔
7118

7119
    tBufferInit(&local);
7120
    if (buffer == NULL) {
2,140,630,984✔
7121
      buffer = &local;
×
7122
    }
7123
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,140,630,984✔
7124
    if (code) {
2,140,571,759✔
7125
      return code;
×
7126
    }
7127

7128
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7129
        input,                                                  // input
7130
        info->compressedSize,                                   // inputSize
2,140,710,854✔
7131
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,140,707,417✔
7132
        output,                                                 // output
7133
        outputSize,                                             // output size
7134
        info->cmprAlg,                                          // compression algorithm
2,140,696,559✔
7135
        buffer->data,                                           // helper buffer
7136
        buffer->capacity                                        // extra buffer size
2,140,671,187✔
7137
    );
7138
    if (decompressedSize < 0) {
2,140,563,305✔
7139
      tBufferDestroy(&local);
7140
      return TSDB_CODE_COMPRESS_ERROR;
×
7141
    }
7142

7143
    if (!(decompressedSize == info->originalSize)) {
2,140,563,305✔
7144
      return TSDB_CODE_COMPRESS_ERROR;
×
7145
    }
7146
    tBufferDestroy(&local);
7147
  }
7148

7149
  return 0;
2,147,483,647✔
7150
}
7151

7152
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,001,815,563✔
7153
  int32_t code;
7154

7155
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
1,001,815,563✔
7156
  if (code) return code;
1,001,759,819✔
7157

7158
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,001,759,819✔
7159
  if (code) return code;
1,001,829,313✔
7160

7161
  output->size += info->compressedSize;
1,001,829,313✔
7162
  return 0;
1,001,864,392✔
7163
}
7164

7165
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
2,019,406,259✔
7166
  int32_t code;
7167

7168
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
2,019,406,259✔
7169
  if (code) return code;
2,018,754,100✔
7170

7171
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
2,018,754,100✔
7172
  if (code) return code;
2,019,429,814✔
7173

7174
  output->size += info->originalSize;
2,019,429,814✔
7175
  return 0;
2,019,470,610✔
7176
}
7177

7178
// handle all types, including var data
7179
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7180
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7181
    pVal->pData = pDatum;
87,081,969✔
7182
    pVal->nData = len;
523,856,074✔
7183
  } else {
7184
    switch (len) {
2,147,483,647✔
7185
      case sizeof(uint8_t):
2,147,483,647✔
7186
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7187
        break;
2,147,483,647✔
7188
      case sizeof(uint16_t):
2,147,483,647✔
7189
        pVal->val = *(uint16_t *)pDatum;
2,147,483,647✔
7190
        break;
2,147,483,647✔
7191
      case sizeof(uint32_t):
2,147,483,647✔
7192
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7193
        break;
2,147,483,647✔
7194
      case sizeof(uint64_t):
2,147,483,647✔
7195
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7196
        break;
2,147,483,647✔
7197
      default:
×
7198
        break;
×
7199
    }
7200
  }
7201
}
2,147,483,647✔
7202

7203
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
1,835,666,710✔
7204
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
1,835,666,710✔
7205
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
202,964,542✔
7206
    pDst->nData = pSrc->nData;
203,367,178✔
7207
  } else {
7208
    pDst->val = pSrc->val;
1,632,702,168✔
7209
  }
7210
}
1,836,065,182✔
7211
void valueClearDatum(SValue *pVal, int8_t type) {
×
7212
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7213
    taosMemoryFreeClear(pVal->pData);
×
7214
    pVal->nData = 0;
×
7215
  } else {
7216
    pVal->val = 0;
×
7217
  }
7218
}
×
7219

7220
int8_t schemaHasBlob(const STSchema *pSchema) {
714,983,258✔
7221
  if (pSchema == NULL) {
714,983,258✔
7222
    return 0;
×
7223
  }
7224
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7225
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7226
      return 1;
59,254✔
7227
    }
7228
  }
7229
  return 0;
715,036,395✔
7230
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc