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

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

  return 2;
}

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

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

  return TSDB_CODE_SUCCESS;
D
dapan 已提交
39 40
}

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

H
Hongze Cheng 已提交
48 49 50
  pColumnData->info.type = pType->type;
  pColumnData->info.bytes = pType->bytes;
  pColumnData->info.scale = pType->scale;
51 52
  pColumnData->info.precision = pType->precision;

H
Haojun Liao 已提交
53
  int32_t code = colInfoDataEnsureCapacity(pColumnData, numOfRows, true);
54 55
  if (code != TSDB_CODE_SUCCESS) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
56
    colDataDestroy(pColumnData);
H
Haojun Liao 已提交
57
    taosMemoryFree(pColumnData);
H
Haojun Liao 已提交
58
    return terrno;
59
  }
H
Haojun Liao 已提交
60 61

  pParam->columnData = pColumnData;
D
dapan1121 已提交
62
  pParam->colAlloced = true;
63 64
  pParam->numOfRows = numOfRows;

H
Haojun Liao 已提交
65
  return TSDB_CODE_SUCCESS;
66 67
}

X
Xiaoyu Wang 已提交
68
int32_t sclConvertValueToSclParam(SValueNode *pValueNode, SScalarParam *out, int32_t *overflow) {
69
  SScalarParam in = {.numOfRows = 1};
H
Hongze Cheng 已提交
70
  int32_t      code = sclCreateColumnInfoData(&pValueNode->node.resType, 1, &in);
H
Haojun Liao 已提交
71 72 73 74
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

75
  colDataSetVal(in.columnData, 0, nodesGetValueFromNode(pValueNode), false);
76

H
Haojun Liao 已提交
77
  colInfoDataEnsureCapacity(out->columnData, 1, true);
D
dapan1121 已提交
78
  code = vectorConvertSingleColImpl(&in, out, overflow, -1, -1);
79 80 81
  sclFreeParam(&in);

  return code;
82 83
}

D
dapan1121 已提交
84
int32_t sclExtendResRows(SScalarParam *pDst, SScalarParam *pSrc, SArray *pBlockList) {
X
Xiaoyu Wang 已提交
85
  SSDataBlock  *pb = taosArrayGetP(pBlockList, 0);
D
dapan1121 已提交
86 87 88
  SScalarParam *pLeft = taosMemoryCalloc(1, sizeof(SScalarParam));
  if (NULL == pLeft) {
    sclError("calloc %d failed", (int32_t)sizeof(SScalarParam));
S
Shengliang Guan 已提交
89
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
90 91 92 93 94
  }

  pLeft->numOfRows = pb->info.rows;

  if (pDst->numOfRows < pb->info.rows) {
H
Haojun Liao 已提交
95
    colInfoDataEnsureCapacity(pDst->columnData, pb->info.rows, true);
D
dapan1121 已提交
96
  }
X
Xiaoyu Wang 已提交
97

D
dapan1121 已提交
98 99 100 101 102 103 104 105
  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(OP_TYPE_ASSIGN);
  OperatorFn(pLeft, pSrc, pDst, TSDB_ORDER_ASC);

  taosMemoryFree(pLeft);

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
106 107 108 109
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);
S
Shengliang Guan 已提交
110
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
111 112
  }

G
Ganlin Zhao 已提交
113
  taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));
D
dapan1121 已提交
114

H
Hongze Cheng 已提交
115
  int32_t        code = 0;
D
dapan1121 已提交
116
  SNodeListNode *nodeList = (SNodeListNode *)pNode;
H
Hongze Cheng 已提交
117 118
  SListCell     *cell = nodeList->pNodeList->pHead;
  SScalarParam   out = {.columnData = taosMemoryCalloc(1, sizeof(SColumnInfoData))};
119

D
dapan1121 已提交
120
  int32_t len = 0;
H
Hongze Cheng 已提交
121
  void   *buf = NULL;
G
Ganlin Zhao 已提交
122

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

D
dapan1121 已提交
126
    if (valueNode->node.resType.type != type) {
127
      out.columnData->info.type = type;
D
dapan1121 已提交
128 129 130 131 132 133 134 135 136
      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;
      }
137

D
dapan1121 已提交
138
      int32_t overflow = 0;
D
dapan1121 已提交
139
      code = sclConvertValueToSclParam(valueNode, &out, &overflow);
140
      if (code != TSDB_CODE_SUCCESS) {
H
Hongze Cheng 已提交
141
        //        sclError("convert data from %d to %d failed", in.type, out.type);
D
dapan1121 已提交
142 143 144
        SCL_ERR_JRET(code);
      }

D
dapan1121 已提交
145 146 147 148 149
      if (overflow) {
        cell = cell->pNext;
        continue;
      }

D
dapan1121 已提交
150
      if (IS_VAR_DATA_TYPE(type)) {
151
        buf = colDataGetVarData(out.columnData, 0);
D
dapan1121 已提交
152
        len = varDataTLen(buf);
D
dapan1121 已提交
153 154
      } else {
        len = tDataTypes[type].bytes;
155
        buf = out.columnData->pData;
D
dapan1121 已提交
156 157 158
      }
    } else {
      buf = nodesGetValueFromNode(valueNode);
D
dapan1121 已提交
159
      if (IS_VAR_DATA_TYPE(type)) {
160
        len = varDataTLen(buf);
D
dapan1121 已提交
161 162
      } else {
        len = valueNode->node.resType.bytes;
163
      }
D
dapan1121 已提交
164
    }
G
Ganlin Zhao 已提交
165

166
    if (taosHashPut(pObj, buf, (size_t)len, NULL, 0)) {
D
dapan1121 已提交
167
      sclError("taosHashPut to set failed");
S
Shengliang Guan 已提交
168
      SCL_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
169
    }
D
dapan1121 已提交
170

171
    colInfoDataCleanup(out.columnData, out.numOfRows);
D
dapan1121 已提交
172
    cell = cell->pNext;
D
dapan1121 已提交
173 174 175
  }

  *data = pObj;
D
dapan1121 已提交
176 177 178

  colDataDestroy(out.columnData);
  taosMemoryFreeClear(out.columnData);
D
dapan1121 已提交
179 180 181
  return TSDB_CODE_SUCCESS;

_return:
D
dapan1121 已提交
182 183 184

  colDataDestroy(out.columnData);
  taosMemoryFreeClear(out.columnData);
D
dapan1121 已提交
185 186 187 188
  taosHashCleanup(pObj);
  SCL_RET(code);
}

D
dapan1121 已提交
189 190
void sclFreeRes(SHashObj *res) {
  SScalarParam *p = NULL;
H
Hongze Cheng 已提交
191
  void         *pIter = taosHashIterate(res, NULL);
D
dapan1121 已提交
192 193 194 195
  while (pIter) {
    p = (SScalarParam *)pIter;

    if (p) {
D
dapan 已提交
196
      sclFreeParam(p);
D
dapan1121 已提交
197 198 199 200 201 202
    }
    pIter = taosHashIterate(res, pIter);
  }
  taosHashCleanup(res);
}

D
dapan1121 已提交
203
void sclFreeParam(SScalarParam *param) {
D
dapan1121 已提交
204
  if (NULL == param || !param->colAlloced) {
D
dapan1121 已提交
205 206
    return;
  }
H
Hongze Cheng 已提交
207

D
dapan1121 已提交
208
  if (param->columnData != NULL) {
209
    colDataDestroy(param->columnData);
210
    taosMemoryFreeClear(param->columnData);
211 212 213 214
  }

  if (param->pHashFilter != NULL) {
    taosHashCleanup(param->pHashFilter);
D
dapan1121 已提交
215
    param->pHashFilter = NULL;
216
  }
D
dapan1121 已提交
217 218
}

D
dapan1121 已提交
219 220 221 222
int32_t sclCopyValueNodeValue(SValueNode *pNode, void **res) {
  if (TSDB_DATA_TYPE_NULL == pNode->node.resType.type) {
    return TSDB_CODE_SUCCESS;
  }
G
Ganlin Zhao 已提交
223

wafwerar's avatar
wafwerar 已提交
224
  *res = taosMemoryMalloc(pNode->node.resType.bytes);
D
dapan1121 已提交
225 226
  if (NULL == (*res)) {
    sclError("malloc %d failed", pNode->node.resType.bytes);
S
Shengliang Guan 已提交
227
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
228 229 230 231 232 233
  }

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

D
dapan1121 已提交
234 235 236 237 238 239
void sclFreeParamList(SScalarParam *param, int32_t paramNum) {
  if (NULL == param) {
    return;
  }

  for (int32_t i = 0; i < paramNum; ++i) {
H
Hongze Cheng 已提交
240
    SScalarParam *p = param + i;
D
dapan1121 已提交
241 242 243 244 245 246
    sclFreeParam(p);
  }

  taosMemoryFree(param);
}

247 248 249 250 251 252
void sclDowngradeValueType(SValueNode *valueNode) {
  switch (valueNode->node.resType.type) {
    case TSDB_DATA_TYPE_BIGINT: {
      int8_t i8 = valueNode->datum.i;
      if (i8 == valueNode->datum.i) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_TINYINT;
H
Hongze Cheng 已提交
253
        *(int8_t *)&valueNode->typeData = i8;
254 255 256 257 258
        break;
      }
      int16_t i16 = valueNode->datum.i;
      if (i16 == valueNode->datum.i) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_SMALLINT;
H
Hongze Cheng 已提交
259
        *(int16_t *)&valueNode->typeData = i16;
260 261 262 263 264
        break;
      }
      int32_t i32 = valueNode->datum.i;
      if (i32 == valueNode->datum.i) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_INT;
H
Hongze Cheng 已提交
265
        *(int32_t *)&valueNode->typeData = i32;
266 267 268 269
        break;
      }
      break;
    }
H
Hongze Cheng 已提交
270
    case TSDB_DATA_TYPE_UBIGINT: {
271 272 273 274 275
      uint8_t u8 = valueNode->datum.i;
      if (u8 == valueNode->datum.i) {
        int8_t i8 = valueNode->datum.i;
        if (i8 == valueNode->datum.i) {
          valueNode->node.resType.type = TSDB_DATA_TYPE_TINYINT;
H
Hongze Cheng 已提交
276
          *(int8_t *)&valueNode->typeData = i8;
277 278
        } else {
          valueNode->node.resType.type = TSDB_DATA_TYPE_UTINYINT;
H
Hongze Cheng 已提交
279
          *(uint8_t *)&valueNode->typeData = u8;
280 281 282 283 284 285 286 287
        }
        break;
      }
      uint16_t u16 = valueNode->datum.i;
      if (u16 == valueNode->datum.i) {
        int16_t i16 = valueNode->datum.i;
        if (i16 == valueNode->datum.i) {
          valueNode->node.resType.type = TSDB_DATA_TYPE_SMALLINT;
H
Hongze Cheng 已提交
288
          *(int16_t *)&valueNode->typeData = i16;
289 290
        } else {
          valueNode->node.resType.type = TSDB_DATA_TYPE_USMALLINT;
H
Hongze Cheng 已提交
291
          *(uint16_t *)&valueNode->typeData = u16;
292 293 294 295 296 297 298 299
        }
        break;
      }
      uint32_t u32 = valueNode->datum.i;
      if (u32 == valueNode->datum.i) {
        int32_t i32 = valueNode->datum.i;
        if (i32 == valueNode->datum.i) {
          valueNode->node.resType.type = TSDB_DATA_TYPE_INT;
H
Hongze Cheng 已提交
300
          *(int32_t *)&valueNode->typeData = i32;
301 302
        } else {
          valueNode->node.resType.type = TSDB_DATA_TYPE_UINT;
H
Hongze Cheng 已提交
303
          *(uint32_t *)&valueNode->typeData = u32;
304 305 306 307 308 309 310 311 312
        }
        break;
      }
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
      float f = valueNode->datum.d;
      if (FLT_EQUAL(f, valueNode->datum.d)) {
        valueNode->node.resType.type = TSDB_DATA_TYPE_FLOAT;
H
Hongze Cheng 已提交
313
        *(float *)&valueNode->typeData = f;
314 315 316 317 318 319 320 321 322
        break;
      }
      break;
    }
    default:
      break;
  }
}

H
Hongze Cheng 已提交
323
int32_t sclInitParam(SNode *node, SScalarParam *param, SScalarCtx *ctx, int32_t *rowNum) {
D
dapan1121 已提交
324
  switch (nodeType(node)) {
D
dapan1121 已提交
325
    case QUERY_NODE_LEFT_VALUE: {
H
Hongze Cheng 已提交
326
      SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, 0);
D
dapan1121 已提交
327 328 329
      param->numOfRows = pb->info.rows;
      break;
    }
D
dapan1121 已提交
330 331
    case QUERY_NODE_VALUE: {
      SValueNode *valueNode = (SValueNode *)node;
332

G
Ganlin Zhao 已提交
333 334 335 336
      if (param->columnData != NULL) {
        sclError("columnData should be NULL");
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }
337
      param->numOfRows = 1;
G
Ganlin Zhao 已提交
338 339 340 341
      int32_t code = sclCreateColumnInfoData(&valueNode->node.resType, 1, param);
      if (code != TSDB_CODE_SUCCESS) {
        SCL_RET(TSDB_CODE_OUT_OF_MEMORY);
      }
wmmhello's avatar
wmmhello 已提交
342
      if (TSDB_DATA_TYPE_NULL == valueNode->node.resType.type || valueNode->isNull) {
343
        colDataSetNULL(param->columnData, 0);
344
      } else {
345
        colDataSetVal(param->columnData, 0, nodesGetValueFromNode(valueNode), false);
D
dapan1121 已提交
346
      }
D
dapan1121 已提交
347 348
      break;
    }
D
dapan1121 已提交
349 350
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)node;
351 352
      if (LIST_LENGTH(nodeList->pNodeList) <= 0) {
        sclError("invalid length in nodeList, length:%d", LIST_LENGTH(nodeList->pNodeList));
D
dapan1121 已提交
353 354 355
        SCL_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan1121 已提交
356 357
      int32_t type = vectorGetConvertType(ctx->type.selfType, ctx->type.peerType);
      if (type == 0) {
358
        type = nodeList->node.resType.type;
D
dapan1121 已提交
359
      }
G
Ganlin Zhao 已提交
360

D
dapan1121 已提交
361 362
      SCL_ERR_RET(scalarGenerateSetFromList((void **)&param->pHashFilter, node, type));
      param->hashValueType = type;
D
dapan1121 已提交
363
      param->colAlloced = true;
D
dapan 已提交
364
      if (taosHashPut(ctx->pRes, &node, POINTER_BYTES, param, sizeof(*param))) {
365
        taosHashCleanup(param->pHashFilter);
D
dapan1121 已提交
366
        param->pHashFilter = NULL;
D
dapan 已提交
367
        sclError("taosHashPut nodeList failed, size:%d", (int32_t)sizeof(*param));
S
Shengliang Guan 已提交
368
        return TSDB_CODE_OUT_OF_MEMORY;
G
Ganlin Zhao 已提交
369
      }
D
dapan1121 已提交
370
      param->colAlloced = false;
D
dapan1121 已提交
371 372
      break;
    }
X
Xiaoyu Wang 已提交
373
    case QUERY_NODE_COLUMN: {
D
dapan 已提交
374 375
      if (NULL == ctx->pBlockList) {
        sclError("invalid node type for constant calculating, type:%d, src:%p", nodeType(node), ctx->pBlockList);
S
Shengliang Guan 已提交
376
        SCL_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
377
      }
G
Ganlin Zhao 已提交
378

X
Xiaoyu Wang 已提交
379
      SColumnNode *ref = (SColumnNode *)node;
380 381

      int32_t index = -1;
H
Hongze Cheng 已提交
382 383
      for (int32_t i = 0; i < taosArrayGetSize(ctx->pBlockList); ++i) {
        SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, i);
H
Haojun Liao 已提交
384
        if (pb->info.id.blockId == ref->dataBlockId) {
385 386 387 388 389 390
          index = i;
          break;
        }
      }

      if (index == -1) {
H
Hongze Cheng 已提交
391 392
        sclError("column tupleId is too big, tupleId:%d, dataBlockNum:%d", ref->dataBlockId,
                 (int32_t)taosArrayGetSize(ctx->pBlockList));
D
dapan 已提交
393 394 395
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

396
      SSDataBlock *block = *(SSDataBlock **)taosArrayGet(ctx->pBlockList, index);
G
Ganlin Zhao 已提交
397 398 399 400 401
      if (NULL == block) {
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

      if (ref->slotId >= taosArrayGetSize(block->pDataBlock)) {
H
Hongze Cheng 已提交
402 403
        sclError("column slotId is too big, slodId:%d, dataBlockSize:%d", ref->slotId,
                 (int32_t)taosArrayGetSize(block->pDataBlock));
D
dapan1121 已提交
404 405 406
        SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
      }

D
dapan 已提交
407
      SColumnInfoData *columnData = (SColumnInfoData *)taosArrayGet(block->pDataBlock, ref->slotId);
408
#if TAG_FILTER_DEBUG
H
Hongze Cheng 已提交
409 410
      qDebug("tagfilter column info, slotId:%d, colId:%d, type:%d", ref->slotId, columnData->info.colId,
             columnData->info.type);
411
#endif
412 413
      param->numOfRows = block->info.rows;
      param->columnData = columnData;
D
dapan1121 已提交
414 415
      break;
    }
416 417
    case QUERY_NODE_FUNCTION:
    case QUERY_NODE_OPERATOR:
D
dapan1121 已提交
418 419
    case QUERY_NODE_LOGIC_CONDITION:
    case QUERY_NODE_CASE_WHEN: {
D
dapan1121 已提交
420 421 422
      SScalarParam *res = (SScalarParam *)taosHashGet(ctx->pRes, &node, POINTER_BYTES);
      if (NULL == res) {
        sclError("no result for node, type:%d, node:%p", nodeType(node), node);
S
Shengliang Guan 已提交
423
        SCL_ERR_RET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
424 425
      }
      *param = *res;
D
dapan1121 已提交
426
      param->colAlloced = false;
D
dapan1121 已提交
427 428
      break;
    }
429 430
    default:
      break;
D
dapan1121 已提交
431 432
  }

433 434 435
  if (param->numOfRows > *rowNum) {
    if ((1 != param->numOfRows) && (1 < *rowNum)) {
      sclError("different row nums, rowNum:%d, newRowNum:%d", *rowNum, param->numOfRows);
D
dapan1121 已提交
436 437
      SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
    }
G
Ganlin Zhao 已提交
438

439
    *rowNum = param->numOfRows;
D
dapan1121 已提交
440 441
  }

442
  param->param = ctx->param;
D
dapan1121 已提交
443 444 445
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
446 447
int32_t sclInitParamList(SScalarParam **pParams, SNodeList *pParamList, SScalarCtx *ctx, int32_t *paramNum,
                         int32_t *rowNum) {
D
dapan1121 已提交
448
  int32_t code = 0;
D
dapan1121 已提交
449 450
  if (NULL == pParamList) {
    if (ctx->pBlockList) {
D
dapan1121 已提交
451
      SSDataBlock *pBlock = taosArrayGetP(ctx->pBlockList, 0);
D
dapan1121 已提交
452 453 454 455 456
      *rowNum = pBlock->info.rows;
    } else {
      *rowNum = 1;
    }

D
dapan 已提交
457
    *paramNum = 1;
D
dapan1121 已提交
458
  } else {
D
dapan 已提交
459
    *paramNum = pParamList->length;
D
dapan1121 已提交
460 461
  }

D
dapan 已提交
462
  SScalarParam *paramList = taosMemoryCalloc(*paramNum, sizeof(SScalarParam));
D
dapan1121 已提交
463
  if (NULL == paramList) {
D
dapan 已提交
464
    sclError("calloc %d failed", (int32_t)((*paramNum) * sizeof(SScalarParam)));
S
Shengliang Guan 已提交
465
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
466 467
  }

D
dapan1121 已提交
468
  if (pParamList) {
H
Hongze Cheng 已提交
469
    SNode  *tnode = NULL;
D
dapan1121 已提交
470 471
    int32_t i = 0;
    if (SCL_IS_CONST_CALC(ctx)) {
H
Hongze Cheng 已提交
472
      WHERE_EACH(tnode, pParamList) {
D
dapan1121 已提交
473
        if (!SCL_IS_CONST_NODE(tnode)) {
D
dapan 已提交
474
          WHERE_NEXT;
D
dapan1121 已提交
475 476 477 478
        } else {
          SCL_ERR_JRET(sclInitParam(tnode, &paramList[i], ctx, rowNum));
          ERASE_NODE(pParamList);
        }
G
Ganlin Zhao 已提交
479

D
dapan1121 已提交
480 481 482
        ++i;
      }
    } else {
G
Ganlin Zhao 已提交
483
      FOREACH(tnode, pParamList) {
D
dapan1121 已提交
484 485 486
        SCL_ERR_JRET(sclInitParam(tnode, &paramList[i], ctx, rowNum));
        ++i;
      }
D
dapan1121 已提交
487
    }
D
dapan1121 已提交
488 489 490
  } else {
    paramList[0].numOfRows = *rowNum;
  }
D
dapan1121 已提交
491

D
dapan1121 已提交
492
  if (0 == *rowNum) {
G
Ganlin Zhao 已提交
493
    taosMemoryFreeClear(paramList);
D
dapan1121 已提交
494
  }
D
dapan1121 已提交
495

D
dapan1121 已提交
496
  *pParams = paramList;
D
dapan1121 已提交
497
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
498

D
dapan1121 已提交
499
_return:
wafwerar's avatar
wafwerar 已提交
500
  taosMemoryFreeClear(paramList);
D
dapan1121 已提交
501 502 503
  SCL_RET(code);
}

D
dapan1121 已提交
504 505 506 507
int32_t sclGetNodeType(SNode *pNode, SScalarCtx *ctx) {
  if (NULL == pNode) {
    return -1;
  }
G
Ganlin Zhao 已提交
508

wafwerar's avatar
wafwerar 已提交
509
  switch ((int)nodeType(pNode)) {
D
dapan1121 已提交
510 511 512 513 514 515
    case QUERY_NODE_VALUE: {
      SValueNode *valueNode = (SValueNode *)pNode;
      return valueNode->node.resType.type;
    }
    case QUERY_NODE_NODE_LIST: {
      SNodeListNode *nodeList = (SNodeListNode *)pNode;
516
      return nodeList->node.resType.type;
D
dapan1121 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    }
    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 已提交
543 544
int32_t sclInitOperatorParams(SScalarParam **pParams, SOperatorNode *node, SScalarCtx *ctx, int32_t *rowNum) {
  int32_t code = 0;
D
dapan1121 已提交
545
  int32_t paramNum = scalarGetOperatorParamNum(node->opType);
D
dapan1121 已提交
546 547 548 549
  if (NULL == node->pLeft || (paramNum == 2 && NULL == node->pRight)) {
    sclError("invalid operation node, left:%p, right:%p", node->pLeft, node->pRight);
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }
G
Ganlin Zhao 已提交
550

wafwerar's avatar
wafwerar 已提交
551
  SScalarParam *paramList = taosMemoryCalloc(paramNum, sizeof(SScalarParam));
D
dapan1121 已提交
552 553
  if (NULL == paramList) {
    sclError("calloc %d failed", (int32_t)(paramNum * sizeof(SScalarParam)));
S
Shengliang Guan 已提交
554
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
555 556
  }

D
dapan1121 已提交
557 558
  sclSetOperatorValueType(node, ctx);

D
dapan1121 已提交
559
  SCL_ERR_JRET(sclInitParam(node->pLeft, &paramList[0], ctx, rowNum));
D
dapan1121 已提交
560
  if (paramNum > 1) {
D
dapan1121 已提交
561
    TSWAP(ctx->type.selfType, ctx->type.peerType);
D
dapan1121 已提交
562
    SCL_ERR_JRET(sclInitParam(node->pRight, &paramList[1], ctx, rowNum));
D
dapan1121 已提交
563 564
  }

D
dapan1121 已提交
565
  *pParams = paramList;
D
dapan1121 已提交
566
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
567 568

_return:
wafwerar's avatar
wafwerar 已提交
569
  taosMemoryFreeClear(paramList);
D
dapan1121 已提交
570
  SCL_RET(code);
D
dapan1121 已提交
571 572
}

X
Xiaoyu Wang 已提交
573
int32_t sclGetNodeRes(SNode *node, SScalarCtx *ctx, SScalarParam **res) {
D
dapan1121 已提交
574 575 576 577 578 579 580 581 582
  if (NULL == node) {
    return TSDB_CODE_SUCCESS;
  }

  int32_t rowNum = 0;
  *res = taosMemoryCalloc(1, sizeof(**res));
  if (NULL == *res) {
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
  }
X
Xiaoyu Wang 已提交
583

D
dapan1121 已提交
584 585 586 587 588
  SCL_ERR_RET(sclInitParam(node, *res, ctx, &rowNum));

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
589 590 591 592 593 594 595 596 597 598 599 600
int32_t sclWalkCaseWhenList(SScalarCtx *ctx, SNodeList *pList, struct SListCell *pCell, SScalarParam *pCase,
                            SScalarParam *pElse, SScalarParam *pComp, SScalarParam *output, int32_t rowIdx,
                            int32_t totalRows, bool *complete) {
  SNode         *node = NULL;
  SWhenThenNode *pWhenThen = NULL;
  SScalarParam  *pWhen = NULL;
  SScalarParam  *pThen = NULL;
  int32_t        code = 0;

  for (SListCell *cell = pCell; (NULL != cell ? (node = cell->pNode, true) : (node = NULL, false));
       cell = cell->pNext) {
    pWhenThen = (SWhenThenNode *)node;
D
dapan1121 已提交
601 602 603

    SCL_ERR_RET(sclGetNodeRes(pWhenThen->pWhen, ctx, &pWhen));
    SCL_ERR_RET(sclGetNodeRes(pWhenThen->pThen, ctx, &pThen));
X
Xiaoyu Wang 已提交
604

D
dapan1121 已提交
605
    vectorCompareImpl(pCase, pWhen, pComp, rowIdx, 1, TSDB_ORDER_ASC, OP_TYPE_EQUAL);
X
Xiaoyu Wang 已提交
606 607

    bool *equal = (bool *)colDataGetData(pComp->columnData, rowIdx);
D
dapan1121 已提交
608
    if (*equal) {
dengyihao's avatar
dengyihao 已提交
609
      bool  isNull = colDataIsNull_s(pThen->columnData, (pThen->numOfRows > 1 ? rowIdx : 0));
D
dapan1121 已提交
610
      char *pData = isNull ? NULL : colDataGetData(pThen->columnData, (pThen->numOfRows > 1 ? rowIdx : 0));
611
      colDataSetVal(output->columnData, rowIdx, pData, isNull);
D
dapan1121 已提交
612 613 614 615 616

      if (0 == rowIdx && 1 == pCase->numOfRows && 1 == pWhen->numOfRows && 1 == pThen->numOfRows && totalRows > 1) {
        SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
        *complete = true;
      }
X
Xiaoyu Wang 已提交
617

D
dapan1121 已提交
618
      goto _return;
D
dapan1121 已提交
619 620 621 622
    }
  }

  if (pElse) {
dengyihao's avatar
dengyihao 已提交
623
    bool  isNull = colDataIsNull_s(pElse->columnData, (pElse->numOfRows > 1 ? rowIdx : 0));
D
dapan1121 已提交
624
    char *pData = isNull ? NULL : colDataGetData(pElse->columnData, (pElse->numOfRows > 1 ? rowIdx : 0));
625
    colDataSetVal(output->columnData, rowIdx, pData, isNull);
D
dapan1121 已提交
626 627 628 629 630

    if (0 == rowIdx && 1 == pCase->numOfRows && 1 == pElse->numOfRows && totalRows > 1) {
      SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
      *complete = true;
    }
X
Xiaoyu Wang 已提交
631

D
dapan1121 已提交
632
    goto _return;
D
dapan1121 已提交
633 634
  }

635
  colDataSetVal(output->columnData, rowIdx, NULL, true);
D
dapan1121 已提交
636

D
dapan1121 已提交
637
  if (0 == rowIdx && 1 == pCase->numOfRows && totalRows > 1) {
D
dapan1121 已提交
638 639 640 641 642
    SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
    *complete = true;
  }

_return:
X
Xiaoyu Wang 已提交
643

D
dapan1121 已提交
644 645
  sclFreeParam(pWhen);
  sclFreeParam(pThen);
D
dapan1121 已提交
646 647
  taosMemoryFree(pWhen);
  taosMemoryFree(pThen);
D
dapan1121 已提交
648 649

  SCL_RET(code);
D
dapan1121 已提交
650 651
}

X
Xiaoyu Wang 已提交
652 653 654 655 656 657 658
int32_t sclWalkWhenList(SScalarCtx *ctx, SNodeList *pList, struct SListCell *pCell, SScalarParam *pElse,
                        SScalarParam *output, int32_t rowIdx, int32_t totalRows, bool *complete, bool preSingle) {
  SNode         *node = NULL;
  SWhenThenNode *pWhenThen = NULL;
  SScalarParam  *pWhen = NULL;
  SScalarParam  *pThen = NULL;
  int32_t        code = 0;
D
dapan1121 已提交
659

X
Xiaoyu Wang 已提交
660 661 662
  for (SListCell *cell = pCell; (NULL != cell ? (node = cell->pNode, true) : (node = NULL, false));
       cell = cell->pNext) {
    pWhenThen = (SWhenThenNode *)node;
D
dapan1121 已提交
663 664
    pWhen = NULL;
    pThen = NULL;
X
Xiaoyu Wang 已提交
665

D
dapan1121 已提交
666 667 668
    SCL_ERR_JRET(sclGetNodeRes(pWhenThen->pWhen, ctx, &pWhen));
    SCL_ERR_JRET(sclGetNodeRes(pWhenThen->pThen, ctx, &pThen));

X
Xiaoyu Wang 已提交
669 670
    bool *whenValue = (bool *)colDataGetData(pWhen->columnData, (pWhen->numOfRows > 1 ? rowIdx : 0));

D
dapan1121 已提交
671
    if (*whenValue) {
dengyihao's avatar
dengyihao 已提交
672
      bool  isNull = colDataIsNull_s(pThen->columnData, (pThen->numOfRows > 1 ? rowIdx : 0));
D
dapan1121 已提交
673
      char *pData = isNull ? NULL : colDataGetData(pThen->columnData, (pThen->numOfRows > 1 ? rowIdx : 0));
674
      colDataSetVal(output->columnData, rowIdx, pData, isNull);
D
dapan1121 已提交
675

D
dapan1121 已提交
676
      if (preSingle && 0 == rowIdx && 1 == pWhen->numOfRows && 1 == pThen->numOfRows && totalRows > 1) {
D
dapan1121 已提交
677
        SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
D
dapan1121 已提交
678
        *complete = true;
D
dapan1121 已提交
679
      }
X
Xiaoyu Wang 已提交
680

D
dapan1121 已提交
681 682 683 684 685
      goto _return;
    }

    sclFreeParam(pWhen);
    sclFreeParam(pThen);
D
dapan1121 已提交
686 687
    taosMemoryFreeClear(pWhen);
    taosMemoryFreeClear(pThen);
D
dapan1121 已提交
688 689 690
  }

  if (pElse) {
dengyihao's avatar
dengyihao 已提交
691
    bool  isNull = colDataIsNull_s(pElse->columnData, (pElse->numOfRows > 1 ? rowIdx : 0));
D
dapan1121 已提交
692
    char *pData = isNull ? NULL : colDataGetData(pElse->columnData, (pElse->numOfRows > 1 ? rowIdx : 0));
693
    colDataSetVal(output->columnData, rowIdx, pData, isNull);
D
dapan1121 已提交
694

D
dapan1121 已提交
695
    if (preSingle && 0 == rowIdx && 1 == pElse->numOfRows && totalRows > 1) {
D
dapan1121 已提交
696
      SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
D
dapan1121 已提交
697
      *complete = true;
D
dapan1121 已提交
698
    }
X
Xiaoyu Wang 已提交
699

D
dapan1121 已提交
700 701 702
    goto _return;
  }

703
  colDataSetVal(output->columnData, rowIdx, NULL, true);
D
dapan1121 已提交
704

D
dapan1121 已提交
705
  if (preSingle && 0 == rowIdx && totalRows > 1) {
D
dapan1121 已提交
706
    SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
D
dapan1121 已提交
707
    *complete = true;
D
dapan1121 已提交
708 709
  }

D
dapan1121 已提交
710 711 712 713
_return:

  sclFreeParam(pWhen);
  sclFreeParam(pThen);
D
dapan1121 已提交
714 715
  taosMemoryFree(pWhen);
  taosMemoryFree(pThen);
D
dapan1121 已提交
716

D
dapan1121 已提交
717
  SCL_RET(code);
D
dapan1121 已提交
718 719
}

720
int32_t sclExecFunction(SFunctionNode *node, SScalarCtx *ctx, SScalarParam *output) {
D
dapan1121 已提交
721
  SScalarParam *params = NULL;
H
Hongze Cheng 已提交
722 723 724
  int32_t       rowNum = 0;
  int32_t       paramNum = 0;
  int32_t       code = 0;
D
dapan 已提交
725
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &paramNum, &rowNum));
D
dapan1121 已提交
726

D
dapan1121 已提交
727
  if (fmIsUserDefinedFunc(node->funcId)) {
728
    code = callUdfScalarFunc(node->functionName, params, paramNum, output);
729 730 731 732
    if (code != 0) {
      sclError("fmExecFunction error. callUdfScalarFunc. function name: %s, udf code:%d", node->functionName, code);
      goto _return;
    }
D
dapan1121 已提交
733 734 735 736 737 738 739
  } else {
    SScalarFuncExecFuncs ffpSet = {0};
    code = fmGetScalarFuncExecFuncs(node->funcId, &ffpSet);
    if (code) {
      sclError("fmGetFuncExecFuncs failed, funcId:%d, code:%s", node->funcId, tstrerror(code));
      SCL_ERR_JRET(code);
    }
G
Ganlin Zhao 已提交
740

H
Haojun Liao 已提交
741 742 743
    code = sclCreateColumnInfoData(&node->node.resType, rowNum, output);
    if (code != TSDB_CODE_SUCCESS) {
      SCL_ERR_JRET(code);
D
dapan1121 已提交
744 745
    }

746 747 748 749
    if (rowNum == 0) {
      goto _return;
    }

D
dapan1121 已提交
750 751 752 753 754
    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 已提交
755 756 757 758
  }

_return:

D
dapan1121 已提交
759
  sclFreeParamList(params, paramNum);
D
dapan1121 已提交
760 761 762 763 764
  SCL_RET(code);
}

int32_t sclExecLogic(SLogicConditionNode *node, SScalarCtx *ctx, SScalarParam *output) {
  if (NULL == node->pParameterList || node->pParameterList->length <= 0) {
H
Hongze Cheng 已提交
765 766
    sclError("invalid logic parameter list, list:%p, paramNum:%d", node->pParameterList,
             node->pParameterList ? node->pParameterList->length : 0);
D
dapan1121 已提交
767 768 769 770 771 772
    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 已提交
773 774
  }

D
dapan1121 已提交
775 776 777 778 779 780
  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;
H
Hongze Cheng 已提交
781 782 783
  int32_t       rowNum = 0;
  int32_t       paramNum = 0;
  int32_t       code = 0;
D
dapan 已提交
784
  SCL_ERR_RET(sclInitParamList(&params, node->pParameterList, ctx, &paramNum, &rowNum));
D
dapan1121 已提交
785 786 787 788
  if (NULL == params) {
    output->numOfRows = 0;
    return TSDB_CODE_SUCCESS;
  }
D
dapan1121 已提交
789

790 791 792 793
  int32_t type = node->node.resType.type;
  output->numOfRows = rowNum;

  SDataType t = {.type = type, .bytes = tDataTypes[type].bytes};
H
Haojun Liao 已提交
794 795 796
  code = sclCreateColumnInfoData(&t, rowNum, output);
  if (code != TSDB_CODE_SUCCESS) {
    SCL_ERR_JRET(code);
D
dapan1121 已提交
797
  }
D
dapan1121 已提交
798

799 800
  int32_t numOfQualified = 0;

D
dapan1121 已提交
801
  bool value = false;
D
dapan 已提交
802
  bool complete = true;
D
dapan1121 已提交
803
  for (int32_t i = 0; i < rowNum; ++i) {
D
dapan 已提交
804 805
    complete = true;
    for (int32_t m = 0; m < paramNum; ++m) {
D
dapan1121 已提交
806
      if (NULL == params[m].columnData) {
D
dapan 已提交
807
        complete = false;
D
dapan1121 已提交
808 809
        continue;
      }
H
Hongze Cheng 已提交
810
      char *p = colDataGetData(params[m].columnData, i);
811 812
      GET_TYPED_DATA(value, bool, params[m].columnData->info.type, p);

D
dapan1121 已提交
813
      if (LOGIC_COND_TYPE_AND == node->condType && (false == value)) {
D
dapan1121 已提交
814
        complete = true;
D
dapan1121 已提交
815 816
        break;
      } else if (LOGIC_COND_TYPE_OR == node->condType && value) {
D
dapan1121 已提交
817
        complete = true;
D
dapan1121 已提交
818 819 820 821 822 823
        break;
      } else if (LOGIC_COND_TYPE_NOT == node->condType) {
        value = !value;
      }
    }

D
dapan 已提交
824
    if (complete) {
825
      colDataSetVal(output->columnData, i, (char *)&value, false);
826 827 828
      if (value) {
        numOfQualified++;
      }
D
dapan 已提交
829
    }
D
dapan1121 已提交
830 831
  }

D
dapan1121 已提交
832 833 834 835 836
  if (SCL_IS_CONST_CALC(ctx) && (false == complete)) {
    sclFreeParam(output);
    output->numOfRows = 0;
  }

837
  output->numOfQualified = numOfQualified;
D
dapan1121 已提交
838

839
_return:
D
dapan1121 已提交
840
  sclFreeParamList(params, paramNum);
D
dapan1121 已提交
841
  SCL_RET(code);
D
dapan1121 已提交
842 843 844 845
}

int32_t sclExecOperator(SOperatorNode *node, SScalarCtx *ctx, SScalarParam *output) {
  SScalarParam *params = NULL;
H
Hongze Cheng 已提交
846 847
  int32_t       rowNum = 0;
  int32_t       code = 0;
848

wmmhello's avatar
wmmhello 已提交
849
  // json not support in in operator
H
Haojun Liao 已提交
850
  if (nodeType(node->pLeft) == QUERY_NODE_VALUE) {
wmmhello's avatar
wmmhello 已提交
851
    SValueNode *valueNode = (SValueNode *)node->pLeft;
H
Hongze Cheng 已提交
852 853
    if (valueNode->node.resType.type == TSDB_DATA_TYPE_JSON &&
        (node->opType == OP_TYPE_IN || node->opType == OP_TYPE_NOT_IN)) {
wmmhello's avatar
wmmhello 已提交
854 855 856 857
      SCL_RET(TSDB_CODE_QRY_JSON_IN_ERROR);
    }
  }

D
dapan1121 已提交
858
  SCL_ERR_RET(sclInitOperatorParams(&params, node, ctx, &rowNum));
859
  if (output->columnData == NULL) {
H
Haojun Liao 已提交
860 861 862 863
    code = sclCreateColumnInfoData(&node->node.resType, rowNum, output);
    if (code != TSDB_CODE_SUCCESS) {
      SCL_ERR_JRET(code);
    }
D
dapan1121 已提交
864 865 866 867
  }

  _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(node->opType);

H
Hongze Cheng 已提交
868 869 870
  int32_t       paramNum = scalarGetOperatorParamNum(node->opType);
  SScalarParam *pLeft = &params[0];
  SScalarParam *pRight = paramNum > 1 ? &params[1] : NULL;
871

wmmhello's avatar
wmmhello 已提交
872
  terrno = TSDB_CODE_SUCCESS;
D
dapan 已提交
873
  OperatorFn(pLeft, pRight, output, TSDB_ORDER_ASC);
wmmhello's avatar
wmmhello 已提交
874
  code = terrno;
D
dapan1121 已提交
875 876

_return:
D
dapan1121 已提交
877

D
dapan1121 已提交
878
  sclFreeParamList(params, paramNum);
D
dapan1121 已提交
879
  SCL_RET(code);
D
dapan1121 已提交
880 881
}

D
dapan1121 已提交
882
int32_t sclExecCaseWhen(SCaseWhenNode *node, SScalarCtx *ctx, SScalarParam *output) {
X
Xiaoyu Wang 已提交
883
  int32_t       code = 0;
D
dapan1121 已提交
884 885
  SScalarParam *pCase = NULL;
  SScalarParam *pElse = NULL;
D
dapan1121 已提交
886 887
  SScalarParam *pWhen = NULL;
  SScalarParam *pThen = NULL;
D
dapan1121 已提交
888
  SScalarParam  comp = {0};
X
Xiaoyu Wang 已提交
889 890
  int32_t       rowNum = 1;
  bool          complete = false;
D
dapan1121 已提交
891 892 893 894 895 896 897

  if (NULL == node->pWhenThenList || node->pWhenThenList->length <= 0) {
    sclError("invalid whenThen list");
    SCL_ERR_RET(TSDB_CODE_INVALID_PARA);
  }

  if (ctx->pBlockList) {
X
Xiaoyu Wang 已提交
898
    SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, 0);
D
dapan1121 已提交
899
    rowNum = pb->info.rows;
D
dapan1121 已提交
900
    output->numOfRows = pb->info.rows;
D
dapan1121 已提交
901
  }
D
dapan1121 已提交
902 903

  SCL_ERR_JRET(sclCreateColumnInfoData(&node->node.resType, rowNum, output));
X
Xiaoyu Wang 已提交
904

D
dapan1121 已提交
905 906 907 908 909 910
  SCL_ERR_JRET(sclGetNodeRes(node->pCase, ctx, &pCase));
  SCL_ERR_JRET(sclGetNodeRes(node->pElse, ctx, &pElse));

  SDataType compType = {0};
  compType.type = TSDB_DATA_TYPE_BOOL;
  compType.bytes = tDataTypes[compType.type].bytes;
X
Xiaoyu Wang 已提交
911

D
dapan1121 已提交
912
  SCL_ERR_JRET(sclCreateColumnInfoData(&compType, rowNum, &comp));
D
dapan1121 已提交
913

X
Xiaoyu Wang 已提交
914 915
  SNode         *tnode = NULL;
  SWhenThenNode *pWhenThen = (SWhenThenNode *)node->pWhenThenList->pHead->pNode;
D
dapan1121 已提交
916 917 918

  SCL_ERR_JRET(sclGetNodeRes(pWhenThen->pWhen, ctx, &pWhen));
  SCL_ERR_JRET(sclGetNodeRes(pWhenThen->pThen, ctx, &pThen));
D
dapan1121 已提交
919 920 921 922
  if (NULL == pWhen || NULL == pThen) {
    sclError("invalid when/then in whenThen list");
    SCL_ERR_JRET(TSDB_CODE_INVALID_PARA);
  }
D
dapan1121 已提交
923 924

  if (pCase) {
D
dapan1121 已提交
925
    vectorCompare(pCase, pWhen, &comp, TSDB_ORDER_ASC, OP_TYPE_EQUAL);
X
Xiaoyu Wang 已提交
926

D
dapan1121 已提交
927
    for (int32_t i = 0; i < rowNum; ++i) {
X
Xiaoyu Wang 已提交
928
      bool *equal = (bool *)colDataGetData(comp.columnData, (comp.numOfRows > 1 ? i : 0));
D
dapan1121 已提交
929
      if (*equal) {
930
        colDataSetVal(output->columnData, i, colDataGetData(pThen->columnData, (pThen->numOfRows > 1 ? i : 0)),
X
Xiaoyu Wang 已提交
931
                      colDataIsNull_s(pThen->columnData, (pThen->numOfRows > 1 ? i : 0)));
D
dapan1121 已提交
932
        if (0 == i && 1 == pCase->numOfRows && 1 == pWhen->numOfRows && 1 == pThen->numOfRows && rowNum > 1) {
D
dapan1121 已提交
933 934 935
          SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
          break;
        }
D
dapan1121 已提交
936
      } else {
X
Xiaoyu Wang 已提交
937 938
        SCL_ERR_JRET(sclWalkCaseWhenList(ctx, node->pWhenThenList, node->pWhenThenList->pHead->pNext, pCase, pElse,
                                         &comp, output, i, rowNum, &complete));
D
dapan1121 已提交
939 940 941
        if (complete) {
          break;
        }
D
dapan1121 已提交
942
      }
D
dapan1121 已提交
943
    }
D
dapan1121 已提交
944 945
  } else {
    for (int32_t i = 0; i < rowNum; ++i) {
X
Xiaoyu Wang 已提交
946
      bool *whenValue = (bool *)colDataGetData(pWhen->columnData, (pWhen->numOfRows > 1 ? i : 0));
D
dapan1121 已提交
947
      if (*whenValue) {
948
        colDataSetVal(output->columnData, i, colDataGetData(pThen->columnData, (pThen->numOfRows > 1 ? i : 0)),
X
Xiaoyu Wang 已提交
949
                      colDataIsNull_s(pThen->columnData, (pThen->numOfRows > 1 ? i : 0)));
D
dapan1121 已提交
950 951 952 953
        if (0 == i && 1 == pWhen->numOfRows && 1 == pThen->numOfRows && rowNum > 1) {
          SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
          break;
        }
D
dapan1121 已提交
954
      } else {
X
Xiaoyu Wang 已提交
955 956
        SCL_ERR_JRET(sclWalkWhenList(ctx, node->pWhenThenList, node->pWhenThenList->pHead->pNext, pElse, output, i,
                                     rowNum, &complete, (pWhen->numOfRows == 1 && pThen->numOfRows == 1)));
D
dapan1121 已提交
957 958 959
        if (complete) {
          break;
        }
D
dapan1121 已提交
960
      }
D
dapan1121 已提交
961 962 963
    }
  }

D
dapan1121 已提交
964 965
  sclFreeParam(pCase);
  sclFreeParam(pElse);
D
dapan1121 已提交
966
  sclFreeParam(&comp);
D
dapan1121 已提交
967 968
  sclFreeParam(pWhen);
  sclFreeParam(pThen);
D
dapan1121 已提交
969 970 971 972
  taosMemoryFree(pCase);
  taosMemoryFree(pElse);
  taosMemoryFree(pWhen);
  taosMemoryFree(pThen);
D
dapan1121 已提交
973

D
dapan1121 已提交
974
  return TSDB_CODE_SUCCESS;
D
dapan1121 已提交
975 976 977

_return:

D
dapan1121 已提交
978 979
  sclFreeParam(pCase);
  sclFreeParam(pElse);
D
dapan1121 已提交
980
  sclFreeParam(&comp);
D
dapan1121 已提交
981 982 983
  sclFreeParam(pWhen);
  sclFreeParam(pThen);
  sclFreeParam(output);
D
dapan1121 已提交
984 985 986 987
  taosMemoryFree(pCase);
  taosMemoryFree(pElse);
  taosMemoryFree(pWhen);
  taosMemoryFree(pThen);
D
dapan1121 已提交
988

D
dapan1121 已提交
989 990 991
  SCL_RET(code);
}

H
Hongze Cheng 已提交
992
EDealRes sclRewriteNullInOptr(SNode **pNode, SScalarCtx *ctx, EOperatorType opType) {
D
dapan1121 已提交
993 994 995
  if (opType <= OP_TYPE_CALC_MAX) {
    SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
    if (NULL == res) {
G
Ganlin Zhao 已提交
996
      sclError("make value node failed");
S
Shengliang Guan 已提交
997
      ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
998 999
      return DEAL_RES_ERROR;
    }
G
Ganlin Zhao 已提交
1000

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

D
dapan1121 已提交
1003
    nodesDestroyNode(*pNode);
H
Hongze Cheng 已提交
1004
    *pNode = (SNode *)res;
D
dapan1121 已提交
1005 1006 1007
  } else {
    SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
    if (NULL == res) {
G
Ganlin Zhao 已提交
1008
      sclError("make value node failed");
S
Shengliang Guan 已提交
1009
      ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1010 1011
      return DEAL_RES_ERROR;
    }
G
Ganlin Zhao 已提交
1012

D
dapan1121 已提交
1013
    res->node.resType.type = TSDB_DATA_TYPE_BOOL;
D
dapan1121 已提交
1014
    res->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
D
dapan1121 已提交
1015
    res->datum.b = false;
G
Ganlin Zhao 已提交
1016

D
dapan1121 已提交
1017
    nodesDestroyNode(*pNode);
H
Hongze Cheng 已提交
1018
    *pNode = (SNode *)res;
D
dapan1121 已提交
1019 1020 1021 1022 1023
  }

  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1024
EDealRes sclAggFuncWalker(SNode *pNode, void *pContext) {
D
dapan1121 已提交
1025
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
H
Hongze Cheng 已提交
1026 1027 1028
    SFunctionNode *pFunc = (SFunctionNode *)pNode;
    *(bool *)pContext = fmIsAggFunc(pFunc->funcId);
    if (*(bool *)pContext) {
D
dapan1121 已提交
1029 1030 1031 1032 1033 1034 1035
      return DEAL_RES_END;
    }
  }

  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1036
bool sclContainsAggFuncNode(SNode *pNode) {
D
dapan1121 已提交
1037 1038 1039 1040
  bool aggFunc = false;
  nodesWalkExpr(pNode, sclAggFuncWalker, (void *)&aggFunc);
  return aggFunc;
}
D
dapan1121 已提交
1041

1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
static uint8_t sclGetOpValueNodeTsPrecision(SNode *pLeft, SNode *pRight) {
  uint8_t lPrec = ((SExprNode *)pLeft)->resType.precision;
  uint8_t rPrec = ((SExprNode *)pRight)->resType.precision;

  uint8_t lType = ((SExprNode *)pLeft)->resType.type;
  uint8_t rType = ((SExprNode *)pRight)->resType.type;

  if (TSDB_DATA_TYPE_TIMESTAMP == lType && TSDB_DATA_TYPE_TIMESTAMP == rType) {
    return TMAX(lPrec, rPrec);
  } else if (TSDB_DATA_TYPE_TIMESTAMP == lType && TSDB_DATA_TYPE_TIMESTAMP != rType) {
    return lPrec;
  } else if (TSDB_DATA_TYPE_TIMESTAMP == rType && TSDB_DATA_TYPE_TIMESTAMP != lType) {
    return rPrec;
  }
1056

1057
  return 0;
1058
}
1059 1060 1061

int32_t sclConvertOpValueNodeTs(SOperatorNode *node, SScalarCtx *ctx) {
  int32_t code = 0;
1062

D
dapan1121 已提交
1063
  if (node->pLeft && SCL_IS_VAR_VALUE_NODE(node->pLeft)) {
1064
    if (node->pRight && (TSDB_DATA_TYPE_TIMESTAMP == ((SExprNode *)node->pRight)->resType.type)) {
X
Xiaoyu Wang 已提交
1065 1066
      SCL_ERR_JRET(
          sclConvertToTsValueNode(sclGetOpValueNodeTsPrecision(node->pLeft, node->pRight), (SValueNode *)node->pLeft));
1067
    }
D
dapan1121 已提交
1068
  } else if (node->pRight && SCL_IS_NOTNULL_CONST_NODE(node->pRight)) {
1069 1070
    if (node->pLeft && (TSDB_DATA_TYPE_TIMESTAMP == ((SExprNode *)node->pLeft)->resType.type)) {
      if (SCL_IS_VAR_VALUE_NODE(node->pRight)) {
X
Xiaoyu Wang 已提交
1071 1072
        SCL_ERR_JRET(sclConvertToTsValueNode(sclGetOpValueNodeTsPrecision(node->pLeft, node->pRight),
                                             (SValueNode *)node->pRight));
D
dapan1121 已提交
1073
      } else if (QUERY_NODE_NODE_LIST == node->pRight->type) {
X
Xiaoyu Wang 已提交
1074 1075
        SNode *pNode;
        FOREACH(pNode, ((SNodeListNode *)node->pRight)->pNodeList) {
1076
          if (SCL_IS_VAR_VALUE_NODE(pNode)) {
X
Xiaoyu Wang 已提交
1077 1078
            SCL_ERR_JRET(
                sclConvertToTsValueNode(sclGetOpValueNodeTsPrecision(node->pLeft, pNode), (SValueNode *)pNode));
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
          }
        }
      }
    }
  }

  return TSDB_CODE_SUCCESS;

_return:

  ctx->code = code;
  return DEAL_RES_ERROR;
}

int32_t sclConvertCaseWhenValueNodeTs(SCaseWhenNode *node, SScalarCtx *ctx) {
  int32_t code = 0;

  if (NULL == node->pCase) {
    return TSDB_CODE_SUCCESS;
  }
1099

1100
  if (SCL_IS_VAR_VALUE_NODE(node->pCase)) {
X
Xiaoyu Wang 已提交
1101
    SNode *pNode;
1102 1103 1104
    FOREACH(pNode, node->pWhenThenList) {
      SExprNode *pExpr = (SExprNode *)((SWhenThenNode *)pNode)->pWhen;
      if (TSDB_DATA_TYPE_TIMESTAMP == pExpr->resType.type) {
X
Xiaoyu Wang 已提交
1105
        SCL_ERR_JRET(sclConvertToTsValueNode(pExpr->resType.precision, (SValueNode *)node->pCase));
1106 1107 1108 1109
        break;
      }
    }
  } else if (TSDB_DATA_TYPE_TIMESTAMP == ((SExprNode *)node->pCase)->resType.type) {
X
Xiaoyu Wang 已提交
1110
    SNode *pNode;
1111 1112
    FOREACH(pNode, node->pWhenThenList) {
      if (SCL_IS_VAR_VALUE_NODE(((SWhenThenNode *)pNode)->pWhen)) {
X
Xiaoyu Wang 已提交
1113 1114
        SCL_ERR_JRET(sclConvertToTsValueNode(((SExprNode *)node->pCase)->resType.precision,
                                             (SValueNode *)((SWhenThenNode *)pNode)->pWhen));
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
      }
    }
  }

  return TSDB_CODE_SUCCESS;

_return:

  ctx->code = code;
  return DEAL_RES_ERROR;
}

H
Hongze Cheng 已提交
1127
EDealRes sclRewriteNonConstOperator(SNode **pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1128
  SOperatorNode *node = (SOperatorNode *)*pNode;
H
Hongze Cheng 已提交
1129
  int32_t        code = 0;
D
dapan1121 已提交
1130 1131 1132

  if (node->pLeft && (QUERY_NODE_VALUE == nodeType(node->pLeft))) {
    SValueNode *valueNode = (SValueNode *)node->pLeft;
H
Hongze Cheng 已提交
1133 1134
    if (SCL_IS_NULL_VALUE_NODE(valueNode) && (node->opType != OP_TYPE_IS_NULL && node->opType != OP_TYPE_IS_NOT_NULL) &&
        (!sclContainsAggFuncNode(node->pRight))) {
D
dapan1121 已提交
1135
      return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
1136
    }
D
dapan 已提交
1137

1138 1139
    if (SCL_IS_COMPARISON_OPERATOR(node->opType) && SCL_DOWNGRADE_DATETYPE(valueNode->node.resType.type)) {
      sclDowngradeValueType(valueNode);
H
Hongze Cheng 已提交
1140
    }
D
dapan1121 已提交
1141 1142 1143 1144
  }

  if (node->pRight && (QUERY_NODE_VALUE == nodeType(node->pRight))) {
    SValueNode *valueNode = (SValueNode *)node->pRight;
H
Hongze Cheng 已提交
1145 1146
    if (SCL_IS_NULL_VALUE_NODE(valueNode) && (node->opType != OP_TYPE_IS_NULL && node->opType != OP_TYPE_IS_NOT_NULL) &&
        (!sclContainsAggFuncNode(node->pLeft))) {
D
dapan1121 已提交
1147
      return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
1148
    }
D
dapan 已提交
1149

1150 1151
    if (SCL_IS_COMPARISON_OPERATOR(node->opType) && SCL_DOWNGRADE_DATETYPE(valueNode->node.resType.type)) {
      sclDowngradeValueType(valueNode);
H
Hongze Cheng 已提交
1152
    }
D
dapan1121 已提交
1153 1154 1155 1156
  }

  if (node->pRight && (QUERY_NODE_NODE_LIST == nodeType(node->pRight))) {
    SNodeListNode *listNode = (SNodeListNode *)node->pRight;
H
Hongze Cheng 已提交
1157
    SNode         *tnode = NULL;
D
dapan1121 已提交
1158 1159 1160 1161 1162
    WHERE_EACH(tnode, listNode->pNodeList) {
      if (SCL_IS_NULL_VALUE_NODE(tnode)) {
        if (node->opType == OP_TYPE_IN) {
          ERASE_NODE(listNode->pNodeList);
          continue;
H
Hongze Cheng 已提交
1163
        } else {  // OP_TYPE_NOT_IN
D
dapan1121 已提交
1164
          return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
1165 1166 1167 1168 1169 1170 1171
        }
      }

      WHERE_NEXT;
    }

    if (listNode->pNodeList->length <= 0) {
D
dapan1121 已提交
1172
      return sclRewriteNullInOptr(pNode, ctx, node->opType);
D
dapan1121 已提交
1173 1174 1175 1176 1177 1178
    }
  }

  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1179
EDealRes sclRewriteFunction(SNode **pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1180
  SFunctionNode *node = (SFunctionNode *)*pNode;
H
Hongze Cheng 已提交
1181
  SNode         *tnode = NULL;
X
Xiaoyu Wang 已提交
1182
  if ((!fmIsScalarFunc(node->funcId) && (!ctx->dual)) || fmIsUserDefinedFunc(node->funcId)) {
G
Ganlin Zhao 已提交
1183 1184
    return DEAL_RES_CONTINUE;
  }
1185

D
dapan1121 已提交
1186 1187 1188 1189 1190 1191
  FOREACH(tnode, node->pParameterList) {
    if (!SCL_IS_CONST_NODE(tnode)) {
      return DEAL_RES_CONTINUE;
    }
  }

D
dapan1121 已提交
1192
  SScalarParam output = {0};
1193

1194
  ctx->code = sclExecFunction(node, ctx, &output);
D
dapan 已提交
1195
  if (ctx->code) {
D
dapan1121 已提交
1196 1197 1198
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
1199
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
1200 1201
  if (NULL == res) {
    sclError("make value node failed");
D
dapan1121 已提交
1202
    sclFreeParam(&output);
S
Shengliang Guan 已提交
1203
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1204 1205 1206
    return DEAL_RES_ERROR;
  }

1207 1208
  res->translate = true;

1209 1210 1211 1212
  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;
1213
  if (colDataIsNull_s(output.columnData, 0)) {
1214
    res->isNull = true;
D
dapan1121 已提交
1215
  } else {
1216
    int32_t type = output.columnData->info.type;
H
Hongze Cheng 已提交
1217
    if (type == TSDB_DATA_TYPE_JSON) {
wmmhello's avatar
wmmhello 已提交
1218 1219 1220 1221
      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)) {
H
Hongze Cheng 已提交
1222
      // res->datum.p = taosMemoryCalloc(res->node.resType.bytes + VARSTR_HEADER_SIZE + 1, 1);
1223
      res->datum.p = taosMemoryCalloc(varDataTLen(output.columnData->pData) + 1, 1);
1224
      res->node.resType.bytes = varDataTLen(output.columnData->pData);
1225
      memcpy(res->datum.p, output.columnData->pData, varDataTLen(output.columnData->pData));
1226
    } else {
D
dapan1121 已提交
1227
      nodesSetValueNodeValue(res, output.columnData->pData);
1228
    }
D
dapan1121 已提交
1229
  }
1230

D
dapan1121 已提交
1231
  nodesDestroyNode(*pNode);
H
Hongze Cheng 已提交
1232
  *pNode = (SNode *)res;
D
dapan1121 已提交
1233

D
dapan1121 已提交
1234
  sclFreeParam(&output);
D
dapan1121 已提交
1235 1236 1237
  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1238
EDealRes sclRewriteLogic(SNode **pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1239 1240
  SLogicConditionNode *node = (SLogicConditionNode *)*pNode;

H
Haojun Liao 已提交
1241
  SScalarParam output = {0};
D
dapan 已提交
1242 1243
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
D
dapan1121 已提交
1244 1245 1246
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
1247 1248 1249 1250
  if (0 == output.numOfRows) {
    return DEAL_RES_CONTINUE;
  }

D
dapan1121 已提交
1251
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
1252 1253
  if (NULL == res) {
    sclError("make value node failed");
1254
    sclFreeParam(&output);
S
Shengliang Guan 已提交
1255
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1256 1257 1258 1259
    return DEAL_RES_ERROR;
  }

  res->node.resType = node->node.resType;
1260
  res->translate = true;
D
dapan1121 已提交
1261

1262 1263 1264 1265
  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 已提交
1266
  } else {
D
dapan1121 已提交
1267
    nodesSetValueNodeValue(res, output.columnData->pData);
D
dapan1121 已提交
1268
  }
D
dapan1121 已提交
1269 1270

  nodesDestroyNode(*pNode);
H
Hongze Cheng 已提交
1271
  *pNode = (SNode *)res;
D
dapan1121 已提交
1272

D
dapan1121 已提交
1273
  sclFreeParam(&output);
D
dapan1121 已提交
1274 1275 1276
  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1277
EDealRes sclRewriteOperator(SNode **pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1278
  SOperatorNode *node = (SOperatorNode *)*pNode;
D
dapan1121 已提交
1279

1280 1281
  SCL_ERR_RET(sclConvertOpValueNodeTs(node, ctx));

D
dapan1121 已提交
1282
  if ((!SCL_IS_CONST_NODE(node->pLeft)) || (!SCL_IS_CONST_NODE(node->pRight))) {
D
dapan 已提交
1283
    return sclRewriteNonConstOperator(pNode, ctx);
D
dapan1121 已提交
1284 1285
  }

H
Haojun Liao 已提交
1286
  SScalarParam output = {0};
D
dapan 已提交
1287 1288
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
dengyihao's avatar
dengyihao 已提交
1289
    sclFreeParam(&output);
D
dapan1121 已提交
1290 1291 1292
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
1293
  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
D
dapan1121 已提交
1294
  if (NULL == res) {
1295 1296
    sclError("make value node failed");
    sclFreeParam(&output);
S
Shengliang Guan 已提交
1297
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1298 1299 1300
    return DEAL_RES_ERROR;
  }

1301
  res->translate = true;
D
dapan1121 已提交
1302

1303
  res->node.resType = node->node.resType;
1304
  if (colDataIsNull_s(output.columnData, 0)) {
1305 1306
    res->isNull = true;
    res->node.resType = node->node.resType;
1307
  } else {
1308 1309 1310 1311 1312
    int32_t type = output.columnData->info.type;
    if (IS_VAR_DATA_TYPE(type)) {  // todo refactor
      res->datum.p = output.columnData->pData;
      output.columnData->pData = NULL;
    } else {
G
Ganlin Zhao 已提交
1313
      nodesSetValueNodeValue(res, output.columnData->pData);
1314
    }
D
dapan1121 已提交
1315
  }
D
dapan1121 已提交
1316 1317

  nodesDestroyNode(*pNode);
H
Hongze Cheng 已提交
1318
  *pNode = (SNode *)res;
D
dapan1121 已提交
1319

H
Haojun Liao 已提交
1320
  sclFreeParam(&output);
D
dapan1121 已提交
1321 1322 1323
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1324
EDealRes sclRewriteCaseWhen(SNode **pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1325 1326
  SCaseWhenNode *node = (SCaseWhenNode *)*pNode;

1327 1328
  SCL_ERR_RET(sclConvertCaseWhenValueNodeTs(node, ctx));

D
dapan1121 已提交
1329 1330 1331 1332
  if ((!SCL_IS_CONST_NODE(node->pCase)) || (!SCL_IS_CONST_NODE(node->pElse))) {
    return DEAL_RES_CONTINUE;
  }

X
Xiaoyu Wang 已提交
1333
  SNode *tnode = NULL;
D
dapan1121 已提交
1334
  FOREACH(tnode, node->pWhenThenList) {
X
Xiaoyu Wang 已提交
1335
    SWhenThenNode *pWhenThen = (SWhenThenNode *)tnode;
D
dapan1121 已提交
1336
    if (!SCL_IS_CONST_NODE(pWhenThen->pWhen) || !SCL_IS_CONST_NODE(pWhenThen->pThen)) {
D
dapan1121 已提交
1337 1338
      return DEAL_RES_CONTINUE;
    }
X
Xiaoyu Wang 已提交
1339
  }
D
dapan1121 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

  SScalarParam output = {0};
  ctx->code = sclExecCaseWhen(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
  }

  SValueNode *res = (SValueNode *)nodesMakeNode(QUERY_NODE_VALUE);
  if (NULL == res) {
    sclError("make value node failed");
    sclFreeParam(&output);
S
Shengliang Guan 已提交
1351
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    return DEAL_RES_ERROR;
  }

  res->translate = true;

  res->node.resType = node->node.resType;
  if (colDataIsNull_s(output.columnData, 0)) {
    res->isNull = true;
    res->node.resType = node->node.resType;
  } else {
    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 {
      nodesSetValueNodeValue(res, output.columnData->pData);
    }
  }

  nodesDestroyNode(*pNode);
X
Xiaoyu Wang 已提交
1372
  *pNode = (SNode *)res;
D
dapan1121 已提交
1373 1374 1375 1376 1377

  sclFreeParam(&output);
  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1378
EDealRes sclConstantsRewriter(SNode **pNode, void *pContext) {
D
dapan 已提交
1379 1380
  SScalarCtx *ctx = (SScalarCtx *)pContext;

D
dapan1121 已提交
1381 1382 1383 1384
  if (QUERY_NODE_OPERATOR == nodeType(*pNode)) {
    return sclRewriteOperator(pNode, ctx);
  }

D
dapan1121 已提交
1385
  if (QUERY_NODE_FUNCTION == nodeType(*pNode)) {
D
dapan 已提交
1386
    return sclRewriteFunction(pNode, ctx);
D
dapan1121 已提交
1387 1388 1389
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pNode)) {
D
dapan 已提交
1390
    return sclRewriteLogic(pNode, ctx);
D
dapan1121 已提交
1391 1392
  }

D
dapan1121 已提交
1393
  if (QUERY_NODE_CASE_WHEN == nodeType(*pNode)) {
D
dapan1121 已提交
1394
    return sclRewriteCaseWhen(pNode, ctx);
1395
  }
1396 1397

  return DEAL_RES_CONTINUE;
D
dapan1121 已提交
1398 1399
}

H
Hongze Cheng 已提交
1400
EDealRes sclWalkFunction(SNode *pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1401
  SFunctionNode *node = (SFunctionNode *)pNode;
H
Hongze Cheng 已提交
1402
  SScalarParam   output = {0};
1403

1404
  ctx->code = sclExecFunction(node, ctx, &output);
D
dapan1121 已提交
1405 1406
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
1407 1408
  }

D
dapan1121 已提交
1409
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
S
Shengliang Guan 已提交
1410
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1411
    return DEAL_RES_ERROR;
D
dapan1121 已提交
1412 1413
  }

D
dapan1121 已提交
1414 1415 1416
  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1417
EDealRes sclWalkLogic(SNode *pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1418
  SLogicConditionNode *node = (SLogicConditionNode *)pNode;
H
Hongze Cheng 已提交
1419
  SScalarParam         output = {0};
1420

D
dapan1121 已提交
1421 1422 1423
  ctx->code = sclExecLogic(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
D
dapan1121 已提交
1424 1425
  }

D
dapan1121 已提交
1426
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
S
Shengliang Guan 已提交
1427
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1428
    return DEAL_RES_ERROR;
D
dapan1121 已提交
1429 1430 1431 1432 1433
  }

  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1434
EDealRes sclWalkOperator(SNode *pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1435
  SOperatorNode *node = (SOperatorNode *)pNode;
H
Hongze Cheng 已提交
1436
  SScalarParam   output = {0};
G
Ganlin Zhao 已提交
1437

D
dapan1121 已提交
1438 1439
  ctx->code = sclExecOperator(node, ctx, &output);
  if (ctx->code) {
dengyihao's avatar
dengyihao 已提交
1440
    sclFreeParam(&output);
D
dapan1121 已提交
1441 1442
    return DEAL_RES_ERROR;
  }
D
dapan1121 已提交
1443

D
dapan1121 已提交
1444
  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
S
Shengliang Guan 已提交
1445
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1446 1447 1448
    return DEAL_RES_ERROR;
  }

D
dapan1121 已提交
1449 1450 1451
  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1452
EDealRes sclWalkTarget(SNode *pNode, SScalarCtx *ctx) {
1453 1454 1455
  STargetNode *target = (STargetNode *)pNode;

  if (target->dataBlockId >= taosArrayGetSize(ctx->pBlockList)) {
H
Hongze Cheng 已提交
1456 1457
    sclError("target tupleId is too big, tupleId:%d, dataBlockNum:%d", target->dataBlockId,
             (int32_t)taosArrayGetSize(ctx->pBlockList));
1458 1459 1460 1461 1462
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

  int32_t index = -1;
H
Hongze Cheng 已提交
1463 1464
  for (int32_t i = 0; i < taosArrayGetSize(ctx->pBlockList); ++i) {
    SSDataBlock *pb = taosArrayGetP(ctx->pBlockList, i);
H
Haojun Liao 已提交
1465
    if (pb->info.id.blockId == target->dataBlockId) {
1466 1467 1468 1469 1470 1471
      index = i;
      break;
    }
  }

  if (index == -1) {
H
Hongze Cheng 已提交
1472 1473
    sclError("column tupleId is too big, tupleId:%d, dataBlockNum:%d", target->dataBlockId,
             (int32_t)taosArrayGetSize(ctx->pBlockList));
1474 1475 1476 1477 1478 1479 1480
    ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
    return DEAL_RES_ERROR;
  }

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

  if (target->slotId >= taosArrayGetSize(block->pDataBlock)) {
H
Hongze Cheng 已提交
1481 1482
    sclError("target slot not exist, dataBlockId:%d, slotId:%d, dataBlockNum:%d", target->dataBlockId, target->slotId,
             (int32_t)taosArrayGetSize(block->pDataBlock));
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
    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));
S
Shengliang Guan 已提交
1493
    ctx->code = TSDB_CODE_APP_ERROR;
1494 1495 1496
    return DEAL_RES_ERROR;
  }

1497
  colDataAssign(col, res->columnData, res->numOfRows, NULL);
1498 1499 1500 1501 1502 1503
  block->info.rows = res->numOfRows;

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

X
Xiaoyu Wang 已提交
1505
EDealRes sclWalkCaseWhen(SNode *pNode, SScalarCtx *ctx) {
D
dapan1121 已提交
1506
  SCaseWhenNode *node = (SCaseWhenNode *)pNode;
X
Xiaoyu Wang 已提交
1507
  SScalarParam   output = {0};
D
dapan1121 已提交
1508 1509 1510 1511 1512 1513 1514

  ctx->code = sclExecCaseWhen(node, ctx, &output);
  if (ctx->code) {
    return DEAL_RES_ERROR;
  }

  if (taosHashPut(ctx->pRes, &pNode, POINTER_BYTES, &output, sizeof(output))) {
S
Shengliang Guan 已提交
1515
    ctx->code = TSDB_CODE_OUT_OF_MEMORY;
D
dapan1121 已提交
1516 1517 1518 1519 1520 1521
    return DEAL_RES_ERROR;
  }

  return DEAL_RES_CONTINUE;
}

H
Hongze Cheng 已提交
1522
EDealRes sclCalcWalker(SNode *pNode, void *pContext) {
X
Xiaoyu Wang 已提交
1523 1524 1525
  if (QUERY_NODE_VALUE == nodeType(pNode) || QUERY_NODE_NODE_LIST == nodeType(pNode) ||
      QUERY_NODE_COLUMN == nodeType(pNode) || QUERY_NODE_LEFT_VALUE == nodeType(pNode) ||
      QUERY_NODE_WHEN_THEN == nodeType(pNode)) {
D
dapan1121 已提交
1526
    return DEAL_RES_CONTINUE;
D
dapan1121 已提交
1527
  }
D
dapan 已提交
1528 1529

  SScalarCtx *ctx = (SScalarCtx *)pContext;
D
dapan1121 已提交
1530 1531 1532 1533
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
    return sclWalkOperator(pNode, ctx);
  }

D
dapan1121 已提交
1534
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
D
dapan 已提交
1535
    return sclWalkFunction(pNode, ctx);
D
dapan1121 已提交
1536
  }
D
dapan1121 已提交
1537

D
dapan1121 已提交
1538
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pNode)) {
D
dapan 已提交
1539
    return sclWalkLogic(pNode, ctx);
D
dapan1121 已提交
1540
  }
D
dapan1121 已提交
1541

1542 1543 1544
  if (QUERY_NODE_TARGET == nodeType(pNode)) {
    return sclWalkTarget(pNode, ctx);
  }
D
dapan1121 已提交
1545

D
dapan1121 已提交
1546 1547 1548 1549
  if (QUERY_NODE_CASE_WHEN == nodeType(pNode)) {
    return sclWalkCaseWhen(pNode, ctx);
  }

D
dapan 已提交
1550
  sclError("invalid node type for scalar calculating, type:%d", nodeType(pNode));
D
dapan1121 已提交
1551 1552
  ctx->code = TSDB_CODE_QRY_INVALID_INPUT;
  return DEAL_RES_ERROR;
D
dapan1121 已提交
1553 1554
}

D
dapan1121 已提交
1555
int32_t sclCalcConstants(SNode *pNode, bool dual, SNode **pRes) {
D
dapan1121 已提交
1556 1557 1558 1559
  if (NULL == pNode) {
    SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT);
  }

H
Hongze Cheng 已提交
1560
  int32_t    code = 0;
D
dapan 已提交
1561
  SScalarCtx ctx = {0};
D
dapan1121 已提交
1562
  ctx.dual = dual;
1563
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
D
dapan 已提交
1564
  if (NULL == ctx.pRes) {
1565
    sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM);
S
Shengliang Guan 已提交
1566
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan 已提交
1567
  }
G
Ganlin Zhao 已提交
1568

X
Xiaoyu Wang 已提交
1569
  nodesRewriteExprPostOrder(&pNode, sclConstantsRewriter, (void *)&ctx);
D
dapan 已提交
1570
  SCL_ERR_JRET(ctx.code);
D
dapan1121 已提交
1571 1572
  *pRes = pNode;

D
dapan 已提交
1573
_return:
1574

D
dapan 已提交
1575 1576
  sclFreeRes(ctx.pRes);
  return code;
D
dapan1121 已提交
1577
}
D
dapan1121 已提交
1578

H
Hongze Cheng 已提交
1579 1580
static int32_t sclGetMinusOperatorResType(SOperatorNode *pOp) {
  if (!IS_MATHABLE_TYPE(((SExprNode *)(pOp->pLeft))->resType.type)) {
1581 1582 1583 1584 1585 1586 1587
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }
  pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE;
  pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1588
static int32_t sclGetMathOperatorResType(SOperatorNode *pOp) {
G
Ganlin Zhao 已提交
1589 1590 1591 1592
  if (pOp == NULL || pOp->pLeft == NULL || pOp->pRight == NULL) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

H
Hongze Cheng 已提交
1593 1594
  SDataType ldt = ((SExprNode *)(pOp->pLeft))->resType;
  SDataType rdt = ((SExprNode *)(pOp->pRight))->resType;
G
Ganlin Zhao 已提交
1595

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
  if ((TSDB_DATA_TYPE_TIMESTAMP == ldt.type && TSDB_DATA_TYPE_TIMESTAMP == rdt.type) ||
      (TSDB_DATA_TYPE_TIMESTAMP == ldt.type && (IS_VAR_DATA_TYPE(rdt.type) || IS_FLOAT_TYPE(rdt.type))) ||
      (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && (IS_VAR_DATA_TYPE(ldt.type) || IS_FLOAT_TYPE(ldt.type)))) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

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

H
Hongze Cheng 已提交
1615
static int32_t sclGetCompOperatorResType(SOperatorNode *pOp) {
G
Ganlin Zhao 已提交
1616
  if (pOp == NULL || pOp->pLeft == NULL) {
G
Ganlin Zhao 已提交
1617 1618 1619
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

H
Hongze Cheng 已提交
1620
  SDataType ldt = ((SExprNode *)(pOp->pLeft))->resType;
G
Ganlin Zhao 已提交
1621

1622
  if (OP_TYPE_IN == pOp->opType || OP_TYPE_NOT_IN == pOp->opType) {
G
Ganlin Zhao 已提交
1623 1624 1625
    if (pOp->pRight == NULL) {
      return TSDB_CODE_TSC_INVALID_OPERATION;
    }
H
Hongze Cheng 已提交
1626
    ((SExprNode *)(pOp->pRight))->resType = ldt;
1627
  } else if (nodesIsRegularOp(pOp)) {
G
Ganlin Zhao 已提交
1628 1629 1630
    if (pOp->pRight == NULL) {
      return TSDB_CODE_TSC_INVALID_OPERATION;
    }
H
Hongze Cheng 已提交
1631
    SDataType rdt = ((SExprNode *)(pOp->pRight))->resType;
1632 1633 1634
    if (!IS_VAR_DATA_TYPE(ldt.type) || QUERY_NODE_VALUE != nodeType(pOp->pRight) ||
        (!IS_STR_DATA_TYPE(rdt.type) && (rdt.type != TSDB_DATA_TYPE_NULL))) {
      return TSDB_CODE_TSC_INVALID_OPERATION;
1635 1636 1637 1638 1639 1640 1641
    }
  }
  pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
  pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1642
static int32_t sclGetJsonOperatorResType(SOperatorNode *pOp) {
G
Ganlin Zhao 已提交
1643 1644 1645 1646
  if (pOp == NULL || pOp->pLeft == NULL || pOp->pRight == NULL) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

H
Hongze Cheng 已提交
1647 1648
  SDataType ldt = ((SExprNode *)(pOp->pLeft))->resType;
  SDataType rdt = ((SExprNode *)(pOp->pRight))->resType;
G
Ganlin Zhao 已提交
1649

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
  if (TSDB_DATA_TYPE_JSON != ldt.type || !IS_STR_DATA_TYPE(rdt.type)) {
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }
  if (pOp->opType == OP_TYPE_JSON_GET_VALUE) {
    pOp->node.resType.type = TSDB_DATA_TYPE_JSON;
  } else if (pOp->opType == OP_TYPE_JSON_CONTAINS) {
    pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
  }
  pOp->node.resType.bytes = tDataTypes[pOp->node.resType.type].bytes;
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1662
static int32_t sclGetBitwiseOperatorResType(SOperatorNode *pOp) {
1663 1664 1665 1666 1667
  pOp->node.resType.type = TSDB_DATA_TYPE_BIGINT;
  pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
1668
int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) { return sclCalcConstants(pNode, false, pRes); }
D
dapan1121 已提交
1669

H
Hongze Cheng 已提交
1670
int32_t scalarCalculateConstantsFromDual(SNode *pNode, SNode **pRes) { return sclCalcConstants(pNode, true, pRes); }
D
dapan1121 已提交
1671 1672 1673 1674 1675 1676

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

H
Hongze Cheng 已提交
1677
  int32_t    code = 0;
D
dapan1121 已提交
1678 1679 1680 1681 1682 1683
  SScalarCtx ctx = {.code = 0, .pBlockList = pBlockList, .param = pDst ? pDst->param : NULL};

  // TODO: OPT performance
  ctx.pRes = taosHashInit(SCL_DEFAULT_OP_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
  if (NULL == ctx.pRes) {
    sclError("taosHashInit failed, num:%d", SCL_DEFAULT_OP_NUM);
S
Shengliang Guan 已提交
1684
    SCL_ERR_RET(TSDB_CODE_OUT_OF_MEMORY);
D
dapan1121 已提交
1685
  }
1686

D
dapan1121 已提交
1687 1688 1689 1690 1691 1692 1693
  nodesWalkExprPostOrder(pNode, sclCalcWalker, (void *)&ctx);
  SCL_ERR_JRET(ctx.code);

  if (pDst) {
    SScalarParam *res = (SScalarParam *)taosHashGet(ctx.pRes, (void *)&pNode, POINTER_BYTES);
    if (NULL == res) {
      sclError("no valid res in hash, node:%p, type:%d", pNode, nodeType(pNode));
S
Shengliang Guan 已提交
1694
      SCL_ERR_JRET(TSDB_CODE_APP_ERROR);
D
dapan1121 已提交
1695 1696
    }

1697 1698
    SSDataBlock *pb = taosArrayGetP(pBlockList, 0);
    if (1 == res->numOfRows && pb->info.rows > 0) {
D
dapan1121 已提交
1699 1700
      SCL_ERR_JRET(sclExtendResRows(pDst, res, pBlockList));
    } else {
H
Haojun Liao 已提交
1701
      colInfoDataEnsureCapacity(pDst->columnData, res->numOfRows, true);
D
dapan1121 已提交
1702 1703
      colDataAssign(pDst->columnData, res->columnData, res->numOfRows, NULL);
      pDst->numOfRows = res->numOfRows;
1704
      pDst->numOfQualified = res->numOfQualified;
D
dapan1121 已提交
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
    }

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

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

H
Hongze Cheng 已提交
1716 1717 1718
int32_t scalarGetOperatorResultType(SOperatorNode *pOp) {
  if (TSDB_DATA_TYPE_BLOB == ((SExprNode *)(pOp->pLeft))->resType.type ||
      (NULL != pOp->pRight && TSDB_DATA_TYPE_BLOB == ((SExprNode *)(pOp->pRight))->resType.type)) {
1719 1720 1721 1722 1723
    return TSDB_CODE_TSC_INVALID_OPERATION;
  }

  switch (pOp->opType) {
    case OP_TYPE_ADD:
D
dapan1121 已提交
1724 1725 1726 1727
    case OP_TYPE_SUB:
    case OP_TYPE_MULTI:
    case OP_TYPE_DIV:
    case OP_TYPE_REM:
D
dapan1121 已提交
1728
      return sclGetMathOperatorResType(pOp);
D
dapan1121 已提交
1729
    case OP_TYPE_MINUS:
D
dapan1121 已提交
1730
      return sclGetMinusOperatorResType(pOp);
1731
    case OP_TYPE_ASSIGN:
H
Hongze Cheng 已提交
1732
      pOp->node.resType = ((SExprNode *)(pOp->pLeft))->resType;
1733 1734 1735
      break;
    case OP_TYPE_BIT_AND:
    case OP_TYPE_BIT_OR:
D
dapan1121 已提交
1736
      return sclGetBitwiseOperatorResType(pOp);
D
dapan1121 已提交
1737 1738 1739 1740 1741 1742
    case OP_TYPE_GREATER_THAN:
    case OP_TYPE_GREATER_EQUAL:
    case OP_TYPE_LOWER_THAN:
    case OP_TYPE_LOWER_EQUAL:
    case OP_TYPE_EQUAL:
    case OP_TYPE_NOT_EQUAL:
1743 1744 1745 1746 1747 1748 1749 1750
    case OP_TYPE_IS_NULL:
    case OP_TYPE_IS_NOT_NULL:
    case OP_TYPE_IS_TRUE:
    case OP_TYPE_IS_FALSE:
    case OP_TYPE_IS_UNKNOWN:
    case OP_TYPE_IS_NOT_TRUE:
    case OP_TYPE_IS_NOT_FALSE:
    case OP_TYPE_IS_NOT_UNKNOWN:
D
dapan1121 已提交
1751 1752 1753 1754
    case OP_TYPE_LIKE:
    case OP_TYPE_NOT_LIKE:
    case OP_TYPE_MATCH:
    case OP_TYPE_NMATCH:
1755 1756
    case OP_TYPE_IN:
    case OP_TYPE_NOT_IN:
D
dapan1121 已提交
1757
      return sclGetCompOperatorResType(pOp);
D
dapan1121 已提交
1758
    case OP_TYPE_JSON_GET_VALUE:
1759
    case OP_TYPE_JSON_CONTAINS:
D
dapan1121 已提交
1760
      return sclGetJsonOperatorResType(pOp);
D
dapan1121 已提交
1761
    default:
1762
      break;
D
dapan1121 已提交
1763 1764
  }

1765 1766
  return TSDB_CODE_SUCCESS;
}