nodesUtilFuncs.c 73.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/>.
 */

16
#include "cmdnodes.h"
X
Xiaoyu Wang 已提交
17
#include "nodesUtil.h"
X
Xiaoyu Wang 已提交
18
#include "plannodes.h"
19
#include "querynodes.h"
X
Xiaoyu Wang 已提交
20
#include "taos.h"
21
#include "taoserror.h"
wmmhello's avatar
wmmhello 已提交
22
#include "tdatablock.h"
X
Xiaoyu Wang 已提交
23
#include "thash.h"
24
#include "tref.h"
25

26 27 28 29 30 31 32 33
typedef struct SNodeMemChunk {
  int32_t               availableSize;
  int32_t               usedSize;
  char*                 pBuf;
  struct SNodeMemChunk* pNext;
} SNodeMemChunk;

typedef struct SNodeAllocator {
34
  int64_t        self;
35
  int64_t        queryId;
36 37 38 39
  int32_t        chunkSize;
  int32_t        chunkNum;
  SNodeMemChunk* pCurrChunk;
  SNodeMemChunk* pChunks;
40
  TdThreadMutex  mutex;
41 42
} SNodeAllocator;

43 44
static threadlocal SNodeAllocator* g_pNodeAllocator;
static int32_t                     g_allocatorReqRefPool = -1;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

static SNodeMemChunk* callocNodeChunk(SNodeAllocator* pAllocator) {
  SNodeMemChunk* pNewChunk = taosMemoryCalloc(1, sizeof(SNodeMemChunk) + pAllocator->chunkSize);
  if (NULL == pNewChunk) {
    return NULL;
  }
  pNewChunk->pBuf = (char*)(pNewChunk + 1);
  pNewChunk->availableSize = pAllocator->chunkSize;
  pNewChunk->usedSize = 0;
  pNewChunk->pNext = NULL;
  if (NULL != pAllocator->pCurrChunk) {
    pAllocator->pCurrChunk->pNext = pNewChunk;
  }
  pAllocator->pCurrChunk = pNewChunk;
  if (NULL == pAllocator->pChunks) {
    pAllocator->pChunks = pNewChunk;
  }
  ++(pAllocator->chunkNum);
  return pNewChunk;
}

66
static void* nodesCallocImpl(int32_t size) {
67
  if (NULL == g_pNodeAllocator) {
68
    return taosMemoryCalloc(1, size);
69 70
  }

71 72
  if (g_pNodeAllocator->pCurrChunk->usedSize + size > g_pNodeAllocator->pCurrChunk->availableSize) {
    if (NULL == callocNodeChunk(g_pNodeAllocator)) {
73 74 75
      return NULL;
    }
  }
76 77
  void* p = g_pNodeAllocator->pCurrChunk->pBuf + g_pNodeAllocator->pCurrChunk->usedSize;
  g_pNodeAllocator->pCurrChunk->usedSize += size;
78 79 80
  return p;
}

81 82 83 84 85
static void* nodesCalloc(int32_t num, int32_t size) {
  void* p = nodesCallocImpl(num * size + 1);
  if (NULL == p) {
    return NULL;
  }
86
  *(char*)p = (NULL != g_pNodeAllocator) ? 1 : 0;
87 88 89
  return (char*)p + 1;
}

D
dapan1121 已提交
90
void nodesFree(void* p) {
91 92 93
  char* ptr = (char*)p - 1;
  if (0 == *ptr) {
    taosMemoryFree(ptr);
94 95 96 97
  }
  return;
}

98
static int32_t createNodeAllocator(int32_t chunkSize, SNodeAllocator** pAllocator) {
99 100 101 102 103 104 105 106 107
  *pAllocator = taosMemoryCalloc(1, sizeof(SNodeAllocator));
  if (NULL == *pAllocator) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  (*pAllocator)->chunkSize = chunkSize;
  if (NULL == callocNodeChunk(*pAllocator)) {
    taosMemoryFreeClear(*pAllocator);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
108
  taosThreadMutexInit(&(*pAllocator)->mutex, NULL);
109 110 111
  return TSDB_CODE_SUCCESS;
}

112
static void destroyNodeAllocator(void* p) {
113
  if (NULL == p) {
114 115 116
    return;
  }

117 118
  SNodeAllocator* pAllocator = p;

119 120
  nodesDebug("query id %" PRIx64 " allocator id %" PRIx64 " alloc chunkNum: %d, chunkTotakSize: %d",
             pAllocator->queryId, pAllocator->self, pAllocator->chunkNum, pAllocator->chunkNum * pAllocator->chunkSize);
121 122 123 124 125 126 127

  SNodeMemChunk* pChunk = pAllocator->pChunks;
  while (NULL != pChunk) {
    SNodeMemChunk* pTemp = pChunk->pNext;
    taosMemoryFree(pChunk);
    pChunk = pTemp;
  }
128
  taosThreadMutexDestroy(&pAllocator->mutex);
129 130 131
  taosMemoryFree(pAllocator);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145
int32_t nodesInitAllocatorSet() {
  if (g_allocatorReqRefPool >= 0) {
    nodesWarn("nodes already initialized");
    return TSDB_CODE_SUCCESS;
  }

  g_allocatorReqRefPool = taosOpenRef(1024, destroyNodeAllocator);
  if (g_allocatorReqRefPool < 0) {
    nodesError("init nodes failed");
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return TSDB_CODE_SUCCESS;
}
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160
void nodesDestroyAllocatorSet() {
  if (g_allocatorReqRefPool >= 0) {
    SNodeAllocator* pAllocator = taosIterateRef(g_allocatorReqRefPool, 0);
    int64_t         refId = 0;
    while (NULL != pAllocator) {
      refId = pAllocator->self;
      taosRemoveRef(g_allocatorReqRefPool, refId);
      pAllocator = taosIterateRef(g_allocatorReqRefPool, refId);
    }
    taosCloseRef(g_allocatorReqRefPool);
  }
}

int32_t nodesCreateAllocator(int64_t queryId, int32_t chunkSize, int64_t* pAllocatorId) {
161
  SNodeAllocator* pAllocator = NULL;
162
  int32_t         code = createNodeAllocator(chunkSize, &pAllocator);
163
  if (TSDB_CODE_SUCCESS == code) {
164 165 166 167 168 169
    pAllocator->self = taosAddRef(g_allocatorReqRefPool, pAllocator);
    if (pAllocator->self <= 0) {
      return terrno;
    }
    pAllocator->queryId = queryId;
    *pAllocatorId = pAllocator->self;
170 171 172 173
  }
  return code;
}

174 175 176
int32_t nodesAcquireAllocator(int64_t allocatorId) {
  if (allocatorId <= 0) {
    return TSDB_CODE_SUCCESS;
177
  }
178 179 180 181 182 183 184 185

  SNodeAllocator* pAllocator = taosAcquireRef(g_allocatorReqRefPool, allocatorId);
  if (NULL == pAllocator) {
    return terrno;
  }
  taosThreadMutexLock(&pAllocator->mutex);
  g_pNodeAllocator = pAllocator;
  return TSDB_CODE_SUCCESS;
186 187
}

188 189 190 191 192
int32_t nodesReleaseAllocator(int64_t allocatorId) {
  if (allocatorId <= 0) {
    return TSDB_CODE_SUCCESS;
  }

D
dapan1121 已提交
193
  if (NULL == g_pNodeAllocator) {
194 195 196 197 198
    nodesError("allocator id %" PRIx64
               " release failed: The nodesReleaseAllocator function needs to be called after the nodesAcquireAllocator "
               "function is called!",
               allocatorId);
    return TSDB_CODE_FAILED;
199
  }
D
dapan1121 已提交
200
  SNodeAllocator* pAllocator = g_pNodeAllocator;
201 202 203
  g_pNodeAllocator = NULL;
  taosThreadMutexUnlock(&pAllocator->mutex);
  return taosReleaseRef(g_allocatorReqRefPool, allocatorId);
204 205
}

206 207 208
int64_t nodesMakeAllocatorWeakRef(int64_t allocatorId) {
  if (allocatorId <= 0) {
    return 0;
209
  }
210 211

  SNodeAllocator* pAllocator = taosAcquireRef(g_allocatorReqRefPool, allocatorId);
X
Xiaoyu Wang 已提交
212 213 214 215
  if (NULL == pAllocator) {
    nodesError("allocator id %" PRIx64 " weak reference failed", allocatorId);
    return -1;
  }
216 217 218
  return pAllocator->self;
}

219 220 221 222 223 224 225 226 227 228
int64_t nodesReleaseAllocatorWeakRef(int64_t allocatorId) { return taosReleaseRef(g_allocatorReqRefPool, allocatorId); }

void nodesDestroyAllocator(int64_t allocatorId) {
  if (allocatorId <= 0) {
    return;
  }

  taosRemoveRef(g_allocatorReqRefPool, allocatorId);
}

229 230
static SNode* makeNode(ENodeType type, int32_t size) {
  SNode* p = nodesCalloc(1, size);
X
Xiaoyu Wang 已提交
231 232 233 234 235 236 237
  if (NULL == p) {
    return NULL;
  }
  setNodeType(p, type);
  return p;
}

238
SNode* nodesMakeNode(ENodeType type) {
239 240
  switch (type) {
    case QUERY_NODE_COLUMN:
X
Xiaoyu Wang 已提交
241
      return makeNode(type, sizeof(SColumnNode));
242
    case QUERY_NODE_VALUE:
X
Xiaoyu Wang 已提交
243
      return makeNode(type, sizeof(SValueNode));
244
    case QUERY_NODE_OPERATOR:
X
Xiaoyu Wang 已提交
245
      return makeNode(type, sizeof(SOperatorNode));
246
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
247
      return makeNode(type, sizeof(SLogicConditionNode));
248
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
249
      return makeNode(type, sizeof(SFunctionNode));
250
    case QUERY_NODE_REAL_TABLE:
X
Xiaoyu Wang 已提交
251
      return makeNode(type, sizeof(SRealTableNode));
252
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
253
      return makeNode(type, sizeof(STempTableNode));
254
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
255
      return makeNode(type, sizeof(SJoinTableNode));
256
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
257
      return makeNode(type, sizeof(SGroupingSetNode));
258
    case QUERY_NODE_ORDER_BY_EXPR:
X
Xiaoyu Wang 已提交
259
      return makeNode(type, sizeof(SOrderByExprNode));
260
    case QUERY_NODE_LIMIT:
X
Xiaoyu Wang 已提交
261
      return makeNode(type, sizeof(SLimitNode));
262
    case QUERY_NODE_STATE_WINDOW:
X
Xiaoyu Wang 已提交
263
      return makeNode(type, sizeof(SStateWindowNode));
264
    case QUERY_NODE_SESSION_WINDOW:
X
Xiaoyu Wang 已提交
265
      return makeNode(type, sizeof(SSessionWindowNode));
266
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
267
      return makeNode(type, sizeof(SIntervalWindowNode));
268
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
269
      return makeNode(type, sizeof(SNodeListNode));
270
    case QUERY_NODE_FILL:
X
Xiaoyu Wang 已提交
271
      return makeNode(type, sizeof(SFillNode));
272
    case QUERY_NODE_RAW_EXPR:
X
Xiaoyu Wang 已提交
273
      return makeNode(type, sizeof(SRawExprNode));
274
    case QUERY_NODE_TARGET:
X
Xiaoyu Wang 已提交
275
      return makeNode(type, sizeof(STargetNode));
276
    case QUERY_NODE_DATABLOCK_DESC:
X
Xiaoyu Wang 已提交
277
      return makeNode(type, sizeof(SDataBlockDescNode));
278
    case QUERY_NODE_SLOT_DESC:
X
Xiaoyu Wang 已提交
279
      return makeNode(type, sizeof(SSlotDescNode));
280
    case QUERY_NODE_COLUMN_DEF:
X
Xiaoyu Wang 已提交
281
      return makeNode(type, sizeof(SColumnDefNode));
X
Xiaoyu Wang 已提交
282
    case QUERY_NODE_DOWNSTREAM_SOURCE:
X
Xiaoyu Wang 已提交
283
      return makeNode(type, sizeof(SDownstreamSourceNode));
X
Xiaoyu Wang 已提交
284
    case QUERY_NODE_DATABASE_OPTIONS:
X
Xiaoyu Wang 已提交
285
      return makeNode(type, sizeof(SDatabaseOptions));
X
Xiaoyu Wang 已提交
286
    case QUERY_NODE_TABLE_OPTIONS:
X
Xiaoyu Wang 已提交
287
      return makeNode(type, sizeof(STableOptions));
X
Xiaoyu Wang 已提交
288
    case QUERY_NODE_INDEX_OPTIONS:
X
Xiaoyu Wang 已提交
289
      return makeNode(type, sizeof(SIndexOptions));
290
    case QUERY_NODE_EXPLAIN_OPTIONS:
X
Xiaoyu Wang 已提交
291
      return makeNode(type, sizeof(SExplainOptions));
292
    case QUERY_NODE_STREAM_OPTIONS:
X
Xiaoyu Wang 已提交
293
      return makeNode(type, sizeof(SStreamOptions));
294
    case QUERY_NODE_LEFT_VALUE:
X
Xiaoyu Wang 已提交
295
      return makeNode(type, sizeof(SLeftValueNode));
296 297
    case QUERY_NODE_COLUMN_REF:
      return makeNode(type, sizeof(SColumnDefNode));
X
Xiaoyu Wang 已提交
298 299 300 301
    case QUERY_NODE_WHEN_THEN:
      return makeNode(type, sizeof(SWhenThenNode));
    case QUERY_NODE_CASE_WHEN:
      return makeNode(type, sizeof(SCaseWhenNode));
X
Xiaoyu Wang 已提交
302 303
    case QUERY_NODE_EVENT_WINDOW:
      return makeNode(type, sizeof(SEventWindowNode));
304
    case QUERY_NODE_SET_OPERATOR:
X
Xiaoyu Wang 已提交
305
      return makeNode(type, sizeof(SSetOperator));
306
    case QUERY_NODE_SELECT_STMT:
X
Xiaoyu Wang 已提交
307
      return makeNode(type, sizeof(SSelectStmt));
X
Xiaoyu Wang 已提交
308 309
    case QUERY_NODE_VNODE_MODIFY_STMT:
      return makeNode(type, sizeof(SVnodeModifyOpStmt));
310
    case QUERY_NODE_CREATE_DATABASE_STMT:
X
Xiaoyu Wang 已提交
311
      return makeNode(type, sizeof(SCreateDatabaseStmt));
312
    case QUERY_NODE_DROP_DATABASE_STMT:
X
Xiaoyu Wang 已提交
313
      return makeNode(type, sizeof(SDropDatabaseStmt));
314
    case QUERY_NODE_ALTER_DATABASE_STMT:
X
Xiaoyu Wang 已提交
315
      return makeNode(type, sizeof(SAlterDatabaseStmt));
X
Xiaoyu Wang 已提交
316 317
    case QUERY_NODE_FLUSH_DATABASE_STMT:
      return makeNode(type, sizeof(SFlushDatabaseStmt));
X
Xiaoyu Wang 已提交
318 319
    case QUERY_NODE_TRIM_DATABASE_STMT:
      return makeNode(type, sizeof(STrimDatabaseStmt));
320
    case QUERY_NODE_CREATE_TABLE_STMT:
X
Xiaoyu Wang 已提交
321
      return makeNode(type, sizeof(SCreateTableStmt));
322
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE:
X
Xiaoyu Wang 已提交
323
      return makeNode(type, sizeof(SCreateSubTableClause));
X
Xiaoyu Wang 已提交
324 325
    case QUERY_NODE_CREATE_MULTI_TABLES_STMT:
      return makeNode(type, sizeof(SCreateMultiTablesStmt));
326
    case QUERY_NODE_DROP_TABLE_CLAUSE:
X
Xiaoyu Wang 已提交
327
      return makeNode(type, sizeof(SDropTableClause));
328
    case QUERY_NODE_DROP_TABLE_STMT:
X
Xiaoyu Wang 已提交
329
      return makeNode(type, sizeof(SDropTableStmt));
330
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:
X
Xiaoyu Wang 已提交
331
      return makeNode(type, sizeof(SDropSuperTableStmt));
332
    case QUERY_NODE_ALTER_TABLE_STMT:
333
    case QUERY_NODE_ALTER_SUPER_TABLE_STMT:
X
Xiaoyu Wang 已提交
334
      return makeNode(type, sizeof(SAlterTableStmt));
335
    case QUERY_NODE_CREATE_USER_STMT:
X
Xiaoyu Wang 已提交
336
      return makeNode(type, sizeof(SCreateUserStmt));
337
    case QUERY_NODE_ALTER_USER_STMT:
X
Xiaoyu Wang 已提交
338
      return makeNode(type, sizeof(SAlterUserStmt));
339
    case QUERY_NODE_DROP_USER_STMT:
X
Xiaoyu Wang 已提交
340
      return makeNode(type, sizeof(SDropUserStmt));
341
    case QUERY_NODE_USE_DATABASE_STMT:
X
Xiaoyu Wang 已提交
342
      return makeNode(type, sizeof(SUseDatabaseStmt));
343
    case QUERY_NODE_CREATE_DNODE_STMT:
X
Xiaoyu Wang 已提交
344
      return makeNode(type, sizeof(SCreateDnodeStmt));
345
    case QUERY_NODE_DROP_DNODE_STMT:
X
Xiaoyu Wang 已提交
346
      return makeNode(type, sizeof(SDropDnodeStmt));
347
    case QUERY_NODE_ALTER_DNODE_STMT:
X
Xiaoyu Wang 已提交
348
      return makeNode(type, sizeof(SAlterDnodeStmt));
X
Xiaoyu Wang 已提交
349
    case QUERY_NODE_CREATE_INDEX_STMT:
X
Xiaoyu Wang 已提交
350
      return makeNode(type, sizeof(SCreateIndexStmt));
351
    case QUERY_NODE_DROP_INDEX_STMT:
X
Xiaoyu Wang 已提交
352
      return makeNode(type, sizeof(SDropIndexStmt));
X
Xiaoyu Wang 已提交
353
    case QUERY_NODE_CREATE_QNODE_STMT:
354 355 356
    case QUERY_NODE_CREATE_BNODE_STMT:
    case QUERY_NODE_CREATE_SNODE_STMT:
    case QUERY_NODE_CREATE_MNODE_STMT:
X
Xiaoyu Wang 已提交
357
      return makeNode(type, sizeof(SCreateComponentNodeStmt));
358
    case QUERY_NODE_DROP_QNODE_STMT:
359 360 361
    case QUERY_NODE_DROP_BNODE_STMT:
    case QUERY_NODE_DROP_SNODE_STMT:
    case QUERY_NODE_DROP_MNODE_STMT:
X
Xiaoyu Wang 已提交
362
      return makeNode(type, sizeof(SDropComponentNodeStmt));
363
    case QUERY_NODE_CREATE_TOPIC_STMT:
X
Xiaoyu Wang 已提交
364
      return makeNode(type, sizeof(SCreateTopicStmt));
365
    case QUERY_NODE_DROP_TOPIC_STMT:
X
Xiaoyu Wang 已提交
366 367 368
      return makeNode(type, sizeof(SDropTopicStmt));
    case QUERY_NODE_DROP_CGROUP_STMT:
      return makeNode(type, sizeof(SDropCGroupStmt));
369 370
    case QUERY_NODE_ALTER_LOCAL_STMT:
      return makeNode(type, sizeof(SAlterLocalStmt));
371
    case QUERY_NODE_EXPLAIN_STMT:
X
Xiaoyu Wang 已提交
372
      return makeNode(type, sizeof(SExplainStmt));
373
    case QUERY_NODE_DESCRIBE_STMT:
X
Xiaoyu Wang 已提交
374
      return makeNode(type, sizeof(SDescribeStmt));
375
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
X
Xiaoyu Wang 已提交
376
      return makeNode(type, sizeof(SNode));
X
Xiaoyu Wang 已提交
377 378
    case QUERY_NODE_COMPACT_DATABASE_STMT:
      return makeNode(type, sizeof(SCompactDatabaseStmt));
379
    case QUERY_NODE_CREATE_FUNCTION_STMT:
X
Xiaoyu Wang 已提交
380
      return makeNode(type, sizeof(SCreateFunctionStmt));
381
    case QUERY_NODE_DROP_FUNCTION_STMT:
X
Xiaoyu Wang 已提交
382
      return makeNode(type, sizeof(SDropFunctionStmt));
383
    case QUERY_NODE_CREATE_STREAM_STMT:
X
Xiaoyu Wang 已提交
384
      return makeNode(type, sizeof(SCreateStreamStmt));
385
    case QUERY_NODE_DROP_STREAM_STMT:
X
Xiaoyu Wang 已提交
386
      return makeNode(type, sizeof(SDropStreamStmt));
387 388 389 390
    case QUERY_NODE_PAUSE_STREAM_STMT:
      return makeNode(type, sizeof(SPauseStreamStmt));
    case QUERY_NODE_RESUME_STREAM_STMT:
      return makeNode(type, sizeof(SResumeStreamStmt));
X
Xiaoyu Wang 已提交
391 392
    case QUERY_NODE_BALANCE_VGROUP_STMT:
      return makeNode(type, sizeof(SBalanceVgroupStmt));
C
cadem 已提交
393 394
    case QUERY_NODE_BALANCE_VGROUP_LEADER_STMT:
      return makeNode(type, sizeof(SBalanceVgroupLeaderStmt));
395
    case QUERY_NODE_MERGE_VGROUP_STMT:
X
Xiaoyu Wang 已提交
396
      return makeNode(type, sizeof(SMergeVgroupStmt));
397
    case QUERY_NODE_REDISTRIBUTE_VGROUP_STMT:
X
Xiaoyu Wang 已提交
398
      return makeNode(type, sizeof(SRedistributeVgroupStmt));
399
    case QUERY_NODE_SPLIT_VGROUP_STMT:
X
Xiaoyu Wang 已提交
400
      return makeNode(type, sizeof(SSplitVgroupStmt));
401 402
    case QUERY_NODE_SYNCDB_STMT:
      break;
403
    case QUERY_NODE_GRANT_STMT:
X
Xiaoyu Wang 已提交
404
      return makeNode(type, sizeof(SGrantStmt));
405
    case QUERY_NODE_REVOKE_STMT:
X
Xiaoyu Wang 已提交
406
      return makeNode(type, sizeof(SRevokeStmt));
407
    case QUERY_NODE_SHOW_DNODES_STMT:
X
Xiaoyu Wang 已提交
408
    case QUERY_NODE_SHOW_MNODES_STMT:
X
Xiaoyu Wang 已提交
409 410
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
411 412
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
413
    case QUERY_NODE_SHOW_CLUSTER_STMT:
414
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
415 416
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
417
    case QUERY_NODE_SHOW_STABLES_STMT:
X
Xiaoyu Wang 已提交
418
    case QUERY_NODE_SHOW_STREAMS_STMT:
419 420
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
X
Xiaoyu Wang 已提交
421
    case QUERY_NODE_SHOW_LICENCES_STMT:
422
    case QUERY_NODE_SHOW_VGROUPS_STMT:
S
Shengliang Guan 已提交
423
    case QUERY_NODE_SHOW_TOPICS_STMT:
424
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
425
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
426 427
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_VNODES_STMT:
428 429
    case QUERY_NODE_SHOW_APPS_STMT:
    case QUERY_NODE_SHOW_SCORES_STMT:
430 431
    case QUERY_NODE_SHOW_VARIABLES_STMT:
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
432
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
433
    case QUERY_NODE_SHOW_SUBSCRIPTIONS_STMT:
434
    case QUERY_NODE_SHOW_TAGS_STMT:
435
    case QUERY_NODE_SHOW_USER_PRIVILEGES_STMT:
436
      return makeNode(type, sizeof(SShowStmt));
437 438
    case QUERY_NODE_SHOW_TABLE_TAGS_STMT:
      return makeNode(type, sizeof(SShowTableTagsStmt));
439 440
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:
      return makeNode(type, sizeof(SShowDnodeVariablesStmt));
441
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
442
      return makeNode(type, sizeof(SShowCreateDatabaseStmt));
443 444
    case QUERY_NODE_SHOW_DB_ALIVE_STMT:
    case QUERY_NODE_SHOW_CLUSTER_ALIVE_STMT:
X
Xiaoyu Wang 已提交
445
      return makeNode(type, sizeof(SShowAliveStmt));
446 447
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
448
      return makeNode(type, sizeof(SShowCreateTableStmt));
449 450
    case QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT:
      return makeNode(type, sizeof(SShowTableDistributedStmt));
451
    case QUERY_NODE_KILL_QUERY_STMT:
X
Xiaoyu Wang 已提交
452
      return makeNode(type, sizeof(SKillQueryStmt));
453
    case QUERY_NODE_KILL_TRANSACTION_STMT:
X
Xiaoyu Wang 已提交
454
    case QUERY_NODE_KILL_CONNECTION_STMT:
X
Xiaoyu Wang 已提交
455
      return makeNode(type, sizeof(SKillStmt));
X
Xiaoyu Wang 已提交
456 457
    case QUERY_NODE_DELETE_STMT:
      return makeNode(type, sizeof(SDeleteStmt));
458 459
    case QUERY_NODE_INSERT_STMT:
      return makeNode(type, sizeof(SInsertStmt));
460 461
    case QUERY_NODE_QUERY:
      return makeNode(type, sizeof(SQuery));
D
dapan1121 已提交
462 463 464 465 466
    case QUERY_NODE_RESTORE_DNODE_STMT:
    case QUERY_NODE_RESTORE_QNODE_STMT:
    case QUERY_NODE_RESTORE_MNODE_STMT:
    case QUERY_NODE_RESTORE_VNODE_STMT:
      return makeNode(type, sizeof(SRestoreComponentNodeStmt));
X
Xiaoyu Wang 已提交
467
    case QUERY_NODE_LOGIC_PLAN_SCAN:
X
Xiaoyu Wang 已提交
468
      return makeNode(type, sizeof(SScanLogicNode));
X
Xiaoyu Wang 已提交
469
    case QUERY_NODE_LOGIC_PLAN_JOIN:
X
Xiaoyu Wang 已提交
470
      return makeNode(type, sizeof(SJoinLogicNode));
X
Xiaoyu Wang 已提交
471
    case QUERY_NODE_LOGIC_PLAN_AGG:
X
Xiaoyu Wang 已提交
472
      return makeNode(type, sizeof(SAggLogicNode));
X
Xiaoyu Wang 已提交
473
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
X
Xiaoyu Wang 已提交
474
      return makeNode(type, sizeof(SProjectLogicNode));
X
Xiaoyu Wang 已提交
475 476
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY:
      return makeNode(type, sizeof(SVnodeModifyLogicNode));
X
Xiaoyu Wang 已提交
477
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
X
Xiaoyu Wang 已提交
478
      return makeNode(type, sizeof(SExchangeLogicNode));
X
Xiaoyu Wang 已提交
479 480
    case QUERY_NODE_LOGIC_PLAN_MERGE:
      return makeNode(type, sizeof(SMergeLogicNode));
X
Xiaoyu Wang 已提交
481
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
X
Xiaoyu Wang 已提交
482
      return makeNode(type, sizeof(SWindowLogicNode));
X
Xiaoyu Wang 已提交
483
    case QUERY_NODE_LOGIC_PLAN_FILL:
X
Xiaoyu Wang 已提交
484
      return makeNode(type, sizeof(SFillLogicNode));
X
Xiaoyu Wang 已提交
485
    case QUERY_NODE_LOGIC_PLAN_SORT:
X
Xiaoyu Wang 已提交
486
      return makeNode(type, sizeof(SSortLogicNode));
487
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
X
Xiaoyu Wang 已提交
488
      return makeNode(type, sizeof(SPartitionLogicNode));
489 490
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC:
      return makeNode(type, sizeof(SIndefRowsFuncLogicNode));
X
Xiaoyu Wang 已提交
491 492
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC:
      return makeNode(type, sizeof(SInterpFuncLogicNode));
X
Xiaoyu Wang 已提交
493
    case QUERY_NODE_LOGIC_SUBPLAN:
X
Xiaoyu Wang 已提交
494
      return makeNode(type, sizeof(SLogicSubplan));
X
Xiaoyu Wang 已提交
495
    case QUERY_NODE_LOGIC_PLAN:
X
Xiaoyu Wang 已提交
496
      return makeNode(type, sizeof(SQueryLogicPlan));
X
Xiaoyu Wang 已提交
497
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
X
Xiaoyu Wang 已提交
498
      return makeNode(type, sizeof(STagScanPhysiNode));
X
Xiaoyu Wang 已提交
499
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
X
Xiaoyu Wang 已提交
500
      return makeNode(type, sizeof(STableScanPhysiNode));
501
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
X
Xiaoyu Wang 已提交
502
      return makeNode(type, sizeof(STableSeqScanPhysiNode));
X
Xiaoyu Wang 已提交
503 504
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
      return makeNode(type, sizeof(STableMergeScanPhysiNode));
505
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
X
Xiaoyu Wang 已提交
506
      return makeNode(type, sizeof(SStreamScanPhysiNode));
X
Xiaoyu Wang 已提交
507
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
X
Xiaoyu Wang 已提交
508
      return makeNode(type, sizeof(SSystemTableScanPhysiNode));
509 510
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
      return makeNode(type, sizeof(SBlockDistScanPhysiNode));
X
Xiaoyu Wang 已提交
511 512
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
      return makeNode(type, sizeof(SLastRowScanPhysiNode));
513 514
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_COUNT_SCAN:
      return makeNode(type, sizeof(STableCountScanPhysiNode));
X
Xiaoyu Wang 已提交
515
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
X
Xiaoyu Wang 已提交
516
      return makeNode(type, sizeof(SProjectPhysiNode));
517
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
518
      return makeNode(type, sizeof(SSortMergeJoinPhysiNode));
519
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
X
Xiaoyu Wang 已提交
520
      return makeNode(type, sizeof(SAggPhysiNode));
521
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
X
Xiaoyu Wang 已提交
522
      return makeNode(type, sizeof(SExchangePhysiNode));
X
Xiaoyu Wang 已提交
523 524
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
      return makeNode(type, sizeof(SMergePhysiNode));
525
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
X
Xiaoyu Wang 已提交
526
      return makeNode(type, sizeof(SSortPhysiNode));
527 528
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT:
      return makeNode(type, sizeof(SGroupSortPhysiNode));
X
Xiaoyu Wang 已提交
529
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
X
Xiaoyu Wang 已提交
530
      return makeNode(type, sizeof(SIntervalPhysiNode));
531 532
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
      return makeNode(type, sizeof(SMergeAlignedIntervalPhysiNode));
X
Xiaoyu Wang 已提交
533
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
X
Xiaoyu Wang 已提交
534
      return makeNode(type, sizeof(SStreamIntervalPhysiNode));
535 536 537 538
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
      return makeNode(type, sizeof(SStreamFinalIntervalPhysiNode));
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
      return makeNode(type, sizeof(SStreamSemiIntervalPhysiNode));
X
Xiaoyu Wang 已提交
539
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
5
54liuyao 已提交
540
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL:
X
Xiaoyu Wang 已提交
541
      return makeNode(type, sizeof(SFillPhysiNode));
542
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
X
Xiaoyu Wang 已提交
543
      return makeNode(type, sizeof(SSessionWinodwPhysiNode));
544
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
X
Xiaoyu Wang 已提交
545
      return makeNode(type, sizeof(SStreamSessionWinodwPhysiNode));
546 547 548 549
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
      return makeNode(type, sizeof(SStreamSemiSessionWinodwPhysiNode));
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
      return makeNode(type, sizeof(SStreamFinalSessionWinodwPhysiNode));
550
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
X
Xiaoyu Wang 已提交
551
      return makeNode(type, sizeof(SStateWinodwPhysiNode));
552
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
5
54liuyao 已提交
553
      return makeNode(type, sizeof(SStreamStateWinodwPhysiNode));
X
Xiaoyu Wang 已提交
554 555 556 557
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_EVENT:
      return makeNode(type, sizeof(SEventWinodwPhysiNode));
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_EVENT:
      return makeNode(type, sizeof(SStreamEventWinodwPhysiNode));
558
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
X
Xiaoyu Wang 已提交
559
      return makeNode(type, sizeof(SPartitionPhysiNode));
560 561
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION:
      return makeNode(type, sizeof(SStreamPartitionPhysiNode));
562 563
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC:
      return makeNode(type, sizeof(SIndefRowsFuncPhysiNode));
X
Xiaoyu Wang 已提交
564 565
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
      return makeNode(type, sizeof(SInterpFuncLogicNode));
566
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
X
Xiaoyu Wang 已提交
567
      return makeNode(type, sizeof(SDataDispatcherNode));
568
    case QUERY_NODE_PHYSICAL_PLAN_INSERT:
X
Xiaoyu Wang 已提交
569
      return makeNode(type, sizeof(SDataInserterNode));
570 571
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT:
      return makeNode(type, sizeof(SQueryInserterNode));
X
Xiaoyu Wang 已提交
572 573
    case QUERY_NODE_PHYSICAL_PLAN_DELETE:
      return makeNode(type, sizeof(SDataDeleterNode));
X
Xiaoyu Wang 已提交
574
    case QUERY_NODE_PHYSICAL_SUBPLAN:
X
Xiaoyu Wang 已提交
575
      return makeNode(type, sizeof(SSubplan));
X
Xiaoyu Wang 已提交
576
    case QUERY_NODE_PHYSICAL_PLAN:
X
Xiaoyu Wang 已提交
577
      return makeNode(type, sizeof(SQueryPlan));
578 579 580
    default:
      break;
  }
581
  nodesError("nodesMakeNode unknown node = %s", nodesNodeName(type));
X
Xiaoyu Wang 已提交
582
  return NULL;
583 584
}

X
Xiaoyu Wang 已提交
585 586 587 588 589 590 591 592 593 594 595 596
static void destroyVgDataBlockArray(SArray* pArray) {
  size_t size = taosArrayGetSize(pArray);
  for (size_t i = 0; i < size; ++i) {
    SVgDataBlocks* pVg = taosArrayGetP(pArray, i);
    taosMemoryFreeClear(pVg->pData);
    taosMemoryFreeClear(pVg);
  }
  taosArrayDestroy(pArray);
}

static void destroyLogicNode(SLogicNode* pNode) {
  nodesDestroyList(pNode->pTargets);
X
Xiaoyu Wang 已提交
597 598 599 600
  nodesDestroyNode(pNode->pConditions);
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pLimit);
  nodesDestroyNode(pNode->pSlimit);
X
Xiaoyu Wang 已提交
601 602 603 604 605
}

static void destroyPhysiNode(SPhysiNode* pNode) {
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pConditions);
606
  nodesDestroyNode((SNode*)pNode->pOutputDataBlockDesc);
607 608
  nodesDestroyNode(pNode->pLimit);
  nodesDestroyNode(pNode->pSlimit);
X
Xiaoyu Wang 已提交
609 610
}

611
static void destroyWinodwPhysiNode(SWindowPhysiNode* pNode) {
X
Xiaoyu Wang 已提交
612 613 614
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pExprs);
  nodesDestroyList(pNode->pFuncs);
615
  nodesDestroyNode(pNode->pTspk);
5
54liuyao 已提交
616
  nodesDestroyNode(pNode->pTsEnd);
X
Xiaoyu Wang 已提交
617 618
}

619 620 621 622 623 624 625
static void destroyPartitionPhysiNode(SPartitionPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pExprs);
  nodesDestroyList(pNode->pPartitionKeys);
  nodesDestroyList(pNode->pTargets);
}

X
Xiaoyu Wang 已提交
626 627 628
static void destroyScanPhysiNode(SScanPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pScanCols);
X
Xiaoyu Wang 已提交
629
  nodesDestroyList(pNode->pScanPseudoCols);
X
Xiaoyu Wang 已提交
630 631
}

632
static void destroyDataSinkNode(SDataSinkNode* pNode) { nodesDestroyNode((SNode*)pNode->pInputDataBlockDesc); }
X
Xiaoyu Wang 已提交
633

X
Xiaoyu Wang 已提交
634 635
static void destroyExprNode(SExprNode* pExpr) { taosArrayDestroy(pExpr->pAssociation); }

636
static void destroyTableCfg(STableCfg* pCfg) {
637 638 639
  if (NULL == pCfg) {
    return;
  }
640 641 642 643 644
  taosArrayDestroy(pCfg->pFuncs);
  taosMemoryFree(pCfg->pComment);
  taosMemoryFree(pCfg->pSchemas);
  taosMemoryFree(pCfg->pTags);
  taosMemoryFree(pCfg);
D
dapan1121 已提交
645 646
}

647 648
static void destroySmaIndex(void* pIndex) { taosMemoryFree(((STableIndexInfo*)pIndex)->expr); }

649
void nodesDestroyNode(SNode* pNode) {
X
Xiaoyu Wang 已提交
650 651 652 653 654
  if (NULL == pNode) {
    return;
  }

  switch (nodeType(pNode)) {
X
Xiaoyu Wang 已提交
655
    case QUERY_NODE_COLUMN:
X
Xiaoyu Wang 已提交
656
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
657
      break;
D
dapan1121 已提交
658
    case QUERY_NODE_VALUE: {
X
Xiaoyu Wang 已提交
659
      SValueNode* pValue = (SValueNode*)pNode;
X
Xiaoyu Wang 已提交
660
      destroyExprNode((SExprNode*)pNode);
wafwerar's avatar
wafwerar 已提交
661
      taosMemoryFreeClear(pValue->literal);
D
dapan1121 已提交
662
      if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
wafwerar's avatar
wafwerar 已提交
663
        taosMemoryFreeClear(pValue->datum.p);
D
dapan1121 已提交
664
      }
X
Xiaoyu Wang 已提交
665 666 667 668
      break;
    }
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOp = (SOperatorNode*)pNode;
X
Xiaoyu Wang 已提交
669
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
670 671
      nodesDestroyNode(pOp->pLeft);
      nodesDestroyNode(pOp->pRight);
D
dapan1121 已提交
672 673 674
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
675
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
676
      nodesDestroyList(((SLogicConditionNode*)pNode)->pParameterList);
D
dapan1121 已提交
677 678
      break;
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
679
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
680
      nodesDestroyList(((SFunctionNode*)pNode)->pParameterList);
X
Xiaoyu Wang 已提交
681 682
      break;
    case QUERY_NODE_REAL_TABLE: {
X
Xiaoyu Wang 已提交
683
      SRealTableNode* pReal = (SRealTableNode*)pNode;
wafwerar's avatar
wafwerar 已提交
684 685
      taosMemoryFreeClear(pReal->pMeta);
      taosMemoryFreeClear(pReal->pVgroupList);
686
      taosArrayDestroyEx(pReal->pSmaIndexes, destroySmaIndex);
X
Xiaoyu Wang 已提交
687 688 689
      break;
    }
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
690
      nodesDestroyNode(((STempTableNode*)pNode)->pSubquery);
D
dapan1121 已提交
691
      break;
X
Xiaoyu Wang 已提交
692 693 694 695 696
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoin = (SJoinTableNode*)pNode;
      nodesDestroyNode(pJoin->pLeft);
      nodesDestroyNode(pJoin->pRight);
      nodesDestroyNode(pJoin->pOnCond);
D
dapan1121 已提交
697
      break;
X
Xiaoyu Wang 已提交
698
    }
D
dapan1121 已提交
699
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
700 701 702 703 704
      nodesDestroyList(((SGroupingSetNode*)pNode)->pParameterList);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      nodesDestroyNode(((SOrderByExprNode*)pNode)->pExpr);
      break;
X
Xiaoyu Wang 已提交
705
    case QUERY_NODE_LIMIT:  // no pointer field
D
dapan1121 已提交
706
      break;
707 708 709 710
    case QUERY_NODE_STATE_WINDOW: {
      SStateWindowNode* pState = (SStateWindowNode*)pNode;
      nodesDestroyNode(pState->pCol);
      nodesDestroyNode(pState->pExpr);
X
Xiaoyu Wang 已提交
711
      break;
712
    }
X
Xiaoyu Wang 已提交
713 714
    case QUERY_NODE_SESSION_WINDOW: {
      SSessionWindowNode* pSession = (SSessionWindowNode*)pNode;
715 716
      nodesDestroyNode((SNode*)pSession->pCol);
      nodesDestroyNode((SNode*)pSession->pGap);
D
dapan1121 已提交
717
      break;
X
Xiaoyu Wang 已提交
718 719 720 721 722 723 724 725 726 727
    }
    case QUERY_NODE_INTERVAL_WINDOW: {
      SIntervalWindowNode* pJoin = (SIntervalWindowNode*)pNode;
      nodesDestroyNode(pJoin->pCol);
      nodesDestroyNode(pJoin->pInterval);
      nodesDestroyNode(pJoin->pOffset);
      nodesDestroyNode(pJoin->pSliding);
      nodesDestroyNode(pJoin->pFill);
      break;
    }
D
dapan1121 已提交
728
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
729 730
      nodesDestroyList(((SNodeListNode*)pNode)->pNodeList);
      break;
X
Xiaoyu Wang 已提交
731 732 733 734
    case QUERY_NODE_FILL: {
      SFillNode* pFill = (SFillNode*)pNode;
      nodesDestroyNode(pFill->pValues);
      nodesDestroyNode(pFill->pWStartTs);
X
Xiaoyu Wang 已提交
735
      break;
X
Xiaoyu Wang 已提交
736
    }
X
Xiaoyu Wang 已提交
737 738
    case QUERY_NODE_RAW_EXPR:
      nodesDestroyNode(((SRawExprNode*)pNode)->pNode);
X
Xiaoyu Wang 已提交
739
      break;
X
Xiaoyu Wang 已提交
740 741 742 743 744 745
    case QUERY_NODE_TARGET:
      nodesDestroyNode(((STargetNode*)pNode)->pExpr);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      nodesDestroyList(((SDataBlockDescNode*)pNode)->pSlots);
      break;
X
Xiaoyu Wang 已提交
746 747 748
    case QUERY_NODE_SLOT_DESC:          // no pointer field
    case QUERY_NODE_COLUMN_DEF:         // no pointer field
    case QUERY_NODE_DOWNSTREAM_SOURCE:  // no pointer field
X
Xiaoyu Wang 已提交
749
      break;
X
Xiaoyu Wang 已提交
750 751
    case QUERY_NODE_DATABASE_OPTIONS: {
      SDatabaseOptions* pOptions = (SDatabaseOptions*)pNode;
752
      nodesDestroyNode((SNode*)pOptions->pDaysPerFile);
X
Xiaoyu Wang 已提交
753 754
      nodesDestroyList(pOptions->pKeep);
      nodesDestroyList(pOptions->pRetentions);
X
Xiaoyu Wang 已提交
755
      break;
X
Xiaoyu Wang 已提交
756
    }
X
Xiaoyu Wang 已提交
757
    case QUERY_NODE_TABLE_OPTIONS: {
X
Xiaoyu Wang 已提交
758
      STableOptions* pOptions = (STableOptions*)pNode;
759 760
      nodesDestroyList(pOptions->pMaxDelay);
      nodesDestroyList(pOptions->pWatermark);
X
Xiaoyu Wang 已提交
761
      nodesDestroyList(pOptions->pRollupFuncs);
762
      nodesDestroyList(pOptions->pSma);
763
      nodesDestroyList(pOptions->pDeleteMark);
X
Xiaoyu Wang 已提交
764
      break;
X
Xiaoyu Wang 已提交
765
    }
766
    case QUERY_NODE_INDEX_OPTIONS: {
X
Xiaoyu Wang 已提交
767 768 769 770 771
      SIndexOptions* pOptions = (SIndexOptions*)pNode;
      nodesDestroyList(pOptions->pFuncs);
      nodesDestroyNode(pOptions->pInterval);
      nodesDestroyNode(pOptions->pOffset);
      nodesDestroyNode(pOptions->pSliding);
772
      nodesDestroyNode(pOptions->pStreamOptions);
773 774
      break;
    }
775 776
    case QUERY_NODE_EXPLAIN_OPTIONS:  // no pointer field
      break;
777 778 779 780
    case QUERY_NODE_STREAM_OPTIONS: {
      SStreamOptions* pOptions = (SStreamOptions*)pNode;
      nodesDestroyNode(pOptions->pDelay);
      nodesDestroyNode(pOptions->pWatermark);
781
      nodesDestroyNode(pOptions->pDeleteMark);
782
      break;
783
    }
784
    case QUERY_NODE_LEFT_VALUE:  // no pointer field
X
Xiaoyu Wang 已提交
785
    case QUERY_NODE_COLUMN_REF:  // no pointer field
786
      break;
X
Xiaoyu Wang 已提交
787
    case QUERY_NODE_WHEN_THEN: {
X
Xiaoyu Wang 已提交
788 789 790
      SWhenThenNode* pWhenThen = (SWhenThenNode*)pNode;
      nodesDestroyNode(pWhenThen->pWhen);
      nodesDestroyNode(pWhenThen->pThen);
X
Xiaoyu Wang 已提交
791 792 793
      break;
    }
    case QUERY_NODE_CASE_WHEN: {
X
Xiaoyu Wang 已提交
794 795 796 797 798 799 800 801 802 803 804
      SCaseWhenNode* pCaseWhen = (SCaseWhenNode*)pNode;
      nodesDestroyNode(pCaseWhen->pCase);
      nodesDestroyNode(pCaseWhen->pElse);
      nodesDestroyList(pCaseWhen->pWhenThenList);
      break;
    }
    case QUERY_NODE_EVENT_WINDOW: {
      SEventWindowNode* pEvent = (SEventWindowNode*)pNode;
      nodesDestroyNode(pEvent->pCol);
      nodesDestroyNode(pEvent->pStartCond);
      nodesDestroyNode(pEvent->pEndCond);
X
Xiaoyu Wang 已提交
805 806
      break;
    }
X
Xiaoyu Wang 已提交
807 808
    case QUERY_NODE_SET_OPERATOR: {
      SSetOperator* pStmt = (SSetOperator*)pNode;
809
      nodesDestroyList(pStmt->pProjectionList);
X
Xiaoyu Wang 已提交
810 811 812 813 814 815
      nodesDestroyNode(pStmt->pLeft);
      nodesDestroyNode(pStmt->pRight);
      nodesDestroyList(pStmt->pOrderByList);
      nodesDestroyNode(pStmt->pLimit);
      break;
    }
X
Xiaoyu Wang 已提交
816
    case QUERY_NODE_SELECT_STMT: {
X
Xiaoyu Wang 已提交
817
      SSelectStmt* pStmt = (SSelectStmt*)pNode;
X
Xiaoyu Wang 已提交
818 819 820 821
      nodesDestroyList(pStmt->pProjectionList);
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyList(pStmt->pPartitionByList);
822 823
      nodesDestroyList(pStmt->pTags);
      nodesDestroyNode(pStmt->pSubtable);
X
Xiaoyu Wang 已提交
824 825 826
      nodesDestroyNode(pStmt->pWindow);
      nodesDestroyList(pStmt->pGroupByList);
      nodesDestroyNode(pStmt->pHaving);
X
Xiaoyu Wang 已提交
827 828 829
      nodesDestroyNode(pStmt->pRange);
      nodesDestroyNode(pStmt->pEvery);
      nodesDestroyNode(pStmt->pFill);
X
Xiaoyu Wang 已提交
830
      nodesDestroyList(pStmt->pOrderByList);
831 832
      nodesDestroyNode((SNode*)pStmt->pLimit);
      nodesDestroyNode((SNode*)pStmt->pSlimit);
X
Xiaoyu Wang 已提交
833 834
      break;
    }
X
Xiaoyu Wang 已提交
835 836
    case QUERY_NODE_VNODE_MODIFY_STMT: {
      SVnodeModifyOpStmt* pStmt = (SVnodeModifyOpStmt*)pNode;
X
Xiaoyu Wang 已提交
837 838
      destroyVgDataBlockArray(pStmt->pDataBlocks);
      taosMemoryFreeClear(pStmt->pTableMeta);
X
Xiaoyu Wang 已提交
839 840
      nodesDestroyNode(pStmt->pTagCond);
      taosArrayDestroy(pStmt->pTableTag);
X
Xiaoyu Wang 已提交
841 842 843 844 845 846 847 848 849 850
      taosHashCleanup(pStmt->pVgroupsHashObj);
      taosHashCleanup(pStmt->pSubTableHashObj);
      taosHashCleanup(pStmt->pTableNameHashObj);
      taosHashCleanup(pStmt->pDbFNameHashObj);
      if (pStmt->freeHashFunc) {
        pStmt->freeHashFunc(pStmt->pTableBlockHashObj);
      }
      if (pStmt->freeArrayFunc) {
        pStmt->freeArrayFunc(pStmt->pVgDataBlocks);
      }
X
Xiaoyu Wang 已提交
851 852
      tdDestroySVCreateTbReq(pStmt->pCreateTblReq);
      taosMemoryFreeClear(pStmt->pCreateTblReq);
X
Xiaoyu Wang 已提交
853
      taosCloseFile(&pStmt->fp);
X
Xiaoyu Wang 已提交
854
      break;
X
Xiaoyu Wang 已提交
855
    }
X
Xiaoyu Wang 已提交
856
    case QUERY_NODE_CREATE_DATABASE_STMT:
