scalar.c 18.5 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 90
  }

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

  return TSDB_CODE_SUCCESS;

_return:

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


D
dapan1121 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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 已提交
119
      param->colData = false;
D
dapan1121 已提交
120 121 122
      
      break;
    }
D
dapan1121 已提交
123 124
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)node;
D
dapan1121 已提交
125 126 127 128 129 130 131 132
      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 已提交
133
      param->colData = false;
D
dapan1121 已提交
134
      
D
dapan1121 已提交
135 136
      break;
    }
X
Xiaoyu Wang 已提交
137
    case QUERY_NODE_COLUMN: {
D
dapan1121 已提交
138 139 140 141 142
      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 已提交
143
      SColumnNode *ref = (SColumnNode *)node;
D
dapan1121 已提交
144
      if (ref->slotId >= taosArrayGetSize(ctx->pSrc->pDataBlock)) {
D
dapan1121 已提交
145
        sclError("column ref slotId is too big, slodId:%d, dataBlockSize:%d", ref->slotId, (int32_t)taosArrayGetSize(ctx->pSrc->pDataBlock));
D
dapan1121 已提交
146 147 148 149
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

      SColumnInfoData *columnData = (SColumnInfoData *)taosArrayGet(ctx->pSrc->pDataBlock, ref->slotId);
D
dapan1121 已提交
150 151 152 153 154 155 156 157
      if (IS_VAR_DATA_TYPE(columnData->info.type)) {
        param->data = columnData;        
        param->colData = true;
      } else {
        param->data = columnData->pData;        
        param->colData = false;
      }
      
D
dapan1121 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      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 已提交
184
    if ((1 != param->num) && (1 < *rowNum)) {
D
dapan1121 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
      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;
}

int32_t sclParamMoveNext(SScalarParam *params, int32_t num) {
  SScalarParam *param = NULL;
  
  for (int32_t i = 0; i < num; ++i) {
    param = params + i;
    
    if (1 == param->num) {
      continue;
    }

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

  return TSDB_CODE_SUCCESS;
}

int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
217 218 219
  SScalarParam *paramList = calloc(pParamList->length, sizeof(SScalarParam));
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(pParamList->length * sizeof(SScalarParam)));
D
dapan1121 已提交
220
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
221 222
  }

D
dapan1121 已提交
223 224
  SListCell *cell = pParamList->pHead;
  for (int32_t i = 0; i < pParamList->length; ++i) {
D
dapan1121 已提交
225 226
    if (NULL == cell || NULL == cell->pNode) {
      sclError("invalid cell, cell:%p, pNode:%p", cell, cell->pNode);
D
dapan1121 已提交
227
      SCL_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
228 229
    }

D
dapan1121 已提交
230
    SCL_ERR_JRET(sclInitParam(cell->pNode, &paramList[i], ctx, rowNum));
D
dapan1121 已提交
231 232 233
    
    cell = cell->pNext;
  }
D
dapan1121 已提交
234

D
dapan1121 已提交
235 236
  *pParams = paramList;

D
dapan1121 已提交
237
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
238

D
dapan1121 已提交
239 240
_return:

D
dapan1121 已提交
241
  tfree(paramList);
D
dapan1121 已提交
242 243 244 245 246
  SCL_RET(code);
}

int32_t sclInitOperatorParams(SScalarParam **pParams, SOperatorNode *node, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
247
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
248 249 250 251 252
  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 已提交
253 254 255
  SScalarParam *paramList = calloc(paramNum, sizeof(SScalarParam));
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam)));
D
dapan1121 已提交
256 257 258
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
259
  SCL_ERR_JRET(sclInitParam(node->pLeft, &paramList[0], ctx, rowNum));
D
dapan1121 已提交
260
  if (paramNum > 1) {
D
dapan1121 已提交
261
    SCL_ERR_JRET(sclInitParam(node->pRight, &paramList[1], ctx, rowNum));
D
dapan1121 已提交
262 263
  }

D
dapan1121 已提交
264 265
  *pParams = paramList;

D
dapan1121 已提交
266
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
267 268 269

_return:

D
dapan1121 已提交
270
  tfree(paramList);
D
dapan1121 已提交
271
  SCL_RET(code);
D
dapan1121 已提交
272 273
}

D
dapan1121 已提交
274 275

int32_t sclExecFuncion(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) {
D
dapan1121 已提交
276 277
  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 已提交
278
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
279 280 281 282 283
  }

  SScalarFuncExecFuncs ffpSet = {0};
  int32_t code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet);
  if (code) {
X
Xiaoyu Wang 已提交
284 285
    sclError(
"fmGetFuncExecFuncs failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
D
dapan1121 已提交
286
    SCL_ERR_RET(code);
D
dapan1121 已提交
287 288
  }

D
dapan1121 已提交
289 290 291 292 293 294 295
  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 已提交
296
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(tDataTypes[output->type].bytes)));
D
dapan1121 已提交
297 298 299 300 301 302
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  for (int32_t i = 0; i < rowNum; ++i) {
    code = (*ffpSet.process)(params, node->pParameterList->length, output);
    if (code) {
X
Xiaoyu Wang 已提交
303 304
      sclError(
"scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
D
dapan1121 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
      SCL_ERR_JRET(code);    
    }

    sclParamMoveNext(output, 1);
    sclParamMoveNext(params, node->pParameterList->length);
  }

  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 已提交
330 331
  }

D
dapan1121 已提交
332 333 334 335 336 337 338 339
  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 已提交
340
  
D
dapan1121 已提交
341 342 343
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &rowNum));

  output->type = node->node.resType.type;
D
dapan1121 已提交
344 345
  output->bytes = sizeof(bool);
  output->num = rowNum;
D
dapan1121 已提交
346 347
  output->data = calloc(rowNum, sizeof(bool));
  if (NULL == output->data) {
D
dapan1121 已提交
348
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(bool)));
D
dapan1121 已提交
349 350
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan1121 已提交
351 352

  void *data = output->data;
D
dapan1121 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  
  bool value = false;

  for (int32_t i = 0; i < rowNum; ++i) {
    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;

    sclParamMoveNext(output, 1);    
    sclParamMoveNext(params, node->pParameterList->length);
  }

D
dapan1121 已提交
375 376
  output->data = data;

D
dapan1121 已提交
377 378 379 380 381
  return TSDB_CODE_SUCCESS;

_return:

  tfree(params);
D
dapan1121 已提交
382
  SCL_RET(code);
D
dapan1121 已提交
383 384 385 386 387 388 389 390 391 392
}

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 已提交
393
  output->num = rowNum;
D
dapan1121 已提交
394
  output->bytes = tDataTypes[output->type].bytes;
D
dapan1121 已提交
395
  output->data = calloc(rowNum, tDataTypes[output->type].bytes);
D
dapan1121 已提交
396
  if (NULL == output->data) {
D
dapan1121 已提交
397
    sclError("calloc %d failed", (int32_t)rowNum * tDataTypes[output->type].bytes);
D
dapan1121 已提交
398 399 400 401 402
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(node->opType);

D
dapan1121 已提交
403
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
404 405
  SScalarParam* pLeft = &params[0];
  SScalarParam* pRight = paramNum > 1 ? &params[1] : NULL;
D
dapan1121 已提交
406
  
D
dapan1121 已提交
407
  OperatorFn(pLeft, pRight, output->data, TSDB_ORDER_ASC);
D
dapan1121 已提交
408 409 410 411 412 413

  return TSDB_CODE_SUCCESS;

_return:

  tfree(params);
D
dapan1121 已提交
414
  SCL_RET(code);
D
dapan1121 已提交
415 416 417 418 419 420 421 422 423
}


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 已提交
424 425 426
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
427
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
428 429
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
430
    sclFreeParam(&output);
D
dapan1121 已提交
431 432 433 434 435 436
    *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

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

D
dapan1121 已提交
437 438 439 440 441 442 443
  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 已提交
444 445 446
  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

D
dapan1121 已提交
447
  sclFreeParam(&output);
D
dapan1121 已提交
448 449 450 451 452 453

  return DEAL_RES_CONTINUE;
}

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

D
dapan1121 已提交
456 457
  *(int32_t *)pContext = sclExecLogic(node, NULL, &output);
  if (*(int32_t *)pContext) {
D
dapan1121 已提交
458 459 460
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
461
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
462 463
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
464
    sclFreeParam(&output);    
D
dapan1121 已提交
465 466 467 468 469 470
    *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

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

D
dapan1121 已提交
471 472 473 474 475 476
  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 已提交
477 478 479 480

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

D
dapan1121 已提交
481 482
  sclFreeParam(&output);

D
dapan1121 已提交
483 484 485 486
  return DEAL_RES_CONTINUE;
}

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

D
dapan1121 已提交
490 491
  *(int32_t *)pContext = sclExecOperator(node, NULL, &output);
  if (*(int32_t *)pContext) {
D
dapan1121 已提交
492 493 494
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
495
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
496
  if (NULL == res) {
D
dapan1121 已提交
497 498
    sclError("make value node failed");    
    sclFreeParam(&output);    
D
dapan1121 已提交
499 500 501 502
    *(int32_t *)pContext = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
503
  res->node.resType = node->node.resType;
D
dapan1121 已提交
504

D
dapan1121 已提交
505 506 507 508 509 510
  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 已提交
511 512 513 514

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

D
dapan1121 已提交
515 516
  sclFreeParam(&output);    

D
dapan1121 已提交
517 518 519 520 521
  return DEAL_RES_CONTINUE;
}


EDealRes sclConstantsRewriter(SNode** pNode, void* pContext) {
D
dapan1121 已提交
522
  if (QUERY_NODE_VALUE == nodeType(*pNode) || QUERY_NODE_NODE_LIST == nodeType(*pNode)) {
D
dapan1121 已提交
523 524 525 526 527 528 529 530 531 532 533
    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 已提交
534 535
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
    return sclRewriteOperator(pNode, pContext);
D
dapan1121 已提交
536 537
  }  
  
D
dapan1121 已提交
538 539 540 541 542
  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 已提交
543 544
}

D
dapan1121 已提交
545

D
dapan1121 已提交
546
EDealRes sclWalkFunction(SNode* pNode, void* pContext) {
D
dapan1121 已提交
547
  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
548
  SFunctionNode *node = (SFunctionNode *)pNode;
D
dapan1121 已提交
549 550 551 552 553
  SScalarParam output = {0};
  
  ctx->code = sclExecFuncion(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
554 555
  }

D
dapan1121 已提交
556
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
557 558
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
D
dapan1121 已提交
559 560
  }

D
dapan1121 已提交
561 562 563 564
  return DEAL_RES_CONTINUE;
}


D
dapan1121 已提交
565
EDealRes sclWalkLogic(SNode* pNode, void* pContext) {
D
dapan1121 已提交
566
  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
567
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
D
dapan1121 已提交
568 569 570 571 572
  SScalarParam output = {0};
  
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
573 574
  }

D
dapan1121 已提交
575
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
576
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
577
    return DEAL_RES_ERROR;
D
dapan1121 已提交
578 579 580 581 582 583
  }

  return DEAL_RES_CONTINUE;
}


D
dapan1121 已提交
584
EDealRes sclWalkOperator(SNode* pNode, void* pContext) {
D
dapan1121 已提交
585
  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
586
  SOperatorNode *node = (SOperatorNode *)pNode;
D
dapan1121 已提交
587
  SScalarParam output = {0};
D
dapan1121 已提交
588
  
D
dapan1121 已提交
589 590
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
591 592
    return DEAL_RES_ERROR;
  }
D
dapan1121 已提交
593

D
dapan1121 已提交
594
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
595
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
596 597 598
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
599 600 601 602
  return DEAL_RES_CONTINUE;
}


D
dapan1121 已提交
603
EDealRes sclCalcWalker(SNode* pNode, void* pContext) {
X
Xiaoyu Wang 已提交
604
  if (QUERY_NODE_VALUE == nodeType(pNode) || QUERY_NODE_NODE_LIST == nodeType(pNode) || QUERY_NODE_COLUMN == nodeType(pNode)) {
D
dapan1121 已提交
605
    return DEAL_RES_CONTINUE;
D
dapan1121 已提交
606
  }
D
dapan1121 已提交
607
    
D
dapan1121 已提交
608
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
D
dapan1121 已提交
609 610
    return sclWalkFunction(pNode, pContext);
  }
D
dapan1121 已提交
611

D
dapan1121 已提交
612
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
D
dapan1121 已提交
613 614
    return sclWalkLogic(pNode, pContext);
  }
D
dapan1121 已提交
615

D
dapan1121 已提交
616
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
D
dapan1121 已提交
617
    return sclWalkOperator(pNode, pContext);
D
dapan1121 已提交
618
  }
D
dapan1121 已提交
619

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

D
dapan1121 已提交
622 623 624 625 626
  SScalarCtx *ctx = (SScalarCtx *)pContext;
  
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  
  return DEAL_RES_ERROR;
D
dapan1121 已提交
627 628
}

D
dapan1121 已提交
629 630


D
dapan1121 已提交
631 632 633 634 635 636 637
int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) {
  if (NULL == pNode) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
  
D
dapan1121 已提交
638 639 640 641 642 643 644 645 646 647 648 649
  nodesRewriteNodePostOrder(&pNode, sclConstantsRewriter, (void *)&code);

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

  *pRes = pNode;

  SCL_RET(code);
}

D
dapan1121 已提交
650 651
int32_t scalarCalculate(SNode *pNode, SSDataBlock *pSrc, SScalarParam *pDst) {
  if (NULL == pNode || NULL == pSrc || NULL == pDst) {
D
dapan1121 已提交
652 653 654 655
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan1121 已提交
656 657 658 659 660 661 662
  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 已提交
663
  
D
dapan1121 已提交
664
  nodesWalkNodePostOrder(pNode, sclCalcWalker, (void *)&ctx);
D
dapan1121 已提交
665

D
dapan1121 已提交
666
  if (ctx.code) {
D
dapan1121 已提交
667
    nodesDestroyNode(pNode);
D
dapan1121 已提交
668 669
    sclFreeRes(ctx.pRes);
    SCL_ERR_RET(ctx.code);
D
dapan1121 已提交
670 671
  }

D
dapan1121 已提交
672
  SScalarParam *res = (SScalarParam *)taosHashGet(ctx.pRes, (void *)&pNode, POINTER_BYTES);
D
dapan1121 已提交
673
  if (NULL == res) {
D
dapan1121 已提交
674
    sclError("no res for calculating, node:%p, type:%d", pNode, nodeType(pNode));
D
dapan1121 已提交
675 676 677 678
    SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
  }
  
  *pDst = *res;
D
dapan1121 已提交
679

D
dapan1121 已提交
680 681 682
  nodesDestroyNode(pNode);

  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
683 684 685 686
}