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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

110922946.31 hits per line

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

63.67
/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) {
216,500,465✔
79
  int32_t n = 0;
216,500,465✔
80
  n += tPutI8(p ? p + n : p, index->type);
216,500,465✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
216,522,480✔
82
  return n;
216,511,602✔
83
}
84

85
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
903,957,727✔
86
  int32_t n = 0;
903,957,727✔
87
  n += tGetI8(p + n, &index->type);
903,957,727✔
88
  n += tGetU32v(p + n, &index->offset);
907,334,317✔
89
  return n;
907,559,580✔
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;
72,181,153✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
72,178,045✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
72,179,340✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
72,180,376✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
72,180,117✔
115
    sinfo->numOfPKs++;
72,178,822✔
116
  }
117

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

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

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

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

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

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

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

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

181
        colValIndex++;
2,147,483,647✔
182
        break;
2,147,483,647✔
183
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {
1,028✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
2,056✔
185
        break;
1,028✔
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;
1,972,303,118✔
197
  }
198
  if (sinfo->numOfValue) {
2,147,483,647✔
199
    sinfo->flag |= HAS_VALUE;
2,147,483,647✔
200
  }
201

202
  // Tuple
203
  sinfo->tupleFlag = sinfo->flag;
2,147,483,647✔
204
  switch (sinfo->flag) {
2,147,483,647✔
205
    case HAS_NONE:
2,147,483,647✔
206
    case HAS_NULL:
207
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
208
      sinfo->tupleFixedSize = 0;
2,147,483,647✔
209
      break;
2,147,483,647✔
210
    case HAS_VALUE:
2,147,483,647✔
211
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
212
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
213
      break;
2,147,483,647✔
214
    case (HAS_NONE | HAS_NULL):
5,556,868✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
5,556,868✔
216
      sinfo->tupleFixedSize = 0;
5,556,868✔
217
      break;
5,557,057✔
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,114,728,229✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,114,728,229✔
225
      sinfo->tupleFixedSize = schema->flen;
1,114,728,607✔
226
      break;
869,010,631✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
72,173,901✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
72,180,117✔
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) {
204,478,458✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
208,486,338✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
208,532,511✔
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;
72,172,606✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
72,179,340✔
252
  }
253
  sinfo->kvRowSize = sizeof(SRow)             // SRow
2,147,483,647✔
254
                     + sinfo->kvPKSize        // primary keys
2,147,483,647✔
255
                     + sinfo->kvIndexSize     // index array
2,147,483,647✔
256
                     + sinfo->kvPayloadSize;  // payload
2,147,483,647✔
257

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

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

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

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

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

289
  // bitmap + fixed + varlen
290
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
2,147,483,647✔
291
  int32_t colValIndex = 1;
2,147,483,647✔
292
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
293
    for (;;) {
294
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
295
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
14,526✔
296
        break;
14,526✔
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
924,195,707✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
858,319,434✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
81,632,816✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
81,646,825✔
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
17,967,168✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
1,028✔
325
        break;
1,028✔
326
      } else {
327
        colValIndex++;
17,966,140✔
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,672,236✔
336
  SBlobItem item = {.type = type, .len = 0};
2,672,236✔
337
  uint64_t  seq = 0;
2,672,236✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
2,672,236✔
339
  if (code != 0) return code;
2,672,236✔
340

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

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

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

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

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

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

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

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

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

453
  return 0;
×
454
}
455

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

611
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
612
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
613
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647✔
614
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647✔
615
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
1,133,879,383✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
1,135,398,888✔
617
            if (colValArray[colValIndex].value.nData > 0) {
1,135,416,369✔
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
1,108,696,076✔
619
                           colValArray[colValIndex].value.nData);
1,108,692,244✔
620
              payloadSize += colValArray[colValIndex].value.nData;
1,108,695,609✔
621
            }
622
          } else {
623
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,147,483,647✔
624
            (void)memcpy(payload + payloadSize,
2,147,483,647✔
625
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,147,483,647✔
626
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647✔
627
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,147,483,647✔
628
          }
629
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
2,147,483,647✔
630
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
1,914,997,311✔
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
×
637
        break;
×
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,
25,986,913✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
25,986,913✔
649

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1004
  return code;
×
1005
}
1006

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

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

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

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

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

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

1048
  return code;
×
1049
}
1050

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

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

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

1080
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
26,527,971✔
1081
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
541,058✔
1082
  } else {
1083
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
25,986,913✔
1084
  }
1085

1086
  return code;
26,527,971✔
1087
}
1088

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

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

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

1102
  return code;
×
1103
}
1104

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

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

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

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

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

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

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

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

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

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

1183
  offset = pBlobSet->len;
26,535,841✔
1184
  pBlobSet->len += len;
26,535,841✔
1185

1186
  pBlobSet->seq++;
26,535,841✔
1187
  *seq = pBlobSet->seq;
26,535,841✔
1188

1189
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow, .type = pItem->type};
26,535,841✔
1190
  if (taosArrayPush(pBlobSet->pSeqTable, &value) == NULL) {
53,071,682✔
1191
    TAOS_CHECK_EXIT(terrno);
×
1192
  }
1193

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

1200
_exit:
26,535,841✔
1201
  return code;
26,535,841✔
1202
}
1203

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

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

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

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

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

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

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

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

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

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

1257
  return code;
×
1258
}
1259

1260
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
1,143,062,214✔
1261
  if (pBlobSet == NULL) return 0;
1,143,062,214✔
1262
  return taosArrayGetSize(pBlobSet->pSeqTable);
80,484✔
1263
}
1264

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

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

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

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

1313
  int32_t code = 0;
13,454,454✔
1314
  int32_t numOfRows = infos[0].bind->num;
13,454,454✔
1315
  SArray *colValArray;
1316
  SColVal colVal;
13,398,300✔
1317

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

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

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

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

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

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

1382
_exit:
3,194,744✔
1383
  taosArrayDestroy(colValArray);
13,440,628✔
1384
  return code;
13,452,280✔
1385
}
1386

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

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

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

1401
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1402
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
11,924,546✔
1403
    return 0;
11,924,727✔
1404
  }
1405

1406
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1407
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
274,147,701✔
1408
    return 0;
274,147,983✔
1409
  }
1410

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

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

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

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

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

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

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

1456
            pData += tGetU32v(pData, &pColVal->value.nData);
2,147,483,647✔
1457
            if (pColVal->value.nData > 0) {
1,182,916,042✔
1458
              pColVal->value.pData = pData;
1,178,965,157✔
1459
            } else {
1460
              pColVal->value.pData = NULL;
3,974,745✔
1461
            }
1462
            if (isBlob == 1) {
1,182,659,421✔
1463
              pData += BSE_SEQUECE_SIZE;  // skip seq
32,789,093✔
1464
            }
1465
          } else {
1466
            valueSetDatum(&pColVal->value, pTColumn->type, pData, pTColumn->bytes);
2,147,483,647✔
1467
          }
1468
        }
1469
        return 0;
2,147,483,647✔
1470
      } else if (TABS(cid) < pTColumn->colId) {
2,147,483,647✔
1471
        lidx = mid + 1;
2,147,483,647✔
1472
      } else {
1473
        ridx = mid - 1;
2,147,483,647✔
1474
      }
1475
    }
1476

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

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

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

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

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

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

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

1532
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
1,135,955,411✔
1533
  SRowKey key1, key2;
1,131,164,458✔
1534
  tRowGetKey(*(SRow **)p1, &key1);
2,147,483,647✔
1535
  tRowGetKey(*(SRow **)p2, &key2);
2,147,483,647✔
1536
  return tRowKeyCompare(&key1, &key2);
1,139,796,665✔
1537
}
1538
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
43,152,215✔
1539

1540
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
21,167,283✔
1541
  SColVal* pColVal = tRowIterNext(iter);
21,167,283✔
1542
  while (pColVal != NULL && pColVal->cid < targetCid) {
24,147,157✔
1543
    pColVal = tRowIterNext(iter);
2,980,525✔
1544
  }
1545
  return pColVal;
21,166,632✔
1546
}
1547

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

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

1562
  for (int32_t i = 0; i < nRow; i++) {
46,074,232✔
1563
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
43,154,379✔
1564

1565
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
43,149,412✔
1566
    if (code) goto _exit;
43,154,596✔
1567
  }
1568

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

1576
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
22,085,679✔
1577
    int32_t targetCid = pTSchema->columns[iCol].colId;
19,165,609✔
1578
    SColVal *pColVal = NULL;
19,165,609✔
1579

1580
    switch (strategy) {
19,165,609✔
1581
      case KEEP_CONSISTENCY:
503,648✔
1582
        if (nRow > 0)
503,648✔
1583
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
503,648✔
1584
        break;
503,648✔
1585

1586
      default:  // default using PREFER_NON_NULL strategy
18,661,961✔
1587
      case PREFER_NON_NULL:
1588
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
20,682,975✔
1589
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
20,663,418✔
1590

1591
          if (COL_VAL_IS_VALUE(pColValT)) {
20,662,767✔
1592
            pColVal = pColValT;
18,641,970✔
1593
            break;
18,641,970✔
1594
          } else if (pColVal == NULL) {
2,021,014✔
1595
            pColVal = pColValT;
1,985,268✔
1596
          }
1597
        }
1598
        break;
18,661,527✔
1599
    }
1600

1601
    if (pColVal) {
19,165,175✔
1602
      if (taosArrayPush(aColVal, pColVal) == NULL) {
19,165,609✔
1603
        code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
    }
1607
  }
1608

1609
  // build
1610
  SRowBuildScanInfo sinfo = {0};
2,919,636✔
1611
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
2,919,853✔
1612

1613
  if (code) goto _exit;
2,919,202✔
1614

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

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

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

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

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

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

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

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

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

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

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

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

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

1710
  tBlobSetSwap(pBlob, pTempBlob);
787✔
1711

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

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

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

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

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

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

1755
    tRowGetKey(row1, &key1);
×
1756

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

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

1765
      iEnd++;
×
1766
    }
1767

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

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

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

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

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

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

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

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

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

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

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

1848
  // build
1849

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

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

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

1870
  return code;
×
1871
}
1872

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

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

1885
  int32_t iStart = 0;
1,161,965✔
1886
  while (iStart < aRowP->size) {
117,252,969✔
1887
    SRowKey key1;
112,941,521✔
1888
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
116,090,570✔
1889

1890
    tRowGetKey(row1, &key1);
232,188,952✔
1891

1892
    int32_t iEnd = iStart + 1;
116,102,505✔
1893
    while (iEnd < aRowP->size) {
156,339,406✔
1894
      SRowKey key2;
152,548,977✔
1895
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
155,173,969✔
1896
      tRowGetKey(row2, &key2);
310,231,192✔
1897

1898
      if (tRowKeyCompare(&key1, &key2) != 0) break;
155,173,318✔
1899

1900
      iEnd++;
40,238,637✔
1901
    }
1902

1903
    if (iEnd - iStart > 1) {
116,092,957✔
1904
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
2,919,636✔
1905
      if (code) return code;
2,919,636✔
1906
    }
1907

1908
    // the array is also changing, so the iStart just ++ instead of iEnd
1909
    iStart++;
116,092,957✔
1910
  }
1911

1912
  return code;
1,161,965✔
1913
}
1914

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

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

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

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

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

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

1944
      iEnd++;
×
1945
    }
1946

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

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

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

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

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

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

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

1994
  int32_t code = 0;
122,589,471✔
1995

1996
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
122,589,471✔
1997
  if (pIter == NULL) {
122,079,113✔
1998
    code = terrno;
×
1999
    goto _exit;
×
2000
  }
2001

2002
  pIter->pRow = pRow;
122,079,113✔
2003
  pIter->pTSchema = pTSchema;
122,079,989✔
2004
  pIter->iTColumn = 0;
122,032,253✔
2005

2006
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
122,093,635✔
2007

2008
  uint8_t         *data = pRow->data;
121,871,469✔
2009
  SPrimaryKeyIndex index;
121,596,683✔
2010
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
122,683,692✔
2011
    data += tGetPrimaryKeyIndex(data, &index);
819,239✔
2012
  }
2013

2014
  if (pRow->flag >> 4) {
121,918,976✔
2015
    pIter->iCol = 0;
78,000,598✔
2016
    pIter->pIdx = (SKVIdx *)data;
78,021,622✔
2017
    if (pRow->flag & KV_FLG_LIT) {
78,023,374✔
2018
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
77,956,579✔
2019
    } else if (pRow->flag & KV_FLG_MID) {
×
2020
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
×
2021
    } else {
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
2023
    }
2024
  } else {
2025
    switch (pRow->flag) {
43,923,403✔
2026
      case (HAS_NULL | HAS_NONE):
631✔
2027
        pIter->pb = data;
631✔
2028
        break;
631✔
2029
      case HAS_VALUE:
43,759,412✔
2030
        pIter->pf = data;
43,759,412✔
2031
        pIter->pv = pIter->pf + pTSchema->flen;
43,759,844✔
2032
        break;
43,760,061✔
2033
      case (HAS_VALUE | HAS_NONE):
181,163✔
2034
      case (HAS_VALUE | HAS_NULL):
2035
        pIter->pb = data;
181,163✔
2036
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
181,163✔
2037
        pIter->pv = pIter->pf + pTSchema->flen;
181,163✔
2038
        break;
181,163✔
2039
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2040
        pIter->pb = data;
×
2041
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2042
        pIter->pv = pIter->pf + pTSchema->flen;
×
2043
        break;
×
2044
      default:
×
2045
        break;
×
2046
    }
2047
  }
2048

2049
_exit:
121,921,979✔
2050
  if (code) {
122,186,903✔
2051
    *ppIter = NULL;
×
2052
  } else {
2053
    *ppIter = pIter;
122,186,903✔
2054
  }
2055
  return code;
122,200,262✔
2056
}
2057

2058
void tRowIterClose(SRowIter **ppIter) {
122,338,507✔
2059
  SRowIter *pIter = *ppIter;
122,338,507✔
2060
  if (pIter) {
122,377,708✔
2061
    taosMemoryFree(pIter);
122,386,687✔
2062
  }
2063
  *ppIter = NULL;
122,119,051✔
2064
}
122,124,307✔
2065

2066
SColVal *tRowIterNext(SRowIter *pIter) {
939,849,156✔
2067
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
939,849,156✔
2068
    return NULL;
78,797,084✔
2069
  }
2070

2071
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
887,187,435✔
2072

2073
  // timestamp
2074
  if (0 == pIter->iTColumn) {
893,243,130✔
2075
    pIter->cv.cid = pTColumn->colId;
83,439,181✔
2076
    pIter->cv.value.type = pTColumn->type;
83,464,649✔
2077
    pIter->cv.flag = CV_FLAG_VALUE;
83,437,057✔
2078
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
83,453,918✔
2079
    goto _exit;
83,357,779✔
2080
  }
2081

2082
  if (pIter->pRow->flag == HAS_NONE) {
811,537,270✔
2083
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,580,004✔
2084
    goto _exit;
1,580,004✔
2085
  }
2086

2087
  if (pIter->pRow->flag == HAS_NULL) {
809,310,714✔
2088
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
398,752✔
2089
    goto _exit;
398,752✔
2090
  }
2091

2092
  if (pIter->pRow->flag >> 4) {  // KV
808,514,041✔
2093
    if (pIter->iCol < pIter->pIdx->nCol) {
783,409,586✔
2094
      uint8_t *pData;
2095

2096
      if (pIter->pRow->flag & KV_FLG_LIT) {
778,053,084✔
2097
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
779,615,868✔
2098
      } else if (pIter->pRow->flag & KV_FLG_MID) {
×
2099
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
×
2100
      } else {
2101
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
2102
      }
2103

2104
      int16_t cid;
779,972,245✔
2105
      pData += tGetI16v(pData, &cid);
778,939,003✔
2106

2107
      if (TABS(cid) == pTColumn->colId) {
778,874,398✔
2108
        if (cid < 0) {
771,895,694✔
2109
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
155,298,418✔
2110
        } else {
2111
          pIter->cv.cid = pTColumn->colId;
616,597,276✔
2112
          pIter->cv.value.type = pTColumn->type;
624,586,177✔
2113
          pIter->cv.flag = CV_FLAG_VALUE;
624,523,169✔
2114

2115
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
624,581,204✔
2116
            pData += tGetU32v(pData, &pIter->cv.value.nData);
310,389,760✔
2117
            if (pIter->cv.value.nData > 0) {
154,967,243✔
2118
              pIter->cv.value.pData = pData;
156,126,410✔
2119
            } else {
2120
              pIter->cv.value.pData = NULL;
×
2121
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2122
              //   pIter->cv.value.pData = pData;
2123
              // }
2124
            }
2125
          } else {
2126
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
469,543,753✔
2127
          }
2128
        }
2129

2130
        pIter->iCol++;
776,750,988✔
2131
        goto _exit;
772,130,964✔
2132
      } else if (TABS(cid) > pTColumn->colId) {
45,538✔
2133
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
45,538✔
2134
        goto _exit;
45,538✔
2135
      } else {
2136
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2137
        goto _exit;
×
2138
      }
2139
    } else {
2140
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
12,793,523✔
2141
      goto _exit;
12,793,523✔
2142
    }
2143
  } else {  // Tuple
2144
    uint8_t bv = BIT_FLG_VALUE;
16,639,382✔
2145
    if (pIter->pb) {
16,639,382✔
2146
      switch (pIter->pRow->flag) {
741,382✔
2147
        case (HAS_NULL | HAS_NONE):
1,893✔
2148
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
1,893✔
2149
          break;
1,893✔
2150
        case (HAS_VALUE | HAS_NONE):
15,354✔
2151
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
15,354✔
2152
          if (bv) bv++;
15,354✔
2153
          break;
15,354✔
2154
        case (HAS_VALUE | HAS_NULL):
724,135✔
2155
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
724,135✔
2156
          break;
724,135✔
2157
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2158
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2159
          break;
×
2160
        default:
×
2161
          break;
×
2162
      }
2163

2164
      if (bv == BIT_FLG_NONE) {
741,382✔
2165
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,761✔
2166
        goto _exit;
7,761✔
2167
      } else if (bv == BIT_FLG_NULL) {
733,621✔
2168
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
129,702✔
2169
        goto _exit;
129,702✔
2170
      }
2171
    }
2172

2173
    pIter->cv.cid = pTColumn->colId;
16,502,204✔
2174
    pIter->cv.value.type = pTColumn->type;
16,501,987✔
2175
    pIter->cv.flag = CV_FLAG_VALUE;
16,501,770✔
2176
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
20,482,786✔
2177
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
3,981,233✔
2178
      pData += tGetU32v(pData, &pIter->cv.value.nData);
7,958,278✔
2179
      if (pIter->cv.value.nData > 0) {
3,980,799✔
2180
        pIter->cv.value.pData = pData;
3,949,551✔
2181
      } else {
2182
        pIter->cv.value.pData = NULL;
31,248✔
2183
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2184
        //   pIter->cv.value.pData = pData;
2185
        // }
2186
      }
2187
    } else {
2188
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
12,520,252✔
2189
    }
2190
    goto _exit;
16,501,553✔
2191
  }
2192

2193
_exit:
873,039,514✔
2194
  pIter->iTColumn++;
873,039,514✔
2195
  return &pIter->cv;
883,978,496✔
2196
}
2197

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

2201
  if (flag) return code;
431,862✔
2202

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

2208
  return code;
329,892✔
2209
}
2210
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
35,380,428✔
2211
  int32_t code = 0;
35,380,428✔
2212

2213
  int32_t   iColData = 0;
35,380,428✔
2214
  SColData *pColData = &aColData[iColData];
35,380,428✔
2215
  int32_t   iTColumn = 1;
35,380,428✔
2216
  STColumn *pTColumn = &pSchema->columns[iTColumn];
35,380,428✔
2217

2218
  while (pColData) {
322,865,039✔
2219
    if (pTColumn) {
287,488,599✔
2220
      if (pTColumn->colId == pColData->cid) {  // NULL
287,488,599✔
2221
        if (flag == 0) {
287,487,450✔
2222
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
287,487,450✔
2223
        } else {
2224
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2225
        }
2226
        if (code) goto _exit;
287,489,429✔
2227

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

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

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

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

2261
  switch (pRow->flag) {
2,147,483,647✔
2262
    case HAS_VALUE:
2,147,483,647✔
2263
      pf = data;  // TODO: fix here
2,147,483,647✔
2264
      pv = pf + pTSchema->flen;
2,147,483,647✔
2265
      break;
2,147,483,647✔
2266
    case (HAS_NULL | HAS_NONE):
60,906✔
2267
      pb = data;
60,906✔
2268
      break;
60,906✔
2269
    case (HAS_VALUE | HAS_NONE):
224,869,784✔
2270
    case (HAS_VALUE | HAS_NULL):
2271
      pb = data;
224,869,784✔
2272
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
224,869,784✔
2273
      pv = pf + pTSchema->flen;
224,868,530✔
2274
      break;
224,869,993✔
UNCOV
2275
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
UNCOV
2276
      pb = data;
×
UNCOV
2277
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2278
      pv = pf + pTSchema->flen;
×
2279
      break;
25✔
2280
    default:
×
2281
      return TSDB_CODE_INVALID_DATA_FMT;
×
2282
  }
2283

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

2310
          if (bv == BIT_FLG_NONE) {
2,147,483,647✔
2311
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
4,838,033✔
2312
              goto _exit;
×
2313
            goto _continue;
4,838,535✔
2314
          } else if (bv == BIT_FLG_NULL) {
2,147,483,647✔
2315
            if (flag == 0) {
337,832,702✔
2316
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
337,527,366✔
2317
            } else {
2318
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
305,336✔
2319
            }
2320
            if (code) goto _exit;
337,845,079✔
2321
            goto _continue;
337,845,079✔
2322
          }
2323
        }
2324

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

2346
      _continue:
2,147,483,647✔
2347
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2348
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2349
      } else if (pTColumn->colId > pColData->cid) {  // NONE
75,523,344✔
2350
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2351
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2352
      } else {
2353
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
75,458,557✔
2354
      }
2355
    } else {
2356
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
67,380,316✔
2357
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
67,379,659✔
2358
    }
2359
  }
2360

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

2367
  uint8_t  *pv = NULL;
1,401,999,699✔
2368
  int32_t   iColData = 0;
1,401,999,699✔
2369
  SColData *pColData = &aColData[iColData];
1,401,999,699✔
2370
  int32_t   iTColumn = 1;
1,402,121,684✔
2371
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,402,121,684✔
2372
  int32_t   iCol = 0;
1,402,147,500✔
2373

2374
  // primary keys
2375
  uint8_t         *data = pRow->data;
1,402,147,500✔
2376
  SPrimaryKeyIndex index;
1,402,123,830✔
2377
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,404,510,568✔
2378
    data += tGetPrimaryKeyIndex(data, &index);
2,375,309✔
2379
  }
2380

2381
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,402,139,851✔
2382
  if (pRow->flag & KV_FLG_LIT) {
1,402,139,851✔
2383
    pv = pKVIdx->idx + pKVIdx->nCol;
1,394,436,983✔
2384
  } else if (pRow->flag & KV_FLG_MID) {
7,695,989✔
2385
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
7,702,085✔
2386
  } else if (pRow->flag & KV_FLG_BIG) {
×
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2388
  } else {
2389
    return TSDB_CODE_INVALID_PARA;
×
2390
  }
2391

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

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

2410
          if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
2411
            if (cid < 0) {
2,147,483,647✔
2412
              if (flag == 0) {
1,684,246,283✔
2413
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,684,470,121✔
2414
              } else {
2415
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
5,197✔
2416
              }
2417
              if (code) goto _exit;
1,681,991,590✔
2418
            } else {
2419
              uint32_t nData;
2,147,483,647✔
2420
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
2421
                pData += tGetU32v(pData, &nData);
635,528,771✔
2422
              } else {
2423
                nData = 0;
2,147,483,647✔
2424
              }
2425
              if (flag == 0) {
2,147,483,647✔
2426
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2427
              } else {
2428
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
7,386,056✔
2429
              }
2430
              if (code) goto _exit;
2,147,483,647✔
2431
            }
2432
            iCol++;
2,147,483,647✔
2433
            goto _continue;
2,147,483,647✔
2434
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
2,147,483,647✔
2435
            break;
2,147,483,647✔
2436
          } else {
2437
            iCol++;
23,192,430✔
2438
          }
2439
        }
2440

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

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

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

2469
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
2470
    return tRowNoneUpsertColData(aColData, nColData, flag);
431,862✔
2471
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2472
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
35,380,428✔
2473
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2474
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,402,039,213✔
2475
  } else {  // TUPLE row
2476
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
2,147,483,647✔
2477
  }
2478
}
2479

2480
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
450,168,154✔
2481
  key->numOfPKs = row->numOfPKs;
450,168,154✔
2482

2483
  if (key->numOfPKs == 0) {
450,186,910✔
2484
    return;
×
2485
  }
2486

2487
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
415,182,713✔
2488

2489
  uint8_t *data = row->data;
450,193,127✔
2490

2491
  for (int32_t i = 0; i < row->numOfPKs; i++) {
900,394,546✔
2492
    data += tGetPrimaryKeyIndex(data, &indices[i]);
450,194,014✔
2493
  }
2494

2495
  // primary keys
2496
  for (int32_t i = 0; i < row->numOfPKs; i++) {
900,382,381✔
2497
    key->pks[i].type = indices[i].type;
450,200,786✔
2498

2499
    uint8_t *tdata = data + indices[i].offset;
450,199,422✔
2500
    if (row->flag >> 4) {
450,205,623✔
2501
      tdata += tGetI16v(tdata, NULL);
97,945,979✔
2502
    }
2503

2504
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
450,205,756✔
2505
      key->pks[i].pData = tdata;
99,287,634✔
2506
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
198,576,386✔
2507
    } else {
2508
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
350,921,943✔
2509
    }
2510
  }
2511
}
2512

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

2524
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
221,247,513✔
2525
  switch (tv1->type) {
221,247,513✔
2526
    case TSDB_DATA_TYPE_BOOL:
×
2527
    case TSDB_DATA_TYPE_TINYINT:
2528
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2529
    case TSDB_DATA_TYPE_SMALLINT:
×
2530
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_INT:
159,441,029✔
2532
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
159,441,029✔
2533
    case TSDB_DATA_TYPE_BIGINT:
11,965,616✔
2534
    case TSDB_DATA_TYPE_TIMESTAMP:
2535
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
11,965,616✔
2536
    case TSDB_DATA_TYPE_FLOAT:
×
2537
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2538
    case TSDB_DATA_TYPE_DOUBLE:
×
2539
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_UTINYINT:
×
2541
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_USMALLINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_UINT:
11,755,521✔
2545
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
11,755,521✔
2546
    case TSDB_DATA_TYPE_UBIGINT:
11,719,269✔
2547
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
11,719,269✔
2548
    case TSDB_DATA_TYPE_GEOMETRY:
26,788,553✔
2549
    case TSDB_DATA_TYPE_BINARY: {
2550
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
26,788,553✔
2551
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
26,789,400✔
2552
    }
2553
    case TSDB_DATA_TYPE_NCHAR: {
×
2554
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2555
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2556
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2557
    }
2558
    case TSDB_DATA_TYPE_VARBINARY: {
7,244✔
2559
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
7,244✔
2560
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2561
    }
2562
    default:
×
2563
      break;
×
2564
  }
2565

2566
  return 0;
×
2567
}
2568

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

2579
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2580
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2581
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
221,684,479✔
2582
      if (ret) return ret;
221,683,602✔
2583
    }
2584
  } else if (key1->numOfPKs < key2->numOfPKs) {
172,204✔
2585
    return -1;
4,995✔
2586
  } else {
2587
    return 1;
167,209✔
2588
  }
2589

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

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

2597
  if (pSrc->numOfPKs > 0) {
2,147,483,647✔
2598
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
171,912,194✔
2599
      SValue *pVal = &pDst->pks[i];
85,958,145✔
2600
      pVal->type = pSrc->pks[i].type;
85,954,948✔
2601

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

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

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

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

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

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

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

2717
  // key
2718
  if (isJson) {
388,990,709✔
2719
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
3,004,512✔
2720
  } else {
2721
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
774,977,019✔
2722
  }
2723

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

2727
  // value
2728
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
389,006,584✔
2729
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
337,990,174✔
2730
  } else {
2731
    p = p ? p + n : p;
219,967,057✔
2732
    n += tDataTypes[pTagVal->type].bytes;
219,968,050✔
2733
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
219,995,542✔
2734
  }
2735

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

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

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

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

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

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

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

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

2779
  // sort
2780
  if (isJson) {
85,026,511✔
2781
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
497,504✔
2782
  } else {
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
84,529,007✔
2784
  }
2785

2786
  // get size
2787
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
279,497,910✔
2788
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
194,493,060✔
2789
  }
2790
  if (szTag <= INT8_MAX) {
85,004,850✔
2791
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
80,331,584✔
2792
  } else {
2793
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
4,673,266✔
2794
    isLarge = 1;
4,673,266✔
2795
  }
2796

2797
  // build tag
2798
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
85,004,850✔
2799
  if ((*ppTag) == NULL) {
85,024,368✔
2800
    code = terrno;
×
2801
    goto _err;
×
2802
  }
2803
  (*ppTag)->flags = 0;
85,026,283✔
2804
  if (isJson) {
85,030,821✔
2805
    (*ppTag)->flags |= TD_TAG_JSON;
497,504✔
2806
  }
2807
  if (isLarge) {
85,030,821✔
2808
    (*ppTag)->flags |= TD_TAG_LARGE;
4,677,096✔
2809
  }
2810
  (*ppTag)->len = szTag;
85,030,821✔
2811
  (*ppTag)->nTag = nTag;
85,025,732✔
2812
  (*ppTag)->ver = version;
85,026,482✔
2813

2814
  if (isLarge) {
85,010,973✔
2815
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
4,677,096✔
2816
  } else {
2817
    p = (uint8_t *)&(*ppTag)->idx[nTag];
80,333,877✔
2818
  }
2819
  n = 0;
85,027,828✔
2820
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
279,520,133✔
2821
    if (isLarge) {
194,487,955✔
2822
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
50,847,027✔
2823
    } else {
2824
      (*ppTag)->idx[iTag] = n;
143,640,928✔
2825
    }
2826
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
194,480,905✔
2827
  }
2828
#ifdef TD_DEBUG_PRINT_TAG
2829
  debugPrintSTag(*ppTag, __func__, __LINE__);
2830
#endif
2831

2832
  return code;
85,032,178✔
2833

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

2838
void tTagFree(STag *pTag) {
1,123,386,879✔
2839
  if (pTag) taosMemoryFree(pTag);
1,123,386,879✔
2840
}
1,123,386,879✔
2841

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

2847
  char  *data = NULL;
490,158,605✔
2848
  int8_t typeBytes = 0;
490,158,605✔
2849
  if (isJson) {
490,158,605✔
2850
    typeBytes = CHAR_BYTES;
2,441,659✔
2851
  }
2852

2853
  if (IS_VAR_DATA_TYPE(value->type)) {
490,158,605✔
2854
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
195,660,017✔
2855
    if (data == NULL) {
195,729,823✔
2856
      return NULL;
×
2857
    }
2858

2859
    if (isJson) {
195,729,823✔
2860
      *data = value->type;
1,908,431✔
2861
    }
2862

2863
    varDataLen(data + typeBytes) = value->nData;
195,729,823✔
2864
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
195,732,519✔
2865
  } else {
2866
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
294,431,006✔
2867
  }
2868

2869
  return data;
490,164,746✔
2870
}
2871

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

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

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

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

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

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

2923
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
248,744,507✔
2924
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
497,489,049✔
2925
}
2926

2927
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
1,283,186,939✔
2928

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

2936
  if (isLarge) {
858,581✔
2937
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,499✔
2938
  } else {
2939
    p = (uint8_t *)&pTag->idx[pTag->nTag];
854,082✔
2940
  }
2941

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

2948
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,087,459✔
2949
    if (isLarge) {
1,228,878✔
2950
      offset = ((int16_t *)pTag->idx)[iTag];
6,074✔
2951
    } else {
2952
      offset = pTag->idx[iTag];
1,222,804✔
2953
    }
2954
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,228,878✔
2955
    if (taosArrayPush(*ppArray, &tv) == NULL) {
2,457,756✔
2956
      code = terrno;
×
2957
      goto _err;
×
2958
    }
2959
  }
2960

2961
  return code;
858,581✔
2962

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

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

2975
  pTSchema->numOfCols = numOfCols;
768,690,843✔
2976
  pTSchema->version = version;
768,699,214✔
2977

2978
  // timestamp column
2979
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
768,730,645✔
2980
    terrno = TSDB_CODE_INVALID_PARA;
×
2981
    taosMemoryFree(pTSchema);
×
2982
    return NULL;
×
2983
  }
2984
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
768,724,177✔
2985
    terrno = TSDB_CODE_INVALID_PARA;
×
2986
    taosMemoryFree(pTSchema);
×
2987
    return NULL;
×
2988
  }
2989
  pTSchema->columns[0].colId = aSchema[0].colId;
768,691,041✔
2990
  pTSchema->columns[0].type = aSchema[0].type;
768,763,813✔
2991
  pTSchema->columns[0].flags = aSchema[0].flags;
768,788,846✔
2992
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
768,805,690✔
2993
  pTSchema->columns[0].offset = -1;
768,788,269✔
2994

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

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

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

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

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

3020
  return pTSchema;
768,804,876✔
3021
}
3022

3023
static int32_t tTColumnCompare(const void *p1, const void *p2) {
64,468,152✔
3024
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
64,468,152✔
3025
    return -1;
10,605,371✔
3026
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
53,864,713✔
3027
    return 1;
38,297,072✔
3028
  }
3029

3030
  return 0;
15,572,079✔
3031
}
3032

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

3038
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
15,571,483✔
3039
}
3040

3041
// SColData ========================================
3042
void tColDataDestroy(void *ph) {
942,166,764✔
3043
  if (ph) {
942,166,764✔
3044
    SColData *pColData = (SColData *)ph;
942,205,295✔
3045

3046
    tFree(pColData->pBitMap);
942,205,295✔
3047
    tFree(pColData->aOffset);
942,249,606✔
3048
    tFree(pColData->pData);
942,236,649✔
3049
  }
3050
}
942,231,707✔
3051

3052
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,026,681,672✔
3053
  pColData->cid = cid;
1,026,681,672✔
3054
  pColData->type = type;
1,026,749,638✔
3055
  pColData->cflag = cflag;
1,026,764,577✔
3056
  tColDataClear(pColData);
1,026,794,820✔
3057
}
1,026,775,774✔
3058

3059
void tColDataClear(SColData *pColData) {
2,110,101,272✔
3060
  pColData->numOfNone = 0;
2,110,101,272✔
3061
  pColData->numOfNull = 0;
2,110,159,005✔
3062
  pColData->numOfValue = 0;
2,110,111,046✔
3063
  pColData->nVal = 0;
2,110,181,683✔
3064
  pColData->flag = 0;
2,110,169,757✔
3065
  pColData->nData = 0;
2,110,178,585✔
3066
}
2,110,156,393✔
3067

3068
void tColDataDeepClear(SColData *pColData) {
7,659,127✔
3069
  pColData->pBitMap = NULL;
7,659,127✔
3070
  pColData->aOffset = NULL;
7,662,093✔
3071
  pColData->pData = NULL;
7,662,388✔
3072

3073
  tColDataClear(pColData);
7,662,496✔
3074
}
7,664,276✔
3075

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

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

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

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

3124
_exit:
2,147,483,647✔
3125
  return code;
2,147,483,647✔
3126
}
3127
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
467,447,844✔
3128
  pColData->flag = HAS_VALUE;
467,542,849✔
3129
  pColData->numOfValue++;
467,563,798✔
3130
  return tColDataPutValue(pColData, pData, nData);
467,566,288✔
3131
}
3132
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
16,551,807✔
3133
  pColData->flag = HAS_NONE;
16,551,807✔
3134
  pColData->numOfNone++;
16,552,464✔
3135
  pColData->nVal++;
16,551,769✔
3136
  return 0;
16,551,138✔
3137
}
3138
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
21,445,401✔
3139
  pColData->flag = HAS_NULL;
21,468,130✔
3140
  pColData->numOfNull++;
21,478,373✔
3141
  pColData->nVal++;
21,478,395✔
3142
  return 0;
21,475,884✔
3143
}
3144
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
863,735✔
3145
  int32_t code = 0;
863,735✔
3146

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

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

3154
  pColData->flag |= HAS_VALUE;
863,735✔
3155
  pColData->numOfValue++;
863,735✔
3156

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

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

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

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

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

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

3200
  return code;
23,299✔
3201
}
3202
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,793,865✔
3203
  int32_t code = 0;
4,794,348✔
3204

3205
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
4,794,348✔
3206
  code = tRealloc(&pColData->pBitMap, nBit);
4,797,899✔
3207
  if (code) return code;
4,797,496✔
3208

3209
  memset(pColData->pBitMap, 0, nBit);
4,797,496✔
3210
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,796,709✔
3211

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

3215
  if (pColData->nVal) {
4,797,899✔
3216
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,315,497✔
3217
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,520,746✔
3218
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,519,959✔
3219
      if (code) return code;
1,519,959✔
3220
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,519,959✔
3221
        memset(pColData->aOffset, 0, nOffset);
1,513,968✔
3222
      }
3223
    } else {
3224
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,277,153✔
3225
      code = tRealloc(&pColData->pData, pColData->nData);
3,277,940✔
3226
      if (code) return code;
3,277,940✔
3227
      memset(pColData->pData, 0, pColData->nData);
3,277,940✔
3228
    }
3229
  }
3230

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3319
  pColData->flag |= HAS_NONE;
6,333,842✔
3320
  pColData->numOfNone++;
6,336,191✔
3321

3322
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
6,336,372✔
3323
  code = tRealloc(&pColData->pBitMap, nBit);
6,336,372✔
3324
  if (code) return code;
6,333,150✔
3325

3326
  memset(pColData->pBitMap, 255, nBit);
6,333,150✔
3327
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
6,335,861✔
3328

3329
  return tColDataPutValue(pColData, NULL, 0);
6,336,217✔
3330
}
3331
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,575,140✔
3332
  int32_t code = 0;
19,776,332✔
3333

3334
  pColData->flag |= HAS_NULL;
19,776,332✔
3335
  pColData->numOfNull++;
19,781,352✔
3336

3337
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
19,781,094✔
3338
  code = tRealloc(&pColData->pBitMap, nBit);
19,781,094✔
3339
  if (code) return code;
19,780,969✔
3340

3341
  memset(pColData->pBitMap, 255, nBit);
19,780,969✔
3342
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
19,780,969✔
3343

3344
  return tColDataPutValue(pColData, NULL, 0);
19,780,814✔
3345
}
3346
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
508,303,459✔
3347
  int32_t code = 0;
508,379,695✔
3348

3349
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
508,379,695✔
3350
  if (code) return code;
508,381,263✔
3351

3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
508,381,263✔
3353
  pColData->numOfValue++;
508,383,205✔
3354

3355
  return tColDataPutValue(pColData, pData, nData);
508,383,111✔
3356
}
3357
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
812,538,790✔
3358
  int32_t code = 0;
812,538,790✔
3359

3360
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
812,538,790✔
3361
  if (code) return code;
812,540,196✔
3362

3363
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
812,540,196✔
3364
  pColData->numOfNone++;
812,541,198✔
3365

3366
  return tColDataPutValue(pColData, NULL, 0);
812,540,321✔
3367
}
3368
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,994✔
3369
  int32_t code = 0;
2,994✔
3370

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

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

3378
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
739,962✔
3379
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
736,968✔
3380
  }
3381
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
2,994✔
3382

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

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

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

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

3401
  pColData->flag |= HAS_NONE;
1,730,673✔
3402
  pColData->numOfNone++;
1,730,673✔
3403

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

3408
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
165,370,873✔
3409
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
163,640,200✔
3410
  }
3411
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
1,730,673✔
3412

3413
  tFree(pColData->pBitMap);
1,730,673✔
3414
  pColData->pBitMap = pBitMap;
1,730,673✔
3415

3416
  return tColDataPutValue(pColData, NULL, 0);
1,730,673✔
3417
}
3418
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,365,868,639✔
3419
  int32_t code = 0;
1,366,399,509✔
3420

3421
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,366,399,509✔
3422
  if (code) return code;
1,366,692,583✔
3423
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,366,692,583✔
3424
  pColData->numOfNull++;
1,367,012,715✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
1,366,956,987✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,364,948✔
3429
  int32_t code = 0;
2,364,948✔
3430

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

3436
  return tColDataPutValue(pColData, pData, nData);
2,364,948✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
312,770,427✔
3439
  int32_t code = 0;
312,770,427✔
3440

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

3446
  return tColDataPutValue(pColData, NULL, 0);
312,770,427✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
10,218✔
3449
  int32_t code = 0;
10,218✔
3450

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

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

3468
    //       VALUE                  NONE                     NULL
3469
};
3470

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3834
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,125✔
3835
  pColData->numOfNone--;
2,125✔
3836
  pColData->nVal--;
2,125✔
3837
  if (pColData->numOfNone) {
2,125✔
3838
    return tColDataAppendValue10(pColData, pData, nData);
×
3839
  } else {
3840
    pColData->flag = 0;
2,125✔
3841
    return tColDataAppendValue00(pColData, pData, nData);
2,125✔
3842
  }
3843
}
3844
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,002✔
3845
  pColData->numOfNone--;
1,002✔
3846
  pColData->nVal--;
1,002✔
3847
  if (pColData->numOfNone) {
1,002✔
3848
    return tColDataAppendValue12(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
1,002✔
3851
    return tColDataAppendValue02(pColData, pData, nData);
1,002✔
3852
  }
3853
  return 0;
3854
}
3855
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
93,363✔
3856
  if (forward) {
93,363✔
3857
    pColData->numOfNull--;
93,363✔
3858
    pColData->nVal--;
93,363✔
3859
    if (pColData->numOfNull) {
93,363✔
3860
      return tColDataAppendValue20(pColData, pData, nData);
483✔
3861
    } else {
3862
      pColData->flag = 0;
92,880✔
3863
      return tColDataAppendValue00(pColData, pData, nData);
92,880✔
3864
    }
3865
  }
3866
  return 0;
×
3867
}
3868
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3869
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3870
    pColData->numOfNone--;
×
3871
    pColData->nVal--;
×
3872
    if (pColData->numOfNone) {
×
3873
      return tColDataAppendValue30(pColData, pData, nData);
×
3874
    } else {
3875
      pColData->flag = HAS_NULL;
×
3876
      return tColDataAppendValue20(pColData, pData, nData);
×
3877
    }
3878
  } else if (forward) {  // NULL ==> VALUE
×
3879
    pColData->numOfNull--;
×
3880
    pColData->nVal--;
×
3881
    if (pColData->numOfNull) {
×
3882
      return tColDataAppendValue30(pColData, pData, nData);
×
3883
    } else {
3884
      pColData->flag = HAS_NONE;
×
3885
      return tColDataAppendValue10(pColData, pData, nData);
×
3886
    }
3887
  }
3888
  return 0;
×
3889
}
3890
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,340✔
3891
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
3,340✔
3892
    pColData->numOfNone--;
3,340✔
3893
    pColData->numOfNull++;
3,340✔
3894
    if (pColData->numOfNone) {
3,340✔
3895
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
×
3896
    } else {
3897
      pColData->flag = HAS_NULL;
3,340✔
3898
    }
3899
  }
3900
  return 0;
3,340✔
3901
}
3902
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3903
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3904
    pColData->nVal--;
2,147,483,647✔
3905
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3906
      pColData->nData = pColData->aOffset[pColData->nVal];
1,239,257,654✔
3907
    } else {
3908
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3909
    }
3910
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3911
  }
3912
  return 0;
×
3913
}
3914
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
211,412✔
3915
  if (forward) {  // VALUE ==> NULL
211,412✔
3916
    pColData->numOfValue--;
211,412✔
3917
    pColData->nVal--;
211,412✔
3918
    if (pColData->numOfValue) {
211,412✔
3919
      if (IS_VAR_DATA_TYPE(pColData->type)) {
189,685✔
3920
        pColData->nData = pColData->aOffset[pColData->nVal];
37,800✔
3921
      } else {
3922
        pColData->nData -= TYPE_BYTES[pColData->type];
151,885✔
3923
      }
3924
      return tColDataAppendValue42(pColData, pData, nData);
189,685✔
3925
    } else {
3926
      pColData->flag = 0;
21,727✔
3927
      pColData->nData = 0;
21,727✔
3928
      return tColDataAppendValue02(pColData, pData, nData);
21,727✔
3929
    }
3930
  }
3931
  return 0;
×
3932
}
3933
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
861,618✔
3934
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
861,618✔
3935
    pColData->numOfNone--;
211,680✔
3936
    pColData->nVal--;
211,680✔
3937
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
211,680✔
3938
      pColData->nData -= TYPE_BYTES[pColData->type];
169,344✔
3939
    }
3940
    if (pColData->numOfNone) {
211,680✔
3941
      return tColDataAppendValue50(pColData, pData, nData);
75,600✔
3942
    } else {
3943
      pColData->flag = HAS_VALUE;
136,080✔
3944
      return tColDataAppendValue40(pColData, pData, nData);
136,080✔
3945
    }
3946
  } else if (forward) {  // VALUE ==> VALUE
649,938✔
3947
    pColData->nVal--;
649,938✔
3948
    if (IS_VAR_DATA_TYPE(pColData->type)) {
649,938✔
3949
      pColData->nData = pColData->aOffset[pColData->nVal];
92,700✔
3950
    } else {
3951
      pColData->nData -= TYPE_BYTES[pColData->type];
557,238✔
3952
    }
3953
    return tColDataPutValue(pColData, pData, nData);
649,938✔
3954
  }
3955
  return 0;
×
3956
}
3957
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
11,507✔
3958
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
11,507✔
3959
    pColData->numOfNone--;
11,507✔
3960
    pColData->nVal--;
11,507✔
3961
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
11,507✔
3962
      pColData->nData -= TYPE_BYTES[pColData->type];
9,239✔
3963
    }
3964
    if (pColData->numOfNone) {
11,507✔
3965
      return tColDataAppendValue52(pColData, pData, nData);
×
3966
    } else {
3967
      pColData->flag = HAS_VALUE;
11,507✔
3968
      return tColDataAppendValue42(pColData, pData, nData);
11,507✔
3969
    }
3970
  } else if (forward) {  // VALUE ==> NULL
×
3971
    pColData->numOfValue--;
×
3972
    pColData->nVal--;
×
3973
    if (pColData->numOfValue) {
×
3974
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3975
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3976
      } else {
3977
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3978
      }
3979
      return tColDataAppendValue52(pColData, pData, nData);
×
3980
    } else {
3981
      pColData->flag = HAS_NONE;
×
3982
      pColData->nData = 0;
×
3983
      return tColDataAppendValue12(pColData, pData, nData);
×
3984
    }
3985
  }
3986
  return 0;
×
3987
}
3988
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,165,871✔
3989
  if (forward) {
2,165,871✔
3990
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
2,165,871✔
3991
      pColData->numOfNull--;
1,570,072✔
3992
      pColData->nVal--;
1,570,072✔
3993
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
1,570,072✔
3994
        pColData->nData -= TYPE_BYTES[pColData->type];
1,282,901✔
3995
      }
3996
      if (pColData->numOfNull) {
1,570,072✔
3997
        return tColDataAppendValue60(pColData, pData, nData);
107,104✔
3998
      } else {
3999
        pColData->flag = HAS_VALUE;
1,462,968✔
4000
        return tColDataAppendValue40(pColData, pData, nData);
1,462,968✔
4001
      }
4002
    } else {  // VALUE ==> VALUE
4003
      pColData->nVal--;
595,799✔
4004
      if (IS_VAR_DATA_TYPE(pColData->type)) {
595,799✔
4005
        pColData->nData = pColData->aOffset[pColData->nVal];
115,428✔
4006
      } else {
4007
        pColData->nData -= TYPE_BYTES[pColData->type];
480,371✔
4008
      }
4009
      return tColDataPutValue(pColData, pData, nData);
595,799✔
4010
    }
4011
  }
4012
  return 0;
×
4013
}
4014
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,158,610✔
4015
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,158,610✔
4016
    pColData->numOfValue--;
389,822✔
4017
    pColData->nVal--;
389,822✔
4018
    if (pColData->numOfValue) {
389,822✔
4019
      if (IS_VAR_DATA_TYPE(pColData->type)) {
389,822✔
4020
        pColData->nData = pColData->aOffset[pColData->nVal];
84,794✔
4021
      } else {
4022
        pColData->nData -= TYPE_BYTES[pColData->type];
305,028✔
4023
      }
4024
      return tColDataAppendValue62(pColData, pData, nData);
389,822✔
4025
    } else {
4026
      pColData->flag = HAS_NULL;
×
4027
      pColData->nData = 0;
×
4028
      return tColDataAppendValue20(pColData, pData, nData);
×
4029
    }
4030
  }
4031
  return 0;
768,788✔
4032
}
4033
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
22,044✔
4034
  int32_t code = 0;
22,044✔
4035

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

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

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

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

4147
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
488,927,521✔
4148
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
488,927,521✔
4149
}
489,107,073✔
4150
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,329,623,538✔
4151
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,329,623,538✔
4152
}
1,329,611,802✔
4153
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
520,326✔
4154
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4155
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
520,326✔
4156
    case 0:
47,538✔
4157
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
47,538✔
4158
      break;
47,538✔
4159
    case 1:
472,788✔
4160
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
472,788✔
4161
      break;
472,788✔
4162
    default:
×
4163
      break;
×
4164
  }
4165
}
520,326✔
4166
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4167
  SValue value = {.type = pColData->type};
2,147,483,647✔
4168
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4169
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4170
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4171
    } else {
4172
      value.nData = pColData->nData - pColData->aOffset[iVal];
101,826,164✔
4173
    }
4174
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4175
  } else {
4176
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4177
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4178
  }
4179
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4180
}
2,147,483,647✔
4181
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
403,433,453✔
4182
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4183
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
403,433,453✔
4184
    case 0:
187,582,362✔
4185
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
187,582,362✔
4186
      break;
187,580,330✔
4187
    case 1:
215,897,514✔
4188
      tColDataGetValue4(pColData, iVal, pColVal);
4189
      break;
215,846,607✔
4190
    default:
×
4191
      break;
×
4192
  }
4193
}
403,426,937✔
4194
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4195
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4196
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4197
    case 0:
1,727,241,251✔
4198
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,727,241,251✔
4199
      break;
1,727,182,622✔
4200
    case 1:
2,147,483,647✔
4201
      tColDataGetValue4(pColData, iVal, pColVal);
4202
      break;
2,147,483,647✔
4203
    default:
×
4204
      break;
×
4205
  }
4206
}
2,147,483,647✔
4207
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
4,289,888✔
4208
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4209
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
4,289,888✔
4210
    case 0:
480,272✔
4211
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
480,272✔
4212
      break;
480,272✔
4213
    case 1:
22,034✔
4214
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
22,034✔
4215
      break;
22,034✔
4216
    case 2:
3,787,582✔
4217
      tColDataGetValue4(pColData, iVal, pColVal);
4218
      break;
3,787,582✔
4219
    default:
×
4220
      break;
×
4221
  }
4222
}
4,289,888✔
4223
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4224
    NULL,               // 0
4225
    tColDataGetValue1,  // HAS_NONE
4226
    tColDataGetValue2,  // HAS_NULL
4227
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4228
    tColDataGetValue4,  // HAS_VALUE
4229
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4230
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4231
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4232
};
4233
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4234
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4235
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4236
    return TSDB_CODE_INVALID_PARA;
×
4237
  }
4238
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4239
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4240
}
4241

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

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

4266
  *pColData = *pColDataFrom;
3,190,594✔
4267

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

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

4305
  // value
4306
  if (pColData->nData) {
3,191,398✔
4307
    pColData->pData = xMalloc(arg, pColData->nData);
3,153,568✔
4308
    if (pColData->pData == NULL) {
3,153,611✔
4309
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4310
      goto _exit;
×
4311
    }
4312

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

4318
_exit:
3,191,441✔
4319
  return code;
3,191,441✔
4320
}
4321

4322
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
424,463,967✔
4323
  int32_t code;
4324
  SBuffer local;
424,391,846✔
4325

4326
  if (!(colData->nVal > 0)) {
424,489,334✔
4327
    return TSDB_CODE_INVALID_PARA;
×
4328
  }
4329

4330
  (*info) = (SColDataCompressInfo){
424,488,292✔
4331
      .cmprAlg = info->cmprAlg,
424,494,566✔
4332
      .columnFlag = colData->cflag,
424,497,801✔
4333
      .flag = colData->flag,
424,495,810✔
4334
      .dataType = colData->type,
424,488,823✔
4335
      .columnId = colData->cid,
424,482,866✔
4336
      .numOfData = colData->nVal,
424,484,165✔
4337
  };
4338

4339
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
424,485,882✔
4340
    return 0;
15,713,775✔
4341
  }
4342

4343
  tBufferInit(&local);
4344
  if (assist == NULL) {
408,777,372✔
4345
    assist = &local;
×
4346
  }
4347

4348
  // bitmap
4349
  if (colData->flag != HAS_VALUE) {
408,777,372✔
4350
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
25,591,173✔
4351
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,576,863✔
4352
    } else {
4353
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
24,014,942✔
4354
    }
4355

4356
    SCompressInfo cinfo = {
25,591,711✔
4357
        .dataType = TSDB_DATA_TYPE_TINYINT,
4358
        .cmprAlg = info->cmprAlg,
25,591,711✔
4359
        .originalSize = info->bitmapOriginalSize,
25,590,769✔
4360
    };
4361

4362
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
25,591,347✔
4363
    if (code) {
25,593,130✔
4364
      tBufferDestroy(&local);
4365
      return code;
×
4366
    }
4367

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

4371
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
408,748,716✔
4372
    tBufferDestroy(&local);
4373
    return 0;
23,011✔
4374
  }
4375

4376
  // offset
4377
  if (IS_VAR_DATA_TYPE(colData->type)) {
408,731,974✔
4378
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
47,731,742✔
4379

4380
    SCompressInfo cinfo = {
47,745,421✔
4381
        .dataType = TSDB_DATA_TYPE_INT,
4382
        .cmprAlg = info->cmprAlg,
47,746,149✔
4383
        .originalSize = info->offsetOriginalSize,
47,745,293✔
4384
    };
4385

4386
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
47,748,648✔
4387
    if (code) {
47,750,568✔
4388
      tBufferDestroy(&local);
4389
      return code;
×
4390
    }
4391

4392
    info->offsetCompressedSize = cinfo.compressedSize;
47,750,568✔
4393
  }
4394

4395
  // data
4396
  if (colData->nData > 0) {
408,755,987✔
4397
    info->dataOriginalSize = colData->nData;
408,742,079✔
4398

4399
    SCompressInfo cinfo = {
408,745,482✔
4400
        .dataType = colData->type,
408,744,703✔
4401
        .cmprAlg = info->cmprAlg,
408,741,983✔
4402
        .originalSize = info->dataOriginalSize,
408,739,436✔
4403
    };
4404

4405
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
408,727,511✔
4406
    if (code) {
408,731,252✔
4407
      tBufferDestroy(&local);
4408
      return code;
×
4409
    }
4410

4411
    info->dataCompressedSize = cinfo.compressedSize;
408,731,252✔
4412
  }
4413

4414
  tBufferDestroy(&local);
4415
  return 0;
408,695,220✔
4416
}
4417

4418
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
543,364,585✔
4419
  int32_t  code;
4420
  SBuffer  local;
543,315,596✔
4421
  uint8_t *data = (uint8_t *)input;
543,396,946✔
4422

4423
  tBufferInit(&local);
4424
  if (assist == NULL) {
543,404,630✔
4425
    assist = &local;
×
4426
  }
4427

4428
  tColDataClear(colData);
543,404,630✔
4429
  colData->cid = info->columnId;
543,400,603✔
4430
  colData->type = info->dataType;
543,415,129✔
4431
  colData->cflag = info->columnFlag;
543,419,592✔
4432
  colData->nVal = info->numOfData;
543,393,916✔
4433
  colData->flag = info->flag;
543,403,379✔
4434

4435
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
543,411,936✔
4436
    goto _exit;
72,463,151✔
4437
  }
4438

4439
  // bitmap
4440
  if (info->bitmapOriginalSize > 0) {
470,935,516✔
4441
    SCompressInfo cinfo = {
65,667,610✔
4442
        .dataType = TSDB_DATA_TYPE_TINYINT,
4443
        .cmprAlg = info->cmprAlg,
65,667,765✔
4444
        .originalSize = info->bitmapOriginalSize,
65,667,632✔
4445
        .compressedSize = info->bitmapCompressedSize,
65,661,941✔
4446
    };
4447

4448
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
65,666,977✔
4449
    if (code) {
65,659,013✔
4450
      tBufferDestroy(&local);
4451
      return code;
×
4452
    }
4453

4454
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
65,659,013✔
4455
    if (code) {
65,658,654✔
4456
      tBufferDestroy(&local);
4457
      return code;
×
4458
    }
4459

4460
    data += cinfo.compressedSize;
65,658,654✔
4461
  }
4462

4463
  if (info->flag == (HAS_NONE | HAS_NULL)) {
470,964,476✔
4464
    goto _exit;
24,570✔
4465
  }
4466

4467
  // offset
4468
  if (info->offsetOriginalSize > 0) {
470,911,110✔
4469
    SCompressInfo cinfo = {
94,215,455✔
4470
        .cmprAlg = info->cmprAlg,
94,207,441✔
4471
        .dataType = TSDB_DATA_TYPE_INT,
4472
        .originalSize = info->offsetOriginalSize,
94,220,714✔
4473
        .compressedSize = info->offsetCompressedSize,
94,222,197✔
4474
    };
4475

4476
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
94,211,422✔
4477
    if (code) {
94,215,912✔
4478
      tBufferDestroy(&local);
4479
      return code;
×
4480
    }
4481

4482
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
94,215,912✔
4483
    if (code) {
94,192,298✔
4484
      tBufferDestroy(&local);
4485
      return code;
×
4486
    }
4487

4488
    data += cinfo.compressedSize;
94,192,298✔
4489
  }
4490

4491
  // data
4492
  if (info->dataOriginalSize > 0) {
470,916,928✔
4493
    colData->nData = info->dataOriginalSize;
470,866,001✔
4494

4495
    SCompressInfo cinfo = {
470,861,602✔
4496
        .cmprAlg = info->cmprAlg,
941,694,373✔
4497
        .dataType = colData->type,
470,867,023✔
4498
        .originalSize = info->dataOriginalSize,
470,888,954✔
4499
        .compressedSize = info->dataCompressedSize,
470,881,841✔
4500
    };
4501

4502
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
470,879,249✔
4503
    if (code) {
470,848,913✔
4504
      tBufferDestroy(&local);
4505
      return code;
×
4506
    }
4507

4508
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
470,848,913✔
4509
    if (code) {
470,858,004✔
4510
      tBufferDestroy(&local);
4511
      return code;
×
4512
    }
4513

4514
    data += cinfo.compressedSize;
470,858,004✔
4515
  }
4516

4517
_exit:
543,381,949✔
4518
  switch (colData->flag) {
543,411,897✔
4519
    case HAS_NONE:
15,572,568✔
4520
      colData->numOfNone = colData->nVal;
15,572,568✔
4521
      break;
15,571,793✔
4522
    case HAS_NULL:
56,879,710✔
4523
      colData->numOfNull = colData->nVal;
56,879,710✔
4524
      break;
56,885,943✔
4525
    case HAS_VALUE:
405,237,040✔
4526
      colData->numOfValue = colData->nVal;
405,237,040✔
4527
      break;
405,274,238✔
4528
    default:
65,660,003✔
4529
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4530
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4531
        if (bitValue == 0) {
2,147,483,647✔
4532
          colData->numOfNone++;
219,881,740✔
4533
        } else if (bitValue == 1) {
2,147,483,647✔
4534
          colData->numOfNull++;
2,147,483,647✔
4535
        } else {
4536
          colData->numOfValue++;
2,147,483,647✔
4537
        }
4538
      }
4539
  }
4540
  tBufferDestroy(&local);
4541
  return 0;
543,349,606✔
4542
}
4543

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

4558
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
30,333✔
4559
    if (!IS_STR_DATA_BLOB(type)) {
5,058✔
4560
      for (int32_t i = 0; i < nRows; ++i) {
14,438✔
4561
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
9,380✔
4562
        if (offset == -1) {
9,380✔
4563
          if (pColData->cflag & COL_IS_KEY) {
×
4564
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4565
            goto _exit;
×
4566
          }
4567
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4568
            goto _exit;
×
4569
          }
4570
        } else {
4571
          if (varDataTLen(data + offset) > bytes) {
9,380✔
4572
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4573
                   bytes);
4574
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4575
            goto _exit;
×
4576
          }
4577
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
18,760✔
4578
                                                                        varDataLen(data + offset));
9,380✔
4579
        }
4580
      }
4581
    } else {
4582
      for (int32_t i = 0; i < nRows; ++i) {
×
4583
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4584
        if (offset == -1) {
×
4585
          if (pColData->cflag & COL_IS_KEY) {
×
4586
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4587
            goto _exit;
×
4588
          }
4589
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4590
            goto _exit;
×
4591
          }
4592
        } else {
4593
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4594
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4595
                   (int)blobDataTLen(data + offset), bytes);
4596
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4597
            goto _exit;
×
4598
          }
4599
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4600
                                                                        blobDataLen(data + offset));
×
4601
        }
4602
      }
4603
    }
4604
  } else {  // fixed-length data type
4605
    bool allValue = true;
25,275✔
4606
    bool allNull = true;
25,275✔
4607
    for (int32_t i = 0; i < nRows; ++i) {
71,938✔
4608
      if (!BMIsNull(lengthOrbitmap, i)) {
46,663✔
4609
        allNull = false;
32,601✔
4610
      } else {
4611
        allValue = false;
14,062✔
4612
      }
4613
    }
4614
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
25,275✔
4615
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4616
      goto _exit;
×
4617
    }
4618

4619
    if (allValue) {
25,275✔
4620
      // optimize (todo)
4621
      for (int32_t i = 0; i < nRows; ++i) {
50,803✔
4622
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
31,867✔
4623
      }
4624
    } else if (allNull) {
6,339✔
4625
      // optimize (todo)
4626
      for (int32_t i = 0; i < nRows; ++i) {
17,283✔
4627
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
11,476✔
4628
        if (code) goto _exit;
11,476✔
4629
      }
4630
    } else {
4631
      for (int32_t i = 0; i < nRows; ++i) {
3,852✔
4632
        if (BMIsNull(lengthOrbitmap, i)) {
3,320✔
4633
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,586✔
4634
          if (code) goto _exit;
2,586✔
4635
        } else {
4636
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
734✔
4637
        }
4638
      }
4639
    }
4640
  }
4641

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

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

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

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

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

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

4757
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
2,147,483,647✔
4758
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
1,052,958,372✔
4759
      code = igeos();
7,697,632✔
4760
      if (code) {
7,684,932✔
4761
        return code;
×
4762
      }
4763
    }
4764
    for (int32_t i = 0; i < pBind->num; ++i) {
1,923,153,981✔
4765
      if (pBind->is_null && pBind->is_null[i]) {
965,385,677✔
4766
        if (pColData->cflag & COL_IS_KEY) {
47,626,145✔
4767
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,338✔
4768
          goto _exit;
1,338✔
4769
        }
4770
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
47,625,520✔
4771
        if (code) goto _exit;
47,581,648✔
4772
      } else if (pBind->length[i] > buffMaxLen) {
917,811,612✔
4773
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4774
      } else {
4775
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
918,187,093✔
4776
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
7,714,236✔
4777
          if (code) {
7,682,740✔
4778
            uError("stmt col[%d] bind geometry wrong format", i);
×
4779
            goto _exit;
×
4780
          }
4781
        }
4782
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,835,955,299✔
4783
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
918,145,834✔
4784
      }
4785
    }
4786
  } else {  // fixed-length data type
4787
    bool allValue;
4788
    bool allNull;
4789
    if (pBind->is_null) {
2,147,483,647✔
4790
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
235,916,106✔
4791
      allNull = (same && pBind->is_null[0] != 0);
235,587,815✔
4792
      allValue = (same && pBind->is_null[0] == 0);
235,860,531✔
4793
    } else {
4794
      allNull = false;
2,147,483,647✔
4795
      allValue = true;
2,147,483,647✔
4796
    }
4797

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

4803
    if (allValue) {
2,147,483,647✔
4804
      // optimize (todo)
4805
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4806
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4807
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4808
      }
4809
    } else if (allNull) {
237,421,776✔
4810
      // optimize (todo)
4811
      for (int32_t i = 0; i < pBind->num; ++i) {
468,466,584✔
4812
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
234,121,240✔
4813
        if (code) goto _exit;
234,208,202✔
4814
      }
4815
    } else {
4816
      for (int32_t i = 0; i < pBind->num; ++i) {
11,652,954✔
4817
        if (pBind->is_null[i]) {
8,489,560✔
4818
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,753,152✔
4819
          if (code) goto _exit;
3,753,152✔
4820
        } else {
4821
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,472,816✔
4822
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,736,408✔
4823
        }
4824
      }
4825
    }
4826
  }
4827

4828
_exit:
92,749,875✔
4829
  return code;
2,147,483,647✔
4830
}
4831

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

5096
  int32_t code = 0;
16,334,641✔
5097
  int32_t numOfRows = -1;
16,334,641✔
5098
  SArray *colValArray, *bufArray;
5099
  SColVal colVal;
1,994,365✔
5100
  int32_t numOfFixedValue = 0;
16,334,641✔
5101
  int32_t lino = 0;
16,334,641✔
5102

5103
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
16,334,641✔
5104
    return terrno;
×
5105
  }
5106
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
16,333,612✔
5107
    taosArrayDestroy(colValArray);
×
5108
    return terrno;
×
5109
  }
5110
  for (int i = 0; i < numOfInfos; ++i) {
105,780,399✔
5111
    if (parsedCols) {
89,428,496✔
5112
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
×
5113
      if (pParsedVal) {
×
5114
        continue;
×
5115
      }
5116
    }
5117
    if (numOfRows == -1) {
89,428,496✔
5118
      numOfRows = infos[i].bind->num;
16,334,307✔
5119
    }
5120

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

5128
  SRowKey rowKey, lastRowKey;
2,005,437✔
5129
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
181,984,556✔
5130
    taosArrayClear(colValArray);
165,672,216✔
5131

5132
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
869,638,875✔
5133
      if (parsedCols) {
703,267,440✔
5134
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
×
5135
        if (pParsedVal) {
×
5136
          numOfFixedValue++;
×
5137
          colVal = *pParsedVal;
×
5138

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

5150
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
703,360,418✔
5151
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5152
          if (iInfo == 0) {
×
5153
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5154
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5155
          }
5156
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5157
        } else {
5158
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5159
        }
5160
      } else {
5161
        SValue value = {
703,366,103✔
5162
            .type = infos[iInfo].type,
702,482,672✔
5163
        };
5164
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
703,371,642✔
5165
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
21,560,488✔
5166
            int32_t   length = infos[iInfo].bind->length[iRow];
1,064✔
5167
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5168
            value.nData = length;
×
5169
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5170
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5171
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5172
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5173
              goto _exit;
×
5174
            }
5175
            value.pData = *data;
×
5176
            *data += length;
×
5177
          } else {
5178
            int32_t   length = infos[iInfo].bind->length[iRow];
21,431,484✔
5179
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
21,429,622✔
5180
            value.nData = length;
21,430,948✔
5181
            if (value.nData > infos[iInfo].bytes - VARSTR_HEADER_SIZE) {
21,430,948✔
5182
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5183
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5184
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5185
              goto _exit;
×
5186
            }
5187
            value.pData = *data;
21,433,076✔
5188
            *data += length;
21,434,144✔
5189
          }
5190
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5191
        } else {
5192
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
682,029,771✔
5193
            if (!pSchemaExt) {
×
5194
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5195
              code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
5196
              goto _exit;
×
5197
            }
5198
            uint8_t precision = 0, scale = 0;
×
5199
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5200
            Decimal128 dec = {0};
×
5201
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5202
            int32_t    length = infos[iInfo].bind->length[iRow];
×
5203
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
×
5204
            *data += length;
×
5205
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5206

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

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

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

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

5231
          } else {
5232
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
682,021,441✔
5233
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
681,820,755✔
5234
              *val = 1;
×
5235
            }
5236
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
681,820,755✔
5237
          }
5238
        }
5239
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
704,099,570✔
5240
      }
5241
      if (taosArrayPush(colValArray, &colVal) == NULL) {
703,964,648✔
5242
        code = terrno;
×
5243
        goto _exit;
×
5244
      }
5245
    }
5246

5247
    SRow *row;
13,304,095✔
5248

5249
    if (hasBlob == 0) {
166,312,889✔
5250
      SRowBuildScanInfo sinfo = {0};
165,672,474✔
5251
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
165,673,724✔
5252
      TAOS_CHECK_GOTO(code, &lino, _exit);
165,644,452✔
5253
    } else {
5254
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
640,463✔
5255
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
638,601✔
5256
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5257
    }
5258

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

5264
    if (pOrdered && pDupTs) {
165,610,437✔
5265
      tRowGetKey(row, &rowKey);
331,244,002✔
5266
      if (iRow == 0) {
165,648,199✔
5267
        *pOrdered = true;
16,330,891✔
5268
        *pDupTs = false;
16,329,526✔
5269
      } else {
5270
        if (*pOrdered) {
149,317,308✔
5271
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
149,321,729✔
5272
          *pOrdered = (res >= 0);
149,321,729✔
5273
          if (!*pDupTs) {
149,313,269✔
5274
            *pDupTs = (res == 0);
149,315,931✔
5275
          }
5276
        }
5277
      }
5278
      lastRowKey = rowKey;
165,643,692✔
5279
    }
5280
  }
5281
_exit:
16,303,332✔
5282
  if (code != 0) {
16,311,802✔
5283
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
×
5284
  }
5285
  taosArrayDestroy(colValArray);
16,311,802✔
5286
  taosArrayDestroy(bufArray);
16,337,433✔
5287
  return code;
16,343,226✔
5288
}
5289
/* build rows to `rowArray` from bind
5290
 * `infos` is the bind information array
5291
 * `numOfInfos` is the number of bind information
5292
 * `infoSorted` is whether the bind information is sorted by column id
5293
 * `pTSchema` is the schema of the table
5294
 * `rowArray` is the array to store the rows
5295
 * `pOrdered` is the pointer to store ordered
5296
 * `pDupTs` is the pointer to store duplicateTs
5297
 */
5298
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
×
5299
                                   SArray *rowArray, bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5300
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
5301
    return TSDB_CODE_INVALID_PARA;
×
5302
  }
5303
  int8_t hasBlob = schemaHasBlob(pTSchema);
×
5304
  if (!infoSorted) {
×
5305
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5306
  }
5307

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

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

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

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

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

5390
    SRow *row;
×
5391

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

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

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

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

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

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

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

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

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

5503
  return code;
46,913,440✔
5504
}
5505

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

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

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

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

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

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

5538
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
1,981,021,618✔
5539
  SColVal cv;
1,932,858,645✔
5540

5541
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
1,981,223,753✔
5542
  key->numOfPKs = 0;
1,982,617,625✔
5543

5544
  for (int i = 1; i < nColData; i++) {
1,987,755,003✔
5545
    if (aColData[i].cflag & COL_IS_KEY) {
1,987,535,788✔
5546
      tColDataGetValue4(&aColData[i], iRow, &cv);
5,202,112✔
5547
      key->pks[key->numOfPKs++] = cv.value;
5,202,112✔
5548
    } else {
5549
      break;
1,982,391,318✔
5550
    }
5551
  }
5552
}
1,981,935,521✔
5553

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

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

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

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

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

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

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

5600
  return TSDB_CODE_SUCCESS;
326,442✔
5601
}
5602

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

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

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

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

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

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

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

5629
  if (nVal < 2) return TSDB_CODE_SUCCESS;
1,004✔
5630

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

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

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

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

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

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

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

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

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

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

5705
int32_t tColDataSortMerge(SArray **arr) {
974,977✔
5706
  SArray   *colDataArr = *arr;
974,977✔
5707
  int32_t   nColData = TARRAY_SIZE(colDataArr);
975,837✔
5708
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
975,837✔
5709

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

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

5722
  int8_t doSort = 0;
945,600✔
5723
  int8_t doMerge = 0;
945,600✔
5724
  // scan -------
5725
  SRowKey lastKey;
842,615✔
5726
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
945,600✔
5727
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
1,092,889,867✔
5728
    SRowKey key;
1,042,874,585✔
5729
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
1,091,476,466✔
5730

5731
    int32_t c = tRowKeyCompare(&lastKey, &key);
1,091,969,573✔
5732
    if (c < 0) {
1,091,969,573✔
5733
      lastKey = key;
1,091,360,765✔
5734
      continue;
1,091,360,765✔
5735
    } else if (c > 0) {
637,443✔
5736
      doSort = 1;
1,004✔
5737
      break;
1,004✔
5738
    } else {
5739
      doMerge = 1;
636,439✔
5740
    }
5741
  }
5742

5743
  // sort -------
5744
  if (doSort) {
1,830,524✔
5745
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
1,004✔
5746
  }
5747

5748
  if ((doMerge != 1) && (doSort == 1)) {
1,867,277✔
5749
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
1,004✔
5750
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
327,446✔
5751
      SRowKey key;
326,442✔
5752
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
326,442✔
5753

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

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

5769
_exit:
973,886✔
5770
  return 0;
975,840✔
5771
}
5772

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

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

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

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

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

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

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

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

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

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

5846
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
19,353,574✔
5847
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
19,353,678✔
5848
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
19,352,237✔
5849
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
19,352,897✔
5850

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

5867
  // value
5868
  if (pColData->flag & HAS_VALUE) {
9,675,468✔
5869
    if (IS_VAR_DATA_TYPE(pColData->type)) {
9,613,132✔
5870
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
585,449✔
5871
      if (code) return code;
582,948✔
5872

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

5876
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
582,948✔
5877
      if (code) return code;
582,691✔
5878
    } else {
5879
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
9,029,632✔
5880
      if (code) return code;
9,031,148✔
5881
    }
5882
  }
5883

5884
  return code;
9,678,119✔
5885
}
5886

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

5890
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
9,561,615✔
5891
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
9,561,787✔
5892
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
9,561,557✔
5893
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
9,561,241✔
5894

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

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

5915
  // value
5916
  if (pColData->flag & HAS_VALUE) {
4,780,915✔
5917
    if (IS_VAR_DATA_TYPE(pColData->type)) {
4,745,178✔
5918
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
236,123✔
5919
      if (code) return code;
235,635✔
5920

5921
      code = tDecodeI32v(pDecoder, &pColData->nData);
235,635✔
5922
      if (code) return code;
235,635✔
5923

5924
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
235,635✔
5925
      if (code) return code;
235,635✔
5926
    } else {
5927
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
4,509,012✔
5928
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
4,509,715✔
5929
      if (code) return code;
4,509,414✔
5930
    }
5931
  }
5932
  pColData->cflag = 0;
4,780,829✔
5933

5934
  return code;
4,780,837✔
5935
}
5936

5937
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
9,677,192✔
5938
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
9,677,192✔
5939
  if (code) return code;
9,676,179✔
5940
  return tEncodeI8(pEncoder, pColData->cflag);
19,353,438✔
5941
}
5942

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

5947
  code = tDecodeI8(pDecoder, &pColData->cflag);
4,780,759✔
5948
  return code;
4,781,009✔
5949
}
5950

5951
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
9,674,800✔
5952
  if (version == 0) {
9,674,800✔
5953
    return tEncodeColDataVersion0(pEncoder, pColData);
×
5954
  } else if (version == 1) {
9,674,800✔
5955
    return tEncodeColDataVersion1(pEncoder, pColData);
×
5956
  } else if (version == 2) {
9,674,800✔
5957
    int32_t posStart = pEncoder->pos;
9,676,690✔
5958
    pEncoder->pos += INT_BYTES;
9,678,036✔
5959
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
9,678,468✔
5960
    if (code) return code;
9,677,080✔
5961
    int32_t posEnd = pEncoder->pos;
9,677,080✔
5962
    int32_t pos = posEnd - posStart;
9,678,023✔
5963
    pEncoder->pos = posStart;
9,678,023✔
5964
    code = tEncodeI32(pEncoder, pos);
9,676,236✔
5965
    pEncoder->pos = posEnd;
9,676,236✔
5966
    return code;
9,676,988✔
5967
  } else {
UNCOV
5968
    return TSDB_CODE_INVALID_PARA;
×
5969
  }
5970
}
5971

5972
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
7,972,313✔
5973
  if (version == 0) {
7,972,313✔
5974
    return tDecodeColDataVersion0(pDecoder, pColData);
×
5975
  } else if (version == 1) {
7,972,313✔
5976
    return tDecodeColDataVersion1(pDecoder, pColData);
×
5977
  } else if (version == 2) {
7,972,313✔
5978
    if (jump) {
7,972,356✔
5979
      int32_t len = 0;
3,191,843✔
5980
      int32_t code = tDecodeI32(pDecoder, &len);
3,191,843✔
5981
      if (code) return code;
3,191,843✔
5982
      pDecoder->pos += (len - INT_BYTES);
3,191,843✔
5983
      return TSDB_CODE_SUCCESS;
3,191,843✔
5984
    } else {
5985
      pDecoder->pos += INT_BYTES;
4,780,513✔
5986
      return tDecodeColDataVersion1(pDecoder, pColData);
4,780,958✔
5987
    }    
5988
  } else {
5989
    return TSDB_CODE_INVALID_PARA;
×
5990
  }
5991
}
5992

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

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

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

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

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

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

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

6021
  // if (pRow->type) {
6022
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
45,348✔
6023
  while (pIter) {
53,101,290✔
6024
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
53,055,942✔
6025
    int32_t  idx = *(int32_t *)pIter;
53,055,942✔
6026
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
53,055,942✔
6027
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
53,055,942✔
6028

6029
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
53,055,942✔
6030
  }
6031
  //}
6032
  for (int32_t i = 0; i < nSeq; i++) {
53,101,290✔
6033
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
53,055,942✔
6034
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
106,111,884✔
6035
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
106,111,884✔
6036
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
106,111,884✔
6037
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
106,111,884✔
6038
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
106,111,884✔
6039
  }
6040

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

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

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

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

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

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

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

6083
  for (int32_t i = 0; i < nSeq; i++) {
26,550,645✔
6084
    SBlobValue value;
26,527,971✔
6085
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
26,527,971✔
6086
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
26,527,971✔
6087
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
26,527,971✔
6088
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
26,527,971✔
6089
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
26,527,971✔
6090
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
53,055,942✔
6091
      TAOS_CHECK_EXIT(terrno);
×
6092
    }
6093
  }
6094

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

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

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

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

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

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

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

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

6163
  int8_t val;
6164
  if (HAS_VALUE == pColData->flag) {
3,081,541✔
6165
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6166
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6167
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6168
    }
6169
  } else {
6170
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,944,863✔
6171
      switch (tColDataGetBitValue(pColData, iVal)) {
125,913,000✔
6172
        case 0:
6,663,851✔
6173
        case 1:
6174
          (*numOfNull)++;
6,663,851✔
6175
          break;
6,663,851✔
6176
        case 2:
119,249,149✔
6177
          val = ((int8_t *)pColData->pData)[iVal];
119,249,149✔
6178
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,249,149✔
6179
          break;
119,249,149✔
6180
        default:
×
6181
          break;
×
6182
      }
6183
    }
6184
  }
6185
}
3,078,514✔
6186

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

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

6219
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
20,192,337✔
6220
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
20,192,337✔
6221
  int16_t *numOfNull = &pAggs->numOfNull;
20,192,578✔
6222
  *sum = 0;
20,191,609✔
6223
  *max = INT32_MIN;
20,191,652✔
6224
  *min = INT32_MAX;
20,191,979✔
6225
  *numOfNull = 0;
20,192,139✔
6226

6227
  int32_t val;
6228
  if (HAS_VALUE == pColData->flag) {
20,192,225✔
6229
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6230
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6231
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6232
    }
6233
  } else {
6234
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
266,305,592✔
6235
      switch (tColDataGetBitValue(pColData, iVal)) {
266,196,321✔
6236
        case 0:
26,300,021✔
6237
        case 1:
6238
          (*numOfNull)++;
26,300,021✔
6239
          break;
26,300,021✔
6240
        case 2:
239,896,300✔
6241
          val = ((int32_t *)pColData->pData)[iVal];
239,896,300✔
6242
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
239,896,300✔
6243
          break;
239,896,300✔
6244
        default:
×
6245
          break;
×
6246
      }
6247
    }
6248
  }
6249
}
20,450,551✔
6250

6251
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
74,348,231✔
6252
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
74,348,231✔
6253
  int16_t *numOfNull = &pAggs->numOfNull;
74,349,938✔
6254
  *sum = 0;
74,348,821✔
6255
  *max = INT64_MIN;
74,348,821✔
6256
  *min = INT64_MAX;
74,349,938✔
6257
  *numOfNull = 0;
74,349,938✔
6258

6259
  int64_t val;
6260
  if (HAS_VALUE == pColData->flag) {
74,349,938✔
6261
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6262
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6263
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6264
    }
6265
  } else {
6266
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
425,264,132✔
6267
      switch (tColDataGetBitValue(pColData, iVal)) {
425,048,721✔
6268
        case 0:
62,369,303✔
6269
        case 1:
6270
          (*numOfNull)++;
62,369,303✔
6271
          break;
62,369,303✔
6272
        case 2:
362,679,418✔
6273
          val = ((int64_t *)pColData->pData)[iVal];
362,679,418✔
6274
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
362,679,418✔
6275
          break;
362,679,418✔
6276
        default:
×
6277
          break;
×
6278
      }
6279
    }
6280
  }
6281
}
83,394,611✔
6282

6283
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
35,432,286✔
6284
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
35,432,286✔
6285
  int16_t *numOfNull = &pAggs->numOfNull;
35,432,888✔
6286
  *(double *)sum = 0;
35,432,286✔
6287
  *(double *)max = -FLT_MAX;
35,432,888✔
6288
  *(double *)min = FLT_MAX;
35,432,888✔
6289
  *numOfNull = 0;
35,432,103✔
6290

6291
  float val;
6292
  if (HAS_VALUE == pColData->flag) {
35,432,705✔
6293
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6294
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6295
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6296
    }
6297
  } else {
6298
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
135,688,119✔
6299
      switch (tColDataGetBitValue(pColData, iVal)) {
135,641,322✔
6300
        case 0:
7,649,633✔
6301
        case 1:
6302
          (*numOfNull)++;
7,649,633✔
6303
          break;
7,649,633✔
6304
        case 2:
127,991,689✔
6305
          val = ((float *)pColData->pData)[iVal];
127,991,689✔
6306
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
127,991,689✔
6307
          break;
127,991,689✔
6308
        default:
×
6309
          break;
×
6310
      }
6311
    }
6312
  }
6313
}
35,916,929✔
6314

6315
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
3,535,183✔
6316
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
3,535,183✔
6317
  int16_t *numOfNull = &pAggs->numOfNull;
3,535,355✔
6318
  *(double *)sum = 0;
3,535,226✔
6319
  *(double *)max = -DBL_MAX;
3,535,312✔
6320
  *(double *)min = DBL_MAX;
3,535,355✔
6321
  *numOfNull = 0;
3,535,355✔
6322

6323
  double val;
6324
  if (HAS_VALUE == pColData->flag) {
3,535,355✔
6325
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6326
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6327
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6328
    }
6329
  } else {
6330
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,944,863✔
6331
      switch (tColDataGetBitValue(pColData, iVal)) {
125,913,000✔
6332
        case 0:
6,624,616✔
6333
        case 1:
6334
          (*numOfNull)++;
6,624,616✔
6335
          break;
6,624,616✔
6336
        case 2:
119,288,384✔
6337
          val = ((double *)pColData->pData)[iVal];
119,288,384✔
6338
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
119,288,384✔
6339
          break;
119,288,384✔
6340
        default:
×
6341
          break;
×
6342
      }
6343
    }
6344
  }
6345
}
2,278,835✔
6346

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

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

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

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

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

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

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

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

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

6483
  switch (pColData->flag) {
7,768,109✔
6484
    case HAS_NONE:
×
6485
    case HAS_NULL:
6486
    case (HAS_NONE | HAS_NULL):
6487
      *numOfNull = pColData->nVal;
×
6488
      break;
×
6489
    case HAS_VALUE:
7,701,828✔
6490
      *numOfNull = 0;
7,701,828✔
6491
      break;
7,701,284✔
6492
    case (HAS_VALUE | HAS_NULL):
65,737✔
6493
    case (HAS_VALUE | HAS_NONE):
6494
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
258,950,737✔
6495
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
258,885,000✔
6496
          (*numOfNull)++;
13,137,845✔
6497
        }
6498
      }
6499
      break;
65,737✔
6500
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6501
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6502
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6503
          (*numOfNull)++;
×
6504
        }
6505
      }
6506
      break;
×
6507
    default:
×
6508
      break;
×
6509
  }
6510
}
7,767,021✔
6511

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

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

6534
  Decimal64         *pVal = NULL;
35,043✔
6535
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
35,043✔
6536
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
35,043✔
6537
  if (HAS_VALUE == pColData->flag) {
35,043✔
6538
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
456,318✔
6539
      pVal = ((Decimal64 *)pColData->pData) + iVal;
451,800✔
6540
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
451,800✔
6541
    }
6542
  } else {
6543
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
22,520,325✔
6544
      switch (tColDataGetBitValue(pColData, iVal)) {
22,489,800✔
6545
        case 0:
501,753✔
6546
        case 1:
6547
          pAggs->numOfNull++;
501,753✔
6548
          break;
501,753✔
6549
        case 2:
21,988,047✔
6550
          pVal = ((Decimal64 *)pColData->pData) + iVal;
21,988,047✔
6551
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
21,988,047✔
6552
          break;
21,988,047✔
6553
        default:
×
6554
          break;
×
6555
      }
6556
    }
6557
  }
6558
}
35,043✔
6559

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

6570
  Decimal128        *pVal = NULL;
84,325✔
6571
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
84,325✔
6572
  if (HAS_VALUE == pColData->flag) {
84,325✔
6573
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
224,523✔
6574
      pVal = ((Decimal128 *)pColData->pData) + iVal;
222,300✔
6575
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
222,300✔
6576
    }
6577
  } else {
6578
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
72,083,402✔
6579
      switch (tColDataGetBitValue(pColData, iVal)) {
72,001,300✔
6580
        case 0:
1,664,594✔
6581
        case 1:
6582
          pAggs->numOfNull++;
1,664,594✔
6583
          break;
1,664,594✔
6584
        case 2:
70,336,706✔
6585
          pVal = ((Decimal128 *)pColData->pData) + iVal;
70,336,706✔
6586
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
70,336,706✔
6587
          break;
70,336,706✔
6588
        default:
×
6589
          break;
×
6590
      }
6591
    }
6592
  }
6593
}
84,325✔
6594

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

6620
// SValueColumn ================================
6621
int32_t tValueColumnInit(SValueColumn *valCol) {
620,327,018✔
6622
  valCol->type = TSDB_DATA_TYPE_NULL;
620,327,018✔
6623
  valCol->numOfValues = 0;
620,354,741✔
6624
  tBufferInit(&valCol->data);
620,357,704✔
6625
  tBufferInit(&valCol->offsets);
620,373,909✔
6626
  return 0;
620,402,896✔
6627
}
6628

6629
void tValueColumnDestroy(SValueColumn *valCol) {
1,530,380,428✔
6630
  valCol->type = TSDB_DATA_TYPE_NULL;
1,530,380,428✔
6631
  valCol->numOfValues = 0;
1,530,437,222✔
6632
  tBufferDestroy(&valCol->data);
1,530,480,717✔
6633
  tBufferDestroy(&valCol->offsets);
1,530,434,359✔
6634
  return;
1,530,387,992✔
6635
}
6636

6637
void tValueColumnClear(SValueColumn *valCol) {
810,234,701✔
6638
  valCol->type = TSDB_DATA_TYPE_NULL;
810,234,701✔
6639
  valCol->numOfValues = 0;
810,288,268✔
6640
  tBufferClear(&valCol->data);
810,292,540✔
6641
  tBufferClear(&valCol->offsets);
810,289,026✔
6642
  return;
810,283,964✔
6643
}
6644

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

6648
  if (valCol->numOfValues == 0) {
1,065,444✔
6649
    valCol->type = value->type;
642,748✔
6650
  }
6651

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

6656
  if (IS_VAR_DATA_TYPE(value->type)) {
1,065,625✔
6657
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
776,699✔
6658
      return code;
×
6659
    }
6660
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
776,544✔
6661
      return code;
×
6662
    }
6663
  } else {
6664
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
677,508✔
6665
    if (code) return code;
677,508✔
6666
  }
6667
  valCol->numOfValues++;
1,065,780✔
6668

6669
  return 0;
1,065,780✔
6670
}
6671

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

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

6679
  if (IS_VAR_DATA_TYPE(valCol->type)) {
40,614,799✔
6680
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
864,245✔
6681
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
865,019✔
6682
    int32_t  oldDataSize = nextOffset - offsets[idx];
865,019✔
6683
    int32_t  bytesAdded = value->nData - oldDataSize;
865,019✔
6684

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

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

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

6708
  value->type = valCol->type;
125,251,900✔
6709
  if (IS_VAR_DATA_TYPE(value->type)) {
142,846,743✔
6710
    int32_t       offset, nextOffset;
17,595,955✔
6711
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
17,593,086✔
6712

6713
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
17,592,107✔
6714
    if (idx == valCol->numOfValues - 1) {
17,591,640✔
6715
      nextOffset = tBufferGetSize(&valCol->data);
10,617,108✔
6716
    } else {
6717
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
6,975,333✔
6718
    }
6719
    value->nData = nextOffset - offset;
17,592,833✔
6720
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
17,592,727✔
6721
  } else {
6722
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
107,657,135✔
6723
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
215,324,311✔
6724
  }
6725
  return 0;
125,255,619✔
6726
}
6727

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

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

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

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

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

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

6755
  // data
6756
  SCompressInfo cinfo = {
642,748✔
6757
      .cmprAlg = info->cmprAlg,
1,285,496✔
6758
      .dataType = valCol->type,
642,748✔
6759
      .originalSize = valCol->data.size,
642,748✔
6760
  };
6761

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

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

6768
  return 0;
642,748✔
6769
}
6770

6771
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
27,843,727✔
6772
                               SBuffer *assist) {
6773
  int32_t code;
6774

6775
  tValueColumnClear(valCol);
27,843,727✔
6776
  valCol->type = info->type;
27,845,229✔
6777
  // offset
6778
  if (IS_VAR_DATA_TYPE(valCol->type)) {
38,141,187✔
6779
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
10,291,407✔
6780

6781
    SCompressInfo cinfo = {
10,294,572✔
6782
        .dataType = TSDB_DATA_TYPE_INT,
6783
        .cmprAlg = info->cmprAlg,
10,295,037✔
6784
        .originalSize = info->offsetOriginalSize,
10,292,769✔
6785
        .compressedSize = info->offsetCompressedSize,
10,293,128✔
6786
    };
6787

6788
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
10,295,061✔
6789
    if (code) {
10,297,198✔
6790
      return code;
×
6791
    }
6792
  } else {
6793
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
17,550,575✔
6794
  }
6795

6796
  // data
6797
  SCompressInfo cinfo = {
27,848,573✔
6798
      .dataType = valCol->type,
27,848,704✔
6799
      .cmprAlg = info->cmprAlg,
27,849,324✔
6800
      .originalSize = info->dataOriginalSize,
27,849,014✔
6801
      .compressedSize = info->dataCompressedSize,
27,846,877✔
6802
  };
6803

6804
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
27,847,985✔
6805
  if (code) {
27,849,812✔
6806
    return code;
×
6807
  }
6808

6809
  return 0;
27,849,812✔
6810
}
6811

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

6816
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,285,496✔
6817
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,285,496✔
6818
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,285,496✔
6819
  if (IS_VAR_DATA_TYPE(info->type)) {
642,748✔
6820
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
473,168✔
6821
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
473,168✔
6822
  }
6823
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,285,496✔
6824
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,285,496✔
6825

6826
  return 0;
642,748✔
6827
}
6828

6829
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
27,848,907✔
6830
  int32_t code;
6831
  uint8_t fmtVer;
27,848,907✔
6832

6833
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
27,849,372✔
6834
  if (fmtVer == 0) {
27,849,502✔
6835
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
27,849,502✔
6836
    if ((code = tBufferGetI8(reader, &info->type))) return code;
27,849,837✔
6837
    if (IS_VAR_DATA_TYPE(info->type)) {
27,849,763✔
6838
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
10,296,554✔
6839
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
10,294,596✔
6840
    } else {
6841
      info->offsetOriginalSize = 0;
17,552,794✔
6842
      info->offsetCompressedSize = 0;
17,552,280✔
6843
    }
6844
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
27,849,348✔
6845
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
27,848,499✔
6846
  } else {
6847
    return TSDB_CODE_INVALID_PARA;
×
6848
  }
6849

6850
  return 0;
27,851,411✔
6851
}
6852

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

6862
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
765,087,960✔
6863
  if (!(outputSize >= extraSizeNeeded)) {
765,154,728✔
6864
    return TSDB_CODE_INVALID_PARA;
×
6865
  }
6866

6867
  if (info->cmprAlg == NO_COMPRESSION) {
765,154,728✔
6868
    (void)memcpy(output, input, info->originalSize);
97,008✔
6869
    info->compressedSize = info->originalSize;
97,008✔
6870
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
871,831,918✔
6871
    SBuffer local;
106,741,228✔
6872

6873
    tBufferInit(&local);
6874
    if (buffer == NULL) {
106,787,094✔
6875
      buffer = &local;
×
6876
    }
6877

6878
    if (info->cmprAlg == TWO_STAGE_COMP) {
106,787,094✔
6879
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
106,670,079✔
6880
      if (code) {
106,650,130✔
6881
        tBufferDestroy(&local);
6882
        return code;
×
6883
      }
6884
    }
6885

6886
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
320,303,781✔
6887
        input,                                                   // input
6888
        info->originalSize,                                      // input size
6889
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
106,782,432✔
6890
        output,                                                  // output
6891
        outputSize,                                              // output size
6892
        info->cmprAlg,                                           // compression algorithm
106,776,926✔
6893
        buffer->data,                                            // buffer
6894
        buffer->capacity                                         // buffer size
106,778,612✔
6895
    );
6896
    if (info->compressedSize < 0) {
106,787,870✔
6897
      tBufferDestroy(&local);
6898
      return TSDB_CODE_COMPRESS_ERROR;
×
6899
    }
6900

6901
    tBufferDestroy(&local);
6902
  } else {
6903
    DEFINE_VAR(info->cmprAlg)
658,265,868✔
6904
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
658,264,032✔
6905
      (void)memcpy(output, input, info->originalSize);
7,248✔
6906
      info->compressedSize = info->originalSize;
7,248✔
6907
      return 0;
7,248✔
6908
    }
6909
    SBuffer local;
658,130,723✔
6910

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

6917
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
1,974,619,906✔
6918
        input,                                                      // input
6919
        info->originalSize,                                         // input size
6920
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
658,264,272✔
6921
        output,                                                     // output
6922
        outputSize,                                                 // output size
6923
        info->cmprAlg,                                              // compression algorithm
6924
        buffer->data,                                               // buffer
6925
        buffer->capacity                                            // buffer size
658,243,943✔
6926
    );
6927
    if (info->compressedSize < 0) {
658,254,795✔
6928
      tBufferDestroy(&local);
6929
      return TSDB_CODE_COMPRESS_ERROR;
×
6930
    }
6931

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

6936
  return 0;
765,110,588✔
6937
}
6938

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

6947
  if (!(outputSize >= info->originalSize)) {
2,147,483,647✔
6948
    return TSDB_CODE_INVALID_PARA;
×
6949
  }
6950

6951
  if (info->cmprAlg == NO_COMPRESSION) {
2,147,483,647✔
6952
    if (!(info->compressedSize == info->originalSize)) {
40,224✔
6953
      return TSDB_CODE_INVALID_PARA;
×
6954
    }
6955
    (void)memcpy(output, input, info->compressedSize);
40,224✔
6956
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
6957
    SBuffer local;
1,023,173,423✔
6958

6959
    tBufferInit(&local);
6960
    if (buffer == NULL) {
1,023,297,508✔
6961
      buffer = &local;
×
6962
    }
6963

6964
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,023,297,508✔
6965
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,022,668,896✔
6966
      if (code) {
1,022,670,633✔
6967
        tBufferDestroy(&local);
6968
        return code;
×
6969
      }
6970
    }
6971

6972
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,046,577,840✔
6973
        input,                                                  // input
6974
        info->compressedSize,                                   // inputSize
1,023,299,922✔
6975
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,023,308,021✔
6976
        output,                                                 // output
6977
        outputSize,                                             // output size
6978
        info->cmprAlg,                                          // compression algorithm
1,023,314,613✔
6979
        buffer->data,                                           // helper buffer
6980
        buffer->capacity                                        // extra buffer size
1,023,296,652✔
6981
    );
6982
    if (decompressedSize < 0) {
1,023,192,005✔
6983
      tBufferDestroy(&local);
6984
      return TSDB_CODE_COMPRESS_ERROR;
×
6985
    }
6986

6987
    if (!(decompressedSize == info->originalSize)) {
1,023,192,005✔
6988
      return TSDB_CODE_COMPRESS_ERROR;
×
6989
    }
6990
    tBufferDestroy(&local);
6991
  } else {
6992
    DEFINE_VAR(info->cmprAlg);
1,135,637,820✔
6993
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
1,135,616,954✔
6994
      (void)memcpy(output, input, info->compressedSize);
12,684✔
6995
      return 0;
12,684✔
6996
    }
6997
    SBuffer local;
1,135,512,046✔
6998

6999
    tBufferInit(&local);
7000
    if (buffer == NULL) {
1,135,643,196✔
7001
      buffer = &local;
×
7002
    }
7003
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,135,643,196✔
7004
    if (code) {
1,135,603,447✔
7005
      return code;
×
7006
    }
7007

7008
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7009
        input,                                                  // input
7010
        info->compressedSize,                                   // inputSize
1,135,652,146✔
7011
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,135,648,160✔
7012
        output,                                                 // output
7013
        outputSize,                                             // output size
7014
        info->cmprAlg,                                          // compression algorithm
1,135,640,100✔
7015
        buffer->data,                                           // helper buffer
7016
        buffer->capacity                                        // extra buffer size
1,135,640,405✔
7017
    );
7018
    if (decompressedSize < 0) {
1,135,500,949✔
7019
      tBufferDestroy(&local);
7020
      return TSDB_CODE_COMPRESS_ERROR;
×
7021
    }
7022

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

7029
  return 0;
2,147,483,647✔
7030
}
7031

7032
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
765,058,587✔
7033
  int32_t code;
7034

7035
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
765,058,587✔
7036
  if (code) return code;
765,031,663✔
7037

7038
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
765,031,663✔
7039
  if (code) return code;
765,072,727✔
7040

7041
  output->size += info->compressedSize;
765,072,727✔
7042
  return 0;
765,122,421✔
7043
}
7044

7045
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,023,197,220✔
7046
  int32_t code;
7047

7048
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
1,023,197,220✔
7049
  if (code) return code;
1,022,921,936✔
7050

7051
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,022,921,936✔
7052
  if (code) return code;
1,023,194,114✔
7053

7054
  output->size += info->originalSize;
1,023,194,114✔
7055
  return 0;
1,023,227,193✔
7056
}
7057

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

7083
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
204,046,242✔
7084
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
204,046,242✔
7085
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
31,050,670✔
7086
    pDst->nData = pSrc->nData;
31,061,590✔
7087
  } else {
7088
    pDst->val = pSrc->val;
172,995,572✔
7089
  }
7090
}
204,064,129✔
7091
void valueClearDatum(SValue *pVal, int8_t type) {
×
7092
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7093
    taosMemoryFreeClear(pVal->pData);
×
7094
    pVal->nData = 0;
×
7095
  } else {
7096
    pVal->val = 0;
×
7097
  }
7098
}
×
7099

7100
int8_t schemaHasBlob(const STSchema *pSchema) {
553,414,658✔
7101
  if (pSchema == NULL) {
553,414,658✔
7102
    return 0;
×
7103
  }
7104
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7105
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7106
      return 1;
32,118✔
7107
    }
7108
  }
7109
  return 0;
553,386,071✔
7110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc