sclvector.c 56.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"
27

28 29 30
#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))

D
dapan1121 已提交
31 32 33 34 35 36 37 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
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);
}
D
dapan1121 已提交
63 64
_getBigintValue_fn_t getVectorBigintValueFn(int32_t srcType) {
    _getBigintValue_fn_t p = NULL;
D
dapan1121 已提交
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
    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 {
        assert(0);
    }
    return p;
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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 已提交
123 124 125 126
void* getVectorValueAddr_default(void *src, int32_t index) {
  return src;
}
void* getVectorValueAddr_VAR(void *src, int32_t index) {
H
Haojun Liao 已提交
127
  return colDataGetData((SColumnInfoData *)src, index);
D
dapan1121 已提交
128
}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

_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 已提交
152 153 154 155
    }else if(srcType==TSDB_DATA_TYPE_BINARY) {
        p = getVectorValueAddr_VAR;
    }else if(srcType==TSDB_DATA_TYPE_NCHAR) {
        p = getVectorValueAddr_VAR;
156
    }else {
D
dapan1121 已提交
157
        p = getVectorValueAddr_default;
158 159 160 161
    }
    return p;
}

162
static FORCE_INLINE void varToSigned(char *buf, SScalarParam* pOut, int32_t rowIndex) {
D
dapan1121 已提交
163
  int64_t value = strtoll(buf, NULL, 10);
164
  colDataAppendInt64(pOut->columnData, rowIndex, &value);
D
dapan1121 已提交
165 166
}

167
static FORCE_INLINE void varToUnsigned(char *buf, SScalarParam* pOut, int32_t rowIndex) {
D
dapan1121 已提交
168
  uint64_t value = strtoull(buf, NULL, 10);
169
  colDataAppendInt64(pOut->columnData, rowIndex, (int64_t*) &value);
D
dapan1121 已提交
170 171
}

172
static FORCE_INLINE void varToFloat(char *buf, SScalarParam* pOut, int32_t rowIndex) {
D
dapan 已提交
173
  double value = strtod(buf, NULL);
174
  colDataAppendDouble(pOut->columnData, rowIndex, &value);
175 176 177 178 179
}

static FORCE_INLINE void varToBool(char *buf, SScalarParam* pOut, int32_t rowIndex) {
  int64_t value = strtoll(buf, NULL, 10);
  bool v = (value != 0)? true:false;
180
  colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*) &v);
D
dapan1121 已提交
181 182
}

183 184 185 186 187 188 189 190 191 192 193
//static FORCE_INLINE void varToNchar(char* buf, SScalarParam* pOut, int32_t rowIndex) {
//  int32_t len = 0;
//  int32_t inputLen = varDataLen(buf);
//
//  char* t = taosMemoryCalloc(1,(inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE);
//  /*int32_t resLen = */taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4*) varDataVal(t), pOut->columnData->info.bytes, &len);
//  varDataSetLen(t, len);
//
//  colDataAppend(pOut->columnData, rowIndex, t, false);
//  taosMemoryFree(t);
//}
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
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));
250 251 252
  int len = taosUcs4ToMbs((TdUcs4 *)varDataVal(inData), varDataLen(inData), tmp);
  if (len < 0) {
    sclError("castConvert taosUcs4ToMbs error 1");
253 254
  }

255 256
  tmp[len] = 0;

257 258 259 260 261 262 263
  ASSERT(outType == TSDB_DATA_TYPE_DOUBLE);
  double value = strtod(tmp, NULL);

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

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
//TODO opt performance, tmp is not needed.
int32_t vectorConvertFromVarData(const SScalarParam* pIn, SScalarParam* pOut, int32_t inType, int32_t outType) {
  ASSERT(!IS_VAR_DATA_TYPE(outType));
  int32_t bufSize = pIn->columnData->info.bytes;
  char *tmp = taosMemoryMalloc(bufSize + VARSTR_HEADER_SIZE);

//  bool vton = false;

  _bufConverteFunc func = NULL;
  if (TSDB_DATA_TYPE_BOOL == outType) {
    func = varToBool;
  } else if (IS_SIGNED_NUMERIC_TYPE(outType) || TSDB_DATA_TYPE_TIMESTAMP == outType) {
    func = varToSigned;
  } else if (IS_UNSIGNED_NUMERIC_TYPE(outType)) {
    func = varToUnsigned;
  } else if (IS_FLOAT_TYPE(outType)) {
    func = varToFloat;
//  } else if (outType == TSDB_DATA_TYPE_NCHAR) {   // can not be nchar or binary
//    func = varToNchar;
//    vton = true;
  } 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) {
    if (colDataIsNull_s(pIn->columnData, i)) {
      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) {
        colDataAppendNULL(pOut->columnData, i);
        continue;
      }
      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;
      }
    }
//    if (vton) {
//      memcpy(tmp, data, varDataTLen(data));
//    } else {
      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;
      }
//    }
    
    (*func)(tmp, pOut, i);
  }
  
  taosMemoryFreeClear(tmp);
  return TSDB_CODE_SUCCESS;
}

338
double getVectorDoubleValue_JSON(void *src, int32_t index){
339 340
  ASSERT(!colDataIsNull_var(((SColumnInfoData*)src), index));
  char *data = colDataGetVarData((SColumnInfoData*)src, index);
341
  double out = 0;
342
  if(*data == TSDB_DATA_TYPE_NCHAR) {   // json inner type can not be BINARY
343 344 345 346 347 348 349
    convertStringToDouble(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  } {
    convertNumberToNumber(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  }
  return out;
}

350
void convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData, char **pRightData, void *pLeftOut, void *pRightOut, bool *isNull){
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  if(optr == OP_TYPE_JSON_CONTAINS) {
    return;
  }

  if(typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON){
    return;
  }

  if(typeLeft == TSDB_DATA_TYPE_JSON){
    typeLeft = **pLeftData;
    (*pLeftData) ++;
  }
  if(typeRight == TSDB_DATA_TYPE_JSON){
    typeRight = **pRightData;
    (*pRightData) ++;
  }
367 368 369 370
  if(typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL){
    *isNull = true;
    return;
  }
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  int8_t type = vectorGetConvertType(typeLeft, typeRight);

  if(type == 0) {
    *fp = filterGetCompFunc(typeLeft, optr);
    return;
  }

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

388
  if(typeRight == TSDB_DATA_TYPE_NCHAR) {
389 390 391 392 393 394 395 396
    convertStringToDouble(*pRightData, pRightOut, typeRight, type);
    *pRightData = pRightOut;
  } else if(typeRight != type) {
    convertNumberToNumber(*pRightData, pRightOut, typeRight, type);
    *pRightData = pRightOut;
  }
}

397
// TODO opt performance
H
Haojun Liao 已提交
398
int32_t vectorConvertImpl(const SScalarParam* pIn, SScalarParam* pOut) {
399 400
  SColumnInfoData* pInputCol  = pIn->columnData;
  SColumnInfoData* pOutputCol = pOut->columnData;
H
Haojun Liao 已提交
401 402

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

405
  if (IS_VAR_DATA_TYPE(inType)) {
D
dapan 已提交
406 407 408
    return vectorConvertFromVarData(pIn, pOut, inType, outType);
  }
  
D
dapan1121 已提交
409
  switch (outType) {
410 411 412
    case TSDB_DATA_TYPE_BOOL: {
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
413
          colDataAppendNULL(pOutputCol, i);
414 415 416 417
          continue;
        }

        bool value = 0;
D
dapan1121 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
        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);
459 460 461
      }
      break;
    }
D
dapan1121 已提交
462
    case TSDB_DATA_TYPE_BIGINT:
463
    case TSDB_DATA_TYPE_TIMESTAMP: {
464 465
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
466
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
467
          continue;
D
dapan1121 已提交
468
        }
D
dapan 已提交
469 470

        int64_t value = 0;
471
        GET_TYPED_DATA(value, int64_t, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
472
        colDataAppendInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
473 474
      }
      break;
475
    }
D
dapan1121 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    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: {
516 517
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
518
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
519
          continue;
D
dapan1121 已提交
520 521
        }
        
D
dapan 已提交
522
        uint64_t value = 0;
523
        GET_TYPED_DATA(value, uint64_t, inType, colDataGetData(pInputCol, i));
524
        colDataAppendInt64(pOutputCol, i, (int64_t*)&value);
D
dapan1121 已提交
525 526
      }
      break;
D
dapan1121 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    }
    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: {
542 543
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
544
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
545
          continue;
D
dapan1121 已提交
546 547
        }
        
D
dapan 已提交
548
        double value = 0;
549
        GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
550
        colDataAppendDouble(pOutputCol, i, (double*)&value);
D
dapan1121 已提交
551
      }
D
dapan1121 已提交
552 553
      break;  
    }
D
dapan1121 已提交
554
    default:
D
dapan1121 已提交
555
      sclError("invalid convert output type:%d", outType);
D
dapan1121 已提交
556 557 558 559 560 561 562
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

int8_t gConvertTypes[TSDB_DATA_TYPE_BLOB+1][TSDB_DATA_TYPE_BLOB+1] = {
H
Haojun Liao 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
/*         NULL BOOL TINY SMAL INT  BIG  FLOA DOUB VARC TIME NCHA UTIN USMA UINT UBIG VARB JSON 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,   0,   3,   4,   5,   6,   7,   7,   9,   7,   0,   12,  13,  14,  7,   0,   0,   0,
/*TINY*/   0,   0,   0,   3,   4,   5,   6,   7,   7,   9,   7,   3,   4,   5,   7,   7,   0,   0,   0,
/*SMAL*/   0,   0,   0,   0,   4,   5,   6,   7,   7,   9,   7,   3,   4,   5,   7,   7,   0,   0,   0,
/*INT */   0,   0,   0,   0,   0,   5,   6,   7,   7,   9,   7,   4,   4,   5,   7,   7,   0,   0,   0,
/*BIGI*/   0,   0,   0,   0,   0,   0,   6,   7,   7,   0,   7,   5,   5,   5,   7,   7,   0,   0,   0,
/*FLOA*/   0,   0,   0,   0,   0,   0,   0,   7,   7,   6,   7,   6,   6,   6,   6,   7,   0,   0,   0,
/*DOUB*/   0,   0,   0,   0,   0,   0,   0,   0,   7,   7,   7,   7,   7,   7,   7,   7,   0,   0,   0,
/*VARC*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,   0,   7,   7,   7,   7,   0,   0,   0,   0,
/*TIME*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,   9,   9,   9,   7,   7,   0,   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,  7,   0,   0,   0,
/*USMA*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   13,  14,  7,   0,   0,   0,
/*UINT*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   14,  7,   0,   0,   0,
/*UBIG*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,   0,   0,   0,
/*VARB*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
/*JSON*/   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 已提交
583 584
};

D
dapan1121 已提交
585 586 587 588 589 590 591 592 593 594 595 596
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 已提交
597
int32_t vectorConvert(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam* pLeftOut, SScalarParam* pRightOut) {
598
  if (pLeft->pHashFilter != NULL || pRight->pHashFilter != NULL) {
D
dapan1121 已提交
599 600 601
    return TSDB_CODE_SUCCESS;
  }

602 603 604
  int32_t leftType  = GET_PARAM_TYPE(pLeft);
  int32_t rightType = GET_PARAM_TYPE(pRight);
  if (leftType == rightType) {
D
dapan1121 已提交
605 606 607
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
608 609
  SScalarParam *param1 = NULL, *paramOut1 = NULL; 
  SScalarParam *param2 = NULL, *paramOut2 = NULL;
D
dapan1121 已提交
610 611
  int32_t code = 0;
  
612
  if (leftType < rightType) {
D
dapan1121 已提交
613 614 615 616 617 618 619 620 621 622 623
    param1 = pLeft;
    param2 = pRight;
    paramOut1 = pLeftOut;
    paramOut2 = pRightOut;
  } else {
    param1 = pRight;
    param2 = pLeft;
    paramOut1 = pRightOut;
    paramOut2 = pLeftOut;
  }

624
  int8_t type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
D
dapan1121 已提交
625 626 627 628
  if (0 == type) {
    return TSDB_CODE_SUCCESS;
  }

629 630 631 632 633 634 635
  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 已提交
636
    }
637

D
dapan1121 已提交
638 639
    code = vectorConvertImpl(param1, paramOut1);
    if (code) {
H
Haojun Liao 已提交
640
//      taosMemoryFreeClear(paramOut1->data);
D
dapan1121 已提交
641 642 643 644
      return code;
    }
  }
  
645 646 647 648 649 650 651 652 653
  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 已提交
654 655 656 657 658 659 660 661 662
    code = vectorConvertImpl(param2, paramOut2);
    if (code) {
      return code;
    }
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
663 664 665 666 667 668
enum {
  VECTOR_DO_CONVERT = 0x1,
  VECTOR_UN_CONVERT = 0x2,
};

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

671
  if (IS_VAR_DATA_TYPE(pCol->info.type) || pCol->info.type != TSDB_DATA_TYPE_JSON) {
H
Haojun Liao 已提交
672
    pDest->numOfRows = pParam->numOfRows;
673 674

    SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
H
Haojun Liao 已提交
675 676
    pDest->columnData = createColumnInfoData(&t, pParam->numOfRows);
    if (pDest->columnData == NULL) {
677 678
      sclError("malloc %d failed", (int32_t)(pParam->numOfRows * sizeof(double)));
      return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
679 680
    }

H
Haojun Liao 已提交
681
    int32_t code = vectorConvertImpl(pParam, pDest);
682 683
    if (code != TSDB_CODE_SUCCESS) {
      return code;
D
dapan1121 已提交
684
    }
685

H
Haojun Liao 已提交
686 687 688
    *convert = VECTOR_DO_CONVERT;
  } else {
    *convert = VECTOR_UN_CONVERT;
D
dapan1121 已提交
689
  }
690 691 692 693 694 695 696 697 698 699 700 701

  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;

  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
702
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
703 704
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
705 706 707 708 709 710
      if (colDataIsNull_s(pLeftCol, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                + getVectorDoubleValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
711
    }
712 713 714
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
715 716
    }
  }
717
}
D
dapan1121 已提交
718

H
Haojun Liao 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
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);
  }
}

741 742 743 744 745 746 747 748 749 750 751 752
char *getJsonValue(char *json, char *key){    //todo
  return NULL;
}

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

753
  char *pRightData = colDataGetVarData(pRight->columnData, 0);
754
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
755 756
    if (colDataIsNull_var(pLeft->columnData, i)) {
      colDataSetNull_var(pOutputCol, i);
757 758 759
      pOutputCol->hasNull = true;
      continue;
    }
760
    char *pLeftData = colDataGetVarData(pLeft->columnData, i);
761 762
    char *value = getJsonValue(pLeftData, pRightData);
    if (!value || *value == TSDB_DATA_TYPE_NULL) {
763
      colDataSetNull_var(pOutputCol, i);
764 765 766 767 768 769 770
      pOutputCol->hasNull = true;
      continue;
    }
    colDataAppend(pOutputCol, i, pLeftData, false);
  }
}

771 772 773 774 775 776
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;

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

H
Haojun Liao 已提交
779 780 781
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
782

783 784 785 786 787 788
  _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) {
789 790 791 792
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
793 794
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                + getVectorDoubleValueFnRight(RIGHT_COL, i);
795 796 797 798 799 800 801
    }

    pOutputCol->hasNull = (pLeftCol->hasNull || pRightCol->hasNull);
    if (pOutputCol->hasNull) {
      int32_t numOfBitLen = BitmapLen(pLeft->numOfRows);
      for (int32_t j = 0; j < numOfBitLen; ++j) {
        pOutputCol->nullbitmap[j] = pLeftCol->nullbitmap[j] | pRightCol->nullbitmap[j];
D
dapan1121 已提交
802
      }
803
    }
D
dapan1121 已提交
804

805 806 807 808 809
  } 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);
  }
H
Haojun Liao 已提交
810 811 812

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
813
}
D
dapan1121 已提交
814

815 816 817 818 819 820 821 822
// 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;

  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
823
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
824 825
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
826 827 828 829 830 831
      if (colDataIsNull_s(pLeftCol, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = (getVectorDoubleValueFnLeft(LEFT_COL, i)
                 - getVectorDoubleValueFnRight(RIGHT_COL, 0)) * factor;
D
dapan1121 已提交
832
    }
833 834 835 836 837 838
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
    }
  }
}
D
dapan1121 已提交
839

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

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

845 846
  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 已提交
847

H
Haojun Liao 已提交
848 849 850
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
851

852 853 854 855 856 857
  _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) {
858 859 860 861
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
862 863
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                - getVectorDoubleValueFnRight(RIGHT_COL, i);
D
dapan1121 已提交
864 865
    }

866 867 868 869 870
    pOutputCol->hasNull = (pLeftCol->hasNull || pRightCol->hasNull);
    if (pOutputCol->hasNull) {
      int32_t numOfBitLen = BitmapLen(pLeft->numOfRows);
      for (int32_t j = 0; j < numOfBitLen; ++j) {
        pOutputCol->nullbitmap[j] = pLeftCol->nullbitmap[j] | pRightCol->nullbitmap[j];
D
dapan1121 已提交
871 872 873
      }
    }

874 875 876 877 878
  } 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);
  }
H
Haojun Liao 已提交
879 880 881

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
882 883
}

884 885 886 887
// 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 已提交
888

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

891
  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
892
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
893 894
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
895 896 897 898 899 900
      if (colDataIsNull_s(pLeftCol, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                * getVectorDoubleValueFnRight(RIGHT_COL, 0);
901 902 903 904 905 906
    }
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
    }
  }
D
dapan1121 已提交
907 908
}

909 910
void vectorMathMultiply(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
911
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
912 913 914 915

  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 已提交
916 917 918
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
919 920 921 922 923 924 925

  _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) {
926 927 928 929
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
930 931
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                * getVectorDoubleValueFnRight(RIGHT_COL, i);
932 933 934 935 936 937 938 939 940 941 942 943 944 945
    }

    pOutputCol->hasNull = (pLeftCol->hasNull || pRightCol->hasNull);
    if (pOutputCol->hasNull) {
      int32_t numOfBitLen = BitmapLen(pLeft->numOfRows);
      for (int32_t j = 0; j < numOfBitLen; ++j) {
        pOutputCol->nullbitmap[j] = pLeftCol->nullbitmap[j] | pRightCol->nullbitmap[j];
      }
    }

  } 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 已提交
946
  }
H
Haojun Liao 已提交
947 948 949

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
950 951
}

952 953
void vectorMathDivide(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
954
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
955

956 957 958
  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 已提交
959 960 961
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
962

963 964 965 966 967 968
  _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) {  // check for the 0 value
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
969 970 971 972
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
973 974
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                 /getVectorDoubleValueFnRight(RIGHT_COL, i);
975 976 977 978 979 980 981 982 983 984 985
    }

    pOutputCol->hasNull = (pLeftCol->hasNull || pRightCol->hasNull);
    if (pOutputCol->hasNull) {
      int32_t numOfBitLen = BitmapLen(pLeft->numOfRows);
      for (int32_t j = 0; j < numOfBitLen; ++j) {
        pOutputCol->nullbitmap[j] = pLeftCol->nullbitmap[j] | pRightCol->nullbitmap[j];
      }
    }

  } else if (pLeft->numOfRows == 1) {
986
    if (colDataIsNull_s(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
987
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
988 989
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
990 991 992 993 994 995
        if (colDataIsNull_s(pRightCol, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, 0)
                  / getVectorDoubleValueFnRight(RIGHT_COL, i);
996 997 998 999 1000 1001 1002
      }
      pOutputCol->hasNull = pRightCol->hasNull;
      if (pOutputCol->hasNull) {
        memcpy(pOutputCol->nullbitmap, pRightCol->nullbitmap, BitmapLen(pRight->numOfRows));
      }
    }
  } else if (pRight->numOfRows == 1) {
1003
    if (colDataIsNull_s(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1004
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1005 1006
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
1007 1008 1009 1010 1011 1012
        if (colDataIsNull_s(pLeftCol, i)) {
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                  / getVectorDoubleValueFnRight(RIGHT_COL, 0);
1013 1014 1015 1016 1017 1018 1019
      }
      pOutputCol->hasNull = pLeftCol->hasNull;
      if (pOutputCol->hasNull) {
        memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(pLeft->numOfRows));
      }
    }
  }
H
Haojun Liao 已提交
1020 1021 1022

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1023 1024
}

1025 1026
void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1027
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1028 1029 1030

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

H
Haojun Liao 已提交
1032 1033 1034
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
1035

1036 1037 1038 1039 1040 1041 1042 1043
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

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

  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
1044
      if (colDataIsNull_s(pLeftCol, i) || colDataIsNull_s(pRightCol, i)) {
1045
        colDataAppendNULL(pOutputCol, i);
1046 1047 1048
        continue;
      }

1049 1050
      double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
      double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
H
Haojun Liao 已提交
1051
      if (isnan(lx) || isinf(lx) || isnan(rx) || isinf(rx)) {
1052
        colDataAppendNULL(pOutputCol, i);
1053 1054 1055
        continue;
      }

1056
      *output = lx - ((int64_t)(lx / rx)) * rx;
1057 1058
    }
  } else if (pLeft->numOfRows == 1) {
1059 1060
    double lx = getVectorDoubleValueFnLeft(LEFT_COL, 0);
    if (colDataIsNull_s(pLeftCol, 0) || isnan(lx) || isinf(lx)) {  // Set pLeft->numOfRows NULL value
1061
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1062 1063
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
1064
        if (colDataIsNull_s(pRightCol, i)) {
1065
          colDataAppendNULL(pOutputCol, i);
1066 1067 1068
          continue;
        }

1069
        double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
H
Haojun Liao 已提交
1070
        if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
1071
          colDataAppendNULL(pOutputCol, i);
1072 1073 1074
          continue;
        }

1075
        *output = lx - ((int64_t)(lx / rx)) * rx;
1076 1077 1078
      }
    }
  } else if (pRight->numOfRows == 1) {
1079 1080
    double rx = getVectorDoubleValueFnRight(RIGHT_COL, 0);
    if (colDataIsNull_s(pRightCol, 0) || FLT_EQUAL(rx, 0)) {  // Set pLeft->numOfRows NULL value
1081
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1082 1083
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
1084
        if (colDataIsNull_s(pLeftCol, i)) {
1085
          colDataAppendNULL(pOutputCol, i);
1086 1087 1088
          continue;
        }

1089
        double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
H
Haojun Liao 已提交
1090
        if (isnan(lx) || isinf(lx)) {
1091
          colDataAppendNULL(pOutputCol, i);
1092 1093 1094
          continue;
        }

1095
        *output = lx - ((int64_t)(lx / rx)) * rx;
1096 1097 1098
      }
    }
  }
H
Haojun Liao 已提交
1099 1100 1101

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1102 1103
}

D
dapan1121 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
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) {
1119 1120 1121 1122
    if (colDataIsNull_s(pLeft->columnData, i)) {
      colDataAppendNULL(pOutputCol, i);
      continue;  // TODO set null or ignore
    }
1123
    *output = - getVectorDoubleValueFnLeft(LEFT_COL, i);
D
dapan1121 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
  }

  pOutputCol->hasNull = pLeftCol->hasNull;
  if (pOutputCol->hasNull) {
    memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(pLeft->numOfRows));
  }

  doReleaseVec(pLeftCol,  leftConvert);
}

D
dapan1121 已提交
1134
void vectorConcat(SScalarParam* pLeft, SScalarParam* pRight, void *out, int32_t _ord) {
1135
#if 0
1136 1137
  int32_t len = pLeft->bytes + pRight->bytes;

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

  char *output = (char *)out;
1142 1143
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += len) {
1144 1145 1146
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
      char* right = POINTER_SHIFT(pRight->data, pRight->bytes * i);

H
Haojun Liao 已提交
1147
      if (isNull(left, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1148 1149 1150 1151 1152 1153 1154 1155 1156
        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));
    }
1157 1158
  } else if (pLeft->numOfRows == 1) {
    for (; i >= 0 && i < pRight->numOfRows; i += step, output += len) {
1159
      char *right = POINTER_SHIFT(pRight->data, pRight->bytes * i);
H
Haojun Liao 已提交
1160
      if (isNull(pLeft->data, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1161 1162 1163 1164 1165 1166 1167 1168
        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));
    }
1169 1170
  } else if (pRight->numOfRows == 1) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step, output += len) {
1171
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
H
Haojun Liao 已提交
1172
      if (isNull(left, pLeftCol->info.type) || isNull(pRight->data, pRight->info.type)) {
1173 1174 1175 1176 1177 1178 1179 1180 1181
        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));
    }
  }
1182
#endif
1183 1184
}

1185 1186 1187
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 已提交
1188

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

1191
  if (colDataIsNull_s(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1192
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1193 1194
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
1195 1196 1197 1198 1199
      if (colDataIsNull_s(pLeftCol, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
1200
    }
1201 1202 1203
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
1204 1205
    }
  }
1206
}
D
dapan1121 已提交
1207

1208 1209
void vectorBitAnd(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1210
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan 已提交
1211

1212 1213
  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 已提交
1214

H
Haojun Liao 已提交
1215 1216 1217
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1218

H
Haojun Liao 已提交
1219 1220
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1221

1222 1223 1224
  int64_t *output = (int64_t *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
1225 1226 1227 1228 1229
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, i);
D
dapan1121 已提交
1230
    }
D
dapan1121 已提交
1231

1232 1233 1234 1235 1236 1237
    pOutputCol->hasNull = (pLeftCol->hasNull || pRightCol->hasNull);
    if (pOutputCol->hasNull) {
      int32_t numOfBitLen = BitmapLen(pLeft->numOfRows);
      for (int32_t j = 0; j < numOfBitLen; ++j) {
        pOutputCol->nullbitmap[j] = pLeftCol->nullbitmap[j] & pRightCol->nullbitmap[j];
      }
D
dapan1121 已提交
1238
    }
D
dapan1121 已提交
1239

1240 1241 1242 1243 1244
  } 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 已提交
1245 1246 1247

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1248 1249
}

1250 1251 1252
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 已提交
1253

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

1256
  if (colDataIsNull_s(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1257
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1258
  } else {
1259
    int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, 0);
1260
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
1261 1262 1263 1264 1265
      if (colDataIsNull_s(pLeftCol, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | rx;
D
dapan1121 已提交
1266
    }
1267 1268 1269
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
1270 1271
    }
  }
1272
}
D
dapan1121 已提交
1273

1274 1275
void vectorBitOr(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1276
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1277

1278 1279
  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 已提交
1280

H
Haojun Liao 已提交
1281 1282 1283
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1284

H
Haojun Liao 已提交
1285 1286
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan 已提交
1287

1288 1289 1290
  int64_t *output = (int64_t *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
1291 1292 1293 1294 1295
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | getVectorBigintValueFnRight(RIGHT_COL, i);
1296
    }
D
dapan1121 已提交
1297

1298 1299 1300 1301 1302 1303
    pOutputCol->hasNull = (pLeftCol->hasNull || pRightCol->hasNull);
    if (pOutputCol->hasNull) {
      int32_t numOfBitLen = BitmapLen(pLeft->numOfRows);
      for (int32_t j = 0; j < numOfBitLen; ++j) {
        pOutputCol->nullbitmap[j] = pLeftCol->nullbitmap[j] | pRightCol->nullbitmap[j];
      }
D
dapan1121 已提交
1304
    }
1305 1306 1307 1308
  } 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 已提交
1309
  }
H
Haojun Liao 已提交
1310 1311 1312

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1313 1314
}

1315 1316 1317 1318 1319 1320 1321 1322 1323
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) {
1324
      if (colDataIsNull_s(pLeft->columnData, i)) {
D
dapan1121 已提交
1325 1326 1327
        continue;
      }

1328 1329
      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter);
1330
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1331
    }
1332
    return;
1333
  }
D
dapan 已提交
1334

1335 1336
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step) {
1337
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
1338
        continue;  // TODO set null or ignore
D
dapan1121 已提交
1339 1340
      }

1341
      char *pLeftData = colDataGetData(pLeft->columnData, i);
1342
      char *pRightData = colDataGetData(pRight->columnData, i);
1343 1344 1345

      int64_t leftOut = 0;
      int64_t rightOut = 0;
1346 1347 1348 1349 1350
      bool isJsonnull = false;
      convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull);
      if(isJsonnull){
        continue;  // TODO set null or ignore
      }
1351
      bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
1352
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
D
dapan1121 已提交
1353
    }
1354
  } else if (pRight->numOfRows == 1) {
1355 1356
    ASSERT(pLeft->pHashFilter == NULL);
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
1357
      if (colDataIsNull_s(pLeft->columnData, i)) {
1358 1359 1360 1361
        continue;
      }

      char *pLeftData = colDataGetData(pLeft->columnData, i);
1362 1363 1364
      char *pRightData = colDataGetData(pRight->columnData, 0);
      int64_t leftOut = 0;
      int64_t rightOut = 0;
1365 1366 1367 1368 1369
      bool isJsonnull = false;
      convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull);
      if(isJsonnull){
        continue;  // TODO set null or ignore
      }
1370
      bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
1371
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1372 1373 1374
    }
  } else if (pLeft->numOfRows == 1) {
    for (; i >= 0 && i < pRight->numOfRows; i += step) {
1375
      if (colDataIsNull_s(pRight->columnData, i)) {
1376 1377 1378
        continue;
      }

1379
      char *pLeftData = colDataGetData(pLeft->columnData, 0);
1380
      char *pRightData = colDataGetData(pLeft->columnData, i);
1381 1382
      int64_t leftOut = 0;
      int64_t rightOut = 0;
1383 1384 1385 1386 1387
      bool isJsonnull = false;
      convertJsonValue(&fp, optr, GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), &pLeftData, &pRightData, &leftOut, &rightOut, &isJsonnull);
      if(isJsonnull){
        continue;  // TODO set null or ignore
      }
1388
      bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
1389
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1390
    }
D
dapan1121 已提交
1391 1392 1393
  }
}

D
dapan 已提交
1394
void vectorCompare(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1395 1396
  SScalarParam pLeftOut = {0}; 
  SScalarParam pRightOut = {0};
D
dapan1121 已提交
1397 1398 1399
  
  vectorConvert(pLeft, pRight, &pLeftOut, &pRightOut);

D
dapan1121 已提交
1400 1401
  SScalarParam *param1 = NULL; 
  SScalarParam *param2 = NULL;
D
dapan1121 已提交
1402

1403
  if (pLeftOut.columnData != NULL) {
D
dapan1121 已提交
1404 1405 1406 1407 1408
    param1 = &pLeftOut;
  } else {
    param1 = pLeft;
  }

1409
  if (pRightOut.columnData != NULL) {
D
dapan1121 已提交
1410 1411 1412 1413 1414
    param2 = &pRightOut;
  } else {
    param2 = pRight;
  }

D
dapan 已提交
1415
  vectorCompareImpl(param1, param2, pOut, _ord, optr);
D
dapan1121 已提交
1416 1417
  sclFreeParam(&pLeftOut);
  sclFreeParam(&pRightOut);  
D
dapan1121 已提交
1418 1419
}

D
dapan 已提交
1420 1421
void vectorGreater(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN);
D
dapan1121 已提交
1422 1423
}

D
dapan 已提交
1424 1425
void vectorGreaterEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL);
D
dapan1121 已提交
1426 1427
}

D
dapan 已提交
1428 1429
void vectorLower(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN);
D
dapan1121 已提交
1430 1431
}

D
dapan 已提交
1432 1433
void vectorLowerEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL);
D
dapan1121 已提交
1434 1435
}

D
dapan 已提交
1436 1437
void vectorEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL);
D
dapan1121 已提交
1438 1439
}

D
dapan 已提交
1440 1441
void vectorNotEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL);
D
dapan1121 已提交
1442 1443
}

D
dapan 已提交
1444 1445
void vectorIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN);
D
dapan1121 已提交
1446 1447
}

D
dapan 已提交
1448 1449
void vectorNotIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN);
D
dapan1121 已提交
1450 1451
}

D
dapan 已提交
1452 1453
void vectorLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE);
D
dapan1121 已提交
1454 1455
}

D
dapan 已提交
1456 1457
void vectorNotLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE);
D
dapan1121 已提交
1458 1459
}

D
dapan 已提交
1460 1461
void vectorMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH);
D
dapan1121 已提交
1462 1463
}

D
dapan 已提交
1464 1465
void vectorNotMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH);
D
dapan1121 已提交
1466 1467
}

1468 1469 1470 1471
void vectorJsonContains(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_JSON_CONTAINS);
}

D
dapan 已提交
1472
void vectorIsNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1473
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
1474
    int8_t v = colDataIsNull_s(pLeft->columnData, i)? 1:0;
1475 1476 1477 1478
    if (v && pLeft->columnData->info.type == TSDB_DATA_TYPE_JSON){
      char *data = colDataGetVarData(pLeft->columnData, i);
      v = (*data == TSDB_DATA_TYPE_NULL)? 1:0;
    }
1479
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1480
  }
1481
  pOut->numOfRows = pLeft->numOfRows;
D
dapan1121 已提交
1482 1483
}

D
dapan 已提交
1484
void vectorNotNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1485
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
1486
    int8_t v = colDataIsNull_s(pLeft->columnData, i)? 0:1;
1487 1488 1489 1490
    if (v && pLeft->columnData->info.type == TSDB_DATA_TYPE_JSON){
      char *data = colDataGetVarData(pLeft->columnData, i);
      v = (*data == TSDB_DATA_TYPE_NULL)? 0:1;
    }
1491
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1492
  }
1493
  pOut->numOfRows = pLeft->numOfRows;
D
dapan 已提交
1494
}
D
dapan1121 已提交
1495

D
dapan 已提交
1496 1497
void vectorIsTrue(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorConvertImpl(pLeft, pOut);
D
dapan1121 已提交
1498 1499
}

1500 1501
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
  switch (binFunctionId) {
D
dapan1121 已提交
1502
    case OP_TYPE_ADD:
1503
      return vectorMathAdd;
D
dapan1121 已提交
1504
    case OP_TYPE_SUB:
1505
      return vectorMathSub;
D
dapan1121 已提交
1506
    case OP_TYPE_MULTI:
1507
      return vectorMathMultiply;
D
dapan1121 已提交
1508
    case OP_TYPE_DIV:
1509
      return vectorMathDivide;
D
dapan1121 已提交
1510
    case OP_TYPE_MOD:
1511
      return vectorMathRemainder;
D
dapan1121 已提交
1512 1513
    case OP_TYPE_MINUS:
      return vectorMathMinus;
D
dapan1121 已提交
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    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 已提交
1538
    case OP_TYPE_IS_NULL:
D
dapan1121 已提交
1539
      return vectorIsNull;
D
dapan1121 已提交
1540
    case OP_TYPE_IS_NOT_NULL:
D
dapan1121 已提交
1541 1542 1543 1544 1545
      return vectorNotNull;
    case OP_TYPE_BIT_AND:
      return vectorBitAnd;
    case OP_TYPE_BIT_OR:
      return vectorBitOr;
D
dapan1121 已提交
1546 1547
    case OP_TYPE_IS_TRUE:
      return vectorIsTrue;
1548 1549 1550 1551
    case OP_TYPE_JSON_GET_VALUE:
      return vectorJsonArrow;
    case OP_TYPE_JSON_CONTAINS:
      return vectorJsonContains;
1552 1553 1554 1555
    default:
      assert(0);
      return NULL;
  }
D
dapan1121 已提交
1556
}