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

taosdata / TDengine / #3903

24 Apr 2025 11:36AM UTC coverage: 55.307% (+0.09%) from 55.213%
#3903

push

travis-ci

happyguoxy
Sync branches at 2025-04-24 19:35

175024 of 316459 relevant lines covered (55.31%)

1151858.11 hits per line

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

64.4
/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 "filter.h"
19
#include "filterInt.h"
20
#include "query.h"
21
#include "querynodes.h"
22
#include "sclInt.h"
23
#include "sclvector.h"
24
#include "tcompare.h"
25
#include "tdatablock.h"
26
#include "tdataformat.h"
27
#include "tdef.h"
28
#include "ttime.h"
29
#include "ttypes.h"
30
#include "geosWrapper.h"
31
#include "decimal.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) {
613✔
44
  return !IS_DECIMAL_TYPE(leftType) && !IS_DECIMAL_TYPE(rightType) && IS_NUMERIC_TYPE(leftType) &&
613✔
45
         IS_NUMERIC_TYPE(rightType) && (optr >= OP_TYPE_GREATER_THAN && optr <= OP_TYPE_NOT_EQUAL);
1,226✔
46
}
47

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

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

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

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

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

124
  tmp[len] = 0;
30✔
125

126
  double value = taosStr2Double(tmp, NULL);
30✔
127

128
  *((double *)outData) = value;
30✔
129

130
_return:
30✔
131
  taosMemoryFreeClear(tmp);
30✔
132
  SCL_RET(code);
30✔
133
}
134

135
typedef int32_t (*_getBigintValue_fn_t)(void *src, int32_t index, int64_t *res);
136

137
int32_t getVectorBigintValue_TINYINT(void *src, int32_t index, int64_t *res) {
1✔
138
  *res = (int64_t) * ((int8_t *)src + index);
1✔
139
  SCL_RET(TSDB_CODE_SUCCESS);
1✔
140
}
141
int32_t getVectorBigintValue_UTINYINT(void *src, int32_t index, int64_t *res) {
1✔
142
  *res = (int64_t) * ((uint8_t *)src + index);
1✔
143
  SCL_RET(TSDB_CODE_SUCCESS);
1✔
144
}
145
int32_t getVectorBigintValue_SMALLINT(void *src, int32_t index, int64_t *res) {
16✔
146
  *res = (int64_t) * ((int16_t *)src + index);
16✔
147
  SCL_RET(TSDB_CODE_SUCCESS);
16✔
148
}
149
int32_t getVectorBigintValue_USMALLINT(void *src, int32_t index, int64_t *res) {
2✔
150
  *res = (int64_t) * ((uint16_t *)src + index);
2✔
151
  SCL_RET(TSDB_CODE_SUCCESS);
2✔
152
}
153
int32_t getVectorBigintValue_INT(void *src, int32_t index, int64_t *res) {
33✔
154
  *res = (int64_t) * ((int32_t *)src + index);
33✔
155
  SCL_RET(TSDB_CODE_SUCCESS);
33✔
156
}
157
int32_t getVectorBigintValue_UINT(void *src, int32_t index, int64_t *res) {
2✔
158
  *res = (int64_t) * ((uint32_t *)src + index);
2✔
159
  SCL_RET(TSDB_CODE_SUCCESS);
2✔
160
}
161
int32_t getVectorBigintValue_BIGINT(void *src, int32_t index, int64_t *res) {
123✔
162
  *res = (int64_t) * ((int64_t *)src + index);
123✔
163
  SCL_RET(TSDB_CODE_SUCCESS);
123✔
164
}
165
int32_t getVectorBigintValue_UBIGINT(void *src, int32_t index, int64_t *res) {
1✔
166
  *res = (int64_t) * ((uint64_t *)src + index);
1✔
167
  SCL_RET(TSDB_CODE_SUCCESS);
1✔
168
}
169
int32_t getVectorBigintValue_FLOAT(void *src, int32_t index, int64_t *res) {
5✔
170
  *res = (int64_t) * ((float *)src + index);
5✔
171
  SCL_RET(TSDB_CODE_SUCCESS);
5✔
172
}
173
int32_t getVectorBigintValue_DOUBLE(void *src, int32_t index, int64_t *res) {
12✔
174
  *res = (int64_t) * ((double *)src + index);
12✔
175
  SCL_RET(TSDB_CODE_SUCCESS);
12✔
176
}
177
int32_t getVectorBigintValue_BOOL(void *src, int32_t index, int64_t *res) {
×
178
  *res = (int64_t) * ((bool *)src + index);
×
179
  SCL_RET(TSDB_CODE_SUCCESS);
×
180
}
181

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

204
int32_t getVectorBigintValueFn(int32_t srcType, _getBigintValue_fn_t *p) {
318✔
205
   *p = NULL;
318✔
206
  if (srcType == TSDB_DATA_TYPE_TINYINT) {
318✔
207
    *p = getVectorBigintValue_TINYINT;
1✔
208
  } else if (srcType == TSDB_DATA_TYPE_UTINYINT) {
317✔
209
    *p = getVectorBigintValue_UTINYINT;
1✔
210
  } else if (srcType == TSDB_DATA_TYPE_SMALLINT) {
316✔
211
    *p = getVectorBigintValue_SMALLINT;
4✔
212
  } else if (srcType == TSDB_DATA_TYPE_USMALLINT) {
312✔
213
    *p = getVectorBigintValue_USMALLINT;
2✔
214
  } else if (srcType == TSDB_DATA_TYPE_INT) {
310✔
215
    *p = getVectorBigintValue_INT;
41✔
216
  } else if (srcType == TSDB_DATA_TYPE_UINT) {
269✔
217
    *p = getVectorBigintValue_UINT;
2✔
218
  } else if (srcType == TSDB_DATA_TYPE_BIGINT) {
267✔
219
    *p = getVectorBigintValue_BIGINT;
111✔
220
  } else if (srcType == TSDB_DATA_TYPE_UBIGINT) {
156✔
221
    *p = getVectorBigintValue_UBIGINT;
1✔
222
  } else if (srcType == TSDB_DATA_TYPE_FLOAT) {
155✔
223
    *p = getVectorBigintValue_FLOAT;
1✔
224
  } else if (srcType == TSDB_DATA_TYPE_DOUBLE) {
154✔
225
    *p = getVectorBigintValue_DOUBLE;
4✔
226
  } else if (srcType == TSDB_DATA_TYPE_TIMESTAMP) {
150✔
227
    *p = getVectorBigintValue_BIGINT;
110✔
228
  } else if (srcType == TSDB_DATA_TYPE_BOOL) {
40✔
229
    *p = getVectorBigintValue_BOOL;
×
230
  } else if (srcType == TSDB_DATA_TYPE_JSON) {
40✔
231
    *p = getVectorBigintValue_JSON;
40✔
232
  } else if (srcType == TSDB_DATA_TYPE_NULL) {
×
233
    *p = NULL;
×
234
  } else {
235
    sclError("getVectorBigintValueFn invalid srcType : %d", srcType);
×
236
    return TSDB_CODE_SCALAR_CONVERT_ERROR;
×
237
  }
238
  return TSDB_CODE_SUCCESS;
318✔
239
}
240

241
static FORCE_INLINE int32_t varToTimestamp(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
×
242
  int64_t value = 0;
×
243
  int32_t code = TSDB_CODE_SUCCESS;
×
244
  if (taosParseTime(buf, &value, strlen(buf), pOut->columnData->info.precision, pOut->tz) != TSDB_CODE_SUCCESS) {
×
245
    value = 0;
×
246
    code = TSDB_CODE_SCALAR_CONVERT_ERROR;
×
247
  }
248

249
  colDataSetInt64(pOut->columnData, rowIndex, &value);
×
250
  SCL_RET(code);
×
251
}
252

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

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

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

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

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

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

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

353
  double value = taosStr2Double(buf, NULL);
71✔
354
  colDataSetDouble(pOut->columnData, rowIndex, &value);
71✔
355
  SCL_RET(TSDB_CODE_SUCCESS);
71✔
356
}
357

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

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

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

405
static FORCE_INLINE int32_t varToNchar(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
10✔
406
  int32_t len = 0;
10✔
407
  int32_t inputLen = varDataLen(buf);
10✔
408
  int32_t outputMaxLen = (inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
10✔
409
  int32_t code = TSDB_CODE_SUCCESS;
10✔
410

411
  char   *t = taosMemoryCalloc(1, outputMaxLen);
10✔
412
  if (NULL == t) {
10✔
413
    SCL_ERR_RET(terrno);
×
414
  }
415
  int32_t ret =
10✔
416
      taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4 *)varDataVal(t), outputMaxLen - VARSTR_HEADER_SIZE, &len, pOut->charsetCxt);
10✔
417
  if (!ret) {
10✔
418
    sclError("failed to convert to NCHAR");
×
419
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
420
  }
421
  varDataSetLen(t, len);
10✔
422

423
  SCL_ERR_JRET(colDataSetVal(pOut->columnData, rowIndex, t, false));
10✔
424

425
_return:
10✔
426
  taosMemoryFree(t);
10✔
427
  SCL_RET(code);
10✔
428
}
429

430
static FORCE_INLINE int32_t ncharToVar(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
×
431
  int32_t code =TSDB_CODE_SUCCESS;
×
432
  int32_t inputLen = varDataLen(buf);
×
433

434
  char   *t = taosMemoryCalloc(1, inputLen + VARSTR_HEADER_SIZE);
×
435
  if (NULL == t) {
×
436
    SCL_ERR_RET(terrno);
×
437
  }
438
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t), pOut->charsetCxt);
×
439
  if (len < 0) {
×
440
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
441
  }
442
  varDataSetLen(t, len);
×
443

444
  SCL_ERR_JRET(colDataSetVal(pOut->columnData, rowIndex, t, false));
×
445

446
_return:
×
447
  taosMemoryFree(t);
×
448
  SCL_RET(code);
×
449
}
450

451
static FORCE_INLINE int32_t varToGeometry(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
1✔
452
#ifdef USE_GEOS
453
  //[ToDo] support to parse WKB as well as WKT
454
  int32_t        code = TSDB_CODE_SUCCESS;
1✔
455
  size_t         len = 0;
1✔
456
  unsigned char *t = NULL;
1✔
457
  char          *output = NULL;
1✔
458

459
  if ((code = initCtxGeomFromText()) != 0) {
1✔
460
    sclError("failed to init geometry ctx, %s", getGeosErrMsg(code));
×
461
    SCL_ERR_JRET(TSDB_CODE_APP_ERROR);
×
462
  }
463
  if ((code = doGeomFromText(buf, &t, &len)) != 0) {
1✔
464
    sclInfo("failed to convert text to geometry, %s", getGeosErrMsg(code));
×
465
    SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
466
  }
467

468
  output = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE);
1✔
469
  if (NULL == output) {
1✔
470
    SCL_ERR_JRET(terrno);
×
471
  }
472
  (void)memcpy(output + VARSTR_HEADER_SIZE, t, len);
1✔
473
  varDataSetLen(output, len);
1✔
474

475
  SCL_ERR_JRET(colDataSetVal(pOut->columnData, rowIndex, output, false));
1✔
476

477
  taosMemoryFree(output);
1✔
478
  geosFreeBuffer(t);
1✔
479

480
  SCL_RET(TSDB_CODE_SUCCESS);
1✔
481

482
_return:
×
483
  taosMemoryFree(output);
×
484
  geosFreeBuffer(t);
×
485
  t = NULL;
×
486
  VarDataLenT dummyHeader = 0;
×
487
  SCL_ERR_RET(colDataSetVal(pOut->columnData, rowIndex, (const char *)&dummyHeader, false));
×
488
  SCL_RET(code);
×
489
#else
490
  TAOS_RETURN(TSDB_CODE_OPS_NOT_SUPPORT);
491
#endif
492
}
493

494
// TODO opt performance, tmp is not needed.
495
int32_t vectorConvertFromVarData(SSclVectorConvCtx *pCtx, int32_t *overflow) {
34✔
496
  int32_t code = TSDB_CODE_SUCCESS;
34✔
497
  bool vton = false;
34✔
498

499
  _bufConverteFunc func = NULL;
34✔
500
  if (TSDB_DATA_TYPE_BOOL == pCtx->outType) {
34✔
501
    func = varToBool;
11✔
502
  } else if (IS_SIGNED_NUMERIC_TYPE(pCtx->outType)) {
23✔
503
    func = varToSigned;
3✔
504
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->outType)) {
20✔
505
    func = varToUnsigned;
×
506
  } else if (IS_FLOAT_TYPE(pCtx->outType)) {
20✔
507
    func = varToFloat;
8✔
508
  } else if ((pCtx->outType == TSDB_DATA_TYPE_VARCHAR || pCtx->outType == TSDB_DATA_TYPE_VARBINARY) &&
12✔
509
             pCtx->inType == TSDB_DATA_TYPE_NCHAR) {  // nchar -> binary
1✔
510
    func = ncharToVar;
×
511
    vton = true;
×
512
  } else if (pCtx->outType == TSDB_DATA_TYPE_NCHAR &&
12✔
513
      (pCtx->inType == TSDB_DATA_TYPE_VARCHAR || pCtx->inType == TSDB_DATA_TYPE_VARBINARY)) {  // binary -> nchar
10✔
514
    func = varToNchar;
10✔
515
    vton = true;
10✔
516
  } else if (TSDB_DATA_TYPE_TIMESTAMP == pCtx->outType) {
2✔
517
    func = varToTimestamp;
×
518
  } else if (TSDB_DATA_TYPE_GEOMETRY == pCtx->outType) {
2✔
519
    func = varToGeometry;
1✔
520
  } else if (TSDB_DATA_TYPE_VARBINARY == pCtx->outType) {
1✔
521
    func = varToVarbinary;
1✔
522
    vton = true;
1✔
523
  } else if (IS_DECIMAL_TYPE(pCtx->outType)) {
×
524
    func = varToDecimal;
×
525
  } else {
526
    sclError("invalid convert outType:%d, inType:%d", pCtx->outType, pCtx->inType);
×
527
    SCL_ERR_RET(TSDB_CODE_APP_ERROR);
×
528
  }
529

530
  pCtx->pOut->numOfRows = pCtx->pIn->numOfRows;
34✔
531
  char* tmp = NULL;
34✔
532

533
  for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
145✔
534
    if (IS_HELPER_NULL(pCtx->pIn->columnData, i)) {
222✔
535
      colDataSetNULL(pCtx->pOut->columnData, i);
8✔
536
      continue;
8✔
537
    }
538

539
    char   *data = colDataGetVarData(pCtx->pIn->columnData, i);
103✔
540
    int32_t convertType = pCtx->inType;
103✔
541
    if (pCtx->inType == TSDB_DATA_TYPE_JSON) {
103✔
542
      if (*data == TSDB_DATA_TYPE_NCHAR) {
8✔
543
        data += CHAR_BYTES;
2✔
544
        convertType = TSDB_DATA_TYPE_NCHAR;
2✔
545
      } else if (tTagIsJson(data) || *data == TSDB_DATA_TYPE_NULL) {
6✔
546
        SCL_ERR_JRET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
×
547
      } else {
548
        SCL_ERR_JRET(convertNumberToNumber(data + CHAR_BYTES, colDataGetNumData(pCtx->pOut->columnData, i), *data, pCtx->outType));
6✔
549
        continue;
6✔
550
      }
551
    }
552

553
    int32_t bufSize = pCtx->pIn->columnData->info.bytes;
97✔
554
    if (tmp == NULL) {
97✔
555
      tmp = taosMemoryMalloc(bufSize);
27✔
556
      if (tmp == NULL) {
27✔
557
        sclError("out of memory in vectorConvertFromVarData");
×
558
        SCL_ERR_JRET(terrno);
×
559
      }
560
    }
561

562
    if (vton) {
97✔
563
      (void)memcpy(tmp, data, varDataTLen(data));
11✔
564
    } else {
565
      if (TSDB_DATA_TYPE_VARCHAR == convertType || TSDB_DATA_TYPE_GEOMETRY == convertType) {
86✔
566
        (void)memcpy(tmp, varDataVal(data), varDataLen(data));
84✔
567
        tmp[varDataLen(data)] = 0;
84✔
568
      } else if (TSDB_DATA_TYPE_NCHAR == convertType) {
2✔
569
        // we need to convert it to native char string, and then perform the string to numeric data
570
        if (varDataLen(data) > bufSize) {
2✔
571
          sclError("castConvert convert buffer size too small");
×
572
          SCL_ERR_JRET(TSDB_CODE_APP_ERROR);
×
573
        }
574

575
        int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(data), varDataLen(data), tmp, pCtx->pIn->charsetCxt);
2✔
576
        if (len < 0) {
2✔
577
          sclError("castConvert taosUcs4ToMbs error 1");
×
578
          SCL_ERR_JRET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
579
        }
580

581
        tmp[len] = 0;
2✔
582
      }
583
    }
584

585
    SCL_ERR_JRET((*func)(tmp, pCtx->pOut, i, overflow));
97✔
586
  }
587

588
_return:
34✔
589
  if (tmp != NULL) {
34✔
590
    taosMemoryFreeClear(tmp);
27✔
591
  }
592
  SCL_RET(code);
34✔
593
}
594

595
int32_t getVectorDoubleValue_JSON(void *src, int32_t index, double *out) {
88✔
596
  char  *data = colDataGetVarData((SColumnInfoData *)src, index);
88✔
597
  *out = 0;
88✔
598
  if (*data == TSDB_DATA_TYPE_NULL) {
88✔
599
    SCL_RET(TSDB_CODE_SUCCESS);
×
600
  } else if (*data == TSDB_DATA_TYPE_NCHAR) {  // json inner type can not be BINARY
88✔
601
    SCL_ERR_RET(convertNcharToDouble(data + CHAR_BYTES, out));
22✔
602
  } else if (tTagIsJson(data)) {
66✔
603
    SCL_ERR_RET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
×
604
  } else {
605
    SCL_ERR_RET(convertNumberToNumber(data + CHAR_BYTES, out, *data, TSDB_DATA_TYPE_DOUBLE));
66✔
606
  }
607
  SCL_RET(TSDB_CODE_SUCCESS);
88✔
608
}
609

610
int32_t ncharTobinary(void *buf, void **out, void* charsetCxt) {  // todo need to remove , if tobinary is nchar
×
611
  int32_t inputLen = varDataTLen(buf);
×
612

613
  *out = taosMemoryCalloc(1, inputLen);
×
614
  if (NULL == *out) {
×
615
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed, since memory alloc failed.",
×
616
             DEFAULT_UNICODE_ENCODEC, charsetCxt != NULL ? ((SConvInfo *)(charsetCxt))->charset : tsCharset, (char *)varDataVal(buf));
617
    SCL_ERR_RET(terrno);
×
618
  }
619
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(*out), charsetCxt);
×
620
  if (len < 0) {
×
621
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed.", DEFAULT_UNICODE_ENCODEC,
×
622
             charsetCxt != NULL ? ((SConvInfo *)(charsetCxt))->charset : tsCharset,
623
             (char *)varDataVal(buf));
624
    taosMemoryFree(*out);
×
625
    SCL_ERR_RET(TSDB_CODE_SCALAR_CONVERT_ERROR);
×
626
  }
627
  varDataSetLen(*out, len);
×
628
  SCL_RET(TSDB_CODE_SUCCESS);
×
629
}
630

631
int32_t convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData,
10,439✔
632
                      char **pRightData, void *pLeftOut, void *pRightOut, bool *isNull, bool *freeLeft,
633
                      bool *freeRight, bool *result, void* charsetCxt) {
634
  *result = false;
10,439✔
635
  if (optr == OP_TYPE_JSON_CONTAINS) {
10,439✔
636
    *result = true;
×
637
    return TSDB_CODE_SUCCESS;
×
638
  }
639

640
  if (typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON) {
10,439✔
641
    *result = true;
10,314✔
642
    return TSDB_CODE_SUCCESS;
10,314✔
643
  }
644

645
  if (typeLeft == TSDB_DATA_TYPE_JSON) {
125✔
646
    if (tTagIsJson(*pLeftData)) {
80✔
647
      *result = false;
×
648
      return TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
×
649
    }
650
    typeLeft = **pLeftData;
80✔
651
    (*pLeftData)++;
80✔
652
  }
653
  if (typeRight == TSDB_DATA_TYPE_JSON) {
125✔
654
    if (tTagIsJson(*pRightData)) {
48✔
655
      *result = false;
×
656
      return TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
×
657
    }
658
    typeRight = **pRightData;
48✔
659
    (*pRightData)++;
48✔
660
  }
661

662
  if (optr == OP_TYPE_LIKE || optr == OP_TYPE_NOT_LIKE || optr == OP_TYPE_MATCH || optr == OP_TYPE_NMATCH) {
125✔
663
    if (typeLeft != TSDB_DATA_TYPE_NCHAR && typeLeft != TSDB_DATA_TYPE_BINARY &&
29✔
664
        typeLeft != TSDB_DATA_TYPE_GEOMETRY && typeLeft != TSDB_DATA_TYPE_VARBINARY) {
24✔
665
      *result = false;
24✔
666
      return TSDB_CODE_SUCCESS;
24✔
667
    }
668
  }
669

670
  // if types can not comparable
671
  if ((IS_NUMERIC_TYPE(typeLeft) && !IS_NUMERIC_TYPE(typeRight)) ||
101✔
672
      (IS_NUMERIC_TYPE(typeRight) && !IS_NUMERIC_TYPE(typeLeft)) ||
77✔
673
      (IS_VAR_DATA_TYPE(typeLeft) && !IS_VAR_DATA_TYPE(typeRight)) ||
53✔
674
      (IS_VAR_DATA_TYPE(typeRight) && !IS_VAR_DATA_TYPE(typeLeft)) ||
53✔
675
      ((typeLeft == TSDB_DATA_TYPE_BOOL) && (typeRight != TSDB_DATA_TYPE_BOOL)) ||
53✔
676
      ((typeRight == TSDB_DATA_TYPE_BOOL) && (typeLeft != TSDB_DATA_TYPE_BOOL))) {
×
677
    *result = false;
48✔
678
    return TSDB_CODE_SUCCESS;
48✔
679
  }
680

681
  if (typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL) {
53✔
682
    *isNull = true;
×
683
    *result = true;
×
684
    return TSDB_CODE_SUCCESS;
×
685
  }
686
  int8_t type = (int8_t)vectorGetConvertType(typeLeft, typeRight);
56✔
687

688
  if (type == 0) {
56✔
689
    *result = true;
8✔
690
    SCL_RET(filterGetCompFunc(fp, typeLeft, optr));
8✔
691
  }
692

693
  SCL_ERR_RET(filterGetCompFunc(fp, type, optr));
48✔
694

695
  if (IS_NUMERIC_TYPE(type)) {
48✔
696
    if (typeLeft == TSDB_DATA_TYPE_NCHAR ||
48✔
697
        typeLeft == TSDB_DATA_TYPE_VARCHAR ||
48✔
698
        typeLeft == TSDB_DATA_TYPE_GEOMETRY) {
699
      *result = false;
×
700
      return TSDB_CODE_SUCCESS;
×
701
    } else if (typeLeft != type) {
48✔
702
      SCL_ERR_RET(convertNumberToNumber(*pLeftData, pLeftOut, typeLeft, type));
24✔
703
      *pLeftData = pLeftOut;
24✔
704
    }
705

706
    if (typeRight == TSDB_DATA_TYPE_NCHAR ||
48✔
707
        typeRight == TSDB_DATA_TYPE_VARCHAR ||
48✔
708
        typeRight == TSDB_DATA_TYPE_GEOMETRY) {
709
      *result = false;
×
710
      return TSDB_CODE_SUCCESS;
×
711
    } else if (typeRight != type) {
48✔
712
      SCL_ERR_RET(convertNumberToNumber(*pRightData, pRightOut, typeRight, type));
24✔
713
      *pRightData = pRightOut;
24✔
714
    }
715
  } else if (type == TSDB_DATA_TYPE_BINARY ||
×
716
             type == TSDB_DATA_TYPE_GEOMETRY) {
717
    if (typeLeft == TSDB_DATA_TYPE_NCHAR) {
×
718
      char *tmpLeft = NULL;
×
719
      SCL_ERR_RET(ncharTobinary(*pLeftData, (void *)&tmpLeft, charsetCxt));
×
720
      *pLeftData = tmpLeft;
×
721
      *freeLeft = true;
×
722
    }
723
    if (typeRight == TSDB_DATA_TYPE_NCHAR) {
×
724
      char *tmpRight = NULL;
×
725
      SCL_ERR_RET(ncharTobinary(*pRightData, (void *)&tmpRight, charsetCxt));
×
726
      *pRightData = tmpRight;
×
727
      *freeRight = true;
×
728
    }
729
  } else {
730
    *result = false;
×
731
    return TSDB_CODE_SUCCESS;
×
732
  }
733

734
  *result = true;
48✔
735
  return TSDB_CODE_SUCCESS;
48✔
736
}
737

738
int32_t vectorConvertToVarData(SSclVectorConvCtx *pCtx) {
×
739
  SColumnInfoData *pInputCol = pCtx->pIn->columnData;
×
740
  SColumnInfoData *pOutputCol = pCtx->pOut->columnData;
×
741
  char             tmp[128] = {0};
×
742

743
  if (IS_SIGNED_NUMERIC_TYPE(pCtx->inType) || pCtx->inType == TSDB_DATA_TYPE_BOOL ||
×
744
      pCtx->inType == TSDB_DATA_TYPE_TIMESTAMP) {
×
745
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
×
746
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
747
        colDataSetNULL(pOutputCol, i);
×
748
        continue;
×
749
      }
750

751
      int64_t value = 0;
×
752
      GET_TYPED_DATA(value, int64_t, pCtx->inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
753
      int32_t len = tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%" PRId64, value);
×
754
      varDataLen(tmp) = len;
×
755
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
×
756
        SCL_ERR_RET(varToNchar(tmp, pCtx->pOut, i, NULL));
×
757
      } else {
758
        SCL_ERR_RET(colDataSetVal(pOutputCol, i, (char *)tmp, false));
×
759
      }
760
    }
761
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inType)) {
×
762
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
×
763
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
764
        colDataSetNULL(pOutputCol, i);
×
765
        continue;
×
766
      }
767

768
      uint64_t value = 0;
×
769
      GET_TYPED_DATA(value, uint64_t, pCtx->inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
770
      int32_t len = tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%" PRIu64, value);
×
771
      varDataLen(tmp) = len;
×
772
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
×
773
        SCL_ERR_RET(varToNchar(tmp, pCtx->pOut, i, NULL));
×
774
      } else {
775
        SCL_ERR_RET(colDataSetVal(pOutputCol, i, (char *)tmp, false));
×
776
      }
777
    }
778
  } else if (IS_FLOAT_TYPE(pCtx->inType)) {
×
779
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
×
780
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
781
        colDataSetNULL(pOutputCol, i);
×
782
        continue;
×
783
      }
784

785
      double value = 0;
×
786
      GET_TYPED_DATA(value, double, pCtx->inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
787
      int32_t len = tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%lf", value);
×
788
      varDataLen(tmp) = len;
×
789
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
×
790
        SCL_ERR_RET(varToNchar(tmp, pCtx->pOut, i, NULL));
×
791
      } else {
792
        SCL_ERR_RET(colDataSetVal(pOutputCol, i, (char *)tmp, false));
×
793
      }
794
    }
795
  } else {
796
    sclError("not supported input type:%d", pCtx->inType);
×
797
    return TSDB_CODE_APP_ERROR;
×
798
  }
799

800
  return TSDB_CODE_SUCCESS;
×
801
}
802

803
// TODO opt performance
804
int32_t vectorConvertSingleColImpl(const SScalarParam *pIn, SScalarParam *pOut, int32_t *overflow, int32_t startIndex,
2,514✔
805
                                   int32_t numOfRows) {
806
  SColumnInfoData *pInputCol = pIn->columnData;
2,514✔
807
  SColumnInfoData *pOutputCol = pOut->columnData;
2,514✔
808

809
  if (NULL == pInputCol) {
2,514✔
810
    sclError("input column is NULL, hashFilter %p", pIn->pHashFilter);
×
811
    return TSDB_CODE_APP_ERROR;
×
812
  }
813

814
  int32_t           rstart = (startIndex >= 0 && startIndex < pIn->numOfRows) ? startIndex : 0;
2,514✔
815
  int32_t           rend = numOfRows > 0 ? rstart + numOfRows - 1 : rstart + pIn->numOfRows - 1;
2,514✔
816
  SSclVectorConvCtx cCtx = {pIn, pOut, rstart, rend, pInputCol->info.type, pOutputCol->info.type};
2,514✔
817

818
  if (IS_VAR_DATA_TYPE(cCtx.inType)) {
2,514✔
819
    return vectorConvertFromVarData(&cCtx, overflow);
35✔
820
  }
821

822
  if (overflow && TSDB_DATA_TYPE_NULL != cCtx.inType) {
2,479✔
823
    if (1 != pIn->numOfRows) {
36✔
824
      sclError("invalid numOfRows %d", pIn->numOfRows);
×
825
      return TSDB_CODE_APP_ERROR;
×
826
    }
827

828
    pOut->numOfRows = 0;
36✔
829

830
    if (IS_SIGNED_NUMERIC_TYPE(cCtx.outType)) {
36✔
831
      int64_t minValue = tDataTypes[cCtx.outType].minValue;
12✔
832
      int64_t maxValue = tDataTypes[cCtx.outType].maxValue;
12✔
833

834
      double value = 0;
12✔
835
      GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, 0), typeGetTypeModFromColInfo(&pInputCol->info));
12✔
836

837
      if (value > maxValue) {
12✔
838
        *overflow = 1;
×
839
        return TSDB_CODE_SUCCESS;
×
840
      } else if (value < minValue) {
12✔
841
        *overflow = -1;
×
842
        return TSDB_CODE_SUCCESS;
×
843
      } else {
844
        *overflow = 0;
12✔
845
      }
846
    } else if (IS_UNSIGNED_NUMERIC_TYPE(cCtx.outType)) {
24✔
847
      uint64_t minValue = (uint64_t)tDataTypes[cCtx.outType].minValue;
×
848
      uint64_t maxValue = (uint64_t)tDataTypes[cCtx.outType].maxValue;
×
849

850
      double value = 0;
×
851
      GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, 0), typeGetTypeModFromColInfo(&pInputCol->info));
×
852

853
      if (value > maxValue) {
×
854
        *overflow = 1;
×
855
        return TSDB_CODE_SUCCESS;
×
856
      } else if (value < minValue) {
×
857
        *overflow = -1;
×
858
        return TSDB_CODE_SUCCESS;
×
859
      } else {
860
        *overflow = 0;
×
861
      }
862
    }
863
  }
864

865
  pOut->numOfRows = pIn->numOfRows;
2,479✔
866
  switch (cCtx.outType) {
2,479✔
867
    case TSDB_DATA_TYPE_BOOL: {
13✔
868
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
26✔
869
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
13✔
870
          colDataSetNULL(pOutputCol, i);
×
871
          continue;
×
872
        }
873

874
        bool value = 0;
13✔
875
        GET_TYPED_DATA(value, bool, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
13✔
876
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
13✔
877
      }
878
      break;
13✔
879
    }
880
    case TSDB_DATA_TYPE_TINYINT: {
×
881
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
882
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
883
          colDataSetNULL(pOutputCol, i);
×
884
          continue;
×
885
        }
886

887
        int8_t value = 0;
×
888
        GET_TYPED_DATA(value, int8_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
889
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
×
890
      }
891
      break;
×
892
    }
893
    case TSDB_DATA_TYPE_SMALLINT: {
×
894
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
895
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
896
          colDataSetNULL(pOutputCol, i);
×
897
          continue;
×
898
        }
899

900
        int16_t value = 0;
×
901
        GET_TYPED_DATA(value, int16_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
902
        colDataSetInt16(pOutputCol, i, (int16_t *)&value);
×
903
      }
904
      break;
×
905
    }
906
    case TSDB_DATA_TYPE_INT: {
113✔
907
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
226✔
908
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
113✔
909
          colDataSetNULL(pOutputCol, i);
×
910
          continue;
×
911
        }
912

913
        int32_t value = 0;
113✔
914
        GET_TYPED_DATA(value, int32_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
113✔
915
        colDataSetInt32(pOutputCol, i, (int32_t *)&value);
113✔
916
      }
917
      break;
113✔
918
    }
919
    case TSDB_DATA_TYPE_BIGINT:
2,289✔
920
    case TSDB_DATA_TYPE_TIMESTAMP: {
921
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
4,578✔
922
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
2,289✔
923
          colDataSetNULL(pOutputCol, i);
×
924
          continue;
×
925
        }
926

927
        int64_t value = 0;
2,289✔
928
        GET_TYPED_DATA(value, int64_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
2,289✔
929
        colDataSetInt64(pOutputCol, i, (int64_t *)&value);
2,289✔
930
      }
931
      break;
2,289✔
932
    }
933
    case TSDB_DATA_TYPE_UTINYINT: {
×
934
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
935
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
936
          colDataSetNULL(pOutputCol, i);
×
937
          continue;
×
938
        }
939

940
        uint8_t value = 0;
×
941
        GET_TYPED_DATA(value, uint8_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
942
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
×
943
      }
944
      break;
×
945
    }
946
    case TSDB_DATA_TYPE_USMALLINT: {
×
947
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
948
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
949
          colDataSetNULL(pOutputCol, i);
×
950
          continue;
×
951
        }
952

953
        uint16_t value = 0;
×
954
        GET_TYPED_DATA(value, uint16_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
955
        colDataSetInt16(pOutputCol, i, (int16_t *)&value);
×
956
      }
957
      break;
×
958
    }
959
    case TSDB_DATA_TYPE_UINT: {
×
960
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
961
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
962
          colDataSetNULL(pOutputCol, i);
×
963
          continue;
×
964
        }
965

966
        uint32_t value = 0;
×
967
        GET_TYPED_DATA(value, uint32_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
968
        colDataSetInt32(pOutputCol, i, (int32_t *)&value);
×
969
      }
970
      break;
×
971
    }
972
    case TSDB_DATA_TYPE_UBIGINT: {
×
973
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
974
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
975
          colDataSetNULL(pOutputCol, i);
×
976
          continue;
×
977
        }
978

979
        uint64_t value = 0;
×
980
        GET_TYPED_DATA(value, uint64_t, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
×
981
        colDataSetInt64(pOutputCol, i, (int64_t *)&value);
×
982
      }
983
      break;
×
984
    }
985
    case TSDB_DATA_TYPE_FLOAT: {
49✔
986
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
98✔
987
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
49✔
988
          colDataSetNULL(pOutputCol, i);
×
989
          continue;
×
990
        }
991

992
        float value = 0;
49✔
993
        GET_TYPED_DATA(value, float, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
49✔
994
        colDataSetFloat(pOutputCol, i, (float *)&value);
49✔
995
      }
996
      break;
49✔
997
    }
998
    case TSDB_DATA_TYPE_DOUBLE: {
15✔
999
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
34✔
1000
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
19✔
1001
          colDataSetNULL(pOutputCol, i);
×
1002
          continue;
×
1003
        }
1004

1005
        double value = 0;
19✔
1006
        GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, i), typeGetTypeModFromColInfo(&pInputCol->info));
19✔
1007
        colDataSetDouble(pOutputCol, i, (double *)&value);
19✔
1008
      }
1009
      break;
15✔
1010
    }
1011
    case TSDB_DATA_TYPE_BINARY:
×
1012
    case TSDB_DATA_TYPE_VARBINARY:
1013
    case TSDB_DATA_TYPE_NCHAR:
1014
    case TSDB_DATA_TYPE_GEOMETRY: {
1015
      return vectorConvertToVarData(&cCtx);
×
1016
    }
1017
    case TSDB_DATA_TYPE_DECIMAL: {
×
1018
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
×
1019
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
×
1020
          colDataSetNULL(pOutputCol, i);
×
1021
          continue;
×
1022
        }
1023

1024
        Decimal value = {0};
×
1025
        SDataType inputType = GET_COL_DATA_TYPE(pInputCol->info), outputType = GET_COL_DATA_TYPE(pOutputCol->info);
×
1026
        int32_t code = convertToDecimal(colDataGetData(pInputCol, i), &inputType, &value, &outputType);
×
1027
        if (TSDB_CODE_SUCCESS != code) return code;
×
1028
        code = colDataSetVal(pOutputCol, i, (const char*)&value, false);
×
1029
        if (TSDB_CODE_SUCCESS != code) return code;
×
1030
      }
1031
      break;
×
1032
    }
1033
    default:
×
1034
      sclError("invalid convert output type:%d", cCtx.outType);
×
1035
      return TSDB_CODE_APP_ERROR;
×
1036
  }
1037

1038
  return TSDB_CODE_SUCCESS;
2,479✔
1039
}
1040

1041
int8_t gConvertTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = {
1042
            /*NULL BOOL TINY SMAL INT  BIG  FLOA DOUB VARC TIME NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB MEDB GEOM DEC64*/
1043
    /*NULL*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
1044
    /*BOOL*/    0,   0,   2,   3,   4,   5,   6,   7,   5,   9,   5,   11, 12,  13,  14,   0,  -1,  17,   0,   0,  -1,  17,
1045
    /*TINY*/    0,   0,   0,   3,   4,   5,   6,   7,   5,   9,   5,   3,   4,   5,   7,   0,  -1,  17,   0,   0,  -1,  17,
1046
    /*SMAL*/    0,   0,   0,   0,   4,   5,   6,   7,   5,   9,   5,   3,   4,   5,   7,   0,  -1,  17,   0,   0,  -1,  17,
1047
    /*INT */    0,   0,   0,   0,   0,   5,   6,   7,   5,   9,   5,   4,   4,   5,   7,   0,  -1,  17,   0,   0,  -1,  17,
1048
    /*BIGI*/    0,   0,   0,   0,   0,   0,   6,   7,   5,   9,   5,   5,   5,   5,   7,   0,  -1,  17,   0,   0,  -1,  17,
1049
    /*FLOA*/    0,   0,   0,   0,   0,   0,   0,   7,   6,   6,   6,   6,   6,   6,   6,   0,  -1,   7,   0,   0,  -1,   7,
1050
    /*DOUB*/    0,   0,   0,   0,   0,   0,   0,   0,   7,   7,   7,   7,   7,   7,   7,   0,  -1,   7,   0,   0,  -1,   7,
1051
    /*VARC*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   8,   7,   7,   7,   7,   0,  16,   7,   0,   0,  20,   7,
1052
    /*TIME*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   9,   9,   9,   7,   0,  -1,  17,   0,   0,  -1,  17,
1053
    /*NCHA*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,   7,   7,   7,   0,  16,   7,   0,   0,  -1,   7,
1054
    /*UTIN*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  12,   13, 14,   0,  -1,  17,   0,   0,  -1,  17,
1055
    /*USMA*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   13, 14,   0,  -1,  17,   0,   0,  -1,  17,
1056
    /*UINT*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  14,   0,  -1,  17,   0,   0,  -1,  17,
1057
    /*UBIG*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,  17,   0,   0,  -1,  17,
1058
    /*JSON*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,  -1,   0,   0,  -1,  -1,
1059
    /*VARB*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,  -1,  -1,  -1,  -1,
1060
    /*DECI*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,   0,  -1,  -1,  -1,  17,
1061
    /*BLOB*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,   0,   0,   0,  -1,  -1,
1062
    /*MEDB*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,   0,   0,   0,  -1,  -1,
1063
    /*GEOM*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,   0,   0,   0,   0,  -1,
1064
    /*DEC64*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,   0,   0,   0,   0,   0,
1065
};
1066

1067
int8_t gDisplyTypes[TSDB_DATA_TYPE_MAX][TSDB_DATA_TYPE_MAX] = {
1068
              /*NULL BOOL TINY SMAL INT  BIGI FLOA DOUB VARC TIM NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB MEDB GEOM DEC64*/
1069
    /*NULL*/    0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  -1,  -1,  20,  21,
1070
    /*BOOL*/    0,   1,   2,   3,   4,   5,   6,   7,   8,   5,  10,  11,  12,  13,  14,  -1,  -1,  17,  -1,  -1,  -1,  17,
1071
    /*TINY*/    0,   0,   2,   3,   4,   5,   8,   8,   8,   5,  10,   3,   4,   5,   8,  -1,  -1,  17,  -1,  -1,  -1,  17,
1072
    /*SMAL*/    0,   0,   0,   3,   4,   5,   8,   8,   8,   5,  10,   3,   4,   5,   8,  -1,  -1,  17,  -1,  -1,  -1,  17,
1073
    /*INT */    0,   0,   0,   0,   4,   5,   8,   8,   8,   5,  10,   4,   4,   5,   8,  -1,  -1,  17,  -1,  -1,  -1,  17,
1074
    /*BIGI*/    0,   0,   0,   0,   0,   5,   8,   8,   8,   5,  10,   5,   5,   5,   8,  -1,  -1,  17,  -1,  -1,  -1,  17,
1075
    /*FLOA*/    0,   0,   0,   0,   0,   0,   6,   7,   8,   8,  10,   8,   8,   8,   8,  -1,  -1,   7,  -1,  -1,  -1,   7,
1076
    /*DOUB*/    0,   0,   0,   0,   0,   0,   0,   7,   8,   8,  10,   8,   8,   8,   8,  -1,  -1,   7,  -1,  -1,  -1,   7,
1077
    /*VARC*/    0,   0,   0,   0,   0,   0,   0,   0,   8,   8,  10,   8,   8,   8,   8,  -1,  16,   7,  -1,  -1,  -1,   7,
1078
    /*TIME*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   9,  10,   5,   5,   5,   8,  -1,  -1,  17,  -1,  -1,  -1,  17,
1079
    /*NCHA*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  10,  10,  10,  10,  10,  -1,  -1,   7,  -1,  -1,  -1,   7,
1080
    /*UTINY*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  11,  12,  13,  14,  -1,  -1,  17,  -1,  -1,  -1,  17,
1081
    /*USMA*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  12,  13,  14,  -1,  -1,  17,  -1,  -1,  -1,  -1,
1082
    /*UINT*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  13,  14,  -1,  -1,  17,  -1,  -1,  -1,  -1,
1083
    /*UBIG*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  14,  -1,  -1,  17,  -1,  -1,  -1,  -1,
1084
    /*JSON*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  15,  -1,  -1,  -1,  -1,  -1,  -1,
1085
    /*VARB*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  16,  -1,  -1,  -1,  -1,  -1,
1086
    /*DECI*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,  -1,  -1,  17,
1087
    /*BLOB*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,  -1,  -1,  -1,
1088
    /*MEDB*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  -1,  -1,  -1,
1089
    /*GEOM*/    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  20,  -1,
1090
    /*DEC64*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  20,   0,
1091
};
1092

1093
int32_t vectorGetConvertType(int32_t type1, int32_t type2) {
3,144✔
1094
  if (type1 == type2) {
3,144✔
1095
    return 0;
418✔
1096
  }
1097

1098
  if (type1 < type2) {
2,726✔
1099
    return gConvertTypes[type1][type2];
91✔
1100
  }
1101

1102
  return gConvertTypes[type2][type1];
2,635✔
1103
}
1104

1105
STypeMod getConvertTypeMod(int32_t type, const SColumnInfo* pCol1, const SColumnInfo* pCol2) {
8✔
1106
  if (IS_DECIMAL_TYPE(type)) {
8✔
1107
    if (IS_DECIMAL_TYPE(pCol1->type) && (!pCol2 || !IS_DECIMAL_TYPE(pCol2->type))) {
×
1108
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), pCol1->scale);
×
1109
    } else if (pCol2 && IS_DECIMAL_TYPE(pCol2->type) && !IS_DECIMAL_TYPE(pCol1->type)) {
×
1110
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), pCol2->scale);
×
1111
    } else if (IS_DECIMAL_TYPE(pCol1->type) && pCol2 && IS_DECIMAL_TYPE(pCol2->type)) {
×
1112
      return decimalCalcTypeMod(GET_DEICMAL_MAX_PRECISION(type), TMAX(pCol1->scale, pCol2->scale));
×
1113
    } else {
1114
      return 0;
×
1115
    }
1116
  }
1117
  return 0;
8✔
1118
}
1119

1120
int32_t vectorConvertSingleCol(SScalarParam *input, SScalarParam *output, int32_t type, STypeMod typeMod,
30✔
1121
                               int32_t startIndex, int32_t numOfRows) {
1122
  if (input->columnData == NULL && (input->pHashFilter != NULL || input->pHashFilterOthers != NULL)){
30✔
1123
    return TSDB_CODE_SUCCESS;
×
1124
  }
1125
  output->numOfRows = input->numOfRows;
30✔
1126

1127
  SDataType t = {.type = type};
30✔
1128
  t.bytes = (IS_VAR_DATA_TYPE(t.type) && input->columnData) ? input->columnData->info.bytes:tDataTypes[type].bytes;
30✔
1129
  t.precision = (IS_TIMESTAMP_TYPE(t.type) && input->columnData) ? input->columnData->info.precision : TSDB_TIME_PRECISION_MILLI;
30✔
1130
  if (IS_DECIMAL_TYPE(type)) {
30✔
1131
    extractTypeFromTypeMod(type, typeMod, &t.precision, &t.scale, NULL);
1✔
1132
    // We do not change scale here for decimal types.
1133
    if (IS_DECIMAL_TYPE(input->columnData->info.type)) t.scale = input->columnData->info.scale;
×
1134
  }
1135

1136
  int32_t code = sclCreateColumnInfoData(&t, input->numOfRows, output);
29✔
1137
  if (code != TSDB_CODE_SUCCESS) {
30✔
1138
    return code;
×
1139
  }
1140

1141
  code = vectorConvertSingleColImpl(input, output, NULL, startIndex, numOfRows);
30✔
1142
  if (code) {
30✔
1143
    return code;
×
1144
  }
1145

1146
  return TSDB_CODE_SUCCESS;
30✔
1147
}
1148

1149
int32_t vectorConvertCols(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pLeftOut, SScalarParam *pRightOut,
355✔
1150
                          int32_t startIndex, int32_t numOfRows) {
1151
  int32_t leftType = GET_PARAM_TYPE(pLeft);
355✔
1152
  int32_t rightType = GET_PARAM_TYPE(pRight);
355✔
1153
  if (leftType == rightType) {
355✔
1154
    return TSDB_CODE_SUCCESS;
176✔
1155
  }
1156

1157
  int8_t   type = 0;
179✔
1158
  int32_t  code = 0;
179✔
1159
  STypeMod outTypeMod = 0;
179✔
1160

1161
  SScalarParam *param1 = pLeft, *paramOut1 = pLeftOut;
179✔
1162
  SScalarParam *param2 = pRight, *paramOut2 = pRightOut;
179✔
1163

1164
  // always convert least data
1165
  if (IS_VAR_DATA_TYPE(leftType) && IS_VAR_DATA_TYPE(rightType) && (pLeft->numOfRows != pRight->numOfRows) &&
179✔
1166
      leftType != TSDB_DATA_TYPE_JSON && rightType != TSDB_DATA_TYPE_JSON) {
9✔
1167
    if (pLeft->numOfRows > pRight->numOfRows) {
10✔
1168
      type = leftType;
10✔
1169
    } else {
1170
      type = rightType;
×
1171
    }
1172
  } else {
1173
    type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
169✔
1174
    if (0 == type) {
168✔
1175
      return TSDB_CODE_SUCCESS;
160✔
1176
    }
1177
    if (-1 == type) {
8✔
1178
      sclError("invalid convert type1:%d, type2:%d", GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
×
1179
      terrno = TSDB_CODE_SCALAR_CONVERT_ERROR;
×
1180
      return TSDB_CODE_SCALAR_CONVERT_ERROR;
×
1181
    }
1182
    outTypeMod = getConvertTypeMod(type, &param1->columnData->info, param2->columnData ? &param2->columnData->info : NULL);
8✔
1183
  }
1184

1185
  if (type != GET_PARAM_TYPE(param1)) {
18✔
1186
    SCL_ERR_RET(vectorConvertSingleCol(param1, paramOut1, type, outTypeMod, startIndex, numOfRows));
4✔
1187
  }
1188

1189
  if (type != GET_PARAM_TYPE(param2)) {
17✔
1190
    SCL_ERR_RET(vectorConvertSingleCol(param2, paramOut2, type, outTypeMod, startIndex, numOfRows));
17✔
1191
  }
1192

1193
  return TSDB_CODE_SUCCESS;
18✔
1194
}
1195

1196
enum {
1197
  VECTOR_DO_CONVERT = 0x1,
1198
  VECTOR_UN_CONVERT = 0x2,
1199
};
1200

1201
// TODO not correct for descending order scan
1202
static int32_t vectorMathAddHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
16✔
1203
                                int32_t numOfRows, int32_t step, int32_t i) {
1204
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1205
  _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1206
  SCL_ERR_RET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
32✔
1207
  SCL_ERR_RET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
32✔
1208

1209
  double *output = (double *)pOutputCol->pData;
16✔
1210

1211
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
16✔
1212
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1213
  } else {
1214
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
164✔
1215
      if (IS_HELPER_NULL(pLeftCol, i)) {
312✔
1216
        colDataSetNULL(pOutputCol, i);
9✔
1217
        continue;  // TODO set null or ignore
9✔
1218
      }
1219
      double leftRes = 0;
147✔
1220
      double rightRes = 0;
147✔
1221
      SCL_ERR_RET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
147✔
1222
      SCL_ERR_RET(getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes));
139✔
1223
      *output =  leftRes + rightRes;
139✔
1224
    }
1225
  }
1226
  SCL_RET(TSDB_CODE_SUCCESS);
8✔
1227
}
1228

1229
static int32_t vectorMathTsAddHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
22✔
1230
                                  int32_t numOfRows, int32_t step, int32_t i, timezone_t tz) {
1231
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
1232
  _getBigintValue_fn_t getVectorBigintValueFnRight;
1233
  SCL_ERR_RET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
22✔
1234
  SCL_ERR_RET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
22✔
1235
  int64_t *output = (int64_t *)pOutputCol->pData;
22✔
1236

1237
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
22✔
1238
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1239
  } else {
1240
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
50✔
1241
      if (IS_HELPER_NULL(pLeftCol, i)) {
56✔
1242
        colDataSetNULL(pOutputCol, i);
×
1243
        continue;  // TODO set null or ignore
×
1244
      }
1245
      int64_t leftRes = 0;
28✔
1246
      int64_t rightRes = 0;
28✔
1247
      SCL_ERR_RET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
28✔
1248
      SCL_ERR_RET(getVectorBigintValueFnRight(pRightCol->pData, 0, &rightRes));
28✔
1249
      *output =
28✔
1250
          taosTimeAdd(leftRes, rightRes, pRightCol->info.scale, pRightCol->info.precision, tz);
28✔
1251
    }
1252
  }
1253
  SCL_RET(TSDB_CODE_SUCCESS);
22✔
1254
}
1255

1256
static int32_t vectorConvertVarToDouble(SScalarParam *pInput, int32_t *converted, SColumnInfoData **pOutputCol) {
961✔
1257
  SScalarParam     output = {0};
961✔
1258
  SColumnInfoData *pCol = pInput->columnData;
961✔
1259
  int32_t          code = TSDB_CODE_SUCCESS;
961✔
1260
  *pOutputCol = NULL;
961✔
1261
  bool isVarChar = IS_VAR_DATA_TYPE(pCol->info.type) && pCol->info.type != TSDB_DATA_TYPE_JSON && pCol->info.type != TSDB_DATA_TYPE_VARBINARY;
961✔
1262
  if (isVarChar || IS_DECIMAL_TYPE(pCol->info.type)) {
961✔
1263
    SCL_ERR_RET(vectorConvertSingleCol(pInput, &output, TSDB_DATA_TYPE_DOUBLE, 0, -1, -1));
9✔
1264
    *converted = VECTOR_DO_CONVERT;
9✔
1265
    *pOutputCol = output.columnData;
9✔
1266
    SCL_RET(code);
9✔
1267
  }
1268

1269
  *converted = VECTOR_UN_CONVERT;
952✔
1270
  *pOutputCol = pInput->columnData;
952✔
1271
  SCL_RET(TSDB_CODE_SUCCESS);
952✔
1272
}
1273

1274
static void doReleaseVec(SColumnInfoData *pCol, int32_t type) {
1,004✔
1275
  if (type == VECTOR_DO_CONVERT) {
1,004✔
1276
    colDataDestroy(pCol);
9✔
1277
    taosMemoryFree(pCol);
9✔
1278
  }
1279
}
1,004✔
1280

1281
int32_t vectorMathAdd(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
163✔
1282
  SColumnInfoData *pOutputCol = pOut->columnData;
163✔
1283

1284
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
163✔
1285
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
163✔
1286

1287
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
163✔
1288

1289
  int32_t          code = TSDB_CODE_SUCCESS;
163✔
1290
  int32_t          leftConvert = 0, rightConvert = 0;
163✔
1291
  SColumnInfoData *pLeftCol = pLeft->columnData;
163✔
1292
  SColumnInfoData *pRightCol = pRight->columnData;
163✔
1293
  if(pOutputCol->info.type == TSDB_DATA_TYPE_TIMESTAMP) {  // timestamp plus duration
163✔
1294
    int64_t             *output = (int64_t *)pOutputCol->pData;
22✔
1295
    _getBigintValue_fn_t getVectorBigintValueFnLeft;
1296
    _getBigintValue_fn_t getVectorBigintValueFnRight;
1297
    SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
22✔
1298
    SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
22✔
1299

1300
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
22✔
1301
      if (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP) {
20✔
1302
        SCL_ERR_JRET(vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pRight->numOfRows, step, i, pLeft->tz));
20✔
1303
      } else {
1304
        SCL_ERR_JRET(vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i, pLeft->tz));
×
1305
      }
1306
    } else if (pLeft->numOfRows == 1) {
2✔
1307
      SCL_ERR_JRET(vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i, pLeft->tz));
×
1308
    } else if (pRight->numOfRows == 1) {
2✔
1309
      SCL_ERR_JRET(vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i, pLeft->tz));
2✔
1310
    } else if (pLeft->numOfRows == pRight->numOfRows) {
×
1311
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
×
1312
        if (IS_NULL) {
×
1313
          colDataSetNULL(pOutputCol, i);
×
1314
          continue;  // TODO set null or ignore
×
1315
        }
1316
        int64_t leftRes = 0;
×
1317
        int64_t rightRes = 0;
×
1318
        SCL_ERR_JRET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
×
1319
        SCL_ERR_JRET(getVectorBigintValueFnRight(pRightCol->pData, i, &rightRes));
×
1320
        *output = leftRes + rightRes;
×
1321
      }
1322
    }
1323
  } else if (IS_DECIMAL_TYPE(pOutputCol->info.type)) {
141✔
1324
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_ADD));
×
1325
  } else {
1326
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
141✔
1327
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
140✔
1328
    double              *output = (double *)pOutputCol->pData;
141✔
1329
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1330
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1331
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
282✔
1332
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
282✔
1333
    if (pLeft->numOfRows == pRight->numOfRows) {
141✔
1334
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
200,201✔
1335
        if (IS_NULL) {
600,211✔
1336
          colDataSetNULL(pOutputCol, i);
26✔
1337
          continue;  // TODO set null or ignore
26✔
1338
        }
1339
        double leftRes = 0;
200,050✔
1340
        double rightRes = 0;
200,050✔
1341
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
200,050✔
1342
        SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes));
200,050✔
1343
        *output = leftRes + rightRes;
200,050✔
1344
      }
1345
    } else if (pLeft->numOfRows == 1) {
16✔
1346
      SCL_ERR_JRET(vectorMathAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i));
1✔
1347
    } else if (pRight->numOfRows == 1) {
15✔
1348
      SCL_ERR_JRET(vectorMathAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i));
15✔
1349
    }
1350
  }
1351

1352
_return:
163✔
1353
  doReleaseVec(pLeftCol, leftConvert);
163✔
1354
  doReleaseVec(pRightCol, rightConvert);
162✔
1355
  SCL_RET(code);
163✔
1356
}
1357

1358
// TODO not correct for descending order scan
1359
static int32_t vectorMathSubHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
111✔
1360
                                int32_t numOfRows, int32_t step, int32_t factor, int32_t i) {
1361
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1362
  _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1363
  SCL_ERR_RET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
222✔
1364
  SCL_ERR_RET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
222✔
1365

1366
  double *output = (double *)pOutputCol->pData;
111✔
1367

1368
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
111✔
1369
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1370
  } else {
1371
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
1,480✔
1372
      if (IS_HELPER_NULL(pLeftCol, i)) {
2,738✔
1373
        colDataSetNULL(pOutputCol, i);
169✔
1374
        continue;  // TODO set null or ignore
169✔
1375
      }
1376
      double leftRes = 0;
1,200✔
1377
      double rightRes = 0;
1,200✔
1378
      SCL_ERR_RET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
1,200✔
1379
      SCL_ERR_RET(getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes));
1,200✔
1380
      *output = (leftRes - rightRes) * factor;
1,200✔
1381
    }
1382
  }
1383
  SCL_RET(TSDB_CODE_SUCCESS);
111✔
1384
}
1385

1386
static int32_t vectorMathTsSubHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
33✔
1387
                                  int32_t numOfRows, int32_t step, int32_t factor, int32_t i, timezone_t tz) {
1388
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
1389
  _getBigintValue_fn_t getVectorBigintValueFnRight;
1390
  SCL_ERR_RET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
33✔
1391
  SCL_ERR_RET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
33✔
1392

1393
  int64_t *output = (int64_t *)pOutputCol->pData;
33✔
1394

1395
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
33✔
1396
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1397
  } else {
1398
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
66✔
1399
      if (IS_HELPER_NULL(pLeftCol, i)) {
66✔
1400
        colDataSetNULL(pOutputCol, i);
×
1401
        continue;  // TODO set null or ignore
×
1402
      }
1403
      int64_t leftRes = 0;
33✔
1404
      int64_t rightRes = 0;
33✔
1405
      SCL_ERR_RET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
33✔
1406
      SCL_ERR_RET(getVectorBigintValueFnRight(pRightCol->pData, 0, &rightRes));
33✔
1407
      *output =
33✔
1408
          taosTimeAdd(leftRes, -rightRes, pRightCol->info.scale, pRightCol->info.precision, tz) * factor;
33✔
1409
    }
1410
  }
1411
  SCL_RET(TSDB_CODE_SUCCESS);
33✔
1412
}
1413

1414
int32_t vectorMathSub(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
183✔
1415
  SColumnInfoData *pOutputCol = pOut->columnData;
183✔
1416

1417
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
183✔
1418

1419
  int32_t code = TSDB_CODE_SUCCESS;
183✔
1420
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
183✔
1421
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
183✔
1422

1423
  int32_t          leftConvert = 0, rightConvert = 0;
183✔
1424
  SColumnInfoData *pLeftCol = NULL;
183✔
1425
  SColumnInfoData *pRightCol = NULL;
183✔
1426

1427
 if (pOutputCol->info.type == TSDB_DATA_TYPE_TIMESTAMP) { // timestamp minus duration
183✔
1428
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
33✔
1429
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
33✔
1430
    int64_t             *output = (int64_t *)pOutputCol->pData;
33✔
1431
    _getBigintValue_fn_t getVectorBigintValueFnLeft;
1432
    _getBigintValue_fn_t getVectorBigintValueFnRight;
1433
    SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
33✔
1434
    SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
33✔
1435

1436
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
33✔
1437
      SCL_ERR_JRET(vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i, pLeft->tz));
33✔
1438
    } else if (pLeft->numOfRows == 1) {
×
1439
      SCL_ERR_JRET(vectorMathTsSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i, pLeft->tz));
×
1440
    } else if (pRight->numOfRows == 1) {
×
1441
      SCL_ERR_JRET(vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i, pLeft->tz));
×
1442
    } else if (pLeft->numOfRows == pRight->numOfRows) {
×
1443
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
×
1444
        if (IS_NULL) {
×
1445
          colDataSetNULL(pOutputCol, i);
×
1446
          continue;  // TODO set null or ignore
×
1447
        }
1448
        int64_t leftRes = 0;
×
1449
        int64_t rightRes = 0;
×
1450
        SCL_ERR_JRET(getVectorBigintValueFnLeft(pLeftCol->pData, i, &leftRes));
×
1451
        SCL_ERR_JRET(getVectorBigintValueFnRight(pRightCol->pData, i, &rightRes));
×
1452
        *output = leftRes - rightRes;
×
1453
      }
1454
    }
1455
  } else if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
150✔
1456
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_SUB));
×
1457
  } else {
1458
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
150✔
1459
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
150✔
1460
    double              *output = (double *)pOutputCol->pData;
150✔
1461
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1462
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1463
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
300✔
1464
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
300✔
1465

1466
    if (pLeft->numOfRows == pRight->numOfRows) {
150✔
1467
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
158✔
1468
        if (IS_NULL) {
346✔
1469
          colDataSetNULL(pOutputCol, i);
18✔
1470
          continue;  // TODO set null or ignore
18✔
1471
        }
1472
        double leftRes = 0;
101✔
1473
        double rightRes = 0;
101✔
1474
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
101✔
1475
        SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes));
101✔
1476
        *output = leftRes - rightRes;
101✔
1477
      }
1478
    } else if (pLeft->numOfRows == 1) {
111✔
1479
      SCL_ERR_JRET(vectorMathSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i));
104✔
1480
    } else if (pRight->numOfRows == 1) {
7✔
1481
      SCL_ERR_JRET(vectorMathSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i));
7✔
1482
    }
1483
  }
1484

1485
_return:
183✔
1486
  doReleaseVec(pLeftCol, leftConvert);
183✔
1487
  doReleaseVec(pRightCol, rightConvert);
183✔
1488
  SCL_RET(code);
183✔
1489
}
1490

1491
// TODO not correct for descending order scan
1492
static int32_t vectorMathMultiplyHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
7✔
1493
                                     int32_t numOfRows, int32_t step, int32_t i) {
1494
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1495
  _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1496
  SCL_ERR_RET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
14✔
1497
  SCL_ERR_RET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
14✔
1498

1499
  double *output = (double *)pOutputCol->pData;
7✔
1500

1501
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
7✔
1502
    colDataSetNNULL(pOutputCol, 0, numOfRows);
×
1503
  } else {
1504
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
96✔
1505
      if (IS_HELPER_NULL(pLeftCol, i)) {
178✔
1506
        colDataSetNULL(pOutputCol, i);
6✔
1507
        continue;  // TODO set null or ignore
6✔
1508
      }
1509
      double leftRes = 0;
83✔
1510
      double rightRes = 0;
83✔
1511
      SCL_ERR_RET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
83✔
1512
      SCL_ERR_RET(getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes));
83✔
1513
      *output = leftRes * rightRes;
83✔
1514
    }
1515
  }
1516
  SCL_RET(TSDB_CODE_SUCCESS);
7✔
1517
}
1518

1519
int32_t vectorMathMultiply(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
45✔
1520
  SColumnInfoData *pOutputCol = pOut->columnData;
45✔
1521
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
45✔
1522

1523
  int32_t code = TSDB_CODE_SUCCESS;
45✔
1524
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
45✔
1525
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
45✔
1526

1527
  int32_t          leftConvert = 0, rightConvert = 0;
45✔
1528
  SColumnInfoData *pLeftCol = NULL;
45✔
1529
  SColumnInfoData *pRightCol = NULL;
45✔
1530
  if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
45✔
1531
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_MULTI));
×
1532
  } else {
1533
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
45✔
1534
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
45✔
1535

1536
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1537
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1538
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
90✔
1539
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
90✔
1540

1541
    double *output = (double *)pOutputCol->pData;
45✔
1542
    if (pLeft->numOfRows == pRight->numOfRows) {
45✔
1543
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
96✔
1544
        if (IS_NULL) {
169✔
1545
          colDataSetNULL(pOutputCol, i);
8✔
1546
          continue;  // TODO set null or ignore
8✔
1547
        }
1548
        double leftRes = 0;
50✔
1549
        double rightRes = 0;
50✔
1550
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
50✔
1551
        SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes));
50✔
1552
        *output = leftRes * rightRes;
50✔
1553
      }
1554
    } else if (pLeft->numOfRows == 1) {
7✔
1555
      SCL_ERR_JRET(vectorMathMultiplyHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i));
×
1556
    } else if (pRight->numOfRows == 1) {
7✔
1557
      SCL_ERR_JRET(vectorMathMultiplyHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i));
7✔
1558
    }
1559
  }
1560

1561
_return:
45✔
1562
  doReleaseVec(pLeftCol, leftConvert);
45✔
1563
  doReleaseVec(pRightCol, rightConvert);
45✔
1564
  SCL_RET(code);
45✔
1565
}
1566

1567
int32_t vectorMathDivide(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
33✔
1568
  SColumnInfoData *pOutputCol = pOut->columnData;
33✔
1569
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
33✔
1570

1571
  int32_t code = TSDB_CODE_SUCCESS;
33✔
1572
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
33✔
1573
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
33✔
1574

1575
  int32_t          leftConvert = 0, rightConvert = 0;
33✔
1576
  SColumnInfoData *pLeftCol = NULL;
33✔
1577
  SColumnInfoData *pRightCol = NULL;
33✔
1578
  if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
33✔
1579
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_DIV));
×
1580
  } else {
1581
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
33✔
1582
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
33✔
1583

1584
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1585
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1586
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
66✔
1587
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
66✔
1588

1589
    double *output = (double *)pOutputCol->pData;
33✔
1590
    if (pLeft->numOfRows == pRight->numOfRows) {
33✔
1591
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
57✔
1592
        if (IS_NULL) {  // divide by 0 check
91✔
1593
          colDataSetNULL(pOutputCol, i);
6✔
1594
          continue;
9✔
1595
        }
1596
        double rightRes = 0;
25✔
1597
        SCL_ERR_JRET((getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes)));
25✔
1598
        if (rightRes == 0) {
25✔
1599
          colDataSetNULL(pOutputCol, i);
3✔
1600
          continue;
3✔
1601
        }
1602
        double leftRes = 0;
22✔
1603
        SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
22✔
1604
        *output = leftRes / rightRes;
22✔
1605
      }
1606
    } else if (pLeft->numOfRows == 1) {
7✔
1607
      if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
×
1608
        colDataSetNNULL(pOutputCol, 0, pRight->numOfRows);
×
1609
      } else {
1610
        for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
×
1611
          if (IS_HELPER_NULL(pRightCol, i)) {  // divide by 0 check
×
1612
            colDataSetNULL(pOutputCol, i);
×
1613
            continue;
×
1614
          }
1615
          double rightRes = 0;
×
1616
          SCL_ERR_JRET((getVectorDoubleValueFnRight(RIGHT_COL, i, &rightRes)));
×
1617
          if (rightRes == 0) {
×
1618
            colDataSetNULL(pOutputCol, i);
×
1619
            continue;
×
1620
          }
1621
          double leftRes = 0;
×
1622
          SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, 0, &leftRes));
×
1623
          *output = leftRes / rightRes;
×
1624
        }
1625
      }
1626
    } else if (pRight->numOfRows == 1) {
7✔
1627
      if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value (divde by 0 check)
14✔
1628
        colDataSetNNULL(pOutputCol, 0, pLeft->numOfRows);
×
1629
      } else {
1630
        double rightRes = 0;
7✔
1631
        SCL_ERR_JRET((getVectorDoubleValueFnRight(RIGHT_COL, 0, &rightRes)));
7✔
1632
        if (rightRes == 0) {
7✔
1633
          colDataSetNNULL(pOutputCol, 0, pLeft->numOfRows);
×
1634
        } else {
1635
          for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
96✔
1636
            if (IS_HELPER_NULL(pLeftCol, i)) {
178✔
1637
              colDataSetNULL(pOutputCol, i);
11✔
1638
              continue;
11✔
1639
            }
1640
            double leftRes = 0;
78✔
1641
            SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &leftRes));
78✔
1642
            *output = leftRes / rightRes;
78✔
1643
          }
1644
        }
1645
      }
1646
    }
1647
  }
1648

1649
_return:
33✔
1650
  doReleaseVec(pLeftCol, leftConvert);
33✔
1651
  doReleaseVec(pRightCol, rightConvert);
33✔
1652
  SCL_RET(code);
33✔
1653
}
1654

1655
int32_t vectorMathRemainder(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
20✔
1656
  SColumnInfoData *pOutputCol = pOut->columnData;
20✔
1657
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
20✔
1658

1659
  int32_t code = TSDB_CODE_SUCCESS;
20✔
1660
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
20✔
1661
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
20✔
1662

1663
  int32_t          leftConvert = 0, rightConvert = 0;
20✔
1664
  SColumnInfoData *pLeftCol = NULL;
20✔
1665
  SColumnInfoData *pRightCol = NULL;
20✔
1666
  if (pOutputCol->info.type == TSDB_DATA_TYPE_DECIMAL) {
20✔
1667
    SCL_ERR_JRET(vectorMathBinaryOpForDecimal(pLeft, pRight, pOut, step, i, OP_TYPE_REM));
×
1668
  } else {
1669
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
20✔
1670
    SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
20✔
1671

1672
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1673
    _getDoubleValue_fn_t getVectorDoubleValueFnRight;
1674
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
40✔
1675
    SCL_ERR_JRET(getVectorDoubleValueFn(pRightCol->info.type, &getVectorDoubleValueFnRight));
40✔
1676

1677
    double *output = (double *)pOutputCol->pData;
20✔
1678

1679
    int32_t numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
20✔
1680
    for (; i < numOfRows && i >= 0; i += step, output += 1) {
40✔
1681
      int32_t leftidx = pLeft->numOfRows == 1 ? 0 : i;
20✔
1682
      int32_t rightidx = pRight->numOfRows == 1 ? 0 : i;
20✔
1683
      if (IS_HELPER_NULL(pLeftCol, leftidx) || IS_HELPER_NULL(pRightCol, rightidx)) {
58✔
1684
        colDataSetNULL(pOutputCol, i);
4✔
1685
        continue;
6✔
1686
      }
1687

1688
      double lx = 0;
16✔
1689
      double rx = 0;
16✔
1690
      SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, leftidx, &lx));
16✔
1691
      SCL_ERR_JRET(getVectorDoubleValueFnRight(RIGHT_COL, rightidx, &rx));
16✔
1692
      if (isnan(lx) || isinf(lx) || isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
16✔
1693
        colDataSetNULL(pOutputCol, i);
2✔
1694
        continue;
2✔
1695
      }
1696

1697
      *output = lx - ((int64_t)(lx / rx)) * rx;
14✔
1698
    }
1699
  }
1700
_return:
20✔
1701
  doReleaseVec(pLeftCol, leftConvert);
20✔
1702
  doReleaseVec(pRightCol, rightConvert);
20✔
1703
  SCL_RET(code);
20✔
1704
}
1705

1706
int32_t vectorMathMinus(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
20✔
1707
  SColumnInfoData *pOutputCol = pOut->columnData;
20✔
1708

1709
  pOut->numOfRows = pLeft->numOfRows;
20✔
1710

1711
  int32_t code = TSDB_CODE_SUCCESS;
20✔
1712
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : (pLeft->numOfRows - 1);
20✔
1713
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
20✔
1714

1715
  int32_t          leftConvert = 0;
20✔
1716
  SColumnInfoData *pLeftCol = NULL;
20✔
1717
  if (IS_DECIMAL_TYPE(pOutputCol->info.type)) {
20✔
1718
    SCL_ERR_JRET(vectorMathUnaryOpForDecimal(pLeft, pOut, step, i, OP_TYPE_MINUS));
×
1719
  } else {
1720
    SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
20✔
1721

1722
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft;
1723
    SCL_ERR_JRET(getVectorDoubleValueFn(pLeftCol->info.type, &getVectorDoubleValueFnLeft));
40✔
1724

1725
    double *output = (double *)pOutputCol->pData;
20✔
1726
    for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) {
40✔
1727
      if (IS_HELPER_NULL(pLeftCol, i)) {
40✔
1728
        colDataSetNULL(pOutputCol, i);
2✔
1729
        continue;
2✔
1730
      }
1731
      double result = 0;
18✔
1732
      SCL_ERR_JRET(getVectorDoubleValueFnLeft(LEFT_COL, i, &result));
18✔
1733
      *output = (result == 0) ? 0 : -result;
18✔
1734
    }
1735
  }
1736

1737
_return:
20✔
1738
  doReleaseVec(pLeftCol, leftConvert);
20✔
1739
  SCL_RET(code);
20✔
1740
}
1741

1742
int32_t vectorAssign(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
52,801✔
1743
  SColumnInfoData *pOutputCol = pOut->columnData;
52,801✔
1744
  pOut->numOfRows = pLeft->numOfRows;
52,801✔
1745

1746
  if (colDataIsNull_s(pRight->columnData, 0)) {
105,602✔
1747
    colDataSetNNULL(pOutputCol, 0, pOut->numOfRows);
×
1748
  } else {
1749
    char *d = colDataGetData(pRight->columnData, 0);
52,801✔
1750
    for (int32_t i = 0; i < pOut->numOfRows; ++i) {
105,638✔
1751
      SCL_ERR_RET(colDataSetVal(pOutputCol, i, d, false));
52,792✔
1752
    }
1753
  }
1754

1755
  if (pRight->numOfQualified != 1 && pRight->numOfQualified != 0) {
52,846✔
1756
    sclError("vectorAssign: invalid qualified number %d", pRight->numOfQualified);
×
1757
    SCL_ERR_RET(TSDB_CODE_APP_ERROR);
×
1758
  }
1759
  pOut->numOfQualified = pRight->numOfQualified * pOut->numOfRows;
52,836✔
1760
  return TSDB_CODE_SUCCESS;
52,836✔
1761
}
1762

1763
int32_t vectorBitAnd(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
25✔
1764
  SColumnInfoData *pOutputCol = pOut->columnData;
25✔
1765
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
25✔
1766

1767
  int32_t code = TSDB_CODE_SUCCESS;
25✔
1768
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
25✔
1769
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
25✔
1770

1771
  int32_t          leftConvert = 0, rightConvert = 0;
25✔
1772
  SColumnInfoData *pLeftCol = NULL;
25✔
1773
  SColumnInfoData *pRightCol = NULL;
25✔
1774
  SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
25✔
1775
  SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
25✔
1776

1777
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
1778
  _getBigintValue_fn_t getVectorBigintValueFnRight;
1779
  SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
25✔
1780
  SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
25✔
1781

1782
  int64_t *output = (int64_t *)pOutputCol->pData;
25✔
1783
  int32_t numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
25✔
1784
  for (; i < numOfRows && i >= 0; i += step, output += 1) {
54✔
1785
    int32_t leftidx = pLeft->numOfRows == 1 ? 0 : i;
29✔
1786
    int32_t rightidx = pRight->numOfRows == 1 ? 0 : i;
29✔
1787
    if (IS_HELPER_NULL(pRightCol, rightidx) || IS_HELPER_NULL(pLeftCol, leftidx)) {
85✔
1788
      colDataSetNULL(pOutputCol, i);
4✔
1789
      continue;  // TODO set null or ignore
4✔
1790
    }
1791
    int64_t leftRes = 0;
25✔
1792
    int64_t rightRes = 0;
25✔
1793
    SCL_ERR_JRET(getVectorBigintValueFnLeft(LEFT_COL, leftidx, &leftRes));
25✔
1794
    SCL_ERR_JRET(getVectorBigintValueFnRight(RIGHT_COL, rightidx, &rightRes));
25✔
1795
    *output = leftRes & rightRes;
25✔
1796
  }
1797

1798
_return:
25✔
1799
  doReleaseVec(pLeftCol, leftConvert);
25✔
1800
  doReleaseVec(pRightCol, rightConvert);
25✔
1801
  SCL_RET(code);
25✔
1802
}
1803

1804
int32_t vectorBitOr(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
24✔
1805
  SColumnInfoData *pOutputCol = pOut->columnData;
24✔
1806
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
24✔
1807

1808
  int32_t code = TSDB_CODE_SUCCESS;
24✔
1809
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
24✔
1810
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
24✔
1811

1812
  int32_t          leftConvert = 0, rightConvert = 0;
24✔
1813
  SColumnInfoData *pLeftCol = NULL;
24✔
1814
  SColumnInfoData *pRightCol = NULL;
24✔
1815
  SCL_ERR_JRET(vectorConvertVarToDouble(pLeft, &leftConvert, &pLeftCol));
24✔
1816
  SCL_ERR_JRET(vectorConvertVarToDouble(pRight, &rightConvert, &pRightCol));
24✔
1817

1818
  _getBigintValue_fn_t getVectorBigintValueFnLeft;
1819
  _getBigintValue_fn_t getVectorBigintValueFnRight;
1820
  SCL_ERR_JRET(getVectorBigintValueFn(pLeftCol->info.type, &getVectorBigintValueFnLeft));
24✔
1821
  SCL_ERR_JRET(getVectorBigintValueFn(pRightCol->info.type, &getVectorBigintValueFnRight));
24✔
1822

1823
  int64_t *output = (int64_t *)pOutputCol->pData;
24✔
1824
  int32_t numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
24✔
1825
  for (; i < numOfRows && i >= 0; i += step, output += 1) {
56✔
1826
    int32_t leftidx = pLeft->numOfRows == 1 ? 0 : i;
32✔
1827
    int32_t rightidx = pRight->numOfRows == 1 ? 0 : i;
32✔
1828
    if (IS_HELPER_NULL(pRightCol, leftidx) || IS_HELPER_NULL(pLeftCol, rightidx)) {
94✔
1829
      colDataSetNULL(pOutputCol, i);
4✔
1830
      continue;  // TODO set null or ignore
4✔
1831
    }
1832

1833
    int64_t leftRes = 0;
28✔
1834
    int64_t rightRes = 0;
28✔
1835
    SCL_ERR_JRET(getVectorBigintValueFnLeft(LEFT_COL, leftidx, &leftRes));
28✔
1836
    SCL_ERR_JRET(getVectorBigintValueFnRight(RIGHT_COL, rightidx, &rightRes));
28✔
1837
    *output = leftRes | rightRes;
28✔
1838
  }
1839

1840
_return:
24✔
1841
  doReleaseVec(pLeftCol, leftConvert);
24✔
1842
  doReleaseVec(pRightCol, rightConvert);
24✔
1843
  SCL_RET(code);
24✔
1844
}
1845

1846
int32_t doVectorCompareImpl(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
587✔
1847
                            int32_t numOfRows, int32_t step, __compar_fn_t fp, int32_t optr, int32_t *num) {
1848
  bool   *pRes = (bool *)pOut->columnData->pData;
587✔
1849
  int32_t code = TSDB_CODE_SUCCESS;
587✔
1850
  if (IS_MATHABLE_TYPE(GET_PARAM_TYPE(pLeft)) && IS_MATHABLE_TYPE(GET_PARAM_TYPE(pRight))) {
587✔
1851
    if (!(pLeft->columnData->hasNull || pRight->columnData->hasNull)) {
262✔
1852
      for (int32_t i = startIndex; i < numOfRows && i >= 0; i += step) {
108,901✔
1853
        int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
108,652✔
1854
        int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;
108,652✔
1855

1856
        pRes[i] = compareForType(fp, optr, pLeft->columnData, leftIndex, pRight->columnData, rightIndex);
108,652✔
1857
        if (pRes[i]) {
108,652✔
1858
          ++(*num);
95,119✔
1859
        }
1860
      }
1861
    } else {
1862
      for (int32_t i = startIndex; i < numOfRows && i >= 0; i += step) {
1,069✔
1863
        int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
1,056✔
1864
        int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;
1,056✔
1865

1866
        if (colDataIsNull_f(pLeft->columnData->nullbitmap, leftIndex) ||
1,056✔
1867
            colDataIsNull_f(pRight->columnData->nullbitmap, rightIndex)) {
1,047✔
1868
          pRes[i] = false;
9✔
1869
          continue;
9✔
1870
        }
1871
        pRes[i] = compareForType(fp, optr, pLeft->columnData, leftIndex, pRight->columnData, rightIndex);
1,047✔
1872
        if (pRes[i]) {
1,047✔
1873
          ++(*num);
1,036✔
1874
        }
1875
      }
1876
    }
1877
  } else {
1878
    //  if (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_JSON || GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_JSON) {
1879
    for (int32_t i = startIndex; i < numOfRows && i >= startIndex; i += step) {
10,793✔
1880
      int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
10,469✔
1881
      int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;
10,469✔
1882

1883
      if (IS_HELPER_NULL(pLeft->columnData, leftIndex) || IS_HELPER_NULL(pRight->columnData, rightIndex)) {
31,387✔
1884
        bool res = false;
32✔
1885
        colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
32✔
1886
        continue;
32✔
1887
      }
1888

1889
      char   *pLeftData = colDataGetData(pLeft->columnData, leftIndex);
10,437✔
1890
      char   *pRightData = colDataGetData(pRight->columnData, rightIndex);
10,437✔
1891
      int64_t leftOut = 0;
10,437✔
1892
      int64_t rightOut = 0;
10,437✔
1893
      bool    freeLeft = false;
10,437✔
1894
      bool    freeRight = false;
10,437✔
1895
      bool    isJsonnull = false;
10,437✔
1896
      bool    result = false;
10,437✔
1897

1898
      SCL_ERR_RET(convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData,
10,437✔
1899
                                   &leftOut, &rightOut, &isJsonnull, &freeLeft, &freeRight, &result, pLeft->charsetCxt));
1900

1901
      if (isJsonnull) {
10,440✔
1902
        sclError("doVectorCompareImpl: invalid json null value");
×
1903
        SCL_ERR_RET(TSDB_CODE_APP_ERROR);
2✔
1904
      }
1905

1906
      if (!pLeftData || !pRightData) {
10,442✔
1907
        result = false;
×
1908
      }
1909
      if (!result) {
10,442✔
1910
        colDataSetInt8(pOut->columnData, i, (int8_t *)&result);
72✔
1911
      } else {
1912
        bool res = filterDoCompare(fp, optr, pLeftData, pRightData);
10,370✔
1913
        colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
10,364✔
1914
        if (res) {
10,364✔
1915
          ++(*num);
2,677✔
1916
        }
1917
      }
1918

1919
      if (freeLeft) {
10,436✔
1920
        taosMemoryFreeClear(pLeftData);
×
1921
      }
1922

1923
      if (freeRight) {
10,436✔
1924
        taosMemoryFreeClear(pRightData);
×
1925
      }
1926
    }
1927
  }
1928

1929
  return code;
586✔
1930
}
1931

1932
int32_t doVectorCompare(SScalarParam *pLeft, SScalarParam *pLeftVar, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
612✔
1933
                     int32_t numOfRows, int32_t _ord, int32_t optr) {
1934
  int32_t       i = 0;
612✔
1935
  int32_t       step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
612✔
1936
  int32_t       lType = GET_PARAM_TYPE(pLeft);
612✔
1937
  int32_t       rType = GET_PARAM_TYPE(pRight);
612✔
1938
  __compar_fn_t fp = NULL;
612✔
1939
  __compar_fn_t fpVar = NULL;
612✔
1940
  int32_t       compRows = 0;
612✔
1941
  if (lType == rType) {
612✔
1942
    SCL_ERR_RET(filterGetCompFunc(&fp, lType, optr));
208✔
1943
  } else {
1944
    fp = filterGetCompFuncEx(lType, rType, optr);
404✔
1945
  }
1946

1947
  if (pLeftVar != NULL) {
611✔
1948
    SCL_ERR_RET(filterGetCompFunc(&fpVar, GET_PARAM_TYPE(pLeftVar), optr));
1✔
1949
  }
1950
  if (startIndex < 0) {
611✔
1951
    i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
611✔
1952
    pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
611✔
1953
    compRows = pOut->numOfRows;
611✔
1954
  } else {
1955
    compRows = startIndex + numOfRows;
×
1956
    i = startIndex;
×
1957
  }
1958

1959
  if (pRight->pHashFilter != NULL) {
611✔
1960
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
236✔
1961
      if (IS_HELPER_NULL(pLeft->columnData, i)) {
424✔
1962
        bool res = false;
12✔
1963
        colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
12✔
1964
        continue;
12✔
1965
      }
1966

1967
      bool  res = compareForTypeWithColAndHash(fp, optr, pLeft->columnData, i, pRight->pHashFilter,
200✔
1968
                                               pRight->filterValueType, pRight->filterValueTypeMod);
1969
      if (pLeftVar != NULL && taosHashGetSize(pRight->pHashFilterOthers) > 0){
200✔
1970
        do{
1971
          if (optr == OP_TYPE_IN && res){
×
1972
            break;
×
1973
          }
1974
          if (optr == OP_TYPE_NOT_IN && !res){
×
1975
            break;
×
1976
          }
1977
          res = compareForTypeWithColAndHash(fpVar, optr, pLeftVar->columnData, i, pRight->pHashFilterOthers,
×
1978
                                             pRight->filterValueType, pRight->filterValueTypeMod);
1979
        }while(0);
1980
      }
1981
      colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
200✔
1982
      if (res) {
200✔
1983
        pOut->numOfQualified++;
10✔
1984
      }
1985
    }
1986
  } else {  // normal compare
1987
    SCL_ERR_RET(doVectorCompareImpl(pLeft, pRight, pOut, i, compRows, step, fp, optr, &(pOut->numOfQualified)));
587✔
1988
  }
1989
  return TSDB_CODE_SUCCESS;
613✔
1990
}
1991

1992
int32_t vectorCompareImpl(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
610✔
1993
                          int32_t numOfRows, int32_t _ord, int32_t optr) {
1994
  SScalarParam  pLeftOut = {0};
610✔
1995
  SScalarParam  pRightOut = {0};
610✔
1996
  SScalarParam *param1 = NULL;
610✔
1997
  SScalarParam *param2 = NULL;
610✔
1998
  SScalarParam *param3 = NULL;
610✔
1999
  int32_t code = TSDB_CODE_SUCCESS;
610✔
2000
  setTzCharset(&pLeftOut, pLeft->tz, pLeft->charsetCxt);
610✔
2001
  setTzCharset(&pRightOut, pLeft->tz, pLeft->charsetCxt);
610✔
2002
  if (noConvertBeforeCompare(GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), optr)) {
613✔
2003
    param1 = pLeft;
257✔
2004
    param2 = pRight;
257✔
2005
  } else {
2006
    SCL_ERR_JRET(vectorConvertCols(pLeft, pRight, &pLeftOut, &pRightOut, startIndex, numOfRows));
356✔
2007
    param1 = (pLeftOut.columnData != NULL) ? &pLeftOut : pLeft;
355✔
2008
    param2 = (pRightOut.columnData != NULL) ? &pRightOut : pRight;
355✔
2009
    if (pRight->pHashFilterOthers != NULL){
355✔
2010
      param3 = pLeft;
1✔
2011
    }
2012
  }
2013

2014
  SCL_ERR_JRET(doVectorCompare(param1, param3, param2, pOut, startIndex, numOfRows, _ord, optr));
612✔
2015

2016
_return:
613✔
2017
  sclFreeParam(&pLeftOut);
613✔
2018
  sclFreeParam(&pRightOut);
613✔
2019
  SCL_RET(code);
612✔
2020
}
2021

2022
int32_t vectorCompare(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
610✔
2023
  SCL_RET(vectorCompareImpl(pLeft, pRight, pOut, -1, -1, _ord, optr));
610✔
2024
}
2025

2026
int32_t vectorGreater(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
65✔
2027
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN));
65✔
2028
}
2029

2030
int32_t vectorGreaterEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
123✔
2031
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL));
123✔
2032
}
2033

2034
int32_t vectorLower(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
67✔
2035
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN));
67✔
2036
}
2037

2038
int32_t vectorLowerEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
26✔
2039
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL));
26✔
2040
}
2041

2042
int32_t vectorEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
150✔
2043
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL));
150✔
2044
}
2045

2046
int32_t vectorNotEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
94✔
2047
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL));
94✔
2048
}
2049

2050
int32_t vectorIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
22✔
2051
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN));
22✔
2052
}
2053

2054
int32_t vectorNotIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
2✔
2055
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN));
2✔
2056
}
2057

2058
int32_t vectorLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
23✔
2059
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE));
23✔
2060
}
2061

2062
int32_t vectorNotLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
12✔
2063
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE));
12✔
2064
}
2065

2066
int32_t vectorMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
12✔
2067
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH));
12✔
2068
}
2069

2070
int32_t vectorNotMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
12✔
2071
  SCL_RET(vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH));
12✔
2072
}
2073

2074
int32_t vectorIsNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
13✔
2075
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
30✔
2076
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 1 : 0;
34✔
2077
    if (v) {
17✔
2078
      ++pOut->numOfQualified;
5✔
2079
    }
2080
    colDataSetInt8(pOut->columnData, i, &v);
17✔
2081
    colDataClearNull_f(pOut->columnData->nullbitmap, i);
17✔
2082
  }
2083
  pOut->numOfRows = pLeft->numOfRows;
13✔
2084
  return TSDB_CODE_SUCCESS;
13✔
2085
}
2086

2087
int32_t vectorNotNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
14✔
2088
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
32✔
2089
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 0 : 1;
36✔
2090
    if (v) {
18✔
2091
      ++pOut->numOfQualified;
14✔
2092
    }
2093
    colDataSetInt8(pOut->columnData, i, &v);
18✔
2094
    colDataClearNull_f(pOut->columnData->nullbitmap, i);
18✔
2095
  }
2096
  pOut->numOfRows = pLeft->numOfRows;
14✔
2097
  return TSDB_CODE_SUCCESS;
14✔
2098
}
2099

2100
int32_t vectorIsTrue(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
24✔
2101
  SCL_ERR_RET(vectorConvertSingleColImpl(pLeft, pOut, NULL, -1, -1));
24✔
2102
  for (int32_t i = 0; i < pOut->numOfRows; ++i) {
52✔
2103
    if (colDataIsNull_s(pOut->columnData, i)) {
56✔
2104
      int8_t v = 0;
2✔
2105
      colDataSetInt8(pOut->columnData, i, &v);
2✔
2106
      colDataClearNull_f(pOut->columnData->nullbitmap, i);
2✔
2107
    }
2108
    {
2109
      bool v = false;
28✔
2110
      GET_TYPED_DATA(v, bool, pOut->columnData->info.type, colDataGetData(pOut->columnData, i), typeGetTypeModFromColInfo(&pOut->columnData->info));
28✔
2111
      if (v) {
28✔
2112
        ++pOut->numOfQualified;
19✔
2113
      }
2114
    }
2115
  }
2116
  pOut->columnData->hasNull = false;
24✔
2117
  return TSDB_CODE_SUCCESS;
24✔
2118
}
2119

2120
int32_t getJsonValue(char *json, char *key, bool *isExist, STagVal *val) {
350✔
2121
  val->pKey = key;
350✔
2122
  if (json == NULL || tTagIsJson((const STag *)json) == false) {
350✔
2123
    if (isExist) {
×
2124
      *isExist = false;
×
2125
    }
2126
    SCL_ERR_RET(TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR);
×
2127
  }
2128

2129
  bool find = tTagGet(((const STag *)json), val);  // json value is null and not exist is different
350✔
2130
  if (isExist) {
350✔
2131
    *isExist = find;
350✔
2132
  }
2133
  SCL_RET(TSDB_CODE_SUCCESS);
350✔
2134
}
2135

2136
int32_t vectorJsonContains(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
×
2137
  SColumnInfoData *pOutputCol = pOut->columnData;
×
2138

2139
  int32_t code = TSDB_CODE_SUCCESS;
×
2140
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
×
2141
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
×
2142

2143
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
×
2144

2145
  char *pRightData = colDataGetVarData(pRight->columnData, 0);
×
2146
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
×
2147
  if (NULL == jsonKey) {
×
2148
    SCL_ERR_RET(terrno);
×
2149
  }
2150
  (void)memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
×
2151
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
×
2152
    bool isExist = false;
×
2153

2154
    if (!colDataIsNull_var(pLeft->columnData, i)) {
×
2155
      char *pLeftData = colDataGetVarData(pLeft->columnData, i);
×
2156
      STagVal value;
2157
      SCL_ERR_JRET(getJsonValue(pLeftData, jsonKey, &isExist, &value));
×
2158
    }
2159
    if (isExist) {
×
2160
      ++pOut->numOfQualified;
×
2161
    }
2162
    SCL_ERR_JRET(colDataSetVal(pOutputCol, i, (const char *)(&isExist), false));
×
2163
  }
2164

2165
_return:
×
2166
  taosMemoryFree(jsonKey);
×
2167
  SCL_RET(code);
×
2168
}
2169

2170
int32_t vectorJsonArrow(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
350✔
2171
  SColumnInfoData *pOutputCol = pOut->columnData;
350✔
2172

2173
  int32_t code = TSDB_CODE_SUCCESS;
350✔
2174
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
350✔
2175
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
350✔
2176

2177
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
350✔
2178

2179
  char *pRightData = colDataGetVarData(pRight->columnData, 0);
350✔
2180
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
350✔
2181
  if (NULL == jsonKey) {
350✔
2182
    SCL_ERR_RET(terrno);
×
2183
  }
2184
  (void)memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
350✔
2185
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
700✔
2186
    if (colDataIsNull_var(pLeft->columnData, i)) {
350✔
2187
      colDataSetNull_var(pOutputCol, i);
×
2188
      pOutputCol->hasNull = true;
×
2189
      continue;
×
2190
    }
2191
    char   *pLeftData = colDataGetVarData(pLeft->columnData, i);
350✔
2192
    bool    isExist = false;
350✔
2193
    STagVal value;
2194
    SCL_ERR_JRET(getJsonValue(pLeftData, jsonKey, &isExist, &value));
350✔
2195
    char   *data = isExist ? tTagValToData(&value, true) : NULL;
350✔
2196
    code = colDataSetVal(pOutputCol, i, data, data == NULL);
350✔
2197
    if (isExist && IS_VAR_DATA_TYPE(value.type) && data) {
350✔
2198
      taosMemoryFree(data);
70✔
2199
    }
2200
    SCL_ERR_JRET(code);
350✔
2201
  }
2202

2203
_return:
350✔
2204
  taosMemoryFree(jsonKey);
350✔
2205
  SCL_RET(code);
350✔
2206
}
2207

2208
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
54,343✔
2209
  switch (binFunctionId) {
54,343✔
2210
    case OP_TYPE_ADD:
163✔
2211
      return vectorMathAdd;
163✔
2212
    case OP_TYPE_SUB:
183✔
2213
      return vectorMathSub;
183✔
2214
    case OP_TYPE_MULTI:
45✔
2215
      return vectorMathMultiply;
45✔
2216
    case OP_TYPE_DIV:
33✔
2217
      return vectorMathDivide;
33✔
2218
    case OP_TYPE_REM:
20✔
2219
      return vectorMathRemainder;
20✔
2220
    case OP_TYPE_MINUS:
20✔
2221
      return vectorMathMinus;
20✔
2222
    case OP_TYPE_ASSIGN:
52,822✔
2223
      return vectorAssign;
52,822✔
2224
    case OP_TYPE_GREATER_THAN:
65✔
2225
      return vectorGreater;
65✔
2226
    case OP_TYPE_GREATER_EQUAL:
123✔
2227
      return vectorGreaterEqual;
123✔
2228
    case OP_TYPE_LOWER_THAN:
67✔
2229
      return vectorLower;
67✔
2230
    case OP_TYPE_LOWER_EQUAL:
26✔
2231
      return vectorLowerEqual;
26✔
2232
    case OP_TYPE_EQUAL:
152✔
2233
      return vectorEqual;
152✔
2234
    case OP_TYPE_NOT_EQUAL:
94✔
2235
      return vectorNotEqual;
94✔
2236
    case OP_TYPE_IN:
22✔
2237
      return vectorIn;
22✔
2238
    case OP_TYPE_NOT_IN:
2✔
2239
      return vectorNotIn;
2✔
2240
    case OP_TYPE_LIKE:
23✔
2241
      return vectorLike;
23✔
2242
    case OP_TYPE_NOT_LIKE:
12✔
2243
      return vectorNotLike;
12✔
2244
    case OP_TYPE_MATCH:
12✔
2245
      return vectorMatch;
12✔
2246
    case OP_TYPE_NMATCH:
12✔
2247
      return vectorNotMatch;
12✔
2248
    case OP_TYPE_IS_NULL:
13✔
2249
      return vectorIsNull;
13✔
2250
    case OP_TYPE_IS_NOT_NULL:
14✔
2251
      return vectorNotNull;
14✔
2252
    case OP_TYPE_BIT_AND:
25✔
2253
      return vectorBitAnd;
25✔
2254
    case OP_TYPE_BIT_OR:
24✔
2255
      return vectorBitOr;
24✔
2256
    case OP_TYPE_IS_TRUE:
24✔
2257
      return vectorIsTrue;
24✔
2258
    case OP_TYPE_JSON_GET_VALUE:
350✔
2259
      return vectorJsonArrow;
350✔
2260
    case OP_TYPE_JSON_CONTAINS:
×
2261
      return vectorJsonContains;
×
2262
    default:
×
2263
      return NULL;
×
2264
  }
2265
}
2266

2267
bool checkOperatorRestypeIsTimestamp(EOperatorType opType, int32_t lType, int32_t rType) {
373✔
2268
  if (opType != OP_TYPE_ADD && opType != OP_TYPE_SUB && opType != OP_TYPE_MINUS) {
373✔
2269
    return false;
44✔
2270
  }
2271
  if ((TSDB_DATA_TYPE_TIMESTAMP == lType && IS_INTEGER_TYPE(rType) && rType != TSDB_DATA_TYPE_UBIGINT) ||
329✔
2272
      (TSDB_DATA_TYPE_TIMESTAMP == rType && IS_INTEGER_TYPE(lType) && lType != TSDB_DATA_TYPE_UBIGINT) ||
283✔
2273
      (TSDB_DATA_TYPE_TIMESTAMP == lType && TSDB_DATA_TYPE_BOOL == rType) ||
283✔
2274
      (TSDB_DATA_TYPE_TIMESTAMP == rType && TSDB_DATA_TYPE_BOOL == lType)) {
×
2275
    return true;
46✔
2276
  }
2277
  return false;
283✔
2278
}
2279

2280
static int32_t vectorMathOpOneRowForDecimal(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t step,
×
2281
                                            int32_t i, EOperatorType op, SScalarParam *pOneRowParam) {
2282
  SScalarParam *pNotOneRowParam = pLeft == pOneRowParam ? pRight : pLeft;
×
2283
  Decimal      *output = (Decimal *)pOut->columnData->pData;
×
2284
  int32_t       code = 0;
×
2285
  SDataType     leftType = GET_COL_DATA_TYPE(pLeft->columnData->info),
×
2286
            rightType = GET_COL_DATA_TYPE(pRight->columnData->info),
×
2287
            outType = GET_COL_DATA_TYPE(pOut->columnData->info);
×
2288
  if (IS_HELPER_NULL(pOneRowParam->columnData, 0)) {
×
2289
    colDataSetNNULL(pOut->columnData, 0, pNotOneRowParam->numOfRows);
×
2290
  }
2291
  Decimal oneRowData = {0};
×
2292
  SDataType oneRowType = outType;
×
2293
  oneRowType.precision = TSDB_DECIMAL_MAX_PRECISION;
×
2294
  if (pLeft == pOneRowParam) {
×
2295
    oneRowType.scale = leftType.scale;
×
2296
    code = convertToDecimal(colDataGetData(pLeft->columnData, 0), &leftType, &oneRowData, &oneRowType);
×
2297
  } else {
2298
    oneRowType.scale = rightType.scale;
×
2299
    code = convertToDecimal(colDataGetData(pRight->columnData, 0), &rightType, &oneRowData, &oneRowType);
×
2300
  }
2301
  if (code != 0) return code;
×
2302

2303
  for (; i < pNotOneRowParam->numOfRows && i >= 0 && TSDB_CODE_SUCCESS == code; i += step, output += 1) {
×
2304
    if (IS_HELPER_NULL(pNotOneRowParam->columnData, i)) {
×
2305
      colDataSetNULL(pOut->columnData, i);
×
2306
      continue;
×
2307
    }
2308
    if (pOneRowParam == pLeft) {
×
2309
      code =
2310
          decimalOp(op, &oneRowType, &rightType, &outType, &oneRowData, colDataGetData(pRight->columnData, i), output);
×
2311
    } else {
2312
      code = decimalOp(op, &leftType, &oneRowType, &outType, colDataGetData(pLeft->columnData, i), &oneRowData, output);
×
2313
    }
2314
  }
2315
  return code;
×
2316
}
2317

2318
static int32_t vectorMathUnaryOpForDecimal(SScalarParam *pCol, SScalarParam *pOut, int32_t step, int32_t i,
×
2319
                                           EOperatorType op) {
2320
  int32_t          code = 0;
×
2321
  SColumnInfoData *pOutputCol = pOut->columnData;
×
2322
  char            *pDec = pOutputCol->pData;
×
2323
  for (; i < pCol->numOfRows && i >= 0; i += step, pDec += tDataTypes[pOutputCol->info.type].bytes) {
×
2324
    if (IS_HELPER_NULL(pCol->columnData, i)) {
×
2325
      colDataSetNULL(pOutputCol, i);
×
2326
      continue;
×
2327
    }
2328
    SDataType colDt = GET_COL_DATA_TYPE(pCol->columnData->info), outDt = GET_COL_DATA_TYPE(pOutputCol->info);
×
2329

2330
    code = decimalOp(op, &colDt, NULL, &outDt, colDataGetData(pCol->columnData, i), NULL, pDec);
×
2331
  }
2332
  return code;
×
2333
}
2334

2335
static int32_t vectorMathBinaryOpForDecimal(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t step,
×
2336
                                            int32_t i, EOperatorType op) {
2337
  Decimal  *output = (Decimal *)pOut->columnData->pData;
×
2338
  int32_t   code = 0;
×
2339
  SDataType leftType = GET_COL_DATA_TYPE(pLeft->columnData->info),
×
2340
            rightType = GET_COL_DATA_TYPE(pRight->columnData->info),
×
2341
            outType = GET_COL_DATA_TYPE(pOut->columnData->info);
×
2342
  if (pLeft->numOfRows == pRight->numOfRows) {
×
2343
    for (; i < pRight->numOfRows && i >= 0 && TSDB_CODE_SUCCESS == code; i += step, output += 1) {
×
2344
      if (IS_NULL) {
×
2345
        colDataSetNULL(pOut->columnData, i);
×
2346
        continue;
×
2347
      }
2348
      code = decimalOp(op, &leftType, &rightType, &outType, colDataGetData(pLeft->columnData, i),
×
2349
                               colDataGetData(pRight->columnData, i), output);
×
2350
    }
2351
  } else if (pLeft->numOfRows == 1) {
×
2352
    code = vectorMathOpOneRowForDecimal(pLeft, pRight, pOut, step, i, op, pLeft);
×
2353
  } else if (pRight->numOfRows == 1) {
×
2354
    code = vectorMathOpOneRowForDecimal(pLeft, pRight, pOut, step, i, op, pRight);
×
2355
  }
2356
  return code;
×
2357
}
2358

2359
bool compareForType(__compar_fn_t fp, int32_t optr, SColumnInfoData* pColL, int32_t idxL, SColumnInfoData* pColR, int32_t idxR) {
109,699✔
2360
  void* pLeftData = colDataGetData(pColL, idxL), *pRightData = colDataGetData(pColR, idxR);
109,699✔
2361
  if (IS_DECIMAL_TYPE(pColL->info.type) || IS_DECIMAL_TYPE(pColR->info.type)) {
109,699✔
2362
    SDecimalCompareCtx ctxL = {.pData = pLeftData,
×
2363
                               .type = pColL->info.type,
×
2364
                               .typeMod = typeGetTypeModFromColInfo(&pColL->info)},
×
2365
                       ctxR = {.pData = pRightData,
×
2366
                               .type = pColR->info.type,
×
2367
                               .typeMod = typeGetTypeModFromColInfo(&pColR->info)};
×
2368
    return filterDoCompare(fp, optr, &ctxL, &ctxR);
×
2369
  } else {
2370
    return filterDoCompare(fp, optr, pLeftData, pRightData);
109,699✔
2371
  }
2372
}
2373

2374
bool compareForTypeWithColAndHash(__compar_fn_t fp, int32_t optr, SColumnInfoData *pColL, int32_t idxL,
200✔
2375
                              const void *pHashData, int32_t hashType, STypeMod hashTypeMod) {
2376
  void * pLeftData = colDataGetData(pColL, idxL);
200✔
2377
  if (IS_DECIMAL_TYPE(pColL->info.type) || IS_DECIMAL_TYPE(hashType)) {
200✔
2378
    SDecimalCompareCtx ctxL = {.pData = pLeftData,
×
2379
                               .type = pColL->info.type,
×
2380
                               .typeMod = typeGetTypeModFromColInfo(&pColL->info)},
×
2381
                       ctxR = {.pData = (void *)pHashData, .type = hashType, .typeMod = hashTypeMod};
×
2382
    return filterDoCompare(fp, optr, &ctxL, &ctxR);
×
2383
  } else {
2384
    return filterDoCompare(fp, optr, pLeftData, (void*)pHashData);
200✔
2385
  }
2386
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc