nodesCloneFuncs.c 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

X
Xiaoyu Wang 已提交
16
#include "nodesUtil.h"
17
#include "plannodes.h"
X
Xiaoyu Wang 已提交
18 19
#include "querynodes.h"
#include "taos.h"
X
Xiaoyu Wang 已提交
20
#include "taoserror.h"
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#define COPY_SCALAR_FIELD(fldname)     \
  do {                                 \
    (pDst)->fldname = (pSrc)->fldname; \
  } while (0)

#define COPY_CHAR_ARRAY_FIELD(fldname)        \
  do {                                        \
    strcpy((pDst)->fldname, (pSrc)->fldname); \
  } while (0)

#define COPY_OBJECT_FIELD(fldname, size)                  \
  do {                                                    \
    memcpy(&((pDst)->fldname), &((pSrc)->fldname), size); \
  } while (0)

X
Xiaoyu Wang 已提交
37 38 39 40 41
#define COPY_CHAR_POINT_FIELD(fldname)         \
  do {                                         \
    if (NULL == (pSrc)->fldname) {             \
      break;                                   \
    }                                          \
X
Xiaoyu Wang 已提交
42
    (pDst)->fldname = strdup((pSrc)->fldname); \
43
    if (NULL == (pDst)->fldname) {             \
44
      return TSDB_CODE_OUT_OF_MEMORY;          \
45
    }                                          \
X
Xiaoyu Wang 已提交
46
  } while (0)
X
Xiaoyu Wang 已提交
47

X
Xiaoyu Wang 已提交
48 49 50 51 52
#define CLONE_NODE_FIELD(fldname)                      \
  do {                                                 \
    if (NULL == (pSrc)->fldname) {                     \
      break;                                           \
    }                                                  \
X
Xiaoyu Wang 已提交
53
    (pDst)->fldname = nodesCloneNode((pSrc)->fldname); \
X
Xiaoyu Wang 已提交
54
    if (NULL == (pDst)->fldname) {                     \
55
      return TSDB_CODE_OUT_OF_MEMORY;                  \
X
Xiaoyu Wang 已提交
56 57 58
    }                                                  \
  } while (0)

59 60 61 62 63 64 65
#define CLONE_NODE_FIELD_EX(fldname, nodePtrType)                           \
  do {                                                                      \
    if (NULL == (pSrc)->fldname) {                                          \
      break;                                                                \
    }                                                                       \
    (pDst)->fldname = (nodePtrType)nodesCloneNode((SNode*)(pSrc)->fldname); \
    if (NULL == (pDst)->fldname) {                                          \
66
      return TSDB_CODE_OUT_OF_MEMORY;                                       \
67 68 69
    }                                                                       \
  } while (0)

X
Xiaoyu Wang 已提交
70 71 72 73 74
#define CLONE_NODE_LIST_FIELD(fldname)                 \
  do {                                                 \
    if (NULL == (pSrc)->fldname) {                     \
      break;                                           \
    }                                                  \
X
Xiaoyu Wang 已提交
75
    (pDst)->fldname = nodesCloneList((pSrc)->fldname); \
X
Xiaoyu Wang 已提交
76
    if (NULL == (pDst)->fldname) {                     \
77
      return TSDB_CODE_OUT_OF_MEMORY;                  \
X
Xiaoyu Wang 已提交
78 79 80 81 82 83 84 85
    }                                                  \
  } while (0)

#define CLONE_OBJECT_FIELD(fldname, cloneFunc)    \
  do {                                            \
    if (NULL == (pSrc)->fldname) {                \
      break;                                      \
    }                                             \
86
    (pDst)->fldname = cloneFunc((pSrc)->fldname); \
X
Xiaoyu Wang 已提交
87
    if (NULL == (pDst)->fldname) {                \
88
      return TSDB_CODE_OUT_OF_MEMORY;             \
X
Xiaoyu Wang 已提交
89 90 91
    }                                             \
  } while (0)

92 93 94 95 96
#define COPY_BASE_OBJECT_FIELD(fldname, copyFunc)                                \
  do {                                                                           \
    if (TSDB_CODE_SUCCESS != copyFunc(&((pSrc)->fldname), &((pDst)->fldname))) { \
      return TSDB_CODE_OUT_OF_MEMORY;                                            \
    }                                                                            \
X
Xiaoyu Wang 已提交
97
  } while (0)
98

99
static int32_t exprNodeCopy(const SExprNode* pSrc, SExprNode* pDst) {
100 101 102
  COPY_OBJECT_FIELD(resType, sizeof(SDataType));
  COPY_CHAR_ARRAY_FIELD(aliasName);
  COPY_CHAR_ARRAY_FIELD(userAlias);
103
  COPY_SCALAR_FIELD(orderAlias);
104
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
105 106
}

107
static int32_t columnNodeCopy(const SColumnNode* pSrc, SColumnNode* pDst) {
108
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
109 110 111 112
  COPY_SCALAR_FIELD(tableId);
  COPY_SCALAR_FIELD(tableType);
  COPY_SCALAR_FIELD(colId);
  COPY_SCALAR_FIELD(colType);
X
Xiaoyu Wang 已提交
113
  COPY_SCALAR_FIELD(hasIndex);
114 115 116 117 118 119
  COPY_CHAR_ARRAY_FIELD(dbName);
  COPY_CHAR_ARRAY_FIELD(tableName);
  COPY_CHAR_ARRAY_FIELD(tableAlias);
  COPY_CHAR_ARRAY_FIELD(colName);
  COPY_SCALAR_FIELD(dataBlockId);
  COPY_SCALAR_FIELD(slotId);
120
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
121 122
}

123
static int32_t valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
124
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
X
Xiaoyu Wang 已提交
125
  COPY_CHAR_POINT_FIELD(literal);
126 127
  COPY_SCALAR_FIELD(isDuration);
  COPY_SCALAR_FIELD(translate);
128
  COPY_SCALAR_FIELD(notReserved);
129 130 131
  COPY_SCALAR_FIELD(placeholderNo);
  COPY_SCALAR_FIELD(typeData);
  COPY_SCALAR_FIELD(unit);
X
Xiaoyu Wang 已提交
132
  if (!pSrc->translate) {
133
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
134
  }
X
Xiaoyu Wang 已提交
135
  switch (pSrc->node.resType.type) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    case TSDB_DATA_TYPE_BOOL:
      COPY_SCALAR_FIELD(datum.b);
      break;
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      COPY_SCALAR_FIELD(datum.i);
      break;
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
      COPY_SCALAR_FIELD(datum.d);
      break;
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      COPY_SCALAR_FIELD(datum.u);
      break;
X
Xiaoyu Wang 已提交
156 157
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
158
    case TSDB_DATA_TYPE_VARBINARY: {
159 160
      int32_t len = varDataTLen(pSrc->datum.p) + 1;
      pDst->datum.p = taosMemoryCalloc(1, len);
X
Xiaoyu Wang 已提交
161
      if (NULL == pDst->datum.p) {
162
        return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
163
      }
164
      memcpy(pDst->datum.p, pSrc->datum.p, len);
X
Xiaoyu Wang 已提交
165
      break;
166
    }
X
Xiaoyu Wang 已提交
167 168 169
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
170
    case TSDB_DATA_TYPE_MEDIUMBLOB:
X
Xiaoyu Wang 已提交
171 172 173
    default:
      break;
  }
174
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
175 176
}

177
static int32_t operatorNodeCopy(const SOperatorNode* pSrc, SOperatorNode* pDst) {
178
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
179
  COPY_SCALAR_FIELD(opType);
180 181
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
182
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
183 184
}

185
static int32_t logicConditionNodeCopy(const SLogicConditionNode* pSrc, SLogicConditionNode* pDst) {
186
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
187
  COPY_SCALAR_FIELD(condType);
188
  CLONE_NODE_LIST_FIELD(pParameterList);
189
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
190 191
}

192
static int32_t functionNodeCopy(const SFunctionNode* pSrc, SFunctionNode* pDst) {
193
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
194 195 196
  COPY_CHAR_ARRAY_FIELD(functionName);
  COPY_SCALAR_FIELD(funcId);
  COPY_SCALAR_FIELD(funcType);
197
  CLONE_NODE_LIST_FIELD(pParameterList);
198
  COPY_SCALAR_FIELD(udfBufSize);
199
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
200 201
}

202
static int32_t tableNodeCopy(const STableNode* pSrc, STableNode* pDst) {
203
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
204 205 206 207
  COPY_CHAR_ARRAY_FIELD(dbName);
  COPY_CHAR_ARRAY_FIELD(tableName);
  COPY_CHAR_ARRAY_FIELD(tableAlias);
  COPY_SCALAR_FIELD(precision);
X
Xiaoyu Wang 已提交
208
  COPY_SCALAR_FIELD(singleTable);
209
  return TSDB_CODE_SUCCESS;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
}

static STableMeta* tableMetaClone(const STableMeta* pSrc) {
  int32_t     len = TABLE_META_SIZE(pSrc);
  STableMeta* pDst = taosMemoryMalloc(len);
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, len);
  return pDst;
}

static SVgroupsInfo* vgroupsInfoClone(const SVgroupsInfo* pSrc) {
  int32_t       len = VGROUPS_INFO_SIZE(pSrc);
  SVgroupsInfo* pDst = taosMemoryMalloc(len);
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, len);
  return pDst;
}

232
static int32_t realTableNodeCopy(const SRealTableNode* pSrc, SRealTableNode* pDst) {
233 234 235
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
  CLONE_OBJECT_FIELD(pMeta, tableMetaClone);
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
236 237
  COPY_CHAR_ARRAY_FIELD(qualDbName);
  COPY_SCALAR_FIELD(ratio);
238
  return TSDB_CODE_SUCCESS;
239 240
}

241
static int32_t tempTableNodeCopy(const STempTableNode* pSrc, STempTableNode* pDst) {
242 243
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
  CLONE_NODE_FIELD(pSubquery);
244
  return TSDB_CODE_SUCCESS;
245 246
}

247
static int32_t joinTableNodeCopy(const SJoinTableNode* pSrc, SJoinTableNode* pDst) {
248
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
249
  COPY_SCALAR_FIELD(joinType);
250 251 252
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
  CLONE_NODE_FIELD(pOnCond);
253
  return TSDB_CODE_SUCCESS;
254 255
}

256
static int32_t targetNodeCopy(const STargetNode* pSrc, STargetNode* pDst) {
257 258
  COPY_SCALAR_FIELD(dataBlockId);
  COPY_SCALAR_FIELD(slotId);
259
  CLONE_NODE_FIELD(pExpr);
260
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
261 262
}

263
static int32_t groupingSetNodeCopy(const SGroupingSetNode* pSrc, SGroupingSetNode* pDst) {
264
  COPY_SCALAR_FIELD(groupingSetType);
265
  CLONE_NODE_LIST_FIELD(pParameterList);
266
  return TSDB_CODE_SUCCESS;
267 268
}

269
static int32_t orderByExprNodeCopy(const SOrderByExprNode* pSrc, SOrderByExprNode* pDst) {
X
Xiaoyu Wang 已提交
270
  CLONE_NODE_FIELD(pExpr);
271 272
  COPY_SCALAR_FIELD(order);
  COPY_SCALAR_FIELD(nullOrder);
273
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
274 275
}

276
static int32_t limitNodeCopy(const SLimitNode* pSrc, SLimitNode* pDst) {
277 278
  COPY_SCALAR_FIELD(limit);
  COPY_SCALAR_FIELD(offset);
279
  return TSDB_CODE_SUCCESS;
280
}
281

282
static int32_t stateWindowNodeCopy(const SStateWindowNode* pSrc, SStateWindowNode* pDst) {
283 284
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pExpr);
285
  return TSDB_CODE_SUCCESS;
286 287
}

288
static int32_t sessionWindowNodeCopy(const SSessionWindowNode* pSrc, SSessionWindowNode* pDst) {
289 290
  CLONE_NODE_FIELD_EX(pCol, SColumnNode*);
  CLONE_NODE_FIELD_EX(pGap, SValueNode*);
291
  return TSDB_CODE_SUCCESS;
292 293
}

294
static int32_t intervalWindowNodeCopy(const SIntervalWindowNode* pSrc, SIntervalWindowNode* pDst) {
295 296 297 298 299
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pInterval);
  CLONE_NODE_FIELD(pOffset);
  CLONE_NODE_FIELD(pSliding);
  CLONE_NODE_FIELD(pFill);
300
  return TSDB_CODE_SUCCESS;
301 302
}

303
static int32_t nodeListNodeCopy(const SNodeListNode* pSrc, SNodeListNode* pDst) {
304
  COPY_OBJECT_FIELD(dataType, sizeof(SDataType));
X
Xiaoyu Wang 已提交
305
  CLONE_NODE_LIST_FIELD(pNodeList);
306
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
307 308
}

309
static int32_t fillNodeCopy(const SFillNode* pSrc, SFillNode* pDst) {
310
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
311
  CLONE_NODE_FIELD(pValues);
X
Xiaoyu Wang 已提交
312
  CLONE_NODE_FIELD(pWStartTs);
313
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
314
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
315 316
}

317
static int32_t logicNodeCopy(const SLogicNode* pSrc, SLogicNode* pDst) {
318 319 320
  CLONE_NODE_LIST_FIELD(pTargets);
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
321
  COPY_SCALAR_FIELD(optimizedFlag);
322
  COPY_SCALAR_FIELD(precision);
323 324
  CLONE_NODE_FIELD(pLimit);
  CLONE_NODE_FIELD(pSlimit);
325
  return TSDB_CODE_SUCCESS;
326 327
}

328
static int32_t logicScanCopy(const SScanLogicNode* pSrc, SScanLogicNode* pDst) {
329 330
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pScanCols);
331
  CLONE_NODE_LIST_FIELD(pScanPseudoCols);
X
Xiaoyu Wang 已提交
332 333
  COPY_SCALAR_FIELD(tableType);
  COPY_SCALAR_FIELD(tableId);
X
Xiaoyu Wang 已提交
334
  COPY_SCALAR_FIELD(stableId);
335
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
336 337 338 339 340 341
  COPY_SCALAR_FIELD(scanType);
  COPY_OBJECT_FIELD(scanSeq[0], sizeof(uint8_t) * 2);
  COPY_OBJECT_FIELD(scanRange, sizeof(STimeWindow));
  COPY_OBJECT_FIELD(tableName, sizeof(SName));
  COPY_SCALAR_FIELD(showRewrite);
  COPY_SCALAR_FIELD(ratio);
342
  CLONE_NODE_LIST_FIELD(pDynamicScanFuncs);
343 344 345 346 347 348
  COPY_SCALAR_FIELD(dataRequired);
  COPY_SCALAR_FIELD(interval);
  COPY_SCALAR_FIELD(offset);
  COPY_SCALAR_FIELD(sliding);
  COPY_SCALAR_FIELD(intervalUnit);
  COPY_SCALAR_FIELD(slidingUnit);
X
Xiaoyu Wang 已提交
349
  CLONE_NODE_FIELD(pTagCond);
X
Xiaoyu Wang 已提交
350
  CLONE_NODE_FIELD(pTagIndexCond);
5
54liuyao 已提交
351 352 353 354
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
  COPY_SCALAR_FIELD(tsColId);
  COPY_SCALAR_FIELD(filesFactor);
355
  CLONE_NODE_LIST_FIELD(pGroupTags);
356
  return TSDB_CODE_SUCCESS;
357 358
}

359
static int32_t logicJoinCopy(const SJoinLogicNode* pSrc, SJoinLogicNode* pDst) {
360
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
361
  COPY_SCALAR_FIELD(joinType);
362
  CLONE_NODE_FIELD(pOnConditions);
X
Xiaoyu Wang 已提交
363
  COPY_SCALAR_FIELD(isSingleTableJoin);
364
  return TSDB_CODE_SUCCESS;
365 366
}

367
static int32_t logicAggCopy(const SAggLogicNode* pSrc, SAggLogicNode* pDst) {
368 369 370
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pGroupKeys);
  CLONE_NODE_LIST_FIELD(pAggFuncs);
371
  return TSDB_CODE_SUCCESS;
372 373
}

374
static int32_t logicProjectCopy(const SProjectLogicNode* pSrc, SProjectLogicNode* pDst) {
375 376
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pProjections);
377
  COPY_CHAR_ARRAY_FIELD(stmtName);
378
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
379 380
}

381
static int32_t logicVnodeModifCopy(const SVnodeModifyLogicNode* pSrc, SVnodeModifyLogicNode* pDst) {
X
Xiaoyu Wang 已提交
382
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
X
Xiaoyu Wang 已提交
383
  COPY_SCALAR_FIELD(modifyType);
384
  COPY_SCALAR_FIELD(msgType);
X
Xiaoyu Wang 已提交
385
  CLONE_NODE_FIELD(pAffectedRows);
X
Xiaoyu Wang 已提交
386 387 388 389
  COPY_SCALAR_FIELD(tableId);
  COPY_SCALAR_FIELD(tableType);
  COPY_CHAR_ARRAY_FIELD(tableFName);
  COPY_OBJECT_FIELD(deleteTimeRange, sizeof(STimeWindow));
390
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
391 392
}

393
static int32_t logicExchangeCopy(const SExchangeLogicNode* pSrc, SExchangeLogicNode* pDst) {
X
Xiaoyu Wang 已提交
394
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
395
  COPY_SCALAR_FIELD(srcGroupId);
396
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
397 398
}

399
static int32_t logicMergeCopy(const SMergeLogicNode* pSrc, SMergeLogicNode* pDst) {
X
Xiaoyu Wang 已提交
400 401
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pMergeKeys);
X
Xiaoyu Wang 已提交
402
  CLONE_NODE_LIST_FIELD(pInputs);
X
Xiaoyu Wang 已提交
403 404
  COPY_SCALAR_FIELD(numOfChannels);
  COPY_SCALAR_FIELD(srcGroupId);
405
  COPY_SCALAR_FIELD(groupSort);
406
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
407 408
}

409
static int32_t logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pDst) {
X
Xiaoyu Wang 已提交
410
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
411
  COPY_SCALAR_FIELD(winType);
X
Xiaoyu Wang 已提交
412
  CLONE_NODE_LIST_FIELD(pFuncs);
413 414 415 416 417 418
  COPY_SCALAR_FIELD(interval);
  COPY_SCALAR_FIELD(offset);
  COPY_SCALAR_FIELD(sliding);
  COPY_SCALAR_FIELD(intervalUnit);
  COPY_SCALAR_FIELD(slidingUnit);
  COPY_SCALAR_FIELD(sessionGap);
X
bugfix  
Xiaoyu Wang 已提交
419
  CLONE_NODE_FIELD(pTspk);
5
54liuyao 已提交
420
  CLONE_NODE_FIELD(pTsEnd);
421 422 423
  CLONE_NODE_FIELD(pStateExpr);
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
5
54liuyao 已提交
424
  COPY_SCALAR_FIELD(filesFactor);
425
  COPY_SCALAR_FIELD(windowAlgo);
426
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
427 428
}

429
static int32_t logicFillCopy(const SFillLogicNode* pSrc, SFillLogicNode* pDst) {
X
Xiaoyu Wang 已提交
430
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
431
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
432 433
  CLONE_NODE_FIELD(pWStartTs);
  CLONE_NODE_FIELD(pValues);
434
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
435
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
436 437
}

438
static int32_t logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) {
X
Xiaoyu Wang 已提交
439 440
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pSortKeys);
441
  COPY_SCALAR_FIELD(groupSort);
442
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
443 444
}

445
static int32_t logicPartitionCopy(const SPartitionLogicNode* pSrc, SPartitionLogicNode* pDst) {
446 447
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
448
  return TSDB_CODE_SUCCESS;
449 450
}

451
static int32_t logicIndefRowsFuncCopy(const SIndefRowsFuncLogicNode* pSrc, SIndefRowsFuncLogicNode* pDst) {
452
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
X
Xiaoyu Wang 已提交
453
  CLONE_NODE_LIST_FIELD(pFuncs);
454
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
455 456
}

457
static int32_t logicInterpFuncCopy(const SInterpFuncLogicNode* pSrc, SInterpFuncLogicNode* pDst) {
X
Xiaoyu Wang 已提交
458 459 460 461
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pFuncs);
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
  COPY_SCALAR_FIELD(interval);
X
Xiaoyu Wang 已提交
462 463 464
  COPY_SCALAR_FIELD(fillMode);
  CLONE_NODE_FIELD(pFillValues);
  CLONE_NODE_FIELD(pTimeSeries);
465
  return TSDB_CODE_SUCCESS;
466 467
}

468
static int32_t logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) {
469
  COPY_OBJECT_FIELD(id, sizeof(SSubplanId));
470
  CLONE_NODE_FIELD_EX(pNode, SLogicNode*);
471 472 473
  COPY_SCALAR_FIELD(subplanType);
  COPY_SCALAR_FIELD(level);
  COPY_SCALAR_FIELD(splitFlag);
474
  return TSDB_CODE_SUCCESS;
475 476
}

477
static int32_t physiNodeCopy(const SPhysiNode* pSrc, SPhysiNode* pDst) {
478
  CLONE_NODE_FIELD_EX(pOutputDataBlockDesc, SDataBlockDescNode*);
X
Xiaoyu Wang 已提交
479 480
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
481
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
482 483
}

484
static int32_t physiScanCopy(const SScanPhysiNode* pSrc, SScanPhysiNode* pDst) {
485 486 487 488 489 490 491
  COPY_BASE_OBJECT_FIELD(node, physiNodeCopy);
  CLONE_NODE_LIST_FIELD(pScanCols);
  CLONE_NODE_LIST_FIELD(pScanPseudoCols);
  COPY_SCALAR_FIELD(uid);
  COPY_SCALAR_FIELD(suid);
  COPY_SCALAR_FIELD(tableType);
  COPY_OBJECT_FIELD(tableName, sizeof(SName));
492
  return TSDB_CODE_SUCCESS;
493 494
}

495
static int32_t physiTagScanCopy(const STagScanPhysiNode* pSrc, STagScanPhysiNode* pDst) {
496 497 498
  return physiScanCopy(pSrc, pDst);
}

499
static int32_t physiTableScanCopy(const STableScanPhysiNode* pSrc, STableScanPhysiNode* pDst) {
500 501 502 503 504 505
  COPY_BASE_OBJECT_FIELD(scan, physiScanCopy);
  COPY_OBJECT_FIELD(scanSeq[0], sizeof(uint8_t) * 2);
  COPY_OBJECT_FIELD(scanRange, sizeof(STimeWindow));
  COPY_SCALAR_FIELD(ratio);
  COPY_SCALAR_FIELD(dataRequired);
  CLONE_NODE_LIST_FIELD(pDynamicScanFuncs);
506
  CLONE_NODE_LIST_FIELD(pGroupTags);
507 508 509 510 511 512 513 514 515
  COPY_SCALAR_FIELD(interval);
  COPY_SCALAR_FIELD(offset);
  COPY_SCALAR_FIELD(sliding);
  COPY_SCALAR_FIELD(intervalUnit);
  COPY_SCALAR_FIELD(slidingUnit);
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
  COPY_SCALAR_FIELD(tsColId);
  COPY_SCALAR_FIELD(filesFactor);
516
  return TSDB_CODE_SUCCESS;
517 518
}

519
static int32_t physiSysTableScanCopy(const SSystemTableScanPhysiNode* pSrc, SSystemTableScanPhysiNode* pDst) {
520 521 522 523
  COPY_BASE_OBJECT_FIELD(scan, physiScanCopy);
  COPY_OBJECT_FIELD(mgmtEpSet, sizeof(SEpSet));
  COPY_SCALAR_FIELD(showRewrite);
  COPY_SCALAR_FIELD(accountId);
524
  return TSDB_CODE_SUCCESS;
525 526
}

527
static int32_t physiWindowCopy(const SWinodwPhysiNode* pSrc, SWinodwPhysiNode* pDst) {
X
Xiaoyu Wang 已提交
528 529 530 531
  COPY_BASE_OBJECT_FIELD(node, physiNodeCopy);
  CLONE_NODE_LIST_FIELD(pExprs);
  CLONE_NODE_LIST_FIELD(pFuncs);
  CLONE_NODE_FIELD(pTspk);
5
54liuyao 已提交
532
  CLONE_NODE_FIELD(pTsEnd);
X
Xiaoyu Wang 已提交
533 534 535
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
  COPY_SCALAR_FIELD(filesFactor);
536
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
537 538
}

539
static int32_t physiIntervalCopy(const SIntervalPhysiNode* pSrc, SIntervalPhysiNode* pDst) {
X
Xiaoyu Wang 已提交
540 541 542 543 544 545
  COPY_BASE_OBJECT_FIELD(window, physiWindowCopy);
  COPY_SCALAR_FIELD(interval);
  COPY_SCALAR_FIELD(offset);
  COPY_SCALAR_FIELD(sliding);
  COPY_SCALAR_FIELD(intervalUnit);
  COPY_SCALAR_FIELD(slidingUnit);
546
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
547 548
}

549
static int32_t physiSessionCopy(const SSessionWinodwPhysiNode* pSrc, SSessionWinodwPhysiNode* pDst) {
550 551
  COPY_BASE_OBJECT_FIELD(window, physiWindowCopy);
  COPY_SCALAR_FIELD(gap);
552
  return TSDB_CODE_SUCCESS;
553 554
}

555
static int32_t physiPartitionCopy(const SPartitionPhysiNode* pSrc, SPartitionPhysiNode* pDst) {
X
Xiaoyu Wang 已提交
556 557 558 559
  COPY_BASE_OBJECT_FIELD(node, physiNodeCopy);
  CLONE_NODE_LIST_FIELD(pExprs);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
  CLONE_NODE_LIST_FIELD(pTargets);
560
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
561 562
}

563
static int32_t dataBlockDescCopy(const SDataBlockDescNode* pSrc, SDataBlockDescNode* pDst) {
564
  COPY_SCALAR_FIELD(dataBlockId);
565
  CLONE_NODE_LIST_FIELD(pSlots);
566 567 568
  COPY_SCALAR_FIELD(totalRowSize);
  COPY_SCALAR_FIELD(outputRowSize);
  COPY_SCALAR_FIELD(precision);
569
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
570 571
}

572
static int32_t slotDescCopy(const SSlotDescNode* pSrc, SSlotDescNode* pDst) {
573 574 575 576 577
  COPY_SCALAR_FIELD(slotId);
  COPY_OBJECT_FIELD(dataType, sizeof(SDataType));
  COPY_SCALAR_FIELD(reserve);
  COPY_SCALAR_FIELD(output);
  COPY_SCALAR_FIELD(tag);
578
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
579 580
}

581
static int32_t downstreamSourceCopy(const SDownstreamSourceNode* pSrc, SDownstreamSourceNode* pDst) {
582 583 584
  COPY_OBJECT_FIELD(addr, sizeof(SQueryNodeAddr));
  COPY_SCALAR_FIELD(taskId);
  COPY_SCALAR_FIELD(schedId);
585
  return TSDB_CODE_SUCCESS;
586 587
}

588
static int32_t selectStmtCopy(const SSelectStmt* pSrc, SSelectStmt* pDst) {
589
  COPY_SCALAR_FIELD(isDistinct);
590 591 592 593 594 595 596 597
  CLONE_NODE_LIST_FIELD(pProjectionList);
  CLONE_NODE_FIELD(pFromTable);
  CLONE_NODE_FIELD(pWhere);
  CLONE_NODE_LIST_FIELD(pPartitionByList);
  CLONE_NODE_FIELD(pWindow);
  CLONE_NODE_LIST_FIELD(pGroupByList);
  CLONE_NODE_FIELD(pHaving);
  CLONE_NODE_LIST_FIELD(pOrderByList);
598 599
  CLONE_NODE_FIELD_EX(pLimit, SLimitNode*);
  CLONE_NODE_FIELD_EX(pLimit, SLimitNode*);
600 601 602 603 604 605
  COPY_CHAR_ARRAY_FIELD(stmtName);
  COPY_SCALAR_FIELD(precision);
  COPY_SCALAR_FIELD(isEmptyResult);
  COPY_SCALAR_FIELD(isTimeOrderQuery);
  COPY_SCALAR_FIELD(hasAggFuncs);
  COPY_SCALAR_FIELD(hasRepeatScanFuncs);
606
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
607 608
}

609
SNode* nodesCloneNode(const SNode* pNode) {
X
Xiaoyu Wang 已提交
610 611 612
  if (NULL == pNode) {
    return NULL;
  }
613

X
Xiaoyu Wang 已提交
614 615
  SNode* pDst = nodesMakeNode(nodeType(pNode));
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
616
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
617 618
    return NULL;
  }
619 620

  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
621 622
  switch (nodeType(pNode)) {
    case QUERY_NODE_COLUMN:
623
      code = columnNodeCopy((const SColumnNode*)pNode, (SColumnNode*)pDst);
624
      break;
X
Xiaoyu Wang 已提交
625
    case QUERY_NODE_VALUE:
626
      code = valueNodeCopy((const SValueNode*)pNode, (SValueNode*)pDst);
627
      break;
X
Xiaoyu Wang 已提交
628
    case QUERY_NODE_OPERATOR:
629
      code = operatorNodeCopy((const SOperatorNode*)pNode, (SOperatorNode*)pDst);
630
      break;
X
Xiaoyu Wang 已提交
631
    case QUERY_NODE_LOGIC_CONDITION:
632
      code = logicConditionNodeCopy((const SLogicConditionNode*)pNode, (SLogicConditionNode*)pDst);
633
      break;
X
Xiaoyu Wang 已提交
634
    case QUERY_NODE_FUNCTION:
635
      code = functionNodeCopy((const SFunctionNode*)pNode, (SFunctionNode*)pDst);
636
      break;
X
Xiaoyu Wang 已提交
637
    case QUERY_NODE_REAL_TABLE:
638
      code = realTableNodeCopy((const SRealTableNode*)pNode, (SRealTableNode*)pDst);
639
      break;
X
Xiaoyu Wang 已提交
640
    case QUERY_NODE_TEMP_TABLE:
641
      code = tempTableNodeCopy((const STempTableNode*)pNode, (STempTableNode*)pDst);
642
      break;
X
Xiaoyu Wang 已提交
643
    case QUERY_NODE_JOIN_TABLE:
644
      code = joinTableNodeCopy((const SJoinTableNode*)pNode, (SJoinTableNode*)pDst);
645
      break;
X
Xiaoyu Wang 已提交
646
    case QUERY_NODE_GROUPING_SET:
647
      code = groupingSetNodeCopy((const SGroupingSetNode*)pNode, (SGroupingSetNode*)pDst);
648
      break;
X
Xiaoyu Wang 已提交
649
    case QUERY_NODE_ORDER_BY_EXPR:
650
      code = orderByExprNodeCopy((const SOrderByExprNode*)pNode, (SOrderByExprNode*)pDst);
651
      break;
X
Xiaoyu Wang 已提交
652
    case QUERY_NODE_LIMIT:
653
      code = limitNodeCopy((const SLimitNode*)pNode, (SLimitNode*)pDst);
654
      break;
655
    case QUERY_NODE_STATE_WINDOW:
656
      code = stateWindowNodeCopy((const SStateWindowNode*)pNode, (SStateWindowNode*)pDst);
657
      break;
658
    case QUERY_NODE_SESSION_WINDOW:
659
      code = sessionWindowNodeCopy((const SSessionWindowNode*)pNode, (SSessionWindowNode*)pDst);
660
      break;
661
    case QUERY_NODE_INTERVAL_WINDOW:
662
      code = intervalWindowNodeCopy((const SIntervalWindowNode*)pNode, (SIntervalWindowNode*)pDst);
663
      break;
X
Xiaoyu Wang 已提交
664
    case QUERY_NODE_NODE_LIST:
665
      code = nodeListNodeCopy((const SNodeListNode*)pNode, (SNodeListNode*)pDst);
666
      break;
X
Xiaoyu Wang 已提交
667
    case QUERY_NODE_FILL:
668
      code = fillNodeCopy((const SFillNode*)pNode, (SFillNode*)pDst);
669
      break;
670
    case QUERY_NODE_TARGET:
671
      code = targetNodeCopy((const STargetNode*)pNode, (STargetNode*)pDst);
672
      break;
X
Xiaoyu Wang 已提交
673
    case QUERY_NODE_DATABLOCK_DESC:
674
      code = dataBlockDescCopy((const SDataBlockDescNode*)pNode, (SDataBlockDescNode*)pDst);
675
      break;
X
Xiaoyu Wang 已提交
676
    case QUERY_NODE_SLOT_DESC:
677
      code = slotDescCopy((const SSlotDescNode*)pNode, (SSlotDescNode*)pDst);
678
      break;
X
Xiaoyu Wang 已提交
679
    case QUERY_NODE_DOWNSTREAM_SOURCE:
680
      code = downstreamSourceCopy((const SDownstreamSourceNode*)pNode, (SDownstreamSourceNode*)pDst);
681
      break;
682
    case QUERY_NODE_LEFT_VALUE:
683
      code = TSDB_CODE_SUCCESS;
684
      break;
685
    case QUERY_NODE_SELECT_STMT:
686
      code = selectStmtCopy((const SSelectStmt*)pNode, (SSelectStmt*)pDst);
687
      break;
688
    case QUERY_NODE_LOGIC_PLAN_SCAN:
689
      code = logicScanCopy((const SScanLogicNode*)pNode, (SScanLogicNode*)pDst);
690
      break;
691
    case QUERY_NODE_LOGIC_PLAN_JOIN:
692
      code = logicJoinCopy((const SJoinLogicNode*)pNode, (SJoinLogicNode*)pDst);
693
      break;
694
    case QUERY_NODE_LOGIC_PLAN_AGG:
695
      code = logicAggCopy((const SAggLogicNode*)pNode, (SAggLogicNode*)pDst);
696
      break;
697
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
698
      code = logicProjectCopy((const SProjectLogicNode*)pNode, (SProjectLogicNode*)pDst);
699
      break;
X
Xiaoyu Wang 已提交
700
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY:
701
      code = logicVnodeModifCopy((const SVnodeModifyLogicNode*)pNode, (SVnodeModifyLogicNode*)pDst);
702
      break;
X
Xiaoyu Wang 已提交
703
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
704
      code = logicExchangeCopy((const SExchangeLogicNode*)pNode, (SExchangeLogicNode*)pDst);
705
      break;
X
Xiaoyu Wang 已提交
706
    case QUERY_NODE_LOGIC_PLAN_MERGE:
707
      code = logicMergeCopy((const SMergeLogicNode*)pNode, (SMergeLogicNode*)pDst);
708
      break;
X
Xiaoyu Wang 已提交
709
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
710
      code = logicWindowCopy((const SWindowLogicNode*)pNode, (SWindowLogicNode*)pDst);
711
      break;
X
Xiaoyu Wang 已提交
712
    case QUERY_NODE_LOGIC_PLAN_FILL:
713
      code = logicFillCopy((const SFillLogicNode*)pNode, (SFillLogicNode*)pDst);
714
      break;
X
Xiaoyu Wang 已提交
715
    case QUERY_NODE_LOGIC_PLAN_SORT:
716
      code = logicSortCopy((const SSortLogicNode*)pNode, (SSortLogicNode*)pDst);
717
      break;
718
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
719
      code = logicPartitionCopy((const SPartitionLogicNode*)pNode, (SPartitionLogicNode*)pDst);
720
      break;
721
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC:
722
      code = logicIndefRowsFuncCopy((const SIndefRowsFuncLogicNode*)pNode, (SIndefRowsFuncLogicNode*)pDst);
723
      break;
X
Xiaoyu Wang 已提交
724
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC:
725
      code = logicInterpFuncCopy((const SInterpFuncLogicNode*)pNode, (SInterpFuncLogicNode*)pDst);
726
      break;
727
    case QUERY_NODE_LOGIC_SUBPLAN:
728
      code = logicSubplanCopy((const SLogicSubplan*)pNode, (SLogicSubplan*)pDst);
729
      break;
730
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
731
      code = physiTagScanCopy((const STagScanPhysiNode*)pNode, (STagScanPhysiNode*)pDst);
732
      break;
733 734 735
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
736
      code = physiTableScanCopy((const STableScanPhysiNode*)pNode, (STableScanPhysiNode*)pDst);
737
      break;
738
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
739
      code = physiSysTableScanCopy((const SSystemTableScanPhysiNode*)pNode, (SSystemTableScanPhysiNode*)pDst);
740
      break;
X
Xiaoyu Wang 已提交
741
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
742
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
X
Xiaoyu Wang 已提交
743 744 745
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
746
      code = physiIntervalCopy((const SIntervalPhysiNode*)pNode, (SIntervalPhysiNode*)pDst);
747
      break;
748 749
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
750
      code = physiSessionCopy((const SSessionWinodwPhysiNode*)pNode, (SSessionWinodwPhysiNode*)pDst);
751
      break;
X
Xiaoyu Wang 已提交
752
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
753
      code = physiPartitionCopy((const SPartitionPhysiNode*)pNode, (SPartitionPhysiNode*)pDst);
754
      break;
X
Xiaoyu Wang 已提交
755 756 757
    default:
      break;
  }
758 759 760 761 762 763 764 765

  if (TSDB_CODE_SUCCESS != code) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    nodesDestroyNode(pDst);
    nodesError("nodesCloneNode failed node = %s", nodesNodeName(nodeType(pNode)));
    return NULL;
  }
  return pDst;
X
Xiaoyu Wang 已提交
766 767 768
}

SNodeList* nodesCloneList(const SNodeList* pList) {
X
Xiaoyu Wang 已提交
769 770 771 772
  if (NULL == pList) {
    return NULL;
  }

773 774
  SNodeList* pDst = NULL;
  SNode*     pNode;
X
Xiaoyu Wang 已提交
775
  FOREACH(pNode, pList) {
776 777 778
    int32_t code = nodesListMakeStrictAppend(&pDst, nodesCloneNode(pNode));
    if (TSDB_CODE_SUCCESS != code) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
779 780 781 782 783
      nodesDestroyList(pDst);
      return NULL;
    }
  }
  return pDst;
784
}