nodesUtilFuncs.c 42.4 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"
X
Xiaoyu Wang 已提交
22
#include "thash.h"
23

24
int32_t nodesNodeSize(ENodeType type) {
25 26
  switch (type) {
    case QUERY_NODE_COLUMN:
27
      return sizeof(SColumnNode);
28
    case QUERY_NODE_VALUE:
29
      return sizeof(SValueNode);
30
    case QUERY_NODE_OPERATOR:
31
      return sizeof(SOperatorNode);
32
    case QUERY_NODE_LOGIC_CONDITION:
33
      return sizeof(SLogicConditionNode);
34
    case QUERY_NODE_FUNCTION:
35
      return sizeof(SFunctionNode);
36
    case QUERY_NODE_REAL_TABLE:
37
      return sizeof(SRealTableNode);
38
    case QUERY_NODE_TEMP_TABLE:
39
      return sizeof(STempTableNode);
40
    case QUERY_NODE_JOIN_TABLE:
41
      return sizeof(SJoinTableNode);
42
    case QUERY_NODE_GROUPING_SET:
43
      return sizeof(SGroupingSetNode);
44
    case QUERY_NODE_ORDER_BY_EXPR:
45
      return sizeof(SOrderByExprNode);
46
    case QUERY_NODE_LIMIT:
47
      return sizeof(SLimitNode);
48
    case QUERY_NODE_STATE_WINDOW:
49
      return sizeof(SStateWindowNode);
50
    case QUERY_NODE_SESSION_WINDOW:
51
      return sizeof(SSessionWindowNode);
52
    case QUERY_NODE_INTERVAL_WINDOW:
53
      return sizeof(SIntervalWindowNode);
54
    case QUERY_NODE_NODE_LIST:
55
      return sizeof(SNodeListNode);
56
    case QUERY_NODE_FILL:
57
      return sizeof(SFillNode);
58
    case QUERY_NODE_RAW_EXPR:
59
      return sizeof(SRawExprNode);
60
    case QUERY_NODE_TARGET:
61
      return sizeof(STargetNode);
62
    case QUERY_NODE_DATABLOCK_DESC:
63
      return sizeof(SDataBlockDescNode);
64
    case QUERY_NODE_SLOT_DESC:
65
      return sizeof(SSlotDescNode);
66
    case QUERY_NODE_COLUMN_DEF:
67
      return sizeof(SColumnDefNode);
X
Xiaoyu Wang 已提交
68
    case QUERY_NODE_DOWNSTREAM_SOURCE:
69
      return sizeof(SDownstreamSourceNode);
X
Xiaoyu Wang 已提交
70
    case QUERY_NODE_DATABASE_OPTIONS:
71
      return sizeof(SDatabaseOptions);
X
Xiaoyu Wang 已提交
72
    case QUERY_NODE_TABLE_OPTIONS:
73
      return sizeof(STableOptions);
X
Xiaoyu Wang 已提交
74
    case QUERY_NODE_INDEX_OPTIONS:
75
      return sizeof(SIndexOptions);
76
    case QUERY_NODE_EXPLAIN_OPTIONS:
77
      return sizeof(SExplainOptions);
78
    case QUERY_NODE_STREAM_OPTIONS:
79
      return sizeof(SStreamOptions);
X
Xiaoyu Wang 已提交
80
    case QUERY_NODE_TOPIC_OPTIONS:
81
      return sizeof(STopicOptions);
82 83
    case QUERY_NODE_LEFT_VALUE:
      return sizeof(SLeftValueNode);
84
    case QUERY_NODE_SET_OPERATOR:
85
      return sizeof(SSetOperator);
86
    case QUERY_NODE_SELECT_STMT:
87
      return sizeof(SSelectStmt);
88
    case QUERY_NODE_VNODE_MODIF_STMT:
89
      return sizeof(SVnodeModifOpStmt);
90
    case QUERY_NODE_CREATE_DATABASE_STMT:
91
      return sizeof(SCreateDatabaseStmt);
92
    case QUERY_NODE_DROP_DATABASE_STMT:
93
      return sizeof(SDropDatabaseStmt);
94
    case QUERY_NODE_ALTER_DATABASE_STMT:
95
      return sizeof(SAlterDatabaseStmt);
96
    case QUERY_NODE_CREATE_TABLE_STMT:
97
      return sizeof(SCreateTableStmt);
98
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE:
99
      return sizeof(SCreateSubTableClause);
100
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
101
      return sizeof(SCreateMultiTableStmt);
102
    case QUERY_NODE_DROP_TABLE_CLAUSE:
103
      return sizeof(SDropTableClause);
104
    case QUERY_NODE_DROP_TABLE_STMT:
105
      return sizeof(SDropTableStmt);
106
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:
107
      return sizeof(SDropSuperTableStmt);
108
    case QUERY_NODE_ALTER_TABLE_STMT:
109
      return sizeof(SAlterTableStmt);
110
    case QUERY_NODE_CREATE_USER_STMT:
111
      return sizeof(SCreateUserStmt);
112
    case QUERY_NODE_ALTER_USER_STMT:
113
      return sizeof(SAlterUserStmt);
114
    case QUERY_NODE_DROP_USER_STMT:
115
      return sizeof(SDropUserStmt);
116
    case QUERY_NODE_USE_DATABASE_STMT:
117
      return sizeof(SUseDatabaseStmt);
118
    case QUERY_NODE_CREATE_DNODE_STMT:
119
      return sizeof(SCreateDnodeStmt);
120
    case QUERY_NODE_DROP_DNODE_STMT:
121
      return sizeof(SDropDnodeStmt);
122
    case QUERY_NODE_ALTER_DNODE_STMT:
123
      return sizeof(SAlterDnodeStmt);
X
Xiaoyu Wang 已提交
124
    case QUERY_NODE_CREATE_INDEX_STMT:
125
      return sizeof(SCreateIndexStmt);
126
    case QUERY_NODE_DROP_INDEX_STMT:
127
      return sizeof(SDropIndexStmt);
X
Xiaoyu Wang 已提交
128
    case QUERY_NODE_CREATE_QNODE_STMT:
129 130 131
    case QUERY_NODE_CREATE_BNODE_STMT:
    case QUERY_NODE_CREATE_SNODE_STMT:
    case QUERY_NODE_CREATE_MNODE_STMT:
132
      return sizeof(SCreateComponentNodeStmt);
133
    case QUERY_NODE_DROP_QNODE_STMT:
134 135 136
    case QUERY_NODE_DROP_BNODE_STMT:
    case QUERY_NODE_DROP_SNODE_STMT:
    case QUERY_NODE_DROP_MNODE_STMT:
137
      return sizeof(SDropComponentNodeStmt);
138
    case QUERY_NODE_CREATE_TOPIC_STMT:
139
      return sizeof(SCreateTopicStmt);
140
    case QUERY_NODE_DROP_TOPIC_STMT:
141
      return sizeof(SDropTopicStmt);
142
    case QUERY_NODE_EXPLAIN_STMT:
143
      return sizeof(SExplainStmt);
144
    case QUERY_NODE_DESCRIBE_STMT:
145
      return sizeof(SDescribeStmt);
146
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
147
      return sizeof(SNode);
148
    case QUERY_NODE_COMPACT_STMT:
149
      break;
150
    case QUERY_NODE_CREATE_FUNCTION_STMT:
151
      return sizeof(SCreateFunctionStmt);
152
    case QUERY_NODE_DROP_FUNCTION_STMT:
153
      return sizeof(SDropFunctionStmt);
154
    case QUERY_NODE_CREATE_STREAM_STMT:
155
      return sizeof(SCreateStreamStmt);
156
    case QUERY_NODE_DROP_STREAM_STMT:
157
      return sizeof(SDropStreamStmt);
158 159 160 161 162
    case QUERY_NODE_MERGE_VGROUP_STMT:
    case QUERY_NODE_REDISTRIBUTE_VGROUP_STMT:
    case QUERY_NODE_SPLIT_VGROUP_STMT:
    case QUERY_NODE_SYNCDB_STMT:
      break;
163
    case QUERY_NODE_GRANT_STMT:
164
      return sizeof(SGrantStmt);
165
    case QUERY_NODE_REVOKE_STMT:
166
      return sizeof(SRevokeStmt);
167
    case QUERY_NODE_SHOW_DNODES_STMT:
X
Xiaoyu Wang 已提交
168
    case QUERY_NODE_SHOW_MNODES_STMT:
X
Xiaoyu Wang 已提交
169 170
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
171 172
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
173
    case QUERY_NODE_SHOW_CLUSTER_STMT:
174
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
175 176
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
177
    case QUERY_NODE_SHOW_STABLES_STMT:
X
Xiaoyu Wang 已提交
178
    case QUERY_NODE_SHOW_STREAMS_STMT:
179 180
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
S
Shengliang Guan 已提交
181
    case QUERY_NODE_SHOW_LICENCE_STMT:
182
    case QUERY_NODE_SHOW_VGROUPS_STMT:
S
Shengliang Guan 已提交
183
    case QUERY_NODE_SHOW_TOPICS_STMT:
184 185 186 187 188 189
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
    case QUERY_NODE_SHOW_SUBSCRIBES_STMT:
    case QUERY_NODE_SHOW_SMAS_STMT:
    case QUERY_NODE_SHOW_CONFIGS_STMT:
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_VNODES_STMT:
190 191 192 193 194 195
    case QUERY_NODE_SHOW_APPS_STMT:
    case QUERY_NODE_SHOW_SCORES_STMT:
    case QUERY_NODE_SHOW_VARIABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
196
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
197
      return sizeof(SShowStmt);
198 199
    case QUERY_NODE_KILL_CONNECTION_STMT:
    case QUERY_NODE_KILL_QUERY_STMT:
200
    case QUERY_NODE_KILL_TRANSACTION_STMT:
201
      return sizeof(SKillStmt);
X
Xiaoyu Wang 已提交
202
    case QUERY_NODE_LOGIC_PLAN_SCAN:
203
      return sizeof(SScanLogicNode);
X
Xiaoyu Wang 已提交
204
    case QUERY_NODE_LOGIC_PLAN_JOIN:
205
      return sizeof(SJoinLogicNode);
X
Xiaoyu Wang 已提交
206
    case QUERY_NODE_LOGIC_PLAN_AGG:
207
      return sizeof(SAggLogicNode);
X
Xiaoyu Wang 已提交
208
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
209
      return sizeof(SProjectLogicNode);
210
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIF:
211
      return sizeof(SVnodeModifLogicNode);
X
Xiaoyu Wang 已提交
212
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
213
      return sizeof(SExchangeLogicNode);
X
Xiaoyu Wang 已提交
214
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
215
      return sizeof(SWindowLogicNode);
X
Xiaoyu Wang 已提交
216
    case QUERY_NODE_LOGIC_PLAN_FILL:
217
      return sizeof(SFillLogicNode);
X
Xiaoyu Wang 已提交
218
    case QUERY_NODE_LOGIC_PLAN_SORT:
219
      return sizeof(SSortLogicNode);
220
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
221
      return sizeof(SPartitionLogicNode);
X
Xiaoyu Wang 已提交
222
    case QUERY_NODE_LOGIC_SUBPLAN:
223
      return sizeof(SLogicSubplan);
X
Xiaoyu Wang 已提交
224
    case QUERY_NODE_LOGIC_PLAN:
225
      return sizeof(SQueryLogicPlan);
X
Xiaoyu Wang 已提交
226
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
227
      return sizeof(STagScanPhysiNode);
X
Xiaoyu Wang 已提交
228
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
229
      return sizeof(STableScanPhysiNode);
230
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
231
      return sizeof(STableSeqScanPhysiNode);
232
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
233
      return sizeof(SStreamScanPhysiNode);
X
Xiaoyu Wang 已提交
234
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
235
      return sizeof(SSystemTableScanPhysiNode);
X
Xiaoyu Wang 已提交
236
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
237
      return sizeof(SProjectPhysiNode);
X
Xiaoyu Wang 已提交
238
    case QUERY_NODE_PHYSICAL_PLAN_JOIN:
239
      return sizeof(SJoinPhysiNode);
X
Xiaoyu Wang 已提交
240
    case QUERY_NODE_PHYSICAL_PLAN_AGG:
241
      return sizeof(SAggPhysiNode);
242
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
243
      return sizeof(SExchangePhysiNode);
244
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
245
      return sizeof(SSortPhysiNode);
X
Xiaoyu Wang 已提交
246
    case QUERY_NODE_PHYSICAL_PLAN_INTERVAL:
247
      return sizeof(SIntervalPhysiNode);
X
Xiaoyu Wang 已提交
248
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
249
      return sizeof(SStreamIntervalPhysiNode);
X
Xiaoyu Wang 已提交
250
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
251
      return sizeof(SFillPhysiNode);
252
    case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW:
253
      return sizeof(SSessionWinodwPhysiNode);
254
    case QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW:
255
      return sizeof(SStateWinodwPhysiNode);
256
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
257
      return sizeof(SPartitionPhysiNode);
258
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
259
      return sizeof(SDataDispatcherNode);
260
    case QUERY_NODE_PHYSICAL_PLAN_INSERT:
261
      return sizeof(SDataInserterNode);
X
Xiaoyu Wang 已提交
262
    case QUERY_NODE_PHYSICAL_SUBPLAN:
263
      return sizeof(SSubplan);
X
Xiaoyu Wang 已提交
264
    case QUERY_NODE_PHYSICAL_PLAN:
265
      return sizeof(SQueryPlan);
266 267 268
    default:
      break;
  }
269
  nodesError("nodesMakeNode unknown node = %s", nodesNodeName(type));
270 271 272 273 274 275 276 277 278 279
  return 0;
}

