scalar.c 39.8 KB
Newer Older
D
dapan1121 已提交
1 2
#include "function.h"
#include "functionMgt.h"
H
Haojun Liao 已提交
3 4
#include "nodes.h"
#include "querynodes.h"
D
dapan1121 已提交
5
#include "sclInt.h"
H
Haojun Liao 已提交
6 7 8
#include "sclvector.h"
#include "tcommon.h"
#include "tdatablock.h"
9
#include "scalar.h"
S
slzhou 已提交
10
#include "tudf.h"
D
fix bug  
dapan1121 已提交
11
#include "ttime.h"
12
#include "tcompare.h"
D
dapan1121 已提交
13

D
dapan1121 已提交
14
int32_t scalarGetOperatorParamNum(EOperatorType type) {
G
Ganlin Zhao 已提交
15
  if (OP_TYPE_IS_NULL == type || OP_TYPE_IS_NOT_NULL == type || OP_TYPE_IS_TRUE == type || OP_TYPE_IS_NOT_TRUE == type
D
dapan1121 已提交
16 17
   || OP_TYPE_IS_FALSE == type || OP_TYPE_IS_NOT_FALSE == type || OP_TYPE_IS_UNKNOWN == type || OP_TYPE_IS_NOT_UNKNOWN == type
   || OP_TYPE_MINUS == type) {
D
dapan1121 已提交
18 19 20 21 22 23
    return 1;
  }

  return 2;
}

D
dapan1121 已提交
24
int32_t sclConvertToTsValueNode(int8_t precision, SValueNode* valueNode) {
D
dapan 已提交
25
  char *timeStr = valueNode->datum.p;
D
dapan1121 已提交
26 27 28
  int32_t code = convertStringToTimestamp(valueNode->node.resType.type, valueNode->datum.p, precision, &valueNode->datum.i);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
D
dapan 已提交
29 30
  }
  taosMemoryFree(timeStr);
D
dapan1121 已提交
31
  valueNode->typeData = valueNode->datum.i;
G
Ganlin Zhao 已提交
32

D
dapan 已提交
33 34
  valueNode->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
  valueNode->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
D
dapan1121 已提交
35 36

  return TSDB_CODE_SUCCESS;
D
dapan 已提交
37 38
}

H
Haojun Liao 已提交
39
int32_t sclCreateColumnInfoData(SDataType* pType, int32_t numOfRows, SScalarParam* pParam) {
H
Haojun Liao 已提交
40
  SColumnInfoData* pColumnData = taosMemoryCalloc(1, sizeof(SColumnInfoData));
41 42
  if (pColumnData == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
43
    return terrno;
44 45 46 47 48 49 50
  }

  pColumnData->info.type      = pType->type;
  pColumnData->info.bytes     = pType->bytes;
  pColumnData->info.scale     = pType->scale;
  pColumnData->info.precision = pType->precision;

51
  int32_t code = colInfoDataEnsureCapacity(pColumnData, numOfRows);
52 53
  if (code != TSDB_CODE_SUCCESS) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
54
    taosMemoryFree(pColumnData);
H
Haojun Liao 已提交
55
    return terrno;
56
  }
H
Haojun Liao 已提交
57 58

  pParam->columnData = pColumnData;
D
dapan1121 已提交
59
  pParam->colAlloced = true;
H
Haojun Liao 已提交
60
  return TSDB_CODE_SUCCESS;
61 62
}

D
dapan1121 已提交
63
int32_t doConvertDataType(SValueNode* pValueNode, SScalarParam* out, int32_t* overflow) {
64
  SScalarParam in = {.numOfRows = 1};
H
Haojun Liao 已提交
65 66 67 68 69
  int32_t code = sclCreateColumnInfoData(&pValueNode->node.resType, 1, &in);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

70
  colDataAppend(in.columnData, 0, nodesGetValueFromNode(pValueNode), false);
71

72
  colInfoDataEnsureCapacity(out->columnData, 1);
D
dapan1121 已提交
73
  code = vectorConvertImpl(&in, out, overflow);
74 75 76
  sclFreeParam(&in);

  return code;
77 78
}

D
dapan1121 已提交
79 80 81 82 83 84 85
int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) {
  SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(type), true, false);
  if (NULL == pObj) {
    sclError("taosHashInit failed, size:%d", 256);
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

G
Ganlin Zhao 已提交
86
  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));
D
dapan1121 已提交
87 88 89 90

  int32_t code = 0;
  SNodeListNode *nodeList = (SNodeListNode *)pNode;
  SListCell *cell = nodeList->pNodeList->pHead;
H
Haojun Liao 已提交
91
  SScalarParam out = {.columnData = taosMemoryCalloc(1, sizeof(SColumnInfoData))};
92

D
dapan1121 已提交
93 94
  int32_t len = 0;
  void *buf = NULL;
G
Ganlin Zhao 已提交
95

D
dapan1121 已提交
96 97
  for (int32_t i = 0; i < nodeList->pNodeList->length; ++i) {
    SValueNode *valueNode = (SValueNode *)cell->pNode;
G
Ganlin Zhao 已提交
98

D
dapan1121 已提交
99
    if (valueNode->node.resType.type != type) {
100
      out.columnData->info.type = type;
D
dapan1121 已提交
101 102 103 104 105 106 107 108 109
      if (IS_VAR_DATA_TYPE(type)) {
        if (IS_VAR_DATA_TYPE(valueNode->node.resType.type)) {
          out.columnData->info.bytes = valueNode->node.resType.bytes * TSDB_NCHAR_SIZE;
        } else {
          out.columnData->info.bytes = 64 * TSDB_NCHAR_SIZE;
        }
      } else {
        out.columnData->info.bytes = tDataTypes[type].bytes;
      }
110

D
dapan1121 已提交
111 112
      int32_t overflow = 0;
      code = doConvertDataType(valueNode, &out, &overflow);
113
      if (code != TSDB_CODE_SUCCESS) {
114
//        sclError("convert data from %d to %d failed", in.type, out.type);
D
dapan1121 已提交
115 116 117
        SCL_ERR_JRET(code);
      }

D
dapan1121 已提交
118 119 120 121 122
      if (overflow) {
        cell = cell->pNext;
        continue;
      }

D
dapan1121 已提交
123
      if (IS_VAR_DATA_TYPE(type)) {
124
        buf = colDataGetVarData(out.columnData, 0);
D
dapan1121 已提交
125
        len = varDataTLen(buf);
D
dapan1121 已提交
126 127
      } else {
        len = tDataTypes[type].bytes;
128
        buf = out.columnData->pData;
D
dapan1121 已提交
129 130 131
      }
    } else {
      buf = nodesGetValueFromNode(valueNode);
D
dapan1121 已提交
132
      if (IS_VAR_DATA_TYPE(type)) {
133
        len = varDataTLen(buf);
D
dapan1121 已提交
134 135
      } else {
        len = valueNode->node.resType.bytes;
136
      }
D
dapan1121 已提交
137
    }
G
Ganlin Zhao 已提交
138

139
    if (taosHashPut(pObj, buf, (size_t)len, NULL, 0)) {
D
dapan1121 已提交
140
      sclError("taosHashPut to set failed");
D
dapan1121 已提交
141 142
      SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
D
dapan1121 已提交
143

D
dapan1121 已提交
144 145 146 147
    colDataDestroy(out.columnData);
    taosMemoryFreeClear(out.columnData);
    out.columnData = taosMemoryCalloc(1, sizeof(SColumnInfoData));

D
dapan1121 已提交
148
    cell = cell->pNext;
D
dapan1121 已提交
149 150 151
  }

  *data = pObj;
D
dapan1121 已提交
152 153 154

  colDataDestroy(out.columnData);
  taosMemoryFreeClear(out.columnData);
D
dapan1121 已提交
155 156 157
  return TSDB_CODE_SUCCESS;

_return:
D
dapan1121 已提交
158 159 160

  colDataDestroy(out.columnData);
  taosMemoryFreeClear(out.columnData);
D
dapan1121 已提交
161 162 163 164
  taosHashCleanup(pObj);
  SCL_RET(code);
}

D
dapan1121 已提交
165 166 167 168 169 170 171
void sclFreeRes(SHashObj *res) {
  SScalarParam *p = NULL;
  void *pIter = taosHashIterate(res, NULL);
  while (pIter) {
    p = (SScalarParam *)pIter;

    if (p) {
D
dapan 已提交
172
      sclFreeParam(p);
D
dapan1121 已提交
173 174 175 176 177 178
    }
    pIter = taosHashIterate(res, pIter);
  }
  taosHashCleanup(res);
}

D
dapan1121 已提交
179
void sclFreeParam(SScalarParam *param) {
D
dapan1121 已提交
180 181 182 183 184
  if (!param->colAlloced) {
    return;
  }
  
  if (param->columnData != NULL) {
185
    colDataDestroy(param->columnData);
186
    taosMemoryFreeClear(param->columnData);
187 188 189 190
  }

  if (param->pHashFilter != NULL) {
    taosHashCleanup(param->pHashFilter);
D
dapan1121 已提交
191
    param->pHashFilter = NULL;
192
  }
D
dapan1121 已提交
193 194
}

D
dapan1121 已提交
195 196 197 198
int32_t sclCopyValueNodeValue(SValueNode *pNode, void **res) {
  if (TSDB_DATA_TYPE_NULL == pNode->node.resType.type) {
    return TSDB_CODE_SUCCESS;
  }
G
Ganlin Zhao 已提交
199

wafwerar's avatar
wafwerar 已提交
200
  *res = taosMemoryMalloc(pNode->node.resType.bytes);
D
dapan1121 已提交
201 202 203 204 205 206 207 208 209
  if (NULL == (*res)) {
    sclError("malloc %d failed", pNode->node.resType.bytes);
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  memcpy(*res, nodesGetValueFromNode(pNode), pNode->node.resType.bytes);
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222
void sclFreeParamList(SScalarParam *param, int32_t paramNum) {
  if (NULL == param) {
    return;
  }

  for (int32_t i = 0; i < paramNum; ++i) {
    SScalarParam* p = param + i;
    sclFreeParam(p);
  }

  taosMemoryFree(param);
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
void sclDowngradeValueType(SValueNode *valueNode) {
  switch (valueNode->node.resType.type) {
    case TSDB_DATA_TYPE_BIGINT: {
      int8_t i8 = valueNode->datum.i;
      if (i8 == valueNode->datum.i) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_TINYINT;
        *(int8_t*)&valueNode->typeData = i8;
        break;
      }
      int16_t i16 = valueNode->datum.i;
      if (i16 == valueNode->datum.i) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_SMALLINT;
        *(int16_t*)&valueNode->typeData = i16;
        break;
      }
      int32_t i32 = valueNode->datum.i;
      if (i32 == valueNode->datum.i) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_INT;
        *(int32_t*)&valueNode->typeData = i32;
        break;
      }
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT:{
      uint8_t u8 = valueNode->datum.i;
      if (u8 == valueNode->datum.i) {
        int8_t i8 = valueNode->datum.i;
        if (i8 == valueNode->datum.i) {
          valueNode->node.resType.type = TSDB_DATA_TYPE_TINYINT;
          *(int8_t*)&valueNode->typeData = i8;
        } else {
          valueNode->node.resType.type = TSDB_DATA_TYPE_UTINYINT;
          *(uint8_t*)&valueNode->typeData = u8;
        }
        break;
      }
      uint16_t u16 = valueNode->datum.i;
      if (u16 == valueNode->datum.i) {
        int16_t i16 = valueNode->datum.i;
        if (i16 == valueNode->datum.i) {
          valueNode->node.resType.type = TSDB_DATA_TYPE_SMALLINT;
          *(int16_t*)&valueNode->typeData = i16;
        } else {
          valueNode->node.resType.type = TSDB_DATA_TYPE_USMALLINT;
          *(uint16_t*)&valueNode->typeData = u16;
        }
        break;
      }
      uint32_t u32 = valueNode->datum.i;
      if (u32 == valueNode->datum.i) {
        int32_t i32 = valueNode->datum.i;
        if (i32 == valueNode->datum.i) {
          valueNode->node.resType.type = TSDB_DATA_TYPE_INT;
          *(int32_t*)&valueNode->typeData = i32;
        } else {
          valueNode->node.resType.type = TSDB_DATA_TYPE_UINT;
          *(uint32_t*)&valueNode->typeData = u32;
        }
        break;
      }
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
      float f = valueNode->datum.d;
      if (FLT_EQUAL(f, valueNode->datum.d)) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_FLOAT;
        *(float*)&valueNode->typeData = f;
        break;
      }
      break;
    }
    default:
      break;
  }
}

D
dapan1121 已提交
299 300
int32_t sclInitParam(SNode* node, SScalarParam *param, SScalarCtx *ctx, int32_t *rowNum) {
  switch (nodeType(node)) {
D
dapan1121 已提交
301 302 303 304 305
    case QUERY_NODE_LEFT_VALUE: {
      SSDataBlock* pb = taosArrayGetP(ctx->pBlockList, 0);
      param->numOfRows = pb->info.rows;
      break;
    }
D
dapan1121 已提交
306 307
    case QUERY_NODE_VALUE: {
      SValueNode *valueNode = (SValueNode *)node;
308

H
Haojun Liao 已提交
309
      ASSERT(param->columnData == NULL);
310
      param->numOfRows = 1;
H
Haojun Liao 已提交
311
      /*int32_t code = */sclCreateColumnInfoData(&valueNode->node.resType, 1, param);
wmmhello's avatar
wmmhello 已提交
312
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
313
        colDataAppendNULL(param->columnData, 0);
314 315
      } else {
        colDataAppend(param->columnData, 0, nodesGetValueFromNode(valueNode), false);
D
dapan1121 已提交
316
      }
D
dapan1121 已提交
317 318
      break;
    }
D
dapan1121 已提交
319 320
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)node;
321 322
      if (LIST_LENGTH(nodeList->pNodeList) <= 0) {
        sclError("invalid length in nodeList, length:%d", LIST_LENGTH(nodeList->pNodeList));
D
dapan1121 已提交
323 324 325
        SCL_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan1121 已提交
326 327 328 329
      int32_t type = vectorGetConvertType(ctx->type.selfType, ctx->type.peerType);
      if (type == 0) {
        type = nodeList->dataType.type;
      }
G
Ganlin Zhao 已提交
330

D
dapan1121 已提交
331 332
      SCL_ERR_RET(scalarGenerateSetFromList((void **)&param->pHashFilter, node, type));
      param->hashValueType = type;
D
dapan1121 已提交
333
      param->colAlloced = true;
D
dapan 已提交
334
      if (taosHashPut(ctx->pRes, &node, POINTER_BYTES, param, sizeof(*param))) {
335
        taosHashCleanup(param->pHashFilter);
D
dapan1121 已提交
336
        param->pHashFilter = NULL;
D
dapan 已提交
337 338
        sclError("taosHashPut nodeList failed, size:%d", (int32_t)sizeof(*param));
        return TSDB_CODE_QRY_OUT_OF_MEMORY;
G
Ganlin Zhao 已提交
339
      }
D
dapan1121 已提交
340
      param->colAlloced = false;
D
dapan1121 已提交
341 342
      break;
    }
X
Xiaoyu Wang 已提交
343
    case QUERY_NODE_COLUMN: {
D
dapan 已提交
344 345
      if (NULL == ctx->pBlockList) {
        sclError("invalid node type for constant calculating, type:%d, src:%p", nodeType(node), ctx->pBlockList);
D
dapan1121 已提交
346 347
        SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
G
Ganlin Zhao 已提交
348

X
Xiaoyu Wang 已提交
349
      SColumnNode *ref = (SColumnNode *)node;
350 351 352 353 354 355 356 357 358 359 360

      int32_t index = -1;
      for(int32_t i = 0; i < taosArrayGetSize(ctx->pBlockList); ++i) {
        SSDataBlock* pb = taosArrayGetP(ctx->pBlockList, i);
        if (pb->info.blockId == ref->dataBlockId) {
          index = i;
          break;
        }
      }

      if (index == -1) {
D
dapan1121 已提交
361
        sclError("column tupleId is too big, tupleId:%d, dataBlockNum:%d", ref->dataBlockId, (int32_t)taosArrayGetSize(ctx->pBlockList));
D
dapan 已提交
362 363 364
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

365
      SSDataBlock *block = *(SSDataBlock **)taosArrayGet(ctx->pBlockList, index);
G
Ganlin Zhao 已提交
366 367 368 369 370
      if (NULL == block) {
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

      if (ref->slotId >= taosArrayGetSize(block->pDataBlock)) {
D
dapan 已提交
371
        sclError("column slotId is too big, slodId:%d, dataBlockSize:%d", ref->slotId, (int32_t)taosArrayGetSize(block->pDataBlock));
D
dapan1121 已提交
372 373 374
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan 已提交
375
      SColumnInfoData *columnData = (SColumnInfoData *)taosArrayGet(block->pDataBlock, ref->slotId);
376 377 378
#if TAG_FILTER_DEBUG
      qDebug("tagfilter column info, slotId:%d, colId:%d, type:%d", ref->slotId, columnData->info.colId, columnData->info.type);
#endif
379 380
      param->numOfRows = block->info.rows;
      param->columnData = columnData;
D
dapan1121 已提交
381 382
      break;
    }
383 384 385
    case QUERY_NODE_FUNCTION:
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION: {
D
dapan1121 已提交
386 387 388 389 390 391
      SScalarParam *res = (SScalarParam *)taosHashGet(ctx->pRes, &node, POINTER_BYTES);
      if (NULL == res) {
        sclError("no result for node, type:%d, node:%p", nodeType(node), node);
        SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
      *param = *res;
D
dapan1121 已提交
392
      param->colAlloced = false;
D
dapan1121 已提交
393 394
      break;
    }
395 396
    default:
      break;
D
dapan1121 已提交
397 398
  }

399 400 401
  if (param->numOfRows > *rowNum) {
    if ((1 != param->numOfRows) && (1 < *rowNum)) {
      sclError("different row nums, rowNum:%d, newRowNum:%d", *rowNum, param->numOfRows);
D
dapan1121 已提交
402 403
      SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
G
Ganlin Zhao 已提交
404

405
    *rowNum = param->numOfRows;
D
dapan1121 已提交
406 407
  }

408
  param->param = ctx->param;
D
dapan1121 已提交
409 410 411
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
412
int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *paramNum, int32_t *rowNum) {
D
dapan1121 已提交
413
  int32_t code = 0;
D
dapan1121 已提交
414 415
  if (NULL == pParamList) {
    if (ctx->pBlockList) {
D
dapan1121 已提交
416
      SSDataBlock *pBlock = taosArrayGetP(ctx->pBlockList, 0);
D
dapan1121 已提交
417 418 419 420 421
      *rowNum = pBlock->info.rows;
    } else {
      *rowNum = 1;
    }

D
dapan 已提交
422
    *paramNum = 1;
D
dapan1121 已提交
423
  } else {
D
dapan 已提交
424
    *paramNum = pParamList->length;
D
dapan1121 已提交
425 426
  }

D
dapan 已提交
427
  SScalarParam *paramList = taosMemoryCalloc(*paramNum, sizeof(SScalarParam));
D
dapan1121 已提交
428
  if (NULL == paramList) {
D
dapan 已提交
429
    sclError("calloc %d failed", (int32_t)((*paramNum) * sizeof(SScalarParam)));
D
dapan1121 已提交
430
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
431 432
  }

D
dapan1121 已提交
433 434 435 436
  if (pParamList) {
    SNode *tnode = NULL;
    int32_t i = 0;
    if (SCL_IS_CONST_CALC(ctx)) {
G
Ganlin Zhao 已提交
437
      WHERE_EACH (tnode, pParamList) {
D
dapan1121 已提交
438
        if (!SCL_IS_CONST_NODE(tnode)) {
D
dapan 已提交
439
          WHERE_NEXT;
D
dapan1121 已提交
440 441 442 443
        } else {
          SCL_ERR_JRET(sclInitParam(tnode, &paramList[i], ctx, rowNum));
          ERASE_NODE(pParamList);
        }
G
Ganlin Zhao 已提交
444

D
dapan1121 已提交
445 446 447
        ++i;
      }
    } else {
G
Ganlin Zhao 已提交
448
      FOREACH(tnode, pParamList) {
D
dapan1121 已提交
449 450 451
        SCL_ERR_JRET(sclInitParam(tnode, &paramList[i], ctx, rowNum));
        ++i;
      }
D
dapan1121 已提交
452
    }
D
dapan1121 已提交
453 454 455
  } else {
    paramList[0].numOfRows = *rowNum;
  }
D
dapan1121 已提交
456

D
dapan1121 已提交
457
  if (0 == *rowNum) {
G
Ganlin Zhao 已提交
458
    taosMemoryFreeClear(paramList);
D
dapan1121 已提交
459
  }
D
dapan1121 已提交
460

D
dapan1121 已提交
461
  *pParams = paramList;
D
dapan1121 已提交
462
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
463

D
dapan1121 已提交
464
_return:
wafwerar's avatar
wafwerar 已提交
465
  taosMemoryFreeClear(paramList);
D
dapan1121 已提交
466 467 468
  SCL_RET(code);
}

D
dapan1121 已提交
469 470 471 472
int32_t sclGetNodeType(SNode *pNode, SScalarCtx *ctx) {
  if (NULL == pNode) {
    return -1;
  }
G
Ganlin Zhao 已提交
473

wafwerar's avatar
wafwerar 已提交
474
  switch ((int)nodeType(pNode)) {
D
dapan1121 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    case QUERY_NODE_VALUE: {
      SValueNode *valueNode = (SValueNode *)pNode;
      return valueNode->node.resType.type;
    }
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)pNode;
      return nodeList->dataType.type;
    }
    case QUERY_NODE_COLUMN: {
      SColumnNode *colNode = (SColumnNode *)pNode;
      return colNode->node.resType.type;
    }
    case QUERY_NODE_FUNCTION:
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION: {
      SScalarParam *res = (SScalarParam *)taosHashGet(ctx->pRes, &pNode, POINTER_BYTES);
      if (NULL == res) {
        sclError("no result for node, type:%d, node:%p", nodeType(pNode), pNode);
        return -1;
      }
      return res->columnData->info.type;
    }
  }

  return -1;
}


void sclSetOperatorValueType(SOperatorNode *node, SScalarCtx *ctx) {
  ctx->type.opResType = node->node.resType.type;
  ctx->type.selfType = sclGetNodeType(node->pLeft, ctx);
  ctx->type.peerType = sclGetNodeType(node->pRight, ctx);
}

D
dapan1121 已提交
509 510
int32_t sclInitOperatorParams(SScalarParam **pParams, SOperatorNode *node, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
511
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
512 513 514 515
  if (NULL == node->pLeft || (paramNum == 2 && NULL == node->pRight)) {
    sclError("invalid operation node, left:%p, right:%p", node->pLeft, node->pRight);
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
G
Ganlin Zhao 已提交
516

wafwerar's avatar
wafwerar 已提交
517
  SScalarParam *paramList = taosMemoryCalloc(paramNum, sizeof(SScalarParam));
D
dapan1121 已提交
518 519
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam)));
D
dapan1121 已提交
520 521 522
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
523 524
  sclSetOperatorValueType(node, ctx);

D
dapan1121 已提交
525
  SCL_ERR_JRET(sclInitParam(node->pLeft, &paramList[0], ctx, rowNum));
D
dapan1121 已提交
526
  if (paramNum > 1) {
D
dapan1121 已提交
527
    TSWAP(ctx->type.selfType, ctx->type.peerType);
D
dapan1121 已提交
528
    SCL_ERR_JRET(sclInitParam(node->pRight, &paramList[1], ctx, rowNum));
D
dapan1121 已提交
529 530
  }

D
dapan1121 已提交
531
  *pParams = paramList;
D
dapan1121 已提交
532
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
533 534

_return:
wafwerar's avatar
wafwerar 已提交
535
  taosMemoryFreeClear(paramList);
D
dapan1121 已提交
536
  SCL_RET(code);
D
dapan1121 已提交
537 538
}

539
int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) {
D
dapan1121 已提交
540 541
  SScalarParam *params = NULL;
  int32_t rowNum = 0;
D
dapan 已提交
542
  int32_t paramNum = 0;
D
dapan1121 已提交
543
  int32_t code = 0;
D
dapan 已提交
544
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &paramNum, &rowNum));
D
dapan1121 已提交
545

D
dapan1121 已提交
546
  if (fmIsUserDefinedFunc(node->funcId)) {
547
    code = callUdfScalarFunc(node->functionName, params, paramNum, output);
548 549 550 551
    if (code != 0) {
      sclError("fmExecFunction error. callUdfScalarFunc. function name: %s, udf code:%d", node->functionName, code);
      goto _return;
    }
D
dapan1121 已提交
552 553 554 555 556 557 558
  } else {
    SScalarFuncExecFuncs ffpSet = {0};
    code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet);
    if (code) {
      sclError("fmGetFuncExecFuncs failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
      SCL_ERR_JRET(code);
    }
G
Ganlin Zhao 已提交
559

H
Haojun Liao 已提交
560 561 562
    code = sclCreateColumnInfoData(&node->node.resType, rowNum, output);
    if (code != TSDB_CODE_SUCCESS) {
      SCL_ERR_JRET(code);
D
dapan1121 已提交
563 564 565 566 567 568 569
    }

    code = (*ffpSet.process)(params, paramNum, output);
    if (code) {
      sclError("scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
      SCL_ERR_JRET(code);
    }
D
dapan1121 已提交
570 571 572 573
  }

_return:

D
dapan1121 已提交
574
  sclFreeParamList(params, paramNum);
D
dapan1121 已提交
575 576 577 578 579 580 581 582 583 584 585 586
  SCL_RET(code);
}

