scalar.c 18.8 KB
Newer Older
D
dapan1121 已提交
1
#include "nodes.h"
D
dapan1121 已提交
2 3 4 5 6 7
#include "common.h"
#include "querynodes.h"
#include "function.h"
#include "functionMgt.h"
#include "sclvector.h"
#include "sclInt.h"
D
dapan1121 已提交
8

D
dapan1121 已提交
9
int32_t scalarGetOperatorParamNum(EOperatorType type) {
D
dapan1121 已提交
10 11
  if (OP_TYPE_IS_NULL == type || OP_TYPE_IS_NOT_NULL == type || OP_TYPE_IS_TRUE == type || OP_TYPE_IS_NOT_TRUE == type 
   || OP_TYPE_IS_FALSE == type || OP_TYPE_IS_NOT_FALSE == type || OP_TYPE_IS_UNKNOWN == type || OP_TYPE_IS_NOT_UNKNOWN == type) {
D
dapan1121 已提交
12 13 14 15 16 17
    return 1;
  }

  return 2;
}

D
dapan1121 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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;
  SScalarParam in = {.num = 1}, out = {.num = 1, .type = type};
  int8_t dummy = 0;
  int32_t bufLen = 60;
  out.data = malloc(bufLen);
  int32_t len = 0;
  void *buf = NULL;
  
  for (int32_t i = 0; i < nodeList->pNodeList->length; ++i) {
    SValueNode *valueNode = (SValueNode *)cell->pNode;

    if (valueNode->node.resType.type != type) {
      in.type = valueNode->node.resType.type;
      in.bytes = valueNode->node.resType.bytes;
      in.data = nodesGetValueFromNode(valueNode);
    
      code = vectorConvertImpl(&in, &out);
      if (code) {
        sclError("convert from %d to %d failed", in.type, out.type);
        SCL_ERR_JRET(code);
      }

      if (IS_VAR_DATA_TYPE(type)) {
        len = varDataLen(out.data);
D
dapan1121 已提交
53
        buf = varDataVal(out.data);
D
dapan1121 已提交
54 55
      } else {
        len = tDataTypes[type].bytes;
D
dapan1121 已提交
56
        buf = out.data;
D
dapan1121 已提交
57 58 59
      }
    } else {
      buf = nodesGetValueFromNode(valueNode);
D
dapan1121 已提交
60 61 62 63 64 65 66
      if (IS_VAR_DATA_TYPE(type)) {
        len = varDataLen(buf);
        buf = varDataVal(buf);
      } else {
        len = valueNode->node.resType.bytes;
        buf = out.data;
      }      
D
dapan1121 已提交
67 68 69 70 71 72
    }
    
    if (taosHashPut(pObj, buf, (size_t)len, &dummy, sizeof(dummy))) {
      sclError("taosHashPut failed");
      SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
    }
D
dapan1121 已提交
73 74

    cell = cell->pNext;
D
dapan1121 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  }

  tfree(out.data);
  *data = pObj;

  return TSDB_CODE_SUCCESS;

_return:

  tfree(out.data);
  taosHashCleanup(pObj);
  
  SCL_RET(code);
}

D
dapan1121 已提交
90 91 92 93 94 95 96 97 98 99 100 101
bool sclIsNull(SScalarParam* param, int32_t idx) {
  if (param->dataInBlock) {
    return colDataIsNull(param->columnData, 0, idx, NULL);
  }

  return colDataIsNull_f(param->bitmap, idx);
}

void sclSetNull(SScalarParam* param, int32_t idx) {
  colDataSetNull_f(param->bitmap, idx);
}

D
dapan1121 已提交
102

D
dapan1121 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
void sclFreeRes(SHashObj *res) {
  SScalarParam *p = NULL;
  void *pIter = taosHashIterate(res, NULL);
  while (pIter) {
    p = (SScalarParam *)pIter;

    if (p) {
      tfree(p->data);
    }
    
    pIter = taosHashIterate(res, pIter);
  }
  
  taosHashCleanup(res);
}

void sclFreeParam(SScalarParam *param) {
  tfree(param->data);
}

int32_t sclInitParam(SNode* node, SScalarParam *param, SScalarCtx *ctx, int32_t *rowNum) {
  switch (nodeType(node)) {
    case QUERY_NODE_VALUE: {
      SValueNode *valueNode = (SValueNode *)node;
      param->data = nodesGetValueFromNode(valueNode);
      param->num = 1;
      param->type = valueNode->node.resType.type;
      param->bytes = valueNode->node.resType.bytes;
D
dapan1121 已提交
131
      param->dataInBlock = false;
D
dapan1121 已提交
132 133 134
      
      break;
    }
D
dapan1121 已提交
135 136
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)node;
D
dapan1121 已提交
137 138 139 140 141 142 143 144
      if (nodeList->pNodeList->length <= 0) {
        sclError("invalid length in nodeList, length:%d", nodeList->pNodeList->length);
        SCL_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

      SCL_ERR_RET(scalarGenerateSetFromList(&param->data, node, nodeList->dataType.type));
      param->num = 1;
      param->type = SCL_DATA_TYPE_DUMMY_HASH;
D
dapan1121 已提交
145
      param->dataInBlock = false;
D
dapan1121 已提交
146
      
D
dapan1121 已提交
147 148
      break;
    }
X
Xiaoyu Wang 已提交
149
    case QUERY_NODE_COLUMN: {
D
dapan1121 已提交
150 151 152 153 154
      if (NULL == ctx) {
        sclError("invalid node type for constant calculating, type:%d, ctx:%p", nodeType(node), ctx);
        SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
      
X
Xiaoyu Wang 已提交
155
      SColumnNode *ref = (SColumnNode *)node;
D
dapan1121 已提交
156
      if (ref->slotId >= taosArrayGetSize(ctx->pSrc->pDataBlock)) {
D
dapan1121 已提交
157
        sclError("column ref slotId is too big, slodId:%d, dataBlockSize:%d", ref->slotId, (int32_t)taosArrayGetSize(ctx->pSrc->pDataBlock));
D
dapan1121 已提交
158 159 160 161
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

      SColumnInfoData *columnData = (SColumnInfoData *)taosArrayGet(ctx->pSrc->pDataBlock, ref->slotId);
D
dapan1121 已提交
162 163 164
      param->data = NULL;
      param->columnData = columnData;
      param->dataInBlock = true;
D
dapan1121 已提交
165
      
D
dapan1121 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
      param->num = ctx->pSrc->info.rows;
      param->type = columnData->info.type;
      param->bytes = columnData->info.bytes;
      
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
    case QUERY_NODE_OPERATOR: {
      if (NULL == ctx) {
        sclError("invalid node type for constant calculating, type:%d, ctx:%p", nodeType(node), ctx);
        SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }

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

  if (param->num > *rowNum) {
D
dapan1121 已提交
192
    if ((1 != param->num) && (1 < *rowNum)) {
D
dapan1121 已提交
193 194 195 196 197 198 199 200 201 202
      sclError("different row nums, rowNum:%d, newRowNum:%d", *rowNum, param->num);
      SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
    
    *rowNum = param->num;
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
203
int32_t sclMoveParamListData(SScalarParam *params, int32_t listNum, int32_t idx) {
D
dapan1121 已提交
204 205
  SScalarParam *param = NULL;
  
D
dapan1121 已提交
206
  for (int32_t i = 0; i < listNum; ++i) {
D
dapan1121 已提交
207 208 209 210 211 212
    param = params + i;
    
    if (1 == param->num) {
      continue;
    }

D
dapan1121 已提交
213 214 215 216 217 218 219 220
    if (param->dataInBlock) {
      param->data = colDataGet(param->columnData, idx);
    } else if (idx) {
      if (IS_VAR_DATA_TYPE(param->type)) {
        param->data = (char *)(param->data) + varDataTLen(param->data);
      } else {
        param->data = (char *)(param->data) + tDataTypes[param->type].bytes;
      }
D
dapan1121 已提交
221 222 223 224 225 226 227 228
    }
  }

  return TSDB_CODE_SUCCESS;
}

int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
229 230 231
  SScalarParam *paramList = calloc(pParamList->length, sizeof(SScalarParam));
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(pParamList->length * sizeof(SScalarParam)));
D
dapan1121 已提交
232
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
233 234
  }

D
dapan1121 已提交
235 236
  SListCell *cell = pParamList->pHead;
  for (int32_t i = 0; i < pParamList->length; ++i) {
D
dapan1121 已提交
237 238
    if (NULL == cell || NULL == cell->pNode) {
      sclError("invalid cell, cell:%p, pNode:%p", cell, cell->pNode);
D
dapan1121 已提交
239
      SCL_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
240 241
    }

D
dapan1121 已提交
242
    SCL_ERR_JRET(sclInitParam(cell->pNode, &paramList[i], ctx, rowNum));
D
dapan1121 已提交
243 244 245
    
    cell = cell->pNext;
  }
D
dapan1121 已提交
246

D
dapan1121 已提交
247 248
  *pParams = paramList;

D
dapan1121 已提交
249
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
250

D
dapan1121 已提交
251 252
_return:

D
dapan1121 已提交
253
  tfree(paramList);
D
dapan1121 已提交
254 255 256 257 258
  SCL_RET(code);
}

int32_t sclInitOperatorParams(SScalarParam **pParams, SOperatorNode *node, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
259
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
260 261 262 263 264
  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);
  }
  
D
dapan1121 已提交
265 266 267
  SScalarParam *paramList = calloc(paramNum, sizeof(SScalarParam));
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam)));
D
dapan1121 已提交
268 269 270
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
271
  SCL_ERR_JRET(sclInitParam(node->pLeft, &paramList[0], ctx, rowNum));
D
dapan1121 已提交
272
  if (paramNum > 1) {
D
dapan1121 已提交
273
    SCL_ERR_JRET(sclInitParam(node->pRight, &paramList[1], ctx, rowNum));
D
dapan1121 已提交
274 275
  }

D
dapan1121 已提交
276 277
  *pParams = paramList;

D
dapan1121 已提交
278
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
279 280 281

_return:

D
dapan1121 已提交
282
  tfree(paramList);
D
dapan1121 已提交
283
  SCL_RET(code);
D
dapan1121 已提交
284 285
}

D
dapan1121 已提交
286 287

int32_t sclExecFuncion(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) {
D
dapan1121 已提交
288 289
  if (NULL == node->pParameterList || node->pParameterList->length <= 0) {
    sclError("invalid function parameter list, list:%p, paramNum:%d", node->pParameterList, node->pParameterList ? node->pParameterList->length : 0);
D
dapan1121 已提交
290
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
291 292 293 294 295
  }

  SScalarFuncExecFuncs ffpSet = {0};
  int32_t code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet);
  if (code) {
D
dapan1121 已提交
296
    sclError("fmGetFuncExecFuncs failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
D
dapan1121 已提交
297
    SCL_ERR_RET(code);
D
dapan1121 已提交
298 299
  }

D
dapan1121 已提交
300 301 302 303 304 305 306
  SScalarParam *params = NULL;
  int32_t rowNum = 0;
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &rowNum));

  output->type = node->node.resType.type;
  output->data = calloc(rowNum, sizeof(tDataTypes[output->type].bytes));
  if (NULL == output->data) {
D
dapan1121 已提交
307
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(tDataTypes[output->type].bytes)));
D
dapan1121 已提交
308 309 310 311
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  for (int32_t i = 0; i < rowNum; ++i) {
D
dapan1121 已提交
312 313 314
    sclMoveParamListData(output, 1, i);
    sclMoveParamListData(params, node->pParameterList->length, i);

D
dapan1121 已提交
315 316
    code = (*ffpSet.process)(params, node->pParameterList->length, output);
    if (code) {
D
dapan1121 已提交
317
      sclError("scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
D
dapan1121 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
      SCL_ERR_JRET(code);    
    }
  }

  return TSDB_CODE_SUCCESS;

_return:

  tfree(params);
  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 已提交
340 341
  }

D
dapan1121 已提交
342 343 344 345 346 347 348 349
  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;
  int32_t code = 0;
D
dapan1121 已提交
350
  
D
dapan1121 已提交
351 352 353
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &rowNum));

  output->type = node->node.resType.type;
D
dapan1121 已提交
354 355
  output->bytes = sizeof(bool);
  output->num = rowNum;
D
dapan1121 已提交
356 357
  output->data = calloc(rowNum, sizeof(bool));
  if (NULL == output->data) {
D
dapan1121 已提交
358
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(bool)));
D
dapan1121 已提交
359 360
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
361 362

  void *data = output->data;
D
dapan1121 已提交
363 364 365 366
  
  bool value = false;

  for (int32_t i = 0; i < rowNum; ++i) {
D
dapan1121 已提交
367 368 369
    sclMoveParamListData(output, 1, i);
    sclMoveParamListData(params, node->pParameterList->length, i);
  
D
dapan1121 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    for (int32_t m = 0; m < node->pParameterList->length; ++m) {
      GET_TYPED_DATA(value, bool, params[m].type, params[m].data);
      
      if (LOGIC_COND_TYPE_AND == node->condType && (false == value)) {
        break;
      } else if (LOGIC_COND_TYPE_OR == node->condType && value) {
        break;
      } else if (LOGIC_COND_TYPE_NOT == node->condType) {
        value = !value;
      }
    }

    *(bool *)output->data = value;
  }

D
dapan1121 已提交
385 386
  output->data = data;

D
dapan1121 已提交
387 388 389 390 391
  return TSDB_CODE_SUCCESS;

_return:

  tfree(params);
D
dapan1121 已提交
392
  SCL_RET(code);
D
dapan1121 已提交
393 394 395 396 397 398 399 400 401 402
}

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

  output->type = node->node.resType.type;
D
dapan1121 已提交
403
  output->num = rowNum;
D
dapan1121 已提交
404
  output->bytes = tDataTypes[output->type].bytes;
D
dapan1121 已提交
405
  output->data = calloc(rowNum, tDataTypes[output->type].bytes);
D
dapan1121 已提交
406
  if (NULL == output->data) {
D
dapan1121 已提交
407
    sclError("calloc %d failed", (int32_t)rowNum * tDataTypes[output->type].bytes);
D
dapan1121 已提交
408 409 410 411 412
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(node->opType);

D
dapan1121 已提交
413
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
414 415
  SScalarParam* pLeft = &params[0];
  SScalarParam* pRight = paramNum > 1 ? &params[1] : NULL;
D
dapan1121 已提交
416
  
D
dapan1121 已提交
417
  OperatorFn(pLeft, pRight, output->data, TSDB_ORDER_ASC);
D
dapan1121 已提交
418 419 420 421 422 423

  return TSDB_CODE_SUCCESS;

_return:

  tfree(params);
D
dapan1121 已提交
424
  SCL_RET(code);
D
dapan1121 已提交
425 426 427 428 429 430 431 432 433
}


EDealRes sclRewriteFunction(SNode** pNode, void* pContext) {
  SFunctionNode *node = (SFunctionNode *)*pNode;
  SScalarParam output = {0};
  
  *(int32_t *)pContext = sclExecFuncion(node, NULL, &output);
  if (*(int32_t *)pContext) {
D
dapan1121 已提交
434 435 436
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
437
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
438 439
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
440
    sclFreeParam(&output);
D
dapan1121 已提交
441 442 443 444 445 446
    *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

  res->node.resType = node->node.resType;

D
dapan1121 已提交
447 448 449 450 451 452 453
  if (IS_VAR_DATA_TYPE(output.type)) {
    res->datum.p = output.data;
    output.data = NULL;
  } else {
    memcpy(nodesGetValueFromNode(res), output.data, tDataTypes[output.type].bytes);
  }
  
D
dapan1121 已提交
454 455 456
  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

D
dapan1121 已提交
457
  sclFreeParam(&output);
D
dapan1121 已提交
458 459 460 461 462 463

  return DEAL_RES_CONTINUE;
}

EDealRes sclRewriteLogic(SNode** pNode, void* pContext) {
  SLogicConditionNode *node = (SLogicConditionNode *)*pNode;
D
dapan1121 已提交
464
  SScalarParam output = {0};
D
dapan1121 已提交
465

D
dapan1121 已提交
466 467
  *(int32_t *)pContext = sclExecLogic(node, NULL, &output);
  if (*(int32_t *)pContext) {
D
dapan1121 已提交
468 469 470
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
471
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
472 473
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
474
    sclFreeParam(&output);    
D
dapan1121 已提交
475 476 477 478 479 480
    *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

  res->node.resType = node->node.resType;

D
dapan1121 已提交
481 482 483 484 485 486
  if (IS_VAR_DATA_TYPE(output.type)) {
    res->datum.p = output.data;
    output.data = NULL;
  } else {
    memcpy(nodesGetValueFromNode(res), output.data, tDataTypes[output.type].bytes);
  }
D
dapan1121 已提交
487 488 489 490

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

D
dapan1121 已提交
491 492
  sclFreeParam(&output);

D
dapan1121 已提交
493 494 495 496
  return DEAL_RES_CONTINUE;
}

EDealRes sclRewriteOperator(SNode** pNode, void* pContext) {
D
dapan1121 已提交
497 498
  SOperatorNode *node = (SOperatorNode *)*pNode;
  SScalarParam output = {0};
D
dapan1121 已提交
499

D
dapan1121 已提交
500 501
  *(int32_t *)pContext = sclExecOperator(node, NULL, &output);
  if (*(int32_t *)pContext) {
D
dapan1121 已提交
502 503 504
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
505
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
506
  if (NULL == res) {
D
dapan1121 已提交
507 508
    sclError("make value node failed");    
    sclFreeParam(&output);    
D
dapan1121 已提交
509 510 511 512
    *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
513
  res->node.resType = node->node.resType;
D
dapan1121 已提交
514

D
dapan1121 已提交
515 516 517 518 519 520
  if (IS_VAR_DATA_TYPE(output.type)) {
    res->datum.p = output.data;
    output.data = NULL;
  } else {
    memcpy(nodesGetValueFromNode(res), output.data, tDataTypes[output.type].bytes);
  }
D
dapan1121 已提交
521 522 523 524

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

D
dapan1121 已提交
525 526
  sclFreeParam(&output);    

D
dapan1121 已提交
527 528 529 530 531
  return DEAL_RES_CONTINUE;
}


EDealRes sclConstantsRewriter(SNode** pNode, void* pContext) {
D
dapan1121 已提交
532
  if (QUERY_NODE_VALUE == nodeType(*pNode) || QUERY_NODE_NODE_LIST == nodeType(*pNode)) {
D
dapan1121 已提交
533 534 535 536 537 538 539 540 541 542 543
    return DEAL_RES_CONTINUE;
  }

  if (QUERY_NODE_FUNCTION == nodeType(*pNode)) {
    return sclRewriteFunction(pNode, pContext);
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) {
    return sclRewriteLogic(pNode, pContext);
  }

D
dapan1121 已提交
544 545
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
    return sclRewriteOperator(pNode, pContext);
D
dapan1121 已提交
546 547
  }  
  
D
dapan1121 已提交
548 549 550 551 552
  sclError("invalid node type for calculating constants, type:%d", nodeType(*pNode));
  
  *(int32_t *)pContext = TSDB_CODE_QRY_INVALID_INPUT;
  
  return DEAL_RES_ERROR;
D
dapan1121 已提交
553 554
}

D
dapan1121 已提交
555

D
dapan1121 已提交
556
EDealRes sclWalkFunction(SNode* pNode, void* pContext) {
D
dapan1121 已提交
557
  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
558
  SFunctionNode *node = (SFunctionNode *)pNode;
D
dapan1121 已提交
559 560 561 562 563
  SScalarParam output = {0};
  
  ctx->code = sclExecFuncion(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
564 565
  }

D
dapan1121 已提交
566
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
567 568
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
D
dapan1121 已提交
569 570
  }

D
dapan1121 已提交
571 572 573 574
  return DEAL_RES_CONTINUE;
}


D
dapan1121 已提交
575
EDealRes sclWalkLogic(SNode* pNode, void* pContext) {
D
dapan1121 已提交
576
  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
577
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
D
dapan1121 已提交
578 579 580 581 582
  SScalarParam output = {0};
  
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
583 584
  }

D
dapan1121 已提交
585
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
586
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
587
    return DEAL_RES_ERROR;
D
dapan1121 已提交
588 589 590 591 592 593
  }

  return DEAL_RES_CONTINUE;
}


D
dapan1121 已提交
594
EDealRes sclWalkOperator(SNode* pNode, void* pContext) {
D
dapan1121 已提交
595
  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
596
  SOperatorNode *node = (SOperatorNode *)pNode;
D
dapan1121 已提交
597
  SScalarParam output = {0};
D
dapan1121 已提交
598
  
D
dapan1121 已提交
599 600
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
601 602
    return DEAL_RES_ERROR;
  }
D
dapan1121 已提交
603

D
dapan1121 已提交
604
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
605
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
606 607 608
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
609 610 611 612
  return DEAL_RES_CONTINUE;
}


D
dapan1121 已提交
613
EDealRes sclCalcWalker(SNode* pNode, void* pContext) {
X
Xiaoyu Wang 已提交
614
  if (QUERY_NODE_VALUE == nodeType(pNode) || QUERY_NODE_NODE_LIST == nodeType(pNode) || QUERY_NODE_COLUMN == nodeType(pNode)) {
D
dapan1121 已提交
615
    return DEAL_RES_CONTINUE;
D
dapan1121 已提交
616
  }
D
dapan1121 已提交
617
    
D
dapan1121 已提交
618
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
D
dapan1121 已提交
619 620
    return sclWalkFunction(pNode, pContext);
  }
D
dapan1121 已提交
621

D
dapan1121 已提交
622
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
D
dapan1121 已提交
623 624
    return sclWalkLogic(pNode, pContext);
  }
D
dapan1121 已提交
625

D
dapan1121 已提交
626
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
D
dapan1121 已提交
627
    return sclWalkOperator(pNode, pContext);
D
dapan1121 已提交
628
  }
D
dapan1121 已提交
629

D
dapan1121 已提交
630
  sclError("invalid node type for scalar calculating, type:%d", nodeType(pNode));
D
dapan1121 已提交
631

D
dapan1121 已提交
632 633 634 635 636
  SScalarCtx *ctx = (SScalarCtx *)pContext;
  
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  
  return DEAL_RES_ERROR;
D
dapan1121 已提交
637 638
}

D
dapan1121 已提交
639 640


D
dapan1121 已提交
641 642 643 644 645 646 647
int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) {
  if (NULL == pNode) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
  
D
dapan1121 已提交
648 649 650 651 652 653 654 655 656 657 658 659
  nodesRewriteNodePostOrder(&pNode, sclConstantsRewriter, (void *)&code);

  if (code) {
    nodesDestroyNode(pNode);
    SCL_ERR_RET(code);
  }

  *pRes = pNode;

  SCL_RET(code);
}

D
dapan1121 已提交
660 661
int32_t scalarCalculate(SNode *pNode, SSDataBlock *pSrc, SScalarParam *pDst) {
  if (NULL == pNode || NULL == pSrc || NULL == pDst) {
D
dapan1121 已提交
662 663 664 665
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan1121 已提交
666 667 668 669 670 671 672
  SScalarCtx ctx = {.code = 0, .pSrc = pSrc};
  
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), 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);
  }
D
dapan1121 已提交
673
  
D
dapan1121 已提交
674
  nodesWalkNodePostOrder(pNode, sclCalcWalker, (void *)&ctx);
D
dapan1121 已提交
675

D
dapan1121 已提交
676
  if (ctx.code) {
D
dapan1121 已提交
677
    nodesDestroyNode(pNode);
D
dapan1121 已提交
678 679
    sclFreeRes(ctx.pRes);
    SCL_ERR_RET(ctx.code);
D
dapan1121 已提交
680 681
  }

D
dapan1121 已提交
682
  SScalarParam *res = (SScalarParam *)taosHashGet(ctx.pRes, (void *)&pNode, POINTER_BYTES);
D
dapan1121 已提交
683
  if (NULL == res) {
D
dapan1121 已提交
684
    sclError("no res for calculating, node:%p, type:%d", pNode, nodeType(pNode));
D
dapan1121 已提交
685 686 687 688
    SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
  
  *pDst = *res;
D
dapan1121 已提交
689

D
dapan1121 已提交
690 691 692
  nodesDestroyNode(pNode);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
693 694 695 696
}