SNodeptr nodesMakeNode(ENodeType type) {
  SNode* p = taosMemoryCalloc(1, nodesNodeSize(type));
  if (NULL == p) {
    return NULL;
  }
  setNodeType(p, type);
  return p;
280 281
}

X
Xiaoyu Wang 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
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->pChildren);
  nodesDestroyNode(pNode->pConditions);
  nodesDestroyList(pNode->pTargets);
}

static void destroyPhysiNode(SPhysiNode* pNode) {
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pConditions);
  nodesDestroyNode(pNode->pOutputDataBlockDesc);
}

static void destroyWinodwPhysiNode(SWinodwPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pExprs);
  nodesDestroyList(pNode->pFuncs);
308
  nodesDestroyNode(pNode->pTspk);
X
Xiaoyu Wang 已提交
309 310 311 312 313 314 315
}

static void destroyScanPhysiNode(SScanPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pScanCols);
}

X
Xiaoyu Wang 已提交
316
static void destroyDataSinkNode(SDataSinkNode* pNode) { nodesDestroyNode(pNode->pInputDataBlockDesc); }
X
Xiaoyu Wang 已提交
317 318 319 320 321 322 323

void nodesDestroyNode(SNodeptr pNode) {
  if (NULL == pNode) {
    return;
  }

  switch (nodeType(pNode)) {
X
Xiaoyu Wang 已提交
324
    case QUERY_NODE_COLUMN:  // pProjectRef is weak reference, no need to release
X
Xiaoyu Wang 已提交
325
      break;
D
dapan1121 已提交
326
    case QUERY_NODE_VALUE: {
X
Xiaoyu Wang 已提交
327
      SValueNode* pValue = (SValueNode*)pNode;
wafwerar's avatar
wafwerar 已提交
328
      taosMemoryFreeClear(pValue->literal);
D
dapan1121 已提交
329
      if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
wafwerar's avatar
wafwerar 已提交
330
        taosMemoryFreeClear(pValue->datum.p);
D
dapan1121 已提交
331
      }
X
Xiaoyu Wang 已提交
332 333 334 335 336 337
      break;
    }
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOp = (SOperatorNode*)pNode;
      nodesDestroyNode(pOp->pLeft);
      nodesDestroyNode(pOp->pRight);
D
dapan1121 已提交
338 339 340
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
341
      nodesDestroyList(((SLogicConditionNode*)pNode)->pParameterList);
D
dapan1121 已提交
342 343
      break;
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
344
      nodesDestroyList(((SFunctionNode*)pNode)->pParameterList);
X
Xiaoyu Wang 已提交
345 346
      break;
    case QUERY_NODE_REAL_TABLE: {
X
Xiaoyu Wang 已提交
347
      SRealTableNode* pReal = (SRealTableNode*)pNode;
wafwerar's avatar
wafwerar 已提交
348 349
      taosMemoryFreeClear(pReal->pMeta);
      taosMemoryFreeClear(pReal->pVgroupList);
X
Xiaoyu Wang 已提交
350 351 352
      break;
    }
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
353
      nodesDestroyNode(((STempTableNode*)pNode)->pSubquery);
D
dapan1121 已提交
354
      break;
X
Xiaoyu Wang 已提交
355 356 357 358 359
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoin = (SJoinTableNode*)pNode;
      nodesDestroyNode(pJoin->pLeft);
      nodesDestroyNode(pJoin->pRight);
      nodesDestroyNode(pJoin->pOnCond);
D
dapan1121 已提交
360
      break;
X
Xiaoyu Wang 已提交
361
    }
D
dapan1121 已提交
362
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
363 364 365 366 367
      nodesDestroyList(((SGroupingSetNode*)pNode)->pParameterList);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      nodesDestroyNode(((SOrderByExprNode*)pNode)->pExpr);
      break;
X
Xiaoyu Wang 已提交
368
    case QUERY_NODE_LIMIT:  // no pointer field
D
dapan1121 已提交
369
      break;
X
Xiaoyu Wang 已提交
370
    case QUERY_NODE_STATE_WINDOW:
371
      nodesDestroyNode(((SStateWindowNode*)pNode)->pExpr);
X
Xiaoyu Wang 已提交
372 373 374 375 376
      break;
    case QUERY_NODE_SESSION_WINDOW: {
      SSessionWindowNode* pSession = (SSessionWindowNode*)pNode;
      nodesDestroyNode(pSession->pCol);
      nodesDestroyNode(pSession->pGap);
D
dapan1121 已提交
377
      break;
X
Xiaoyu Wang 已提交
378 379 380 381 382 383 384 385 386 387
    }
    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 已提交
388
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
389 390
      nodesDestroyList(((SNodeListNode*)pNode)->pNodeList);
      break;
X
Xiaoyu Wang 已提交
391 392 393 394
    case QUERY_NODE_FILL: {
      SFillNode* pFill = (SFillNode*)pNode;
      nodesDestroyNode(pFill->pValues);
      nodesDestroyNode(pFill->pWStartTs);
X
Xiaoyu Wang 已提交
395
      break;
X
Xiaoyu Wang 已提交
396
    }
X
Xiaoyu Wang 已提交
397 398
    case QUERY_NODE_RAW_EXPR:
      nodesDestroyNode(((SRawExprNode*)pNode)->pNode);
X
Xiaoyu Wang 已提交
399
      break;
X
Xiaoyu Wang 已提交
400 401 402 403 404 405
    case QUERY_NODE_TARGET:
      nodesDestroyNode(((STargetNode*)pNode)->pExpr);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      nodesDestroyList(((SDataBlockDescNode*)pNode)->pSlots);
      break;
X
Xiaoyu Wang 已提交
406 407 408
    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 已提交
409
      break;
X
Xiaoyu Wang 已提交
410 411 412 413 414
    case QUERY_NODE_DATABASE_OPTIONS: {
      SDatabaseOptions* pOptions = (SDatabaseOptions*)pNode;
      nodesDestroyNode(pOptions->pDaysPerFile);
      nodesDestroyList(pOptions->pKeep);
      nodesDestroyList(pOptions->pRetentions);
X
Xiaoyu Wang 已提交
415
      break;
X
Xiaoyu Wang 已提交
416
    }
X
Xiaoyu Wang 已提交
417
    case QUERY_NODE_TABLE_OPTIONS: {
X
Xiaoyu Wang 已提交
418 419 420
      STableOptions* pOptions = (STableOptions*)pNode;
      nodesDestroyList(pOptions->pSma);
      nodesDestroyList(pOptions->pRollupFuncs);
X
Xiaoyu Wang 已提交
421
      break;
X
Xiaoyu Wang 已提交
422
    }
423
    case QUERY_NODE_INDEX_OPTIONS: {
X
Xiaoyu Wang 已提交
424 425 426 427 428
      SIndexOptions* pOptions = (SIndexOptions*)pNode;
      nodesDestroyList(pOptions->pFuncs);
      nodesDestroyNode(pOptions->pInterval);
      nodesDestroyNode(pOptions->pOffset);
      nodesDestroyNode(pOptions->pSliding);
429 430
      break;
    }
X
Xiaoyu Wang 已提交
431 432 433 434 435 436 437 438
    case QUERY_NODE_SET_OPERATOR: {
      SSetOperator* pStmt = (SSetOperator*)pNode;
      nodesDestroyNode(pStmt->pLeft);
      nodesDestroyNode(pStmt->pRight);
      nodesDestroyList(pStmt->pOrderByList);
      nodesDestroyNode(pStmt->pLimit);
      break;
    }
X
Xiaoyu Wang 已提交
439
    case QUERY_NODE_SELECT_STMT: {
X
Xiaoyu Wang 已提交
440
      SSelectStmt* pStmt = (SSelectStmt*)pNode;
X
Xiaoyu Wang 已提交
441 442 443 444 445 446 447 448 449 450 451 452
      nodesDestroyList(pStmt->pProjectionList);
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyList(pStmt->pPartitionByList);
      nodesDestroyNode(pStmt->pWindow);
      nodesDestroyList(pStmt->pGroupByList);
      nodesDestroyNode(pStmt->pHaving);
      nodesDestroyList(pStmt->pOrderByList);
      nodesDestroyNode(pStmt->pLimit);
      nodesDestroyNode(pStmt->pSlimit);
      break;
    }
X
Xiaoyu Wang 已提交
453 454 455 456 457 458
    case QUERY_NODE_VNODE_MODIF_STMT:
      destroyVgDataBlockArray(((SVnodeModifOpStmt*)pNode)->pDataBlocks);
      break;
    case QUERY_NODE_CREATE_DATABASE_STMT:
      nodesDestroyNode(((SCreateDatabaseStmt*)pNode)->pOptions);
      break;
X
Xiaoyu Wang 已提交
459
    case QUERY_NODE_DROP_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