int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *output) {
  if (NULL == node->pParameterList || node->pParameterList->length <= 0) {
    sclError("invalid logic parameter list, list:%p, paramNum:%d", node->pParameterList, node->pParameterList ? node->pParameterList->length : 0);
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  if (TSDB_DATA_TYPE_BOOL != node->node.resType.type) {
    sclError("invalid logic resType, type:%d", node->node.resType.type);
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
587 588
  }

D
dapan1121 已提交
589 590 591 592 593 594 595
  if (LOGIC_COND_TYPE_NOT == node->condType && node->pParameterList->length > 1) {
    sclError("invalid NOT operation parameter number, paramNum:%d", node->pParameterList->length);
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  SScalarParam *params = NULL;
  int32_t rowNum = 0;
D
dapan 已提交
596
  int32_t paramNum = 0;
D
dapan1121 已提交
597
  int32_t code = 0;
D
dapan 已提交
598
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &paramNum, &rowNum));
D
dapan1121 已提交
599 600 601 602
  if (NULL == params) {
    output->numOfRows = 0;
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
603

604 605 606 607
  int32_t type = node->node.resType.type;
  output->numOfRows = rowNum;

  SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
H
Haojun Liao 已提交
608 609 610
  code = sclCreateColumnInfoData(&t, rowNum, output);
  if (code != TSDB_CODE_SUCCESS) {
    SCL_ERR_JRET(code);
D
dapan1121 已提交
611
  }
D
dapan1121 已提交
612

613 614
  int32_t numOfQualified = 0;

D
dapan1121 已提交
615
  bool value = false;
D
dapan 已提交
616
  bool complete = true;
D
dapan1121 已提交
617
  for (int32_t i = 0; i < rowNum; ++i) {
D
dapan 已提交
618 619
    complete = true;
    for (int32_t m = 0; m < paramNum; ++m) {
D
dapan1121 已提交
620
      if (NULL == params[m].columnData) {
D
dapan 已提交
621
        complete = false;
D
dapan1121 已提交
622 623
        continue;
      }
624 625 626
      char* p = colDataGetData(params[m].columnData, i);
      GET_TYPED_DATA(value, bool, params[m].columnData->info.type, p);

D
dapan1121 已提交
627
      if (LOGIC_COND_TYPE_AND == node->condType && (false == value)) {
D
dapan1121 已提交
628
        complete = true;
D
dapan1121 已提交
629 630
        break;
      } else if (LOGIC_COND_TYPE_OR == node->condType && value) {
D
dapan1121 已提交
631
        complete = true;
D
dapan1121 已提交
632 633 634 635 636 637
        break;
      } else if (LOGIC_COND_TYPE_NOT == node->condType) {
        value = !value;
      }
    }

D
dapan 已提交
638 639
    if (complete) {
      colDataAppend(output->columnData, i, (char*) &value, false);
640 641 642
      if (value) {
        numOfQualified++;
      }
D
dapan 已提交
643
    }
D
dapan1121 已提交
644 645
  }

D
dapan1121 已提交
646 647 648 649 650
  if (SCL_IS_CONST_CALC(ctx) && (false == complete)) {
    sclFreeParam(output);
    output->numOfRows = 0;
  }

651
  output->numOfQualified = numOfQualified;
D
dapan1121 已提交
652

653
_return:
D
dapan1121 已提交
654
  sclFreeParamList(params, paramNum);
D
dapan1121 已提交
655
  SCL_RET(code);
D
dapan1121 已提交
656 657 658 659 660 661
}

int32_t sclExecOperator(SOperatorNode *node, SScalarCtx *ctx, SScalarParam *output) {
  SScalarParam *params = NULL;
  int32_t rowNum = 0;
  int32_t code = 0;
662

wmmhello's avatar
wmmhello 已提交
663
  // json not support in in operator
H
Haojun Liao 已提交
664
  if (nodeType(node->pLeft) == QUERY_NODE_VALUE) {
wmmhello's avatar
wmmhello 已提交
665
    SValueNode *valueNode = (SValueNode *)node->pLeft;
H
Haojun Liao 已提交
666
    if (valueNode->node.resType.type == TSDB_DATA_TYPE_JSON && (node->opType == OP_TYPE_IN || node->opType == OP_TYPE_NOT_IN)) {
wmmhello's avatar
wmmhello 已提交
667 668 669 670
      SCL_RET(TSDB_CODE_QRY_JSON_IN_ERROR);
    }
  }

D
dapan1121 已提交
671
  SCL_ERR_RET(sclInitOperatorParams(&params, node, ctx, &rowNum));
672
  if (output->columnData == NULL) {
H
Haojun Liao 已提交
673 674 675 676
    code = sclCreateColumnInfoData(&node->node.resType, rowNum, output);
    if (code != TSDB_CODE_SUCCESS) {
      SCL_ERR_JRET(code);
    }
D
dapan1121 已提交
677 678 679 680
  }

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(node->opType);

D
dapan1121 已提交
681
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
682 683
  SScalarParam* pLeft = &params[0];
  SScalarParam* pRight = paramNum > 1 ? &params[1] : NULL;
684

wmmhello's avatar
wmmhello 已提交
685
  terrno = TSDB_CODE_SUCCESS;
D
dapan 已提交
686
  OperatorFn(pLeft, pRight, output, TSDB_ORDER_ASC);
wmmhello's avatar
wmmhello 已提交
687
  code = terrno;
D
dapan1121 已提交
688 689

_return:
D
dapan1121 已提交
690

D
dapan1121 已提交
691
  sclFreeParamList(params, paramNum);
D
dapan1121 已提交
692
  SCL_RET(code);
D
dapan1121 已提交
693 694
}

D
dapan1121 已提交
695
EDealRes sclRewriteNullInOptr(SNode** pNode, SScalarCtx *ctx, EOperatorType opType) {
D
dapan1121 已提交
696 697 698
  if (opType <= OP_TYPE_CALC_MAX) {
    SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
    if (NULL == res) {
G
Ganlin Zhao 已提交
699
      sclError("make value node failed");
D
dapan1121 已提交
700 701 702
      ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return DEAL_RES_ERROR;
    }
G
Ganlin Zhao 已提交
703

D
dapan1121 已提交
704
    res->node.resType.type = TSDB_DATA_TYPE_NULL;
G
Ganlin Zhao 已提交
705

D
dapan1121 已提交
706 707 708 709 710
    nodesDestroyNode(*pNode);
    *pNode = (SNode*)res;
  } else {
    SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
    if (NULL == res) {
G
Ganlin Zhao 已提交
711
      sclError("make value node failed");
D
dapan1121 已提交
712 713 714
      ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return DEAL_RES_ERROR;
    }
G
Ganlin Zhao 已提交
715

D
dapan1121 已提交
716
    res->node.resType.type = TSDB_DATA_TYPE_BOOL;
D
dapan1121 已提交
717
    res->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
D
dapan1121 已提交
718
    res->datum.b = false;
G
Ganlin Zhao 已提交
719

D
dapan1121 已提交
720 721 722 723 724 725 726
    nodesDestroyNode(*pNode);
    *pNode = (SNode*)res;
  }

  return DEAL_RES_CONTINUE;
}

D
dapan1121 已提交
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
EDealRes sclAggFuncWalker(SNode* pNode, void* pContext) {
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
    *(bool*)pContext = fmIsAggFunc(pFunc->funcId);
    if (*(bool*)pContext) {
      return DEAL_RES_END;
    }
  }

  return DEAL_RES_CONTINUE;
}


bool sclContainsAggFuncNode(SNode* pNode) {
  bool aggFunc = false;
  nodesWalkExpr(pNode, sclAggFuncWalker, (void *)&aggFunc);
  return aggFunc;
}
D
dapan1121 已提交
745

D
dapan 已提交
746
EDealRes sclRewriteNonConstOperator(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
747
  SOperatorNode *node = (SOperatorNode *)*pNode;
D
dapan1121 已提交
748
  int32_t code = 0;
D
dapan1121 已提交
749 750 751

  if (node->pLeft && (QUERY_NODE_VALUE == nodeType(node->pLeft))) {
    SValueNode *valueNode = (SValueNode *)node->pLeft;
G
Ganlin Zhao 已提交
752
    if (SCL_IS_NULL_VALUE_NODE(valueNode) && (node->opType != OP_TYPE_IS_NULL && node->opType != OP_TYPE_IS_NOT_NULL)
D
dapan1121 已提交
753 754
        && (!sclContainsAggFuncNode(node->pRight))) {
      return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
755
    }
D
dapan 已提交
756

G
Ganlin Zhao 已提交
757
    if (IS_STR_DATA_TYPE(valueNode->node.resType.type) && node->pRight && nodesIsExprNode(node->pRight)
D
dapan 已提交
758
      && ((SExprNode*)node->pRight)->resType.type == TSDB_DATA_TYPE_TIMESTAMP) {
D
dapan1121 已提交
759 760 761 762 763
      code = sclConvertToTsValueNode(((SExprNode*)node->pRight)->resType.precision, valueNode);
      if (code) {
        ctx->code = code;
        return DEAL_RES_ERROR;
      }
D
dapan 已提交
764
    }
765 766 767 768

    if (SCL_IS_COMPARISON_OPERATOR(node->opType) && SCL_DOWNGRADE_DATETYPE(valueNode->node.resType.type)) {
      sclDowngradeValueType(valueNode);
    }    
D
dapan1121 已提交
769 770 771 772
  }

  if (node->pRight && (QUERY_NODE_VALUE == nodeType(node->pRight))) {
    SValueNode *valueNode = (SValueNode *)node->pRight;
D
dapan1121 已提交
773 774 775
    if (SCL_IS_NULL_VALUE_NODE(valueNode) && (node->opType != OP_TYPE_IS_NULL && node->opType != OP_TYPE_IS_NOT_NULL)
       && (!sclContainsAggFuncNode(node->pLeft))) {
      return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
776
    }
D
dapan 已提交
777

G
Ganlin Zhao 已提交
778
    if (IS_STR_DATA_TYPE(valueNode->node.resType.type) && node->pLeft && nodesIsExprNode(node->pLeft)
D
dapan 已提交
779
      && ((SExprNode*)node->pLeft)->resType.type == TSDB_DATA_TYPE_TIMESTAMP) {
D
dapan1121 已提交
780 781 782 783 784
      code = sclConvertToTsValueNode(((SExprNode*)node->pLeft)->resType.precision, valueNode);
      if (code) {
        ctx->code = code;
        return DEAL_RES_ERROR;
      }
D
dapan 已提交
785
    }
786 787 788 789

    if (SCL_IS_COMPARISON_OPERATOR(node->opType) && SCL_DOWNGRADE_DATETYPE(valueNode->node.resType.type)) {
      sclDowngradeValueType(valueNode);
    }    
D
dapan1121 已提交
790 791 792 793 794 795 796 797 798 799 800
  }

  if (node->pRight && (QUERY_NODE_NODE_LIST == nodeType(node->pRight))) {
    SNodeListNode *listNode = (SNodeListNode *)node->pRight;
    SNode* tnode = NULL;
    WHERE_EACH(tnode, listNode->pNodeList) {
      if (SCL_IS_NULL_VALUE_NODE(tnode)) {
        if (node->opType == OP_TYPE_IN) {
          ERASE_NODE(listNode->pNodeList);
          continue;
        } else { //OP_TYPE_NOT_IN
D
dapan1121 已提交
801
          return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
802 803 804 805 806 807 808
        }
      }

      WHERE_NEXT;
    }

    if (listNode->pNodeList->length <= 0) {
D
dapan1121 已提交
809
      return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
810 811 812 813 814 815
    }
  }

  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
816
EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
817
  SFunctionNode *node = (SFunctionNode *)*pNode;
D
dapan1121 已提交
818
  SNode* tnode = NULL;
819
  if (!fmIsScalarFunc(node->funcId) && (!ctx->dual)) {
G
Ganlin Zhao 已提交
820 821
    return DEAL_RES_CONTINUE;
  }
822

D
dapan1121 已提交
823 824 825 826 827 828
  FOREACH(tnode, node->pParameterList) {
    if (!SCL_IS_CONST_NODE(tnode)) {
      return DEAL_RES_CONTINUE;
    }
  }

D
dapan1121 已提交
829
  SScalarParam output = {0};
830

831
  ctx->code = sclExecFunction(node, ctx, &output);
D
dapan 已提交
832
  if (ctx->code) {
D
dapan1121 已提交
833 834 835
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
836
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
837 838
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
839
    sclFreeParam(&output);
D
dapan 已提交
840
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
841 842 843
    return DEAL_RES_ERROR;
  }

844 845
  res->translate = true;

846 847 848 849
  res->node.resType.type = output.columnData->info.type;
  res->node.resType.bytes = output.columnData->info.bytes;
  res->node.resType.scale = output.columnData->info.scale;
  res->node.resType.precision = output.columnData->info.precision;
850
  if (colDataIsNull_s(output.columnData, 0)) {
851
    res->isNull = true;
D
dapan1121 已提交
852
  } else {
853
    int32_t type = output.columnData->info.type;
wmmhello's avatar
wmmhello 已提交
854 855 856 857 858
    if (type == TSDB_DATA_TYPE_JSON){
      int32_t len = getJsonValueLen(output.columnData->pData);
      res->datum.p = taosMemoryCalloc(len, 1);
      memcpy(res->datum.p, output.columnData->pData, len);
    } else if (IS_VAR_DATA_TYPE(type)) {
859
      //res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1);
860
      res->datum.p = taosMemoryCalloc(varDataTLen(output.columnData->pData) + 1, 1);
861
      res->node.resType.bytes = varDataTLen(output.columnData->pData);
862
      memcpy(res->datum.p, output.columnData->pData, varDataTLen(output.columnData->pData));
863
    } else {
D
dapan1121 已提交
864
      nodesSetValueNodeValue(res, output.columnData->pData);
865
    }
D
dapan1121 已提交
866
  }
867

D
dapan1121 已提交
868 869 870
  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

D
dapan1121 已提交
871
  sclFreeParam(&output);
D
dapan1121 已提交
872 873 874
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
875
EDealRes sclRewriteLogic(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
876 877
  SLogicConditionNode *node = (SLogicConditionNode *)*pNode;

H
Haojun Liao 已提交
878
  SScalarParam output = {0};
D
dapan 已提交
879 880
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
881 882 883
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
884 885 886 887
  if (0 == output.numOfRows) {
    return DEAL_RES_CONTINUE;
  }

D
dapan1121 已提交
888
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
889 890
  if (NULL == res) {
    sclError("make value node failed");
891
    sclFreeParam(&output);
D
dapan 已提交
892
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
893 894 895 896
    return DEAL_RES_ERROR;
  }

  res->node.resType = node->node.resType;
897
  res->translate = true;
D
dapan1121 已提交
898

899 900 901 902
  int32_t type = output.columnData->info.type;
  if (IS_VAR_DATA_TYPE(type)) {
    res->datum.p = output.columnData->pData;
    output.columnData->pData = NULL;
D
dapan1121 已提交
903
  } else {
D
dapan1121 已提交
904
    nodesSetValueNodeValue(res, output.columnData->pData);
D
dapan1121 已提交
905
  }
D
dapan1121 已提交
906 907 908 909

  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

D
dapan1121 已提交
910
  sclFreeParam(&output);
D
dapan1121 已提交
911 912 913
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
914
EDealRes sclRewriteOperator(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
915
  SOperatorNode *node = (SOperatorNode *)*pNode;
D
dapan1121 已提交
916

D
dapan1121 已提交
917
  if ((!SCL_IS_CONST_NODE(node->pLeft)) || (!SCL_IS_CONST_NODE(node->pRight))) {
D
dapan 已提交
918
    return sclRewriteNonConstOperator(pNode, ctx);
D
dapan1121 已提交
919 920
  }

H
Haojun Liao 已提交
921
  SScalarParam output = {0};
D
dapan 已提交
922 923
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
924 925 926
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
927
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
928
  if (NULL == res) {
929 930
    sclError("make value node failed");
    sclFreeParam(&output);
D
dapan 已提交
931
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
932 933 934
    return DEAL_RES_ERROR;
  }

935
  res->translate = true;
D
dapan1121 已提交
936

937
  res->node.resType = node->node.resType;
938
  if (colDataIsNull_s(output.columnData, 0)) {
939 940
    res->isNull = true;
    res->node.resType = node->node.resType;
941
  } else {
942 943 944 945 946
    int32_t type = output.columnData->info.type;
    if (IS_VAR_DATA_TYPE(type)) {  // todo refactor
      res->datum.p = output.columnData->pData;
      output.columnData->pData = NULL;
    } else {
G
Ganlin Zhao 已提交
947
      nodesSetValueNodeValue(res, output.columnData->pData);
948
    }
D
dapan1121 已提交
949
  }
D
dapan1121 已提交
950 951 952 953

  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

H
Haojun Liao 已提交
954
  sclFreeParam(&output);
D
dapan1121 已提交
955 956 957 958
  return DEAL_RES_CONTINUE;
}

EDealRes sclConstantsRewriter(SNode** pNode, void* pContext) {
D
dapan 已提交
959 960
  SScalarCtx *ctx = (SScalarCtx *)pContext;

D
dapan1121 已提交
961
  if (QUERY_NODE_FUNCTION == nodeType(*pNode)) {
D
dapan 已提交
962
    return sclRewriteFunction(pNode, ctx);
D
dapan1121 已提交
963 964 965
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) {
D
dapan 已提交
966
    return sclRewriteLogic(pNode, ctx);
D
dapan1121 已提交
967 968
  }

D
dapan1121 已提交
969
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
D
dapan 已提交
970
    return sclRewriteOperator(pNode, ctx);
971
  }
972 973

  return DEAL_RES_CONTINUE;
D
dapan1121 已提交
974 975
}

D
dapan 已提交
976
EDealRes sclWalkFunction(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
977
  SFunctionNode *node = (SFunctionNode *)pNode;
D
dapan1121 已提交
978
  SScalarParam output = {0};
979

980
  ctx->code = sclExecFunction(node, ctx, &output);
D
dapan1121 已提交
981 982
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
983 984
  }

D
dapan1121 已提交
985
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
986 987
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
D
dapan1121 已提交
988 989
  }

D
dapan1121 已提交
990 991 992
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
993
EDealRes sclWalkLogic(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
994
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
D
dapan1121 已提交
995
  SScalarParam output = {0};
996

D
dapan1121 已提交
997 998 999
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
1000 1001
  }

D
dapan1121 已提交
1002
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
1003
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
1004
    return DEAL_RES_ERROR;
D
dapan1121 已提交
1005 1006 1007 1008 1009
  }

  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
1010
EDealRes sclWalkOperator(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1011
  SOperatorNode *node = (SOperatorNode *)pNode;
D
dapan1121 已提交
1012
  SScalarParam output = {0};
G
Ganlin Zhao 已提交
1013

D
dapan1121 已提交
1014 1015
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
1016 1017
    return DEAL_RES_ERROR;
  }
D
dapan1121 已提交
1018

D
dapan1121 已提交
1019
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
1020
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
1021 1022 1023
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
1024 1025 1026
  return DEAL_RES_CONTINUE;
}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
EDealRes sclWalkTarget(SNode* pNode, SScalarCtx *ctx) {
  STargetNode *target = (STargetNode *)pNode;

  if (target->dataBlockId >= taosArrayGetSize(ctx->pBlockList)) {
    sclError("target tupleId is too big, tupleId:%d, dataBlockNum:%d", target->dataBlockId, (int32_t)taosArrayGetSize(ctx->pBlockList));
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

  int32_t index = -1;
  for(int32_t i = 0; i < taosArrayGetSize(ctx->pBlockList); ++i) {
    SSDataBlock* pb = taosArrayGetP(ctx->pBlockList, i);
    if (pb->info.blockId == target->dataBlockId) {
      index = i;
      break;
    }
  }

  if (index == -1) {
    sclError("column tupleId is too big, tupleId:%d, dataBlockNum:%d", target->dataBlockId, (int32_t)taosArrayGetSize(ctx->pBlockList));
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

  SSDataBlock *block = *(SSDataBlock **)taosArrayGet(ctx->pBlockList, index);

  if (target->slotId >= taosArrayGetSize(block->pDataBlock)) {
    sclError("target slot not exist, dataBlockId:%d, slotId:%d, dataBlockNum:%d", target->dataBlockId, target->slotId, (int32_t)taosArrayGetSize(block->pDataBlock));
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

  // if block->pDataBlock is not enough, there are problems if target->slotId bigger than the size of block->pDataBlock,
  SColumnInfoData *col = taosArrayGet(block->pDataBlock, target->slotId);

  SScalarParam *res = (SScalarParam *)taosHashGet(ctx->pRes, (void *)&target->pExpr, POINTER_BYTES);
  if (NULL == res) {
    sclError("no valid res in hash, node:%p, type:%d", target->pExpr, nodeType(target->pExpr));
    ctx->code = TSDB_CODE_QRY_APP_ERROR;
    return DEAL_RES_ERROR;
  }

1069
  colDataAssign(col, res->columnData, res->numOfRows, NULL);
1070 1071 1072 1073 1074 1075
  block->info.rows = res->numOfRows;

  sclFreeParam(res);
  taosHashRemove(ctx->pRes, (void *)&target->pExpr, POINTER_BYTES);
  return DEAL_RES_CONTINUE;
}
D
dapan 已提交
1076

D
dapan1121 已提交
1077
EDealRes sclCalcWalker(SNode* pNode, void* pContext) {
D
dapan1121 已提交
1078
  if (QUERY_NODE_VALUE == nodeType(pNode) || QUERY_NODE_NODE_LIST == nodeType(pNode) || QUERY_NODE_COLUMN == nodeType(pNode)|| QUERY_NODE_LEFT_VALUE == nodeType(pNode)) {
D
dapan1121 已提交
1079
    return DEAL_RES_CONTINUE;
D
dapan1121 已提交
1080
  }
D
dapan 已提交
1081 1082

  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
1083
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
D
dapan 已提交
1084
    return sclWalkFunction(pNode, ctx);
D
dapan1121 已提交
1085
  }
D
dapan1121 已提交
1086

D
dapan1121 已提交
1087
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
D
dapan 已提交
1088
    return sclWalkLogic(pNode, ctx);
D
dapan1121 已提交
1089
  }
D
dapan1121 已提交
1090

D
dapan1121 已提交
1091
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
D
dapan 已提交
1092
    return sclWalkOperator(pNode, ctx);
D
dapan1121 已提交
1093
  }
D
dapan1121 已提交
1094

1095 1096 1097
  if (QUERY_NODE_TARGET == nodeType(pNode)) {
    return sclWalkTarget(pNode, ctx);
  }
D
dapan1121 已提交
1098

D
dapan 已提交
1099
  sclError("invalid node type for scalar calculating, type:%d", nodeType(pNode));
D
dapan1121 已提交
1100 1101
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  return DEAL_RES_ERROR;
D
dapan1121 已提交
1102 1103
}

D
dapan1121 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
int32_t sclExtendResRows(SScalarParam *pDst, SScalarParam *pSrc, SArray *pBlockList) {
  SSDataBlock* pb = taosArrayGetP(pBlockList, 0);
  SScalarParam *pLeft = taosMemoryCalloc(1, sizeof(SScalarParam));
  if (NULL == pLeft) {
    sclError("calloc %d failed", (int32_t)sizeof(SScalarParam));
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  pLeft->numOfRows = pb->info.rows;
  colInfoDataEnsureCapacity(pDst->columnData, pb->info.rows);

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(OP_TYPE_ASSIGN);
  OperatorFn(pLeft, pSrc, pDst, TSDB_ORDER_ASC);

  taosMemoryFree(pLeft);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1123
int32_t sclCalcConstants(SNode *pNode, bool dual, SNode **pRes) {
D
dapan1121 已提交
1124 1125 1126 1127 1128
  if (NULL == pNode) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan 已提交
1129
  SScalarCtx ctx = {0};
D
dapan1121 已提交
1130
  ctx.dual = dual;
1131
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
D
dapan 已提交
1132
  if (NULL == ctx.pRes) {
1133
    sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM);
D
dapan 已提交
1134 1135
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
G
Ganlin Zhao 已提交
1136

X
Xiaoyu Wang 已提交
1137
  nodesRewriteExprPostOrder(&pNode, sclConstantsRewriter, (void *)&ctx);
D
dapan 已提交
1138
  SCL_ERR_JRET(ctx.code);
D
dapan1121 已提交
1139 1140
  *pRes = pNode;

D
dapan 已提交
1141
_return:
1142

D
dapan 已提交
1143 1144
  sclFreeRes(ctx.pRes);
  return code;
D
dapan1121 已提交
1145
}
D
dapan1121 已提交
1146

D
dapan1121 已提交
1147
static int32_t sclGetMinusOperatorResType(SOperatorNode* pOp) {
1148 1149 1150 1151 1152 1153 1154 1155
  if (!IS_MATHABLE_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }
  pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE;
  pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1156
static int32_t sclGetMathOperatorResType(SOperatorNode* pOp) {
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
  SDataType ldt = ((SExprNode*)(pOp->pLeft))->resType;
  SDataType rdt = ((SExprNode*)(pOp->pRight))->resType;
  if ((TSDB_DATA_TYPE_TIMESTAMP == ldt.type && TSDB_DATA_TYPE_TIMESTAMP == rdt.type) ||
      (TSDB_DATA_TYPE_TIMESTAMP == ldt.type && (IS_VAR_DATA_TYPE(rdt.type) || IS_FLOAT_TYPE(rdt.type))) ||
      (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && (IS_VAR_DATA_TYPE(ldt.type) || IS_FLOAT_TYPE(ldt.type)))) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

  if ((TSDB_DATA_TYPE_TIMESTAMP == ldt.type && IS_INTEGER_TYPE(rdt.type)) ||
      (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && IS_INTEGER_TYPE(ldt.type)) ||
      (TSDB_DATA_TYPE_TIMESTAMP == ldt.type && TSDB_DATA_TYPE_BOOL == rdt.type) ||
      (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && TSDB_DATA_TYPE_BOOL == ldt.type)) {
    pOp->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
    pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
  } else {
    pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE;
    pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
  }
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1178
static int32_t sclGetCompOperatorResType(SOperatorNode* pOp) {
1179
  SDataType ldt = ((SExprNode*)(pOp->pLeft))->resType;
1180 1181 1182
  if (OP_TYPE_IN == pOp->opType || OP_TYPE_NOT_IN == pOp->opType) {
    ((SExprNode*)(pOp->pRight))->resType = ldt;
  } else if (nodesIsRegularOp(pOp)) {
1183
    SDataType rdt = ((SExprNode*)(pOp->pRight))->resType;
1184 1185 1186
    if (!IS_VAR_DATA_TYPE(ldt.type) || QUERY_NODE_VALUE != nodeType(pOp->pRight) ||
        (!IS_STR_DATA_TYPE(rdt.type) && (rdt.type != TSDB_DATA_TYPE_NULL))) {
      return TSDB_CODE_TSC_INVALID_OPERATION;
1187 1188 1189 1190 1191 1192 1193
    }
  }
  pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
  pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1194
static int32_t sclGetJsonOperatorResType(SOperatorNode* pOp) {
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  SDataType ldt = ((SExprNode*)(pOp->pLeft))->resType;
  SDataType rdt = ((SExprNode*)(pOp->pRight))->resType;
  if (TSDB_DATA_TYPE_JSON != ldt.type || !IS_STR_DATA_TYPE(rdt.type)) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }
  if (pOp->opType == OP_TYPE_JSON_GET_VALUE) {
    pOp->node.resType.type = TSDB_DATA_TYPE_JSON;
  } else if (pOp->opType == OP_TYPE_JSON_CONTAINS) {
    pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
  }
  pOp->node.resType.bytes = tDataTypes[pOp->node.resType.type].bytes;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1209
static int32_t sclGetBitwiseOperatorResType(SOperatorNode* pOp) {
1210 1211 1212 1213 1214
  pOp->node.resType.type = TSDB_DATA_TYPE_BIGINT;
  pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) {
  return sclCalcConstants(pNode, false, pRes);
}

int32_t scalarCalculateConstantsFromDual(SNode *pNode, SNode **pRes) {
  return sclCalcConstants(pNode, true, pRes);
}

int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) {
  if (NULL == pNode || NULL == pBlockList) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
  SScalarCtx ctx = {.code = 0, .pBlockList = pBlockList, .param = pDst ? pDst->param : NULL};

  // TODO: OPT performance
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == ctx.pRes) {
    sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM);
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
1238

D
dapan1121 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  nodesWalkExprPostOrder(pNode, sclCalcWalker, (void *)&ctx);
  SCL_ERR_JRET(ctx.code);

  if (pDst) {
    SScalarParam *res = (SScalarParam *)taosHashGet(ctx.pRes, (void *)&pNode, POINTER_BYTES);
    if (NULL == res) {
      sclError("no valid res in hash, node:%p, type:%d", pNode, nodeType(pNode));
      SCL_ERR_JRET(TSDB_CODE_QRY_APP_ERROR);
    }

    if (1 == res->numOfRows) {
      SCL_ERR_JRET(sclExtendResRows(pDst, res, pBlockList));
    } else {
      colInfoDataEnsureCapacity(pDst->columnData, res->numOfRows);
      colDataAssign(pDst->columnData, res->columnData, res->numOfRows, NULL);
      pDst->numOfRows = res->numOfRows;
1255
      pDst->numOfQualified = res->numOfQualified;
D
dapan1121 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    }

    sclFreeParam(res);
    taosHashRemove(ctx.pRes, (void *)&pNode, POINTER_BYTES);
  }

_return:
  sclFreeRes(ctx.pRes);
  return code;
}

1267 1268 1269 1270 1271 1272 1273 1274
int32_t scalarGetOperatorResultType(SOperatorNode* pOp) {
  if (TSDB_DATA_TYPE_BLOB == ((SExprNode*)(pOp->pLeft))->resType.type ||
      (NULL != pOp->pRight && TSDB_DATA_TYPE_BLOB == ((SExprNode*)(pOp->pRight))->resType.type)) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

  switch (pOp->opType) {
    case OP_TYPE_ADD:
D
dapan1121 已提交
1275 1276 1277 1278
    case OP_TYPE_SUB:
    case OP_TYPE_MULTI:
    case OP_TYPE_DIV:
    case OP_TYPE_REM:
D
dapan1121 已提交
1279
      return sclGetMathOperatorResType(pOp);
D
dapan1121 已提交
1280
    case OP_TYPE_MINUS:
D
dapan1121 已提交
1281
      return sclGetMinusOperatorResType(pOp);
1282 1283 1284 1285 1286
    case OP_TYPE_ASSIGN:
      pOp->node.resType = ((SExprNode*)(pOp->pLeft))->resType;
      break;
    case OP_TYPE_BIT_AND:
    case OP_TYPE_BIT_OR:
D
dapan1121 已提交
1287
      return sclGetBitwiseOperatorResType(pOp);
D
dapan1121 已提交
1288 1289 1290 1291 1292 1293
    case OP_TYPE_GREATER_THAN:
    case OP_TYPE_GREATER_EQUAL:
    case OP_TYPE_LOWER_THAN:
    case OP_TYPE_LOWER_EQUAL:
    case OP_TYPE_EQUAL:
    case OP_TYPE_NOT_EQUAL:
1294 1295 1296 1297 1298 1299 1300 1301
    case OP_TYPE_IS_NULL:
    case OP_TYPE_IS_NOT_NULL:
    case OP_TYPE_IS_TRUE:
    case OP_TYPE_IS_FALSE:
    case OP_TYPE_IS_UNKNOWN:
    case OP_TYPE_IS_NOT_TRUE:
    case OP_TYPE_IS_NOT_FALSE:
    case OP_TYPE_IS_NOT_UNKNOWN:
D
dapan1121 已提交
1302 1303 1304 1305
    case OP_TYPE_LIKE:
    case OP_TYPE_NOT_LIKE:
    case OP_TYPE_MATCH:
    case OP_TYPE_NMATCH:
1306 1307
    case OP_TYPE_IN:
    case OP_TYPE_NOT_IN:
D
dapan1121 已提交
1308
      return sclGetCompOperatorResType(pOp);
D
dapan1121 已提交
1309
    case OP_TYPE_JSON_GET_VALUE:
1310
    case OP_TYPE_JSON_CONTAINS:
D
dapan1121 已提交
1311
      return sclGetJsonOperatorResType(pOp);
D
dapan1121 已提交
1312
    default:
1313
      break;
D
dapan1121 已提交
1314 1315
  }

1316 1317
  return TSDB_CODE_SUCCESS;
}