nodesCloneFuncs.c 27.8 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"
wmmhello's avatar
wmmhello 已提交
21
#include "tdatablock.h"
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#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 已提交
38 39 40 41 42
#define COPY_CHAR_POINT_FIELD(fldname)         \
  do {                                         \
    if (NULL == (pSrc)->fldname) {             \
      break;                                   \
    }                                          \
X
Xiaoyu Wang 已提交
43
    (pDst)->fldname = strdup((pSrc)->fldname); \
44
    if (NULL == (pDst)->fldname) {             \
45
      return TSDB_CODE_OUT_OF_MEMORY;          \
46
    }                                          \
X
Xiaoyu Wang 已提交
47
  } while (0)
X
Xiaoyu Wang 已提交
48

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

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

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

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

93 94 95 96 97
#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 已提交
98
  } while (0)
99

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

108
static int32_t columnNodeCopy(const SColumnNode* pSrc, SColumnNode* pDst) {
109
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
110 111 112 113
  COPY_SCALAR_FIELD(tableId);
  COPY_SCALAR_FIELD(tableType);
  COPY_SCALAR_FIELD(colId);
  COPY_SCALAR_FIELD(colType);
X
Xiaoyu Wang 已提交
114
  COPY_SCALAR_FIELD(hasIndex);
115 116 117 118 119 120
  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);
121
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
122 123
}

124
static int32_t valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
125
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
X
Xiaoyu Wang 已提交
126
  COPY_CHAR_POINT_FIELD(literal);
127 128
  COPY_SCALAR_FIELD(isDuration);
  COPY_SCALAR_FIELD(translate);
129
  COPY_SCALAR_FIELD(notReserved);
130 131 132
  COPY_SCALAR_FIELD(placeholderNo);
  COPY_SCALAR_FIELD(typeData);
  COPY_SCALAR_FIELD(unit);
X
Xiaoyu Wang 已提交
133
  if (!pSrc->translate) {
134
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
135
  }
X
Xiaoyu Wang 已提交
136
  switch (pSrc->node.resType.type) {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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 已提交
157 158
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
159
    case TSDB_DATA_TYPE_VARBINARY: {
160 161
      int32_t len = varDataTLen(pSrc->datum.p) + 1;
      pDst->datum.p = taosMemoryCalloc(1, len);
X
Xiaoyu Wang 已提交
162
      if (NULL == pDst->datum.p) {
163
        return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
164
      }
165
      memcpy(pDst->datum.p, pSrc->datum.p, len);
X
Xiaoyu Wang 已提交
166
      break;
167
    }
168
    case TSDB_DATA_TYPE_JSON: {
wmmhello's avatar
wmmhello 已提交
169 170 171 172 173 174 175 176
      int32_t len = getJsonValueLen(pSrc->datum.p);
      pDst->datum.p = taosMemoryCalloc(1, len);
      if (NULL == pDst->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      memcpy(pDst->datum.p, pSrc->datum.p, len);
      break;
    }
X
Xiaoyu Wang 已提交
177 178
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
179
    case TSDB_DATA_TYPE_MEDIUMBLOB:
X
Xiaoyu Wang 已提交
180 181 182
    default:
      break;
  }
183
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
184 185
}

186
static int32_t operatorNodeCopy(const SOperatorNode* pSrc, SOperatorNode* pDst) {
187
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
188
  COPY_SCALAR_FIELD(opType);
189 190
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
191
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
192 193
}

194
static int32_t logicConditionNodeCopy(const SLogicConditionNode* pSrc, SLogicConditionNode* pDst) {
195
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
196
  COPY_SCALAR_FIELD(condType);
197
  CLONE_NODE_LIST_FIELD(pParameterList);
198
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
199 200
}

201
static int32_t functionNodeCopy(const SFunctionNode* pSrc, SFunctionNode* pDst) {
202
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
203 204 205
  COPY_CHAR_ARRAY_FIELD(functionName);
  COPY_SCALAR_FIELD(funcId);
  COPY_SCALAR_FIELD(funcType);
206
  CLONE_NODE_LIST_FIELD(pParameterList);
207
  COPY_SCALAR_FIELD(udfBufSize);
208
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
209 210
}

211
static int32_t tableNodeCopy(const STableNode* pSrc, STableNode* pDst) {
212
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
213 214 215 216
  COPY_CHAR_ARRAY_FIELD(dbName);
  COPY_CHAR_ARRAY_FIELD(tableName);
  COPY_CHAR_ARRAY_FIELD(tableAlias);
  COPY_SCALAR_FIELD(precision);
X
Xiaoyu Wang 已提交
217
  COPY_SCALAR_FIELD(singleTable);
218
  return TSDB_CODE_SUCCESS;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

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

241
static int32_t realTableNodeCopy(const SRealTableNode* pSrc, SRealTableNode* pDst) {
242 243 244
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
  CLONE_OBJECT_FIELD(pMeta, tableMetaClone);
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
245 246
  COPY_CHAR_ARRAY_FIELD(qualDbName);
  COPY_SCALAR_FIELD(ratio);
247
  return TSDB_CODE_SUCCESS;
248 249
}

250
static int32_t tempTableNodeCopy(const STempTableNode* pSrc, STempTableNode* pDst) {
251 252
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
  CLONE_NODE_FIELD(pSubquery);
253
  return TSDB_CODE_SUCCESS;
254 255
}

256
static int32_t joinTableNodeCopy(const SJoinTableNode* pSrc, SJoinTableNode* pDst) {
257
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
258
  COPY_SCALAR_FIELD(joinType);
259 260 261
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
  CLONE_NODE_FIELD(pOnCond);
262
  return TSDB_CODE_SUCCESS;
263 264
}

265
static int32_t targetNodeCopy(const STargetNode* pSrc, STargetNode* pDst) {
266 267
  COPY_SCALAR_FIELD(dataBlockId);
  COPY_SCALAR_FIELD(slotId);
268
  CLONE_NODE_FIELD(pExpr);
269
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
270 271
}

272
static int32_t groupingSetNodeCopy(const SGroupingSetNode* pSrc, SGroupingSetNode* pDst) {
273
  COPY_SCALAR_FIELD(groupingSetType);
274
  CLONE_NODE_LIST_FIELD(pParameterList);
275
  return TSDB_CODE_SUCCESS;
276 277
}

278
static int32_t orderByExprNodeCopy(const SOrderByExprNode* pSrc, SOrderByExprNode* pDst) {
X
Xiaoyu Wang 已提交
279
  CLONE_NODE_FIELD(pExpr);
280 281
  COPY_SCALAR_FIELD(order);
  COPY_SCALAR_FIELD(nullOrder);
282
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
283 284
}

285
static int32_t limitNodeCopy(const SLimitNode* pSrc, SLimitNode* pDst) {
286 287
  COPY_SCALAR_FIELD(limit);
  COPY_SCALAR_FIELD(offset);
288
  return TSDB_CODE_SUCCESS;
289
}
290

291
static int32_t stateWindowNodeCopy(const SStateWindowNode* pSrc, SStateWindowNode* pDst) {
292 293
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pExpr);
294
  return TSDB_CODE_SUCCESS;
295 296
}

297
static int32_t sessionWindowNodeCopy(const SSessionWindowNode* pSrc, SSessionWindowNode* pDst) {
298 299
  CLONE_NODE_FIELD_EX(pCol, SColumnNode*);
  CLONE_NODE_FIELD_EX(pGap, SValueNode*);
300
  return TSDB_CODE_SUCCESS;
301 302
}

303
static int32_t intervalWindowNodeCopy(const SIntervalWindowNode* pSrc, SIntervalWindowNode* pDst) {
304 305 306 307 308
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pInterval);
  CLONE_NODE_FIELD(pOffset);
  CLONE_NODE_FIELD(pSliding);
  CLONE_NODE_FIELD(pFill);
309
  return TSDB_CODE_SUCCESS;
310 311
}

312
static int32_t nodeListNodeCopy(const SNodeListNode* pSrc, SNodeListNode* pDst) {
313
  COPY_OBJECT_FIELD(dataType, sizeof(SDataType));
X
Xiaoyu Wang 已提交
314
  CLONE_NODE_LIST_FIELD(pNodeList);
315
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
316 317
}

318
static int32_t fillNodeCopy(const SFillNode* pSrc, SFillNode* pDst) {
319
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
320
  CLONE_NODE_FIELD(pValues);
X
Xiaoyu Wang 已提交
321
  CLONE_NODE_FIELD(pWStartTs);
322
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
323
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
324 325
}

326
static int32_t logicNodeCopy(const SLogicNode* pSrc, SLogicNode* pDst) {
327 328 329
  CLONE_NODE_LIST_FIELD(pTargets);
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
330
  COPY_SCALAR_FIELD(optimizedFlag);
331
  COPY_SCALAR_FIELD(precision);
332 333
  CLONE_NODE_FIELD(pLimit);
  CLONE_NODE_FIELD(pSlimit);
334
  return TSDB_CODE_SUCCESS;
335 336
}

337
static int32_t logicScanCopy(const SScanLogicNode* pSrc, SScanLogicNode* pDst) {
338 339
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pScanCols);
340
  CLONE_NODE_LIST_FIELD(pScanPseudoCols);
X
Xiaoyu Wang 已提交
341 342
  COPY_SCALAR_FIELD(tableType);
  COPY_SCALAR_FIELD(tableId);
X
Xiaoyu Wang 已提交
343
  COPY_SCALAR_FIELD(stableId);
344
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
345 346 347 348 349 350
  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);
351
  CLONE_NODE_LIST_FIELD(pDynamicScanFuncs);
352 353 354 355 356 357
  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 已提交
358
  CLONE_NODE_FIELD(pTagCond);
X
Xiaoyu Wang 已提交
359
  CLONE_NODE_FIELD(pTagIndexCond);
5
54liuyao 已提交
360 361
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
362
  COPY_SCALAR_FIELD(igExpired);
363
  CLONE_NODE_LIST_FIELD(pGroupTags);
364
  COPY_SCALAR_FIELD(groupSort);
365
  return TSDB_CODE_SUCCESS;
366 367
}

368
static int32_t logicJoinCopy(const SJoinLogicNode* pSrc, SJoinLogicNode* pDst) {
369
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
370
  COPY_SCALAR_FIELD(joinType);
371
  CLONE_NODE_FIELD(pMergeCondition);
372
  CLONE_NODE_FIELD(pOnConditions);
X
Xiaoyu Wang 已提交
373
  COPY_SCALAR_FIELD(isSingleTableJoin);
374
  return TSDB_CODE_SUCCESS;
375 376
}

377
static int32_t logicAggCopy(const SAggLogicNode* pSrc, SAggLogicNode* pDst) {
378 379 380
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pGroupKeys);
  CLONE_NODE_LIST_FIELD(pAggFuncs);
381
  return TSDB_CODE_SUCCESS;
382 383
}

384
static int32_t logicProjectCopy(const SProjectLogicNode* pSrc, SProjectLogicNode* pDst) {
385 386
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pProjections);
387
  COPY_CHAR_ARRAY_FIELD(stmtName);
388
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
389 390
}

391
static int32_t logicVnodeModifCopy(const SVnodeModifyLogicNode* pSrc, SVnodeModifyLogicNode* pDst) {
X
Xiaoyu Wang 已提交
392
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
X
Xiaoyu Wang 已提交
393
  COPY_SCALAR_FIELD(modifyType);
394
  COPY_SCALAR_FIELD(msgType);
X
Xiaoyu Wang 已提交
395
  CLONE_NODE_FIELD(pAffectedRows);
X
Xiaoyu Wang 已提交
396
  COPY_SCALAR_FIELD(tableId);
397
  COPY_SCALAR_FIELD(stableId);
X
Xiaoyu Wang 已提交
398 399 400
  COPY_SCALAR_FIELD(tableType);
  COPY_CHAR_ARRAY_FIELD(tableFName);
  COPY_OBJECT_FIELD(deleteTimeRange, sizeof(STimeWindow));
401
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
402
  CLONE_NODE_LIST_FIELD(pInsertCols);
403
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
404 405
}

406
static int32_t logicExchangeCopy(const SExchangeLogicNode* pSrc, SExchangeLogicNode* pDst) {
X
Xiaoyu Wang 已提交
407
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
408
  COPY_SCALAR_FIELD(srcGroupId);
409
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
410 411
}

412
static int32_t logicMergeCopy(const SMergeLogicNode* pSrc, SMergeLogicNode* pDst) {
X
Xiaoyu Wang 已提交
413 414
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pMergeKeys);
X
Xiaoyu Wang 已提交
415
  CLONE_NODE_LIST_FIELD(pInputs);
X
Xiaoyu Wang 已提交
416 417
  COPY_SCALAR_FIELD(numOfChannels);
  COPY_SCALAR_FIELD(srcGroupId);
418
  COPY_SCALAR_FIELD(groupSort);
419
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
420 421
}

422
static int32_t logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pDst) {
X
Xiaoyu Wang 已提交
423
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
424
  COPY_SCALAR_FIELD(winType);
X
Xiaoyu Wang 已提交
425
  CLONE_NODE_LIST_FIELD(pFuncs);
426 427 428 429 430 431
  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 已提交
432
  CLONE_NODE_FIELD(pTspk);
5
54liuyao 已提交
433
  CLONE_NODE_FIELD(pTsEnd);
434 435 436
  CLONE_NODE_FIELD(pStateExpr);
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
437
  COPY_SCALAR_FIELD(igExpired);
438
  COPY_SCALAR_FIELD(windowAlgo);
439
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
440 441
}

442
static int32_t logicFillCopy(const SFillLogicNode* pSrc, SFillLogicNode* pDst) {
X
Xiaoyu Wang 已提交
443
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
444
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
445 446
  CLONE_NODE_FIELD(pWStartTs);
  CLONE_NODE_FIELD(pValues);
447
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
448
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
449 450
}

451
static int32_t logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) {
X
Xiaoyu Wang 已提交
452 453
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pSortKeys);
454
  COPY_SCALAR_FIELD(groupSort);
455
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
456 457
}

458
static int32_t logicPartitionCopy(const SPartitionLogicNode* pSrc, SPartitionLogicNode* pDst) {
459 460
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
461
  return TSDB_CODE_SUCCESS;
462 463
}

464
static int32_t logicIndefRowsFuncCopy(const SIndefRowsFuncLogicNode* pSrc, SIndefRowsFuncLogicNode* pDst) {
465
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
X
Xiaoyu Wang 已提交
466
  CLONE_NODE_LIST_FIELD(pFuncs);
467
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
468 469
}

470
static int32_t logicInterpFuncCopy(const SInterpFuncLogicNode* pSrc, SInterpFuncLogicNode* pDst) {
X
Xiaoyu Wang 已提交
471 472 473 474
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pFuncs);
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
  COPY_SCALAR_FIELD(interval);
X
Xiaoyu Wang 已提交
475 476 477
  COPY_SCALAR_FIELD(fillMode);
  CLONE_NODE_FIELD(pFillValues);
  CLONE_NODE_FIELD(pTimeSeries);
478
  return TSDB_CODE_SUCCESS;
479 480
}

481
static int32_t logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) {
482
  COPY_OBJECT_FIELD(id, sizeof(SSubplanId));
483
  CLONE_NODE_FIELD_EX(pNode, SLogicNode*);
484 485 486
  COPY_SCALAR_FIELD(subplanType);
  COPY_SCALAR_FIELD(level);
  COPY_SCALAR_FIELD(splitFlag);
487
  return TSDB_CODE_SUCCESS;
488 489
}

490
static int32_t physiNodeCopy(const SPhysiNode* pSrc, SPhysiNode* pDst) {
491
  CLONE_NODE_FIELD_EX(pOutputDataBlockDesc, SDataBlockDescNode*);
X
Xiaoyu Wang 已提交
492 493
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
494
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
495 496
}

497
static int32_t physiScanCopy(const SScanPhysiNode* pSrc, SScanPhysiNode* pDst) {
498 499 500 501 502 503 504
  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));
505
  return TSDB_CODE_SUCCESS;
506 507
}

508
static int32_t physiTagScanCopy(const STagScanPhysiNode* pSrc, STagScanPhysiNode* pDst) {
509 510 511
  return physiScanCopy(pSrc, pDst);
}

512
static int32_t physiTableScanCopy(const STableScanPhysiNode* pSrc, STableScanPhysiNode* pDst) {
513 514 515 516 517 518
  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);
519
  CLONE_NODE_LIST_FIELD(pGroupTags);
520 521 522 523 524 525 526
  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);
527
  COPY_SCALAR_FIELD(igExpired);
528
  return TSDB_CODE_SUCCESS;
529 530
}

531
static int32_t physiSysTableScanCopy(const SSystemTableScanPhysiNode* pSrc, SSystemTableScanPhysiNode* pDst) {
532 533 534 535
  COPY_BASE_OBJECT_FIELD(scan, physiScanCopy);
  COPY_OBJECT_FIELD(mgmtEpSet, sizeof(SEpSet));
  COPY_SCALAR_FIELD(showRewrite);
  COPY_SCALAR_FIELD(accountId);
536
  return TSDB_CODE_SUCCESS;
537 538
}

539
static int32_t physiWindowCopy(const SWinodwPhysiNode* pSrc, SWinodwPhysiNode* pDst) {
X
Xiaoyu Wang 已提交
540 541 542 543
  COPY_BASE_OBJECT_FIELD(node, physiNodeCopy);
  CLONE_NODE_LIST_FIELD(pExprs);
  CLONE_NODE_LIST_FIELD(pFuncs);
  CLONE_NODE_FIELD(pTspk);
5
54liuyao 已提交
544
  CLONE_NODE_FIELD(pTsEnd);
X
Xiaoyu Wang 已提交
545 546
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
547
  COPY_SCALAR_FIELD(igExpired);
548
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
549 550
}

551
static int32_t physiIntervalCopy(const SIntervalPhysiNode* pSrc, SIntervalPhysiNode* pDst) {
X
Xiaoyu Wang 已提交
552 553 554 555 556 557
  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);
558
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
559 560
}

561
static int32_t physiSessionCopy(const SSessionWinodwPhysiNode* pSrc, SSessionWinodwPhysiNode* pDst) {
562 563
  COPY_BASE_OBJECT_FIELD(window, physiWindowCopy);
  COPY_SCALAR_FIELD(gap);
564
  return TSDB_CODE_SUCCESS;
565 566
}

567
static int32_t physiPartitionCopy(const SPartitionPhysiNode* pSrc, SPartitionPhysiNode* pDst) {
X
Xiaoyu Wang 已提交
568 569 570 571
  COPY_BASE_OBJECT_FIELD(node, physiNodeCopy);
  CLONE_NODE_LIST_FIELD(pExprs);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
  CLONE_NODE_LIST_FIELD(pTargets);
572
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
573 574
}

575
static int32_t dataBlockDescCopy(const SDataBlockDescNode* pSrc, SDataBlockDescNode* pDst) {
576
  COPY_SCALAR_FIELD(dataBlockId);
577
  CLONE_NODE_LIST_FIELD(pSlots);
578 579 580
  COPY_SCALAR_FIELD(totalRowSize);
  COPY_SCALAR_FIELD(outputRowSize);
  COPY_SCALAR_FIELD(precision);
581
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
582 583
}

584
static int32_t slotDescCopy(const SSlotDescNode* pSrc, SSlotDescNode* pDst) {
585 586 587 588 589
  COPY_SCALAR_FIELD(slotId);
  COPY_OBJECT_FIELD(dataType, sizeof(SDataType));
  COPY_SCALAR_FIELD(reserve);
  COPY_SCALAR_FIELD(output);
  COPY_SCALAR_FIELD(tag);
590
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
591 592
}

593
static int32_t downstreamSourceCopy(const SDownstreamSourceNode* pSrc, SDownstreamSourceNode* pDst) {
594 595 596
  COPY_OBJECT_FIELD(addr, sizeof(SQueryNodeAddr));
  COPY_SCALAR_FIELD(taskId);
  COPY_SCALAR_FIELD(schedId);
D
dapan1121 已提交
597
  COPY_SCALAR_FIELD(execId);
D
dapan1121 已提交
598
  COPY_SCALAR_FIELD(fetchMsgType);
599
  return TSDB_CODE_SUCCESS;
600 601
}

602
static int32_t selectStmtCopy(const SSelectStmt* pSrc, SSelectStmt* pDst) {
603
  COPY_SCALAR_FIELD(isDistinct);
604 605 606 607 608 609 610 611
  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);
612 613
  CLONE_NODE_FIELD_EX(pLimit, SLimitNode*);
  CLONE_NODE_FIELD_EX(pLimit, SLimitNode*);
614 615 616
  COPY_CHAR_ARRAY_FIELD(stmtName);
  COPY_SCALAR_FIELD(precision);
  COPY_SCALAR_FIELD(isEmptyResult);
617
  COPY_SCALAR_FIELD(isTimeLineResult);
618 619
  COPY_SCALAR_FIELD(hasAggFuncs);
  COPY_SCALAR_FIELD(hasRepeatScanFuncs);
620
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
621 622
}

623
SNode* nodesCloneNode(const SNode* pNode) {
X
Xiaoyu Wang 已提交
624 625 626
  if (NULL == pNode) {
    return NULL;
  }
627

X
Xiaoyu Wang 已提交
628 629
  SNode* pDst = nodesMakeNode(nodeType(pNode));
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
630
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
631 632
    return NULL;
  }
633 634

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

  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 已提交
780 781 782
}

SNodeList* nodesCloneList(const SNodeList* pList) {
X
Xiaoyu Wang 已提交
783 784 785 786
  if (NULL == pList) {
    return NULL;
  }

787 788
  SNodeList* pDst = NULL;
  SNode*     pNode;
X
Xiaoyu Wang 已提交
789
  FOREACH(pNode, pList) {
790 791 792
    int32_t code = nodesListMakeStrictAppend(&pDst, nodesCloneNode(pNode));
    if (TSDB_CODE_SUCCESS != code) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
793 794 795 796 797
      nodesDestroyList(pDst);
      return NULL;
    }
  }
  return pDst;
798
}