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

taosdata / TDengine / #4877

11 Dec 2025 02:43AM UTC coverage: 64.586% (-0.05%) from 64.632%
#4877

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

673 existing lines in 130 files now uncovered.

163673 of 253417 relevant lines covered (64.59%)

105540806.95 hits per line

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

63.64
/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) {
215,755,015✔
79
  int32_t n = 0;
215,755,015✔
80
  n += tPutI8(p ? p + n : p, index->type);
215,755,015✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
215,780,755✔
82
  return n;
215,770,615✔
83
}
84

85
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
1,236,719,132✔
86
  int32_t n = 0;
1,236,719,132✔
87
  n += tGetI8(p + n, &index->type);
1,236,719,132✔
88
  n += tGetU32v(p + n, &index->offset);
1,236,917,841✔
89
  return n;
1,237,385,916✔
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;
71,928,045✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
71,929,085✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
71,931,165✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
71,930,385✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
71,929,865✔
115
    sinfo->numOfPKs++;
71,876,305✔
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
51,233,098✔
122
                             + BSE_SEQUECE_SIZE;                     // value
25,616,549✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
51,233,098✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
25,616,549✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
51,233,098✔
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;
99,772✔
164
        break;
49,886✔
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) {
1,020✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
2,040✔
185
        break;
1,020✔
186
      } else {  // skip useless value
187
        colValIndex++;
×
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,012,465,369✔
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):
5,941,194✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
5,941,194✔
216
      sinfo->tupleFixedSize = 0;
5,941,194✔
217
      break;
5,941,005✔
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,124,895,786✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,124,895,786✔
225
      sinfo->tupleFixedSize = schema->flen;
1,124,894,652✔
226
      break;
926,073,682✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
71,923,885✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
71,931,165✔
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) {
230,580,967✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
234,549,127✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
234,561,599✔
245
  } else {
246
    sinfo->kvFlag = (KV_FLG_BIG | sinfo->flag);
167✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint32_t);
167✔
248
  }
249
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
250
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
71,919,725✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
71,931,165✔
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);
66,561,294✔
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);
14,570✔
296
        break;
14,570✔
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
934,469,907✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
866,093,103✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
82,864,766✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
82,878,431✔
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
12,478,169✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
1,020✔
325
        break;
1,020✔
326
      } else {
327
        colValIndex++;
12,477,149✔
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,634,548✔
336
  SBlobItem item = {.type = type, .len = 0};
2,634,548✔
337
  uint64_t  seq = 0;
2,634,548✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
2,634,548✔
339
  if (code != 0) return code;
2,634,548✔
340

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

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

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

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

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

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

395
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
396
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
397
              hasBlob = 1;
×
398
            }
399
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
×
400
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
×
401
            if (colValArray[colValIndex].value.nData) {
×
402
              if (hasBlob == 0) {
×
403
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
×
404
                varlen += colValArray[colValIndex].value.nData;
×
405
              } else {
406
                uint64_t  seq = 0;
×
407
                SBlobItem item = {.seqOffsetInRow = varlen - (*ppRow)->data,
×
408
                                  .data = colValArray[colValIndex].value.pData,
×
409
                                  .len = colValArray[colValIndex].value.nData,
×
410
                                  .type = TSDB_DATA_BLOB_VALUE};
411
                code = tBlobSetPush(pBlobSet, &item, &seq, firstBlobCol);
×
412
                if (firstBlobCol == 1) {
×
413
                  firstBlobCol = 0;
×
414
                }
415
                if (code != 0) return code;
×
416
                varlen += tPutU64(varlen, seq);
×
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,
×
423
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
×
424
                         tDataTypes[schema->columns[i].type].bytes);
×
425
          }
426
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
427
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
×
428
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
429
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
430
          }
431
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
432
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
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);
×
436
        }
437

438
        colValIndex++;
×
439
        break;
×
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) {
×
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
×
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) {
382,872,733✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
382,872,733✔
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);
5,368,571✔
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;
35,316✔
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);
1,107,566,838✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
1,107,934,795✔
617
            if (colValArray[colValIndex].value.nData > 0) {
1,108,004,178✔
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
1,081,695,730✔
619
                           colValArray[colValIndex].value.nData);
1,081,689,449✔
620
              payloadSize += colValArray[colValIndex].value.nData;
1,081,699,125✔
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);
1,938,173,586✔
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
200✔
637
        break;
×
638
      } else {
639
        colValIndex++;
200✔
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,
25,616,549✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
25,616,549✔
649

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

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

664
  if (((*ppRow)->flag) == 0) {
25,616,549✔
665
    return TSDB_CODE_INVALID_PARA;
×
666
  }
667

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

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

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
25,616,549✔
679
  int32_t colValIndex = 1;
25,616,549✔
680
  int8_t  firstBlobCol = 1;
25,616,549✔
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
51,233,098✔
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
25,616,549✔
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
25,616,549✔
687

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
25,616,549✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
25,616,549✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
25,616,549✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
25,616,549✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
25,616,549✔
693
              hasBlob = 1;
25,616,549✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
25,616,549✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
25,616,549✔
697
            if (colValArray[colValIndex].value.nData > 0) {
25,616,549✔
698
              if (hasBlob == 0) {
23,523,465✔
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
×
700
                             colValArray[colValIndex].value.nData);
×
701
                payloadSize += colValArray[colValIndex].value.nData;
×
702
              } else {
703
                uint64_t  seq = 0;
23,523,465✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
23,523,465✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
23,523,465✔
706
                                  .data = colValArray[colValIndex].value.pData,
23,523,465✔
707
                                  .len = colValArray[colValIndex].value.nData,
23,523,465✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
23,523,465✔
710
                if (code != 0) return code;
23,523,465✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
47,046,930✔
712
              }
713
            } else {
714
              if (hasBlob) {
2,093,084✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
2,093,084✔
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
720
            (void)memcpy(payload + payloadSize, &colValArray[colValIndex].value.val,
×
721
                         tDataTypes[schema->columns[i].type].bytes);
×
722
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
×
723
          }
724
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
725
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
726
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
×
727
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
728
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
729
          }
730
        }
731
        colValIndex++;
25,616,549✔
732
        break;
25,616,549✔
733
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
734
        break;
×
735
      } else {
736
        colValIndex++;
×
737
      }
738
    }
739
  }
740

741
  if (((*ppRow)->flag) == 0) {
25,616,549✔
742
    return TSDB_CODE_INVALID_PARA;
×
743
  }
744
  return 0;
25,616,549✔
745
}
746

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

892
  p->cap = cap;
×
893
  p->len = 0;
×
894
  p->seq = 1;
×
895

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

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

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

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

941
  offset = pBlobRow->len;
×
942
  pBlobRow->len += len;
×
943

944
  pBlobRow->seq++;
×
945
  *seq = pBlobRow->seq;
×
946

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

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

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

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

981
  int32_t code = 0;
×
982
  int32_t len = 0;
×
983
  int32_t dataOffset = 0;
×
984
  int8_t  nextRow = 0;
×
985

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

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

996
  len = value->len;
×
997
  dataOffset = value->dataOffset;
×
998
  nextRow = value->nextRow;
×
999

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

1004
  return code;
×
1005
}
1006

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

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

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

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

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

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

1048
  return code;
×
1049
}
1050

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

1073
int32_t tRowBuildWithBlob(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SBlobSet *pBlobSet,
26,150,103✔
1074
                          SRowBuildScanInfo *sinfo) {
1075
  int32_t code;
1076

1077
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
26,150,103✔
1078
  if (code) return code;
26,150,103✔
1079

1080
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
26,150,103✔
1081
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
533,554✔
1082
  } else {
1083
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
25,616,549✔
1084
  }
1085

1086
  return code;
26,150,103✔
1087
}
1088

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

1093
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
×
1094
  if (code) return code;
×
1095

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

1102
  return code;
×
1103
}
1104

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

1113
  p->type = type;
23,507✔
1114
  p->pSeqTable = taosArrayInit(128, sizeof(SBlobValue));
23,507✔
1115
  if (p->pSeqTable == NULL) {
23,507✔
1116
    TAOS_CHECK_EXIT(terrno);
×
1117
  }
1118

1119
  p->data = taosMemCalloc(1024, cap * sizeof(uint8_t));
23,507✔
1120
  if (p->data == NULL) {
23,507✔
1121
    TAOS_CHECK_EXIT(terrno);
×
1122
  }
1123

1124
  p->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
23,507✔
1125
  if (p->pSeqToffset == NULL) {
23,507✔
1126
    TAOS_CHECK_EXIT(terrno);
×
1127
  }
1128

1129
  p->pSet = taosArrayInit(4, sizeof(int32_t));
23,507✔
1130
  if (p->pSet == NULL) {
23,507✔
1131
    TAOS_CHECK_EXIT(terrno);
×
1132
  }
1133

1134
  p->cap = cap;
23,507✔
1135
  p->len = 0;
23,507✔
1136
  p->seq = 1;
23,507✔
1137

1138
  *ppBlobSet = p;
23,507✔
1139
_exit:
23,507✔
1140
  if (code != 0) {
23,507✔
1141
    taosHashCleanup(p->pSeqToffset);
×
1142
    taosArrayDestroy(p->pSeqTable);
×
1143
    taosArrayDestroy(p->pSet);
×
1144

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

1164
  uint64_t tlen = pBlobSet->len + len;
26,158,013✔
1165
  if (tlen > pBlobSet->cap) {
26,158,013✔
1166
    int64_t cap = pBlobSet->cap;
21,148✔
1167
    // opt later
1168
    do {
1169
      cap = cap * 2;
21,148✔
1170
    } while (tlen > cap);
21,148✔
1171

1172
    uint8_t *data = taosMemRealloc(pBlobSet->data, cap);
21,148✔
1173
    if (data == NULL) {
21,148✔
1174
      return terrno;
×
1175
    }
1176
    pBlobSet->data = data;
21,148✔
1177
    pBlobSet->cap = cap;
21,148✔
1178
  }
1179
  if (len > 0) {
26,158,013✔
1180
    (void)memcpy(pBlobSet->data + pBlobSet->len, data, len);
23,523,465✔
1181
  }
1182

1183
  offset = pBlobSet->len;
26,158,013✔
1184
  pBlobSet->len += len;
26,158,013✔
1185

1186
  pBlobSet->seq++;
26,158,013✔
1187
  *seq = pBlobSet->seq;
26,158,013✔
1188

1189
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow, .type = pItem->type};
26,158,013✔
1190
  if (taosArrayPush(pBlobSet->pSeqTable, &value) == NULL) {
52,316,026✔
1191
    TAOS_CHECK_EXIT(terrno);
×
1192
  }
1193

1194
  int32_t sz = taosArrayGetSize(pBlobSet->pSeqTable);
26,158,013✔
1195
  code = taosHashPut(pBlobSet->pSeqToffset, seq, sizeof(int64_t), &sz, sizeof(int32_t));
26,158,013✔
1196
  if (code != 0) {
26,158,013✔
1197
    TAOS_CHECK_EXIT(code);
×
1198
  }
1199

1200
_exit:
26,158,013✔
1201
  return code;
26,158,013✔
1202
}
1203

1204
void tBlobSetSwap(SBlobSet *p1, SBlobSet *p2) {
791✔
1205
  SBlobSet t = {0};
791✔
1206

1207
  memcpy(&t, p1, sizeof(SBlobSet));
791✔
1208

1209
  memcpy(p1, p2, sizeof(SBlobSet));
791✔
1210
  memcpy(p2, &t, sizeof(SBlobSet));
791✔
1211
}
791✔
1212

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

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

1233
  int32_t code = 0;
×
1234
  int32_t len = 0;
×
1235
  int32_t dataOffset = 0;
×
1236
  int8_t  nextRow = 0;
×
1237

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

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

1248
  len = value->len;
×
1249
  dataOffset = value->dataOffset;
×
1250
  nextRow = value->nextRow;
×
1251

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

1257
  return code;
×
1258
}
1259

1260
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
1,116,034,188✔
1261
  if (pBlobSet == NULL) return 0;
1,116,034,188✔
1262
  return taosArrayGetSize(pBlobSet->pSeqTable);
80,997✔
1263
}
1264

1265
void tBlobSetDestroy(SBlobSet *pBlobSet) {
1,679,618✔
1266
  if (pBlobSet == NULL) return;
1,679,618✔
1267
  uTrace("destroy blob row, seqTable size %p", pBlobSet);
45,881✔
1268
  taosMemoryFree(pBlobSet->data);
45,881✔
1269
  taosArrayDestroy(pBlobSet->pSeqTable);
46,223✔
1270
  taosHashCleanup(pBlobSet->pSeqToffset);
46,223✔
1271
  taosArrayDestroy(pBlobSet->pSet);
46,223✔
1272
  taosMemoryFree(pBlobSet);
46,223✔
1273
}
1274
int32_t tBlobSetClear(SBlobSet *pBlobSet) {
×
1275
  if (pBlobSet == NULL) return 0;
×
1276
  int32_t code = 0;
×
1277
  uTrace("clear blob row, seqTable size %p", pBlobSet);
×
1278
  taosArrayClear(pBlobSet->pSeqTable);
×
1279
  taosHashClear(pBlobSet->pSeqToffset);
×
1280
  pBlobSet->len = 0;
×
1281
  pBlobSet->seq = 1;
×
1282
  return code;
×
1283
}
1284

1285
static int32_t tBindInfoCompare(const void *p1, const void *p2, const void *param) {
×
1286
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
×
1287
    return -1;
×
1288
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
×
1289
    return 1;
×
1290
  }
1291
  return 0;
×
1292
}
1293

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

1309
  if (!infoSorted) {
13,167,293✔
1310
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1311
  }
1312

1313
  int32_t code = 0;
13,167,293✔
1314
  int32_t numOfRows = infos[0].bind->num;
13,167,293✔
1315
  SArray *colValArray;
1316
  SColVal colVal;
12,963,421✔
1317

1318
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
13,170,070✔
1319
    return terrno;
×
1320
  }
1321

1322
  SRowKey rowKey, lastRowKey;
12,961,511✔
1323
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
2,147,483,647✔
1324
    taosArrayClear(colValArray);
2,147,483,647✔
1325

1326
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
2,147,483,647✔
1327
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
2,147,483,647✔
1328
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
8,320✔
1329
      } else {
1330
        SValue value = {
2,147,483,647✔
1331
            .type = infos[iInfo].type,
2,147,483,647✔
1332
        };
1333
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
2,147,483,647✔
1334
          value.nData = infos[iInfo].bind->length[iRow];
38,062,313✔
1335
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
48,356,460✔
1336
            code = TSDB_CODE_INVALID_PARA;
×
1337
            goto _exit;
×
1338
          }
1339
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
48,356,775✔
1340
        } else {
1341
          valueSetDatum(&value, infos[iInfo].type,
2,147,483,647✔
1342
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
2,147,483,647✔
1343
                        infos[iInfo].bind->buffer_length);
2,147,483,647✔
1344
        }
1345
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
2,147,483,647✔
1346
      }
1347
      if (taosArrayPush(colValArray, &colVal) == NULL) {
2,147,483,647✔
1348
        code = terrno;
×
1349
        goto _exit;
×
1350
      }
1351
    }
1352

1353
    SRow             *row;
2,147,483,647✔
1354
    SRowBuildScanInfo sinfo = {0};
2,147,483,647✔
1355
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
2,147,483,647✔
1356
      goto _exit;
×
1357
    }
1358

1359
    if ((taosArrayPush(rowArray, &row)) == NULL) {
2,147,483,647✔
1360
      code = terrno;
×
1361
      goto _exit;
×
1362
    }
1363

1364
    if (pOrdered && pDupTs) {
2,147,483,647✔
1365
      tRowGetKey(row, &rowKey);
2,147,483,647✔
1366
      if (iRow == 0) {
2,147,483,647✔
1367
        *pOrdered = true;
13,168,957✔
1368
        *pDupTs = false;
13,168,383✔
1369
      } else {
1370
        if (*pOrdered) {
2,147,483,647✔
1371
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
2,147,483,647✔
1372
          *pOrdered = (res >= 0);
2,147,483,647✔
1373
          if (!*pDupTs) {
2,147,483,647✔
1374
            *pDupTs = (res == 0);
2,147,483,647✔
1375
          }
1376
        }
1377
      }
1378
      lastRowKey = rowKey;
2,147,483,647✔
1379
    }
1380
  }
1381

1382
_exit:
4,140,373✔
1383
  taosArrayDestroy(colValArray);
12,964,569✔
1384
  return code;
13,159,217✔
1385
}
1386

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

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

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

1401
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1402
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
12,741,866✔
1403
    return 0;
12,740,107✔
1404
  }
1405

1406
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1407
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
311,205,611✔
1408
    return 0;
311,206,146✔
1409
  }
1410

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

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

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

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

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

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

1453
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
1454
            int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
1,145,031,977✔
1455

1456
            pData += tGetU32v(pData, &pColVal->value.nData);
2,147,483,647✔
1457
            if (pColVal->value.nData > 0) {
1,144,760,482✔
1458
              pColVal->value.pData = pData;
1,140,543,282✔
1459
            } else {
1460
              pColVal->value.pData = NULL;
4,236,030✔
1461
            }
1462
            if (isBlob == 1) {
1,144,718,815✔
1463
              pData += BSE_SEQUECE_SIZE;  // skip seq
32,321,709✔
1464
            }
1465
          } else {
1466
            valueSetDatum(&pColVal->value, pTColumn->type, pData, pTColumn->bytes);
2,147,483,647✔
1467
          }
1468
        }
1469
        return 0;
2,147,483,647✔
1470
      } else if (TABS(cid) < pTColumn->colId) {
2,147,483,647✔
1471
        lidx = mid + 1;
2,147,483,647✔
1472
      } else {
1473
        ridx = mid - 1;
2,147,483,647✔
1474
      }
1475
    }
1476

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

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

1494
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
2,147,483,647✔
1495
        if (bit) bit++;
9,035,839✔
1496
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
2,147,483,647✔
1497
        bit++;
2,147,483,647✔
1498
      }
1499
    }
1500
    varlen = fixed + pTSchema->flen;
2,147,483,647✔
1501

1502
    if (bit == BIT_FLG_NONE) {
2,147,483,647✔
1503
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,264,103✔
1504
      return 0;
1,264,103✔
1505
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1506
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
2,147,483,647✔
1507
      return 0;
2,147,483,647✔
1508
    }
1509

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

1525
  return 0;
2,147,483,647✔
1526
}
1527

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

1532
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
1,192,468,035✔
1533
  SRowKey key1, key2;
1,187,769,801✔
1534
  tRowGetKey(*(SRow **)p1, &key1);
2,147,483,647✔
1535
  tRowGetKey(*(SRow **)p2, &key2);
2,147,483,647✔
1536
  return tRowKeyCompare(&key1, &key2);
1,192,909,917✔
1537
}
1538
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
47,076,648✔
1539

1540
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
21,465,437✔
1541
  SColVal* pColVal = tRowIterNext(iter);
21,465,437✔
1542
  while (pColVal != NULL && pColVal->cid < targetCid) {
24,457,360✔
1543
    pColVal = tRowIterNext(iter);
2,991,948✔
1544
  }
1545
  return pColVal;
21,465,412✔
1546
}
1547

1548
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, ERowMergeStrategy strategy) {
2,980,088✔
1549
  int32_t code = 0;
2,980,088✔
1550

1551
  int32_t    nRow = iEnd - iStart;
2,980,088✔
1552
  SRowIter **aIter = NULL;
2,980,088✔
1553
  SArray    *aColVal = NULL;
2,980,088✔
1554
  SRow      *pRow = NULL;
2,980,088✔
1555
  uint8_t    hasBlob = 0;
2,980,088✔
1556
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
2,980,088✔
1557
  if (aIter == NULL) {
2,980,088✔
1558
    code = terrno;
×
1559
    goto _exit;
×
1560
  }
1561

1562
  for (int32_t i = 0; i < nRow; i++) {
50,056,514✔
1563
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
47,076,870✔
1564

1565
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
47,076,870✔
1566
    if (code) goto _exit;
47,076,426✔
1567
  }
1568

1569
  // merge
1570
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
2,979,644✔
1571
  if (aColVal == NULL) {
2,980,088✔
1572
    code = terrno;
×
1573
    goto _exit;
×
1574
  }
1575

1576
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
22,453,624✔
1577
    int32_t targetCid = pTSchema->columns[iCol].colId;
19,455,776✔
1578
    SColVal *pColVal = NULL;
19,456,220✔
1579

1580
    switch (strategy) {
19,456,220✔
1581
      case KEEP_CONSISTENCY:
518,152✔
1582
        if (nRow > 0)
518,152✔
1583
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
518,152✔
1584
        break;
518,571✔
1585

1586
      default:  // default using PREFER_NON_NULL strategy
18,938,068✔
1587
      case PREFER_NON_NULL:
1588
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
20,966,094✔
1589
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
20,930,635✔
1590

1591
          if (COL_VAL_IS_VALUE(pColValT)) {
20,946,619✔
1592
            pColVal = pColValT;
18,918,593✔
1593
            break;
18,918,593✔
1594
          } else if (pColVal == NULL) {
2,028,026✔
1595
            pColVal = pColValT;
1,992,927✔
1596
          }
1597
        }
1598
        break;
18,954,052✔
1599
    }
1600

1601
    if (pColVal) {
19,472,623✔
1602
      if (taosArrayPush(aColVal, pColVal) == NULL) {
19,457,108✔
1603
        code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
    }
1607
  }
1608

1609
  // build
1610
  SRowBuildScanInfo sinfo = {0};
2,978,756✔
1611
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
2,980,088✔
1612

1613
  if (code) goto _exit;
2,980,088✔
1614

1615
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
2,980,088✔
1616
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
2,979,866✔
1617
    code = terrno;
×
1618
    goto _exit;
×
1619
  }
1620

1621
_exit:
2,979,866✔
1622
  if (aIter) {
2,980,088✔
1623
    for (int32_t i = 0; i < nRow; i++) {
50,056,736✔
1624
      tRowIterClose(&aIter[i]);
47,076,648✔
1625
    }
1626
    taosMemoryFree(aIter);
2,980,088✔
1627
  }
1628
  if (aColVal) taosArrayDestroy(aColVal);
2,980,310✔
1629
  if (code) tRowDestroy(pRow);
2,980,088✔
1630
  return code;
2,980,088✔
1631
}
1632
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
7,910✔
1633
  int32_t code = 0;
7,910✔
1634
  int32_t lino = 0;
7,910✔
1635
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
15,820✔
1636
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
7,910✔
1637
    code = addEmptyItemToBlobSet(pDst, type, NULL);
7,910✔
1638
    TAOS_CHECK_GOTO(code, &lino, _error);
7,910✔
1639
  } else {
1640
    uint64_t seq = 0;
×
1641
    if (tGetU64(pVal->value.pData, &seq) < 0) {
×
1642
      uError("tBlobSetTransferTo: invalid blob value, seq %p", pVal->value.pData);
×
1643
      return TSDB_CODE_INVALID_PARA;
×
1644
    }
1645

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

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

1658
_error:
7,910✔
1659
  return code;
7,910✔
1660
}
1661

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

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

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

1678
  for (int32_t i = 0; i < nRow; i++) {
8,701✔
1679
    SRow *pRowT = taosArrayGetP(aRowP, 0 + i);
7,910✔
1680
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
7,910✔
1681
    TAOS_CHECK_GOTO(code, &lino, _error);
7,910✔
1682
  }
1683

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

1699
  code = tBlobSetCreate(pBlob->cap, pBlob->type, &pTempBlob);
791✔
1700
  TAOS_CHECK_GOTO(code, &lino, _error);
791✔
1701

1702
  for (int32_t i = 0; i < taosArrayGetSize(aColVal); i++) {
8,701✔
1703
    uint64_t seq = 0;
7,910✔
1704
    SColVal *pVal = taosArrayGet(aColVal, i);
7,910✔
1705

1706
    code = tBlobSetTransferTo(pBlob, pTempBlob, pVal);
7,910✔
1707
    TAOS_CHECK_GOTO(code, &lino, _error);
7,910✔
1708
  }
1709

1710
  tBlobSetSwap(pBlob, pTempBlob);
791✔
1711

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

1717
  if (aIter) {
791✔
1718
    for (int32_t i = 0; i < nRow; i++) {
8,701✔
1719
      tRowIterClose(&aIter[i]);
7,910✔
1720
    }
1721
    taosMemoryFree(aIter);
791✔
1722
  }
1723
  if (aColVal) {
791✔
1724
    taosArrayDestroy(aColVal);
791✔
1725
  }
1726
  tBlobSetDestroy(pTempBlob);
791✔
1727
  return code;
791✔
1728
}
1729

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

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

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

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

1755
    tRowGetKey(row1, &key1);
×
1756

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

1763
      if (tRowKeyCompare(&key1, &key2) != 0) break;
×
1764

1765
      iEnd++;
×
1766
    }
1767

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

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

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

1788
  tBlobSetDestroy(pTempBlobSet);