460 461 462
      break;
    case QUERY_NODE_ALTER_DATABASE_STMT:
      nodesDestroyNode(((SAlterDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
463
      break;
X
Xiaoyu Wang 已提交
464
    case QUERY_NODE_CREATE_TABLE_STMT: {
X
Xiaoyu Wang 已提交
465
      SCreateTableStmt* pStmt = (SCreateTableStmt*)pNode;
X
Xiaoyu Wang 已提交
466 467
      nodesDestroyList(pStmt->pCols);
      nodesDestroyList(pStmt->pTags);
X
Xiaoyu Wang 已提交
468
      nodesDestroyNode(pStmt->pOptions);
X
Xiaoyu Wang 已提交
469 470 471
      break;
    }
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE: {
X
Xiaoyu Wang 已提交
472
      SCreateSubTableClause* pStmt = (SCreateSubTableClause*)pNode;
X
Xiaoyu Wang 已提交
473 474 475 476 477
      nodesDestroyList(pStmt->pSpecificTags);
      nodesDestroyList(pStmt->pValsOfTags);
      break;
    }
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
X
Xiaoyu Wang 已提交
478 479
      nodesDestroyList(((SCreateMultiTableStmt*)pNode)->pSubTables);
      break;
X
Xiaoyu Wang 已提交
480
    case QUERY_NODE_DROP_TABLE_CLAUSE:  // no pointer field
X
Xiaoyu Wang 已提交
481 482 483 484
      break;
    case QUERY_NODE_DROP_TABLE_STMT:
      nodesDestroyNode(((SDropTableStmt*)pNode)->pTables);
      break;
X
Xiaoyu Wang 已提交
485
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
486 487 488 489 490 491 492
      break;
    case QUERY_NODE_ALTER_TABLE_STMT: {
      SAlterTableStmt* pStmt = (SAlterTableStmt*)pNode;
      nodesDestroyNode(pStmt->pOptions);
      nodesDestroyNode(pStmt->pVal);
      break;
    }
X
Xiaoyu Wang 已提交
493 494 495 496 497 498 499
    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
500
      break;
501
    case QUERY_NODE_CREATE_INDEX_STMT: {
X
Xiaoyu Wang 已提交
502
      SCreateIndexStmt* pStmt = (SCreateIndexStmt*)pNode;
503 504 505 506
      nodesDestroyNode(pStmt->pOptions);
      nodesDestroyList(pStmt->pCols);
      break;
    }
X
Xiaoyu Wang 已提交
507 508 509
    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
X
Xiaoyu Wang 已提交
510 511 512 513
      break;
    case QUERY_NODE_CREATE_TOPIC_STMT:
      nodesDestroyNode(((SCreateTopicStmt*)pNode)->pQuery);
      break;
X
Xiaoyu Wang 已提交
514 515
    case QUERY_NODE_DROP_TOPIC_STMT:   // no pointer field
    case QUERY_NODE_ALTER_LOCAL_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
      break;
    case QUERY_NODE_SHOW_DATABASES_STMT:
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_STABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
    case QUERY_NODE_SHOW_DNODES_STMT:
    case QUERY_NODE_SHOW_VGROUPS_STMT:
    case QUERY_NODE_SHOW_MNODES_STMT:
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
    case QUERY_NODE_SHOW_STREAMS_STMT: {
      SShowStmt* pStmt = (SShowStmt*)pNode;
      nodesDestroyNode(pStmt->pDbName);
      nodesDestroyNode(pStmt->pTbNamePattern);
      break;
    }
534 535 536 537 538 539 540 541 542 543 544 545
    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);
      break;
    }
X
Xiaoyu Wang 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    case QUERY_NODE_LOGIC_PLAN_SCAN: {
      SScanLogicNode* pLogicNode = (SScanLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pScanCols);
      taosMemoryFreeClear(pLogicNode->pMeta);
      taosMemoryFreeClear(pLogicNode->pVgroupList);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_JOIN: {
      SJoinLogicNode* pLogicNode = (SJoinLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyNode(pLogicNode->pOnConditions);
      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;
    }
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIF: {
      SVnodeModifLogicNode* pLogicNode = (SVnodeModifLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      destroyVgDataBlockArray(pLogicNode->pDataBlocks);
X
bugfix  
Xiaoyu Wang 已提交
577
      // pVgDataBlocks is weak reference
X
Xiaoyu Wang 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      destroyLogicNode((SLogicNode*)pNode);
      break;
    case QUERY_NODE_LOGIC_PLAN_WINDOW: {
      SWindowLogicNode* pLogicNode = (SWindowLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
      nodesDestroyNode(pLogicNode->pTspk);
      break;
    }
    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);
      break;
    }
    case QUERY_NODE_LOGIC_SUBPLAN: {
      SLogicSubplan* pSubplan = (SLogicSubplan*)pNode;
      nodesDestroyList(pSubplan->pChildren);
      nodesDestroyNode(pSubplan->pNode);
X
bugfix  
Xiaoyu Wang 已提交
606
      nodesClearList(pSubplan->pParents);
X
Xiaoyu Wang 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
      taosMemoryFreeClear(pSubplan->pVgroupList);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN:
      nodesDestroyList(((SQueryLogicPlan*)pNode)->pTopSubplans);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT: {
      SProjectPhysiNode* pPhyNode = (SProjectPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pProjections);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_JOIN: {
      SJoinPhysiNode* pPhyNode = (SJoinPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pOnConditions);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_AGG: {
      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;
    }
    case QUERY_NODE_PHYSICAL_PLAN_SORT: {
      SSortPhysiNode* pPhyNode = (SSortPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pExprs);
      nodesDestroyNode(pPhyNode->pSortKeys);
      break;
    }
X
Xiaoyu Wang 已提交
662
    case QUERY_NODE_PHYSICAL_PLAN_INTERVAL:
X
Xiaoyu Wang 已提交
663
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
X
Xiaoyu Wang 已提交
664
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
X
Xiaoyu Wang 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
      break;
    case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW:
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
      break;
    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;
    }
    case QUERY_NODE_PHYSICAL_SUBPLAN: {
      SSubplan* pSubplan = (SSubplan*)pNode;
      nodesDestroyList(pSubplan->pChildren);
      nodesDestroyNode(pSubplan->pNode);
      nodesDestroyNode(pSubplan->pDataSink);
X
bugfix  
Xiaoyu Wang 已提交
683 684 685 686 687 688 689
      nodesClearList(pSubplan->pParents);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN: {
      SQueryPlan* pPlan = (SQueryPlan*)pNode;
      if (NULL != pPlan->pSubplans) {
        // only need to destroy the top-level subplans, because they will recurse to all the subplans below
X
Xiaoyu Wang 已提交
690
        bool   first = true;
X
bugfix  
Xiaoyu Wang 已提交
691 692 693 694 695 696 697 698 699 700 701 702
        SNode* pElement = NULL;
        FOREACH(pElement, pPlan->pSubplans) {
          if (first) {
            first = false;
            nodesDestroyNode(pElement);
          } else {
            nodesClearList(((SNodeListNode*)pElement)->pNodeList);
            taosMemoryFreeClear(pElement);
          }
        }
        nodesClearList(pPlan->pSubplans);
      }
X
Xiaoyu Wang 已提交
703 704
      break;
    }
705 706 707
    default:
      break;
  }
X
Xiaoyu Wang 已提交
708 709
  taosMemoryFreeClear(pNode);
  return;
710 711
}

712
SNodeList* nodesMakeList() {
wafwerar's avatar
wafwerar 已提交
713
  SNodeList* p = taosMemoryCalloc(1, sizeof(SNodeList));
714 715 716 717 718 719
  if (NULL == p) {
    return NULL;
  }
  return p;
}

X
Xiaoyu Wang 已提交
720
int32_t nodesListAppend(SNodeList* pList, SNodeptr pNode) {
721
  if (NULL == pList || NULL == pNode) {
X
Xiaoyu Wang 已提交
722
    return TSDB_CODE_FAILED;
723
  }
wafwerar's avatar
wafwerar 已提交
724
  SListCell* p = taosMemoryCalloc(1, sizeof(SListCell));
725
  if (NULL == p) {
X
Xiaoyu Wang 已提交
726 727
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_OUT_OF_MEMORY;
728 729
  }
  p->pNode = pNode;
730 731 732 733 734 735
  if (NULL == pList->pHead) {
    pList->pHead = p;
  }
  if (NULL != pList->pTail) {
    pList->pTail->pNext = p;
  }
736
  p->pPrev = pList->pTail;
737
  pList->pTail = p;
738
  ++(pList->length);
X
Xiaoyu Wang 已提交
739
  return TSDB_CODE_SUCCESS;
740 741
}

742 743
int32_t nodesListStrictAppend(SNodeList* pList, SNodeptr pNode) {
  if (NULL == pNode) {
X
Xiaoyu Wang 已提交
744
    terrno = TSDB_CODE_OUT_OF_MEMORY;
745 746 747 748 749 750 751 752 753
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t code = nodesListAppend(pList, pNode);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyNode(pNode);
  }
  return code;
}

X
Xiaoyu Wang 已提交
754 755 756 757 758 759 760 761 762 763 764
int32_t nodesListMakeAppend(SNodeList** pList, SNodeptr pNode) {
  if (NULL == *pList) {
    *pList = nodesMakeList();
    if (NULL == *pList) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return nodesListAppend(*pList, pNode);
}

S
Shengliang Guan 已提交
765 766 767 768 769 770 771 772 773 774 775
int32_t nodesListMakeStrictAppend(SNodeList** pList, SNodeptr pNode) {
  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 已提交
776
int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) {
X
Xiaoyu Wang 已提交
777
  if (NULL == pTarget || NULL == pSrc) {
X
Xiaoyu Wang 已提交
778
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
779 780 781 782 783 784 785 786 787 788
  }

  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 已提交
789 790
  pTarget->pTail = pSrc->pTail;
  pTarget->length += pSrc->length;
wafwerar's avatar
wafwerar 已提交
791
  taosMemoryFreeClear(pSrc);
X
Xiaoyu Wang 已提交
792

X
Xiaoyu Wang 已提交
793 794 795
  return TSDB_CODE_SUCCESS;
}

796 797 798 799 800 801 802 803 804 805 806
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;
}

X
Xiaoyu Wang 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
int32_t nodesListPushFront(SNodeList* pList, SNodeptr pNode) {
  if (NULL == pList || NULL == pNode) {
    return TSDB_CODE_FAILED;
  }
  SListCell* p = taosMemoryCalloc(1, sizeof(SListCell));
  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;
}

826 827 828 829 830
SListCell* nodesListErase(SNodeList* pList, SListCell* pCell) {
  if (NULL == pCell->pPrev) {
    pList->pHead = pCell->pNext;
  } else {
    pCell->pPrev->pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
831 832 833 834
  }
  if (NULL == pCell->pNext) {
    pList->pTail = pCell->pPrev;
  } else {
835 836 837
    pCell->pNext->pPrev = pCell->pPrev;
  }
  SListCell* pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
838
  nodesDestroyNode(pCell->pNode);
wafwerar's avatar
wafwerar 已提交
839
  taosMemoryFreeClear(pCell);
840 841 842 843
  --(pList->length);
  return pNext;
}

X
Xiaoyu Wang 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
void nodesListInsertList(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc) {
  if (NULL == pTarget || NULL == pPos || NULL == pSrc) {
    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;
  taosMemoryFreeClear(pSrc);
}

X
Xiaoyu Wang 已提交
862
SNodeptr nodesListGetNode(SNodeList* pList, int32_t index) {
863 864 865 866 867 868 869 870 871
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return node;
    }
  }
  return NULL;
}

872
void nodesDestroyList(SNodeList* pList) {
873 874 875
  if (NULL == pList) {
    return;
  }
X
Xiaoyu Wang 已提交
876

D
dapan1121 已提交
877 878 879
  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    pNext = nodesListErase(pList, pNext);
880
  }
wafwerar's avatar
wafwerar 已提交
881
  taosMemoryFreeClear(pList);
882 883
}

X
Xiaoyu Wang 已提交
884 885 886 887 888 889 890 891 892
void nodesClearList(SNodeList* pList) {
  if (NULL == pList) {
    return;
  }

  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    SListCell* tmp = pNext;
    pNext = pNext->pNext;
wafwerar's avatar
wafwerar 已提交
893
    taosMemoryFreeClear(tmp);
X
Xiaoyu Wang 已提交
894
  }
wafwerar's avatar
wafwerar 已提交
895
  taosMemoryFreeClear(pList);
X
Xiaoyu Wang 已提交
896 897
}

X
Xiaoyu Wang 已提交
898
void* nodesGetValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
899 900 901 902 903 904 905 906 907 908 909 910
  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:
911
    case TSDB_DATA_TYPE_DOUBLE:
D
dapan1121 已提交
912
      return (void*)&pNode->typeData;
D
dapan1121 已提交
913 914
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
915
    case TSDB_DATA_TYPE_VARBINARY:
X
Xiaoyu Wang 已提交
916
      return (void*)pNode->datum.p;
D
dapan1121 已提交
917 918 919 920 921 922 923
    default:
      break;
  }

  return NULL;
}

X
Xiaoyu Wang 已提交
924
int32_t nodesSetValueNodeValue(SValueNode* pNode, void* value) {
D
dapan1121 已提交
925 926 927
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
      pNode->datum.b = *(bool*)value;
D
dapan1121 已提交
928
      *(bool*)&pNode->typeData = pNode->datum.b;
D
dapan1121 已提交
929 930 931
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pNode->datum.i = *(int8_t*)value;
D
dapan1121 已提交
932
      *(int8_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
933 934 935
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pNode->datum.i = *(int16_t*)value;
D
dapan1121 已提交
936
      *(int16_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
937 938 939
      break;
    case TSDB_DATA_TYPE_INT:
      pNode->datum.i = *(int32_t*)value;
D
dapan1121 已提交
940
      *(int32_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
941 942 943
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
944
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
945 946 947
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
948
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
949 950 951
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pNode->datum.u = *(int8_t*)value;
D
dapan1121 已提交
952
      *(int8_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
953 954 955
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pNode->datum.u = *(int16_t*)value;
D
dapan1121 已提交
956
      *(int16_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
957 958 959
      break;
    case TSDB_DATA_TYPE_UINT:
      pNode->datum.u = *(int32_t*)value;
D
dapan1121 已提交
960
      *(int32_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
961 962 963
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pNode->datum.u = *(uint64_t*)value;
D
dapan1121 已提交
964
      *(uint64_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
965 966 967
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pNode->datum.d = *(float*)value;
D
dapan1121 已提交
968
      *(float*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
969 970 971
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pNode->datum.d = *(double*)value;
D
dapan1121 已提交
972
      *(double*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
      pNode->datum.p = (char*)value;
      break;
    default:
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
986
char* nodesGetStrValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
987
  switch (pNode->node.resType.type) {
D
dapan1121 已提交
988
    case TSDB_DATA_TYPE_BOOL: {
X
Xiaoyu Wang 已提交
989
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
990 991 992
      if (NULL == buf) {
        return NULL;
      }
993

D
dapan1121 已提交
994 995
      sprintf(buf, "%s", pNode->datum.b ? "true" : "false");
      return buf;
D
dapan1121 已提交
996
    }
D
dapan1121 已提交
997 998 999 1000 1001
    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 已提交
1002
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1003 1004 1005
      if (NULL == buf) {
        return NULL;
      }
1006

D
dapan1121 已提交
1007 1008 1009 1010 1011 1012 1013
      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 已提交
1014
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1015 1016 1017
      if (NULL == buf) {
        return NULL;
      }
1018

D
dapan1121 已提交
1019 1020 1021 1022 1023
      sprintf(buf, "%" PRIu64, pNode->datum.u);
      return buf;
    }
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE: {
X
Xiaoyu Wang 已提交
1024
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1025 1026 1027
      if (NULL == buf) {
        return NULL;
      }
1028

D
dapan1121 已提交
1029 1030 1031 1032 1033 1034
      sprintf(buf, "%e", pNode->datum.d);
      return buf;
    }
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY: {
D
dapan1121 已提交
1035
      int32_t bufSize = varDataLen(pNode->datum.p) + 2 + 1;
X
Xiaoyu Wang 已提交
1036
      void*   buf = taosMemoryMalloc(bufSize);
D
dapan1121 已提交
1037 1038 1039
      if (NULL == buf) {
        return NULL;
      }
1040

D
dapan1121 已提交
1041
      snprintf(buf, bufSize, "'%s'", varDataVal(pNode->datum.p));
D
dapan1121 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050
      return buf;
    }
    default:
      break;
  }

  return NULL;
}

1051 1052
bool nodesIsExprNode(const SNode* pNode) {
  ENodeType type = nodeType(pNode);
X
Xiaoyu Wang 已提交
1053 1054
  return (QUERY_NODE_COLUMN == type || QUERY_NODE_VALUE == type || QUERY_NODE_OPERATOR == type ||
          QUERY_NODE_FUNCTION == type);
1055 1056
}

X
Xiaoyu Wang 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
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;
}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
bool nodesIsArithmeticOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_ADD:
    case OP_TYPE_SUB:
    case OP_TYPE_MULTI:
    case OP_TYPE_DIV:
    case OP_TYPE_MOD:
      return true;
    default:
      break;
  }
  return false;
}
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
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:
1103
    case OP_TYPE_JSON_CONTAINS:
X
Xiaoyu Wang 已提交
1104 1105 1106 1107 1108 1109 1110 1111
    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:
1112 1113 1114 1115 1116
      return true;
    default:
      break;
  }
  return false;
1117 1118
}

1119 1120 1121
bool nodesIsJsonOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_JSON_GET_VALUE:
wmmhello's avatar
wmmhello 已提交
1122
    case OP_TYPE_JSON_CONTAINS:
1123 1124 1125 1126 1127 1128
      return true;
    default:
      break;
  }
  return false;
}
1129

X
Xiaoyu Wang 已提交
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
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 已提交
1143
typedef struct SCollectColumnsCxt {
1144 1145 1146 1147 1148
  int32_t         errCode;
  const char*     pTableAlias;
  ECollectColType collectType;
  SNodeList*      pCols;
  SHashObj*       pColHash;
X
Xiaoyu Wang 已提交
1149 1150
} SCollectColumnsCxt;

1151
static EDealRes doCollect(SCollectColumnsCxt* pCxt, SColumnNode* pCol, SNode* pNode) {
X
Xiaoyu Wang 已提交
1152
  char    name[TSDB_TABLE_NAME_LEN + TSDB_COL_NAME_LEN];
1153 1154 1155 1156 1157 1158 1159
  int32_t len = 0;
  if ('\0' == pCol->tableAlias[0]) {
    len = sprintf(name, "%s", pCol->colName);
  }
  len = sprintf(name, "%s.%s", pCol->tableAlias, pCol->colName);
  if (NULL == taosHashGet(pCxt->pColHash, name, len)) {
    pCxt->errCode = taosHashPut(pCxt->pColHash, name, len, NULL, 0);
X
Xiaoyu Wang 已提交
1160
    if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1161
      pCxt->errCode = nodesListStrictAppend(pCxt->pCols, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1162 1163 1164 1165 1166 1167
    }
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

1168 1169 1170 1171 1172 1173
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 已提交
1174 1175
static EDealRes collectColumns(SNode* pNode, void* pContext) {
  SCollectColumnsCxt* pCxt = (SCollectColumnsCxt*)pContext;
X
Xiaoyu Wang 已提交
1176
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
X
Xiaoyu Wang 已提交
1177
    SColumnNode* pCol = (SColumnNode*)pNode;
1178 1179
    if (isCollectType(pCxt->collectType, pCol->colType) &&
        (NULL == pCxt->pTableAlias || 0 == strcmp(pCxt->pTableAlias, pCol->tableAlias))) {
1180
      return doCollect(pCxt, pCol, pNode);
X
Xiaoyu Wang 已提交
1181 1182 1183 1184 1185
    }
  }
  return DEAL_RES_CONTINUE;
}

1186 1187
int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, ECollectColType type,
                            SNodeList** pCols) {
X
Xiaoyu Wang 已提交
1188
  if (NULL == pSelect || NULL == pCols) {
1189
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1190 1191 1192
  }

  SCollectColumnsCxt cxt = {
X
Xiaoyu Wang 已提交
1193 1194
      .errCode = TSDB_CODE_SUCCESS,
      .pTableAlias = pTableAlias,
1195
      .collectType = type,
1196
      .pCols = (NULL == *pCols ? nodesMakeList() : *pCols),
X
Xiaoyu Wang 已提交
1197
      .pColHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK)};
1198
  if (NULL == cxt.pCols || NULL == cxt.pColHash) {
X
Xiaoyu Wang 已提交
1199 1200
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1201
  *pCols = NULL;
X
Xiaoyu Wang 已提交
1202
  nodesWalkSelectStmt(pSelect, clause, collectColumns, &cxt);
1203
  taosHashCleanup(cxt.pColHash);
X
Xiaoyu Wang 已提交
1204
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
1205
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1206 1207
    return cxt.errCode;
  }
1208 1209 1210
  if (LIST_LENGTH(cxt.pCols) > 0) {
    *pCols = cxt.pCols;
  } else {
1211
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1212
  }
1213

X
Xiaoyu Wang 已提交
1214 1215 1216 1217
  return TSDB_CODE_SUCCESS;
}

