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

#include "os.h"

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

30 31 32
#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 已提交
33 34 35 36 37 38
#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))

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

92
void convertNcharToDouble(const void *inData, void *outData){
93 94 95 96 97 98 99 100
  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;

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

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

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

D
dapan1121 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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);
}
151 152 153 154
int64_t getVectorBigintValue_BOOL(void *src, int32_t index) {
  return (int64_t)*((bool *)src + index);
}

155 156 157 158 159 160 161
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
162
    convertNcharToDouble(data+CHAR_BYTES, &out);
wmmhello's avatar
wmmhello 已提交
163 164 165
  } else if(tTagIsJson(data)){
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
    return 0;
166 167 168 169 170 171
  } else {
    convertNumberToNumber(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  }
  return (int64_t)out;
}

D
dapan1121 已提交
172 173
_getBigintValue_fn_t getVectorBigintValueFn(int32_t srcType) {
    _getBigintValue_fn_t p = NULL;
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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 {
203
      ASSERT(0);
D
dapan1121 已提交
204 205 206 207
    }
    return p;
}

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
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 已提交
240 241 242 243
void* getVectorValueAddr_default(void *src, int32_t index) {
  return src;
}
void* getVectorValueAddr_VAR(void *src, int32_t index) {
H
Haojun Liao 已提交
244
  return colDataGetData((SColumnInfoData *)src, index);
D
dapan1121 已提交
245
}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

_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 已提交
269 270 271 272
    }else if(srcType==TSDB_DATA_TYPE_BINARY) {
        p = getVectorValueAddr_VAR;
    }else if(srcType==TSDB_DATA_TYPE_NCHAR) {
        p = getVectorValueAddr_VAR;
273
    }else {
D
dapan1121 已提交
274
        p = getVectorValueAddr_default;
275 276 277 278
    }
    return p;
}

D
dapan1121 已提交
279
static FORCE_INLINE void varToTimestamp(char *buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
D
dapan1121 已提交
280 281 282 283 284 285 286 287
  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);
}

D
dapan1121 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
static FORCE_INLINE void varToSigned(char *buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
  if (overflow) {
    int64_t minValue = tDataTypes[pOut->columnData->info.type].minValue;
    int64_t maxValue = tDataTypes[pOut->columnData->info.type].maxValue;
    int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
    if (value > maxValue) {
      *overflow = 1;
      return;
    } else if (value < minValue) {
      *overflow = -1;
      return;
    } else {
      *overflow = 0;
    }
  }
      
D
fix bug  
dapan1121 已提交
304 305
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_TINYINT: {
wafwerar's avatar
wafwerar 已提交
306
      int8_t value = (int8_t)taosStr2Int8(buf, NULL, 10);
D
dapan1121 已提交
307
     
D
fix bug  
dapan1121 已提交
308 309 310 311
      colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_SMALLINT: {
wafwerar's avatar
wafwerar 已提交
312
      int16_t value = (int16_t)taosStr2Int16(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
313 314 315 316
      colDataAppendInt16(pOut->columnData, rowIndex, (int16_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_INT: {
wafwerar's avatar
wafwerar 已提交
317
      int32_t value = (int32_t)taosStr2Int32(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
318 319 320 321
      colDataAppendInt32(pOut->columnData, rowIndex, (int32_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_BIGINT: {
wafwerar's avatar
wafwerar 已提交
322
      int64_t value = (int64_t)taosStr2Int64(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
323 324 325 326
      colDataAppendInt64(pOut->columnData, rowIndex, (int64_t*)&value);
      break;
    }   
  }
D
dapan1121 已提交
327 328
}

D
dapan1121 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static FORCE_INLINE void varToUnsigned(char *buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
  if (overflow) {
    uint64_t minValue = (uint64_t)tDataTypes[pOut->columnData->info.type].minValue;
    uint64_t maxValue = (uint64_t)tDataTypes[pOut->columnData->info.type].maxValue;
    uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
    if (value > maxValue) {
      *overflow = 1;
      return;
    } else if (value < minValue) {
      *overflow = -1;
      return;
    } else {
      *overflow = 0;
    }
  }

D
fix bug  
dapan1121 已提交
345 346
  switch (pOut->columnData->info.type) {
    case TSDB_DATA_TYPE_UTINYINT: {
wafwerar's avatar
wafwerar 已提交
347
      uint8_t value = (uint8_t)taosStr2UInt8(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
348 349 350 351
      colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_USMALLINT: {
wafwerar's avatar
wafwerar 已提交
352
      uint16_t value = (uint16_t)taosStr2UInt16(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
353 354 355 356
      colDataAppendInt16(pOut->columnData, rowIndex, (int16_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_UINT: {
wafwerar's avatar
wafwerar 已提交
357
      uint32_t value = (uint32_t)taosStr2UInt32(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
358 359 360 361
      colDataAppendInt32(pOut->columnData, rowIndex, (int32_t*)&value);
      break;
    } 
    case TSDB_DATA_TYPE_UBIGINT: {
wafwerar's avatar
wafwerar 已提交
362
      uint64_t value = (uint64_t)taosStr2UInt64(buf, NULL, 10);
D
fix bug  
dapan1121 已提交
363 364 365 366
      colDataAppendInt64(pOut->columnData, rowIndex, (int64_t*)&value);
      break;
    }   
  }
D
dapan1121 已提交
367 368
}

D
dapan1121 已提交
369 370 371 372 373 374 375
static FORCE_INLINE void varToFloat(char *buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
  if (TSDB_DATA_TYPE_FLOAT == pOut->columnData->info.type) {
    float value = taosStr2Float(buf, NULL);
    colDataAppendFloat(pOut->columnData, rowIndex, &value);
    return;
  }
  
wafwerar's avatar
wafwerar 已提交
376
  double value = taosStr2Double(buf, NULL);
377
  colDataAppendDouble(pOut->columnData, rowIndex, &value);
378 379
}

D
dapan1121 已提交
380
static FORCE_INLINE void varToBool(char *buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
wafwerar's avatar
wafwerar 已提交
381
  int64_t value = taosStr2Int64(buf, NULL, 10);
382
  bool v = (value != 0)? true:false;
383
  colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*) &v);
D
dapan1121 已提交
384 385
}

D
dapan1121 已提交
386
static FORCE_INLINE void varToNchar(char* buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
wmmhello's avatar
wmmhello 已提交
387 388
  int32_t len = 0;
  int32_t inputLen = varDataLen(buf);
D
fix bug  
dapan1121 已提交
389
  int32_t outputMaxLen = (inputLen + 1) * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
wmmhello's avatar
wmmhello 已提交
390

D
fix bug  
dapan1121 已提交
391
  char* t = taosMemoryCalloc(1, outputMaxLen);
wafwerar's avatar
wafwerar 已提交
392
  /*int32_t resLen = */taosMbsToUcs4(varDataVal(buf), inputLen, (TdUcs4*) varDataVal(t), outputMaxLen - VARSTR_HEADER_SIZE, &len);
wmmhello's avatar
wmmhello 已提交
393 394 395 396 397
  varDataSetLen(t, len);

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

D
dapan1121 已提交
399
static FORCE_INLINE void ncharToVar(char* buf, SScalarParam* pOut, int32_t rowIndex, int32_t* overflow) {
D
dapan1121 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  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);
}


415
//TODO opt performance, tmp is not needed.
D
dapan1121 已提交
416
int32_t vectorConvertFromVarData(const SScalarParam* pIn, SScalarParam* pOut, int32_t inType, int32_t outType, int32_t* overflow) {
wmmhello's avatar
wmmhello 已提交
417
  bool vton = false;
418 419 420 421

  _bufConverteFunc func = NULL;
  if (TSDB_DATA_TYPE_BOOL == outType) {
    func = varToBool;
D
dapan1121 已提交
422
  } else if (IS_SIGNED_NUMERIC_TYPE(outType)) {
423 424 425 426 427
    func = varToSigned;
  } else if (IS_UNSIGNED_NUMERIC_TYPE(outType)) {
    func = varToUnsigned;
  } else if (IS_FLOAT_TYPE(outType)) {
    func = varToFloat;
D
dapan1121 已提交
428 429 430 431
  } else if (outType == TSDB_DATA_TYPE_BINARY) {   // nchar -> binary
    ASSERT(inType == TSDB_DATA_TYPE_NCHAR);
    func = ncharToVar;
    vton = true;
wmmhello's avatar
wmmhello 已提交
432 433 434 435
  } else if (outType == TSDB_DATA_TYPE_NCHAR) {   // binary -> nchar
    ASSERT(inType == TSDB_DATA_TYPE_VARCHAR);
    func = varToNchar;
    vton = true;
D
dapan1121 已提交
436 437
  } else if (TSDB_DATA_TYPE_TIMESTAMP == outType) {
    func = varToTimestamp;
438 439 440 441 442 443 444
  } 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 已提交
445
    if (IS_HELPER_NULL(pIn->columnData, i)) {
446 447 448 449 450 451 452 453
      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 已提交
454
        ASSERT(0);
D
dapan1121 已提交
455
      } else if(*data == TSDB_DATA_TYPE_NCHAR) {
456 457
        data += CHAR_BYTES;
        convertType = TSDB_DATA_TYPE_NCHAR;
wmmhello's avatar
wmmhello 已提交
458 459 460
      } else if(tTagIsJson(data)){
        terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
        return terrno;
461 462 463 464 465
      } else {
        convertNumberToNumber(data+CHAR_BYTES, colDataGetNumData(pOut->columnData, i), *data, outType);
        continue;
      }
    }
466 467 468 469 470 471
    int32_t bufSize = pIn->columnData->info.bytes;
    char *tmp = taosMemoryMalloc(varDataTLen(data));
    if(!tmp){
      sclError("out of memory in vectorConvertFromVarData");
      return TSDB_CODE_OUT_OF_MEMORY;
    }
wmmhello's avatar
wmmhello 已提交
472 473 474
    if (vton) {
      memcpy(tmp, data, varDataTLen(data));
    } else {
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
      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 已提交
490
    }
491
    
D
dapan1121 已提交
492
    (*func)(tmp, pOut, i, overflow);
493
    taosMemoryFreeClear(tmp);
494 495 496 497 498
  }
  
  return TSDB_CODE_SUCCESS;
}

499
double getVectorDoubleValue_JSON(void *src, int32_t index){
500
  char *data = colDataGetVarData((SColumnInfoData*)src, index);
501
  double out = 0;
502 503 504
  if (*data == TSDB_DATA_TYPE_NULL){
    return out;
  } else if(*data == TSDB_DATA_TYPE_NCHAR) {   // json inner type can not be BINARY
505
    convertNcharToDouble(data+CHAR_BYTES, &out);
wmmhello's avatar
wmmhello 已提交
506 507 508 509
  } else if(tTagIsJson(data)){
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
    return 0;
  } else{
510 511 512 513 514
    convertNumberToNumber(data+CHAR_BYTES, &out, *data, TSDB_DATA_TYPE_DOUBLE);
  }
  return out;
}

515
void* ncharTobinary(void *buf){            // todo need to remove , if tobinary is nchar
wmmhello's avatar
wmmhello 已提交
516
  int32_t inputLen = varDataTLen(buf);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

  void* t = taosMemoryCalloc(1, inputLen);
  int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(buf), varDataLen(buf), varDataVal(t));
  if (len < 0) {
    sclError("charset:%s to %s. val:%s convert ncharTobinary failed.", DEFAULT_UNICODE_ENCODEC, tsCharset,
             (char*)varDataVal(buf));
    taosMemoryFree(t);
    return NULL;
  }
  varDataSetLen(t, len);
  return t;
}

bool convertJsonValue(__compar_fn_t *fp, int32_t optr, int8_t typeLeft, int8_t typeRight, char **pLeftData, char **pRightData,
                      void *pLeftOut, void *pRightOut, bool *isNull, bool *freeLeft, bool *freeRight){
532
  if(optr == OP_TYPE_JSON_CONTAINS) {
wmmhello's avatar
wmmhello 已提交
533
    return true;
534 535 536
  }

  if(typeLeft != TSDB_DATA_TYPE_JSON && typeRight != TSDB_DATA_TYPE_JSON){
wmmhello's avatar
wmmhello 已提交
537
    return true;
538 539 540
  }

  if(typeLeft == TSDB_DATA_TYPE_JSON){
wmmhello's avatar
wmmhello 已提交
541 542 543 544
    if(tTagIsJson(*pLeftData)){
      terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
      return false;
    }
545 546 547 548
    typeLeft = **pLeftData;
    (*pLeftData) ++;
  }
  if(typeRight == TSDB_DATA_TYPE_JSON){
wmmhello's avatar
wmmhello 已提交
549 550 551 552
    if(tTagIsJson(*pLeftData)){
      terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
      return false;
    }
553 554 555
    typeRight = **pRightData;
    (*pRightData) ++;
  }
wmmhello's avatar
wmmhello 已提交
556 557 558 559 560 561 562

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

wmmhello's avatar
wmmhello 已提交
563 564 565 566 567 568 569 570 571 572
  // if types can not comparable
  if((IS_NUMERIC_TYPE(typeLeft) && !IS_NUMERIC_TYPE(typeRight)) ||
     (IS_NUMERIC_TYPE(typeRight) && !IS_NUMERIC_TYPE(typeLeft)) ||
     (IS_VAR_DATA_TYPE(typeLeft) && !IS_VAR_DATA_TYPE(typeRight)) ||
     (IS_VAR_DATA_TYPE(typeRight) && !IS_VAR_DATA_TYPE(typeLeft)) ||
     ((typeLeft == TSDB_DATA_TYPE_BOOL) && (typeRight != TSDB_DATA_TYPE_BOOL)) ||
     ((typeRight == TSDB_DATA_TYPE_BOOL) && (typeLeft != TSDB_DATA_TYPE_BOOL)))
    return false;


573 574
  if(typeLeft == TSDB_DATA_TYPE_NULL || typeRight == TSDB_DATA_TYPE_NULL){
    *isNull = true;
wmmhello's avatar
wmmhello 已提交
575
    return true;
576
  }
577 578 579 580
  int8_t type = vectorGetConvertType(typeLeft, typeRight);

  if(type == 0) {
    *fp = filterGetCompFunc(typeLeft, optr);
wmmhello's avatar
wmmhello 已提交
581
    return true;
582 583 584 585
  }

  *fp = filterGetCompFunc(type, optr);

wmmhello's avatar
wmmhello 已提交
586
  if(IS_NUMERIC_TYPE(type)){
587
    if(typeLeft == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
588 589 590
      ASSERT(0);
//      convertNcharToDouble(*pLeftData, pLeftOut);
//      *pLeftData = pLeftOut;
591
    } else if(typeLeft == TSDB_DATA_TYPE_BINARY) {
wmmhello's avatar
wmmhello 已提交
592 593 594
      ASSERT(0);
//      convertBinaryToDouble(*pLeftData, pLeftOut);
//      *pLeftData = pLeftOut;
595 596 597 598 599 600
    } else if(typeLeft != type) {
      convertNumberToNumber(*pLeftData, pLeftOut, typeLeft, type);
      *pLeftData = pLeftOut;
    }

    if(typeRight == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
601 602 603
      ASSERT(0);
//      convertNcharToDouble(*pRightData, pRightOut);
//      *pRightData = pRightOut;
604
    } else if(typeRight == TSDB_DATA_TYPE_BINARY) {
wmmhello's avatar
wmmhello 已提交
605 606 607
      ASSERT(0);
//      convertBinaryToDouble(*pRightData, pRightOut);
//      *pRightData = pRightOut;
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
    } else if(typeRight != type) {
      convertNumberToNumber(*pRightData, pRightOut, typeRight, type);
      *pRightData = pRightOut;
    }
  }else if(type == TSDB_DATA_TYPE_BINARY){
    if(typeLeft == TSDB_DATA_TYPE_NCHAR){
      *pLeftData = ncharTobinary(*pLeftData);
      *freeLeft = true;
    }
    if(typeRight == TSDB_DATA_TYPE_NCHAR){
      *pRightData = ncharTobinary(*pRightData);
      *freeRight = true;
    }
  }else{
    ASSERT(0);
623 624
  }

wmmhello's avatar
wmmhello 已提交
625
  return true;
626 627
}

D
fix bug  
dapan1121 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
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) {
D
dapan1121 已提交
645
        varToNchar(tmp, pOut, i, NULL);
D
fix bug  
dapan1121 已提交
646
      } else {
D
dapan1121 已提交
647
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661
      }
    }
  } 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) {
D
dapan1121 已提交
662
        varToNchar(tmp, pOut, i, NULL);
D
fix bug  
dapan1121 已提交
663
      } else {
D
dapan1121 已提交
664
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678
      }
    }
  } 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) {
D
dapan1121 已提交
679
        varToNchar(tmp, pOut, i, NULL);
D
fix bug  
dapan1121 已提交
680
      } else {
D
dapan1121 已提交
681
        colDataAppend(pOutputCol, i, (char *)tmp, false);
D
fix bug  
dapan1121 已提交
682 683 684 685 686 687 688 689 690 691
      }
    }
  } else {
    sclError("not supported input type:%d", inType);
    return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

692
// TODO opt performance
D
dapan1121 已提交
693
int32_t vectorConvertImpl(const SScalarParam* pIn, SScalarParam* pOut, int32_t* overflow) {
694 695
  SColumnInfoData* pInputCol  = pIn->columnData;
  SColumnInfoData* pOutputCol = pOut->columnData;
H
Haojun Liao 已提交
696

D
dapan1121 已提交
697 698 699 700 701
  if (NULL == pInputCol) {
    sclError("input column is NULL, hashFilter %p", pIn->pHashFilter);
    return TSDB_CODE_APP_ERROR;
  }
  
H
Haojun Liao 已提交
702
  int16_t inType   = pInputCol->info.type;
703
  int16_t outType  = pOutputCol->info.type;
D
dapan1121 已提交
704

705
  if (IS_VAR_DATA_TYPE(inType)) {
D
dapan1121 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    return vectorConvertFromVarData(pIn, pOut, inType, outType, overflow);
  }

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

    pOut->numOfRows = 0;
    
    if (IS_SIGNED_NUMERIC_TYPE(outType)) {
      int64_t minValue = tDataTypes[outType].minValue;
      int64_t maxValue = tDataTypes[outType].maxValue;
      
      double value = 0;
      GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, 0));
      
      if (value > maxValue) {
        *overflow = 1;
        return TSDB_CODE_SUCCESS;
      } else if (value < minValue) {
        *overflow = -1;
        return TSDB_CODE_SUCCESS;
      } else {
        *overflow = 0;
      }
    } else if (IS_UNSIGNED_NUMERIC_TYPE(outType)) {
      uint64_t minValue = (uint64_t)tDataTypes[outType].minValue;
      uint64_t maxValue = (uint64_t)tDataTypes[outType].maxValue;
      
      double value = 0;
      GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, 0));
      
      if (value > maxValue) {
        *overflow = 1;
        return TSDB_CODE_SUCCESS;
      } else if (value < minValue) {
        *overflow = -1;
        return TSDB_CODE_SUCCESS;
      } else {
        *overflow = 0;
      }
    }
D
dapan 已提交
747
  }
748 749

  pOut->numOfRows = pIn->numOfRows;
D
dapan1121 已提交
750
  switch (outType) {
751 752 753
    case TSDB_DATA_TYPE_BOOL: {
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
754
          colDataAppendNULL(pOutputCol, i);
755 756 757 758
          continue;
        }

        bool value = 0;
D
dapan1121 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
        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);
800 801 802
      }
      break;
    }
D
dapan1121 已提交
803
    case TSDB_DATA_TYPE_BIGINT:
804
    case TSDB_DATA_TYPE_TIMESTAMP: {
805 806
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
807
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
808
          continue;
D
dapan1121 已提交
809
        }
D
dapan 已提交
810 811

        int64_t value = 0;
812
        GET_TYPED_DATA(value, int64_t, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
813
        colDataAppendInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
814 815
      }
      break;
816
    }
D
dapan1121 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
    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: {
857 858
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
859
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
860
          continue;
D
dapan1121 已提交
861 862
        }
        
D
dapan 已提交
863
        uint64_t value = 0;
864
        GET_TYPED_DATA(value, uint64_t, inType, colDataGetData(pInputCol, i));
865
        colDataAppendInt64(pOutputCol, i, (int64_t*)&value);
D
dapan1121 已提交
866 867
      }
      break;
D
dapan1121 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    }
    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: {
883 884
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
885
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
886
          continue;
D
dapan1121 已提交
887 888
        }
        
D
dapan 已提交
889
        double value = 0;
890
        GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
891
        colDataAppendDouble(pOutputCol, i, (double*)&value);
D
dapan1121 已提交
892
      }
D
dapan1121 已提交
893 894
      break;  
    }
D
fix bug  
dapan1121 已提交
895 896 897 898
    case TSDB_DATA_TYPE_BINARY: 
    case TSDB_DATA_TYPE_NCHAR: {
      return vectorConvertToVarData(pIn, pOut, inType, outType);
    }
D
dapan1121 已提交
899
    default:
D
dapan1121 已提交
900
      sclError("invalid convert output type:%d", outType);
D
dapan1121 已提交
901 902 903 904 905 906
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
907 908


D
dapan1121 已提交
909
int8_t gConvertTypes[TSDB_DATA_TYPE_BLOB+1][TSDB_DATA_TYPE_BLOB+1] = {
910
/*         NULL BOOL TINY SMAL INT  BIG  FLOA DOUB VARC TIME NCHA UTIN USMA UINT UBIG JSON VARB DECI BLOB */
H
Haojun Liao 已提交
911
/*NULL*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
D
dapan1121 已提交
912
/*BOOL*/   0,   0,   2,   3,   4,   5,   6,   7,   7,   9,   7,   11,  12,  13,  14,  0,   7,   0,   0,
913 914 915
/*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,
D
dapan1121 已提交
916
/*BIGI*/   0,   0,   0,   0,   0,   0,   6,   7,   7,   9,   7,   5,   5,   5,   7,   0,   7,   0,   0,
917 918
/*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 已提交
919
/*VARC*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   8,   7,   7,   7,   7,   0,   0,   0,   0,
D
dapan1121 已提交
920
/*TIME*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   9,   9,   9,   9,   7,   0,   7,   0,   0,
H
Haojun Liao 已提交
921
/*NCHA*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   7,   7,   7,   7,   0,   0,   0,   0,
922 923 924 925
/*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 已提交
926
/*JSON*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
927
/*VARB*/   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
H
Haojun Liao 已提交
928 929
/*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 已提交
930 931
};

D
dapan1121 已提交
932 933 934 935 936 937 938 939 940 941 942 943
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 已提交
944 945 946 947
int32_t vectorConvertScalarParam(SScalarParam *input, SScalarParam *output, int32_t type) {
  SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
  output->numOfRows = input->numOfRows;

H
Haojun Liao 已提交
948 949
  int32_t code = sclCreateColumnInfoData(&t, input->numOfRows, output);
  if (code != TSDB_CODE_SUCCESS) {
D
dapan1121 已提交
950
    return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
951 952
  }

D
dapan1121 已提交
953
  code = vectorConvertImpl(input, output, NULL);
D
dapan1121 已提交
954 955 956 957 958 959 960 961 962
  if (code) {
//      taosMemoryFreeClear(paramOut1->data);
    return code;
  }

  return TSDB_CODE_SUCCESS;
}

int32_t vectorConvert(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam* pLeftOut, SScalarParam* pRightOut) {
963 964 965
  int32_t leftType  = GET_PARAM_TYPE(pLeft);
  int32_t rightType = GET_PARAM_TYPE(pRight);
  if (leftType == rightType) {
D
dapan1121 已提交
966 967 968
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
969 970
  SScalarParam *param1 = NULL, *paramOut1 = NULL; 
  SScalarParam *param2 = NULL, *paramOut2 = NULL;
D
dapan1121 已提交
971 972
  int32_t code = 0;
  
973
  if (leftType < rightType) {
D
dapan1121 已提交
974 975 976 977 978 979 980 981 982 983 984
    param1 = pLeft;
    param2 = pRight;
    paramOut1 = pLeftOut;
    paramOut2 = pRightOut;
  } else {
    param1 = pRight;
    param2 = pLeft;
    paramOut1 = pRightOut;
    paramOut2 = pLeftOut;
  }

985
  int8_t type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
D
dapan1121 已提交
986 987 988 989
  if (0 == type) {
    return TSDB_CODE_SUCCESS;
  }

990
  if (type != GET_PARAM_TYPE(param1)) {
D
dapan1121 已提交
991 992 993 994
    code = vectorConvertScalarParam(param1, paramOut1, type);
    if (code) {
      return code;
    }
D
dapan1121 已提交
995 996
  }
  
997
  if (type != GET_PARAM_TYPE(param2)) {
D
dapan1121 已提交
998 999 1000 1001
    code = vectorConvertScalarParam(param2, paramOut2, type);
    if (code) {
      return code;
    }
D
dapan1121 已提交
1002 1003 1004 1005 1006
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
1007 1008 1009 1010 1011 1012
enum {
  VECTOR_DO_CONVERT = 0x1,
  VECTOR_UN_CONVERT = 0x2,
};

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

1015
  if (IS_VAR_DATA_TYPE(pCol->info.type) && pCol->info.type != TSDB_DATA_TYPE_JSON) {
H
Haojun Liao 已提交
1016
    pDest->numOfRows = pParam->numOfRows;
1017 1018

    SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
H
Haojun Liao 已提交
1019 1020 1021
    int32_t code = sclCreateColumnInfoData(&t, pParam->numOfRows, pDest);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
D
dapan1121 已提交
1022 1023
    }

D
dapan1121 已提交
1024
    code = vectorConvertImpl(pParam, pDest, NULL);
1025 1026
    if (code != TSDB_CODE_SUCCESS) {
      return code;
D
dapan1121 已提交
1027
    }
1028

H
Haojun Liao 已提交
1029 1030 1031
    *convert = VECTOR_DO_CONVERT;
  } else {
    *convert = VECTOR_UN_CONVERT;
D
dapan1121 已提交
1032
  }
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043

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

D
dapan1121 已提交
1058
static void vectorMathTsAddHelper(SColumnInfoData* pLeftCol, SColumnInfoData* pRightCol, SColumnInfoData* pOutputCol, int32_t numOfRows, int32_t step, int32_t i) {
1059 1060 1061 1062 1063
  _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 已提交
1064
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1065 1066 1067
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1068
      if (IS_HELPER_NULL(pLeftCol, i)) {
1069 1070 1071
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
D
dapan1121 已提交
1072 1073
      *output = taosTimeAdd(getVectorBigintValueFnLeft(pLeftCol->pData, i), getVectorBigintValueFnRight(pRightCol->pData, 0),
                            pRightCol->info.scale, pRightCol->info.precision);
1074 1075 1076 1077
    }
  }
}

H
Haojun Liao 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
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);
H
Haojun Liao 已提交
1097
    taosMemoryFree(pCol);
H
Haojun Liao 已提交
1098 1099 1100
  }
}

1101 1102 1103 1104 1105 1106
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;

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

H
Haojun Liao 已提交
1109 1110 1111
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
1112

1113 1114 1115 1116
  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
1117 1118 1119
    int64_t *output = (int64_t *)pOutputCol->pData;
    _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
1120

D
dapan1121 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
      if (GET_PARAM_TYPE(pLeft) == TSDB_DATA_TYPE_TIMESTAMP) {
        vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pRight->numOfRows, step, i);
      } else {
        vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
      }
    } else if (pLeft->numOfRows == 1) {
      vectorMathTsAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathTsAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
    } else if (pLeft->numOfRows == pRight->numOfRows) {
1132
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1133
        if (IS_NULL) {
wmmhello's avatar
wmmhello 已提交
1134 1135 1136
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
1137
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, i);
1138
      }
D
dapan1121 已提交
1139
    } 
1140 1141 1142 1143
  } else {
    double *output = (double *)pOutputCol->pData;
    _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
    _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1144

1145 1146
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1147
        if (IS_NULL){
1148 1149 1150 1151
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) + getVectorDoubleValueFnRight(RIGHT_COL, i);
1152 1153 1154 1155 1156 1157
      }
    } 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);
    }
1158
  }
H
Haojun Liao 已提交
1159 1160 1161

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1162
}
D
dapan1121 已提交
1163

1164 1165 1166 1167 1168 1169 1170
// 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 已提交
1171
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1172
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1173 1174
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1175
      if (IS_HELPER_NULL(pLeftCol, i)) {
1176 1177 1178 1179 1180
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = (getVectorDoubleValueFnLeft(LEFT_COL, i)
                 - getVectorDoubleValueFnRight(RIGHT_COL, 0)) * factor;
D
dapan1121 已提交
1181
    }
1182 1183
  }
}
D
dapan1121 已提交
1184

D
dapan1121 已提交
1185
static void vectorMathTsSubHelper(SColumnInfoData* pLeftCol, SColumnInfoData* pRightCol, SColumnInfoData* pOutputCol, int32_t numOfRows, int32_t step, int32_t factor, int32_t i) {
1186 1187 1188 1189 1190
  _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 已提交
1191
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1192 1193 1194
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1195
      if (IS_HELPER_NULL(pLeftCol, i)) {
1196 1197 1198
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
H
Haojun Liao 已提交
1199
      *output = taosTimeAdd(getVectorBigintValueFnLeft(pLeftCol->pData, i), -getVectorBigintValueFnRight(pRightCol->pData, 0),
D
dapan1121 已提交
1200 1201
                            pRightCol->info.scale, pRightCol->info.precision);
      
1202 1203 1204 1205
    }
  }
}

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

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

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

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

1218 1219 1220 1221 1222
  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);
1223

D
dapan1121 已提交
1224 1225 1226 1227 1228 1229 1230
    if (pLeft->numOfRows == 1 && pRight->numOfRows == 1) {
      vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
    } else if (pLeft->numOfRows == 1) {
      vectorMathTsSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathTsSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
    } else if (pLeft->numOfRows == pRight->numOfRows) {
1231
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1232
        if (IS_NULL) {
wmmhello's avatar
wmmhello 已提交
1233 1234 1235
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
1236
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, i);
1237
      }
D
dapan1121 已提交
1238
    }
1239 1240 1241 1242 1243 1244 1245
  } 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 已提交
1246
        if (IS_NULL) {
1247 1248 1249 1250
          colDataAppendNULL(pOutputCol, i);
          continue;  // TODO set null or ignore
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i) - getVectorDoubleValueFnRight(RIGHT_COL, i);
1251 1252 1253 1254 1255 1256
      }
    } 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);
    }
1257
  }
H
Haojun Liao 已提交
1258 1259 1260

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1261 1262
}

1263 1264 1265 1266
// 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 已提交
1267

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

wmmhello's avatar
wmmhello 已提交
1270
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1271
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1272 1273
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1274
      if (IS_HELPER_NULL(pLeftCol, i)) {
1275 1276 1277
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1278
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, 0);
1279 1280
    }
  }
D
dapan1121 已提交
1281 1282
}

1283 1284
void vectorMathMultiply(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1285
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1286 1287 1288 1289

  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 已提交
1290 1291 1292
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
1293 1294 1295 1296 1297 1298 1299

  _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 已提交
1300
      if (IS_NULL) {
1301 1302 1303
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
1304
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i) * getVectorDoubleValueFnRight(RIGHT_COL, i);
1305 1306 1307 1308 1309
    }
  } 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 已提交
1310
  }
H
Haojun Liao 已提交
1311 1312 1313

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

1316 1317
void vectorMathDivide(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1318
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1319

1320 1321 1322
  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 已提交
1323 1324 1325
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
1326

1327 1328 1329 1330
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);

  double *output = (double *)pOutputCol->pData;
1331
  if (pLeft->numOfRows == pRight->numOfRows) {
1332
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1333
      if (IS_NULL || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) { //divide by 0 check
1334
        colDataAppendNULL(pOutputCol, i);
1335
        continue;
1336
      }
1337
      *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
1338
                / getVectorDoubleValueFnRight(RIGHT_COL, i);
1339 1340
    }
  } else if (pLeft->numOfRows == 1) {
wmmhello's avatar
wmmhello 已提交
1341
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1342
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1343 1344
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1345
        if (IS_HELPER_NULL(pRightCol, i) || (getVectorDoubleValueFnRight(RIGHT_COL, i) == 0)) { // divide by 0 check
1346
          colDataAppendNULL(pOutputCol, i);
1347
          continue;
1348 1349 1350
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, 0)
                  / getVectorDoubleValueFnRight(RIGHT_COL, i);
1351 1352 1353
      }
    }
  } else if (pRight->numOfRows == 1) {
wmmhello's avatar
wmmhello 已提交
1354
    if (IS_HELPER_NULL(pRightCol, 0) || (getVectorDoubleValueFnRight(RIGHT_COL, 0) == 0)) {  // Set pLeft->numOfRows NULL value (divde by 0 check)
1355
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1356 1357
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1358
        if (IS_HELPER_NULL(pLeftCol, i)) {
1359
          colDataAppendNULL(pOutputCol, i);
1360
          continue;
1361 1362 1363
        }
        *output = getVectorDoubleValueFnLeft(LEFT_COL, i)
                  / getVectorDoubleValueFnRight(RIGHT_COL, 0);
1364 1365 1366
      }
    }
  }
H
Haojun Liao 已提交
1367 1368 1369

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1370 1371
}

1372 1373
void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1374
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
1375 1376 1377

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

H
Haojun Liao 已提交
1379 1380 1381
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
1382

1383 1384
  _getDoubleValue_fn_t getVectorDoubleValueFnLeft  = getVectorDoubleValueFn(pLeftCol->info.type);
  _getDoubleValue_fn_t getVectorDoubleValueFnRight = getVectorDoubleValueFn(pRightCol->info.type);
1385 1386 1387 1388 1389

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

  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1390
      if (IS_NULL) {
1391
        colDataAppendNULL(pOutputCol, i);
1392 1393 1394
        continue;
      }

1395 1396 1397
      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)) {
1398
        colDataAppendNULL(pOutputCol, i);
1399 1400 1401
        continue;
      }

1402
      *output = lx - ((int64_t)(lx / rx)) * rx;
1403 1404
    }
  } else if (pLeft->numOfRows == 1) {
1405
    double lx = getVectorDoubleValueFnLeft(LEFT_COL, 0);
wmmhello's avatar
wmmhello 已提交
1406
    if (IS_HELPER_NULL(pLeftCol, 0)) {  // Set pLeft->numOfRows NULL value
1407
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
1408 1409
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1410
        if (IS_HELPER_NULL(pRightCol, i)) {
1411
          colDataAppendNULL(pOutputCol, i);
1412 1413 1414
          continue;
        }

1415 1416
        double rx = getVectorDoubleValueFnRight(RIGHT_COL, i);
        if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
1417
          colDataAppendNULL(pOutputCol, i);
1418 1419 1420
          continue;
        }

1421
        *output = lx - ((int64_t)(lx / rx)) * rx;
1422 1423 1424
      }
    }
  } else if (pRight->numOfRows == 1) {
1425 1426
    double rx = getVectorDoubleValueFnRight(RIGHT_COL, 0);
    if (IS_HELPER_NULL(pRightCol, 0) || FLT_EQUAL(rx, 0)) {  // Set pLeft->numOfRows NULL value
1427
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
1428 1429
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1430
        if (IS_HELPER_NULL(pLeftCol, i)) {
1431
          colDataAppendNULL(pOutputCol, i);
1432 1433 1434
          continue;
        }

1435 1436 1437 1438 1439 1440 1441
        double lx = getVectorDoubleValueFnLeft(LEFT_COL, i);
        if (isnan(lx) || isinf(lx)) {
          colDataAppendNULL(pOutputCol, i);
          continue;
        }

        *output = lx - ((int64_t)(lx / rx)) * rx;
1442 1443 1444
      }
    }
  }
H
Haojun Liao 已提交
1445 1446 1447

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
1448 1449
}

D
dapan1121 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
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 已提交
1465
    if (IS_HELPER_NULL(pLeftCol, i)) {
1466
      colDataAppendNULL(pOutputCol, i);
1467
      continue;
1468
    }
1469 1470
    double result = getVectorDoubleValueFnLeft(LEFT_COL, i);
    *output = (result == 0) ? 0 : -result;
D
dapan1121 已提交
1471 1472 1473 1474 1475
  }

  doReleaseVec(pLeftCol,  leftConvert);
}

D
dapan1121 已提交
1476 1477 1478 1479 1480
void vectorAssign(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;

  pOut->numOfRows = pLeft->numOfRows;

wmmhello's avatar
wmmhello 已提交
1481 1482
//  if (IS_HELPER_NULL(pRight->columnData, 0)) {
  if(colDataIsNull_s(pRight->columnData, 0)){
D
dapan1121 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
    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 已提交
1493
void vectorConcat(SScalarParam* pLeft, SScalarParam* pRight, void *out, int32_t _ord) {
1494
#if 0
1495 1496
  int32_t len = pLeft->bytes + pRight->bytes;

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

  char *output = (char *)out;
1501 1502
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += len) {
1503 1504 1505
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
      char* right = POINTER_SHIFT(pRight->data, pRight->bytes * i);

H
Haojun Liao 已提交
1506
      if (isNull(left, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1507 1508 1509 1510 1511 1512 1513 1514 1515
        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));
    }
1516 1517
  } else if (pLeft->numOfRows == 1) {
    for (; i >= 0 && i < pRight->numOfRows; i += step, output += len) {
1518
      char *right = POINTER_SHIFT(pRight->data, pRight->bytes * i);
H
Haojun Liao 已提交
1519
      if (isNull(pLeft->data, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1520 1521 1522 1523 1524 1525 1526 1527
        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));
    }
1528 1529
  } else if (pRight->numOfRows == 1) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step, output += len) {
1530
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
H
Haojun Liao 已提交
1531
      if (isNull(left, pLeftCol->info.type) || isNull(pRight->data, pRight->info.type)) {
1532 1533 1534 1535 1536 1537 1538 1539 1540
        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));
    }
  }
1541
#endif
1542 1543
}

1544 1545 1546
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 已提交
1547

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

wmmhello's avatar
wmmhello 已提交
1550
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1551
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1552 1553
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1554
      if (IS_HELPER_NULL(pLeftCol, i)) {
1555 1556 1557 1558
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, 0);
D
dapan1121 已提交
1559 1560
    }
  }
1561
}
D
dapan1121 已提交
1562

1563 1564
void vectorBitAnd(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1565
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan 已提交
1566

1567 1568
  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 已提交
1569

H
Haojun Liao 已提交
1570 1571 1572
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1573

H
Haojun Liao 已提交
1574 1575
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1576

1577 1578 1579
  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 已提交
1580
      if (IS_NULL) {
1581 1582 1583 1584
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) & getVectorBigintValueFnRight(RIGHT_COL, i);
D
dapan1121 已提交
1585
    }
1586 1587 1588 1589 1590
  } 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 已提交
1591 1592 1593

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1594 1595
}

1596 1597 1598
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 已提交
1599

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

wmmhello's avatar
wmmhello 已提交
1602
  if (IS_HELPER_NULL(pRightCol, 0)) {  // Set pLeft->numOfRows NULL value
1603
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1604
  } else {
1605
    int64_t rx = getVectorBigintValueFnRight(RIGHT_COL, 0);
1606
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
wmmhello's avatar
wmmhello 已提交
1607
      if (IS_HELPER_NULL(pLeftCol, i)) {
1608 1609 1610 1611
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | rx;
D
dapan1121 已提交
1612 1613
    }
  }
1614
}
D
dapan1121 已提交
1615

1616 1617
void vectorBitOr(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1618
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1619

1620 1621
  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 已提交
1622

H
Haojun Liao 已提交
1623 1624 1625
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1626

H
Haojun Liao 已提交
1627 1628
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan 已提交
1629

1630 1631 1632
  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 已提交
1633
      if (IS_NULL) {
1634 1635 1636 1637
        colDataAppendNULL(pOutputCol, i);
        continue;  // TODO set null or ignore
      }
      *output = getVectorBigintValueFnLeft(LEFT_COL, i) | getVectorBigintValueFnRight(RIGHT_COL, i);
1638 1639 1640 1641 1642
    }
  } 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 已提交
1643
  }
H
Haojun Liao 已提交
1644 1645 1646

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1647 1648
}

1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
int32_t doVectorCompareImpl(int32_t numOfRows, SScalarParam *pOut, int32_t startIndex, int32_t step, __compar_fn_t fp,
                         SScalarParam *pLeft, SScalarParam *pRight, int32_t optr) {
  int32_t num = 0;

  for (int32_t i = startIndex; i < numOfRows && i >= 0; i += step) {
    int32_t leftIndex = (i >= pLeft->numOfRows)? 0:i;
    int32_t rightIndex = (i >= pRight->numOfRows)? 0:i;

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

    char *  pLeftData = colDataGetData(pLeft->columnData, leftIndex);
    char *  pRightData = colDataGetData(pRight->columnData, rightIndex);
    int64_t leftOut = 0;
    int64_t rightOut = 0;
    bool    freeLeft = false;
    bool    freeRight = false;
    bool    isJsonnull = false;

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

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

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

    if (freeLeft) {
      taosMemoryFreeClear(pLeftData);
    }

    if (freeRight) {
      taosMemoryFreeClear(pRightData);
    }
1698 1699
  }

1700 1701 1702
  return num;
}

1703 1704 1705
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;
D
dapan1121 已提交
1706 1707 1708 1709 1710 1711 1712 1713
  int32_t       lType = GET_PARAM_TYPE(pLeft);
  int32_t       rType = GET_PARAM_TYPE(pRight);
  __compar_fn_t fp = NULL;
  
  if (lType == rType) {
    fp = filterGetCompFunc(lType, optr);
  } else {
    fp = filterGetCompFuncEx(lType, rType, optr);
wmmhello's avatar
wmmhello 已提交
1714
  }
1715 1716 1717 1718 1719

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

  if (pRight->pHashFilter != NULL) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
wmmhello's avatar
wmmhello 已提交
1720
      if (IS_HELPER_NULL(pLeft->columnData, i)) {
D
dapan1121 已提交
1721 1722
        bool  res = false;
        colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
D
dapan1121 已提交
1723 1724 1725
        continue;
      }

1726 1727
      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter);
1728
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1729 1730 1731
      if (res) {
        pOut->numOfQualified++;
      }
1732
    }
1733 1734
  } else {  // normal compare
    pOut->numOfQualified = doVectorCompareImpl(pOut->numOfRows, pOut, i, step, fp, pLeft, pRight, optr);
D
dapan1121 已提交
1735 1736 1737
  }
}

D
dapan 已提交
1738
void vectorCompare(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1739 1740 1741 1742
  SScalarParam pLeftOut = {0}; 
  SScalarParam pRightOut = {0};
  SScalarParam *param1 = NULL; 
  SScalarParam *param2 = NULL;
D
dapan1121 已提交
1743

D
dapan1121 已提交
1744
  if (SCL_NO_NEED_CONVERT_COMPARISION(GET_PARAM_TYPE(pLeft), GET_PARAM_TYPE(pRight), optr)) {
D
dapan1121 已提交
1745 1746
    param1 = pLeft;
    param2 = pRight;
D
dapan1121 已提交
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
  } else {
    vectorConvert(pLeft, pRight, &pLeftOut, &pRightOut);

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

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

D
dapan 已提交
1763
  vectorCompareImpl(param1, param2, pOut, _ord, optr);
D
dapan1121 已提交
1764 1765
  sclFreeParam(&pLeftOut);
  sclFreeParam(&pRightOut);  
D
dapan1121 已提交
1766 1767
}

D
dapan 已提交
1768 1769
void vectorGreater(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN);
D
dapan1121 已提交
1770 1771
}

D
dapan 已提交
1772 1773
void vectorGreaterEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL);
D
dapan1121 已提交
1774 1775
}

D
dapan 已提交
1776 1777
void vectorLower(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN);
D
dapan1121 已提交
1778 1779
}

D
dapan 已提交
1780 1781
void vectorLowerEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL);
D
dapan1121 已提交
1782 1783
}

D
dapan 已提交
1784 1785
void vectorEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL);
D
dapan1121 已提交
1786 1787
}

D
dapan 已提交
1788 1789
void vectorNotEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL);
D
dapan1121 已提交
1790 1791
}

D
dapan 已提交
1792 1793
void vectorIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN);
D
dapan1121 已提交
1794 1795
}

D
dapan 已提交
1796 1797
void vectorNotIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN);
D
dapan1121 已提交
1798 1799
}

D
dapan 已提交
1800 1801
void vectorLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE);
D
dapan1121 已提交
1802 1803
}

D
dapan 已提交
1804 1805
void vectorNotLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE);
D
dapan1121 已提交
1806 1807
}

D
dapan 已提交
1808 1809
void vectorMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH);
D
dapan1121 已提交
1810 1811
}

D
dapan 已提交
1812 1813
void vectorNotMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH);
D
dapan1121 已提交
1814 1815
}

D
dapan 已提交
1816
void vectorIsNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1817
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1818
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 1 : 0;
1819
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1820
  }
1821
  pOut->numOfRows = pLeft->numOfRows;
D
dapan1121 已提交
1822 1823
}

D
dapan 已提交
1824
void vectorNotNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1825
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1826
    int8_t v = IS_HELPER_NULL(pLeft->columnData, i) ? 0 : 1;
1827
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1828
  }
1829
  pOut->numOfRows = pLeft->numOfRows;
D
dapan 已提交
1830
}
D
dapan1121 已提交
1831

D
dapan 已提交
1832
void vectorIsTrue(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
D
dapan1121 已提交
1833
  vectorConvertImpl(pLeft, pOut, NULL);
1834 1835 1836 1837 1838 1839 1840 1841
  for(int32_t i = 0; i < pOut->numOfRows; ++i) {
    if(colDataIsNull_s(pOut->columnData, i)) {
      int8_t v = 0;
      colDataAppendInt8(pOut->columnData, i, &v);
      colDataSetNotNull_f(pOut->columnData->nullbitmap, i);
    }
  }
  pOut->columnData->hasNull = false;
D
dapan1121 已提交
1842 1843
}

1844 1845
STagVal getJsonValue(char *json, char *key, bool *isExist) {
  STagVal val = {.pKey = key};
wmmhello's avatar
wmmhello 已提交
1846
  if (tTagIsJson((const STag *)json) == false){
wmmhello's avatar
wmmhello 已提交
1847
    terrno = TSDB_CODE_QRY_JSON_NOT_SUPPORT_ERROR;
wmmhello's avatar
wmmhello 已提交
1848 1849 1850 1851 1852 1853
    if(isExist){
      *isExist = false;
    }
    return val;
  }

1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
  bool find = tTagGet(((const STag *)json), &val);  // json value is null and not exist is different
  if(isExist){
    *isExist = find;
  }
  return val;
}

void vectorJsonContains(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);

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

    if (!colDataIsNull_var(pLeft->columnData, i)) {
      char *pLeftData = colDataGetVarData(pLeft->columnData, i);
      getJsonValue(pLeftData, jsonKey, &isExist);
    }

    colDataAppend(pOutputCol, i, (const char*)(&isExist), false);

  }
  taosMemoryFree(jsonKey);
}

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

  char *pRightData = colDataGetVarData(pRight->columnData, 0);
  char *jsonKey = taosMemoryCalloc(1, varDataLen(pRightData) + 1);
  memcpy(jsonKey, varDataVal(pRightData), varDataLen(pRightData));
  for (; i >= 0 && i < pLeft->numOfRows; i += step) {
    if (colDataIsNull_var(pLeft->columnData, i)) {
      colDataSetNull_var(pOutputCol, i);
      pOutputCol->hasNull = true;
      continue;
    }
    char *pLeftData = colDataGetVarData(pLeft->columnData, i);
    bool isExist = false;
    STagVal value = getJsonValue(pLeftData, jsonKey, &isExist);
    char *data = isExist ? tTagValToData(&value, true) : NULL;
    colDataAppend(pOutputCol, i, data, data == NULL);
    if(isExist && IS_VAR_DATA_TYPE(value.type) && data){
      taosMemoryFree(data);
    }
  }
  taosMemoryFree(jsonKey);
}

1915 1916
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
  switch (binFunctionId) {
D
dapan1121 已提交
1917
    case OP_TYPE_ADD:
1918
      return vectorMathAdd;
D
dapan1121 已提交
1919
    case OP_TYPE_SUB:
1920
      return vectorMathSub;
D
dapan1121 已提交
1921
    case OP_TYPE_MULTI:
1922
      return vectorMathMultiply;
D
dapan1121 已提交
1923
    case OP_TYPE_DIV:
1924
      return vectorMathDivide;
1925
    case OP_TYPE_REM:
1926
      return vectorMathRemainder;
D
dapan1121 已提交
1927 1928
    case OP_TYPE_MINUS:
      return vectorMathMinus;
D
dapan1121 已提交
1929 1930
    case OP_TYPE_ASSIGN:
      return vectorAssign;
D
dapan1121 已提交
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
    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 已提交
1955
    case OP_TYPE_IS_NULL:
D
dapan1121 已提交
1956
      return vectorIsNull;
D
dapan1121 已提交
1957
    case OP_TYPE_IS_NOT_NULL:
D
dapan1121 已提交
1958 1959 1960 1961 1962
      return vectorNotNull;
    case OP_TYPE_BIT_AND:
      return vectorBitAnd;
    case OP_TYPE_BIT_OR:
      return vectorBitOr;
D
dapan1121 已提交
1963 1964
    case OP_TYPE_IS_TRUE:
      return vectorIsTrue;
1965 1966 1967 1968
    case OP_TYPE_JSON_GET_VALUE:
      return vectorJsonArrow;
    case OP_TYPE_JSON_CONTAINS:
      return vectorJsonContains;
1969
    default:
1970
      ASSERT(0);
1971 1972
      return NULL;
  }
D
dapan1121 已提交
1973
}
1974