×
1789
  return code;
×
1790
}
1791

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

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

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

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

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

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

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

1848
  // build
1849

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

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

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

1870
  return code;
×
1871
}
1872

1873
int32_t tRowSort(SArray *aRowP) {
1,137,463✔
1874
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
1,137,463✔
1875
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
1,126,630✔
1876
  if (code != TSDB_CODE_SUCCESS) {
1,126,630✔
1877
    uError("taosArrayMSort failed caused by %d", code);
×
1878
  }
1879
  return code;
1,126,630✔
1880
}
1881

1882
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, ERowMergeStrategy strategy) {
1,163,088✔
1883
  int32_t code = 0;
1,163,088✔
1884

1885
  int32_t iStart = 0;
1,163,088✔
1886
  while (iStart < aRowP->size) {
119,608,418✔
1887
    SRowKey key1;
115,356,528✔
1888
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
118,444,904✔
1889

1890
    tRowGetKey(row1, &key1);
236,920,648✔
1891

1892
    int32_t iEnd = iStart + 1;
118,476,333✔
1893
    while (iEnd < aRowP->size) {
162,568,922✔
1894
      SRowKey key2;
158,831,449✔
1895
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
161,406,271✔
1896
      tRowGetKey(row2, &key2);
322,691,534✔
1897

1898
      if (tRowKeyCompare(&key1, &key2) != 0) break;
161,383,951✔
1899

1900
      iEnd++;
44,103,467✔
1901
    }
1902

1903
    if (iEnd - iStart > 1) {
118,449,129✔
1904
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
2,979,669✔
1905
      if (code) return code;
2,979,866✔
1906
    }
1907

1908
    // the array is also changing, so the iStart just ++ instead of iEnd
1909
    iStart++;
118,449,326✔
1910
  }
1911

1912
  return code;
1,163,292✔
1913
}
1914

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

1925
int8_t tRowNeedDoMerge(SArray *aRowP) {
791✔
1926
  if (aRowP == NULL || aRowP->size <= 1) return 0;
791✔
1927

1928
  int8_t  doMerge = 0;
791✔
1929
  int32_t iStart = 0;
791✔
1930
  while (iStart < aRowP->size) {
8,701✔
1931
    SRowKey key1;
7,910✔
1932
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
7,910✔
1933

1934
    tRowGetKey(row1, &key1);
15,820✔
1935

1936
    int32_t iEnd = iStart + 1;
7,910✔
1937
    while (iEnd < aRowP->size) {
7,910✔
1938
      SRowKey key2;
7,119✔
1939
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
7,119✔
1940
      tRowGetKey(row2, &key2);
14,238✔
1941

1942
      if (tRowKeyCompare(&key1, &key2) != 0) break;
7,119✔
1943

1944
      iEnd++;
×
1945
    }
1946

1947
    if (iEnd - iStart > 1) {
7,910✔
1948
      doMerge = 1;
×
1949
      break;
×
1950
    }
1951
    iStart++;
7,910✔
1952
  }
1953
  return doMerge;
791✔
1954
}
1955

1956
int32_t tRowMergeWithBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlobSet, int8_t flag) {
791✔
1957
  int32_t code = 0;
791✔
1958

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

1966
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1967
  }
1968
  return code;
791✔
1969
}
1970

1971
// SRowIter ========================================
1972
struct SRowIter {
1973
  SRow     *pRow;
1974
  STSchema *pTSchema;
1975

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

1991
int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
82,891,947✔
1992
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
82,891,947✔
1993

1994
  int32_t code = 0;
83,407,064✔
1995

1996
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
83,407,064✔
1997
  if (pIter == NULL) {
83,157,998✔
1998
    code = terrno;
×
1999
    goto _exit;
×
2000
  }
2001

2002
  pIter->pRow = pRow;
83,157,998✔
2003
  pIter->pTSchema = pTSchema;
83,177,836✔
2004
  pIter->iTColumn = 0;
83,198,529✔
2005

2006
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
83,256,918✔
2007

2008
  uint8_t         *data = pRow->data;
83,004,052✔
2009
  SPrimaryKeyIndex index;
82,814,570✔
2010
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
84,462,642✔
2011
    data += tGetPrimaryKeyIndex(data, &index);
1,573,036✔
2012
  }
2013

2014
  if (pRow->flag >> 4) {
83,006,820✔
2015
    pIter->iCol = 0;
34,970,856✔
2016
    pIter->pIdx = (SKVIdx *)data;
35,243,792✔
2017
    if (pRow->flag & KV_FLG_LIT) {
35,250,768✔
2018
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
35,305,050✔
2019
    } else if (pRow->flag & KV_FLG_MID) {
×
2020
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
×
2021
    } else {
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
2023
    }
2024
  } else {
2025
    switch (pRow->flag) {
48,054,716✔
2026
      case (HAS_NULL | HAS_NONE):
618✔
2027
        pIter->pb = data;
618✔
2028
        break;
618✔
2029
      case HAS_VALUE:
47,633,320✔
2030
        pIter->pf = data;
47,633,320✔
2031
        pIter->pv = pIter->pf + pTSchema->flen;
47,633,538✔
2032
        break;
47,633,320✔
2033
      case (HAS_VALUE | HAS_NONE):
179,302✔
2034
      case (HAS_VALUE | HAS_NULL):
2035
        pIter->pb = data;
179,302✔
2036
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
179,302✔
2037
        pIter->pv = pIter->pf + pTSchema->flen;
179,302✔
2038
        break;
179,302✔
2039
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
218✔
2040
        pIter->pb = data;
218✔
2041
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2042
        pIter->pv = pIter->pf + pTSchema->flen;
×
2043
        break;
×
2044
      default:
×
2045
        break;
×
2046
    }
2047
  }
2048

2049
_exit:
83,252,712✔
2050
  if (code) {
83,215,216✔
2051
    *ppIter = NULL;
×
2052
  } else {
2053
    *ppIter = pIter;
83,215,216✔
2054
  }
2055
  return code;
83,286,737✔
2056
}
2057

2058
void tRowIterClose(SRowIter **ppIter) {
83,455,896✔
2059
  SRowIter *pIter = *ppIter;
83,455,896✔
2060
  if (pIter) {
83,466,796✔
2061
    taosMemoryFree(pIter);
83,482,274✔
2062
  }
2063
  *ppIter = NULL;
83,300,275✔
2064
}
83,313,787✔
2065

2066
SColVal *tRowIterNext(SRowIter *pIter) {
457,713,415✔
2067
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
457,713,415✔
2068
    return NULL;
35,836,440✔
2069
  }
2070

2071
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
432,292,973✔
2072

2073
  // timestamp
2074
  if (0 == pIter->iTColumn) {
434,502,488✔
2075
    pIter->cv.cid = pTColumn->colId;
40,445,229✔
2076
    pIter->cv.value.type = pTColumn->type;
40,460,271✔
2077
    pIter->cv.flag = CV_FLAG_VALUE;
40,391,869✔
2078
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
40,408,169✔
2079
    goto _exit;
40,428,661✔
2080
  }
2081

2082
  if (pIter->pRow->flag == HAS_NONE) {
394,970,817✔
2083
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
3,094,064✔
2084
    goto _exit;
3,094,064✔
2085
  }
2086

2087
  if (pIter->pRow->flag == HAS_NULL) {
391,632,761✔
2088
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
396,973✔
2089
    goto _exit;
396,973✔
2090
  }
2091

2092
  if (pIter->pRow->flag >> 4) {  // KV
391,341,367✔
2093
    if (pIter->iCol < pIter->pIdx->nCol) {
373,970,079✔
2094
      uint8_t *pData;
2095

2096
      if (pIter->pRow->flag & KV_FLG_LIT) {
347,761,917✔
2097
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
348,903,583✔
2098
      } else if (pIter->pRow->flag & KV_FLG_MID) {
×
2099
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
×
2100
      } else {
2101
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
2102
      }
2103

2104
      int16_t cid;
349,142,293✔
2105
      pData += tGetI16v(pData, &cid);
348,718,065✔
2106

2107
      if (TABS(cid) == pTColumn->colId) {
348,791,313✔
2108
        if (cid < 0) {
345,103,075✔
2109
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
67,596,373✔
2110
        } else {
2111
          pIter->cv.cid = pTColumn->colId;
277,506,702✔
2112
          pIter->cv.value.type = pTColumn->type;
281,357,820✔
2113
          pIter->cv.flag = CV_FLAG_VALUE;
281,382,672✔
2114

2115
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
281,443,930✔
2116
            pData += tGetU32v(pData, &pIter->cv.value.nData);
137,040,633✔
2117
            if (pIter->cv.value.nData > 0) {
69,316,544✔
2118
              pIter->cv.value.pData = pData;
69,482,224✔
2119
            } else {
2120
              pIter->cv.value.pData = NULL;
×
2121
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2122
              //   pIter->cv.value.pData = pData;
2123
              // }
2124
            }
2125
          } else {
2126
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
211,933,148✔
2127
          }
2128
        }
2129

2130
        pIter->iCol++;
347,945,795✔
2131
        goto _exit;
345,832,869✔
2132
      } else if (TABS(cid) > pTColumn->colId) {
44,656✔
2133
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
44,656✔
2134
        goto _exit;
44,656✔
2135
      } else {
2136
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2137
        goto _exit;
×
2138
      }
2139
    } else {
2140
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
26,281,297✔
2141
      goto _exit;
26,281,480✔
2142
    }
2143
  } else {  // Tuple
2144
    uint8_t bv = BIT_FLG_VALUE;
16,425,470✔
2145
    if (pIter->pb) {
16,425,470✔
2146
      switch (pIter->pRow->flag) {
741,763✔
2147
        case (HAS_NULL | HAS_NONE):
1,854✔
2148
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
1,854✔
2149
          break;
1,854✔
2150
        case (HAS_VALUE | HAS_NONE):
15,513✔
2151
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
15,513✔
2152
          if (bv) bv++;
15,513✔
2153
          break;
15,513✔
2154
        case (HAS_VALUE | HAS_NULL):
724,396✔
2155
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
724,396✔
2156
          break;
724,396✔
2157
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2158
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2159
          break;
×
2160
        default:
×
2161
          break;
×
2162
      }
2163

2164
      if (bv == BIT_FLG_NONE) {
741,763✔
2165
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,780✔
2166
        goto _exit;
7,780✔
2167
      } else if (bv == BIT_FLG_NULL) {
733,983✔
2168
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
128,526✔
2169
        goto _exit;
128,526✔
2170
      }
2171
    }
2172

2173
    pIter->cv.cid = pTColumn->colId;
16,289,449✔
2174
    pIter->cv.value.type = pTColumn->type;
16,289,449✔
2175
    pIter->cv.flag = CV_FLAG_VALUE;
16,289,030✔
2176
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
20,288,502✔
2177
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
3,999,116✔
2178
      pData += tGetU32v(pData, &pIter->cv.value.nData);
7,995,672✔
2179
      if (pIter->cv.value.nData > 0) {
3,999,715✔
2180
        pIter->cv.value.pData = pData;
3,921,613✔
2181
      } else {
2182
        pIter->cv.value.pData = NULL;
78,102✔
2183
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2184
        //   pIter->cv.value.pData = pData;
2185
        // }
2186
      }
2187
    } else {
2188
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
12,289,407✔
2189
    }
2190
    goto _exit;
16,288,561✔
2191
  }
2192

2193
_exit:
425,839,964✔
2194
  pIter->iTColumn++;
425,839,964✔
2195
  return &pIter->cv;
429,308,269✔
2196
}
2197

2198
static int32_t tRowNoneUpsertColData(SColData *aColData, int32_t nColData, int32_t flag) {
435,712✔
2199
  int32_t code = 0;
435,712✔
2200

2201
  if (flag) return code;
435,712✔
2202

2203
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
3,296,547✔
2204
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
2,965,379✔
2205
    if (code) return code;
2,965,379✔
2206
  }
2207

2208
  return code;
331,168✔
2209
}
2210
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
52,930,332✔
2211
  int32_t code = 0;
52,930,332✔
2212

2213
  int32_t   iColData = 0;
52,930,332✔
2214
  SColData *pColData = &aColData[iColData];
52,930,332✔
2215
  int32_t   iTColumn = 1;
52,930,780✔
2216
  STColumn *pTColumn = &pSchema->columns[iTColumn];
52,930,780✔
2217

2218
  while (pColData) {
366,192,041✔
2219
    if (pTColumn) {
313,230,797✔
2220
      if (pTColumn->colId == pColData->cid) {  // NULL
313,230,797✔
2221
        if (flag == 0) {
313,388,250✔
2222
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
313,388,250✔
2223
        } else {
2224
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2225
        }
2226
        if (code) goto _exit;
313,267,807✔
2227

2228
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
313,267,807✔
2229
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
313,325,599✔
2230
      } else if (pTColumn->colId > pColData->cid) {  // NONE
×
2231
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2232
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2233
      } else {
2234
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
×
2235
      }
2236
    } else {  // NONE
2237
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2238
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2239
    }
2240
  }
2241

2242
_exit:
52,961,244✔
2243
  return code;
52,961,244✔
2244
}
2245
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
2,147,483,647✔
2246
                                      int32_t flag) {
2247
  int32_t code = 0;
2,147,483,647✔
2248

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

2254
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
2,147,483,647✔
2255
  SPrimaryKeyIndex index;
2,147,483,647✔
2256
  uint8_t         *data = pRow->data;
2,147,483,647✔
2257
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
2258
    data += tGetPrimaryKeyIndex(data, &index);
42,608,602✔
2259
  }
2260

2261
  switch (pRow->flag) {
2,147,483,647✔
2262
    case HAS_VALUE:
2,147,483,647✔
2263
      pf = data;  // TODO: fix here
2,147,483,647✔
2264
      pv = pf + pTSchema->flen;
2,147,483,647✔
2265
      break;
2,147,483,647✔
2266
    case (HAS_NULL | HAS_NONE):
59,754✔
2267
      pb = data;
59,754✔
2268
      break;
59,754✔
2269
    case (HAS_VALUE | HAS_NONE):
420,806,735✔
2270
    case (HAS_VALUE | HAS_NULL):
2271
      pb = data;
420,806,735✔
2272
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
420,806,735✔
2273
      pv = pf + pTSchema->flen;
420,771,522✔
2274
      break;
420,787,426✔
UNCOV
2275
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
UNCOV
2276
      pb = data;
×
UNCOV
2277
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2278
      pv = pf + pTSchema->flen;
×
2279
      break;
×
2280
    default:
×
2281
      return TSDB_CODE_INVALID_DATA_FMT;
×
2282
  }
2283

2284
  while (pColData) {
2,147,483,647✔
2285
    if (pTColumn) {
2,147,483,647✔
2286
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2287
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647✔
2288
          return TSDB_CODE_INVALID_PARA;
×
2289
        }
2290
        if (pb) {
2,147,483,647✔
2291
          uint8_t bv;
2292
          switch (pRow->flag) {
2,147,483,647✔
2293
            case (HAS_NULL | HAS_NONE):
1,104,636✔
2294
              bv = GET_BIT1(pb, iTColumn - 1);
1,104,636✔
2295
              break;
1,104,636✔
2296
            case (HAS_VALUE | HAS_NONE):
55,677,579✔
2297
              bv = GET_BIT1(pb, iTColumn - 1);
55,677,579✔
2298
              if (bv) bv++;
55,681,167✔
2299
              break;
55,681,167✔
2300
            case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
2301
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
2,147,483,647✔
2302
              break;
2,147,483,647✔
UNCOV
2303
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
UNCOV
2304
              bv = GET_BIT2(pb, iTColumn - 1);
×
2305
              break;
×
2306
            default:
×
2307
              return TSDB_CODE_INVALID_DATA_FMT;
×
2308
          }
2309

2310
          if (bv == BIT_FLG_NONE) {
2,147,483,647✔
2311
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
4,824,222✔
2312
              goto _exit;
×
2313
            goto _continue;
4,823,435✔
2314
          } else if (bv == BIT_FLG_NULL) {
2,147,483,647✔
2315
            if (flag == 0) {
531,798,888✔
2316
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
531,592,283✔
2317
            } else {
2318
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
206,605✔
2319
            }
2320
            if (code) goto _exit;
531,809,928✔
2321
            goto _continue;
531,809,928✔
2322
          }
2323
        }
2324

2325
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
2326
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
2,147,483,647✔
2327
          uint32_t nData;
2,147,483,647✔
2328
          pData += tGetU32v(pData, &nData);
2,147,483,647✔
2329
          if (flag == 0) {
2,147,483,647✔
2330
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2331
          } else {
2332
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
59,315,731✔
2333
          }
2334
          if (code) goto _exit;
2,147,483,647✔
2335
        } else {
2336
          if (flag == 0) {
2,147,483,647✔
2337
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2338
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
2339
          } else {
2340
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
236,153,542✔
2341
                                                                          TYPE_BYTES[pColData->type], flag > 0);
118,125,271✔
2342
          }
2343
          if (code) goto _exit;
2,147,483,647✔
2344
        }
2345

2346
      _continue:
2,147,483,647✔
2347
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2348
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2349
      } else if (pTColumn->colId > pColData->cid) {  // NONE
125,436,905✔
2350
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2351
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2352
      } else {
2353
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
125,426,411✔
2354
      }
2355
    } else {
2356
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
115,802,953✔
2357
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
115,803,389✔
2358
    }
2359
  }
2360

2361
_exit:
2,147,483,647✔
2362
  return code;
2,147,483,647✔
2363
}
2364
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
1,584,627,094✔
2365
  int32_t code = 0;
1,584,627,094✔
2366

2367
  uint8_t  *pv = NULL;
1,584,627,094✔
2368
  int32_t   iColData = 0;
1,584,627,094✔
2369
  SColData *pColData = &aColData[iColData];
1,584,627,094✔
2370
  int32_t   iTColumn = 1;
1,584,763,261✔
2371
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,584,763,261✔
2372
  int32_t   iCol = 0;
1,584,867,162✔
2373

2374
  // primary keys
2375
  uint8_t         *data = pRow->data;
1,584,867,162✔
2376
  SPrimaryKeyIndex index;
1,584,852,109✔
2377
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,587,291,140✔
2378
    data += tGetPrimaryKeyIndex(data, &index);
2,426,170✔
2379
  }
2380

2381
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,584,875,451✔
2382
  if (pRow->flag & KV_FLG_LIT) {
1,584,875,451✔
2383
    pv = pKVIdx->idx + pKVIdx->nCol;
1,577,308,088✔
2384
  } else if (pRow->flag & KV_FLG_MID) {
7,546,998✔
2385
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
7,558,448✔
2386
  } else if (pRow->flag & KV_FLG_BIG) {
1,350✔
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2388
  } else {
2389
    return TSDB_CODE_INVALID_PARA;
×
2390
  }
2391

2392
  while (pColData) {
2,147,483,647✔
2393
    if (pTColumn) {
2,147,483,647✔
2394
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2395
        while (iCol < pKVIdx->nCol) {
2,147,483,647✔
2396
          uint8_t *pData;
2397
          if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
2398
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
2,147,483,647✔
2399
          } else if (pRow->flag & KV_FLG_MID) {
208,871,554✔
2400
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
208,515,338✔
2401
          } else if (pRow->flag & KV_FLG_BIG) {
×
2402
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
×
2403
          } else {
2404
            return TSDB_CODE_INVALID_DATA_FMT;
×
2405
          }
2406

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

2410
          if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
2411
            if (cid < 0) {
2,147,483,647✔
2412
              if (flag == 0) {
1,867,126,503✔
2413
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,867,110,883✔
2414
              } else {
2415
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
15,620✔
2416
              }
2417
              if (code) goto _exit;
1,864,773,081✔
2418
            } else {
2419
              uint32_t nData;
2,147,483,647✔
2420
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
2421
                pData += tGetU32v(pData, &nData);
629,664,399✔
2422
              } else {
2423
                nData = 0;
2,147,483,647✔
2424
              }
2425
              if (flag == 0) {
2,147,483,647✔
2426
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2427
              } else {
2428
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
8,484,155✔
2429
              }
2430
              if (code) goto _exit;
2,147,483,647✔
2431
            }
2432
            iCol++;
2,147,483,647✔
2433
            goto _continue;
2,147,483,647✔
2434
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
2,147,483,647✔
2435
            break;
2,147,483,647✔
2436
          } else {
2437
            iCol++;
23,055,858✔
2438
          }
2439
        }
2440

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

2443
      _continue:
2,147,483,647✔
2444
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2445
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2446
      } else if (pTColumn->colId > pColData->cid) {
23,052,806✔
2447
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2448
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2449
      } else {
2450
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
23,052,806✔
2451
      }
2452
    } else {
2453
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
21,805,618✔
2454
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
21,810,196✔
2455
    }
2456
  }
2457

2458
_exit:
1,585,111,184✔
2459
  return code;
1,584,638,743✔
2460
}
2461
/* flag > 0: forward update
2462
 * flag == 0: append
2463
 * flag < 0: backward update
2464
 */
2465
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
2,147,483,647✔
2466
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2467
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2468

2469
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
2470
    return tRowNoneUpsertColData(aColData, nColData, flag);
435,712✔
2471
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2472
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
52,933,020✔
2473
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2474
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,584,812,212✔
2475
  } else {  // TUPLE row
2476
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
2,147,483,647✔
2477
  }
2478
}
2479

2480
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
473,754,106✔
2481
  key->numOfPKs = row->numOfPKs;
473,754,106✔
2482

2483
  if (key->numOfPKs == 0) {
473,828,807✔
2484
    return;
×
2485
  }
2486

2487
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
439,314,412✔
2488

2489
  uint8_t *data = row->data;
473,851,247✔
2490

2491
  for (int32_t i = 0; i < row->numOfPKs; i++) {
947,696,661✔
2492
    data += tGetPrimaryKeyIndex(data, &indices[i]);
473,846,563✔
2493
  }
2494

2495
  // primary keys
2496
  for (int32_t i = 0; i < row->numOfPKs; i++) {
947,730,291✔
2497
    key->pks[i].type = indices[i].type;
473,859,453✔
2498

2499
    uint8_t *tdata = data + indices[i].offset;
473,864,243✔
2500
    if (row->flag >> 4) {
473,872,027✔
2501
      tdata += tGetI16v(tdata, NULL);
125,229,757✔
2502
    }
2503

2504
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
473,891,714✔
2505
      key->pks[i].pData = tdata;
109,302,604✔
2506
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
218,621,722✔
2507
    } else {
2508
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
364,580,505✔
2509
    }
2510
  }
2511
}
2512

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

2524
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
222,283,519✔
2525
  switch (tv1->type) {
222,283,519✔
2526
    case TSDB_DATA_TYPE_BOOL:
×
2527
    case TSDB_DATA_TYPE_TINYINT:
2528
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2529
    case TSDB_DATA_TYPE_SMALLINT:
×
2530
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_INT:
158,928,659✔
2532
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
158,928,659✔
2533
    case TSDB_DATA_TYPE_BIGINT:
12,092,108✔
2534
    case TSDB_DATA_TYPE_TIMESTAMP:
2535
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
12,092,108✔
2536
    case TSDB_DATA_TYPE_FLOAT:
×
2537
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2538
    case TSDB_DATA_TYPE_DOUBLE:
×
2539
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_UTINYINT:
×
2541
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_USMALLINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_UINT:
11,963,851✔
2545
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
11,963,851✔
2546
    case TSDB_DATA_TYPE_UBIGINT:
11,957,069✔
2547
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
11,957,069✔
2548
    case TSDB_DATA_TYPE_GEOMETRY:
27,335,349✔
2549
    case TSDB_DATA_TYPE_BINARY: {
2550
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
27,335,349✔
2551
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
27,337,118✔
2552
    }
2553
    case TSDB_DATA_TYPE_NCHAR: {
×
2554
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2555
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2556
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2557
    }
2558
    case TSDB_DATA_TYPE_VARBINARY: {
14,509✔
2559
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
14,509✔
2560
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2561
    }
2562
    default:
×
2563
      break;
×
2564
  }
2565

2566
  return 0;
×
2567
}
2568

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

2579
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2580
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2581
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
222,281,277✔
2582
      if (ret) return ret;
222,280,241✔
2583
    }
2584
  } else if (key1->numOfPKs < key2->numOfPKs) {
5,019✔
2585
    return -1;
5,019✔
2586
  } else {
2587
    return 1;
×
2588
  }
2589

2590
  return 0;
2,147,483,647✔
2591
}
2592

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

2597
  if (pSrc->numOfPKs > 0) {
2,147,483,647✔
2598
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
144,187,355✔
2599
      SValue *pVal = &pDst->pks[i];
72,095,355✔
2600
      pVal->type = pSrc->pks[i].type;
72,084,341✔
2601

2602
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
72,086,322✔
2603
    }
2604
  }
2605
}
2,147,483,647✔
2606

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

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

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

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

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

2714
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
388,092,177✔
2715
  int32_t n = 0;
388,092,177✔
2716

2717
  // key
2718
  if (isJson) {
388,092,177✔
2719
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
3,011,508✔
2720
  } else {
2721
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
773,166,368✔
2722
  }
2723

2724
  // type
2725
  n += tPutI8(p ? p + n : p, pTagVal->type);
388,085,699✔
2726

2727
  // value
2728
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
388,115,020✔
2729
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
338,425,230✔
2730
  } else {
2731
    p = p ? p + n : p;
218,897,729✔
2732
    n += tDataTypes[pTagVal->type].bytes;
218,898,182✔
2733
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
218,823,528✔
2734
  }
2735

2736
  return n;
388,033,910✔
2737
}
2738
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
2,147,483,647✔
2739
  int32_t n = 0;
2,147,483,647✔
2740

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

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

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

2759
  return n;
2,147,483,647✔
2760
}
2761

2762
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
11,638,307✔
2763

2764
bool tTagIsJsonNull(void *data) {
1,431,204✔
2765
  STag  *pTag = (STag *)data;
1,431,204✔
2766
  int8_t isJson = tTagIsJson(pTag);
1,431,204✔
2767
  if (!isJson) return false;
1,431,204✔
2768
  return ((STag *)data)->nTag == 0;
826,654✔
2769
}
2770

2771
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
85,055,396✔
2772
  int32_t  code = 0;
85,055,396✔
2773
  uint8_t *p = NULL;
85,055,396✔
2774
  int16_t  n = 0;
85,055,396✔
2775
  int16_t  nTag = taosArrayGetSize(pArray);
85,055,396✔
2776
  int32_t  szTag = 0;
85,061,309✔
2777
  int8_t   isLarge = 0;
85,061,309✔
2778

2779
  // sort
2780
  if (isJson) {
85,061,309✔
2781
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
496,062✔
2782
  } else {
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
84,565,247✔
2784
  }
2785

2786
  // get size
2787
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
279,049,918✔
2788
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
194,043,798✔
2789
  }
2790
  if (szTag <= INT8_MAX) {
85,006,120✔
2791
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
80,358,579✔
2792
  } else {
2793
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
4,647,541✔
2794
    isLarge = 1;
4,647,541✔
2795
  }
2796

2797
  // build tag
2798
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
85,006,120✔
2799
  if ((*ppTag) == NULL) {
85,053,706✔
2800
    code = terrno;
×
2801
    goto _err;
×
2802
  }
2803
  (*ppTag)->flags = 0;
85,057,335✔
2804
  if (isJson) {
85,062,422✔
2805
    (*ppTag)->flags |= TD_TAG_JSON;
496,062✔
2806
  }
2807
  if (isLarge) {
85,062,422✔
2808
    (*ppTag)->flags |= TD_TAG_LARGE;
4,656,767✔
2809
  }
2810
  (*ppTag)->len = szTag;
85,062,422✔
2811
  (*ppTag)->nTag = nTag;
85,064,023✔
2812
  (*ppTag)->ver = version;
85,039,758✔
2813

2814
  if (isLarge) {
85,023,183✔
2815
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
4,656,654✔
2816
  } else {
2817
    p = (uint8_t *)&(*ppTag)->idx[nTag];
80,366,529✔
2818
  }
2819
  n = 0;
85,054,435✔
2820
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
279,109,595✔
2821
    if (isLarge) {
194,040,340✔
2822
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
50,649,360✔
2823
    } else {
2824
      (*ppTag)->idx[iTag] = n;
143,390,980✔
2825
    }
2826
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
194,030,946✔
2827
  }
2828
#ifdef TD_DEBUG_PRINT_TAG
2829
  debugPrintSTag(*ppTag, __func__, __LINE__);
2830
#endif
2831

2832
  return code;
85,069,255✔
2833

2834
_err:
×
2835
  return code;
×
2836
}
2837

2838
void tTagFree(STag *pTag) {
1,132,653,405✔
2839
  if (pTag) taosMemoryFree(pTag);
1,132,653,405✔
2840
}
1,132,653,405✔
2841

2842
char *tTagValToData(const STagVal *value, bool isJson) {
466,617,776✔
2843
  if (!value) {
466,617,776✔
2844
    return NULL;
×
2845
  }
2846

2847
  char  *data = NULL;
466,617,776✔
2848
  int8_t typeBytes = 0;
466,617,776✔
2849
  if (isJson) {
466,617,776✔
2850
    typeBytes = CHAR_BYTES;
2,465,594✔
2851
  }
2852

2853
  if (IS_VAR_DATA_TYPE(value->type)) {
466,617,776✔
2854
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
193,187,647✔
2855
    if (data == NULL) {
193,183,522✔
2856
      return NULL;
×
2857
    }
2858

2859
    if (isJson) {
193,183,522✔
2860
      *data = value->type;
1,901,924✔
2861
    }
2862

2863
    varDataLen(data + typeBytes) = value->nData;
193,183,522✔
2864
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
193,182,288✔
2865
  } else {
2866
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
273,441,243✔
2867
  }
2868

2869
  return data;
466,620,769✔
2870
}
2871

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

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

2887
  if (isLarge) {
2,147,483,647✔
2888
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
264,752,798✔
2889
  } else {
2890
    p = (uint8_t *)&pTag->idx[pTag->nTag];
2,147,483,647✔
2891
  }
2892

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

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

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

2923
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
249,432,518✔
2924
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
498,887,453✔
2925
}
2926

2927
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
1,320,227,682✔
2928

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

2936
  if (isLarge) {
874,623✔
2937
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,527✔
2938
  } else {
2939
    p = (uint8_t *)&pTag->idx[pTag->nTag];
870,096✔
2940
  }
2941

2942
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
874,623✔
2943
  if (*ppArray == NULL) {
874,623✔
2944
    code = terrno;
×
2945
    goto _err;
×
2946
  }
2947

2948
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,145,902✔
2949
    if (isLarge) {
1,271,279✔
2950
      offset = ((int16_t *)pTag->idx)[iTag];
6,081✔
2951
    } else {
2952
      offset = pTag->idx[iTag];
1,265,198✔
2953
    }
2954
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,271,279✔
2955
    if (taosArrayPush(*ppArray, &tv) == NULL) {
2,542,558✔
2956
      code = terrno;
×
2957
      goto _err;
×
2958
    }
2959
  }
2960

2961
  return code;
874,623✔
2962

2963
_err:
×
2964
  return code;
×
2965
}
2966

2967
// STSchema ========================================
2968
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
750,683,919✔
2969
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
750,683,919✔
2970
  if (pTSchema == NULL) {
750,595,690✔
2971
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2972
    return NULL;
×
2973
  }
2974

2975
  pTSchema->numOfCols = numOfCols;
750,595,690✔
2976
  pTSchema->version = version;
750,599,929✔
2977

2978
  // timestamp column
2979
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
750,629,341✔
2980
    terrno = TSDB_CODE_INVALID_PARA;
×
2981
    taosMemoryFree(pTSchema);
×
2982
    return NULL;
×
2983
  }
2984
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
750,638,671✔
2985
    terrno = TSDB_CODE_INVALID_PARA;
×
2986
    taosMemoryFree(pTSchema);
×
2987
    return NULL;
×
2988
  }
2989
  pTSchema->columns[0].colId = aSchema[0].colId;
750,573,483✔
2990
  pTSchema->columns[0].type = aSchema[0].type;
750,683,052✔
2991
  pTSchema->columns[0].flags = aSchema[0].flags;
750,740,920✔
2992
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
750,657,580✔
2993
  pTSchema->columns[0].offset = -1;
750,715,288✔
2994

2995
  // other columns
2996
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
2,147,483,647✔
2997
    SSchema  *pSchema = &aSchema[iCol];
2,147,483,647✔
2998
    STColumn *pTColumn = &pTSchema->columns[iCol];
2,147,483,647✔
2999

3000
    pTColumn->colId = pSchema->colId;
2,147,483,647✔
3001
    pTColumn->type = pSchema->type;
2,147,483,647✔
3002
    pTColumn->flags = pSchema->flags;
2,147,483,647✔
3003
    pTColumn->offset = pTSchema->flen;
2,147,483,647✔
3004

3005
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
2,147,483,647✔
3006
      pTColumn->bytes = pSchema->bytes;
2,147,483,647✔
3007
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
2,147,483,647✔
3008
    } else {
3009
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
2,147,483,647✔
3010
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
2,147,483,647✔
3011
    }
3012

3013
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
2,147,483,647✔
3014
  }
3015

3016
#if 1  // todo : remove this
3017
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
750,803,491✔
3018
#endif
3019

3020
  return pTSchema;
750,709,297✔
3021
}
3022

3023
static int32_t tTColumnCompare(const void *p1, const void *p2) {
40,221,092✔
3024
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
40,221,092✔
3025
    return -1;
6,558,953✔
3026
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
33,666,006✔
3027
    return 1;
23,864,492✔
3028
  }
3029

3030
  return 0;
9,805,759✔
3031
}
3032

3033
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid) {
9,807,163✔
3034
  STColumn tcol = {
9,807,163✔
3035
      .colId = cid,
3036
  };
3037

3038
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
9,807,319✔
3039
}
3040

3041
// SColData ========================================
3042
void tColDataDestroy(void *ph) {
911,711,031✔
3043
  if (ph) {
911,711,031✔
3044
    SColData *pColData = (SColData *)ph;
911,788,179✔
3045

3046
    tFree(pColData->pBitMap);
911,788,179✔
3047
    tFree(pColData->aOffset);
911,848,510✔
3048
    tFree(pColData->pData);
911,785,315✔
3049
  }
3050
}
911,749,908✔
3051

3052
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
994,137,420✔
3053
  pColData->cid = cid;
994,137,420✔
3054
  pColData->type = type;
994,200,839✔
3055
  pColData->cflag = cflag;
994,226,861✔
3056
  tColDataClear(pColData);
994,300,166✔
3057
}
994,178,617✔
3058

3059
void tColDataClear(SColData *pColData) {
2,036,416,923✔
3060
  pColData->numOfNone = 0;
2,036,416,923✔
3061
  pColData->numOfNull = 0;
2,036,560,689✔
3062
  pColData->numOfValue = 0;
2,036,485,496✔
3063
  pColData->nVal = 0;
2,036,559,795✔
3064
  pColData->flag = 0;
2,036,535,497✔
3065
  pColData->nData = 0;
2,036,575,661✔
3066
}
2,036,487,414✔
3067

3068
void tColDataDeepClear(SColData *pColData) {
7,859,765✔
3069
  pColData->pBitMap = NULL;
7,859,765✔
3070
  pColData->aOffset = NULL;
7,860,925✔
3071
  pColData->pData = NULL;
7,859,864✔
3072

3073
  tColDataClear(pColData);
7,860,784✔
3074
}
7,862,361✔
3075

3076
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
3077
  int32_t  code = 0;
2,147,483,647✔
3078
  uint32_t cvtNData = sizeof(uint64_t);
2,147,483,647✔
3079
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3080
    // TODO
3081
    if (IS_STR_DATA_BLOB(pColData->type)) {
2,147,483,647✔
3082
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
45,452,727✔
3083
      if (code) goto _exit;
31,040,680✔
3084
      pColData->aOffset[pColData->nVal] = pColData->nData;
31,040,680✔
3085

3086
      if (nData) {
31,040,680✔
3087
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
27,940,463✔
3088
        if (code) goto _exit;
27,940,463✔
3089
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
27,940,463✔
3090
        pColData->nData += BSE_SEQUECE_SIZE;
27,940,463✔
3091
      } else {
3092
        // uint64_t zero = 0;
3093
        // (void)memcpy(pColData->pData + pColData->nData, &zero, BSE_SEQUECE_SIZE);
3094
        // pColData->nData += BSE_SEQUECE_SIZE;
3095
      }
3096

3097
    } else {
3098
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
2,147,483,647✔
3099
      if (code) goto _exit;
2,147,483,647✔
3100
      pColData->aOffset[pColData->nVal] = pColData->nData;
2,147,483,647✔
3101

3102
      if (nData) {
2,147,483,647✔
3103
        code = tRealloc(&pColData->pData, pColData->nData + nData);
2,147,483,647✔
3104
        if (code) goto _exit;
2,147,483,647✔
3105
        (void)memcpy(pColData->pData + pColData->nData, pData, nData);
2,147,483,647✔
3106
        pColData->nData += nData;
2,147,483,647✔
3107
      }
3108
    }
3109
  } else {
3110
    if (!(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal)) {
2,147,483,647✔
3111
      return TSDB_CODE_INVALID_PARA;
×
3112
    }
3113
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
2,147,483,647✔
3114
    if (code) goto _exit;
2,147,483,647✔
3115
    if (pData) {
2,147,483,647✔
3116
      (void)memcpy(pColData->pData + pColData->nData, pData, TYPE_BYTES[pColData->type]);
2,147,483,647✔
3117
    } else {
3118
      memset(pColData->pData + pColData->nData, 0, TYPE_BYTES[pColData->type]);
1,059,178,012✔
3119
    }
3120
    pColData->nData += tDataTypes[pColData->type].bytes;
2,147,483,647✔
3121
  }
3122
  pColData->nVal++;
2,147,483,647✔
3123

3124
_exit:
2,147,483,647✔
3125
  return code;
2,147,483,647✔
3126
}
3127
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
454,619,268✔
3128
  pColData->flag = HAS_VALUE;
454,712,797✔
3129
  pColData->numOfValue++;
454,731,219✔
3130
  return tColDataPutValue(pColData, pData, nData);
454,740,895✔
3131
}
3132
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
16,257,104✔
3133
  pColData->flag = HAS_NONE;
16,257,104✔
3134
  pColData->numOfNone++;
16,257,976✔
3135
  pColData->nVal++;
16,257,540✔
3136
  return 0;
16,257,322✔
3137
}
3138
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
21,568,433✔
3139
  pColData->flag = HAS_NULL;
21,590,898✔
3140
  pColData->numOfNull++;
21,599,687✔
3141
  pColData->nVal++;
21,599,566✔
3142
  return 0;
21,593,870✔
3143
}
3144
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
852,817✔
3145
  int32_t code = 0;
852,817✔
3146

3147
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
852,817✔
3148
  code = tRealloc(&pColData->pBitMap, nBit);
852,817✔
3149
  if (code) return code;
852,817✔
3150

3151
  memset(pColData->pBitMap, 0, nBit);
852,817✔
3152
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
852,817✔
3153

3154
  pColData->flag |= HAS_VALUE;
852,817✔
3155
  pColData->numOfValue++;
852,817✔
3156

3157
  if (pColData->nVal) {
852,817✔
3158
    if (IS_VAR_DATA_TYPE(pColData->type)) {
852,817✔
3159
      if (IS_STR_DATA_BLOB(pColData->type)) {
18,727✔
3160
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3161
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3162
        if (code) return code;
×
3163
        memset(pColData->aOffset, 0, nOffset);
×
3164

3165
      } else {
3166
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
18,727✔
3167
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
18,727✔
3168
        if (code) return code;
18,727✔
3169
        memset(pColData->aOffset, 0, nOffset);
18,727✔
3170
      }
3171
    } else {
3172
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
834,090✔
3173
      code = tRealloc(&pColData->pData, pColData->nData);
834,090✔
3174
      if (code) return code;
834,090✔
3175
      memset(pColData->pData, 0, pColData->nData);
834,090✔
3176
    }
3177
  }
3178

3179
  return tColDataPutValue(pColData, pData, nData);
852,817✔
3180
}
3181
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3182
  pColData->nVal++;
2,147,483,647✔
3183
  pColData->numOfNone++;
2,147,483,647✔
3184
  return 0;
2,147,483,647✔
3185
}
3186
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
23,303✔
3187
  int32_t code = 0;
23,303✔
3188

3189
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
23,303✔
3190
  code = tRealloc(&pColData->pBitMap, nBit);
23,303✔
3191
  if (code) return code;
23,303✔
3192

3193
  memset(pColData->pBitMap, 0, nBit);
23,303✔
3194
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
23,303✔
3195

3196
  pColData->flag |= HAS_NULL;
23,303✔
3197
  pColData->numOfNull++;
23,303✔
3198
  pColData->nVal++;
23,303✔
3199

3200
  return code;
23,303✔
3201
}
3202
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,933,561✔
3203
  int32_t code = 0;
4,934,047✔
3204

3205
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
4,934,047✔
3206
  code = tRealloc(&pColData->pBitMap, nBit);
4,934,047✔
3207
  if (code) return code;
4,934,047✔
3208

3209
  memset(pColData->pBitMap, 0, nBit);
4,934,047✔
3210
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,934,047✔
3211

3212
  pColData->flag |= HAS_VALUE;
4,934,047✔
3213
  pColData->numOfValue++;
4,934,047✔
3214

3215
  if (pColData->nVal) {
4,934,047✔
3216
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,454,276✔
3217
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,520,229✔
3218
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,520,229✔
3219
      if (code) return code;
1,520,229✔
3220
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,520,229✔
3221
        memset(pColData->aOffset, 0, nOffset);
1,514,293✔
3222
      }
3223
    } else {
3224
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,413,818✔
3225
      code = tRealloc(&pColData->pData, pColData->nData);
3,413,818✔
3226
      if (code) return code;
3,413,818✔
3227
      memset(pColData->pData, 0, pColData->nData);
3,413,818✔
3228
    }
3229
  }
3230

3231
  return tColDataPutValue(pColData, pData, nData);
4,934,047✔
3232
}
3233
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,280✔
3234
  int32_t code = 0;
3,280✔
3235

3236
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
3,280✔
3237
  code = tRealloc(&pColData->pBitMap, nBit);
3,280✔
3238
  if (code) return code;
3,280✔
3239

3240
  memset(pColData->pBitMap, 255, nBit);
3,280✔
3241
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
3,280✔
3242

3243
  pColData->flag |= HAS_NONE;
3,280✔
3244
  pColData->numOfNone++;
3,280✔
3245
  pColData->nVal++;
3,280✔
3246

3247
  return code;
3,280✔
3248
}
3249
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3250
  pColData->nVal++;
2,147,483,647✔
3251
  pColData->numOfNull++;
2,147,483,647✔
3252
  return 0;
2,147,483,647✔
3253
}
3254
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3255
  int32_t code = 0;
×
3256

3257
  pColData->flag |= HAS_VALUE;
×
3258
  pColData->numOfValue++;
×
3259

3260
  uint8_t *pBitMap = NULL;
×
3261
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3262
  if (code) return code;
×
3263

3264
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3265
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3266
  }
3267
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3268

3269
  tFree(pColData->pBitMap);
×
3270
  pColData->pBitMap = pBitMap;
×
3271

3272
  if (pColData->nVal) {
×
3273
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3274
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3275
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3276
      if (code) return code;
×
3277
      memset(pColData->aOffset, 0, nOffset);
×
3278
    } else {
3279
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3280
      code = tRealloc(&pColData->pData, pColData->nData);
×
3281
      if (code) return code;
×
3282
      memset(pColData->pData, 0, pColData->nData);
×
3283
    }
3284
  }
3285

3286
  return tColDataPutValue(pColData, pData, nData);
×
3287
}
3288
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
15,120✔
3289
  int32_t code = 0;
15,120✔
3290

3291
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
15,120✔
3292
  if (code) return code;
15,120✔
3293

3294
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
15,120✔
3295
  pColData->numOfNone++;
15,120✔
3296
  pColData->nVal++;
15,120✔
3297

3298
  return code;
15,120✔
3299
}
3300
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
355,320✔
3301
  int32_t code = 0;
355,320✔
3302

3303
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
355,320✔
3304
  if (code) return code;
355,320✔
3305

3306
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
355,320✔
3307
  pColData->numOfNull++;
355,320✔
3308
  pColData->nVal++;
355,320✔
3309

3310
  return code;
355,320✔
3311
}
3312
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3313
  pColData->numOfValue++;
2,147,483,647✔
3314
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3315
}
3316
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
5,827,111✔
3317
  int32_t code = 0;
5,827,111✔
3318

3319
  pColData->flag |= HAS_NONE;
5,827,111✔
3320
  pColData->numOfNone++;
5,827,294✔
3321

3322
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
5,827,111✔
3323
  code = tRealloc(&pColData->pBitMap, nBit);
5,827,294✔
3324
  if (code) return code;
5,825,857✔
3325

3326
  memset(pColData->pBitMap, 255, nBit);
5,825,857✔
3327
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
5,827,294✔
3328

3329
  return tColDataPutValue(pColData, NULL, 0);
5,827,138✔
3330
}
3331
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
20,311,564✔
3332
  int32_t code = 0;
20,512,464✔
3333

3334
  pColData->flag |= HAS_NULL;
20,512,464✔
3335
  pColData->numOfNull++;
20,512,902✔
3336

3337
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
20,513,091✔
3338
  code = tRealloc(&pColData->pBitMap, nBit);
20,513,091✔
3339
  if (code) return code;
20,512,019✔
3340

3341
  memset(pColData->pBitMap, 255, nBit);
20,512,019✔
3342
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
20,512,558✔
3343

3344
  return tColDataPutValue(pColData, NULL, 0);
20,511,967✔
3345
}
3346
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
509,474,576✔
3347
  int32_t code = 0;
509,550,812✔
3348

3349
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
509,550,812✔
3350
  if (code) return code;
509,591,993✔
3351

3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
509,591,993✔
3353
  pColData->numOfValue++;
509,585,597✔
3354

3355
  return tColDataPutValue(pColData, pData, nData);
509,560,709✔
3356
}
3357
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
804,285,131✔
3358
  int32_t code = 0;
804,285,131✔
3359

3360
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
804,285,131✔
3361
  if (code) return code;
804,287,240✔
3362

3363
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
804,287,240✔
3364
  pColData->numOfNone++;
804,288,557✔
3365

3366
  return tColDataPutValue(pColData, NULL, 0);
804,287,771✔
3367
}
3368
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,841✔
3369
  int32_t code = 0;
2,841✔
3370

3371
  pColData->flag |= HAS_NULL;
2,841✔
3372
  pColData->numOfNull++;
2,841✔
3373

3374
  uint8_t *pBitMap = NULL;
2,841✔
3375
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
2,841✔
3376
  if (code) return code;
2,841✔
3377

3378
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
692,073✔
3379
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
689,232✔
3380
  }
3381
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
2,841✔
3382

3383
  tFree(pColData->pBitMap);
2,841✔
3384
  pColData->pBitMap = pBitMap;
2,841✔
3385

3386
  return tColDataPutValue(pColData, NULL, 0);
2,841✔
3387
}
3388
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3389
  int32_t code = 0;
2,147,483,647✔
3390

3391
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
2,147,483,647✔
3392
  if (code) return code;
2,147,483,647✔
3393
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
2,147,483,647✔
3394
  pColData->numOfValue++;
2,147,483,647✔
3395

3396
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3397
}
3398
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,686,047✔
3399
  int32_t code = 0;
1,686,047✔
3400

3401
  pColData->flag |= HAS_NONE;
1,686,047✔
3402
  pColData->numOfNone++;
1,686,425✔
3403

3404
  uint8_t *pBitMap = NULL;
1,686,047✔
3405
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
1,686,425✔
3406
  if (code) return code;
1,686,425✔
3407

3408
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
161,565,680✔
3409
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
159,879,822✔
3410
  }
3411
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
1,686,614✔
3412

3413
  tFree(pColData->pBitMap);
1,686,425✔
3414
  pColData->pBitMap = pBitMap;
1,686,236✔
3415

3416
  return tColDataPutValue(pColData, NULL, 0);
1,686,425✔
3417
}
3418
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,766,580,542✔
3419
  int32_t code = 0;
1,767,094,648✔
3420

3421
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,767,094,648✔
3422
  if (code) return code;
1,767,413,926✔
3423
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,767,413,926✔
3424
  pColData->numOfNull++;
1,767,998,822✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
1,767,990,548✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,220,180✔
3429
  int32_t code = 0;
2,220,180✔
3430

3431
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
2,220,180✔
3432
  if (code) return code;
2,220,180✔
3433
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
2,220,180✔
3434
  pColData->numOfValue++;
2,220,180✔
3435

3436
  return tColDataPutValue(pColData, pData, nData);
2,220,180✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
306,817,662✔
3439
  int32_t code = 0;
306,817,662✔
3440

3441
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
306,817,662✔
3442
  if (code) return code;
306,817,662✔
3443
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
306,817,662✔
3444
  pColData->numOfNone++;
306,817,662✔
3445

3446
  return tColDataPutValue(pColData, NULL, 0);
306,817,662✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
9,555✔
3449
  int32_t code = 0;
9,555✔
3450

3451
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
9,555✔
3452
  if (code) return code;
9,555✔
3453
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
9,555✔
3454
  pColData->numOfNull++;
9,555✔
3455

3456
  return tColDataPutValue(pColData, NULL, 0);
9,555✔
3457
}
3458
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
3459
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
3460
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
3461
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
3462
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
3463
    {tColDataAppendValue40, tColDataAppendValue41, tColDataAppendValue42},  // HAS_VALUE
3464
    {tColDataAppendValue50, tColDataAppendValue51, tColDataAppendValue52},  // HAS_VALUE|HAS_NONE
3465
    {tColDataAppendValue60, tColDataAppendValue61, tColDataAppendValue62},  // HAS_VALUE|HAS_NULL
3466
    {tColDataAppendValue70, tColDataAppendValue71, tColDataAppendValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
3467

3468
    //       VALUE                  NONE                     NULL
3469
};
3470

3471
static FORCE_INLINE int32_t tColDataPutValueBlob(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
3472
  int32_t  code = 0;
×
3473
  uint8_t  buf[sizeof(uint64_t) + 1] = {0};
×
3474
  uint64_t seq = 0;
×
3475

3476
  int32_t offset = 0;
×
3477
  if (IS_STR_DATA_BLOB(pColData->type)) {
×
3478
    code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
×
3479
    if (code) goto _exit;
×
3480
    pColData->aOffset[pColData->nVal] = pColData->nData;
×
3481
    if (nData) {
×
3482
      SBlobItem item = {.seqOffsetInRow = seq, .len = nData, .data = pData, .type = TSDB_DATA_BLOB_VALUE};
×
3483
      code = tBlobSetPush(pArg, &item, &seq, 0);
×
3484
      if (code != 0) return code;
×
3485

3486
      offset = tPutU64(buf, seq);
×
3487
      code = tRealloc(&pColData->pData, pColData->nData + offset);
×
3488
      if (code != 0) return code;
×
3489
      memcpy(pColData->pData + pColData->nData, buf, offset);
×
3490
      pColData->nData += offset;
×
3491
    } else {
3492
      int8_t type = pData ? TSDB_DATA_BLOB_EMPTY_VALUE : TSDB_DATA_BLOB_NULL_VALUE;
×
3493
      code = addEmptyItemToBlobSet(pArg, type, NULL);
×
3494
    }
3495
  }
3496
  pColData->nVal++;
×
3497

3498
_exit:
×
3499
  return code;
×
3500
}
3501

3502
static FORCE_INLINE int32_t tColDataAppendValueBlob00(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3503
  pColData->flag = HAS_VALUE;
×
3504
  pColData->numOfValue++;
×
3505
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3506
}
3507
static FORCE_INLINE int32_t tColDataAppendValueBlob01(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3508
  pColData->flag = HAS_NONE;
×
3509
  pColData->numOfNone++;
×
3510
  pColData->nVal++;
×
3511
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3512
}
3513
static FORCE_INLINE int32_t tColDataAppendValueBlob02(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3514
  pColData->flag = HAS_NULL;
×
3515
  pColData->numOfNull++;
×
3516
  pColData->nVal++;
×
3517
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3518
}
3519
static FORCE_INLINE int32_t tColDataAppendValueBlob10(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3520
  int32_t code = 0;
×
3521

3522
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3523
  code = tRealloc(&pColData->pBitMap, nBit);
×
3524
  if (code) return code;
×
3525

3526
  memset(pColData->pBitMap, 0, nBit);
×
3527
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3528

3529
  pColData->flag |= HAS_VALUE;
×
3530
  pColData->numOfValue++;
×
3531

3532
  if (pColData->nVal) {
×
3533
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3534
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3535
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3536
      if (code) return code;
×
3537
      memset(pColData->aOffset, 0, nOffset);
×
3538
    } else {
3539
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3540
      code = tRealloc(&pColData->pData, pColData->nData);
×
3541
      if (code) return code;
×
3542
      memset(pColData->pData, 0, pColData->nData);
×
3543
    }
3544
  }
3545

3546
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3547
}
3548
static FORCE_INLINE int32_t tColDataAppendValueBlob11(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3549
  pColData->nVal++;
×
3550
  pColData->numOfNone++;
×
3551
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3552
}
3553
static FORCE_INLINE int32_t tColDataAppendValueBlob12(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3554
  int32_t code = 0;
×
3555

3556
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3557
  code = tRealloc(&pColData->pBitMap, nBit);
×
3558
  if (code) return code;
×
3559

3560
  memset(pColData->pBitMap, 0, nBit);
×
3561
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3562

3563
  pColData->flag |= HAS_NULL;
×
3564
  pColData->numOfNull++;
×
3565
  pColData->nVal++;
×
3566
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3567
}
3568
static FORCE_INLINE int32_t tColDataAppendValueBlob20(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3569
  int32_t code = 0;
×
3570

3571
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3572
  code = tRealloc(&pColData->pBitMap, nBit);
×
3573
  if (code) return code;
×
3574

3575
  memset(pColData->pBitMap, 0, nBit);
×
3576
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3577

3578
  pColData->flag |= HAS_VALUE;
×
3579
  pColData->numOfValue++;
×
3580

3581
  if (pColData->nVal) {
×
3582
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3583
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3584
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3585
      if (code) return code;
×
3586
      memset(pColData->aOffset, 0, nOffset);
×
3587
    } else {
3588
      return TSDB_CODE_INVALID_MSG;
×
3589
    }
3590
  }
3591

3592
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3593
}
3594
static FORCE_INLINE int32_t tColDataAppendValueBlob21(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3595
  int32_t code = 0;
×
3596

3597
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3598
  code = tRealloc(&pColData->pBitMap, nBit);
×
3599
  if (code) return code;
×
3600

3601
  memset(pColData->pBitMap, 255, nBit);
×
3602
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3603

3604
  pColData->flag |= HAS_NONE;
×
3605
  pColData->numOfNone++;
×
3606
  pColData->nVal++;
×
3607
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3608
}
3609
static FORCE_INLINE int32_t tColDataAppendValueBlob22(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3610
  pColData->nVal++;
×
3611
  pColData->numOfNull++;
×
3612
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3613
}
3614
static FORCE_INLINE int32_t tColDataAppendValueBlob30(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3615
  int32_t code = 0;
×
3616

3617
  pColData->flag |= HAS_VALUE;
×
3618
  pColData->numOfValue++;
×
3619

3620
  uint8_t *pBitMap = NULL;
×
3621
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3622
  if (code) return code;
×
3623

3624
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3625
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3626
  }
3627
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3628

3629
  tFree(pColData->pBitMap);
×
3630
  pColData->pBitMap = pBitMap;
×
3631

3632
  if (pColData->nVal) {
×
3633
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3634
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3635
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3636
      if (code) return code;
×
3637
      memset(pColData->aOffset, 0, nOffset);
×
3638
    } else {
3639
      return TSDB_CODE_INVALID_MSG;
×
3640
    }
3641
  }
3642

3643
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3644
}
3645
static FORCE_INLINE int32_t tColDataAppendValueBlob31(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3646
  int32_t code = 0;
×
3647

3648
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3649
  if (code) return code;
×
3650

3651
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3652
  pColData->numOfNone++;
×
3653
  pColData->nVal++;
×
3654

3655
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3656
}
3657
static FORCE_INLINE int32_t tColDataAppendValueBlob32(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3658
  int32_t code = 0;
×
3659

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

3663
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3664
  pColData->numOfNull++;
×
3665
  pColData->nVal++;
×
3666
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3667
}
3668
static FORCE_INLINE int32_t tColDataAppendValueBlob40(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3669
  pColData->numOfValue++;
×
3670
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3671
}
3672
static FORCE_INLINE int32_t tColDataAppendValueBlob41(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3673
  int32_t code = 0;
×
3674

3675
  pColData->flag |= HAS_NONE;
×
3676
  pColData->numOfNone++;
×
3677

3678
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3679
  code = tRealloc(&pColData->pBitMap, nBit);
×
3680
  if (code) return code;
×
3681

3682
  memset(pColData->pBitMap, 255, nBit);
×
3683
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3684

3685
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3686
}
3687
static FORCE_INLINE int32_t tColDataAppendValueBlob42(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3688
  int32_t code = 0;
×
3689

3690
  pColData->flag |= HAS_NULL;
×
3691
  pColData->numOfNull++;
×
3692

3693
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3694
  code = tRealloc(&pColData->pBitMap, nBit);
×
3695
  if (code) return code;
×
3696

3697
  memset(pColData->pBitMap, 255, nBit);
×
3698
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3699

3700
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3701
}
3702
static FORCE_INLINE int32_t tColDataAppendValueBlob50(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3703
  int32_t code = 0;
×
3704

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

3708
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3709
  pColData->numOfValue++;
×
3710

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

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

3719
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3720
  pColData->numOfNone++;
×
3721

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

3727
  pColData->flag |= HAS_NULL;
×
3728
  pColData->numOfNull++;
×
3729

3730
  uint8_t *pBitMap = NULL;
×
3731
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3732
  if (code) return code;
×
3733

3734
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3735
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
×
3736
  }
3737
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
×
3738

3739
  tFree(pColData->pBitMap);
×
3740
  pColData->pBitMap = pBitMap;
×
3741

3742
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3743
}
3744
static FORCE_INLINE int32_t tColDataAppendValueBlob60(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3745
  int32_t code = 0;
×
3746

3747
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3748
  if (code) return code;
×
3749
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3750
  pColData->numOfValue++;
×
3751

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

3757
  pColData->flag |= HAS_NONE;
×
3758
  pColData->numOfNone++;
×
3759

3760
  uint8_t *pBitMap = NULL;
×
3761
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3762
  if (code) return code;
×
3763

3764
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3765
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
×
3766
  }
3767
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
×
3768

3769
  tFree(pColData->pBitMap);
×
3770
  pColData->pBitMap = pBitMap;
×
3771

3772
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3773
}
3774
static FORCE_INLINE int32_t tColDataAppendValueBlob62(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3775
  int32_t code = 0;
×
3776

3777
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3778
  if (code) return code;
×
3779
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3780
  pColData->numOfNull++;
×
3781

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

3787
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3788
  if (code) return code;
×
3789
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
×
3790
  pColData->numOfValue++;
×
3791

3792
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3793
}
3794
static FORCE_INLINE int32_t tColDataAppendValueBlob71(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, 0);
×
3800
  pColData->numOfNone++;
×
3801

3802
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3803
}
3804
static FORCE_INLINE int32_t tColDataAppendValueBlob72(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, 1);
×
3810
  pColData->numOfNull++;
×
3811

3812
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3813
}
3814

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

3834
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,099✔
3835
  pColData->numOfNone--;
2,099✔
3836
  pColData->nVal--;
2,099✔
3837
  if (pColData->numOfNone) {
2,099✔
3838
    return tColDataAppendValue10(pColData, pData, nData);
×
3839
  } else {
3840
    pColData->flag = 0;
2,099✔
3841
    return tColDataAppendValue00(pColData, pData, nData);
2,099✔
3842
  }
3843
}
3844
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
984✔
3845
  pColData->numOfNone--;
984✔
3846
  pColData->nVal--;
984✔
3847
  if (pColData->numOfNone) {
984✔
3848
    return tColDataAppendValue12(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
984✔
3851
    return tColDataAppendValue02(pColData, pData, nData);
984✔
3852
  }
3853
  return 0;
3854
}
3855
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
91,916✔
3856
  if (forward) {
91,916✔
3857
    pColData->numOfNull--;
91,916✔
3858
    pColData->nVal--;
91,916✔
3859
    if (pColData->numOfNull) {
91,916✔
3860
      return tColDataAppendValue20(pColData, pData, nData);
486✔
3861
    } else {
3862
      pColData->flag = 0;
91,430✔
3863
      return tColDataAppendValue00(pColData, pData, nData);
91,430✔
3864
    }
3865
  }
3866
  return 0;
×
3867
}
3868
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3869
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3870
    pColData->numOfNone--;
×
3871
    pColData->nVal--;
×
3872
    if (pColData->numOfNone) {
×
3873
      return tColDataAppendValue30(pColData, pData, nData);
×
3874
    } else {
3875
      pColData->flag = HAS_NULL;
×
3876
      return tColDataAppendValue20(pColData, pData, nData);
×
3877
    }
3878
  } else if (forward) {  // NULL ==> VALUE
×
3879
    pColData->numOfNull--;
×
3880
    pColData->nVal--;
×
3881
    if (pColData->numOfNull) {
×
3882
      return tColDataAppendValue30(pColData, pData, nData);
×
3883
    } else {
3884
      pColData->flag = HAS_NONE;
×
3885
      return tColDataAppendValue10(pColData, pData, nData);
×
3886
    }
3887
  }
3888
  return 0;
×
3889
}
3890
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,280✔
3891
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
3,280✔
3892
    pColData->numOfNone--;
3,280✔
3893
    pColData->numOfNull++;
3,280✔
3894
    if (pColData->numOfNone) {
3,280✔
3895
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
×
3896
    } else {
3897
      pColData->flag = HAS_NULL;
3,280✔
3898
    }
3899
  }
3900
  return 0;
3,280✔
3901
}
3902
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3903
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3904
    pColData->nVal--;
2,147,483,647✔
3905
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3906
      pColData->nData = pColData->aOffset[pColData->nVal];
1,260,530,238✔
3907
    } else {
3908
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3909
    }
3910
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3911
  }
3912
  return 0;
×
3913
}
3914
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
210,877✔
3915
  if (forward) {  // VALUE ==> NULL
210,877✔
3916
    pColData->numOfValue--;
210,877✔
3917
    pColData->nVal--;
210,877✔
3918
    if (pColData->numOfValue) {
210,877✔
3919
      if (IS_VAR_DATA_TYPE(pColData->type)) {
189,396✔
3920
        pColData->nData = pColData->aOffset[pColData->nVal];
37,800✔
3921
      } else {
3922
        pColData->nData -= TYPE_BYTES[pColData->type];
151,596✔
3923
      }
3924
      return tColDataAppendValue42(pColData, pData, nData);
189,396✔
3925
    } else {
3926
      pColData->flag = 0;
21,481✔
3927
      pColData->nData = 0;
21,481✔
3928
      return tColDataAppendValue02(pColData, pData, nData);
21,481✔
3929
    }
3930
  }
3931
  return 0;
×
3932
}
3933
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,029,003✔
3934
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
1,029,003✔
3935
    pColData->numOfNone--;
211,491✔
3936
    pColData->nVal--;
211,491✔
3937
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
211,680✔
3938
      pColData->nData -= TYPE_BYTES[pColData->type];
169,344✔
3939
    }
3940
    if (pColData->numOfNone) {
211,680✔
3941
      return tColDataAppendValue50(pColData, pData, nData);
75,600✔
3942
    } else {
3943
      pColData->flag = HAS_VALUE;
135,702✔
3944
      return tColDataAppendValue40(pColData, pData, nData);
135,702✔
3945
    }
3946
  } else if (forward) {  // VALUE ==> VALUE
817,512✔
3947
    pColData->nVal--;
817,512✔
3948
    if (IS_VAR_DATA_TYPE(pColData->type)) {
817,512✔
3949
      pColData->nData = pColData->aOffset[pColData->nVal];
116,640✔
3950
    } else {
3951
      pColData->nData -= TYPE_BYTES[pColData->type];
700,872✔
3952
    }
3953
    return tColDataPutValue(pColData, pData, nData);
817,512✔
3954
  }
3955
  return 0;
×
3956
}
3957
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
11,504✔
3958
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
11,504✔
3959
    pColData->numOfNone--;
11,504✔
3960
    pColData->nVal--;
11,504✔
3961
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
11,504✔
3962
      pColData->nData -= TYPE_BYTES[pColData->type];
9,236✔
3963
    }
3964
    if (pColData->numOfNone) {
11,504✔
3965
      return tColDataAppendValue52(pColData, pData, nData);
×
3966
    } else {
3967
      pColData->flag = HAS_VALUE;
11,504✔
3968
      return tColDataAppendValue42(pColData, pData, nData);
11,315✔
3969
    }
3970
  } else if (forward) {  // VALUE ==> NULL
×
3971
    pColData->numOfValue--;
×
3972
    pColData->nVal--;
×
3973
    if (pColData->numOfValue) {
×
3974
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3975
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3976
      } else {
3977
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3978
      }
3979
      return tColDataAppendValue52(pColData, pData, nData);
×
3980
    } else {
3981
      pColData->flag = HAS_NONE;
×
3982
      pColData->nData = 0;
×
3983
      return tColDataAppendValue12(pColData, pData, nData);
×
3984
    }
3985
  }
3986
  return 0;
×
3987
}
3988
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,175,937✔
3989
  if (forward) {
2,175,937✔
3990
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
2,175,937✔
3991
      pColData->numOfNull--;
1,559,352✔
3992
      pColData->nVal--;
1,559,352✔
3993
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
1,559,352✔
3994
        pColData->nData -= TYPE_BYTES[pColData->type];
1,274,097✔
3995
      }
3996
      if (pColData->numOfNull) {
1,559,352✔
3997
        return tColDataAppendValue60(pColData, pData, nData);
108,264✔
3998
      } else {
3999
        pColData->flag = HAS_VALUE;
1,450,899✔
4000
        return tColDataAppendValue40(pColData, pData, nData);
1,451,088✔
4001
      }
4002
    } else {  // VALUE ==> VALUE
4003
      pColData->nVal--;
616,774✔
4004
      if (IS_VAR_DATA_TYPE(pColData->type)) {
616,774✔
4005
        pColData->nData = pColData->aOffset[pColData->nVal];
115,208✔
4006
      } else {
4007
        pColData->nData -= TYPE_BYTES[pColData->type];
501,566✔
4008
      }
4009
      return tColDataPutValue(pColData, pData, nData);
616,774✔
4010
    }
4011
  }
4012
  return 0;
×
4013
}
4014
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,049,386✔
4015
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,049,386✔
4016
    pColData->numOfValue--;
387,339✔
4017
    pColData->nVal--;
387,339✔
4018
    if (pColData->numOfValue) {
387,339✔
4019
      if (IS_VAR_DATA_TYPE(pColData->type)) {
387,339✔
4020
        pColData->nData = pColData->aOffset[pColData->nVal];
85,374✔
4021
      } else {
4022
        pColData->nData -= TYPE_BYTES[pColData->type];
301,965✔
4023
      }
4024
      return tColDataAppendValue62(pColData, pData, nData);
387,339✔
4025
    } else {
4026
      pColData->flag = HAS_NULL;
×
4027
      pColData->nData = 0;
×
4028
      return tColDataAppendValue20(pColData, pData, nData);
×
4029
    }
4030
  }
4031
  return 0;
662,047✔
4032
}
4033
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
21,636✔
4034
  int32_t code = 0;
21,636✔
4035

4036
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
21,636✔
4037
  if (bv == 0) {  // NONE ==> VALUE
21,636✔
4038
    pColData->numOfNone--;
15,120✔
4039
    pColData->nVal--;
15,120✔
4040
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
15,120✔
4041
      pColData->nData -= TYPE_BYTES[pColData->type];
12,096✔
4042
    }
4043
    if (pColData->numOfNone) {
15,120✔
4044
      return tColDataAppendValue70(pColData, pData, nData);
×
4045
    } else {
4046
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
801,360✔
4047
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
786,240✔
4048
      }
4049
      pColData->flag = (HAS_VALUE | HAS_NULL);
15,120✔
4050
      return tColDataAppendValue60(pColData, pData, nData);
15,120✔
4051
    }
4052
  } else if (bv == 1) {  // NULL ==> VALUE
6,516✔
4053
    if (forward) {
636✔
4054
      pColData->numOfNull--;
636✔
4055
      pColData->nVal--;
636✔
4056
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
636✔
4057
        pColData->nData -= TYPE_BYTES[pColData->type];
636✔
4058
      }
4059
      if (pColData->numOfNull) {
636✔
4060
        return tColDataAppendValue70(pColData, pData, nData);
×
4061
      } else {
4062
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
1,908✔
4063
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,272✔
4064
        }
4065
        pColData->flag = (HAS_VALUE | HAS_NONE);
636✔
4066
        return tColDataAppendValue50(pColData, pData, nData);
636✔
4067
      }
4068
    }
4069
  } else if (bv == 2) {  // VALUE ==> VALUE
5,880✔
4070
    if (forward) {
5,880✔
4071
      pColData->nVal--;
5,880✔
4072
      if (IS_VAR_DATA_TYPE(pColData->type)) {
5,880✔
4073
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4074
      } else {
4075
        pColData->nData -= TYPE_BYTES[pColData->type];
5,880✔
4076
      }
4077
      return tColDataPutValue(pColData, pData, nData);
5,880✔
4078
    }
4079
  } else {
4080
    return TSDB_CODE_INVALID_PARA;
×
4081
  }
4082
  return 0;
×
4083
}
4084
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
126,389✔
4085
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
126,389✔
4086
  if (bv == 0) {  // NONE ==> NULL
126,767✔
4087
    pColData->numOfNone--;
126,767✔
4088
    pColData->nVal--;
126,767✔
4089
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
126,767✔
4090
      pColData->nData -= TYPE_BYTES[pColData->type];
91,603✔
4091
    }
4092
    if (pColData->numOfNone) {
126,389✔
4093
      return tColDataAppendValue72(pColData, pData, nData);
×
4094
    } else {
4095
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
3,784,337✔
4096
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
3,657,570✔
4097
      }
4098
      pColData->flag = (HAS_VALUE | HAS_NULL);
126,956✔
4099
      return tColDataAppendValue62(pColData, pData, nData);
126,767✔
4100
    }
4101
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4102
    pColData->numOfValue--;
×
4103
    pColData->nVal--;
×
4104
    if (pColData->numOfValue) {
×
4105
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4106
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4107
      } else {
4108
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4109
      }
4110
      return tColDataAppendValue72(pColData, pData, nData);
×
4111
    } else {
4112
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4113
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4114
      }
4115
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4116
      pColData->nData = 0;
×
4117
      return tColDataAppendValue32(pColData, pData, nData);
×
4118
    }
4119
  }
4120
  return 0;
×
4121
}
4122
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
395,721✔
4123
  return 0;
395,721✔
4124
}
4125
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4126
    {NULL, NULL, NULL},                                                     // 0
4127
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4128
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4129
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4130
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4131
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4132
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4133
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4134

4135
    //    VALUE             NONE        NULL
4136
};
4137
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward) {
2,147,483,647✔
4138
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
4139
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
4140

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

4143
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
4144
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData, forward);
2,147,483,647✔
4145
}
4146

4147
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
379,281,258✔
4148
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
379,281,258✔
4149
}
379,324,143✔
4150
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,558,048,484✔
4151
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,558,048,484✔
4152
}
1,557,989,260✔
4153
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
593,089✔
4154
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4155
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
593,089✔
4156
    case 0:
54,152✔
4157
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
54,152✔
4158
      break;
54,152✔
4159
    case 1:
538,748✔
4160
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
538,748✔
4161
      break;
538,748✔
4162
    default:
×
4163
      break;
×
4164
  }
4165
}
592,900✔
4166
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4167
  SValue value = {.type = pColData->type};
2,147,483,647✔
4168
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4169
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4170
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4171
    } else {
4172
      value.nData = pColData->nData - pColData->aOffset[iVal];
98,889,423✔
4173
    }
4174
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4175
  } else {
4176
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4177
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4178
  }
4179
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4180
}
2,147,483,647✔
4181
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
262,117,436✔
4182
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4183
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
262,117,436✔
4184
    case 0:
106,957,940✔
4185
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
106,957,940✔
4186
      break;
106,956,283✔
4187
    case 1:
155,176,398✔
4188
      tColDataGetValue4(pColData, iVal, pColVal);
4189
      break;
155,154,135✔
4190
    default:
×
4191
      break;
×
4192
  }
4193
}
262,110,418✔
4194
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4195
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4196
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4197
    case 0:
1,887,347,963✔
4198
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,887,347,963✔
4199
      break;
1,887,323,354✔
4200
    case 1:
2,147,483,647✔
4201
      tColDataGetValue4(pColData, iVal, pColVal);
4202
      break;
2,147,483,647✔
4203
    default:
×
4204
      break;
×
4205
  }
4206
}
2,147,483,647✔
4207
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
4,011,890✔
4208
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4209
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
4,011,890✔
4210
    case 0:
449,270✔
4211
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
449,270✔
4212
      break;
449,270✔
4213
    case 1:
20,765✔
4214
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
20,765✔
4215
      break;
20,765✔
4216
    case 2:
3,541,855✔
4217
      tColDataGetValue4(pColData, iVal, pColVal);
4218
      break;
3,541,855✔
4219
    default:
×
4220
      break;
×
4221
  }
4222
}
4,011,890✔
4223
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4224
    NULL,               // 0
4225
    tColDataGetValue1,  // HAS_NONE
4226
    tColDataGetValue2,  // HAS_NULL
4227
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4228
    tColDataGetValue4,  // HAS_VALUE
4229
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4230
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4231
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4232
};
4233
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4234
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4235
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4236
    return TSDB_CODE_INVALID_PARA;
×
4237
  }
4238
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4239
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4240
}
4241

4242
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
2,147,483,647✔
4243
  switch (pColData->flag) {
2,147,483,647✔
4244
    case HAS_NONE:
×
4245
      return 0;
×
4246
    case HAS_NULL:
×
4247
      return 1;
×
4248
    case (HAS_NULL | HAS_NONE):
592,515✔
4249
      return GET_BIT1(pColData->pBitMap, iVal);
592,515✔
4250
    case HAS_VALUE:
×
4251
      return 2;
×
4252
    case (HAS_VALUE | HAS_NONE):
2,147,483,647✔
4253
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
2,147,483,647✔
4254
    case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
4255
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
2,147,483,647✔
4256
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
79,038,010✔
4257
      return GET_BIT2(pColData->pBitMap, iVal);
79,038,010✔
4258
    default:
×
4259
      return 0;
×
4260
  }
4261
}
4262

4263
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
3,145,296✔
4264
  int32_t code = 0;
3,145,296✔
4265

4266
  *pColData = *pColDataFrom;
3,145,296✔
4267

4268
  // bitmap
4269
  switch (pColData->flag) {
3,145,338✔
4270
    case (HAS_NULL | HAS_NONE):
411,998✔
4271
    case (HAS_VALUE | HAS_NONE):
4272
    case (HAS_VALUE | HAS_NULL):
4273
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
411,998✔
4274
      if (pColData->pBitMap == NULL) {
411,998✔
4275
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4276
        goto _exit;
×
4277
      }
4278
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
411,998✔
4279
      break;
411,998✔
4280
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
4281
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
4282
      if (pColData->pBitMap == NULL) {
×
4283
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4284
        goto _exit;
×
4285
      }
4286
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
4287
      break;
×
4288
    default:
2,733,340✔
4289
      pColData->pBitMap = NULL;
2,733,340✔
4290
      break;
2,733,340✔
4291
  }
4292

4293
  // offset
4294
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
3,145,338✔
4295
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
227,864✔
4296
    if (pColData->aOffset == NULL) {
227,864✔
4297
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4298
      goto _exit;
×
4299
    }
4300
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
227,864✔
4301
  } else {
4302
    pColData->aOffset = NULL;
2,917,432✔
4303
  }
4304

4305
  // value
4306
  if (pColData->nData) {
3,145,338✔
4307
    pColData->pData = xMalloc(arg, pColData->nData);
3,108,009✔
4308
    if (pColData->pData == NULL) {
3,108,009✔
4309
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4310
      goto _exit;
×
4311
    }
4312

4313
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
3,108,009✔
4314
  } else {
4315
    pColData->pData = NULL;
37,329✔
4316
  }
4317

4318
_exit:
3,145,380✔
4319
  return code;
3,145,380✔
4320
}
4321

4322
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
414,241,151✔
4323
  int32_t code;
4324
  SBuffer local;
414,169,912✔
4325

4326
  if (!(colData->nVal > 0)) {
414,272,185✔
4327
    return TSDB_CODE_INVALID_PARA;
×
4328
  }
4329

4330
  (*info) = (SColDataCompressInfo){
414,281,488✔
4331
      .cmprAlg = info->cmprAlg,
414,279,430✔
4332
      .columnFlag = colData->cflag,
414,283,149✔
4333
      .flag = colData->flag,
414,290,037✔
4334
      .dataType = colData->type,
414,281,441✔
4335
      .columnId = colData->cid,
414,284,448✔
4336
      .numOfData = colData->nVal,
414,276,212✔
4337
  };
4338

4339
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
414,273,064✔
4340
    return 0;
15,707,032✔
4341
  }
4342

4343
  tBufferInit(&local);
4344
  if (assist == NULL) {
398,566,491✔
4345
    assist = &local;
×
4346
  }
4347

4348
  // bitmap
4349
  if (colData->flag != HAS_VALUE) {
398,566,491✔
4350
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
25,949,282✔
4351
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,546,743✔
4352
    } else {
4353
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
24,402,539✔
4354
    }
4355

4356
    SCompressInfo cinfo = {
25,949,282✔
4357
        .dataType = TSDB_DATA_TYPE_TINYINT,
4358
        .cmprAlg = info->cmprAlg,
25,949,282✔
4359
        .originalSize = info->bitmapOriginalSize,
25,949,015✔
4360
    };
4361

4362
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
25,948,969✔
4363
    if (code) {
25,949,438✔
4364
      tBufferDestroy(&local);
4365
      return code;
×
4366
    }
4367

4368
    info->bitmapCompressedSize = cinfo.compressedSize;
25,949,438✔
4369
  }
4370

4371
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
398,532,670✔
4372
    tBufferDestroy(&local);
4373
    return 0;
23,016✔
4374
  }
4375

4376
  // offset
4377
  if (IS_VAR_DATA_TYPE(colData->type)) {
398,515,909✔
4378
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
46,914,431✔
4379

4380
    SCompressInfo cinfo = {
46,925,277✔
4381
        .dataType = TSDB_DATA_TYPE_INT,
4382
        .cmprAlg = info->cmprAlg,
46,927,950✔
4383
        .originalSize = info->offsetOriginalSize,
46,919,198✔
4384
    };
4385

4386
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
46,925,312✔
4387
    if (code) {
46,924,480✔
4388
      tBufferDestroy(&local);
4389
      return code;
×
4390
    }
4391

4392
    info->offsetCompressedSize = cinfo.compressedSize;
46,924,480✔
4393
  }
4394

4395
  // data
4396
  if (colData->nData > 0) {
398,547,545✔
4397
    info->dataOriginalSize = colData->nData;
398,540,368✔
4398

4399
    SCompressInfo cinfo = {
398,548,175✔
4400
        .dataType = colData->type,
398,534,405✔
4401
        .cmprAlg = info->cmprAlg,
398,542,442✔
4402
        .originalSize = info->dataOriginalSize,
398,538,099✔
4403
    };
4404

4405
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
398,526,769✔
4406
    if (code) {
398,526,631✔
4407
      tBufferDestroy(&local);
4408
      return code;
×
4409
    }
4410

4411
    info->dataCompressedSize = cinfo.compressedSize;
398,526,631✔
4412
  }
4413

4414
  tBufferDestroy(&local);
4415
  return 0;
398,526,012✔
4416
}
4417

4418
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
518,804,166✔
4419
  int32_t  code;
4420
  SBuffer  local;
518,757,254✔
4421
  uint8_t *data = (uint8_t *)input;
518,888,278✔
4422

4423
  tBufferInit(&local);
4424
  if (assist == NULL) {
518,920,135✔
4425
    assist = &local;
×
4426
  }
4427

4428
  tColDataClear(colData);
518,920,135✔
4429
  colData->cid = info->columnId;
518,897,686✔
4430
  colData->type = info->dataType;
518,917,723✔
4431
  colData->cflag = info->columnFlag;
518,901,055✔
4432
  colData->nVal = info->numOfData;
518,940,828✔
4433
  colData->flag = info->flag;
518,898,377✔
4434

4435
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
518,930,669✔
4436
    goto _exit;
65,769,954✔
4437
  }
4438

4439
  // bitmap
4440
  if (info->bitmapOriginalSize > 0) {
453,095,644✔
4441
    SCompressInfo cinfo = {
59,705,680✔
4442
        .dataType = TSDB_DATA_TYPE_TINYINT,
4443
        .cmprAlg = info->cmprAlg,
59,708,932✔
4444
        .originalSize = info->bitmapOriginalSize,
59,708,647✔
4445
        .compressedSize = info->bitmapCompressedSize,
59,698,250✔
4446
    };
4447

4448
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
59,706,566✔
4449
    if (code) {
59,700,708✔
4450
      tBufferDestroy(&local);
4451
      return code;
×
4452
    }
4453

4454
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
59,700,708✔
4455
    if (code) {
59,693,355✔
4456
      tBufferDestroy(&local);
4457
      return code;
×
4458
    }
4459

4460
    data += cinfo.compressedSize;
59,693,355✔
4461
  }
4462

4463
  if (info->flag == (HAS_NONE | HAS_NULL)) {
453,159,018✔
4464
    goto _exit;
25,893✔
4465
  }
4466

4467
  // offset
4468
  if (info->offsetOriginalSize > 0) {
453,057,997✔
4469
    SCompressInfo cinfo = {
90,589,514✔
4470
        .cmprAlg = info->cmprAlg,
90,579,305✔
4471
        .dataType = TSDB_DATA_TYPE_INT,
4472
        .originalSize = info->offsetOriginalSize,
90,600,174✔
4473
        .compressedSize = info->offsetCompressedSize,
90,591,810✔
4474
    };
4475

4476
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
90,591,279✔
4477
    if (code) {
90,585,998✔
4478
      tBufferDestroy(&local);
4479
      return code;
×
4480
    }
4481

4482
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
90,585,998✔
4483
    if (code) {
90,550,303✔
4484
      tBufferDestroy(&local);
4485
      return code;
×
4486
    }
4487

4488
    data += cinfo.compressedSize;
90,550,303✔
4489
  }
4490

4491
  // data
4492
  if (info->dataOriginalSize > 0) {
453,072,235✔
4493
    colData->nData = info->dataOriginalSize;
453,061,952✔
4494

4495
    SCompressInfo cinfo = {
453,084,010✔
4496
        .cmprAlg = info->cmprAlg,
906,087,981✔
4497
        .dataType = colData->type,
453,068,736✔
4498
        .originalSize = info->dataOriginalSize,
453,073,337✔
4499
        .compressedSize = info->dataCompressedSize,
453,054,203✔
4500
    };
4501

4502
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
453,066,807✔
4503
    if (code) {
453,010,510✔
4504
      tBufferDestroy(&local);
4505
      return code;
×
4506
    }
4507

4508
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
453,010,510✔
4509
    if (code) {
452,993,915✔
4510
      tBufferDestroy(&local);
4511
      return code;
×
4512
    }
4513

4514
    data += cinfo.compressedSize;
452,993,915✔
4515
  }
4516

4517
_exit:
518,872,208✔
4518
  switch (colData->flag) {
518,914,987✔
4519
    case HAS_NONE:
9,807,943✔
4520
      colData->numOfNone = colData->nVal;
9,807,943✔
4521
      break;
9,807,631✔
4522
    case HAS_NULL:
55,944,132✔
4523
      colData->numOfNull = colData->nVal;
55,944,132✔
4524
      break;
55,959,189✔
4525
    case HAS_VALUE:
393,341,974✔
4526
      colData->numOfValue = colData->nVal;
393,341,974✔
4527
      break;
393,413,425✔
4528
    default:
59,703,124✔
4529
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4530
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4531
        if (bitValue == 0) {
2,147,483,647✔
4532
          colData->numOfNone++;
65,598,649✔
4533
        } else if (bitValue == 1) {
2,147,483,647✔
4534
          colData->numOfNull++;
2,147,483,647✔
4535
        } else {
4536
          colData->numOfValue++;
2,147,483,647✔
4537
        }
4538
      }
4539
  }
4540
  tBufferDestroy(&local);
4541
  return 0;
518,804,388✔
4542
}
4543

4544
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
28,875✔
4545
                                    char *data) {
4546
  int32_t code = 0;
28,875✔
4547
  if (data == NULL) {
28,875✔
4548
    if (pColData->cflag & COL_IS_KEY) {
709✔
4549
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4550
    } else {
4551
      for (int32_t i = 0; i < nRows; ++i) {
1,532✔
4552
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
823✔
4553
      }
4554
    }
4555
    goto _exit;
709✔
4556
  }
4557

4558
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
28,166✔
4559
    if (!IS_STR_DATA_BLOB(type)) {
4,617✔
4560
      for (int32_t i = 0; i < nRows; ++i) {
13,139✔
4561
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
8,522✔
4562
        if (offset == -1) {
8,522✔
4563
          if (pColData->cflag & COL_IS_KEY) {
×
4564
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4565
            goto _exit;
×
4566
          }
4567
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4568
            goto _exit;
×
4569
          }
4570
        } else {
4571
          if (varDataTLen(data + offset) > bytes) {
8,522✔
4572
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4573
                   bytes);
4574
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4575
            goto _exit;
×
4576
          }
4577
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
17,044✔
4578
                                                                        varDataLen(data + offset));
8,522✔
4579
        }
4580
      }
4581
    } else {
4582
      for (int32_t i = 0; i < nRows; ++i) {
×
4583
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4584
        if (offset == -1) {
×
4585
          if (pColData->cflag & COL_IS_KEY) {
×
4586
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4587
            goto _exit;
×
4588
          }
4589
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4590
            goto _exit;
×
4591
          }
4592
        } else {
4593
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4594
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4595
                   (int)blobDataTLen(data + offset), bytes);
4596
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4597
            goto _exit;
×
4598
          }
4599
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4600
                                                                        blobDataLen(data + offset));
×
4601
        }
4602
      }
4603
    }
4604
  } else {  // fixed-length data type
4605
    bool allValue = true;
23,549✔
4606
    bool allNull = true;
23,549✔
4607
    for (int32_t i = 0; i < nRows; ++i) {
67,038✔
4608
      if (!BMIsNull(lengthOrbitmap, i)) {
43,489✔
4609
        allNull = false;
29,556✔
4610
      } else {
4611
        allValue = false;
13,933✔
4612
      }
4613
    }
4614
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
23,549✔
4615
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4616
      goto _exit;
×
4617
    }
4618

4619
    if (allValue) {
23,549✔
4620
      // optimize (todo)
4621
      for (int32_t i = 0; i < nRows; ++i) {
46,100✔
4622
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
28,902✔
4623
      }
4624
    } else if (allNull) {
6,351✔
4625
      // optimize (todo)
4626
      for (int32_t i = 0; i < nRows; ++i) {
17,466✔
4627
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
11,593✔
4628
        if (code) goto _exit;
11,593✔
4629
      }
4630
    } else {
4631
      for (int32_t i = 0; i < nRows; ++i) {
3,472✔
4632
        if (BMIsNull(lengthOrbitmap, i)) {
2,994✔
4633
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,340✔
4634
          if (code) goto _exit;
2,340✔
4635
        } else {
4636
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
654✔
4637
        }
4638
      }
4639
    }
4640
  }
4641

4642
_exit:
478✔
4643
  return code;
28,875✔
4644
}
4645
int32_t tColDataAddValueByDataBlockWithBlob(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows,
×
4646
                                            char *lengthOrbitmap, char *data, void *pBlobSet) {
4647
  int32_t code = 0;
×
4648
  if (data == NULL) {
×
4649
    if (pColData->cflag & COL_IS_KEY) {
×
4650
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4651
    } else {
4652
      for (int32_t i = 0; i < nRows; ++i) {
×
4653
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4654
      }
4655
    }
4656
    goto _exit;
×
4657
  }
4658

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

4720
    if (allValue) {
×
4721
      // optimize (todo)
4722
      for (int32_t i = 0; i < nRows; ++i) {
×
4723
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4724
      }
4725
    } else if (allNull) {
×
4726
      // optimize (todo)
4727
      for (int32_t i = 0; i < nRows; ++i) {
×
4728
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4729
        if (code) goto _exit;
×
4730
      }
4731
    } else {
4732
      for (int32_t i = 0; i < nRows; ++i) {
×
4733
        if (BMIsNull(lengthOrbitmap, i)) {
×
4734
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4735
          if (code) goto _exit;
×
4736
        } else {
4737
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4738
        }
4739
      }
4740
    }
4741
  }
4742

4743
_exit:
×
4744
  return code;
×
4745
}
4746

4747
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
2,147,483,647✔
4748
                               checkWKBGeometryFn cgeos) {
4749
  int32_t code = 0;
2,147,483,647✔
4750

4751
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
2,147,483,647✔
4752
    if (!(pColData->type == pBind->buffer_type)) {
2,147,483,647✔
4753
      return TSDB_CODE_INVALID_PARA;
×
4754
    }
4755
  }
4756

4757
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
2,147,483,647✔
4758
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
944,412,741✔
4759
      code = igeos();
7,560,410✔
4760
      if (code) {
7,530,910✔
4761
        return code;
×
4762
      }
4763
    }
4764
    for (int32_t i = 0; i < pBind->num; ++i) {
1,911,892,121✔
4765
      if (pBind->is_null && pBind->is_null[i]) {
960,363,637✔
4766
        if (pColData->cflag & COL_IS_KEY) {
47,502,376✔
4767
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,342✔
4768
          goto _exit;
1,342✔
4769
        }
4770
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
47,504,102✔
4771
        if (code) goto _exit;
47,487,764✔
4772
      } else if (pBind->length[i] > buffMaxLen) {
912,736,000✔
4773
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4774
      } else {
4775
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
914,240,876✔
4776
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
7,578,350✔
4777
          if (code) {
7,501,350✔
4778
            uError("stmt col[%d] bind geometry wrong format", i);
×
4779
            goto _exit;
×
4780
          }
4781
        }
4782
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,828,085,649✔
4783
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
914,646,102✔
4784
      }
4785
    }
4786
  } else {  // fixed-length data type
4787
    bool allValue;
4788
    bool allNull;
4789
    if (pBind->is_null) {
2,147,483,647✔
4790
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
236,070,945✔
4791
      allNull = (same && pBind->is_null[0] != 0);
235,900,264✔
4792
      allValue = (same && pBind->is_null[0] == 0);
236,015,042✔
4793
    } else {
4794
      allNull = false;
2,147,483,647✔
4795
      allValue = true;
2,147,483,647✔
4796
    }
4797

4798
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
2,147,483,647✔
4799
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,684✔
4800
      goto _exit;
2,684✔
4801
    }
4802

4803
    if (allValue) {
2,147,483,647✔
4804
      // optimize (todo)
4805
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4806
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4807
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4808
      }
4809
    } else if (allNull) {
241,878,048✔
4810
      // optimize (todo)
4811
      for (int32_t i = 0; i < pBind->num; ++i) {
468,642,388✔
4812
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
234,201,255✔
4813
        if (code) goto _exit;
234,348,359✔
4814
      }
4815
    } else {
4816
      for (int32_t i = 0; i < pBind->num; ++i) {
16,073,583✔
4817
        if (pBind->is_null[i]) {
8,489,564✔
4818
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,753,154✔
4819
          if (code) goto _exit;
3,753,154✔
4820
        } else {
4821
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,472,820✔
4822
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,736,410✔
4823
        }
4824
      }
4825
    }
4826
  }
4827

4828
_exit:
210,141,038✔
4829
  return code;
2,147,483,647✔
4830
}
4831

4832
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
21,576✔
4833
  int32_t code = 0;
21,576✔
4834

4835
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
21,576✔
4836
    if (!(pColData->type == pBind->buffer_type)) {
21,576✔
4837
      return TSDB_CODE_INVALID_PARA;
×
4838
    }
4839
  }
4840

4841
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
21,576✔
4842
    uint8_t *buf = pBind->buffer;
×
4843
    for (int32_t i = 0; i < pBind->num; ++i) {
×
4844
      if (pBind->is_null && pBind->is_null[i]) {
×
4845
        if (pColData->cflag & COL_IS_KEY) {
×
4846
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4847
          goto _exit;
×
4848
        }
4849
        if (pBind->is_null[i] == 1) {
×
4850
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4851
          if (code) goto _exit;
×
4852
        } else {
4853
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4854
          if (code) goto _exit;
×
4855
        }
4856
      } else if (pBind->length[i] > buffMaxLen) {
×
4857
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4858
      } else {
4859
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
×
4860
        buf += pBind->length[i];
×
4861
      }
4862
    }
4863
  } else {  // fixed-length data type
4864
    bool allValue;
4865
    bool allNull;
4866
    bool allNone;
4867
    if (pBind->is_null) {
21,576✔
4868
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
11,376✔
4869
      allNull = (same && pBind->is_null[0] == 1);
11,376✔
4870
      allNone = (same && pBind->is_null[0] > 1);
11,376✔
4871
      allValue = (same && pBind->is_null[0] == 0);
11,376✔
4872
    } else {
4873
      allNull = false;
10,200✔
4874
      allNone = false;
10,200✔
4875
      allValue = true;
10,200✔
4876
    }
4877

4878
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
21,576✔
4879
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4880
      goto _exit;
×
4881
    }
4882

4883
    uint8_t *buf = pBind->buffer;
21,576✔
4884

4885
    if (allValue) {
21,576✔
4886
      // optimize (todo)
4887
      for (int32_t i = 0; i < pBind->num; ++i) {
210,552✔
4888
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
188,976✔
4889
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
188,976✔
4890
          *val = 1;
×
4891
        }
4892
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
188,976✔
4893
      }
4894
    } else if (allNull) {
×
4895
      // optimize (todo)
4896
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4897
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4898
        if (code) goto _exit;
×
4899
      }
4900
    } else if (allNone) {
×
4901
      // optimize (todo)
4902
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4903
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4904
        if (code) goto _exit;
×
4905
      }
4906
    } else {
4907
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4908
        if (pBind->is_null[i]) {
×
4909
          if (pBind->is_null[i] == 1) {
×
4910
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4911
            if (code) goto _exit;
×
4912
          } else {
4913
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4914
            if (code) goto _exit;
×
4915
          }
4916
        } else {
4917
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
4918
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
4919
            *val = 1;
×
4920
          }
4921

4922
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
×
4923
        }
4924
      }
4925
    }
4926
  }
4927

4928
_exit:
×
4929
  return code;
21,576✔
4930
}
4931

4932
int32_t tColDataAddValueByBind2WithGeos(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
×
4933
                                        initGeosFn igeos, checkWKBGeometryFn cgeos) {
4934
  int32_t code = 0;
×
4935

4936
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
×
4937
    if (!(pColData->type == pBind->buffer_type)) {
×
4938
      return TSDB_CODE_INVALID_PARA;
×
4939
    }
4940
  }
4941

4942
  if (pColData->type != TSDB_DATA_TYPE_GEOMETRY) {
×
4943
    return TSDB_CODE_INVALID_OPTION;
×
4944
  }
4945

4946
  code = igeos();
×
4947
  if (code) {
×
4948
    return code;
×
4949
  }
4950

4951
  uint8_t *buf = pBind->buffer;
×
4952
  for (int32_t i = 0; i < pBind->num; ++i) {
×
4953
    if (pBind->is_null && pBind->is_null[i]) {
×
4954
      if (pColData->cflag & COL_IS_KEY) {
×
4955
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4956
        goto _exit;
×
4957
      }
4958
      if (pBind->is_null[i] == 1) {
×
4959
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4960
        if (code) goto _exit;
×
4961
      } else {
4962
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4963
        if (code) goto _exit;
×
4964
      }
4965
    } else if (pBind->length[i] > buffMaxLen) {
×
4966
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4967
    } else {
4968
      code = cgeos(buf, pBind->length[i]);
×
4969
      if (code) {
×
4970
        uError("stmt2 col[%d] bind geometry with wrong format", i);
×
4971
        goto _exit;
×
4972
      }
4973
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
×
4974
      buf += pBind->length[i];
×
4975
    }
4976
  }
4977

4978
_exit:
×
4979
  return code;
×
4980
}
4981

4982
int32_t tColDataAddValueByBind2WithBlob(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
×
4983
                                        SBlobSet *pBlobSet) {
4984
  int32_t code = 0;
×
4985

4986
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
×
4987
    if (!(pColData->type == pBind->buffer_type)) {
×
4988
      return TSDB_CODE_INVALID_PARA;
×
4989
    }
4990
  }
4991

4992
  if (IS_STR_DATA_BLOB(pColData->type)) {  // var-length data type
×
4993
    uint8_t *buf = pBind->buffer;
×
4994
    for (int32_t i = 0; i < pBind->num; ++i) {
×
4995
      if (pBind->is_null && pBind->is_null[i]) {
×
4996
        if (pColData->cflag & COL_IS_KEY) {
×
4997
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4998
          goto _exit;
×
4999
        }
5000
        if (pBind->is_null[i] == 1) {
×
5001
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0);
×
5002
          if (code) goto _exit;
×
5003
        } else {
5004
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NONE](pBlobSet, pColData, NULL, 0);
×
5005
          if (code) goto _exit;
×
5006
        }
5007
      } else if (pBind->length[i] > buffMaxLen) {
×
5008
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5009
      } else {
5010
        code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](pBlobSet, pColData, buf, pBind->length[i]);
×
5011
        buf += pBind->length[i];
×
5012
      }
5013
    }
5014
  }
5015
_exit:
×
5016
  return code;
×
5017
}
5018

5019
int32_t tColDataAddValueByBind2WithDecimal(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
×
5020
                                           uint8_t precision, uint8_t scale) {
5021
  int32_t code = 0;
×
5022

5023
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
×
5024
    if (!(pColData->type == pBind->buffer_type)) {
×
5025
      return TSDB_CODE_INVALID_PARA;
×
5026
    }
5027
  }
5028

5029
  if (!IS_DECIMAL_TYPE(pColData->type)) {
×
5030
    return TSDB_CODE_INVALID_OPTION;
×
5031
  }
5032

5033
  uint8_t *buf = pBind->buffer;
×
5034
  for (int32_t i = 0; i < pBind->num; ++i) {
×
5035
    if (pBind->is_null && pBind->is_null[i]) {
×
5036
      if (pColData->cflag & COL_IS_KEY) {
×
5037
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5038
        goto _exit;
×
5039
      }
5040
      if (pBind->is_null[i] == 1) {
×
5041
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
5042
        if (code) goto _exit;
×
5043
      } else {
5044
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
5045
        if (code) goto _exit;
×
5046
      }
5047
    } else {
5048
      if (pColData->type == TSDB_DATA_TYPE_DECIMAL64) {
×
5049
        Decimal64 dec = {0};
×
5050
        int32_t   code = decimal64FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
×
5051
        buf += pBind->length[i];
×
5052
        if (TSDB_CODE_SUCCESS != code) {
×
5053
          return code;
×
5054
        }
5055
        int64_t tmp = DECIMAL64_GET_VALUE(&dec);
×
5056
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)&tmp,
×
5057
                                                                      TYPE_BYTES[pColData->type]);
×
5058
      } else if (pColData->type == TSDB_DATA_TYPE_DECIMAL) {
×
5059
        Decimal128 dec = {0};
×
5060
        int32_t    code = decimal128FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
×
5061
        buf += pBind->length[i];
×
5062
        if (TSDB_CODE_SUCCESS != code) {
×
5063
          return code;
×
5064
        }
5065
        uint8_t *pV = taosMemCalloc(1, sizeof(Decimal128));
×
5066
        if (!pV) return terrno;
×
5067
        memcpy(pV, &dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
×
5068
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pV, TYPE_BYTES[pColData->type]);
×
5069
      }
5070
    }
5071
  }
5072

5073
_exit:
×
5074
  return code;
×
5075
}
5076
/* build rows to `rowArray` from bind
5077
 * `infos` is the bind information array
5078
 * `numOfInfos` is the number of bind information
5079
 * `infoSorted` is whether the bind information is sorted by column id
5080
 * `pTSchema` is the schema of the table
5081
 * `rowArray` is the array to store the rows
5082
 * `pOrdered` is the pointer to store ordered
5083
 * `pDupTs` is the pointer to store duplicateTs
5084
 */
5085
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
16,466,999✔
5086
                           const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray, bool *pOrdered,
5087
                           bool *pDupTs) {
5088
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
16,466,999✔
5089
    return TSDB_CODE_INVALID_PARA;
×
5090
  }
5091
  int8_t hasBlob = schemaHasBlob(pTSchema);
16,475,111✔
5092
  if (!infoSorted) {
16,475,319✔
5093
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5094
  }
5095

5096
  int32_t code = 0;
16,475,319✔
5097
  int32_t numOfRows = -1;
16,475,319✔
5098
  SArray *colValArray, *bufArray;
5099
  SColVal colVal;
2,011,863✔
5100
  int32_t numOfFixedValue = 0;
16,475,045✔
5101
  int32_t lino = 0;
16,475,045✔
5102

5103
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
16,474,223✔
5104
    return terrno;
×
5105
  }
5106
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
16,472,429✔
5107
    taosArrayDestroy(colValArray);
274✔
5108
    return terrno;
×
5109
  }
5110
  for (int i = 0; i < numOfInfos; ++i) {
106,690,840✔
5111
    if (parsedCols) {
90,210,098✔
5112
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
×
5113
      if (pParsedVal) {
×
5114
        continue;
×
5115
      }
5116
    }
5117
    if (numOfRows == -1) {
90,210,098✔
5118
      numOfRows = infos[i].bind->num;
16,475,814✔
5119
    }
5120

5121
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
180,424,612✔
5122
      taosArrayDestroy(colValArray);
×
5123
      taosArrayDestroy(bufArray);
×
5124
      return terrno;
×
5125
    }
5126
  }
5127

5128
  SRowKey rowKey, lastRowKey;
2,015,408✔
5129
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
182,787,385✔
5130
    taosArrayClear(colValArray);
166,394,139✔
5131

5132
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
873,505,835✔
5133
      if (parsedCols) {
706,218,017✔
5134
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
×
5135
        if (pParsedVal) {
×
5136
          numOfFixedValue++;
×
5137
          colVal = *pParsedVal;
×
5138

5139
          if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5140
            if (IS_VAR_DATA_TYPE(pParsedVal->value.type)) {
×
5141
              taosMemoryFree(colVal.value.pData);
×
5142
            }
5143
            code = terrno;
×
5144
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5145
          }
5146
          continue;
×
5147
        }
5148
      }
5149

5150
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
706,311,951✔
5151
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5152
          if (iInfo == 0) {
×
5153
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5154
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5155
          }
5156
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5157
        } else {
5158
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5159
        }
5160
      } else {
5161
        SValue value = {
706,324,876✔
5162
            .type = infos[iInfo].type,
706,282,879✔
5163
        };
5164
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
706,326,896✔
5165
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
23,031,456✔
UNCOV
5166
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5167
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5168
            value.nData = length;
×
5169
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5170
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5171
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5172
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5173
              goto _exit;
×
5174
            }
5175
            value.pData = *data;
×
5176
            *data += length;
×
5177
          } else {
5178
            int32_t   length = infos[iInfo].bind->length[iRow];
21,716,196✔
5179
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
21,713,466✔
5180
            value.nData = length;
21,716,205✔
5181
            if (value.nData > infos[iInfo].bytes - VARSTR_HEADER_SIZE) {
21,716,205✔
5182
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5183
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5184
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5185
              goto _exit;
×
5186
            }
5187
            value.pData = *data;
21,715,653✔
5188
            *data += length;
21,715,923✔
5189
          }
5190
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5191
        } else {
5192
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
684,727,498✔
5193
            if (!pSchemaExt) {
×
5194
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5195
              code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
5196
              goto _exit;
×
5197
            }
5198
            uint8_t precision = 0, scale = 0;
×
5199
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5200
            Decimal128 dec = {0};
×
5201
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5202
            int32_t    length = infos[iInfo].bind->length[iRow];
×
5203
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
×
5204
            *data += length;
×
5205
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5206

5207
            // precision check
5208
            // scale auto fit
5209

5210
            code = decimal128ToDataVal(&dec, &value);
×
5211
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5212

5213
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
684,736,576✔
5214
            if (!pSchemaExt) {
×
5215
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5216
              code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
5217
              goto _exit;
×
5218
            }
5219
            uint8_t precision = 0, scale = 0;
×
5220
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5221
            Decimal64 dec = {0};
×
5222
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5223
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5224
            code = decimal64FromStr(*(char **)data, length, precision, scale, &dec);
×
5225
            *data += length;
×
5226
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5227

5228
            code = decimal64ToDataVal(&dec, &value);
×
5229
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5230

5231
          } else {
5232
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
684,736,282✔
5233
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
684,617,213✔
5234
              *val = 1;
×
5235
            }
5236
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
684,617,213✔
5237
          }
5238
        }
5239
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
707,192,934✔
5240
      }
5241
      if (taosArrayPush(colValArray, &colVal) == NULL) {
707,111,153✔
5242
        code = terrno;
×
5243
        goto _exit;
×
5244
      }
5245
    }
5246

5247
    SRow *row;
13,514,230✔
5248

5249
    if (hasBlob == 0) {
167,142,541✔
5250
      SRowBuildScanInfo sinfo = {0};
166,355,700✔
5251
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
166,355,437✔
5252
      TAOS_CHECK_GOTO(code, &lino, _exit);
166,344,185✔
5253
    } else {
5254
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
786,841✔
5255
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
786,271✔
5256
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5257
    }
5258

5259
    if ((taosArrayPush(rowArray, &row)) == NULL) {
166,286,473✔
5260
      code = terrno;
×
5261
      goto _exit;
×
5262
    }
5263

5264
    if (pOrdered && pDupTs) {
166,286,473✔
5265
      tRowGetKey(row, &rowKey);
332,596,960✔
5266
      if (iRow == 0) {
166,323,405✔
5267
        *pOrdered = true;
16,467,293✔
5268
        *pDupTs = false;
16,467,293✔
5269
      } else {
5270
        if (*pOrdered) {
149,856,112✔
5271
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
149,860,127✔
5272
          *pOrdered = (res >= 0);
149,860,127✔
5273
          if (!*pDupTs) {
149,860,407✔
5274
            *pDupTs = (res == 0);
149,859,902✔
5275
          }
5276
        }
5277
      }
5278
      lastRowKey = rowKey;
166,322,050✔
5279
    }
5280
  }
5281
_exit:
16,393,538✔
5282
  if (code != 0) {
16,393,246✔
5283
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
×
5284
  }
5285
  taosArrayDestroy(colValArray);
16,393,246✔
5286
  taosArrayDestroy(bufArray);
16,477,655✔
5287
  return code;
16,483,876✔
5288
}
5289
/* build rows to `rowArray` from bind
5290
 * `infos` is the bind information array
5291
 * `numOfInfos` is the number of bind information
5292
 * `infoSorted` is whether the bind information is sorted by column id
5293
 * `pTSchema` is the schema of the table
5294
 * `rowArray` is the array to store the rows
5295
 * `pOrdered` is the pointer to store ordered
5296
 * `pDupTs` is the pointer to store duplicateTs
5297
 */
5298
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
×
5299
                                   SArray *rowArray, bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5300
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
5301
    return TSDB_CODE_INVALID_PARA;
×
5302
  }
5303
  int8_t hasBlob = schemaHasBlob(pTSchema);
×
5304
  if (!infoSorted) {
×
5305
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5306
  }
5307

5308
  int32_t code = 0;
×
5309
  int32_t numOfRows = infos[0].bind->num;
×
5310
  SArray *colValArray, *bufArray;
5311
  SColVal colVal;
×
5312

5313
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
×
5314
    return terrno;
×
5315
  }
5316
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
×
5317
    taosArrayDestroy(colValArray);
×
5318
    return terrno;
×
5319
  }
5320
  for (int i = 0; i < numOfInfos; ++i) {
×
5321
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
×
5322
      taosArrayDestroy(colValArray);
×
5323
      taosArrayDestroy(bufArray);
×
5324
      return terrno;
×
5325
    }
5326
  }
5327

5328
  SRowKey rowKey, lastRowKey;
×
5329
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
×
5330
    taosArrayClear(colValArray);
×
5331

5332
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
×
5333
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
×
5334
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5335
          if (iInfo == 0) {
×
5336
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5337
            goto _exit;
×
5338
          }
5339
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5340
        } else {
5341
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5342
        }
5343
      } else {
5344
        SValue value = {
×
5345
            .type = infos[iInfo].type,
×
5346
        };
5347
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
×
5348
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
×
5349
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5350
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
5351
            value.nData = length;
×
5352
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5353
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5354
              uError("stmt bind param[%d] length:%d  greater than type maximum lenght: %d", iInfo, value.nData,
×
5355
                     pTSchema->columns[infos[iInfo].columnId - 1].bytes);
5356
              goto _exit;
×
5357
            }
5358
            value.pData = *data;
×
5359
            *data += length;
×
5360
          } else {
5361
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5362
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
5363
            value.nData = length;
×
5364
            if (value.nData > pTSchema->columns[infos[iInfo].columnId - 1].bytes - VARSTR_HEADER_SIZE) {
×
5365
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5366
              uError("stmt bind param[%d] length:%d  greater than type maximum lenght: %d", iInfo, value.nData,
×
5367
                     pTSchema->columns[infos[iInfo].columnId - 1].bytes);
5368
              goto _exit;
×
5369
            }
5370
            value.pData = *data;
×
5371
            *data += length;
×
5372
          }
5373

5374
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5375
        } else {
5376
          uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
×
5377
          if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
×
5378
            *val = 1;
×
5379
          }
5380
          valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
×
5381
        }
5382
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
×
5383
      }
5384
      if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5385
        code = terrno;
×
5386
        goto _exit;
×
5387
      }
5388
    }
5389

5390
    SRow *row;
×
5391

5392
    if (hasBlob == 0) {
×
5393
      SRowBuildScanInfo sinfo = {0};
×
5394
      if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
×
5395
        goto _exit;
×
5396
      }
5397
    } else {
5398
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
×
5399
      if ((code = tRowBuildWithBlob(colValArray, pTSchema, &row, pBlobSet, &sinfo))) {
×
5400
        goto _exit;
×
5401
      }
5402
    }
5403

5404
    if ((taosArrayPush(rowArray, &row)) == NULL) {
×
5405
      code = terrno;
×
5406
      goto _exit;
×
5407
    }
5408

5409
    if (pOrdered && pDupTs) {
×
5410
      tRowGetKey(row, &rowKey);
×
5411
      if (iRow == 0) {
×
5412
        *pOrdered = true;
×
5413
        *pDupTs = false;
×
5414
      } else {
5415
        if (*pOrdered) {
×
5416
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
×
5417
          *pOrdered = (res >= 0);
×
5418
          if (!*pDupTs) {
×
5419
            *pDupTs = (res == 0);
×
5420
          }
5421
        }
5422
      }
5423
      lastRowKey = rowKey;
×
5424
    }
5425
  }
5426
_exit:
×
5427
  taosArrayDestroy(colValArray);
×
5428
  taosArrayDestroy(bufArray);
×
5429
  return code;
×
5430
}
5431

5432
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
45,284,190✔
5433
  int32_t code = TSDB_CODE_SUCCESS;
45,284,190✔
5434

5435
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
45,284,190✔
5436
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,703,049✔
5437
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
5,133,123✔
5438
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
10,836,172✔
5439
    if (iToRow == 0) {
5,703,049✔
5440
      pToColData->aOffset[iToRow] = 0;
12,286✔
5441
    }
5442

5443
    if (iToRow < pToColData->nVal - 1) {
5,703,049✔
5444
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,691,692✔
5445
    }
5446

5447
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,703,049✔
5448
                 nData);
5449
  } else {
5450
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
39,581,141✔
5451
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
39,581,141✔
5452
  }
5453
  return code;
45,284,190✔
5454
}
5455

5456
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
46,912,854✔
5457
                                        int32_t iToRow) {
5458
  int32_t code = TSDB_CODE_SUCCESS;
46,912,854✔
5459
  int     bit_val = 0;
46,912,854✔
5460

5461
  switch (pFromColData->flag) {
46,912,854✔
5462
    case HAS_NONE: {
×
5463
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5464
    } break;
×
5465
    case HAS_NULL: {
1,628,664✔
5466
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,628,664✔
5467
    } break;
1,628,664✔
5468
    case (HAS_NULL | HAS_NONE): {
×
5469
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5470
      if (0 == bit_val)
×
5471
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5472
      else
5473
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5474
    } break;
×
5475
    case HAS_VALUE: {
5,065,622✔
5476
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
5,065,622✔
5477
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
5,065,622✔
5478
    } break;
5,065,622✔
5479
    case (HAS_VALUE | HAS_NONE): {
×
5480
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5481
      if (0 == bit_val)
×
5482
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5483
      else
5484
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5485
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5486
    } break;
×
5487
    case (HAS_VALUE | HAS_NULL): {
40,218,568✔
5488
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
40,218,568✔
5489
      if (0 == bit_val)
40,218,568✔
5490
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
18,155,807✔
5491
      else
5492
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
22,062,761✔
5493
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
40,218,568✔
5494
    } break;
40,218,568✔
5495
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5496
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5497
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5498
    } break;
×
5499
    default:
×
5500
      return -1;
×
5501
  }
5502

5503
  return code;
46,912,854✔
5504
}
5505

5506
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
2,935,295✔
5507
                               int32_t nColData) {
5508
  int32_t code = TSDB_CODE_SUCCESS;
2,935,295✔
5509

5510
  for (int32_t i = 0; i < nColData; i++) {
49,848,149✔
5511
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
46,912,854✔
5512
    if (code != TSDB_CODE_SUCCESS) {
46,912,854✔
5513
      return code;
×
5514
    }
5515
  }
5516

5517
  return code;
2,935,295✔
5518
}
5519

5520
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
2,935,295✔
5521
  int32_t code = TSDB_CODE_SUCCESS;
2,935,295✔
5522

5523
  for (int32_t i = 0; i < nColData; i++) {
49,848,149✔
5524
    SColVal cv = {0};
46,912,854✔
5525
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
46,912,854✔
5526
    if (code != TSDB_CODE_SUCCESS) {
46,912,854✔
5527
      return code;
×
5528
    }
5529
    code = tColDataAppendValue(&aToColData[i], &cv);
46,912,854✔
5530
    if (code != TSDB_CODE_SUCCESS) {
46,912,854✔
5531
      return code;
×
5532
    }
5533
  }
5534

5535
  return code;
2,935,295✔
5536
}
5537

5538
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
1,992,939,928✔
5539
  SColVal cv;
1,944,772,833✔
5540

5541
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
1,998,788,329✔
5542
  key->numOfPKs = 0;
1,999,264,748✔
5543

5544
  for (int i = 1; i < nColData; i++) {
2,004,252,872✔
5545
    if (aColData[i].cflag & COL_IS_KEY) {
2,004,089,388✔
5546
      tColDataGetValue4(&aColData[i], iRow, &cv);
5,181,804✔
5547
      key->pks[key->numOfPKs++] = cv.value;
5,205,889✔
5548
    } else {
5549
      break;
1,998,077,400✔
5550
    }
5551
  }
5552
}
1,998,177,342✔
5553

5554
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
326,397✔
5555
  SColData *aDstColData = NULL;
326,397✔
5556
  int32_t   i = start, j = mid + 1, k = 0;
326,397✔
5557
  SRowKey   keyi, keyj;
326,397✔
5558

5559
  if (end > start) {
326,397✔
5560
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
326,397✔
5561
    if (aDstColData == NULL) {
326,397✔
5562
      return terrno;
×
5563
    }
5564
    for (int c = 0; c < nColData; ++c) {
5,532,827✔
5565
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
5,206,430✔
5566
    }
5567
  }
5568

5569
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
326,397✔
5570
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
326,397✔
5571
  while (i <= mid && j <= end) {
1,794,191✔
5572
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,467,794✔
5573
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,846✔
5574
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
1,846✔
5575
    } else {
5576
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,465,948✔
5577
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,465,948✔
5578
    }
5579
  }
5580

5581
  while (i <= mid) {
1,792,471✔
5582
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,466,074✔
5583
  }
5584

5585
  while (j <= end) {
327,824✔
5586
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,427✔
5587
  }
5588

5589
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,261,692✔
5590
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
2,935,295✔
5591
  }
5592

5593
  if (aDstColData) {
326,397✔
5594
    for (int32_t i = 0; i < nColData; i++) {
5,532,827✔
5595
      tColDataDestroy(&aDstColData[i]);
5,206,430✔
5596
    }
5597
    taosMemoryFree(aDstColData);
326,397✔
5598
  }
5599

5600
  return TSDB_CODE_SUCCESS;
326,397✔
5601
}
5602

5603
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
653,782✔
5604
  int32_t ret = TSDB_CODE_SUCCESS;
653,782✔
5605
  int32_t mid;
5606

5607
  if (start >= end) {
653,782✔
5608
    return TSDB_CODE_SUCCESS;
327,385✔
5609
  }
5610

5611
  mid = (start + end) / 2;
326,397✔
5612

5613
  ret = tColDataMergeSort(aColData, start, mid, nColData);
326,397✔
5614
  if (ret != TSDB_CODE_SUCCESS) {
326,397✔
5615
    return ret;
×
5616
  }
5617

5618
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
326,397✔
5619
  if (ret != TSDB_CODE_SUCCESS) {
326,397✔
5620
    return ret;
×
5621
  }
5622

5623
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
326,397✔
5624
}
5625

5626
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
988✔
5627
  int32_t nVal = aColData[0].nVal;
988✔
5628

5629
  if (nVal < 2) return TSDB_CODE_SUCCESS;
988✔
5630

5631
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
988✔
5632
}
5633

5634
static int32_t tColDataMerge(SArray **colArr) {
167✔
5635
  int32_t code = 0;
167✔
5636
  SArray *src = *colArr;
167✔
5637
  SArray *dst = NULL;
167✔
5638

5639
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
167✔
5640
  if (dst == NULL) {
167✔
5641
    return terrno;
×
5642
  }
5643

5644
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
668✔
5645
    SColData *srcCol = taosArrayGet(src, i);
501✔
5646

5647
    SColData *dstCol = taosArrayReserve(dst, 1);
501✔
5648
    if (dstCol == NULL) {
501✔
5649
      code = terrno;
×
5650
      goto _exit;
×
5651
    }
5652
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
501✔
5653
  }
5654

5655
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
167✔
5656
  SRowKey lastKey;
167✔
5657
  for (int32_t i = 0; i < numRows; i++) {
501✔
5658
    SRowKey key;
334✔
5659
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
334✔
5660

5661
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
501✔
5662
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
668✔
5663
        SColData *srcCol = taosArrayGet(src, j);
501✔
5664
        SColData *dstCol = taosArrayGet(dst, j);
501✔
5665

5666
        SColVal cv;
501✔
5667
        code = tColDataGetValue(srcCol, i, &cv);
501✔
5668
        if (code != TSDB_CODE_SUCCESS) {
501✔
5669
          goto _exit;
×
5670
        }
5671
        code = tColDataAppendValue(dstCol, &cv);
501✔
5672
        if (code) {
501✔
5673
          goto _exit;
×
5674
        }
5675
      }
5676
      lastKey = key;
167✔
5677
    } else {  // update existing row
5678
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
668✔
5679
        SColData *srcCol = taosArrayGet(src, j);
501✔
5680
        SColData *dstCol = taosArrayGet(dst, j);
501✔
5681

5682
        SColVal cv;
501✔
5683
        code = tColDataGetValue(srcCol, i, &cv);
501✔
5684
        if (code != TSDB_CODE_SUCCESS) {
501✔
5685
          goto _exit;
×
5686
        }
5687
        code = tColDataUpdateValue(dstCol, &cv, true);
501✔
5688
        if (code) {
501✔
5689
          goto _exit;
×
5690
        }
5691
      }
5692
    }
5693
  }
5694

5695
_exit:
167✔
5696
  if (code) {
167✔
5697
    taosArrayDestroyEx(dst, tColDataDestroy);
×
5698
  } else {
5699
    taosArrayDestroyEx(src, tColDataDestroy);
167✔
5700
    *colArr = dst;
167✔
5701
  }
5702
  return code;
167✔
5703
}
5704

5705
int32_t tColDataSortMerge(SArray **arr) {
1,003,658✔
5706
  SArray   *colDataArr = *arr;
1,003,658✔
5707
  int32_t   nColData = TARRAY_SIZE(colDataArr);
1,004,374✔
5708
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
1,004,431✔
5709

5710
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,004,317✔
5711
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5712
  }
5713
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,004,374✔
5714
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5715
  }
5716
  if (!(aColData[0].flag == HAS_VALUE)) {
1,004,374✔
5717
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5718
  }
5719

5720
  if (aColData[0].nVal <= 1) goto _exit;
1,004,374✔
5721

5722
  int8_t doSort = 0;
974,564✔
5723
  int8_t doMerge = 0;
974,564✔
5724
  // scan -------
5725
  SRowKey lastKey;
872,337✔
5726
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
974,564✔
5727
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
1,121,291,408✔
5728
    SRowKey key;
1,071,961,308✔
5729
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
1,119,710,344✔
5730

5731
    int32_t c = tRowKeyCompare(&lastKey, &key);
1,120,579,569✔
5732
    if (c < 0) {
1,120,579,569✔
5733
      lastKey = key;
1,119,475,451✔
5734
      continue;
1,119,475,451✔
5735
    } else if (c > 0) {
1,104,273✔
5736
      doSort = 1;
988✔
5737
      break;
988✔
5738
    } else {
5739
      doMerge = 1;
1,103,661✔
5740
    }
5741
  }
5742

5743
  // sort -------
5744
  if (doSort) {
1,505,342✔
5745
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
988✔
5746
  }
5747

5748
  if ((doMerge != 1) && (doSort == 1)) {
1,506,020✔
5749
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
988✔
5750
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
327,385✔
5751
      SRowKey key;
326,397✔
5752
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
326,397✔
5753

5754
      int32_t c = tRowKeyCompare(&lastKey, &key);
326,397✔
5755
      if (c == 0) {
326,397✔
5756
        doMerge = 1;
×
5757
        break;
×
5758
      }
5759
      lastKey = key;
326,397✔
5760
    }
5761
  }
5762

5763
  // merge -------
5764
  if (doMerge) {
974,959✔
5765
    int32_t code = tColDataMerge(arr);
167✔
5766
    if (code) return code;
167✔
5767
  }
5768

5769
_exit:
1,002,784✔
5770
  return 0;
1,004,769✔
5771
}
5772

5773
int32_t tColDataSortMergeWithBlob(SArray **arr, SBlobSet *pBlob) {
×
5774
  SArray   *colDataArr = *arr;
×
5775
  int32_t   nColData = TARRAY_SIZE(colDataArr);
×
5776
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
×
5777

5778
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
×
5779
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5780
  }
5781
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
5782
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5783
  }
5784
  if (!(aColData[0].flag == HAS_VALUE)) {
×
5785
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5786
  }
5787

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

5790
  int8_t doSort = 0;
×
5791
  int8_t doMerge = 0;
×
5792
  // scan -------
5793
  SRowKey lastKey;
×
5794
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5795
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5796
    SRowKey key;
×
5797
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5798

