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

X
Xiaoyu Wang 已提交
24 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;
}

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

X
Xiaoyu Wang 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
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);
310
  nodesDestroyNode(pNode->pTspk);
X
Xiaoyu Wang 已提交
311 312 313 314 315 316 317
}

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

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

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

  switch (nodeType(pNode)) {
X
Xiaoyu Wang 已提交
326
    case QUERY_NODE_COLUMN:  // pProjectRef is weak reference, no need to release
X
Xiaoyu Wang 已提交
327
      break;
D
dapan1121 已提交
328
    case QUERY_NODE_VALUE: {
X
Xiaoyu Wang 已提交
329
      SValueNode* pValue = (SValueNode*)pNode;
wafwerar's avatar
wafwerar 已提交
330
      taosMemoryFreeClear(pValue->literal);
D
dapan1121 已提交
331
      if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
wafwerar's avatar
wafwerar 已提交
332
        taosMemoryFreeClear(pValue->datum.p);
D
dapan1121 已提交
333
      }
X
Xiaoyu Wang 已提交
334 335 336 337 338 339
      break;
    }
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOp = (SOperatorNode*)pNode;
      nodesDestroyNode(pOp->pLeft);
      nodesDestroyNode(pOp->pRight);
D
dapan1121 已提交
340 341 342
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
343
      nodesDestroyList(((SLogicConditionNode*)pNode)->pParameterList);
D
dapan1121 已提交
344 345
      break;
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
346
      nodesDestroyList(((SFunctionNode*)pNode)->pParameterList);
X
Xiaoyu Wang 已提交
347 348
      break;
    case QUERY_NODE_REAL_TABLE: {
X
Xiaoyu Wang 已提交
349
      SRealTableNode* pReal = (SRealTableNode*)pNode;
wafwerar's avatar
wafwerar 已提交
350 351
      taosMemoryFreeClear(pReal->pMeta);
      taosMemoryFreeClear(pReal->pVgroupList);
X
Xiaoyu Wang 已提交
352 353 354
      break;
    }
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
355
      nodesDestroyNode(((STempTableNode*)pNode)->pSubquery);
D
dapan1121 已提交
356
      break;
X
Xiaoyu Wang 已提交
357 358 359 360 361
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoin = (SJoinTableNode*)pNode;
      nodesDestroyNode(pJoin->pLeft);
      nodesDestroyNode(pJoin->pRight);
      nodesDestroyNode(pJoin->pOnCond);
D
dapan1121 已提交
362
      break;
X
Xiaoyu Wang 已提交
363
    }
D
dapan1121 已提交
364
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
365 366 367 368 369
      nodesDestroyList(((SGroupingSetNode*)pNode)->pParameterList);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      nodesDestroyNode(((SOrderByExprNode*)pNode)->pExpr);
      break;
X
Xiaoyu Wang 已提交
370
    case QUERY_NODE_LIMIT:  // no pointer field
D
dapan1121 已提交
371
      break;
X
Xiaoyu Wang 已提交
372
    case QUERY_NODE_STATE_WINDOW:
373
      nodesDestroyNode(((SStateWindowNode*)pNode)->pExpr);
X
Xiaoyu Wang 已提交
374 375 376 377 378
      break;
    case QUERY_NODE_SESSION_WINDOW: {
      SSessionWindowNode* pSession = (SSessionWindowNode*)pNode;
      nodesDestroyNode(pSession->pCol);
      nodesDestroyNode(pSession->pGap);
D
dapan1121 已提交
379
      break;
X
Xiaoyu Wang 已提交
380 381 382 383 384 385 386 387 388 389
    }
    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 已提交
390
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
391 392
      nodesDestroyList(((SNodeListNode*)pNode)->pNodeList);
      break;
X
Xiaoyu Wang 已提交
393 394 395 396
    case QUERY_NODE_FILL: {
      SFillNode* pFill = (SFillNode*)pNode;
      nodesDestroyNode(pFill->pValues);
      nodesDestroyNode(pFill->pWStartTs);
X
Xiaoyu Wang 已提交
397
      break;
X
Xiaoyu Wang 已提交
398
    }
X
Xiaoyu Wang 已提交
399 400
    case QUERY_NODE_RAW_EXPR:
      nodesDestroyNode(((SRawExprNode*)pNode)->pNode);
X
Xiaoyu Wang 已提交
401
      break;
X
Xiaoyu Wang 已提交
402 403 404 405 406 407
    case QUERY_NODE_TARGET:
      nodesDestroyNode(((STargetNode*)pNode)->pExpr);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      nodesDestroyList(((SDataBlockDescNode*)pNode)->pSlots);
      break;
X
Xiaoyu Wang 已提交
408 409 410
    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 已提交
411
      break;
X
Xiaoyu Wang 已提交
412 413 414 415 416
    case QUERY_NODE_DATABASE_OPTIONS: {
      SDatabaseOptions* pOptions = (SDatabaseOptions*)pNode;
      nodesDestroyNode(pOptions->pDaysPerFile);
      nodesDestroyList(pOptions->pKeep);
      nodesDestroyList(pOptions->pRetentions);
X
Xiaoyu Wang 已提交
417
      break;
X
Xiaoyu Wang 已提交
418
    }
X
Xiaoyu Wang 已提交
419
    case QUERY_NODE_TABLE_OPTIONS: {
X
Xiaoyu Wang 已提交
420 421 422
      STableOptions* pOptions = (STableOptions*)pNode;
      nodesDestroyList(pOptions->pSma);
      nodesDestroyList(pOptions->pRollupFuncs);
X
Xiaoyu Wang 已提交
423
      break;
X
Xiaoyu Wang 已提交
424
    }
425
    case QUERY_NODE_INDEX_OPTIONS: {
X
Xiaoyu Wang 已提交
426 427 428 429 430
      SIndexOptions* pOptions = (SIndexOptions*)pNode;
      nodesDestroyList(pOptions->pFuncs);
      nodesDestroyNode(pOptions->pInterval);
      nodesDestroyNode(pOptions->pOffset);
      nodesDestroyNode(pOptions->pSliding);
431 432
      break;
    }
X
Xiaoyu Wang 已提交
433 434 435 436 437 438 439 440
    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 已提交
441
    case QUERY_NODE_SELECT_STMT: {
X
Xiaoyu Wang 已提交
442
      SSelectStmt* pStmt = (SSelectStmt*)pNode;
X
Xiaoyu Wang 已提交
443 444 445 446 447 448 449 450 451 452 453 454
      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 已提交
455 456 457 458 459 460
    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 已提交
461
    case QUERY_NODE_DROP_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
462 463 464
      break;
    case QUERY_NODE_ALTER_DATABASE_STMT:
      nodesDestroyNode(((SAlterDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
465
      break;
X
Xiaoyu Wang 已提交
466
    case QUERY_NODE_CREATE_TABLE_STMT: {
X
Xiaoyu Wang 已提交
467
      SCreateTableStmt* pStmt = (SCreateTableStmt*)pNode;
X
Xiaoyu Wang 已提交
468 469
      nodesDestroyList(pStmt->pCols);
      nodesDestroyList(pStmt->pTags);
X
Xiaoyu Wang 已提交
470
      nodesDestroyNode(pStmt->pOptions);
X
Xiaoyu Wang 已提交
471 472 473
      break;
    }
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE: {
X
Xiaoyu Wang 已提交
474
      SCreateSubTableClause* pStmt = (SCreateSubTableClause*)pNode;
X
Xiaoyu Wang 已提交
475 476 477 478 479
      nodesDestroyList(pStmt->pSpecificTags);
      nodesDestroyList(pStmt->pValsOfTags);
      break;
    }
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
X
Xiaoyu Wang 已提交
480 481
      nodesDestroyList(((SCreateMultiTableStmt*)pNode)->pSubTables);
      break;
X
Xiaoyu Wang 已提交
482
    case QUERY_NODE_DROP_TABLE_CLAUSE:  // no pointer field
X
Xiaoyu Wang 已提交
483 484 485 486
      break;
    case QUERY_NODE_DROP_TABLE_STMT:
      nodesDestroyNode(((SDropTableStmt*)pNode)->pTables);
      break;
X
Xiaoyu Wang 已提交
487
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
488 489 490 491 492 493 494
      break;
    case QUERY_NODE_ALTER_TABLE_STMT: {
      SAlterTableStmt* pStmt = (SAlterTableStmt*)pNode;
      nodesDestroyNode(pStmt->pOptions);
      nodesDestroyNode(pStmt->pVal);
      break;
    }
X
Xiaoyu Wang 已提交
495 496 497 498 499 500 501
    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
502
      break;
503
    case QUERY_NODE_CREATE_INDEX_STMT: {
X
Xiaoyu Wang 已提交
504
      SCreateIndexStmt* pStmt = (SCreateIndexStmt*)pNode;
505 506 507 508
      nodesDestroyNode(pStmt->pOptions);
      nodesDestroyList(pStmt->pCols);
      break;
    }
X
Xiaoyu Wang 已提交
509 510 511
    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 已提交
512 513 514 515
      break;
    case QUERY_NODE_CREATE_TOPIC_STMT:
      nodesDestroyNode(((SCreateTopicStmt*)pNode)->pQuery);
      break;
X
Xiaoyu Wang 已提交
516 517
    case QUERY_NODE_DROP_TOPIC_STMT:   // no pointer field
    case QUERY_NODE_ALTER_LOCAL_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
      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;
    }
536 537 538 539 540 541 542 543 544 545 546 547
    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 已提交
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 577 578
    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 已提交
579
      // pVgDataBlocks is weak reference
X
Xiaoyu Wang 已提交
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 606 607
      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 已提交
608
      nodesClearList(pSubplan->pParents);
X
Xiaoyu Wang 已提交
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 662 663
      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 已提交
664
    case QUERY_NODE_PHYSICAL_PLAN_INTERVAL:
X
Xiaoyu Wang 已提交
665
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
X
Xiaoyu Wang 已提交
666
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
X
Xiaoyu Wang 已提交
667 668
      break;
    case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW:
5
54liuyao 已提交
669
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION_WINDOW:
X
Xiaoyu Wang 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
      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 已提交
686 687 688 689 690 691 692
      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 已提交
693
        bool   first = true;
X
bugfix  
Xiaoyu Wang 已提交
694 695 696 697 698 699 700 701 702 703 704 705
        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 已提交
706 707
      break;
    }
708 709 710
    default:
      break;
  }
X
Xiaoyu Wang 已提交
711 712
  taosMemoryFreeClear(pNode);
  return;
713 714
}

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

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

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

X
Xiaoyu Wang 已提交
757 758 759 760 761 762 763 764 765 766 767
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 已提交
768 769 770 771 772 773 774 775 776 777 778
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 已提交
779
int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) {
X
Xiaoyu Wang 已提交
780
  if (NULL == pTarget || NULL == pSrc) {
X
Xiaoyu Wang 已提交
781
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
782 783 784 785 786 787 788 789 790 791
  }

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

X
Xiaoyu Wang 已提交
796 797 798
  return TSDB_CODE_SUCCESS;
}

799 800 801 802 803 804 805 806 807 808 809
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 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
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;
}

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

X
Xiaoyu Wang 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
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 已提交
865
SNodeptr nodesListGetNode(SNodeList* pList, int32_t index) {
866 867 868 869 870 871 872 873 874
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return node;
    }
  }
  return NULL;
}

875
void nodesDestroyList(SNodeList* pList) {
876 877 878
  if (NULL == pList) {
    return;
  }
X
Xiaoyu Wang 已提交
879

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

X
Xiaoyu Wang 已提交
887 888 889 890 891 892 893 894 895
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 已提交
896
    taosMemoryFreeClear(tmp);
X
Xiaoyu Wang 已提交
897
  }
wafwerar's avatar
wafwerar 已提交
898
  taosMemoryFreeClear(pList);
X
Xiaoyu Wang 已提交
899 900
}

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

  return NULL;
}

X
Xiaoyu Wang 已提交
927
int32_t nodesSetValueNodeValue(SValueNode* pNode, void* value) {
D
dapan1121 已提交
928 929 930
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
      pNode->datum.b = *(bool*)value;
D
dapan1121 已提交
931
      *(bool*)&pNode->typeData = pNode->datum.b;
D
dapan1121 已提交
932 933 934
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pNode->datum.i = *(int8_t*)value;
D
dapan1121 已提交
935
      *(int8_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
936 937 938
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pNode->datum.i = *(int16_t*)value;
D
dapan1121 已提交
939
      *(int16_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
940 941 942
      break;
    case TSDB_DATA_TYPE_INT:
      pNode->datum.i = *(int32_t*)value;
D
dapan1121 已提交
943
      *(int32_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
944 945 946
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
947
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
948 949 950
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
951
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
952 953 954
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pNode->datum.u = *(int8_t*)value;
D
dapan1121 已提交
955
      *(int8_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
956 957 958
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pNode->datum.u = *(int16_t*)value;
D
dapan1121 已提交
959
      *(int16_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
960 961 962
      break;
    case TSDB_DATA_TYPE_UINT:
      pNode->datum.u = *(int32_t*)value;
D
dapan1121 已提交
963
      *(int32_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
964 965 966
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pNode->datum.u = *(uint64_t*)value;
D
dapan1121 已提交
967
      *(uint64_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
968 969 970
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pNode->datum.d = *(float*)value;
D
dapan1121 已提交
971
      *(float*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
972 973 974
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pNode->datum.d = *(double*)value;
D
dapan1121 已提交
975
      *(double*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
976 977 978 979 980 981 982 983 984 985 986 987 988
      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 已提交
989
char* nodesGetStrValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
990
  switch (pNode->node.resType.type) {
D
dapan1121 已提交
991
    case TSDB_DATA_TYPE_BOOL: {
X
Xiaoyu Wang 已提交
992
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
993 994 995
      if (NULL == buf) {
        return NULL;
      }
996

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

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

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

D
dapan1121 已提交
1032 1033 1034 1035 1036 1037
      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 已提交
1038
      int32_t bufSize = varDataLen(pNode->datum.p) + 2 + 1;
X
Xiaoyu Wang 已提交
1039
      void*   buf = taosMemoryMalloc(bufSize);
D
dapan1121 已提交
1040 1041 1042
      if (NULL == buf) {
        return NULL;
      }
1043

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

  return NULL;
}

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

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

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
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;
}
1091

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

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

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

1154
static EDealRes doCollect(SCollectColumnsCxt* pCxt, SColumnNode* pCol, SNode* pNode) {
X
Xiaoyu Wang 已提交
1155
  char    name[TSDB_TABLE_NAME_LEN + TSDB_COL_NAME_LEN];
1156 1157 1158 1159 1160 1161 1162
  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 已提交
1163
    if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1164
      pCxt->errCode = nodesListStrictAppend(pCxt->pCols, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1165 1166 1167 1168 1169 1170
    }
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

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

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

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

X
Xiaoyu Wang 已提交
1217 1218 1219 1220
  return TSDB_CODE_SUCCESS;
}

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

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

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

1240 1241
  SCollectFuncsCxt cxt = {
      .errCode = TSDB_CODE_SUCCESS, .classifier = classifier, .pFuncs = (NULL == *pFuncs ? nodesMakeList() : *pFuncs)};
X
Xiaoyu Wang 已提交
1242 1243 1244
  if (NULL == cxt.pFuncs) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1245
  *pFuncs = NULL;
X
Xiaoyu Wang 已提交
1246
  nodesWalkSelectStmt(pSelect, clause, collectFuncs, &cxt);
X
Xiaoyu Wang 已提交
1247 1248 1249 1250
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
    nodesDestroyList(cxt.pFuncs);
    return cxt.errCode;
  }
X
Xiaoyu Wang 已提交
1251 1252 1253 1254 1255
  if (LIST_LENGTH(cxt.pFuncs) > 0) {
    *pFuncs = cxt.pFuncs;
  } else {
    nodesDestroyList(cxt.pFuncs);
  }
X
Xiaoyu Wang 已提交
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 1294 1295 1296
  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 已提交
1297 1298
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1299

X
Xiaoyu Wang 已提交
1300
char* getFillModeString(EFillMode mode) {
D
dapan1121 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
  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 已提交
1319
char* nodesGetNameFromColumnNode(SNode* pNode) {
D
dapan1121 已提交
1320 1321 1322
  if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) {
    return "NULL";
  }
X
Xiaoyu Wang 已提交
1323 1324

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

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

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

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

  return num;
}

D
dapan1121 已提交
1348 1349 1350 1351 1352
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 已提交
1353
      break;
D
dapan1121 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
    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 已提交
1377 1378 1379
      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 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388
      break;
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }
}