nodesUtilFuncs.c 66.5 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

X
Xiaoyu Wang 已提交
25 26 27 28 29 30 31 32 33
static SNode* makeNode(ENodeType type, size_t size) {
  SNode* p = taosMemoryCalloc(1, size);
  if (NULL == p) {
    return NULL;
  }
  setNodeType(p, type);
  return p;
}

34
SNode* nodesMakeNode(ENodeType type) {
35 36
  switch (type) {
    case QUERY_NODE_COLUMN:
X
Xiaoyu Wang 已提交
37
      return makeNode(type, sizeof(SColumnNode));
38
    case QUERY_NODE_VALUE:
X
Xiaoyu Wang 已提交
39
      return makeNode(type, sizeof(SValueNode));
40
    case QUERY_NODE_OPERATOR:
X
Xiaoyu Wang 已提交
41
      return makeNode(type, sizeof(SOperatorNode));
42
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
43
      return makeNode(type, sizeof(SLogicConditionNode));
44
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
45
      return makeNode(type, sizeof(SFunctionNode));
46
    case QUERY_NODE_REAL_TABLE:
X
Xiaoyu Wang 已提交
47
      return makeNode(type, sizeof(SRealTableNode));
48
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
49
      return makeNode(type, sizeof(STempTableNode));
50
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
51
      return makeNode(type, sizeof(SJoinTableNode));
52
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
53
      return makeNode(type, sizeof(SGroupingSetNode));
54
    case QUERY_NODE_ORDER_BY_EXPR:
X
Xiaoyu Wang 已提交
55
      return makeNode(type, sizeof(SOrderByExprNode));
56
    case QUERY_NODE_LIMIT:
X
Xiaoyu Wang 已提交
57
      return makeNode(type, sizeof(SLimitNode));
58
    case QUERY_NODE_STATE_WINDOW:
X
Xiaoyu Wang 已提交
59
      return makeNode(type, sizeof(SStateWindowNode));
60
    case QUERY_NODE_SESSION_WINDOW:
X
Xiaoyu Wang 已提交
61
      return makeNode(type, sizeof(SSessionWindowNode));
62
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
63
      return makeNode(type, sizeof(SIntervalWindowNode));
64
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
65
      return makeNode(type, sizeof(SNodeListNode));
66
    case QUERY_NODE_FILL:
X
Xiaoyu Wang 已提交
67
      return makeNode(type, sizeof(SFillNode));
68
    case QUERY_NODE_RAW_EXPR:
X
Xiaoyu Wang 已提交
69
      return makeNode(type, sizeof(SRawExprNode));
70
    case QUERY_NODE_TARGET:
X
Xiaoyu Wang 已提交
71
      return makeNode(type, sizeof(STargetNode));
72
    case QUERY_NODE_DATABLOCK_DESC:
X
Xiaoyu Wang 已提交
73
      return makeNode(type, sizeof(SDataBlockDescNode));
74
    case QUERY_NODE_SLOT_DESC:
X
Xiaoyu Wang 已提交
75
      return makeNode(type, sizeof(SSlotDescNode));
76
    case QUERY_NODE_COLUMN_DEF:
X
Xiaoyu Wang 已提交
77
      return makeNode(type, sizeof(SColumnDefNode));
X
Xiaoyu Wang 已提交
78
    case QUERY_NODE_DOWNSTREAM_SOURCE:
X
Xiaoyu Wang 已提交
79
      return makeNode(type, sizeof(SDownstreamSourceNode));
X
Xiaoyu Wang 已提交
80
    case QUERY_NODE_DATABASE_OPTIONS:
X
Xiaoyu Wang 已提交
81
      return makeNode(type, sizeof(SDatabaseOptions));
X
Xiaoyu Wang 已提交
82
    case QUERY_NODE_TABLE_OPTIONS:
X
Xiaoyu Wang 已提交
83
      return makeNode(type, sizeof(STableOptions));
X
Xiaoyu Wang 已提交
84
    case QUERY_NODE_INDEX_OPTIONS:
X
Xiaoyu Wang 已提交
85
      return makeNode(type, sizeof(SIndexOptions));
86
    case QUERY_NODE_EXPLAIN_OPTIONS:
X
Xiaoyu Wang 已提交
87
      return makeNode(type, sizeof(SExplainOptions));
88
    case QUERY_NODE_STREAM_OPTIONS:
X
Xiaoyu Wang 已提交
89
      return makeNode(type, sizeof(SStreamOptions));
90
    case QUERY_NODE_LEFT_VALUE:
X
Xiaoyu Wang 已提交
91
      return makeNode(type, sizeof(SLeftValueNode));
92 93
    case QUERY_NODE_COLUMN_REF:
      return makeNode(type, sizeof(SColumnDefNode));
94
    case QUERY_NODE_SET_OPERATOR:
X
Xiaoyu Wang 已提交
95
      return makeNode(type, sizeof(SSetOperator));
96
    case QUERY_NODE_SELECT_STMT:
X
Xiaoyu Wang 已提交
97
      return makeNode(type, sizeof(SSelectStmt));
98
    case QUERY_NODE_VNODE_MODIF_STMT:
X
Xiaoyu Wang 已提交
99
      return makeNode(type, sizeof(SVnodeModifOpStmt));
100
    case QUERY_NODE_CREATE_DATABASE_STMT:
X
Xiaoyu Wang 已提交
101
      return makeNode(type, sizeof(SCreateDatabaseStmt));
102
    case QUERY_NODE_DROP_DATABASE_STMT:
X
Xiaoyu Wang 已提交
103
      return makeNode(type, sizeof(SDropDatabaseStmt));
104
    case QUERY_NODE_ALTER_DATABASE_STMT:
X
Xiaoyu Wang 已提交
105
      return makeNode(type, sizeof(SAlterDatabaseStmt));
X
Xiaoyu Wang 已提交
106 107
    case QUERY_NODE_FLUSH_DATABASE_STMT:
      return makeNode(type, sizeof(SFlushDatabaseStmt));
X
Xiaoyu Wang 已提交
108 109
    case QUERY_NODE_TRIM_DATABASE_STMT:
      return makeNode(type, sizeof(STrimDatabaseStmt));
110
    case QUERY_NODE_CREATE_TABLE_STMT:
X
Xiaoyu Wang 已提交
111
      return makeNode(type, sizeof(SCreateTableStmt));
112
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE:
X
Xiaoyu Wang 已提交
113
      return makeNode(type, sizeof(SCreateSubTableClause));
114
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
X
Xiaoyu Wang 已提交
115
      return makeNode(type, sizeof(SCreateMultiTableStmt));
116
    case QUERY_NODE_DROP_TABLE_CLAUSE:
X
Xiaoyu Wang 已提交
117
      return makeNode(type, sizeof(SDropTableClause));
118
    case QUERY_NODE_DROP_TABLE_STMT:
X
Xiaoyu Wang 已提交
119
      return makeNode(type, sizeof(SDropTableStmt));
120
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:
X
Xiaoyu Wang 已提交
121
      return makeNode(type, sizeof(SDropSuperTableStmt));
122
    case QUERY_NODE_ALTER_TABLE_STMT:
X
Xiaoyu Wang 已提交
123
      return makeNode(type, sizeof(SAlterTableStmt));
124
    case QUERY_NODE_CREATE_USER_STMT:
X
Xiaoyu Wang 已提交
125
      return makeNode(type, sizeof(SCreateUserStmt));
126
    case QUERY_NODE_ALTER_USER_STMT:
X
Xiaoyu Wang 已提交
127
      return makeNode(type, sizeof(SAlterUserStmt));
128
    case QUERY_NODE_DROP_USER_STMT:
X
Xiaoyu Wang 已提交
129
      return makeNode(type, sizeof(SDropUserStmt));
130
    case QUERY_NODE_USE_DATABASE_STMT:
X
Xiaoyu Wang 已提交
131
      return makeNode(type, sizeof(SUseDatabaseStmt));
132
    case QUERY_NODE_CREATE_DNODE_STMT:
X
Xiaoyu Wang 已提交
133
      return makeNode(type, sizeof(SCreateDnodeStmt));
134
    case QUERY_NODE_DROP_DNODE_STMT:
X
Xiaoyu Wang 已提交
135
      return makeNode(type, sizeof(SDropDnodeStmt));
136
    case QUERY_NODE_ALTER_DNODE_STMT:
X
Xiaoyu Wang 已提交
137
      return makeNode(type, sizeof(SAlterDnodeStmt));
X
Xiaoyu Wang 已提交
138
    case QUERY_NODE_CREATE_INDEX_STMT:
X
Xiaoyu Wang 已提交
139
      return makeNode(type, sizeof(SCreateIndexStmt));
140
    case QUERY_NODE_DROP_INDEX_STMT:
X
Xiaoyu Wang 已提交
141
      return makeNode(type, sizeof(SDropIndexStmt));
X
Xiaoyu Wang 已提交
142
    case QUERY_NODE_CREATE_QNODE_STMT:
143 144 145
    case QUERY_NODE_CREATE_BNODE_STMT:
    case QUERY_NODE_CREATE_SNODE_STMT:
    case QUERY_NODE_CREATE_MNODE_STMT:
X
Xiaoyu Wang 已提交
146
      return makeNode(type, sizeof(SCreateComponentNodeStmt));
147
    case QUERY_NODE_DROP_QNODE_STMT:
148 149 150
    case QUERY_NODE_DROP_BNODE_STMT:
    case QUERY_NODE_DROP_SNODE_STMT:
    case QUERY_NODE_DROP_MNODE_STMT:
X
Xiaoyu Wang 已提交
151
      return makeNode(type, sizeof(SDropComponentNodeStmt));
152
    case QUERY_NODE_CREATE_TOPIC_STMT:
X
Xiaoyu Wang 已提交
153
      return makeNode(type, sizeof(SCreateTopicStmt));
154
    case QUERY_NODE_DROP_TOPIC_STMT:
X
Xiaoyu Wang 已提交
155 156 157
      return makeNode(type, sizeof(SDropTopicStmt));
    case QUERY_NODE_DROP_CGROUP_STMT:
      return makeNode(type, sizeof(SDropCGroupStmt));
158 159
    case QUERY_NODE_ALTER_LOCAL_STMT:
      return makeNode(type, sizeof(SAlterLocalStmt));
160
    case QUERY_NODE_EXPLAIN_STMT:
X
Xiaoyu Wang 已提交
161
      return makeNode(type, sizeof(SExplainStmt));
162
    case QUERY_NODE_DESCRIBE_STMT:
X
Xiaoyu Wang 已提交
163
      return makeNode(type, sizeof(SDescribeStmt));
164
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
X
Xiaoyu Wang 已提交
165
      return makeNode(type, sizeof(SNode));
166
    case QUERY_NODE_COMPACT_STMT:
167
      break;
168
    case QUERY_NODE_CREATE_FUNCTION_STMT:
X
Xiaoyu Wang 已提交
169
      return makeNode(type, sizeof(SCreateFunctionStmt));
170
    case QUERY_NODE_DROP_FUNCTION_STMT:
X
Xiaoyu Wang 已提交
171
      return makeNode(type, sizeof(SDropFunctionStmt));
172
    case QUERY_NODE_CREATE_STREAM_STMT:
X
Xiaoyu Wang 已提交
173
      return makeNode(type, sizeof(SCreateStreamStmt));
174
    case QUERY_NODE_DROP_STREAM_STMT:
X
Xiaoyu Wang 已提交
175
      return makeNode(type, sizeof(SDropStreamStmt));
X
Xiaoyu Wang 已提交
176 177
    case QUERY_NODE_BALANCE_VGROUP_STMT:
      return makeNode(type, sizeof(SBalanceVgroupStmt));
178
    case QUERY_NODE_MERGE_VGROUP_STMT:
X
Xiaoyu Wang 已提交
179
      return makeNode(type, sizeof(SMergeVgroupStmt));
180
    case QUERY_NODE_REDISTRIBUTE_VGROUP_STMT:
X
Xiaoyu Wang 已提交
181
      return makeNode(type, sizeof(SRedistributeVgroupStmt));
182
    case QUERY_NODE_SPLIT_VGROUP_STMT:
X
Xiaoyu Wang 已提交
183
      return makeNode(type, sizeof(SSplitVgroupStmt));
184 185
    case QUERY_NODE_SYNCDB_STMT:
      break;
186
    case QUERY_NODE_GRANT_STMT:
X
Xiaoyu Wang 已提交
187
      return makeNode(type, sizeof(SGrantStmt));
188
    case QUERY_NODE_REVOKE_STMT:
X
Xiaoyu Wang 已提交
189
      return makeNode(type, sizeof(SRevokeStmt));
190
    case QUERY_NODE_SHOW_DNODES_STMT:
X
Xiaoyu Wang 已提交
191
    case QUERY_NODE_SHOW_MNODES_STMT:
X
Xiaoyu Wang 已提交
192 193
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
194 195
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
196
    case QUERY_NODE_SHOW_CLUSTER_STMT:
197
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
198 199
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
200
    case QUERY_NODE_SHOW_STABLES_STMT:
X
Xiaoyu Wang 已提交
201
    case QUERY_NODE_SHOW_STREAMS_STMT:
202 203
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
S
Shengliang Guan 已提交
204
    case QUERY_NODE_SHOW_LICENCE_STMT:
205
    case QUERY_NODE_SHOW_VGROUPS_STMT:
S
Shengliang Guan 已提交
206
    case QUERY_NODE_SHOW_TOPICS_STMT:
207 208 209
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
    case QUERY_NODE_SHOW_SUBSCRIBES_STMT:
    case QUERY_NODE_SHOW_SMAS_STMT:
210
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
211 212
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_VNODES_STMT:
213 214
    case QUERY_NODE_SHOW_APPS_STMT:
    case QUERY_NODE_SHOW_SCORES_STMT:
215 216
    case QUERY_NODE_SHOW_VARIABLES_STMT:
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
217
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
218
    case QUERY_NODE_SHOW_SUBSCRIPTIONS_STMT:
219
    case QUERY_NODE_SHOW_TAGS_STMT:
220
      return makeNode(type, sizeof(SShowStmt));
221 222
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:
      return makeNode(type, sizeof(SShowDnodeVariablesStmt));
223
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
224
      return makeNode(type, sizeof(SShowCreateDatabaseStmt));
225 226
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
227
      return makeNode(type, sizeof(SShowCreateTableStmt));
228 229
    case QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT:
      return makeNode(type, sizeof(SShowTableDistributedStmt));
230
    case QUERY_NODE_KILL_QUERY_STMT:
X
Xiaoyu Wang 已提交
231
      return makeNode(type, sizeof(SKillQueryStmt));
232
    case QUERY_NODE_KILL_TRANSACTION_STMT:
X
Xiaoyu Wang 已提交
233
    case QUERY_NODE_KILL_CONNECTION_STMT:
X
Xiaoyu Wang 已提交
234
      return makeNode(type, sizeof(SKillStmt));
X
Xiaoyu Wang 已提交
235 236
    case QUERY_NODE_DELETE_STMT:
      return makeNode(type, sizeof(SDeleteStmt));
237 238
    case QUERY_NODE_INSERT_STMT:
      return makeNode(type, sizeof(SInsertStmt));
239 240
    case QUERY_NODE_QUERY:
      return makeNode(type, sizeof(SQuery));
X
Xiaoyu Wang 已提交
241
    case QUERY_NODE_LOGIC_PLAN_SCAN:
X
Xiaoyu Wang 已提交
242
      return makeNode(type, sizeof(SScanLogicNode));
X
Xiaoyu Wang 已提交
243
    case QUERY_NODE_LOGIC_PLAN_JOIN:
X
Xiaoyu Wang 已提交
244
      return makeNode(type, sizeof(SJoinLogicNode));
X
Xiaoyu Wang 已提交
245
    case QUERY_NODE_LOGIC_PLAN_AGG:
X
Xiaoyu Wang 已提交
246
      return makeNode(type, sizeof(SAggLogicNode));
X
Xiaoyu Wang 已提交
247
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
X
Xiaoyu Wang 已提交
248
      return makeNode(type, sizeof(SProjectLogicNode));
X
Xiaoyu Wang 已提交
249 250
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY:
      return makeNode(type, sizeof(SVnodeModifyLogicNode));
X
Xiaoyu Wang 已提交
251
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
X
Xiaoyu Wang 已提交
252
      return makeNode(type, sizeof(SExchangeLogicNode));
X
Xiaoyu Wang 已提交
253 254
    case QUERY_NODE_LOGIC_PLAN_MERGE:
      return makeNode(type, sizeof(SMergeLogicNode));
X
Xiaoyu Wang 已提交
255
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
X
Xiaoyu Wang 已提交
256
      return makeNode(type, sizeof(SWindowLogicNode));
X
Xiaoyu Wang 已提交
257
    case QUERY_NODE_LOGIC_PLAN_FILL:
X
Xiaoyu Wang 已提交
258
      return makeNode(type, sizeof(SFillLogicNode));
X
Xiaoyu Wang 已提交
259
    case QUERY_NODE_LOGIC_PLAN_SORT:
X
Xiaoyu Wang 已提交
260
      return makeNode(type, sizeof(SSortLogicNode));
261
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
X
Xiaoyu Wang 已提交
262
      return makeNode(type, sizeof(SPartitionLogicNode));
263 264
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC:
      return makeNode(type, sizeof(SIndefRowsFuncLogicNode));
X
Xiaoyu Wang 已提交
265 266
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC:
      return makeNode(type, sizeof(SInterpFuncLogicNode));
X
Xiaoyu Wang 已提交
267
    case QUERY_NODE_LOGIC_SUBPLAN:
X
Xiaoyu Wang 已提交
268
      return makeNode(type, sizeof(SLogicSubplan));
X
Xiaoyu Wang 已提交
269
    case QUERY_NODE_LOGIC_PLAN:
X
Xiaoyu Wang 已提交
270
      return makeNode(type, sizeof(SQueryLogicPlan));
X
Xiaoyu Wang 已提交
271
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
X
Xiaoyu Wang 已提交
272
      return makeNode(type, sizeof(STagScanPhysiNode));
X
Xiaoyu Wang 已提交
273
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
X
Xiaoyu Wang 已提交
274
      return makeNode(type, sizeof(STableScanPhysiNode));
275
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
X
Xiaoyu Wang 已提交
276
      return makeNode(type, sizeof(STableSeqScanPhysiNode));
X
Xiaoyu Wang 已提交
277 278
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
      return makeNode(type, sizeof(STableMergeScanPhysiNode));
279
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
X
Xiaoyu Wang 已提交
280
      return makeNode(type, sizeof(SStreamScanPhysiNode));
X
Xiaoyu Wang 已提交
281
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
X
Xiaoyu Wang 已提交
282
      return makeNode(type, sizeof(SSystemTableScanPhysiNode));
283 284
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
      return makeNode(type, sizeof(SBlockDistScanPhysiNode));
X
Xiaoyu Wang 已提交
285 286
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
      return makeNode(type, sizeof(SLastRowScanPhysiNode));
X
Xiaoyu Wang 已提交
287
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
X
Xiaoyu Wang 已提交
288
      return makeNode(type, sizeof(SProjectPhysiNode));
289
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
X
Xiaoyu Wang 已提交
290
      return makeNode(type, sizeof(SJoinPhysiNode));
291
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
X
Xiaoyu Wang 已提交
292
      return makeNode(type, sizeof(SAggPhysiNode));
293
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
X
Xiaoyu Wang 已提交
294
      return makeNode(type, sizeof(SExchangePhysiNode));
X
Xiaoyu Wang 已提交
295 296
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
      return makeNode(type, sizeof(SMergePhysiNode));
297
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
X
Xiaoyu Wang 已提交
298
      return makeNode(type, sizeof(SSortPhysiNode));
299 300
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT:
      return makeNode(type, sizeof(SGroupSortPhysiNode));
X
Xiaoyu Wang 已提交
301
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
X
Xiaoyu Wang 已提交
302
      return makeNode(type, sizeof(SIntervalPhysiNode));
303 304
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
      return makeNode(type, sizeof(SMergeAlignedIntervalPhysiNode));
X
Xiaoyu Wang 已提交
305
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
X
Xiaoyu Wang 已提交
306
      return makeNode(type, sizeof(SStreamIntervalPhysiNode));
307 308 309 310
    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 已提交
311
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
X
Xiaoyu Wang 已提交
312
      return makeNode(type, sizeof(SFillPhysiNode));
313
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
X
Xiaoyu Wang 已提交
314
      return makeNode(type, sizeof(SSessionWinodwPhysiNode));
315
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
X
Xiaoyu Wang 已提交
316
      return makeNode(type, sizeof(SStreamSessionWinodwPhysiNode));
317 318 319 320
    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));
321
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
X
Xiaoyu Wang 已提交
322
      return makeNode(type, sizeof(SStateWinodwPhysiNode));
323
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
5
54liuyao 已提交
324
      return makeNode(type, sizeof(SStreamStateWinodwPhysiNode));
325
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
X
Xiaoyu Wang 已提交
326
      return makeNode(type, sizeof(SPartitionPhysiNode));
327 328
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC:
      return makeNode(type, sizeof(SIndefRowsFuncPhysiNode));
X
Xiaoyu Wang 已提交
329 330
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
      return makeNode(type, sizeof(SInterpFuncLogicNode));
331
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
X
Xiaoyu Wang 已提交
332
      return makeNode(type, sizeof(SDataDispatcherNode));
333
    case QUERY_NODE_PHYSICAL_PLAN_INSERT:
X
Xiaoyu Wang 已提交
334
      return makeNode(type, sizeof(SDataInserterNode));
335 336
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT:
      return makeNode(type, sizeof(SQueryInserterNode));
X
Xiaoyu Wang 已提交
337 338
    case QUERY_NODE_PHYSICAL_PLAN_DELETE:
      return makeNode(type, sizeof(SDataDeleterNode));
X
Xiaoyu Wang 已提交
339
    case QUERY_NODE_PHYSICAL_SUBPLAN:
X
Xiaoyu Wang 已提交
340
      return makeNode(type, sizeof(SSubplan));
X
Xiaoyu Wang 已提交
341
    case QUERY_NODE_PHYSICAL_PLAN:
X
Xiaoyu Wang 已提交
342
      return makeNode(type, sizeof(SQueryPlan));
343 344 345
    default:
      break;
  }
346
  nodesError("nodesMakeNode unknown node = %s", nodesNodeName(type));
X
Xiaoyu Wang 已提交
347
  return NULL;
348 349
}

X
Xiaoyu Wang 已提交
350 351 352 353 354 355 356 357 358 359 360 361
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 已提交
362 363 364 365
  nodesDestroyNode(pNode->pConditions);
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pLimit);
  nodesDestroyNode(pNode->pSlimit);
X
Xiaoyu Wang 已提交
366 367 368 369 370
}

static void destroyPhysiNode(SPhysiNode* pNode) {
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pConditions);
371
  nodesDestroyNode((SNode*)pNode->pOutputDataBlockDesc);
372 373
  nodesDestroyNode(pNode->pLimit);
  nodesDestroyNode(pNode->pSlimit);
X
Xiaoyu Wang 已提交
374 375 376 377 378 379
}

static void destroyWinodwPhysiNode(SWinodwPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pExprs);
  nodesDestroyList(pNode->pFuncs);
380
  nodesDestroyNode(pNode->pTspk);
5
54liuyao 已提交
381
  nodesDestroyNode(pNode->pTsEnd);
X
Xiaoyu Wang 已提交
382 383 384 385 386
}

static void destroyScanPhysiNode(SScanPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pScanCols);
X
Xiaoyu Wang 已提交
387
  nodesDestroyList(pNode->pScanPseudoCols);
X
Xiaoyu Wang 已提交
388 389
}

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

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

394 395 396 397 398 399
static void destroyTableCfg(STableCfg* pCfg) {
  taosArrayDestroy(pCfg->pFuncs);
  taosMemoryFree(pCfg->pComment);
  taosMemoryFree(pCfg->pSchemas);
  taosMemoryFree(pCfg->pTags);
  taosMemoryFree(pCfg);
D
dapan1121 已提交
400 401
}

402 403
static void destroySmaIndex(void* pIndex) { taosMemoryFree(((STableIndexInfo*)pIndex)->expr); }

404
void nodesDestroyNode(SNode* pNode) {
X
Xiaoyu Wang 已提交
405 406 407 408 409
  if (NULL == pNode) {
    return;
  }

  switch (nodeType(pNode)) {
X
Xiaoyu Wang 已提交
410
    case QUERY_NODE_COLUMN:  // pProjectRef is weak reference, no need to release
X
Xiaoyu Wang 已提交
411
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
412
      break;
D
dapan1121 已提交
413
    case QUERY_NODE_VALUE: {
X
Xiaoyu Wang 已提交
414
      SValueNode* pValue = (SValueNode*)pNode;
X
Xiaoyu Wang 已提交
415
      destroyExprNode((SExprNode*)pNode);
wafwerar's avatar
wafwerar 已提交
416
      taosMemoryFreeClear(pValue->literal);
D
dapan1121 已提交
417
      if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
wafwerar's avatar
wafwerar 已提交
418
        taosMemoryFreeClear(pValue->datum.p);
D
dapan1121 已提交
419
      }
X
Xiaoyu Wang 已提交
420 421 422 423
      break;
    }
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOp = (SOperatorNode*)pNode;
X
Xiaoyu Wang 已提交
424
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
425 426
      nodesDestroyNode(pOp->pLeft);
      nodesDestroyNode(pOp->pRight);
D
dapan1121 已提交
427 428 429
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
430
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
431
      nodesDestroyList(((SLogicConditionNode*)pNode)->pParameterList);
D
dapan1121 已提交
432 433
      break;
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
434
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
435
      nodesDestroyList(((SFunctionNode*)pNode)->pParameterList);
X
Xiaoyu Wang 已提交
436 437
      break;
    case QUERY_NODE_REAL_TABLE: {
X
Xiaoyu Wang 已提交
438
      SRealTableNode* pReal = (SRealTableNode*)pNode;
wafwerar's avatar
wafwerar 已提交
439 440
      taosMemoryFreeClear(pReal->pMeta);
      taosMemoryFreeClear(pReal->pVgroupList);
441
      taosArrayDestroyEx(pReal->pSmaIndexes, destroySmaIndex);
X
Xiaoyu Wang 已提交
442 443 444
      break;
    }
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
445
      nodesDestroyNode(((STempTableNode*)pNode)->pSubquery);
D
dapan1121 已提交
446
      break;
X
Xiaoyu Wang 已提交
447 448 449 450 451
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoin = (SJoinTableNode*)pNode;
      nodesDestroyNode(pJoin->pLeft);
      nodesDestroyNode(pJoin->pRight);
      nodesDestroyNode(pJoin->pOnCond);
D
dapan1121 已提交
452
      break;
X
Xiaoyu Wang 已提交
453
    }
D
dapan1121 已提交
454
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
455 456 457 458 459
      nodesDestroyList(((SGroupingSetNode*)pNode)->pParameterList);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      nodesDestroyNode(((SOrderByExprNode*)pNode)->pExpr);
      break;
X
Xiaoyu Wang 已提交
460
    case QUERY_NODE_LIMIT:  // no pointer field
D
dapan1121 已提交
461
      break;
462 463 464 465
    case QUERY_NODE_STATE_WINDOW: {
      SStateWindowNode* pState = (SStateWindowNode*)pNode;
      nodesDestroyNode(pState->pCol);
      nodesDestroyNode(pState->pExpr);
X
Xiaoyu Wang 已提交
466
      break;
467
    }
X
Xiaoyu Wang 已提交
468 469
    case QUERY_NODE_SESSION_WINDOW: {
      SSessionWindowNode* pSession = (SSessionWindowNode*)pNode;
470 471
      nodesDestroyNode((SNode*)pSession->pCol);
      nodesDestroyNode((SNode*)pSession->pGap);
D
dapan1121 已提交
472
      break;
X
Xiaoyu Wang 已提交
473 474 475 476 477 478 479 480 481 482
    }
    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 已提交
483
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
484 485
      nodesDestroyList(((SNodeListNode*)pNode)->pNodeList);
      break;
X
Xiaoyu Wang 已提交
486 487 488 489
    case QUERY_NODE_FILL: {
      SFillNode* pFill = (SFillNode*)pNode;
      nodesDestroyNode(pFill->pValues);
      nodesDestroyNode(pFill->pWStartTs);
X
Xiaoyu Wang 已提交
490
      break;
X
Xiaoyu Wang 已提交
491
    }
X
Xiaoyu Wang 已提交
492 493
    case QUERY_NODE_RAW_EXPR:
      nodesDestroyNode(((SRawExprNode*)pNode)->pNode);
X
Xiaoyu Wang 已提交
494
      break;
X
Xiaoyu Wang 已提交
495 496 497 498 499 500
    case QUERY_NODE_TARGET:
      nodesDestroyNode(((STargetNode*)pNode)->pExpr);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      nodesDestroyList(((SDataBlockDescNode*)pNode)->pSlots);
      break;
X
Xiaoyu Wang 已提交
501 502 503
    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 已提交
504
      break;
X
Xiaoyu Wang 已提交
505 506
    case QUERY_NODE_DATABASE_OPTIONS: {
      SDatabaseOptions* pOptions = (SDatabaseOptions*)pNode;
507
      nodesDestroyNode((SNode*)pOptions->pDaysPerFile);
X
Xiaoyu Wang 已提交
508 509
      nodesDestroyList(pOptions->pKeep);
      nodesDestroyList(pOptions->pRetentions);
X
Xiaoyu Wang 已提交
510
      break;
X
Xiaoyu Wang 已提交
511
    }
X
Xiaoyu Wang 已提交
512
    case QUERY_NODE_TABLE_OPTIONS: {
X
Xiaoyu Wang 已提交
513
      STableOptions* pOptions = (STableOptions*)pNode;
514 515
      nodesDestroyList(pOptions->pMaxDelay);
      nodesDestroyList(pOptions->pWatermark);
X
Xiaoyu Wang 已提交
516
      nodesDestroyList(pOptions->pRollupFuncs);
517
      nodesDestroyList(pOptions->pSma);
X
Xiaoyu Wang 已提交
518
      break;
X
Xiaoyu Wang 已提交
519
    }
520
    case QUERY_NODE_INDEX_OPTIONS: {
X
Xiaoyu Wang 已提交
521 522 523 524 525
      SIndexOptions* pOptions = (SIndexOptions*)pNode;
      nodesDestroyList(pOptions->pFuncs);
      nodesDestroyNode(pOptions->pInterval);
      nodesDestroyNode(pOptions->pOffset);
      nodesDestroyNode(pOptions->pSliding);
526
      nodesDestroyNode(pOptions->pStreamOptions);
527 528
      break;
    }
529 530
    case QUERY_NODE_EXPLAIN_OPTIONS:  // no pointer field
      break;
531 532 533 534
    case QUERY_NODE_STREAM_OPTIONS: {
      SStreamOptions* pOptions = (SStreamOptions*)pNode;
      nodesDestroyNode(pOptions->pDelay);
      nodesDestroyNode(pOptions->pWatermark);
535
      break;
536
    }
537 538
    case QUERY_NODE_LEFT_VALUE:  // no pointer field
      break;
X
Xiaoyu Wang 已提交
539 540
    case QUERY_NODE_SET_OPERATOR: {
      SSetOperator* pStmt = (SSetOperator*)pNode;
541
      nodesDestroyList(pStmt->pProjectionList);
X
Xiaoyu Wang 已提交
542 543 544 545 546 547
      nodesDestroyNode(pStmt->pLeft);
      nodesDestroyNode(pStmt->pRight);
      nodesDestroyList(pStmt->pOrderByList);
      nodesDestroyNode(pStmt->pLimit);
      break;
    }
X
Xiaoyu Wang 已提交
548
    case QUERY_NODE_SELECT_STMT: {
X
Xiaoyu Wang 已提交
549
      SSelectStmt* pStmt = (SSelectStmt*)pNode;
X
Xiaoyu Wang 已提交
550 551 552 553 554 555 556
      nodesDestroyList(pStmt->pProjectionList);
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyList(pStmt->pPartitionByList);
      nodesDestroyNode(pStmt->pWindow);
      nodesDestroyList(pStmt->pGroupByList);
      nodesDestroyNode(pStmt->pHaving);
X
Xiaoyu Wang 已提交
557 558 559
      nodesDestroyNode(pStmt->pRange);
      nodesDestroyNode(pStmt->pEvery);
      nodesDestroyNode(pStmt->pFill);
X
Xiaoyu Wang 已提交
560
      nodesDestroyList(pStmt->pOrderByList);
561 562
      nodesDestroyNode((SNode*)pStmt->pLimit);
      nodesDestroyNode((SNode*)pStmt->pSlimit);
X
Xiaoyu Wang 已提交
563 564
      break;
    }
X
Xiaoyu Wang 已提交
565 566 567 568
    case QUERY_NODE_VNODE_MODIF_STMT:
      destroyVgDataBlockArray(((SVnodeModifOpStmt*)pNode)->pDataBlocks);
      break;
    case QUERY_NODE_CREATE_DATABASE_STMT:
569
      nodesDestroyNode((SNode*)((SCreateDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
570
      break;
X
Xiaoyu Wang 已提交
571
    case QUERY_NODE_DROP_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
572 573
      break;
    case QUERY_NODE_ALTER_DATABASE_STMT:
574
      nodesDestroyNode((SNode*)((SAlterDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
575
      break;
X
Xiaoyu Wang 已提交
576
    case QUERY_NODE_FLUSH_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
577
    case QUERY_NODE_TRIM_DATABASE_STMT:   // no pointer field
X
Xiaoyu Wang 已提交
578
      break;
X
Xiaoyu Wang 已提交
579
    case QUERY_NODE_CREATE_TABLE_STMT: {
X
Xiaoyu Wang 已提交
580
      SCreateTableStmt* pStmt = (SCreateTableStmt*)pNode;
X
Xiaoyu Wang 已提交
581 582
      nodesDestroyList(pStmt->pCols);
      nodesDestroyList(pStmt->pTags);
583
      nodesDestroyNode((SNode*)pStmt->pOptions);
X
Xiaoyu Wang 已提交
584 585 586
      break;
    }
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE: {
X
Xiaoyu Wang 已提交
587
      SCreateSubTableClause* pStmt = (SCreateSubTableClause*)pNode;
X
Xiaoyu Wang 已提交
588 589
      nodesDestroyList(pStmt->pSpecificTags);
      nodesDestroyList(pStmt->pValsOfTags);
wmmhello's avatar
wmmhello 已提交
590
      nodesDestroyNode((SNode*)pStmt->pOptions);
X
Xiaoyu Wang 已提交
591 592 593
      break;
    }
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
X
Xiaoyu Wang 已提交
594 595
      nodesDestroyList(((SCreateMultiTableStmt*)pNode)->pSubTables);
      break;
X
Xiaoyu Wang 已提交
596
    case QUERY_NODE_DROP_TABLE_CLAUSE:  // no pointer field
X
Xiaoyu Wang 已提交
597 598
      break;
    case QUERY_NODE_DROP_TABLE_STMT:
599
      nodesDestroyList(((SDropTableStmt*)pNode)->pTables);
X
Xiaoyu Wang 已提交
600
      break;
X
Xiaoyu Wang 已提交
601
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
602
      break;
603 604
    case QUERY_NODE_ALTER_TABLE_STMT:
    case QUERY_NODE_ALTER_SUPER_TABLE_STMT: {
X
Xiaoyu Wang 已提交
605
      SAlterTableStmt* pStmt = (SAlterTableStmt*)pNode;
606 607
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode((SNode*)pStmt->pVal);
X
Xiaoyu Wang 已提交
608 609
      break;
    }
X
Xiaoyu Wang 已提交
610 611 612 613 614 615 616
    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
617
      break;
618
    case QUERY_NODE_CREATE_INDEX_STMT: {
X
Xiaoyu Wang 已提交
619
      SCreateIndexStmt* pStmt = (SCreateIndexStmt*)pNode;
620
      nodesDestroyNode((SNode*)pStmt->pOptions);
621 622 623
      nodesDestroyList(pStmt->pCols);
      break;
    }
X
Xiaoyu Wang 已提交
624 625 626
    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
627 628 629 630 631 632
    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 已提交
633 634 635 636
      break;
    case QUERY_NODE_CREATE_TOPIC_STMT:
      nodesDestroyNode(((SCreateTopicStmt*)pNode)->pQuery);
      break;
X
Xiaoyu Wang 已提交
637
    case QUERY_NODE_DROP_TOPIC_STMT:   // no pointer field
638
    case QUERY_NODE_DROP_CGROUP_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
639
    case QUERY_NODE_ALTER_LOCAL_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
640
      break;
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    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
    case QUERY_NODE_COMPACT_STMT:            // no pointer field
    case QUERY_NODE_CREATE_FUNCTION_STMT:    // no pointer field
    case QUERY_NODE_DROP_FUNCTION_STMT:      // no pointer field
      break;
    case QUERY_NODE_CREATE_STREAM_STMT: {
      SCreateStreamStmt* pStmt = (SCreateStreamStmt*)pNode;
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode(pStmt->pQuery);
      break;
    }
    case QUERY_NODE_DROP_STREAM_STMT:     // no pointer field
    case QUERY_NODE_BALANCE_VGROUP_STMT:  // no pointer field
    case QUERY_NODE_MERGE_VGROUP_STMT:    // no pointer field
      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
    case QUERY_NODE_GRANT_STMT:         // no pointer field
    case QUERY_NODE_REVOKE_STMT:        // no pointer field
      break;
X
Xiaoyu Wang 已提交
673 674 675 676
    case QUERY_NODE_SHOW_DNODES_STMT:
    case QUERY_NODE_SHOW_MNODES_STMT:
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
677 678 679 680
    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 已提交
681 682
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    case QUERY_NODE_SHOW_STABLES_STMT:
    case QUERY_NODE_SHOW_STREAMS_STMT:
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
    case QUERY_NODE_SHOW_LICENCE_STMT:
    case QUERY_NODE_SHOW_VGROUPS_STMT:
    case QUERY_NODE_SHOW_TOPICS_STMT:
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
    case QUERY_NODE_SHOW_SUBSCRIBES_STMT:
    case QUERY_NODE_SHOW_SMAS_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:
698 699
    case QUERY_NODE_SHOW_VARIABLES_STMT:
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
700
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
701 702
    case QUERY_NODE_SHOW_SUBSCRIPTIONS_STMT:
    case QUERY_NODE_SHOW_TAGS_STMT: {
X
Xiaoyu Wang 已提交
703 704
      SShowStmt* pStmt = (SShowStmt*)pNode;
      nodesDestroyNode(pStmt->pDbName);
705
      nodesDestroyNode(pStmt->pTbName);
X
Xiaoyu Wang 已提交
706 707
      break;
    }
708 709
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:
      nodesDestroyNode(((SShowDnodeVariablesStmt*)pNode)->pDnodeId);
710
      break;
711 712 713 714 715
    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:
716
      destroyTableCfg((STableCfg*)(((SShowCreateTableStmt*)pNode)->pCfg));
717 718 719 720 721
      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
722 723 724 725 726 727
      break;
    case QUERY_NODE_DELETE_STMT: {
      SDeleteStmt* pStmt = (SDeleteStmt*)pNode;
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyNode(pStmt->pCountFunc);
X
Xiaoyu Wang 已提交
728
      nodesDestroyNode(pStmt->pTagCond);
729 730
      break;
    }
731 732 733 734 735 736 737
    case QUERY_NODE_INSERT_STMT: {
      SInsertStmt* pStmt = (SInsertStmt*)pNode;
      nodesDestroyNode(pStmt->pTable);
      nodesDestroyList(pStmt->pCols);
      nodesDestroyNode(pStmt->pQuery);
      break;
    }
738 739 740 741 742 743 744 745 746 747
    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);
748 749
      taosArrayDestroy(pQuery->pPlaceholderValues);
      nodesDestroyNode(pQuery->pPrepareRoot);
750 751
      break;
    }
X
Xiaoyu Wang 已提交
752 753 754 755
    case QUERY_NODE_LOGIC_PLAN_SCAN: {
      SScanLogicNode* pLogicNode = (SScanLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pScanCols);
X
Xiaoyu Wang 已提交
756
      nodesDestroyList(pLogicNode->pScanPseudoCols);
X
Xiaoyu Wang 已提交
757
      taosMemoryFreeClear(pLogicNode->pVgroupList);
X
Xiaoyu Wang 已提交
758 759 760
      nodesDestroyList(pLogicNode->pDynamicScanFuncs);
      nodesDestroyNode(pLogicNode->pTagCond);
      nodesDestroyNode(pLogicNode->pTagIndexCond);
761
      taosArrayDestroyEx(pLogicNode->pSmaIndexes, destroySmaIndex);
762
      nodesDestroyList(pLogicNode->pGroupTags);
X
Xiaoyu Wang 已提交
763 764 765 766 767
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_JOIN: {
      SJoinLogicNode* pLogicNode = (SJoinLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
768
      nodesDestroyNode(pLogicNode->pMergeCondition);
X
Xiaoyu Wang 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
      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;
    }
X
Xiaoyu Wang 已提交
785 786
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY: {
      SVnodeModifyLogicNode* pLogicNode = (SVnodeModifyLogicNode*)pNode;
X
Xiaoyu Wang 已提交
787 788
      destroyLogicNode((SLogicNode*)pLogicNode);
      destroyVgDataBlockArray(pLogicNode->pDataBlocks);
X
bugfix  
Xiaoyu Wang 已提交
789
      // pVgDataBlocks is weak reference
790 791 792
      nodesDestroyNode(pLogicNode->pAffectedRows);
      taosMemoryFreeClear(pLogicNode->pVgroupList);
      nodesDestroyList(pLogicNode->pInsertCols);
X
Xiaoyu Wang 已提交
793 794 795 796 797
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      destroyLogicNode((SLogicNode*)pNode);
      break;
798 799 800 801 802 803 804
    case QUERY_NODE_LOGIC_PLAN_MERGE: {
      SMergeLogicNode* pLogicNode = (SMergeLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pMergeKeys);
      nodesDestroyList(pLogicNode->pInputs);
      break;
    }
X
Xiaoyu Wang 已提交
805 806 807 808 809
    case QUERY_NODE_LOGIC_PLAN_WINDOW: {
      SWindowLogicNode* pLogicNode = (SWindowLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
      nodesDestroyNode(pLogicNode->pTspk);
5
54liuyao 已提交
810
      nodesDestroyNode(pLogicNode->pTsEnd);
811
      nodesDestroyNode(pLogicNode->pStateExpr);
X
Xiaoyu Wang 已提交
812 813
      break;
    }
814 815 816 817 818 819 820
    case QUERY_NODE_LOGIC_PLAN_FILL: {
      SFillLogicNode* pLogicNode = (SFillLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyNode(pLogicNode->pWStartTs);
      nodesDestroyNode(pLogicNode->pValues);
      break;
    }
X
Xiaoyu Wang 已提交
821 822 823 824 825 826 827 828 829 830 831 832
    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;
    }
833 834 835
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC: {
      SIndefRowsFuncLogicNode* pLogicNode = (SIndefRowsFuncLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
X
Xiaoyu Wang 已提交
836 837 838 839 840 841 842
      nodesDestroyList(pLogicNode->pFuncs);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC: {
      SInterpFuncLogicNode* pLogicNode = (SInterpFuncLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
X
Xiaoyu Wang 已提交
843 844
      nodesDestroyNode(pLogicNode->pFillValues);
      nodesDestroyNode(pLogicNode->pTimeSeries);
845 846
      break;
    }
X
Xiaoyu Wang 已提交
847 848 849
    case QUERY_NODE_LOGIC_SUBPLAN: {
      SLogicSubplan* pSubplan = (SLogicSubplan*)pNode;
      nodesDestroyList(pSubplan->pChildren);
850
      nodesDestroyNode((SNode*)pSubplan->pNode);
X
bugfix  
Xiaoyu Wang 已提交
851
      nodesClearList(pSubplan->pParents);
X
Xiaoyu Wang 已提交
852 853 854 855 856 857 858 859
      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:
860
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
X
Xiaoyu Wang 已提交
861 862
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
863 864 865 866 867 868
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN: {
      SLastRowScanPhysiNode* pPhyNode = (SLastRowScanPhysiNode*)pNode;
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      nodesDestroyList(pPhyNode->pGroupTags);
      break;
    }
X
Xiaoyu Wang 已提交
869 870 871 872 873 874 875
    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);
876
      nodesDestroyList(pPhyNode->pGroupTags);
X
Xiaoyu Wang 已提交
877 878
      break;
    }
X
Xiaoyu Wang 已提交
879 880 881 882 883 884
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT: {
      SProjectPhysiNode* pPhyNode = (SProjectPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pProjections);
      break;
    }
885
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN: {
X
Xiaoyu Wang 已提交
886 887
      SJoinPhysiNode* pPhyNode = (SJoinPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
888
      nodesDestroyNode(pPhyNode->pMergeCondition);
X
Xiaoyu Wang 已提交
889 890 891 892
      nodesDestroyNode(pPhyNode->pOnConditions);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
893
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG: {
X
Xiaoyu Wang 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906
      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;
    }
907 908 909 910 911 912 913
    case QUERY_NODE_PHYSICAL_PLAN_MERGE: {
      SMergePhysiNode* pPhyNode = (SMergePhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pMergeKeys);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
914 915
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT: {
X
Xiaoyu Wang 已提交
916 917
      SSortPhysiNode* pPhyNode = (SSortPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
918 919
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pSortKeys);
X
Xiaoyu Wang 已提交
920
      nodesDestroyList(pPhyNode->pTargets);
X
Xiaoyu Wang 已提交
921 922
      break;
    }
X
Xiaoyu Wang 已提交
923
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
924
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
X
Xiaoyu Wang 已提交
925
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
926 927
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
X
Xiaoyu Wang 已提交
928
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
X
Xiaoyu Wang 已提交
929
      break;
930 931 932 933 934 935 936 937
    case QUERY_NODE_PHYSICAL_PLAN_FILL: {
      SFillPhysiNode* pPhyNode = (SFillPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pWStartTs);
      nodesDestroyNode(pPhyNode->pValues);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
938 939
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
940
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
941
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
X
Xiaoyu Wang 已提交
942 943
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
      break;
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE: {
      SStateWinodwPhysiNode* pPhyNode = (SStateWinodwPhysiNode*)pNode;
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pStateKey);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION: {
      SPartitionPhysiNode* pPhyNode = (SPartitionPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pPartitionKeys);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC: {
      SIndefRowsFuncPhysiNode* pPhyNode = (SIndefRowsFuncPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
X
Xiaoyu Wang 已提交
963 964 965 966 967 968 969 970
      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 已提交
971 972
      nodesDestroyNode(pPhyNode->pFillValues);
      nodesDestroyNode(pPhyNode->pTimeSeries);
973 974
      break;
    }
X
Xiaoyu Wang 已提交
975 976 977 978 979 980 981 982 983
    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;
    }
984 985 986
    case QUERY_NODE_PHYSICAL_PLAN_QUERY_INSERT: {
      SQueryInserterNode* pSink = (SQueryInserterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
D
dapan1121 已提交
987
      nodesDestroyList(pSink->pCols);
988 989
      break;
    }
990 991 992 993 994 995
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
      SDataDeleterNode* pSink = (SDataDeleterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
      nodesDestroyNode(pSink->pAffectedRows);
      break;
    }
X
Xiaoyu Wang 已提交
996 997
    case QUERY_NODE_PHYSICAL_SUBPLAN: {
      SSubplan* pSubplan = (SSubplan*)pNode;
X
Xiaoyu Wang 已提交
998
      nodesClearList(pSubplan->pChildren);
999 1000
      nodesDestroyNode((SNode*)pSubplan->pNode);
      nodesDestroyNode((SNode*)pSubplan->pDataSink);
X
Xiaoyu Wang 已提交
1001 1002
      nodesDestroyNode((SNode*)pSubplan->pTagCond);
      nodesDestroyNode((SNode*)pSubplan->pTagIndexCond);
X
bugfix  
Xiaoyu Wang 已提交
1003 1004 1005
      nodesClearList(pSubplan->pParents);
      break;
    }
1006 1007
    case QUERY_NODE_PHYSICAL_PLAN:
      nodesDestroyList(((SQueryPlan*)pNode)->pSubplans);
X
Xiaoyu Wang 已提交
1008
      break;
1009 1010 1011
    default:
      break;
  }
X
Xiaoyu Wang 已提交
1012 1013
  taosMemoryFreeClear(pNode);
  return;
1014 1015
}

1016
SNodeList* nodesMakeList() {
wafwerar's avatar
wafwerar 已提交
1017
  SNodeList* p = taosMemoryCalloc(1, sizeof(SNodeList));
1018 1019 1020 1021 1022 1023
  if (NULL == p) {
    return NULL;
  }
  return p;
}

1024
int32_t nodesListAppend(SNodeList* pList, SNode* pNode) {
1025
  if (NULL == pList || NULL == pNode) {
X
Xiaoyu Wang 已提交
1026
    return TSDB_CODE_FAILED;
1027
  }
wafwerar's avatar
wafwerar 已提交
1028
  SListCell* p = taosMemoryCalloc(1, sizeof(SListCell));
1029
  if (NULL == p) {
X
Xiaoyu Wang 已提交
1030 1031
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_OUT_OF_MEMORY;
1032 1033
  }
  p->pNode = pNode;
1034 1035 1036 1037 1038 1039
  if (NULL == pList->pHead) {
    pList->pHead = p;
  }
  if (NULL != pList->pTail) {
    pList->pTail->pNext = p;
  }
1040
  p->pPrev = pList->pTail;
1041
  pList->pTail = p;
1042
  ++(pList->length);
X
Xiaoyu Wang 已提交
1043
  return TSDB_CODE_SUCCESS;
1044 1045
}

1046
int32_t nodesListStrictAppend(SNodeList* pList, SNode* pNode) {
1047
  if (NULL == pNode) {
X
Xiaoyu Wang 已提交
1048
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1049 1050 1051 1052 1053 1054 1055 1056 1057
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t code = nodesListAppend(pList, pNode);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyNode(pNode);
  }
  return code;
}

1058
int32_t nodesListMakeAppend(SNodeList** pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
  if (NULL == *pList) {
    *pList = nodesMakeList();
    if (NULL == *pList) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return nodesListAppend(*pList, pNode);
}

1069
int32_t nodesListMakeStrictAppend(SNodeList** pList, SNode* pNode) {
S
Shengliang Guan 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
  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 已提交
1080
int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) {
X
Xiaoyu Wang 已提交
1081
  if (NULL == pTarget || NULL == pSrc) {
X
Xiaoyu Wang 已提交
1082
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  }

  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 已提交
1093 1094
  pTarget->pTail = pSrc->pTail;
  pTarget->length += pSrc->length;
wafwerar's avatar
wafwerar 已提交
1095
  taosMemoryFreeClear(pSrc);
X
Xiaoyu Wang 已提交
1096

X
Xiaoyu Wang 已提交
1097 1098 1099
  return TSDB_CODE_SUCCESS;
}

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
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;
}

1111
int32_t nodesListPushFront(SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  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;
}

1130 1131 1132 1133 1134
SListCell* nodesListErase(SNodeList* pList, SListCell* pCell) {
  if (NULL == pCell->pPrev) {
    pList->pHead = pCell->pNext;
  } else {
    pCell->pPrev->pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
1135 1136 1137 1138
  }
  if (NULL == pCell->pNext) {
    pList->pTail = pCell->pPrev;
  } else {
1139 1140 1141
    pCell->pNext->pPrev = pCell->pPrev;
  }
  SListCell* pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
1142
  nodesDestroyNode(pCell->pNode);
wafwerar's avatar
wafwerar 已提交
1143
  taosMemoryFreeClear(pCell);
1144 1145 1146 1147
  --(pList->length);
  return pNext;
}

X
Xiaoyu Wang 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
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);
}

1166
SNode* nodesListGetNode(SNodeList* pList, int32_t index) {
1167 1168 1169 1170 1171 1172 1173 1174 1175
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return node;
    }
  }
  return NULL;
}

X
Xiaoyu Wang 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
SListCell* nodesListGetCell(SNodeList* pList, int32_t index) {
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return cell;
    }
  }
  return NULL;
}

1186
void nodesDestroyList(SNodeList* pList) {
1187 1188 1189
  if (NULL == pList) {
    return;
  }
X
Xiaoyu Wang 已提交
1190

D
dapan1121 已提交
1191 1192 1193
  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    pNext = nodesListErase(pList, pNext);
1194
  }
wafwerar's avatar
wafwerar 已提交
1195
  taosMemoryFreeClear(pList);
1196 1197
}

X
Xiaoyu Wang 已提交
1198 1199 1200 1201 1202 1203 1204 1205 1206
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 已提交
1207
    taosMemoryFreeClear(tmp);
X
Xiaoyu Wang 已提交
1208
  }
wafwerar's avatar
wafwerar 已提交
1209
  taosMemoryFreeClear(pList);
X
Xiaoyu Wang 已提交
1210 1211
}

X
Xiaoyu Wang 已提交
1212
void* nodesGetValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
  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:
1225
    case TSDB_DATA_TYPE_DOUBLE:
D
dapan1121 已提交
1226
      return (void*)&pNode->typeData;
D
dapan1121 已提交
1227 1228
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
1229
    case TSDB_DATA_TYPE_VARBINARY:
wmmhello's avatar
wmmhello 已提交
1230
    case TSDB_DATA_TYPE_JSON:
X
Xiaoyu Wang 已提交
1231
      return (void*)pNode->datum.p;
D
dapan1121 已提交
1232 1233 1234 1235 1236 1237 1238
    default:
      break;
  }

  return NULL;
}

X
Xiaoyu Wang 已提交
1239
int32_t nodesSetValueNodeValue(SValueNode* pNode, void* value) {
D
dapan1121 已提交
1240 1241 1242
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
      pNode->datum.b = *(bool*)value;
D
dapan1121 已提交
1243
      *(bool*)&pNode->typeData = pNode->datum.b;
D
dapan1121 已提交
1244 1245 1246
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pNode->datum.i = *(int8_t*)value;
D
dapan1121 已提交
1247
      *(int8_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1248 1249 1250
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pNode->datum.i = *(int16_t*)value;
D
dapan1121 已提交
1251
      *(int16_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1252 1253 1254
      break;
    case TSDB_DATA_TYPE_INT:
      pNode->datum.i = *(int32_t*)value;
D
dapan1121 已提交
1255
      *(int32_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1256 1257 1258
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
1259
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1260 1261 1262
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
1263
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1264 1265 1266
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pNode->datum.u = *(int8_t*)value;
D
dapan1121 已提交
1267
      *(int8_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1268 1269 1270
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pNode->datum.u = *(int16_t*)value;
D
dapan1121 已提交
1271
      *(int16_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1272 1273 1274
      break;
    case TSDB_DATA_TYPE_UINT:
      pNode->datum.u = *(int32_t*)value;
D
dapan1121 已提交
1275
      *(int32_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1276 1277 1278
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pNode->datum.u = *(uint64_t*)value;
D
dapan1121 已提交
1279
      *(uint64_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1280 1281 1282
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pNode->datum.d = *(float*)value;
D
dapan1121 已提交
1283
      *(float*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
1284 1285 1286
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pNode->datum.d = *(double*)value;
D
dapan1121 已提交
1287
      *(double*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
1288 1289 1290 1291
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
wmmhello's avatar
wmmhello 已提交
1292
    case TSDB_DATA_TYPE_JSON:
D
dapan1121 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301
      pNode->datum.p = (char*)value;
      break;
    default:
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1302
char* nodesGetStrValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
1303
  switch (pNode->node.resType.type) {
D
dapan1121 已提交
1304
    case TSDB_DATA_TYPE_BOOL: {
X
Xiaoyu Wang 已提交
1305
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1306 1307 1308
      if (NULL == buf) {
        return NULL;
      }
1309

D
dapan1121 已提交
1310 1311
      sprintf(buf, "%s", pNode->datum.b ? "true" : "false");
      return buf;
D
dapan1121 已提交
1312
    }
D
dapan1121 已提交
1313 1314 1315 1316 1317
    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 已提交
1318
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1319 1320 1321
      if (NULL == buf) {
        return NULL;
      }
1322

D
dapan1121 已提交
1323 1324 1325 1326 1327 1328 1329
      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 已提交
1330
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1331 1332 1333
      if (NULL == buf) {
        return NULL;
      }
1334

D
dapan1121 已提交
1335 1336 1337 1338 1339
      sprintf(buf, "%" PRIu64, pNode->datum.u);
      return buf;
    }
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE: {
X
Xiaoyu Wang 已提交
1340
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1341 1342 1343
      if (NULL == buf) {
        return NULL;
      }
1344

D
dapan1121 已提交
1345 1346 1347 1348 1349 1350
      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 已提交
1351
      int32_t bufSize = varDataLen(pNode->datum.p) + 2 + 1;
X
Xiaoyu Wang 已提交
1352
      void*   buf = taosMemoryMalloc(bufSize);
D
dapan1121 已提交
1353 1354 1355
      if (NULL == buf) {
        return NULL;
      }
1356

D
dapan1121 已提交
1357
      snprintf(buf, bufSize, "'%s'", varDataVal(pNode->datum.p));
D
dapan1121 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366
      return buf;
    }
    default:
      break;
  }

  return NULL;
}

1367 1368
bool nodesIsExprNode(const SNode* pNode) {
  ENodeType type = nodeType(pNode);
X
Xiaoyu Wang 已提交
1369
  return (QUERY_NODE_COLUMN == type || QUERY_NODE_VALUE == type || QUERY_NODE_OPERATOR == type ||
X
Xiaoyu Wang 已提交
1370
          QUERY_NODE_FUNCTION == type || QUERY_NODE_LOGIC_CONDITION == type);
1371 1372
}

X
Xiaoyu Wang 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
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;
}

1391 1392 1393 1394 1395 1396
bool nodesIsArithmeticOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_ADD:
    case OP_TYPE_SUB:
    case OP_TYPE_MULTI:
    case OP_TYPE_DIV:
1397
    case OP_TYPE_REM:
1398 1399 1400 1401 1402 1403
      return true;
    default:
      break;
  }
  return false;
}
1404

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
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:
1419
    case OP_TYPE_JSON_CONTAINS:
X
Xiaoyu Wang 已提交
1420 1421 1422 1423 1424 1425 1426 1427
    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:
1428 1429 1430 1431 1432
      return true;
    default:
      break;
  }
  return false;
1433 1434
}

1435 1436 1437
bool nodesIsJsonOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_JSON_GET_VALUE:
wmmhello's avatar
wmmhello 已提交
1438
    case OP_TYPE_JSON_CONTAINS:
1439 1440 1441 1442 1443 1444
      return true;
    default:
      break;
  }
  return false;
}
1445

X
Xiaoyu Wang 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
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 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
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 已提交
1470
typedef struct SCollectColumnsCxt {
1471 1472 1473 1474 1475
  int32_t         errCode;
  const char*     pTableAlias;
  ECollectColType collectType;
  SNodeList*      pCols;
  SHashObj*       pColHash;
X
Xiaoyu Wang 已提交
1476 1477
} SCollectColumnsCxt;

1478
static EDealRes doCollect(SCollectColumnsCxt* pCxt, SColumnNode* pCol, SNode* pNode) {
X
Xiaoyu Wang 已提交
1479
  char    name[TSDB_TABLE_NAME_LEN + TSDB_COL_NAME_LEN];
1480 1481 1482 1483 1484 1485 1486
  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 已提交
1487
    if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1488
      pCxt->errCode = nodesListStrictAppend(pCxt->pCols, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1489 1490 1491 1492 1493 1494
    }
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

1495 1496 1497 1498 1499 1500
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 已提交
1501 1502
static EDealRes collectColumns(SNode* pNode, void* pContext) {
  SCollectColumnsCxt* pCxt = (SCollectColumnsCxt*)pContext;
X
Xiaoyu Wang 已提交
1503
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
X
Xiaoyu Wang 已提交
1504
    SColumnNode* pCol = (SColumnNode*)pNode;
1505 1506
    if (isCollectType(pCxt->collectType, pCol->colType) &&
        (NULL == pCxt->pTableAlias || 0 == strcmp(pCxt->pTableAlias, pCol->tableAlias))) {
1507
      return doCollect(pCxt, pCol, pNode);
X
Xiaoyu Wang 已提交
1508 1509 1510 1511 1512
    }
  }
  return DEAL_RES_CONTINUE;
}

1513 1514
int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, ECollectColType type,
                            SNodeList** pCols) {
X
Xiaoyu Wang 已提交
1515
  if (NULL == pSelect || NULL == pCols) {
1516
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1517 1518 1519
  }

  SCollectColumnsCxt cxt = {
X
Xiaoyu Wang 已提交
1520 1521
      .errCode = TSDB_CODE_SUCCESS,
      .pTableAlias = pTableAlias,
1522
      .collectType = type,
1523
      .pCols = (NULL == *pCols ? nodesMakeList() : *pCols),
X
Xiaoyu Wang 已提交
1524
      .pColHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK)};
1525
  if (NULL == cxt.pCols || NULL == cxt.pColHash) {
X
Xiaoyu Wang 已提交
1526 1527
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1528
  *pCols = NULL;
X
Xiaoyu Wang 已提交
1529
  nodesWalkSelectStmt(pSelect, clause, collectColumns, &cxt);
1530
  taosHashCleanup(cxt.pColHash);
X
Xiaoyu Wang 已提交
1531
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
1532
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1533 1534
    return cxt.errCode;
  }
1535 1536 1537
  if (LIST_LENGTH(cxt.pCols) > 0) {
    *pCols = cxt.pCols;
  } else {
1538
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1539
  }
1540

X
Xiaoyu Wang 已提交
1541 1542 1543
  return TSDB_CODE_SUCCESS;
}

1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
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 已提交
1575
typedef struct SCollectFuncsCxt {
X
Xiaoyu Wang 已提交
1576
  int32_t         errCode;
X
Xiaoyu Wang 已提交
1577
  FFuncClassifier classifier;
X
Xiaoyu Wang 已提交
1578
  SNodeList*      pFuncs;
X
Xiaoyu Wang 已提交
1579
  SHashObj*       pFuncsSet;
X
Xiaoyu Wang 已提交
1580 1581 1582 1583
} SCollectFuncsCxt;

static EDealRes collectFuncs(SNode* pNode, void* pContext) {
  SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext;
1584 1585
  if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId) &&
      !(((SExprNode*)pNode)->orderAlias)) {
X
Xiaoyu Wang 已提交
1586
    SExprNode* pExpr = (SExprNode*)pNode;
X
Xiaoyu Wang 已提交
1587
    if (NULL == taosHashGet(pCxt->pFuncsSet, &pExpr, POINTER_BYTES)) {
X
Xiaoyu Wang 已提交
1588
      pCxt->errCode = nodesListStrictAppend(pCxt->pFuncs, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1589
      taosHashPut(pCxt->pFuncsSet, &pExpr, POINTER_BYTES, &pExpr, POINTER_BYTES);
X
Xiaoyu Wang 已提交
1590
    }
X
Xiaoyu Wang 已提交
1591 1592 1593 1594 1595
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1596 1597 1598 1599 1600 1601
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 已提交
1602 1603 1604
  if (0 != strcmp((*(const SExprNode**)pLeft)->aliasName, (*(const SExprNode**)pRight)->aliasName)) {
    return 1;
  }
X
Xiaoyu Wang 已提交
1605 1606 1607
  return nodesEqualNode(*(const SNode**)pLeft, *(const SNode**)pRight) ? 0 : 1;
}

X
Xiaoyu Wang 已提交
1608
int32_t nodesCollectFuncs(SSelectStmt* pSelect, ESqlClause clause, FFuncClassifier classifier, SNodeList** pFuncs) {
X
Xiaoyu Wang 已提交
1609
  if (NULL == pSelect || NULL == pFuncs) {
1610
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1611 1612
  }

X
Xiaoyu Wang 已提交
1613 1614 1615 1616 1617
  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 已提交
1618 1619
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1620
  taosHashSetEqualFp(cxt.pFuncsSet, funcNodeEqual);
1621
  *pFuncs = NULL;
X
Xiaoyu Wang 已提交
1622
  nodesWalkSelectStmt(pSelect, clause, collectFuncs, &cxt);
X
Xiaoyu Wang 已提交
1623 1624 1625 1626 1627 1628
  if (TSDB_CODE_SUCCESS == cxt.errCode) {
    if (LIST_LENGTH(cxt.pFuncs) > 0) {
      *pFuncs = cxt.pFuncs;
    } else {
      nodesDestroyList(cxt.pFuncs);
    }
X
Xiaoyu Wang 已提交
1629 1630 1631
  } else {
    nodesDestroyList(cxt.pFuncs);
  }
X
Xiaoyu Wang 已提交
1632
  taosHashCleanup(cxt.pFuncsSet);
X
Xiaoyu Wang 已提交
1633

X
Xiaoyu Wang 已提交
1634
  return cxt.errCode;
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
}

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 已提交
1674 1675
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1676

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
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 已提交
1697
char* nodesGetFillModeString(EFillMode mode) {
D
dapan1121 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
  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 已提交
1716
char* nodesGetNameFromColumnNode(SNode* pNode) {
D
dapan1121 已提交
1717 1718 1719
  if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) {
    return "NULL";
  }
X
Xiaoyu Wang 已提交
1720 1721

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

D
dapan1121 已提交
1724 1725 1726 1727 1728
int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) {
  if (NULL == pSlots || pSlots->length <= 0) {
    return 0;
  }

X
Xiaoyu Wang 已提交
1729
  SNode*  pNode = NULL;
D
dapan1121 已提交
1730 1731 1732 1733 1734 1735
  int32_t num = 0;
  FOREACH(pNode, pSlots) {
    if (QUERY_NODE_SLOT_DESC != pNode->type) {
      continue;
    }

X
Xiaoyu Wang 已提交
1736
    SSlotDescNode* descNode = (SSlotDescNode*)pNode;
D
dapan1121 已提交
1737 1738 1739 1740 1741 1742 1743 1744
    if (descNode->output) {
      ++num;
    }
  }

  return num;
}

X
Xiaoyu Wang 已提交
1745
void nodesValueNodeToVariant(const SValueNode* pNode, SVariant* pVal) {
1746 1747 1748 1749 1750
  if (pNode->isNull) {
    pVal->nType = TSDB_DATA_TYPE_NULL;
    pVal->nLen = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
    return;
  }
D
dapan1121 已提交
1751 1752 1753 1754
  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 已提交
1755
      break;
D
dapan1121 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
    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 已提交
1779 1780 1781
      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 已提交
1782 1783
      break;
    case TSDB_DATA_TYPE_JSON:
wmmhello's avatar
wmmhello 已提交
1784 1785 1786 1787
      pVal->nLen = getJsonValueLen(pNode->datum.p);
      pVal->pz = taosMemoryMalloc(pVal->nLen);
      memcpy(pVal->pz, pNode->datum.p, pVal->nLen);
      break;
D
dapan1121 已提交
1788 1789 1790 1791 1792 1793 1794
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }
}
X
Xiaoyu Wang 已提交
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804

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 {
1805
    SLogicConditionNode* pLogicCond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
X
Xiaoyu Wang 已提交
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
    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;
}

typedef struct SClassifyConditionCxt {
  bool hasPrimaryKey;
  bool hasTagIndexCol;
X
Xiaoyu Wang 已提交
1823
  bool hasTagCol;
X
Xiaoyu Wang 已提交
1824 1825 1826 1827 1828 1829 1830
  bool hasOtherCol;
} SClassifyConditionCxt;

static EDealRes classifyConditionImpl(SNode* pNode, void* pContext) {
  SClassifyConditionCxt* pCxt = (SClassifyConditionCxt*)pContext;
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    SColumnNode* pCol = (SColumnNode*)pNode;
1831
    if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && TSDB_SYSTEM_TABLE != pCol->tableType) {
X
Xiaoyu Wang 已提交
1832 1833 1834
      pCxt->hasPrimaryKey = true;
    } else if (pCol->hasIndex) {
      pCxt->hasTagIndexCol = true;
X
Xiaoyu Wang 已提交
1835
      pCxt->hasTagCol = true;
X
Xiaoyu Wang 已提交
1836
    } else if (COLUMN_TYPE_TAG == pCol->colType || COLUMN_TYPE_TBNAME == pCol->colType) {
X
Xiaoyu Wang 已提交
1837
      pCxt->hasTagCol = true;
X
Xiaoyu Wang 已提交
1838 1839 1840 1841 1842 1843 1844
    } else {
      pCxt->hasOtherCol = true;
    }
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1845 1846 1847 1848 1849 1850
typedef enum EConditionType {
  COND_TYPE_PRIMARY_KEY = 1,
  COND_TYPE_TAG_INDEX,
  COND_TYPE_TAG,
  COND_TYPE_NORMAL
} EConditionType;
X
Xiaoyu Wang 已提交
1851 1852 1853 1854 1855

static EConditionType classifyCondition(SNode* pNode) {
  SClassifyConditionCxt cxt = {.hasPrimaryKey = false, .hasTagIndexCol = false, .hasOtherCol = false};
  nodesWalkExpr(pNode, classifyConditionImpl, &cxt);
  return cxt.hasOtherCol ? COND_TYPE_NORMAL
X
Xiaoyu Wang 已提交
1856
                         : (cxt.hasPrimaryKey && cxt.hasTagCol
X
Xiaoyu Wang 已提交
1857
                                ? COND_TYPE_NORMAL
X
Xiaoyu Wang 已提交
1858 1859
                                : (cxt.hasPrimaryKey ? COND_TYPE_PRIMARY_KEY
                                                     : (cxt.hasTagIndexCol ? COND_TYPE_TAG_INDEX : COND_TYPE_TAG)));
X
Xiaoyu Wang 已提交
1860 1861
}

X
Xiaoyu Wang 已提交
1862 1863
static int32_t partitionLogicCond(SNode** pCondition, SNode** pPrimaryKeyCond, SNode** pTagIndexCond, SNode** pTagCond,
                                  SNode** pOtherCond) {
X
Xiaoyu Wang 已提交
1864 1865 1866 1867 1868
  SLogicConditionNode* pLogicCond = (SLogicConditionNode*)(*pCondition);

  int32_t code = TSDB_CODE_SUCCESS;

  SNodeList* pPrimaryKeyConds = NULL;
X
Xiaoyu Wang 已提交
1869
  SNodeList* pTagIndexConds = NULL;
X
Xiaoyu Wang 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
  SNodeList* pTagConds = NULL;
  SNodeList* pOtherConds = NULL;
  SNode*     pCond = NULL;
  FOREACH(pCond, pLogicCond->pParameterList) {
    switch (classifyCondition(pCond)) {
      case COND_TYPE_PRIMARY_KEY:
        if (NULL != pPrimaryKeyCond) {
          code = nodesListMakeAppend(&pPrimaryKeyConds, nodesCloneNode(pCond));
        }
        break;
      case COND_TYPE_TAG_INDEX:
X
Xiaoyu Wang 已提交
1881 1882 1883 1884 1885 1886 1887 1888
        if (NULL != pTagIndexCond) {
          code = nodesListMakeAppend(&pTagIndexConds, nodesCloneNode(pCond));
        }
        if (NULL != pTagCond) {
          code = nodesListMakeAppend(&pTagConds, nodesCloneNode(pCond));
        }
        break;
      case COND_TYPE_TAG:
X
Xiaoyu Wang 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
        if (NULL != pTagCond) {
          code = nodesListMakeAppend(&pTagConds, nodesCloneNode(pCond));
        }
        break;
      case COND_TYPE_NORMAL:
      default:
        if (NULL != pOtherCond) {
          code = nodesListMakeAppend(&pOtherConds, nodesCloneNode(pCond));
        }
        break;
    }
    if (TSDB_CODE_SUCCESS != code) {
      break;
    }
  }

  SNode* pTempPrimaryKeyCond = NULL;
X
Xiaoyu Wang 已提交
1906
  SNode* pTempTagIndexCond = NULL;
X
Xiaoyu Wang 已提交
1907 1908 1909 1910 1911
  SNode* pTempTagCond = NULL;
  SNode* pTempOtherCond = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempPrimaryKeyCond, &pPrimaryKeyConds);
  }
