sclvector.c 64.0 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 26
#include "sclvector.h"
#include "tcompare.h"
#include "tdatablock.h"
#include "ttypes.h"
D
dapan1121 已提交
27
#include "ttime.h"
28

29 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))

wmmhello's avatar
wmmhello 已提交
32 33 34 35 36 37
#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))

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

38 39 40 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100
void convertNumberToNumber(const void *inData, void *outData, int8_t inType, int8_t outType){
  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;
    }
    default:{
      ASSERT(0);
    }
  }
}

void convertStringToDouble(const void *inData, void *outData, int8_t inType, int8_t outType){
  char *tmp = taosMemoryMalloc(varDataTLen(inData));
  int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(inData), varDataLen(inData), tmp);
  if (len < 0) {
    sclError("castConvert taosUcs4ToMbs error 1");
  }

  tmp[len] = 0;

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

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

D
dapan1121 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
typedef int64_t (*_getBigintValue_fn_t)(void *src, int32_t index);

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);
}
139 140 141 142
int64_t getVectorBigintValue_BOOL(void *src, int32_t index) {
  return (int64_t)*((bool *)src + index);
}

143 144 145 146 147 148 149 150 151 152 153 154 155 156
int64_t getVectorBigintValue_JSON(void *src, int32_t index){
  ASSERT(!colDataIsNull_var(((SColumnInfoData*)src), index));
  char *data = colDataGetVarData((SColumnInfoData*)src, index);
  double out = 0;
  if (*data == TSDB_DATA_TYPE_NULL){
    return 0;
  } else if(*data == TSDB_DATA_TYPE_NCHAR) {   // json inner type can not be BINARY
    convertStringToDouble(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  } else {
    convertNumberToNumber(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  }
  return (int64_t)out;
}

D
dapan1121 已提交
157 158
_getBigintValue_fn_t getVectorBigintValueFn(int32_t srcType) {
    _getBigintValue_fn_t p = NULL;
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
    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 {
188
      ASSERT(0);
D
dapan1121 已提交
189 190 191 192
    }
    return p;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
typedef void* (*_getValueAddr_fn_t)(void *src, int32_t index);

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);
}
D
dapan1121 已提交
225 226 227 228
void* getVectorValueAddr_default(void *src, int32_t index) {
  return src;
}
void* getVectorValueAddr_VAR(void *src, int32_t index) {
H
Haojun Liao 已提交
229
  return colDataGetData((SColumnInfoData *)src, index);
D
dapan1121 已提交
230
}
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

_getValueAddr_fn_t getVectorValueAddrFn(int32_t srcType) {
    _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;
D
dapan1121 已提交
254 255 256 257
    }else if(srcType==TSDB_DATA_TYPE_BINARY) {
        p = getVectorValueAddr_VAR;
    }else if(srcType==TSDB_DATA_TYPE_NCHAR) {
        p = getVectorValueAddr_VAR;
258
    }else {
D
dapan1121 已提交
259
        p = getVectorValueAddr_default;
260 261 262 263
    }
    return p;
}

D
dapan1121 已提交
264 265 266 267 268 269 270 271 272
static FORCE_INLINE void varToTimestamp(char *buf, SScalarParam* pOut, int32_t rowIndex) {
  int64_t value = 0;
  if (taosParseTime(buf, &value, strlen(buf), pOut->columnData->info.precision, tsDaylight) != TSDB_CODE_SUCCESS) {
    value = 0;
  }
  
  colDataAppendInt64(pOut->columnData, rowIndex, &value);
}

273
static FORCE_INLINE void varToSigned(char *buf, SScalarParam* pOut, int32_t rowIndex) {
D
fix bug  
dapan1121 已提交
274 275
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_TINYINT: {
wafwerar's avatar
wafwerar 已提交
276
      int8_t value = (int8_t)taosStr2Int8(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
277 278 279 280
      colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_SMALLINT: {
wafwerar's avatar
wafwerar 已提交
281
      int16_t value = (int16_t)taosStr2Int16(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
282 283 284 285
      colDataAppendInt16(pOut->columnData, rowIndex, (int16_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_INT: {
wafwerar's avatar
wafwerar 已提交
286
      int32_t value = (int32_t)taosStr2Int32(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
287 288 289 290
      colDataAppendInt32(pOut->columnData, rowIndex, (int32_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_BIGINT: {
wafwerar's avatar
wafwerar 已提交
291
      int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
292 293 294 295
      colDataAppendInt64(pOut->columnData, rowIndex, (int64_t*)&value);
      break;
    }   
  }
D
dapan1121 已提交
296 297
}

298
static FORCE_INLINE void varToUnsigned(char *buf, SScalarParam* pOut, int32_t rowIndex) {
D
fix bug  
dapan1121 已提交
299 300
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_UTINYINT: {
wafwerar's avatar
wafwerar 已提交
301
      uint8_t value = (uint8_t)taosStr2UInt8(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
302 303 304 305
      colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_USMALLINT: {
wafwerar's avatar
wafwerar 已提交
306
      uint16_t value = (uint16_t)taosStr2UInt16(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
307 308 309 310
      colDataAppendInt16(pOut->columnData, rowIndex, (int16_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_UINT: {
wafwerar's avatar
wafwerar 已提交
311
      uint32_t value = (uint32_t)taosStr2UInt32(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
312 313 314 315
      colDataAppendInt32(pOut->columnData, rowIndex, (int32_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_UBIGINT: {
wafwerar's avatar
wafwerar 已提交
316
      uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
317 318 319 320
      colDataAppendInt64(pOut->columnData, rowIndex, (int64_t*)&value);
      break;
    }   
  }
D
dapan1121 已提交
321 322
}

323
static FORCE_INLINE void varToFloat(char *buf, SScalarParam* pOut, int32_t rowIndex) {
wafwerar's avatar
wafwerar 已提交
324
  double value = taosStr2Double(buf, NULL);
325
  colDataAppendDouble(pOut->columnData, rowIndex, &value);
326 327 328
}

static FORCE_INLINE void varToBool(char *buf, SScalarParam* pOut, int32_t rowIndex) {
wafwerar's avatar
wafwerar 已提交
329
  int64_t value = taosStr2Int64(buf, NULL, 10);
330
  bool v = (value != 0)? true:false;
331
  colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*) &v);
D
dapan1121 已提交
332 333
}

wmmhello's avatar
wmmhello 已提交
334 335 336
static FORCE_INLINE void varToNchar(char* buf, SScalarParam* pOut, int32_t rowIndex) {
  int32_t len = 0;
  int32_t inputLen = varDataLen(buf);
D
fix bug  
dapan1121 已提交
337
  int32_t outputMaxLen = (inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
wmmhello's avatar
wmmhello 已提交
338

D
fix bug  
dapan1121 已提交
339 340
  char* t = taosMemoryCalloc(1, outputMaxLen);
  /*int32_t resLen = */taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4*) varDataVal(t), outputMaxLen, &len);
wmmhello's avatar
wmmhello 已提交
341 342 343 344 345
  varDataSetLen(t, len);

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

D
dapan1121 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
static FORCE_INLINE void ncharToVar(char* buf, SScalarParam* pOut, int32_t rowIndex) {
  int32_t inputLen = varDataLen(buf);

  char* t = taosMemoryCalloc(1, inputLen + VARSTR_HEADER_SIZE);
  int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t));
  if (len < 0) {
    taosMemoryFree(t);
    return;
  }
  varDataSetLen(t, len);

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


363 364 365 366 367
//TODO opt performance, tmp is not needed.
int32_t vectorConvertFromVarData(const SScalarParam* pIn, SScalarParam* pOut, int32_t inType, int32_t outType) {
  int32_t bufSize = pIn->columnData->info.bytes;
  char *tmp = taosMemoryMalloc(bufSize + VARSTR_HEADER_SIZE);

wmmhello's avatar
wmmhello 已提交
368
  bool vton = false;
369 370 371 372

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

  pOut->numOfRows = pIn->numOfRows;
  for (int32_t i = 0; i < pIn->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
396
    if (IS_HELPER_NULL(pIn->columnData, i)) {
397 398 399 400 401 402 403 404
      colDataAppendNULL(pOut->columnData, i);
      continue;
    }

    char* data = colDataGetVarData(pIn->columnData, i);
    int32_t convertType = inType;
    if(inType == TSDB_DATA_TYPE_JSON){
      if(*data == TSDB_DATA_TYPE_NULL) {
wmmhello's avatar
wmmhello 已提交
405
        ASSERT(0);
406 407 408 409 410 411 412 413 414
      }
      else if(*data == TSDB_DATA_TYPE_NCHAR) {
        data += CHAR_BYTES;
        convertType = TSDB_DATA_TYPE_NCHAR;
      } else {
        convertNumberToNumber(data+CHAR_BYTES, colDataGetNumData(pOut->columnData, i), *data, outType);
        continue;
      }
    }
wmmhello's avatar
wmmhello 已提交
415 416 417
    if (vton) {
      memcpy(tmp, data, varDataTLen(data));
    } else {
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
      if (TSDB_DATA_TYPE_VARCHAR == convertType) {
        memcpy(tmp, varDataVal(data), varDataLen(data));
        tmp[varDataLen(data)] = 0;
      } else if (TSDB_DATA_TYPE_NCHAR == convertType){
        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 已提交
433
    }
434 435 436 437 438 439 440 441
    
    (*func)(tmp, pOut, i);
  }
  
  taosMemoryFreeClear(tmp);
  return TSDB_CODE_SUCCESS;
}

442
double getVectorDoubleValue_JSON(void *src, int32_t index){
443
  char *data = colDataGetVarData((SColumnInfoData*)src, index);
444
  double out = 0;
445 446 447
  if (*data == TSDB_DATA_TYPE_NULL){
    return out;
  } else if(*data == TSDB_DATA_TYPE_NCHAR) {   // json inner type can not be BINARY
448
    convertStringToDouble(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
449
  } else {
450 451 452 453 454
    convertNumberToNumber(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  }
  return out;
}

wmmhello's avatar
wmmhello 已提交
455
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){
456
  if(optr == OP_TYPE_JSON_CONTAINS) {
wmmhello's avatar
wmmhello 已提交
457
    return true;
458 459 460
  }

  if(typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON){
wmmhello's avatar
wmmhello 已提交
461
    return true;
462 463 464 465 466 467 468 469 470 471
  }

  if(typeLeft == TSDB_DATA_TYPE_JSON){
    typeLeft = **pLeftData;
    (*pLeftData) ++;
  }
  if(typeRight == TSDB_DATA_TYPE_JSON){
    typeRight = **pRightData;
    (*pRightData) ++;
  }
wmmhello's avatar
wmmhello 已提交
472 473 474 475 476 477 478

  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){
      return false;
    }
  }

479 480
  if(typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL){
    *isNull = true;
wmmhello's avatar
wmmhello 已提交
481
    return true;
482
  }
483 484 485 486
  int8_t type = vectorGetConvertType(typeLeft, typeRight);

  if(type == 0) {
    *fp = filterGetCompFunc(typeLeft, optr);
wmmhello's avatar
wmmhello 已提交
487
    return true;
488 489 490 491 492 493 494 495 496 497 498 499
  }

  *fp = filterGetCompFunc(type, optr);

  if(typeLeft == TSDB_DATA_TYPE_NCHAR) {
    convertStringToDouble(*pLeftData, pLeftOut, typeLeft, type);
    *pLeftData = pLeftOut;
  } else if(typeLeft != type) {
    convertNumberToNumber(*pLeftData, pLeftOut, typeLeft, type);
    *pLeftData = pLeftOut;
  }

500
  if(typeRight == TSDB_DATA_TYPE_NCHAR) {
501 502 503 504 505 506
    convertStringToDouble(*pRightData, pRightOut, typeRight, type);
    *pRightData = pRightOut;
  } else if(typeRight != type) {
    convertNumberToNumber(*pRightData, pRightOut, typeRight, type);
    *pRightData = pRightOut;
  }
wmmhello's avatar
wmmhello 已提交
507
  return true;
508 509
}

D
fix bug  
dapan1121 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
int32_t vectorConvertToVarData(const SScalarParam* pIn, SScalarParam* pOut, int16_t inType, int16_t outType) {
  SColumnInfoData* pInputCol  = pIn->columnData;
  SColumnInfoData* pOutputCol = pOut->columnData;
  char tmp[128] = {0};

  if (IS_SIGNED_NUMERIC_TYPE(inType) || inType == TSDB_DATA_TYPE_BOOL || inType == TSDB_DATA_TYPE_TIMESTAMP) {
    for (int32_t i = 0; i < pIn->numOfRows; ++i) {
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;
      }
      
      int64_t value = 0;
      GET_TYPED_DATA(value, int64_t, inType, colDataGetData(pInputCol, i));
      int32_t len = sprintf(varDataVal(tmp), "%" PRId64, value);
      varDataLen(tmp) = len;
      if (outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pOut, i);
      } else {
D
dapan1121 已提交
529
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
      }
    }
  } else if (IS_UNSIGNED_NUMERIC_TYPE(inType)) {
    for (int32_t i = 0; i < pIn->numOfRows; ++i) {
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;
      }
      
      uint64_t value = 0;
      GET_TYPED_DATA(value, uint64_t, inType, colDataGetData(pInputCol, i));
      int32_t len = sprintf(varDataVal(tmp), "%" PRIu64, value);
      varDataLen(tmp) = len;
      if (outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pOut, i);
      } else {
D
dapan1121 已提交
546
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
      }
    }
  } else if (IS_FLOAT_TYPE(inType)) {
    for (int32_t i = 0; i < pIn->numOfRows; ++i) {
      if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;
      }
      
      double value = 0;
      GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, i));
      int32_t len = sprintf(varDataVal(tmp), "%lf", value);
      varDataLen(tmp) = len;
      if (outType == TSDB_DATA_TYPE_NCHAR) {
        varToNchar(tmp, pOut, i);
      } else {
D
dapan1121 已提交
563
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
564 565 566 567 568 569 570 571 572 573 574
      }
    }
  } else {
    sclError("not supported input type:%d", inType);
    return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}


575
// TODO opt performance
H
Haojun Liao 已提交
576
int32_t vectorConvertImpl(const SScalarParam* pIn, SScalarParam* pOut) {
577 578
  SColumnInfoData* pInputCol  = pIn->columnData;
  SColumnInfoData* pOutputCol = pOut->columnData;
H
Haojun Liao 已提交
579 580

  int16_t inType   = pInputCol->info.type;
581
  int16_t outType  = pOutputCol->info.type;
D
dapan1121 已提交
582

583
  if (IS_VAR_DATA_TYPE(inType)) {
D
dapan 已提交
584 585 586
    return vectorConvertFromVarData(pIn, pOut, inType, outType);
  }
  
D
dapan1121 已提交
587
  switch (outType) {
588 589 590
    case TSDB_DATA_TYPE_BOOL: {
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
591
          colDataAppendNULL(pOutputCol, i);
592 593 594 595
          continue;
        }

        bool value = 0;
D
dapan1121 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
        GET_TYPED_DATA(value, bool, inType, colDataGetData(pInputCol, i));
        colDataAppendInt8(pOutputCol, i, (int8_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_TINYINT: {
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        int8_t value = 0;
        GET_TYPED_DATA(value, int8_t, inType, colDataGetData(pInputCol, i));
        colDataAppendInt8(pOutputCol, i, (int8_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_SMALLINT:{
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        int16_t value = 0;
        GET_TYPED_DATA(value, int16_t, inType, colDataGetData(pInputCol, i));
        colDataAppendInt16(pOutputCol, i, (int16_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_INT:{
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        int32_t value = 0;
        GET_TYPED_DATA(value, int32_t, inType, colDataGetData(pInputCol, i));
        colDataAppendInt32(pOutputCol, i, (int32_t *)&value);
637 638 639
      }
      break;
    }
D
dapan1121 已提交
640
    case TSDB_DATA_TYPE_BIGINT:
641
    case TSDB_DATA_TYPE_TIMESTAMP: {
642 643
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
644
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
645
          continue;
D
dapan1121 已提交
646
        }
D
dapan 已提交
647 648

        int64_t value = 0;
649
        GET_TYPED_DATA(value, int64_t, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
650
        colDataAppendInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
651 652
      }
      break;
653
    }
D
dapan1121 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
    case TSDB_DATA_TYPE_UTINYINT:{
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
        
        uint8_t value = 0;
        GET_TYPED_DATA(value, uint8_t, inType, colDataGetData(pInputCol, i));
        colDataAppendInt8(pOutputCol, i, (int8_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT:{
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
        
        uint16_t value = 0;
        GET_TYPED_DATA(value, uint16_t, inType, colDataGetData(pInputCol, i));
        colDataAppendInt16(pOutputCol, i, (int16_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_UINT:{
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
        
        uint32_t value = 0;
        GET_TYPED_DATA(value, uint32_t, inType, colDataGetData(pInputCol, i));
        colDataAppendInt32(pOutputCol, i, (int32_t *)&value);
      }
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
694 695
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
696
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
697
          continue;
D
dapan1121 已提交
698 699
        }
        
D
dapan 已提交
700
        uint64_t value = 0;
701
        GET_TYPED_DATA(value, uint64_t, inType, colDataGetData(pInputCol, i));
702
        colDataAppendInt64(pOutputCol, i, (int64_t*)&value);
D
dapan1121 已提交
703 704
      }
      break;
D
dapan1121 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    }
    case TSDB_DATA_TYPE_FLOAT:{
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }
        
        float value = 0;
        GET_TYPED_DATA(value, float, inType, colDataGetData(pInputCol, i));
        colDataAppendFloat(pOutputCol, i, (float*)&value);
      }
      break;  
    }
    case TSDB_DATA_TYPE_DOUBLE: {
720 721
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
722
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
723
          continue;
D
dapan1121 已提交
724 725
        }
        
D
dapan 已提交
726
        double value = 0;
727
        GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
728
        colDataAppendDouble(pOutputCol, i, (double*)&value);
D
dapan1121 已提交
729
      }
D
dapan1121 已提交
730 731
      break;  
    }
D
fix bug  
dapan1121 已提交
732 733 734 735
    case TSDB_DATA_TYPE_BINARY: 
    case TSDB_DATA_TYPE_NCHAR: {
      return vectorConvertToVarData(pIn, pOut, inType, outType);
    }
D
dapan1121 已提交
736
    default:
D
dapan1121 已提交
737
      sclError("invalid convert output type:%d", outType);
D
dapan1121 已提交
738 739 740 741 742 743 744
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

int8_t gConvertTypes[TSDB_DATA_TYPE_BLOB+1][TSDB_DATA_TYPE_BLOB+1] = {
745
/*         NULL BOOL TINY SMAL INT  BIG  FLOA DOUB VARC TIME NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB */
H
Haojun Liao 已提交
746
/*NULL*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
747 748 749 750 751 752 753
/*BOOL*/   0,   0,   0,   3,   4,   5,   6,   7,   7,   9,   7,   0,   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,   0,   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,
D
dapan1121 已提交
754
/*VARC*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   8,   7,   7,   7,   7,   0,   0,   0,   0,
D
dapan1121 已提交
755
/*TIME*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   9,   9,   9,   7,   0,   7,   0,   0,
H
Haojun Liao 已提交
756
/*NCHA*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,   7,   7,   7,   0,   0,   0,   0,
757 758 759 760
/*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,
H
Haojun Liao 已提交
761
/*JSON*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
762
/*VARB*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
H
Haojun Liao 已提交
763 764
/*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 已提交
765 766
};

D
dapan1121 已提交
767 768 769 770 771 772 773 774 775 776 777 778
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 已提交
779
int32_t vectorConvert(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam* pLeftOut, SScalarParam* pRightOut) {
780
  if (pLeft->pHashFilter != NULL || pRight->pHashFilter != NULL) {
D
dapan1121 已提交
781 782 783
    return TSDB_CODE_SUCCESS;
  }

784 785 786
  int32_t leftType  = GET_PARAM_TYPE(pLeft);
  int32_t rightType = GET_PARAM_TYPE(pRight);
  if (leftType == rightType) {
D
dapan1121 已提交
787 788 789
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
790 791
  SScalarParam *param1 = NULL, *paramOut1 = NULL; 
  SScalarParam *param2 = NULL, *paramOut2 = NULL;
D
dapan1121 已提交
792 793
  int32_t code = 0;
  
794
  if (leftType < rightType) {
D
dapan1121 已提交
795 796 797 798 799 800 801 802 803 804 805
    param1 = pLeft;
    param2 = pRight;
    paramOut1 = pLeftOut;
    paramOut2 = pRightOut;
  } else {
    param1 = pRight;
    param2 = pLeft;
    paramOut1 = pRightOut;
    paramOut2 = pLeftOut;
  }

806
  int8_t type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
D
dapan1121 已提交
807 808 809 810
  if (0 == type) {
    return TSDB_CODE_SUCCESS;
  }

811 812 813 814 815 816 817
  if (type != GET_PARAM_TYPE(param1)) {
    SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
    paramOut1->numOfRows = param1->numOfRows;

    paramOut1->columnData = createColumnInfoData(&t, param1->numOfRows);
    if (paramOut1->columnData == NULL) {
      return terrno;
D
dapan1121 已提交
818
    }
819

D
dapan1121 已提交
820 821
    code = vectorConvertImpl(param1, paramOut1);
    if (code) {
H
Haojun Liao 已提交
822
//      taosMemoryFreeClear(paramOut1->data);
D
dapan1121 已提交
823 824 825 826
      return code;
    }
  }
  
827 828 829 830 831 832 833 834 835
  if (type != GET_PARAM_TYPE(param2)) {
    SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
    paramOut2->numOfRows = param2->numOfRows;

    paramOut2->columnData = createColumnInfoData(&t, param2->numOfRows);
    if (paramOut2->columnData == NULL) {
      return terrno;
    }

D
dapan1121 已提交
836 837 838 839 840 841 842 843 844
    code = vectorConvertImpl(param2, paramOut2);
    if (code) {
      return code;
    }
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
845 846 847 848 849 850
enum {
  VECTOR_DO_CONVERT = 0x1,
  VECTOR_UN_CONVERT = 0x2,
};

static int32_t doConvertHelper(SScalarParam* pDest, int32_t* convert, const SScalarParam* pParam, int32_t type) {
851 852
  SColumnInfoData* pCol = pParam->columnData;

853
  if (IS_VAR_DATA_TYPE(pCol->info.type) && pCol->info.type != TSDB_DATA_TYPE_JSON) {
H
Haojun Liao 已提交
854
    pDest->numOfRows = pParam->numOfRows;
855 856

    SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
H
Haojun Liao 已提交
857 858
    pDest->columnData = createColumnInfoData(&t, pParam->numOfRows);
    if (pDest->columnData == NULL) {
859 860
      sclError("malloc %d failed", (int32_t)(pParam->numOfRows * sizeof(double)));
      return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
861 862
    }

H
Haojun Liao 已提交
863
    int32_t code = vectorConvertImpl(pParam, pDest);
864 865
    if (code != TSDB_CODE_SUCCESS) {
      return code;
D
dapan1121 已提交
866
    }
867

H
Haojun Liao 已提交
868 869 870
    *convert = VECTOR_DO_CONVERT;
  } else {
    *convert = VECTOR_UN_CONVERT;
D
dapan1121 已提交
871
  }
872 873 874 875 876 877 878 879 880 881 882

  return TSDB_CODE_SUCCESS;
}

// TODO not correct for descending order scan
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);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
883
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
884
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
885 886
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
887
      if (IS_HELPER_NULL(pLeftCol, i)) {
888 889 890 891 892
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                + getVectorDoubleValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
893 894
    }
  }
895
}
D
dapan1121 已提交
896

897 898 899 900 901 902
static void vectorMathBigintAddHelper(SColumnInfoData* pLeftCol, SColumnInfoData* pRightCol, SColumnInfoData* pOutputCol, int32_t numOfRows, int32_t step, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
903
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
904 905 906
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
907
      if (IS_HELPER_NULL(pLeftCol, i)) {
908 909 910
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
911 912 913 914 915
      *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, 0);
    }
  }
}

H
Haojun Liao 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
static SColumnInfoData* doVectorConvert(SScalarParam* pInput, int32_t* doConvert) {
  SScalarParam convertParam = {0};

  int32_t code = doConvertHelper(&convertParam, doConvert, pInput, TSDB_DATA_TYPE_DOUBLE);
  if (code != TSDB_CODE_SUCCESS) {
    terrno = code;
    return NULL;
  }

  if (*doConvert == VECTOR_DO_CONVERT) {
    return convertParam.columnData;
  } else {
    return pInput->columnData;
  }
}

static void doReleaseVec(SColumnInfoData* pCol, int32_t type) {
  if (type == VECTOR_DO_CONVERT) {
    colDataDestroy(pCol);
  }
}

wmmhello's avatar
wmmhello 已提交
938 939 940 941 942
STagVal getJsonValue(char *json, char *key, bool *isExist) {
  STagVal val = {.pKey = key};
  bool find = tTagGet(((const STag *)json), &val);  // json value is null and not exist is different
  if(isExist){
    *isExist = find;
943
  }
wmmhello's avatar
wmmhello 已提交
944
  return val;
945 946 947 948 949 950 951 952 953 954
}

void vectorJsonArrow(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;

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

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

955
  char *pRightData = colDataGetVarData(pRight->columnData, 0);
wmmhello's avatar
wmmhello 已提交
956 957
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
  memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
958
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
959 960
    if (colDataIsNull_var(pLeft->columnData, i)) {
      colDataSetNull_var(pOutputCol, i);
961 962 963
      pOutputCol->hasNull = true;
      continue;
    }
964
    char *pLeftData = colDataGetVarData(pLeft->columnData, i);
wmmhello's avatar
wmmhello 已提交
965 966 967
    bool isExist = false;
    STagVal value = getJsonValue(pLeftData, jsonKey, &isExist);
    char *data = isExist ? tTagValToData(&value, true) : NULL;
wmmhello's avatar
wmmhello 已提交
968
    colDataAppend(pOutputCol, i, data, data == NULL);
wmmhello's avatar
wmmhello 已提交
969
    if(isExist && IS_VAR_DATA_TYPE(value.type) && data){
wmmhello's avatar
wmmhello 已提交
970
      taosMemoryFree(data);
971 972
    }
  }
wmmhello's avatar
wmmhello 已提交
973
  taosMemoryFree(jsonKey);
974 975
}

976 977 978 979 980 981
void vectorMathAdd(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;

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

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

H
Haojun Liao 已提交
984 985 986
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
987

988 989 990 991
  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) ||
      (GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_TIMESTAMP && GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_BOOL)) { //timestamp plus duration
992 993 994
    int64_t *output = (int64_t *)pOutputCol->pData;
    _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
995

996 997
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
998
        if (IS_NULL) {
wmmhello's avatar
wmmhello 已提交
999 1000 1001
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
1002
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, i);
1003
      }
1004 1005 1006 1007
    } else if (pLeft->numOfRows == 1) {
      vectorMathBigintAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathBigintAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
1008
    }
1009 1010 1011 1012
  } else {
    double *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
    _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1013

1014 1015
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1016
        if (IS_NULL){
1017 1018 1019 1020
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) + getVectorDoubleValueFnRight(RIGHT_COL, i);
1021 1022 1023 1024 1025 1026
      }
    } 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);
    }
1027
  }
H
Haojun Liao 已提交
1028 1029 1030

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1031
}
D
dapan1121 已提交
1032

1033 1034 1035 1036 1037 1038 1039
// TODO not correct for descending order scan
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);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
1040
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1041
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1042 1043
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1044
      if (IS_HELPER_NULL(pLeftCol, i)) {
1045 1046 1047 1048 1049
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = (getVectorDoubleValueFnLeft(LEFT_COL, i)
                 - getVectorDoubleValueFnRight(RIGHT_COL, 0)) * factor;
D
dapan1121 已提交
1050
    }
1051 1052
  }
}
D
dapan1121 已提交
1053

1054 1055 1056 1057 1058 1059
static void vectorMathBigintSubHelper(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);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

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

wmmhello's avatar
wmmhello 已提交
1060
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1061 1062 1063
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1064
      if (IS_HELPER_NULL(pLeftCol, i)) {
1065 1066 1067
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1068 1069 1070 1071 1072
      *output = (getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, 0)) * factor;
    }
  }
}

1073 1074
void vectorMathSub(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
D
dapan1121 已提交
1075

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

1078 1079
  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 已提交
1080

H
Haojun Liao 已提交
1081 1082 1083
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
1084

1085 1086 1087 1088 1089
  if ((GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP && GET_PARAM_TYPE(pRight) == TSDB_DATA_TYPE_BIGINT) ||
      (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);
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
1090

1091 1092
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1093
        if (IS_NULL) {
wmmhello's avatar
wmmhello 已提交
1094 1095 1096
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
1097
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, i);
1098
      }
1099 1100 1101 1102
    } else if (pLeft->numOfRows == 1) {
      vectorMathBigintSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathBigintSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
D
dapan1121 已提交
1103
    }
1104 1105 1106 1107 1108 1109 1110
  } else {
    double *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
    _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 已提交
1111
        if (IS_NULL) {
1112 1113 1114 1115
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) - getVectorDoubleValueFnRight(RIGHT_COL, i);
1116 1117 1118 1119 1120 1121
      }
    } 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);
    }
1122
  }
H
Haojun Liao 已提交
1123 1124 1125

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1126 1127
}

1128 1129 1130 1131
// TODO not correct for descending order scan
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);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
D
dapan1121 已提交
1132

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

wmmhello's avatar
wmmhello 已提交
1135
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1136
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1137 1138
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1139
      if (IS_HELPER_NULL(pLeftCol, i)) {
1140 1141 1142
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1143
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, 0);
1144 1145
    }
  }
D
dapan1121 已提交
1146 1147
}

1148 1149
void vectorMathMultiply(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1150
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1151 1152 1153 1154

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

H
Haojun Liao 已提交
1155 1156 1157
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
1158 1159 1160 1161 1162 1163 1164

  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
  _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 已提交
1165
      if (IS_NULL) {
1166 1167 1168
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1169
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, i);
1170 1171 1172 1173 1174
    }
  } 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 已提交
1175
  }
H
Haojun Liao 已提交
1176 1177 1178

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1179 1180
}

1181 1182
void vectorMathDivide(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1183
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1184

1185 1186 1187
  int32_t i = ((_ord) == TSDB_ORDER_ASC)? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t step = ((_ord) == TSDB_ORDER_ASC)? 1 : -1;

H
Haojun Liao 已提交
1188 1189 1190
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
1191

1192 1193 1194 1195
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;
1196
  if (pLeft->numOfRows == pRight->numOfRows) {
1197
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1198
      if (IS_NULL || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) { //divide by 0 check
1199
        colDataAppendNULL(pOutputCol, i);
1200
        continue;
1201
      }
1202
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
1203
                / getVectorDoubleValueFnRight(RIGHT_COL, i);
1204 1205
    }
  } else if (pLeft->numOfRows == 1) {
wmmhello's avatar
wmmhello 已提交
1206
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1207
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1208 1209
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1210
        if (IS_HELPER_NULL(pRightCol, i) || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) { // divide by 0 check
1211
          colDataAppendNULL(pOutputCol, i);
1212
          continue;
1213 1214 1215
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, 0)
                  / getVectorDoubleValueFnRight(RIGHT_COL, i);
1216 1217 1218
      }
    }
  } else if (pRight->numOfRows == 1) {
wmmhello's avatar
wmmhello 已提交
1219
    if (IS_HELPER_NULL(pRightCol, 0) || (getVectorDoubleValueFnRight(RIGHT_COL, 0) == 0)) {  // Set pLeft->numOfRows NULL value (divde by 0 check)
1220
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1221 1222
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1223
        if (IS_HELPER_NULL(pLeftCol, i)) {
1224
          colDataAppendNULL(pOutputCol, i);
1225
          continue;
1226 1227 1228
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                  / getVectorDoubleValueFnRight(RIGHT_COL, 0);
1229 1230 1231
      }
    }
  }
H
Haojun Liao 已提交
1232 1233 1234

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1235 1236
}

1237 1238
void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1239
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1240 1241 1242

  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 已提交
1243

H
Haojun Liao 已提交
1244 1245 1246
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
1247

1248 1249
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1250 1251 1252 1253 1254

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

  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1255
      if (IS_NULL) {
1256
        colDataAppendNULL(pOutputCol, i);
1257 1258 1259
        continue;
      }

1260 1261 1262
      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)) {
1263
        colDataAppendNULL(pOutputCol, i);
1264 1265 1266
        continue;
      }

1267
      *output = lx - ((int64_t)(lx / rx)) * rx;
1268 1269
    }
  } else if (pLeft->numOfRows == 1) {
1270
    double lx = getVectorDoubleValueFnLeft(LEFT_COL, 0);
wmmhello's avatar
wmmhello 已提交
1271
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1272
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1273 1274
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1275
        if (IS_HELPER_NULL(pRightCol, i)) {
1276
          colDataAppendNULL(pOutputCol, i);
1277 1278 1279
          continue;
        }

1280 1281
        double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
        if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
1282
          colDataAppendNULL(pOutputCol, i);
1283 1284 1285
          continue;
        }

1286
        *output = lx - ((int64_t)(lx / rx)) * rx;
1287 1288 1289
      }
    }
  } else if (pRight->numOfRows == 1) {
1290 1291
    double rx = getVectorDoubleValueFnRight(RIGHT_COL, 0);
    if (IS_HELPER_NULL(pRightCol, 0) || FLT_EQUAL(rx, 0)) {  // Set pLeft->numOfRows NULL value
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 1298 1299
          continue;
        }

1300 1301 1302 1303 1304 1305 1306
        double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
        if (isnan(lx) || isinf(lx)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        *output = lx - ((int64_t)(lx / rx)) * rx;
1307 1308 1309
      }
    }
  }
H
Haojun Liao 已提交
1310 1311 1312

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1313 1314
}

D
dapan1121 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
void vectorMathMinus(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;

  pOut->numOfRows = pLeft->numOfRows;

  int32_t i = ((_ord) == TSDB_ORDER_ASC)? 0 : (pLeft->numOfRows - 1);
  int32_t step = ((_ord) == TSDB_ORDER_ASC)? 1 : -1;

  int32_t leftConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);

  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);

  double *output = (double *)pOutputCol->pData;
  for (; i < pLeft->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1330
    if (IS_HELPER_NULL(pLeftCol, i)) {
1331
      colDataAppendNULL(pOutputCol, i);
1332
      continue;
1333
    }
1334 1335
    double result = getVectorDoubleValueFnLeft(LEFT_COL, i);
    *output = (result == 0) ? 0 : -result;
D
dapan1121 已提交
1336 1337 1338 1339 1340
  }

  doReleaseVec(pLeftCol,  leftConvert);
}

D
dapan1121 已提交
1341 1342 1343 1344 1345
void vectorAssign(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;

  pOut->numOfRows = pLeft->numOfRows;

wmmhello's avatar
wmmhello 已提交
1346
  if (IS_HELPER_NULL(pRight->columnData, 0)) {
D
dapan1121 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    for (int32_t i = 0; i < pOut->numOfRows; ++i) {
      colDataAppend(pOutputCol, i, NULL, true);
    }
  } else {
    for (int32_t i = 0; i < pOut->numOfRows; ++i) {
      colDataAppend(pOutputCol, i, colDataGetData(pRight->columnData, 0), false);
    }
  }
}

D
dapan1121 已提交
1357
void vectorConcat(SScalarParam* pLeft, SScalarParam* pRight, void *out, int32_t _ord) {
1358
#if 0
1359 1360
  int32_t len = pLeft->bytes + pRight->bytes;

1361
  int32_t i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
1362 1363 1364
  int32_t step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;

  char *output = (char *)out;
1365 1366
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += len) {
1367 1368 1369
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
      char* right = POINTER_SHIFT(pRight->data, pRight->bytes * i);

H
Haojun Liao 已提交
1370
      if (isNull(left, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1371 1372 1373 1374 1375 1376 1377 1378 1379
        setVardataNull(output, TSDB_DATA_TYPE_BINARY);
        continue;
      }

      // todo define a macro
      memcpy(varDataVal(output), varDataVal(left), varDataLen(left));
      memcpy(varDataVal(output) + varDataLen(left), varDataVal(right), varDataLen(right));
      varDataSetLen(output, varDataLen(left) + varDataLen(right));
    }
1380 1381
  } else if (pLeft->numOfRows == 1) {
    for (; i >= 0 && i < pRight->numOfRows; i += step, output += len) {
1382
      char *right = POINTER_SHIFT(pRight->data, pRight->bytes * i);
H
Haojun Liao 已提交
1383
      if (isNull(pLeft->data, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1384 1385 1386 1387 1388 1389 1390 1391
        setVardataNull(output, TSDB_DATA_TYPE_BINARY);
        continue;
      }

      memcpy(varDataVal(output), varDataVal(pLeft->data), varDataLen(pLeft->data));
      memcpy(varDataVal(output) + varDataLen(pLeft->data), varDataVal(right), varDataLen(right));
      varDataSetLen(output, varDataLen(pLeft->data) + varDataLen(right));
    }
1392 1393
  } else if (pRight->numOfRows == 1) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step, output += len) {
1394
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
H
Haojun Liao 已提交
1395
      if (isNull(left, pLeftCol->info.type) || isNull(pRight->data, pRight->info.type)) {
1396 1397 1398 1399 1400 1401 1402 1403 1404
        SET_DOUBLE_NULL(output);
        continue;
      }

      memcpy(varDataVal(output), varDataVal(left), varDataLen(pRight->data));
      memcpy(varDataVal(output) + varDataLen(left), varDataVal(pRight->data), varDataLen(pRight->data));
      varDataSetLen(output, varDataLen(left) + varDataLen(pRight->data));
    }
  }
1405
#endif
1406 1407
}

1408 1409 1410
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);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1411

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

wmmhello's avatar
wmmhello 已提交
1414
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1415
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1416 1417
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1418
      if (IS_HELPER_NULL(pLeftCol, i)) {
1419 1420 1421 1422
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
1423 1424
    }
  }
1425
}
D
dapan1121 已提交
1426

1427 1428
void vectorBitAnd(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1429
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan 已提交
1430

1431 1432
  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 已提交
1433

H
Haojun Liao 已提交
1434 1435 1436
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1437

H
Haojun Liao 已提交
1438 1439
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1440

1441 1442 1443
  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 已提交
1444
      if (IS_NULL) {
1445 1446 1447 1448
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, i);
D
dapan1121 已提交
1449
    }
1450 1451 1452 1453 1454
  } 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 已提交
1455 1456 1457

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1458 1459
}

1460 1461 1462
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);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1463

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

wmmhello's avatar
wmmhello 已提交
1466
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1467
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1468
  } else {
1469
    int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, 0);
1470
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1471
      if (IS_HELPER_NULL(pLeftCol, i)) {
1472 1473 1474 1475
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | rx;
D
dapan1121 已提交
1476 1477
    }
  }
1478
}
D
dapan1121 已提交
1479

1480 1481
void vectorBitOr(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1482
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1483

1484 1485
  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 已提交
1486

H
Haojun Liao 已提交
1487 1488 1489
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1490

H
Haojun Liao 已提交
1491 1492
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan 已提交
1493

1494 1495 1496
  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 已提交
1497
      if (IS_NULL) {
1498 1499 1500 1501
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | getVectorBigintValueFnRight(RIGHT_COL, i);
1502 1503 1504 1505 1506
    }
  } 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 已提交
1507
  }
H
Haojun Liao 已提交
1508 1509 1510

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1511 1512
}

1513 1514 1515 1516 1517 1518 1519 1520 1521
void vectorCompareImpl(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
  int32_t       i = ((_ord) == TSDB_ORDER_ASC) ? 0 : TMAX(pLeft->numOfRows, pRight->numOfRows) - 1;
  int32_t       step = ((_ord) == TSDB_ORDER_ASC) ? 1 : -1;
  __compar_fn_t fp = filterGetCompFunc(GET_PARAM_TYPE(pLeft), optr);

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

  if (pRight->pHashFilter != NULL) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
wmmhello's avatar
wmmhello 已提交
1522
      if (IS_HELPER_NULL(pLeft->columnData, i)) {
D
dapan1121 已提交
1523 1524
        bool  res = false;
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
D
dapan1121 已提交
1525 1526 1527
        continue;
      }

1528 1529
      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter);
1530
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1531
    }
1532
    return;
1533
  }
D
dapan 已提交
1534

1535 1536
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step) {
wmmhello's avatar
wmmhello 已提交
1537
      if (IS_HELPER_NULL(pLeft->columnData, i) || IS_HELPER_NULL(pRight->columnData, i)) {
D
dapan1121 已提交
1538 1539
        bool  res = false;
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1540
        continue;  // TODO set null or ignore
D
dapan1121 已提交
1541 1542
      }

1543
      char *pLeftData = colDataGetData(pLeft->columnData, i);
1544
      char *pRightData = colDataGetData(pRight->columnData, i);
1545 1546 1547

      int64_t leftOut = 0;
      int64_t rightOut = 0;
1548
      bool isJsonnull = false;
wmmhello's avatar
wmmhello 已提交
1549
      bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull);
1550
      if(isJsonnull){
wmmhello's avatar
wmmhello 已提交
1551
        ASSERT(0);
1552
      }
wmmhello's avatar
wmmhello 已提交
1553 1554 1555 1556 1557 1558
      if(!result){
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&result);
      }else{
        bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
      }
D
dapan1121 已提交
1559
    }
1560
  } else if (pRight->numOfRows == 1) {
1561 1562
    ASSERT(pLeft->pHashFilter == NULL);
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
wmmhello's avatar
wmmhello 已提交
1563
      if (IS_HELPER_NULL(pLeft->columnData, i) || IS_HELPER_NULL(pRight->columnData, 0)) {
D
dapan1121 已提交
1564 1565
        bool  res = false;
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1566 1567 1568 1569
        continue;
      }

      char *pLeftData = colDataGetData(pLeft->columnData, i);
1570 1571 1572
      char *pRightData = colDataGetData(pRight->columnData, 0);
      int64_t leftOut = 0;
      int64_t rightOut = 0;
1573
      bool isJsonnull = false;
wmmhello's avatar
wmmhello 已提交
1574
      bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull);
1575
      if(isJsonnull){
wmmhello's avatar
wmmhello 已提交
1576
        ASSERT(0);
1577
      }
wmmhello's avatar
wmmhello 已提交
1578 1579 1580 1581 1582 1583
      if(!result){
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&result);
      }else{
        bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
      }
1584 1585 1586
    }
  } else if (pLeft->numOfRows == 1) {
    for (; i >= 0 && i < pRight->numOfRows; i += step) {
wmmhello's avatar
wmmhello 已提交
1587
      if (IS_HELPER_NULL(pRight->columnData, i) || IS_HELPER_NULL(pLeft->columnData, 0)) {
D
dapan1121 已提交
1588 1589
        bool  res = false;
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1590 1591 1592
        continue;
      }

1593
      char *pLeftData = colDataGetData(pLeft->columnData, 0);
1594
      char *pRightData = colDataGetData(pLeft->columnData, i);
1595 1596
      int64_t leftOut = 0;
      int64_t rightOut = 0;
1597
      bool isJsonnull = false;
wmmhello's avatar
wmmhello 已提交
1598
      bool result = convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull);
1599
      if(isJsonnull){
wmmhello's avatar
wmmhello 已提交
1600
        ASSERT(0);
1601
      }
wmmhello's avatar
wmmhello 已提交
1602 1603 1604 1605 1606 1607
      if(!result){
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&result);
      }else{
        bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
      }
1608
    }
D
dapan1121 已提交
1609 1610 1611
  }
}

D
dapan 已提交
1612
void vectorCompare(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1613 1614
  SScalarParam pLeftOut = {0}; 
  SScalarParam pRightOut = {0};
D
dapan1121 已提交
1615 1616 1617
  
  vectorConvert(pLeft, pRight, &pLeftOut, &pRightOut);

D
dapan1121 已提交
1618 1619
  SScalarParam *param1 = NULL; 
  SScalarParam *param2 = NULL;
D
dapan1121 已提交
1620

1621
  if (pLeftOut.columnData != NULL) {
D
dapan1121 已提交
1622 1623 1624 1625 1626
    param1 = &pLeftOut;
  } else {
    param1 = pLeft;
  }

1627
  if (pRightOut.columnData != NULL) {
D
dapan1121 已提交
1628 1629 1630 1631 1632
    param2 = &pRightOut;
  } else {
    param2 = pRight;
  }

D
dapan 已提交
1633
  vectorCompareImpl(param1, param2, pOut, _ord, optr);
D
dapan1121 已提交
1634 1635
  sclFreeParam(&pLeftOut);
  sclFreeParam(&pRightOut);  
D
dapan1121 已提交
1636 1637
}

D
dapan 已提交
1638 1639
void vectorGreater(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN);
D
dapan1121 已提交
1640 1641
}

D
dapan 已提交
1642 1643
void vectorGreaterEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL);
D
dapan1121 已提交
1644 1645
}

D
dapan 已提交
1646 1647
void vectorLower(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN);
D
dapan1121 已提交
1648 1649
}

D
dapan 已提交
1650 1651
void vectorLowerEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL);
D
dapan1121 已提交
1652 1653
}

D
dapan 已提交
1654 1655
void vectorEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL);
D
dapan1121 已提交
1656 1657
}

D
dapan 已提交
1658 1659
void vectorNotEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL);
D
dapan1121 已提交
1660 1661
}

D
dapan 已提交
1662 1663
void vectorIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN);
D
dapan1121 已提交
1664 1665
}

D
dapan 已提交
1666 1667
void vectorNotIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN);
D
dapan1121 已提交
1668 1669
}

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

D
dapan 已提交
1674 1675
void vectorNotLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE);
D
dapan1121 已提交
1676 1677
}

D
dapan 已提交
1678 1679
void vectorMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH);
D
dapan1121 已提交
1680 1681
}

D
dapan 已提交
1682 1683
void vectorNotMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH);
D
dapan1121 已提交
1684 1685
}

1686 1687 1688 1689
void vectorJsonContains(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_JSON_CONTAINS);
}

D
dapan 已提交
1690
void vectorIsNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1691
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1692
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 1 : 0;
1693
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1694
  }
1695
  pOut->numOfRows = pLeft->numOfRows;
D
dapan1121 已提交
1696 1697
}

D
dapan 已提交
1698
void vectorNotNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1699
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1700
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 0 : 1;
1701
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1702
  }
1703
  pOut->numOfRows = pLeft->numOfRows;
D
dapan 已提交
1704
}
D
dapan1121 已提交
1705

D
dapan 已提交
1706 1707
void vectorIsTrue(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorConvertImpl(pLeft, pOut);
D
dapan1121 已提交
1708 1709
}

1710 1711
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
  switch (binFunctionId) {
D
dapan1121 已提交
1712
    case OP_TYPE_ADD:
1713
      return vectorMathAdd;
D
dapan1121 已提交
1714
    case OP_TYPE_SUB:
1715
      return vectorMathSub;
D
dapan1121 已提交
1716
    case OP_TYPE_MULTI:
1717
      return vectorMathMultiply;
D
dapan1121 已提交
1718
    case OP_TYPE_DIV:
1719
      return vectorMathDivide;
1720
    case OP_TYPE_REM:
1721
      return vectorMathRemainder;
D
dapan1121 已提交
1722 1723
    case OP_TYPE_MINUS:
      return vectorMathMinus;
D
dapan1121 已提交
1724 1725
    case OP_TYPE_ASSIGN:
      return vectorAssign;
D
dapan1121 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
    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 已提交
1750
    case OP_TYPE_IS_NULL:
D
dapan1121 已提交
1751
      return vectorIsNull;
D
dapan1121 已提交
1752
    case OP_TYPE_IS_NOT_NULL:
D
dapan1121 已提交
1753 1754 1755 1756 1757
      return vectorNotNull;
    case OP_TYPE_BIT_AND:
      return vectorBitAnd;
    case OP_TYPE_BIT_OR:
      return vectorBitOr;
D
dapan1121 已提交
1758 1759
    case OP_TYPE_IS_TRUE:
      return vectorIsTrue;
1760 1761 1762 1763
    case OP_TYPE_JSON_GET_VALUE:
      return vectorJsonArrow;
    case OP_TYPE_JSON_CONTAINS:
      return vectorJsonContains;
1764
    default:
1765
      ASSERT(0);
1766 1767
      return NULL;
  }
D
dapan1121 已提交
1768
}
1769