typedef struct SCollectFuncsCxt {
X
Xiaoyu Wang 已提交
1218
  int32_t         errCode;
X
Xiaoyu Wang 已提交
1219
  FFuncClassifier classifier;
X
Xiaoyu Wang 已提交
1220
  SNodeList*      pFuncs;
X
Xiaoyu Wang 已提交
1221 1222 1223 1224 1225
} SCollectFuncsCxt;

static EDealRes collectFuncs(SNode* pNode, void* pContext) {
  SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext;
  if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId)) {
1226
    pCxt->errCode = nodesListStrictAppend(pCxt->pFuncs, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1227 1228 1229 1230 1231
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1232
int32_t nodesCollectFuncs(SSelectStmt* pSelect, ESqlClause clause, FFuncClassifier classifier, SNodeList** pFuncs) {
X
Xiaoyu Wang 已提交
1233
  if (NULL == pSelect || NULL == pFuncs) {
1234
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1235 1236
  }

1237 1238
  SCollectFuncsCxt cxt = {
      .errCode = TSDB_CODE_SUCCESS, .classifier = classifier, .pFuncs = (NULL == *pFuncs ? nodesMakeList() : *pFuncs)};
X
Xiaoyu Wang 已提交
1239 1240 1241
  if (NULL == cxt.pFuncs) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1242
  *pFuncs = NULL;
X
Xiaoyu Wang 已提交
1243
  nodesWalkSelectStmt(pSelect, clause, collectFuncs, &cxt);
X
Xiaoyu Wang 已提交
1244 1245 1246 1247
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
    nodesDestroyList(cxt.pFuncs);
    return cxt.errCode;
  }
X
Xiaoyu Wang 已提交
1248 1249 1250 1251 1252
  if (LIST_LENGTH(cxt.pFuncs) > 0) {
    *pFuncs = cxt.pFuncs;
  } else {
    nodesDestroyList(cxt.pFuncs);
  }
X
Xiaoyu Wang 已提交
1253

1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
  return TSDB_CODE_SUCCESS;
}

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 已提交
1294 1295
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1296

X
Xiaoyu Wang 已提交
1297
char* getFillModeString(EFillMode mode) {
D
dapan1121 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
  switch (mode) {
    case FILL_MODE_NONE:
      return "none";
    case FILL_MODE_VALUE:
      return "value";
    case FILL_MODE_PREV:
      return "prev";
    case FILL_MODE_NULL:
      return "null";
    case FILL_MODE_LINEAR:
      return "linear";
    case FILL_MODE_NEXT:
      return "next";
    default:
      return "unknown";
  }
}

X
Xiaoyu Wang 已提交
1316
char* nodesGetNameFromColumnNode(SNode* pNode) {
D
dapan1121 已提交
1317 1318 1319
  if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) {
    return "NULL";
  }
X
Xiaoyu Wang 已提交
1320 1321

  return ((SColumnNode*)pNode)->colName;
D
dapan1121 已提交
1322 1323
}

D
dapan1121 已提交
1324 1325 1326 1327 1328
int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) {
  if (NULL == pSlots || pSlots->length <= 0) {
    return 0;
  }

X
Xiaoyu Wang 已提交
1329
  SNode*  pNode = NULL;
D
dapan1121 已提交
1330 1331 1332 1333 1334 1335
  int32_t num = 0;
  FOREACH(pNode, pSlots) {
    if (QUERY_NODE_SLOT_DESC != pNode->type) {
      continue;
    }

X
Xiaoyu Wang 已提交
1336
    SSlotDescNode* descNode = (SSlotDescNode*)pNode;
D
dapan1121 已提交
1337 1338 1339 1340 1341 1342 1343 1344
    if (descNode->output) {
      ++num;
    }
  }

  return num;
}

D
dapan1121 已提交
1345 1346 1347 1348 1349
void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal) {
  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 已提交
1350
      break;
D
dapan1121 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
    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:
    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
dapan1121 已提交
1374 1375 1376
      pVal->pz = taosMemoryMalloc(pVal->nLen + VARSTR_HEADER_SIZE + 1);
      memcpy(pVal->pz, pNode->datum.p, pVal->nLen + VARSTR_HEADER_SIZE);
      pVal->pz[pVal->nLen + VARSTR_HEADER_SIZE] = 0;
D
dapan1121 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385
      break;
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }
}