5799
    int32_t c = tRowKeyCompare(&lastKey, &key);
×
5800
    if (c < 0) {
×
5801
      lastKey = key;
×
5802
      continue;
×
5803
    } else if (c > 0) {
×
5804
      doSort = 1;
×
5805
      break;
×
5806
    } else {
5807
      doMerge = 1;
×
5808
    }
5809
  }
5810
  if (doMerge || doSort) {
×
5811
    return TSDB_CODE_BLOB_NOT_SUPPORT;
×
5812
  }
5813
  // sort -------
5814
  if (doSort) {
×
5815
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
×
5816
  }
5817

5818
  if ((doMerge != 1) && (doSort == 1)) {
×
5819
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5820
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5821
      SRowKey key;
×
5822
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5823

5824
      int32_t c = tRowKeyCompare(&lastKey, &key);
×
5825
      if (c == 0) {
×
5826
        doMerge = 1;
×
5827
        break;
×
5828
      }
5829
      lastKey = key;
×
5830
    }
5831
  }
5832

5833
  // merge -------
5834
  if (doMerge) {
×
5835
    int32_t code = tColDataMerge(arr);
×
5836
    if (code) return code;
×
5837
  }
5838

5839
_exit:
×
5840
  return 0;
×
5841
}
5842

5843
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
9,846,008✔
5844
  int32_t code = 0;
9,846,008✔
5845

5846
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
19,693,206✔
5847
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
19,693,128✔
5848
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
19,689,803✔
5849
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
19,690,173✔
5850

5851
  // bitmap
5852
  switch (pColData->flag) {
9,846,300✔
5853
    case (HAS_NULL | HAS_NONE):
905,410✔
5854
    case (HAS_VALUE | HAS_NONE):
5855
    case (HAS_VALUE | HAS_NULL):
5856
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
905,410✔
5857
      if (code) return code;
905,410✔
5858
      break;
905,410✔
5859
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5860
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5861
      if (code) return code;
×
5862
      break;
×
5863
    default:
8,940,053✔
5864
      break;
8,940,053✔
5865
  }
5866

5867
  // value
5868
  if (pColData->flag & HAS_VALUE) {
9,845,463✔
5869
    if (IS_VAR_DATA_TYPE(pColData->type)) {
9,782,506✔
5870
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
578,389✔
5871
      if (code) return code;
579,470✔
5872

5873
      code = tEncodeI32v(pEncoder, pColData->nData);
579,470✔
5874
      if (code) return code;
579,470✔
5875

5876
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
579,470✔
5877
      if (code) return code;
579,470✔
5878
    } else {
5879
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
9,204,365✔
5880
      if (code) return code;
9,201,897✔
5881
    }
5882
  }
5883

5884
  return code;
9,845,003✔
5885
}
5886

5887
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
4,694,023✔
5888
  int32_t code = 0;
4,694,023✔
5889

5890
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
9,388,160✔
5891
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
9,388,550✔
5892
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
9,388,544✔
5893
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
9,388,520✔
5894

5895
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
4,694,389✔
5896
    return TSDB_CODE_INVALID_PARA;
×
5897
  }
5898

5899
  // bitmap
5900
  switch (pColData->flag) {
4,693,903✔
5901
    case (HAS_NULL | HAS_NONE):
412,710✔
5902
    case (HAS_VALUE | HAS_NONE):
5903
    case (HAS_VALUE | HAS_NULL):
5904
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
412,710✔
5905
      if (code) return code;
412,710✔
5906
      break;
412,710✔
5907
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5908
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
5909
      if (code) return code;
×
5910
      break;
×
5911
    default:
4,280,731✔
5912
      break;
4,280,731✔
5913
  }
5914

5915
  // value
5916
  if (pColData->flag & HAS_VALUE) {
4,693,441✔
5917
    if (IS_VAR_DATA_TYPE(pColData->type)) {
4,658,814✔
5918
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
231,434✔
5919
      if (code) return code;
231,656✔
5920

5921
      code = tDecodeI32v(pDecoder, &pColData->nData);
231,656✔
5922
      if (code) return code;
231,632✔
5923

5924
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
231,632✔
5925
      if (code) return code;
231,632✔
5926
    } else {
5927
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
4,427,200✔
5928
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
4,426,907✔
5929
      if (code) return code;
4,427,368✔
5930
    }
5931
  }
5932
  pColData->cflag = 0;
4,694,281✔
5933

5934
  return code;
4,694,173✔
5935
}
5936

5937
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
9,846,412✔
5938
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
9,846,412✔
5939
  if (code) return code;
9,845,147✔
5940
  return tEncodeI8(pEncoder, pColData->cflag);
19,692,176✔
5941
}
5942

5943
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
4,694,137✔
5944
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
4,694,137✔
5945
  if (code) return code;
4,694,113✔
5946

5947
  code = tDecodeI8(pDecoder, &pColData->cflag);
4,694,113✔
5948
  return code;
4,694,347✔
5949
}
5950

5951
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
9,844,055✔
5952
  if (version == 0) {
9,844,055✔
5953
    return tEncodeColDataVersion0(pEncoder, pColData);
×
5954
  } else if (version == 1) {
9,844,055✔
5955
    return tEncodeColDataVersion1(pEncoder, pColData);
×
5956
  } else if (version == 2) {
9,844,055✔
5957
    int32_t posStart = pEncoder->pos;
9,844,561✔
5958
    pEncoder->pos += INT_BYTES;
9,845,650✔
5959
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
9,846,922✔
5960
    if (code) return code;
9,846,202✔
5961
    int32_t posEnd = pEncoder->pos;
9,846,202✔
5962
    int32_t pos = posEnd - posStart;
9,847,419✔
5963
    pEncoder->pos = posStart;
9,847,419✔
5964
    code = tEncodeI32(pEncoder, pos);
9,847,288✔
5965
    pEncoder->pos = posEnd;
9,847,288✔
5966
    return code;
9,848,339✔
5967
  } else {
5968
    return TSDB_CODE_INVALID_PARA;
145✔
5969
  }
5970
}
5971

5972
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
7,839,218✔
5973
  if (version == 0) {
7,839,218✔
5974
    return tDecodeColDataVersion0(pDecoder, pColData);
×
5975
  } else if (version == 1) {
7,839,218✔
5976
    return tDecodeColDataVersion1(pDecoder, pColData);
×
5977
  } else if (version == 2) {
7,839,218✔
5978
    if (jump) {
7,839,326✔
5979
      int32_t len = 0;
3,145,380✔
5980
      int32_t code = tDecodeI32(pDecoder, &len);
3,145,380✔
5981
      if (code) return code;
3,145,380✔
5982
      pDecoder->pos += (len - INT_BYTES);
3,145,380✔
5983
      return TSDB_CODE_SUCCESS;
3,145,380✔
5984
    } else {
5985
      pDecoder->pos += INT_BYTES;
4,693,946✔
5986
      return tDecodeColDataVersion1(pDecoder, pColData);
4,694,180✔
5987
    }    
5988
  } else {
UNCOV
5989
    return TSDB_CODE_INVALID_PARA;
×
5990
  }
5991
}
5992

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

5995
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
2,147,483,647✔
5996
  if (ppRow == NULL) {
2,147,483,647✔
5997
    return TSDB_CODE_INVALID_PARA;
×
5998
  }
5999

6000
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
2,147,483,647✔
6001
    return TSDB_CODE_OUT_OF_RANGE;
×
6002
  }
6003

6004
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
2,147,483,647✔
6005
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
2,147,483,647✔
6006
}
6007

6008
int32_t tEncodeBlobSet(SEncoder *pEncoder, SBlobSet *pRow) {
45,432✔
6009
  int32_t code = 0;
45,432✔
6010
  int32_t lino = 0;
45,432✔
6011
  uint8_t compressed = 0;
45,432✔
6012
  int32_t compressSize = 0;
45,432✔
6013
  char   *p = NULL;
45,432✔
6014

6015
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
90,864✔
6016
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
90,864✔
6017

6018
  int32_t nSeq = taosArrayGetSize(pRow->pSeqTable);
45,432✔
6019
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nSeq));
45,432✔
6020

6021
  // if (pRow->type) {
6022
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
45,432✔
6023
  while (pIter) {
52,345,638✔
6024
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
52,300,206✔
6025
    int32_t  idx = *(int32_t *)pIter;
52,300,206✔
6026
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
52,300,206✔
6027
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
52,300,206✔
6028

6029
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
52,300,206✔
6030
  }
6031
  //}
6032
  for (int32_t i = 0; i < nSeq; i++) {
52,345,638✔
6033
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
52,300,206✔
6034
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
104,600,412✔
6035
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
104,600,412✔
6036
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
104,600,412✔
6037
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
104,600,412✔
6038
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
104,600,412✔
6039
  }
6040

6041
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
90,864✔
6042

6043
_exit:
45,432✔
6044
  return code;
45,432✔
6045
}
6046

6047
int32_t tDecodeBlobSet(SDecoder *pDecoder, SBlobSet **pBlobSet) {
22,716✔
6048
  int32_t    code = 0;
22,716✔
6049
  int32_t    lino = 0;
22,716✔
6050
  int32_t    nSeq = 0;
22,716✔
6051
  uint8_t    compressd = 0;
22,716✔
6052
  int32_t    compressSize = 0;
22,716✔
6053
  SBlobSet  *pBlob = taosMemCalloc(1, sizeof(SBlobSet));
22,716✔
6054
  if (pBlob == NULL) {
22,716✔
6055
    TAOS_CHECK_EXIT(terrno);
×
6056
  }
6057
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pBlob->type));
45,432✔
6058
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pBlob->len));
45,432✔
6059

6060
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
22,716✔
6061

6062
  // if (pBlob->type) {
6063
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
22,716✔
6064
  if (pBlob->pSeqToffset == NULL) {
22,716✔
6065
    TAOS_CHECK_EXIT(terrno);
×
6066
  }
6067
  for (int32_t i = 0; i < nSeq; i++) {
26,172,819✔
6068
    uint64_t seq;
26,150,103✔
6069
    int32_t  idx;
26,150,103✔
6070
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
26,150,103✔
6071
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
26,150,103✔
6072

6073
    code = taosHashPut(pBlob->pSeqToffset, &seq, sizeof(seq), &idx, sizeof(idx));
26,150,103✔
6074
    TAOS_CHECK_EXIT(code);
26,150,103✔
6075
  }
6076
  //}
6077

6078
  pBlob->pSeqTable = taosArrayInit(nSeq, sizeof(SBlobValue));
22,716✔
6079
  if (pBlob->pSeqTable == NULL) {
22,716✔
6080
    TAOS_CHECK_EXIT(terrno);
×
6081
  }
6082

6083
  for (int32_t i = 0; i < nSeq; i++) {
26,172,819✔
6084
    SBlobValue value;
26,150,103✔
6085
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
26,150,103✔
6086
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
26,150,103✔
6087
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
26,150,103✔
6088
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
26,150,103✔
6089
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
26,150,103✔
6090
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
52,300,206✔
6091
      TAOS_CHECK_EXIT(terrno);
×
6092
    }
6093
  }
6094

6095
  pBlob->data = taosMemCalloc(1, pBlob->len);
22,716✔
6096
  if (pBlob->data == NULL) {
22,716✔
6097
    TAOS_CHECK_EXIT(terrno);
×
6098
  }
6099

6100
  TAOS_CHECK_EXIT(tDecodeFixed(pDecoder, pBlob->data, pBlob->len));
22,716✔
6101
  *pBlobSet = pBlob;
22,716✔
6102

6103
  uTrace("decode blob len:%d", (int32_t)(pBlob->len));
22,716✔
6104

6105
_exit:
22,716✔
6106
  if (code != 0) {
22,716✔
6107
    if (pBlob != NULL) {
×
6108
      taosMemoryFree(pBlob->data);
×
6109
      taosArrayDestroy(pBlob->pSeqTable);
×
6110
      taosMemoryFree(pBlob);
×
6111
    }
6112
  }
6113
  return code;
22,716✔
6114
}
6115

6116
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
6117
  do {                                       \
6118
    (SUM) += (VAL);                          \
6119
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
6120
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
6121
  } while (0)
6122

6123
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
2,790,590✔
6124
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,790,590✔
6125
  int16_t *numOfNull = &pAggs->numOfNull;
2,790,590✔
6126
  *sum = 0;
2,790,590✔
6127
  *max = 0;
2,790,590✔
6128
  *min = 1;
2,790,590✔
6129
  *numOfNull = 0;
2,790,590✔
6130

6131
  int8_t val;
6132
  if (HAS_VALUE == pColData->flag) {
2,790,590✔
6133
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6134
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,147,483,647✔
6135
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6136
    }
6137
  } else {
6138
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6139
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6140
        case 0:
6,329,147✔
6141
        case 1:
6142
          (*numOfNull)++;
6,329,147✔
6143
          break;
6,329,147✔
6144
        case 2:
119,070,853✔
6145
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
119,070,853✔
6146
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,070,853✔
6147
          break;
119,070,853✔
6148
        default:
×
6149
          break;
×
6150
      }
6151
    }
6152
  }
6153
}
2,769,231✔
6154

6155
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,809,974✔
6156
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,809,974✔
6157
  int16_t *numOfNull = &pAggs->numOfNull;
2,809,974✔
6158
  *sum = 0;
2,809,974✔
6159
  *max = INT8_MIN;
2,809,974✔
6160
  *min = INT8_MAX;
2,809,974✔
6161
  *numOfNull = 0;
2,809,974✔
6162

6163
  int8_t val;
6164
  if (HAS_VALUE == pColData->flag) {
2,809,974✔
6165
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6166
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6167
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6168
    }
6169
  } else {
6170
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6171
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6172
        case 0:
6,290,691✔
6173
        case 1:
6174
          (*numOfNull)++;
6,290,691✔
6175
          break;
6,290,691✔
6176
        case 2:
119,109,309✔
6177
          val = ((int8_t *)pColData->pData)[iVal];
119,109,309✔
6178
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,109,309✔
6179
          break;
119,109,309✔
6180
        default:
×
6181
          break;
×
6182
      }
6183
    }
6184
  }
6185
}
2,806,579✔
6186

6187
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,795,760✔
6188
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,795,760✔
6189
  int16_t *numOfNull = &pAggs->numOfNull;
2,795,760✔
6190
  *sum = 0;
2,795,760✔
6191
  *max = INT16_MIN;
2,795,760✔
6192
  *min = INT16_MAX;
2,795,760✔
6193
  *numOfNull = 0;
2,795,760✔
6194

6195
  int16_t val;
6196
  if (HAS_VALUE == pColData->flag) {
2,795,760✔
6197
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6198
      val = ((int16_t *)pColData->pData)[iVal];
2,147,483,647✔
6199
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6200
    }
6201
  } else {
6202
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6203
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6204
        case 0:
6,272,299✔
6205
        case 1:
6206
          (*numOfNull)++;
6,272,299✔
6207
          break;
6,272,299✔
6208
        case 2:
119,127,701✔
6209
          val = ((int16_t *)pColData->pData)[iVal];
119,127,701✔
6210
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,127,701✔
6211
          break;
119,127,701✔
6212
        default:
×
6213
          break;
×
6214
      }
6215
    }
6216
  }
6217
}
2,794,062✔
6218

6219
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
18,966,459✔
6220
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
18,966,459✔
6221
  int16_t *numOfNull = &pAggs->numOfNull;
18,966,328✔
6222
  *sum = 0;
18,965,814✔
6223
  *max = INT32_MIN;
18,966,029✔
6224
  *min = INT32_MAX;
18,966,113✔
6225
  *numOfNull = 0;
18,965,725✔
6226

6227
  int32_t val;
6228
  if (HAS_VALUE == pColData->flag) {
18,965,725✔
6229
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6230
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6231
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6232
    }
6233
  } else {
6234
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,143,293,952✔
6235
      switch (tColDataGetBitValue(pColData, iVal)) {
2,142,724,380✔
6236
        case 0:
214,192,895✔
6237
        case 1:
6238
          (*numOfNull)++;
214,192,895✔
6239
          break;
214,192,895✔
6240
        case 2:
1,928,531,485✔
6241
          val = ((int32_t *)pColData->pData)[iVal];
1,928,531,485✔
6242
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
1,928,531,485✔
6243
          break;
1,928,531,485✔
6244
        default:
×
6245
          break;
×
6246
      }
6247
    }
6248
  }
6249
}
16,838,787✔
6250

6251
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
74,255,967✔
6252
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
74,255,967✔
6253
  int16_t *numOfNull = &pAggs->numOfNull;
74,257,837✔
6254
  *sum = 0;
74,257,433✔
6255
  *max = INT64_MIN;
74,257,433✔
6256
  *min = INT64_MAX;
74,255,917✔
6257
  *numOfNull = 0;
74,256,675✔
6258

6259
  int64_t val;
6260
  if (HAS_VALUE == pColData->flag) {
74,256,321✔
6261
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6262
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6263
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6264
    }
6265
  } else {
6266
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6267
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6268
        case 0:
248,550,156✔
6269
        case 1:
6270
          (*numOfNull)++;
248,550,156✔
6271
          break;
248,545,406✔
6272
        case 2:
2,052,312,932✔
6273
          val = ((int64_t *)pColData->pData)[iVal];
2,052,312,932✔
6274
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,052,317,557✔
6275
          break;
2,052,321,682✔
6276
        default:
×
6277
          break;
×
6278
      }
6279
    }
6280
  }
6281
}
86,833,763✔
6282

6283
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
32,341,549✔
6284
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
32,341,549✔
6285
  int16_t *numOfNull = &pAggs->numOfNull;
32,341,549✔
6286
  *(double *)sum = 0;
32,341,376✔
6287
  *(double *)max = -FLT_MAX;
32,341,722✔
6288
  *(double *)min = FLT_MAX;
32,341,549✔
6289
  *numOfNull = 0;
32,341,549✔
6290

6291
  float val;
6292
  if (HAS_VALUE == pColData->flag) {
32,341,722✔
6293
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6294
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6295
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6296
    }
6297
  } else {
6298
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
134,546,332✔
6299
      switch (tColDataGetBitValue(pColData, iVal)) {
134,500,980✔
6300
        case 0:
7,149,579✔
6301
        case 1:
6302
          (*numOfNull)++;
7,149,579✔
6303
          break;
7,149,579✔
6304
        case 2:
127,351,401✔
6305
          val = ((float *)pColData->pData)[iVal];
127,351,401✔
6306
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
127,351,401✔
6307
          break;
127,351,401✔
6308
        default:
×
6309
          break;
×
6310
      }
6311
    }
6312
  }
6313
}
33,838,944✔
6314

6315
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
3,258,693✔
6316
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
3,258,693✔
6317
  int16_t *numOfNull = &pAggs->numOfNull;
3,258,987✔
6318
  *(double *)sum = 0;
3,258,861✔
6319
  *(double *)max = -DBL_MAX;
3,258,945✔
6320
  *(double *)min = DBL_MAX;
3,258,861✔
6321
  *numOfNull = 0;
3,258,819✔
6322

6323
  double val;
6324
  if (HAS_VALUE == pColData->flag) {
3,258,819✔
6325
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6326
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6327
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6328
    }
6329
  } else {
6330
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6331
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6332
        case 0:
6,238,232✔
6333
        case 1:
6334
          (*numOfNull)++;
6,238,232✔
6335
          break;
6,238,232✔
6336
        case 2:
119,161,768✔
6337
          val = ((double *)pColData->pData)[iVal];
119,161,768✔
6338
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
119,161,768✔
6339
          break;
119,161,768✔
6340
        default:
×
6341
          break;
×
6342
      }
6343
    }
6344
  }
6345
}
3,298,371✔
6346

6347
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,675,092✔
6348
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,675,092✔
6349
  int16_t *numOfNull = &pAggs->numOfNull;
2,675,092✔
6350
  *(uint64_t *)sum = 0;
2,675,092✔
6351
  *(uint64_t *)max = 0;
2,675,092✔
6352
  *(uint64_t *)min = UINT8_MAX;
2,675,092✔
6353
  *numOfNull = 0;
2,675,092✔
6354

6355
  uint8_t val;
6356
  if (HAS_VALUE == pColData->flag) {
2,675,092✔
6357
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6358
      val = ((uint8_t *)pColData->pData)[iVal];
2,147,483,647✔
6359
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6360
    }
6361
  } else {
6362
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6363
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6364
        case 0:
6,203,747✔
6365
        case 1:
6366
          (*numOfNull)++;
6,203,747✔
6367
          break;
6,203,747✔
6368
        case 2:
119,196,253✔
6369
          val = ((uint8_t *)pColData->pData)[iVal];
119,196,253✔
6370
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,196,253✔
6371
          break;
119,196,253✔
6372
        default:
×
6373
          break;
×
6374
      }
6375
    }
6376
  }
6377
}
2,675,000✔
6378

6379
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,675,092✔
6380
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,675,092✔
6381
  int16_t *numOfNull = &pAggs->numOfNull;
2,675,092✔
6382
  *(uint64_t *)sum = 0;
2,675,092✔
6383
  *(uint64_t *)max = 0;
2,675,092✔
6384
  *(uint64_t *)min = UINT16_MAX;
2,675,092✔
6385
  *numOfNull = 0;
2,675,092✔
6386

6387
  uint16_t val;
6388
  if (HAS_VALUE == pColData->flag) {
2,675,092✔
6389
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6390
      val = ((uint16_t *)pColData->pData)[iVal];
2,147,483,647✔
6391
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6392
    }
6393
  } else {
6394
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6395
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6396
        case 0:
6,354,645✔
6397
        case 1:
6398
          (*numOfNull)++;
6,354,645✔
6399
          break;
6,354,645✔
6400
        case 2:
119,045,355✔
6401
          val = ((uint16_t *)pColData->pData)[iVal];
119,045,355✔
6402
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,045,355✔
6403
          break;
119,045,355✔
6404
        default:
×
6405
          break;
×
6406
      }
6407
    }
6408
  }
6409
}
2,673,209✔
6410

6411
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,675,092✔
6412
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,675,092✔
6413
  int16_t *numOfNull = &pAggs->numOfNull;
2,675,092✔
6414
  *(uint64_t *)sum = 0;
2,675,092✔
6415
  *(uint64_t *)max = 0;
2,675,092✔
6416
  *(uint64_t *)min = UINT32_MAX;
2,675,092✔
6417
  *numOfNull = 0;
2,675,092✔
6418

6419
  uint32_t val;
6420
  if (HAS_VALUE == pColData->flag) {
2,675,092✔
6421
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6422
      val = ((uint32_t *)pColData->pData)[iVal];
2,147,483,647✔
6423
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6424
    }
6425
  } else {
6426
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6427
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6428
        case 0:
6,327,684✔
6429
        case 1:
6430
          (*numOfNull)++;
6,327,684✔
6431
          break;
6,327,684✔
6432
        case 2:
119,072,316✔
6433
          val = ((uint32_t *)pColData->pData)[iVal];
119,072,316✔
6434
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,072,316✔
6435
          break;
119,072,316✔
6436
        default:
×
6437
          break;
×
6438
      }
6439
    }
6440
  }
6441
}
2,674,361✔
6442

6443
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,675,092✔
6444
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,675,092✔
6445
  int16_t *numOfNull = &pAggs->numOfNull;
2,675,092✔
6446
  *(uint64_t *)sum = 0;
2,675,092✔
6447
  *(uint64_t *)max = 0;
2,675,092✔
6448
  *(uint64_t *)min = UINT64_MAX;
2,675,092✔
6449
  *numOfNull = 0;
2,675,092✔
6450

6451
  uint64_t val;
6452
  if (HAS_VALUE == pColData->flag) {
2,675,092✔
6453
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6454
      val = ((uint64_t *)pColData->pData)[iVal];
2,147,483,647✔
6455
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6456
    }
6457
  } else {
6458
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6459
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6460
        case 0:
6,288,810✔
6461
        case 1:
6462
          (*numOfNull)++;
6,288,810✔
6463
          break;
6,288,810✔
6464
        case 2:
119,111,190✔
6465
          val = ((uint64_t *)pColData->pData)[iVal];
119,111,190✔
6466
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,111,190✔
6467
          break;
119,111,190✔
6468
        default:
×
6469
          break;
×
6470
      }
6471
    }
6472
  }
6473
}
2,664,322✔
6474

6475
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
7,237,197✔
6476
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,237,197✔
6477
  int16_t *numOfNull = &pAggs->numOfNull;
7,237,197✔
6478
  *(uint64_t *)sum = 0;
7,237,197✔
6479
  *(uint64_t *)max = 0;
7,237,197✔
6480
  *(uint64_t *)min = 0;
7,236,646✔
6481
  *numOfNull = 0;
7,237,197✔
6482

6483
  switch (pColData->flag) {
7,237,197✔
6484
    case HAS_NONE:
×
6485
    case HAS_NULL:
6486
    case (HAS_NONE | HAS_NULL):
6487
      *numOfNull = pColData->nVal;
×
6488
      break;
×
6489
    case HAS_VALUE:
7,171,458✔
6490
      *numOfNull = 0;
7,171,458✔
6491
      break;
7,172,009✔
6492
    case (HAS_VALUE | HAS_NULL):
65,188✔
6493
    case (HAS_VALUE | HAS_NONE):
6494
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
258,329,188✔
6495
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
258,264,000✔
6496
          (*numOfNull)++;
12,665,872✔
6497
        }
6498
      }
6499
      break;
65,188✔
6500
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6501
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6502
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6503
          (*numOfNull)++;
×
6504
        }
6505
      }
6506
      break;
×
6507
    default:
×
6508
      break;
×
6509
  }
6510
}
7,237,197✔
6511

6512
#define CALC_DECIMAL_SUM_MAX_MIN(TYPE, pSumOp, pCompOp, pColData, pSum, pMax, pMin)                   \
6513
  do {                                                                                                \
6514
    if (decimal128AddCheckOverflow((Decimal *)pSum, pVal, DECIMAL_WORD_NUM(TYPE))) *pOverflow = true; \
6515
    pSumOp->add(pSum, pVal, DECIMAL_WORD_NUM(TYPE));                                                  \
6516
    if (pCompOp->gt(pVal, pMax, DECIMAL_WORD_NUM(TYPE))) {                                            \
6517
      *(pMax) = *pVal;                                                                                \
6518
    }                                                                                                 \
6519
    if (pCompOp->lt(pVal, pMin, DECIMAL_WORD_NUM(TYPE))) {                                            \
6520
      *(pMin) = *pVal;                                                                                \
6521
    }                                                                                                 \
6522
  } while (0)
6523

6524
static FORCE_INLINE void tColDataCalcSMADecimal64Type(SColData *pColData, SColumnDataAgg *pAggs) {
18,516✔
6525
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum;
18,516✔
6526
  Decimal64  *pMax = (Decimal64 *)pAggs->decimal128Max, *pMin = (Decimal64 *)pAggs->decimal128Min;
18,516✔
6527
  uint8_t    *pOverflow = &pAggs->overflow;
18,516✔
6528
  *pSum = DECIMAL128_ZERO;
18,516✔
6529
  *pMax = DECIMAL64_MIN;
18,516✔
6530
  *pMin = DECIMAL64_MAX;
18,516✔
6531
  pAggs->numOfNull = 0;
18,516✔
6532
  pAggs->colId |= DECIMAL_AGG_FLAG;
18,516✔
6533

6534
  Decimal64         *pVal = NULL;
18,516✔
6535
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
18,516✔
6536
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
18,516✔
6537
  if (HAS_VALUE == pColData->flag) {
18,516✔
UNCOV
6538
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
UNCOV
6539
      pVal = ((Decimal64 *)pColData->pData) + iVal;
×
UNCOV
6540
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
×
6541
    }
6542
  } else {
6543
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
13,689,816✔
6544
      switch (tColDataGetBitValue(pColData, iVal)) {
13,671,300✔
6545
        case 0:
202,931✔
6546
        case 1:
6547
          pAggs->numOfNull++;
202,931✔
6548
          break;
202,931✔
6549
        case 2:
13,468,369✔
6550
          pVal = ((Decimal64 *)pColData->pData) + iVal;
13,468,369✔
6551
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
13,468,369✔
6552
          break;
13,468,369✔
6553
        default:
×
6554
          break;
×
6555
      }
6556
    }
6557
  }
6558
}
18,516✔
6559

6560
static FORCE_INLINE void tColDataCalcSMADecimal128Type(SColData *pColData, SColumnDataAgg *pAggs) {
49,436✔
6561
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum, *pMax = (Decimal128 *)pAggs->decimal128Max,
49,436✔
6562
             *pMin = (Decimal128 *)pAggs->decimal128Min;
49,436✔
6563
  uint8_t *pOverflow = &pAggs->overflow;
49,436✔
6564
  *pSum = DECIMAL128_ZERO;
49,436✔
6565
  *pMax = DECIMAL128_MIN;
49,436✔
6566
  *pMin = DECIMAL128_MAX;
49,436✔
6567
  pAggs->numOfNull = 0;
49,436✔
6568
  pAggs->colId |= DECIMAL_AGG_FLAG;
49,436✔
6569

6570
  Decimal128        *pVal = NULL;
49,436✔
6571
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
49,436✔
6572
  if (HAS_VALUE == pColData->flag) {
49,436✔
6573
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
155,338✔
6574
      pVal = ((Decimal128 *)pColData->pData) + iVal;
153,800✔
6575
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
153,800✔
6576
    }
6577
  } else {
6578
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
44,485,398✔
6579
      switch (tColDataGetBitValue(pColData, iVal)) {
44,437,500✔
6580
        case 0:
733,693✔
6581
        case 1:
6582
          pAggs->numOfNull++;
733,693✔
6583
          break;
733,693✔
6584
        case 2:
43,703,807✔
6585
          pVal = ((Decimal128 *)pColData->pData) + iVal;
43,703,807✔
6586
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
43,703,807✔
6587
          break;
43,703,807✔
6588
        default:
×
6589
          break;
×
6590
      }
6591
    }
6592
  }
6593
}
49,436✔
6594

6595
void (*tColDataCalcSMA[])(SColData *pColData, SColumnDataAgg *pAggs) = {
6596
    NULL,
6597
    tColDataCalcSMABool,            // TSDB_DATA_TYPE_BOOL
6598
    tColDataCalcSMATinyInt,         // TSDB_DATA_TYPE_TINYINT
6599
    tColDataCalcSMATinySmallInt,    // TSDB_DATA_TYPE_SMALLINT
6600
    tColDataCalcSMAInt,             // TSDB_DATA_TYPE_INT
6601
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_BIGINT
6602
    tColDataCalcSMAFloat,           // TSDB_DATA_TYPE_FLOAT
6603
    tColDataCalcSMADouble,          // TSDB_DATA_TYPE_DOUBLE
6604
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARCHAR
6605
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_TIMESTAMP
6606
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_NCHAR
6607
    tColDataCalcSMAUTinyInt,        // TSDB_DATA_TYPE_UTINYINT
6608
    tColDataCalcSMATinyUSmallInt,   // TSDB_DATA_TYPE_USMALLINT
6609
    tColDataCalcSMAUInt,            // TSDB_DATA_TYPE_UINT
6610
    tColDataCalcSMAUBigInt,         // TSDB_DATA_TYPE_UBIGINT
6611
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_JSON
6612
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARBINARY
6613
    tColDataCalcSMADecimal128Type,  // TSDB_DATA_TYPE_DECIMAL
6614
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_BLOB
6615
    NULL,                           // TSDB_DATA_TYPE_MEDIUMBLOB
6616
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_GEOMETRY
6617
    tColDataCalcSMADecimal64Type,   // TSDB_DATA_TYPE_DECIMAL64
6618
};
6619

6620
// SValueColumn ================================
6621
int32_t tValueColumnInit(SValueColumn *valCol) {
622,679,371✔
6622
  valCol->type = TSDB_DATA_TYPE_NULL;
622,679,371✔
6623
  valCol->numOfValues = 0;
622,719,756✔
6624
  tBufferInit(&valCol->data);
622,732,055✔
6625
  tBufferInit(&valCol->offsets);
622,709,696✔
6626
  return 0;
622,799,078✔
6627
}
6628

6629
void tValueColumnDestroy(SValueColumn *valCol) {
1,495,905,219✔
6630
  valCol->type = TSDB_DATA_TYPE_NULL;
1,495,905,219✔
6631
  valCol->numOfValues = 0;
1,495,995,910✔
6632
  tBufferDestroy(&valCol->data);
1,496,020,455✔
6633
  tBufferDestroy(&valCol->offsets);
1,495,917,799✔
6634
  return;
1,495,839,279✔
6635
}
6636

6637
void tValueColumnClear(SValueColumn *valCol) {
802,907,571✔
6638
  valCol->type = TSDB_DATA_TYPE_NULL;
802,907,571✔
6639
  valCol->numOfValues = 0;
803,014,712✔
6640
  tBufferClear(&valCol->data);
803,044,337✔
6641
  tBufferClear(&valCol->offsets);
803,053,976✔
6642
  return;
803,051,395✔
6643
}
6644

6645
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
1,069,639✔
6646
  int32_t code;
6647

6648
  if (valCol->numOfValues == 0) {
1,069,639✔
6649
    valCol->type = value->type;
567,274✔
6650
  }
6651

6652
  if (!(value->type == valCol->type)) {
1,069,639✔
6653
    return TSDB_CODE_INVALID_PARA;
×
6654
  }
6655

6656
  if (IS_VAR_DATA_TYPE(value->type)) {
1,069,795✔
6657
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
782,400✔
6658
      return code;
×
6659
    }
6660
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
782,400✔
6661
      return code;
×
6662
    }
6663
  } else {
6664
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
677,924✔
6665
    if (code) return code;
677,924✔
6666
  }
6667
  valCol->numOfValues++;
1,069,124✔
6668

6669
  return 0;
1,070,466✔
6670
}
6671

6672
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
40,655,141✔
6673
  int32_t code;
6674

6675
  if (idx < 0 || idx >= valCol->numOfValues) {
40,655,141✔
6676
    return TSDB_CODE_OUT_OF_RANGE;
×
6677
  }
6678

6679
  if (IS_VAR_DATA_TYPE(valCol->type)) {
40,655,324✔
6680
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
871,896✔
6681
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
874,151✔
6682
    int32_t  oldDataSize = nextOffset - offsets[idx];
874,151✔
6683
    int32_t  bytesAdded = value->nData - oldDataSize;
874,151✔
6684

6685
    if (bytesAdded != 0) {
874,151✔
6686
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
36,860✔
6687
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
18,430✔
6688
              tBufferGetSize(&valCol->data) - nextOffset);
18,430✔
6689
      valCol->data.size += bytesAdded;
18,430✔
6690

6691
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
18,430✔
6692
        offsets[i] += bytesAdded;
×
6693
      }
6694
    }
6695
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
874,151✔
6696
  } else {
6697
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
39,785,393✔
6698
                        tDataTypes[valCol->type].bytes);
39,784,357✔
6699
  }
6700
  return 0;
6701
}
6702

6703
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
119,577,627✔
6704
  if (idx < 0 || idx >= valCol->numOfValues) {
119,577,627✔
6705
    return TSDB_CODE_OUT_OF_RANGE;
×
6706
  }
6707

6708
  value->type = valCol->type;
119,598,197✔
6709
  if (IS_VAR_DATA_TYPE(value->type)) {
134,682,347✔
6710
    int32_t       offset, nextOffset;
15,089,065✔
6711
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
15,081,112✔
6712

6713
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
15,078,990✔
6714
    if (idx == valCol->numOfValues - 1) {
15,080,753✔
6715
      nextOffset = tBufferGetSize(&valCol->data);
9,443,347✔
6716
    } else {
6717
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
5,637,406✔
6718
    }
6719
    value->nData = nextOffset - offset;
15,081,892✔
6720
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
15,079,926✔
6721
  } else {
6722
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
104,521,194✔
6723
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
209,060,237✔
6724
  }
6725
  return 0;
119,611,207✔
6726
}
6727

6728
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
568,101✔
6729
  int32_t code;
6730

6731
  if (!(valCol->numOfValues > 0)) {
568,101✔
6732
    return TSDB_CODE_INVALID_PARA;
×
6733
  }
6734

6735
  (*info) = (SValueColumnCompressInfo){
568,101✔
6736
      .cmprAlg = info->cmprAlg,
568,101✔
6737
      .type = valCol->type,
568,101✔
6738
  };
6739

6740
  // offset
6741
  if (IS_VAR_DATA_TYPE(valCol->type)) {
568,101✔
6742
    SCompressInfo cinfo = {
199,490✔
6743
        .cmprAlg = info->cmprAlg,
199,490✔
6744
        .dataType = TSDB_DATA_TYPE_INT,
6745
        .originalSize = valCol->offsets.size,
199,490✔
6746
    };
6747

6748
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
199,490✔
6749
    if (code) return code;
199,490✔
6750

6751
    info->offsetOriginalSize = cinfo.originalSize;
199,490✔
6752
    info->offsetCompressedSize = cinfo.compressedSize;
199,490✔
6753
  }
6754

6755
  // data
6756
  SCompressInfo cinfo = {
568,772✔
6757
      .cmprAlg = info->cmprAlg,
1,136,873✔
6758
      .dataType = valCol->type,
568,772✔
6759
      .originalSize = valCol->data.size,
568,101✔
6760
  };
6761

6762
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
568,772✔
6763
  if (code) return code;
568,772✔
6764

6765
  info->dataOriginalSize = cinfo.originalSize;
568,772✔
6766
  info->dataCompressedSize = cinfo.compressedSize;
568,772✔
6767

6768
  return 0;
568,772✔
6769
}
6770

6771
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
32,719,113✔
6772
                               SBuffer *assist) {
6773
  int32_t code;
6774

6775
  tValueColumnClear(valCol);
32,719,113✔
6776
  valCol->type = info->type;
32,731,719✔
6777
  // offset
6778
  if (IS_VAR_DATA_TYPE(valCol->type)) {
42,736,466✔
6779
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
10,006,869✔
6780

6781
    SCompressInfo cinfo = {
10,007,478✔
6782
        .dataType = TSDB_DATA_TYPE_INT,
6783
        .cmprAlg = info->cmprAlg,
10,007,634✔
6784
        .originalSize = info->offsetOriginalSize,
10,006,651✔
6785
        .compressedSize = info->offsetCompressedSize,
10,007,010✔
6786
    };
6787

6788
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
10,007,322✔
6789
    if (code) {
10,008,102✔
6790
      return code;
×
6791
    }
6792
  } else {
6793
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
22,720,262✔
6794
  }
6795

6796
  // data
6797
  SCompressInfo cinfo = {
32,732,967✔
6798
      .dataType = valCol->type,
32,733,794✔
6799
      .cmprAlg = info->cmprAlg,
32,734,777✔
6800
      .originalSize = info->dataOriginalSize,
32,733,217✔
6801
      .compressedSize = info->dataCompressedSize,
32,732,343✔
6802
  };
6803

6804
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
32,732,764✔
6805
  if (code) {
32,735,557✔
6806
    return code;
×
6807
  }
6808

6809
  return 0;
32,735,557✔
6810
}
6811

6812
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
568,772✔
6813
  int32_t code;
6814
  uint8_t fmtVer = 0;
568,772✔
6815

6816
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,137,544✔
6817
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,137,544✔
6818
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,136,873✔
6819
  if (IS_VAR_DATA_TYPE(info->type)) {
568,101✔
6820
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
398,309✔
6821
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
398,980✔
6822
  }
6823
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,136,873✔
6824
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,137,544✔
6825

6826
  return 0;
568,772✔
6827
}
6828

6829
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
32,730,751✔
6830
  int32_t code;
6831
  uint8_t fmtVer;
32,730,751✔
6832

6833
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
32,727,210✔
6834
  if (fmtVer == 0) {
32,733,279✔
6835
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
32,733,279✔
6836
    if ((code = tBufferGetI8(reader, &info->type))) return code;
32,731,672✔
6837
    if (IS_VAR_DATA_TYPE(info->type)) {
32,735,198✔
6838
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
10,008,976✔
6839
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
10,005,933✔
6840
    } else {
6841
      info->offsetOriginalSize = 0;
22,724,397✔
6842
      info->offsetCompressedSize = 0;
22,726,363✔
6843
    }
6844
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
32,732,967✔
6845
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
32,732,296✔
6846
  } else {
6847
    return TSDB_CODE_INVALID_PARA;
×
6848
  }
6849

6850
  return 0;
32,736,540✔
6851
}
6852

6853
int32_t tCompressData(void          *input,       // input
748,388,321✔
6854
                      SCompressInfo *info,        // compress info
6855
                      void          *output,      // output
6856
                      int32_t        outputSize,  // output size
6857
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6858
) {
6859
  int32_t extraSizeNeeded;
6860
  int32_t code;
6861

6862
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
748,388,321✔
6863
  if (!(outputSize >= extraSizeNeeded)) {
748,454,255✔
6864
    return TSDB_CODE_INVALID_PARA;
×
6865
  }
6866

6867
  if (info->cmprAlg == NO_COMPRESSION) {
748,454,255✔
6868
    (void)memcpy(output, input, info->originalSize);
97,268✔
6869
    info->compressedSize = info->originalSize;
97,268✔
6870
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
853,644,066✔
6871
    SBuffer local;
105,254,729✔
6872

6873
    tBufferInit(&local);
6874
    if (buffer == NULL) {
105,287,702✔
6875
      buffer = &local;
×
6876
    }
6877

6878
    if (info->cmprAlg == TWO_STAGE_COMP) {
105,287,702✔
6879
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
105,168,940✔
6880
      if (code) {
105,160,183✔
6881
        tBufferDestroy(&local);
6882
        return code;
×
6883
      }
6884
    }
6885

6886
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
315,827,490✔
6887
        input,                                                   // input
6888
        info->originalSize,                                      // input size
6889
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
105,289,385✔
6890
        output,                                                  // output
6891
        outputSize,                                              // output size
6892
        info->cmprAlg,                                           // compression algorithm
105,286,218✔
6893
        buffer->data,                                            // buffer
6894
        buffer->capacity                                         // buffer size
105,284,096✔
6895
    );
6896
    if (info->compressedSize < 0) {
105,291,718✔
6897
      tBufferDestroy(&local);
6898
      return TSDB_CODE_COMPRESS_ERROR;
×
6899
    }
6900

6901
    tBufferDestroy(&local);
6902
  } else {
6903
    DEFINE_VAR(info->cmprAlg)
643,084,896✔
6904
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
643,096,810✔
6905
      (void)memcpy(output, input, info->originalSize);
7,248✔
6906
      info->compressedSize = info->originalSize;
7,248✔
6907
      return 0;
7,248✔
6908
    }
6909
    SBuffer local;
642,964,894✔
6910

6911
    tBufferInit(&local);
6912
    if (buffer == NULL) {
643,096,633✔
6913
      buffer = &local;
×
6914
    }
6915
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
643,096,633✔
6916

6917
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
1,929,043,136✔
6918
        input,                                                      // input
6919
        info->originalSize,                                         // input size
6920
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
643,092,302✔
6921
        output,                                                     // output
6922
        outputSize,                                                 // output size
6923
        info->cmprAlg,                                              // compression algorithm
6924
        buffer->data,                                               // buffer
6925
        buffer->capacity                                            // buffer size
643,068,809✔
6926
    );
6927
    if (info->compressedSize < 0) {
643,093,144✔
6928
      tBufferDestroy(&local);
6929
      return TSDB_CODE_COMPRESS_ERROR;
×
6930
    }
6931

6932
    tBufferDestroy(&local);
6933
    // new col compress
6934
  }
6935

6936
  return 0;
748,436,689✔
6937
}
6938

6939
int32_t tDecompressData(void                *input,       // input
2,117,427,821✔
6940
                        const SCompressInfo *info,        // compress info
6941
                        void                *output,      // output
6942
                        int32_t              outputSize,  // output size
6943
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
6944
) {
6945
  int32_t code;
6946

6947
  if (!(outputSize >= info->originalSize)) {
2,117,427,821✔
6948
    return TSDB_CODE_INVALID_PARA;
×
6949
  }
6950

6951
  if (info->cmprAlg == NO_COMPRESSION) {
2,117,643,938✔
6952
    if (!(info->compressedSize == info->originalSize)) {
32,112✔
6953
      return TSDB_CODE_INVALID_PARA;
×
6954
    }
6955
    (void)memcpy(output, input, info->compressedSize);
32,112✔
6956
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
6957
    SBuffer local;
1,014,442,945✔
6958

6959
    tBufferInit(&local);
6960
    if (buffer == NULL) {
1,014,607,125✔
6961
      buffer = &local;
×
6962
    }
6963

6964
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,014,607,125✔
6965
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,014,074,928✔
6966
      if (code) {
1,013,998,670✔
6967
        tBufferDestroy(&local);
6968
        return code;
×
6969
      }
6970
    }
6971

6972
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,029,139,736✔
6973
        input,                                                  // input
6974
        info->compressedSize,                                   // inputSize
1,014,613,855✔
6975
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,014,623,844✔
6976
        output,                                                 // output
6977
        outputSize,                                             // output size
6978
        info->cmprAlg,                                          // compression algorithm
1,014,638,209✔
6979
        buffer->data,                                           // helper buffer
6980
        buffer->capacity                                        // extra buffer size
1,014,612,700✔
6981
    );
6982
    if (decompressedSize < 0) {
1,014,447,126✔
6983
      tBufferDestroy(&local);
6984
      return TSDB_CODE_COMPRESS_ERROR;
×
6985
    }
6986

6987
    if (!(decompressedSize == info->originalSize)) {
1,014,447,126✔
6988
      return TSDB_CODE_COMPRESS_ERROR;
×
6989
    }
6990
    tBufferDestroy(&local);
6991
  } else {
6992
    DEFINE_VAR(info->cmprAlg);
1,103,058,670✔
6993
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
1,103,115,495✔
6994
      (void)memcpy(output, input, info->compressedSize);
14,496✔
6995
      return 0;
14,496✔
6996
    }
6997
    SBuffer local;
1,103,012,817✔
6998

6999
    tBufferInit(&local);
7000
    if (buffer == NULL) {
1,103,162,257✔
7001
      buffer = &local;
×
7002
    }
7003
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,103,162,257✔
7004
    if (code) {
1,103,068,159✔
7005
      return code;
×
7006
    }
7007

7008
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7009
        input,                                                  // input
7010
        info->compressedSize,                                   // inputSize
1,103,159,182✔
7011
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,103,205,540✔
7012
        output,                                                 // output
7013
        outputSize,                                             // output size
7014
        info->cmprAlg,                                          // compression algorithm
1,103,185,268✔
7015
        buffer->data,                                           // helper buffer
7016
        buffer->capacity                                        // extra buffer size
1,103,173,518✔
7017
    );
7018
    if (decompressedSize < 0) {
1,102,835,668✔
7019
      tBufferDestroy(&local);
7020
      return TSDB_CODE_COMPRESS_ERROR;
×
7021
    }
7022

7023
    if (!(decompressedSize == info->originalSize)) {
1,102,835,668✔
7024
      return TSDB_CODE_COMPRESS_ERROR;
×
7025
    }
7026
    tBufferDestroy(&local);
7027
  }
7028

7029
  return 0;
2,117,496,595✔
7030
}
7031

7032
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
748,368,582✔
7033
  int32_t code;
7034

7035
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
748,368,582✔
7036
  if (code) return code;
748,338,214✔
7037

7038
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
748,338,214✔
7039
  if (code) return code;
748,401,864✔
7040

7041
  output->size += info->compressedSize;
748,401,864✔
7042
  return 0;
748,452,550✔
7043
}
7044

7045
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,014,445,021✔
7046
  int32_t code;
7047

7048
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
1,014,445,021✔
7049
  if (code) return code;
1,014,015,240✔
7050

7051
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,014,015,240✔
7052
  if (code) return code;
1,014,445,087✔
7053

7054
  output->size += info->originalSize;
1,014,445,087✔
7055
  return 0;
1,014,558,880✔
7056
}
7057

7058
// handle all types, including var data
7059
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7060
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7061
    pVal->pData = pDatum;
70,887,909✔
7062
    pVal->nData = len;
571,638,789✔
7063
  } else {
7064
    switch (len) {
2,147,483,647✔
7065
      case sizeof(uint8_t):
2,147,483,647✔
7066
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7067
        break;
2,147,483,647✔
7068
      case sizeof(uint16_t):
2,147,483,647✔
7069
        pVal->val = *(uint16_t *)pDatum;
2,147,483,647✔
7070
        break;
2,147,483,647✔
7071
      case sizeof(uint32_t):
2,147,483,647✔
7072
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7073
        break;
2,147,483,647✔
7074
      case sizeof(uint64_t):
2,147,483,647✔
7075
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7076
        break;
2,147,483,647✔
7077
      default:
112,951✔
7078
        break;
112,951✔
7079
    }
7080
  }
7081
}
2,147,483,647✔
7082

7083
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
172,236,734✔
7084
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
172,236,734✔
7085
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
26,374,227✔
7086
    pDst->nData = pSrc->nData;
26,272,897✔
7087
  } else {
7088
    pDst->val = pSrc->val;
145,862,507✔
7089
  }
7090
}
172,322,592✔
7091
void valueClearDatum(SValue *pVal, int8_t type) {
×
7092
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7093
    taosMemoryFreeClear(pVal->pData);
×
7094
    pVal->nData = 0;
×
7095
  } else {
7096
    pVal->val = 0;
×
7097
  }
7098
}
×
7099

7100
int8_t schemaHasBlob(const STSchema *pSchema) {
540,118,707✔
7101
  if (pSchema == NULL) {
540,118,707✔
7102
    return 0;
×
7103
  }
7104
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7105
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7106
      return 1;
32,208✔
7107
    }
7108
  }
7109
  return 0;
540,096,035✔
7110
}
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