X
Xiaoyu Wang 已提交
1912 1913 1914
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempTagIndexCond, &pTagIndexConds);
  }
X
Xiaoyu Wang 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempTagCond, &pTagConds);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempOtherCond, &pOtherConds);
  }

  if (TSDB_CODE_SUCCESS == code) {
    if (NULL != pPrimaryKeyCond) {
      *pPrimaryKeyCond = pTempPrimaryKeyCond;
    }
X
Xiaoyu Wang 已提交
1926 1927 1928
    if (NULL != pTagIndexCond) {
      *pTagIndexCond = pTempTagIndexCond;
    }
X
Xiaoyu Wang 已提交
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
    if (NULL != pTagCond) {
      *pTagCond = pTempTagCond;
    }
    if (NULL != pOtherCond) {
      *pOtherCond = pTempOtherCond;
    }
    nodesDestroyNode(*pCondition);
    *pCondition = NULL;
  } else {
    nodesDestroyList(pPrimaryKeyConds);
X
Xiaoyu Wang 已提交
1939
    nodesDestroyList(pTagIndexConds);
X
Xiaoyu Wang 已提交
1940 1941 1942
    nodesDestroyList(pTagConds);
    nodesDestroyList(pOtherConds);
    nodesDestroyNode(pTempPrimaryKeyCond);
X
Xiaoyu Wang 已提交
1943
    nodesDestroyNode(pTempTagIndexCond);
X
Xiaoyu Wang 已提交
1944 1945 1946 1947 1948 1949 1950
    nodesDestroyNode(pTempTagCond);
    nodesDestroyNode(pTempOtherCond);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1951 1952
int32_t nodesPartitionCond(SNode** pCondition, SNode** pPrimaryKeyCond, SNode** pTagIndexCond, SNode** pTagCond,
                           SNode** pOtherCond) {
X
Xiaoyu Wang 已提交
1953 1954
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pCondition) &&
      LOGIC_COND_TYPE_AND == ((SLogicConditionNode*)*pCondition)->condType) {
X
Xiaoyu Wang 已提交
1955
    return partitionLogicCond(pCondition, pPrimaryKeyCond, pTagIndexCond, pTagCond, pOtherCond);
X
Xiaoyu Wang 已提交
1956 1957
  }

D
dapan1121 已提交
1958
  bool needOutput = false;
X
Xiaoyu Wang 已提交
1959 1960 1961 1962
  switch (classifyCondition(*pCondition)) {
    case COND_TYPE_PRIMARY_KEY:
      if (NULL != pPrimaryKeyCond) {
        *pPrimaryKeyCond = *pCondition;
D
dapan1121 已提交
1963
        needOutput = true;
X
Xiaoyu Wang 已提交
1964 1965 1966
      }
      break;
    case COND_TYPE_TAG_INDEX:
X
Xiaoyu Wang 已提交
1967 1968
      if (NULL != pTagIndexCond) {
        *pTagIndexCond = *pCondition;
D
dapan1121 已提交
1969
        needOutput = true;
X
Xiaoyu Wang 已提交
1970 1971
      }
      if (NULL != pTagCond) {
1972 1973 1974 1975 1976 1977 1978 1979
        SNode* pTempCond = *pCondition;
        if (NULL != pTagIndexCond) {
          pTempCond = nodesCloneNode(*pCondition);
          if (NULL == pTempCond) {
            return TSDB_CODE_OUT_OF_MEMORY;
          }
        }
        *pTagCond = pTempCond;
D
dapan1121 已提交
1980
        needOutput = true;
X
Xiaoyu Wang 已提交
1981 1982 1983
      }
      break;
    case COND_TYPE_TAG:
X
Xiaoyu Wang 已提交
1984 1985
      if (NULL != pTagCond) {
        *pTagCond = *pCondition;
D
dapan1121 已提交
1986
        needOutput = true;
X
Xiaoyu Wang 已提交
1987 1988 1989 1990 1991 1992
      }
      break;
    case COND_TYPE_NORMAL:
    default:
      if (NULL != pOtherCond) {
        *pOtherCond = *pCondition;
D
dapan1121 已提交
1993
        needOutput = true;
X
Xiaoyu Wang 已提交
1994 1995 1996
      }
      break;
  }
D
dapan1121 已提交
1997 1998 1999
  if (needOutput) {
    *pCondition = NULL;
  }
X
Xiaoyu Wang 已提交
2000 2001 2002

  return TSDB_CODE_SUCCESS;
}