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

taosdata / TDengine / #5050

12 May 2026 05:36AM UTC coverage: 73.398% (+0.09%) from 73.313%
#5050

push

travis-ci

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

90 of 101 new or added lines in 2 files covered. (89.11%)

489 existing lines in 125 files now uncovered.

281602 of 383662 relevant lines covered (73.4%)

138099127.08 hits per line

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

69.31
/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) {
250,275,621✔
79
  int32_t n = 0;
250,275,621✔
80
  n += tPutI8(p ? p + n : p, index->type);
250,275,621✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
250,278,501✔
82
  return n;
250,278,501✔
83
}
84

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

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

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

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

109
  if (isPK) {
2,147,483,647✔
110
    sinfo->tupleIndices[sinfo->numOfPKs].type = colVal->value.type;
83,426,727✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
83,426,871✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
83,426,855✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
83,426,919✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
83,426,503✔
115
    sinfo->numOfPKs++;
83,426,439✔
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
54,028,666✔
122
                             + BSE_SEQUECE_SIZE;                     // value
27,014,433✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
54,028,666✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
27,014,433✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
54,028,666✔
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;
178,592✔
164
        break;
89,296✔
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) {
19,748✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
45,880✔
185
        break;
22,940✔
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,147,483,647✔
197
  }
198
  if (sinfo->numOfValue) {
2,147,483,647✔
199
    sinfo->flag |= HAS_VALUE;
2,147,483,647✔
200
  }
201

202
  // Tuple
203
  sinfo->tupleFlag = sinfo->flag;
2,147,483,647✔
204
  switch (sinfo->flag) {
2,147,483,647✔
205
    case HAS_NONE:
2,147,483,647✔
206
    case HAS_NULL:
207
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
208
      sinfo->tupleFixedSize = 0;
2,147,483,647✔
209
      break;
2,147,483,647✔
210
    case HAS_VALUE:
2,147,483,647✔
211
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
212
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
213
      break;
2,147,483,647✔
214
    case (HAS_NONE | HAS_NULL):
2,647,772✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,647,772✔
216
      sinfo->tupleFixedSize = 0;
2,647,121✔
217
      break;
2,647,121✔
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,252,157,246✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,252,157,246✔
225
      sinfo->tupleFixedSize = schema->flen;
1,252,152,689✔
226
      break;
946,072,681✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
83,426,055✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
83,426,583✔
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) {
50,259,759✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
50,421,401✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
50,421,429✔
245
  } else {
246
    sinfo->kvFlag = (KV_FLG_BIG | sinfo->flag);
×
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint32_t);
×
248
  }
249
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
250
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
83,426,135✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
83,426,359✔
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);
77,036,229✔
287
  }
288

289
  // bitmap + fixed + varlen
290
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
2,147,483,647✔
291
  int32_t colValIndex = 1;
2,147,483,647✔
292
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
293
    for (;;) {
294
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
295
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
15,584✔
296
        break;
15,584✔
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
831,842,351✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
776,457,878✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
56,524,851✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
56,531,298✔
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
3,936✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
140✔
325
        break;
140✔
326
      } else {
327
        colValIndex++;
3,796✔
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,747,548✔
336
  SBlobItem item = {.type = type, .len = 0};
2,747,548✔
337
  uint64_t  seq = 0;
2,747,548✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
2,747,548✔
339
  if (code != 0) return code;
2,747,548✔
340

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

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

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

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

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

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

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

438
        colValIndex++;
93,048✔
439
        break;
93,048✔
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) {
5,088✔
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
5,088✔
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) {
811,477,720✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
811,477,720✔
570
  } else {
571
    ((uint32_t *)indices->idx)[indices->nCol] = (uint32_t)offset;
×
572
  }
573
  indices->nCol++;
2,147,483,647✔
574
}
2,147,483,647✔
575

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

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

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

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

598
  // primary keys
599
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
600
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
6,390,274✔
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;
38,832✔
609
      }
610

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

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

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

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

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

664
  if (((*ppRow)->flag) == 0) {
27,011,382✔
665
    return TSDB_CODE_INVALID_PARA;
×
666
  }
667

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

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

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
27,011,382✔
679
  int32_t colValIndex = 1;
27,011,382✔
680
  int8_t  firstBlobCol = 1;
27,011,382✔
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
61,034,768✔
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
34,023,386✔
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
34,023,386✔
687

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
34,023,386✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
34,023,386✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
30,525,887✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
30,525,887✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
27,721,980✔
693
              hasBlob = 1;
27,009,843✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
27,721,980✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
27,721,980✔
697
            if (colValArray[colValIndex].value.nData > 0) {
27,721,980✔
698
              if (hasBlob == 0) {
25,543,128✔
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
712,137✔
700
                             colValArray[colValIndex].value.nData);
712,137✔
701
                payloadSize += colValArray[colValIndex].value.nData;
712,137✔
702
              } else {
703
                uint64_t  seq = 0;
24,830,991✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
24,830,991✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
24,830,991✔
706
                                  .data = colValArray[colValIndex].value.pData,
24,830,991✔
707
                                  .len = colValArray[colValIndex].value.nData,
24,830,991✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
24,830,991✔
710
                if (code != 0) return code;
24,830,991✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
49,661,982✔
712
              }
713
            } else {
714
              if (hasBlob) {
2,178,852✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
2,178,852✔
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,803,907✔
720
            (void)memcpy(payload + payloadSize,
2,804,307✔
721
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,803,907✔
722
                         tDataTypes[schema->columns[i].type].bytes);
2,803,907✔
723
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,803,907✔
724
          }
725
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
3,497,499✔
726
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
1,539✔
727
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
1,539✔
728
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
1,539✔
729
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
1,539✔
730
          }
731
        }
732
        colValIndex++;
34,023,386✔
733
        break;
34,023,386✔
734
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
735
        break;
×
736
      } else {
737
        colValIndex++;
×
738
      }
739
    }
740
  }
741

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1006
  return code;
×
1007
}
1008

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

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

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

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

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

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

1050
  return code;
×
1051
}
1052

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

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

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

1082
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
27,574,318✔
1083
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
562,936✔
1084
  } else {
1085
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
27,011,382✔
1086
  }
1087

1088
  return code;
27,574,318✔
1089
}
1090

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

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

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

1104
  return code;
×
1105
}
1106

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

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

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

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

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

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

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

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

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

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

1185
  offset = pBlobSet->len;
27,583,271✔
1186
  pBlobSet->len += len;
27,583,271✔
1187

1188
  pBlobSet->seq++;
27,583,271✔
1189
  *seq = pBlobSet->seq;
27,583,271✔
1190

1191
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow, .type = pItem->type};
27,583,271✔
1192
  if (taosArrayPush(pBlobSet->pSeqTable, &value) == NULL) {
55,166,542✔
1193
    TAOS_CHECK_EXIT(terrno);
×
1194
  }
1195

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

1202
_exit:
27,583,271✔
1203
  return code;
27,583,271✔
1204
}
1205

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

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

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

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

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

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

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

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

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

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

1259
  return code;
×
1260
}
1261

1262
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
1,725,065,516✔
1263
  if (pBlobSet == NULL) return 0;
1,725,065,516✔
1264
  return taosArrayGetSize(pBlobSet->pSeqTable);
131,796✔
1265
}
1266

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

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

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

1311
  if (!infoSorted) {
2,171,780✔
1312
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1313
  }
1314

1315
  int32_t code = 0;
2,171,780✔
1316
  int32_t numOfRows = infos[0].bind->num;
2,171,780✔
1317
  SArray *colValArray;
1318
  SColVal colVal;
2,150,632✔
1319

1320
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
2,172,504✔
1321
    return terrno;
×
1322
  }
1323

1324
  SRowKey rowKey, lastRowKey;
2,147,334✔
1325
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
18,295,060✔
1326
    taosArrayClear(colValArray);
16,498,039✔
1327

1328
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
82,668,712✔
1329
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
66,564,956✔
1330
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
1,544✔
1331
      } else {
1332
        SValue value = {
66,575,135✔
1333
            .type = infos[iInfo].type,
66,553,224✔
1334
        };
1335
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
66,621,104✔
1336
          value.nData = infos[iInfo].bind->length[iRow];
4,568,372✔
1337
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
4,694,068✔
1338
            code = TSDB_CODE_INVALID_PARA;
×
1339
            goto _exit;
×
1340
          }
1341
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
4,694,800✔
1342
        } else {
1343
          valueSetDatum(&value, infos[iInfo].type,
62,167,262✔
1344
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
62,149,900✔
1345
                        infos[iInfo].bind->buffer_length);
62,172,504✔
1346
        }
1347
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
67,855,673✔
1348
      }
1349
      if (taosArrayPush(colValArray, &colVal) == NULL) {
66,264,922✔
1350
        code = terrno;
×
1351
        goto _exit;
×
1352
      }
1353
    }
1354

1355
    SRow             *row;
4,365,711✔
1356
    SRowBuildScanInfo sinfo = {0};
16,083,641✔
1357
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
16,093,507✔
1358
      goto _exit;
×
1359
    }
1360

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

1366
    if (pOrdered && pDupTs) {
16,083,512✔
1367
      tRowGetKey(row, &rowKey);
32,286,122✔
1368
      if (iRow == 0) {
16,157,601✔
1369
        *pOrdered = true;
2,168,191✔
1370
        *pDupTs = false;
2,169,215✔
1371
      } else {
1372
        if (*pOrdered) {
13,989,410✔
1373
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
14,044,290✔
1374
          *pOrdered = (res >= 0);
14,044,290✔
1375
          if (!*pDupTs) {
14,023,394✔
1376
            *pDupTs = (res == 0);
13,945,024✔
1377
          }
1378
        }
1379
      }
1380
      lastRowKey = rowKey;
16,148,239✔
1381
    }
1382
  }
1383

1384
_exit:
2,150,362✔
1385
  taosArrayDestroy(colValArray);
2,152,902✔
1386
  return code;
2,170,797✔
1387
}
1388

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

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

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

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

1408
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1409
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
571,275,485✔
1410
    return 0;
573,369,022✔
1411
  }
1412

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

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

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

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

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

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

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

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

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

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

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

1504
    if (bit == BIT_FLG_NONE) {
2,147,483,647✔
1505
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,875,486✔
1506
      return 0;
1,875,486✔
1507
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1508
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
2,023,857,867✔
1509
      return 0;
2,023,861,305✔
1510
    }
1511

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

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

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

1534
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
479,924,931✔
1535
  SRowKey key1, key2;
479,901,631✔
1536
  tRowGetKey(*(SRow **)p1, &key1);
962,362,202✔
1537
  tRowGetKey(*(SRow **)p2, &key2);
963,305,658✔
1538
  return tRowKeyCompare(&key1, &key2);
482,730,915✔
1539
}
1540
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
45,867,330✔
1541

1542
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
94,775,088✔
1543
  SColVal* pColVal = tRowIterNext(iter);
94,775,088✔
1544
  while (pColVal != NULL && pColVal->cid < targetCid) {
123,076,602✔
1545
    pColVal = tRowIterNext(iter);
28,298,664✔
1546
  }
1547
  return pColVal;
94,777,994✔
1548
}
1549

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

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

1564
  for (int32_t i = 0; i < nRow; i++) {
51,656,171✔
1565
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
45,869,378✔
1566

1567
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
45,868,846✔
1568
    if (code) goto _exit;
45,869,574✔
1569
  }
1570

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

1578
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
77,736,402✔
1579
    int32_t targetCid = pTSchema->columns[iCol].colId;
71,949,181✔
1580
    SColVal *pColVal = NULL;
71,948,417✔
1581

1582
    switch (strategy) {
71,948,417✔
1583
      case KEEP_CONSISTENCY:
5,004,529✔
1584
        if (nRow > 0)
5,004,529✔
1585
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
5,004,529✔
1586
        break;
5,004,529✔
1587

1588
      default:  // default using PREFER_NON_NULL strategy
66,943,888✔
1589
      case PREFER_NON_NULL:
1590
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
109,438,454✔
1591
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
89,772,441✔
1592

1593
          if (COL_VAL_IS_VALUE(pColValT)) {
89,773,101✔
1594
            pColVal = pColValT;
47,278,667✔
1595
            break;
47,278,667✔
1596
          } else if (pColVal == NULL) {
42,494,566✔
1597
            pColVal = pColValT;
21,507,753✔
1598
          }
1599
        }
1600
        break;
66,944,680✔
1601
    }
1602

1603
    if (pColVal) {
71,949,209✔
1604
      if (taosArrayPush(aColVal, pColVal) == NULL) {
71,949,549✔
1605
        code = terrno;
×
1606
        goto _exit;
×
1607
      }
1608
    }
1609
  }
1610

1611
  // build
1612
  SRowBuildScanInfo sinfo = {0};
5,784,297✔
1613
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
5,786,585✔
1614

1615
  if (code) goto _exit;
5,786,217✔
1616

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

1623
_exit:
5,786,577✔
1624
  if (aIter) {
5,786,589✔
1625
    for (int32_t i = 0; i < nRow; i++) {
51,656,363✔
1626
      tRowIterClose(&aIter[i]);
45,869,738✔
1627
    }
1628
    taosMemoryFree(aIter);
5,786,625✔
1629
  }
1630
  if (aColVal) taosArrayDestroy(aColVal);
5,786,621✔
1631
  if (code) tRowDestroy(pRow);
5,786,629✔
1632
  return code;
5,786,629✔
1633
}
1634
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
8,520✔
1635
  int32_t code = 0;
8,520✔
1636
  int32_t lino = 0;
8,520✔
1637
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
17,040✔
1638
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
8,520✔
1639
    code = addEmptyItemToBlobSet(pDst, type, NULL);
8,520✔
1640
    TAOS_CHECK_GOTO(code, &lino, _error);
8,520✔
1641
  } else {
1642
    uint64_t seq = 0;
×
1643
    if (tGetU64(pVal->value.pData, &seq) < 0) {
×
1644
      uError("tBlobSetTransferTo: invalid blob value, seq %p", pVal->value.pData);
×
1645
      return TSDB_CODE_INVALID_PARA;
×
1646
    }
1647

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

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

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

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

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

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

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

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

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

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

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

1712
  tBlobSetSwap(pBlob, pTempBlob);
852✔
1713

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

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

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

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

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

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

1757
    tRowGetKey(row1, &key1);
×
1758

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

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

1767
      iEnd++;
×
1768
    }
1769

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

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

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

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

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

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

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

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

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

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

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

1850
  // build
1851

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

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

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

1872
  return code;
×
1873
}
1874

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

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

1887
  int32_t iStart = 0;
1,862,019✔
1888
  while (iStart < aRowP->size) {
36,687,337✔
1889
    SRowKey key1;
34,809,784✔
1890
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
34,828,746✔
1891

1892
    tRowGetKey(row1, &key1);
69,633,572✔
1893

1894
    int32_t iEnd = iStart + 1;
34,824,102✔
1895
    while (iEnd < aRowP->size) {
74,892,079✔
1896
      SRowKey key2;
73,028,873✔
1897
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
73,037,332✔
1898
      tRowGetKey(row2, &key2);
146,003,364✔
1899

1900
      if (tRowKeyCompare(&key1, &key2) != 0) break;
73,044,432✔
1901

1902
      iEnd++;
40,087,357✔
1903
    }
1904

1905
    if (iEnd - iStart > 1) {
34,820,882✔
1906
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
5,786,545✔
1907
      if (code) return code;
5,786,521✔
1908
    }
1909

1910
    // the array is also changing, so the iStart just ++ instead of iEnd
1911
    iStart++;
34,820,858✔
1912
  }
1913

1914
  return code;
1,862,019✔
1915
}
1916

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

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

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

1936
    tRowGetKey(row1, &key1);
17,040✔
1937

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

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

1946
      iEnd++;
×
1947
    }
1948

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

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

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

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

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

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

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

1996
  int32_t code = 0;
48,370,504✔
1997

1998
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
48,370,504✔
1999
  if (pIter == NULL) {
48,371,446✔
2000
    code = terrno;
×
2001
    goto _exit;
×
2002
  }
2003

2004
  pIter->pRow = pRow;
48,371,446✔
2005
  pIter->pTSchema = pTSchema;
48,371,282✔
2006
  pIter->iTColumn = 0;
48,371,426✔
2007

2008
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
48,371,450✔
2009

2010
  uint8_t         *data = pRow->data;
48,208,760✔
2011
  SPrimaryKeyIndex index;
48,201,538✔
2012
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
48,325,028✔
2013
    data += tGetPrimaryKeyIndex(data, &index);
116,352✔
2014
  }
2015

2016
  if (pRow->flag >> 4) {
48,208,172✔
2017
    pIter->iCol = 0;
4,820,096✔
2018
    pIter->pIdx = (SKVIdx *)data;
4,820,096✔
2019
    if (pRow->flag & KV_FLG_LIT) {
4,820,096✔
2020
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
4,820,096✔
2021
    } else if (pRow->flag & KV_FLG_MID) {
×
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
×
2023
    } else {
2024
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
2025
    }
2026
  } else {
2027
    switch (pRow->flag) {
43,388,580✔
2028
      case (HAS_NULL | HAS_NONE):
3,108✔
2029
        pIter->pb = data;
3,108✔
2030
        break;
3,108✔
2031
      case HAS_VALUE:
40,326,821✔
2032
        pIter->pf = data;
40,326,821✔
2033
        pIter->pv = pIter->pf + pTSchema->flen;
40,326,869✔
2034
        break;
40,326,625✔
2035
      case (HAS_VALUE | HAS_NONE):
3,058,651✔
2036
      case (HAS_VALUE | HAS_NULL):
2037
        pIter->pb = data;
3,058,651✔
2038
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
3,058,651✔
2039
        pIter->pv = pIter->pf + pTSchema->flen;
3,058,651✔
2040
        break;
3,058,651✔
2041
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2042
        pIter->pb = data;
×
2043
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2044
        pIter->pv = pIter->pf + pTSchema->flen;
×
2045
        break;
×
2046
      default:
×
2047
        break;
×
2048
    }
2049
  }
2050

2051
_exit:
48,371,246✔
2052
  if (code) {
48,370,530✔
2053
    *ppIter = NULL;
×
2054
  } else {
2055
    *ppIter = pIter;
48,370,530✔
2056
  }
2057
  return code;
48,370,974✔
2058
}
2059

2060
void tRowIterClose(SRowIter **ppIter) {
48,371,120✔
2061
  SRowIter *pIter = *ppIter;
48,371,120✔
2062
  if (pIter) {
48,371,160✔
2063
    taosMemoryFree(pIter);
48,371,168✔
2064
  }
2065
  *ppIter = NULL;
48,371,370✔
2066
}
48,371,398✔
2067

2068
SColVal *tRowIterNext(SRowIter *pIter) {
135,522,966✔
2069
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
135,522,966✔
2070
    return NULL;
1,581,910✔
2071
  }
2072

2073
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
133,944,484✔
2074

2075
  // timestamp
2076
  if (0 == pIter->iTColumn) {
133,948,388✔
2077
    pIter->cv.cid = pTColumn->colId;
12,106,905✔
2078
    pIter->cv.value.type = pTColumn->type;
12,107,347✔
2079
    pIter->cv.flag = CV_FLAG_VALUE;
12,107,859✔
2080
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
12,106,779✔
2081
    goto _exit;
12,107,839✔
2082
  }
2083

2084
  if (pIter->pRow->flag == HAS_NONE) {
121,840,124✔
2085
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
157,961✔
2086
    goto _exit;
157,961✔
2087
  }
2088

2089
  if (pIter->pRow->flag == HAS_NULL) {
121,682,214✔
2090
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
380,201✔
2091
    goto _exit;
379,721✔
2092
  }
2093

2094
  if (pIter->pRow->flag >> 4) {  // KV
121,301,016✔
2095
    if (pIter->iCol < pIter->pIdx->nCol) {
82,469,313✔
2096
      uint8_t *pData;
2097

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

2106
      int16_t cid;
45,645,125✔
2107
      pData += tGetI16v(pData, &cid);
45,645,125✔
2108

2109
      if (TABS(cid) == pTColumn->colId) {
45,645,125✔
2110
        if (cid < 0) {
44,225,227✔
2111
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,621,950✔
2112
        } else {
2113
          pIter->cv.cid = pTColumn->colId;
40,603,277✔
2114
          pIter->cv.value.type = pTColumn->type;
40,603,277✔
2115
          pIter->cv.flag = CV_FLAG_VALUE;
40,603,277✔
2116

2117
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
40,603,277✔
2118
            pData += tGetU32v(pData, &pIter->cv.value.nData);
17,287,474✔
2119
            if (pIter->cv.value.nData > 0) {
8,643,737✔
2120
              pIter->cv.value.pData = pData;
8,642,243✔
2121
            } else {
2122
              pIter->cv.value.pData = NULL;
1,494✔
2123
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2124
              //   pIter->cv.value.pData = pData;
2125
              // }
2126
            }
2127
          } else {
2128
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
31,959,540✔
2129
          }
2130
        }
2131

2132
        pIter->iCol++;
44,225,227✔
2133
        goto _exit;
44,225,227✔
2134
      } else if (TABS(cid) > pTColumn->colId) {
1,419,898✔
2135
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,419,898✔
2136
        goto _exit;
1,419,898✔
2137
      } else {
2138
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2139
        goto _exit;
×
2140
      }
2141
    } else {
2142
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
36,824,188✔
2143
      goto _exit;
36,824,188✔
2144
    }
2145
  } else {  // Tuple
2146
    uint8_t bv = BIT_FLG_VALUE;
38,833,524✔
2147
    if (pIter->pb) {
38,833,524✔
2148
      switch (pIter->pRow->flag) {
16,190,833✔
2149
        case (HAS_NULL | HAS_NONE):
28,104✔
2150
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
28,104✔
2151
          break;
28,104✔
2152
        case (HAS_VALUE | HAS_NONE):
9,856,867✔
2153
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
9,856,867✔
2154
          if (bv) bv++;
9,856,867✔
2155
          break;
9,856,867✔
2156
        case (HAS_VALUE | HAS_NULL):
6,305,862✔
2157
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
6,305,862✔
2158
          break;
6,305,862✔
2159
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2160
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2161
          break;
×
2162
        default:
×
2163
          break;
×
2164
      }
2165

2166
      if (bv == BIT_FLG_NONE) {
16,190,833✔
2167
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,013,804✔
2168
        goto _exit;
1,013,804✔
2169
      } else if (bv == BIT_FLG_NULL) {
15,177,029✔
2170
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,111,352✔
2171
        goto _exit;
1,111,352✔
2172
      }
2173
    }
2174

2175
    pIter->cv.cid = pTColumn->colId;
36,708,628✔
2176
    pIter->cv.value.type = pTColumn->type;
36,707,654✔
2177
    pIter->cv.flag = CV_FLAG_VALUE;
36,707,709✔
2178
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
41,303,830✔
2179
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
4,594,639✔
2180
      pData += tGetU32v(pData, &pIter->cv.value.nData);
9,191,482✔
2181
      if (pIter->cv.value.nData > 0) {
4,595,228✔
2182
        pIter->cv.value.pData = pData;
4,590,956✔
2183
      } else {
2184
        pIter->cv.value.pData = NULL;
4,868✔
2185
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2186
        //   pIter->cv.value.pData = pData;
2187
        // }
2188
      }
2189
    } else {
2190
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
32,113,671✔
2191
    }
2192
    goto _exit;
36,700,474✔
2193
  }
2194

2195
_exit:
133,940,464✔
2196
  pIter->iTColumn++;
133,940,464✔
2197
  return &pIter->cv;
133,939,787✔
2198
}
2199

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

2203
  if (flag) return code;
820,757✔
2204

2205
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
6,539,613✔
2206
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
6,002,788✔
2207
    if (code) return code;
6,002,788✔
2208
  }
2209

2210
  return code;
536,825✔
2211
}
2212
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
65,360,999✔
2213
  int32_t code = 0;
65,360,999✔
2214

2215
  int32_t   iColData = 0;
65,360,999✔
2216
  SColData *pColData = &aColData[iColData];
65,360,999✔
2217
  int32_t   iTColumn = 1;
65,375,467✔
2218
  STColumn *pTColumn = &pSchema->columns[iTColumn];
65,375,467✔
2219

2220
  while (pColData) {
423,305,935✔
2221
    if (pTColumn) {
357,789,316✔
2222
      if (pTColumn->colId == pColData->cid) {  // NULL
357,789,316✔
2223
        if (flag == 0) {
357,843,667✔
2224
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
357,843,667✔
2225
        } else {
2226
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2227
        }
2228
        if (code) goto _exit;
357,810,749✔
2229

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

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

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

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

2263
  switch (pRow->flag) {
2,147,483,647✔
2264
    case HAS_VALUE:
2,147,483,647✔
2265
      pf = data;  // TODO: fix here
2,147,483,647✔
2266
      pv = pf + pTSchema->flen;
2,147,483,647✔
2267
      break;
2,147,483,647✔
2268
    case (HAS_NULL | HAS_NONE):
195,351✔
2269
      pb = data;
195,351✔
2270
      break;
195,351✔
2271
    case (HAS_VALUE | HAS_NONE):
476,482,505✔
2272
    case (HAS_VALUE | HAS_NULL):
2273
      pb = data;
476,482,505✔
2274
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
476,482,505✔
2275
      pv = pf + pTSchema->flen;
476,480,947✔
2276
      break;
476,528,297✔
2277
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
64,987✔
2278
      pb = data;
64,987✔
2279
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
64,987✔
2280
      pv = pf + pTSchema->flen;
×
2281
      break;
×
2282
    default:
602✔
2283
      return TSDB_CODE_INVALID_DATA_FMT;
602✔
2284
  }
2285

2286
  while (pColData) {
2,147,483,647✔
2287
    if (pTColumn) {
2,147,483,647✔
2288
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2289
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647✔
2290
          return TSDB_CODE_INVALID_PARA;
×
2291
        }
2292
        if (pb) {
2,147,483,647✔
2293
          uint8_t bv;
2294
          switch (pRow->flag) {
1,980,081,490✔
2295
            case (HAS_NULL | HAS_NONE):
3,858,717✔
2296
              bv = GET_BIT1(pb, iTColumn - 1);
3,858,717✔
2297
              break;
3,858,717✔
2298
            case (HAS_VALUE | HAS_NONE):
66,532,901✔
2299
              bv = GET_BIT1(pb, iTColumn - 1);
66,532,901✔
2300
              if (bv) bv++;
66,799,357✔
2301
              break;
66,799,357✔
2302
            case (HAS_VALUE | HAS_NULL):
1,909,864,573✔
2303
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
1,909,864,573✔
2304
              break;
1,909,858,590✔
2305
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
132✔
2306
              bv = GET_BIT2(pb, iTColumn - 1);
132✔
2307
              break;
×
2308
            default:
×
2309
              return TSDB_CODE_INVALID_DATA_FMT;
×
2310
          }
2311

2312
          if (bv == BIT_FLG_NONE) {
1,980,516,664✔
2313
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
8,516,009✔
2314
              goto _exit;
×
2315
            goto _continue;
8,521,136✔
2316
          } else if (bv == BIT_FLG_NULL) {
1,972,000,655✔
2317
            if (flag == 0) {
568,435,796✔
2318
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
568,111,293✔
2319
            } else {
2320
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
324,503✔
2321
            }
2322
            if (code) goto _exit;
568,433,860✔
2323
            goto _continue;
568,433,860✔
2324
          }
2325
        }
2326

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

2348
      _continue:
2,147,483,647✔
2349
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2350
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2351
      } else if (pTColumn->colId > pColData->cid) {  // NONE
119,497,137✔
2352
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2353
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2354
      } else {
2355
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
119,352,830✔
2356
      }
2357
    } else {
2358
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
88,587,493✔
2359
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
88,564,827✔
2360
    }
2361
  }
2362

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

2369
  uint8_t  *pv = NULL;
1,982,504,486✔
2370
  int32_t   iColData = 0;
1,982,504,486✔
2371
  SColData *pColData = &aColData[iColData];
1,982,504,486✔
2372
  int32_t   iTColumn = 1;
1,982,722,180✔
2373
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,982,722,180✔
2374
  int32_t   iCol = 0;
1,982,831,334✔
2375

2376
  // primary keys
2377
  uint8_t         *data = pRow->data;
1,982,831,334✔
2378
  SPrimaryKeyIndex index;
1,982,745,038✔
2379
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,988,758,262✔
2380
    data += tGetPrimaryKeyIndex(data, &index);
6,001,041✔
2381
  }
2382

2383
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,982,763,309✔
2384
  if (pRow->flag & KV_FLG_LIT) {
1,982,763,309✔
2385
    pv = pKVIdx->idx + pKVIdx->nCol;
1,970,671,349✔
2386
  } else if (pRow->flag & KV_FLG_MID) {
12,034,526✔
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
12,048,534✔
2388
  } else if (pRow->flag & KV_FLG_BIG) {
×
2389
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2390
  } else {
2391
    return TSDB_CODE_INVALID_PARA;
×
2392
  }
2393

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

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

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

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

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

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

2471
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
2472
    return tRowNoneUpsertColData(aColData, nColData, flag);
820,757✔
2473
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2474
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
65,437,618✔
2475
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2476
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,982,538,368✔
2477
  } else {  // TUPLE row
2478
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
2,147,483,647✔
2479
  }
2480
}
2481

2482
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
1,954,079,389✔
2483
  key->numOfPKs = row->numOfPKs;
1,954,079,389✔
2484

2485
  if (key->numOfPKs == 0) {
1,955,025,176✔
2486
    return;
×
2487
  }
2488

2489
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
1,950,334,353✔
2490

2491
  uint8_t *data = row->data;
1,956,708,676✔
2492

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

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

2501
    uint8_t *tdata = data + indices[i].offset;
1,955,375,733✔
2502
    if (row->flag >> 4) {
1,956,323,257✔
2503
      tdata += tGetI16v(tdata, NULL);
1,312,852,683✔
2504
    }
2505

2506
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
1,956,851,643✔
2507
      key->pks[i].pData = tdata;
495,373,919✔
2508
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
992,514,941✔
2509
    } else {
2510
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
1,460,964,265✔
2511
    }
2512
  }
2513
}
2514

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

2526
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
1,289,433,628✔
2527
  switch (tv1->type) {
1,289,433,628✔
2528
    case TSDB_DATA_TYPE_BOOL:
×
2529
    case TSDB_DATA_TYPE_TINYINT:
2530
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_SMALLINT:
×
2532
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2533
    case TSDB_DATA_TYPE_INT:
561,641,529✔
2534
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
561,641,529✔
2535
    case TSDB_DATA_TYPE_BIGINT:
154,358,810✔
2536
    case TSDB_DATA_TYPE_TIMESTAMP:
2537
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
154,358,810✔
2538
    case TSDB_DATA_TYPE_FLOAT:
×
2539
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_DOUBLE:
×
2541
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_UTINYINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_USMALLINT:
×
2545
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2546
    case TSDB_DATA_TYPE_UINT:
153,680,323✔
2547
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
153,680,323✔
2548
    case TSDB_DATA_TYPE_UBIGINT:
154,651,024✔
2549
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
154,651,024✔
2550
    case TSDB_DATA_TYPE_GEOMETRY:
265,192,588✔
2551
    case TSDB_DATA_TYPE_BINARY: {
2552
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
265,192,588✔
2553
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
265,108,707✔
2554
    }
2555
    case TSDB_DATA_TYPE_NCHAR: {
×
2556
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2557
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2558
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2559
    }
2560
    case TSDB_DATA_TYPE_VARBINARY: {
×
2561
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2562
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2563
    }
2564
    default:
×
2565
      break;
×
2566
  }
2567

2568
  return 0;
×
2569
}
2570

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

2581
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2582
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2583
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
1,289,500,579✔
2584
      if (ret) return ret;
1,289,287,267✔
2585
    }
2586
  } else if (key1->numOfPKs < key2->numOfPKs) {
4,400✔
2587
    return -1;
4,400✔
2588
  } else {
UNCOV
2589
    return 1;
×
2590
  }
2591

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

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

2599
  if (pSrc->numOfPKs > 0) {
2,147,483,647✔
2600
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
1,572,865,012✔
2601
      SValue *pVal = &pDst->pks[i];
786,651,259✔
2602
      pVal->type = pSrc->pks[i].type;
786,491,344✔
2603

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

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

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

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

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

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

2716
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
401,697,954✔
2717
  int32_t n = 0;
401,697,954✔
2718

2719
  // key
2720
  if (isJson) {
401,697,954✔
2721
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,755,360✔
2722
  } else {
2723
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
800,624,727✔
2724
  }
2725

2726
  // type
2727
  n += tPutI8(p ? p + n : p, pTagVal->type);
401,682,133✔
2728

2729
  // value
2730
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
401,736,276✔
2731
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
341,778,875✔
2732
  } else {
2733
    p = p ? p + n : p;
230,823,358✔
2734
    n += tDataTypes[pTagVal->type].bytes;
230,824,762✔
2735
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
230,813,806✔
2736
  }
