sclvector.c 51.6 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

D
dapan1121 已提交
28 29 30 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
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 已提交
60 61
_getBigintValue_fn_t getVectorBigintValueFn(int32_t srcType) {
    _getBigintValue_fn_t p = NULL;
D
dapan1121 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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;
82 83
    }else if(srcType==TSDB_DATA_TYPE_TIMESTAMP) {
        p = getVectorBigintValue_BIGINT;
D
dapan1121 已提交
84 85 86 87 88 89
    }else {
        assert(0);
    }
    return p;
}

90 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
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 已提交
122 123 124 125
void* getVectorValueAddr_default(void *src, int32_t index) {
  return src;
}
void* getVectorValueAddr_VAR(void *src, int32_t index) {
H
Haojun Liao 已提交
126
  return colDataGetData((SColumnInfoData *)src, index);
D
dapan1121 已提交
127
}
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

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

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

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

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

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;
179
  colDataAppendInt8(pOut->columnData, rowIndex, (int8_t*) &v);
D
dapan1121 已提交
180 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);
}

H
Haojun Liao 已提交
194
//TODO opt performance, tmp is not needed.
H
Haojun Liao 已提交
195
int32_t vectorConvertFromVarData(const SScalarParam* pIn, SScalarParam* pOut, int32_t inType, int32_t outType) {
196
  int32_t bufSize = pIn->columnData->info.bytes;
H
Haojun Liao 已提交
197
  char *tmp = taosMemoryMalloc(bufSize + VARSTR_HEADER_SIZE);
198

199 200
  bool vton = false;

D
dapan1121 已提交
201
  _bufConverteFunc func = NULL;
202 203 204
  if (TSDB_DATA_TYPE_BOOL == outType) {
    func = varToBool;
  } else if (IS_SIGNED_NUMERIC_TYPE(outType) || TSDB_DATA_TYPE_TIMESTAMP == outType) {
D
dapan 已提交
205
    func = varToSigned;
D
dapan1121 已提交
206
  } else if (IS_UNSIGNED_NUMERIC_TYPE(outType)) {
D
dapan 已提交
207
    func = varToUnsigned;
D
dapan1121 已提交
208
  } else if (IS_FLOAT_TYPE(outType)) {
D
dapan 已提交
209
    func = varToFloat;
210 211 212
  } else if (outType == TSDB_DATA_TYPE_NCHAR) {
    func = varToNchar;
    vton = true;
D
dapan1121 已提交
213
  } else {
D
dapan 已提交
214
    sclError("invalid convert outType:%d", outType);
D
dapan1121 已提交
215 216
    return TSDB_CODE_QRY_APP_ERROR;
  }
217 218

  pOut->numOfRows = pIn->numOfRows;
219
  for (int32_t i = 0; i < pIn->numOfRows; ++i) {
220
    if (colDataIsNull_s(pIn->columnData, i)) {
221
      colDataAppendNULL(pOut->columnData, i);
D
dapan1121 已提交
222 223
      continue;
    }
D
dapan 已提交
224

225
    char* data = colDataGetData(pIn->columnData, i);
226 227
    if (vton) {
      memcpy(tmp, data, varDataTLen(data));
D
dapan 已提交
228
    } else {
229 230 231 232 233 234 235 236 237 238 239 240 241 242
      if (TSDB_DATA_TYPE_VARCHAR == inType) {
        memcpy(tmp, varDataVal(data), varDataLen(data));
        tmp[varDataLen(data)] = 0;
      } else {
        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;
D
dapan 已提交
243
      }
D
dapan1121 已提交
244 245
    }
    
246
    (*func)(tmp, pOut, i);
D
dapan1121 已提交
247 248
  }
  
wafwerar's avatar
wafwerar 已提交
249
  taosMemoryFreeClear(tmp);
D
dapan 已提交
250
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
251
}
D
dapan1121 已提交
252

253
// TODO opt performance
H
Haojun Liao 已提交
254
int32_t vectorConvertImpl(const SScalarParam* pIn, SScalarParam* pOut) {
255 256
  SColumnInfoData* pInputCol  = pIn->columnData;
  SColumnInfoData* pOutputCol = pOut->columnData;
H
Haojun Liao 已提交
257 258

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

261
  if (IS_VAR_DATA_TYPE(inType)) {
D
dapan 已提交
262 263 264
    return vectorConvertFromVarData(pIn, pOut, inType, outType);
  }
  
D
dapan1121 已提交
265
  switch (outType) {
266 267 268
    case TSDB_DATA_TYPE_BOOL: {
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
269
          colDataAppendNULL(pOutputCol, i);
270 271 272 273
          continue;
        }

        bool value = 0;
D
dapan1121 已提交
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
        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);
315 316 317
      }
      break;
    }
D
dapan1121 已提交
318
    case TSDB_DATA_TYPE_BIGINT:
319
    case TSDB_DATA_TYPE_TIMESTAMP: {
320 321
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
322
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
323
          continue;
D
dapan1121 已提交
324
        }
D
dapan 已提交
325 326

        int64_t value = 0;
327
        GET_TYPED_DATA(value, int64_t, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
328
        colDataAppendInt64(pOutputCol, i, (int64_t *)&value);
D
dapan1121 已提交
329 330
      }
      break;
331
    }
D
dapan1121 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    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: {
372 373
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
374
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
375
          continue;
D
dapan1121 已提交
376 377
        }
        
D
dapan 已提交
378
        uint64_t value = 0;
379
        GET_TYPED_DATA(value, uint64_t, inType, colDataGetData(pInputCol, i));
380
        colDataAppendInt64(pOutputCol, i, (int64_t*)&value);
D
dapan1121 已提交
381 382
      }
      break;
D
dapan1121 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    }
    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: {
398 399
      for (int32_t i = 0; i < pIn->numOfRows; ++i) {
        if (colDataIsNull_f(pInputCol->nullbitmap, i)) {
400
          colDataAppendNULL(pOutputCol, i);
D
dapan 已提交
401
          continue;
D
dapan1121 已提交
402 403
        }
        
D
dapan 已提交
404
        double value = 0;
405
        GET_TYPED_DATA(value, double, inType, colDataGetData(pInputCol, i));
D
dapan1121 已提交
406
        colDataAppendDouble(pOutputCol, i, (double*)&value);
D
dapan1121 已提交
407
      }
D
dapan1121 已提交
408 409
      break;  
    }
D
dapan1121 已提交
410
    default:
D
dapan1121 已提交
411
      sclError("invalid convert output type:%d", outType);
