scalar.c 30.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"
D
dapan1121 已提交
12

D
dapan1121 已提交
13
int32_t scalarGetOperatorParamNum(EOperatorType type) {
D
dapan1121 已提交
14
  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 已提交
15 16
   || 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 已提交
17 18 19 20 21 22
    return 1;
  }

  return 2;
}

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

  return TSDB_CODE_SUCCESS;
D
dapan 已提交
36 37 38
}


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

  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);
55 56 57 58 59 60
    return NULL;
  } else {
    return pColumnData;
  }
}

61 62
int32_t doConvertDataType(SValueNode* pValueNode, SScalarParam* out) {
  SScalarParam in = {.numOfRows = 1};
63
  in.columnData = sclCreateColumnInfoData(&pValueNode->node.resType, 1);
64
  colDataAppend(in.columnData, 0, nodesGetValueFromNode(pValueNode), false);
65

66
  colInfoDataEnsureCapacity(out->columnData, 1);
67 68 69 70
  int32_t code = vectorConvertImpl(&in, out);
  sclFreeParam(&in);

  return code;
71 72
}

D
dapan1121 已提交
73 74 75 76 77 78 79 80 81 82 83 84
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);
  }

  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type)); 

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

D
dapan1121 已提交
87 88 89 90 91
  int32_t len = 0;
  void *buf = NULL;
  
  for (int32_t i = 0; i < nodeList->pNodeList->length; ++i) {
    SValueNode *valueNode = (SValueNode *)cell->pNode;
D
dapan1121 已提交
92
    
D
dapan1121 已提交
93
    if (valueNode->node.resType.type != type) {
94
      out.columnData->info.type = type;
D
dapan1121 已提交
95 96 97 98 99 100 101 102 103
      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;
      }
104

105 106
      code = doConvertDataType(valueNode, &out);
      if (code != TSDB_CODE_SUCCESS) {
107
//        sclError("convert data from %d to %d failed", in.type, out.type);
D
dapan1121 已提交
108 109 110 111
        SCL_ERR_JRET(code);
      }

      if (IS_VAR_DATA_TYPE(type)) {
D
dapan1121 已提交
112 113 114
        char* data = colDataGetVarData(out.columnData, 0);
        len = varDataLen(data);
        buf = varDataVal(data);
D
dapan1121 已提交
115 116
      } else {
        len = tDataTypes[type].bytes;
117
        buf = out.columnData->pData;
D
dapan1121 已提交
118 119 120
      }
    } else {
      buf = nodesGetValueFromNode(valueNode);
D
dapan1121 已提交
121 122 123 124 125
      if (IS_VAR_DATA_TYPE(type)) {
        len = varDataLen(buf);
        buf = varDataVal(buf);
      } else {
        len = valueNode->node.resType.bytes;
126
      }
D
dapan1121 已提交
127 128
    }
    
129
    if (taosHashPut(pObj, buf, (size_t)len, NULL, 0)) {
D
dapan1121 已提交
130
      sclError("taosHashPut to set failed");
D
dapan1121 已提交
131 132
      SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
D
dapan1121 已提交
133 134

    cell = cell->pNext;
D
dapan1121 已提交
135 136 137 138 139 140 141 142 143 144
  }

  *data = pObj;
  return TSDB_CODE_SUCCESS;

_return:
  taosHashCleanup(pObj);
  SCL_RET(code);
}

D
dapan1121 已提交
145 146 147 148 149 150 151
void sclFreeRes(SHashObj *res) {
  SScalarParam *p = NULL;
  void *pIter = taosHashIterate(res, NULL);
  while (pIter) {
    p = (SScalarParam *)pIter;

    if (p) {
D
dapan 已提交
152
      sclFreeParam(p);
D
dapan1121 已提交
153 154 155 156 157 158
    }
    pIter = taosHashIterate(res, pIter);
  }
  taosHashCleanup(res);
}

D
dapan1121 已提交
159
void sclFreeParam(SScalarParam *param) {
160 161
  if (param->columnData != NULL) {
    colDataDestroy(param->columnData);
wmmhello's avatar
wmmhello 已提交
162
    taosMemoryFree(param->columnData);
163 164 165 166 167
  }

  if (param->pHashFilter != NULL) {
    taosHashCleanup(param->pHashFilter);
  }
D
dapan1121 已提交
168 169
}

D
dapan1121 已提交
170 171 172 173 174
int32_t sclCopyValueNodeValue(SValueNode *pNode, void **res) {
  if (TSDB_DATA_TYPE_NULL == pNode->node.resType.type) {
    return TSDB_CODE_SUCCESS;
  }
  
wafwerar's avatar
wafwerar 已提交
175
  *res = taosMemoryMalloc(pNode->node.resType.bytes);
D
dapan1121 已提交
176 177 178 179 180 181 182 183 184
  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 已提交
185 186
int32_t sclInitParam(SNode* node, SScalarParam *param, SScalarCtx *ctx, int32_t *rowNum) {
  switch (nodeType(node)) {
D
dapan1121 已提交
187 188 189 190 191
    case QUERY_NODE_LEFT_VALUE: {
      SSDataBlock* pb = taosArrayGetP(ctx->pBlockList, 0);
      param->numOfRows = pb->info.rows;
      break;
    }
D
dapan1121 已提交
192 193
    case QUERY_NODE_VALUE: {
      SValueNode *valueNode = (SValueNode *)node;
194 195

      param->numOfRows = 1;
196
      param->columnData = sclCreateColumnInfoData(&valueNode->node.resType, 1);
197
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type) {
198
        colDataAppendNULL(param->columnData, 0);
199 200
      } else {
        colDataAppend(param->columnData, 0, nodesGetValueFromNode(valueNode), false);
D
dapan1121 已提交
201
      }
D
dapan1121 已提交
202 203
      break;
    }
D
dapan1121 已提交
204 205
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)node;
206 207
      if (LIST_LENGTH(nodeList->pNodeList) <= 0) {
        sclError("invalid length in nodeList, length:%d", LIST_LENGTH(nodeList->pNodeList));
D
dapan1121 已提交
208 209 210
        SCL_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan1121 已提交
211 212 213 214 215 216 217
      int32_t type = vectorGetConvertType(ctx->type.selfType, ctx->type.peerType);
      if (type == 0) {
        type = nodeList->dataType.type;
      }
      
      SCL_ERR_RET(scalarGenerateSetFromList((void **)&param->pHashFilter, node, type));
      param->hashValueType = type;
D
dapan 已提交
218
      if (taosHashPut(ctx->pRes, &node, POINTER_BYTES, param, sizeof(*param))) {
219
        taosHashCleanup(param->pHashFilter);
D
dapan 已提交
220 221 222
        sclError("taosHashPut nodeList failed, size:%d", (int32_t)sizeof(*param));
        return TSDB_CODE_QRY_OUT_OF_MEMORY;
      }   
D
dapan1121 已提交
223 224
      break;
    }
X
Xiaoyu Wang 已提交
225
    case QUERY_NODE_COLUMN: {
D
dapan 已提交
226 227
      if (NULL == ctx->pBlockList) {
        sclError("invalid node type for constant calculating, type:%d, src:%p", nodeType(node), ctx->pBlockList);
D
dapan1121 已提交
228 229 230
        SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
      
X
Xiaoyu Wang 已提交
231
      SColumnNode *ref = (SColumnNode *)node;
232 233 234 235 236 237 238 239 240 241 242

      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 已提交
243
        sclError("column tupleId is too big, tupleId:%d, dataBlockNum:%d", ref->dataBlockId, (int32_t)taosArrayGetSize(ctx->pBlockList));
D
dapan 已提交
244 245 246
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

247
      SSDataBlock *block = *(SSDataBlock **)taosArrayGet(ctx->pBlockList, index);
248
      if (NULL == block || ref->slotId >= taosArrayGetSize(block->pDataBlock)) {
D
dapan 已提交
249
        sclError("column slotId is too big, slodId:%d, dataBlockSize:%d", ref->slotId, (int32_t)taosArrayGetSize(block->pDataBlock));
D
dapan1121 已提交
250 251 252
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan 已提交
253
      SColumnInfoData *columnData = (SColumnInfoData *)taosArrayGet(block->pDataBlock, ref->slotId);
254 255
      param->numOfRows = block->info.rows;
      param->columnData = columnData;
D
dapan1121 已提交
256 257
      break;
    }
258 259 260
    case QUERY_NODE_FUNCTION:
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION: {
D
dapan1121 已提交
261 262 263 264 265 266 267 268
      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;
      break;
    }
269 270
    default:
      break;
D
dapan1121 已提交
271 272
  }

273 274 275
  if (param->numOfRows > *rowNum) {
    if ((1 != param->numOfRows) && (1 < *rowNum)) {
      sclError("different row nums, rowNum:%d, newRowNum:%d", *rowNum, param->numOfRows);
D
dapan1121 已提交
276 277 278
      SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
    
279
    *rowNum = param->numOfRows;
D
dapan1121 已提交
280 281
  }

282
  param->param = ctx->param;
D
dapan1121 已提交
283 284 285
  return TSDB_CODE_SUCCESS;
}

D
dapan 已提交
286
int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *paramNum, int32_t *rowNum) {  
D
dapan1121 已提交
287
  int32_t code = 0;
D
dapan1121 已提交
288 289
  if (NULL == pParamList) {
    if (ctx->pBlockList) {
D
dapan1121 已提交
290
      SSDataBlock *pBlock = taosArrayGetP(ctx->pBlockList, 0);
D
dapan1121 已提交
291 292 293 294 295
      *rowNum = pBlock->info.rows;
    } else {
      *rowNum = 1;
    }

D
dapan 已提交
296
    *paramNum = 1;
D
dapan1121 已提交
297
  } else {
D
dapan 已提交
298
    *paramNum = pParamList->length;
D
dapan1121 已提交
299 300
  }

D
dapan 已提交
301
  SScalarParam *paramList = taosMemoryCalloc(*paramNum, sizeof(SScalarParam));
D
dapan1121 已提交
302
  if (NULL == paramList) {
D
dapan 已提交
303
    sclError("calloc %d failed", (int32_t)((*paramNum) * sizeof(SScalarParam)));
D
dapan1121 已提交
304
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
305 306
  }

D
dapan1121 已提交
307 308 309 310 311 312
  if (pParamList) {
    SNode *tnode = NULL;
    int32_t i = 0;
    if (SCL_IS_CONST_CALC(ctx)) {
      WHERE_EACH (tnode, pParamList) { 
        if (!SCL_IS_CONST_NODE(tnode)) {
D
dapan 已提交
313
          WHERE_NEXT;
D
dapan1121 已提交
314 315 316 317 318 319 320 321 322 323 324 325
        } else {
          SCL_ERR_JRET(sclInitParam(tnode, &paramList[i], ctx, rowNum));
          ERASE_NODE(pParamList);
        }
        
        ++i;
      }
    } else {
      FOREACH(tnode, pParamList) { 
        SCL_ERR_JRET(sclInitParam(tnode, &paramList[i], ctx, rowNum));
        ++i;
      }
D
dapan1121 已提交
326
    }
D
dapan1121 已提交
327 328 329
  } else {
    paramList[0].numOfRows = *rowNum;
  }
D
dapan1121 已提交
330

D
dapan1121 已提交
331 332
  if (0 == *rowNum) {
    taosMemoryFreeClear(paramList);    
D
dapan1121 已提交
333
  }
D
dapan1121 已提交
334

D
dapan1121 已提交
335
  *pParams = paramList;
D
dapan1121 已提交
336
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
337

D
dapan1121 已提交
338
_return:
wafwerar's avatar
wafwerar 已提交
339
  taosMemoryFreeClear(paramList);
D
dapan1121 已提交
340 341 342
  SCL_RET(code);
}

D
dapan1121 已提交
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 372 373 374 375 376 377 378 379 380 381 382
int32_t sclGetNodeType(SNode *pNode, SScalarCtx *ctx) {
  if (NULL == pNode) {
    return -1;
  }
  
  switch (nodeType(pNode)) {
    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 已提交
383 384
int32_t sclInitOperatorParams(SScalarParam **pParams, SOperatorNode *node, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
385
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
386 387 388 389 390
  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);
  }
  
wafwerar's avatar
wafwerar 已提交
391
  SScalarParam *paramList = taosMemoryCalloc(paramNum, sizeof(SScalarParam));
D
dapan1121 已提交
392 393
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam)));
D
dapan1121 已提交
394 395 396
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
397 398
  sclSetOperatorValueType(node, ctx);

D
dapan1121 已提交
399
  SCL_ERR_JRET(sclInitParam(node->pLeft, &paramList[0], ctx, rowNum));
D
dapan1121 已提交
400
  if (paramNum > 1) {
D
dapan1121 已提交
401
    TSWAP(ctx->type.selfType, ctx->type.peerType);
D
dapan1121 已提交
402
    SCL_ERR_JRET(sclInitParam(node->pRight, &paramList[1], ctx, rowNum));
D
dapan1121 已提交
403 404
  }

D
dapan1121 已提交
405
  *pParams = paramList;
D
dapan1121 已提交
406
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
407 408

_return:
wafwerar's avatar
wafwerar 已提交
409
  taosMemoryFreeClear(paramList);
D
dapan1121 已提交
410
  SCL_RET(code);
D
dapan1121 已提交
411 412
}

413
int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) {
D
dapan1121 已提交
414 415
  SScalarParam *params = NULL;
  int32_t rowNum = 0;
D
dapan 已提交
416
  int32_t paramNum = 0;
D
dapan1121 已提交
417
  int32_t code = 0;
D
dapan 已提交
418
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &paramNum, &rowNum));
D
dapan1121 已提交
419

D
dapan1121 已提交
420
  if (fmIsUserDefinedFunc(node->funcId)) {
421
    code = callUdfScalarFunc(node->functionName, params, paramNum, output);
422 423 424 425
    if (code != 0) {
      sclError("fmExecFunction error. callUdfScalarFunc. function name: %s, udf code:%d", node->functionName, code);
      goto _return;
    }
D
dapan1121 已提交
426 427 428 429 430 431 432 433
  } 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);
    }
  
434
    output->columnData = sclCreateColumnInfoData(&node->node.resType, rowNum);
D
dapan1121 已提交
435 436 437 438 439 440 441 442 443 444
    if (output->columnData == NULL) {
      sclError("calloc %d failed", (int32_t)(rowNum * output->columnData->info.bytes));
      SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }

    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 已提交
445 446 447 448
  }

_return:

D
dapan 已提交
449
  for (int32_t i = 0; i < paramNum; ++i) {
H
Haojun Liao 已提交
450
//    sclFreeParamNoData(params + i);
D
dapan1121 已提交
451 452
  }

wafwerar's avatar
wafwerar 已提交
453
  taosMemoryFreeClear(params);
D
dapan1121 已提交
454 455 456 457 458 459 460 461 462 463 464 465
  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 已提交
466 467
  }

D
dapan1121 已提交
468 469 470 471 472 473 474
  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 已提交
475
  int32_t paramNum = 0;
D
dapan1121 已提交
476
  int32_t code = 0;
D
dapan 已提交
477
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &paramNum, &rowNum));
D
dapan1121 已提交
478 479 480 481
  if (NULL == params) {
    output->numOfRows = 0;
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
482

483 484 485 486
  int32_t type = node->node.resType.type;
  output->numOfRows = rowNum;

  SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
487
  output->columnData = sclCreateColumnInfoData(&t, rowNum);
488
  if (output->columnData == NULL) {
D
dapan1121 已提交
489
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(bool)));
D
dapan1121 已提交
490 491
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
492

D
dapan1121 已提交
493
  bool value = false;
D
dapan 已提交
494
  bool complete = true;
D
dapan1121 已提交
495
  for (int32_t i = 0; i < rowNum; ++i) {
D
dapan 已提交
496 497
    complete = true;
    for (int32_t m = 0; m < paramNum; ++m) {
D
dapan1121 已提交
498
      if (NULL == params[m].columnData) {
D
dapan 已提交
499
        complete = false;
D
dapan1121 已提交
500 501
        continue;
      }
502 503 504
      char* p = colDataGetData(params[m].columnData, i);
      GET_TYPED_DATA(value, bool, params[m].columnData->info.type, p);

D
dapan1121 已提交
505
      if (LOGIC_COND_TYPE_AND == node->condType && (false == value)) {
D
dapan1121 已提交
506
        complete = true;
D
dapan1121 已提交
507 508
        break;
      } else if (LOGIC_COND_TYPE_OR == node->condType && value) {
D
dapan1121 已提交
509
        complete = true;
D
dapan1121 已提交
510 511 512 513 514 515
        break;
      } else if (LOGIC_COND_TYPE_NOT == node->condType) {
        value = !value;
      }
    }

D
dapan 已提交
516 517 518
    if (complete) {
      colDataAppend(output->columnData, i, (char*) &value, false);
    }
D
dapan1121 已提交
519 520
  }

D
dapan1121 已提交
521 522 523 524 525
  if (SCL_IS_CONST_CALC(ctx) && (false == complete)) {
    sclFreeParam(output);
    output->numOfRows = 0;
  }

D
dapan1121 已提交
526
_return:
D
dapan1121 已提交
527

D
dapan 已提交
528
  for (int32_t i = 0; i < paramNum; ++i) {
H
Haojun Liao 已提交
529
//    sclFreeParamNoData(params + i);
D
dapan1121 已提交
530 531
  }

wafwerar's avatar
wafwerar 已提交
532
  taosMemoryFreeClear(params);
D
dapan1121 已提交
533
  SCL_RET(code);
D
dapan1121 已提交
534 535 536 537 538 539
}

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

D
dapan1121 已提交
541
  SCL_ERR_RET(sclInitOperatorParams(&params, node, ctx, &rowNum));
542
  output->columnData = sclCreateColumnInfoData(&node->node.resType, rowNum);
543 544
  if (output->columnData == NULL) {
    sclError("calloc failed, size:%d", (int32_t)rowNum * node->node.resType.bytes);
D
dapan1121 已提交
545 546 547 548 549
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(node->opType);

D
dapan1121 已提交
550
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
551 552
  SScalarParam* pLeft = &params[0];
  SScalarParam* pRight = paramNum > 1 ? &params[1] : NULL;
553

wmmhello's avatar
wmmhello 已提交
554
  terrno = TSDB_CODE_SUCCESS;
D
dapan 已提交
555
  OperatorFn(pLeft, pRight, output, TSDB_ORDER_ASC);
wmmhello's avatar
wmmhello 已提交
556
  code = terrno;
D
dapan1121 已提交
557 558

_return:
D
dapan1121 已提交
559
  for (int32_t i = 0; i < paramNum; ++i) {
560
//    sclFreeParam(&params[i]);
D
dapan1121 已提交
561 562
  }

wafwerar's avatar
wafwerar 已提交
563
  taosMemoryFreeClear(params);
D
dapan1121 已提交
564
  SCL_RET(code);
D
dapan1121 已提交
565 566
}

D
dapan1121 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
EDealRes sclRewriteBasedOnOptr(SNode** pNode, SScalarCtx *ctx, EOperatorType opType) {
  if (opType <= OP_TYPE_CALC_MAX) {
    SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
    if (NULL == res) {
      sclError("make value node failed");    
      ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return DEAL_RES_ERROR;
    }
    
    res->node.resType.type = TSDB_DATA_TYPE_NULL;
    
    nodesDestroyNode(*pNode);
    *pNode = (SNode*)res;
  } else {
    SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
    if (NULL == res) {
      sclError("make value node failed");    
      ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
      return DEAL_RES_ERROR;
    }
    
    res->node.resType.type = TSDB_DATA_TYPE_BOOL;
D
dapan1121 已提交
589
    res->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
D
dapan1121 已提交
590 591 592 593 594 595 596 597 598 599
    res->datum.b = false;
    
    nodesDestroyNode(*pNode);
    *pNode = (SNode*)res;
  }

  return DEAL_RES_CONTINUE;
}


D
dapan 已提交
600
EDealRes sclRewriteNonConstOperator(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
601
  SOperatorNode *node = (SOperatorNode *)*pNode;
D
dapan1121 已提交
602
  int32_t code = 0;
D
dapan1121 已提交
603 604 605

  if (node->pLeft && (QUERY_NODE_VALUE == nodeType(node->pLeft))) {
    SValueNode *valueNode = (SValueNode *)node->pLeft;
D
dapan1121 已提交
606
    if (SCL_IS_NULL_VALUE_NODE(valueNode) && (node->opType != OP_TYPE_IS_NULL && node->opType != OP_TYPE_IS_NOT_NULL)) {
D
dapan1121 已提交
607 608
      return sclRewriteBasedOnOptr(pNode, ctx, node->opType);
    }
D
dapan 已提交
609 610 611

    if (IS_STR_DATA_TYPE(valueNode->node.resType.type) && node->pRight && nodesIsExprNode(node->pRight) 
      && ((SExprNode*)node->pRight)->resType.type == TSDB_DATA_TYPE_TIMESTAMP) {
D
dapan1121 已提交
612 613 614 615 616
      code = sclConvertToTsValueNode(((SExprNode*)node->pRight)->resType.precision, valueNode);
      if (code) {
        ctx->code = code;
        return DEAL_RES_ERROR;
      }
D
dapan 已提交
617
    }
D
dapan1121 已提交
618 619 620 621
  }

  if (node->pRight && (QUERY_NODE_VALUE == nodeType(node->pRight))) {
    SValueNode *valueNode = (SValueNode *)node->pRight;
D
dapan1121 已提交
622
    if (SCL_IS_NULL_VALUE_NODE(valueNode) && (node->opType != OP_TYPE_IS_NULL && node->opType != OP_TYPE_IS_NOT_NULL)) {
D
dapan1121 已提交
623 624
      return sclRewriteBasedOnOptr(pNode, ctx, node->opType);
    }
D
dapan 已提交
625 626 627

    if (IS_STR_DATA_TYPE(valueNode->node.resType.type) && node->pLeft && nodesIsExprNode(node->pLeft) 
      && ((SExprNode*)node->pLeft)->resType.type == TSDB_DATA_TYPE_TIMESTAMP) {
D
dapan1121 已提交
628 629 630 631 632
      code = sclConvertToTsValueNode(((SExprNode*)node->pLeft)->resType.precision, valueNode);
      if (code) {
        ctx->code = code;
        return DEAL_RES_ERROR;
      }
D
dapan 已提交
633
    }
D
dapan1121 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  }

  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
          return sclRewriteBasedOnOptr(pNode, ctx, node->opType);
        }
      }

      WHERE_NEXT;
    }

    if (listNode->pNodeList->length <= 0) {
      return sclRewriteBasedOnOptr(pNode, ctx, node->opType);
    }
  }

  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
660
EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
661
  SFunctionNode *node = (SFunctionNode *)*pNode;
D
dapan1121 已提交
662
  SNode* tnode = NULL;
663
  if (!fmIsScalarFunc(node->funcId)) {
D
dapan1121 已提交
664 665
    return DEAL_RES_CONTINUE;
  }
666

D
dapan1121 已提交
667 668 669 670 671 672
  FOREACH(tnode, node->pParameterList) {
    if (!SCL_IS_CONST_NODE(tnode)) {
      return DEAL_RES_CONTINUE;
    }
  }

D
dapan1121 已提交
673
  SScalarParam output = {0};
674

675
  ctx->code = sclExecFunction(node, ctx, &output);
D
dapan 已提交
676
  if (ctx->code) {
D
dapan1121 已提交
677 678 679
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
680
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
681 682
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
683
    sclFreeParam(&output);
D
dapan 已提交
684
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
685 686 687
    return DEAL_RES_ERROR;
  }

688 689
  res->translate = true;

690 691
  if (colDataIsNull_s(output.columnData, 0)) {
    res->node.resType.type = TSDB_DATA_TYPE_NULL;
D
dapan1121 已提交
692
  } else {
D
dapan1121 已提交
693 694 695 696
    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;
697
    int32_t type = output.columnData->info.type;
wmmhello's avatar
wmmhello 已提交
698 699 700 701 702
    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)) {
703
      res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1);
704
      memcpy(res->datum.p, output.columnData->pData, varDataTLen(output.columnData->pData));
705
    } else {
D
dapan1121 已提交
706
      nodesSetValueNodeValue(res, output.columnData->pData);
707
    }
D
dapan1121 已提交
708
  }
709

D
dapan1121 已提交
710 711 712
  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

D
dapan1121 已提交
713
  sclFreeParam(&output);
D
dapan1121 已提交
714 715 716
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
717
EDealRes sclRewriteLogic(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
718 719
  SLogicConditionNode *node = (SLogicConditionNode *)*pNode;

H
Haojun Liao 已提交
720
  SScalarParam output = {0};
D
dapan 已提交
721 722
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
723 724 725
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
726 727 728 729
  if (0 == output.numOfRows) {
    return DEAL_RES_CONTINUE;
  }

D
dapan1121 已提交
730
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
731 732
  if (NULL == res) {
    sclError("make value node failed");
733
    sclFreeParam(&output);
D
dapan 已提交
734
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
735 736 737 738
    return DEAL_RES_ERROR;
  }

  res->node.resType = node->node.resType;
739
  res->translate = true;
D
dapan1121 已提交
740

741 742 743 744
  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 已提交
745
  } else {
D
dapan1121 已提交
746
    nodesSetValueNodeValue(res, output.columnData->pData);
D
dapan1121 已提交
747
  }
D
dapan1121 已提交
748 749 750 751

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

D
dapan1121 已提交
752
  sclFreeParam(&output);
D
dapan1121 已提交
753 754 755
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
756
EDealRes sclRewriteOperator(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
757
  SOperatorNode *node = (SOperatorNode *)*pNode;
D
dapan1121 已提交
758

D
dapan1121 已提交
759
  if ((!SCL_IS_CONST_NODE(node->pLeft)) || (!SCL_IS_CONST_NODE(node->pRight))) {
D
dapan 已提交
760
    return sclRewriteNonConstOperator(pNode, ctx);
D
dapan1121 已提交
761 762
  }

H
Haojun Liao 已提交
763
  SScalarParam output = {.columnData = taosMemoryCalloc(1, sizeof(SColumnInfoData))};
D
dapan 已提交
764 765
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
766 767 768
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
769
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
770
  if (NULL == res) {
771 772
    sclError("make value node failed");
    sclFreeParam(&output);
D
dapan 已提交
773
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
774 775 776
    return DEAL_RES_ERROR;
  }

777
  res->translate = true;
D
dapan1121 已提交
778

779 780
  if (colDataIsNull_s(output.columnData, 0)) {
    res->node.resType.type = TSDB_DATA_TYPE_NULL;
D
dapan1121 已提交
781
  } else {
782 783 784 785 786 787
    res->node.resType = node->node.resType;
    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 {
D
dapan1121 已提交
788
      nodesSetValueNodeValue(res, output.columnData->pData);    
789
    }
D
dapan1121 已提交
790
  }
D
dapan1121 已提交
791 792 793 794

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

H
Haojun Liao 已提交
795
  sclFreeParam(&output);
D
dapan1121 已提交
796 797 798 799
  return DEAL_RES_CONTINUE;
}

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

D
dapan1121 已提交
802
  if (QUERY_NODE_FUNCTION == nodeType(*pNode)) {
D
dapan 已提交
803
    return sclRewriteFunction(pNode, ctx);
D
dapan1121 已提交
804 805 806
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) {
D
dapan 已提交
807
    return sclRewriteLogic(pNode, ctx);
D
dapan1121 已提交
808 809
  }

D
dapan1121 已提交
810
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
D
dapan 已提交
811
    return sclRewriteOperator(pNode, ctx);
812
  }
813 814

  return DEAL_RES_CONTINUE;
D
dapan1121 已提交
815 816
}

D
dapan 已提交
817
EDealRes sclWalkFunction(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
818
  SFunctionNode *node = (SFunctionNode *)pNode;
D
dapan1121 已提交
819
  SScalarParam output = {0};
820

821
  ctx->code = sclExecFunction(node, ctx, &output);
D
dapan1121 已提交
822 823
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
824 825
  }

D
dapan1121 已提交
826
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
827 828
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
D
dapan1121 已提交
829 830
  }

D
dapan1121 已提交
831 832 833
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
834
EDealRes sclWalkLogic(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
835
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
D
dapan1121 已提交
836
  SScalarParam output = {0};
837

D
dapan1121 已提交
838 839 840
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
841 842
  }

D
dapan1121 已提交
843
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
844
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
845
    return DEAL_RES_ERROR;
D
dapan1121 已提交
846 847 848 849 850
  }

  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
851
EDealRes sclWalkOperator(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
852
  SOperatorNode *node = (SOperatorNode *)pNode;
D
dapan1121 已提交
853
  SScalarParam output = {0};
D
dapan1121 已提交
854
  
D
dapan1121 已提交
855 856
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
857 858
    return DEAL_RES_ERROR;
  }
D
dapan1121 已提交
859

D
dapan1121 已提交
860
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
861
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
862 863 864
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
865 866 867
  return DEAL_RES_CONTINUE;
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
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;
  }

910
  colDataAssign(col, res->columnData, res->numOfRows, NULL);
911 912 913 914 915 916
  block->info.rows = res->numOfRows;

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

D
dapan1121 已提交
918
EDealRes sclCalcWalker(SNode* pNode, void* pContext) {
D
dapan1121 已提交
919
  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 已提交
920
    return DEAL_RES_CONTINUE;
D
dapan1121 已提交
921
  }
D
dapan 已提交
922 923

  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
924
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
D
dapan 已提交
925
    return sclWalkFunction(pNode, ctx);
D
dapan1121 已提交
926
  }
D
dapan1121 已提交
927

D
dapan1121 已提交
928
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
D
dapan 已提交
929
    return sclWalkLogic(pNode, ctx);
D
dapan1121 已提交
930
  }
D
dapan1121 已提交
931

D
dapan1121 已提交
932
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
D
dapan 已提交
933
    return sclWalkOperator(pNode, ctx);
D
dapan1121 已提交
934
  }
D
dapan1121 已提交
935

936 937 938
  if (QUERY_NODE_TARGET == nodeType(pNode)) {
    return sclWalkTarget(pNode, ctx);
  }
D
dapan1121 已提交
939

D
dapan 已提交
940
  sclError("invalid node type for scalar calculating, type:%d", nodeType(pNode));
D
dapan1121 已提交
941 942
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  return DEAL_RES_ERROR;
D
dapan1121 已提交
943 944
}

D
dapan1121 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
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 已提交
964 965 966 967 968 969
int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) {
  if (NULL == pNode) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan 已提交
970
  SScalarCtx ctx = {0};
971
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
D
dapan 已提交
972
  if (NULL == ctx.pRes) {
973
    sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM);
D
dapan 已提交
974 975
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
976
  
X
Xiaoyu Wang 已提交
977
  nodesRewriteExprPostOrder(&pNode, sclConstantsRewriter, (void *)&ctx);
D
dapan 已提交
978
  SCL_ERR_JRET(ctx.code);
D
dapan1121 已提交
979 980
  *pRes = pNode;

D
dapan 已提交
981 982 983
_return:
  sclFreeRes(ctx.pRes);
  return code;
D
dapan1121 已提交
984 985
}

D
dapan 已提交
986
int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) {
D
dapan1121 已提交
987
  if (NULL == pNode || NULL == pBlockList) {
D
dapan1121 已提交
988 989 990 991
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
wmmhello's avatar
wmmhello 已提交
992
  SScalarCtx ctx = {.code = 0, .pBlockList = pBlockList, .param = pDst ? pDst->param : NULL};
993

994
  // TODO: OPT performance
995
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
D
dapan1121 已提交
996
  if (NULL == ctx.pRes) {
997
    sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM);
D
dapan1121 已提交
998 999
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
1000
  
X
Xiaoyu Wang 已提交
1001
  nodesWalkExprPostOrder(pNode, sclCalcWalker, (void *)&ctx);
D
dapan 已提交
1002
  SCL_ERR_JRET(ctx.code);
D
dapan1121 已提交
1003

D
dapan1121 已提交
1004 1005 1006 1007 1008 1009
  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);
    }
1010

D
dapan1121 已提交
1011 1012 1013 1014 1015 1016 1017 1018
    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;
    }
    
D
dapan1121 已提交
1019
    taosHashRemove(ctx.pRes, (void *)&pNode, POINTER_BYTES);
D
dapan1121 已提交
1020
  }
D
dapan1121 已提交
1021

D
dapan 已提交
1022
_return:
D
dapan1121 已提交
1023
  //nodesDestroyNode(pNode);
D
dapan 已提交
1024 1025
  sclFreeRes(ctx.pRes);
  return code;
D
dapan1121 已提交
1026
}