2737

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

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

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

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

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

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

2766
bool tTagIsJsonNull(void *data) {
1,434,529✔
2767
  STag  *pTag = (STag *)data;
1,434,529✔
2768
  int8_t isJson = tTagIsJson(pTag);
1,434,529✔
2769
  if (!isJson) return false;
1,433,753✔
2770
  return ((STag *)data)->nTag == 0;
825,761✔
2771
}
2772

2773
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
83,297,883✔
2774
  int32_t  code = 0;
83,297,883✔
2775
  uint8_t *p = NULL;
83,297,883✔
2776
  int16_t  n = 0;
83,297,883✔
2777
  int16_t  nTag = taosArrayGetSize(pArray);
83,297,883✔
2778
  int32_t  szTag = 0;
83,311,503✔
2779
  int8_t   isLarge = 0;
83,311,503✔
2780

2781
  // sort
2782
  if (isJson) {
83,311,503✔
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
451,178✔
2784
  } else {
2785
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
82,860,325✔
2786
  }
2787

2788
  // get size
2789
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
284,081,638✔
2790
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
200,852,662✔
2791
  }
2792
  if (szTag <= INT8_MAX) {
83,228,976✔
2793
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
74,341,236✔
2794
  } else {
2795
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
8,887,770✔
2796
    isLarge = 1;
8,887,770✔
2797
  }
2798

2799
  // build tag
2800
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
83,228,976✔
2801
  if ((*ppTag) == NULL) {
83,310,119✔
2802
    code = terrno;
×
2803
    goto _err;
×
2804
  }
2805
  (*ppTag)->flags = 0;
83,278,430✔
2806
  if (isJson) {
83,307,317✔
2807
    (*ppTag)->flags |= TD_TAG_JSON;
451,178✔
2808
  }
2809
  if (isLarge) {
83,307,317✔
2810
    (*ppTag)->flags |= TD_TAG_LARGE;
8,901,000✔
2811
  }
2812
  (*ppTag)->len = szTag;
83,307,317✔
2813
  (*ppTag)->nTag = nTag;
83,259,354✔
2814
  (*ppTag)->ver = version;
83,269,714✔
2815

2816
  if (isLarge) {
83,248,914✔
2817
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
8,900,941✔
2818
  } else {
2819
    p = (uint8_t *)&(*ppTag)->idx[nTag];
74,347,973✔
2820
  }
2821
  n = 0;
83,291,644✔
2822
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
284,116,410✔
2823
    if (isLarge) {
200,787,290✔
2824
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
52,356,027✔
2825
    } else {
2826
      (*ppTag)->idx[iTag] = n;
148,431,263✔
2827
    }
2828
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
200,810,229✔
2829
  }
2830
#ifdef TD_DEBUG_PRINT_TAG
2831
  debugPrintSTag(*ppTag, __func__, __LINE__);
2832
#endif
2833

2834
  return code;
83,329,120✔
2835

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

2840
void tTagFree(STag *pTag) {
1,244,794,983✔
2841
  if (pTag) taosMemoryFree(pTag);
1,244,794,983✔
2842
}
1,244,794,983✔
2843

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

2849
  char  *data = NULL;
684,349,865✔
2850
  int8_t typeBytes = 0;
684,349,865✔
2851
  if (isJson) {
684,349,865✔
2852
    typeBytes = CHAR_BYTES;
2,396,915✔
2853
  }
2854

2855
  if (IS_VAR_DATA_TYPE(value->type)) {
684,349,865✔
2856
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
259,812,185✔
2857
    if (data == NULL) {
260,495,067✔
2858
      return NULL;
×
2859
    }
2860

2861
    if (isJson) {
260,495,067✔
2862
      *data = value->type;
1,831,051✔
2863
    }
2864

2865
    varDataLen(data + typeBytes) = value->nData;
260,495,067✔
2866
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
260,372,603✔
2867
  } else {
2868
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
424,056,323✔
2869
  }
2870

2871
  return data;
684,703,259✔
2872
}
2873

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

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

2889
  if (isLarge) {
2,147,483,647✔
2890
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
476,979,837✔
2891
  } else {
2892
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,927,710,373✔
2893
  }
2894

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

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

2913
    if (c < 0) {
2,147,483,647✔
2914
      ridx = midx - 1;
2,043,329,863✔
2915
    } else if (c > 0) {
2,147,483,647✔
2916
      lidx = midx + 1;
2,147,483,647✔
2917
    } else {
2918
      (void)memcpy(pTagVal, &tv, sizeof(tv));
2,147,483,647✔
2919
      return true;
2,147,483,647✔
2920
    }
2921
  }
2922
  return false;
63,216,966✔
2923
}
2924

2925
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
308,152,385✔
2926
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
616,412,953✔
2927
}
2928

2929
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
2,147,483,647✔
2930

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

2938
  if (isLarge) {
1,009,528✔
2939
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,428✔
2940
  } else {
2941
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,005,100✔
2942
  }
2943

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

2950
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,559,644✔
2951
    if (isLarge) {
1,550,116✔
2952
      offset = ((int16_t *)pTag->idx)[iTag];
5,919✔
2953
    } else {
2954
      offset = pTag->idx[iTag];
1,544,197✔
2955
    }
2956
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,550,116✔
2957
    if (taosArrayPush(*ppArray, &tv) == NULL) {
3,100,232✔
2958
      code = terrno;
×
2959
      goto _err;
×
2960
    }
2961
  }
2962

2963
  return code;
1,009,528✔
2964

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

2969

2970
void destroyTagVal(void *pTag) {
70,458,644✔
2971
  STagVal* pTagVal = (STagVal*)pTag;
70,458,644✔
2972
  if (pTagVal && IS_VAR_DATA_TYPE(pTagVal->type)) {
70,458,644✔
2973
    taosMemoryFree(pTagVal->pData);
30,993,714✔
2974
  }
2975
}
70,458,644✔
2976

2977
// STSchema ========================================
2978
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
1,308,782,039✔
2979
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
1,308,782,039✔
2980
  if (pTSchema == NULL) {
1,308,163,581✔
2981
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2982
    return NULL;
×
2983
  }
2984

2985
  pTSchema->numOfCols = numOfCols;
1,308,163,581✔
2986
  pTSchema->version = version;
1,308,100,614✔
2987

2988
  // timestamp column
2989
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,308,326,830✔
2990
    terrno = TSDB_CODE_INVALID_PARA;
×
2991
    taosMemoryFree(pTSchema);
×
2992
    return NULL;
×
2993
  }
2994
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,308,084,180✔
2995
    terrno = TSDB_CODE_INVALID_PARA;
×
2996
    taosMemoryFree(pTSchema);
×
2997
    return NULL;
×
2998
  }
2999
  pTSchema->columns[0].colId = aSchema[0].colId;
1,308,100,333✔
3000
  pTSchema->columns[0].type = aSchema[0].type;
1,308,896,895✔
3001
  pTSchema->columns[0].flags = aSchema[0].flags;
1,308,400,456✔
3002
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
1,308,776,356✔
3003
  pTSchema->columns[0].offset = -1;
1,308,978,314✔
3004

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

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

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

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

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

3030
  return pTSchema;
1,309,419,635✔
3031
}
3032

3033
static int32_t tTColumnCompare(const void *p1, const void *p2) {
91,888,484✔
3034
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
91,888,484✔
3035
    return -1;
15,292,654✔
3036
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
76,607,170✔
3037
    return 1;
54,247,322✔
3038
  }
3039

3040
  return 0;
22,367,537✔
3041
}
3042

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

3048
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
22,369,556✔
3049
}
3050

3051
// SColData ========================================
3052
void tColDataDestroy(void *ph) {
1,681,583,311✔
3053
  if (ph) {
1,681,583,311✔
3054
    SColData *pColData = (SColData *)ph;
1,681,799,421✔
3055

3056
    tFree(pColData->pBitMap);
1,681,799,421✔
3057
    tFree(pColData->aOffset);
1,682,040,779✔
3058
    tFree(pColData->pData);
1,681,775,221✔
3059
  }
3060
}
1,681,612,327✔
3061

3062
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,768,371,908✔
3063
  pColData->cid = cid;
1,768,371,908✔
3064
  pColData->type = type;
1,768,813,362✔
3065
  pColData->cflag = cflag;
1,768,941,604✔
3066
  tColDataClear(pColData);
1,768,978,280✔
3067
}
1,768,229,841✔
3068

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

3078
void tColDataDeepClear(SColData *pColData) {
46,864,651✔
3079
  pColData->pBitMap = NULL;
46,864,651✔
3080
  pColData->aOffset = NULL;
46,875,085✔
3081
  pColData->pData = NULL;
46,876,356✔
3082

3083
  tColDataClear(pColData);
46,882,629✔
3084
}
46,884,400✔
3085

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

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

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

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

3134
_exit:
2,147,483,647✔
3135
  return code;
2,147,483,647✔
3136
}
3137
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
724,542,648✔
3138
  pColData->flag = HAS_VALUE;
724,642,085✔
3139
  pColData->numOfValue++;
724,738,799✔
3140
  return tColDataPutValue(pColData, pData, nData);
724,711,950✔
3141
}
3142
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
17,960,040✔
3143
  pColData->flag = HAS_NONE;
17,960,040✔
3144
  pColData->numOfNone++;
17,960,040✔
3145
  pColData->nVal++;
17,960,040✔
3146
  return 0;
17,960,757✔
3147
}
3148
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
34,379,568✔
3149
  pColData->flag = HAS_NULL;
34,401,689✔
3150
  pColData->numOfNull++;
34,417,230✔
3151
  pColData->nVal++;
34,416,044✔
3152
  return 0;
34,406,568✔
3153
}
3154
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,016,037✔
3155
  int32_t code = 0;
1,016,037✔
3156

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

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

3164
  pColData->flag |= HAS_VALUE;
1,015,557✔
3165
  pColData->numOfValue++;
1,015,077✔
3166

3167
  if (pColData->nVal) {
1,015,077✔
3168
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,015,557✔
3169
      if (IS_STR_DATA_BLOB(pColData->type)) {
37,715✔
3170
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3171
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3172
        if (code) return code;
×
3173
        memset(pColData->aOffset, 0, nOffset);
×
3174

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

3189
  return tColDataPutValue(pColData, pData, nData);
1,015,077✔
3190
}
3191
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3192
  pColData->nVal++;
2,147,483,647✔
3193
  pColData->numOfNone++;
2,147,483,647✔
3194
  return 0;
2,147,483,647✔
3195
}
3196
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
70,805✔
3197
  int32_t code = 0;
94,241✔
3198

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

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

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

3210
  return code;
94,241✔
3211
}
3212
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,869,180✔
3213
  int32_t code = 0;
4,869,629✔
3214

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

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

3222
  pColData->flag |= HAS_VALUE;
4,869,629✔
3223
  pColData->numOfValue++;
4,870,463✔
3224

3225
  if (pColData->nVal) {
4,869,629✔
3226
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,326,156✔
3227
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,454,859✔
3228
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,456,527✔
3229
      if (code) return code;
1,456,527✔
3230
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,456,527✔
3231
        memset(pColData->aOffset, 0, nOffset);
1,450,294✔
3232
      }
3233
    } else {
3234
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,413,936✔
3235
      code = tRealloc(&pColData->pData, pColData->nData);
3,413,936✔
3236
      if (code) return code;
3,413,936✔
3237
      memset(pColData->pData, 0, pColData->nData);
3,413,936✔
3238
    }
3239
  }
3240

3241
  return tColDataPutValue(pColData, pData, nData);
4,870,463✔
3242
}
3243
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,632✔
3244
  int32_t code = 0;
19,632✔
3245

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

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

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

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

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

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

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

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

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

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

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

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

3308
  return code;
817,656✔
3309
}
3310
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
151,032✔
3311
  int32_t code = 0;
151,032✔
3312

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

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

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

3329
  pColData->flag |= HAS_NONE;
8,006,533✔
3330
  pColData->numOfNone++;
8,006,533✔
3331

3332
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
8,005,816✔
3333
  code = tRealloc(&pColData->pBitMap, nBit);
8,006,533✔
3334
  if (code) return code;
8,004,382✔
3335

3336
  memset(pColData->pBitMap, 255, nBit);
8,004,382✔
3337
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
8,005,099✔
3338

3339
  return tColDataPutValue(pColData, NULL, 0);
8,005,882✔
3340
}
3341
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
23,677,082✔
3342
  int32_t code = 0;
24,326,312✔
3343

3344
  pColData->flag |= HAS_NULL;
24,326,312✔
3345
  pColData->numOfNull++;
24,329,194✔
3346

3347
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
24,330,164✔
3348
  code = tRealloc(&pColData->pBitMap, nBit);
24,330,164✔
3349
  if (code) return code;
24,328,653✔
3350

3351
  memset(pColData->pBitMap, 255, nBit);
24,328,653✔
3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
24,328,216✔
3353

3354
  return tColDataPutValue(pColData, NULL, 0);
24,330,310✔
3355
}
3356
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
358,907,718✔
3357
  int32_t code = 0;
358,908,379✔
3358

3359
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
358,908,379✔
3360
  if (code) return code;
358,894,171✔
3361

3362
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
358,894,171✔
3363
  pColData->numOfValue++;
358,917,729✔
3364

3365
  return tColDataPutValue(pColData, pData, nData);
358,922,379✔
3366
}
3367
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
901,265,110✔
3368
  int32_t code = 0;
901,265,110✔
3369

3370
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
901,265,110✔
3371
  if (code) return code;
901,284,523✔
3372

3373
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
901,284,523✔
3374
  pColData->numOfNone++;
901,289,704✔
3375

3376
  return tColDataPutValue(pColData, NULL, 0);
901,289,122✔
3377
}
3378
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
104,719✔
3379
  int32_t code = 0;
104,719✔
3380

3381
  pColData->flag |= HAS_NULL;
104,719✔
3382
  pColData->numOfNull++;
104,719✔
3383

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

3388
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,535,648✔
3389
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2,429,627✔
3390
  }
3391
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
104,068✔
3392

3393
  tFree(pColData->pBitMap);
104,068✔
3394
  pColData->pBitMap = pBitMap;
104,068✔
3395

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

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

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

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

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

3418
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
190,141,138✔
3419
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
187,939,722✔
3420
  }
3421
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
2,201,416✔
3422

3423
  tFree(pColData->pBitMap);
2,201,416✔
3424
  pColData->pBitMap = pBitMap;
2,201,416✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
2,201,416✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,472,830,783✔
3429
  int32_t code = 0;
1,474,198,445✔
3430

3431
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,474,198,445✔
3432
  if (code) return code;
1,474,288,405✔
3433
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,474,288,405✔
3434
  pColData->numOfNull++;
1,474,253,947✔
3435

3436
  return tColDataPutValue(pColData, NULL, 0);
1,474,354,818✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,356,100✔
3439
  int32_t code = 0;
3,475,884✔
3440

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

3446
  return tColDataPutValue(pColData, pData, nData);
3,475,884✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
346,068,781✔
3449
  int32_t code = 0;
346,068,781✔
3450

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

3456
  return tColDataPutValue(pColData, NULL, 0);
346,068,781✔
3457
}
3458
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
393,630✔
3459
  int32_t code = 0;
393,630✔
3460

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

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

3478
    //       VALUE                  NONE                     NULL
3479
};
3480

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3844
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,317✔
3845
  pColData->numOfNone--;
3,317✔
3846
  pColData->nVal--;
3,317✔
3847
  if (pColData->numOfNone) {
3,317✔
3848
    return tColDataAppendValue10(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
3,317✔
3851
    return tColDataAppendValue00(pColData, pData, nData);
3,317✔
3852
  }
3853
}
3854
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
26,982✔
3855
  pColData->numOfNone--;
26,982✔
3856
  pColData->nVal--;
26,982✔
3857
  if (pColData->numOfNone) {
26,982✔
3858
    return tColDataAppendValue12(pColData, pData, nData);
23,436✔
3859
  } else {
3860
    pColData->flag = 0;
3,546✔
3861
    return tColDataAppendValue02(pColData, pData, nData);
3,546✔
3862
  }
3863
  return 0;
3864
}
3865
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
96,569✔
3866
  if (forward) {
96,569✔
3867
    pColData->numOfNull--;
96,569✔
3868
    pColData->nVal--;
96,569✔
3869
    if (pColData->numOfNull) {
96,569✔
3870
      return tColDataAppendValue20(pColData, pData, nData);
449✔
3871
    } else {
3872
      pColData->flag = 0;
96,120✔
3873
      return tColDataAppendValue00(pColData, pData, nData);
96,120✔
3874
    }
3875
  }
3876
  return 0;
×
3877
}
3878
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3879
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3880
    pColData->numOfNone--;
×
3881
    pColData->nVal--;
×
3882
    if (pColData->numOfNone) {
×
3883
      return tColDataAppendValue30(pColData, pData, nData);
×
3884
    } else {
3885
      pColData->flag = HAS_NULL;
×
3886
      return tColDataAppendValue20(pColData, pData, nData);
×
3887
    }
3888
  } else if (forward) {  // NULL ==> VALUE
×
3889
    pColData->numOfNull--;
×
3890
    pColData->nVal--;
×
3891
    if (pColData->numOfNull) {
×
3892
      return tColDataAppendValue30(pColData, pData, nData);
×
3893
    } else {
3894
      pColData->flag = HAS_NONE;
×
3895
      return tColDataAppendValue10(pColData, pData, nData);
×
3896
    }
3897
  }
3898
  return 0;
×
3899
}
3900
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
339,924✔
3901
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
339,924✔
3902
    pColData->numOfNone--;
339,924✔
3903
    pColData->numOfNull++;
339,924✔
3904
    if (pColData->numOfNone) {
339,924✔
3905
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
328,104✔
3906
    } else {
3907
      pColData->flag = HAS_NULL;
11,820✔
3908
    }
3909
  }
3910
  return 0;
339,924✔
3911
}
3912
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3913
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3914
    pColData->nVal--;
2,147,483,647✔
3915
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3916
      pColData->nData = pColData->aOffset[pColData->nVal];
1,874,772,001✔
3917
    } else {
3918
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3919
    }
3920
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3921
  }
UNCOV
3922
  return 0;
×
3923
}
3924
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
643,778✔
3925
  if (forward) {  // VALUE ==> NULL
643,778✔
3926
    pColData->numOfValue--;
643,778✔
3927
    pColData->nVal--;
643,778✔
3928
    if (pColData->numOfValue) {
643,778✔
3929
      if (IS_VAR_DATA_TYPE(pColData->type)) {
625,203✔
3930
        pColData->nData = pColData->aOffset[pColData->nVal];
182,280✔
3931
      } else {
3932
        pColData->nData -= TYPE_BYTES[pColData->type];
442,923✔
3933
      }
3934
      return tColDataAppendValue42(pColData, pData, nData);
625,203✔
3935
    } else {
3936
      pColData->flag = 0;
18,575✔
3937
      pColData->nData = 0;
18,575✔
3938
      return tColDataAppendValue02(pColData, pData, nData);
18,575✔
3939
    }
3940
  }
3941
  return 0;
×
3942
}
3943
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,660,680✔
3944
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
2,660,680✔
3945
    pColData->numOfNone--;
130,200✔
3946
    pColData->nVal--;
130,200✔
3947
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
130,200✔
3948
      pColData->nData -= TYPE_BYTES[pColData->type];
130,200✔
3949
    }
3950
    if (pColData->numOfNone) {
130,200✔
3951
      return tColDataAppendValue50(pColData, pData, nData);
×
3952
    } else {
3953
      pColData->flag = HAS_VALUE;
130,200✔
3954
      return tColDataAppendValue40(pColData, pData, nData);
130,200✔
3955
    }
3956
  } else if (forward) {  // VALUE ==> VALUE
2,530,480✔
3957
    pColData->nVal--;
2,530,480✔
3958
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,530,480✔
3959
      pColData->nData = pColData->aOffset[pColData->nVal];
361,368✔
3960
    } else {
3961
      pColData->nData -= TYPE_BYTES[pColData->type];
2,169,112✔
3962
    }
3963
    return tColDataPutValue(pColData, pData, nData);
2,530,480✔
3964
  }
3965
  return 0;
×
3966
}
3967
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
24,027✔
3968
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
24,027✔
3969
    pColData->numOfNone--;
24,027✔
3970
    pColData->nVal--;
24,027✔
3971
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
24,027✔
3972
      pColData->nData -= TYPE_BYTES[pColData->type];
16,215✔
3973
    }
3974
    if (pColData->numOfNone) {
24,027✔
3975
      return tColDataAppendValue52(pColData, pData, nData);
×
3976
    } else {
3977
      pColData->flag = HAS_VALUE;
24,027✔
3978
      return tColDataAppendValue42(pColData, pData, nData);
24,027✔
3979
    }
3980
  } else if (forward) {  // VALUE ==> NULL
×
3981
    pColData->numOfValue--;
×
3982
    pColData->nVal--;
×
3983
    if (pColData->numOfValue) {
×
3984
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3985
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3986
      } else {
3987
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3988
      }
3989
      return tColDataAppendValue52(pColData, pData, nData);
×
3990
    } else {
3991
      pColData->flag = HAS_NONE;
×
3992
      pColData->nData = 0;
×
3993
      return tColDataAppendValue12(pColData, pData, nData);
×
3994
    }
3995
  }
3996
  return 0;
×
3997
}
3998
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,663,696✔
3999
  if (forward) {
3,663,696✔
4000
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,663,696✔
4001
      pColData->numOfNull--;
2,352,420✔
4002
      pColData->nVal--;
2,352,420✔
4003
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
2,352,420✔
4004
        pColData->nData -= TYPE_BYTES[pColData->type];
1,946,074✔
4005
      }
4006
      if (pColData->numOfNull) {
2,351,769✔
4007
        return tColDataAppendValue60(pColData, pData, nData);
2,110✔
4008
      } else {
4009
        pColData->flag = HAS_VALUE;
2,349,005✔
4010
        return tColDataAppendValue40(pColData, pData, nData);
2,349,659✔
4011
      }
4012
    } else {  // VALUE ==> VALUE
4013
      pColData->nVal--;
1,311,276✔
4014
      if (IS_VAR_DATA_TYPE(pColData->type)) {
1,311,276✔
4015
        pColData->nData = pColData->aOffset[pColData->nVal];
123,945✔
4016
      } else {
4017
        pColData->nData -= TYPE_BYTES[pColData->type];
1,187,331✔
4018
      }
4019
      return tColDataPutValue(pColData, pData, nData);
1,311,276✔
4020
    }
4021
  }
4022
  return 0;
×
4023
}
4024
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,569,061✔
4025
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,569,061✔
4026
    pColData->numOfValue--;
978,274✔
4027
    pColData->nVal--;
978,274✔
4028
    if (pColData->numOfValue) {
978,274✔
4029
      if (IS_VAR_DATA_TYPE(pColData->type)) {
978,274✔
4030
        pColData->nData = pColData->aOffset[pColData->nVal];
104,814✔
4031
      } else {
4032
        pColData->nData -= TYPE_BYTES[pColData->type];
873,460✔
4033
      }
4034
      return tColDataAppendValue62(pColData, pData, nData);
978,274✔
4035
    } else {
4036
      pColData->flag = HAS_NULL;
×
4037
      pColData->nData = 0;
×
4038
      return tColDataAppendValue20(pColData, pData, nData);
×
4039
    }
4040
  }
4041
  return 0;
590,787✔
4042
}
4043
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
330,229✔
4044
  int32_t code = 0;
330,229✔
4045

4046
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
330,229✔
4047
  if (bv == 0) {  // NONE ==> VALUE
330,229✔
4048
    pColData->numOfNone--;
78,120✔
4049
    pColData->nVal--;
78,120✔
4050
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
78,120✔
4051
      pColData->nData -= TYPE_BYTES[pColData->type];
78,120✔
4052
    }
4053
    if (pColData->numOfNone) {
78,120✔
4054
      return tColDataAppendValue70(pColData, pData, nData);
78,120✔
4055
    } else {
4056
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4057
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
×
4058
      }
4059
      pColData->flag = (HAS_VALUE | HAS_NULL);
×
4060
      return tColDataAppendValue60(pColData, pData, nData);
×
4061
    }
4062
  } else if (bv == 1) {  // NULL ==> VALUE
252,109✔
4063
    if (forward) {
42,325✔
4064
      pColData->numOfNull--;
42,325✔
4065
      pColData->nVal--;
42,325✔
4066
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
42,325✔
4067
        pColData->nData -= TYPE_BYTES[pColData->type];
42,325✔
4068
      }
4069
      if (pColData->numOfNull) {
42,325✔
4070
        return tColDataAppendValue70(pColData, pData, nData);
41,664✔
4071
      } else {
4072
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
1,983✔
4073
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,322✔
4074
        }
4075
        pColData->flag = (HAS_VALUE | HAS_NONE);
661✔
4076
        return tColDataAppendValue50(pColData, pData, nData);
661✔
4077
      }
4078
    }
4079
  } else if (bv == 2) {  // VALUE ==> VALUE
209,784✔
4080
    if (forward) {
209,784✔
4081
      pColData->nVal--;
209,784✔
4082
      if (IS_VAR_DATA_TYPE(pColData->type)) {
209,784✔
4083
        pColData->nData = pColData->aOffset[pColData->nVal];
52,080✔
4084
      } else {
4085
        pColData->nData -= TYPE_BYTES[pColData->type];
157,704✔
4086
      }
4087
      return tColDataPutValue(pColData, pData, nData);
209,784✔
4088
    }
4089
  } else {
4090
    return TSDB_CODE_INVALID_PARA;
×
4091
  }
4092
  return 0;
×
4093
}
4094
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
389,388✔
4095
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
389,388✔
4096
  if (bv == 0) {  // NONE ==> NULL
389,388✔
4097
    pColData->numOfNone--;
389,388✔
4098
    pColData->nVal--;
389,388✔
4099
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
389,388✔
4100
      pColData->nData -= TYPE_BYTES[pColData->type];
241,677✔
4101
    }
4102
    if (pColData->numOfNone) {
389,388✔
4103
      return tColDataAppendValue72(pColData, pData, nData);
×
4104
    } else {
4105
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
12,247,752✔
4106
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
11,858,364✔
4107
      }
4108
      pColData->flag = (HAS_VALUE | HAS_NULL);
389,388✔
4109
      return tColDataAppendValue62(pColData, pData, nData);
389,388✔
4110
    }
4111
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4112
    pColData->numOfValue--;
×
4113
    pColData->nVal--;
×
4114
    if (pColData->numOfValue) {
×
4115
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4116
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4117
      } else {
4118
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4119
      }
4120
      return tColDataAppendValue72(pColData, pData, nData);
×
4121
    } else {
4122
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4123
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4124
      }
4125
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4126
      pColData->nData = 0;
×
4127
      return tColDataAppendValue32(pColData, pData, nData);
×
4128
    }
4129
  }
4130
  return 0;
×
4131
}
4132
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,001,159✔
4133
  return 0;
3,001,159✔
4134
}
4135
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4136
    {NULL, NULL, NULL},                                                     // 0
4137
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4138
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4139
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4140
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4141
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4142
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4143
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4144

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

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

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

4157
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
1,170,255,820✔
4158
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,170,255,820✔
4159
}
1,170,727,504✔
4160
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,763,084,404✔
4161
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,763,084,404✔
4162
}
1,763,073,096✔
4163
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
6,590,233✔
4164
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4165
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
6,590,233✔
4166
    case 0:
3,286,979✔
4167
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,286,979✔
4168
      break;
3,285,677✔
4169
    case 1:
3,308,462✔
4170
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
3,308,462✔
4171
      break;
3,308,462✔
4172
    default:
×
4173
      break;
×
4174
  }
4175
}
6,594,139✔
4176
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4177
  SValue value = {.type = pColData->type};
2,147,483,647✔
4178
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4179
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4180
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4181
    } else {
4182
      value.nData = pColData->nData - pColData->aOffset[iVal];
228,020,057✔
4183
    }
4184
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4185
  } else {
4186
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4187
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4188
  }
4189
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4190
}
2,147,483,647✔
4191
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
997,345,200✔
4192
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4193
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
997,345,200✔
4194
    case 0:
468,832,040✔
4195
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
468,832,040✔
4196
      break;
468,711,514✔
4197
    case 1:
529,207,250✔
4198
      tColDataGetValue4(pColData, iVal, pColVal);
4199
      break;
528,861,564✔
4200
    default:
×
4201
      break;
×
4202
  }
4203
}
997,573,078✔
4204
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4205
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4206
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4207
    case 0:
2,147,483,647✔
4208
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,147,483,647✔
4209
      break;
2,147,483,647✔
4210
    case 1:
2,147,483,647✔
4211
      tColDataGetValue4(pColData, iVal, pColVal);
4212
      break;
2,147,483,647✔
4213
    default:
×
4214
      break;
×
4215
  }
4216
}
2,147,483,647✔
4217
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
22,682,814✔
4218
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4219
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
22,682,814✔
4220
    case 0:
4,148,613✔
4221
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
4,148,613✔
4222
      break;
4,147,311✔
4223
    case 1:
4,739,817✔
4224
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
4,739,817✔
4225
      break;
4,739,817✔
4226
    case 2:
13,776,156✔
4227
      tColDataGetValue4(pColData, iVal, pColVal);
4228
      break;
13,800,243✔
4229
    default:
×
4230
      break;
×
4231
  }
4232
}
22,687,371✔
4233
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4234
    NULL,               // 0
4235
    tColDataGetValue1,  // HAS_NONE
4236
    tColDataGetValue2,  // HAS_NULL
4237
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4238
    tColDataGetValue4,  // HAS_VALUE
4239
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4240
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4241
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4242
};
4243
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4244
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4245
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4246
    return TSDB_CODE_INVALID_PARA;
318✔
4247
  }
4248
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4249
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4250
}
4251

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

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

4276
  *pColData = *pColDataFrom;
15,864,627✔
4277

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

4303
  // offset
4304
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
15,864,760✔
4305
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
87,327✔
4306
    if (pColData->aOffset == NULL) {
87,327✔
4307
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4308
      goto _exit;
×
4309
    }
4310
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
87,648✔
4311
  } else {
4312
    pColData->aOffset = NULL;
15,778,654✔
4313
  }
4314

4315
  // value
4316
  if (pColData->nData) {
15,866,021✔
4317
    pColData->pData = xMalloc(arg, pColData->nData);
15,821,921✔
4318
    if (pColData->pData == NULL) {
15,822,968✔
4319
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4320
      goto _exit;
×
4321
    }
4322

4323
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
15,821,856✔
4324
  } else {
4325
    pColData->pData = NULL;
43,057✔
4326
  }
4327

4328
_exit:
15,865,329✔
4329
  return code;
15,865,329✔
4330
}
4331

4332
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
602,201,801✔
4333
  int32_t code;
4334
  SBuffer local;
602,197,211✔
4335

4336
  if (!(colData->nVal > 0)) {
602,267,473✔
4337
    return TSDB_CODE_INVALID_PARA;
×
4338
  }
4339

4340
  (*info) = (SColDataCompressInfo){
602,361,596✔
4341
      .cmprAlg = info->cmprAlg,
602,322,286✔
4342
      .columnFlag = colData->cflag,
602,321,054✔
4343
      .flag = colData->flag,
602,333,522✔
4344
      .dataType = colData->type,
602,360,102✔
4345
      .columnId = colData->cid,
602,386,011✔
4346
      .numOfData = colData->nVal,
602,323,185✔
4347
  };
4348

4349
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
602,357,249✔
4350
    return 0;
19,708,772✔
4351
  }
4352

4353
  tBufferInit(&local);
4354
  if (assist == NULL) {
582,674,296✔
4355
    assist = &local;
×
4356
  }
4357

4358
  // bitmap
4359
  if (colData->flag != HAS_VALUE) {
582,674,296✔
4360
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
31,171,910✔
4361
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,890,046✔
4362
    } else {
4363
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
29,279,446✔
4364
    }
4365

4366
    SCompressInfo cinfo = {
31,170,317✔
4367
        .dataType = TSDB_DATA_TYPE_TINYINT,
4368
        .cmprAlg = info->cmprAlg,
31,170,968✔
4369
        .originalSize = info->bitmapOriginalSize,
31,171,355✔
4370
    };
4371

4372
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
31,171,427✔
4373
    if (code) {
31,169,075✔
4374
      tBufferDestroy(&local);
4375
      return code;
×
4376
    }
4377

4378
    info->bitmapCompressedSize = cinfo.compressedSize;
31,169,075✔
4379
  }
4380

4381
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
582,664,533✔
4382
    tBufferDestroy(&local);
4383
    return 0;
101,322✔
4384
  }
4385

4386
  // offset
4387
  if (IS_VAR_DATA_TYPE(colData->type)) {
582,379,521✔
4388
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
92,710,308✔
4389

4390
    SCompressInfo cinfo = {
92,757,585✔
4391
        .dataType = TSDB_DATA_TYPE_INT,
4392
        .cmprAlg = info->cmprAlg,
92,764,121✔
4393
        .originalSize = info->offsetOriginalSize,
92,763,248✔
4394
    };
4395

4396
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
92,759,580✔
4397
    if (code) {
92,759,081✔
4398
      tBufferDestroy(&local);
4399
      return code;
×
4400
    }
4401

4402
    info->offsetCompressedSize = cinfo.compressedSize;
92,759,081✔
4403
  }
4404

4405
  // data
4406
  if (colData->nData > 0) {
582,580,376✔
4407
    info->dataOriginalSize = colData->nData;
582,523,401✔
4408

4409
    SCompressInfo cinfo = {
582,535,319✔
4410
        .dataType = colData->type,
582,540,303✔
4411
        .cmprAlg = info->cmprAlg,
582,526,050✔
4412
        .originalSize = info->dataOriginalSize,
582,517,640✔
4413
    };
4414

4415
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
582,503,424✔
4416
    if (code) {
582,488,973✔
4417
      tBufferDestroy(&local);
4418
      return code;
×
4419
    }
4420

4421
    info->dataCompressedSize = cinfo.compressedSize;
582,488,973✔
4422
  }
4423

4424
  tBufferDestroy(&local);
4425
  return 0;
582,458,887✔
4426
}
4427

4428
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
1,005,562,065✔
4429
  int32_t  code;
4430
  SBuffer  local;
1,005,558,415✔
4431
  uint8_t *data = (uint8_t *)input;
1,005,893,294✔
4432

4433
  tBufferInit(&local);
4434
  if (assist == NULL) {
1,005,927,742✔
4435
    assist = &local;
×
4436
  }
4437

4438
  tColDataClear(colData);
1,005,927,742✔
4439
  colData->cid = info->columnId;
1,006,007,050✔
4440
  colData->type = info->dataType;
1,006,045,416✔
4441
  colData->cflag = info->columnFlag;
1,005,788,136✔
4442
  colData->nVal = info->numOfData;
1,005,872,103✔
4443
  colData->flag = info->flag;
1,005,735,119✔
4444

4445
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
1,005,660,933✔
4446
    goto _exit;
61,311,198✔
4447
  }
4448

4449
  // bitmap
4450
  if (info->bitmapOriginalSize > 0) {
944,464,459✔
4451
    SCompressInfo cinfo = {
106,658,548✔
4452
        .dataType = TSDB_DATA_TYPE_TINYINT,
4453
        .cmprAlg = info->cmprAlg,
106,675,016✔
4454
        .originalSize = info->bitmapOriginalSize,
106,692,283✔
4455
        .compressedSize = info->bitmapCompressedSize,
106,664,385✔
4456
    };
4457

4458
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
106,649,163✔
4459
    if (code) {
106,663,474✔
4460
      tBufferDestroy(&local);
4461
      return code;
×
4462
    }
4463

4464
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
106,663,474✔
4465
    if (code) {
106,640,214✔
4466
      tBufferDestroy(&local);
4467
      return code;
×
4468
    }
4469

4470
    data += cinfo.compressedSize;
106,640,214✔
4471
  }
4472

4473
  if (info->flag == (HAS_NONE | HAS_NULL)) {
944,477,132✔
4474
    goto _exit;
199,857✔
4475
  }
4476

4477
  // offset
4478
  if (info->offsetOriginalSize > 0) {
944,414,224✔
4479
    SCompressInfo cinfo = {
201,724,500✔
4480
        .cmprAlg = info->cmprAlg,
201,724,590✔
4481
        .dataType = TSDB_DATA_TYPE_INT,
4482
        .originalSize = info->offsetOriginalSize,
201,680,114✔
4483
        .compressedSize = info->offsetCompressedSize,
201,643,470✔
4484
    };
4485

4486
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
201,644,330✔
4487
    if (code) {
201,634,371✔
4488
      tBufferDestroy(&local);
4489
      return code;
×
4490
    }
4491

4492
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
201,634,371✔
4493
    if (code) {
201,600,328✔
4494
      tBufferDestroy(&local);
4495
      return code;
×
4496
    }
4497

4498
    data += cinfo.compressedSize;
201,600,328✔
4499
  }
4500

4501
  // data
4502
  if (info->dataOriginalSize > 0) {
944,367,515✔
4503
    colData->nData = info->dataOriginalSize;
944,096,342✔
4504

4505
    SCompressInfo cinfo = {
944,049,019✔
4506
        .cmprAlg = info->cmprAlg,
1,888,390,867✔
4507
        .dataType = colData->type,
943,963,397✔
4508
        .originalSize = info->dataOriginalSize,
944,169,809✔
4509
        .compressedSize = info->dataCompressedSize,
944,118,474✔
4510
    };
4511

4512
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
944,132,096✔
4513
    if (code) {
944,088,669✔
4514
      tBufferDestroy(&local);
4515
      return code;
×
4516
    }
4517

4518
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
944,088,669✔
4519
    if (code) {
944,175,032✔
4520
      tBufferDestroy(&local);
4521
      return code;
×
4522
    }
4523

4524
    data += cinfo.compressedSize;
944,175,032✔
4525
  }
4526

4527
_exit:
1,005,995,069✔
4528
  switch (colData->flag) {
1,006,007,485✔
4529
    case HAS_NONE:
22,378,028✔
4530
      colData->numOfNone = colData->nVal;
22,378,028✔
4531
      break;
22,370,294✔
4532
    case HAS_NULL:
38,839,610✔
4533
      colData->numOfNull = colData->nVal;
38,839,610✔
4534
      break;
38,946,224✔
4535
    case HAS_VALUE:
837,660,557✔
4536
      colData->numOfValue = colData->nVal;
837,660,557✔
4537
      break;
837,760,473✔
4538
    default:
106,664,362✔
4539
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4540
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4541
        if (bitValue == 0) {
2,147,483,647✔
4542
          colData->numOfNone++;
484,554,968✔
4543
        } else if (bitValue == 1) {
2,147,483,647✔
4544
          colData->numOfNull++;
2,147,483,647✔
4545
        } else {
4546
          colData->numOfValue++;
2,147,483,647✔
4547
        }
4548
      }
4549
  }
4550
  tBufferDestroy(&local);
4551
  return 0;
1,005,464,433✔
4552
}
4553

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

4568
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
236,866✔
4569
    if (!IS_STR_DATA_BLOB(type)) {
45,459✔
4570
      for (int32_t i = 0; i < nRows; ++i) {
120,592✔
4571
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
75,133✔
4572
        if (offset == -1) {
75,133✔
4573
          if (pColData->cflag & COL_IS_KEY) {
×
4574
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4575
            goto _exit;
×
4576
          }
4577
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4578
            goto _exit;
×
4579
          }
4580
        } else {
4581
          if (varDataTLen(data + offset) > bytes || (type == TSDB_DATA_TYPE_NCHAR && varDataLen(data + offset) % TSDB_NCHAR_SIZE != 0)) {
75,133✔
4582
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4583
                   bytes);
4584
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4585
            goto _exit;
×
4586
          }
4587
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
150,266✔
4588
                                                                        varDataLen(data + offset));
75,133✔
4589
        }
4590
      }
4591
    } else {
4592
      for (int32_t i = 0; i < nRows; ++i) {
×
4593
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4594
        if (offset == -1) {
×
4595
          if (pColData->cflag & COL_IS_KEY) {
×
4596
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4597
            goto _exit;
×
4598
          }
4599
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4600
            goto _exit;
×
4601
          }
4602
        } else {
4603
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4604
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4605
                   (int)blobDataTLen(data + offset), bytes);
4606
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4607
            goto _exit;
×
4608
          }
4609
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4610
                                                                        blobDataLen(data + offset));
×
4611
        }
4612
      }
4613
    }
4614
  } else {  // fixed-length data type
4615
    bool allValue = true;
191,407✔
4616
    bool allNull = true;
191,407✔
4617
    for (int32_t i = 0; i < nRows; ++i) {
519,797✔
4618
      if (!BMIsNull(lengthOrbitmap, i)) {
328,390✔
4619
        allNull = false;
249,119✔
4620
      } else {
4621
        allValue = false;
79,271✔
4622
      }
4623
    }
4624
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
191,407✔
4625
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4626
      goto _exit;
×
4627
    }
4628

4629
    if (allValue) {
191,407✔
4630
      // optimize (todo)
4631
      for (int32_t i = 0; i < nRows; ++i) {
400,406✔
4632
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
244,464✔
4633
      }
4634
    } else if (allNull) {
35,465✔
4635
      // optimize (todo)
4636
      for (int32_t i = 0; i < nRows; ++i) {
95,391✔
4637
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
63,257✔
4638
        if (code) goto _exit;
63,257✔
4639
      }
4640
    } else {
4641
      for (int32_t i = 0; i < nRows; ++i) {
24,000✔
4642
        if (BMIsNull(lengthOrbitmap, i)) {
20,669✔
4643
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
16,014✔
4644
          if (code) goto _exit;
16,014✔
4645
        } else {
4646
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
4,655✔
4647
        }
4648
      }
4649
    }
4650
  }
4651

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

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

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

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

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

4761
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
22,843,156✔
4762
    if (!(pColData->type == pBind->buffer_type)) {
22,852,496✔
4763
      return TSDB_CODE_INVALID_PARA;
×
4764
    }
4765
  }
4766

4767
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
22,857,864✔
4768
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
27,151✔
4769
      code = igeos();
1,074✔
4770
      if (code) {
1,074✔
4771
        return code;
×
4772
      }
4773
    }
4774
    for (int32_t i = 0; i < pBind->num; ++i) {
1,455,893✔
4775
      if (pBind->is_null && pBind->is_null[i]) {
1,436,682✔
4776
        if (pColData->cflag & COL_IS_KEY) {
674,786✔
4777
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,446✔
4778
          goto _exit;
1,446✔
4779
        }
4780
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
673,340✔
4781
        if (code) goto _exit;
673,340✔
4782
      } else if (pBind->length[i] > buffMaxLen) {
761,896✔
4783
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4784
      } else {
4785
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
761,896✔
4786
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
13,081✔
4787
          if (code) {
13,081✔
4788
            uError("stmt col[%d] bind geometry wrong format", i);
100✔
4789
            goto _exit;
100✔
4790
          }
4791
        }
4792
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,509,882✔
4793
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
761,796✔
4794
      }
4795
    }
4796
  } else {  // fixed-length data type
4797
    bool allValue;
4798
    bool allNull;
4799
    if (pBind->is_null) {
22,829,553✔
4800
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
15,243,368✔
4801
      allNull = (same && pBind->is_null[0] != 0);
15,241,512✔
4802
      allValue = (same && pBind->is_null[0] == 0);
15,242,290✔
4803
    } else {
4804
      allNull = false;
7,579,768✔
4805
      allValue = true;
7,579,768✔
4806
    }
4807

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

4813
    if (allValue) {
22,817,235✔
4814
      // optimize (todo)
4815
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4816
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4817
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4818
      }
4819
    } else if (allNull) {
26,287✔
4820
      // optimize (todo)
4821
      for (int32_t i = 0; i < pBind->num; ++i) {
1,310✔
4822
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
655✔
4823
        if (code) goto _exit;
655✔
4824
      }
4825
    } else {
4826
      for (int32_t i = 0; i < pBind->num; ++i) {
8,770,056✔
4827
        if (pBind->is_null[i]) {
8,744,424✔
4828
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,865,897✔
4829
          if (code) goto _exit;
3,865,897✔
4830
        } else {
4831
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,757,054✔
4832
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,878,527✔
4833
        }
4834
      }
4835
    }
4836
  }
4837

4838
_exit:
12,771,685✔
4839
  return code;
22,860,153✔
4840
}
4841

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

4845
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,138,529✔
4846
    if (!(pColData->type == pBind->buffer_type)) {
1,133,578✔
4847
      return TSDB_CODE_INVALID_PARA;
×
4848
    }
4849
  }
4850

4851
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
1,198,528✔
4852
    uint8_t *buf = pBind->buffer;
58,149✔
4853
    for (int32_t i = 0; i < pBind->num; ++i) {
121,709,737✔
4854
      if (pBind->is_null && pBind->is_null[i]) {
121,735,798✔
4855
        if (pColData->cflag & COL_IS_KEY) {
7,730,381✔
4856
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4857
          goto _exit;
×
4858
        }
4859
        if (pBind->is_null[i] == 1) {
7,731,337✔
4860
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
7,731,675✔
4861
          if (code) goto _exit;
7,731,240✔
4862
        } else {
4863
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4864
          if (code) goto _exit;
37✔
4865
        }
4866
      } else if (pBind->length[i] > buffMaxLen) {
114,093,016✔
4867
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4868
      } else {
4869
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
114,501,922✔
4870
        buf += pBind->length[i];
113,852,722✔
4871
      }
4872
    }
4873
  } else {  // fixed-length data type
4874
    bool allValue;
4875
    bool allNull;
4876
    bool allNone;
4877
    if (pBind->is_null) {
1,079,961✔
4878
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
1,056,104✔
4879
      allNull = (same && pBind->is_null[0] == 1);
1,056,099✔
4880
      allNone = (same && pBind->is_null[0] > 1);
1,055,673✔
4881
      allValue = (same && pBind->is_null[0] == 0);
1,055,485✔
4882
    } else {
4883
      allNull = false;
23,920✔
4884
      allNone = false;
23,920✔
4885
      allValue = true;
23,920✔
4886
    }
4887

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

4893
    uint8_t *buf = pBind->buffer;
1,079,295✔
4894

4895
    if (allValue) {
1,079,453✔
4896
      // optimize (todo)
4897
      for (int32_t i = 0; i < pBind->num; ++i) {
683,351,881✔
4898
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
682,569,181✔
4899
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
682,786,355✔
4900
          *val = 1;
×
4901
        }
4902
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
682,834,870✔
4903
      }
4904
    } else if (allNull) {
47,166✔
4905
      // optimize (todo)
4906
      for (int32_t i = 0; i < pBind->num; ++i) {
8,886,799✔
4907
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
8,881,355✔
4908
        if (code) goto _exit;
8,881,355✔
4909
      }
4910
    } else if (allNone) {
41,722✔
4911
      // optimize (todo)
4912
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4913
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4914
        if (code) goto _exit;
×
4915
      }
4916
    } else {
4917
      for (int32_t i = 0; i < pBind->num; ++i) {
407,321,331✔
4918
        if (pBind->is_null[i]) {
413,375,099✔
4919
          if (pBind->is_null[i] == 1) {
24,675,200✔
4920
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
24,678,482✔
4921
            if (code) goto _exit;
24,661,295✔
4922
          } else {
4923
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4924
            if (code) goto _exit;
×
4925
          }
4926
        } else {
4927
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
391,851,329✔
4928
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
391,456,194✔
4929
            *val = 1;
×
4930
          }
4931

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

4938
_exit:
84,073✔
4939
  return code;
1,122,880✔
4940
}
4941

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

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

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

4956
  code = igeos();
3,685✔
4957
  if (code) {
3,685✔
4958
    return code;
×
4959
  }
4960

4961
  uint8_t *buf = pBind->buffer;
3,685✔
4962
  for (int32_t i = 0; i < pBind->num; ++i) {
1,769,703✔
4963
    if (pBind->is_null && pBind->is_null[i]) {
1,770,257✔
4964
      if (pColData->cflag & COL_IS_KEY) {
327✔
4965
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4966
        goto _exit;
×
4967
      }
4968
      if (pBind->is_null[i] == 1) {
327✔
4969
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
327✔
4970
        if (code) goto _exit;
327✔
4971
      } else {
4972
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4973
        if (code) goto _exit;
×
4974
      }
4975
    } else if (pBind->length[i] > buffMaxLen) {
1,772,046✔
4976
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4977
    } else {
4978
      code = cgeos(buf, pBind->length[i]);
1,771,566✔
4979
      if (code) {
1,721,101✔
4980
        uError("stmt2 col[%d] bind geometry with wrong format", i);
×
4981
        goto _exit;
×
4982
      }
4983
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
1,721,101✔
4984
      buf += pBind->length[i];
1,761,318✔
4985
    }
4986
  }
4987

4988
_exit:
3,685✔
4989
  return code;
3,685✔
4990
}
4991

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

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

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

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

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

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

5043
  uint8_t *buf = pBind->buffer;
4,119✔
5044
  for (int32_t i = 0; i < pBind->num; ++i) {
29,423,329✔
5045
    if (pBind->is_null && pBind->is_null[i]) {
29,459,785✔
5046
      if (pColData->cflag & COL_IS_KEY) {
1,475,475✔
5047
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5048
        goto _exit;
×
5049
      }
5050
      if (pBind->is_null[i] == 1) {
1,475,499✔
5051
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,475,282✔
5052
        if (code) goto _exit;
1,475,146✔
5053
      } else {
5054
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
200✔
5055
        if (code) goto _exit;
214✔
5056
      }
5057
    } else {
5058
      if (pColData->type == TSDB_DATA_TYPE_DECIMAL64) {
27,965,527✔
5059
        Decimal64 dec = {0};
14,011,637✔
5060
        int32_t   code = decimal64FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
14,008,590✔
5061
        buf += pBind->length[i];
13,934,048✔
5062
        if (TSDB_CODE_SUCCESS != code) {
13,958,416✔
5063
          return code;
×
5064
        }
5065
        int64_t tmp = DECIMAL64_GET_VALUE(&dec);
13,958,416✔
5066
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)&tmp,
13,965,029✔
5067
                                                                      TYPE_BYTES[pColData->type]);
13,966,603✔
5068
      } else if (pColData->type == TSDB_DATA_TYPE_DECIMAL) {
14,054,968✔
5069
        Decimal128 dec = {0};
14,047,911✔
5070
        int32_t    code = decimal128FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
14,050,619✔
5071
        buf += pBind->length[i];
14,010,607✔
5072
        if (TSDB_CODE_SUCCESS != code) {
14,017,869✔
5073
          return code;
×
5074
        }
5075
        uint8_t *pV = taosMemCalloc(1, sizeof(Decimal128));
14,017,869✔
5076
        if (!pV) return terrno;
14,057,400✔
5077
        memcpy(pV, &dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
14,057,400✔
5078
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pV, TYPE_BYTES[pColData->type]);
14,060,382✔
5079
        taosMemoryFree(pV);
14,052,579✔
5080
      }
5081
    }
5082
  }
5083

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

5107
  int32_t code = 0;
1,067,343✔
5108
  int32_t numOfRows = -1;
1,067,343✔
5109
  SArray *colValArray, *bufArray;
5110
  SColVal colVal;
361,546✔
5111
  int32_t numOfFixedValue = 0;
1,067,241✔
5112
  int32_t lino = 0;
1,067,241✔
5113
  bool    hasDecimal128 = false;
1,067,241✔
5114

5115
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,067,241✔
5116
    return terrno;
×
5117
  }
5118
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
1,066,923✔
5119
    taosArrayDestroy(colValArray);
12✔
5120
    return terrno;
×
5121
  }
5122
  for (int i = 0; i < numOfInfos; ++i) {
10,291,072✔
5123
    if (parsedCols) {
9,223,521✔
5124
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
14,400✔
5125
      if (pParsedVal) {
14,400✔
5126
        continue;
3,000✔
5127
      }
5128
    }
5129
    if (numOfRows == -1) {
9,220,521✔
5130
      numOfRows = infos[i].bind->num;
1,066,691✔
5131
    }
5132

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

5140
  SRowKey rowKey, lastRowKey;
361,712✔
5141
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
12,604,899✔
5142
    taosArrayClear(colValArray);
11,551,671✔
5143
    numOfFixedValue = 0;
11,550,494✔
5144

5145
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
66,096,070✔
5146
      if (parsedCols) {
54,323,158✔
5147
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
15,600✔
5148
        if (pParsedVal) {
15,600✔
5149
          numOfFixedValue++;
3,168✔
5150
          colVal = *pParsedVal;
3,168✔
5151

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

5160
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
54,349,624✔
5161
        if (infos[iInfo].bind->is_null[iRow] == 1) {
500✔
5162
          if (iInfo == 0) {
300✔
5163
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
100✔
5164
            TAOS_CHECK_GOTO(code, &lino, _exit);
100✔
5165
          }
5166
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
200✔
5167
        } else {
5168
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
200✔
5169
        }
5170
      } else {
5171
        SValue value = {
54,386,453✔
5172
            .type = infos[iInfo].type,
54,354,191✔
5173
        };
5174
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
54,426,935✔
5175
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
4,422,251✔
UNCOV
5176
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5177
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5178
            value.nData = length;
×
5179
            if (value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE) > TSDB_MAX_BLOB_LEN) {
×
5180
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5181
              uError("stmt2 bind col:%d, row:%d length:%d  greater than blob maximum length: %d", iInfo, iRow,
×
5182
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), TSDB_MAX_BLOB_LEN);
5183
              goto _exit;
100✔
5184
            }
5185
            value.pData = *data;
×
5186
            *data += length;
×
5187
          } else {
5188
            int32_t   length = infos[iInfo].bind->length[iRow];
4,549,960✔
5189
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
4,549,232✔
5190
            value.nData = length;
4,550,072✔
5191
            if (value.nData + VARSTR_HEADER_SIZE > infos[iInfo].bytes) {
4,550,072✔
5192
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5193
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum length: %d", iInfo, iRow,
×
5194
                     value.nData + (uint32_t)(VARSTR_HEADER_SIZE), infos[iInfo].bytes);
5195
              goto _exit;
×
5196
            }
5197
            value.pData = *data;
4,547,608✔
5198
            *data += length;
4,545,144✔
5199
          }
5200
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5201
        } else {
5202
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
50,163,824✔
5203
            if (!pSchemaExt) {
400✔
5204
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5205
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5206
              goto _exit;
×
5207
            }
5208
            uint8_t precision = 0, scale = 0;
400✔
5209
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
400✔
5210
            Decimal128 dec = {0};
400✔
5211
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
400✔
5212
            int32_t    length = infos[iInfo].bind->length[iRow];
400✔
5213
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
400✔
5214
            *data += length;
400✔
5215
            hasDecimal128 = true;
400✔
5216
            TAOS_CHECK_GOTO(code, &lino, _exit);
400✔
5217

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

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

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

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

5242
          } else {
5243
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
50,066,850✔
5244
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
50,060,733✔
5245
              *val = 1;
×
5246
            }
5247
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
50,060,733✔
5248
          }
5249
        }
5250
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
54,651,947✔
5251
      }
5252
      if (taosArrayPush(colValArray, &colVal) == NULL) {
54,541,976✔
5253
        code = terrno;
×
5254
        goto _exit;
×
5255
      }
5256
    }
5257

5258
    SRow *row;
2,529,538✔
5259

5260
    if (hasBlob == 0) {
11,665,909✔
5261
      SRowBuildScanInfo sinfo = {0};
11,527,516✔
5262
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
11,531,538✔
5263
      TAOS_CHECK_GOTO(code, &lino, _exit);
11,517,270✔
5264
    } else {
5265
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
141,813✔
5266
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
141,813✔
5267
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5268
    }
5269

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

5275
    // fix decimal memory leak
5276
    if (hasDecimal128) {
11,512,659✔
5277
      int32_t num = taosArrayGetSize(colValArray);
400✔
5278
      for (int32_t i = 0; i < num; ++i) {
1,600✔
5279
        SColVal *pCol = taosArrayGet(colValArray, i);
1,200✔
5280
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
1,200✔
5281
          taosMemoryFreeClear(pCol->value.pData);
400✔
5282
        }
5283
      }
5284
      hasDecimal128 = false;
400✔
5285
    }
5286

5287
    if (pOrdered && pDupTs) {
11,512,659✔
5288
      tRowGetKey(row, &rowKey);
23,036,495✔
5289
      if (iRow == 0) {
11,534,103✔
5290
        *pOrdered = true;
1,066,543✔
5291
        *pDupTs = false;
1,066,533✔
5292
      } else {
5293
        if (*pOrdered) {
10,467,560✔
5294
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
10,466,177✔
5295
          *pOrdered = (res >= 0);
10,466,177✔
5296
          if (!*pDupTs) {
10,464,447✔
5297
            *pDupTs = (res == 0);
10,461,406✔
5298
          }
5299
        }
5300
      }
5301
      lastRowKey = rowKey;
11,537,747✔
5302
    }
5303
  }
5304
_exit:
1,053,123✔
5305
  if (code != 0) {
1,053,428✔
5306
    if (hasDecimal128) {
200✔
5307
      int32_t num = taosArrayGetSize(colValArray);
×
5308
      for (int32_t i = 0; i < num; ++i) {
×
5309
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5310
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5311
          taosMemoryFreeClear(pCol->value.pData);
×
5312
        }
5313
      }
5314
    }
5315
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
200✔
5316
  }
5317
  taosArrayDestroy(colValArray);
1,053,428✔
5318
  taosArrayDestroy(bufArray);
1,066,955✔
5319
  return code;
1,067,908✔
5320
}
5321
/* build rows to `rowArray` from bind
5322
 * `infos` is the bind information array
5323
 * `numOfInfos` is the number of bind information
5324
 * `infoSorted` is whether the bind information is sorted by column id
5325
 * `pTSchema` is the schema of the table
5326
 * `rowArray` is the array to store the rows
5327
 * `pOrdered` is the pointer to store ordered
5328
 * `pDupTs` is the pointer to store duplicateTs
5329
 */
5330
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
100✔
5331
                                   const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray,
5332
                                   bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5333
  if (infos == NULL || numOfInfos <= 0 || pTSchema == NULL || numOfInfos > pTSchema->numOfCols || rowArray == NULL) {
100✔
5334
    return TSDB_CODE_INVALID_PARA;
×
5335
  }
5336
  int8_t hasBlob = schemaHasBlob(pTSchema);
100✔
5337
  if (!infoSorted) {
100✔
5338
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5339
  }
5340

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

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

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

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

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

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

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

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

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

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

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

5488
    SRow *row;
×
5489

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

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

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

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

5552
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
46,736,287✔
5553
  int32_t code = TSDB_CODE_SUCCESS;
46,736,287✔
5554

5555
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
46,736,287✔
5556
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,893,672✔
5557
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
5,300,612✔
5558
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
11,194,284✔
5559
    if (iToRow == 0) {
5,893,672✔
5560
      pToColData->aOffset[iToRow] = 0;
16,548✔
5561
    }
5562

5563
    if (iToRow < pToColData->nVal - 1) {
5,893,672✔
5564
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,879,475✔
5565
    }
5566

5567
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,893,672✔
5568
                 nData);
5569
  } else {
5570
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
40,842,615✔
5571
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
40,842,615✔
5572
  }
5573
  return code;
46,736,287✔
5574
}
5575

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

5581
  switch (pFromColData->flag) {
48,415,819✔
5582
    case HAS_NONE: {
×
5583
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5584
    } break;
×
5585
    case HAS_NULL: {
1,679,532✔
5586
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,679,532✔
5587
    } break;
1,679,532✔
5588
    case (HAS_NULL | HAS_NONE): {
×
5589
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5590
      if (0 == bit_val)
×
5591
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5592
      else
5593
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5594
    } break;
×
5595
    case HAS_VALUE: {
5,302,876✔
5596
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
5,302,876✔
5597
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
5,302,876✔
5598
    } break;
5,302,876✔
5599
    case (HAS_VALUE | HAS_NONE): {
×
5600
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5601
      if (0 == bit_val)
×
5602
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5603
      else
5604
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5605
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5606
    } break;
×
5607
    case (HAS_VALUE | HAS_NULL): {
41,433,411✔
5608
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
41,433,411✔
5609
      if (0 == bit_val)
41,433,411✔
5610
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
18,706,253✔
5611
      else
5612
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
22,727,158✔
5613
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
41,433,411✔
5614
    } break;
41,433,411✔
5615
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5616
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5617
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5618
    } break;
×
5619
    default:
×
5620
      return -1;
×
5621
  }
5622

5623
  return code;
48,415,819✔
5624
}
5625

5626
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
3,045,454✔
5627
                               int32_t nColData) {
5628
  int32_t code = TSDB_CODE_SUCCESS;
3,045,454✔
5629

5630
  for (int32_t i = 0; i < nColData; i++) {
51,461,273✔
5631
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
48,415,819✔
5632
    if (code != TSDB_CODE_SUCCESS) {
48,415,819✔
5633
      return code;
×
5634
    }
5635
  }
5636

5637
  return code;
3,045,454✔
5638
}
5639

5640
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
3,045,454✔
5641
  int32_t code = TSDB_CODE_SUCCESS;
3,045,454✔
5642

5643
  for (int32_t i = 0; i < nColData; i++) {
51,461,273✔
5644
    SColVal cv = {0};
48,415,819✔
5645
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
48,415,819✔
5646
    if (code != TSDB_CODE_SUCCESS) {
48,415,819✔
5647
      return code;
×
5648
    }
5649
    code = tColDataAppendValue(&aToColData[i], &cv);
48,415,819✔
5650
    if (code != TSDB_CODE_SUCCESS) {
48,415,819✔
5651
      return code;
×
5652
    }
5653
  }
5654

5655
  return code;
3,045,454✔
5656
}
5657

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

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

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

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

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

5689
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
343,066✔
5690
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
343,066✔
5691
  while (i <= mid && j <= end) {
1,866,841✔
5692
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,523,775✔
5693
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
10,922✔
5694
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
10,922✔
5695
    } else {
5696
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,512,853✔
5697
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,512,853✔
5698
    }
5699
  }
5700

5701
  while (i <= mid) {
1,856,502✔
5702
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,513,436✔
5703
  }
5704

5705
  while (j <= end) {
351,309✔
5706
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
8,243✔
5707
  }
5708

5709
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,388,520✔
5710
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
3,045,454✔
5711
  }
5712

5713
  if (aDstColData) {
343,066✔
5714
    for (int32_t i = 0; i < nColData; i++) {
5,735,512✔
5715
      tColDataDestroy(&aDstColData[i]);
5,392,446✔
5716
    }
5717
    taosMemoryFree(aDstColData);
343,066✔
5718
  }
5719

5720
  return TSDB_CODE_SUCCESS;
343,066✔
5721
}
5722

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

5727
  if (start >= end) {
688,954✔
5728
    return TSDB_CODE_SUCCESS;
345,888✔
5729
  }
5730

5731
  mid = (start + end) / 2;
343,066✔
5732

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

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

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

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

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

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

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

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

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

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

5775
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
1,113✔
5776
  SRowKey lastKey;
1,013✔
5777
  for (int32_t i = 0; i < numRows; i++) {
3,739✔
5778
    SRowKey key;
2,026✔
5779
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
2,626✔
5780

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

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

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

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

5825
int32_t tColDataSortMerge(SArray **arr) {
7,806,889✔
5826
  SArray   *colDataArr = *arr;
7,806,889✔
5827
  int32_t   nColData = TARRAY_SIZE(colDataArr);
7,810,132✔
5828
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
7,813,537✔
5829

5830
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
7,814,301✔
5831
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5832
  }
5833
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
7,810,764✔
5834
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5835
  }
5836
  if (!(aColData[0].flag == HAS_VALUE)) {
7,811,154✔
5837
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5838
  }
5839

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

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

5851
    int32_t c = tRowKeyCompare(&lastKey, &key);
2,147,483,647✔
5852
    if (c < 0) {
2,147,483,647✔
5853
      lastKey = key;
2,147,483,647✔
5854
      continue;
2,147,483,647✔
5855
    } else if (c > 0) {
6,424,840✔
5856
      doSort = 1;
2,822✔
5857
      break;
2,822✔
5858
    } else {
5859
      doMerge = 1;
6,422,018✔
5860
    }
5861
  }
5862

5863
  // sort -------
5864
  if (doSort) {
12,200,658✔
5865
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
2,822✔
5866
  }
5867

5868
  if ((doMerge != 1) && (doSort == 1)) {
12,201,456✔
5869
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
2,822✔
5870
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
345,388✔
5871
      SRowKey key;
342,566✔
5872
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
342,666✔
5873

5874
      int32_t c = tRowKeyCompare(&lastKey, &key);
342,666✔
5875
      if (c == 0) {
342,666✔
5876
        doMerge = 1;
100✔
5877
        break;
100✔
5878
      }
5879
      lastKey = key;
342,566✔
5880
    }
5881
  }
5882

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

5889
_exit:
7,812,496✔
5890
  return 0;
7,812,834✔
5891
}
5892

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

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

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

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

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

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

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

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

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

5963
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
48,336,013✔
5964
  int32_t code = 0;
48,336,013✔
5965

5966
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
96,701,765✔
5967
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
96,715,475✔
5968
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
96,708,385✔
5969
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
96,711,456✔
5970

5971
  // bitmap
5972
  switch (pColData->flag) {
48,352,794✔
5973
    case (HAS_NULL | HAS_NONE):
179,982✔
5974
    case (HAS_VALUE | HAS_NONE):
5975
    case (HAS_VALUE | HAS_NULL):
5976
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
179,982✔
5977
      if (code) return code;
179,982✔
5978
      break;
179,982✔
5979
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5980
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5981
      if (code) return code;
×
5982
      break;
×
5983
    default:
48,149,169✔
5984
      break;
48,149,169✔
5985
  }
5986

5987
  // value
5988
  if (pColData->flag & HAS_VALUE) {
48,329,151✔
5989
    if (IS_VAR_DATA_TYPE(pColData->type)) {
48,262,896✔
5990
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
246,318✔
5991
      if (code) return code;
237,350✔
5992

5993
      code = tEncodeI32v(pEncoder, pColData->nData);
237,350✔
5994
      if (code) return code;
236,987✔
5995

5996
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
236,987✔
5997
      if (code) return code;
237,118✔
5998
    } else {
5999
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
48,024,706✔
6000
      if (code) return code;
47,986,503✔
6001
    }
6002
  }
6003

6004
  return code;
48,308,119✔
6005
}
6006

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

6010
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
67,444,628✔
6011
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
67,446,755✔
6012
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
67,444,982✔
6013
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
67,445,857✔
6014

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

6019
  // bitmap
6020
  switch (pColData->flag) {
33,723,758✔
6021
    case (HAS_NULL | HAS_NONE):
58,183✔
6022
    case (HAS_VALUE | HAS_NONE):
6023
    case (HAS_VALUE | HAS_NULL):
6024
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
58,183✔
6025
      if (code) return code;
58,183✔
6026
      break;
58,183✔
6027
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
6028
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
6029
      if (code) return code;
×
6030
      break;
×
6031
    default:
33,663,072✔
6032
      break;
33,663,072✔
6033
  }
6034

6035
  // value
6036
  if (pColData->flag & HAS_VALUE) {
33,721,255✔
6037
    if (IS_VAR_DATA_TYPE(pColData->type)) {
33,671,544✔
6038
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
138,757✔
6039
      if (code) return code;
141,576✔
6040

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

6044
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
141,576✔
6045
      if (code) return code;
141,576✔
6046
    } else {
6047
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
33,528,777✔
6048
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
33,530,543✔
6049
      if (code) return code;
33,530,337✔
6050
    }
6051
  }
6052
  pColData->cflag = 0;
33,722,325✔
6053

6054
  return code;
33,723,305✔
6055
}
6056

6057
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
48,335,234✔
6058
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
48,335,234✔
6059
  if (code) return code;
48,308,357✔
6060
  return tEncodeI8(pEncoder, pColData->cflag);
96,654,055✔
6061
}
6062

6063
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
33,723,009✔
6064
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
33,723,009✔
6065
  if (code) return code;
33,721,472✔
6066

6067
  code = tDecodeI8(pDecoder, &pColData->cflag);
33,721,472✔
6068
  return code;
33,725,962✔
6069
}
6070

6071
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
48,312,590✔
6072
  if (version == 0) {
48,312,590✔
6073
    return tEncodeColDataVersion0(pEncoder, pColData);
×
6074
  } else if (version == 1) {
48,312,590✔
6075
    return tEncodeColDataVersion1(pEncoder, pColData);
×
6076
  } else if (version == 2) {
48,312,590✔
6077
    int32_t posStart = pEncoder->pos;
48,322,288✔
6078
    pEncoder->pos += INT_BYTES;
48,335,180✔
6079
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
48,342,455✔
6080
    if (code) return code;
48,341,088✔
6081
    int32_t posEnd = pEncoder->pos;
48,341,088✔
6082
    int32_t pos = posEnd - posStart;
48,338,080✔
6083
    pEncoder->pos = posStart;
48,338,080✔
6084
    code = tEncodeI32(pEncoder, pos);
48,348,821✔
6085
    pEncoder->pos = posEnd;
48,348,821✔
6086
    return code;
48,350,802✔
6087
  } else {
6088
    return TSDB_CODE_INVALID_PARA;
38✔
6089
  }
6090
}
6091

6092
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
49,588,159✔
6093
  if (version == 0) {
49,588,159✔
6094
    return tDecodeColDataVersion0(pDecoder, pColData);
×
6095
  } else if (version == 1) {
49,588,159✔
6096
    return tDecodeColDataVersion1(pDecoder, pColData);
×
6097
  } else if (version == 2) {
49,588,159✔
6098
    if (jump) {
49,588,490✔
6099
      int32_t len = 0;
15,867,018✔
6100
      int32_t code = tDecodeI32(pDecoder, &len);
15,867,729✔
6101
      if (code) return code;
15,867,729✔
6102
      pDecoder->pos += (len - INT_BYTES);
15,867,729✔
6103
      return TSDB_CODE_SUCCESS;
15,867,729✔
6104
    } else {
6105
      pDecoder->pos += INT_BYTES;
33,721,472✔
6106
      return tDecodeColDataVersion1(pDecoder, pColData);
33,723,056✔
6107
    }    
6108
  } else {
6109
    return TSDB_CODE_INVALID_PARA;
9✔
6110
  }
6111
}
6112

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

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

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

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

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

6135
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
202,133✔
6136
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
202,176✔
6137

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

6141
  // if (pRow->type) {
6142
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
101,088✔
6143
  while (pIter) {
55,250,590✔
6144
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
55,149,502✔
6145
    int32_t  idx = *(int32_t *)pIter;
55,149,459✔
6146
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
55,149,502✔
6147
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
55,149,502✔
6148

6149
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
55,149,502✔
6150
  }
6151
  //}
6152
  for (int32_t i = 0; i < nSeq; i++) {
55,250,590✔
6153
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
55,149,502✔
6154
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
110,299,004✔
6155
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
110,299,004✔
6156
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
110,299,004✔
6157
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
110,299,004✔
6158
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
110,299,004✔
6159
  }
6160

6161
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
202,176✔
6162

6163
_exit:
101,088✔
6164
  return code;
101,088✔
6165
}
6166

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

6180
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
50,412✔
6181

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

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

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

6203
  for (int32_t i = 0; i < nSeq; i++) {
27,624,821✔
6204
    SBlobValue value;
27,574,409✔
6205
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
27,574,409✔
6206
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
27,574,409✔
6207
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
27,574,409✔
6208
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
27,574,409✔
6209
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
27,574,409✔
6210
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
55,148,818✔
6211
      TAOS_CHECK_EXIT(terrno);
×
6212
    }
6213
  }
6214

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

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

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

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

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

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

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

6275
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
9,016,363✔
6276
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,016,363✔
6277
  int16_t *numOfNull = &pAggs->numOfNull;
9,017,039✔
6278
  *sum = 0;
9,016,363✔
6279
  *max = INT8_MIN;
9,016,363✔
6280
  *min = INT8_MAX;
9,015,687✔
6281
  *numOfNull = 0;
9,015,687✔
6282

6283
  int8_t val;
6284
  if (HAS_VALUE == pColData->flag) {
9,016,363✔
6285
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6286
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6287
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6288
    }
6289
  } else {
6290
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,318,317✔
6291
      switch (tColDataGetBitValue(pColData, iVal)) {
1,317,000✔
6292
        case 0:
1,050,527✔
6293
        case 1:
6294
          (*numOfNull)++;
1,050,527✔
6295
          break;
1,050,527✔
6296
        case 2:
266,473✔
6297
          val = ((int8_t *)pColData->pData)[iVal];
266,473✔
6298
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
266,473✔
6299
          break;
266,473✔
6300
        default:
×
6301
          break;
×
6302
      }
6303
    }
6304
  }
6305
}
8,824,703✔
6306

6307
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
9,000,478✔
6308
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,000,478✔
6309
  int16_t *numOfNull = &pAggs->numOfNull;
9,000,478✔
6310
  *sum = 0;
9,000,478✔
6311
  *max = INT16_MIN;
9,000,478✔
6312
  *min = INT16_MAX;
9,000,478✔
6313
  *numOfNull = 0;
8,999,802✔
6314

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

6339
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
30,811,530✔
6340
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
30,811,530✔
6341
  int16_t *numOfNull = &pAggs->numOfNull;
30,817,446✔
6342
  *sum = 0;
30,817,814✔
6343
  *max = INT32_MIN;
30,815,048✔
6344
  *min = INT32_MAX;
30,818,180✔
6345
  *numOfNull = 0;
30,818,173✔
6346

6347
  int32_t val;
6348
  if (HAS_VALUE == pColData->flag) {
30,810,656✔
6349
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6350
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6351
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6352
    }
6353
  } else {
6354
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6355
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6356
        case 0:
312,057,021✔
6357
        case 1:
6358
          (*numOfNull)++;
312,057,021✔
6359
          break;
312,055,698✔
6360
        case 2:
2,147,483,647✔
6361
          val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6362
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6363
          break;
2,147,483,647✔
6364
        default:
×
6365
          break;
×
6366
      }
6367
    }
6368
  }
6369
}
25,650,420✔
6370

6371
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
72,995,671✔
6372
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
72,995,671✔
6373
  int16_t *numOfNull = &pAggs->numOfNull;
72,998,861✔
6374
  *sum = 0;
72,998,861✔
6375
  *max = INT64_MIN;
72,997,857✔
6376
  *min = INT64_MAX;
72,996,853✔
6377
  *numOfNull = 0;
72,996,045✔
6378

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

6403
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
33,663,930✔
6404
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
33,663,930✔
6405
  int16_t *numOfNull = &pAggs->numOfNull;
33,663,296✔
6406
  *(double *)sum = 0;
33,663,773✔
6407
  *(double *)max = -FLT_MAX;
33,663,097✔
6408
  *(double *)min = FLT_MAX;
33,663,761✔
6409
  *numOfNull = 0;
33,663,297✔
6410

6411
  float val;
6412
  if (HAS_VALUE == pColData->flag) {
33,663,973✔
6413
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6414
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6415
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6416
    }
6417
  } else {
6418
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
12,000,189✔
6419
      switch (tColDataGetBitValue(pColData, iVal)) {
11,982,192✔
6420
        case 0:
2,106,811✔
6421
        case 1:
6422
          (*numOfNull)++;
2,106,811✔
6423
          break;
2,106,811✔
6424
        case 2:
9,875,381✔
6425
          val = ((float *)pColData->pData)[iVal];
9,875,381✔
6426
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
9,875,381✔
6427
          break;
9,875,381✔
6428
        default:
×
6429
          break;
×
6430
      }
6431
    }
6432
  }
6433
}
25,470,486✔
6434

6435
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
16,848,948✔
6436
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
16,848,948✔
6437
  int16_t *numOfNull = &pAggs->numOfNull;
16,855,977✔
6438
  *(double *)sum = 0;
16,857,084✔
6439
  *(double *)max = -DBL_MAX;
16,856,348✔
6440
  *(double *)min = DBL_MAX;
16,857,690✔
6441
  *numOfNull = 0;
16,854,418✔
6442

6443
  double val;
6444
  if (HAS_VALUE == pColData->flag) {
16,857,014✔
6445
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6446
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6447
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6448
    }
6449
  } else {
6450
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,318,317✔
6451
      switch (tColDataGetBitValue(pColData, iVal)) {
1,317,000✔
6452
        case 0:
1,047,015✔
6453
        case 1:
6454
          (*numOfNull)++;
1,047,015✔
6455
          break;
1,047,015✔
6456
        case 2:
269,985✔
6457
          val = ((double *)pColData->pData)[iVal];
269,985✔
6458
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
269,985✔
6459
          break;
269,985✔
6460
        default:
×
6461
          break;
×
6462
      }
6463
    }
6464
  }
6465
}
15,478,209✔
6466

6467
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,630,870✔
6468
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,630,870✔
6469
  int16_t *numOfNull = &pAggs->numOfNull;
8,630,870✔
6470
  *(uint64_t *)sum = 0;
8,630,870✔
6471
  *(uint64_t *)max = 0;
8,630,194✔
6472
  *(uint64_t *)min = UINT8_MAX;
8,630,194✔
6473
  *numOfNull = 0;
8,630,870✔
6474

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

6499
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,630,194✔
6500
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,630,194✔
6501
  int16_t *numOfNull = &pAggs->numOfNull;
8,629,518✔
6502
  *(uint64_t *)sum = 0;
8,630,194✔
6503
  *(uint64_t *)max = 0;
8,628,166✔
6504
  *(uint64_t *)min = UINT16_MAX;
8,629,518✔
6505
  *numOfNull = 0;
8,629,518✔
6506

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

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

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

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

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

6595
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
33,338,086✔
6596
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
33,338,086✔
6597
  int16_t *numOfNull = &pAggs->numOfNull;
33,339,369✔
6598
  *(uint64_t *)sum = 0;
33,338,086✔
6599
  *(uint64_t *)max = 0;
33,338,086✔
6600
  *(uint64_t *)min = 0;
33,338,086✔
6601
  *numOfNull = 0;
33,335,579✔
6602

6603
  switch (pColData->flag) {
33,335,346✔
6604
    case HAS_NONE:
×
6605
    case HAS_NULL:
6606
    case (HAS_NONE | HAS_NULL):
6607
      *numOfNull = pColData->nVal;
×
6608
      break;
×
6609
    case HAS_VALUE:
33,329,058✔
6610
      *numOfNull = 0;
33,329,058✔
6611
      break;
33,328,215✔
6612
    case (HAS_VALUE | HAS_NULL):
3,905✔
6613
    case (HAS_VALUE | HAS_NONE):
6614
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
9,084,905✔
6615
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
9,081,000✔
6616
          (*numOfNull)++;
1,211,953✔
6617
        }
6618
      }
6619
      break;
3,905✔
6620
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6621
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6622
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6623
          (*numOfNull)++;
×
6624
        }
6625
      }
6626
      break;
×
6627
    default:
×
6628
      break;
×
6629
  }
6630
}
33,332,120✔
6631

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

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

6654
  Decimal64         *pVal = NULL;
21,941✔
6655
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
21,941✔
6656
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
21,941✔
6657
  if (HAS_VALUE == pColData->flag) {
21,941✔
6658
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
249,672✔
6659
      pVal = ((Decimal64 *)pColData->pData) + iVal;
247,200✔
6660
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
247,200✔
6661
    }
6662
  } else {
6663
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
15,780,469✔
6664
      switch (tColDataGetBitValue(pColData, iVal)) {
15,761,000✔
6665
        case 0:
628,954✔
6666
        case 1:
6667
          pAggs->numOfNull++;
628,954✔
6668
          break;
628,954✔
6669
        case 2:
15,132,046✔
6670
          pVal = ((Decimal64 *)pColData->pData) + iVal;
15,132,046✔
6671
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
15,132,046✔
6672
          break;
15,132,046✔
6673
        default:
×
6674
          break;
×
6675
      }
6676
    }
6677
  }
6678
}
21,941✔
6679

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

6690
  Decimal128        *pVal = NULL;
57,599✔
6691
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
57,599✔
6692
  if (HAS_VALUE == pColData->flag) {
57,599✔
6693
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
166,448✔
6694
      pVal = ((Decimal128 *)pColData->pData) + iVal;
164,800✔
6695
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
164,800✔
6696
    }
6697
  } else {
6698
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
51,557,351✔
6699
      switch (tColDataGetBitValue(pColData, iVal)) {
51,501,400✔
6700
        case 0:
1,938,033✔
6701
        case 1:
6702
          pAggs->numOfNull++;
1,938,033✔
6703
          break;
1,938,033✔
6704
        case 2:
49,563,367✔
6705
          pVal = ((Decimal128 *)pColData->pData) + iVal;
49,563,367✔
6706
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
49,563,367✔
6707
          break;
49,563,367✔
6708
        default:
×
6709
          break;
×
6710
      }
6711
    }
6712
  }
6713
}
57,599✔
6714

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

6740
// SValueColumn ================================
6741
int32_t tValueColumnInit(SValueColumn *valCol) {
1,147,417,535✔
6742
  valCol->type = TSDB_DATA_TYPE_NULL;
1,147,417,535✔
6743
  valCol->numOfValues = 0;
1,147,659,600✔
6744
  tBufferInit(&valCol->data);
1,147,857,247✔
6745
  tBufferInit(&valCol->offsets);
1,147,511,586✔
6746
  return 0;
1,148,212,644✔
6747
}
6748

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

6757
void tValueColumnClear(SValueColumn *valCol) {
1,558,304,335✔
6758
  valCol->type = TSDB_DATA_TYPE_NULL;
1,558,304,335✔
6759
  valCol->numOfValues = 0;
1,558,596,575✔
6760
  tBufferClear(&valCol->data);
1,558,737,604✔
6761
  tBufferClear(&valCol->offsets);
1,558,470,607✔
6762
  return;
1,558,345,406✔
6763
}
6764

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

6768
  if (valCol->numOfValues == 0) {
1,955,235✔
6769
    valCol->type = value->type;
738,678✔
6770
  }
6771

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

6776
  if (IS_VAR_DATA_TYPE(value->type)) {
1,954,534✔
6777
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
1,686,306✔
6778
      return code;
×
6779
    }
6780
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
1,687,748✔
6781
      return code;
×
6782
    }
6783
  } else {
6784
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
1,111,377✔
6785
    if (code) return code;
1,112,102✔
6786
  }
6787
  valCol->numOfValues++;
1,955,976✔
6788

6789
  return 0;
1,954,534✔
6790
}
6791

6792
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
63,387,030✔
6793
  int32_t code;
6794

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

6799
  if (IS_VAR_DATA_TYPE(valCol->type)) {
63,388,558✔
6800
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
1,939,671✔
6801
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
1,938,843✔
6802
    int32_t  oldDataSize = nextOffset - offsets[idx];
1,938,843✔
6803
    int32_t  bytesAdded = value->nData - oldDataSize;
1,938,843✔
6804

6805
    if (bytesAdded != 0) {
1,938,843✔
6806
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
44,194✔
6807
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
22,097✔
6808
              tBufferGetSize(&valCol->data) - nextOffset);
22,097✔
6809
      valCol->data.size += bytesAdded;
22,097✔
6810

6811
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
22,097✔
6812
        offsets[i] += bytesAdded;
×
6813
      }
6814
    }
6815
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
1,938,843✔
6816
  } else {
6817
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
61,452,956✔
6818
                        tDataTypes[valCol->type].bytes);
61,450,655✔
6819
  }
6820
  return 0;
6821
}
6822

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

6828
  value->type = valCol->type;
360,689,132✔
6829
  if (IS_VAR_DATA_TYPE(value->type)) {
460,494,901✔
6830
    int32_t       offset, nextOffset;
99,785,980✔
6831
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
99,753,134✔
6832

6833
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
99,765,315✔
6834
    if (idx == valCol->numOfValues - 1) {
99,749,540✔
6835
      nextOffset = tBufferGetSize(&valCol->data);
17,951,903✔
6836
    } else {
6837
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
81,789,766✔
6838
    }
6839
    value->nData = nextOffset - offset;
99,781,112✔
6840
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
99,751,030✔
6841
  } else {
6842
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
260,987,261✔
6843
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
522,021,592✔
6844
  }
6845
  return 0;
360,753,714✔
6846
}
6847

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

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

6855
  (*info) = (SValueColumnCompressInfo){
738,678✔
6856
      .cmprAlg = info->cmprAlg,
737,228✔
6857
      .type = valCol->type,
738,678✔
6858
  };
6859

6860
  // offset
6861
  if (IS_VAR_DATA_TYPE(valCol->type)) {
737,953✔
6862
    SCompressInfo cinfo = {
262,987✔
6863
        .cmprAlg = info->cmprAlg,
263,712✔
6864
        .dataType = TSDB_DATA_TYPE_INT,
6865
        .originalSize = valCol->offsets.size,
263,712✔
6866
    };
6867

6868
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
263,712✔
6869
    if (code) return code;
263,712✔
6870

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

6875
  // data
6876
  SCompressInfo cinfo = {
737,953✔
6877
      .cmprAlg = info->cmprAlg,
1,475,866✔
6878
      .dataType = valCol->type,
737,953✔
6879
      .originalSize = valCol->data.size,
737,953✔
6880
  };
6881

6882
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
737,953✔
6883
  if (code) return code;
738,678✔
6884

6885
  info->dataOriginalSize = cinfo.originalSize;
738,678✔
6886
  info->dataCompressedSize = cinfo.compressedSize;
738,678✔
6887

6888
  return 0;
738,678✔
6889
}
6890

6891
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
105,541,947✔
6892
                               SBuffer *assist) {
6893
  int32_t code;
6894

6895
  tValueColumnClear(valCol);
105,541,947✔
6896
  valCol->type = info->type;
105,536,856✔
6897
  // offset
6898
  if (IS_VAR_DATA_TYPE(valCol->type)) {
139,819,627✔
6899
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
34,285,679✔
6900

6901
    SCompressInfo cinfo = {
34,226,741✔
6902
        .dataType = TSDB_DATA_TYPE_INT,
6903
        .cmprAlg = info->cmprAlg,
34,228,908✔
6904
        .originalSize = info->offsetOriginalSize,
34,223,889✔
6905
        .compressedSize = info->offsetCompressedSize,
34,215,285✔
6906
    };
6907

6908
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
34,230,342✔
6909
    if (code) {
34,277,027✔
6910
      return code;
×
6911
    }
6912
  } else {
6913
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
71,299,312✔
6914
  }
6915

6916
  // data
6917
  SCompressInfo cinfo = {
105,638,838✔
6918
      .dataType = valCol->type,
105,625,948✔
6919
      .cmprAlg = info->cmprAlg,
105,639,547✔
6920
      .originalSize = info->dataOriginalSize,
105,618,738✔
6921
      .compressedSize = info->dataCompressedSize,
105,607,967✔
6922
  };
6923

6924
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
105,575,702✔
6925
  if (code) {
105,704,157✔
6926
    return code;
×
6927
  }
6928

6929
  return 0;
105,704,157✔
6930
}
6931

6932
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
738,678✔
6933
  int32_t code;
6934
  uint8_t fmtVer = 0;
738,678✔
6935

6936
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,476,631✔
6937
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,476,631✔
6938
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,476,631✔
6939
  if (IS_VAR_DATA_TYPE(info->type)) {
737,953✔
6940
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
528,149✔
6941
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
527,424✔
6942
  }
6943
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,476,631✔
6944
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,477,356✔
6945

6946
  return 0;
738,678✔
6947
}
6948

6949
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
105,587,835✔
6950
  int32_t code;
6951
  uint8_t fmtVer;
105,587,835✔
6952

6953
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
105,595,722✔
6954
  if (fmtVer == 0) {
105,616,555✔
6955
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
105,616,555✔
6956
    if ((code = tBufferGetI8(reader, &info->type))) return code;
105,604,390✔
6957
    if (IS_VAR_DATA_TYPE(info->type)) {
105,664,714✔
6958
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
34,309,316✔
6959
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
34,236,835✔
6960
    } else {
6961
      info->offsetOriginalSize = 0;
71,346,029✔
6962
      info->offsetCompressedSize = 0;
71,351,773✔
6963
    }
6964
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
105,624,482✔
6965
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
105,633,054✔
6966
  } else {
6967
    return TSDB_CODE_INVALID_PARA;
×
6968
  }
6969

6970
  return 0;
105,679,715✔
6971
}
6972

6973
int32_t tCompressData(void          *input,       // input
1,049,173,773✔
6974
                      SCompressInfo *info,        // compress info
6975
                      void          *output,      // output
6976
                      int32_t        outputSize,  // output size
6977
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6978
) {
6979
  int32_t extraSizeNeeded;
6980
  int32_t code;
6981

6982
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
1,049,173,773✔
6983
  if (!(outputSize >= extraSizeNeeded)) {
1,049,366,759✔
6984
    return TSDB_CODE_INVALID_PARA;
×
6985
  }
6986

6987
  if (info->cmprAlg == NO_COMPRESSION) {
1,049,366,759✔
6988
    (void)memcpy(output, input, info->originalSize);
36,216✔
6989
    info->compressedSize = info->originalSize;
36,216✔
6990
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
1,175,135,435✔
6991
    SBuffer local;
125,740,814✔
6992

6993
    tBufferInit(&local);
6994
    if (buffer == NULL) {
125,819,237✔
6995
      buffer = &local;
×
6996
    }
6997

6998
    if (info->cmprAlg == TWO_STAGE_COMP) {
125,819,237✔
6999
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
125,698,496✔
7000
      if (code) {
125,679,000✔
7001
        tBufferDestroy(&local);
7002
        return code;
×
7003
      }
7004
    }
7005

7006
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
377,417,528✔
7007
        input,                                                   // input
7008
        info->originalSize,                                      // input size
7009
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
125,809,719✔
7010
        output,                                                  // output
7011
        outputSize,                                              // output size
7012
        info->cmprAlg,                                           // compression algorithm
125,809,924✔
7013
        buffer->data,                                            // buffer
7014
        buffer->capacity                                         // buffer size
125,792,432✔
7015
    );
7016
    if (info->compressedSize < 0) {
125,826,492✔
7017
      tBufferDestroy(&local);
7018
      return TSDB_CODE_COMPRESS_ERROR;
×
7019
    }
7020

7021
    tBufferDestroy(&local);
7022
  } else {
7023
    DEFINE_VAR(info->cmprAlg)
923,557,442✔
7024
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
923,679,928✔
7025
      (void)memcpy(output, input, info->originalSize);
7,572✔
7026
      info->compressedSize = info->originalSize;
7,572✔
7027
      return 0;
7,572✔
7028
    }
7029
    SBuffer local;
923,664,194✔
7030

7031
    tBufferInit(&local);
7032
    if (buffer == NULL) {
923,567,670✔
7033
      buffer = &local;
522,330✔
7034
    }
7035
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
923,567,670✔
7036

7037
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
2,147,483,647✔
7038
        input,                                                      // input
7039
        info->originalSize,                                         // input size
7040
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
923,571,022✔
7041
        output,                                                     // output
7042
        outputSize,                                                 // output size
7043
        info->cmprAlg,                                              // compression algorithm
7044
        buffer->data,                                               // buffer
7045
        buffer->capacity                                            // buffer size
923,526,958✔
7046
    );
7047
    if (info->compressedSize < 0) {
923,680,379✔
7048
      tBufferDestroy(&local);
7049
      return TSDB_CODE_COMPRESS_ERROR;
×
7050
    }
7051

7052
    tBufferDestroy(&local);
7053
    // new col compress
7054
  }
7055

7056
  return 0;
1,049,349,329✔
7057
}
7058

7059
int32_t tDecompressData(void                *input,       // input
2,147,483,647✔
7060
                        const SCompressInfo *info,        // compress info
7061
                        void                *output,      // output
7062
                        int32_t              outputSize,  // output size
7063
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
7064
) {
7065
  int32_t code;
7066

7067
  if (!(outputSize >= info->originalSize)) {
2,147,483,647✔
7068
    return TSDB_CODE_INVALID_PARA;
×
7069
  }
7070

7071
  if (info->cmprAlg == NO_COMPRESSION) {
2,147,483,647✔
7072
    if (!(info->compressedSize == info->originalSize)) {
27,324✔
7073
      return TSDB_CODE_INVALID_PARA;
×
7074
    }
7075
    (void)memcpy(output, input, info->compressedSize);
27,324✔
7076
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
7077
    SBuffer local;
2,032,765,763✔
7078

7079
    tBufferInit(&local);
7080
    if (buffer == NULL) {
2,033,118,882✔
7081
      buffer = &local;
×
7082
    }
7083

7084
    if (info->cmprAlg == TWO_STAGE_COMP) {
2,033,118,882✔
7085
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,032,490,337✔
7086
      if (code) {
2,032,379,497✔
7087
        tBufferDestroy(&local);
7088
        return code;
×
7089
      }
7090
    }
7091

7092
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,147,483,647✔
7093
        input,                                                  // input
7094
        info->compressedSize,                                   // inputSize
2,033,276,325✔
7095
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,033,442,952✔
7096
        output,                                                 // output
7097
        outputSize,                                             // output size
7098
        info->cmprAlg,                                          // compression algorithm
2,033,431,256✔
7099
        buffer->data,                                           // helper buffer
7100
        buffer->capacity                                        // extra buffer size
2,033,051,977✔
7101
    );
7102
    if (decompressedSize < 0) {
2,032,550,005✔
7103
      tBufferDestroy(&local);
7104
      return TSDB_CODE_COMPRESS_ERROR;
×
7105
    }
7106

7107
    if (!(decompressedSize == info->originalSize)) {
2,032,550,005✔
7108
      return TSDB_CODE_COMPRESS_ERROR;
×
7109
    }
7110
    tBufferDestroy(&local);
7111
  } else {
7112
    DEFINE_VAR(info->cmprAlg);
2,147,483,647✔
7113
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
2,147,483,647✔
7114
      (void)memcpy(output, input, info->compressedSize);
13,251✔
7115
      return 0;
13,251✔
7116
    }
7117
    SBuffer local;
2,147,483,647✔
7118

7119
    tBufferInit(&local);
7120
    if (buffer == NULL) {
2,147,483,647✔
7121
      buffer = &local;
441,774✔
7122
    }
7123
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,147,483,647✔
7124
    if (code) {
2,147,483,647✔
7125
      return code;
×
7126
    }
7127

7128
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7129
        input,                                                  // input
7130
        info->compressedSize,                                   // inputSize
2,147,483,647✔
7131
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,147,483,647✔
7132
        output,                                                 // output
7133
        outputSize,                                             // output size
7134
        info->cmprAlg,                                          // compression algorithm
2,147,483,647✔
7135
        buffer->data,                                           // helper buffer
7136
        buffer->capacity                                        // extra buffer size
2,147,483,647✔
7137
    );
7138
    if (decompressedSize < 0) {
2,147,483,647✔
7139
      tBufferDestroy(&local);
7140
      return TSDB_CODE_COMPRESS_ERROR;
×
7141
    }
7142

7143
    if (!(decompressedSize == info->originalSize)) {
2,147,483,647✔
7144
      return TSDB_CODE_COMPRESS_ERROR;
×
7145
    }
7146
    tBufferDestroy(&local);
7147
  }
7148

7149
  return 0;
2,147,483,647✔
7150
}
7151

7152
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,048,197,346✔
7153
  int32_t code;
7154

7155
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
1,048,197,346✔
7156
  if (code) return code;
1,048,217,633✔
7157

7158
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,048,217,633✔
7159
  if (code) return code;
1,048,281,277✔
7160

7161
  output->size += info->compressedSize;
1,048,281,277✔
7162
  return 0;
1,048,344,693✔
7163
}
7164

7165
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
2,032,271,856✔
7166
  int32_t code;
7167

7168
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
2,032,271,856✔
7169
  if (code) return code;
2,030,532,041✔
7170

7171
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
2,030,532,041✔
7172
  if (code) return code;
2,032,306,173✔
7173

7174
  output->size += info->originalSize;
2,032,306,173✔
7175
  return 0;
2,032,359,508✔
7176
}
7177

7178
// handle all types, including var data
7179
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7180
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7181
    pVal->pData = pDatum;
73,605,393✔
7182
    pVal->nData = len;
491,708,345✔
7183
  } else {
7184
    switch (len) {
2,147,483,647✔
7185
      case sizeof(uint8_t):
2,147,483,647✔
7186
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7187
        break;
2,147,483,647✔
7188
      case sizeof(uint16_t):
2,147,483,647✔
7189
        pVal->val = *(uint16_t *)pDatum;
2,147,483,647✔
7190
        break;
2,147,483,647✔
7191
      case sizeof(uint32_t):
2,147,483,647✔
7192
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7193
        break;
2,147,483,647✔
7194
      case sizeof(uint64_t):
2,147,483,647✔
7195
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7196
        break;
2,147,483,647✔
7197
      default:
45✔
7198
        break;
45✔
7199
    }
7200
  }
7201
}
2,147,483,647✔
7202

7203
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
1,914,576,040✔
7204
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
1,914,576,040✔
7205
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
213,774,328✔
7206
    pDst->nData = pSrc->nData;
215,063,544✔
7207
  } else {
7208
    pDst->val = pSrc->val;
1,700,801,712✔
7209
  }
7210
}
1,916,391,328✔
7211
void valueClearDatum(SValue *pVal, int8_t type) {
474,829✔
7212
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
474,829✔
7213
    taosMemoryFreeClear(pVal->pData);
25,308✔
7214
    pVal->nData = 0;
25,308✔
7215
  } else {
7216
    pVal->val = 0;
449,521✔
7217
  }
7218
}
474,829✔
7219

7220
int8_t schemaHasBlob(const STSchema *pSchema) {
852,042,114✔
7221
  if (pSchema == NULL) {
852,042,114✔
7222
    return 0;
×
7223
  }
7224
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7225
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7226
      return 1;
60,858✔
7227
    }
7228
  }
7229
  return 0;
852,103,785✔
7230
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc