nodesCloneFuncs.c 20.2 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); \
X
Xiaoyu Wang 已提交
43
  } while (0)
X
Xiaoyu Wang 已提交
44

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

#define CLONE_NODE_LIST_FIELD(fldname)                 \
  do {                                                 \
    if (NULL == (pSrc)->fldname) {                     \
      break;                                           \
    }                                                  \
X
Xiaoyu Wang 已提交
62
    (pDst)->fldname = nodesCloneList((pSrc)->fldname); \
X
Xiaoyu Wang 已提交
63 64 65 66 67 68 69 70 71 72 73
    if (NULL == (pDst)->fldname) {                     \
      nodesDestroyNode((SNode*)(pDst));                \
      return NULL;                                     \
    }                                                  \
  } while (0)

#define CLONE_OBJECT_FIELD(fldname, cloneFunc)    \
  do {                                            \
    if (NULL == (pSrc)->fldname) {                \
      break;                                      \
    }                                             \
74
    (pDst)->fldname = cloneFunc((pSrc)->fldname); \
X
Xiaoyu Wang 已提交
75 76 77 78 79 80 81 82
    if (NULL == (pDst)->fldname) {                \
      nodesDestroyNode((SNode*)(pDst));           \
      return NULL;                                \
    }                                             \
  } while (0)

#define COPY_BASE_OBJECT_FIELD(fldname, copyFunc)                   \
  do {                                                              \
83
    if (NULL == copyFunc(&((pSrc)->fldname), &((pDst)->fldname))) { \
X
Xiaoyu Wang 已提交
84 85 86
      return NULL;                                                  \
    }                                                               \
  } while (0)
87

88
static SNode* exprNodeCopy(const SExprNode* pSrc, SExprNode* pDst) {
89 90 91
  COPY_OBJECT_FIELD(resType, sizeof(SDataType));
  COPY_CHAR_ARRAY_FIELD(aliasName);
  COPY_CHAR_ARRAY_FIELD(userAlias);
92
  return (SNode*)pDst;
X
Xiaoyu Wang 已提交
93 94 95
}

static SNode* columnNodeCopy(const SColumnNode* pSrc, SColumnNode* pDst) {
96
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
97 98 99 100
  COPY_SCALAR_FIELD(tableId);
  COPY_SCALAR_FIELD(tableType);
  COPY_SCALAR_FIELD(colId);
  COPY_SCALAR_FIELD(colType);
X
Xiaoyu Wang 已提交
101
  COPY_SCALAR_FIELD(hasIndex);
102 103 104 105 106 107
  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);
X
Xiaoyu Wang 已提交
108 109 110 111
  return (SNode*)pDst;
}

static SNode* valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
112
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
X
Xiaoyu Wang 已提交
113
  COPY_CHAR_POINT_FIELD(literal);
114 115
  COPY_SCALAR_FIELD(isDuration);
  COPY_SCALAR_FIELD(translate);
116
  COPY_SCALAR_FIELD(notReserved);
117 118 119
  COPY_SCALAR_FIELD(placeholderNo);
  COPY_SCALAR_FIELD(typeData);
  COPY_SCALAR_FIELD(unit);
X
Xiaoyu Wang 已提交
120 121 122
  if (!pSrc->translate) {
    return (SNode*)pDst;
  }
X
Xiaoyu Wang 已提交
123
  switch (pSrc->node.resType.type) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    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 已提交
144 145
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
146
    case TSDB_DATA_TYPE_VARBINARY: {
147 148
      int32_t len = varDataTLen(pSrc->datum.p) + 1;
      pDst->datum.p = taosMemoryCalloc(1, len);
X
Xiaoyu Wang 已提交
149 150 151 152
      if (NULL == pDst->datum.p) {
        nodesDestroyNode(pDst);
        return NULL;
      }
153
      memcpy(pDst->datum.p, pSrc->datum.p, len);
X
Xiaoyu Wang 已提交
154
      break;
155
    }
X
Xiaoyu Wang 已提交
156 157 158
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
159
    case TSDB_DATA_TYPE_MEDIUMBLOB:
X
Xiaoyu Wang 已提交
160 161 162 163 164 165 166
    default:
      break;
  }
  return (SNode*)pDst;
}

static SNode* operatorNodeCopy(const SOperatorNode* pSrc, SOperatorNode* pDst) {
167
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
168
  COPY_SCALAR_FIELD(opType);
169 170
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
X
Xiaoyu Wang 已提交
171 172 173 174
  return (SNode*)pDst;
}

static SNode* logicConditionNodeCopy(const SLogicConditionNode* pSrc, SLogicConditionNode* pDst) {
175
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
176
  COPY_SCALAR_FIELD(condType);
177
  CLONE_NODE_LIST_FIELD(pParameterList);
X
Xiaoyu Wang 已提交
178 179 180 181
  return (SNode*)pDst;
}

static SNode* functionNodeCopy(const SFunctionNode* pSrc, SFunctionNode* pDst) {
182
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
183 184 185
  COPY_CHAR_ARRAY_FIELD(functionName);
  COPY_SCALAR_FIELD(funcId);
  COPY_SCALAR_FIELD(funcType);
186
  CLONE_NODE_LIST_FIELD(pParameterList);
187
  COPY_SCALAR_FIELD(udfBufSize);
X
Xiaoyu Wang 已提交
188 189 190
  return (SNode*)pDst;
}

191 192
static SNode* tableNodeCopy(const STableNode* pSrc, STableNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, exprNodeCopy);
193 194 195 196
  COPY_CHAR_ARRAY_FIELD(dbName);
  COPY_CHAR_ARRAY_FIELD(tableName);
  COPY_CHAR_ARRAY_FIELD(tableAlias);
  COPY_SCALAR_FIELD(precision);
X
Xiaoyu Wang 已提交
197
  COPY_SCALAR_FIELD(singleTable);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  return (SNode*)pDst;
}

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

static SNode* realTableNodeCopy(const SRealTableNode* pSrc, SRealTableNode* pDst) {
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
  CLONE_OBJECT_FIELD(pMeta, tableMetaClone);
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
225 226
  COPY_CHAR_ARRAY_FIELD(qualDbName);
  COPY_SCALAR_FIELD(ratio);
227 228 229 230 231 232 233 234 235 236 237
  return (SNode*)pDst;
}

static SNode* tempTableNodeCopy(const STempTableNode* pSrc, STempTableNode* pDst) {
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
  CLONE_NODE_FIELD(pSubquery);
  return (SNode*)pDst;
}

static SNode* joinTableNodeCopy(const SJoinTableNode* pSrc, SJoinTableNode* pDst) {
  COPY_BASE_OBJECT_FIELD(table, tableNodeCopy);
238
  COPY_SCALAR_FIELD(joinType);
239 240 241 242 243 244
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
  CLONE_NODE_FIELD(pOnCond);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
245
static SNode* targetNodeCopy(const STargetNode* pSrc, STargetNode* pDst) {
246 247
  COPY_SCALAR_FIELD(dataBlockId);
  COPY_SCALAR_FIELD(slotId);
248
  CLONE_NODE_FIELD(pExpr);
X
Xiaoyu Wang 已提交
249 250 251
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
252
static SNode* groupingSetNodeCopy(const SGroupingSetNode* pSrc, SGroupingSetNode* pDst) {
253
  COPY_SCALAR_FIELD(groupingSetType);
254 255 256 257
  CLONE_NODE_LIST_FIELD(pParameterList);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
258 259
static SNode* orderByExprNodeCopy(const SOrderByExprNode* pSrc, SOrderByExprNode* pDst) {
  CLONE_NODE_FIELD(pExpr);
260 261
  COPY_SCALAR_FIELD(order);
  COPY_SCALAR_FIELD(nullOrder);
X
Xiaoyu Wang 已提交
262 263 264
  return (SNode*)pDst;
}

265 266 267 268 269
static SNode* limitNodeCopy(const SLimitNode* pSrc, SLimitNode* pDst) {
  COPY_SCALAR_FIELD(limit);
  COPY_SCALAR_FIELD(offset);
  return (SNode*)pDst;
}
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

static SNode* stateWindowNodeCopy(const SStateWindowNode* pSrc, SStateWindowNode* pDst) {
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pExpr);
  return (SNode*)pDst;
}

static SNode* sessionWindowNodeCopy(const SSessionWindowNode* pSrc, SSessionWindowNode* pDst) {
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pGap);
  return (SNode*)pDst;
}

static SNode* intervalWindowNodeCopy(const SIntervalWindowNode* pSrc, SIntervalWindowNode* pDst) {
  CLONE_NODE_FIELD(pCol);
  CLONE_NODE_FIELD(pInterval);
  CLONE_NODE_FIELD(pOffset);
  CLONE_NODE_FIELD(pSliding);
  CLONE_NODE_FIELD(pFill);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
292
static SNode* nodeListNodeCopy(const SNodeListNode* pSrc, SNodeListNode* pDst) {
293
  COPY_OBJECT_FIELD(dataType, sizeof(SDataType));
X
Xiaoyu Wang 已提交
294 295 296 297
  CLONE_NODE_LIST_FIELD(pNodeList);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
298
static SNode* fillNodeCopy(const SFillNode* pSrc, SFillNode* pDst) {
299
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
300
  CLONE_NODE_FIELD(pValues);
X
Xiaoyu Wang 已提交
301
  CLONE_NODE_FIELD(pWStartTs);
302
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
X
Xiaoyu Wang 已提交
303 304 305
  return (SNode*)pDst;
}

306 307 308 309
static SNode* logicNodeCopy(const SLogicNode* pSrc, SLogicNode* pDst) {
  CLONE_NODE_LIST_FIELD(pTargets);
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
310
  COPY_SCALAR_FIELD(optimizedFlag);
311
  COPY_SCALAR_FIELD(precision);
312 313 314 315 316 317
  return (SNode*)pDst;
}

static SNode* logicScanCopy(const SScanLogicNode* pSrc, SScanLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pScanCols);
318
  CLONE_NODE_LIST_FIELD(pScanPseudoCols);
319 320
  CLONE_OBJECT_FIELD(pMeta, tableMetaClone);
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
321 322 323 324 325 326
  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);
327
  CLONE_NODE_LIST_FIELD(pDynamicScanFuncs);
328 329 330 331 332 333
  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 已提交
334
  CLONE_NODE_FIELD(pTagCond);
5
54liuyao 已提交
335 336 337 338
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
  COPY_SCALAR_FIELD(tsColId);
  COPY_SCALAR_FIELD(filesFactor);
339 340 341 342 343
  return (SNode*)pDst;
}

static SNode* logicJoinCopy(const SJoinLogicNode* pSrc, SJoinLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
344
  COPY_SCALAR_FIELD(joinType);
345
  CLONE_NODE_FIELD(pOnConditions);
X
Xiaoyu Wang 已提交
346
  COPY_SCALAR_FIELD(isSingleTableJoin);
347 348 349 350 351 352 353 354 355 356 357 358 359
  return (SNode*)pDst;
}

static SNode* logicAggCopy(const SAggLogicNode* pSrc, SAggLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pGroupKeys);
  CLONE_NODE_LIST_FIELD(pAggFuncs);
  return (SNode*)pDst;
}

static SNode* logicProjectCopy(const SProjectLogicNode* pSrc, SProjectLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pProjections);
360 361 362 363 364
  COPY_CHAR_ARRAY_FIELD(stmtName);
  COPY_SCALAR_FIELD(limit);
  COPY_SCALAR_FIELD(offset);
  COPY_SCALAR_FIELD(slimit);
  COPY_SCALAR_FIELD(soffset);
X
Xiaoyu Wang 已提交
365 366 367
  return (SNode*)pDst;
}

368
static SNode* logicVnodeModifCopy(const SVnodeModifLogicNode* pSrc, SVnodeModifLogicNode* pDst) {
X
Xiaoyu Wang 已提交
369
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
370
  COPY_SCALAR_FIELD(msgType);
X
Xiaoyu Wang 已提交
371 372 373
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
374 375
static SNode* logicExchangeCopy(const SExchangeLogicNode* pSrc, SExchangeLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
376
  COPY_SCALAR_FIELD(srcGroupId);
X
Xiaoyu Wang 已提交
377 378 379 380 381 382 383 384
  return (SNode*)pDst;
}

static SNode* logicMergeCopy(const SMergeLogicNode* pSrc, SMergeLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pMergeKeys);
  COPY_SCALAR_FIELD(numOfChannels);
  COPY_SCALAR_FIELD(srcGroupId);
X
Xiaoyu Wang 已提交
385 386 387
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
388 389
static SNode* logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
390
  COPY_SCALAR_FIELD(winType);
X
Xiaoyu Wang 已提交
391
  CLONE_NODE_LIST_FIELD(pFuncs);
392 393 394 395 396 397
  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 已提交
398
  CLONE_NODE_FIELD(pTspk);
399 400 401
  CLONE_NODE_FIELD(pStateExpr);
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
5
54liuyao 已提交
402
  COPY_SCALAR_FIELD(filesFactor);
403
  COPY_SCALAR_FIELD(stmInterAlgo);
X
Xiaoyu Wang 已提交
404 405 406
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
407 408
static SNode* logicFillCopy(const SFillLogicNode* pSrc, SFillLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
409
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
410 411
  CLONE_NODE_FIELD(pWStartTs);
  CLONE_NODE_FIELD(pValues);
412
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
X
Xiaoyu Wang 已提交
413 414 415
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
416 417 418 419 420 421
static SNode* logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pSortKeys);
  return (SNode*)pDst;
}

422 423 424 425 426 427
static SNode* logicPartitionCopy(const SPartitionLogicNode* pSrc, SPartitionLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
428
static SNode* logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) {
429
  COPY_OBJECT_FIELD(id, sizeof(SSubplanId));
430
  CLONE_NODE_FIELD(pNode);
431 432 433
  COPY_SCALAR_FIELD(subplanType);
  COPY_SCALAR_FIELD(level);
  COPY_SCALAR_FIELD(splitFlag);
434 435 436
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
437
static SNode* dataBlockDescCopy(const SDataBlockDescNode* pSrc, SDataBlockDescNode* pDst) {
438
  COPY_SCALAR_FIELD(dataBlockId);
439
  CLONE_NODE_LIST_FIELD(pSlots);
440 441 442
  COPY_SCALAR_FIELD(totalRowSize);
  COPY_SCALAR_FIELD(outputRowSize);
  COPY_SCALAR_FIELD(precision);
X
Xiaoyu Wang 已提交
443 444 445 446
  return (SNode*)pDst;
}

static SNode* slotDescCopy(const SSlotDescNode* pSrc, SSlotDescNode* pDst) {
447 448 449 450 451
  COPY_SCALAR_FIELD(slotId);
  COPY_OBJECT_FIELD(dataType, sizeof(SDataType));
  COPY_SCALAR_FIELD(reserve);
  COPY_SCALAR_FIELD(output);
  COPY_SCALAR_FIELD(tag);
X
Xiaoyu Wang 已提交
452 453 454
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
455
static SNode* downstreamSourceCopy(const SDownstreamSourceNode* pSrc, SDownstreamSourceNode* pDst) {
456 457 458
  COPY_OBJECT_FIELD(addr, sizeof(SQueryNodeAddr));
  COPY_SCALAR_FIELD(taskId);
  COPY_SCALAR_FIELD(schedId);
459 460 461 462
  return (SNode*)pDst;
}

static SNode* selectStmtCopy(const SSelectStmt* pSrc, SSelectStmt* pDst) {
463
  COPY_SCALAR_FIELD(isDistinct);
464 465 466 467 468 469 470 471 472 473
  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);
  CLONE_NODE_FIELD(pLimit);
  CLONE_NODE_FIELD(pLimit);
474 475 476 477 478 479
  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);
X
Xiaoyu Wang 已提交
480 481 482
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
483
SNodeptr nodesCloneNode(const SNodeptr pNode) {
X
Xiaoyu Wang 已提交
484 485 486 487 488
  if (NULL == pNode) {
    return NULL;
  }
  SNode* pDst = nodesMakeNode(nodeType(pNode));
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
489
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503
    return NULL;
  }
  switch (nodeType(pNode)) {
    case QUERY_NODE_COLUMN:
      return columnNodeCopy((const SColumnNode*)pNode, (SColumnNode*)pDst);
    case QUERY_NODE_VALUE:
      return valueNodeCopy((const SValueNode*)pNode, (SValueNode*)pDst);
    case QUERY_NODE_OPERATOR:
      return operatorNodeCopy((const SOperatorNode*)pNode, (SOperatorNode*)pDst);
    case QUERY_NODE_LOGIC_CONDITION:
      return logicConditionNodeCopy((const SLogicConditionNode*)pNode, (SLogicConditionNode*)pDst);
    case QUERY_NODE_FUNCTION:
      return functionNodeCopy((const SFunctionNode*)pNode, (SFunctionNode*)pDst);
    case QUERY_NODE_REAL_TABLE:
504
      return realTableNodeCopy((const SRealTableNode*)pNode, (SRealTableNode*)pDst);
X
Xiaoyu Wang 已提交
505
    case QUERY_NODE_TEMP_TABLE:
506
      return tempTableNodeCopy((const STempTableNode*)pNode, (STempTableNode*)pDst);
X
Xiaoyu Wang 已提交
507
    case QUERY_NODE_JOIN_TABLE:
508
      return joinTableNodeCopy((const SJoinTableNode*)pNode, (SJoinTableNode*)pDst);
X
Xiaoyu Wang 已提交
509
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
510
      return groupingSetNodeCopy((const SGroupingSetNode*)pNode, (SGroupingSetNode*)pDst);
X
Xiaoyu Wang 已提交
511
    case QUERY_NODE_ORDER_BY_EXPR:
X
Xiaoyu Wang 已提交
512
      return orderByExprNodeCopy((const SOrderByExprNode*)pNode, (SOrderByExprNode*)pDst);
X
Xiaoyu Wang 已提交
513
    case QUERY_NODE_LIMIT:
514 515 516 517 518 519 520
      return limitNodeCopy((const SLimitNode*)pNode, (SLimitNode*)pDst);
    case QUERY_NODE_STATE_WINDOW:
      return stateWindowNodeCopy((const SStateWindowNode*)pNode, (SStateWindowNode*)pDst);
    case QUERY_NODE_SESSION_WINDOW:
      return sessionWindowNodeCopy((const SSessionWindowNode*)pNode, (SSessionWindowNode*)pDst);
    case QUERY_NODE_INTERVAL_WINDOW:
      return intervalWindowNodeCopy((const SIntervalWindowNode*)pNode, (SIntervalWindowNode*)pDst);
X
Xiaoyu Wang 已提交
521 522
    case QUERY_NODE_NODE_LIST:
      return nodeListNodeCopy((const SNodeListNode*)pNode, (SNodeListNode*)pDst);
X
Xiaoyu Wang 已提交
523 524
    case QUERY_NODE_FILL:
      return fillNodeCopy((const SFillNode*)pNode, (SFillNode*)pDst);
525 526
    case QUERY_NODE_TARGET:
      return targetNodeCopy((const STargetNode*)pNode, (STargetNode*)pDst);
X
Xiaoyu Wang 已提交
527 528 529 530
    case QUERY_NODE_DATABLOCK_DESC:
      return dataBlockDescCopy((const SDataBlockDescNode*)pNode, (SDataBlockDescNode*)pDst);
    case QUERY_NODE_SLOT_DESC:
      return slotDescCopy((const SSlotDescNode*)pNode, (SSlotDescNode*)pDst);
X
Xiaoyu Wang 已提交
531 532
    case QUERY_NODE_DOWNSTREAM_SOURCE:
      return downstreamSourceCopy((const SDownstreamSourceNode*)pNode, (SDownstreamSourceNode*)pDst);
533 534
    case QUERY_NODE_LEFT_VALUE:
      return pDst;
535 536
    case QUERY_NODE_SELECT_STMT:
      return selectStmtCopy((const SSelectStmt*)pNode, (SSelectStmt*)pDst);
537 538
    case QUERY_NODE_LOGIC_PLAN_SCAN:
      return logicScanCopy((const SScanLogicNode*)pNode, (SScanLogicNode*)pDst);
539 540
    case QUERY_NODE_LOGIC_PLAN_JOIN:
      return logicJoinCopy((const SJoinLogicNode*)pNode, (SJoinLogicNode*)pDst);
541 542 543 544 545 546
    case QUERY_NODE_LOGIC_PLAN_AGG:
      return logicAggCopy((const SAggLogicNode*)pNode, (SAggLogicNode*)pDst);
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
      return logicProjectCopy((const SProjectLogicNode*)pNode, (SProjectLogicNode*)pDst);
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIF:
      return logicVnodeModifCopy((const SVnodeModifLogicNode*)pNode, (SVnodeModifLogicNode*)pDst);
X
Xiaoyu Wang 已提交
547 548
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      return logicExchangeCopy((const SExchangeLogicNode*)pNode, (SExchangeLogicNode*)pDst);
X
Xiaoyu Wang 已提交
549 550
    case QUERY_NODE_LOGIC_PLAN_MERGE:
      return logicMergeCopy((const SMergeLogicNode*)pNode, (SMergeLogicNode*)pDst);
X
Xiaoyu Wang 已提交
551 552
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
      return logicWindowCopy((const SWindowLogicNode*)pNode, (SWindowLogicNode*)pDst);
X
Xiaoyu Wang 已提交
553 554
    case QUERY_NODE_LOGIC_PLAN_FILL:
      return logicFillCopy((const SFillLogicNode*)pNode, (SFillLogicNode*)pDst);
X
Xiaoyu Wang 已提交
555 556
    case QUERY_NODE_LOGIC_PLAN_SORT:
      return logicSortCopy((const SSortLogicNode*)pNode, (SSortLogicNode*)pDst);
557 558
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
      return logicPartitionCopy((const SPartitionLogicNode*)pNode, (SPartitionLogicNode*)pDst);
559
    case QUERY_NODE_LOGIC_SUBPLAN:
X
Xiaoyu Wang 已提交
560
      return logicSubplanCopy((const SLogicSubplan*)pNode, (SLogicSubplan*)pDst);
X
Xiaoyu Wang 已提交
561 562 563
    default:
      break;
  }
564 565 566
  nodesDestroyNode(pDst);
  nodesError("nodesCloneNode unknown node = %s", nodesNodeName(nodeType(pNode)));
  return NULL;
X
Xiaoyu Wang 已提交
567 568 569
}

SNodeList* nodesCloneList(const SNodeList* pList) {
X
Xiaoyu Wang 已提交
570 571 572 573
  if (NULL == pList) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
574 575
  SNodeList* pDst = nodesMakeList();
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
576
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
577 578 579 580 581 582 583 584 585 586 587 588
    return NULL;
  }
  SNode* pNode;
  FOREACH(pNode, pList) {
    SNode* pNewNode = nodesCloneNode(pNode);
    if (NULL == pNewNode) {
      nodesDestroyList(pDst);
      return NULL;
    }
    nodesListAppend(pDst, pNewNode);
  }
  return pDst;
589
}