scalar.c 21.6 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
dapan 已提交
8
#include "tep.h"
D
dapan1121 已提交
9

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

  return 2;
}

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

    cell = cell->pNext;
D
dapan1121 已提交
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
dapan 已提交
91
FORCE_INLINE bool sclIsNull(SScalarParam* param, int32_t idx) {
D
dapan1121 已提交
92
  if (param->dataInBlock) {
D
dapan 已提交
93
    return colDataIsNull(param->orig.columnData, 0, idx, NULL);
D
dapan1121 已提交
94 95
  }

D
dapan 已提交
96
  return param->bitmap ? colDataIsNull_f(param->bitmap, idx) : false;
D
dapan1121 已提交
97 98
}

D
dapan 已提交
99 100 101 102 103 104 105 106 107
FORCE_INLINE void sclSetNull(SScalarParam* param, int32_t idx) {
  if (NULL == param->bitmap) {
    param->bitmap = calloc(BitmapLen(param->num), sizeof(char));
    if (NULL == param->bitmap) {
      sclError("calloc %d failed", param->num);
      return;
    }
  }
  
D
dapan1121 已提交
108 109 110
  colDataSetNull_f(param->bitmap, idx);
}

D
dapan1121 已提交
111

D
dapan1121 已提交
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) {
D
dapan 已提交
119
      sclFreeParam(p);
D
dapan1121 已提交
120 121 122 123 124 125 126 127 128
    }
    
    pIter = taosHashIterate(res, pIter);
  }
  
  taosHashCleanup(res);
}

void sclFreeParam(SScalarParam *param) {
D
dapan 已提交
129 130 131 132 133 134 135 136 137
  tfree(param->bitmap);
  
  if (!param->dataInBlock) {
    if (SCL_DATA_TYPE_DUMMY_HASH == param->type) {
      taosHashCleanup((SHashObj *)param->orig.data);
    } else {
      tfree(param->orig.data);
    }
  }
D
dapan1121 已提交
138 139 140 141 142 143 144
}

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);
D
dapan1121 已提交
145
      param->orig.data = param->data;
D
dapan1121 已提交
146 147 148
      param->num = 1;
      param->type = valueNode->node.resType.type;
      param->bytes = valueNode->node.resType.bytes;
D
dapan1121 已提交
149 150 151
      if (TSDB_DATA_TYPE_NULL == param->type) {
        sclSetNull(param, 0);
      }
D
dapan1121 已提交
152
      param->dataInBlock = false;
D
dapan1121 已提交
153 154 155
      
      break;
    }
D
dapan1121 已提交
156 157
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)node;
D
dapan1121 已提交
158 159 160 161 162 163
      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));
D
dapan 已提交
164
      param->orig.data = param->data;
D
dapan1121 已提交
165 166
      param->num = 1;
      param->type = SCL_DATA_TYPE_DUMMY_HASH;
D
dapan1121 已提交
167
      param->dataInBlock = false;
D
dapan 已提交
168 169 170 171 172 173 174

      if (taosHashPut(ctx->pRes, &node, POINTER_BYTES, param, sizeof(*param))) {
        taosHashCleanup(param->orig.data);
        param->orig.data = NULL;
        sclError("taosHashPut nodeList failed, size:%d", (int32_t)sizeof(*param));
        return TSDB_CODE_QRY_OUT_OF_MEMORY;
      }   
D
dapan1121 已提交
175
      
D
dapan1121 已提交
176 177
      break;
    }
X
Xiaoyu Wang 已提交
178
    case QUERY_NODE_COLUMN: {
D
dapan 已提交
179 180
      if (NULL == ctx->pBlockList) {
        sclError("invalid node type for constant calculating, type:%d, src:%p", nodeType(node), ctx->pBlockList);
D
dapan1121 已提交
181 182 183
        SCL_ERR_RET(TSDB_CODE_QRY_APP_ERROR);
      }
      
X
Xiaoyu Wang 已提交
184
      SColumnNode *ref = (SColumnNode *)node;
D
dapan1121 已提交
185 186
      if (ref->dataBlockId >= taosArrayGetSize(ctx->pBlockList)) {
        sclError("column tupleId is too big, tupleId:%d, dataBlockNum:%d", ref->dataBlockId, (int32_t)taosArrayGetSize(ctx->pBlockList));
D
dapan 已提交
187 188 189
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan1121 已提交
190
      SSDataBlock *block = *(SSDataBlock **)taosArrayGet(ctx->pBlockList, ref->dataBlockId);
D
dapan 已提交
191 192 193
      
      if (NULL == block || ref->slotId >= taosArrayGetSize(block->pDataBlock)) {
        sclError("column slotId is too big, slodId:%d, dataBlockSize:%d", ref->slotId, (int32_t)taosArrayGetSize(block->pDataBlock));
D
dapan1121 已提交
194 195 196
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan 已提交
197
      SColumnInfoData *columnData = (SColumnInfoData *)taosArrayGet(block->pDataBlock, ref->slotId);
D
dapan1121 已提交
198
      param->data = NULL;
D
dapan 已提交
199
      param->orig.columnData = columnData;
D
dapan1121 已提交
200
      param->dataInBlock = true;
D
dapan1121 已提交
201
      
D
dapan 已提交
202
      param->num = block->info.rows;
D
dapan1121 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      param->type = columnData->info.type;
      param->bytes = columnData->info.bytes;
      
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
    case QUERY_NODE_OPERATOR: {
      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 已提交
223
    if ((1 != param->num) && (1 < *rowNum)) {
D
dapan1121 已提交
224 225 226 227 228 229 230 231 232 233
      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 已提交
234
int32_t sclMoveParamListData(SScalarParam *params, int32_t listNum, int32_t idx) {
D
dapan1121 已提交
235 236
  SScalarParam *param = NULL;
  
D
dapan1121 已提交
237
  for (int32_t i = 0; i < listNum; ++i) {
D
dapan1121 已提交
238 239 240 241 242 243
    param = params + i;
    
    if (1 == param->num) {
      continue;
    }

D
dapan1121 已提交
244
    if (param->dataInBlock) {
D
dapan 已提交
245
      param->data = colDataGet(param->orig.columnData, idx);
D
dapan1121 已提交
246 247 248 249 250 251
    } 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
dapan 已提交
252 253
    } else {
      param->data = param->orig.data;
D
dapan1121 已提交
254 255 256 257 258 259 260 261
    }
  }

  return TSDB_CODE_SUCCESS;
}

int32_t sclInitParamList(SScalarParam **pParams, SNodeList* pParamList, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
262 263 264
  SScalarParam *paramList = calloc(pParamList->length, sizeof(SScalarParam));
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(pParamList->length * sizeof(SScalarParam)));
D
dapan1121 已提交
265
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
D
dapan1121 已提交
266 267
  }

D
dapan1121 已提交
268 269
  SListCell *cell = pParamList->pHead;
  for (int32_t i = 0; i < pParamList->length; ++i) {
D
dapan1121 已提交
270 271
    if (NULL == cell || NULL == cell->pNode) {
      sclError("invalid cell, cell:%p, pNode:%p", cell, cell->pNode);
D
dapan1121 已提交
272
      SCL_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
273 274
    }

D
dapan1121 已提交
275
    SCL_ERR_JRET(sclInitParam(cell->pNode, &paramList[i], ctx, rowNum));
D
dapan1121 已提交
276 277 278
    
    cell = cell->pNext;
  }
D
dapan1121 已提交
279

D
dapan1121 已提交
280 281
  *pParams = paramList;

D
dapan1121 已提交
282
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
283

D
dapan1121 已提交
284 285
_return:

D
dapan1121 已提交
286
  tfree(paramList);
D
dapan1121 已提交
287 288 289 290 291
  SCL_RET(code);
}

int32_t sclInitOperatorParams(SScalarParam **pParams, SOperatorNode *node, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
292
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
293 294 295 296 297
  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 已提交
298 299 300
  SScalarParam *paramList = calloc(paramNum, sizeof(SScalarParam));
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam)));
D
dapan1121 已提交
301 302 303
    SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }

D
dapan1121 已提交
304
  SCL_ERR_JRET(sclInitParam(node->pLeft, &paramList[0], ctx, rowNum));
D
dapan1121 已提交
305
  if (paramNum > 1) {
D
dapan1121 已提交
306
    SCL_ERR_JRET(sclInitParam(node->pRight, &paramList[1], ctx, rowNum));
D
dapan1121 已提交
307 308
  }

D
dapan1121 已提交
309 310
  *pParams = paramList;

D
dapan1121 已提交
311
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
312 313 314

_return:

D
dapan1121 已提交
315
  tfree(paramList);
D
dapan1121 已提交
316
  SCL_RET(code);
D
dapan1121 已提交
317 318
}

D
dapan1121 已提交
319 320

int32_t sclExecFuncion(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) {
D
dapan1121 已提交
321 322
  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 已提交
323
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
D
dapan1121 已提交
324 325 326 327 328
  }

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

D
dapan1121 已提交
333 334 335 336 337 338 339
  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 已提交
340
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(tDataTypes[output->type].bytes)));
D
dapan1121 已提交
341 342
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan 已提交
343
  output->orig.data = output->data;
D
dapan1121 已提交
344 345

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

D
dapan1121 已提交
349 350
    code = (*ffpSet.process)(params, node->pParameterList->length, output);
    if (code) {
D
dapan1121 已提交
351
      sclError("scalar function exec failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
D
dapan1121 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
      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 已提交
374 375
  }

D
dapan1121 已提交
376 377 378 379 380 381 382 383
  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 已提交
384
  
D
dapan1121 已提交
385 386 387
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &rowNum));

  output->type = node->node.resType.type;
D
dapan1121 已提交
388 389
  output->bytes = sizeof(bool);
  output->num = rowNum;
D
dapan1121 已提交
390 391
  output->data = calloc(rowNum, sizeof(bool));
  if (NULL == output->data) {
D
dapan1121 已提交
392
    sclError("calloc %d failed", (int32_t)(rowNum * sizeof(bool)));
D
dapan1121 已提交
393 394
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan 已提交
395
  output->orig.data = output->data;
D
dapan1121 已提交
396

D
dapan1121 已提交
397 398 399
  bool value = false;

  for (int32_t i = 0; i < rowNum; ++i) {
D
dapan1121 已提交
400 401 402
    sclMoveParamListData(output, 1, i);
    sclMoveParamListData(params, node->pParameterList->length, i);
  
D
dapan1121 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    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;
  }

  return TSDB_CODE_SUCCESS;

_return:

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

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 已提交
434
  output->num = rowNum;
D
dapan1121 已提交
435
  output->bytes = tDataTypes[output->type].bytes;
D
dapan1121 已提交
436
  output->data = calloc(rowNum, tDataTypes[output->type].bytes);
D
dapan1121 已提交
437
  if (NULL == output->data) {
D
dapan1121 已提交
438
    sclError("calloc %d failed", (int32_t)rowNum * tDataTypes[output->type].bytes);
D
dapan1121 已提交
439 440
    SCL_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY);
  }
D
dapan 已提交
441
  output->orig.data = output->data;
D
dapan1121 已提交
442 443 444

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(node->opType);

D
dapan1121 已提交
445
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
446 447
  SScalarParam* pLeft = &params[0];
  SScalarParam* pRight = paramNum > 1 ? &params[1] : NULL;
D
dapan1121 已提交
448
  
D
dapan 已提交
449
  OperatorFn(pLeft, pRight, output, TSDB_ORDER_ASC);
D
dapan1121 已提交
450 451 452 453 454 455

  return TSDB_CODE_SUCCESS;

_return:

  tfree(params);
D
dapan1121 已提交
456
  SCL_RET(code);
D
dapan1121 已提交
457 458 459
}


D
dapan 已提交
460
EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
461 462 463
  SFunctionNode *node = (SFunctionNode *)*pNode;
  SScalarParam output = {0};
  
D
dapan 已提交
464 465
  ctx->code = sclExecFuncion(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
466 467 468
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
469
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
470 471
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
472
    sclFreeParam(&output);
D
dapan 已提交
473
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
474 475 476 477 478
    return DEAL_RES_ERROR;
  }

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

D
dapan1121 已提交
479 480 481 482 483 484 485
  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 已提交
486 487 488
  nodesDestroyNode(*pNode);
  *pNode = (SNode*)res;

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

  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
494
EDealRes sclRewriteLogic(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
495
  SLogicConditionNode *node = (SLogicConditionNode *)*pNode;
D
dapan1121 已提交
496
  SScalarParam output = {0};
D
dapan1121 已提交
497

D
dapan 已提交
498 499
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
500 501 502
    return DEAL_RES_ERROR;
  }

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

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

D
dapan1121 已提交
513 514 515 516 517 518
  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 已提交
519 520 521 522

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

D
dapan1121 已提交
523 524
  sclFreeParam(&output);

D
dapan1121 已提交
525 526 527
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
528
EDealRes sclRewriteOperator(SNode** pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
529 530
  SOperatorNode *node = (SOperatorNode *)*pNode;
  SScalarParam output = {0};
D
dapan1121 已提交
531

D
dapan 已提交
532 533
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
534 535 536
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
537
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
538
  if (NULL == res) {
D
dapan1121 已提交
539 540
    sclError("make value node failed");    
    sclFreeParam(&output);    
D
dapan 已提交
541
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
542 543 544
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
545
  res->node.resType = node->node.resType;
D
dapan1121 已提交
546

D
dapan1121 已提交
547 548 549 550 551 552
  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 已提交
553 554 555 556

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

D
dapan1121 已提交
557 558
  sclFreeParam(&output);    

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


EDealRes sclConstantsRewriter(SNode** pNode, void* pContext) {
D
dapan1121 已提交
564
  if (QUERY_NODE_VALUE == nodeType(*pNode) || QUERY_NODE_NODE_LIST == nodeType(*pNode)) {
D
dapan1121 已提交
565 566 567
    return DEAL_RES_CONTINUE;
  }

D
dapan 已提交
568 569
  SScalarCtx *ctx = (SScalarCtx *)pContext;

D
dapan1121 已提交
570
  if (QUERY_NODE_FUNCTION == nodeType(*pNode)) {
D
dapan 已提交
571
    return sclRewriteFunction(pNode, ctx);
D
dapan1121 已提交
572 573 574
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) {
D
dapan 已提交
575
    return sclRewriteLogic(pNode, ctx);
D
dapan1121 已提交
576 577
  }

D
dapan1121 已提交
578
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
D
dapan 已提交
579
    return sclRewriteOperator(pNode, ctx);
D
dapan1121 已提交
580 581
  }  
  
D
dapan1121 已提交
582
  sclError("invalid node type for calculating constants, type:%d", nodeType(*pNode));
D
dapan 已提交
583 584

  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
D
dapan1121 已提交
585 586
  
  return DEAL_RES_ERROR;
D
dapan1121 已提交
587 588
}

D
dapan1121 已提交
589

D
dapan 已提交
590
EDealRes sclWalkFunction(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
591
  SFunctionNode *node = (SFunctionNode *)pNode;
D
dapan1121 已提交
592 593 594 595 596
  SScalarParam output = {0};
  
  ctx->code = sclExecFuncion(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
597 598
  }

D
dapan1121 已提交
599
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
600 601
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
D
dapan1121 已提交
602 603
  }

D
dapan1121 已提交
604 605 606 607
  return DEAL_RES_CONTINUE;
}


D
dapan 已提交
608
EDealRes sclWalkLogic(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
609
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
D
dapan1121 已提交
610 611 612 613 614
  SScalarParam output = {0};
  
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
615 616
  }

D
dapan1121 已提交
617
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
618
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
619
    return DEAL_RES_ERROR;
D
dapan1121 已提交
620 621 622 623 624 625
  }

  return DEAL_RES_CONTINUE;
}


D
dapan 已提交
626
EDealRes sclWalkOperator(SNode* pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
627
  SOperatorNode *node = (SOperatorNode *)pNode;
D
dapan1121 已提交
628
  SScalarParam output = {0};
D
dapan1121 已提交
629
  
D
dapan1121 已提交
630 631
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
632 633
    return DEAL_RES_ERROR;
  }
D
dapan1121 已提交
634

D
dapan1121 已提交
635
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
D
dapan1121 已提交
636
    ctx->code = TSDB_CODE_QRY_OUT_OF_MEMORY;
D
dapan1121 已提交
637 638 639
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
640 641 642
  return DEAL_RES_CONTINUE;
}

D
dapan 已提交
643 644 645
EDealRes sclWalkTarget(SNode* pNode, SScalarCtx *ctx) {
  STargetNode *target = (STargetNode *)pNode;
  
D
dapan1121 已提交
646 647
  if (target->dataBlockId >= taosArrayGetSize(ctx->pBlockList)) {
    sclError("target tupleId is too big, tupleId:%d, dataBlockNum:%d", target->dataBlockId, (int32_t)taosArrayGetSize(ctx->pBlockList));
D
dapan 已提交
648 649 650 651
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
652
  SSDataBlock *block = *(SSDataBlock **)taosArrayGet(ctx->pBlockList, target->dataBlockId);
D
dapan 已提交
653
  if (target->slotId >= taosArrayGetSize(block->pDataBlock)) {
D
dapan1121 已提交
654
    sclError("target slot not exist, dataBlockId:%d, slotId:%d, dataBlockNum:%d", target->dataBlockId, target->slotId, (int32_t)taosArrayGetSize(block->pDataBlock));
D
dapan 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

  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;
  }
  
  taosHashRemove(ctx->pRes, (void *)&target->pExpr, POINTER_BYTES);

  for (int32_t i = 0; i < res->num; ++i) {
    sclMoveParamListData(res, 1, i);

    colDataAppend(col, i, res->data, sclIsNull(res, i));
  }

  sclFreeParam(res);

  return DEAL_RES_CONTINUE;
}

D
dapan1121 已提交
681

D
dapan1121 已提交
682
EDealRes sclCalcWalker(SNode* pNode, void* pContext) {
X
Xiaoyu Wang 已提交
683
  if (QUERY_NODE_VALUE == nodeType(pNode) || QUERY_NODE_NODE_LIST == nodeType(pNode) || QUERY_NODE_COLUMN == nodeType(pNode)) {
D
dapan1121 已提交
684
    return DEAL_RES_CONTINUE;
D
dapan1121 已提交
685
  }
D
dapan 已提交
686 687

  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
688
    
D
dapan1121 已提交
689
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
D
dapan 已提交
690
    return sclWalkFunction(pNode, ctx);
D
dapan1121 已提交
691
  }
D
dapan1121 已提交
692

D
dapan1121 已提交
693
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
D
dapan 已提交
694
    return sclWalkLogic(pNode, ctx);
D
dapan1121 已提交
695
  }
D
dapan1121 已提交
696

D
dapan1121 已提交
697
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
D
dapan 已提交
698
    return sclWalkOperator(pNode, ctx);
D
dapan1121 已提交
699
  }
D
dapan1121 已提交
700

D
dapan 已提交
701 702 703
  if (QUERY_NODE_TARGET == nodeType(pNode)) {
    return sclWalkTarget(pNode, ctx);
  }
D
dapan1121 已提交
704

D
dapan 已提交
705
  sclError("invalid node type for scalar calculating, type:%d", nodeType(pNode));
D
dapan1121 已提交
706 707 708 709
  
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  
  return DEAL_RES_ERROR;
D
dapan1121 已提交
710 711
}

D
dapan1121 已提交
712 713


D
dapan1121 已提交
714 715 716 717 718 719
int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) {
  if (NULL == pNode) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan 已提交
720 721 722 723 724 725
  SScalarCtx ctx = {0};
  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 已提交
726
  
D
dapan 已提交
727
  nodesRewriteNodePostOrder(&pNode, sclConstantsRewriter, (void *)&ctx);
D
dapan1121 已提交
728

D
dapan 已提交
729
  SCL_ERR_JRET(ctx.code);
D
dapan1121 已提交
730 731 732

  *pRes = pNode;

D
dapan 已提交
733 734 735 736 737
_return:
  
  sclFreeRes(ctx.pRes);

  return code;
D
dapan1121 已提交
738 739
}

D
dapan 已提交
740
int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) {
D
dapan1121 已提交
741
  if (NULL == pNode || NULL == pBlockList) {
D
dapan1121 已提交
742 743 744 745
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

  int32_t code = 0;
D
dapan 已提交
746
  SScalarCtx ctx = {.code = 0, .pBlockList = pBlockList};
D
dapan1121 已提交
747 748 749 750 751 752
  
  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 已提交
753
  
D
dapan1121 已提交
754
  nodesWalkNodePostOrder(pNode, sclCalcWalker, (void *)&ctx);
D
dapan1121 已提交
755

D
dapan 已提交
756
  SCL_ERR_JRET(ctx.code);
D
dapan1121 已提交
757

D
dapan1121 已提交
758 759 760 761 762 763 764 765 766 767
  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);
    }
    
    taosHashRemove(ctx.pRes, (void *)&pNode, POINTER_BYTES);
    
    *pDst = *res;
D
dapan1121 已提交
768
  }
D
dapan1121 已提交
769

D
dapan 已提交
770 771
_return:
  
D
dapan1121 已提交
772
  nodesDestroyNode(pNode);
D
dapan 已提交
773
  sclFreeRes(ctx.pRes);
D
dapan1121 已提交
774

D
dapan 已提交
775
  return code;
D
dapan1121 已提交
776 777 778 779
}