D
dapan1121 已提交
412 413 414 415 416 417 418
      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 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/*         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 已提交
439 440
};

D
dapan1121 已提交
441 442 443 444 445 446 447 448 449 450 451 452
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 已提交
453
int32_t vectorConvert(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam* pLeftOut, SScalarParam* pRightOut) {
454
  if (pLeft->pHashFilter != NULL || pRight->pHashFilter != NULL) {
D
dapan1121 已提交
455 456 457
    return TSDB_CODE_SUCCESS;
  }

458 459 460
  int32_t leftType  = GET_PARAM_TYPE(pLeft);
  int32_t rightType = GET_PARAM_TYPE(pRight);
  if (leftType == rightType) {
D
dapan1121 已提交
461 462 463
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
464 465
  SScalarParam *param1 = NULL, *paramOut1 = NULL; 
  SScalarParam *param2 = NULL, *paramOut2 = NULL;
D
dapan1121 已提交
466 467
  int32_t code = 0;
  
468
  if (leftType < rightType) {
D
dapan1121 已提交
469 470 471 472 473 474 475 476 477 478 479
    param1 = pLeft;
    param2 = pRight;
    paramOut1 = pLeftOut;
    paramOut2 = pRightOut;
  } else {
    param1 = pRight;
    param2 = pLeft;
    paramOut1 = pRightOut;
    paramOut2 = pLeftOut;
  }

480
  int8_t type = vectorGetConvertType(GET_PARAM_TYPE(param1), GET_PARAM_TYPE(param2));
D
dapan1121 已提交
481 482 483 484
  if (0 == type) {
    return TSDB_CODE_SUCCESS;
  }

485 486 487 488 489 490 491
  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 已提交
492
    }
493

D
dapan1121 已提交
494 495
    code = vectorConvertImpl(param1, paramOut1);
    if (code) {
H
Haojun Liao 已提交
496
//      taosMemoryFreeClear(paramOut1->data);
D
dapan1121 已提交
497 498 499 500
      return code;
    }
  }
  
501 502 503 504 505 506 507 508 509
  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 已提交
510 511 512 513 514 515 516 517 518
    code = vectorConvertImpl(param2, paramOut2);
    if (code) {
      return code;
    }
  }

  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
519 520 521 522 523 524
enum {
  VECTOR_DO_CONVERT = 0x1,
  VECTOR_UN_CONVERT = 0x2,
};

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

  if (IS_VAR_DATA_TYPE(pCol->info.type)) {
H
Haojun Liao 已提交
528
    pDest->numOfRows = pParam->numOfRows;
529 530

    SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
H
Haojun Liao 已提交
531 532
    pDest->columnData = createColumnInfoData(&t, pParam->numOfRows);
    if (pDest->columnData == NULL) {
533 534
      sclError("malloc %d failed", (int32_t)(pParam->numOfRows * sizeof(double)));
      return TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
535 536
    }

H
Haojun Liao 已提交
537
    int32_t code = vectorConvertImpl(pParam, pDest);
538 539
    if (code != TSDB_CODE_SUCCESS) {
      return code;
D
dapan1121 已提交
540
    }
541

H
Haojun Liao 已提交
542 543 544
    *convert = VECTOR_DO_CONVERT;
  } else {
    *convert = VECTOR_UN_CONVERT;
D
dapan1121 已提交
545
  }
546 547 548 549 550 551 552 553 554 555 556 557

  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
558
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
559 560
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
561
      *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) + getVectorDoubleValueFnRight(pRightCol->pData, 0);
D
dapan1121 已提交
562
    }
563 564 565
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
566 567
    }
  }
568
}
D
dapan1121 已提交
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
static void vectorMathBigintAddHelper(SColumnInfoData* pLeftCol, SColumnInfoData* pRightCol, SColumnInfoData* pOutputCol, int32_t numOfRows, int32_t step, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

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

  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
      *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, 0);
    }
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
    }
  }
}

H
Haojun Liao 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
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);
  }
}

611 612 613 614 615 616
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;

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

H
Haojun Liao 已提交
619 620 621
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
622

623 624 625 626 627
  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 plus duration
    int64_t *output = (int64_t *)pOutputCol->pData;
    _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
    _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
628

629 630 631 632
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) + getVectorBigintValueFnRight(pRightCol->pData, i);
      }
633

634 635 636 637 638 639
      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 已提交
640
      }
641 642 643 644 645

    } else if (pLeft->numOfRows == 1) {
      vectorMathBigintAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathBigintAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
646
    }
647 648 649 650 651 652 653 654 655
  } 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) {
        *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) + getVectorDoubleValueFnRight(pRightCol->pData, i);
      }
D
dapan1121 已提交
656

657 658 659 660 661 662 663 664 665 666 667 668 669
      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) {
      vectorMathAddHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathAddHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, i);
    }
670
  }
H
Haojun Liao 已提交
671 672 673

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
674
}
D
dapan1121 已提交
675

676 677 678 679 680 681 682 683
// 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
684
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
685 686
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
687
      *output = (getVectorDoubleValueFnLeft(pLeftCol->pData, i) - getVectorDoubleValueFnRight(pRightCol->pData, 0)) * factor;
D
dapan1121 已提交
688
    }
689 690 691 692 693 694
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
    }
  }
}
D
dapan1121 已提交
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
static void vectorMathBigintSubHelper(SColumnInfoData* pLeftCol, SColumnInfoData* pRightCol, SColumnInfoData* pOutputCol, int32_t numOfRows, int32_t step, int32_t factor, int32_t i) {
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);

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

  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
      *output = (getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, 0)) * factor;
    }
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
    }
  }
}

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

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

720 721
  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 已提交
722

H
Haojun Liao 已提交
723 724 725
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
726

727 728 729 730 731
  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);
732

733 734 735 736
    if (pLeft->numOfRows == pRight->numOfRows) {
      for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
        *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) - getVectorBigintValueFnRight(pRightCol->pData, i);
      }
D
dapan1121 已提交
737

738 739 740 741 742 743
      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 已提交
744
      }
745 746 747 748 749

    } else if (pLeft->numOfRows == 1) {
      vectorMathBigintSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathBigintSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
D
dapan1121 已提交
750
    }
751 752 753 754 755 756 757 758 759
  } 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) {
        *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) - getVectorDoubleValueFnRight(pRightCol->pData, i);
      }
D
dapan1121 已提交
760

761 762 763 764 765 766 767 768 769 770 771 772 773
      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) {
      vectorMathSubHelper(pRightCol, pLeftCol, pOutputCol, pRight->numOfRows, step, -1, i);
    } else if (pRight->numOfRows == 1) {
      vectorMathSubHelper(pLeftCol, pRightCol, pOutputCol, pLeft->numOfRows, step, 1, i);
    }
774
  }
H
Haojun Liao 已提交
775 776 777

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
778 779
}

780 781 782 783
// 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 已提交
784

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

787
  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
788
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
789 790
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
791
      *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) * getVectorDoubleValueFnRight(pRightCol->pData, 0);
792 793 794 795 796 797
    }
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
    }
  }
D
dapan1121 已提交
798 799
}

800 801
void vectorMathMultiply(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
802
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
803 804 805 806

  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 已提交
807 808 809
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
810 811 812 813 814 815 816

  _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) {
817
      *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) * getVectorDoubleValueFnRight(pRightCol->pData, i);
818 819 820 821 822 823 824 825 826 827 828 829 830 831
    }

    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 已提交
832
  }
H
Haojun Liao 已提交
833 834 835

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
836 837
}

838 839
void vectorMathDivide(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
840
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
841

842 843 844
  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 已提交
845 846 847
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
848

849 850 851 852 853 854
  _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) {
855
      *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) / getVectorDoubleValueFnRight(pRightCol->pData, i);
856 857 858 859 860 861 862 863 864 865 866 867
    }

    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) {
    if (colDataIsNull_f(pLeftCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
868
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
869 870
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
871
        *output = getVectorDoubleValueFnLeft(pLeftCol->pData, 0) / getVectorDoubleValueFnRight(pRightCol->pData, i);
872 873 874 875 876 877 878 879
      }
      pOutputCol->hasNull = pRightCol->hasNull;
      if (pOutputCol->hasNull) {
        memcpy(pOutputCol->nullbitmap, pRightCol->nullbitmap, BitmapLen(pRight->numOfRows));
      }
    }
  } else if (pRight->numOfRows == 1) {
    if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
880
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
881 882
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
883
        *output = getVectorDoubleValueFnLeft(pLeftCol->pData, i) / getVectorDoubleValueFnRight(pRightCol->pData, 0);
884 885 886 887 888 889 890
      }
      pOutputCol->hasNull = pLeftCol->hasNull;
      if (pOutputCol->hasNull) {
        memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(pLeft->numOfRows));
      }
    }
  }
H
Haojun Liao 已提交
891 892 893

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
894 895
}

896 897
void vectorMathRemainder(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
898
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
899 900 901

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

H
Haojun Liao 已提交
903 904 905
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol  = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol = doVectorConvert(pRight, &rightConvert);
906

907 908 909 910 911 912 913 914 915
  _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) {
      if (colDataIsNull_f(pLeftCol->nullbitmap, i) || colDataIsNull_f(pRightCol->nullbitmap, i)) {
916
        colDataAppendNULL(pOutputCol, i);
917 918 919 920 921
        continue;
      }

      double lx = getVectorDoubleValueFnLeft(pLeftCol->pData, i);
      double rx = getVectorDoubleValueFnRight(pRightCol->pData, i);
H
Haojun Liao 已提交
922
      if (isnan(lx) || isinf(lx) || isnan(rx) || isinf(rx)) {
923
        colDataAppendNULL(pOutputCol, i);
924 925 926
        continue;
      }

927
      *output = lx - ((int64_t)(lx / rx)) * rx;
928 929 930
    }
  } else if (pLeft->numOfRows == 1) {
    double lx = getVectorDoubleValueFnLeft(pLeftCol->pData, 0);
H
Haojun Liao 已提交
931
    if (colDataIsNull_f(pLeftCol->nullbitmap, 0) || isnan(lx) || isinf(lx)) {  // Set pLeft->numOfRows NULL value
932
      colDataAppendNNULL(pOutputCol, 0, pRight->numOfRows);
933 934 935
    } else {
      for (; i >= 0 && i < pRight->numOfRows; i += step, output += 1) {
        if (colDataIsNull_f(pRightCol->nullbitmap, i)) {
936
          colDataAppendNULL(pOutputCol, i);
937 938 939 940
          continue;
        }

        double rx = getVectorDoubleValueFnRight(pRightCol->pData, i);
H
Haojun Liao 已提交
941
        if (isnan(rx) || isinf(rx) || FLT_EQUAL(rx, 0)) {
942
          colDataAppendNULL(pOutputCol, i);
943 944 945
          continue;
        }

946
        *output = lx - ((int64_t)(lx / rx)) * rx;
947 948 949 950
      }
    }
  } else if (pRight->numOfRows == 1) {
    double rx = getVectorDoubleValueFnRight(pRightCol->pData, 0);
H
Haojun Liao 已提交
951
    if (colDataIsNull_f(pRightCol->nullbitmap, 0) || FLT_EQUAL(rx, 0)) {  // Set pLeft->numOfRows NULL value
952
      colDataAppendNNULL(pOutputCol, 0, pLeft->numOfRows);
953 954
    } else {
      for (; i >= 0 && i < pLeft->numOfRows; i += step, output += 1) {
H
Haojun Liao 已提交
955
        if (colDataIsNull_f(pLeftCol->nullbitmap, i)) {
956
          colDataAppendNULL(pOutputCol, i);
957 958 959
          continue;
        }

H
Haojun Liao 已提交
960 961
        double lx = getVectorDoubleValueFnLeft(pLeftCol->pData, i);
        if (isnan(lx) || isinf(lx)) {
962
          colDataAppendNULL(pOutputCol, i);
963 964 965
          continue;
        }

966
        *output = lx - ((int64_t)(lx / rx)) * rx;
967 968 969
      }
    }
  }
H
Haojun Liao 已提交
970 971 972

  doReleaseVec(pLeftCol, leftConvert);
  doReleaseVec(pRightCol, rightConvert);
973 974
}

D
dapan1121 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
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) {
    *output = - getVectorDoubleValueFnLeft(pLeftCol->pData, i);
  }

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

  doReleaseVec(pLeftCol,  leftConvert);
}

D
dapan1121 已提交
1001
void vectorConcat(SScalarParam* pLeft, SScalarParam* pRight, void *out, int32_t _ord) {
1002
#if 0
1003 1004
  int32_t len = pLeft->bytes + pRight->bytes;

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

  char *output = (char *)out;
1009 1010
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += len) {
1011 1012 1013
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
      char* right = POINTER_SHIFT(pRight->data, pRight->bytes * i);

H
Haojun Liao 已提交
1014
      if (isNull(left, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1015 1016 1017 1018 1019 1020 1021 1022 1023
        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));
    }
1024 1025
  } else if (pLeft->numOfRows == 1) {
    for (; i >= 0 && i < pRight->numOfRows; i += step, output += len) {
1026
      char *right = POINTER_SHIFT(pRight->data, pRight->bytes * i);
H
Haojun Liao 已提交
1027
      if (isNull(pLeft->data, pLeftCol->info.type) || isNull(right, pRight->info.type)) {
1028 1029 1030 1031 1032 1033 1034 1035
        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));
    }
1036 1037
  } else if (pRight->numOfRows == 1) {
    for (; i >= 0 && i < pLeft->numOfRows; i += step, output += len) {
1038
      char* left = POINTER_SHIFT(pLeft->data, pLeft->bytes * i);
H
Haojun Liao 已提交
1039
      if (isNull(left, pLeftCol->info.type) || isNull(pRight->data, pRight->info.type)) {
1040 1041 1042 1043 1044 1045 1046 1047 1048
        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));
    }
  }
1049
#endif
1050 1051
}

1052 1053 1054
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 已提交
1055

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

1058
  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
1059
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1060 1061
  } else {
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
1062
      *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) & getVectorBigintValueFnRight(pRightCol->pData, 0);
D
dapan1121 已提交
1063
    }
1064 1065 1066
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
1067 1068
    }
  }
1069
}
D
dapan1121 已提交
1070

1071 1072
void vectorBitAnd(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1073
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan 已提交
1074

1075 1076
  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 已提交
1077

H
Haojun Liao 已提交
1078 1079 1080
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1081

H
Haojun Liao 已提交
1082 1083
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan1121 已提交
1084

1085 1086 1087
  int64_t *output = (int64_t *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
1088
      *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) & getVectorBigintValueFnRight(pRightCol->pData, i);
D
dapan1121 已提交
1089
    }
D
dapan1121 已提交
1090

1091 1092 1093 1094 1095 1096
    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 已提交
1097
    }
D
dapan1121 已提交
1098

1099 1100 1101 1102 1103
  } 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 已提交
1104 1105 1106

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1107 1108
}

1109 1110 1111
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 已提交
1112

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

1115
  if (colDataIsNull_f(pRightCol->nullbitmap, 0)) {  // Set pLeft->numOfRows NULL value
1116
    colDataAppendNNULL(pOutputCol, 0, numOfRows);
1117
  } else {
1118
    int64_t rx = getVectorBigintValueFnRight(pRightCol->pData, 0);
1119
    for (; i >= 0 && i < numOfRows; i += step, output += 1) {
1120
      *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) | rx;
D
dapan1121 已提交
1121
    }
1122 1123 1124
    pOutputCol->hasNull = pLeftCol->hasNull;
    if (pOutputCol->hasNull) {
      memcpy(pOutputCol->nullbitmap, pLeftCol->nullbitmap, BitmapLen(numOfRows));
D
dapan1121 已提交
1125 1126
    }
  }
1127
}
D
dapan1121 已提交
1128

1129 1130
void vectorBitOr(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  SColumnInfoData *pOutputCol = pOut->columnData;
1131
  pOut->numOfRows = TMAX(pLeft->numOfRows, pRight->numOfRows);
D
dapan1121 已提交
1132

1133 1134
  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 已提交
1135

H
Haojun Liao 已提交
1136 1137 1138
  int32_t leftConvert = 0, rightConvert = 0;
  SColumnInfoData *pLeftCol   = doVectorConvert(pLeft, &leftConvert);
  SColumnInfoData *pRightCol  = doVectorConvert(pRight, &rightConvert);
D
dapan1121 已提交
1139

H
Haojun Liao 已提交
1140 1141
  _getBigintValue_fn_t getVectorBigintValueFnLeft  = getVectorBigintValueFn(pLeftCol->info.type);
  _getBigintValue_fn_t getVectorBigintValueFnRight = getVectorBigintValueFn(pRightCol->info.type);
D
dapan 已提交
1142

1143 1144 1145
  int64_t *output = (int64_t *)pOutputCol->pData;
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step, output += 1) {
1146
      *output = getVectorBigintValueFnLeft(pLeftCol->pData, i) | getVectorBigintValueFnRight(pRightCol->pData, i);
1147
    }
D
dapan1121 已提交
1148

1149 1150 1151 1152 1153 1154
    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 已提交
1155
    }
1156 1157 1158 1159
  } 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 已提交
1160
  }
H
Haojun Liao 已提交
1161 1162 1163

  doReleaseVec(pLeftCol,  leftConvert);
  doReleaseVec(pRightCol, rightConvert);
D
dapan1121 已提交
1164 1165
}

1166 1167 1168 1169 1170 1171 1172 1173 1174
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) {
1175
      if (colDataIsNull_s(pLeft->columnData, i)) {
D
dapan1121 已提交
1176 1177 1178
        continue;
      }

1179 1180
      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRight->pHashFilter);
1181
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1182
    }
1183
    return;
1184
  }
D
dapan 已提交
1185

1186 1187
  if (pLeft->numOfRows == pRight->numOfRows) {
    for (; i < pRight->numOfRows && i >= 0; i += step) {
1188
      if (colDataIsNull_s(pLeft->columnData, i) || colDataIsNull_s(pRight->columnData, i)) {
1189
        continue;  // TODO set null or ignore
D
dapan1121 已提交
1190 1191
      }

1192
      char *pLeftData = colDataGetData(pLeft->columnData, i);
1193
      char *pRightData = colDataGetData(pRight->columnData, i);
1194
      bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
1195
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
D
dapan1121 已提交
1196
    }
1197
  } else if (pRight->numOfRows == 1) {
1198 1199 1200 1201
    char *pRightData = colDataGetData(pRight->columnData, 0);
    ASSERT(pLeft->pHashFilter == NULL);

    for (; i >= 0 && i < pLeft->numOfRows; i += step) {
1202
      if (colDataIsNull_s(pLeft->columnData, i)) {
1203 1204 1205 1206 1207
        continue;
      }

      char *pLeftData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
1208
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1209 1210 1211 1212
    }
  } else if (pLeft->numOfRows == 1) {
    char *pLeftData = colDataGetData(pLeft->columnData, 0);
    for (; i >= 0 && i < pRight->numOfRows; i += step) {
1213
      if (colDataIsNull_s(pRight->columnData, i)) {
1214 1215 1216 1217 1218
        continue;
      }

      char *pRightData = colDataGetData(pLeft->columnData, i);
      bool  res = filterDoCompare(fp, optr, pLeftData, pRightData);
1219
      colDataAppendInt8(pOut->columnData, i, (int8_t*)&res);
1220
    }
D
dapan1121 已提交
1221 1222 1223
  }
}

D
dapan 已提交
1224
void vectorCompare(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord, int32_t optr) {
D
dapan1121 已提交
1225 1226
  SScalarParam pLeftOut = {0}; 
  SScalarParam pRightOut = {0};
D
dapan1121 已提交
1227 1228 1229
  
  vectorConvert(pLeft, pRight, &pLeftOut, &pRightOut);

D
dapan1121 已提交
1230 1231
  SScalarParam *param1 = NULL; 
  SScalarParam *param2 = NULL;
D
dapan1121 已提交
1232

1233
  if (pLeftOut.columnData != NULL) {
D
dapan1121 已提交
1234 1235 1236 1237 1238
    param1 = &pLeftOut;
  } else {
    param1 = pLeft;
  }

1239
  if (pRightOut.columnData != NULL) {
D
dapan1121 已提交
1240 1241 1242 1243 1244
    param2 = &pRightOut;
  } else {
    param2 = pRight;
  }

D
dapan 已提交
1245
  vectorCompareImpl(param1, param2, pOut, _ord, optr);
D
dapan1121 已提交
1246 1247
  sclFreeParam(&pLeftOut);
  sclFreeParam(&pRightOut);  
D
dapan1121 已提交
1248 1249
}

D
dapan 已提交
1250 1251
void vectorGreater(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_THAN);
D
dapan1121 已提交
1252 1253
}

D
dapan 已提交
1254 1255
void vectorGreaterEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_GREATER_EQUAL);
D
dapan1121 已提交
1256 1257
}

D
dapan 已提交
1258 1259
void vectorLower(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_THAN);
D
dapan1121 已提交
1260 1261
}

D
dapan 已提交
1262 1263
void vectorLowerEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LOWER_EQUAL);
D
dapan1121 已提交
1264 1265
}

D
dapan 已提交
1266 1267
void vectorEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_EQUAL);
D
dapan1121 已提交
1268 1269
}

D
dapan 已提交
1270 1271
void vectorNotEqual(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_EQUAL);
D
dapan1121 已提交
1272 1273
}

D
dapan 已提交
1274 1275
void vectorIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_IN);
D
dapan1121 已提交
1276 1277
}

D
dapan 已提交
1278 1279
void vectorNotIn(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_IN);
D
dapan1121 已提交
1280 1281
}

D
dapan 已提交
1282 1283
void vectorLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_LIKE);
D
dapan1121 已提交
1284 1285
}

D
dapan 已提交
1286 1287
void vectorNotLike(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NOT_LIKE);
D
dapan1121 已提交
1288 1289
}

D
dapan 已提交
1290 1291
void vectorMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_MATCH);
D
dapan1121 已提交
1292 1293
}

D
dapan 已提交
1294 1295
void vectorNotMatch(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorCompare(pLeft, pRight, pOut, _ord, OP_TYPE_NMATCH);
D
dapan1121 已提交
1296 1297
}

D
dapan 已提交
1298
void vectorIsNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1299
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
1300 1301
    int8_t v = colDataIsNull_s(pLeft->columnData, i)? 1:0;
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1302
  }
1303
  pOut->numOfRows = pLeft->numOfRows;
D
dapan1121 已提交
1304 1305
}

D
dapan 已提交
1306
void vectorNotNull(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
1307
  for(int32_t i = 0; i < pLeft->numOfRows; ++i) {
1308 1309
    int8_t v = colDataIsNull_s(pLeft->columnData, i)? 0:1;
    colDataAppendInt8(pOut->columnData, i, &v);
D
dapan1121 已提交
1310
  }
1311
  pOut->numOfRows = pLeft->numOfRows;
D
dapan 已提交
1312
}
D
dapan1121 已提交
1313

D
dapan 已提交
1314 1315
void vectorIsTrue(SScalarParam* pLeft, SScalarParam* pRight, SScalarParam *pOut, int32_t _ord) {
  vectorConvertImpl(pLeft, pOut);
D
dapan1121 已提交
1316 1317
}

1318 1319
_bin_scalar_fn_t getBinScalarOperatorFn(int32_t binFunctionId) {
  switch (binFunctionId) {
D
dapan1121 已提交
1320
    case OP_TYPE_ADD:
1321
      return vectorMathAdd;
D
dapan1121 已提交
1322
    case OP_TYPE_SUB:
1323
      return vectorMathSub;
D
dapan1121 已提交
1324
    case OP_TYPE_MULTI:
1325
      return vectorMathMultiply;
D
dapan1121 已提交
1326
    case OP_TYPE_DIV:
1327
      return vectorMathDivide;
D
dapan1121 已提交
1328
    case OP_TYPE_MOD:
1329
      return vectorMathRemainder;
D
dapan1121 已提交
1330 1331
    case OP_TYPE_MINUS:
      return vectorMathMinus;
D
dapan1121 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    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 已提交
1356
    case OP_TYPE_IS_NULL:
D
dapan1121 已提交
1357
      return vectorIsNull;
D
dapan1121 已提交
1358
    case OP_TYPE_IS_NOT_NULL:
D
dapan1121 已提交
1359 1360 1361 1362 1363
      return vectorNotNull;
    case OP_TYPE_BIT_AND:
      return vectorBitAnd;
    case OP_TYPE_BIT_OR:
      return vectorBitOr;
D
dapan1121 已提交
1364 1365
    case OP_TYPE_IS_TRUE:
      return vectorIsTrue;
1366 1367 1368 1369
    default:
      assert(0);
      return NULL;
  }
D
dapan1121 已提交
1370
}