nodesCloneFuncs.c 12.6 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

X
Xiaoyu Wang 已提交
22 23 24 25 26
#define COPY_ALL_SCALAR_FIELDS \
	do { \
    memcpy((pDst), (pSrc), sizeof(*pSrc)); \
	} while (0)

X
Xiaoyu Wang 已提交
27 28 29 30
#define COPY_SCALAR_FIELD(fldname) \
	do { \
    (pDst)->fldname = (pSrc)->fldname; \
	} while (0)
31

X
Xiaoyu Wang 已提交
32 33 34 35 36 37 38
#define COPY_CHAR_ARRAY_FIELD(fldname) \
	do { \
    strcpy((pDst)->fldname, (pSrc)->fldname); \
	} while (0)

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

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

57
#define CLONE_NODE_LIST_FIELD(fldname) \
X
Xiaoyu Wang 已提交
58
	do { \
59 60 61
    if (NULL == (pSrc)->fldname) { \
      break; \
    } \
X
Xiaoyu Wang 已提交
62 63 64 65 66 67 68
    (pDst)->fldname = nodesCloneList((pSrc)->fldname); \
    if (NULL == (pDst)->fldname) { \
      nodesDestroyNode((SNode*)(pDst)); \
      return NULL; \
    } \
	} while (0)

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

#define COPY_BASE_OBJECT_FIELD(fldname, copyFunc) \
	do { \
    if (NULL == copyFunc(&((pSrc)->fldname), &((pDst)->fldname))) { \
      return NULL; \
    } \
	} while (0)

X
Xiaoyu Wang 已提交
88 89 90 91 92 93 94 95 96 97
static void dataTypeCopy(const SDataType* pSrc, SDataType* pDst) {
  COPY_SCALAR_FIELD(type);
  COPY_SCALAR_FIELD(precision);
  COPY_SCALAR_FIELD(scale);
  COPY_SCALAR_FIELD(bytes);
}

static void exprNodeCopy(const SExprNode* pSrc, SExprNode* pDst) {
  dataTypeCopy(&pSrc->resType, &pDst->resType);
  COPY_CHAR_ARRAY_FIELD(aliasName);
98
  // CLONE_NODE_LIST_FIELD(pAssociationList);
X
Xiaoyu Wang 已提交
99 100 101
}

static SNode* columnNodeCopy(const SColumnNode* pSrc, SColumnNode* pDst) {
X
Xiaoyu Wang 已提交
102
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
103 104 105 106 107 108
  COPY_SCALAR_FIELD(colId);
  COPY_SCALAR_FIELD(colType);
  COPY_CHAR_ARRAY_FIELD(dbName);
  COPY_CHAR_ARRAY_FIELD(tableName);
  COPY_CHAR_ARRAY_FIELD(tableAlias);
  COPY_CHAR_ARRAY_FIELD(colName);
X
Xiaoyu Wang 已提交
109 110
  COPY_SCALAR_FIELD(dataBlockId);
  COPY_SCALAR_FIELD(slotId);
X
Xiaoyu Wang 已提交
111 112 113 114
  return (SNode*)pDst;
}

static SNode* valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
X
Xiaoyu Wang 已提交
115
  COPY_ALL_SCALAR_FIELDS;
X
Xiaoyu Wang 已提交
116
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
117
  COPY_CHAR_POINT_FIELD(literal);
X
Xiaoyu Wang 已提交
118 119 120
  if (!pSrc->translate) {
    return (SNode*)pDst;
  }
X
Xiaoyu Wang 已提交
121 122 123 124
  switch (pSrc->node.resType.type) {
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
wafwerar's avatar
wafwerar 已提交
125
      pDst->datum.p = taosMemoryMalloc(pSrc->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
X
Xiaoyu Wang 已提交
126 127 128 129
      if (NULL == pDst->datum.p) {
        nodesDestroyNode(pDst);
        return NULL;
      }
X
bugfix  
Xiaoyu Wang 已提交
130
      memcpy(pDst->datum.p, pSrc->datum.p, pSrc->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
X
Xiaoyu Wang 已提交
131 132 133 134 135 136 137 138 139 140 141 142
      break;
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }
  return (SNode*)pDst;
}

static SNode* operatorNodeCopy(const SOperatorNode* pSrc, SOperatorNode* pDst) {
X
Xiaoyu Wang 已提交
143
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
144
  COPY_SCALAR_FIELD(opType);
145 146
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
X
Xiaoyu Wang 已提交
147 148 149 150
  return (SNode*)pDst;
}

static SNode* logicConditionNodeCopy(const SLogicConditionNode* pSrc, SLogicConditionNode* pDst) {
X
Xiaoyu Wang 已提交
151
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
152
  COPY_SCALAR_FIELD(condType);
153
  CLONE_NODE_LIST_FIELD(pParameterList);
X
Xiaoyu Wang 已提交
154 155 156 157
  return (SNode*)pDst;
}

static SNode* functionNodeCopy(const SFunctionNode* pSrc, SFunctionNode* pDst) {
X
Xiaoyu Wang 已提交
158
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
159 160 161
  COPY_CHAR_ARRAY_FIELD(functionName);
  COPY_SCALAR_FIELD(funcId);
  COPY_SCALAR_FIELD(funcType);
162
  CLONE_NODE_LIST_FIELD(pParameterList);
X
Xiaoyu Wang 已提交
163 164 165
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
166
static SNode* targetNodeCopy(const STargetNode* pSrc, STargetNode* pDst) {
X
Xiaoyu Wang 已提交
167
  COPY_SCALAR_FIELD(dataBlockId);
X
Xiaoyu Wang 已提交
168
  COPY_SCALAR_FIELD(slotId);
169
  CLONE_NODE_FIELD(pExpr);
X
Xiaoyu Wang 已提交
170 171 172
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
173 174
static SNode* groupingSetNodeCopy(const SGroupingSetNode* pSrc, SGroupingSetNode* pDst) {
  COPY_SCALAR_FIELD(groupingSetType);
175 176 177 178
  CLONE_NODE_LIST_FIELD(pParameterList);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
179 180 181 182 183 184
static SNode* orderByExprNodeCopy(const SOrderByExprNode* pSrc, SOrderByExprNode* pDst) {
  COPY_ALL_SCALAR_FIELDS;
  CLONE_NODE_FIELD(pExpr);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
185 186 187 188 189 190
static SNode* nodeListNodeCopy(const SNodeListNode* pSrc, SNodeListNode* pDst) {
  COPY_ALL_SCALAR_FIELDS;
  CLONE_NODE_LIST_FIELD(pNodeList);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
191 192 193 194 195 196
static SNode* fillNodeCopy(const SFillNode* pSrc, SFillNode* pDst) {
  COPY_SCALAR_FIELD(mode);
  CLONE_NODE_FIELD(pValues);
  return (SNode*)pDst;
}

197 198 199 200 201 202 203 204
static SNode* logicNodeCopy(const SLogicNode* pSrc, SLogicNode* pDst) {
  CLONE_NODE_LIST_FIELD(pTargets);
  CLONE_NODE_FIELD(pConditions);
  CLONE_NODE_LIST_FIELD(pChildren);
  return (SNode*)pDst;
}

static STableMeta* tableMetaClone(const STableMeta* pSrc) {
X
Xiaoyu Wang 已提交
205
  int32_t len = TABLE_META_SIZE(pSrc);
wafwerar's avatar
wafwerar 已提交
206
  STableMeta* pDst = taosMemoryMalloc(len);
207 208 209 210 211 212 213 214
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, len);
  return pDst;
}

static SVgroupsInfo* vgroupsInfoClone(const SVgroupsInfo* pSrc) {
X
Xiaoyu Wang 已提交
215
  int32_t len = VGROUPS_INFO_SIZE(pSrc);
wafwerar's avatar
wafwerar 已提交
216
  SVgroupsInfo* pDst = taosMemoryMalloc(len);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, len);
  return pDst;
}

static SNode* logicScanCopy(const SScanLogicNode* pSrc, SScanLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pScanCols);
  CLONE_OBJECT_FIELD(pMeta, tableMetaClone);
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
  COPY_SCALAR_FIELD(scanType);
  COPY_SCALAR_FIELD(scanFlag);
  COPY_SCALAR_FIELD(scanRange);
X
Xiaoyu Wang 已提交
232
  COPY_SCALAR_FIELD(tableName);
D
dapan1121 已提交
233
  COPY_SCALAR_FIELD(showRewrite);
234 235 236 237 238 239 240 241 242 243 244
  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) {
245
  COPY_ALL_SCALAR_FIELDS;
246 247
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pProjections);
X
Xiaoyu Wang 已提交
248 249 250
  return (SNode*)pDst;
}

251
static SNode* logicVnodeModifCopy(const SVnodeModifLogicNode* pSrc, SVnodeModifLogicNode* pDst) {
X
Xiaoyu Wang 已提交
252
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
253
  COPY_SCALAR_FIELD(msgType);
X
Xiaoyu Wang 已提交
254 255 256
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
257 258 259 260 261 262
static SNode* logicExchangeCopy(const SExchangeLogicNode* pSrc, SExchangeLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  COPY_SCALAR_FIELD(srcGroupId);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
263
static SNode* logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pDst) {
X
bugfix  
Xiaoyu Wang 已提交
264
  COPY_ALL_SCALAR_FIELDS;
X
Xiaoyu Wang 已提交
265
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
X
bugfix  
Xiaoyu Wang 已提交
266
  // COPY_SCALAR_FIELD(winType);
X
Xiaoyu Wang 已提交
267
  CLONE_NODE_LIST_FIELD(pFuncs);
X
bugfix  
Xiaoyu Wang 已提交
268 269 270 271 272
  // COPY_SCALAR_FIELD(interval);
  // COPY_SCALAR_FIELD(offset);
  // COPY_SCALAR_FIELD(sliding);
  // COPY_SCALAR_FIELD(intervalUnit);
  // COPY_SCALAR_FIELD(slidingUnit);
X
Xiaoyu Wang 已提交
273
  CLONE_NODE_FIELD(pFill);
X
bugfix  
Xiaoyu Wang 已提交
274 275
  // COPY_SCALAR_FIELD(sessionGap);
  CLONE_NODE_FIELD(pTspk);
X
Xiaoyu Wang 已提交
276 277 278
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
279 280 281 282 283 284
static SNode* logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pSortKeys);
  return (SNode*)pDst;
}

285 286 287 288 289 290
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 已提交
291
static SNode* logicSubplanCopy(const SLogicSubplan* pSrc, SLogicSubplan* pDst) {
292
  CLONE_NODE_FIELD(pNode);
293 294 295 296
  COPY_SCALAR_FIELD(subplanType);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
297
static SNode* dataBlockDescCopy(const SDataBlockDescNode* pSrc, SDataBlockDescNode* pDst) {
298
  COPY_ALL_SCALAR_FIELDS;
299
  CLONE_NODE_LIST_FIELD(pSlots);
X
Xiaoyu Wang 已提交
300 301 302 303 304 305 306 307 308 309 310 311
  return (SNode*)pDst;
}

static SNode* slotDescCopy(const SSlotDescNode* pSrc, SSlotDescNode* pDst) {
  COPY_SCALAR_FIELD(slotId);
  dataTypeCopy(&pSrc->dataType, &pDst->dataType);
  COPY_SCALAR_FIELD(reserve);
  COPY_SCALAR_FIELD(output);
  COPY_SCALAR_FIELD(tag);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
312 313 314 315 316 317 318
static SNode* downstreamSourceCopy(const SDownstreamSourceNode* pSrc, SDownstreamSourceNode* pDst) {
  COPY_SCALAR_FIELD(addr);
  COPY_SCALAR_FIELD(taskId);
  COPY_SCALAR_FIELD(schedId);
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
319
SNodeptr nodesCloneNode(const SNodeptr pNode) {
X
Xiaoyu Wang 已提交
320 321 322 323 324
  if (NULL == pNode) {
    return NULL;
  }
  SNode* pDst = nodesMakeNode(nodeType(pNode));
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
325
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338
    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);
X
Xiaoyu Wang 已提交
339 340
    case QUERY_NODE_TARGET:
      return targetNodeCopy((const STargetNode*)pNode, (STargetNode*)pDst);
X
Xiaoyu Wang 已提交
341 342 343
    case QUERY_NODE_REAL_TABLE:
    case QUERY_NODE_TEMP_TABLE:
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
344
      break;
X
Xiaoyu Wang 已提交
345
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
346
      return groupingSetNodeCopy((const SGroupingSetNode*)pNode, (SGroupingSetNode*)pDst);
X
Xiaoyu Wang 已提交
347
    case QUERY_NODE_ORDER_BY_EXPR:
X
Xiaoyu Wang 已提交
348
      return orderByExprNodeCopy((const SOrderByExprNode*)pNode, (SOrderByExprNode*)pDst);
X
Xiaoyu Wang 已提交
349
    case QUERY_NODE_LIMIT:
350
      break;
X
Xiaoyu Wang 已提交
351 352
    case QUERY_NODE_NODE_LIST:
      return nodeListNodeCopy((const SNodeListNode*)pNode, (SNodeListNode*)pDst);
X
Xiaoyu Wang 已提交
353 354
    case QUERY_NODE_FILL:
      return fillNodeCopy((const SFillNode*)pNode, (SFillNode*)pDst);
X
Xiaoyu Wang 已提交
355 356 357 358
    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 已提交
359 360
    case QUERY_NODE_DOWNSTREAM_SOURCE:
      return downstreamSourceCopy((const SDownstreamSourceNode*)pNode, (SDownstreamSourceNode*)pDst);
361 362 363 364 365 366 367 368
    case QUERY_NODE_LOGIC_PLAN_SCAN:
      return logicScanCopy((const SScanLogicNode*)pNode, (SScanLogicNode*)pDst);
    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 已提交
369 370
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      return logicExchangeCopy((const SExchangeLogicNode*)pNode, (SExchangeLogicNode*)pDst);
X
Xiaoyu Wang 已提交
371 372
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
      return logicWindowCopy((const SWindowLogicNode*)pNode, (SWindowLogicNode*)pDst);
X
Xiaoyu Wang 已提交
373 374
    case QUERY_NODE_LOGIC_PLAN_SORT:
      return logicSortCopy((const SSortLogicNode*)pNode, (SSortLogicNode*)pDst);
375 376
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
      return logicPartitionCopy((const SPartitionLogicNode*)pNode, (SPartitionLogicNode*)pDst);
377
    case QUERY_NODE_LOGIC_SUBPLAN:
X
Xiaoyu Wang 已提交
378
      return logicSubplanCopy((const SLogicSubplan*)pNode, (SLogicSubplan*)pDst);
X
Xiaoyu Wang 已提交
379 380 381
    default:
      break;
  }
382 383 384
  nodesDestroyNode(pDst);
  nodesError("nodesCloneNode unknown node = %s", nodesNodeName(nodeType(pNode)));
  return NULL;
X
Xiaoyu Wang 已提交
385 386 387
}

SNodeList* nodesCloneList(const SNodeList* pList) {
X
Xiaoyu Wang 已提交
388 389 390 391
  if (NULL == pList) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
392 393
  SNodeList* pDst = nodesMakeList();
  if (NULL == pDst) {
X
Xiaoyu Wang 已提交
394
    terrno = TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
395 396 397 398 399 400 401 402 403 404 405 406
    return NULL;
  }
  SNode* pNode;
  FOREACH(pNode, pList) {
    SNode* pNewNode = nodesCloneNode(pNode);
    if (NULL == pNewNode) {
      nodesDestroyList(pDst);
      return NULL;
    }
    nodesListAppend(pDst, pNewNode);
  }
  return pDst;
407
}