857
      nodesDestroyNode((SNode*)((SCreateDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
858
      break;
X
Xiaoyu Wang 已提交
859
    case QUERY_NODE_DROP_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
860 861
      break;
    case QUERY_NODE_ALTER_DATABASE_STMT:
862
      nodesDestroyNode((SNode*)((SAlterDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
863
      break;
X
Xiaoyu Wang 已提交
864
    case QUERY_NODE_FLUSH_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
865
    case QUERY_NODE_TRIM_DATABASE_STMT:   // no pointer field
X
Xiaoyu Wang 已提交
866
      break;
X
Xiaoyu Wang 已提交
867
    case QUERY_NODE_CREATE_TABLE_STMT: {
X
Xiaoyu Wang 已提交
868
      SCreateTableStmt* pStmt = (SCreateTableStmt*)pNode;
X
Xiaoyu Wang 已提交
869 870
      nodesDestroyList(pStmt->pCols);
      nodesDestroyList(pStmt->pTags);
871
      nodesDestroyNode((SNode*)pStmt->pOptions);
X
Xiaoyu Wang 已提交
872 873 874
      break;
    }
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE: {
X
Xiaoyu Wang 已提交
875
      SCreateSubTableClause* pStmt = (SCreateSubTableClause*)pNode;
X
Xiaoyu Wang 已提交
876 877
      nodesDestroyList(pStmt->pSpecificTags);
      nodesDestroyList(pStmt->pValsOfTags);
wmmhello's avatar
wmmhello 已提交
878
      nodesDestroyNode((SNode*)pStmt->pOptions);
X
Xiaoyu Wang 已提交
879 880
      break;
    }
X
Xiaoyu Wang 已提交
881 882
    case QUERY_NODE_CREATE_MULTI_TABLES_STMT:
      nodesDestroyList(((SCreateMultiTablesStmt*)pNode)->pSubTables);
X
Xiaoyu Wang 已提交
883
      break;
X
Xiaoyu Wang 已提交
884
    case QUERY_NODE_DROP_TABLE_CLAUSE:  // no pointer field
X
Xiaoyu Wang 已提交
885 886
      break;
    case QUERY_NODE_DROP_TABLE_STMT:
887
      nodesDestroyList(((SDropTableStmt*)pNode)->pTables);
X
Xiaoyu Wang 已提交
888
      break;
X
Xiaoyu Wang 已提交
889
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
890
      break;
891 892
    case QUERY_NODE_ALTER_TABLE_STMT:
    case QUERY_NODE_ALTER_SUPER_TABLE_STMT: {
X
Xiaoyu Wang 已提交
893
      SAlterTableStmt* pStmt = (SAlterTableStmt*)pNode;
894 895
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode((SNode*)pStmt->pVal);
X
Xiaoyu Wang 已提交
896 897
      break;
    }
X
Xiaoyu Wang 已提交
898 899 900 901 902 903 904
    case QUERY_NODE_CREATE_USER_STMT:   // no pointer field
    case QUERY_NODE_ALTER_USER_STMT:    // no pointer field
    case QUERY_NODE_DROP_USER_STMT:     // no pointer field
    case QUERY_NODE_USE_DATABASE_STMT:  // no pointer field
    case QUERY_NODE_CREATE_DNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_DNODE_STMT:    // no pointer field
    case QUERY_NODE_ALTER_DNODE_STMT:   // no pointer field
905
      break;
906
    case QUERY_NODE_CREATE_INDEX_STMT: {
X
Xiaoyu Wang 已提交
907
      SCreateIndexStmt* pStmt = (SCreateIndexStmt*)pNode;
908
      nodesDestroyNode((SNode*)pStmt->pOptions);
909 910 911
      nodesDestroyList(pStmt->pCols);
      break;
    }
X
Xiaoyu Wang 已提交
912 913 914
    case QUERY_NODE_DROP_INDEX_STMT:    // no pointer field
    case QUERY_NODE_CREATE_QNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_QNODE_STMT:    // no pointer field
915 916 917 918 919 920
    case QUERY_NODE_CREATE_BNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_BNODE_STMT:    // no pointer field
    case QUERY_NODE_CREATE_SNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_SNODE_STMT:    // no pointer field
    case QUERY_NODE_CREATE_MNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_MNODE_STMT:    // no pointer field
X
Xiaoyu Wang 已提交
921 922 923
      break;
    case QUERY_NODE_CREATE_TOPIC_STMT:
      nodesDestroyNode(((SCreateTopicStmt*)pNode)->pQuery);
wmmhello's avatar
wmmhello 已提交
924
      nodesDestroyNode(((SCreateTopicStmt*)pNode)->pWhere);
X
Xiaoyu Wang 已提交
925
      break;
X
Xiaoyu Wang 已提交
926
    case QUERY_NODE_DROP_TOPIC_STMT:   // no pointer field
927
    case QUERY_NODE_DROP_CGROUP_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
928
    case QUERY_NODE_ALTER_LOCAL_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
929
      break;
930 931 932 933 934 935 936 937 938 939
    case QUERY_NODE_EXPLAIN_STMT: {
      SExplainStmt* pStmt = (SExplainStmt*)pNode;
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode(pStmt->pQuery);
      break;
    }
    case QUERY_NODE_DESCRIBE_STMT:
      taosMemoryFree(((SDescribeStmt*)pNode)->pMeta);
      break;
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
940
      break;
941 942 943 944 945 946 947 948
    case QUERY_NODE_COMPACT_DATABASE_STMT: {
      SCompactDatabaseStmt* pStmt = (SCompactDatabaseStmt*)pNode;
      nodesDestroyNode(pStmt->pStart);
      nodesDestroyNode(pStmt->pEnd);
      break;
    }
    case QUERY_NODE_CREATE_FUNCTION_STMT:  // no pointer field
    case QUERY_NODE_DROP_FUNCTION_STMT:    // no pointer field
949 950 951 952 953
      break;
    case QUERY_NODE_CREATE_STREAM_STMT: {
      SCreateStreamStmt* pStmt = (SCreateStreamStmt*)pNode;
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode(pStmt->pQuery);
954 955
      nodesDestroyList(pStmt->pTags);
      nodesDestroyNode(pStmt->pSubtable);
956 957
      break;
    }
X
Xiaoyu Wang 已提交
958
    case QUERY_NODE_DROP_STREAM_STMT:            // no pointer field
959 960
    case QUERY_NODE_PAUSE_STREAM_STMT:           // no pointer field
    case QUERY_NODE_RESUME_STREAM_STMT:          // no pointer field
X
Xiaoyu Wang 已提交
961
    case QUERY_NODE_BALANCE_VGROUP_STMT:         // no pointer field
C
cadem 已提交
962
    case QUERY_NODE_BALANCE_VGROUP_LEADER_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
963
    case QUERY_NODE_MERGE_VGROUP_STMT:           // no pointer field
964 965 966 967 968 969
      break;
    case QUERY_NODE_REDISTRIBUTE_VGROUP_STMT:
      nodesDestroyList(((SRedistributeVgroupStmt*)pNode)->pDnodes);
      break;
    case QUERY_NODE_SPLIT_VGROUP_STMT:  // no pointer field
    case QUERY_NODE_SYNCDB_STMT:        // no pointer field
X
Xiaoyu Wang 已提交
970 971 972 973 974 975
      break;
    case QUERY_NODE_GRANT_STMT:
      nodesDestroyNode(((SGrantStmt*)pNode)->pTagCond);
      break;
    case QUERY_NODE_REVOKE_STMT:
      nodesDestroyNode(((SRevokeStmt*)pNode)->pTagCond);
976
      break;
X
Xiaoyu Wang 已提交
977 978 979 980
    case QUERY_NODE_SHOW_DNODES_STMT:
    case QUERY_NODE_SHOW_MNODES_STMT:
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
981 982 983 984
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
    case QUERY_NODE_SHOW_CLUSTER_STMT:
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
985 986
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
987 988 989 990
    case QUERY_NODE_SHOW_STABLES_STMT:
    case QUERY_NODE_SHOW_STREAMS_STMT:
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
X
Xiaoyu Wang 已提交
991
    case QUERY_NODE_SHOW_LICENCES_STMT:
992 993 994 995 996 997 998 999
    case QUERY_NODE_SHOW_VGROUPS_STMT:
    case QUERY_NODE_SHOW_TOPICS_STMT:
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_VNODES_STMT:
    case QUERY_NODE_SHOW_APPS_STMT:
    case QUERY_NODE_SHOW_SCORES_STMT:
1000 1001
    case QUERY_NODE_SHOW_VARIABLES_STMT:
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
1002
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
1003
    case QUERY_NODE_SHOW_SUBSCRIPTIONS_STMT:
1004 1005
    case QUERY_NODE_SHOW_TAGS_STMT:
    case QUERY_NODE_SHOW_USER_PRIVILEGES_STMT: {
X
Xiaoyu Wang 已提交
1006 1007
      SShowStmt* pStmt = (SShowStmt*)pNode;
      nodesDestroyNode(pStmt->pDbName);
1008
      nodesDestroyNode(pStmt->pTbName);
X
Xiaoyu Wang 已提交
1009 1010
      break;
    }
1011 1012 1013 1014 1015 1016 1017
    case QUERY_NODE_SHOW_TABLE_TAGS_STMT: {
      SShowTableTagsStmt* pStmt = (SShowTableTagsStmt*)pNode;
      nodesDestroyNode(pStmt->pDbName);
      nodesDestroyNode(pStmt->pTbName);
      nodesDestroyList(pStmt->pTags);
      break;
    }
1018 1019
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:
      nodesDestroyNode(((SShowDnodeVariablesStmt*)pNode)->pDnodeId);
1020
      nodesDestroyNode(((SShowDnodeVariablesStmt*)pNode)->pLikePattern);
1021
      break;
1022 1023 1024 1025 1026
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
      taosMemoryFreeClear(((SShowCreateDatabaseStmt*)pNode)->pCfg);
      break;
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
X
Xiaoyu Wang 已提交
1027 1028
      taosMemoryFreeClear(((SShowCreateTableStmt*)pNode)->pDbCfg);
      destroyTableCfg((STableCfg*)(((SShowCreateTableStmt*)pNode)->pTableCfg));
1029 1030 1031 1032 1033
      break;
    case QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT:  // no pointer field
    case QUERY_NODE_KILL_CONNECTION_STMT:         // no pointer field
    case QUERY_NODE_KILL_QUERY_STMT:              // no pointer field
    case QUERY_NODE_KILL_TRANSACTION_STMT:        // no pointer field
1034 1035 1036 1037 1038 1039
      break;
    case QUERY_NODE_DELETE_STMT: {
      SDeleteStmt* pStmt = (SDeleteStmt*)pNode;
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyNode(pStmt->pCountFunc);
1040 1041
      nodesDestroyNode(pStmt->pFirstFunc);
      nodesDestroyNode(pStmt->pLastFunc);
X
Xiaoyu Wang 已提交
1042
      nodesDestroyNode(pStmt->pTagCond);
1043 1044
      break;
    }
1045 1046 1047 1048 1049 1050 1051
    case QUERY_NODE_INSERT_STMT: {
      SInsertStmt* pStmt = (SInsertStmt*)pNode;
      nodesDestroyNode(pStmt->pTable);
      nodesDestroyList(pStmt->pCols);
      nodesDestroyNode(pStmt->pQuery);
      break;
    }
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
    case QUERY_NODE_QUERY: {
      SQuery* pQuery = (SQuery*)pNode;
      nodesDestroyNode(pQuery->pRoot);
      taosMemoryFreeClear(pQuery->pResSchema);
      if (NULL != pQuery->pCmdMsg) {
        taosMemoryFreeClear(pQuery->pCmdMsg->pMsg);
        taosMemoryFreeClear(pQuery->pCmdMsg);
      }
      taosArrayDestroy(pQuery->pDbList);
      taosArrayDestroy(pQuery->pTableList);
D
dapan1121 已提交
1062
      taosArrayDestroy(pQuery->pTargetTableList);
1063 1064
      taosArrayDestroy(pQuery->pPlaceholderValues);
      nodesDestroyNode(pQuery->pPrepareRoot);
1065 1066
      break;
    }
D
dapan1121 已提交
1067 1068 1069 1070 1071
    case QUERY_NODE_RESTORE_DNODE_STMT:   // no pointer field
    case QUERY_NODE_RESTORE_QNODE_STMT:   // no pointer field
    case QUERY_NODE_RESTORE_MNODE_STMT:   // no pointer field
    case QUERY_NODE_RESTORE_VNODE_STMT:   // no pointer field
      break;
X
Xiaoyu Wang 已提交
1072 1073 1074 1075
    case QUERY_NODE_LOGIC_PLAN_SCAN: {
      SScanLogicNode* pLogicNode = (SScanLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pScanCols);
X
Xiaoyu Wang 已提交
1076
      nodesDestroyList(pLogicNode->pScanPseudoCols);
X
Xiaoyu Wang 已提交
1077
      taosMemoryFreeClear(pLogicNode->pVgroupList);
X
Xiaoyu Wang 已提交
1078 1079 1080
      nodesDestroyList(pLogicNode->pDynamicScanFuncs);
      nodesDestroyNode(pLogicNode->pTagCond);
      nodesDestroyNode(pLogicNode->pTagIndexCond);
1081
      taosArrayDestroyEx(pLogicNode->pSmaIndexes, destroySmaIndex);
1082
      nodesDestroyList(pLogicNode->pGroupTags);
1083 1084
      nodesDestroyList(pLogicNode->pTags);
      nodesDestroyNode(pLogicNode->pSubtable);
X
Xiaoyu Wang 已提交
1085 1086 1087 1088 1089
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_JOIN: {
      SJoinLogicNode* pLogicNode = (SJoinLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
1090
      nodesDestroyNode(pLogicNode->pMergeCondition);
X
Xiaoyu Wang 已提交
1091
      nodesDestroyNode(pLogicNode->pOnConditions);
S
slzhou 已提交
1092
      nodesDestroyNode(pLogicNode->pColEqualOnConditions);
X
Xiaoyu Wang 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_AGG: {
      SAggLogicNode* pLogicNode = (SAggLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pAggFuncs);
      nodesDestroyList(pLogicNode->pGroupKeys);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_PROJECT: {
      SProjectLogicNode* pLogicNode = (SProjectLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pProjections);
      break;
    }
X
Xiaoyu Wang 已提交
1108 1109
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY: {
      SVnodeModifyLogicNode* pLogicNode = (SVnodeModifyLogicNode*)pNode;
X
Xiaoyu Wang 已提交
1110 1111
      destroyLogicNode((SLogicNode*)pLogicNode);
      destroyVgDataBlockArray(pLogicNode->pDataBlocks);
X
bugfix  
Xiaoyu Wang 已提交
1112
      // pVgDataBlocks is weak reference
1113
      nodesDestroyNode(pLogicNode->pAffectedRows);
1114 1115
      nodesDestroyNode(pLogicNode->pStartTs);
      nodesDestroyNode(pLogicNode->pEndTs);
1116 1117
      taosMemoryFreeClear(pLogicNode->pVgroupList);
      nodesDestroyList(pLogicNode->pInsertCols);
X
Xiaoyu Wang 已提交
1118 1119 1120 1121 1122
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      destroyLogicNode((SLogicNode*)pNode);
      break;
1123 1124 1125 1126 1127 1128 1129
    case QUERY_NODE_LOGIC_PLAN_MERGE: {
      SMergeLogicNode* pLogicNode = (SMergeLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pMergeKeys);
      nodesDestroyList(pLogicNode->pInputs);
      break;
    }
X
Xiaoyu Wang 已提交
1130 1131 1132 1133 1134
    case QUERY_NODE_LOGIC_PLAN_WINDOW: {
      SWindowLogicNode* pLogicNode = (SWindowLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
      nodesDestroyNode(pLogicNode->pTspk);
5
54liuyao 已提交
1135
      nodesDestroyNode(pLogicNode->pTsEnd);
1136
      nodesDestroyNode(pLogicNode->pStateExpr);
X
Xiaoyu Wang 已提交
1137 1138
      nodesDestroyNode(pLogicNode->pStartCond);
      nodesDestroyNode(pLogicNode->pEndCond);
X
Xiaoyu Wang 已提交
1139 1140
      break;
    }
1141 1142 1143 1144 1145
    case QUERY_NODE_LOGIC_PLAN_FILL: {
      SFillLogicNode* pLogicNode = (SFillLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyNode(pLogicNode->pWStartTs);
      nodesDestroyNode(pLogicNode->pValues);
F
freemine 已提交
1146
      nodesDestroyList(pLogicNode->pFillExprs);
1147
      nodesDestroyList(pLogicNode->pNotFillExprs);
1148 1149
      break;
    }
X
Xiaoyu Wang 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    case QUERY_NODE_LOGIC_PLAN_SORT: {
      SSortLogicNode* pLogicNode = (SSortLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pSortKeys);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_PARTITION: {
      SPartitionLogicNode* pLogicNode = (SPartitionLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pPartitionKeys);
1160 1161
      nodesDestroyList(pLogicNode->pTags);
      nodesDestroyNode(pLogicNode->pSubtable);
X
Xiaoyu Wang 已提交
1162 1163
      break;
    }
1164 1165 1166
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC: {
      SIndefRowsFuncLogicNode* pLogicNode = (SIndefRowsFuncLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
X
Xiaoyu Wang 已提交
1167 1168 1169 1170 1171 1172 1173
      nodesDestroyList(pLogicNode->pFuncs);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC: {
      SInterpFuncLogicNode* pLogicNode = (SInterpFuncLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
X
Xiaoyu Wang 已提交
1174 1175
      nodesDestroyNode(pLogicNode->pFillValues);
      nodesDestroyNode(pLogicNode->pTimeSeries);
1176 1177
      break;
    }
X
Xiaoyu Wang 已提交
1178 1179 1180
    case QUERY_NODE_LOGIC_SUBPLAN: {
      SLogicSubplan* pSubplan = (SLogicSubplan*)pNode;
      nodesDestroyList(pSubplan->pChildren);
1181
      nodesDestroyNode((SNode*)pSubplan->pNode);
X
bugfix  
Xiaoyu Wang 已提交
1182
      nodesClearList(pSubplan->pParents);
X
Xiaoyu Wang 已提交
1183 1184 1185 1186 1187 1188 1189 1190
      taosMemoryFreeClear(pSubplan->pVgroupList);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN:
      nodesDestroyList(((SQueryLogicPlan*)pNode)->pTopSubplans);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
1191
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
X
Xiaoyu Wang 已提交
1192 1193
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
1194 1195
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_COUNT_SCAN: {
1196 1197 1198 1199 1200
      SLastRowScanPhysiNode* pPhyNode = (SLastRowScanPhysiNode*)pNode;
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      nodesDestroyList(pPhyNode->pGroupTags);
      break;
    }
X
Xiaoyu Wang 已提交
1201 1202 1203 1204 1205 1206 1207
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN: {
      STableScanPhysiNode* pPhyNode = (STableScanPhysiNode*)pNode;
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      nodesDestroyList(pPhyNode->pDynamicScanFuncs);
1208
      nodesDestroyList(pPhyNode->pGroupTags);
1209 1210
      nodesDestroyList(pPhyNode->pTags);
      nodesDestroyNode(pPhyNode->pSubtable);
X
Xiaoyu Wang 已提交
1211 1212
      break;
    }
X
Xiaoyu Wang 已提交
1213 1214 1215 1216 1217 1218
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT: {
      SProjectPhysiNode* pPhyNode = (SProjectPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pProjections);
      break;
    }
1219
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN: {
1220
      SSortMergeJoinPhysiNode* pPhyNode = (SSortMergeJoinPhysiNode*)pNode;
X
Xiaoyu Wang 已提交
1221
      destroyPhysiNode((SPhysiNode*)pPhyNode);
1222
      nodesDestroyNode(pPhyNode->pMergeCondition);
X
Xiaoyu Wang 已提交
1223 1224
      nodesDestroyNode(pPhyNode->pOnConditions);
      nodesDestroyList(pPhyNode->pTargets);
S
slzhou 已提交
1225
      nodesDestroyNode(pPhyNode->pColEqualOnConditions);
X
Xiaoyu Wang 已提交
1226 1227
      break;
    }
1228
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG: {
X
Xiaoyu Wang 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
      SAggPhysiNode* pPhyNode = (SAggPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pAggFuncs);
      nodesDestroyList(pPhyNode->pGroupKeys);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: {
      SExchangePhysiNode* pPhyNode = (SExchangePhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pSrcEndPoints);
      break;
    }
1242 1243 1244 1245 1246 1247 1248
    case QUERY_NODE_PHYSICAL_PLAN_MERGE: {
      SMergePhysiNode* pPhyNode = (SMergePhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pMergeKeys);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
1249 1250
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT: {
X
Xiaoyu Wang 已提交
1251 1252
      SSortPhysiNode* pPhyNode = (SSortPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
1253 1254
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pSortKeys);
X
Xiaoyu Wang 已提交
1255
      nodesDestroyList(pPhyNode->pTargets);
X
Xiaoyu Wang 已提交
1256 1257
      break;
    }
X
Xiaoyu Wang 已提交
1258
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
1259
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
X
Xiaoyu Wang 已提交
1260
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
1261 1262
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
1263
      destroyWinodwPhysiNode((SWindowPhysiNode*)pNode);
X
Xiaoyu Wang 已提交
1264
      break;
5
54liuyao 已提交
1265 1266
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FILL: {
1267 1268
      SFillPhysiNode* pPhyNode = (SFillPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
1269 1270
      nodesDestroyList(pPhyNode->pFillExprs);
      nodesDestroyList(pPhyNode->pNotFillExprs);
1271 1272 1273 1274
      nodesDestroyNode(pPhyNode->pWStartTs);
      nodesDestroyNode(pPhyNode->pValues);
      break;
    }
1275 1276
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
1277
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
1278
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
1279
      destroyWinodwPhysiNode((SWindowPhysiNode*)pNode);
X
Xiaoyu Wang 已提交
1280
      break;
1281 1282 1283
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE: {
      SStateWinodwPhysiNode* pPhyNode = (SStateWinodwPhysiNode*)pNode;
1284
      destroyWinodwPhysiNode((SWindowPhysiNode*)pPhyNode);
1285 1286 1287
      nodesDestroyNode(pPhyNode->pStateKey);
      break;
    }
X
Xiaoyu Wang 已提交
1288 1289 1290
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_EVENT:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_EVENT: {
      SEventWinodwPhysiNode* pPhyNode = (SEventWinodwPhysiNode*)pNode;
1291
      destroyWinodwPhysiNode((SWindowPhysiNode*)pPhyNode);
X
Xiaoyu Wang 已提交
1292 1293 1294 1295
      nodesDestroyNode(pPhyNode->pStartCond);
      nodesDestroyNode(pPhyNode->pEndCond);
      break;
    }
1296 1297 1298 1299
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION: {
      destroyPartitionPhysiNode((SPartitionPhysiNode*)pNode);
      break;
    }
1300
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_PARTITION: {
1301 1302 1303 1304
      SStreamPartitionPhysiNode* pPhyNode = (SStreamPartitionPhysiNode*)pNode;
      destroyPartitionPhysiNode((SPartitionPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pTags);
      nodesDestroyNode(pPhyNode->pSubtable);
1305 1306 1307 1308 1309 1310
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC: {
      SIndefRowsFuncPhysiNode* pPhyNode = (SIndefRowsFuncPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
X
Xiaoyu Wang 已提交
1311 1312 1313 1314 1315 1316 1317 1318
      nodesDestroyList(pPhyNode->pFuncs);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC: {
      SInterpFuncPhysiNode* pPhyNode = (SInterpFuncPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pFuncs);
X
Xiaoyu Wang 已提交
1319 1320
      nodesDestroyNode(pPhyNode->pFillValues);
      nodesDestroyNode(pPhyNode->pTimeSeries);
1321 1322
      break;
    }
X
Xiaoyu Wang 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
      destroyDataSinkNode((SDataSinkNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_INSERT: {
      SDataInserterNode* pSink = (SDataInserterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
      taosMemoryFreeClear(pSink->pData);
      break;
    }
1332 1333 1334
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
      SQueryInserterNode* pSink = (SQueryInserterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
D
dapan1121 已提交
1335
      nodesDestroyList(pSink->pCols);
1336 1337
      break;
    }
1338 1339 1340 1341
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
      SDataDeleterNode* pSink = (SDataDeleterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
      nodesDestroyNode(pSink->pAffectedRows);
1342 1343
      nodesDestroyNode(pSink->pStartTs);
      nodesDestroyNode(pSink->pEndTs);
1344 1345
      break;
    }
X
Xiaoyu Wang 已提交
1346 1347
    case QUERY_NODE_PHYSICAL_SUBPLAN: {
      SSubplan* pSubplan = (SSubplan*)pNode;
X
Xiaoyu Wang 已提交
1348
      nodesClearList(pSubplan->pChildren);
1349 1350
      nodesDestroyNode((SNode*)pSubplan->pNode);
      nodesDestroyNode((SNode*)pSubplan->pDataSink);
X
Xiaoyu Wang 已提交
1351 1352
      nodesDestroyNode((SNode*)pSubplan->pTagCond);
      nodesDestroyNode((SNode*)pSubplan->pTagIndexCond);
X
bugfix  
Xiaoyu Wang 已提交
1353 1354 1355
      nodesClearList(pSubplan->pParents);
      break;
    }
1356 1357
    case QUERY_NODE_PHYSICAL_PLAN:
      nodesDestroyList(((SQueryPlan*)pNode)->pSubplans);
X
Xiaoyu Wang 已提交
1358
      break;
1359 1360 1361
    default:
      break;
  }
1362
  nodesFree(pNode);
X
Xiaoyu Wang 已提交
1363
  return;
1364 1365
}

1366
SNodeList* nodesMakeList() {
1367
  SNodeList* p = nodesCalloc(1, sizeof(SNodeList));
1368 1369 1370 1371 1372 1373
  if (NULL == p) {
    return NULL;
  }
  return p;
}

1374
int32_t nodesListAppend(SNodeList* pList, SNode* pNode) {
1375
  if (NULL == pList || NULL == pNode) {
X
Xiaoyu Wang 已提交
1376
    return TSDB_CODE_FAILED;
1377
  }
1378
  SListCell* p = nodesCalloc(1, sizeof(SListCell));
1379
  if (NULL == p) {
X
Xiaoyu Wang 已提交
1380 1381
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_OUT_OF_MEMORY;
1382 1383
  }
  p->pNode = pNode;
1384 1385 1386 1387 1388 1389
  if (NULL == pList->pHead) {
    pList->pHead = p;
  }
  if (NULL != pList->pTail) {
    pList->pTail->pNext = p;
  }
1390
  p->pPrev = pList->pTail;
1391
  pList->pTail = p;
1392
  ++(pList->length);
X
Xiaoyu Wang 已提交
1393
  return TSDB_CODE_SUCCESS;
1394 1395
}

1396
int32_t nodesListStrictAppend(SNodeList* pList, SNode* pNode) {
1397
  if (NULL == pNode) {
X
Xiaoyu Wang 已提交
1398
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1399 1400 1401 1402 1403 1404 1405 1406 1407
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t code = nodesListAppend(pList, pNode);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyNode(pNode);
  }
  return code;
}

1408
int32_t nodesListMakeAppend(SNodeList** pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  if (NULL == *pList) {
    *pList = nodesMakeList();
    if (NULL == *pList) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return nodesListAppend(*pList, pNode);
}

1419
int32_t nodesListMakeStrictAppend(SNodeList** pList, SNode* pNode) {
S
Shengliang Guan 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
  if (NULL == *pList) {
    *pList = nodesMakeList();
    if (NULL == *pList) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return nodesListStrictAppend(*pList, pNode);
}

X
Xiaoyu Wang 已提交
1430
int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) {
X
Xiaoyu Wang 已提交
1431
  if (NULL == pTarget || NULL == pSrc) {
X
Xiaoyu Wang 已提交
1432
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
  }

  if (NULL == pTarget->pHead) {
    pTarget->pHead = pSrc->pHead;
  } else {
    pTarget->pTail->pNext = pSrc->pHead;
    if (NULL != pSrc->pHead) {
      pSrc->pHead->pPrev = pTarget->pTail;
    }
  }
X
Xiaoyu Wang 已提交
1443 1444
  pTarget->pTail = pSrc->pTail;
  pTarget->length += pSrc->length;
1445
  nodesFree(pSrc);
X
Xiaoyu Wang 已提交
1446

X
Xiaoyu Wang 已提交
1447 1448 1449
  return TSDB_CODE_SUCCESS;
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
int32_t nodesListStrictAppendList(SNodeList* pTarget, SNodeList* pSrc) {
  if (NULL == pSrc) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t code = nodesListAppendList(pTarget, pSrc);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyList(pSrc);
  }
  return code;
}

1461
int32_t nodesListPushFront(SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
1462 1463 1464
  if (NULL == pList || NULL == pNode) {
    return TSDB_CODE_FAILED;
  }
1465
  SListCell* p = nodesCalloc(1, sizeof(SListCell));
X
Xiaoyu Wang 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
  if (NULL == p) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  p->pNode = pNode;
  if (NULL != pList->pHead) {
    pList->pHead->pPrev = p;
    p->pNext = pList->pHead;
  }
  pList->pHead = p;
  ++(pList->length);
  return TSDB_CODE_SUCCESS;
}

1480 1481 1482 1483 1484
SListCell* nodesListErase(SNodeList* pList, SListCell* pCell) {
  if (NULL == pCell->pPrev) {
    pList->pHead = pCell->pNext;
  } else {
    pCell->pPrev->pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
1485 1486 1487 1488
  }
  if (NULL == pCell->pNext) {
    pList->pTail = pCell->pPrev;
  } else {
1489 1490 1491
    pCell->pNext->pPrev = pCell->pPrev;
  }
  SListCell* pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
1492
  nodesDestroyNode(pCell->pNode);
1493
  nodesFree(pCell);
1494 1495 1496 1497
  --(pList->length);
  return pNext;
}

X
Xiaoyu Wang 已提交
1498
void nodesListInsertList(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc) {
1499
  if (NULL == pTarget || NULL == pPos || NULL == pSrc || NULL == pSrc->pHead) {
X
Xiaoyu Wang 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
    return;
  }

  if (NULL == pPos->pPrev) {
    pTarget->pHead = pSrc->pHead;
  } else {
    pPos->pPrev->pNext = pSrc->pHead;
  }
  pSrc->pHead->pPrev = pPos->pPrev;
  pSrc->pTail->pNext = pPos;
  pPos->pPrev = pSrc->pTail;

  pTarget->length += pSrc->length;
1513
  nodesFree(pSrc);
X
Xiaoyu Wang 已提交
1514 1515
}

1516
SNode* nodesListGetNode(SNodeList* pList, int32_t index) {
1517 1518 1519 1520 1521 1522 1523 1524 1525
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return node;
    }
  }
  return NULL;
}

X
Xiaoyu Wang 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
SListCell* nodesListGetCell(SNodeList* pList, int32_t index) {
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return cell;
    }
  }
  return NULL;
}

1536
void nodesDestroyList(SNodeList* pList) {
1537 1538 1539
  if (NULL == pList) {
    return;
  }
X
Xiaoyu Wang 已提交
1540

D
dapan1121 已提交
1541 1542 1543
  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    pNext = nodesListErase(pList, pNext);
1544
  }
1545
  nodesFree(pList);
1546 1547
}

X
Xiaoyu Wang 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556
void nodesClearList(SNodeList* pList) {
  if (NULL == pList) {
    return;
  }

  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    SListCell* tmp = pNext;
    pNext = pNext->pNext;
1557
    nodesFree(tmp);
X
Xiaoyu Wang 已提交
1558
  }
1559
  nodesFree(pList);
X
Xiaoyu Wang 已提交
1560 1561
}

X
Xiaoyu Wang 已提交
1562
void* nodesGetValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
    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:
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
    case TSDB_DATA_TYPE_FLOAT:
1575
    case TSDB_DATA_TYPE_DOUBLE:
D
dapan1121 已提交
1576
      return (void*)&pNode->typeData;
D
dapan1121 已提交
1577 1578
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
1579
    case TSDB_DATA_TYPE_VARBINARY:
wmmhello's avatar
wmmhello 已提交
1580
    case TSDB_DATA_TYPE_JSON:
D
Dingle Zhang 已提交
1581
    case TSDB_DATA_TYPE_GEOMETRY:
X
Xiaoyu Wang 已提交
1582
      return (void*)pNode->datum.p;
D
dapan1121 已提交
1583 1584 1585 1586 1587 1588 1589
    default:
      break;
  }

  return NULL;
}

X
Xiaoyu Wang 已提交
1590
int32_t nodesSetValueNodeValue(SValueNode* pNode, void* value) {
D
dapan1121 已提交
1591 1592 1593
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
      pNode->datum.b = *(bool*)value;
D
dapan1121 已提交
1594
      *(bool*)&pNode->typeData = pNode->datum.b;
D
dapan1121 已提交
1595 1596 1597
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pNode->datum.i = *(int8_t*)value;
D
dapan1121 已提交
1598
      *(int8_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1599 1600 1601
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pNode->datum.i = *(int16_t*)value;
D
dapan1121 已提交
1602
      *(int16_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1603 1604 1605
      break;
    case TSDB_DATA_TYPE_INT:
      pNode->datum.i = *(int32_t*)value;
D
dapan1121 已提交
1606
      *(int32_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1607 1608 1609
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
1610
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1611 1612 1613
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
1614
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1615 1616 1617
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pNode->datum.u = *(int8_t*)value;
D
dapan1121 已提交
1618
      *(int8_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1619 1620 1621
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pNode->datum.u = *(int16_t*)value;
D
dapan1121 已提交
1622
      *(int16_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1623 1624 1625
      break;
    case TSDB_DATA_TYPE_UINT:
      pNode->datum.u = *(int32_t*)value;
D
dapan1121 已提交
1626
      *(int32_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1627 1628 1629
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pNode->datum.u = *(uint64_t*)value;
D
dapan1121 已提交
1630
      *(uint64_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1631 1632 1633
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pNode->datum.d = *(float*)value;
D
dapan1121 已提交
1634
      *(float*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
1635 1636 1637
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pNode->datum.d = *(double*)value;
D
dapan1121 已提交
1638
      *(double*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
1639 1640 1641 1642
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
wmmhello's avatar
wmmhello 已提交
1643
    case TSDB_DATA_TYPE_JSON:
D
Dingle Zhang 已提交
1644
    case TSDB_DATA_TYPE_GEOMETRY:
D
dapan1121 已提交
1645 1646 1647
      pNode->datum.p = (char*)value;
      break;
    default:
S
Shengliang Guan 已提交
1648
      return TSDB_CODE_APP_ERROR;
D
dapan1121 已提交
1649 1650 1651 1652 1653
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1654
char* nodesGetStrValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
1655
  switch (pNode->node.resType.type) {
D
dapan1121 已提交
1656
    case TSDB_DATA_TYPE_BOOL: {
X
Xiaoyu Wang 已提交
1657
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1658 1659 1660
      if (NULL == buf) {
        return NULL;
      }
1661

D
dapan1121 已提交
1662 1663
      sprintf(buf, "%s", pNode->datum.b ? "true" : "false");
      return buf;
D
dapan1121 已提交
1664
    }
D
dapan1121 已提交
1665 1666 1667 1668 1669
    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: {
X
Xiaoyu Wang 已提交
1670
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1671 1672 1673
      if (NULL == buf) {
        return NULL;
      }
1674

D
dapan1121 已提交
1675 1676 1677 1678 1679 1680 1681
      sprintf(buf, "%" PRId64, pNode->datum.i);
      return buf;
    }
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT: {
X
Xiaoyu Wang 已提交
1682
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1683 1684 1685
      if (NULL == buf) {
        return NULL;
      }
1686

D
dapan1121 已提交
1687 1688 1689 1690 1691
      sprintf(buf, "%" PRIu64, pNode->datum.u);
      return buf;
    }
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE: {
X
Xiaoyu Wang 已提交
1692
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1693 1694 1695
      if (NULL == buf) {
        return NULL;
      }
1696

D
dapan1121 已提交
1697 1698 1699 1700 1701
      sprintf(buf, "%e", pNode->datum.d);
      return buf;
    }
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
D
Dingle Zhang 已提交
1702 1703
    case TSDB_DATA_TYPE_VARBINARY:
    case TSDB_DATA_TYPE_GEOMETRY: {
D
dapan1121 已提交
1704
      int32_t bufSize = varDataLen(pNode->datum.p) + 2 + 1;
X
Xiaoyu Wang 已提交
1705
      void*   buf = taosMemoryMalloc(bufSize);
D
dapan1121 已提交
1706 1707 1708
      if (NULL == buf) {
        return NULL;
      }
1709

D
dapan1121 已提交
1710
      snprintf(buf, bufSize, "'%s'", varDataVal(pNode->datum.p));
D
dapan1121 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719
      return buf;
    }
    default:
      break;
  }

  return NULL;
}

1720 1721
bool nodesIsExprNode(const SNode* pNode) {
  ENodeType type = nodeType(pNode);
X
Xiaoyu Wang 已提交
1722
  return (QUERY_NODE_COLUMN == type || QUERY_NODE_VALUE == type || QUERY_NODE_OPERATOR == type ||
D
dapan1121 已提交
1723
          QUERY_NODE_FUNCTION == type || QUERY_NODE_LOGIC_CONDITION == type || QUERY_NODE_CASE_WHEN == type);
1724 1725
}

X
Xiaoyu Wang 已提交
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
bool nodesIsUnaryOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_MINUS:
    case OP_TYPE_IS_NULL:
    case OP_TYPE_IS_NOT_NULL:
    case OP_TYPE_IS_TRUE:
    case OP_TYPE_IS_FALSE:
    case OP_TYPE_IS_UNKNOWN:
    case OP_TYPE_IS_NOT_TRUE:
    case OP_TYPE_IS_NOT_FALSE:
    case OP_TYPE_IS_NOT_UNKNOWN:
      return true;
    default:
      break;
  }
  return false;
}

1744 1745 1746 1747 1748 1749
bool nodesIsArithmeticOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_ADD:
    case OP_TYPE_SUB:
    case OP_TYPE_MULTI:
    case OP_TYPE_DIV:
1750
    case OP_TYPE_REM:
1751 1752 1753 1754 1755 1756
      return true;
    default:
      break;
  }
  return false;
}
1757

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
bool nodesIsComparisonOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_GREATER_THAN:
    case OP_TYPE_GREATER_EQUAL:
    case OP_TYPE_LOWER_THAN:
    case OP_TYPE_LOWER_EQUAL:
    case OP_TYPE_EQUAL:
    case OP_TYPE_NOT_EQUAL:
    case OP_TYPE_IN:
    case OP_TYPE_NOT_IN:
    case OP_TYPE_LIKE:
    case OP_TYPE_NOT_LIKE:
    case OP_TYPE_MATCH:
    case OP_TYPE_NMATCH:
1772
    case OP_TYPE_JSON_CONTAINS:
X
Xiaoyu Wang 已提交
1773 1774 1775 1776 1777 1778 1779 1780
    case OP_TYPE_IS_NULL:
    case OP_TYPE_IS_NOT_NULL:
    case OP_TYPE_IS_TRUE:
    case OP_TYPE_IS_FALSE:
    case OP_TYPE_IS_UNKNOWN:
    case OP_TYPE_IS_NOT_TRUE:
    case OP_TYPE_IS_NOT_FALSE:
    case OP_TYPE_IS_NOT_UNKNOWN:
1781 1782 1783 1784 1785
      return true;
    default:
      break;
  }
  return false;
1786 1787
}

1788 1789 1790
bool nodesIsJsonOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_JSON_GET_VALUE:
wmmhello's avatar
wmmhello 已提交
1791
    case OP_TYPE_JSON_CONTAINS:
1792 1793 1794 1795 1796 1797
      return true;
    default:
      break;
  }
  return false;
}
1798

X
Xiaoyu Wang 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
bool nodesIsRegularOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_LIKE:
    case OP_TYPE_NOT_LIKE:
    case OP_TYPE_MATCH:
    case OP_TYPE_NMATCH:
      return true;
    default:
      break;
  }
  return false;
}

X
Xiaoyu Wang 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
bool nodesIsBitwiseOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_BIT_AND:
    case OP_TYPE_BIT_OR:
      return true;
    default:
      break;
  }
  return false;
}

X
Xiaoyu Wang 已提交
1823
typedef struct SCollectColumnsCxt {
1824 1825 1826 1827 1828
  int32_t         errCode;
  const char*     pTableAlias;
  ECollectColType collectType;
  SNodeList*      pCols;
  SHashObj*       pColHash;
X
Xiaoyu Wang 已提交
1829 1830
} SCollectColumnsCxt;

1831
static EDealRes doCollect(SCollectColumnsCxt* pCxt, SColumnNode* pCol, SNode* pNode) {
X
Xiaoyu Wang 已提交
1832
  char    name[TSDB_TABLE_NAME_LEN + TSDB_COL_NAME_LEN];
1833 1834
  int32_t len = 0;
  if ('\0' == pCol->tableAlias[0]) {
X
Xiaoyu Wang 已提交
1835 1836 1837
    len = snprintf(name, sizeof(name), "%s", pCol->colName);
  } else {
    len = snprintf(name, sizeof(name), "%s.%s", pCol->tableAlias, pCol->colName);
1838 1839 1840
  }
  if (NULL == taosHashGet(pCxt->pColHash, name, len)) {
    pCxt->errCode = taosHashPut(pCxt->pColHash, name, len, NULL, 0);
X
Xiaoyu Wang 已提交
1841
    if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1842
      pCxt->errCode = nodesListStrictAppend(pCxt->pCols, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1843 1844 1845 1846 1847 1848
    }
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

1849 1850 1851 1852 1853 1854
static bool isCollectType(ECollectColType collectType, EColumnType colType) {
  return COLLECT_COL_TYPE_ALL == collectType
             ? true
             : (COLLECT_COL_TYPE_TAG == collectType ? COLUMN_TYPE_TAG == colType : COLUMN_TYPE_TAG != colType);
}

X
Xiaoyu Wang 已提交
1855 1856
static EDealRes collectColumns(SNode* pNode, void* pContext) {
  SCollectColumnsCxt* pCxt = (SCollectColumnsCxt*)pContext;
X
Xiaoyu Wang 已提交
1857
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
X
Xiaoyu Wang 已提交
1858
    SColumnNode* pCol = (SColumnNode*)pNode;
X
Xiaoyu Wang 已提交
1859
    if (isCollectType(pCxt->collectType, pCol->colType) && 0 != strcmp(pCol->colName, "*") &&
1860
        (NULL == pCxt->pTableAlias || 0 == strcmp(pCxt->pTableAlias, pCol->tableAlias))) {
1861
      return doCollect(pCxt, pCol, pNode);
X
Xiaoyu Wang 已提交
1862 1863 1864 1865 1866
    }
  }
  return DEAL_RES_CONTINUE;
}

1867 1868
int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, ECollectColType type,
                            SNodeList** pCols) {
X
Xiaoyu Wang 已提交
1869
  if (NULL == pSelect || NULL == pCols) {
1870
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1871 1872 1873
  }

  SCollectColumnsCxt cxt = {
X
Xiaoyu Wang 已提交
1874 1875
      .errCode = TSDB_CODE_SUCCESS,
      .pTableAlias = pTableAlias,
1876
      .collectType = type,
1877
      .pCols = (NULL == *pCols ? nodesMakeList() : *pCols),
X
Xiaoyu Wang 已提交
1878
      .pColHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK)};
1879
  if (NULL == cxt.pCols || NULL == cxt.pColHash) {
X
Xiaoyu Wang 已提交
1880 1881
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1882
  *pCols = NULL;
X
Xiaoyu Wang 已提交
1883
  nodesWalkSelectStmt(pSelect, clause, collectColumns, &cxt);
1884
  taosHashCleanup(cxt.pColHash);
X
Xiaoyu Wang 已提交
1885
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
1886
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1887 1888
    return cxt.errCode;
  }
1889 1890 1891
  if (LIST_LENGTH(cxt.pCols) > 0) {
    *pCols = cxt.pCols;
  } else {
1892
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1893
  }
1894

X
Xiaoyu Wang 已提交
1895 1896 1897
  return TSDB_CODE_SUCCESS;
}

1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
int32_t nodesCollectColumnsFromNode(SNode* node, const char* pTableAlias, ECollectColType type, SNodeList** pCols) {
  if (NULL == pCols) {
    return TSDB_CODE_FAILED;
  }
  SCollectColumnsCxt cxt = {
      .errCode = TSDB_CODE_SUCCESS,
      .pTableAlias = pTableAlias,
      .collectType = type,
      .pCols = (NULL == *pCols ? nodesMakeList() : *pCols),
      .pColHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK)};
  if (NULL == cxt.pCols || NULL == cxt.pColHash) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  *pCols = NULL;

  nodesWalkExpr(node, collectColumns, &cxt);

  taosHashCleanup(cxt.pColHash);
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
    nodesDestroyList(cxt.pCols);
    return cxt.errCode;
  }
  if (LIST_LENGTH(cxt.pCols) > 0) {
    *pCols = cxt.pCols;
  } else {
    nodesDestroyList(cxt.pCols);
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1929
typedef struct SCollectFuncsCxt {
X
Xiaoyu Wang 已提交
1930
  int32_t         errCode;
X
Xiaoyu Wang 已提交
1931
  FFuncClassifier classifier;
X
Xiaoyu Wang 已提交
1932
  SNodeList*      pFuncs;
X
Xiaoyu Wang 已提交
1933
  SHashObj*       pFuncsSet;
X
Xiaoyu Wang 已提交
1934 1935 1936 1937
} SCollectFuncsCxt;

static EDealRes collectFuncs(SNode* pNode, void* pContext) {
  SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext;
1938 1939
  if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId) &&
      !(((SExprNode*)pNode)->orderAlias)) {
X
Xiaoyu Wang 已提交
1940
    SExprNode* pExpr = (SExprNode*)pNode;
X
Xiaoyu Wang 已提交
1941
    if (NULL == taosHashGet(pCxt->pFuncsSet, &pExpr, sizeof(SExprNode*))) {
X
Xiaoyu Wang 已提交
1942
      pCxt->errCode = nodesListStrictAppend(pCxt->pFuncs, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1943
      taosHashPut(pCxt->pFuncsSet, &pExpr, POINTER_BYTES, &pExpr, POINTER_BYTES);
X
Xiaoyu Wang 已提交
1944
    }
X
Xiaoyu Wang 已提交
1945 1946 1947 1948 1949
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1950 1951 1952 1953 1954 1955
static uint32_t funcNodeHash(const char* pKey, uint32_t len) {
  SExprNode* pExpr = *(SExprNode**)pKey;
  return MurmurHash3_32(pExpr->aliasName, strlen(pExpr->aliasName));
}

static int32_t funcNodeEqual(const void* pLeft, const void* pRight, size_t len) {
X
Xiaoyu Wang 已提交
1956 1957 1958
  if (0 != strcmp((*(const SExprNode**)pLeft)->aliasName, (*(const SExprNode**)pRight)->aliasName)) {
    return 1;
  }
X
Xiaoyu Wang 已提交
1959 1960 1961
  return nodesEqualNode(*(const SNode**)pLeft, *(const SNode**)pRight) ? 0 : 1;
}

X
Xiaoyu Wang 已提交
1962
int32_t nodesCollectFuncs(SSelectStmt* pSelect, ESqlClause clause, FFuncClassifier classifier, SNodeList** pFuncs) {
X
Xiaoyu Wang 已提交
1963
  if (NULL == pSelect || NULL == pFuncs) {
1964
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1965 1966
  }

X
Xiaoyu Wang 已提交
1967 1968 1969 1970 1971
  SCollectFuncsCxt cxt = {.errCode = TSDB_CODE_SUCCESS,
                          .classifier = classifier,
                          .pFuncs = (NULL == *pFuncs ? nodesMakeList() : *pFuncs),
                          .pFuncsSet = taosHashInit(4, funcNodeHash, false, false)};
  if (NULL == cxt.pFuncs || NULL == cxt.pFuncsSet) {
X
Xiaoyu Wang 已提交
1972 1973
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1974
  taosHashSetEqualFp(cxt.pFuncsSet, funcNodeEqual);
1975
  *pFuncs = NULL;
X
Xiaoyu Wang 已提交
1976
  nodesWalkSelectStmt(pSelect, clause, collectFuncs, &cxt);
X
Xiaoyu Wang 已提交
1977 1978 1979 1980 1981 1982
  if (TSDB_CODE_SUCCESS == cxt.errCode) {
    if (LIST_LENGTH(cxt.pFuncs) > 0) {
      *pFuncs = cxt.pFuncs;
    } else {
      nodesDestroyList(cxt.pFuncs);
    }
X
Xiaoyu Wang 已提交
1983 1984 1985
  } else {
    nodesDestroyList(cxt.pFuncs);
  }
X
Xiaoyu Wang 已提交
1986
  taosHashCleanup(cxt.pFuncsSet);
X
Xiaoyu Wang 已提交
1987

X
Xiaoyu Wang 已提交
1988
  return cxt.errCode;
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
}

typedef struct SCollectSpecialNodesCxt {
  int32_t    errCode;
  ENodeType  type;
  SNodeList* pNodes;
} SCollectSpecialNodesCxt;

static EDealRes collectSpecialNodes(SNode* pNode, void* pContext) {
  SCollectSpecialNodesCxt* pCxt = (SCollectSpecialNodesCxt*)pContext;
  if (pCxt->type == nodeType(pNode)) {
    pCxt->errCode = nodesListStrictAppend(pCxt->pNodes, nodesCloneNode(pNode));
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

int32_t nodesCollectSpecialNodes(SSelectStmt* pSelect, ESqlClause clause, ENodeType type, SNodeList** pNodes) {
  if (NULL == pSelect || NULL == pNodes) {
    return TSDB_CODE_FAILED;
  }

  SCollectSpecialNodesCxt cxt = {
      .errCode = TSDB_CODE_SUCCESS, .type = type, .pNodes = (NULL == *pNodes ? nodesMakeList() : *pNodes)};
  if (NULL == cxt.pNodes) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  *pNodes = NULL;
  nodesWalkSelectStmt(pSelect, SQL_CLAUSE_GROUP_BY, collectSpecialNodes, &cxt);
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
    nodesDestroyList(cxt.pNodes);
    return cxt.errCode;
  }
  if (LIST_LENGTH(cxt.pNodes) > 0) {
    *pNodes = cxt.pNodes;
  } else {
    nodesDestroyList(cxt.pNodes);
  }

X
Xiaoyu Wang 已提交
2028 2029
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
2030

2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
static EDealRes hasColumn(SNode* pNode, void* pContext) {
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    *(bool*)pContext = true;
    return DEAL_RES_END;
  }
  return DEAL_RES_CONTINUE;
}

bool nodesExprHasColumn(SNode* pNode) {
  bool hasCol = false;
  nodesWalkExprPostOrder(pNode, hasColumn, &hasCol);
  return hasCol;
}

bool nodesExprsHasColumn(SNodeList* pList) {
  bool hasCol = false;
  nodesWalkExprsPostOrder(pList, hasColumn, &hasCol);
  return hasCol;
}

X
Xiaoyu Wang 已提交
2051
char* nodesGetFillModeString(EFillMode mode) {
D
dapan1121 已提交
2052 2053 2054 2055 2056
  switch (mode) {
    case FILL_MODE_NONE:
      return "none";
    case FILL_MODE_VALUE:
      return "value";
D
dapan1121 已提交
2057 2058
    case FILL_MODE_VALUE_F:
      return "value_f";
D
dapan1121 已提交
2059 2060 2061 2062
    case FILL_MODE_PREV:
      return "prev";
    case FILL_MODE_NULL:
      return "null";
D
dapan1121 已提交
2063 2064
    case FILL_MODE_NULL_F:
      return "null_f";
D
dapan1121 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073
    case FILL_MODE_LINEAR:
      return "linear";
    case FILL_MODE_NEXT:
      return "next";
    default:
      return "unknown";
  }
}

X
Xiaoyu Wang 已提交
2074
char* nodesGetNameFromColumnNode(SNode* pNode) {
D
dapan1121 已提交
2075 2076 2077
  if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) {
    return "NULL";
  }
X
Xiaoyu Wang 已提交
2078

2079
  return ((SColumnNode*)pNode)->node.userAlias;
D
dapan1121 已提交
2080 2081
}

D
dapan1121 已提交
2082 2083 2084 2085 2086
int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) {
  if (NULL == pSlots || pSlots->length <= 0) {
    return 0;
  }

X
Xiaoyu Wang 已提交
2087
  SNode*  pNode = NULL;
D
dapan1121 已提交
2088 2089 2090 2091 2092 2093
  int32_t num = 0;
  FOREACH(pNode, pSlots) {
    if (QUERY_NODE_SLOT_DESC != pNode->type) {
      continue;
    }

X
Xiaoyu Wang 已提交
2094
    SSlotDescNode* descNode = (SSlotDescNode*)pNode;
D
dapan1121 已提交
2095 2096 2097 2098 2099 2100 2101 2102
    if (descNode->output) {
      ++num;
    }
  }

  return num;
}

X
Xiaoyu Wang 已提交
2103
void nodesValueNodeToVariant(const SValueNode* pNode, SVariant* pVal) {
2104 2105 2106 2107 2108
  if (pNode->isNull) {
    pVal->nType = TSDB_DATA_TYPE_NULL;
    pVal->nLen = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
    return;
  }
D
dapan1121 已提交
2109 2110 2111 2112
  pVal->nType = pNode->node.resType.type;
  pVal->nLen = pNode->node.resType.bytes;
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_NULL:
X
Xiaoyu Wang 已提交
2113
      break;
D
dapan1121 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    case TSDB_DATA_TYPE_BOOL:
      pVal->i = pNode->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:
      pVal->i = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      pVal->u = pNode->datum.u;
      break;
    case TSDB_DATA_TYPE_FLOAT:
2131 2132
      pVal->f = pNode->datum.d;
      break;
D
dapan1121 已提交
2133 2134 2135 2136 2137 2138
    case TSDB_DATA_TYPE_DOUBLE:
      pVal->d = pNode->datum.d;
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
D
Dingle Zhang 已提交
2139
    case TSDB_DATA_TYPE_GEOMETRY:
H
Haojun Liao 已提交
2140 2141 2142
      pVal->pz = taosMemoryMalloc(pVal->nLen + 1);
      memcpy(pVal->pz, pNode->datum.p, pVal->nLen);
      pVal->pz[pVal->nLen] = 0;
D
dapan1121 已提交
2143 2144
      break;
    case TSDB_DATA_TYPE_JSON:
wmmhello's avatar
wmmhello 已提交
2145 2146 2147 2148
      pVal->nLen = getJsonValueLen(pNode->datum.p);
      pVal->pz = taosMemoryMalloc(pVal->nLen);
      memcpy(pVal->pz, pNode->datum.p, pVal->nLen);
      break;
D
dapan1121 已提交
2149 2150 2151 2152 2153 2154 2155
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }
}
X
Xiaoyu Wang 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165

int32_t nodesMergeConds(SNode** pDst, SNodeList** pSrc) {
  if (NULL == *pSrc) {
    return TSDB_CODE_SUCCESS;
  }

  if (1 == LIST_LENGTH(*pSrc)) {
    *pDst = nodesListGetNode(*pSrc, 0);
    nodesClearList(*pSrc);
  } else {
2166
    SLogicConditionNode* pLogicCond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
X
Xiaoyu Wang 已提交
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
    if (NULL == pLogicCond) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pLogicCond->node.resType.type = TSDB_DATA_TYPE_BOOL;
    pLogicCond->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
    pLogicCond->condType = LOGIC_COND_TYPE_AND;
    pLogicCond->pParameterList = *pSrc;
    *pDst = (SNode*)pLogicCond;
  }
  *pSrc = NULL;

  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195

const char* dataOrderStr(EDataOrderLevel order) {
  switch (order) {
    case DATA_ORDER_LEVEL_NONE:
      return "no order required";
    case DATA_ORDER_LEVEL_IN_BLOCK:
      return "in-datablock order";
    case DATA_ORDER_LEVEL_IN_GROUP:
      return "in-group order";
    case DATA_ORDER_LEVEL_GLOBAL:
      return "global order";
    default:
      break;
  }
  return "unknown";
}