sclvector.c 71.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "os.h"

H
Haojun Liao 已提交
18
#include "filter.h"
D
dapan1121 已提交
19
#include "filterInt.h"
D
dapan1121 已提交
20
#include "query.h"
H
Haojun Liao 已提交
21
#include "querynodes.h"
D
dapan1121 已提交
22
#include "sclInt.h"
H
Haojun Liao 已提交
23 24 25
#include "sclvector.h"
#include "tcompare.h"
#include "tdatablock.h"
wmmhello's avatar
wmmhello 已提交
26
#include "tdataformat.h"
D
dapan1121 已提交
27
#include "ttime.h"
H
Hongze Cheng 已提交
28
#include "ttypes.h"
29

H
Hongze Cheng 已提交
30 31
#define LEFT_COL  ((pLeftCol->info.type == TSDB_DATA_TYPE_JSON ? (void *)pLeftCol : pLeftCol->pData))
#define RIGHT_COL ((pRightCol->info.type == TSDB_DATA_TYPE_JSON ? (void *)pRightCol : pRightCol->pData))
32

H
Hongze Cheng 已提交
33 34 35 36
#define IS_NULL                                                                              \
  colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i) ||         \
      IS_JSON_NULL(pLeft->columnData->info.type, colDataGetVarData(pLeft->columnData, i)) || \
      IS_JSON_NULL(pRight->columnData->info.type, colDataGetVarData(pRight->columnData, i))
wmmhello's avatar
wmmhello 已提交
37

H
Hongze Cheng 已提交
38
#define IS_HELPER_NULL(col, i) colDataIsNull_s(col, i) || IS_JSON_NULL(col->info.type, colDataGetVarData(col, i))
wmmhello's avatar
wmmhello 已提交
39

H
Haojun Liao 已提交
40 41 42 43 44
bool noConvertBeforeCompare(int32_t leftType, int32_t rightType, int32_t optr) {
  return IS_NUMERIC_TYPE(leftType) && IS_NUMERIC_TYPE(rightType) &&
         (optr >= OP_TYPE_GREATER_THAN && optr <= OP_TYPE_NOT_EQUAL);
}

H
Hongze Cheng 已提交
45
void convertNumberToNumber(const void *inData, void *outData, int8_t inType, int8_t outType) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  switch (outType) {
    case TSDB_DATA_TYPE_BOOL: {
      GET_TYPED_DATA(*((bool *)outData), bool, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_TINYINT: {
      GET_TYPED_DATA(*((int8_t *)outData), int8_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_SMALLINT: {
      GET_TYPED_DATA(*((int16_t *)outData), int16_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_INT: {
      GET_TYPED_DATA(*((int32_t *)outData), int32_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP: {
      GET_TYPED_DATA(*((int64_t *)outData), int64_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_UTINYINT: {
      GET_TYPED_DATA(*((uint8_t *)outData), uint8_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT: {
      GET_TYPED_DATA(*((uint16_t *)outData), uint16_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_UINT: {
      GET_TYPED_DATA(*((uint32_t *)outData), uint32_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
      GET_TYPED_DATA(*((uint64_t *)outData), uint64_t, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
      GET_TYPED_DATA(*((float *)outData), float, inType, inData);
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
      GET_TYPED_DATA(*((double *)outData), double, inType, inData);
      break;
    }
H
Hongze Cheng 已提交
92
    default: {
93 94 95 96 97
      ASSERT(0);
    }
  }
}

H
Hongze Cheng 已提交
98
void convertNcharToDouble(const void *inData, void *outData) {
99
  char *tmp = taosMemoryMalloc(varDataTLen(inData));
H
Hongze Cheng 已提交
100
  int   len = taosUcs4ToMbs((TdUcs4 *)varDataVal(inData), varDataLen(inData), tmp);
101 102 103 104 105 106
  if (len < 0) {
    sclError("castConvert taosUcs4ToMbs error 1");
  }

  tmp[len] = 0;

wafwerar's avatar
wafwerar 已提交
107
  double value = taosStr2Double(tmp, NULL);
108 109 110 111 112

  *((double *)outData) = value;
  taosMemoryFreeClear(tmp);
}

H
Hongze Cheng 已提交
113
void convertBinaryToDouble(const void *inData, void *outData) {
114
  char *tmp = taosMemoryCalloc(1, varDataTLen(inData));
H
Hongze Cheng 已提交
115
  if (tmp == NULL) {
116 117 118 119 120 121 122 123 124
    *((double *)outData) = 0.;
    return;
  }
  memcpy(tmp, varDataVal(inData), varDataLen(inData));
  double ret = taosStr2Double(tmp, NULL);
  taosMemoryFree(tmp);
  *((double *)outData) = ret;
}

D
dapan1121 已提交
125 126
typedef int64_t (*_getBigintValue_fn_t)(void *src, int32_t index);

H
Hongze Cheng 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
int64_t getVectorBigintValue_TINYINT(void *src, int32_t index) { return (int64_t) * ((int8_t *)src + index); }
int64_t getVectorBigintValue_UTINYINT(void *src, int32_t index) { return (int64_t) * ((uint8_t *)src + index); }
int64_t getVectorBigintValue_SMALLINT(void *src, int32_t index) { return (int64_t) * ((int16_t *)src + index); }
int64_t getVectorBigintValue_USMALLINT(void *src, int32_t index) { return (int64_t) * ((uint16_t *)src + index); }
int64_t getVectorBigintValue_INT(void *src, int32_t index) { return (int64_t) * ((int32_t *)src + index); }
int64_t getVectorBigintValue_UINT(void *src, int32_t index) { return (int64_t) * ((uint32_t *)src + index); }
int64_t getVectorBigintValue_BIGINT(void *src, int32_t index) { return (int64_t) * ((int64_t *)src + index); }
int64_t getVectorBigintValue_UBIGINT(void *src, int32_t index) { return (int64_t) * ((uint64_t *)src + index); }
int64_t getVectorBigintValue_FLOAT(void *src, int32_t index) { return (int64_t) * ((float *)src + index); }
int64_t getVectorBigintValue_DOUBLE(void *src, int32_t index) { return (int64_t) * ((double *)src + index); }
int64_t getVectorBigintValue_BOOL(void *src, int32_t index) { return (int64_t) * ((bool *)src + index); }

int64_t getVectorBigintValue_JSON(void *src, int32_t index) {
  ASSERT(!colDataIsNull_var(((SColumnInfoData *)src), index));
  char  *data = colDataGetVarData((SColumnInfoData *)src, index);
142
  double out = 0;
H
Hongze Cheng 已提交
143
  if (*data == TSDB_DATA_TYPE_NULL) {
144
    return 0;
H
Hongze Cheng 已提交
145 146 147
  } else if (*data == TSDB_DATA_TYPE_NCHAR) {  // json inner type can not be BINARY
    convertNcharToDouble(data + CHAR_BYTES, &out);
  } else if (tTagIsJson(data)) {
wmmhello's avatar
wmmhello 已提交
148 149
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
    return 0;
150
  } else {
H
Hongze Cheng 已提交
151
    convertNumberToNumber(data + CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
152 153 154 155
  }
  return (int64_t)out;
}

D
dapan1121 已提交
156
_getBigintValue_fn_t getVectorBigintValueFn(int32_t srcType) {
H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  _getBigintValue_fn_t p = NULL;
  if (srcType == TSDB_DATA_TYPE_TINYINT) {
    p = getVectorBigintValue_TINYINT;
  } else if (srcType == TSDB_DATA_TYPE_UTINYINT) {
    p = getVectorBigintValue_UTINYINT;
  } else if (srcType == TSDB_DATA_TYPE_SMALLINT) {
    p = getVectorBigintValue_SMALLINT;
  } else if (srcType == TSDB_DATA_TYPE_USMALLINT) {
    p = getVectorBigintValue_USMALLINT;
  } else if (srcType == TSDB_DATA_TYPE_INT) {
    p = getVectorBigintValue_INT;
  } else if (srcType == TSDB_DATA_TYPE_UINT) {
    p = getVectorBigintValue_UINT;
  } else if (srcType == TSDB_DATA_TYPE_BIGINT) {
    p = getVectorBigintValue_BIGINT;
  } else if (srcType == TSDB_DATA_TYPE_UBIGINT) {
    p = getVectorBigintValue_UBIGINT;
  } else if (srcType == TSDB_DATA_TYPE_FLOAT) {
    p = getVectorBigintValue_FLOAT;
  } else if (srcType == TSDB_DATA_TYPE_DOUBLE) {
    p = getVectorBigintValue_DOUBLE;
  } else if (srcType == TSDB_DATA_TYPE_TIMESTAMP) {
    p = getVectorBigintValue_BIGINT;
  } else if (srcType == TSDB_DATA_TYPE_BOOL) {
    p = getVectorBigintValue_BOOL;
  } else if (srcType == TSDB_DATA_TYPE_JSON) {
    p = getVectorBigintValue_JSON;
  } else if (srcType == TSDB_DATA_TYPE_NULL) {
    p = NULL;
  } else {
    ASSERT(0);
  }
  return p;
D
dapan1121 已提交
190 191
}

H
Hongze Cheng 已提交
192
typedef void *(*_getValueAddr_fn_t)(void *src, int32_t index);
193

H
Hongze Cheng 已提交
194 195 196 197 198 199 200 201 202 203 204 205
void *getVectorValueAddr_TINYINT(void *src, int32_t index) { return (void *)((int8_t *)src + index); }
void *getVectorValueAddr_UTINYINT(void *src, int32_t index) { return (void *)((uint8_t *)src + index); }
void *getVectorValueAddr_SMALLINT(void *src, int32_t index) { return (void *)((int16_t *)src + index); }
void *getVectorValueAddr_USMALLINT(void *src, int32_t index) { return (void *)((uint16_t *)src + index); }
void *getVectorValueAddr_INT(void *src, int32_t index) { return (void *)((int32_t *)src + index); }
void *getVectorValueAddr_UINT(void *src, int32_t index) { return (void *)((uint32_t *)src + index); }
void *getVectorValueAddr_BIGINT(void *src, int32_t index) { return (void *)((int64_t *)src + index); }
void *getVectorValueAddr_UBIGINT(void *src, int32_t index) { return (void *)((uint64_t *)src + index); }
void *getVectorValueAddr_FLOAT(void *src, int32_t index) { return (void *)((float *)src + index); }
void *getVectorValueAddr_DOUBLE(void *src, int32_t index) { return (void *)((double *)src + index); }
void *getVectorValueAddr_default(void *src, int32_t index) { return src; }
void *getVectorValueAddr_VAR(void *src, int32_t index) { return colDataGetData((SColumnInfoData *)src, index); }
206 207

_getValueAddr_fn_t getVectorValueAddrFn(int32_t srcType) {
H
Hongze Cheng 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  _getValueAddr_fn_t p = NULL;
  if (srcType == TSDB_DATA_TYPE_TINYINT) {
    p = getVectorValueAddr_TINYINT;
  } else if (srcType == TSDB_DATA_TYPE_UTINYINT) {
    p = getVectorValueAddr_UTINYINT;
  } else if (srcType == TSDB_DATA_TYPE_SMALLINT) {
    p = getVectorValueAddr_SMALLINT;
  } else if (srcType == TSDB_DATA_TYPE_USMALLINT) {
    p = getVectorValueAddr_USMALLINT;
  } else if (srcType == TSDB_DATA_TYPE_INT) {
    p = getVectorValueAddr_INT;
  } else if (srcType == TSDB_DATA_TYPE_UINT) {
    p = getVectorValueAddr_UINT;
  } else if (srcType == TSDB_DATA_TYPE_BIGINT) {
    p = getVectorValueAddr_BIGINT;
  } else if (srcType == TSDB_DATA_TYPE_UBIGINT) {
    p = getVectorValueAddr_UBIGINT;
  } else if (srcType == TSDB_DATA_TYPE_FLOAT) {
    p = getVectorValueAddr_FLOAT;
  } else if (srcType == TSDB_DATA_TYPE_DOUBLE) {
    p = getVectorValueAddr_DOUBLE;
  } else if (srcType == TSDB_DATA_TYPE_BINARY) {
    p = getVectorValueAddr_VAR;
  } else if (srcType == TSDB_DATA_TYPE_NCHAR) {
    p = getVectorValueAddr_VAR;
  } else {
    p = getVectorValueAddr_default;
  }
  return p;
}

static FORCE_INLINE void varToTimestamp(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
240 241 242 243
  int64_t value = 0;
  if (taosParseTime(buf, &value, strlen(buf), pOut->columnData->info.precision, tsDaylight) != TSDB_CODE_SUCCESS) {
    value = 0;
  }
G
Ganlin Zhao 已提交
244

245
  colDataSetInt64(pOut->columnData, rowIndex, &value);
D
dapan1121 已提交
246 247
}

H
Hongze Cheng 已提交
248
static FORCE_INLINE void varToSigned(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262
  if (overflow) {
    int64_t minValue = tDataTypes[pOut->columnData->info.type].minValue;
    int64_t maxValue = tDataTypes[pOut->columnData->info.type].maxValue;
    int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
    if (value > maxValue) {
      *overflow = 1;
      return;
    } else if (value < minValue) {
      *overflow = -1;
      return;
    } else {
      *overflow = 0;
    }
  }
G
Ganlin Zhao 已提交
263

D
fix bug  
dapan1121 已提交
264 265
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_TINYINT: {
wafwerar's avatar
wafwerar 已提交
266
      int8_t value = (int8_t)taosStr2Int8(buf, NULL, 10);
G
Ganlin Zhao 已提交
267

268
      colDataSetInt8(pOut->columnData, rowIndex, (int8_t *)&value);
D
fix bug  
dapan1121 已提交
269
      break;
G
Ganlin Zhao 已提交
270
    }
D
fix bug  
dapan1121 已提交
271
    case TSDB_DATA_TYPE_SMALLINT: {
wafwerar's avatar
wafwerar 已提交
272
      int16_t value = (int16_t)taosStr2Int16(buf, NULL, 10);
273
      colDataSetInt16(pOut->columnData, rowIndex, (int16_t *)&value);
D
fix bug  
dapan1121 已提交
274
      break;
G
Ganlin Zhao 已提交
275
    }
D
fix bug  
dapan1121 已提交
276
    case TSDB_DATA_TYPE_INT: {
wafwerar's avatar
wafwerar 已提交
277
      int32_t value = (int32_t)taosStr2Int32(buf, NULL, 10);
278
      colDataSetInt32(pOut->columnData, rowIndex, (int32_t *)&value);
D
fix bug  
dapan1121 已提交
279
      break;
G
Ganlin Zhao 已提交
280
    }
D
fix bug  
dapan1121 已提交
281
    case TSDB_DATA_TYPE_BIGINT: {
wafwerar's avatar
wafwerar 已提交
282
      int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
283
      colDataSetInt64(pOut->columnData, rowIndex, (int64_t *)&value);
D
fix bug  
dapan1121 已提交
284
      break;
G
Ganlin Zhao 已提交
285
    }
D
fix bug  
dapan1121 已提交
286
  }
D
dapan1121 已提交
287 288
}

H
Hongze Cheng 已提交
289
static FORCE_INLINE void varToUnsigned(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  if (overflow) {
    uint64_t minValue = (uint64_t)tDataTypes[pOut->columnData->info.type].minValue;
    uint64_t maxValue = (uint64_t)tDataTypes[pOut->columnData->info.type].maxValue;
    uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
    if (value > maxValue) {
      *overflow = 1;
      return;
    } else if (value < minValue) {
      *overflow = -1;
      return;
    } else {
      *overflow = 0;
    }
  }

D
fix bug  
dapan1121 已提交
305 306
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_UTINYINT: {
wafwerar's avatar
wafwerar 已提交
307
      uint8_t value = (uint8_t)taosStr2UInt8(buf, NULL, 10);
308
      colDataSetInt8(pOut->columnData, rowIndex, (int8_t *)&value);
D
fix bug  
dapan1121 已提交
309
      break;
G
Ganlin Zhao 已提交
310
    }
D
fix bug  
dapan1121 已提交
311
    case TSDB_DATA_TYPE_USMALLINT: {
wafwerar's avatar
wafwerar 已提交
312
      uint16_t value = (uint16_t)taosStr2UInt16(buf, NULL, 10);
313
      colDataSetInt16(pOut->columnData, rowIndex, (int16_t *)&value);
D
fix bug  
dapan1121 已提交
314
      break;
G
Ganlin Zhao 已提交
315
    }
D
fix bug  
dapan1121 已提交
316
    case TSDB_DATA_TYPE_UINT: {
wafwerar's avatar
wafwerar 已提交
317
      uint32_t value = (uint32_t)taosStr2UInt32(buf, NULL, 10);
318
      colDataSetInt32(pOut->columnData, rowIndex, (int32_t *)&value);
D
fix bug  
dapan1121 已提交
319
      break;
G
Ganlin Zhao 已提交
320
    }
D
fix bug  
dapan1121 已提交
321
    case TSDB_DATA_TYPE_UBIGINT: {
wafwerar's avatar
wafwerar 已提交
322
      uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
323
      colDataSetInt64(pOut->columnData, rowIndex, (int64_t *)&value);
D
fix bug  
dapan1121 已提交
324
      break;
G
Ganlin Zhao 已提交
325
    }
D
fix bug  
dapan1121 已提交
326
  }
D
dapan1121 已提交
327 328
}

H
Hongze Cheng 已提交
329
static FORCE_INLINE void varToFloat(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
330 331
  if (TSDB_DATA_TYPE_FLOAT == pOut->columnData->info.type) {
    float value = taosStr2Float(buf, NULL);
332
    colDataSetFloat(pOut->columnData, rowIndex, &value);
D
dapan1121 已提交
333 334
    return;
  }
G
Ganlin Zhao 已提交
335

wafwerar's avatar
wafwerar 已提交
336
  double value = taosStr2Double(buf, NULL);
337
  colDataSetDouble(pOut->columnData, rowIndex, &value);
338 339
}

H
Hongze Cheng 已提交
340
static FORCE_INLINE void varToBool(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
wafwerar's avatar
wafwerar 已提交
341
  int64_t value = taosStr2Int64(buf, NULL, 10);
H
Hongze Cheng 已提交
342
  bool    v = (value != 0) ? true : false;
343
  colDataSetInt8(pOut->columnData, rowIndex, (int8_t *)&v);
D
dapan1121 已提交
344 345
}

H
Haojun Liao 已提交
346
// todo remove this malloc
H
Hongze Cheng 已提交
347
static FORCE_INLINE void varToNchar(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
wmmhello's avatar
wmmhello 已提交
348 349
  int32_t len = 0;
  int32_t inputLen = varDataLen(buf);
D
fix bug  
dapan1121 已提交
350
  int32_t outputMaxLen = (inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
wmmhello's avatar
wmmhello 已提交
351

dengyihao's avatar
dengyihao 已提交
352 353 354
  char   *t = taosMemoryCalloc(1, outputMaxLen);
  int32_t ret =
      taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4 *)varDataVal(t), outputMaxLen - VARSTR_HEADER_SIZE, &len);
G
Ganlin Zhao 已提交
355
  if (!ret) {
dengyihao's avatar
dengyihao 已提交
356
    sclError("failed to convert to NCHAR");
G
Ganlin Zhao 已提交
357
  }
wmmhello's avatar
wmmhello 已提交
358 359
  varDataSetLen(t, len);

360
  colDataSetVal(pOut->columnData, rowIndex, t, false);
wmmhello's avatar
wmmhello 已提交
361 362
  taosMemoryFree(t);
}
363

H
Hongze Cheng 已提交
364
static FORCE_INLINE void ncharToVar(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
365 366
  int32_t inputLen = varDataLen(buf);

H
Hongze Cheng 已提交
367 368
  char   *t = taosMemoryCalloc(1, inputLen + VARSTR_HEADER_SIZE);
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t));
D
dapan1121 已提交
369 370 371 372 373 374
  if (len < 0) {
    taosMemoryFree(t);
    return;
  }
  varDataSetLen(t, len);

375
  colDataSetVal(pOut->columnData, rowIndex, t, false);
D
dapan1121 已提交
376 377 378
  taosMemoryFree(t);
}

dengyihao's avatar
dengyihao 已提交
379 380
// TODO opt performance, tmp is not needed.
int32_t vectorConvertFromVarData(SSclVectorConvCtx *pCtx, int32_t *overflow) {
wmmhello's avatar
wmmhello 已提交
381
  bool vton = false;
382 383

  _bufConverteFunc func = NULL;
D
dapan1121 已提交
384
  if (TSDB_DATA_TYPE_BOOL == pCtx->outType) {
385
    func = varToBool;
D
dapan1121 已提交
386
  } else if (IS_SIGNED_NUMERIC_TYPE(pCtx->outType)) {
387
    func = varToSigned;
D
dapan1121 已提交
388
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->outType)) {
389
    func = varToUnsigned;
D
dapan1121 已提交
390
  } else if (IS_FLOAT_TYPE(pCtx->outType)) {
391
    func = varToFloat;
G
Ganlin Zhao 已提交
392 393
  } else if (pCtx->outType == TSDB_DATA_TYPE_VARCHAR &&
             pCtx->inType == TSDB_DATA_TYPE_NCHAR) {  // nchar -> binary
D
dapan1121 已提交
394 395
    func = ncharToVar;
    vton = true;
G
Ganlin Zhao 已提交
396 397
  } else if (pCtx->outType == TSDB_DATA_TYPE_NCHAR &&
             pCtx->inType == TSDB_DATA_TYPE_VARCHAR) {  // binary -> nchar
wmmhello's avatar
wmmhello 已提交
398 399
    func = varToNchar;
    vton = true;
D
dapan1121 已提交
400
  } else if (TSDB_DATA_TYPE_TIMESTAMP == pCtx->outType) {
D
dapan1121 已提交
401
    func = varToTimestamp;
402
  } else {
G
Ganlin Zhao 已提交
403
    sclError("invalid convert outType:%d, inType:%d", pCtx->outType, pCtx->inType);
S
Shengliang Guan 已提交
404
    return TSDB_CODE_APP_ERROR;
405 406
  }

D
dapan1121 已提交
407
  pCtx->pOut->numOfRows = pCtx->pIn->numOfRows;
H
Haojun Liao 已提交
408 409
  char* tmp = NULL;

D
dapan1121 已提交
410 411
  for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
    if (IS_HELPER_NULL(pCtx->pIn->columnData, i)) {
412
      colDataSetNULL(pCtx->pOut->columnData, i);
413 414 415
      continue;
    }

dengyihao's avatar
dengyihao 已提交
416
    char   *data = colDataGetVarData(pCtx->pIn->columnData, i);
D
dapan1121 已提交
417
    int32_t convertType = pCtx->inType;
dengyihao's avatar
dengyihao 已提交
418
    if (pCtx->inType == TSDB_DATA_TYPE_JSON) {
G
Ganlin Zhao 已提交
419
      if (*data == TSDB_DATA_TYPE_NCHAR) {
420 421
        data += CHAR_BYTES;
        convertType = TSDB_DATA_TYPE_NCHAR;
G
Ganlin Zhao 已提交
422
      } else if (tTagIsJson(data) || *data == TSDB_DATA_TYPE_NULL) {
wmmhello's avatar
wmmhello 已提交
423 424
        terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
        return terrno;
425
      } else {
dengyihao's avatar
dengyihao 已提交
426
        convertNumberToNumber(data + CHAR_BYTES, colDataGetNumData(pCtx->pOut->columnData, i), *data, pCtx->outType);
427 428 429
        continue;
      }
    }
H
Haojun Liao 已提交
430

D
dapan1121 已提交
431
    int32_t bufSize = pCtx->pIn->columnData->info.bytes;
H
Haojun Liao 已提交
432 433 434 435 436 437
    if (tmp == NULL) {
      tmp = taosMemoryMalloc(bufSize);
      if (tmp == NULL) {
        sclError("out of memory in vectorConvertFromVarData");
        return TSDB_CODE_OUT_OF_MEMORY;
      }
438
    }
H
Haojun Liao 已提交
439

wmmhello's avatar
wmmhello 已提交
440 441 442
    if (vton) {
      memcpy(tmp, data, varDataTLen(data));
    } else {
443 444 445
      if (TSDB_DATA_TYPE_VARCHAR == convertType) {
        memcpy(tmp, varDataVal(data), varDataLen(data));
        tmp[varDataLen(data)] = 0;
H
Hongze Cheng 已提交
446
      } else if (TSDB_DATA_TYPE_NCHAR == convertType) {
H
Haojun Liao 已提交
447
        // we need to convert it to native char string, and then perform the string to numeric data
G
Ganlin Zhao 已提交
448 449 450 451 452
        if (varDataLen(data) > bufSize) {
          sclError("castConvert convert buffer size too small");
          taosMemoryFreeClear(tmp);
          return TSDB_CODE_APP_ERROR;
        }
453 454 455 456 457

        int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(data), varDataLen(data), tmp);
        if (len < 0) {
          sclError("castConvert taosUcs4ToMbs error 1");
          taosMemoryFreeClear(tmp);
S
Shengliang Guan 已提交
458
          return TSDB_CODE_APP_ERROR;
459 460 461 462
        }

        tmp[len] = 0;
      }
wmmhello's avatar
wmmhello 已提交
463
    }
dengyihao's avatar
dengyihao 已提交
464

D
dapan1121 已提交
465
    (*func)(tmp, pCtx->pOut, i, overflow);
466
  }
G
Ganlin Zhao 已提交
467

H
Haojun Liao 已提交
468 469 470
  if (tmp != NULL) {
    taosMemoryFreeClear(tmp);
  }
471 472 473
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
474 475
double getVectorDoubleValue_JSON(void *src, int32_t index) {
  char  *data = colDataGetVarData((SColumnInfoData *)src, index);
476
  double out = 0;
H
Hongze Cheng 已提交
477
  if (*data == TSDB_DATA_TYPE_NULL) {
478
    return out;
H
Hongze Cheng 已提交
479 480 481
  } else if (*data == TSDB_DATA_TYPE_NCHAR) {  // json inner type can not be BINARY
    convertNcharToDouble(data + CHAR_BYTES, &out);
  } else if (tTagIsJson(data)) {
wmmhello's avatar
wmmhello 已提交
482 483
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
    return 0;
H
Hongze Cheng 已提交
484 485
  } else {
    convertNumberToNumber(data + CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
486 487 488 489
  }
  return out;
}

H
Hongze Cheng 已提交
490
void *ncharTobinary(void *buf) {  // todo need to remove , if tobinary is nchar
wmmhello's avatar
wmmhello 已提交
491
  int32_t inputLen = varDataTLen(buf);
492

H
Hongze Cheng 已提交
493 494
  void   *t = taosMemoryCalloc(1, inputLen);
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t));
495 496
  if (len < 0) {
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed.", DEFAULT_UNICODE_ENCODEC, tsCharset,
H
Hongze Cheng 已提交
497
             (char *)varDataVal(buf));
498 499 500 501 502 503 504
    taosMemoryFree(t);
    return NULL;
  }
  varDataSetLen(t, len);
  return t;
}

H
Hongze Cheng 已提交
505 506 507
bool convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData,
                      char **pRightData, void *pLeftOut, void *pRightOut, bool *isNull, bool *freeLeft,
                      bool *freeRight) {
G
Ganlin Zhao 已提交
508
  if (optr == OP_TYPE_JSON_CONTAINS) {
wmmhello's avatar
wmmhello 已提交
509
    return true;
510 511
  }

G
Ganlin Zhao 已提交
512
  if (typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
513
    return true;
514 515
  }

G
Ganlin Zhao 已提交
516 517
  if (typeLeft == TSDB_DATA_TYPE_JSON) {
    if (tTagIsJson(*pLeftData)) {
wmmhello's avatar
wmmhello 已提交
518 519 520
      terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
      return false;
    }
521
    typeLeft = **pLeftData;
H
Hongze Cheng 已提交
522
    (*pLeftData)++;
523
  }
G
Ganlin Zhao 已提交
524
  if (typeRight == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
525
    if (tTagIsJson(*pRightData)) {
wmmhello's avatar
wmmhello 已提交
526 527 528
      terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
      return false;
    }
529
    typeRight = **pRightData;
H
Hongze Cheng 已提交
530
    (*pRightData)++;
531
  }
wmmhello's avatar
wmmhello 已提交
532

G
Ganlin Zhao 已提交
533 534
  if (optr == OP_TYPE_LIKE || optr == OP_TYPE_NOT_LIKE || optr == OP_TYPE_MATCH || optr == OP_TYPE_NMATCH) {
    if (typeLeft != TSDB_DATA_TYPE_NCHAR && typeLeft != TSDB_DATA_TYPE_BINARY) {
wmmhello's avatar
wmmhello 已提交
535 536 537 538
      return false;
    }
  }

wmmhello's avatar
wmmhello 已提交
539
  // if types can not comparable
G
Ganlin Zhao 已提交
540 541 542 543 544 545
  if ((IS_NUMERIC_TYPE(typeLeft) && !IS_NUMERIC_TYPE(typeRight)) ||
      (IS_NUMERIC_TYPE(typeRight) && !IS_NUMERIC_TYPE(typeLeft)) ||
      (IS_VAR_DATA_TYPE(typeLeft) && !IS_VAR_DATA_TYPE(typeRight)) ||
      (IS_VAR_DATA_TYPE(typeRight) && !IS_VAR_DATA_TYPE(typeLeft)) ||
      ((typeLeft == TSDB_DATA_TYPE_BOOL) && (typeRight != TSDB_DATA_TYPE_BOOL)) ||
      ((typeRight == TSDB_DATA_TYPE_BOOL) && (typeLeft != TSDB_DATA_TYPE_BOOL)))
wmmhello's avatar
wmmhello 已提交
546 547
    return false;

G
Ganlin Zhao 已提交
548
  if (typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL) {
549
    *isNull = true;
wmmhello's avatar
wmmhello 已提交
550
    return true;
551
  }
552 553
  int8_t type = vectorGetConvertType(typeLeft, typeRight);

G
Ganlin Zhao 已提交
554
  if (type == 0) {
555
    *fp = filterGetCompFunc(typeLeft, optr);
wmmhello's avatar
wmmhello 已提交
556
    return true;
557 558 559 560
  }

  *fp = filterGetCompFunc(type, optr);

G
Ganlin Zhao 已提交
561
  if (IS_NUMERIC_TYPE(type)) {
G
Ganlin Zhao 已提交
562 563 564
    if (typeLeft == TSDB_DATA_TYPE_NCHAR ||
        typeLeft == TSDB_DATA_TYPE_VARCHAR) {
      return false;
G
Ganlin Zhao 已提交
565
    } else if (typeLeft != type) {
566 567 568 569
      convertNumberToNumber(*pLeftData, pLeftOut, typeLeft, type);
      *pLeftData = pLeftOut;
    }

G
Ganlin Zhao 已提交
570 571 572
    if (typeRight == TSDB_DATA_TYPE_NCHAR ||
        typeRight == TSDB_DATA_TYPE_VARCHAR) {
      return false;
G
Ganlin Zhao 已提交
573
    } else if (typeRight != type) {
574 575 576
      convertNumberToNumber(*pRightData, pRightOut, typeRight, type);
      *pRightData = pRightOut;
    }
G
Ganlin Zhao 已提交
577 578
  } else if (type == TSDB_DATA_TYPE_BINARY) {
    if (typeLeft == TSDB_DATA_TYPE_NCHAR) {
579 580 581
      *pLeftData = ncharTobinary(*pLeftData);
      *freeLeft = true;
    }
G
Ganlin Zhao 已提交
582
    if (typeRight == TSDB_DATA_TYPE_NCHAR) {
583 584 585
      *pRightData = ncharTobinary(*pRightData);
      *freeRight = true;
    }
G
Ganlin Zhao 已提交
586
  } else {
G
Ganlin Zhao 已提交
587
    return false;
588 589
  }

wmmhello's avatar
wmmhello 已提交
590
  return true;
591 592
}

D
dapan1121 已提交
593
int32_t vectorConvertToVarData(SSclVectorConvCtx *pCtx) {
dengyihao's avatar
dengyihao 已提交
594 595 596
  SColumnInfoData *pInputCol = pCtx->pIn->columnData;
  SColumnInfoData *pOutputCol = pCtx->pOut->columnData;
  char             tmp[128] = {0};
D
fix bug  
dapan1121 已提交
597

dengyihao's avatar
dengyihao 已提交
598 599
  if (IS_SIGNED_NUMERIC_TYPE(pCtx->inType) || pCtx->inType == TSDB_DATA_TYPE_BOOL ||
      pCtx->inType == TSDB_DATA_TYPE_TIMESTAMP) {
D
dapan1121 已提交
600
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
D
fix bug  
dapan1121 已提交
601
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
602
        colDataSetNULL(pOutputCol, i);
D
fix bug  
dapan1121 已提交
603 604
        continue;
      }
G
Ganlin Zhao 已提交
605

D
fix bug  
dapan1121 已提交
606
      int64_t value = 0;
D
dapan1121 已提交
607
      GET_TYPED_DATA(value, int64_t, pCtx->inType, colDataGetData(pInputCol, i));
D
fix bug  
dapan1121 已提交
608 609
      int32_t len = sprintf(varDataVal(tmp), "%" PRId64, value);
      varDataLen(tmp) = len;
D
dapan1121 已提交
610 611
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pCtx->pOut, i, NULL);
D
fix bug  
dapan1121 已提交
612
      } else {
613
        colDataSetVal(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
614 615
      }
    }
D
dapan1121 已提交
616 617
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inType)) {
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
D
fix bug  
dapan1121 已提交
618
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
619
        colDataSetNULL(pOutputCol, i);
D
fix bug  
dapan1121 已提交
620 621
        continue;
      }
G
Ganlin Zhao 已提交
622

D
fix bug  
dapan1121 已提交
623
      uint64_t value = 0;
D
dapan1121 已提交
624
      GET_TYPED_DATA(value, uint64_t, pCtx->inType, colDataGetData(pInputCol, i));
D
fix bug  
dapan1121 已提交
625 626
      int32_t len = sprintf(varDataVal(tmp), "%" PRIu64, value);
      varDataLen(tmp) = len;
D
dapan1121 已提交
627 628
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pCtx->pOut, i, NULL);
D
fix bug  
dapan1121 已提交
629
      } else {
630
        colDataSetVal(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
631 632
      }
    }
D
dapan1121 已提交
633 634
  } else if (IS_FLOAT_TYPE(pCtx->inType)) {
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
D
fix bug  
dapan1121 已提交
635
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
636
        colDataSetNULL(pOutputCol, i);
D
fix bug  
dapan1121 已提交
637 638
        continue;
      }
G
Ganlin Zhao 已提交
639

D
fix bug  
dapan1121 已提交
640
      double value = 0;
D
dapan1121 已提交
641
      GET_TYPED_DATA(value, double, pCtx->inType, colDataGetData(pInputCol, i));
D
fix bug  
dapan1121 已提交
642 643
      int32_t len = sprintf(varDataVal(tmp), "%lf", value);
      varDataLen(tmp) = len;
D
dapan1121 已提交
644 645
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pCtx->pOut, i, NULL);
D
fix bug  
dapan1121 已提交
646
      } else {
647
        colDataSetVal(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
648 649 650
      }
    }
  } else {
D
dapan1121 已提交
651
    sclError("not supported input type:%d", pCtx->inType);
S
Shengliang Guan 已提交
652
    return TSDB_CODE_APP_ERROR;
D
fix bug  
dapan1121 已提交
653 654 655 656 657
  }

  return TSDB_CODE_SUCCESS;
}

658
// TODO opt performance
dengyihao's avatar
dengyihao 已提交
659 660 661 662
int32_t vectorConvertSingleColImpl(const SScalarParam *pIn, SScalarParam *pOut, int32_t *overflow, int32_t startIndex,
                                   int32_t numOfRows) {
  SColumnInfoData *pInputCol = pIn->columnData;
  SColumnInfoData *pOutputCol = pOut->columnData;
H
Haojun Liao 已提交
663

D
dapan1121 已提交
664 665 666 667
  if (NULL == pInputCol) {
    sclError("input column is NULL, hashFilter %p", pIn->pHashFilter);
    return TSDB_CODE_APP_ERROR;
  }
D
dapan1121 已提交
668

dengyihao's avatar
dengyihao 已提交
669 670
  int32_t           rstart = (startIndex >= 0 && startIndex < pIn->numOfRows) ? startIndex : 0;
  int32_t           rend = numOfRows > 0 ? rstart + numOfRows - 1 : rstart + pIn->numOfRows - 1;
D
dapan1121 已提交
671 672 673 674
  SSclVectorConvCtx cCtx = {pIn, pOut, rstart, rend, pInputCol->info.type, pOutputCol->info.type};

  if (IS_VAR_DATA_TYPE(cCtx.inType)) {
    return vectorConvertFromVarData(&cCtx, overflow);
D
dapan1121 已提交
675 676 677
  }

  if (overflow) {
G
Ganlin Zhao 已提交
678 679 680 681
    if (1 != pIn->numOfRows) {
      sclError("invalid numOfRows %d", pIn->numOfRows);
      return TSDB_CODE_APP_ERROR;
    }
D
dapan1121 已提交
682 683

    pOut->numOfRows = 0;
dengyihao's avatar
dengyihao 已提交
684

D
dapan1121 已提交
685 686 687
    if (IS_SIGNED_NUMERIC_TYPE(cCtx.outType)) {
      int64_t minValue = tDataTypes[cCtx.outType].minValue;
      int64_t maxValue = tDataTypes[cCtx.outType].maxValue;
dengyihao's avatar
dengyihao 已提交
688

D
dapan1121 已提交
689
      double value = 0;
D
dapan1121 已提交
690
      GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, 0));
dengyihao's avatar
dengyihao 已提交
691

D
dapan1121 已提交
692 693 694 695 696 697 698 699 700
      if (value > maxValue) {
        *overflow = 1;
        return TSDB_CODE_SUCCESS;
      } else if (value < minValue) {
        *overflow = -1;
        return TSDB_CODE_SUCCESS;
      } else {
        *overflow = 0;
      }
D
dapan1121 已提交
701 702 703
    } else if (IS_UNSIGNED_NUMERIC_TYPE(cCtx.outType)) {
      uint64_t minValue = (uint64_t)tDataTypes[cCtx.outType].minValue;
      uint64_t maxValue = (uint64_t)tDataTypes[cCtx.outType].maxValue;
dengyihao's avatar
dengyihao 已提交
704

D
dapan1121 已提交
705
      double value = 0;
D
dapan1121 已提交
706
      GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, 0));
dengyihao's avatar
dengyihao 已提交
707

D
dapan1121 已提交
708 709 710 711 712 713 714 715 716 717
      if (value > maxValue) {
        *overflow = 1;
        return TSDB_CODE_SUCCESS;
      } else if (value < minValue) {
        *overflow = -1;
        return TSDB_CODE_SUCCESS;
      } else {
        *overflow = 0;
      }
    }
D
dapan 已提交
718
  }
719 720

  pOut->numOfRows = pIn->numOfRows;
D
dapan1121 已提交
721
  switch (cCtx.outType) {
722
    case TSDB_DATA_TYPE_BOOL: {
D
dapan1121 已提交
723
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
724
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
725
          colDataSetNULL(pOutputCol, i);
726 727 728 729
          continue;
        }

        bool value = 0;
D
dapan1121 已提交
730
        GET_TYPED_DATA(value, bool, cCtx.inType, colDataGetData(pInputCol, i));
731
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
D
dapan1121 已提交
732 733 734 735
      }
      break;
    }
    case TSDB_DATA_TYPE_TINYINT: {
D
dapan1121 已提交
736
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
737
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
738
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
739 740 741 742
          continue;
        }

        int8_t value = 0;
D
dapan1121 已提交
743
        GET_TYPED_DATA(value, int8_t, cCtx.inType, colDataGetData(pInputCol, i));
744
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
D
dapan1121 已提交
745 746 747
      }
      break;
    }
dengyihao's avatar
dengyihao 已提交
748
    case TSDB_DATA_TYPE_SMALLINT: {
D
dapan1121 已提交
749
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
750
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
751
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
752 753 754 755
          continue;
        }

        int16_t value = 0;
D
dapan1121 已提交
756
        GET_TYPED_DATA(value, int16_t, cCtx.inType, colDataGetData(pInputCol, i));
757
        colDataSetInt16(pOutputCol, i, (int16_t *)&value);
D
dapan1121 已提交
758 759 760
      }
      break;
    }
dengyihao's avatar
dengyihao 已提交
761
    case TSDB_DATA_TYPE_INT: {
D
dapan1121 已提交
762
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
763
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
764
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
765 766 767 768
          continue;
        }

        int32_t value = 0;
D
dapan1121 已提交
769
        GET_TYPED_DATA(value, int32_t, cCtx.inType, colDataGetData(pInputCol, i));
770
        colDataSetInt32(pOutputCol, i, (int32_t *)&value);
771 772 773
      }
      break;
    }
D
dapan1121 已提交
774
    case TSDB_DATA_TYPE_BIGINT:
775
    case TSDB_DATA_TYPE_TIMESTAMP: {
D
dapan1121 已提交
776
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
777
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
778
          colDataSetNULL(pOutputCol, i);
D
dapan 已提交
779
          continue;
D
dapan1121 已提交
780
        }
D
dapan 已提交
781 782

        int64_t value = 0;
D
dapan1121 已提交
783
        GET_TYPED_DATA(value, int64_t, cCtx.inType, colDataGetData(pInputCol, i));
784
        colDataSetInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
785 786
      }
      break;
787
    }
dengyihao's avatar
dengyihao 已提交
788
    case TSDB_DATA_TYPE_UTINYINT: {
D
dapan1121 已提交
789
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
790
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
791
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
792 793
          continue;
        }
G
Ganlin Zhao 已提交
794

D
dapan1121 已提交
795
        uint8_t value = 0;
D
dapan1121 已提交
796
        GET_TYPED_DATA(value, uint8_t, cCtx.inType, colDataGetData(pInputCol, i));
797
        colDataSetInt8(pOutputCol, i, (int8_t *)&value);
D
dapan1121 已提交
798 799 800
      }
      break;
    }
dengyihao's avatar
dengyihao 已提交
801
    case TSDB_DATA_TYPE_USMALLINT: {
D
dapan1121 已提交
802
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
803
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
804
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
805 806
          continue;
        }
G
Ganlin Zhao 已提交
807

D
dapan1121 已提交
808
        uint16_t value = 0;
D
dapan1121 已提交
809
        GET_TYPED_DATA(value, uint16_t, cCtx.inType, colDataGetData(pInputCol, i));
810
        colDataSetInt16(pOutputCol, i, (int16_t *)&value);
D
dapan1121 已提交
811 812 813
      }
      break;
    }
dengyihao's avatar
dengyihao 已提交
814
    case TSDB_DATA_TYPE_UINT: {
D
dapan1121 已提交
815
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
816
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
817
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
818 819
          continue;
        }
G
Ganlin Zhao 已提交
820

D
dapan1121 已提交
821
        uint32_t value = 0;
D
dapan1121 已提交
822
        GET_TYPED_DATA(value, uint32_t, cCtx.inType, colDataGetData(pInputCol, i));
823
        colDataSetInt32(pOutputCol, i, (int32_t *)&value);
D
dapan1121 已提交
824 825 826 827
      }
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
D
dapan1121 已提交
828
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
829
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
830
          colDataSetNULL(pOutputCol, i);
D
dapan 已提交
831
          continue;
D
dapan1121 已提交
832
        }
G
Ganlin Zhao 已提交
833

D
dapan 已提交
834
        uint64_t value = 0;
D
dapan1121 已提交
835
        GET_TYPED_DATA(value, uint64_t, cCtx.inType, colDataGetData(pInputCol, i));
836
        colDataSetInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
837 838
      }
      break;
D
dapan1121 已提交
839
    }
dengyihao's avatar
dengyihao 已提交
840
    case TSDB_DATA_TYPE_FLOAT: {
D
dapan1121 已提交
841
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
842
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
843
          colDataSetNULL(pOutputCol, i);
D
dapan1121 已提交
844 845
          continue;
        }
G
Ganlin Zhao 已提交
846

D
dapan1121 已提交
847
        float value = 0;
D
dapan1121 已提交
848
        GET_TYPED_DATA(value, float, cCtx.inType, colDataGetData(pInputCol, i));
849
        colDataSetFloat(pOutputCol, i, (float *)&value);
D
dapan1121 已提交
850
      }
G
Ganlin Zhao 已提交
851
      break;
D
dapan1121 已提交
852 853
    }
    case TSDB_DATA_TYPE_DOUBLE: {
D
dapan1121 已提交
854
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
855
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
856
          colDataSetNULL(pOutputCol, i);
D
dapan 已提交
857
          continue;
D
dapan1121 已提交
858
        }
G
Ganlin Zhao 已提交
859

D
dapan 已提交
860
        double value = 0;
D
dapan1121 已提交
861
        GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, i));
862
        colDataSetDouble(pOutputCol, i, (double *)&value);
D
dapan1121 已提交
863
      }
G
Ganlin Zhao 已提交
864
      break;
D
dapan1121 已提交
865
    }
G
Ganlin Zhao 已提交
866
    case TSDB_DATA_TYPE_BINARY:
D
fix bug  
dapan1121 已提交
867
    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
868
      return vectorConvertToVarData(&cCtx);
D
fix bug  
dapan1121 已提交
869
    }
D
dapan1121 已提交
870
    default:
D
dapan1121 已提交
871
      sclError("invalid convert output type:%d", cCtx.outType);
S
Shengliang Guan 已提交
872
      return TSDB_CODE_APP_ERROR;
D
dapan1121 已提交
873 874 875 876 877
  }

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
878 879
int8_t gConvertTypes[TSDB_DATA_TYPE_BLOB + 1][TSDB_DATA_TYPE_BLOB + 1] = {
    /*         NULL BOOL TINY SMAL INT  BIG  FLOA DOUB VARC TIME NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB */
dengyihao's avatar
dengyihao 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    /*NULL*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0, 0, 0, 0,
    /*BOOL*/ 0, 0, 2, 3, 4, 5, 6, 7, 5, 9, 7, 11, 12, 13, 14, 0, 7, 0, 0,
    /*TINY*/ 0, 0, 0, 3, 4, 5, 6, 7, 5, 9, 7, 3,  4,  5,  7,  0, 7, 0, 0,
    /*SMAL*/ 0, 0, 0, 0, 4, 5, 6, 7, 5, 9, 7, 3,  4,  5,  7,  0, 7, 0, 0,
    /*INT */ 0, 0, 0, 0, 0, 5, 6, 7, 5, 9, 7, 4,  4,  5,  7,  0, 7, 0, 0,
    /*BIGI*/ 0, 0, 0, 0, 0, 0, 6, 7, 5, 9, 7, 5,  5,  5,  7,  0, 7, 0, 0,
    /*FLOA*/ 0, 0, 0, 0, 0, 0, 0, 7, 7, 6, 7, 6,  6,  6,  6,  0, 7, 0, 0,
    /*DOUB*/ 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7,  7,  7,  7,  0, 7, 0, 0,
    /*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 8, 7,  7,  7,  7,  0, 0, 0, 0,
    /*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9,  9,  9,  7,  0, 7, 0, 0,
    /*NCHA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,  7,  7,  7,  0, 0, 0, 0,
    /*UTIN*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  12, 13, 14, 0, 7, 0, 0,
    /*USMA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  13, 14, 0, 7, 0, 0,
    /*UINT*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  14, 0, 7, 0, 0,
    /*UBIG*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0, 7, 0, 0,
    /*JSON*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0, 0, 0, 0,
    /*VARB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0, 0, 0, 0,
    /*DECI*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0, 0, 0, 0,
    /*BLOB*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0, 0, 0, 0};
D
dapan1121 已提交
899

D
dapan1121 已提交
900 901 902 903 904 905 906 907 908 909 910 911
int32_t vectorGetConvertType(int32_t type1, int32_t type2) {
  if (type1 == type2) {
    return 0;
  }

  if (type1 < type2) {
    return gConvertTypes[type1][type2];
  }

  return gConvertTypes[type2][type1];
}

dengyihao's avatar
dengyihao 已提交
912 913
int32_t vectorConvertSingleCol(SScalarParam *input, SScalarParam *output, int32_t type, int32_t startIndex,
                               int32_t numOfRows) {
D
dapan1121 已提交
914 915
  output->numOfRows = input->numOfRows;

H
Haojun Liao 已提交
916 917
  SDataType t = {.type = type};
  t.bytes = IS_VAR_DATA_TYPE(t.type)? input->columnData->info.bytes:tDataTypes[type].bytes;
5
54liuyao 已提交
918
  t.precision = input->columnData->info.precision;
H
Haojun Liao 已提交
919

H
Haojun Liao 已提交
920 921
  int32_t code = sclCreateColumnInfoData(&t, input->numOfRows, output);
  if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
922
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
923 924
  }

D
dapan1121 已提交
925
  code = vectorConvertSingleColImpl(input, output, NULL, startIndex, numOfRows);
D
dapan1121 已提交
926 927 928 929 930 931 932
  if (code) {
    return code;
  }

  return TSDB_CODE_SUCCESS;
}

dengyihao's avatar
dengyihao 已提交
933 934 935
int32_t vectorConvertCols(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pLeftOut, SScalarParam *pRightOut,
                          int32_t startIndex, int32_t numOfRows) {
  int32_t leftType = GET_PARAM_TYPE(pLeft);
936 937
  int32_t rightType = GET_PARAM_TYPE(pRight);
  if (leftType == rightType) {
D
dapan1121 已提交
938 939 940
    return TSDB_CODE_SUCCESS;
  }

H
Haojun Liao 已提交
941 942 943
  int8_t  type = 0;
  int32_t code = 0;

G
Ganlin Zhao 已提交
944
  SScalarParam *param1 = NULL, *paramOut1 = NULL;
D
dapan1121 已提交
945
  SScalarParam *param2 = NULL, *paramOut2 = NULL;
G
Ganlin Zhao 已提交
946

H
Haojun Liao 已提交
947 948 949
  // always convert least data
  if (IS_VAR_DATA_TYPE(leftType) && IS_VAR_DATA_TYPE(rightType) && (pLeft->numOfRows != pRight->numOfRows) &&
      leftType != TSDB_DATA_TYPE_JSON && rightType != TSDB_DATA_TYPE_JSON) {
D
dapan1121 已提交
950 951 952 953
    param1 = pLeft;
    param2 = pRight;
    paramOut1 = pLeftOut;
    paramOut2 = pRightOut;
H
Haojun Liao 已提交
954 955 956 957 958 959

    if (pLeft->numOfRows > pRight->numOfRows) {
      type = leftType;
    } else {
      type = rightType;
    }
D
dapan1121 已提交
960
  } else {
H
Haojun Liao 已提交
961 962 963 964 965 966 967 968 969 970 971 972
    // we only define half value in the convert-matrix, so make sure param1 always less equal than param2
    if (leftType < rightType) {
      param1 = pLeft;
      param2 = pRight;
      paramOut1 = pLeftOut;
      paramOut2 = pRightOut;
    } else {
      param1 = pRight;
      param2 = pLeft;
      paramOut1 = pRightOut;
      paramOut2 = pLeftOut;
    }
D
dapan1121 已提交
973

H
Haojun Liao 已提交
974 975 976 977
    type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
    if (0 == type) {
      return TSDB_CODE_SUCCESS;
    }
D
dapan1121 已提交
978 979
  }

980
  if (type != GET_PARAM_TYPE(param1)) {
D
dapan1121 已提交
981
    code = vectorConvertSingleCol(param1, paramOut1, type, startIndex, numOfRows);
D
dapan1121 已提交
982 983 984
    if (code) {
      return code;
    }
D
dapan1121 已提交
985
  }
G
Ganlin Zhao 已提交
986

987
  if (type != GET_PARAM_TYPE(param2)) {
D
dapan1121 已提交
988
    code = vectorConvertSingleCol(param2, paramOut2, type, startIndex, numOfRows);
D
dapan1121 已提交
989 990 991
    if (code) {
      return code;
    }
D
dapan1121 已提交
992 993 994 995 996
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
997 998 999 1000 1001
enum {
  VECTOR_DO_CONVERT = 0x1,
  VECTOR_UN_CONVERT = 0x2,
};

1002
// TODO not correct for descending order scan
H
Hongze Cheng 已提交
1003 1004 1005
static void vectorMathAddHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                                int32_t numOfRows, int32_t step, int32_t i) {
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1006 1007 1008 1009
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;

wmmhello's avatar
wmmhello 已提交
1010
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1011
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1012 1013
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1014
      if (IS_HELPER_NULL(pLeftCol, i)) {
1015
        colDataSetNULL(pOutputCol, i);
1016 1017
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1018
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) + getVectorDoubleValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
1019 1020
    }
  }
1021
}
D
dapan1121 已提交
1022

H
Hongze Cheng 已提交
1023 1024 1025
static void vectorMathTsAddHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                                  int32_t numOfRows, int32_t step, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
1026 1027 1028 1029
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

  int64_t *output = (int64_t *)pOutputCol->pData;

wmmhello's avatar
wmmhello 已提交
1030
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1031
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1032 1033
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1034
      if (IS_HELPER_NULL(pLeftCol, i)) {
1035
        colDataSetNULL(pOutputCol, i);
1036 1037
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1038 1039 1040
      *output =
          taosTimeAdd(getVectorBigintValueFnLeft(pLeftCol->pData, i), getVectorBigintValueFnRight(pRightCol->pData, 0),
                      pRightCol->info.scale, pRightCol->info.precision);
1041 1042 1043 1044
    }
  }
}

dengyihao's avatar
dengyihao 已提交
1045 1046 1047
static SColumnInfoData *vectorConvertVarToDouble(SScalarParam *pInput, int32_t *converted) {
  SScalarParam     output = {0};
  SColumnInfoData *pCol = pInput->columnData;
H
Haojun Liao 已提交
1048

D
dapan1121 已提交
1049 1050 1051 1052 1053 1054
  if (IS_VAR_DATA_TYPE(pCol->info.type) && pCol->info.type != TSDB_DATA_TYPE_JSON) {
    int32_t code = vectorConvertSingleCol(pInput, &output, TSDB_DATA_TYPE_DOUBLE, -1, -1);
    if (code != TSDB_CODE_SUCCESS) {
      terrno = code;
      return NULL;
    }
H
Haojun Liao 已提交
1055

D
dapan1121 已提交
1056 1057 1058
    *converted = VECTOR_DO_CONVERT;

    return output.columnData;
H
Haojun Liao 已提交
1059
  }
D
dapan1121 已提交
1060 1061

  *converted = VECTOR_UN_CONVERT;
dengyihao's avatar
dengyihao 已提交
1062

D
dapan1121 已提交
1063
  return pInput->columnData;
H
Haojun Liao 已提交
1064 1065
}

H
Hongze Cheng 已提交
1066
static void doReleaseVec(SColumnInfoData *pCol, int32_t type) {
H
Haojun Liao 已提交
1067 1068
  if (type == VECTOR_DO_CONVERT) {
    colDataDestroy(pCol);
H
Haojun Liao 已提交
1069
    taosMemoryFree(pCol);
H
Haojun Liao 已提交
1070 1071 1072
  }
}

H
Hongze Cheng 已提交
1073
void vectorMathAdd(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1074 1075
  SColumnInfoData *pOutputCol = pOut->columnData;

H
Hongze Cheng 已提交
1076 1077
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1078

1079 1080
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);

dengyihao's avatar
dengyihao 已提交
1081 1082 1083
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1084

1085 1086 1087
  if ((GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP && IS_INTEGER_TYPE(GET_PARAM_TYPE(pRight))) ||
      (GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_TIMESTAMP && IS_INTEGER_TYPE(GET_PARAM_TYPE(pLeft))) ||
      (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP && GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_BOOL) ||
H
Hongze Cheng 已提交
1088 1089 1090 1091
      (GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_TIMESTAMP &&
       GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_BOOL)) {  // timestamp plus duration
    int64_t             *output = (int64_t *)pOutputCol->pData;
    _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
1092
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
1093

D
dapan1121 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
      if (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP) {
        vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pRight->numOfRows, step, i);
      } else {
        vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
      }
    } else if (pLeft->numOfRows == 1) {
      vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
    } else if (pLeft->numOfRows == pRight->numOfRows) {
1105
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1106
        if (IS_NULL) {
1107
          colDataSetNULL(pOutputCol, i);
wmmhello's avatar
wmmhello 已提交
1108 1109
          continue;  // TODO set null or ignore
        }
1110
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, i);
1111
      }
G
Ganlin Zhao 已提交
1112
    }
1113
  } else {
H
Hongze Cheng 已提交
1114 1115
    double              *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1116
    _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1117

1118 1119
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
H
Hongze Cheng 已提交
1120
        if (IS_NULL) {
1121
          colDataSetNULL(pOutputCol, i);
1122 1123 1124
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) + getVectorDoubleValueFnRight(RIGHT_COL, i);
1125 1126 1127 1128 1129 1130
      }
    } else if (pLeft->numOfRows == 1) {
      vectorMathAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
    }
1131
  }
H
Haojun Liao 已提交
1132 1133 1134

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1135
}
D
dapan1121 已提交
1136

1137
// TODO not correct for descending order scan
H
Hongze Cheng 已提交
1138 1139 1140
static void vectorMathSubHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                                int32_t numOfRows, int32_t step, int32_t factor, int32_t i) {
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1141 1142 1143 1144
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;

wmmhello's avatar
wmmhello 已提交
1145
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1146
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1147 1148
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1149
      if (IS_HELPER_NULL(pLeftCol, i)) {
1150
        colDataSetNULL(pOutputCol, i);
1151 1152
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1153
      *output = (getVectorDoubleValueFnLeft(LEFT_COL, i) - getVectorDoubleValueFnRight(RIGHT_COL, 0)) * factor;
D
dapan1121 已提交
1154
    }
1155 1156
  }
}
D
dapan1121 已提交
1157

H
Hongze Cheng 已提交
1158 1159 1160
static void vectorMathTsSubHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                                  int32_t numOfRows, int32_t step, int32_t factor, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
1161 1162 1163 1164
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

  int64_t *output = (int64_t *)pOutputCol->pData;

wmmhello's avatar
wmmhello 已提交
1165
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1166
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1167 1168
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1169
      if (IS_HELPER_NULL(pLeftCol, i)) {
1170
        colDataSetNULL(pOutputCol, i);
1171 1172
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1173 1174 1175
      *output =
          taosTimeAdd(getVectorBigintValueFnLeft(pLeftCol->pData, i), -getVectorBigintValueFnRight(pRightCol->pData, 0),
                      pRightCol->info.scale, pRightCol->info.precision);
1176 1177 1178 1179
    }
  }
}

H
Hongze Cheng 已提交
1180
void vectorMathSub(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1181
  SColumnInfoData *pOutputCol = pOut->columnData;
D
dapan1121 已提交
1182

1183 1184
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);

H
Hongze Cheng 已提交
1185 1186
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1187

dengyihao's avatar
dengyihao 已提交
1188 1189 1190
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1191

1192
  if ((GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP && GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_BIGINT) ||
H
Hongze Cheng 已提交
1193 1194 1195 1196
      (GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_TIMESTAMP &&
       GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_BIGINT)) {  // timestamp minus duration
    int64_t             *output = (int64_t *)pOutputCol->pData;
    _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
1197
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
1198

D
dapan1121 已提交
1199 1200 1201 1202 1203 1204 1205
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
      vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
    } else if (pLeft->numOfRows == 1) {
      vectorMathTsSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
    } else if (pLeft->numOfRows == pRight->numOfRows) {
1206
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1207
        if (IS_NULL) {
1208
          colDataSetNULL(pOutputCol, i);
wmmhello's avatar
wmmhello 已提交
1209 1210
          continue;  // TODO set null or ignore
        }
1211
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, i);
1212
      }
D
dapan1121 已提交
1213
    }
1214
  } else {
H
Hongze Cheng 已提交
1215 1216
    double              *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1217 1218 1219 1220
    _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1221
        if (IS_NULL) {
1222
          colDataSetNULL(pOutputCol, i);
1223 1224 1225
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) - getVectorDoubleValueFnRight(RIGHT_COL, i);
1226 1227 1228 1229 1230 1231
      }
    } else if (pLeft->numOfRows == 1) {
      vectorMathSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
    }
1232
  }
H
Haojun Liao 已提交
1233

H
Hongze Cheng 已提交
1234
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1235
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1236 1237
}

1238
// TODO not correct for descending order scan
H
Hongze Cheng 已提交
1239 1240 1241
static void vectorMathMultiplyHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                                     int32_t numOfRows, int32_t step, int32_t i) {
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1242
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
D
dapan1121 已提交
1243

1244
  double *output = (double *)pOutputCol->pData;
D
dapan1121 已提交
1245

wmmhello's avatar
wmmhello 已提交
1246
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1247
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1248 1249
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1250
      if (IS_HELPER_NULL(pLeftCol, i)) {
1251
        colDataSetNULL(pOutputCol, i);
1252 1253
        continue;  // TODO set null or ignore
      }
1254
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, 0);
1255 1256
    }
  }
D
dapan1121 已提交
1257 1258
}

H
Hongze Cheng 已提交
1259
void vectorMathMultiply(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1260
  SColumnInfoData *pOutputCol = pOut->columnData;
1261
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1262

H
Hongze Cheng 已提交
1263 1264
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1265

dengyihao's avatar
dengyihao 已提交
1266 1267 1268
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1269

H
Hongze Cheng 已提交
1270
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1271 1272 1273 1274 1275
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1276
      if (IS_NULL) {
1277
        colDataSetNULL(pOutputCol, i);
1278 1279
        continue;  // TODO set null or ignore
      }
1280
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, i);
1281 1282 1283 1284 1285
    }
  } else if (pLeft->numOfRows == 1) {
    vectorMathMultiplyHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
  } else if (pRight->numOfRows == 1) {
    vectorMathMultiplyHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
D
dapan1121 已提交
1286
  }
H
Haojun Liao 已提交
1287 1288 1289

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1290 1291
}

H
Hongze Cheng 已提交
1292
void vectorMathDivide(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1293
  SColumnInfoData *pOutputCol = pOut->columnData;
1294
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1295

H
Hongze Cheng 已提交
1296 1297
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1298

dengyihao's avatar
dengyihao 已提交
1299 1300
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
D
dapan1121 已提交
1301
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1302

H
Hongze Cheng 已提交
1303
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1304 1305 1306
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;
1307
  if (pLeft->numOfRows == pRight->numOfRows) {
1308
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
H
Hongze Cheng 已提交
1309
      if (IS_NULL || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) {  // divide by 0 check
1310
        colDataSetNULL(pOutputCol, i);
1311
        continue;
1312
      }
H
Hongze Cheng 已提交
1313
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) / getVectorDoubleValueFnRight(RIGHT_COL, i);
1314 1315
    }
  } else if (pLeft->numOfRows == 1) {
wmmhello's avatar
wmmhello 已提交
1316
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1317
      colDataSetNNULL(pOutputCol, 0, pRight->numOfRows);
1318 1319
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
H
Hongze Cheng 已提交
1320
        if (IS_HELPER_NULL(pRightCol, i) || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) {  // divide by 0 check
1321
          colDataSetNULL(pOutputCol, i);
1322
          continue;
1323
        }
H
Hongze Cheng 已提交
1324
        *output = getVectorDoubleValueFnLeft(LEFT_COL, 0) / getVectorDoubleValueFnRight(RIGHT_COL, i);
1325 1326 1327
      }
    }
  } else if (pRight->numOfRows == 1) {
H
Hongze Cheng 已提交
1328 1329
    if (IS_HELPER_NULL(pRightCol, 0) ||
        (getVectorDoubleValueFnRight(RIGHT_COL, 0) == 0)) {  // Set pLeft->numOfRows NULL value (divde by 0 check)
1330
      colDataSetNNULL(pOutputCol, 0, pLeft->numOfRows);
1331 1332
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1333
        if (IS_HELPER_NULL(pLeftCol, i)) {
1334
          colDataSetNULL(pOutputCol, i);
1335
          continue;
1336
        }
H
Hongze Cheng 已提交
1337
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) / getVectorDoubleValueFnRight(RIGHT_COL, 0);
1338 1339 1340
      }
    }
  }
H
Haojun Liao 已提交
1341

H
Hongze Cheng 已提交
1342
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1343
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1344 1345
}

H
Hongze Cheng 已提交
1346
void vectorMathRemainder(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1347
  SColumnInfoData *pOutputCol = pOut->columnData;
1348
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1349

H
Hongze Cheng 已提交
1350 1351
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1352

dengyihao's avatar
dengyihao 已提交
1353 1354
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
D
dapan1121 已提交
1355
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1356

H
Hongze Cheng 已提交
1357
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1358
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1359 1360 1361 1362 1363

  double *output = (double *)pOutputCol->pData;

  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1364
      if (IS_NULL) {
1365
        colDataSetNULL(pOutputCol, i);
1366 1367 1368
        continue;
      }

1369 1370 1371
      double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
      double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
      if (isnan(lx) || isinf(lx) || isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
1372
        colDataSetNULL(pOutputCol, i);
1373 1374 1375
        continue;
      }

1376
      *output = lx - ((int64_t)(lx / rx)) * rx;
1377 1378
    }
  } else if (pLeft->numOfRows == 1) {
1379
    double lx = getVectorDoubleValueFnLeft(LEFT_COL, 0);
wmmhello's avatar
wmmhello 已提交
1380
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1381
      colDataSetNNULL(pOutputCol, 0, pRight->numOfRows);
1382 1383
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1384
        if (IS_HELPER_NULL(pRightCol, i)) {
1385
          colDataSetNULL(pOutputCol, i);
1386 1387 1388
          continue;
        }

1389 1390
        double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
        if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
1391
          colDataSetNULL(pOutputCol, i);
1392 1393 1394
          continue;
        }

1395
        *output = lx - ((int64_t)(lx / rx)) * rx;
1396 1397 1398
      }
    }
  } else if (pRight->numOfRows == 1) {
1399 1400
    double rx = getVectorDoubleValueFnRight(RIGHT_COL, 0);
    if (IS_HELPER_NULL(pRightCol, 0) || FLT_EQUAL(rx, 0)) {  // Set pLeft->numOfRows NULL value
1401
      colDataSetNNULL(pOutputCol, 0, pLeft->numOfRows);
1402 1403
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1404
        if (IS_HELPER_NULL(pLeftCol, i)) {
1405
          colDataSetNULL(pOutputCol, i);
1406 1407 1408
          continue;
        }

1409 1410
        double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
        if (isnan(lx) || isinf(lx)) {
1411
          colDataSetNULL(pOutputCol, i);
1412 1413 1414 1415
          continue;
        }

        *output = lx - ((int64_t)(lx / rx)) * rx;
1416 1417 1418
      }
    }
  }
H
Haojun Liao 已提交
1419 1420 1421

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1422 1423
}

H
Hongze Cheng 已提交
1424
void vectorMathMinus(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1425 1426 1427 1428
  SColumnInfoData *pOutputCol = pOut->columnData;

  pOut->numOfRows = pLeft->numOfRows;

H
Hongze Cheng 已提交
1429 1430
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : (pLeft->numOfRows - 1);
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1431

dengyihao's avatar
dengyihao 已提交
1432 1433
  int32_t          leftConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
D
dapan1121 已提交
1434

H
Hongze Cheng 已提交
1435
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
D
dapan1121 已提交
1436 1437 1438

  double *output = (double *)pOutputCol->pData;
  for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1439
    if (IS_HELPER_NULL(pLeftCol, i)) {
1440
      colDataSetNULL(pOutputCol, i);
1441
      continue;
1442
    }
1443 1444
    double result = getVectorDoubleValueFnLeft(LEFT_COL, i);
    *output = (result == 0) ? 0 : -result;
D
dapan1121 已提交
1445 1446
  }

H
Hongze Cheng 已提交
1447
  doReleaseVec(pLeftCol, leftConvert);
D
dapan1121 已提交
1448 1449
}

H
Hongze Cheng 已提交
1450
void vectorAssign(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1451 1452 1453
  SColumnInfoData *pOutputCol = pOut->columnData;
  pOut->numOfRows = pLeft->numOfRows;

H
Hongze Cheng 已提交
1454
  if (colDataIsNull_s(pRight->columnData, 0)) {
1455
    colDataSetNNULL(pOutputCol, 0, pOut->numOfRows);
D
dapan1121 已提交
1456
  } else {
H
Hongze Cheng 已提交
1457
    char *d = colDataGetData(pRight->columnData, 0);
D
dapan1121 已提交
1458
    for (int32_t i = 0; i < pOut->numOfRows; ++i) {
1459
      colDataSetVal(pOutputCol, i, d, false);
D
dapan1121 已提交
1460 1461
    }
  }
1462 1463 1464

  ASSERT(pRight->numOfQualified == 1 || pRight->numOfQualified == 0);
  pOut->numOfQualified = pRight->numOfQualified * pOut->numOfRows;
D
dapan1121 已提交
1465 1466
}

H
Hongze Cheng 已提交
1467 1468 1469
static void vectorBitAndHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                               int32_t numOfRows, int32_t step, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
1470
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1471

wmmhello's avatar
wmmhello 已提交
1472
  int64_t *output = (int64_t *)pOutputCol->pData;
D
dapan1121 已提交
1473

wmmhello's avatar
wmmhello 已提交
1474
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1475
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1476 1477
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1478
      if (IS_HELPER_NULL(pLeftCol, i)) {
1479
        colDataSetNULL(pOutputCol, i);
1480 1481 1482
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
1483 1484
    }
  }
1485
}
D
dapan1121 已提交
1486

H
Hongze Cheng 已提交
1487
void vectorBitAnd(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1488
  SColumnInfoData *pOutputCol = pOut->columnData;
1489
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan 已提交
1490

1491 1492
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1493

dengyihao's avatar
dengyihao 已提交
1494 1495 1496
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
D
dapan1121 已提交
1497

H
Hongze Cheng 已提交
1498
  _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
H
Haojun Liao 已提交
1499
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1500

1501 1502 1503
  int64_t *output = (int64_t *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1504
      if (IS_NULL) {
1505
        colDataSetNULL(pOutputCol, i);
1506 1507 1508
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, i);
D
dapan1121 已提交
1509
    }
1510 1511 1512 1513 1514
  } else if (pLeft->numOfRows == 1) {
    vectorBitAndHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
  } else if (pRight->numOfRows == 1) {
    vectorBitAndHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
  }
H
Haojun Liao 已提交
1515

H
Hongze Cheng 已提交
1516
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1517
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1518 1519
}

H
Hongze Cheng 已提交
1520 1521 1522
static void vectorBitOrHelper(SColumnInfoData *pLeftCol, SColumnInfoData *pRightCol, SColumnInfoData *pOutputCol,
                              int32_t numOfRows, int32_t step, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
1523
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1524

1525
  int64_t *output = (int64_t *)pOutputCol->pData;
D
dapan1121 已提交
1526

wmmhello's avatar
wmmhello 已提交
1527
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1528
    colDataSetNNULL(pOutputCol, 0, numOfRows);
1529
  } else {
1530
    int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, 0);
1531
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1532
      if (IS_HELPER_NULL(pLeftCol, i)) {
1533
        colDataSetNULL(pOutputCol, i);
1534 1535 1536
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | rx;
D
dapan1121 已提交
1537 1538
    }
  }
1539
}
D
dapan1121 已提交
1540

H
Hongze Cheng 已提交
1541
void vectorBitOr(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1542
  SColumnInfoData *pOutputCol = pOut->columnData;
1543
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1544

1545 1546
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1547

dengyihao's avatar
dengyihao 已提交
1548 1549 1550
  int32_t          leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
D
dapan1121 已提交
1551

H
Hongze Cheng 已提交
1552
  _getBigintValue_fn_t getVectorBigintValueFnLeft = getVectorBigintValueFn(pLeftCol->info.type);
H
Haojun Liao 已提交
1553
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan 已提交
1554

1555 1556 1557
  int64_t *output = (int64_t *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1558
      if (IS_NULL) {
1559
        colDataSetNULL(pOutputCol, i);
1560 1561 1562
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | getVectorBigintValueFnRight(RIGHT_COL, i);
1563 1564 1565 1566 1567
    }
  } else if (pLeft->numOfRows == 1) {
    vectorBitOrHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
  } else if (pRight->numOfRows == 1) {
    vectorBitOrHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
D
dapan1121 已提交
1568
  }
H
Haojun Liao 已提交
1569

H
Hongze Cheng 已提交
1570
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1571
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1572 1573
}

dengyihao's avatar
dengyihao 已提交
1574 1575
int32_t doVectorCompareImpl(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
                            int32_t numOfRows, int32_t step, __compar_fn_t fp, int32_t optr) {
1576
  int32_t num = 0;
1577
  bool   *pRes = (bool *)pOut->columnData->pData;
1578

1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
  if (IS_MATHABLE_TYPE(GET_PARAM_TYPE(pLeft)) && IS_MATHABLE_TYPE(GET_PARAM_TYPE(pRight))) {
    if (!(pLeft->columnData->hasNull || pRight->columnData->hasNull)) {
      for (int32_t i = startIndex; i < numOfRows && i >= 0; i += step) {
        int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
        int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;

        char *pLeftData = colDataGetData(pLeft->columnData, leftIndex);
        char *pRightData = colDataGetData(pRight->columnData, rightIndex);

        pRes[i] = filterDoCompare(fp, optr, pLeftData, pRightData);
        if (pRes[i]) {
          ++num;
        }
      }
    } else {
      for (int32_t i = startIndex; i < numOfRows && i >= 0; i += step) {
        int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
        int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;

        if (colDataIsNull_f(pLeft->columnData->nullbitmap, leftIndex) ||
            colDataIsNull_f(pRight->columnData->nullbitmap, rightIndex)) {
          pRes[i] = false;
          continue;
        }

        char *pLeftData = colDataGetData(pLeft->columnData, leftIndex);
        char *pRightData = colDataGetData(pRight->columnData, rightIndex);

        pRes[i] = filterDoCompare(fp, optr, pLeftData, pRightData);
        if (pRes[i]) {
          ++num;
        }
      }
    }
  } else {
    //  if (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_JSON || GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_JSON) {
H
Haojun Liao 已提交
1615 1616 1617
    for (int32_t i = startIndex; i < numOfRows && i >= startIndex; i += step) {
      int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
      int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;
1618

H
Haojun Liao 已提交
1619 1620
      if (IS_HELPER_NULL(pLeft->columnData, leftIndex) || IS_HELPER_NULL(pRight->columnData, rightIndex)) {
        bool res = false;
1621
        colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
H
Haojun Liao 已提交
1622 1623
        continue;
      }
1624

1625 1626
      char   *pLeftData = colDataGetData(pLeft->columnData, leftIndex);
      char   *pRightData = colDataGetData(pRight->columnData, rightIndex);
H
Haojun Liao 已提交
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
      int64_t leftOut = 0;
      int64_t rightOut = 0;
      bool    freeLeft = false;
      bool    freeRight = false;
      bool    isJsonnull = false;

      bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData,
                                     &leftOut, &rightOut, &isJsonnull, &freeLeft, &freeRight);
      if (isJsonnull) {
        ASSERT(0);
      }
1638

H
Haojun Liao 已提交
1639 1640 1641
      if (!pLeftData || !pRightData) {
        result = false;
      }
1642

H
Haojun Liao 已提交
1643
      if (!result) {
1644
        colDataSetInt8(pOut->columnData, i, (int8_t *)&result);
H
Haojun Liao 已提交
1645 1646
      } else {
        bool res = filterDoCompare(fp, optr, pLeftData, pRightData);
1647
        colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
H
Haojun Liao 已提交
1648 1649 1650 1651
        if (res) {
          ++num;
        }
      }
1652

H
Haojun Liao 已提交
1653 1654
      if (freeLeft) {
        taosMemoryFreeClear(pLeftData);
1655 1656
      }

H
Haojun Liao 已提交
1657 1658 1659
      if (freeRight) {
        taosMemoryFreeClear(pRightData);
      }
1660
    }
1661 1662
  }

1663 1664 1665
  return num;
}

dengyihao's avatar
dengyihao 已提交
1666 1667
void doVectorCompare(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
                     int32_t numOfRows, int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1668
  int32_t       i = 0;
1669
  int32_t       step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1670 1671 1672
  int32_t       lType = GET_PARAM_TYPE(pLeft);
  int32_t       rType = GET_PARAM_TYPE(pRight);
  __compar_fn_t fp = NULL;
D
dapan1121 已提交
1673
  int32_t       compRows = 0;
dengyihao's avatar
dengyihao 已提交
1674

D
dapan1121 已提交
1675 1676 1677 1678
  if (lType == rType) {
    fp = filterGetCompFunc(lType, optr);
  } else {
    fp = filterGetCompFuncEx(lType, rType, optr);
wmmhello's avatar
wmmhello 已提交
1679
  }
1680

D
dapan1121 已提交
1681 1682
  if (startIndex < 0) {
    i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
D
dapan1121 已提交
1683
    pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1684 1685 1686 1687 1688 1689
    compRows = pOut->numOfRows;
  } else {
    compRows = startIndex + numOfRows;
    i = startIndex;
  }

1690 1691
  if (pRight->pHashFilter != NULL) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
wmmhello's avatar
wmmhello 已提交
1692
      if (IS_HELPER_NULL(pLeft->columnData, i)) {
H
Hongze Cheng 已提交
1693
        bool res = false;
1694
        colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
D
dapan1121 已提交
1695 1696 1697
        continue;
      }

1698 1699
      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter);
1700
      colDataSetInt8(pOut->columnData, i, (int8_t *)&res);
1701 1702 1703
      if (res) {
        pOut->numOfQualified++;
      }
1704
    }
1705
  } else {  // normal compare
D
dapan1121 已提交
1706
    pOut->numOfQualified = doVectorCompareImpl(pLeft, pRight, pOut, i, compRows, step, fp, optr);
D
dapan1121 已提交
1707 1708 1709
  }
}

dengyihao's avatar
dengyihao 已提交
1710 1711 1712 1713
void vectorCompareImpl(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t startIndex,
                       int32_t numOfRows, int32_t _ord, int32_t optr) {
  SScalarParam  pLeftOut = {0};
  SScalarParam  pRightOut = {0};
G
Ganlin Zhao 已提交
1714
  SScalarParam *param1 = NULL;
D
dapan1121 已提交
1715
  SScalarParam *param2 = NULL;
D
dapan1121 已提交
1716

H
Haojun Liao 已提交
1717
  if (noConvertBeforeCompare(GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), optr)) {
D
dapan1121 已提交
1718 1719
    param1 = pLeft;
    param2 = pRight;
D
dapan1121 已提交
1720
  } else {
D
dapan1121 已提交
1721
    vectorConvertCols(pLeft, pRight, &pLeftOut, &pRightOut, startIndex, numOfRows);
1722 1723
    param1 = (pLeftOut.columnData != NULL) ? &pLeftOut : pLeft;
    param2 = (pRightOut.columnData != NULL) ? &pRightOut : pRight;
D
dapan1121 已提交
1724 1725
  }

D
dapan1121 已提交
1726
  doVectorCompare(param1, param2, pOut, startIndex, numOfRows, _ord, optr);
dengyihao's avatar
dengyihao 已提交
1727

D
dapan1121 已提交
1728
  sclFreeParam(&pLeftOut);
G
Ganlin Zhao 已提交
1729
  sclFreeParam(&pRightOut);
D
dapan1121 已提交
1730 1731
}

dengyihao's avatar
dengyihao 已提交
1732
void vectorCompare(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1733 1734 1735
  vectorCompareImpl(pLeft, pRight, pOut, -1, -1, _ord, optr);
}

dengyihao's avatar
dengyihao 已提交
1736
void vectorGreater(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1737
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN);
D
dapan1121 已提交
1738 1739
}

H
Hongze Cheng 已提交
1740
void vectorGreaterEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1741
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL);
D
dapan1121 已提交
1742 1743
}

H
Hongze Cheng 已提交
1744
void vectorLower(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1745
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN);
D
dapan1121 已提交
1746 1747
}

H
Hongze Cheng 已提交
1748
void vectorLowerEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1749
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL);
D
dapan1121 已提交
1750 1751
}

H
Hongze Cheng 已提交
1752
void vectorEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1753
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL);
D
dapan1121 已提交
1754 1755
}

H
Hongze Cheng 已提交
1756
void vectorNotEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1757
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL);
D
dapan1121 已提交
1758 1759
}

H
Hongze Cheng 已提交
1760
void vectorIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1761
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN);
D
dapan1121 已提交
1762 1763
}

H
Hongze Cheng 已提交
1764
void vectorNotIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1765
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN);
D
dapan1121 已提交
1766 1767
}

H
Hongze Cheng 已提交
1768
void vectorLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1769
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE);
D
dapan1121 已提交
1770 1771
}

H
Hongze Cheng 已提交
1772
void vectorNotLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1773
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE);
D
dapan1121 已提交
1774 1775
}

H
Hongze Cheng 已提交
1776
void vectorMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1777
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH);
D
dapan1121 已提交
1778 1779
}

H
Hongze Cheng 已提交
1780
void vectorNotMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1781
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH);
D
dapan1121 已提交
1782 1783
}

H
Hongze Cheng 已提交
1784 1785
void vectorIsNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1786
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 1 : 0;
S
slzhou 已提交
1787 1788 1789
    if (v) {
      ++pOut->numOfQualified;
    }
1790
    colDataSetInt8(pOut->columnData, i, &v);
S
slzhou 已提交
1791
    colDataClearNull_f(pOut->columnData->nullbitmap, i);
D
dapan1121 已提交
1792
  }
1793
  pOut->numOfRows = pLeft->numOfRows;
D
dapan1121 已提交
1794 1795
}

H
Hongze Cheng 已提交
1796 1797
void vectorNotNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1798
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 0 : 1;
S
slzhou 已提交
1799 1800 1801
    if (v) {
      ++pOut->numOfQualified;
    }
1802
    colDataSetInt8(pOut->columnData, i, &v);
S
slzhou 已提交
1803
    colDataClearNull_f(pOut->columnData->nullbitmap, i);
D
dapan1121 已提交
1804
  }
1805
  pOut->numOfRows = pLeft->numOfRows;
D
dapan 已提交
1806
}
D
dapan1121 已提交
1807

dengyihao's avatar
dengyihao 已提交
1808
void vectorIsTrue(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1809
  vectorConvertSingleColImpl(pLeft, pOut, NULL, -1, -1);
dengyihao's avatar
dengyihao 已提交
1810 1811
  for (int32_t i = 0; i < pOut->numOfRows; ++i) {
    if (colDataIsNull_s(pOut->columnData, i)) {
1812
      int8_t v = 0;
1813
      colDataSetInt8(pOut->columnData, i, &v);
H
Haojun Liao 已提交
1814
      colDataClearNull_f(pOut->columnData->nullbitmap, i);
1815
    }
S
slzhou 已提交
1816 1817 1818 1819 1820 1821 1822
    {
      bool v = false;
      GET_TYPED_DATA(v, bool, pOut->columnData->info.type, colDataGetData(pOut->columnData, i));
      if (v) {
        ++pOut->numOfQualified;
      }
    }
1823 1824
  }
  pOut->columnData->hasNull = false;
D
dapan1121 已提交
1825 1826
}

1827 1828
STagVal getJsonValue(char *json, char *key, bool *isExist) {
  STagVal val = {.pKey = key};
dengyihao's avatar
dengyihao 已提交
1829
  if (json == NULL || tTagIsJson((const STag *)json) == false) {
wmmhello's avatar
wmmhello 已提交
1830
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
H
Hongze Cheng 已提交
1831
    if (isExist) {
wmmhello's avatar
wmmhello 已提交
1832 1833 1834 1835 1836
      *isExist = false;
    }
    return val;
  }

1837
  bool find = tTagGet(((const STag *)json), &val);  // json value is null and not exist is different
H
Hongze Cheng 已提交
1838
  if (isExist) {
1839 1840 1841 1842 1843
    *isExist = find;
  }
  return val;
}

H
Hongze Cheng 已提交
1844
void vectorJsonContains(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1845 1846
  SColumnInfoData *pOutputCol = pOut->columnData;

H
Hongze Cheng 已提交
1847 1848
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861

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

  char *pRightData = colDataGetVarData(pRight->columnData, 0);
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
  memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
    bool isExist = false;

    if (!colDataIsNull_var(pLeft->columnData, i)) {
      char *pLeftData = colDataGetVarData(pLeft->columnData, i);
      getJsonValue(pLeftData, jsonKey, &isExist);
    }
S
slzhou 已提交
1862 1863 1864
    if (isExist) {
      ++pOut->numOfQualified;
    }
1865
    colDataSetVal(pOutputCol, i, (const char *)(&isExist), false);
1866 1867 1868 1869
  }
  taosMemoryFree(jsonKey);
}

H
Hongze Cheng 已提交
1870
void vectorJsonArrow(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1871 1872
  SColumnInfoData *pOutputCol = pOut->columnData;

H
Hongze Cheng 已提交
1873 1874
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886

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

  char *pRightData = colDataGetVarData(pRight->columnData, 0);
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
  memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
    if (colDataIsNull_var(pLeft->columnData, i)) {
      colDataSetNull_var(pOutputCol, i);
      pOutputCol->hasNull = true;
      continue;
    }
H
Hongze Cheng 已提交
1887 1888
    char   *pLeftData = colDataGetVarData(pLeft->columnData, i);
    bool    isExist = false;
1889
    STagVal value = getJsonValue(pLeftData, jsonKey, &isExist);
H
Hongze Cheng 已提交
1890
    char   *data = isExist ? tTagValToData(&value, true) : NULL;
1891
    colDataSetVal(pOutputCol, i, data, data == NULL);
H
Hongze Cheng 已提交
1892
    if (isExist && IS_VAR_DATA_TYPE(value.type) && data) {
1893 1894 1895 1896 1897 1898
      taosMemoryFree(data);
    }
  }
  taosMemoryFree(jsonKey);
}

1899 1900
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
  switch (binFunctionId) {
D
dapan1121 已提交
1901
    case OP_TYPE_ADD:
1902
      return vectorMathAdd;
D
dapan1121 已提交
1903
    case OP_TYPE_SUB:
1904
      return vectorMathSub;
D
dapan1121 已提交
1905
    case OP_TYPE_MULTI:
1906
      return vectorMathMultiply;
D
dapan1121 已提交
1907
    case OP_TYPE_DIV:
1908
      return vectorMathDivide;
1909
    case OP_TYPE_REM:
1910
      return vectorMathRemainder;
D
dapan1121 已提交
1911 1912
    case OP_TYPE_MINUS:
      return vectorMathMinus;
D
dapan1121 已提交
1913 1914
    case OP_TYPE_ASSIGN:
      return vectorAssign;
D
dapan1121 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
    case OP_TYPE_GREATER_THAN:
      return vectorGreater;
    case OP_TYPE_GREATER_EQUAL:
      return vectorGreaterEqual;
    case OP_TYPE_LOWER_THAN:
      return vectorLower;
    case OP_TYPE_LOWER_EQUAL:
      return vectorLowerEqual;
    case OP_TYPE_EQUAL:
      return vectorEqual;
    case OP_TYPE_NOT_EQUAL:
      return vectorNotEqual;
    case OP_TYPE_IN:
      return vectorIn;
    case OP_TYPE_NOT_IN:
      return vectorNotIn;
    case OP_TYPE_LIKE:
      return vectorLike;
    case OP_TYPE_NOT_LIKE:
      return vectorNotLike;
    case OP_TYPE_MATCH:
      return vectorMatch;
    case OP_TYPE_NMATCH:
      return vectorNotMatch;
D
dapan1121 已提交
1939
    case OP_TYPE_IS_NULL:
D
dapan1121 已提交
1940
      return vectorIsNull;
D
dapan1121 已提交
1941
    case OP_TYPE_IS_NOT_NULL:
D
dapan1121 已提交
1942 1943 1944 1945 1946
      return vectorNotNull;
    case OP_TYPE_BIT_AND:
      return vectorBitAnd;
    case OP_TYPE_BIT_OR:
      return vectorBitOr;
D
dapan1121 已提交
1947 1948
    case OP_TYPE_IS_TRUE:
      return vectorIsTrue;
1949 1950 1951 1952
    case OP_TYPE_JSON_GET_VALUE:
      return vectorJsonArrow;
    case OP_TYPE_JSON_CONTAINS:
      return vectorJsonContains;
1953 1954 1955
    default:
      return NULL;
  }
D
dapan1121 已提交
1956
}