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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

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

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

89.07
/source/libs/scalar/src/sclvector.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
#include "os.h"
17

18
#include "decimal.h"
19
#include "filter.h"
20
#include "filterInt.h"
21
#include "geosWrapper.h"
22
#include "query.h"
23
#include "querynodes.h"
24
#include "sclInt.h"
25
#include "sclvector.h"
26
#include "tcompare.h"
27
#include "tdatablock.h"
28
#include "tdataformat.h"
29
#include "tdef.h"
30
#include "ttime.h"
31
#include "ttypes.h"
32

33
#define LEFT_COL  ((pLeftCol->info.type == TSDB_DATA_TYPE_JSON ? (void *)pLeftCol : pLeftCol->pData))
34
#define RIGHT_COL ((pRightCol->info.type == TSDB_DATA_TYPE_JSON ? (void *)pRightCol : pRightCol->pData))
35

36
#define IS_NULL                                                                              \
37
  colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i) ||         \
38
      IS_JSON_NULL(pLeft->columnData->info.type, colDataGetVarData(pLeft->columnData, i)) || \
39
      IS_JSON_NULL(pRight->columnData->info.type, colDataGetVarData(pRight->columnData, i))
40

41
#define IS_HELPER_NULL(col, i) colDataIsNull_s(col, i) || IS_JSON_NULL(col->info.type, colDataGetVarData(col, i))
42

43
bool noConvertBeforeCompare(int32_t leftType, int32_t rightType, int32_t optr) {
146,093,697✔
44
  return (TSDB_DATA_TYPE_NULL == leftType || TSDB_DATA_TYPE_NULL == rightType) ||
284,234,021✔
45
         (!IS_DECIMAL_TYPE(leftType) && !IS_DECIMAL_TYPE(rightType) && IS_NUMERIC_TYPE(leftType) &&
138,140,324✔
46
          IS_NUMERIC_TYPE(rightType) && (optr >= OP_TYPE_GREATER_THAN && optr <= OP_TYPE_NOT_EQUAL));
98,823,377✔
47
}
48

49
bool compareForType(__compar_fn_t fp, int32_t optr, SColumnInfoData *pColL, int32_t idxL, SColumnInfoData *pColR,
50
                    int32_t idxR);
51
bool compareForTypeWithColAndHash(__compar_fn_t fp, int32_t optr, SColumnInfoData *pColL, int32_t idxL,
52
                                  const void *hashData, int32_t hashType, STypeMod hashTypeMod);
53

54
static int32_t vectorMathBinaryOpForDecimal(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t step,
55
                                            int32_t i, EOperatorType op);
56

57
static int32_t vectorMathUnaryOpForDecimal(SScalarParam *pCol, SScalarParam *pOut, int32_t step, int32_t i,
58
                                           EOperatorType op);
59

60
int32_t convertNumberToNumber(const void *inData, void *outData, int8_t inType, int8_t outType) {
137,016✔
61
  switch (outType) {
137,016✔
62
    case TSDB_DATA_TYPE_BOOL: {
1,200✔
63
      GET_TYPED_DATA(*((bool *)outData), bool, inType, inData, 0);
1,200✔
64
      break;
1,200✔
65
    }
66
    case TSDB_DATA_TYPE_TINYINT: {
×
67
      GET_TYPED_DATA(*((int8_t *)outData), int8_t, inType, inData, 0);
×
68
      break;
×
69
    }
70
    case TSDB_DATA_TYPE_SMALLINT: {
×
71
      GET_TYPED_DATA(*((int16_t *)outData), int16_t, inType, inData, 0);
×
72
      break;
×
73
    }
74
    case TSDB_DATA_TYPE_INT: {
×
75
      GET_TYPED_DATA(*((int32_t *)outData), int32_t, inType, inData, 0);
×
76
      break;
×
77
    }
78
    case TSDB_DATA_TYPE_BIGINT:
×
79
    case TSDB_DATA_TYPE_TIMESTAMP: {
80
      GET_TYPED_DATA(*((int64_t *)outData), int64_t, inType, inData, 0);
×
81
      break;
×
82
    }
83
    case TSDB_DATA_TYPE_UTINYINT: {
×
84
      GET_TYPED_DATA(*((uint8_t *)outData), uint8_t, inType, inData, 0);
×
85
      break;
×
86
    }
87
    case TSDB_DATA_TYPE_USMALLINT: {
×
88
      GET_TYPED_DATA(*((uint16_t *)outData), uint16_t, inType, inData, 0);
×
89
      break;
×
90
    }
91
    case TSDB_DATA_TYPE_UINT: {
×
92
      GET_TYPED_DATA(*((uint32_t *)outData), uint32_t, inType, inData, 0);
×
93
      break;
×
94
    }
95
    case TSDB_DATA_TYPE_UBIGINT: {
×
96
      GET_TYPED_DATA(*((uint64_t *)outData), uint64_t, inType, inData, 0);
×
97
      break;
×
98
    }
99
    case TSDB_DATA_TYPE_FLOAT: {
×
100
      GET_TYPED_DATA(*((float *)outData), float, inType, inData, 0);
×
101
      break;
×
102
    }
103
    case TSDB_DATA_TYPE_DOUBLE: {
135,816✔
104
      GET_TYPED_DATA(*((double *)outData), double, inType, inData, 0);
135,816✔
105
      break;
135,816✔
106
    }
107
    default: {
×
108
      return TSDB_CODE_SCALAR_CONVERT_ERROR;
×
109
    }
110
  }
111
  return TSDB_CODE_SUCCESS;
137,016✔
112
}
113

114
int32_t convertNcharToDouble(const void *inData, void *outData) {
7,002✔
115
  int32_t code = TSDB_CODE_SUCCESS;
7,002✔
116
  char   *tmp = taosMemoryMalloc(varDataTLen(inData));
7,002✔
117
  if (NULL == tmp) {
7,002✔
118
    SCL_ERR_RET(terrno);
×
119
  }
120
  int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(inData), varDataLen(inData), tmp, NULL);
7,002✔
121
  if (len < 0) {
7,002✔
122
    sclError("castConvert taosUcs4ToMbs error 1");
×
123
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
124
  }
125

126
  tmp[len] = 0;
7,002✔
127

128
  double value = taosStr2Double(tmp, NULL);
7,002✔
129

130
  *((double *)outData) = value;
7,002✔
131

132
_return:
7,002✔
133
  taosMemoryFreeClear(tmp);
7,002✔
134
  SCL_RET(code);
7,002✔
135
}
136

137
typedef int32_t (*_getBigintValue_fn_t)(void *src, int32_t index, int64_t *res);
138

139
int32_t getVectorBigintValue_TINYINT(void *src, int32_t index, int64_t *res) {
15,349,167✔
140
  *res = (int64_t) * ((int8_t *)src + index);
15,349,167✔
141
  SCL_RET(TSDB_CODE_SUCCESS);
15,352,043✔
142
}
143
int32_t getVectorBigintValue_UTINYINT(void *src, int32_t index, int64_t *res) {
1,799✔
144
  *res = (int64_t) * ((uint8_t *)src + index);
1,799✔
145
  SCL_RET(TSDB_CODE_SUCCESS);
1,799✔
146
}
147
int32_t getVectorBigintValue_SMALLINT(void *src, int32_t index, int64_t *res) {
19,701,026✔
148
  *res = (int64_t) * ((int16_t *)src + index);
19,701,026✔
149
  SCL_RET(TSDB_CODE_SUCCESS);
19,701,934✔
150
}
151
int32_t getVectorBigintValue_USMALLINT(void *src, int32_t index, int64_t *res) {
1,999✔
152
  *res = (int64_t) * ((uint16_t *)src + index);
1,999✔
153
  SCL_RET(TSDB_CODE_SUCCESS);
1,999✔
154
}
155
int32_t getVectorBigintValue_INT(void *src, int32_t index, int64_t *res) {
25,469,003✔
156
  *res = (int64_t) * ((int32_t *)src + index);
25,469,003✔
157
  SCL_RET(TSDB_CODE_SUCCESS);
25,468,956✔
158
}
159
int32_t getVectorBigintValue_UINT(void *src, int32_t index, int64_t *res) {
1,999✔
160
  *res = (int64_t) * ((uint32_t *)src + index);
1,999✔
161
  SCL_RET(TSDB_CODE_SUCCESS);
1,999✔
162
}
163
int32_t getVectorBigintValue_BIGINT(void *src, int32_t index, int64_t *res) {
2,147,483,647✔
164
  *res = (int64_t) * ((int64_t *)src + index);
2,147,483,647✔
165
  SCL_RET(TSDB_CODE_SUCCESS);
2,147,483,647✔
166
}
167
int32_t getVectorBigintValue_UBIGINT(void *src, int32_t index, int64_t *res) {
200✔
168
  *res = (int64_t) * ((uint64_t *)src + index);
200✔
169
  SCL_RET(TSDB_CODE_SUCCESS);
200✔
170
}
171
int32_t getVectorBigintValue_FLOAT(void *src, int32_t index, int64_t *res) {
10,594✔
172
  *res = (int64_t) * ((float *)src + index);
10,594✔
173
  SCL_RET(TSDB_CODE_SUCCESS);
10,594✔
174
}
175
int32_t getVectorBigintValue_DOUBLE(void *src, int32_t index, int64_t *res) {
17,957✔
176
  *res = (int64_t) * ((double *)src + index);
17,957✔
177
  SCL_RET(TSDB_CODE_SUCCESS);
17,957✔
178
}
179
int32_t getVectorBigintValue_BOOL(void *src, int32_t index, int64_t *res) {
7,396,244✔
180
  *res = (int64_t) * ((bool *)src + index);
7,396,244✔
181
  SCL_RET(TSDB_CODE_SUCCESS);
7,395,761✔
182
}
183

184
int32_t getVectorBigintValue_JSON(void *src, int32_t index, int64_t *res) {
6,400✔
185
  if (colDataIsNull_var(((SColumnInfoData *)src), index)) {
6,400✔
186
    sclError("getVectorBigintValue_JSON get json data null with index %d", index);
×
187
    SCL_ERR_RET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
188
  }
189
  char  *data = colDataGetVarData((SColumnInfoData *)src, index);
6,400✔
190
  double out = 0;
6,400✔
191
  if (*data == TSDB_DATA_TYPE_NULL) {
6,400✔
192
    *res = 0;
×
193
    SCL_RET(TSDB_CODE_SUCCESS);
×
194
  } else if (*data == TSDB_DATA_TYPE_NCHAR) {  // json inner type can not be BINARY
6,400✔
195
    SCL_ERR_RET(convertNcharToDouble(data + CHAR_BYTES, &out));
1,600✔
196
  } else if (tTagIsJson(data)) {
4,800✔
197
    *res = 0;
×
198
    SCL_ERR_RET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
×
199
  } else {
200
    SCL_ERR_RET(convertNumberToNumber(data + CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE));
4,800✔
201
  }
202
  *res = (int64_t)out;
6,400✔
203
  SCL_RET(TSDB_CODE_SUCCESS);
6,400✔
204
}
205

206
int32_t getVectorBigintValueFn(int32_t srcType, _getBigintValue_fn_t *p) {
173,459,131✔
207
  *p = NULL;
173,459,131✔
208
  if (srcType == TSDB_DATA_TYPE_TINYINT) {
173,530,040✔
209
    *p = getVectorBigintValue_TINYINT;
7,801,291✔
210
  } else if (srcType == TSDB_DATA_TYPE_UTINYINT) {
165,728,749✔
211
    *p = getVectorBigintValue_UTINYINT;
3,398✔
212
  } else if (srcType == TSDB_DATA_TYPE_SMALLINT) {
165,725,351✔
213
    *p = getVectorBigintValue_SMALLINT;
9,972,859✔
214
  } else if (srcType == TSDB_DATA_TYPE_USMALLINT) {
155,752,492✔
215
    *p = getVectorBigintValue_USMALLINT;
3,598✔
216
  } else if (srcType == TSDB_DATA_TYPE_INT) {
155,748,894✔
217
    *p = getVectorBigintValue_INT;
12,871,160✔
218
  } else if (srcType == TSDB_DATA_TYPE_UINT) {
142,877,734✔
219
    *p = getVectorBigintValue_UINT;
3,598✔
220
  } else if (srcType == TSDB_DATA_TYPE_BIGINT) {
142,874,136✔
221
    *p = getVectorBigintValue_BIGINT;
52,462,918✔
222
  } else if (srcType == TSDB_DATA_TYPE_UBIGINT) {
90,411,218✔
223
    *p = getVectorBigintValue_UBIGINT;
200✔
224
  } else if (srcType == TSDB_DATA_TYPE_FLOAT) {
90,411,018✔
225
    *p = getVectorBigintValue_FLOAT;
1,266✔
226
  } else if (srcType == TSDB_DATA_TYPE_DOUBLE) {
90,409,752✔
227
    *p = getVectorBigintValue_DOUBLE;
3,565✔
228
  } else if (srcType == TSDB_DATA_TYPE_TIMESTAMP) {
90,406,187✔
229
    *p = getVectorBigintValue_BIGINT;
86,629,987✔
230
  } else if (srcType == TSDB_DATA_TYPE_BOOL) {
3,776,200✔
231
    *p = getVectorBigintValue_BOOL;
3,763,672✔
232
  } else if (srcType == TSDB_DATA_TYPE_JSON) {
12,528✔
233
    *p = getVectorBigintValue_JSON;
8,000✔
234
  } else if (srcType == TSDB_DATA_TYPE_NULL) {
4,528✔
235
    *p = NULL;
×
236
  } else {
237
    sclError("getVectorBigintValueFn invalid srcType : %d", srcType);
4,528✔
238
    return TSDB_CODE_SCALAR_CONVERT_ERROR;
×
239
  }
240
  return TSDB_CODE_SUCCESS;
173,530,186✔
241
}
242

243
static FORCE_INLINE int32_t varToTimestamp(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
1,086,516✔
244
  int64_t value = 0;
1,086,516✔
245
  int32_t code = TSDB_CODE_SUCCESS;
1,086,516✔
246
  if (taosParseTime(buf, &value, strlen(buf), pOut->columnData->info.precision, pOut->tz) != TSDB_CODE_SUCCESS) {
1,086,516✔
247
    value = 0;
1,086,480✔
248
  }
249

250
  colDataSetInt64(pOut->columnData, rowIndex, &value);
1,086,516✔
251
  SCL_RET(code);
1,086,516✔
252
}
253

254
static FORCE_INLINE int32_t varToDecimal(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
×
255
  Decimal *pDec = (Decimal *)colDataGetData(pOut->columnData, rowIndex);
×
256
  int32_t code = decimalFromStr(buf, strlen(buf), pOut->columnData->info.precision, pOut->columnData->info.scale, pDec);
×
257
  if (TSDB_CODE_SUCCESS != code) {
×
258
    if (overflow) *overflow = code == TSDB_CODE_DECIMAL_OVERFLOW;
×
259
    SCL_RET(code);
×
260
  }
261
  SCL_RET(code);
×
262
}
263

264
static FORCE_INLINE int32_t varToSigned(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
65,926,788✔
265
  if (overflow) {
65,926,788✔
266
    int64_t minValue = tDataTypes[pOut->columnData->info.type].minValue;
1,267,233✔
267
    int64_t maxValue = tDataTypes[pOut->columnData->info.type].maxValue;
1,267,233✔
268
    int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
1,267,233✔
269
    if (value > maxValue) {
1,267,233✔
270
      *overflow = 1;
×
271
      SCL_RET(TSDB_CODE_SUCCESS);
×
272
    } else if (value < minValue) {
1,267,233✔
273
      *overflow = -1;
×
274
      SCL_RET(TSDB_CODE_SUCCESS);
×
275
    } else {
276
      *overflow = 0;
1,267,233✔
277
    }
278
  }
279

280
  switch (pOut->columnData->info.type) {
65,926,788✔
281
    case TSDB_DATA_TYPE_TINYINT: {
×
282
      int8_t value = (int8_t)taosStr2Int8(buf, NULL, 10);
×
283

284
      colDataSetInt8(pOut->columnData, rowIndex, (int8_t *)&value);
×
285
      break;
×
286
    }
287
    case TSDB_DATA_TYPE_SMALLINT: {
×
288
      int16_t value = (int16_t)taosStr2Int16(buf, NULL, 10);
×
289
      colDataSetInt16(pOut->columnData, rowIndex, (int16_t *)&value);
×
290
      break;
×
291
    }
292
    case TSDB_DATA_TYPE_INT: {
×
293
      int32_t value = (int32_t)taosStr2Int32(buf, NULL, 10);
×
294
      colDataSetInt32(pOut->columnData, rowIndex, (int32_t *)&value);
×
295
      break;
×
296
    }
297
    case TSDB_DATA_TYPE_BIGINT: {
65,924,666✔
298
      int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
65,924,666✔
299
      colDataSetInt64(pOut->columnData, rowIndex, (int64_t *)&value);
65,922,747✔
300
      break;
65,924,851✔
301
    }
302
  }
303
  SCL_RET(TSDB_CODE_SUCCESS);
65,923,122✔
304
}
305

306
static FORCE_INLINE int32_t varToUnsigned(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
1,494,624✔
307
  if (overflow) {
1,494,624✔
308
    uint64_t minValue = (uint64_t)tDataTypes[pOut->columnData->info.type].minValue;
915,168✔
309
    uint64_t maxValue = (uint64_t)tDataTypes[pOut->columnData->info.type].maxValue;
915,168✔
310
    uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
915,168✔
311
    if (value > maxValue) {
915,168✔
312
      *overflow = 1;
×
313
      SCL_RET(TSDB_CODE_SUCCESS);
×
314
    } else if (value < minValue) {
915,168✔
315
      *overflow = -1;
×
316
      SCL_RET(TSDB_CODE_SUCCESS);
×
317
    } else {
318
      *overflow = 0;
915,168✔
319
    }
320
  }
321

322
  switch (pOut->columnData->info.type) {
1,494,624✔
323
    case TSDB_DATA_TYPE_UTINYINT: {
48,288✔
324
      uint8_t value = (uint8_t)taosStr2UInt8(buf, NULL, 10);
48,288✔
325
      colDataSetInt8(pOut->columnData, rowIndex, (int8_t *)&value);
48,288✔
326
      break;
48,288✔
327
    }
328
    case TSDB_DATA_TYPE_USMALLINT: {
48,288✔
329
      uint16_t value = (uint16_t)taosStr2UInt16(buf, NULL, 10);
48,288✔
330
      colDataSetInt16(pOut->columnData, rowIndex, (int16_t *)&value);
48,288✔
331
      break;
48,288✔
332
    }
333
    case TSDB_DATA_TYPE_UINT: {
322,368✔
334
      uint32_t value = (uint32_t)taosStr2UInt32(buf, NULL, 10);
322,368✔
335
      colDataSetInt32(pOut->columnData, rowIndex, (int32_t *)&value);
322,368✔
336
      break;
322,368✔
337
    }
338
    case TSDB_DATA_TYPE_UBIGINT: {
1,075,680✔
339
      uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
1,075,680✔
340
      colDataSetInt64(pOut->columnData, rowIndex, (int64_t *)&value);
1,075,680✔
341
      break;
1,075,680✔
342
    }
343
  }
344
  SCL_RET(TSDB_CODE_SUCCESS);
1,494,624✔
345
}
346

347
static FORCE_INLINE int32_t varToFloat(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
750,363,334✔
348
  if (TSDB_DATA_TYPE_FLOAT == pOut->columnData->info.type) {
750,363,334✔
349
    float value = taosStr2Float(buf, NULL);
888,424✔
350
    colDataSetFloat(pOut->columnData, rowIndex, &value);
888,424✔
351
    SCL_RET(TSDB_CODE_SUCCESS);
888,424✔
352
  }
353

354
  double value = taosStr2Double(buf, NULL);
749,485,771✔
355
  colDataSetDouble(pOut->columnData, rowIndex, &value);
749,509,025✔
356
  SCL_RET(TSDB_CODE_SUCCESS);
749,481,704✔
357
}
358

359
static FORCE_INLINE int32_t varToBool(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
53,928✔
360
  int64_t value = taosStr2Int64(buf, NULL, 10);
53,928✔
361
  bool    v = (value != 0) ? true : false;
53,928✔
362
  colDataSetInt8(pOut->columnData, rowIndex, (int8_t *)&v);
53,928✔
363
  SCL_RET(TSDB_CODE_SUCCESS);
53,928✔
364
}
365

366
// todo remove this malloc
367
static FORCE_INLINE int32_t varToVarbinary(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
255,125✔
368
  if (isHex(varDataVal(buf), varDataLen(buf))) {
255,125✔
369
    if (!isValidateHex(varDataVal(buf), varDataLen(buf))) {
556✔
370
      SCL_ERR_RET(TSDB_CODE_PAR_INVALID_VARBINARY);
×
371
    }
372

373
    void    *data = NULL;
556✔
374
    uint32_t size = 0;
556✔
375
    if (taosHex2Ascii(varDataVal(buf), varDataLen(buf), &data, &size) < 0) {
556✔
376
      SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
×
377
    }
378
    int32_t inputLen = size + VARSTR_HEADER_SIZE;
556✔
379
    char   *t = taosMemoryCalloc(1, inputLen);
556✔
380
    if (t == NULL) {
556✔
381
      sclError("Out of memory");
×
382
      taosMemoryFree(data);
×
383
      SCL_ERR_RET(terrno);
×
384
    }
385
    varDataSetLen(t, size);
556✔
386
    (void)memcpy(varDataVal(t), data, size);
556✔
387
    int32_t code = colDataSetVal(pOut->columnData, rowIndex, t, false);
556✔
388
    taosMemoryFree(t);
556✔
389
    taosMemoryFree(data);
556✔
390
    SCL_ERR_RET(code);
556✔
391
  } else {
392
    int32_t inputLen = varDataTLen(buf);
254,569✔
393
    char   *t = taosMemoryCalloc(1, inputLen);
254,569✔
394
    if (t == NULL) {
254,569✔
395
      sclError("Out of memory");
×
396
      SCL_ERR_RET(terrno);
×
397
    }
398
    (void)memcpy(t, buf, inputLen);
254,569✔
399
    int32_t code = colDataSetVal(pOut->columnData, rowIndex, t, false);
254,569✔
400
    taosMemoryFree(t);
254,569✔
401
    SCL_ERR_RET(code);
254,569✔
402
  }
403
  SCL_RET(TSDB_CODE_SUCCESS);
255,125✔
404
}
405

406
static FORCE_INLINE int32_t varToVarbinaryBlob(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
×
407
  if (isHex(blobDataVal(buf), blobDataLen(buf))) {
×
408
    if (!isValidateHex(blobDataVal(buf), blobDataLen(buf))) {
×
409
      SCL_ERR_RET(TSDB_CODE_PAR_INVALID_VARBINARY);
×
410
    }
411

412
    void    *data = NULL;
×
413
    uint32_t size = 0;
×
414
    if (taosHex2Ascii(blobDataVal(buf), blobDataLen(buf), &data, &size) < 0) {
×
415
      SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
×
416
    }
417
    int32_t inputLen = size + BLOBSTR_HEADER_SIZE;
×
418
    char   *t = taosMemoryCalloc(1, inputLen);
×
419
    if (t == NULL) {
×
420
      sclError("Out of memory");
×
421
      taosMemoryFree(data);
×
422
      SCL_ERR_RET(terrno);
×
423
    }
424
    blobDataSetLen(t, size);
×
425
    (void)memcpy(blobDataVal(t), data, size);
×
426
    int32_t code = colDataSetVal(pOut->columnData, rowIndex, t, false);
×
427
    taosMemoryFree(t);
×
428
    taosMemoryFree(data);
×
429
    SCL_ERR_RET(code);
×
430
  } else {
431
    int32_t inputLen = blobDataTLen(buf);
×
432
    char   *t = taosMemoryCalloc(1, inputLen);
×
433
    if (t == NULL) {
×
434
      sclError("Out of memory");
×
435
      SCL_ERR_RET(terrno);
×
436
    }
437
    (void)memcpy(t, buf, inputLen);
×
438
    int32_t code = colDataSetVal(pOut->columnData, rowIndex, t, false);
×
439
    taosMemoryFree(t);
×
440
    SCL_ERR_RET(code);
×
441
  }
442
  SCL_RET(TSDB_CODE_SUCCESS);
×
443
}
444

445
static FORCE_INLINE int32_t varToNchar(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
5,540✔
446
  int32_t len = 0;
5,540✔
447
  int32_t inputLen = varDataLen(buf);
19,260✔
448
  int32_t outputMaxLen = (inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
19,260✔
449
  int32_t code = TSDB_CODE_SUCCESS;
19,260✔
450

451
  char *t = taosMemoryCalloc(1, outputMaxLen);
19,260✔
452
  if (NULL == t) {
19,260✔
453
    SCL_ERR_RET(terrno);
×
454
  }
455
  int32_t ret = taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4 *)varDataVal(t), outputMaxLen - VARSTR_HEADER_SIZE,
19,260✔
456
                              &len, pOut->charsetCxt);
457
  if (!ret) {
19,260✔
458
    sclError("failed to convert to NCHAR");
×
459
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
460
  }
461
  varDataSetLen(t, len);
19,260✔
462

463
  SCL_ERR_JRET(colDataSetVal(pOut->columnData, rowIndex, t, false));
19,260✔
464

465
_return:
19,260✔
466
  taosMemoryFree(t);
19,260✔
467
  SCL_RET(code);
19,260✔
468
}
469

470
static FORCE_INLINE int32_t ncharToVar(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
1,514,825,457✔
471
  int32_t code = TSDB_CODE_SUCCESS;
1,514,825,457✔
472
  int32_t inputLen = varDataLen(buf);
1,514,825,457✔
473

474
  char *t = taosMemoryCalloc(1, inputLen + VARSTR_HEADER_SIZE);
1,514,824,535✔
475
  if (NULL == t) {
1,514,659,417✔
476
    SCL_ERR_RET(terrno);
×
477
  }
478
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t), pOut->charsetCxt);
1,514,659,417✔
479
  if (len < 0) {
1,514,797,495✔
480
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
481
  }
482
  varDataSetLen(t, len);
1,514,797,495✔
483

484
  SCL_ERR_JRET(colDataSetVal(pOut->columnData, rowIndex, t, false));
1,514,815,407✔
485

486
_return:
1,514,816,879✔
487
  taosMemoryFree(t);
1,514,816,879✔
488
  SCL_RET(code);
1,514,795,848✔
489
}
490

491
static FORCE_INLINE int32_t varToGeometry(char *buf, SScalarParam *pOut, int32_t rowIndex, int8_t *overflow) {
124,348✔
492
#ifdef USE_GEOS
493
  //[ToDo] support to parse WKB as well as WKT
494
  int32_t        code = TSDB_CODE_SUCCESS;
124,348✔
495
  size_t         len = 0;
124,348✔
496
  unsigned char *t = NULL;
124,348✔
497
  char          *output = NULL;
124,348✔
498

499
  if ((code = initCtxGeomFromText()) != 0) {
124,348✔
500
    sclError("failed to init geometry ctx, %s", getGeosErrMsg(code));
×
501
    SCL_ERR_JRET(TSDB_CODE_APP_ERROR);
×
502
  }
503
  if ((code = doGeomFromText(buf, &t, &len)) != 0) {
124,348✔
504
    sclInfo("failed to convert text to geometry, %s", getGeosErrMsg(code));
84,504✔
505
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
84,504✔
506
  }
507

508
  output = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE);
39,844✔
509
  if (NULL == output) {
39,844✔
510
    SCL_ERR_JRET(terrno);
×
511
  }
512
  (void)memcpy(output + VARSTR_HEADER_SIZE, t, len);
39,844✔
513
  varDataSetLen(output, len);
39,844✔
514

515
  SCL_ERR_JRET(colDataSetVal(pOut->columnData, rowIndex, output, false));
39,844✔
516

517
  taosMemoryFree(output);
39,844✔
518
  geosFreeBuffer(t);
39,844✔
519

520
  SCL_RET(TSDB_CODE_SUCCESS);
39,844✔
521

522
_return:
84,504✔
523
  taosMemoryFree(output);
84,504✔
524
  geosFreeBuffer(t);
84,504✔
525
  t = NULL;
84,504✔
526
  VarDataLenT dummyHeader = 0;
84,504✔
527
  SCL_ERR_RET(colDataSetVal(pOut->columnData, rowIndex, (const char *)&dummyHeader, false));
84,504✔
528
  SCL_RET(code);
84,504✔
529
#else
530
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
531
#endif
532
}
533

534
// TODO opt performance, tmp is not needed.
535
int32_t vectorConvertFromVarData(SSclVectorConvCtx *pCtx, int8_t *overflow) {
210,222,707✔
536
  int32_t code = TSDB_CODE_SUCCESS;
210,222,707✔
537
  bool    vton = false;
210,222,707✔
538

539
  _bufConverteFunc func = NULL;
210,222,707✔
540
  if (TSDB_DATA_TYPE_BOOL == pCtx->outType) {
210,222,707✔
541
    func = varToBool;
54,728✔
542
  } else if (IS_SIGNED_NUMERIC_TYPE(pCtx->outType)) {
210,182,461✔
543
    func = varToSigned;
4,480,323✔
544
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->outType)) {
205,701,259✔
545
    func = varToUnsigned;
1,229,040✔
546
  } else if (IS_FLOAT_TYPE(pCtx->outType)) {
204,502,957✔
547
    func = varToFloat;
196,551,990✔
548
  } else if ((pCtx->outType == TSDB_DATA_TYPE_VARCHAR || pCtx->outType == TSDB_DATA_TYPE_VARBINARY) &&
7,939,302✔
549
             pCtx->inType == TSDB_DATA_TYPE_NCHAR) {  // nchar -> binary
6,976,024✔
550
    func = ncharToVar;
6,776,926✔
551
    vton = true;
6,776,926✔
552
  } else if (pCtx->outType == TSDB_DATA_TYPE_NCHAR &&
1,168,511✔
553
             (pCtx->inType == TSDB_DATA_TYPE_VARCHAR || pCtx->inType == TSDB_DATA_TYPE_VARBINARY)) {  // binary -> nchar
5,540✔
554
    func = varToNchar;
5,540✔
555
    vton = true;
5,540✔
556
  } else if (TSDB_DATA_TYPE_TIMESTAMP == pCtx->outType) {
1,159,141✔
557
    func = varToTimestamp;
814,896✔
558
  } else if (TSDB_DATA_TYPE_GEOMETRY == pCtx->outType) {
344,245✔
559
    func = varToGeometry;
142,456✔
560
  } else if (TSDB_DATA_TYPE_VARBINARY == pCtx->outType) {
201,789✔
561
    func = varToVarbinary;
200,801✔
562
    vton = true;
200,801✔
563
  } else if (IS_DECIMAL_TYPE(pCtx->outType)) {
1,002✔
564
    func = varToDecimal;
×
565
  } else if (IS_STR_DATA_BLOB(pCtx->outType)) {
1,002✔
566
    func = varToVarbinaryBlob;
×
567
    vton = true;
×
568
  } else {
569
    sclError("invalid convert outType:%d, inType:%d", pCtx->outType, pCtx->inType);
1,002✔
570
    SCL_ERR_RET(TSDB_CODE_APP_ERROR);
1,002✔
571
  }
572

573
  pCtx->pOut->numOfRows = pCtx->pIn->numOfRows;
210,256,686✔
574
  char *tmp = NULL;
210,227,105✔
575

576
  for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
2,147,483,647✔
577
    if (IS_HELPER_NULL(pCtx->pIn->columnData, i)) {
2,147,483,647✔
578
      colDataSetNULL(pCtx->pOut->columnData, i);
827,912,472✔
579
      continue;
827,880,526✔
580
    }
581

582
    char   *data = colDataGetVarData(pCtx->pIn->columnData, i);
2,147,483,647✔
583
    int32_t convertType = pCtx->inType;
2,147,483,647✔
584
    if (pCtx->inType == TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
585
      if (*data == TSDB_DATA_TYPE_NCHAR) {
1,600✔
586
        data += CHAR_BYTES;
400✔
587
        convertType = TSDB_DATA_TYPE_NCHAR;
400✔
588
      } else if (tTagIsJson(data) || *data == TSDB_DATA_TYPE_NULL) {
1,200✔
589
        SCL_ERR_JRET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
×
590
      } else {
591
        SCL_ERR_JRET(convertNumberToNumber(data + CHAR_BYTES, colDataGetNumData(pCtx->pOut->columnData, i), *data,
1,200✔
592
                                           pCtx->outType));
593
        continue;
1,200✔
594
      }
595
    }
596

597
    int32_t bufSize = pCtx->pIn->columnData->info.bytes;
2,147,483,647✔
598
    int32_t actualSize = vton ? varDataTLen(data) : (varDataLen(data) + 1);
2,147,483,647✔
599

600
    // Reallocate buffer if actual data size exceeds allocated buffer
601
    if (tmp == NULL || actualSize > bufSize) {
2,147,483,647✔
602
      if (tmp != NULL) {
197,118,221✔
603
        taosMemoryFree(tmp);
2,540✔
604
      }
605
      tmp = taosMemoryMalloc(TMAX(bufSize, actualSize));
197,118,221✔
606
      if (tmp == NULL) {
197,112,856✔
607
        sclError("out of memory in vectorConvertFromVarData");
×
608
        SCL_ERR_JRET(terrno);
×
609
      }
610
      bufSize = TMAX(bufSize, actualSize);
197,112,856✔
611
    }
612

613
    if (vton) {
2,147,483,647✔
614
      (void)memcpy(tmp, data, varDataTLen(data));
1,515,083,096✔
615
    } else {
616
      if (TSDB_DATA_TYPE_VARCHAR == convertType || TSDB_DATA_TYPE_GEOMETRY == convertType ||
819,081,271✔
617
          TSDB_DATA_TYPE_VARBINARY == convertType) {
618
        (void)memcpy(tmp, varDataVal(data), varDataLen(data));
487,793,969✔
619
        tmp[varDataLen(data)] = 0;
487,806,143✔
620
      } else if (TSDB_DATA_TYPE_NCHAR == convertType) {
331,287,302✔
621
        // we need to convert it to native char string, and then perform the string to numeric data
622
        if (varDataLen(data) > bufSize) {
331,295,643✔
623
          sclError("castConvert convert buffer size too small");
×
624
          SCL_ERR_JRET(TSDB_CODE_APP_ERROR);
×
625
        }
626

627
        int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(data), varDataLen(data), tmp, pCtx->pIn->charsetCxt);
331,298,425✔
628
        if (len < 0) {
331,296,795✔
629
          sclError("castConvert taosUcs4ToMbs error 1");
×
630
          SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
631
        }
632

633
        tmp[len] = 0;
331,296,795✔
634
      }
635
    }
636

637
    SCL_ERR_JRET((*func)(tmp, pCtx->pOut, i, overflow ? overflow + i : NULL));
2,147,483,647✔
638
  }
639

640
_return:
210,164,479✔
641
  if (tmp != NULL) {
210,248,983✔
642
    taosMemoryFreeClear(tmp);
197,139,246✔
643
  }
644
  SCL_RET(code);
210,239,014✔
645
}
646

647
int32_t getVectorDoubleValue_JSON(void *src, int32_t index, double *out) {
21,107✔
648
  char *data = colDataGetVarData((SColumnInfoData *)src, index);
21,107✔
649
  *out = 0;
21,107✔
650
  if (*data == TSDB_DATA_TYPE_NULL) {
21,107✔
651
    SCL_RET(TSDB_CODE_SUCCESS);
×
652
  } else if (*data == TSDB_DATA_TYPE_NCHAR) {  // json inner type can not be BINARY
21,107✔
653
    SCL_ERR_RET(convertNcharToDouble(data + CHAR_BYTES, out));
5,402✔
654
  } else if (tTagIsJson(data)) {
15,705✔
655
    SCL_ERR_RET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
501✔
656
  } else {
657
    SCL_ERR_RET(convertNumberToNumber(data + CHAR_BYTES, out, *data, TSDB_DATA_TYPE_DOUBLE));
15,204✔
658
  }
659
  SCL_RET(TSDB_CODE_SUCCESS);
20,606✔
660
}
661

662
int32_t ncharTobinary(void *buf, void **out, void *charsetCxt) {  // todo need to remove , if tobinary is nchar
54,373✔
663
  int32_t inputLen = varDataTLen(buf);
54,373✔
664

665
  *out = taosMemoryCalloc(1, inputLen);
54,373✔
666
  if (NULL == *out) {
54,373✔
667
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed, since memory alloc failed.",
×
668
             DEFAULT_UNICODE_ENCODEC, charsetCxt != NULL ? ((SConvInfo *)(charsetCxt))->charset : tsCharset,
669
             (char *)varDataVal(buf));
670
    SCL_ERR_RET(terrno);
×
671
  }
672
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(*out), charsetCxt);
54,373✔
673
  if (len < 0) {
54,373✔
674
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed.", DEFAULT_UNICODE_ENCODEC,
×
675
             charsetCxt != NULL ? ((SConvInfo *)(charsetCxt))->charset : tsCharset, (char *)varDataVal(buf));
676
    taosMemoryFree(*out);
×
677
    SCL_ERR_RET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
678
  }
679
  varDataSetLen(*out, len);
54,373✔
680
  SCL_RET(TSDB_CODE_SUCCESS);
54,373✔
681
}
682

683
int32_t convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData,
2,147,483,647✔
684
                         char **pRightData, void *pLeftOut, void *pRightOut, bool *isNull, bool *freeLeft,
685
                         bool *freeRight, bool *result, void *charsetCxt) {
686
  *result = false;
2,147,483,647✔
687
  if (optr == OP_TYPE_JSON_CONTAINS) {
2,147,483,647✔
688
    *result = true;
×
689
    return TSDB_CODE_SUCCESS;
×
690
  }
691

692
  if (typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON) {
2,147,483,647✔
693
    *result = true;
2,147,483,647✔
694
    return TSDB_CODE_SUCCESS;
2,147,483,647✔
695
  }
696

697
  if (typeLeft == TSDB_DATA_TYPE_JSON) {
421,325✔
698
    if (tTagIsJson(*pLeftData)) {
452,436✔
699
      *result = false;
1,503✔
700
      return TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
1,503✔
701
    }
702
    typeLeft = **pLeftData;
450,933✔
703
    (*pLeftData)++;
450,933✔
704
  }
705
  if (typeRight == TSDB_DATA_TYPE_JSON) {
419,822✔
706
    if (tTagIsJson(*pRightData)) {
62,506✔
707
      *result = false;
×
708
      return TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
×
709
    }
710
    typeRight = **pRightData;
62,506✔
711
    (*pRightData)++;
62,506✔
712
  }
713

714
  if (optr == OP_TYPE_LIKE || optr == OP_TYPE_NOT_LIKE || optr == OP_TYPE_MATCH || optr == OP_TYPE_NMATCH) {
419,822✔
715
    if (typeLeft != TSDB_DATA_TYPE_NCHAR && typeLeft != TSDB_DATA_TYPE_BINARY && typeLeft != TSDB_DATA_TYPE_GEOMETRY &&
16,158✔
716
        typeLeft != TSDB_DATA_TYPE_VARBINARY) {
717
      *result = false;
23,838✔
718
      return TSDB_CODE_SUCCESS;
23,838✔
719
    }
720
  }
721

722
  // if types can not comparable
723
  if ((IS_NUMERIC_TYPE(typeLeft) && !IS_NUMERIC_TYPE(typeRight)) ||
395,984✔
724
      (IS_NUMERIC_TYPE(typeRight) && !IS_NUMERIC_TYPE(typeLeft)) ||
364,130✔
725
      (IS_VAR_DATA_TYPE(typeLeft) && !IS_VAR_DATA_TYPE(typeRight)) ||
209,531✔
726
      (IS_VAR_DATA_TYPE(typeRight) && !IS_VAR_DATA_TYPE(typeLeft)) ||
201,515✔
727
      ((typeLeft == TSDB_DATA_TYPE_BOOL) && (typeRight != TSDB_DATA_TYPE_BOOL)) ||
198,509✔
728
      ((typeRight == TSDB_DATA_TYPE_BOOL) && (typeLeft != TSDB_DATA_TYPE_BOOL))) {
4,008✔
729
    *result = false;
197,475✔
730
    return TSDB_CODE_SUCCESS;
197,475✔
731
  }
732

733
  if (typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL) {
198,509✔
734
    *isNull = true;
49✔
735
    *result = true;
×
736
    return TSDB_CODE_SUCCESS;
×
737
  }
738
  int8_t type = (int8_t)vectorGetConvertType(typeLeft, typeRight);
239,220✔
739
  if (type < 0) {
239,220✔
740
    sclError("not supported convertion between %d and %d", typeLeft, typeRight);
×
741
    return TSDB_CODE_SCALAR_CONVERT_ERROR;
×
742
  }
743

744
  if (type == 0) {
239,220✔
745
    *result = true;
69,035✔
746
    SCL_RET(filterGetCompFunc(fp, typeLeft, optr));
69,035✔
747
  }
748

749
  SCL_ERR_RET(filterGetCompFunc(fp, type, optr));
170,185✔
750

751
  if (IS_NUMERIC_TYPE(type)) {
170,185✔
752
    if (typeLeft == TSDB_DATA_TYPE_NCHAR || typeLeft == TSDB_DATA_TYPE_VARCHAR || typeLeft == TSDB_DATA_TYPE_GEOMETRY) {
115,812✔
753
      *result = false;
×
754
      return TSDB_CODE_SUCCESS;
×
755
    } else if (typeLeft != type) {
115,812✔
756
      SCL_ERR_RET(convertNumberToNumber(*pLeftData, pLeftOut, typeLeft, type));
4,800✔
757
      *pLeftData = pLeftOut;
4,800✔
758
    }
759

760
    if (typeRight == TSDB_DATA_TYPE_NCHAR || typeRight == TSDB_DATA_TYPE_VARCHAR ||
115,812✔
761
        typeRight == TSDB_DATA_TYPE_GEOMETRY) {
762
      *result = false;
×
763
      return TSDB_CODE_SUCCESS;
×
764
    } else if (typeRight != type) {
115,812✔
765
      SCL_ERR_RET(convertNumberToNumber(*pRightData, pRightOut, typeRight, type));
111,012✔
766
      *pRightData = pRightOut;
111,012✔
767
    }
768
  } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_GEOMETRY) {
54,373✔
769
    if (typeLeft == TSDB_DATA_TYPE_NCHAR) {
54,373✔
770
      char *tmpLeft = NULL;
54,373✔
771
      SCL_ERR_RET(ncharTobinary(*pLeftData, (void *)&tmpLeft, charsetCxt));
54,373✔
772
      *pLeftData = tmpLeft;
54,373✔
773
      *freeLeft = true;
54,373✔
774
    }
775
    if (typeRight == TSDB_DATA_TYPE_NCHAR) {
54,373✔
776
      char *tmpRight = NULL;
×
777
      SCL_ERR_RET(ncharTobinary(*pRightData, (void *)&tmpRight, charsetCxt));
×
778
      *pRightData = tmpRight;
×
779
      *freeRight = true;
×
780
    }
781
  } else {
782
    *result = false;
×
783
    return TSDB_CODE_SUCCESS;
×
784
  }
785

786
  *result = true;
170,185✔
787
  return TSDB_CODE_SUCCESS;
170,185✔
788
}
789

790
int32_t vectorConvertToVarData(SSclVectorConvCtx *pCtx) {
424,991✔
791
  SColumnInfoData *pInputCol = pCtx->pIn->columnData;
424,991✔
792
  SColumnInfoData *pOutputCol = pCtx->pOut->columnData;
424,991✔
793
  char             tmp[128] = {0};
424,991✔
794

795
  if (IS_SIGNED_NUMERIC_TYPE(pCtx->inType) || pCtx->inType == TSDB_DATA_TYPE_BOOL ||
424,991✔
796
      pCtx->inType == TSDB_DATA_TYPE_TIMESTAMP) {
240,196✔
797
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
391,854✔
798
      if (colDataIsNull_f(pInputCol, i)) {
206,047✔
799
        colDataSetNULL(pOutputCol, i);
1,012✔
800
        continue;
1,012✔
801
      }
802

803
      int64_t value = 0;
205,035✔
804
      GET_TYPED_DATA(value, int64_t, pCtx->inType, colDataGetData(pInputCol, i),
205,035✔
805
                     typeGetTypeModFromColInfo(&pInputCol->info));
806
      int32_t len = snprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%" PRId64, value);
205,035✔
807
      varDataLen(tmp) = len;
205,035✔
808
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
205,035✔
809
        SCL_ERR_RET(varToNchar(tmp, pCtx->pOut, i, NULL));
18,968✔
810
      } else {
811
        SCL_ERR_RET(colDataSetVal(pOutputCol, i, (char *)tmp, false));
195,551✔
812
      }
813
    }
814
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inType)) {
239,184✔
815
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
16,944✔
816
      if (colDataIsNull_f(pInputCol, i)) {
8,472✔
817
        colDataSetNULL(pOutputCol, i);
×
818
        continue;
×
819
      }
820

821
      uint64_t value = 0;
8,472✔
822
      GET_TYPED_DATA(value, uint64_t, pCtx->inType, colDataGetData(pInputCol, i),
8,472✔
823
                     typeGetTypeModFromColInfo(&pInputCol->info));
824
      int32_t len = snprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%" PRIu64, value);
8,472✔
825
      varDataLen(tmp) = len;
8,472✔
826
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
8,472✔
827
        SCL_ERR_RET(varToNchar(tmp, pCtx->pOut, i, NULL));
×
828
      } else {
829
        SCL_ERR_RET(colDataSetVal(pOutputCol, i, (char *)tmp, false));
8,472✔
830
      }
831
    }
832
  } else if (IS_FLOAT_TYPE(pCtx->inType)) {
230,712✔
833
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
481,664✔
834
      if (colDataIsNull_f(pInputCol, i)) {
250,952✔
835
        colDataSetNULL(pOutputCol, i);
1,012✔
836
        continue;
1,012✔
837
      }
838

839
      double value = 0;
249,940✔
840
      GET_TYPED_DATA(value, double, pCtx->inType, colDataGetData(pInputCol, i),
249,940✔
841
                     typeGetTypeModFromColInfo(&pInputCol->info));
842
      int32_t len = snprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%lf", value);
249,940✔
843
      varDataLen(tmp) = len;
249,940✔
844
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
249,940✔
845
        SCL_ERR_RET(varToNchar(tmp, pCtx->pOut, i, NULL));
8,472✔
846
      } else {
847
        SCL_ERR_RET(colDataSetVal(pOutputCol, i, (char *)tmp, false));
245,704✔
848
      }
849
    }
850
  } else {
851
    sclError("not supported input type:%d", pCtx->inType);
×
852
    return TSDB_CODE_APP_ERROR;
×
853
  }
854

855
  return TSDB_CODE_SUCCESS;
424,991✔
856
}
857

858
void vectorConvertCheckOverflow(SColumnInfoData *pInputCol, int16_t inType, int16_t outType, int8_t *overflow) {
4,234,795✔
859
  if (IS_SIGNED_NUMERIC_TYPE(outType)) {
4,234,795✔
860
    int64_t minValue = tDataTypes[outType].minValue;
2,883,431✔
861
    int64_t maxValue = tDataTypes[outType].maxValue;
2,883,431✔
862

863
    double value = 0;
2,883,431✔
864
    GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, 0), typeGetTypeModFromColInfo(&pInputCol->info));
2,883,431✔
865

866
    if (value > maxValue) {
2,883,431✔
867
      *overflow = 1;
×
868
    } else if (value < minValue) {
2,883,431✔
869
      *overflow = -1;
×
870
    } else {
871
      *overflow = 0;
2,883,431✔
872
    }
873

874
    return;
2,883,431✔
875
  }
876

877
  if (IS_UNSIGNED_NUMERIC_TYPE(outType)) {
1,351,364✔
878
    uint64_t minValue = (uint64_t)tDataTypes[outType].minValue;
563,919✔
879
    uint64_t maxValue = (uint64_t)tDataTypes[outType].maxValue;
563,919✔
880

881
    double value = 0;
563,919✔
882
    GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, 0), typeGetTypeModFromColInfo(&pInputCol->info));
563,919✔
883

884
    if (value > maxValue) {
563,919✔
885
      *overflow = 1;
×
886
    } else if (value < minValue) {
563,919✔
887
      *overflow = -1;
15,990✔
888
    } else {
889
      *overflow = 0;
547,929✔
890
    }
891

892
    return;
563,919✔
893
  }
894
}
895

896
// TODO opt performance
897
int32_t vectorConvertSingleColImpl(const SScalarParam *pIn, SScalarParam *pOut, int8_t *overflow, int32_t startIndex,
274,781,008✔
898
                                   int32_t numOfRows) {
899
  SColumnInfoData *pInputCol = pIn->columnData;
274,781,008✔
900
  SColumnInfoData *pOutputCol = pOut->columnData;
274,796,554✔
901

902
  if (NULL == pInputCol) {
274,735,598✔
903
    sclError("input column is NULL, hasHashParam: %d", pIn->hashParam.hasHashParam);
×
904
    return TSDB_CODE_APP_ERROR;
×
905
  }
906

907
  int32_t           rstart = (startIndex >= 0 && startIndex < pIn->numOfRows) ? startIndex : 0;
274,735,598✔
908
  int32_t           rend = numOfRows > 0 ? rstart + numOfRows - 1 : rstart + pIn->numOfRows - 1;
274,765,866✔
909
  SSclVectorConvCtx cCtx = {pIn, pOut, rstart, rend, pInputCol->info.type, pOutputCol->info.type};
274,741,577✔
910

911
  if (IS_VAR_DATA_TYPE(cCtx.inType)) {
274,750,319✔
912
    return vectorConvertFromVarData(&cCtx, overflow);
210,221,286✔
913
  }
914

915
  pOut->numOfRows = pIn->numOfRows;
64,529,033✔
916
  switch (cCtx.outType) {
64,524,586✔
917
    case TSDB_DATA_TYPE_BOOL: {
708,198✔
918
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
2,282,868✔
919
        if (colDataIsNull_f(pInputCol, i)) {
1,574,670✔
920
          colDataSetNULL(pOutputCol, i);
62,392✔
921
          continue;
62,392✔
922
        }
923

924
        if (overflow) {
1,512,278✔
925
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
×
926
        }
927

928
        bool value = 0;
1,512,278✔
929
        GET_TYPED_DATA(value, bool, cCtx.inType, colDataGetData(pInputCol, i),
1,512,278✔
930
                       typeGetTypeModFromColInfo(&pInputCol->info));
931
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
1,512,278✔
932
      }
933
      break;
708,198✔
934
    }
935
    case TSDB_DATA_TYPE_TINYINT: {
3,122,776✔
936
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
1,949,947,961✔
937
        if (colDataIsNull_f(pInputCol, i)) {
1,947,185,554✔
938
          colDataSetNULL(pOutputCol, i);
936,802✔
939
          continue;
1,557,400✔
940
        }
941

942
        if (overflow) {
1,945,343,984✔
943
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
1,410,528✔
944
        }
945

946
        int8_t value = 0;
1,945,343,984✔
947
        GET_TYPED_DATA(value, int8_t, cCtx.inType, colDataGetData(pInputCol, i),
1,945,205,977✔
948
                       typeGetTypeModFromColInfo(&pInputCol->info));
949
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
1,945,437,150✔
950
      }
951
      break;
2,762,407✔
952
    }
953
    case TSDB_DATA_TYPE_SMALLINT: {
870,407✔
954
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
2,197,514✔
955
        if (colDataIsNull_f(pInputCol, i)) {
1,329,143✔
956
          colDataSetNULL(pOutputCol, i);
269,654✔
957
          continue;
265,584✔
958
        }
959

960
        if (overflow) {
1,060,845✔
961
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
194,288✔
962
        }
963

964
        int16_t value = 0;
1,060,845✔
965
        GET_TYPED_DATA(value, int16_t, cCtx.inType, colDataGetData(pInputCol, i),
1,063,559✔
966
                       typeGetTypeModFromColInfo(&pInputCol->info));
967
        colDataSetInt16(pOutputCol, i, (int16_t *)&value);
1,062,203✔
968
      }
969
      break;
868,371✔
970
    }
971
    case TSDB_DATA_TYPE_INT: {
4,106,877✔
972
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
8,946,329✔
973
        if (colDataIsNull_f(pInputCol, i)) {
4,844,773✔
974
          colDataSetNULL(pOutputCol, i);
415,399✔
975
          continue;
414,376✔
976
        }
977

978
        if (overflow) {
4,429,944✔
979
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
292,128✔
980
        }
981

982
        int32_t value = 0;
4,429,944✔
983
        GET_TYPED_DATA(value, int32_t, cCtx.inType, colDataGetData(pInputCol, i),
4,430,692✔
984
                       typeGetTypeModFromColInfo(&pInputCol->info));
985
        colDataSetInt32(pOutputCol, i, (int32_t *)&value);
4,430,298✔
986
      }
987
      break;
4,101,556✔
988
    }
989
    case TSDB_DATA_TYPE_BIGINT:
39,818,608✔
990
    case TSDB_DATA_TYPE_TIMESTAMP: {
991
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
730,257,091✔
992
        if (colDataIsNull_f(pInputCol, i)) {
690,452,143✔
993
          colDataSetNULL(pOutputCol, i);
3,481,406✔
994
          continue;
3,460,166✔
995
        }
996

997
        if (overflow) {
686,974,481✔
998
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
1,773,932✔
999
        }
1000

1001
        int64_t value = 0;
686,974,507✔
1002
        GET_TYPED_DATA(value, int64_t, cCtx.inType, colDataGetData(pInputCol, i),
686,989,513✔
1003
                       typeGetTypeModFromColInfo(&pInputCol->info));
1004
        colDataSetInt64(pOutputCol, i, (int64_t *)&value);
686,978,850✔
1005
      }
1006
      break;
39,804,948✔
1007
    }
1008
    case TSDB_DATA_TYPE_UTINYINT: {
175,044✔
1009
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
488,916✔
1010
        if (colDataIsNull_f(pInputCol, i)) {
313,872✔
1011
          colDataSetNULL(pOutputCol, i);
84,504✔
1012
          continue;
84,504✔
1013
        }
1014

1015
        if (overflow) {
229,368✔
1016
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
24,144✔
1017
        }
1018

1019
        uint8_t value = 0;
229,368✔
1020
        GET_TYPED_DATA(value, uint8_t, cCtx.inType, colDataGetData(pInputCol, i),
229,368✔
1021
                       typeGetTypeModFromColInfo(&pInputCol->info));
1022
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
229,368✔
1023
      }
1024
      break;
175,044✔
1025
    }
1026
    case TSDB_DATA_TYPE_USMALLINT: {
217,296✔
1027
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
639,816✔
1028
        if (colDataIsNull_f(pInputCol, i)) {
422,520✔
1029
          colDataSetNULL(pOutputCol, i);
120,720✔
1030
          continue;
120,720✔
1031
        }
1032

1033
        if (overflow) {
301,800✔
1034
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
48,288✔
1035
        }
1036

1037
        uint16_t value = 0;
301,800✔
1038
        GET_TYPED_DATA(value, uint16_t, cCtx.inType, colDataGetData(pInputCol, i),
301,800✔
1039
                       typeGetTypeModFromColInfo(&pInputCol->info));
1040
        colDataSetInt16(pOutputCol, i, (int16_t *)&value);
301,800✔
1041
      }
1042
      break;
217,296✔
1043
    }
1044
    case TSDB_DATA_TYPE_UINT: {
260,684✔
1045
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
792,988✔
1046
        if (colDataIsNull_f(pInputCol, i)) {
532,304✔
1047
          colDataSetNULL(pOutputCol, i);
156,936✔
1048
          continue;
156,936✔
1049
        }
1050

1051
        if (overflow) {
375,368✔
1052
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
73,568✔
1053
        }
1054

1055
        uint32_t value = 0;
375,368✔
1056
        GET_TYPED_DATA(value, uint32_t, cCtx.inType, colDataGetData(pInputCol, i),
375,368✔
1057
                       typeGetTypeModFromColInfo(&pInputCol->info));
1058
        colDataSetInt32(pOutputCol, i, (int32_t *)&value);
375,368✔
1059
      }
1060
      break;
260,684✔
1061
    }
1062
    case TSDB_DATA_TYPE_UBIGINT: {
840,439✔
1063
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
2,550,062✔
1064
        if (colDataIsNull_f(pInputCol, i)) {
1,709,623✔
1065
          colDataSetNULL(pOutputCol, i);
507,024✔
1066
          continue;
507,024✔
1067
        }
1068

1069
        if (overflow) {
1,202,599✔
1070
          vectorConvertCheckOverflow(pInputCol, cCtx.inType, cCtx.outType, overflow + i);
417,919✔
1071
        }
1072

1073
        uint64_t value = 0;
1,202,599✔
1074
        GET_TYPED_DATA(value, uint64_t, cCtx.inType, colDataGetData(pInputCol, i),
1,202,599✔
1075
                       typeGetTypeModFromColInfo(&pInputCol->info));
1076
        colDataSetInt64(pOutputCol, i, (int64_t *)&value);
1,202,599✔
1077
      }
1078
      break;
840,439✔
1079
    }
1080
    case TSDB_DATA_TYPE_FLOAT: {
1,624,838✔
1081
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
4,474,687✔
1082
        if (colDataIsNull_f(pInputCol, i)) {
2,850,430✔
1083
          colDataSetNULL(pOutputCol, i);
606,636✔
1084
          continue;
606,636✔
1085
        }
1086

1087
        float value = 0;
2,243,794✔
1088
        GET_TYPED_DATA(value, float, cCtx.inType, colDataGetData(pInputCol, i),
2,243,794✔
1089
                       typeGetTypeModFromColInfo(&pInputCol->info));
1090
        colDataSetFloat(pOutputCol, i, (float *)&value);
2,243,116✔
1091
      }
1092
      break;
1,624,257✔
1093
    }
1094
    case TSDB_DATA_TYPE_DOUBLE: {
8,381,262✔
1095
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
1,586,601,469✔
1096
        if (colDataIsNull_f(pInputCol, i)) {
1,578,305,522✔
1097
          colDataSetNULL(pOutputCol, i);
23,037,792✔
1098
          continue;
23,025,492✔
1099
        }
1100

1101
        double value = 0;
1,555,324,752✔
1102
        GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, i),
1,555,313,616✔
1103
                       typeGetTypeModFromColInfo(&pInputCol->info));
1104
        colDataSetDouble(pOutputCol, i, (double *)&value);
1,555,188,705✔
1105
      }
1106
      break;
8,295,947✔
1107
    }
1108
    case TSDB_DATA_TYPE_BINARY:
424,991✔
1109
    case TSDB_DATA_TYPE_VARBINARY:
1110
    case TSDB_DATA_TYPE_NCHAR:
1111
    case TSDB_DATA_TYPE_GEOMETRY: {
1112
      return vectorConvertToVarData(&cCtx);
424,991✔
1113
    }
1114
    case TSDB_DATA_TYPE_DECIMAL: {
3,973,166✔
1115
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
429,470,164✔
1116
        if (colDataIsNull_f(pInputCol, i)) {
425,496,998✔
1117
          colDataSetNULL(pOutputCol, i);
4,563,232✔
1118
          continue;
4,563,232✔
1119
        }
1120

1121
        Decimal   value = {0};
420,933,766✔
1122
        SDataType inputType = GET_COL_DATA_TYPE(pInputCol->info), outputType = GET_COL_DATA_TYPE(pOutputCol->info);
420,933,766✔
1123
        int32_t   code = convertToDecimal(colDataGetData(pInputCol, i), &inputType, &value, &outputType);
420,933,766✔
1124
        if (TSDB_CODE_SUCCESS != code) return code;
420,933,766✔
1125
        code = colDataSetVal(pOutputCol, i, (const char *)&value, false);
420,933,766✔
1126
        if (TSDB_CODE_SUCCESS != code) return code;
420,933,766✔
1127
      }
1128
      break;
3,973,166✔
1129
    }
UNCOV
1130
    default:
×
UNCOV
1131
      sclError("invalid convert output type:%d", cCtx.outType);
×
1132
      return TSDB_CODE_APP_ERROR;
×
1133
  }
1134

1135
  return TSDB_CODE_SUCCESS;
63,632,313✔
1136
}
1137

1138
int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = {
1139
    /*      NUL BOO TIN  SMA INT BIG FLO DOU VAR TIM NCH UTI USM UIN UBI JSO VAR DEC BLO MED GEO DEC64*/
1140
    /*NULL*/ 0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
1141
    /*BOOL*/ 0,  0, 2, 3, 4, 5, 6, 7, 5, 9, 5,  11, 12, 13, 14, 0,  5,  17, -1, -1, -1, 17,
1142
    /*TINY*/ 0,  0, 0, 3, 4, 5, 6, 7, 5, 9, 5,  3,  4,  5,  7,  0,  5,  17, -1, -1, -1, 17,
1143
    /*SMAL*/ 0,  0, 0, 0, 4, 5, 6, 7, 5, 9, 5,  3,  4,  5,  7,  0,  5,  17, -1, -1, -1, 17,
1144
    /*INT */ 0,  0, 0, 0, 0, 5, 6, 7, 5, 9, 5,  4,  4,  5,  7,  0,  5,  17, -1, -1, -1, 17,
1145
    /*BIGI*/ 0,  0, 0, 0, 0, 0, 6, 7, 5, 9, 5,  5,  5,  5,  7,  0,  5,  17, -1, -1, -1, 17,
1146
    /*FLOA*/ 0,  0, 0, 0, 0, 0, 0, 7, 6, 6, 6,  6,  6,  6,  6,  0,  6,  7,  -1, -1, -1, 7,
1147
    /*DOUB*/ 0,  0, 0, 0, 0, 0, 0, 0, 7, 7, 7,  7,  7,  7,  7,  0,  7,  7,  -1, -1, -1, 7,
1148
    /*VARC*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 9, 8,  7,  7,  7,  7,  0,  16, 7,  -1, -1, 20, 7,
1149
    /*TIME*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 9,  9,  9,  9,  7,  0,  9,  17, -1, -1, -1, 17,
1150
    /*NCHA*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  7,  7,  7,  7,  0,  16, 7,  -1, -1, -1, 7,
1151
    /*UTIN*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  12, 13, 14, 0,  14, 17, -1, -1, -1, 17,
1152
    /*USMA*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  13, 14, 0,  14, 17, -1, -1, -1, 17,
1153
    /*UINT*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  14, 0,  14, 17, -1, -1, -1, 17,
1154
    /*UBIG*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  14, 17, -1, -1, -1, 17,
1155
    /*JSON*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  16, -1, -1, -1, -1, -1,
1156
    /*VARB*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  -1, -1, -1, -1, -1,
1157
    /*DECI*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  -1, -1, -1, 17,
1158
    /*BLOB*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  -1, -1, -1,
1159
    /*MEDB*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  -1, -1,
1160
    /*GEOM*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  -1,
1161
    /*DEC64*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1162
};
1163

1164
int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = {
1165
    /*NULL BOOL TINY SMAL INT  BIGI FLOA DOUB VARC TIM NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB MEDB GEOM DEC64*/
1166
    /*NULL*/ 0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
1167
    /*BOOL*/ 0,  1, 2, 3, 4, 5, 6, 7, 8, 5, 10, 11, 12, 13, 14, -1, -1, 17, -1, -1, -1, 17,
1168
    /*TINY*/ 0,  0, 2, 3, 4, 5, 8, 8, 8, 5, 10, 3,  4,  5,  8,  -1, -1, 17, -1, -1, -1, 17,
1169
    /*SMAL*/ 0,  0, 0, 3, 4, 5, 8, 8, 8, 5, 10, 3,  4,  5,  8,  -1, -1, 17, -1, -1, -1, 17,
1170
    /*INT */ 0,  0, 0, 0, 4, 5, 8, 8, 8, 5, 10, 4,  4,  5,  8,  -1, -1, 17, -1, -1, -1, 17,
1171
    /*BIGI*/ 0,  0, 0, 0, 0, 5, 8, 8, 8, 5, 10, 5,  5,  5,  8,  -1, -1, 17, -1, -1, -1, 17,
1172
    /*FLOA*/ 0,  0, 0, 0, 0, 0, 6, 7, 8, 8, 10, 8,  8,  8,  8,  -1, -1, 7,  -1, -1, -1, 7,
1173
    /*DOUB*/ 0,  0, 0, 0, 0, 0, 0, 7, 8, 8, 10, 8,  8,  8,  8,  -1, -1, 7,  -1, -1, -1, 7,
1174
    /*VARC*/ 0,  0, 0, 0, 0, 0, 0, 0, 8, 8, 10, 8,  8,  8,  8,  -1, 16, 7,  -1, -1, -1, 7,
1175
    /*TIME*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 9, 10, 5,  5,  5,  8,  -1, -1, 17, -1, -1, -1, 17,
1176
    /*NCHA*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, -1, -1, 7,  -1, -1, -1, 7,
1177
    /*UTINY*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  11, 12, 13, 14, -1, -1, 17, -1, -1, -1, 17,
1178
    /*USMA*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  12, 13, 14, -1, -1, 17, -1, -1, -1, -1,
1179
    /*UINT*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  13, 14, -1, -1, 17, -1, -1, -1, -1,
1180
    /*UBIG*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  14, -1, -1, 17, -1, -1, -1, -1,
1181
    /*JSON*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  15, -1, -1, -1, -1, -1, -1,
1182
    /*VARB*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  16, -1, -1, -1, -1, -1,
1183
    /*DECI*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  -1, -1, -1, 17,
1184
    /*BLOB*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  -1, -1, -1, -1,
1185
    /*MEDB*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  -1, -1, -1,
1186
    /*GEOM*/ 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  20, -1,
1187
    /*DEC64*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  20, 0,
1188
};
1189

1190
int32_t vectorGetConvertType(int32_t type1, int32_t type2) {
177,809,815✔
1191
  if (type1 == type2) {
177,809,815✔
1192
    return 0;
72,959,412✔
1193
  }
1194

1195
  if (type1 < type2) {
104,850,403✔
1196
    return gConvertTypes[type1][type2];
34,699,301✔
1197
  }
1198

1199
  return gConvertTypes[type2][type1];
70,151,102✔
1200
}
1201

1202
STypeMod getConvertTypeMod(int32_t type, const SColumnInfo *pCol1, SScalarParam *param2) {
34,207,414✔
1203
  SColumnInfo *pCol2 = param2->columnData ? &param2->columnData->info : NULL;
34,207,414✔
1204

1205
  if (!IS_DECIMAL_TYPE(type)) {
34,204,649✔
1206
    return 0;
32,054,580✔
1207
  }
1208

1209
  if (IS_DECIMAL_TYPE(pCol1->type)) {
2,150,069✔
1210
    if (pCol2) {
1,011,433✔
1211
      if (!IS_DECIMAL_TYPE(pCol2->type)) {
725,933✔
1212
        return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), pCol1->scale);
642,773✔
1213
      } else {
1214
        return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), TMAX(pCol1->scale, pCol2->scale));
83,160✔
1215
      }
1216
    } else if (!param2->hashParam.hasHashParam || !IS_DECIMAL_TYPE(param2->hashParam.filterValueType)) {
285,500✔
1217
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), pCol1->scale);
×
1218
    } else {
1219
      uint8_t scale2 = 0;
285,500✔
1220
      decimalFromTypeMod(param2->hashParam.filterValueTypeMod, NULL, &scale2);
285,500✔
1221
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), TMAX(pCol1->scale, scale2));
285,500✔
1222
    }
1223
  } else {
1224
    if (pCol2 && IS_DECIMAL_TYPE(pCol2->type)) {
1,141,056✔
1225
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), pCol2->scale);
754,752✔
1226
    } else if (!pCol2 && param2->hashParam.hasHashParam && IS_DECIMAL_TYPE(param2->hashParam.filterValueType)) {
386,304✔
1227
      uint8_t scale2 = 0;
386,304✔
1228
      decimalFromTypeMod(param2->hashParam.filterValueTypeMod, NULL, &scale2);
386,304✔
1229
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), scale2);
386,304✔
1230
    }
1231
  }
1232

1233
  return 0;
×
1234
}
1235

1236
int32_t vectorConvertSingleCol(SScalarParam *input, SScalarParam *output, int32_t type, STypeMod typeMod,
232,612,829✔
1237
                               int32_t startIndex, int32_t numOfRows) {
1238
  if (input->columnData == NULL && input->hashParam.hasHashParam) {
232,612,829✔
1239
    return TSDB_CODE_SUCCESS;
×
1240
  }
1241
  output->numOfRows = input->numOfRows;
232,649,098✔
1242

1243
  SDataType t = {.type = type};
232,650,574✔
1244
  t.bytes = (IS_VAR_DATA_TYPE(t.type) && input->columnData) ? input->columnData->info.bytes : tDataTypes[type].bytes;
232,643,751✔
1245
  t.precision =
232,637,782✔
1246
      (IS_TIMESTAMP_TYPE(t.type) && input->columnData) ? input->columnData->info.precision : TSDB_TIME_PRECISION_MILLI;
232,634,854✔
1247
  if (IS_DECIMAL_TYPE(type)) {
232,638,565✔
1248
    extractTypeFromTypeMod(type, typeMod, &t.precision, &t.scale, NULL);
3,184,332✔
1249
    // We do not change scale here for decimal types.
1250
    if (IS_DECIMAL_TYPE(input->columnData->info.type)) t.scale = input->columnData->info.scale;
3,217,590✔
1251
  }
1252

1253
  int32_t code = sclCreateColumnInfoData(&t, input->numOfRows, output);
232,671,823✔
1254
  if (code != TSDB_CODE_SUCCESS) {
232,688,404✔
1255
    return code;
×
1256
  }
1257

1258
  code = vectorConvertSingleColImpl(input, output, NULL, startIndex, numOfRows);
232,688,404✔
1259
  if (code) {
232,646,010✔
1260
    return code;
72,432✔
1261
  }
1262

1263
  return TSDB_CODE_SUCCESS;
232,573,578✔
1264
}
1265

1266
int32_t vectorConvertCols(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pLeftOut, SScalarParam *pRightOut,
51,566,944✔
1267
                          int32_t startIndex, int32_t numOfRows) {
1268
  int32_t leftType = GET_PARAM_TYPE(pLeft);
51,566,944✔
1269
  int32_t rightType = GET_PARAM_TYPE(pRight);
51,570,313✔
1270
  if (leftType == rightType) {
51,569,074✔
1271
    return TSDB_CODE_SUCCESS;
16,562,752✔
1272
  }
1273

1274
  int8_t   type = 0;
35,006,322✔
1275
  int32_t  code = 0;
35,006,322✔
1276
  STypeMod outTypeMod = 0;
35,006,322✔
1277

1278
  SScalarParam *param1 = pLeft, *paramOut1 = pLeftOut;
35,006,322✔
1279
  SScalarParam *param2 = pRight, *paramOut2 = pRightOut;
35,006,322✔
1280

1281
  type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
35,006,322✔
1282
  if (0 == type) {
35,000,975✔
1283
    return TSDB_CODE_SUCCESS;
93,888✔
1284
  }
1285
  if (-1 == type) {
34,907,087✔
1286
    sclError("invalid convert type1:%d, type2:%d", GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
704,926✔
1287
    terrno = TSDB_CODE_SCALAR_CONVERT_ERROR;
704,926✔
1288
    return TSDB_CODE_SCALAR_CONVERT_ERROR;
704,926✔
1289
  }
1290
  outTypeMod = getConvertTypeMod(type, &param1->columnData->info, param2);
34,202,161✔
1291

1292
  if (type != GET_PARAM_TYPE(param1)) {
34,187,501✔
1293
    SCL_ERR_RET(vectorConvertSingleCol(param1, paramOut1, type, outTypeMod, startIndex, numOfRows));
27,008,456✔
1294
  }
1295

1296
  if (type != GET_PARAM_TYPE(param2)) {
34,155,082✔
1297
    SCL_ERR_RET(vectorConvertSingleCol(param2, paramOut2, type, outTypeMod, startIndex, numOfRows));
12,660,596✔
1298
  }
1299

1300
  return TSDB_CODE_SUCCESS;
34,135,092✔
1301
}
1302

1303
enum {
1304
  VECTOR_DO_CONVERT = 0x1,
1305
  VECTOR_UN_CONVERT = 0x2,
1306
};
1307

1308
// TODO not correct for descending order scan
1309
static int32_t vectorMathAddHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
2,261,639✔
1310
                                   int32_t numOfRows, int32_t step, int32_t i) {
1311
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
2,261,439✔
1312
  _getDoubleValue_fn_t getVectorDoubleValueFnRight;
2,261,439✔
1313
  SCL_ERR_RET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
4,523,278✔
1314
  SCL_ERR_RET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
4,523,278✔
1315

1316
  double *output = (double *)pOutputCol->pData;
2,261,639✔
1317

1318
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
2,261,639✔
1319
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1320
  } else {
1321
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
1,685,246,318✔
1322
      if (IS_HELPER_NULL(pLeftCol, i)) {
2,147,483,647✔
1323
        colDataSetNULL(pOutputCol, i);
430,189,858✔
1324
        continue;  // TODO set null or ignore
430,186,311✔
1325
      }
1326
      double leftRes = 0;
1,252,780,330✔
1327
      double rightRes = 0;
1,252,761,543✔
1328
      SCL_ERR_RET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
1,252,772,144✔
1329
      SCL_ERR_RET(getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes));
1,252,810,746✔
1330
      *output = leftRes + rightRes;
1,251,426,922✔
1331
    }
1332
  }
1333
  SCL_RET(TSDB_CODE_SUCCESS);
2,272,666✔
1334
}
1335

1336
static int32_t vectorMathTsAddHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
31,493,612✔
1337
                                     int32_t numOfRows, int32_t step, int32_t i, timezone_t tz) {
1338
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
31,491,681✔
1339
  _getBigintValue_fn_t getVectorBigintValueFnRight;
31,495,458✔
1340
  SCL_ERR_RET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
31,498,780✔
1341
  SCL_ERR_RET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
31,498,921✔
1342
  int64_t *output = (int64_t *)pOutputCol->pData;
31,499,800✔
1343

1344
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
31,495,032✔
1345
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1,800,998✔
1346
  } else {
1347
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
2,147,483,647✔
1348
      if (IS_HELPER_NULL(pLeftCol, i)) {
2,147,483,647✔
1349
        colDataSetNULL(pOutputCol, i);
7,152✔
1350
        continue;  // TODO set null or ignore
560✔
1351
      }
1352
      int64_t leftRes = 0;
2,147,483,647✔
1353
      int64_t rightRes = 0;
2,147,483,647✔
1354
      SCL_ERR_RET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
2,147,483,647✔
1355
      SCL_ERR_RET(getVectorBigintValueFnRight(pRightCol->pData, 0, &rightRes));
2,147,483,647✔
1356
      *output = taosTimeAdd(leftRes, rightRes, pRightCol->info.scale, pRightCol->info.precision, tz);
2,147,483,647✔
1357
    }
1358
  }
1359
  SCL_RET(TSDB_CODE_SUCCESS);
31,504,547✔
1360
}
1361

1362
static int32_t vectorConvertVarToDouble(SScalarParam *pInput, int32_t *converted, SColumnInfoData **pOutputCol) {
454,596,405✔
1363
  SScalarParam     output = {0};
454,596,405✔
1364
  SColumnInfoData *pCol = pInput->columnData;
454,636,098✔
1365
  int32_t          code = TSDB_CODE_SUCCESS;
454,657,343✔
1366
  *pOutputCol = NULL;
454,657,343✔
1367
  bool isVarChar = IS_VAR_DATA_TYPE(pCol->info.type) && pCol->info.type != TSDB_DATA_TYPE_JSON &&
909,106,552✔
1368
                   pCol->info.type != TSDB_DATA_TYPE_VARBINARY && !IS_STR_DATA_BLOB(pCol->info.type);
909,377,761✔
1369
  if (isVarChar || IS_DECIMAL_TYPE(pCol->info.type)) {
454,667,545✔
1370
    SCL_ERR_RET(vectorConvertSingleCol(pInput, &output, TSDB_DATA_TYPE_DOUBLE, 0, -1, -1));
192,223,635✔
1371
    *converted = VECTOR_DO_CONVERT;
192,226,626✔
1372
    *pOutputCol = output.columnData;
192,232,433✔
1373
    SCL_RET(code);
192,227,756✔
1374
  }
1375

1376
  *converted = VECTOR_UN_CONVERT;
262,448,741✔
1377
  *pOutputCol = pInput->columnData;
262,451,324✔
1378
  SCL_RET(TSDB_CODE_SUCCESS);
262,449,104✔
1379
}
1380

1381
static void doReleaseVec(SColumnInfoData *pCol, int32_t type) {
540,911,150✔
1382
  if (type == VECTOR_DO_CONVERT) {
540,911,150✔
1383
    colDataDestroy(pCol);
192,244,238✔
1384
    taosMemoryFree(pCol);
192,233,928✔
1385
  }
1386
}
540,895,045✔
1387

1388
int32_t vectorMathAdd(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
221,626,394✔
1389
  SColumnInfoData *pOutputCol = pOut->columnData;
221,626,394✔
1390

1391
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
221,636,024✔
1392
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
221,657,090✔
1393

1394
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
221,657,090✔
1395

1396
  int32_t          code = TSDB_CODE_SUCCESS;
221,639,502✔
1397
  int32_t          leftConvert = 0, rightConvert = 0;
221,639,502✔
1398
  SColumnInfoData *pLeftCol = pLeft->columnData;
221,629,947✔
1399
  SColumnInfoData *pRightCol = pRight->columnData;
221,667,743✔
1400
  if (pOutputCol->info.type == TSDB_DATA_TYPE_TIMESTAMP) {  // timestamp plus duration
221,656,826✔
1401
    int64_t             *output = (int64_t *)pOutputCol->pData;
42,986,709✔
1402
    _getBigintValue_fn_t getVectorBigintValueFnLeft;
42,988,613✔
1403
    _getBigintValue_fn_t getVectorBigintValueFnRight;
42,991,943✔
1404
    SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
42,992,389✔
1405
    SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
42,990,980✔
1406

1407
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
42,991,489✔
1408
      if (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP) {
21,476,439✔
1409
        SCL_ERR_JRET(vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pRight->numOfRows, step, i, pLeft->tz));
4,229,726✔
1410
      } else {
1411
        SCL_ERR_JRET(vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i, pLeft->tz));
17,244,262✔
1412
      }
1413
    } else if (pLeft->numOfRows == 1) {
21,515,561✔
1414
      SCL_ERR_JRET(vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i, pLeft->tz));
×
1415
    } else if (pRight->numOfRows == 1) {
21,514,113✔
1416
      SCL_ERR_JRET(vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i, pLeft->tz));
10,021,981✔
1417
    } else if (pLeft->numOfRows == pRight->numOfRows) {
11,493,098✔
1418
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
98,196,394✔
1419
        if (IS_NULL) {
248,450,410✔
1420
          colDataSetNULL(pOutputCol, i);
11,677,996✔
1421
          continue;  // TODO set null or ignore
11,663,680✔
1422
        }
1423
        int64_t leftRes = 0;
75,037,230✔
1424
        int64_t rightRes = 0;
75,036,747✔
1425
        SCL_ERR_JRET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
75,036,747✔
1426
        SCL_ERR_JRET(getVectorBigintValueFnRight(pRightCol->pData, i, &rightRes));
75,036,264✔
1427
        *output = leftRes + rightRes;
75,039,133✔
1428
      }
1429
    }
1430
  } else if (IS_DECIMAL_TYPE(pOutputCol->info.type)) {
178,617,530✔
1431
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_ADD));
75,334✔
1432
  } else {
1433
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
178,606,165✔
1434
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
178,595,217✔
1435
    double              *output = (double *)pOutputCol->pData;
178,628,091✔
1436
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
178,609,670✔
1437
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
178,632,647✔
1438
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
357,250,629✔
1439
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
357,216,751✔
1440
    if (pLeft->numOfRows == pRight->numOfRows) {
178,608,864✔
1441
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
2,147,483,647✔
1442
        if (IS_NULL) {
2,147,483,647✔
1443
          colDataSetNULL(pOutputCol, i);
2,147,483,647✔
1444
          continue;  // TODO set null or ignore
2,147,483,647✔
1445
        }
1446
        double leftRes = 0;
2,147,483,647✔
1447
        double rightRes = 0;
2,147,483,647✔
1448
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
2,147,483,647✔
1449
        SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes));
2,147,483,647✔
1450
        *output = leftRes + rightRes;
2,147,483,647✔
1451
      }
1452
    } else if (pLeft->numOfRows == 1) {
2,261,096✔
1453
      SCL_ERR_JRET(vectorMathAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i));
19,882✔
1454
    } else if (pRight->numOfRows == 1) {
2,241,214✔
1455
      SCL_ERR_JRET(vectorMathAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i));
2,241,757✔
1456
    }
1457
  }
1458

1459
_return:
221,672,741✔
1460
  doReleaseVec(pLeftCol, leftConvert);
221,665,067✔
1461
  doReleaseVec(pRightCol, rightConvert);
221,640,811✔
1462
  SCL_RET(code);
221,650,050✔
1463
}
1464

1465
// TODO not correct for descending order scan
1466
static int32_t vectorMathSubHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
6,028,008✔
1467
                                   int32_t numOfRows, int32_t step, int32_t factor, int32_t i) {
1468
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
6,028,008✔
1469
  _getDoubleValue_fn_t getVectorDoubleValueFnRight;
6,027,465✔
1470
  SCL_ERR_RET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
12,054,930✔
1471
  SCL_ERR_RET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
12,053,844✔
1472

1473
  double *output = (double *)pOutputCol->pData;
6,026,922✔
1474

1475
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
6,027,465✔
UNCOV
1476
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1477
  } else {
1478
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
890,187,753✔
1479
      if (IS_HELPER_NULL(pLeftCol, i)) {
1,768,320,576✔
1480
        colDataSetNULL(pOutputCol, i);
372,106,156✔
1481
        continue;  // TODO set null or ignore
372,108,871✔
1482
      }
1483
      double leftRes = 0;
512,055,218✔
1484
      double rightRes = 0;
512,055,218✔
1485
      SCL_ERR_RET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
512,055,218✔
1486
      SCL_ERR_RET(getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes));
512,047,616✔
1487
      *output = (leftRes - rightRes) * factor;
512,046,530✔
1488
    }
1489
  }
1490
  SCL_RET(TSDB_CODE_SUCCESS);
6,026,922✔
1491
}
1492

1493
static int32_t vectorMathTsSubHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
6,067,453✔
1494
                                     int32_t numOfRows, int32_t step, int32_t factor, int32_t i, timezone_t tz) {
1495
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
6,061,565✔
1496
  _getBigintValue_fn_t getVectorBigintValueFnRight;
6,062,307✔
1497
  SCL_ERR_RET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
6,068,195✔
1498
  SCL_ERR_RET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
6,068,195✔
1499

1500
  int64_t *output = (int64_t *)pOutputCol->pData;
6,068,195✔
1501

1502
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
6,068,195✔
1503
    colDataSetNNULL(pOutputCol, 0, numOfRows);
742✔
1504
  } else {
1505
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
818,668,400✔
1506
      if (IS_HELPER_NULL(pLeftCol, i)) {
1,625,344,358✔
1507
        colDataSetNULL(pOutputCol, i);
4,117✔
1508
        continue;  // TODO set null or ignore
407✔
1509
      }
1510
      int64_t leftRes = 0;
812,668,804✔
1511
      int64_t rightRes = 0;
812,668,804✔
1512
      SCL_ERR_RET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
812,665,094✔
1513
      SCL_ERR_RET(getVectorBigintValueFnRight(pRightCol->pData, 0, &rightRes));
812,609,444✔
1514
      *output = taosTimeAdd(leftRes, -rightRes, pRightCol->info.scale, pRightCol->info.precision, tz) * factor;
811,484,572✔
1515
    }
1516
  }
1517
  SCL_RET(TSDB_CODE_SUCCESS);
5,991,769✔
1518
}
1519

1520
int32_t vectorMathSub(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
26,557,977✔
1521
  SColumnInfoData *pOutputCol = pOut->columnData;
26,557,977✔
1522

1523
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
26,560,692✔
1524

1525
  int32_t code = TSDB_CODE_SUCCESS;
26,557,977✔
1526
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
26,557,977✔
1527
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
26,559,063✔
1528

1529
  int32_t          leftConvert = 0, rightConvert = 0;
26,559,063✔
1530
  SColumnInfoData *pLeftCol = NULL;
26,555,805✔
1531
  SColumnInfoData *pRightCol = NULL;
26,557,977✔
1532

1533
  if (pOutputCol->info.type == TSDB_DATA_TYPE_TIMESTAMP) {  // timestamp minus duration
26,557,434✔
1534
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
6,076,273✔
1535
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
6,076,273✔
1536
    int64_t             *output = (int64_t *)pOutputCol->pData;
6,076,273✔
1537
    _getBigintValue_fn_t getVectorBigintValueFnLeft;
6,070,385✔
1538
    _getBigintValue_fn_t getVectorBigintValueFnRight;
6,070,385✔
1539
    SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
6,076,273✔
1540
    SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
6,076,273✔
1541

1542
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
6,076,273✔
1543
      SCL_ERR_JRET(vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i, pLeft->tz));
5,824,088✔
1544
    } else if (pLeft->numOfRows == 1) {
252,185✔
1545
      SCL_ERR_JRET(vectorMathTsSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i, pLeft->tz));
568✔
1546
    } else if (pRight->numOfRows == 1) {
251,617✔
1547
      SCL_ERR_JRET(vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i, pLeft->tz));
243,539✔
1548
    } else if (pLeft->numOfRows == pRight->numOfRows) {
8,078✔
1549
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
142,174✔
1550
        if (IS_NULL) {
402,288✔
1551
          colDataSetNULL(pOutputCol, i);
×
1552
          continue;  // TODO set null or ignore
×
1553
        }
1554
        int64_t leftRes = 0;
134,096✔
1555
        int64_t rightRes = 0;
134,096✔
1556
        SCL_ERR_JRET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
134,096✔
1557
        SCL_ERR_JRET(getVectorBigintValueFnRight(pRightCol->pData, i, &rightRes));
134,096✔
1558
        *output = leftRes - rightRes;
134,096✔
1559
      }
1560
    }
1561
  } else if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
20,481,704✔
1562
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_SUB));
42,896✔
1563
  } else {
1564
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
20,433,921✔
1565
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
20,437,179✔
1566
    double              *output = (double *)pOutputCol->pData;
20,440,980✔
1567
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
20,431,648✔
1568
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
20,434,906✔
1569
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
40,877,616✔
1570
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
40,875,987✔
1571

1572
    if (pLeft->numOfRows == pRight->numOfRows) {
20,438,808✔
1573
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
70,883,320✔
1574
        if (IS_NULL) {
168,597,120✔
1575
          colDataSetNULL(pOutputCol, i);
9,238,150✔
1576
          continue;  // TODO set null or ignore
9,234,892✔
1577
        }
1578
        double leftRes = 0;
47,236,542✔
1579
        double rightRes = 0;
47,235,465✔
1580
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
47,237,637✔
1581
        SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes));
47,238,171✔
1582
        *output = leftRes - rightRes;
47,237,628✔
1583
      }
1584
    } else if (pLeft->numOfRows == 1) {
6,025,836✔
1585
      SCL_ERR_JRET(vectorMathSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i));
5,731,896✔
1586
    } else if (pRight->numOfRows == 1) {
295,569✔
1587
      SCL_ERR_JRET(vectorMathSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i));
295,569✔
1588
    }
1589
  }
1590

1591
_return:
26,560,149✔
1592
  doReleaseVec(pLeftCol, leftConvert);
26,559,606✔
1593
  doReleaseVec(pRightCol, rightConvert);
26,556,348✔
1594
  SCL_RET(code);
26,559,063✔
1595
}
1596

1597
// TODO not correct for descending order scan
1598
static int32_t vectorMathMultiplyHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol,
2,799,210✔
1599
                                        SColumnInfoData *pOutputCol, int32_t numOfRows, int32_t step, int32_t i) {
1600
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
2,799,210✔
1601
  _getDoubleValue_fn_t getVectorDoubleValueFnRight;
2,799,210✔
1602
  SCL_ERR_RET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
5,598,420✔
1603
  SCL_ERR_RET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
5,598,420✔
1604

1605
  double *output = (double *)pOutputCol->pData;
2,799,210✔
1606

1607
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
2,799,210✔
1608
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1609
  } else {
1610
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
2,147,483,647✔
1611
      if (IS_HELPER_NULL(pLeftCol, i)) {
2,147,483,647✔
1612
        colDataSetNULL(pOutputCol, i);
415,930✔
1613
        continue;  // TODO set null or ignore
415,930✔
1614
      }
1615
      double leftRes = 0;
2,147,483,647✔
1616
      double rightRes = 0;
2,147,483,647✔
1617
      SCL_ERR_RET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
2,147,483,647✔
1618
      SCL_ERR_RET(getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes));
2,147,483,647✔
1619
      *output = leftRes * rightRes;
2,147,483,647✔
1620
    }
1621
  }
1622
  SCL_RET(TSDB_CODE_SUCCESS);
2,799,210✔
1623
}
1624

1625
int32_t vectorMathMultiply(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
4,007,689✔
1626
  SColumnInfoData *pOutputCol = pOut->columnData;
4,007,689✔
1627
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
4,007,689✔
1628

1629
  int32_t code = TSDB_CODE_SUCCESS;
4,007,689✔
1630
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
4,007,689✔
1631
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
4,007,689✔
1632

1633
  int32_t          leftConvert = 0, rightConvert = 0;
4,007,689✔
1634
  SColumnInfoData *pLeftCol = NULL;
4,007,689✔
1635
  SColumnInfoData *pRightCol = NULL;
4,007,689✔
1636
  if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
4,007,689✔
1637
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_MULTI));
43,774✔
1638
  } else {
1639
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
3,963,915✔
1640
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
3,963,915✔
1641

1642
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
3,959,715✔
1643
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
3,959,715✔
1644
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
7,927,830✔
1645
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
7,927,830✔
1646

1647
    double *output = (double *)pOutputCol->pData;
3,963,915✔
1648
    if (pLeft->numOfRows == pRight->numOfRows) {
3,963,915✔
1649
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
62,730,937✔
1650
        if (IS_NULL) {
183,686,057✔
1651
          colDataSetNULL(pOutputCol, i);
6,560,120✔
1652
          continue;  // TODO set null or ignore
6,560,120✔
1653
        }
1654
        double leftRes = 0;
55,006,112✔
1655
        double rightRes = 0;
55,006,112✔
1656
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
55,006,112✔
1657
        SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes));
55,006,112✔
1658
        *output = leftRes * rightRes;
55,006,112✔
1659
      }
1660
    } else if (pLeft->numOfRows == 1) {
2,799,210✔
1661
      SCL_ERR_JRET(vectorMathMultiplyHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i));
2,680,675✔
1662
    } else if (pRight->numOfRows == 1) {
118,535✔
1663
      SCL_ERR_JRET(vectorMathMultiplyHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i));
118,535✔
1664
    }
1665
  }
1666

1667
_return:
4,007,689✔
1668
  doReleaseVec(pLeftCol, leftConvert);
4,007,689✔
1669
  doReleaseVec(pRightCol, rightConvert);
4,007,689✔
1670
  SCL_RET(code);
4,007,689✔
1671
}
1672

1673
int32_t vectorMathDivide(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
9,722,849✔
1674
  SColumnInfoData *pOutputCol = pOut->columnData;
9,722,849✔
1675
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
9,722,849✔
1676

1677
  int32_t code = TSDB_CODE_SUCCESS;
9,722,849✔
1678
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
9,722,849✔
1679
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
9,722,849✔
1680

1681
  int32_t          leftConvert = 0, rightConvert = 0;
9,722,849✔
1682
  SColumnInfoData *pLeftCol = NULL;
9,722,849✔
1683
  SColumnInfoData *pRightCol = NULL;
9,722,849✔
1684
  if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
9,722,849✔
1685
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_DIV));
×
1686
  } else {
1687
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
9,722,849✔
1688
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
9,722,849✔
1689

1690
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
9,627,953✔
1691
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
9,627,953✔
1692
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
19,445,698✔
1693
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
19,445,698✔
1694

1695
    double *output = (double *)pOutputCol->pData;
9,722,849✔
1696
    if (pLeft->numOfRows == pRight->numOfRows) {
9,722,849✔
1697
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
2,147,483,647✔
1698
        if (IS_NULL) {  // divide by 0 check
2,147,483,647✔
1699
          colDataSetNULL(pOutputCol, i);
8,103,689✔
1700
          continue;
8,102,045✔
1701
        }
1702
        double rightRes = 0;
2,147,483,647✔
1703
        SCL_ERR_JRET((getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes)));
2,147,483,647✔
1704
        if (rightRes == 0) {
2,147,483,647✔
1705
          colDataSetNULL(pOutputCol, i);
925,484✔
1706
          continue;
925,484✔
1707
        }
1708
        double leftRes = 0;
2,147,483,647✔
1709
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
2,147,483,647✔
1710
        *output = leftRes / rightRes;
2,147,483,647✔
1711
      }
1712
    } else if (pLeft->numOfRows == 1) {
555,836✔
1713
      if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
408,336✔
1714
        colDataSetNNULL(pOutputCol, 0, pRight->numOfRows);
×
1715
      } else {
1716
        for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
30,828,825✔
1717
          if (IS_HELPER_NULL(pRightCol, i)) {  // divide by 0 check
61,246,056✔
1718
            colDataSetNULL(pOutputCol, i);
×
1719
            continue;
×
1720
          }
1721
          double rightRes = 0;
30,623,571✔
1722
          SCL_ERR_JRET((getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes)));
30,623,028✔
1723
          if (rightRes == 0) {
30,622,485✔
1724
            colDataSetNULL(pOutputCol, i);
×
1725
            continue;
×
1726
          }
1727
          double leftRes = 0;
30,622,485✔
1728
          SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, 0, &leftRes));
30,622,485✔
1729
          *output = leftRes / rightRes;
30,622,485✔
1730
        }
1731
      }
1732
    } else if (pRight->numOfRows == 1) {
351,668✔
1733
      if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value (divde by 0 check)
703,336✔
1734
        colDataSetNNULL(pOutputCol, 0, pLeft->numOfRows);
×
1735
      } else {
1736
        double rightRes = 0;
351,668✔
1737
        SCL_ERR_JRET((getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes)));
351,668✔
1738
        if (rightRes == 0) {
351,668✔
1739
          colDataSetNNULL(pOutputCol, 0, pLeft->numOfRows);
11,029✔
1740
        } else {
1741
          for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
149,240,400✔
1742
            if (IS_HELPER_NULL(pLeftCol, i)) {
297,799,522✔
1743
              colDataSetNULL(pOutputCol, i);
12,581✔
1744
              continue;
12,581✔
1745
            }
1746
            double leftRes = 0;
148,887,180✔
1747
            SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
148,887,180✔
1748
            *output = leftRes / rightRes;
148,887,180✔
1749
          }
1750
        }
1751
      }
1752
    }
1753
  }
1754

1755
_return:
9,722,849✔
1756
  doReleaseVec(pLeftCol, leftConvert);
9,722,849✔
1757
  doReleaseVec(pRightCol, rightConvert);
9,722,315✔
1758
  SCL_RET(code);
9,722,315✔
1759
}
1760

1761
int32_t vectorMathRemainder(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
8,307,237✔
1762
  SColumnInfoData *pOutputCol = pOut->columnData;
8,307,237✔
1763
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
8,308,755✔
1764

1765
  int32_t code = TSDB_CODE_SUCCESS;
8,306,562✔
1766
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
8,306,562✔
1767
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
8,308,042✔
1768

1769
  int32_t          leftConvert = 0, rightConvert = 0;
8,308,042✔
1770
  SColumnInfoData *pLeftCol = NULL;
8,307,854✔
1771
  SColumnInfoData *pRightCol = NULL;
8,308,660✔
1772
  if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
8,308,380✔
1773
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_REM));
×
1774
  } else {
1775
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
8,306,751✔
1776
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
8,306,504✔
1777

1778
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
8,305,710✔
1779
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
8,302,971✔
1780
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
16,612,372✔
1781
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
16,610,275✔
1782

1783
    double *output = (double *)pOutputCol->pData;
8,304,875✔
1784

1785
    int32_t numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
8,304,199✔
1786
    for (; i < numOfRows && i >= 0; i += step, output += 1) {
2,147,483,647✔
1787
      int32_t leftidx = pLeft->numOfRows == 1 ? 0 : i;
2,147,483,647✔
1788
      int32_t rightidx = pRight->numOfRows == 1 ? 0 : i;
2,147,483,647✔
1789
      if (IS_HELPER_NULL(pLeftCol, leftidx) || IS_HELPER_NULL(pRightCol, rightidx)) {
2,147,483,647✔
1790
        colDataSetNULL(pOutputCol, i);
5,653,685✔
1791
        continue;
5,356,200✔
1792
      }
1793

1794
      double lx = 0;
2,147,483,647✔
1795
      double rx = 0;
2,147,483,647✔
1796
      SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, leftidx, &lx));
2,147,483,647✔
1797
      SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, rightidx, &rx));
2,147,483,647✔
1798
      if (isnan(lx) || isinf(lx) || isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
2,147,483,647✔
1799
        colDataSetNULL(pOutputCol, i);
1,343,830✔
1800
        continue;
643,000✔
1801
      }
1802

1803
      *output = lx - ((int64_t)(lx / rx)) * rx;
2,147,483,647✔
1804
    }
1805
  }
1806
_return:
8,307,631✔
1807
  doReleaseVec(pLeftCol, leftConvert);
8,309,242✔
1808
  doReleaseVec(pRightCol, rightConvert);
8,307,742✔
1809
  SCL_RET(code);
8,308,997✔
1810
}
1811

1812
int32_t vectorMathMinus(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
153,242✔
1813
  SColumnInfoData *pOutputCol = pOut->columnData;
153,242✔
1814

1815
  pOut->numOfRows = pLeft->numOfRows;
153,242✔
1816

1817
  int32_t code = TSDB_CODE_SUCCESS;
153,242✔
1818
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : (pLeft->numOfRows - 1);
153,242✔
1819
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
153,242✔
1820

1821
  int32_t          leftConvert = 0;
153,242✔
1822
  SColumnInfoData *pLeftCol = NULL;
153,242✔
1823
  if (IS_DECIMAL_TYPE(pOutputCol->info.type)) {
153,242✔
1824
    SCL_ERR_JRET(vectorMathUnaryOpForDecimal(pLeft, pOut, step, i, OP_TYPE_MINUS));
3,830✔
1825
  } else {
1826
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
149,412✔
1827

1828
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
145,412✔
1829
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
298,824✔
1830

1831
    double *output = (double *)pOutputCol->pData;
149,412✔
1832
    for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) {
20,125,565✔
1833
      if (IS_HELPER_NULL(pLeftCol, i)) {
39,952,306✔
1834
        colDataSetNULL(pOutputCol, i);
58,616✔
1835
        continue;
58,616✔
1836
      }
1837
      double result = 0;
19,917,537✔
1838
      SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &result));
19,917,537✔
1839
      *output = (result == 0) ? 0 : -result;
19,917,537✔
1840
    }
1841
  }
1842

1843
_return:
153,242✔
1844
  doReleaseVec(pLeftCol, leftConvert);
153,242✔
1845
  SCL_RET(code);
153,242✔
1846
}
1847

1848
int32_t vectorAssign(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
205,918,796✔
1849
  SColumnInfoData *pOutputCol = pOut->columnData;
205,918,796✔
1850
  pOut->numOfRows = pLeft->numOfRows;
205,977,285✔
1851

1852
  if (colDataIsNull_s(pRight->columnData, 0)) {
411,945,497✔
1853
    colDataSetNNULL(pOutputCol, 0, pOut->numOfRows);
16,597,010✔
1854
  } else {
1855
    for (int32_t i = 0; i < pOut->numOfRows; ++i) {
2,147,483,647✔
1856
      char *d = colDataGetData(pRight->columnData, 0);
2,147,483,647✔
1857
      SCL_ERR_RET(colDataSetVal(pOutputCol, i, d, false));
2,147,483,647✔
1858
    }
1859
  }
1860

1861
  if (pRight->numOfQualified != 1 && pRight->numOfQualified != 0) {
205,998,625✔
1862
    sclError("vectorAssign: invalid qualified number %d", pRight->numOfQualified);
×
1863
    SCL_ERR_RET(TSDB_CODE_APP_ERROR);
×
1864
  }
1865
  pOut->numOfQualified = pRight->numOfQualified * pOut->numOfRows;
205,987,697✔
1866
  return TSDB_CODE_SUCCESS;
205,963,790✔
1867
}
1868

1869
int32_t vectorAssignRange(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t rowStartIdx,
63,601✔
1870
                          int32_t rowEndIdx, int32_t _ord) {
1871
  SColumnInfoData *pOutputCol = pOut->columnData;
63,601✔
1872

1873
  if (colDataIsNull_s(pRight->columnData, 0)) {
127,202✔
1874
    colDataSetNNULL(pOutputCol, rowStartIdx, (rowEndIdx - rowStartIdx + 1));
×
1875
  } else {
1876
    char *d = colDataGetData(pRight->columnData, 0);
63,601✔
1877
    for (int32_t i = rowStartIdx; i <= rowEndIdx; ++i) {
127,202✔
1878
      SCL_ERR_RET(colDataSetVal(pOutputCol, i, d, false));
63,601✔
1879
    }
1880
  }
1881

1882
  if (pRight->numOfQualified != 1 && pRight->numOfQualified != 0) {
63,601✔
1883
    sclError("vectorAssign: invalid qualified number %d", pRight->numOfQualified);
×
1884
    SCL_ERR_RET(TSDB_CODE_APP_ERROR);
×
1885
  }
1886
  pOut->numOfQualified += pRight->numOfQualified * ((rowEndIdx - rowStartIdx + 1));
63,601✔
1887
  return TSDB_CODE_SUCCESS;
63,601✔
1888
}
1889

1890
int32_t vectorBitAnd(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
105,304✔
1891
  SColumnInfoData *pOutputCol = pOut->columnData;
105,304✔
1892
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
105,304✔
1893

1894
  int32_t code = TSDB_CODE_SUCCESS;
105,304✔
1895
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
105,304✔
1896
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
105,304✔
1897

1898
  int32_t          leftConvert = 0, rightConvert = 0;
105,304✔
1899
  SColumnInfoData *pLeftCol = NULL;
105,304✔
1900
  SColumnInfoData *pRightCol = NULL;
105,304✔
1901
  SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
105,304✔
1902
  SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
105,304✔
1903

1904
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
100,304✔
1905
  _getBigintValue_fn_t getVectorBigintValueFnRight;
100,304✔
1906
  SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
105,304✔
1907
  SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
105,304✔
1908

1909
  int64_t *output = (int64_t *)pOutputCol->pData;
105,304✔
1910
  int32_t  numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
105,304✔
1911
  for (; i < numOfRows && i >= 0; i += step, output += 1) {
1,696,879✔
1912
    int32_t leftidx = pLeft->numOfRows == 1 ? 0 : i;
1,591,575✔
1913
    int32_t rightidx = pRight->numOfRows == 1 ? 0 : i;
1,591,575✔
1914
    if (IS_HELPER_NULL(pRightCol, rightidx) || IS_HELPER_NULL(pLeftCol, leftidx)) {
4,646,405✔
1915
      colDataSetNULL(pOutputCol, i);
180,954✔
1916
      continue;  // TODO set null or ignore
180,954✔
1917
    }
1918
    int64_t leftRes = 0;
1,410,621✔
1919
    int64_t rightRes = 0;
1,410,621✔
1920
    SCL_ERR_JRET(getVectorBigintValueFnLeft(LEFT_COL, leftidx, &leftRes));
1,410,621✔
1921
    SCL_ERR_JRET(getVectorBigintValueFnRight(RIGHT_COL, rightidx, &rightRes));
1,410,621✔
1922
    *output = leftRes & rightRes;
1,410,621✔
1923
  }
1924

1925
_return:
105,304✔
1926
  doReleaseVec(pLeftCol, leftConvert);
105,304✔
1927
  doReleaseVec(pRightCol, rightConvert);
105,304✔
1928
  SCL_RET(code);
105,304✔
1929
}
1930

1931
int32_t vectorBitOr(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
25,054✔
1932
  SColumnInfoData *pOutputCol = pOut->columnData;
25,054✔
1933
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
25,054✔
1934

1935
  int32_t code = TSDB_CODE_SUCCESS;
25,054✔
1936
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
25,054✔
1937
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
25,054✔
1938

1939
  int32_t          leftConvert = 0, rightConvert = 0;
25,054✔
1940
  SColumnInfoData *pLeftCol = NULL;
25,054✔
1941
  SColumnInfoData *pRightCol = NULL;
25,054✔
1942
  SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
25,054✔
1943
  SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
25,054✔
1944

1945
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
20,254✔
1946
  _getBigintValue_fn_t getVectorBigintValueFnRight;
20,254✔
1947
  SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
25,054✔
1948
  SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
25,054✔
1949

1950
  int64_t *output = (int64_t *)pOutputCol->pData;
25,054✔
1951
  int32_t  numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
25,054✔
1952
  for (; i < numOfRows && i >= 0; i += step, output += 1) {
826,157✔
1953
    int32_t leftidx = pLeft->numOfRows == 1 ? 0 : i;
801,103✔
1954
    int32_t rightidx = pRight->numOfRows == 1 ? 0 : i;
801,103✔
1955
    if (IS_HELPER_NULL(pRightCol, leftidx) || IS_HELPER_NULL(pLeftCol, rightidx)) {
2,359,736✔
1956
      colDataSetNULL(pOutputCol, i);
48,237✔
1957
      continue;  // TODO set null or ignore
48,237✔
1958
    }
1959

1960
    int64_t leftRes = 0;
752,866✔
1961
    int64_t rightRes = 0;
752,866✔
1962
    SCL_ERR_JRET(getVectorBigintValueFnLeft(LEFT_COL, leftidx, &leftRes));
752,866✔
1963
    SCL_ERR_JRET(getVectorBigintValueFnRight(RIGHT_COL, rightidx, &rightRes));
752,866✔
1964
    *output = leftRes | rightRes;
752,866✔
1965
  }
1966

1967
_return:
25,054✔
1968
  doReleaseVec(pLeftCol, leftConvert);
25,054✔
1969
  doReleaseVec(pRightCol, rightConvert);
25,054✔
1970
  SCL_RET(code);
25,054✔
1971
}
1972

1973
int32_t vectorCompareWithHashParam(SSclCompareCtx *pCtx) {
14,947,755✔
1974
  int32_t     code = TSDB_CODE_SUCCESS, i = pCtx->startIndex;
14,947,755✔
1975
  SHashParam *pHParam = &pCtx->pRight->hashParam;
14,949,728✔
1976
  bool        isNegativeOp = pCtx->pOut->hashParam.isNegativeOp;
14,949,063✔
1977
  bool multiRowsInHash = (taosHashGetSize(pHParam->pHashFilter) > 1 || taosHashGetSize(pHParam->pHashFilterOthers) > 1);
14,947,957✔
1978
  bool res = false, resIsNull = false;
14,946,213✔
1979

1980
  sclDebug("%s compare param, hasValue:%d, hasNull:%d, hasNotNull:%d, isNevativeOp:%d, hashNum:%d, hashOthersNum:%d",
14,947,124✔
1981
           __func__, pHParam->hasValue, pHParam->hasNull, pHParam->hasNotNull, isNegativeOp,
1982
           taosHashGetSize(pHParam->pHashFilter), taosHashGetSize(pHParam->pHashFilterOthers));
1983

1984
  if (!pHParam->hasValue) {
14,947,768✔
1985
    res = (pCtx->optr == OP_TYPE_IN) ? false : true;
2,705,071✔
1986
    char *pRes = colDataGetData(pCtx->pOut->columnData, pCtx->startIndex);
2,705,071✔
1987
    memset(pRes, res, pCtx->pLeft->numOfRows);
2,705,071✔
1988
    if (res) {
2,705,071✔
1989
      *pCtx->qualifiedNum += pCtx->pLeft->numOfRows;
901,629✔
1990
    }
1991

1992
    return code;
2,705,071✔
1993
  }
1994

1995
  if ((NULL == pHParam->pHashFilter || 0 == taosHashGetSize(pHParam->pHashFilter)) &&
12,243,951✔
1996
      (NULL == pHParam->pHashFilterOthers || 0 == taosHashGetSize(pHParam->pHashFilterOthers))) {
2,717,977✔
1997
    if (isNegativeOp) {
2,435,286✔
1998
      if (!pHParam->hasNotNull) {
1,207,200✔
1999
        res = false;
1,207,200✔
2000
        resIsNull = true;
1,207,200✔
2001
      } else {
2002
        res = (pCtx->optr == OP_TYPE_IN) ? true : false;
×
2003
        resIsNull = false;
×
2004
      }
2005
    } else {
2006
      res = pHParam->hasNull ? false : ((pCtx->optr == OP_TYPE_IN) ? false : true);
1,228,086✔
2007
      resIsNull = true;
1,228,086✔
2008
    }
2009

2010
    for (; i < pCtx->endIndex; i++) {
8,185,128✔
2011
      if (IS_HELPER_NULL(pCtx->pLeft->columnData, i)) {
11,499,684✔
2012
        bool res1 = false;
1,659,554✔
2013
        colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res1);
1,659,554✔
2014
        colDataSetNULL(pCtx->pOut->columnData, i);
1,659,554✔
2015
        continue;
1,659,554✔
2016
      }
2017

2018
      colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res);
4,090,288✔
2019
      if (res) {
4,090,288✔
2020
        ++(*pCtx->qualifiedNum);
5,330✔
2021
      } else if (resIsNull) {
4,084,958✔
2022
        colDataSetNULL(pCtx->pOut->columnData, i);
4,084,958✔
2023
      }
2024
    }
2025

2026
    return code;
2,435,286✔
2027
  }
2028

2029
  __compar_fn_t fpVar = NULL;
9,805,610✔
2030
  if (pCtx->pLeftVar != NULL) {
9,808,665✔
2031
    SCL_ERR_RET(filterGetCompFunc(&fpVar, GET_PARAM_TYPE(pCtx->pLeftVar), pCtx->optr));
583,136✔
2032
  }
2033

2034
  for (; i < pCtx->endIndex; i++) {
1,033,344,000✔
2035
    if (IS_HELPER_NULL(pCtx->pLeft->columnData, i)) {
2,047,378,300✔
2036
      res = false;
14,170,222✔
2037
      colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res);
14,170,222✔
2038
      colDataSetNULL(pCtx->pOut->columnData, i);
14,174,530✔
2039
      continue;
14,174,530✔
2040
    }
2041

2042
    if (isNegativeOp && multiRowsInHash) {
1,009,645,311✔
2043
      res = OP_TYPE_IN == pCtx->optr ? true : false;
3,652,312✔
2044
      colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res);
3,652,312✔
2045
      if (res) {
3,652,312✔
2046
        ++(*pCtx->qualifiedNum);
2,501,120✔
2047
      }
2048

2049
      continue;
3,652,312✔
2050
    }
2051

2052
    res = pHParam->pHashFilter
1,006,032,399✔
2053
              ? compareForTypeWithColAndHash(pCtx->fp, pCtx->optr, pCtx->pLeft->columnData, i, pHParam->pHashFilter,
1,005,683,232✔
2054
                                             pHParam->filterValueType, pHParam->filterValueTypeMod)
2055
              : (OP_TYPE_IN == pCtx->optr ? false : true);
2,011,145,656✔
2056
    if (!((pCtx->optr == OP_TYPE_IN && res) || (pCtx->optr == OP_TYPE_NOT_IN && !res))) {
1,005,462,424✔
2057
      if (pCtx->pLeftVar != NULL && pHParam->pHashFilterOthers && taosHashGetSize(pHParam->pHashFilterOthers) > 0) {
839,386,939✔
2058
        res = compareForTypeWithColAndHash(fpVar, pCtx->optr, pCtx->pLeftVar->columnData, i, pHParam->pHashFilterOthers,
5,814,283✔
2059
                                           pHParam->filterValueType, pHParam->filterValueTypeMod);
2060
      }
2061
    }
2062

2063
    if (isNegativeOp) {
1,005,424,927✔
2064
      if ((!((pCtx->optr == OP_TYPE_IN && res) || (pCtx->optr == OP_TYPE_NOT_IN && !res))) || (!pHParam->hasNull)) {
915,773✔
2065
        res = !res;
882,575✔
2066
      } else {
2067
        res = false;
33,198✔
2068
        colDataSetNULL(pCtx->pOut->columnData, i);
33,198✔
2069
      }
2070
    } else if (pHParam->hasNull && !((pCtx->optr == OP_TYPE_IN && res) || (pCtx->optr == OP_TYPE_NOT_IN && !res))) {
1,004,509,154✔
2071
      res = false;
1,451,541✔
2072
      colDataSetNULL(pCtx->pOut->columnData, i);
1,451,541✔
2073
    }
2074

2075
    colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res);
1,005,437,113✔
2076
    if (res) {
1,005,732,082✔
2077
      ++(*pCtx->qualifiedNum);
503,221,716✔
2078
    }
2079
  }
2080

2081
  return code;
9,811,088✔
2082
}
2083

2084
int32_t vectorCompareBetweenMathTypes(SSclCompareCtx *pCtx) {
103,285,689✔
2085
  bool   *pRes = (bool *)pCtx->pOut->columnData->pData;
103,285,689✔
2086
  bool    chkTrue = pCtx->pRight->remoteParam.hasRemoteParam && !pCtx->isAny;
103,318,340✔
2087
  bool    chkFalse = pCtx->pRight->remoteParam.hasRemoteParam && pCtx->isAny;
103,289,952✔
2088
  bool    hasNull = pCtx->pRight->remoteParam.hasRemoteParam && pCtx->pRight->remoteParam.hasNull;
103,291,597✔
2089
  int32_t code = TSDB_CODE_SUCCESS;
103,289,962✔
2090

2091
  if (!(pCtx->pLeft->columnData->hasNull || pCtx->pRight->columnData->hasNull)) {
103,289,962✔
2092
    for (int32_t i = pCtx->startIndex; i < pCtx->endIndex && i >= 0; i++) {
2,147,483,647✔
2093
      int32_t leftIndex = (i >= pCtx->pLeft->numOfRows) ? 0 : i;
2,147,483,647✔
2094
      int32_t rightIndex = (i >= pCtx->pRight->numOfRows) ? 0 : i;
2,147,483,647✔
2095

2096
      pRes[i] = compareForType(pCtx->fp, pCtx->optr, pCtx->pLeft->columnData, leftIndex, pCtx->pRight->columnData,
2,147,483,647✔
2097
                               rightIndex);
2098
      if (pRes[i]) {
2,147,483,647✔
2099
        if (chkTrue && hasNull) {
2,147,483,647✔
2100
          pRes[i] = false;
256,530✔
2101
          colDataSetNULL(pCtx->pOut->columnData, i);
256,530✔
2102
        } else {
2103
          ++(*pCtx->qualifiedNum);
2,147,483,647✔
2104
        }
2105
      } else if (chkFalse && hasNull) {
2,147,483,647✔
2106
        colDataSetNULL(pCtx->pOut->columnData, i);
513,060✔
2107
      }
2108
    }
2109

2110
    return code;
79,235,155✔
2111
  }
2112

2113
  for (int32_t i = pCtx->startIndex; i < pCtx->endIndex; i++) {
2,147,483,647✔
2114
    int32_t leftIndex = (i >= pCtx->pLeft->numOfRows) ? 0 : i;
2,147,483,647✔
2115
    int32_t rightIndex = (i >= pCtx->pRight->numOfRows) ? 0 : i;
2,147,483,647✔
2116

2117
    if (colDataIsNull_f(pCtx->pLeft->columnData, leftIndex) || colDataIsNull_f(pCtx->pRight->columnData, rightIndex)) {
2,147,483,647✔
2118
      pRes[i] = false;
2,147,483,647✔
2119
      colDataSetNULL(pCtx->pOut->columnData, i);
2,147,483,647✔
2120
      continue;
2,147,483,647✔
2121
    }
2122

2123
    pRes[i] =
2,147,483,647✔
2124
        compareForType(pCtx->fp, pCtx->optr, pCtx->pLeft->columnData, leftIndex, pCtx->pRight->columnData, rightIndex);
2,147,483,647✔
2125
    if (pRes[i]) {
2,147,483,647✔
2126
      if (chkTrue && hasNull) {
2,147,483,647✔
2127
        pRes[i] = false;
852,082✔
2128
        colDataSetNULL(pCtx->pOut->columnData, i);
852,082✔
2129
      } else {
2130
        ++(*pCtx->qualifiedNum);
2,147,483,647✔
2131
      }
2132
    } else if (chkFalse && hasNull) {
2,147,483,647✔
2133
      colDataSetNULL(pCtx->pOut->columnData, i);
1,704,164✔
2134
    }
2135
  }
2136

2137
  return code;
24,089,258✔
2138
}
2139

2140
int32_t vectorCompareIncludeVarTypes(SSclCompareCtx *pCtx) {
16,636,518✔
2141
  bool chkTrue = pCtx->pRight->remoteParam.hasRemoteParam && !pCtx->isAny;
16,636,518✔
2142
  bool chkFalse = pCtx->pRight->remoteParam.hasRemoteParam && pCtx->isAny;
16,636,979✔
2143
  bool hasNull = pCtx->pRight->remoteParam.hasRemoteParam && pCtx->pRight->remoteParam.hasNull;
16,638,832✔
2144

2145
  for (int32_t i = pCtx->startIndex; i < pCtx->endIndex; i++) {
2,147,483,647✔
2146
    int32_t leftIndex = (i >= pCtx->pLeft->numOfRows) ? 0 : i;
2,147,483,647✔
2147
    int32_t rightIndex = (i >= pCtx->pRight->numOfRows) ? 0 : i;
2,147,483,647✔
2148

2149
    if (IS_HELPER_NULL(pCtx->pLeft->columnData, leftIndex) || IS_HELPER_NULL(pCtx->pRight->columnData, rightIndex)) {
2,147,483,647✔
2150
      bool res = false;
855,280,559✔
2151
      colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res);
855,287,293✔
2152
      colDataSetNULL(pCtx->pOut->columnData, i);
855,287,293✔
2153
      continue;
855,287,293✔
2154
    }
2155

2156
    char   *pLeftData = colDataGetData(pCtx->pLeft->columnData, leftIndex);
2,147,483,647✔
2157
    char   *pRightData = colDataGetData(pCtx->pRight->columnData, rightIndex);
2,147,483,647✔
2158
    int64_t leftOut = 0;
2,147,483,647✔
2159
    int64_t rightOut = 0;
2,147,483,647✔
2160
    bool    freeLeft = false;
2,147,483,647✔
2161
    bool    freeRight = false;
2,147,483,647✔
2162
    bool    isJsonnull = false;
2,147,483,647✔
2163
    bool    result = false;
2,147,483,647✔
2164

2165
    SCL_ERR_RET(convertJsonValue(&pCtx->fp, pCtx->optr, GET_PARAM_TYPE(pCtx->pLeft), GET_PARAM_TYPE(pCtx->pRight),
2,147,483,647✔
2166
                                 &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull, &freeLeft, &freeRight,
2167
                                 &result, pCtx->pLeft->charsetCxt));
2168

2169
    if (isJsonnull) {
2,147,483,647✔
2170
      sclError("doVectorCompareImpl: invalid json null value");
×
2171
      SCL_ERR_RET(TSDB_CODE_APP_ERROR);
77,066✔
2172
    }
2173

2174
    if (!pLeftData || !pRightData) {
2,147,483,647✔
2175
      result = false;
×
2176
    }
2177
    if (!result) {
2,147,483,647✔
2178
      colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&result);
221,313✔
2179
    } else {
2180
      bool res = filterDoCompare(pCtx->fp, pCtx->optr, pLeftData, pRightData);
2,147,483,647✔
2181
      colDataSetInt8(pCtx->pOut->columnData, i, (int8_t *)&res);
2,147,483,647✔
2182
      if (res) {
2,147,483,647✔
2183
        if (chkTrue && hasNull) {
909,650,352✔
2184
          colDataSetNULL(pCtx->pOut->columnData, i);
35,210✔
2185
        } else {
2186
          ++(*pCtx->qualifiedNum);
909,615,142✔
2187
        }
2188
      } else if (chkFalse && hasNull) {
1,426,782,140✔
2189
        colDataSetNULL(pCtx->pOut->columnData, i);
70,420✔
2190
      }
2191
    }
2192

2193
    if (freeLeft) {
2,147,483,647✔
2194
      taosMemoryFreeClear(pLeftData);
54,373✔
2195
    }
2196

2197
    if (freeRight) {
2,147,483,647✔
2198
      taosMemoryFreeClear(pRightData);
×
2199
    }
2200
  }
2201

2202
  return TSDB_CODE_SUCCESS;
16,645,474✔
2203
}
2204

2205
int32_t vectorCompareWithRemoteParam(SSclCompareCtx *pCtx) {
20,820,368✔
2206
  SRemoteParam *pRemote = &pCtx->pRight->remoteParam;
20,820,368✔
2207
  int32_t       code = TSDB_CODE_SUCCESS, i = pCtx->startIndex;
20,820,368✔
2208

2209
  pCtx->isAny = ((OP_TYPE_GREATER_EQUAL == pCtx->optr || OP_TYPE_GREATER_THAN == pCtx->optr) && pRemote->isMinVal) ||
34,783,587✔
2210
                ((OP_TYPE_LOWER_EQUAL == pCtx->optr || OP_TYPE_LOWER_THAN == pCtx->optr) && !pRemote->isMinVal);
13,963,219✔
2211

2212
  if (!pRemote->hasValue) {
20,820,368✔
2213
    bool  res = pCtx->isAny ? false : true;
5,613,480✔
2214
    char *pRes = colDataGetData(pCtx->pOut->columnData, pCtx->startIndex);
5,613,480✔
2215
    memset(pRes, res, pCtx->pLeft->numOfRows);
5,613,480✔
2216
    if (res) {
5,613,480✔
2217
      *pCtx->qualifiedNum += pCtx->pLeft->numOfRows;
1,871,160✔
2218
    }
2219

2220
    return code;
5,613,480✔
2221
  }
2222

2223
  if (colDataIsNull_s(pCtx->pRight->columnData, 0)) {
30,413,776✔
2224
    bool  res = false;
4,792,584✔
2225
    char *pRes = colDataGetData(pCtx->pOut->columnData, pCtx->startIndex);
4,792,584✔
2226
    memset(pRes, res, pCtx->pLeft->numOfRows);
4,792,584✔
2227
    colDataSetNNULL(pCtx->pOut->columnData, pCtx->startIndex, pCtx->pLeft->numOfRows);
4,792,584✔
2228
    return code;
4,792,584✔
2229
  }
2230

2231
  if (IS_MATHABLE_TYPE(GET_PARAM_TYPE(pCtx->pLeft)) && IS_MATHABLE_TYPE(GET_PARAM_TYPE(pCtx->pRight))) {
10,414,304✔
2232
    return vectorCompareBetweenMathTypes(pCtx);
9,714,128✔
2233
  }
2234

2235
  return vectorCompareIncludeVarTypes(pCtx);
700,176✔
2236
}
2237

2238
int32_t doVectorCompare(SSclCompareCtx *pCtx) {
145,293,942✔
2239
  int32_t code = TSDB_CODE_SUCCESS;
145,293,942✔
2240

2241
  if (pCtx->pRight->hashParam.hasHashParam) {
145,293,942✔
2242
    return vectorCompareWithHashParam(pCtx);
14,948,378✔
2243
  }
2244

2245
  if (pCtx->pRight->remoteParam.hasRemoteParam) {
130,347,356✔
2246
    return vectorCompareWithRemoteParam(pCtx);
20,820,368✔
2247
  }
2248

2249
  if (IS_MATHABLE_TYPE(GET_PARAM_TYPE(pCtx->pLeft)) && IS_MATHABLE_TYPE(GET_PARAM_TYPE(pCtx->pRight))) {
109,478,060✔
2250
    return vectorCompareBetweenMathTypes(pCtx);
93,562,711✔
2251
  }
2252

2253
  return vectorCompareIncludeVarTypes(pCtx);
15,932,170✔
2254
}
2255

2256
int32_t vectorCompareImpl(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
146,082,783✔
2257
                          int32_t numOfRows, int32_t optr) {
2258
  SScalarParam   pLeftOut = {0};
146,082,783✔
2259
  SScalarParam   pRightOut = {0};
146,094,414✔
2260
  SSclCompareCtx ctx = {0};
146,084,837✔
2261
  int32_t        code = TSDB_CODE_SUCCESS;
146,086,778✔
2262

2263
  ctx.pOut = pOut;
146,086,778✔
2264
  ctx.optr = optr;
146,086,778✔
2265
  ctx.qualifiedNum = &pOut->numOfQualified;
146,086,778✔
2266

2267
  setTzCharset(&pLeftOut, pLeft->tz, pLeft->charsetCxt);
146,112,509✔
2268
  setTzCharset(&pRightOut, pLeft->tz, pLeft->charsetCxt);
146,119,964✔
2269
  if (noConvertBeforeCompare(GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), optr)) {
146,061,251✔
2270
    ctx.pLeft = pLeft;
94,538,356✔
2271
    ctx.pRight = pRight;
94,538,356✔
2272
  } else {
2273
    SCL_ERR_JRET(vectorConvertCols(pLeft, pRight, &pLeftOut, &pRightOut, startIndex, numOfRows));
51,542,692✔
2274
    ctx.pLeft = (pLeftOut.columnData != NULL) ? &pLeftOut : pLeft;
50,789,303✔
2275
    if (pRightOut.columnData != NULL) {
50,789,303✔
2276
      ctx.pRight = &pRightOut;
12,631,703✔
2277
      if (pRight->hashParam.hasHashParam) {
12,631,703✔
2278
        pRightOut.hashParam = pRight->hashParam;
×
2279
      }
2280
      if (pRight->remoteParam.hasRemoteParam) {
12,632,722✔
2281
        pRightOut.remoteParam = pRight->remoteParam;
6,361,944✔
2282
      }
2283
    } else {
2284
      ctx.pRight = pRight;
38,157,600✔
2285
    }
2286
    if (pRight->hashParam.pHashFilterOthers != NULL) {
50,794,730✔
2287
      ctx.pLeftVar = pLeft;
715,782✔
2288
    }
2289
  }
2290

2291
  int32_t lType = GET_PARAM_TYPE(ctx.pLeft);
145,330,623✔
2292
  int32_t rType = GET_PARAM_TYPE(ctx.pRight);
145,292,743✔
2293
  if (lType == rType) {
145,293,857✔
2294
    SCL_ERR_JRET(filterGetCompFunc(&ctx.fp, lType, optr));
72,005,664✔
2295
  } else {
2296
    ctx.fp = filterGetCompFuncEx(lType, rType, optr);
73,288,193✔
2297
  }
2298

2299
  if (startIndex < 0) {
145,272,706✔
2300
    ctx.startIndex = 0;
144,881,287✔
2301
    pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
144,881,287✔
2302
    ctx.endIndex = pOut->numOfRows;
144,877,244✔
2303
  } else {
2304
    ctx.endIndex = startIndex + numOfRows;
391,419✔
2305
    ctx.startIndex = startIndex;
391,419✔
2306
  }
2307

2308
  SCL_ERR_JRET(doVectorCompare(&ctx));
145,268,085✔
2309

2310
_return:
146,118,534✔
2311

2312
  sclFreeParam(&pLeftOut);
146,111,587✔
2313
  sclFreeParam(&pRightOut);
146,073,820✔
2314

2315
  SCL_RET(code);
146,091,095✔
2316
}
2317

2318
int32_t vectorCompare(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
145,687,219✔
2319
  SCL_RET(vectorCompareImpl(pLeft, pRight, pOut, -1, -1, optr));
145,687,219✔
2320
}
2321

2322
int32_t vectorGreater(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
23,105,589✔
2323
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN));
23,105,589✔
2324
}
2325

2326
int32_t vectorGreaterEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
22,151,767✔
2327
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL));
22,151,767✔
2328
}
2329

2330
int32_t vectorLower(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
12,726,620✔
2331
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN));
12,726,620✔
2332
}
2333

2334
int32_t vectorLowerEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
13,717,856✔
2335
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL));
13,717,856✔
2336
}
2337

2338
int32_t vectorEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
34,602,097✔
2339
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL));
34,602,097✔
2340
}
2341

2342
int32_t vectorNotEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
17,513,415✔
2343
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL));
17,513,415✔
2344
}
2345

2346
int32_t vectorIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
10,487,210✔
2347
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN));
10,487,210✔
2348
}
2349

2350
int32_t vectorNotIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
4,509,956✔
2351
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN));
4,509,956✔
2352
}
2353

2354
int32_t vectorLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
4,661,341✔
2355
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE));
4,661,341✔
2356
}
2357

2358
int32_t vectorNotLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
74,271✔
2359
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE));
74,271✔
2360
}
2361

2362
int32_t vectorMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1,016,084✔
2363
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH));
1,016,084✔
2364
}
2365

2366
int32_t vectorNotMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
922,240✔
2367
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH));
922,240✔
2368
}
2369

2370
int32_t vectorIsNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1,072,725✔
2371
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
784,247,952✔
2372
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 1 : 0;
1,566,350,454✔
2373
    if (v) {
783,175,227✔
2374
      ++pOut->numOfQualified;
78,781,258✔
2375
    }
2376
    colDataSetInt8(pOut->columnData, i, &v);
783,175,227✔
2377
    colDataClearNull_f(pOut->columnData->nullbitmap, i);
783,175,227✔
2378
  }
2379
  pOut->numOfRows = pLeft->numOfRows;
1,072,725✔
2380
  return TSDB_CODE_SUCCESS;
1,072,725✔
2381
}
2382

2383
int32_t vectorNotNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
51,645,961✔
2384
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
2,147,483,647✔
2385
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 0 : 1;
2,147,483,647✔
2386
    if (v) {
2,147,483,647✔
2387
      ++pOut->numOfQualified;
2,147,483,647✔
2388
    }
2389
    colDataSetInt8(pOut->columnData, i, &v);
2,147,483,647✔
2390
    colDataClearNull_f(pOut->columnData->nullbitmap, i);
2,147,483,647✔
2391
  }
2392
  pOut->numOfRows = pLeft->numOfRows;
51,651,684✔
2393
  return TSDB_CODE_SUCCESS;
51,652,254✔
2394
}
2395

2396
int32_t vectorIsTrue(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
762,926✔
2397
  SCL_ERR_RET(vectorConvertSingleColImpl(pLeft, pOut, NULL, -1, -1));
762,926✔
2398
  for (int32_t i = 0; i < pOut->numOfRows; ++i) {
2,393,124✔
2399
    if (colDataIsNull_s(pOut->columnData, i)) {
3,260,396✔
2400
      int8_t v = 0;
62,792✔
2401
      colDataSetInt8(pOut->columnData, i, &v);
62,792✔
2402
      // colDataClearNull_f(pOut->columnData->nullbitmap, i);
2403
    }
2404
    {
2405
      bool v = false;
1,630,198✔
2406
      GET_TYPED_DATA(v, bool, pOut->columnData->info.type, colDataGetData(pOut->columnData, i),
1,630,198✔
2407
                     typeGetTypeModFromColInfo(&pOut->columnData->info));
2408
      if (v) {
1,630,198✔
2409
        ++pOut->numOfQualified;
1,483,488✔
2410
      }
2411
    }
2412
  }
2413
  pOut->columnData->hasNull = false;
762,926✔
2414
  return TSDB_CODE_SUCCESS;
762,926✔
2415
}
2416

2417
int32_t getJsonValue(char *json, char *key, bool *isExist, STagVal *val) {
2,932,737✔
2418
  val->pKey = key;
2,932,737✔
2419
  if (json == NULL || tTagIsJson((const STag *)json) == false) {
2,932,737✔
2420
    if (isExist) {
501✔
2421
      *isExist = false;
501✔
2422
    }
2423
    SCL_ERR_RET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
501✔
2424
  }
2425

2426
  bool find = tTagGet(((const STag *)json), val);  // json value is null and not exist is different
2,932,236✔
2427
  if (isExist) {
2,932,236✔
2428
    *isExist = find;
2,932,236✔
2429
  }
2430
  SCL_RET(TSDB_CODE_SUCCESS);
2,932,236✔
2431
}
2432

2433
int32_t vectorJsonContains(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
5,078✔
2434
  SColumnInfoData *pOutputCol = pOut->columnData;
5,078✔
2435

2436
  int32_t code = TSDB_CODE_SUCCESS;
5,078✔
2437
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
5,078✔
2438
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
5,078✔
2439

2440
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
5,078✔
2441

2442
  char *pRightData = colDataGetVarData(pRight->columnData, 0);
5,078✔
2443
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
5,078✔
2444
  if (NULL == jsonKey) {
5,078✔
2445
    SCL_ERR_RET(terrno);
×
2446
  }
2447
  (void)memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
5,078✔
2448
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
50,270✔
2449
    bool isExist = false;
45,192✔
2450

2451
    if (!colDataIsNull_var(pLeft->columnData, i)) {
45,192✔
2452
      char   *pLeftData = colDataGetVarData(pLeft->columnData, i);
29,593✔
2453
      STagVal value;
29,593✔
2454
      SCL_ERR_JRET(getJsonValue(pLeftData, jsonKey, &isExist, &value));
29,593✔
2455
    }
2456
    if (isExist) {
45,192✔
2457
      ++pOut->numOfQualified;
14,062✔
2458
    }
2459
    SCL_ERR_JRET(colDataSetVal(pOutputCol, i, (const char *)(&isExist), false));
45,192✔
2460
  }
2461

2462
_return:
5,078✔
2463
  taosMemoryFree(jsonKey);
5,078✔
2464
  SCL_RET(code);
5,078✔
2465
}
2466

2467
int32_t vectorJsonArrow(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
591,115✔
2468
  SColumnInfoData *pOutputCol = pOut->columnData;
591,115✔
2469

2470
  int32_t code = TSDB_CODE_SUCCESS;
590,706✔
2471
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
590,706✔
2472
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
590,706✔
2473

2474
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
590,706✔
2475

2476
  char *pRightData = colDataGetVarData(pRight->columnData, 0);
590,713✔
2477
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
590,706✔
2478
  if (NULL == jsonKey) {
591,115✔
2479
    SCL_ERR_RET(terrno);
×
2480
  }
2481
  (void)memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
591,115✔
2482
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
3,859,005✔
2483
    if (colDataIsNull_var(pLeft->columnData, i)) {
3,268,391✔
2484
      colDataSetNull_var(pOutputCol, i);
365,247✔
2485
      pOutputCol->hasNull = true;
365,247✔
2486
      continue;
365,247✔
2487
    }
2488
    char   *pLeftData = colDataGetVarData(pLeft->columnData, i);
2,902,735✔
2489
    bool    isExist = false;
2,903,144✔
2490
    STagVal value;
2,833,144✔
2491
    SCL_ERR_JRET(getJsonValue(pLeftData, jsonKey, &isExist, &value));
2,903,144✔
2492
    char *data = isExist ? tTagValToData(&value, true) : NULL;
2,902,643✔
2493
    code = colDataSetVal(pOutputCol, i, data, data == NULL);
2,902,643✔
2494
    if (isExist && IS_VAR_DATA_TYPE(value.type) && data) {
2,902,643✔
2495
      taosMemoryFree(data);
1,736,601✔
2496
    }
2497
    SCL_ERR_JRET(code);
2,902,643✔
2498
  }
2499

2500
_return:
590,614✔
2501
  taosMemoryFree(jsonKey);
591,115✔
2502
  SCL_RET(code);
591,115✔
2503
}
2504

2505
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
676,066,008✔
2506
  switch (binFunctionId) {
676,066,008✔
2507
    case OP_TYPE_ADD:
221,650,758✔
2508
      return vectorMathAdd;
221,650,758✔
2509
    case OP_TYPE_SUB:
26,557,434✔
2510
      return vectorMathSub;
26,557,434✔
2511
    case OP_TYPE_MULTI:
4,007,689✔
2512
      return vectorMathMultiply;
4,007,689✔
2513
    case OP_TYPE_DIV:
9,722,849✔
2514
      return vectorMathDivide;
9,722,849✔
2515
    case OP_TYPE_REM:
8,307,855✔
2516
      return vectorMathRemainder;
8,307,855✔
2517
    case OP_TYPE_MINUS:
153,242✔
2518
      return vectorMathMinus;
153,242✔
2519
    case OP_TYPE_ASSIGN:
205,950,198✔
2520
      return vectorAssign;
205,950,198✔
2521
    case OP_TYPE_GREATER_THAN:
23,106,413✔
2522
      return vectorGreater;
23,106,413✔
2523
    case OP_TYPE_GREATER_EQUAL:
22,154,368✔
2524
      return vectorGreaterEqual;
22,154,368✔
2525
    case OP_TYPE_LOWER_THAN:
12,726,819✔
2526
      return vectorLower;
12,726,819✔
2527
    case OP_TYPE_LOWER_EQUAL:
13,722,951✔
2528
      return vectorLowerEqual;
13,722,951✔
2529
    case OP_TYPE_EQUAL:
34,603,726✔
2530
      return vectorEqual;
34,603,726✔
2531
    case OP_TYPE_NOT_EQUAL:
17,517,515✔
2532
      return vectorNotEqual;
17,517,515✔
2533
    case OP_TYPE_IN:
10,487,455✔
2534
      return vectorIn;
10,487,455✔
2535
    case OP_TYPE_NOT_IN:
4,509,956✔
2536
      return vectorNotIn;
4,509,956✔
2537
    case OP_TYPE_LIKE:
4,661,341✔
2538
      return vectorLike;
4,661,341✔
2539
    case OP_TYPE_NOT_LIKE:
74,271✔
2540
      return vectorNotLike;
74,271✔
2541
    case OP_TYPE_MATCH:
1,017,725✔
2542
      return vectorMatch;
1,017,725✔
2543
    case OP_TYPE_NMATCH:
921,733✔
2544
      return vectorNotMatch;
921,733✔
2545
    case OP_TYPE_IS_NULL:
1,072,725✔
2546
      return vectorIsNull;
1,072,725✔
2547
    case OP_TYPE_IS_NOT_NULL:
51,647,917✔
2548
      return vectorNotNull;
51,647,917✔
2549
    case OP_TYPE_BIT_AND:
105,304✔
2550
      return vectorBitAnd;
105,304✔
2551
    case OP_TYPE_BIT_OR:
25,054✔
2552
      return vectorBitOr;
25,054✔
2553
    case OP_TYPE_IS_TRUE:
762,926✔
2554
      return vectorIsTrue;
762,926✔
2555
    case OP_TYPE_JSON_GET_VALUE:
591,115✔
2556
      return vectorJsonArrow;
591,115✔
2557
    case OP_TYPE_JSON_CONTAINS:
5,078✔
2558
      return vectorJsonContains;
5,078✔
2559
    default:
2,060✔
2560
      return NULL;
2,060✔
2561
  }
2562
}
2563

2564
bool checkOperatorRestypeIsTimestamp(EOperatorType opType, int32_t lType, int32_t rType) {
118,526,284✔
2565
  if (opType != OP_TYPE_ADD && opType != OP_TYPE_SUB && opType != OP_TYPE_MINUS) {
118,526,284✔
2566
    return false;
13,619,553✔
2567
  }
2568
  if ((TSDB_DATA_TYPE_TIMESTAMP == lType && IS_INTEGER_TYPE(rType) && rType != TSDB_DATA_TYPE_UBIGINT) ||
104,906,731✔
2569
      (TSDB_DATA_TYPE_TIMESTAMP == rType && IS_INTEGER_TYPE(lType) && lType != TSDB_DATA_TYPE_UBIGINT) ||
97,167,649✔
2570
      (TSDB_DATA_TYPE_TIMESTAMP == lType && TSDB_DATA_TYPE_BOOL == rType) ||
90,333,783✔
2571
      (TSDB_DATA_TYPE_TIMESTAMP == rType && TSDB_DATA_TYPE_BOOL == lType)) {
738,120✔
2572
    return true;
15,218,400✔
2573
  }
2574
  return false;
89,688,331✔
2575
}
2576

2577
static int32_t vectorMathOpOneRowForDecimal(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t step,
55,152✔
2578
                                            int32_t i, EOperatorType op, SScalarParam *pOneRowParam) {
2579
  SScalarParam *pNotOneRowParam = pLeft == pOneRowParam ? pRight : pLeft;
55,152✔
2580
  Decimal      *output = (Decimal *)pOut->columnData->pData;
55,152✔
2581
  int32_t       code = 0;
55,152✔
2582
  SDataType     leftType = GET_COL_DATA_TYPE(pLeft->columnData->info),
55,152✔
2583
            rightType = GET_COL_DATA_TYPE(pRight->columnData->info),
55,152✔
2584
            outType = GET_COL_DATA_TYPE(pOut->columnData->info);
55,152✔
2585
  if (IS_HELPER_NULL(pOneRowParam->columnData, 0)) {
110,304✔
2586
    colDataSetNNULL(pOut->columnData, 0, pNotOneRowParam->numOfRows);
×
2587
  }
2588
  Decimal   oneRowData = {0};
55,152✔
2589
  SDataType oneRowType = outType;
55,152✔
2590
  oneRowType.precision = TSDB_DECIMAL_MAX_PRECISION;
55,152✔
2591
  if (pLeft == pOneRowParam) {
55,152✔
2592
    oneRowType.scale = leftType.scale;
27,576✔
2593
    code = convertToDecimal(colDataGetData(pLeft->columnData, 0), &leftType, &oneRowData, &oneRowType);
27,576✔
2594
  } else {
2595
    oneRowType.scale = rightType.scale;
27,576✔
2596
    code = convertToDecimal(colDataGetData(pRight->columnData, 0), &rightType, &oneRowData, &oneRowType);
27,576✔
2597
  }
2598
  if (code != 0) return code;
55,152✔
2599

2600
  for (; i < pNotOneRowParam->numOfRows && i >= 0 && TSDB_CODE_SUCCESS == code; i += step, output += 1) {
49,099,068✔
2601
    if (IS_HELPER_NULL(pNotOneRowParam->columnData, i)) {
98,087,832✔
2602
      colDataSetNULL(pOut->columnData, i);
652,632✔
2603
      continue;
652,632✔
2604
    }
2605
    if (pOneRowParam == pLeft) {
48,391,284✔
2606
      code =
2607
          decimalOp(op, &oneRowType, &rightType, &outType, &oneRowData, colDataGetData(pRight->columnData, i), output);
24,195,642✔
2608
    } else {
2609
      code = decimalOp(op, &leftType, &oneRowType, &outType, colDataGetData(pLeft->columnData, i), &oneRowData, output);
24,195,642✔
2610
    }
2611
  }
2612
  return code;
55,152✔
2613
}
2614

2615
static int32_t vectorMathUnaryOpForDecimal(SScalarParam *pCol, SScalarParam *pOut, int32_t step, int32_t i,
3,830✔
2616
                                           EOperatorType op) {
2617
  int32_t          code = 0;
3,830✔
2618
  SColumnInfoData *pOutputCol = pOut->columnData;
3,830✔
2619
  char            *pDec = pOutputCol->pData;
3,830✔
2620
  for (; i < pCol->numOfRows && i >= 0; i += step, pDec += tDataTypes[pOutputCol->info.type].bytes) {
3,833,830✔
2621
    if (IS_HELPER_NULL(pCol->columnData, i)) {
7,660,000✔
2622
      colDataSetNULL(pOutputCol, i);
49,790✔
2623
      continue;
49,790✔
2624
    }
2625
    SDataType colDt = GET_COL_DATA_TYPE(pCol->columnData->info), outDt = GET_COL_DATA_TYPE(pOutputCol->info);
3,780,210✔
2626

2627
    code = decimalOp(op, &colDt, NULL, &outDt, colDataGetData(pCol->columnData, i), NULL, pDec);
3,780,210✔
2628
  }
2629
  return code;
3,830✔
2630
}
2631

2632
static int32_t vectorMathBinaryOpForDecimal(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t step,
129,566✔
2633
                                            int32_t i, EOperatorType op) {
2634
  Decimal  *output = (Decimal *)pOut->columnData->pData;
129,566✔
2635
  int32_t   code = 0;
129,566✔
2636
  SDataType leftType = GET_COL_DATA_TYPE(pLeft->columnData->info),
129,566✔
2637
            rightType = GET_COL_DATA_TYPE(pRight->columnData->info),
129,566✔
2638
            outType = GET_COL_DATA_TYPE(pOut->columnData->info);
129,566✔
2639
  if (pLeft->numOfRows == pRight->numOfRows) {
129,566✔
2640
    for (; i < pRight->numOfRows && i >= 0 && TSDB_CODE_SUCCESS == code; i += step, output += 1) {
63,005,592✔
2641
      if (IS_NULL) {
188,175,260✔
2642
        colDataSetNULL(pOut->columnData, i);
1,125,366✔
2643
        continue;
1,125,366✔
2644
      }
2645
      code = decimalOp(op, &leftType, &rightType, &outType, colDataGetData(pLeft->columnData, i),
61,805,812✔
2646
                       colDataGetData(pRight->columnData, i), output);
61,805,812✔
2647
    }
2648
  } else if (pLeft->numOfRows == 1) {
55,152✔
2649
    code = vectorMathOpOneRowForDecimal(pLeft, pRight, pOut, step, i, op, pLeft);
27,576✔
2650
  } else if (pRight->numOfRows == 1) {
27,576✔
2651
    code = vectorMathOpOneRowForDecimal(pLeft, pRight, pOut, step, i, op, pRight);
27,576✔
2652
  }
2653
  return code;
129,566✔
2654
}
2655

2656
bool compareForType(__compar_fn_t fp, int32_t optr, SColumnInfoData *pColL, int32_t idxL, SColumnInfoData *pColR,
2,147,483,647✔
2657
                    int32_t idxR) {
2658
  void *pLeftData = colDataGetData(pColL, idxL), *pRightData = colDataGetData(pColR, idxR);
2,147,483,647✔
2659
  if (IS_DECIMAL_TYPE(pColL->info.type) || IS_DECIMAL_TYPE(pColR->info.type)) {
2,147,483,647✔
2660
    SDecimalCompareCtx ctxL = {.pData = pLeftData,
633,259,083✔
2661
                               .type = pColL->info.type,
637,985,761✔
2662
                               .typeMod = typeGetTypeModFromColInfo(&pColL->info)},
637,985,761✔
2663
                       ctxR = {.pData = pRightData,
637,985,761✔
2664
                               .type = pColR->info.type,
637,985,761✔
2665
                               .typeMod = typeGetTypeModFromColInfo(&pColR->info)};
637,985,761✔
2666
    return filterDoCompare(fp, optr, &ctxL, &ctxR);
637,985,761✔
2667
  } else {
2668
    return filterDoCompare(fp, optr, pLeftData, pRightData);
2,147,483,647✔
2669
  }
2670
}
2671

2672
bool compareForTypeWithColAndHash(__compar_fn_t fp, int32_t optr, SColumnInfoData *pColL, int32_t idxL,
1,011,102,311✔
2673
                                  const void *pHashData, int32_t hashType, STypeMod hashTypeMod) {
2674
  void *pLeftData = colDataGetData(pColL, idxL);
1,011,102,311✔
2675
  if (IS_DECIMAL_TYPE(pColL->info.type) || IS_DECIMAL_TYPE(hashType)) {
1,011,418,080✔
2676
    SDecimalCompareCtx ctxL = {.pData = pLeftData,
92,267,631✔
2677
                               .type = pColL->info.type,
92,261,264✔
2678
                               .typeMod = typeGetTypeModFromColInfo(&pColL->info)},
92,261,264✔
2679
                       ctxR = {.pData = (void *)pHashData, .type = hashType, .typeMod = hashTypeMod};
92,298,032✔
2680
    return filterDoCompare(fp, optr, &ctxL, &ctxR);
92,289,606✔
2681
  } else {
2682
    return filterDoCompare(fp, optr, pLeftData, (void *)pHashData);
919,152,037✔
2683
  }
2684
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc