sclvector.c 68.4 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
Hongze Cheng 已提交
40
void convertNumberToNumber(const void *inData, void *outData, int8_t inType, int8_t outType) {
41 42 43 44 45 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
  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 已提交
87
    default: {
88 89 90 91 92
      ASSERT(0);
    }
  }
}

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

  tmp[len] = 0;

wafwerar's avatar
wafwerar 已提交
102
  double value = taosStr2Double(tmp, NULL);
103 104 105 106 107

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

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

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

H
Hongze Cheng 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
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);
137
  double out = 0;
H
Hongze Cheng 已提交
138
  if (*data == TSDB_DATA_TYPE_NULL) {
139
    return 0;
H
Hongze Cheng 已提交
140 141 142
  } 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 已提交
143 144
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
    return 0;
145
  } else {
H
Hongze Cheng 已提交
146
    convertNumberToNumber(data + CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
147 148 149 150
  }
  return (int64_t)out;
}

D
dapan1121 已提交
151
_getBigintValue_fn_t getVectorBigintValueFn(int32_t srcType) {
H
Hongze Cheng 已提交
152 153 154 155 156 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
  _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 已提交
185 186
}

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

H
Hongze Cheng 已提交
189 190 191 192 193 194 195 196 197 198 199 200
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); }
201 202

_getValueAddr_fn_t getVectorValueAddrFn(int32_t srcType) {
H
Hongze Cheng 已提交
203 204 205 206 207 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
  _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 已提交
235 236 237 238
  int64_t value = 0;
  if (taosParseTime(buf, &value, strlen(buf), pOut->columnData->info.precision, tsDaylight) != TSDB_CODE_SUCCESS) {
    value = 0;
  }
G
Ganlin Zhao 已提交
239

D
dapan1121 已提交
240 241 242
  colDataAppendInt64(pOut->columnData, rowIndex, &value);
}

H
Hongze Cheng 已提交
243
static FORCE_INLINE void varToSigned(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257
  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 已提交
258

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

H
Hongze Cheng 已提交
263
      colDataAppendInt8(pOut->columnData, rowIndex, (int8_t *)&value);
D
fix bug  
dapan1121 已提交
264
      break;
G
Ganlin Zhao 已提交
265
    }
D
fix bug  
dapan1121 已提交
266
    case TSDB_DATA_TYPE_SMALLINT: {
wafwerar's avatar
wafwerar 已提交
267
      int16_t value = (int16_t)taosStr2Int16(buf, NULL, 10);
H
Hongze Cheng 已提交
268
      colDataAppendInt16(pOut->columnData, rowIndex, (int16_t *)&value);
D
fix bug  
dapan1121 已提交
269
      break;
G
Ganlin Zhao 已提交
270
    }
D
fix bug  
dapan1121 已提交
271
    case TSDB_DATA_TYPE_INT: {
wafwerar's avatar
wafwerar 已提交
272
      int32_t value = (int32_t)taosStr2Int32(buf, NULL, 10);
H
Hongze Cheng 已提交
273
      colDataAppendInt32(pOut->columnData, rowIndex, (int32_t *)&value);
D
fix bug  
dapan1121 已提交
274
      break;
G
Ganlin Zhao 已提交
275
    }
D
fix bug  
dapan1121 已提交
276
    case TSDB_DATA_TYPE_BIGINT: {
wafwerar's avatar
wafwerar 已提交
277
      int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
H
Hongze Cheng 已提交
278
      colDataAppendInt64(pOut->columnData, rowIndex, (int64_t *)&value);
D
fix bug  
dapan1121 已提交
279
      break;
G
Ganlin Zhao 已提交
280
    }
D
fix bug  
dapan1121 已提交
281
  }
D
dapan1121 已提交
282 283
}

H
Hongze Cheng 已提交
284
static FORCE_INLINE void varToUnsigned(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  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 已提交
300 301
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_UTINYINT: {
wafwerar's avatar
wafwerar 已提交
302
      uint8_t value = (uint8_t)taosStr2UInt8(buf, NULL, 10);
H
Hongze Cheng 已提交
303
      colDataAppendInt8(pOut->columnData, rowIndex, (int8_t *)&value);
D
fix bug  
dapan1121 已提交
304
      break;
G
Ganlin Zhao 已提交
305
    }
D
fix bug  
dapan1121 已提交
306
    case TSDB_DATA_TYPE_USMALLINT: {
wafwerar's avatar
wafwerar 已提交
307
      uint16_t value = (uint16_t)taosStr2UInt16(buf, NULL, 10);
H
Hongze Cheng 已提交
308
      colDataAppendInt16(pOut->columnData, rowIndex, (int16_t *)&value);
D
fix bug  
dapan1121 已提交
309
      break;
G
Ganlin Zhao 已提交
310
    }
D
fix bug  
dapan1121 已提交
311
    case TSDB_DATA_TYPE_UINT: {
wafwerar's avatar
wafwerar 已提交
312
      uint32_t value = (uint32_t)taosStr2UInt32(buf, NULL, 10);
H
Hongze Cheng 已提交
313
      colDataAppendInt32(pOut->columnData, rowIndex, (int32_t *)&value);
D
fix bug  
dapan1121 已提交
314
      break;
G
Ganlin Zhao 已提交
315
    }
D
fix bug  
dapan1121 已提交
316
    case TSDB_DATA_TYPE_UBIGINT: {
wafwerar's avatar
wafwerar 已提交
317
      uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
H
Hongze Cheng 已提交
318
      colDataAppendInt64(pOut->columnData, rowIndex, (int64_t *)&value);
D
fix bug  
dapan1121 已提交
319
      break;
G
Ganlin Zhao 已提交
320
    }
D
fix bug  
dapan1121 已提交
321
  }
D
dapan1121 已提交
322 323
}

H
Hongze Cheng 已提交
324
static FORCE_INLINE void varToFloat(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
D
dapan1121 已提交
325 326 327 328 329
  if (TSDB_DATA_TYPE_FLOAT == pOut->columnData->info.type) {
    float value = taosStr2Float(buf, NULL);
    colDataAppendFloat(pOut->columnData, rowIndex, &value);
    return;
  }
G
Ganlin Zhao 已提交
330

wafwerar's avatar
wafwerar 已提交
331
  double value = taosStr2Double(buf, NULL);
332
  colDataAppendDouble(pOut->columnData, rowIndex, &value);
333 334
}

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

H
Hongze Cheng 已提交
341
static FORCE_INLINE void varToNchar(char *buf, SScalarParam *pOut, int32_t rowIndex, int32_t *overflow) {
wmmhello's avatar
wmmhello 已提交
342 343
  int32_t len = 0;
  int32_t inputLen = varDataLen(buf);
D
fix bug  
dapan1121 已提交
344
  int32_t outputMaxLen = (inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
wmmhello's avatar
wmmhello 已提交
345

H
Hongze Cheng 已提交
346 347 348
  char *t = taosMemoryCalloc(1, outputMaxLen);
  /*int32_t resLen = */ taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4 *)varDataVal(t),
                                      outputMaxLen - VARSTR_HEADER_SIZE, &len);
wmmhello's avatar
wmmhello 已提交
349 350 351 352 353
  varDataSetLen(t, len);

  colDataAppend(pOut->columnData, rowIndex, t, false);
  taosMemoryFree(t);
}
354

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

H
Hongze Cheng 已提交
358 359
  char   *t = taosMemoryCalloc(1, inputLen + VARSTR_HEADER_SIZE);
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t));
D
dapan1121 已提交
360 361 362 363 364 365 366 367 368 369
  if (len < 0) {
    taosMemoryFree(t);
    return;
  }
  varDataSetLen(t, len);

  colDataAppend(pOut->columnData, rowIndex, t, false);
  taosMemoryFree(t);
}

370
//TODO opt performance, tmp is not needed.
D
dapan1121 已提交
371
int32_t vectorConvertFromVarData(SSclVectorConvCtx *pCtx, int32_t* overflow) {
wmmhello's avatar
wmmhello 已提交
372
  bool vton = false;
373 374

  _bufConverteFunc func = NULL;
D
dapan1121 已提交
375
  if (TSDB_DATA_TYPE_BOOL == pCtx->outType) {
376
    func = varToBool;
D
dapan1121 已提交
377
  } else if (IS_SIGNED_NUMERIC_TYPE(pCtx->outType)) {
378
    func = varToSigned;
D
dapan1121 已提交
379
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->outType)) {
380
    func = varToUnsigned;
D
dapan1121 已提交
381
  } else if (IS_FLOAT_TYPE(pCtx->outType)) {
382
    func = varToFloat;
D
dapan1121 已提交
383 384
  } else if (pCtx->outType == TSDB_DATA_TYPE_BINARY) {   // nchar -> binary
    ASSERT(pCtx->inType == TSDB_DATA_TYPE_NCHAR);
D
dapan1121 已提交
385 386
    func = ncharToVar;
    vton = true;
D
dapan1121 已提交
387 388
  } else if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {   // binary -> nchar
    ASSERT(pCtx->inType == TSDB_DATA_TYPE_VARCHAR);
wmmhello's avatar
wmmhello 已提交
389 390
    func = varToNchar;
    vton = true;
D
dapan1121 已提交
391
  } else if (TSDB_DATA_TYPE_TIMESTAMP == pCtx->outType) {
D
dapan1121 已提交
392
    func = varToTimestamp;
393
  } else {
D
dapan1121 已提交
394
    sclError("invalid convert outType:%d", pCtx->outType);
395 396 397
    return TSDB_CODE_QRY_APP_ERROR;
  }

D
dapan1121 已提交
398 399 400 401
  pCtx->pOut->numOfRows = pCtx->pIn->numOfRows;
  for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
    if (IS_HELPER_NULL(pCtx->pIn->columnData, i)) {
      colDataAppendNULL(pCtx->pOut->columnData, i);
402 403 404
      continue;
    }

D
dapan1121 已提交
405 406 407
    char* data = colDataGetVarData(pCtx->pIn->columnData, i);
    int32_t convertType = pCtx->inType;
    if(pCtx->inType == TSDB_DATA_TYPE_JSON){
408
      if(*data == TSDB_DATA_TYPE_NULL) {
wmmhello's avatar
wmmhello 已提交
409
        ASSERT(0);
H
Hongze Cheng 已提交
410
      } else if (*data == TSDB_DATA_TYPE_NCHAR) {
411 412
        data += CHAR_BYTES;
        convertType = TSDB_DATA_TYPE_NCHAR;
H
Hongze Cheng 已提交
413
      } else if (tTagIsJson(data)) {
wmmhello's avatar
wmmhello 已提交
414 415
        terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
        return terrno;
416
      } else {
D
dapan1121 已提交
417
        convertNumberToNumber(data+CHAR_BYTES, colDataGetNumData(pCtx->pOut->columnData, i), *data, pCtx->outType);
418 419 420
        continue;
      }
    }
D
dapan1121 已提交
421
    int32_t bufSize = pCtx->pIn->columnData->info.bytes;
422 423 424 425 426
    char *tmp = taosMemoryMalloc(varDataTLen(data));
    if(!tmp){
      sclError("out of memory in vectorConvertFromVarData");
      return TSDB_CODE_OUT_OF_MEMORY;
    }
wmmhello's avatar
wmmhello 已提交
427 428 429
    if (vton) {
      memcpy(tmp, data, varDataTLen(data));
    } else {
430 431 432
      if (TSDB_DATA_TYPE_VARCHAR == convertType) {
        memcpy(tmp, varDataVal(data), varDataLen(data));
        tmp[varDataLen(data)] = 0;
H
Hongze Cheng 已提交
433
      } else if (TSDB_DATA_TYPE_NCHAR == convertType) {
434 435 436 437 438 439 440 441 442 443 444
        ASSERT(varDataLen(data) <= bufSize);

        int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(data), varDataLen(data), tmp);
        if (len < 0) {
          sclError("castConvert taosUcs4ToMbs error 1");
          taosMemoryFreeClear(tmp);
          return TSDB_CODE_QRY_APP_ERROR;
        }

        tmp[len] = 0;
      }
wmmhello's avatar
wmmhello 已提交
445
    }
446
    
D
dapan1121 已提交
447
    (*func)(tmp, pCtx->pOut, i, overflow);
448
    taosMemoryFreeClear(tmp);
449
  }
G
Ganlin Zhao 已提交
450

451 452 453
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
454 455
double getVectorDoubleValue_JSON(void *src, int32_t index) {
  char  *data = colDataGetVarData((SColumnInfoData *)src, index);
456
  double out = 0;
H
Hongze Cheng 已提交
457
  if (*data == TSDB_DATA_TYPE_NULL) {
458
    return out;
H
Hongze Cheng 已提交
459 460 461
  } 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 已提交
462 463
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
    return 0;
H
Hongze Cheng 已提交
464 465
  } else {
    convertNumberToNumber(data + CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
466 467 468 469
  }
  return out;
}

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

H
Hongze Cheng 已提交
473 474
  void   *t = taosMemoryCalloc(1, inputLen);
  int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t));
475 476
  if (len < 0) {
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed.", DEFAULT_UNICODE_ENCODEC, tsCharset,
H
Hongze Cheng 已提交
477
             (char *)varDataVal(buf));
478 479 480 481 482 483 484
    taosMemoryFree(t);
    return NULL;
  }
  varDataSetLen(t, len);
  return t;
}

H
Hongze Cheng 已提交
485 486 487
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 已提交
488
  if (optr == OP_TYPE_JSON_CONTAINS) {
wmmhello's avatar
wmmhello 已提交
489
    return true;
490 491
  }

G
Ganlin Zhao 已提交
492
  if (typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
493
    return true;
494 495
  }

G
Ganlin Zhao 已提交
496 497
  if (typeLeft == TSDB_DATA_TYPE_JSON) {
    if (tTagIsJson(*pLeftData)) {
wmmhello's avatar
wmmhello 已提交
498 499 500
      terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
      return false;
    }
501
    typeLeft = **pLeftData;
H
Hongze Cheng 已提交
502
    (*pLeftData)++;
503
  }
G
Ganlin Zhao 已提交
504 505
  if (typeRight == TSDB_DATA_TYPE_JSON) {
    if (tTagIsJson(*pLeftData)) {
wmmhello's avatar
wmmhello 已提交
506 507 508
      terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
      return false;
    }
509
    typeRight = **pRightData;
H
Hongze Cheng 已提交
510
    (*pRightData)++;
511
  }
wmmhello's avatar
wmmhello 已提交
512

G
Ganlin Zhao 已提交
513 514
  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 已提交
515 516 517 518
      return false;
    }
  }

wmmhello's avatar
wmmhello 已提交
519
  // if types can not comparable
G
Ganlin Zhao 已提交
520 521 522 523 524 525
  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 已提交
526 527
    return false;

G
Ganlin Zhao 已提交
528
  if (typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL) {
529
    *isNull = true;
wmmhello's avatar
wmmhello 已提交
530
    return true;
531
  }
532 533
  int8_t type = vectorGetConvertType(typeLeft, typeRight);

G
Ganlin Zhao 已提交
534
  if (type == 0) {
535
    *fp = filterGetCompFunc(typeLeft, optr);
wmmhello's avatar
wmmhello 已提交
536
    return true;
537 538 539 540
  }

  *fp = filterGetCompFunc(type, optr);

G
Ganlin Zhao 已提交
541 542
  if (IS_NUMERIC_TYPE(type)) {
    if (typeLeft == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
543
      ASSERT(0);
H
Hongze Cheng 已提交
544 545
      //      convertNcharToDouble(*pLeftData, pLeftOut);
      //      *pLeftData = pLeftOut;
G
Ganlin Zhao 已提交
546
    } else if (typeLeft == TSDB_DATA_TYPE_BINARY) {
wmmhello's avatar
wmmhello 已提交
547
      ASSERT(0);
H
Hongze Cheng 已提交
548 549
      //      convertBinaryToDouble(*pLeftData, pLeftOut);
      //      *pLeftData = pLeftOut;
G
Ganlin Zhao 已提交
550
    } else if (typeLeft != type) {
551 552 553 554
      convertNumberToNumber(*pLeftData, pLeftOut, typeLeft, type);
      *pLeftData = pLeftOut;
    }

G
Ganlin Zhao 已提交
555
    if (typeRight == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
556
      ASSERT(0);
H
Hongze Cheng 已提交
557 558
      //      convertNcharToDouble(*pRightData, pRightOut);
      //      *pRightData = pRightOut;
G
Ganlin Zhao 已提交
559
    } else if (typeRight == TSDB_DATA_TYPE_BINARY) {
wmmhello's avatar
wmmhello 已提交
560
      ASSERT(0);
H
Hongze Cheng 已提交
561 562
      //      convertBinaryToDouble(*pRightData, pRightOut);
      //      *pRightData = pRightOut;
G
Ganlin Zhao 已提交
563
    } else if (typeRight != type) {
564 565 566
      convertNumberToNumber(*pRightData, pRightOut, typeRight, type);
      *pRightData = pRightOut;
    }
G
Ganlin Zhao 已提交
567 568
  } else if (type == TSDB_DATA_TYPE_BINARY) {
    if (typeLeft == TSDB_DATA_TYPE_NCHAR) {
569 570 571
      *pLeftData = ncharTobinary(*pLeftData);
      *freeLeft = true;
    }
G
Ganlin Zhao 已提交
572
    if (typeRight == TSDB_DATA_TYPE_NCHAR) {
573 574 575
      *pRightData = ncharTobinary(*pRightData);
      *freeRight = true;
    }
G
Ganlin Zhao 已提交
576
  } else {
577
    ASSERT(0);
578 579
  }

wmmhello's avatar
wmmhello 已提交
580
  return true;
581 582
}

D
dapan1121 已提交
583 584 585
int32_t vectorConvertToVarData(SSclVectorConvCtx *pCtx) {
  SColumnInfoData* pInputCol  = pCtx->pIn->columnData;
  SColumnInfoData* pOutputCol = pCtx->pOut->columnData;
D
fix bug  
dapan1121 已提交
586 587
  char tmp[128] = {0};

D
dapan1121 已提交
588 589
  if (IS_SIGNED_NUMERIC_TYPE(pCtx->inType) || pCtx->inType == TSDB_DATA_TYPE_BOOL || pCtx->inType == TSDB_DATA_TYPE_TIMESTAMP) {
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
D
fix bug  
dapan1121 已提交
590 591 592 593
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;
      }
G
Ganlin Zhao 已提交
594

D
fix bug  
dapan1121 已提交
595
      int64_t value = 0;
D
dapan1121 已提交
596
      GET_TYPED_DATA(value, int64_t, pCtx->inType, colDataGetData(pInputCol, i));
D
fix bug  
dapan1121 已提交
597 598
      int32_t len = sprintf(varDataVal(tmp), "%" PRId64, value);
      varDataLen(tmp) = len;
D
dapan1121 已提交
599 600
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pCtx->pOut, i, NULL);
D
fix bug  
dapan1121 已提交
601
      } else {
D
dapan1121 已提交
602
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
603 604
      }
    }
D
dapan1121 已提交
605 606
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inType)) {
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
D
fix bug  
dapan1121 已提交
607 608 609 610
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;
      }
G
Ganlin Zhao 已提交
611

D
fix bug  
dapan1121 已提交
612
      uint64_t value = 0;
D
dapan1121 已提交
613
      GET_TYPED_DATA(value, uint64_t, pCtx->inType, colDataGetData(pInputCol, i));
D
fix bug  
dapan1121 已提交
614 615
      int32_t len = sprintf(varDataVal(tmp), "%" PRIu64, value);
      varDataLen(tmp) = len;
D
dapan1121 已提交
616 617
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pCtx->pOut, i, NULL);
D
fix bug  
dapan1121 已提交
618
      } else {
D
dapan1121 已提交
619
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
620 621
      }
    }
D
dapan1121 已提交
622 623
  } else if (IS_FLOAT_TYPE(pCtx->inType)) {
    for (int32_t i = pCtx->startIndex; i <= pCtx->endIndex; ++i) {
D
fix bug  
dapan1121 已提交
624 625 626 627
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;
      }
G
Ganlin Zhao 已提交
628

D
fix bug  
dapan1121 已提交
629
      double value = 0;
D
dapan1121 已提交
630
      GET_TYPED_DATA(value, double, pCtx->inType, colDataGetData(pInputCol, i));
D
fix bug  
dapan1121 已提交
631 632
      int32_t len = sprintf(varDataVal(tmp), "%lf", value);
      varDataLen(tmp) = len;
D
dapan1121 已提交
633 634
      if (pCtx->outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pCtx->pOut, i, NULL);
D
fix bug  
dapan1121 已提交
635
      } else {
D
dapan1121 已提交
636
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
637 638 639
      }
    }
  } else {
D
dapan1121 已提交
640
    sclError("not supported input type:%d", pCtx->inType);
D
fix bug  
dapan1121 已提交
641 642 643 644 645 646
    return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

647
// TODO opt performance
D
dapan1121 已提交
648
int32_t vectorConvertSingleColImpl(const SScalarParam* pIn, SScalarParam* pOut, int32_t* overflow, int32_t startIndex, int32_t numOfRows) {
649 650
  SColumnInfoData* pInputCol  = pIn->columnData;
  SColumnInfoData* pOutputCol = pOut->columnData;
H
Haojun Liao 已提交
651

D
dapan1121 已提交
652 653 654 655
  if (NULL == pInputCol) {
    sclError("input column is NULL, hashFilter %p", pIn->pHashFilter);
    return TSDB_CODE_APP_ERROR;
  }
D
dapan1121 已提交
656

D
dapan1121 已提交
657 658 659 660 661 662
  int32_t rstart = startIndex >= 0 ? startIndex : 0;
  int32_t rend = numOfRows > 0 ? rstart + numOfRows - 1 : rstart + pIn->numOfRows - 1;
  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 已提交
663 664 665 666 667 668 669
  }

  if (overflow) {
    ASSERT(1 == pIn->numOfRows);

    pOut->numOfRows = 0;
    
D
dapan1121 已提交
670 671 672
    if (IS_SIGNED_NUMERIC_TYPE(cCtx.outType)) {
      int64_t minValue = tDataTypes[cCtx.outType].minValue;
      int64_t maxValue = tDataTypes[cCtx.outType].maxValue;
D
dapan1121 已提交
673 674
      
      double value = 0;
D
dapan1121 已提交
675
      GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, 0));
D
dapan1121 已提交
676 677 678 679 680 681 682 683 684 685
      
      if (value > maxValue) {
        *overflow = 1;
        return TSDB_CODE_SUCCESS;
      } else if (value < minValue) {
        *overflow = -1;
        return TSDB_CODE_SUCCESS;
      } else {
        *overflow = 0;
      }
D
dapan1121 已提交
686 687 688
    } 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;
D
dapan1121 已提交
689 690
      
      double value = 0;
D
dapan1121 已提交
691
      GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, 0));
D
dapan1121 已提交
692 693 694 695 696 697 698 699 700 701 702
      
      if (value > maxValue) {
        *overflow = 1;
        return TSDB_CODE_SUCCESS;
      } else if (value < minValue) {
        *overflow = -1;
        return TSDB_CODE_SUCCESS;
      } else {
        *overflow = 0;
      }
    }
D
dapan 已提交
703
  }
704 705

  pOut->numOfRows = pIn->numOfRows;
D
dapan1121 已提交
706
  switch (cCtx.outType) {
707
    case TSDB_DATA_TYPE_BOOL: {
D
dapan1121 已提交
708
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
709
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
710
          colDataAppendNULL(pOutputCol, i);
711 712 713 714
          continue;
        }

        bool value = 0;
D
dapan1121 已提交
715
        GET_TYPED_DATA(value, bool, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
716 717 718 719 720
        colDataAppendInt8(pOutputCol, i, (int8_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_TINYINT: {
D
dapan1121 已提交
721
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
722 723 724 725 726 727
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

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

        int16_t value = 0;
D
dapan1121 已提交
741
        GET_TYPED_DATA(value, int16_t, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
742 743 744 745 746
        colDataAppendInt16(pOutputCol, i, (int16_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_INT:{
D
dapan1121 已提交
747
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
748 749 750 751 752 753
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        int32_t value = 0;
D
dapan1121 已提交
754
        GET_TYPED_DATA(value, int32_t, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
755
        colDataAppendInt32(pOutputCol, i, (int32_t *)&value);
756 757 758
      }
      break;
    }
D
dapan1121 已提交
759
    case TSDB_DATA_TYPE_BIGINT:
760
    case TSDB_DATA_TYPE_TIMESTAMP: {
D
dapan1121 已提交
761
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
762
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
763
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
764
          continue;
D
dapan1121 已提交
765
        }
D
dapan 已提交
766 767

        int64_t value = 0;
D
dapan1121 已提交
768
        GET_TYPED_DATA(value, int64_t, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
769
        colDataAppendInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
770 771
      }
      break;
772
    }
D
dapan1121 已提交
773
    case TSDB_DATA_TYPE_UTINYINT:{
D
dapan1121 已提交
774
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
775 776 777 778
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
G
Ganlin Zhao 已提交
779

D
dapan1121 已提交
780
        uint8_t value = 0;
D
dapan1121 已提交
781
        GET_TYPED_DATA(value, uint8_t, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
782 783 784 785 786
        colDataAppendInt8(pOutputCol, i, (int8_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT:{
D
dapan1121 已提交
787
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
788 789 790 791
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
G
Ganlin Zhao 已提交
792

D
dapan1121 已提交
793
        uint16_t value = 0;
D
dapan1121 已提交
794
        GET_TYPED_DATA(value, uint16_t, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
795 796 797 798 799
        colDataAppendInt16(pOutputCol, i, (int16_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_UINT:{
D
dapan1121 已提交
800
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
D
dapan1121 已提交
801 802 803 804
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
G
Ganlin Zhao 已提交
805

D
dapan1121 已提交
806
        uint32_t value = 0;
D
dapan1121 已提交
807
        GET_TYPED_DATA(value, uint32_t, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
808 809 810 811 812
        colDataAppendInt32(pOutputCol, i, (int32_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
D
dapan1121 已提交
813
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
814
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
815
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
816
          continue;
D
dapan1121 已提交
817
        }
G
Ganlin Zhao 已提交
818

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

D
dapan1121 已提交
832
        float value = 0;
D
dapan1121 已提交
833
        GET_TYPED_DATA(value, float, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
834 835
        colDataAppendFloat(pOutputCol, i, (float*)&value);
      }
G
Ganlin Zhao 已提交
836
      break;
D
dapan1121 已提交
837 838
    }
    case TSDB_DATA_TYPE_DOUBLE: {
D
dapan1121 已提交
839
      for (int32_t i = cCtx.startIndex; i <= cCtx.endIndex; ++i) {
840
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
841
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
842
          continue;
D
dapan1121 已提交
843
        }
G
Ganlin Zhao 已提交
844

D
dapan 已提交
845
        double value = 0;
D
dapan1121 已提交
846
        GET_TYPED_DATA(value, double, cCtx.inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
847
        colDataAppendDouble(pOutputCol, i, (double*)&value);
D
dapan1121 已提交
848
      }
G
Ganlin Zhao 已提交
849
      break;
D
dapan1121 已提交
850
    }
G
Ganlin Zhao 已提交
851
    case TSDB_DATA_TYPE_BINARY:
D
fix bug  
dapan1121 已提交
852
    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
853
      return vectorConvertToVarData(&cCtx);
D
fix bug  
dapan1121 已提交
854
    }
D
dapan1121 已提交
855
    default:
D
dapan1121 已提交
856
      sclError("invalid convert output type:%d", cCtx.outType);
D
dapan1121 已提交
857 858 859 860 861 862
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
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 */
    /*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, 7, 9, 7, 11, 12, 13, 14, 0, 7, 0, 0,
    /*TINY*/ 0, 0, 0, 3, 4, 5, 6, 7, 7, 9, 7, 3,  4,  5,  7,  0, 7, 0, 0,
    /*SMAL*/ 0, 0, 0, 0, 4, 5, 6, 7, 7, 9, 7, 3,  4,  5,  7,  0, 7, 0, 0,
    /*INT */ 0, 0, 0, 0, 0, 5, 6, 7, 7, 9, 7, 4,  4,  5,  7,  0, 7, 0, 0,
    /*BIGI*/ 0, 0, 0, 0, 0, 0, 6, 7, 7, 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 已提交
884

D
dapan1121 已提交
885 886 887 888 889 890 891 892 893 894 895 896
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];
}

D
dapan1121 已提交
897
int32_t vectorConvertSingleCol(SScalarParam *input, SScalarParam *output, int32_t type, int32_t startIndex, int32_t numOfRows) {
D
dapan1121 已提交
898 899 900
  SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
  output->numOfRows = input->numOfRows;

H
Haojun Liao 已提交
901 902
  int32_t code = sclCreateColumnInfoData(&t, input->numOfRows, output);
  if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
903
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
904 905
  }

D
dapan1121 已提交
906
  code = vectorConvertSingleColImpl(input, output, NULL, startIndex, numOfRows);
D
dapan1121 已提交
907 908 909 910 911 912 913
  if (code) {
    return code;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
914
int32_t vectorConvertCols(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam* pLeftOut, SScalarParam* pRightOut, int32_t startIndex, int32_t numOfRows) {
915 916 917
  int32_t leftType  = GET_PARAM_TYPE(pLeft);
  int32_t rightType = GET_PARAM_TYPE(pRight);
  if (leftType == rightType) {
D
dapan1121 已提交
918 919 920
    return TSDB_CODE_SUCCESS;
  }

G
Ganlin Zhao 已提交
921
  SScalarParam *param1 = NULL, *paramOut1 = NULL;
D
dapan1121 已提交
922
  SScalarParam *param2 = NULL, *paramOut2 = NULL;
H
Hongze Cheng 已提交
923
  int32_t       code = 0;
G
Ganlin Zhao 已提交
924

925
  if (leftType < rightType) {
D
dapan1121 已提交
926 927 928 929 930 931 932 933 934 935 936
    param1 = pLeft;
    param2 = pRight;
    paramOut1 = pLeftOut;
    paramOut2 = pRightOut;
  } else {
    param1 = pRight;
    param2 = pLeft;
    paramOut1 = pRightOut;
    paramOut2 = pLeftOut;
  }

937
  int8_t type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
D
dapan1121 已提交
938 939 940 941
  if (0 == type) {
    return TSDB_CODE_SUCCESS;
  }

942
  if (type != GET_PARAM_TYPE(param1)) {
D
dapan1121 已提交
943
    code = vectorConvertSingleCol(param1, paramOut1, type, startIndex, numOfRows);
D
dapan1121 已提交
944 945 946
    if (code) {
      return code;
    }
D
dapan1121 已提交
947
  }
G
Ganlin Zhao 已提交
948

949
  if (type != GET_PARAM_TYPE(param2)) {
D
dapan1121 已提交
950
    code = vectorConvertSingleCol(param2, paramOut2, type, startIndex, numOfRows);
D
dapan1121 已提交
951 952 953
    if (code) {
      return code;
    }
D
dapan1121 已提交
954 955 956 957 958
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
959 960 961 962 963
enum {
  VECTOR_DO_CONVERT = 0x1,
  VECTOR_UN_CONVERT = 0x2,
};

964
// TODO not correct for descending order scan
H
Hongze Cheng 已提交
965 966 967
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);
968 969 970 971
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
972
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
973
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
974 975
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
976
      if (IS_HELPER_NULL(pLeftCol, i)) {
977 978 979
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
980
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) + getVectorDoubleValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
981 982
    }
  }
983
}
D
dapan1121 已提交
984

H
Hongze Cheng 已提交
985 986 987
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);
988 989 990 991
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
992
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
993 994 995
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
996
      if (IS_HELPER_NULL(pLeftCol, i)) {
997 998 999
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1000 1001 1002
      *output =
          taosTimeAdd(getVectorBigintValueFnLeft(pLeftCol->pData, i), getVectorBigintValueFnRight(pRightCol->pData, 0),
                      pRightCol->info.scale, pRightCol->info.precision);
1003 1004 1005 1006
    }
  }
}

D
dapan1121 已提交
1007 1008 1009
static SColumnInfoData* vectorConvertVarToDouble(SScalarParam* pInput, int32_t* converted) {
  SScalarParam output = {0};
  SColumnInfoData* pCol = pInput->columnData;
H
Haojun Liao 已提交
1010

D
dapan1121 已提交
1011 1012 1013 1014 1015 1016
  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 已提交
1017

D
dapan1121 已提交
1018 1019 1020
    *converted = VECTOR_DO_CONVERT;

    return output.columnData;
H
Haojun Liao 已提交
1021
  }
D
dapan1121 已提交
1022 1023 1024 1025

  *converted = VECTOR_UN_CONVERT;
  
  return pInput->columnData;
H
Haojun Liao 已提交
1026 1027
}

H
Hongze Cheng 已提交
1028
static void doReleaseVec(SColumnInfoData *pCol, int32_t type) {
H
Haojun Liao 已提交
1029 1030
  if (type == VECTOR_DO_CONVERT) {
    colDataDestroy(pCol);
H
Haojun Liao 已提交
1031
    taosMemoryFree(pCol);
H
Haojun Liao 已提交
1032 1033 1034
  }
}

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

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

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

H
Haojun Liao 已提交
1043
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1044 1045
  SColumnInfoData *pLeftCol   = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = vectorConvertVarToDouble(pRight, &rightConvert);
1046

1047 1048 1049
  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 已提交
1050 1051 1052 1053
      (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);
1054
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
1055

D
dapan1121 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
    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) {
1067
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1068
        if (IS_NULL) {
wmmhello's avatar
wmmhello 已提交
1069 1070 1071
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
1072
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, i);
1073
      }
G
Ganlin Zhao 已提交
1074
    }
1075
  } else {
H
Hongze Cheng 已提交
1076 1077
    double              *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1078
    _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1079

1080 1081
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
H
Hongze Cheng 已提交
1082
        if (IS_NULL) {
1083 1084 1085 1086
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) + getVectorDoubleValueFnRight(RIGHT_COL, i);
1087 1088 1089 1090 1091 1092
      }
    } 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);
    }
1093
  }
H
Haojun Liao 已提交
1094 1095 1096

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1097
}
D
dapan1121 已提交
1098

1099
// TODO not correct for descending order scan
H
Hongze Cheng 已提交
1100 1101 1102
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);
1103 1104 1105 1106
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
1107
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1108
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1109 1110
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1111
      if (IS_HELPER_NULL(pLeftCol, i)) {
1112 1113 1114
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1115
      *output = (getVectorDoubleValueFnLeft(LEFT_COL, i) - getVectorDoubleValueFnRight(RIGHT_COL, 0)) * factor;
D
dapan1121 已提交
1116
    }
1117 1118
  }
}
D
dapan1121 已提交
1119

H
Hongze Cheng 已提交
1120 1121 1122
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);
1123 1124 1125 1126
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
1127
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1128 1129 1130
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1131
      if (IS_HELPER_NULL(pLeftCol, i)) {
1132 1133 1134
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
H
Hongze Cheng 已提交
1135 1136 1137
      *output =
          taosTimeAdd(getVectorBigintValueFnLeft(pLeftCol->pData, i), -getVectorBigintValueFnRight(pRightCol->pData, 0),
                      pRightCol->info.scale, pRightCol->info.precision);
1138 1139 1140 1141
    }
  }
}

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

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

H
Hongze Cheng 已提交
1147 1148
  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 已提交
1149

H
Haojun Liao 已提交
1150
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1151 1152
  SColumnInfoData *pLeftCol   = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = vectorConvertVarToDouble(pRight, &rightConvert);
1153

1154
  if ((GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP && GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_BIGINT) ||
H
Hongze Cheng 已提交
1155 1156 1157 1158
      (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);
1159
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
1160

D
dapan1121 已提交
1161 1162 1163 1164 1165 1166 1167
    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) {
1168
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1169
        if (IS_NULL) {
wmmhello's avatar
wmmhello 已提交
1170 1171 1172
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
1173
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, i);
1174
      }
D
dapan1121 已提交
1175
    }
1176
  } else {
H
Hongze Cheng 已提交
1177 1178
    double              *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1179 1180 1181 1182
    _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 已提交
1183
        if (IS_NULL) {
1184 1185 1186 1187
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) - getVectorDoubleValueFnRight(RIGHT_COL, i);
1188 1189 1190 1191 1192 1193
      }
    } 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);
    }
1194
  }
H
Haojun Liao 已提交
1195

H
Hongze Cheng 已提交
1196
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1197
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1198 1199
}

1200
// TODO not correct for descending order scan
H
Hongze Cheng 已提交
1201 1202 1203
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);
1204
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
D
dapan1121 已提交
1205

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

wmmhello's avatar
wmmhello 已提交
1208
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1209
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1210 1211
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1212
      if (IS_HELPER_NULL(pLeftCol, i)) {
1213 1214 1215
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1216
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, 0);
1217 1218
    }
  }
D
dapan1121 已提交
1219 1220
}

H
Hongze Cheng 已提交
1221
void vectorMathMultiply(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1222
  SColumnInfoData *pOutputCol = pOut->columnData;
1223
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1224

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

H
Haojun Liao 已提交
1228
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1229 1230
  SColumnInfoData *pLeftCol   = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = vectorConvertVarToDouble(pRight, &rightConvert);
1231

H
Hongze Cheng 已提交
1232
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1233 1234 1235 1236 1237
  _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 已提交
1238
      if (IS_NULL) {
1239 1240 1241
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1242
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, i);
1243 1244 1245 1246 1247
    }
  } 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 已提交
1248
  }
H
Haojun Liao 已提交
1249 1250 1251

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1252 1253
}

H
Hongze Cheng 已提交
1254
void vectorMathDivide(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1255
  SColumnInfoData *pOutputCol = pOut->columnData;
1256
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1257

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

H
Haojun Liao 已提交
1261
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1262 1263
  SColumnInfoData *pLeftCol  = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1264

H
Hongze Cheng 已提交
1265
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1266 1267 1268
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;
1269
  if (pLeft->numOfRows == pRight->numOfRows) {
1270
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
H
Hongze Cheng 已提交
1271
      if (IS_NULL || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) {  // divide by 0 check
1272
        colDataAppendNULL(pOutputCol, i);
1273
        continue;
1274
      }
H
Hongze Cheng 已提交
1275
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) / getVectorDoubleValueFnRight(RIGHT_COL, i);
1276 1277
    }
  } else if (pLeft->numOfRows == 1) {
wmmhello's avatar
wmmhello 已提交
1278
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1279
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1280 1281
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
H
Hongze Cheng 已提交
1282
        if (IS_HELPER_NULL(pRightCol, i) || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) {  // divide by 0 check
1283
          colDataAppendNULL(pOutputCol, i);
1284
          continue;
1285
        }
H
Hongze Cheng 已提交
1286
        *output = getVectorDoubleValueFnLeft(LEFT_COL, 0) / getVectorDoubleValueFnRight(RIGHT_COL, i);
1287 1288 1289
      }
    }
  } else if (pRight->numOfRows == 1) {
H
Hongze Cheng 已提交
1290 1291
    if (IS_HELPER_NULL(pRightCol, 0) ||
        (getVectorDoubleValueFnRight(RIGHT_COL, 0) == 0)) {  // Set pLeft->numOfRows NULL value (divde by 0 check)
1292
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1293 1294
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1295
        if (IS_HELPER_NULL(pLeftCol, i)) {
1296
          colDataAppendNULL(pOutputCol, i);
1297
          continue;
1298
        }
H
Hongze Cheng 已提交
1299
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) / getVectorDoubleValueFnRight(RIGHT_COL, 0);
1300 1301 1302
      }
    }
  }
H
Haojun Liao 已提交
1303

H
Hongze Cheng 已提交
1304
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1305
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1306 1307
}

H
Hongze Cheng 已提交
1308
void vectorMathRemainder(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1309
  SColumnInfoData *pOutputCol = pOut->columnData;
1310
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1311

H
Hongze Cheng 已提交
1312 1313
  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 已提交
1314

H
Haojun Liao 已提交
1315
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1316 1317
  SColumnInfoData *pLeftCol  = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = vectorConvertVarToDouble(pRight, &rightConvert);
1318

H
Hongze Cheng 已提交
1319
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
1320
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1321 1322 1323 1324 1325

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

  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1326
      if (IS_NULL) {
1327
        colDataAppendNULL(pOutputCol, i);
1328 1329 1330
        continue;
      }

1331 1332 1333
      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)) {
1334
        colDataAppendNULL(pOutputCol, i);
1335 1336 1337
        continue;
      }

1338
      *output = lx - ((int64_t)(lx / rx)) * rx;
1339 1340
    }
  } else if (pLeft->numOfRows == 1) {
1341
    double lx = getVectorDoubleValueFnLeft(LEFT_COL, 0);
wmmhello's avatar
wmmhello 已提交
1342
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1343
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1344 1345
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1346
        if (IS_HELPER_NULL(pRightCol, i)) {
1347
          colDataAppendNULL(pOutputCol, i);
1348 1349 1350
          continue;
        }

1351 1352
        double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
        if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
1353
          colDataAppendNULL(pOutputCol, i);
1354 1355 1356
          continue;
        }

1357
        *output = lx - ((int64_t)(lx / rx)) * rx;
1358 1359 1360
      }
    }
  } else if (pRight->numOfRows == 1) {
1361 1362
    double rx = getVectorDoubleValueFnRight(RIGHT_COL, 0);
    if (IS_HELPER_NULL(pRightCol, 0) || FLT_EQUAL(rx, 0)) {  // Set pLeft->numOfRows NULL value
1363
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1364 1365
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1366
        if (IS_HELPER_NULL(pLeftCol, i)) {
1367
          colDataAppendNULL(pOutputCol, i);
1368 1369 1370
          continue;
        }

1371 1372 1373 1374 1375 1376 1377
        double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
        if (isnan(lx) || isinf(lx)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        *output = lx - ((int64_t)(lx / rx)) * rx;
1378 1379 1380
      }
    }
  }
H
Haojun Liao 已提交
1381 1382 1383

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1384 1385
}

H
Hongze Cheng 已提交
1386
void vectorMathMinus(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1387 1388 1389 1390
  SColumnInfoData *pOutputCol = pOut->columnData;

  pOut->numOfRows = pLeft->numOfRows;

H
Hongze Cheng 已提交
1391 1392
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : (pLeft->numOfRows - 1);
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1393 1394

  int32_t leftConvert = 0;
D
dapan1121 已提交
1395
  SColumnInfoData *pLeftCol   = vectorConvertVarToDouble(pLeft, &leftConvert);
D
dapan1121 已提交
1396

H
Hongze Cheng 已提交
1397
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft = getVectorDoubleValueFn(pLeftCol->info.type);
D
dapan1121 已提交
1398 1399 1400

  double *output = (double *)pOutputCol->pData;
  for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1401
    if (IS_HELPER_NULL(pLeftCol, i)) {
1402
      colDataAppendNULL(pOutputCol, i);
1403
      continue;
1404
    }
1405 1406
    double result = getVectorDoubleValueFnLeft(LEFT_COL, i);
    *output = (result == 0) ? 0 : -result;
D
dapan1121 已提交
1407 1408
  }

H
Hongze Cheng 已提交
1409
  doReleaseVec(pLeftCol, leftConvert);
D
dapan1121 已提交
1410 1411
}

H
Hongze Cheng 已提交
1412
void vectorAssign(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1413 1414 1415
  SColumnInfoData *pOutputCol = pOut->columnData;
  pOut->numOfRows = pLeft->numOfRows;

H
Hongze Cheng 已提交
1416
  if (colDataIsNull_s(pRight->columnData, 0)) {
1417
    colDataAppendNNULL(pOutputCol, 0, pOut->numOfRows);
D
dapan1121 已提交
1418
  } else {
H
Hongze Cheng 已提交
1419
    char *d = colDataGetData(pRight->columnData, 0);
D
dapan1121 已提交
1420
    for (int32_t i = 0; i < pOut->numOfRows; ++i) {
1421
      colDataAppend(pOutputCol, i, d, false);
D
dapan1121 已提交
1422 1423
    }
  }
1424 1425 1426

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

H
Hongze Cheng 已提交
1429 1430 1431
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);
1432
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1433

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

wmmhello's avatar
wmmhello 已提交
1436
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1437
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1438 1439
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1440
      if (IS_HELPER_NULL(pLeftCol, i)) {
1441 1442 1443 1444
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
1445 1446
    }
  }
1447
}
D
dapan1121 已提交
1448

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

1453 1454
  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 已提交
1455

H
Haojun Liao 已提交
1456
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1457 1458
  SColumnInfoData *pLeftCol   = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = vectorConvertVarToDouble(pRight, &rightConvert);
D
dapan1121 已提交
1459

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

1463 1464 1465
  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 已提交
1466
      if (IS_NULL) {
1467 1468 1469 1470
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, i);
D
dapan1121 已提交
1471
    }
1472 1473 1474 1475 1476
  } 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 已提交
1477

H
Hongze Cheng 已提交
1478
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1479
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1480 1481
}

H
Hongze Cheng 已提交
1482 1483 1484
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);
1485
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1486

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

wmmhello's avatar
wmmhello 已提交
1489
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1490
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1491
  } else {
1492
    int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, 0);
1493
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1494
      if (IS_HELPER_NULL(pLeftCol, i)) {
1495 1496 1497 1498
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | rx;
D
dapan1121 已提交
1499 1500
    }
  }
1501
}
D
dapan1121 已提交
1502

H
Hongze Cheng 已提交
1503
void vectorBitOr(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
1504
  SColumnInfoData *pOutputCol = pOut->columnData;
1505
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1506

1507 1508
  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 已提交
1509

H
Haojun Liao 已提交
1510
  int32_t leftConvert = 0, rightConvert = 0;
D
dapan1121 已提交
1511 1512
  SColumnInfoData *pLeftCol   = vectorConvertVarToDouble(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = vectorConvertVarToDouble(pRight, &rightConvert);
D
dapan1121 已提交
1513

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

1517 1518 1519
  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 已提交
1520
      if (IS_NULL) {
1521 1522 1523 1524
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | getVectorBigintValueFnRight(RIGHT_COL, i);
1525 1526 1527 1528 1529
    }
  } 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 已提交
1530
  }
H
Haojun Liao 已提交
1531

H
Hongze Cheng 已提交
1532
  doReleaseVec(pLeftCol, leftConvert);
H
Haojun Liao 已提交
1533
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1534 1535
}

D
dapan1121 已提交
1536 1537
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) {
1538 1539 1540
  int32_t num = 0;

  for (int32_t i = startIndex; i < numOfRows && i >= 0; i += step) {
H
Hongze Cheng 已提交
1541 1542
    int32_t leftIndex = (i >= pLeft->numOfRows) ? 0 : i;
    int32_t rightIndex = (i >= pRight->numOfRows) ? 0 : i;
1543 1544 1545 1546 1547 1548 1549

    if (IS_HELPER_NULL(pLeft->columnData, leftIndex) || IS_HELPER_NULL(pRight->columnData, rightIndex)) {
      bool res = false;
      colDataAppendInt8(pOut->columnData, i, (int8_t *)&res);
      continue;
    }

H
Hongze Cheng 已提交
1550 1551
    char   *pLeftData = colDataGetData(pLeft->columnData, leftIndex);
    char   *pRightData = colDataGetData(pRight->columnData, rightIndex);
1552 1553 1554 1555 1556 1557
    int64_t leftOut = 0;
    int64_t rightOut = 0;
    bool    freeLeft = false;
    bool    freeRight = false;
    bool    isJsonnull = false;

H
Hongze Cheng 已提交
1558
    bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData,
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
                                   &leftOut, &rightOut, &isJsonnull, &freeLeft, &freeRight);
    if (isJsonnull) {
      ASSERT(0);
    }

    if (!pLeftData || !pRightData) {
      result = false;
    }

    if (!result) {
      colDataAppendInt8(pOut->columnData, i, (int8_t *)&result);
    } else {
      bool res = filterDoCompare(fp, optr, pLeftData, pRightData);
      colDataAppendInt8(pOut->columnData, i, (int8_t *)&res);
      if (res) {
        ++num;
      }
    }

    if (freeLeft) {
      taosMemoryFreeClear(pLeftData);
    }

    if (freeRight) {
      taosMemoryFreeClear(pRightData);
    }
1585 1586
  }

1587 1588 1589
  return num;
}

D
dapan1121 已提交
1590 1591 1592
void doVectorCompare(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t startIndex, int32_t numOfRows, 
                          int32_t _ord, int32_t optr) {
  int32_t       i = 0;
1593
  int32_t       step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
D
dapan1121 已提交
1594 1595 1596
  int32_t       lType = GET_PARAM_TYPE(pLeft);
  int32_t       rType = GET_PARAM_TYPE(pRight);
  __compar_fn_t fp = NULL;
D
dapan1121 已提交
1597
  int32_t       compRows = 0;
D
dapan1121 已提交
1598 1599 1600 1601 1602
  
  if (lType == rType) {
    fp = filterGetCompFunc(lType, optr);
  } else {
    fp = filterGetCompFuncEx(lType, rType, optr);
wmmhello's avatar
wmmhello 已提交
1603
  }
1604

D
dapan1121 已提交
1605 1606
  if (startIndex < 0) {
    i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
D
dapan1121 已提交
1607
    pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1608 1609 1610 1611 1612 1613
    compRows = pOut->numOfRows;
  } else {
    compRows = startIndex + numOfRows;
    i = startIndex;
  }

1614 1615
  if (pRight->pHashFilter != NULL) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
wmmhello's avatar
wmmhello 已提交
1616
      if (IS_HELPER_NULL(pLeft->columnData, i)) {
H
Hongze Cheng 已提交
1617 1618
        bool res = false;
        colDataAppendInt8(pOut->columnData, i, (int8_t *)&res);
D
dapan1121 已提交
1619 1620 1621
        continue;
      }

1622 1623
      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter);
H
Hongze Cheng 已提交
1624
      colDataAppendInt8(pOut->columnData, i, (int8_t *)&res);
1625 1626 1627
      if (res) {
        pOut->numOfQualified++;
      }
1628
    }
1629
  } else {  // normal compare
D
dapan1121 已提交
1630
    pOut->numOfQualified = doVectorCompareImpl(pLeft, pRight, pOut, i, compRows, step, fp, optr);
D
dapan1121 已提交
1631 1632 1633
  }
}

D
dapan1121 已提交
1634 1635
void vectorCompareImpl(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t startIndex, int32_t numOfRows, 
                             int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1636 1637
  SScalarParam pLeftOut = {0}; 
  SScalarParam pRightOut = {0};
G
Ganlin Zhao 已提交
1638
  SScalarParam *param1 = NULL;
D
dapan1121 已提交
1639
  SScalarParam *param2 = NULL;
D
dapan1121 已提交
1640

D
dapan1121 已提交
1641
  if (SCL_NO_NEED_CONVERT_COMPARISION(GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), optr)) {
D
dapan1121 已提交
1642 1643
    param1 = pLeft;
    param2 = pRight;
D
dapan1121 已提交
1644
  } else {
D
dapan1121 已提交
1645
    vectorConvertCols(pLeft, pRight, &pLeftOut, &pRightOut, startIndex, numOfRows);
D
dapan1121 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657

    if (pLeftOut.columnData != NULL) {
      param1 = &pLeftOut;
    } else {
      param1 = pLeft;
    }

    if (pRightOut.columnData != NULL) {
      param2 = &pRightOut;
    } else {
      param2 = pRight;
    }
D
dapan1121 已提交
1658 1659
  }

D
dapan1121 已提交
1660
  doVectorCompare(param1, param2, pOut, startIndex, numOfRows, _ord, optr);
D
dapan1121 已提交
1661
  
D
dapan1121 已提交
1662
  sclFreeParam(&pLeftOut);
G
Ganlin Zhao 已提交
1663
  sclFreeParam(&pRightOut);
D
dapan1121 已提交
1664 1665
}

D
dapan1121 已提交
1666 1667 1668 1669
void vectorCompare(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
  vectorCompareImpl(pLeft, pRight, pOut, -1, -1, _ord, optr);
}

D
dapan 已提交
1670 1671
void vectorGreater(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN);
D
dapan1121 已提交
1672 1673
}

H
Hongze Cheng 已提交
1674
void vectorGreaterEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1675
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL);
D
dapan1121 已提交
1676 1677
}

H
Hongze Cheng 已提交
1678
void vectorLower(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1679
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN);
D
dapan1121 已提交
1680 1681
}

H
Hongze Cheng 已提交
1682
void vectorLowerEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1683
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL);
D
dapan1121 已提交
1684 1685
}

H
Hongze Cheng 已提交
1686
void vectorEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1687
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL);
D
dapan1121 已提交
1688 1689
}

H
Hongze Cheng 已提交
1690
void vectorNotEqual(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1691
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL);
D
dapan1121 已提交
1692 1693
}

H
Hongze Cheng 已提交
1694
void vectorIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1695
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN);
D
dapan1121 已提交
1696 1697
}

H
Hongze Cheng 已提交
1698
void vectorNotIn(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1699
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN);
D
dapan1121 已提交
1700 1701
}

H
Hongze Cheng 已提交
1702
void vectorLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1703
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE);
D
dapan1121 已提交
1704 1705
}

H
Hongze Cheng 已提交
1706
void vectorNotLike(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1707
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE);
D
dapan1121 已提交
1708 1709
}

H
Hongze Cheng 已提交
1710
void vectorMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1711
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH);
D
dapan1121 已提交
1712 1713
}

H
Hongze Cheng 已提交
1714
void vectorNotMatch(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan 已提交
1715
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH);
D
dapan1121 已提交
1716 1717
}

H
Hongze Cheng 已提交
1718 1719
void vectorIsNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1720
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 1 : 0;
1721
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1722
  }
1723
  pOut->numOfRows = pLeft->numOfRows;
D
dapan1121 已提交
1724 1725
}

H
Hongze Cheng 已提交
1726 1727
void vectorNotNull(SScalarParam *pLeft, SScalarParam *pRight, SScalarParam *pOut, int32_t _ord) {
  for (int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1728
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 0 : 1;
1729
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1730
  }
1731
  pOut->numOfRows = pLeft->numOfRows;
D
dapan 已提交
1732
}
D
dapan1121 已提交
1733

D
dapan 已提交
1734
void vectorIsTrue(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1735
  vectorConvertSingleColImpl(pLeft, pOut, NULL, -1, -1);
1736 1737 1738 1739 1740 1741 1742 1743
  for(int32_t i = 0; i < pOut->numOfRows; ++i) {
    if(colDataIsNull_s(pOut->columnData, i)) {
      int8_t v = 0;
      colDataAppendInt8(pOut->columnData, i, &v);
      colDataSetNotNull_f(pOut->columnData->nullbitmap, i);
    }
  }
  pOut->columnData->hasNull = false;
D
dapan1121 已提交
1744 1745
}

1746 1747
STagVal getJsonValue(char *json, char *key, bool *isExist) {
  STagVal val = {.pKey = key};
H
Hongze Cheng 已提交
1748
  if (tTagIsJson((const STag *)json) == false) {
wmmhello's avatar
wmmhello 已提交
1749
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
H
Hongze Cheng 已提交
1750
    if (isExist) {
wmmhello's avatar
wmmhello 已提交
1751 1752 1753 1754 1755
      *isExist = false;
    }
    return val;
  }

1756
  bool find = tTagGet(((const STag *)json), &val);  // json value is null and not exist is different
H
Hongze Cheng 已提交
1757
  if (isExist) {
1758 1759 1760 1761 1762
    *isExist = find;
  }
  return val;
}

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

H
Hongze Cheng 已提交
1766 1767
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

  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);
    }

H
Hongze Cheng 已提交
1782
    colDataAppend(pOutputCol, i, (const char *)(&isExist), false);
1783 1784 1785 1786
  }
  taosMemoryFree(jsonKey);
}

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

H
Hongze Cheng 已提交
1790 1791
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803

  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 已提交
1804 1805
    char   *pLeftData = colDataGetVarData(pLeft->columnData, i);
    bool    isExist = false;
1806
    STagVal value = getJsonValue(pLeftData, jsonKey, &isExist);
H
Hongze Cheng 已提交
1807
    char   *data = isExist ? tTagValToData(&value, true) : NULL;
1808
    colDataAppend(pOutputCol, i, data, data == NULL);
H
Hongze Cheng 已提交
1809
    if (isExist && IS_VAR_DATA_TYPE(value.type) && data) {
1810 1811 1812 1813 1814 1815
      taosMemoryFree(data);
    }
  }
  taosMemoryFree(jsonKey);
}

1816 1817
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
  switch (binFunctionId) {
D
dapan1121 已提交
1818
    case OP_TYPE_ADD:
1819
      return vectorMathAdd;
D
dapan1121 已提交
1820
    case OP_TYPE_SUB:
1821
      return vectorMathSub;
D
dapan1121 已提交
1822
    case OP_TYPE_MULTI:
1823
      return vectorMathMultiply;
D
dapan1121 已提交
1824
    case OP_TYPE_DIV:
1825
      return vectorMathDivide;
1826
    case OP_TYPE_REM:
1827
      return vectorMathRemainder;
D
dapan1121 已提交
1828 1829
    case OP_TYPE_MINUS:
      return vectorMathMinus;
D
dapan1121 已提交
1830 1831
    case OP_TYPE_ASSIGN:
      return vectorAssign;
D
dapan1121 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
    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 已提交
1856
    case OP_TYPE_IS_NULL:
D
dapan1121 已提交
1857
      return vectorIsNull;
D
dapan1121 已提交
1858
    case OP_TYPE_IS_NOT_NULL:
D
dapan1121 已提交
1859 1860 1861 1862 1863
      return vectorNotNull;
    case OP_TYPE_BIT_AND:
      return vectorBitAnd;
    case OP_TYPE_BIT_OR:
      return vectorBitOr;
D
dapan1121 已提交
1864 1865
    case OP_TYPE_IS_TRUE:
      return vectorIsTrue;
1866 1867 1868 1869
    case OP_TYPE_JSON_GET_VALUE:
      return vectorJsonArrow;
    case OP_TYPE_JSON_CONTAINS:
      return vectorJsonContains;
1870
    default:
1871
      ASSERT(0);
1872 1873
      return NULL;
  }
D
dapan1121 已提交
1874
}