nodesCloneFuncs.c 24.1 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);
X
Xiaoyu Wang 已提交
319 320
  COPY_SCALAR_FIELD(tableType);
  COPY_SCALAR_FIELD(tableId);
X
Xiaoyu Wang 已提交
321
  COPY_SCALAR_FIELD(stableId);
322
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
323 324 325 326 327 328
  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);
329
  CLONE_NODE_LIST_FIELD(pDynamicScanFuncs);
330 331 332 333 334 335
  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 已提交
336
  CLONE_NODE_FIELD(pTagCond);
5
54liuyao 已提交
337 338 339 340
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
  COPY_SCALAR_FIELD(tsColId);
  COPY_SCALAR_FIELD(filesFactor);
341 342 343 344 345
  return (SNode*)pDst;
}

static SNode* logicJoinCopy(const SJoinLogicNode* pSrc, SJoinLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
346
  COPY_SCALAR_FIELD(joinType);
347
  CLONE_NODE_FIELD(pOnConditions);
X
Xiaoyu Wang 已提交
348
  COPY_SCALAR_FIELD(isSingleTableJoin);
349 350 351 352 353 354 355 356 357 358 359 360 361
  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);
362 363 364 365 366
  COPY_CHAR_ARRAY_FIELD(stmtName);
  COPY_SCALAR_FIELD(limit);
  COPY_SCALAR_FIELD(offset);
  COPY_SCALAR_FIELD(slimit);
  COPY_SCALAR_FIELD(soffset);
X
Xiaoyu Wang 已提交
367 368 369
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
370
static SNode* logicVnodeModifCopy(const SVnodeModifyLogicNode* pSrc, SVnodeModifyLogicNode* pDst) {
X
Xiaoyu Wang 已提交
371
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
X
Xiaoyu Wang 已提交
372
  COPY_SCALAR_FIELD(modifyType);
373
  COPY_SCALAR_FIELD(msgType);
X
Xiaoyu Wang 已提交
374
  CLONE_NODE_FIELD(pAffectedRows);
X
Xiaoyu Wang 已提交
375 376 377 378
  COPY_SCALAR_FIELD(tableId);
  COPY_SCALAR_FIELD(tableType);
  COPY_CHAR_ARRAY_FIELD(tableFName);
  COPY_OBJECT_FIELD(deleteTimeRange, sizeof(STimeWindow));
X
Xiaoyu Wang 已提交
379 380 381
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
382 383
static SNode* logicExchangeCopy(const SExchangeLogicNode* pSrc, SExchangeLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
384
  COPY_SCALAR_FIELD(srcGroupId);
X
Xiaoyu Wang 已提交
385 386 387 388 389 390
  return (SNode*)pDst;
}

static SNode* logicMergeCopy(const SMergeLogicNode* pSrc, SMergeLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pMergeKeys);
X
Xiaoyu Wang 已提交
391
  CLONE_NODE_LIST_FIELD(pInputs);
X
Xiaoyu Wang 已提交
392 393
  COPY_SCALAR_FIELD(numOfChannels);
  COPY_SCALAR_FIELD(srcGroupId);
X
Xiaoyu Wang 已提交
394 395 396
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
397 398
static SNode* logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
399
  COPY_SCALAR_FIELD(winType);
X
Xiaoyu Wang 已提交
400
  CLONE_NODE_LIST_FIELD(pFuncs);
401 402 403 404 405 406
  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 已提交
407
  CLONE_NODE_FIELD(pTspk);
408 409 410
  CLONE_NODE_FIELD(pStateExpr);
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
5
54liuyao 已提交
411
  COPY_SCALAR_FIELD(filesFactor);
X
Xiaoyu Wang 已提交
412
  COPY_SCALAR_FIELD(intervalAlgo);
X
Xiaoyu Wang 已提交
413 414 415
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
416 417
static SNode* logicFillCopy(const SFillLogicNode* pSrc, SFillLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
418
  COPY_SCALAR_FIELD(mode);
X
Xiaoyu Wang 已提交
419 420
  CLONE_NODE_FIELD(pWStartTs);
  CLONE_NODE_FIELD(pValues);
421
  COPY_OBJECT_FIELD(timeRange, sizeof(STimeWindow));
X
Xiaoyu Wang 已提交
422 423 424
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
425 426 427 428 429 430
static SNode* logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pSortKeys);
  return (SNode*)pDst;
}

431 432 433 434 435 436
static SNode* logicPartitionCopy(const SPartitionLogicNode* pSrc, SPartitionLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
  return (SNode*)pDst;
}

437 438 439 440 441 442
static SNode* logicIndefRowsFuncCopy(const SIndefRowsFuncLogicNode* pSrc, SIndefRowsFuncLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pVectorFuncs);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
443
static SNode* logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) {
444
  COPY_OBJECT_FIELD(id, sizeof(SSubplanId));
445
  CLONE_NODE_FIELD(pNode);
446 447 448
  COPY_SCALAR_FIELD(subplanType);
  COPY_SCALAR_FIELD(level);
  COPY_SCALAR_FIELD(splitFlag);
449 450 451
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
452 453 454 455 456 457 458
static SNode* physiNodeCopy(const SPhysiNode* pSrc, SPhysiNode* pDst) {
  CLONE_NODE_FIELD(pOutputDataBlockDesc);
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
  return (SNode*)pDst;
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
static SNode* physiScanCopy(const SScanPhysiNode* pSrc, SScanPhysiNode* pDst) {
  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));
  return (SNode*)pDst;
}

static SNode* physiTagScanCopy(const STagScanPhysiNode* pSrc, STagScanPhysiNode* pDst) {
  return physiScanCopy(pSrc, pDst);
}

static SNode* physiTableScanCopy(const STableScanPhysiNode* pSrc, STableScanPhysiNode* pDst) {
  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);
  CLONE_NODE_LIST_FIELD(pPartitionKeys);
  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);
  return (SNode*)pDst;
}

static SNode* physiSysTableScanCopy(const SSystemTableScanPhysiNode* pSrc, SSystemTableScanPhysiNode* pDst) {
  COPY_BASE_OBJECT_FIELD(scan, physiScanCopy);
  COPY_OBJECT_FIELD(mgmtEpSet, sizeof(SEpSet));
  COPY_SCALAR_FIELD(showRewrite);
  COPY_SCALAR_FIELD(accountId);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
static SNode* physiWindowCopy(const SWinodwPhysiNode* pSrc, SWinodwPhysiNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, physiNodeCopy);
  CLONE_NODE_LIST_FIELD(pExprs);
  CLONE_NODE_LIST_FIELD(pFuncs);
  CLONE_NODE_FIELD(pTspk);
  COPY_SCALAR_FIELD(triggerType);
  COPY_SCALAR_FIELD(watermark);
  COPY_SCALAR_FIELD(filesFactor);
  return (SNode*)pDst;
}

static SNode* physiIntervalCopy(const SIntervalPhysiNode* pSrc, SIntervalPhysiNode* pDst) {
  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);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
523
static SNode* dataBlockDescCopy(const SDataBlockDescNode* pSrc, SDataBlockDescNode* pDst) {
524
  COPY_SCALAR_FIELD(dataBlockId);
525
  CLONE_NODE_LIST_FIELD(pSlots);
526 527 528
  COPY_SCALAR_FIELD(totalRowSize);
  COPY_SCALAR_FIELD(outputRowSize);
  COPY_SCALAR_FIELD(precision);
X
Xiaoyu Wang 已提交
529 530 531 532
  return (SNode*)pDst;
}

static SNode* slotDescCopy(const SSlotDescNode* pSrc, SSlotDescNode* pDst) {
533 534 535 536 537
  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 已提交
538 539 540
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
541
static SNode* downstreamSourceCopy(const SDownstreamSourceNode* pSrc, SDownstreamSourceNode* pDst) {
542 543 544
  COPY_OBJECT_FIELD(addr, sizeof(SQueryNodeAddr));
  COPY_SCALAR_FIELD(taskId);
  COPY_SCALAR_FIELD(schedId);
545 546 547 548
  return (SNode*)pDst;
}

static SNode* selectStmtCopy(const SSelectStmt* pSrc, SSelectStmt* pDst) {
549
  COPY_SCALAR_FIELD(isDistinct);
550 551 552 553 554 555 556 557 558 559
  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);
560 561 562 563 564 565
  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 已提交
566 567 568
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
569
SNodeptr nodesCloneNode(const SNodeptr pNode) {
X
Xiaoyu Wang 已提交
570 571 572 573 574
  if (NULL == pNode) {
    return NULL;
  }
  SNode* pDst = nodesMakeNode(nodeType(pNode));
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
575
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589
    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:
590
      return realTableNodeCopy((const SRealTableNode*)pNode, (SRealTableNode*)pDst);
X
Xiaoyu Wang 已提交
591
    case QUERY_NODE_TEMP_TABLE:
592
      return tempTableNodeCopy((const STempTableNode*)pNode, (STempTableNode*)pDst);
X
Xiaoyu Wang 已提交
593
    case QUERY_NODE_JOIN_TABLE:
594
      return joinTableNodeCopy((const SJoinTableNode*)pNode, (SJoinTableNode*)pDst);
X
Xiaoyu Wang 已提交
595
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
596
      return groupingSetNodeCopy((const SGroupingSetNode*)pNode, (SGroupingSetNode*)pDst);
X
Xiaoyu Wang 已提交
597
    case QUERY_NODE_ORDER_BY_EXPR:
X
Xiaoyu Wang 已提交
598
      return orderByExprNodeCopy((const SOrderByExprNode*)pNode, (SOrderByExprNode*)pDst);
X
Xiaoyu Wang 已提交
599
    case QUERY_NODE_LIMIT:
600 601 602 603 604 605 606
      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 已提交
607 608
    case QUERY_NODE_NODE_LIST:
      return nodeListNodeCopy((const SNodeListNode*)pNode, (SNodeListNode*)pDst);
X
Xiaoyu Wang 已提交
609 610
    case QUERY_NODE_FILL:
      return fillNodeCopy((const SFillNode*)pNode, (SFillNode*)pDst);
611 612
    case QUERY_NODE_TARGET:
      return targetNodeCopy((const STargetNode*)pNode, (STargetNode*)pDst);
X
Xiaoyu Wang 已提交
613 614 615 616
    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 已提交
617 618
    case QUERY_NODE_DOWNSTREAM_SOURCE:
      return downstreamSourceCopy((const SDownstreamSourceNode*)pNode, (SDownstreamSourceNode*)pDst);
619 620
    case QUERY_NODE_LEFT_VALUE:
      return pDst;
621 622
    case QUERY_NODE_SELECT_STMT:
      return selectStmtCopy((const SSelectStmt*)pNode, (SSelectStmt*)pDst);
623 624
    case QUERY_NODE_LOGIC_PLAN_SCAN:
      return logicScanCopy((const SScanLogicNode*)pNode, (SScanLogicNode*)pDst);
625 626
    case QUERY_NODE_LOGIC_PLAN_JOIN:
      return logicJoinCopy((const SJoinLogicNode*)pNode, (SJoinLogicNode*)pDst);
627 628 629 630
    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);
X
Xiaoyu Wang 已提交
631 632
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY:
      return logicVnodeModifCopy((const SVnodeModifyLogicNode*)pNode, (SVnodeModifyLogicNode*)pDst);
X
Xiaoyu Wang 已提交
633 634
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      return logicExchangeCopy((const SExchangeLogicNode*)pNode, (SExchangeLogicNode*)pDst);
X
Xiaoyu Wang 已提交
635 636
    case QUERY_NODE_LOGIC_PLAN_MERGE:
      return logicMergeCopy((const SMergeLogicNode*)pNode, (SMergeLogicNode*)pDst);
X
Xiaoyu Wang 已提交
637 638
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
      return logicWindowCopy((const SWindowLogicNode*)pNode, (SWindowLogicNode*)pDst);
X
Xiaoyu Wang 已提交
639 640
    case QUERY_NODE_LOGIC_PLAN_FILL:
      return logicFillCopy((const SFillLogicNode*)pNode, (SFillLogicNode*)pDst);
X
Xiaoyu Wang 已提交
641 642
    case QUERY_NODE_LOGIC_PLAN_SORT:
      return logicSortCopy((const SSortLogicNode*)pNode, (SSortLogicNode*)pDst);
643 644
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
      return logicPartitionCopy((const SPartitionLogicNode*)pNode, (SPartitionLogicNode*)pDst);
645 646
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC:
      return logicIndefRowsFuncCopy((const SIndefRowsFuncLogicNode*)pNode, (SIndefRowsFuncLogicNode*)pDst);
647
    case QUERY_NODE_LOGIC_SUBPLAN:
X
Xiaoyu Wang 已提交
648
      return logicSubplanCopy((const SLogicSubplan*)pNode, (SLogicSubplan*)pDst);
649 650 651 652 653 654 655 656
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
      return physiTagScanCopy((const STagScanPhysiNode*)pNode, (STagScanPhysiNode*)pDst);
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
      return physiTableScanCopy((const STableScanPhysiNode*)pNode, (STableScanPhysiNode*)pDst);
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
      return physiSysTableScanCopy((const SSystemTableScanPhysiNode*)pNode, (SSystemTableScanPhysiNode*)pDst);
X
Xiaoyu Wang 已提交
657 658 659 660 661 662
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
      return physiIntervalCopy((const SIntervalPhysiNode*)pNode, (SIntervalPhysiNode*)pDst);
X
Xiaoyu Wang 已提交
663 664 665
    default:
      break;
  }
666 667 668
  nodesDestroyNode(pDst);
  nodesError("nodesCloneNode unknown node = %s", nodesNodeName(nodeType(pNode)));
  return NULL;
X
Xiaoyu Wang 已提交
669 670 671
}

SNodeList* nodesCloneList(const SNodeList* pList) {
X
Xiaoyu Wang 已提交
672 673 674 675
  if (NULL == pList) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
676 677
  SNodeList* pDst = nodesMakeList();
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
678
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
679 680 681 682 683 684 685 686 687 688 689 690
    return NULL;
  }
  SNode* pNode;
  FOREACH(pNode, pList) {
    SNode* pNewNode = nodesCloneNode(pNode);
    if (NULL == pNewNode) {
      nodesDestroyList(pDst);
      return NULL;
    }
    nodesListAppend(pDst, pNewNode);
  }
  return pDst;
691
}