nodesCloneFuncs.c 13.9 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
#define COPY_ALL_SCALAR_FIELDS             \
  do {                                     \
X
Xiaoyu Wang 已提交
24
    memcpy((pDst), (pSrc), sizeof(*pSrc)); \
X
Xiaoyu Wang 已提交
25
  } while (0)
X
Xiaoyu Wang 已提交
26

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

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

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

X
Xiaoyu Wang 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
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);
}

static SNode* columnNodeCopy(const SColumnNode* pSrc, SColumnNode* pDst) {
X
Xiaoyu Wang 已提交
101
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
102 103 104 105 106 107
  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 已提交
108 109
  COPY_SCALAR_FIELD(dataBlockId);
  COPY_SCALAR_FIELD(slotId);
X
Xiaoyu Wang 已提交
110 111 112 113
  return (SNode*)pDst;
}

static SNode* valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
X
Xiaoyu Wang 已提交
114
  COPY_ALL_SCALAR_FIELDS;
X
Xiaoyu Wang 已提交
115
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
116
  COPY_CHAR_POINT_FIELD(literal);
X
Xiaoyu Wang 已提交
117 118 119
  if (!pSrc->translate) {
    return (SNode*)pDst;
  }
X
Xiaoyu Wang 已提交
120 121 122 123
  switch (pSrc->node.resType.type) {
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
wafwerar's avatar
wafwerar 已提交
124
      pDst->datum.p = taosMemoryMalloc(pSrc->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
X
Xiaoyu Wang 已提交
125 126 127 128
      if (NULL == pDst->datum.p) {
        nodesDestroyNode(pDst);
        return NULL;
      }
X
bugfix  
Xiaoyu Wang 已提交
129
      memcpy(pDst->datum.p, pSrc->datum.p, pSrc->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
X
Xiaoyu Wang 已提交
130 131 132 133 134 135 136 137 138 139 140 141
      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 已提交
142
  exprNodeCopy((const SExprNode*)pSrc, (SExprNode*)pDst);
X
Xiaoyu Wang 已提交
143
  COPY_SCALAR_FIELD(opType);
144 145
  CLONE_NODE_FIELD(pLeft);
  CLONE_NODE_FIELD(pRight);
X
Xiaoyu Wang 已提交
146 147 148 149
  return (SNode*)pDst;
}

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

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

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

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

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

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

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

196 197 198 199 200 201 202 203
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 已提交
204
  int32_t     len = TABLE_META_SIZE(pSrc);
wafwerar's avatar
wafwerar 已提交
205
  STableMeta* pDst = taosMemoryMalloc(len);
206 207 208 209 210 211 212 213
  if (NULL == pDst) {
    return NULL;
  }
  memcpy(pDst, pSrc, len);
  return pDst;
}

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

static SNode* logicScanCopy(const SScanLogicNode* pSrc, SScanLogicNode* pDst) {
224
  COPY_ALL_SCALAR_FIELDS;
225 226 227 228
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pScanCols);
  CLONE_OBJECT_FIELD(pMeta, tableMetaClone);
  CLONE_OBJECT_FIELD(pVgroupList, vgroupsInfoClone);
229 230 231 232 233 234 235 236
  CLONE_NODE_LIST_FIELD(pDynamicScanFuncs);
  return (SNode*)pDst;
}

static SNode* logicJoinCopy(const SJoinLogicNode* pSrc, SJoinLogicNode* pDst) {
  COPY_ALL_SCALAR_FIELDS;
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_FIELD(pOnConditions);
237 238 239 240 241 242 243 244 245 246 247
  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) {
248
  COPY_ALL_SCALAR_FIELDS;
249 250
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pProjections);
X
Xiaoyu Wang 已提交
251 252 253
  return (SNode*)pDst;
}

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

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

X
Xiaoyu Wang 已提交
266
static SNode* logicWindowCopy(const SWindowLogicNode* pSrc, SWindowLogicNode* pDst) {
X
bugfix  
Xiaoyu Wang 已提交
267
  COPY_ALL_SCALAR_FIELDS;
X
Xiaoyu Wang 已提交
268 269 270
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pFuncs);
  CLONE_NODE_FIELD(pFill);
X
bugfix  
Xiaoyu Wang 已提交
271
  CLONE_NODE_FIELD(pTspk);
X
Xiaoyu Wang 已提交
272 273 274
  return (SNode*)pDst;
}

X
Xiaoyu Wang 已提交
275 276 277 278 279 280
static SNode* logicSortCopy(const SSortLogicNode* pSrc, SSortLogicNode* pDst) {
  COPY_BASE_OBJECT_FIELD(node, logicNodeCopy);
  CLONE_NODE_LIST_FIELD(pSortKeys);
  return (SNode*)pDst;
}

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

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

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

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