nodesTraverseFuncs.c 11.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 "querynodes.h"
17

18 19
typedef enum ETraversalOrder {
  TRAVERSAL_PREORDER = 1,
D
dapan1121 已提交
20 21
  TRAVERSAL_INORDER,
  TRAVERSAL_POSTORDER,
22
} ETraversalOrder;
23

24
static EDealRes walkList(SNodeList* pNodeList, ETraversalOrder order, FNodeWalker walker, void* pContext);
25

26
static EDealRes walkNode(SNode* pNode, ETraversalOrder order, FNodeWalker walker, void* pContext) {
27
  if (NULL == pNode) {
28
    return DEAL_RES_CONTINUE;
29 30
  }

31 32 33 34 35 36 37
  EDealRes res = DEAL_RES_CONTINUE;

  if (TRAVERSAL_PREORDER == order) {
    res = walker(pNode, pContext);
    if (DEAL_RES_CONTINUE != res) {
      return res;
    }
38 39 40 41 42
  }

  switch (nodeType(pNode)) {
    case QUERY_NODE_COLUMN:
    case QUERY_NODE_VALUE:
43
    case QUERY_NODE_LIMIT:
44
      // these node types with no subnodes
45
      break;
46 47
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOpNode = (SOperatorNode*)pNode;
48
      res = walkNode(pOpNode->pLeft, order, walker, pContext);
49
      if (DEAL_RES_ERROR != res) {
50
        res = walkNode(pOpNode->pRight, order, walker, pContext);
51
      }
52
      break;
53 54
    }
    case QUERY_NODE_LOGIC_CONDITION:
55 56
      res = walkList(((SLogicConditionNode*)pNode)->pParameterList, order, walker, pContext);
      break;
57
    case QUERY_NODE_FUNCTION:
58 59
      res = walkList(((SFunctionNode*)pNode)->pParameterList, order, walker, pContext);
      break;
60 61
    case QUERY_NODE_REAL_TABLE:
    case QUERY_NODE_TEMP_TABLE:
62
      break; // todo
63 64
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoinTableNode = (SJoinTableNode*)pNode;
65
      res = walkNode(pJoinTableNode->pLeft, order, walker, pContext);
66
      if (DEAL_RES_ERROR != res) {
67
        res = walkNode(pJoinTableNode->pRight, order, walker, pContext);
68
      }
69
      if (DEAL_RES_ERROR != res) {
70
        res = walkNode(pJoinTableNode->pOnCond, order, walker, pContext);
71
      }
72
      break;
73 74
    }
    case QUERY_NODE_GROUPING_SET:
75 76
      res = walkList(((SGroupingSetNode*)pNode)->pParameterList, order, walker, pContext);
      break;
77
    case QUERY_NODE_ORDER_BY_EXPR:
78 79
      res = walkNode(((SOrderByExprNode*)pNode)->pExpr, order, walker, pContext);
      break;
80
    case QUERY_NODE_STATE_WINDOW:
81
      res = walkNode(((SStateWindowNode*)pNode)->pExpr, order, walker, pContext);
82
      break;
X
Xiaoyu Wang 已提交
83 84 85 86 87 88
    case QUERY_NODE_SESSION_WINDOW: {
      SSessionWindowNode* pSession = (SSessionWindowNode*)pNode;
      res = walkNode(pSession->pCol, order, walker, pContext);
      if (DEAL_RES_ERROR != res) {
        res = walkNode(pSession->pGap, order, walker, pContext);
      }
89
      break;
X
Xiaoyu Wang 已提交
90
    }
91 92 93
    case QUERY_NODE_INTERVAL_WINDOW: {
      SIntervalWindowNode* pInterval = (SIntervalWindowNode*)pNode;
      res = walkNode(pInterval->pInterval, order, walker, pContext);
94
      if (DEAL_RES_ERROR != res) {
95 96
        res = walkNode(pInterval->pOffset, order, walker, pContext);
      }
97
      if (DEAL_RES_ERROR != res) {
98 99
        res = walkNode(pInterval->pSliding, order, walker, pContext);
      }
100
      if (DEAL_RES_ERROR != res) {
101 102
        res = walkNode(pInterval->pFill, order, walker, pContext);
      }
X
bugfix  
Xiaoyu Wang 已提交
103 104 105
      if (DEAL_RES_ERROR != res) {
        res = walkNode(pInterval->pCol, order, walker, pContext);
      }
106 107 108 109 110 111 112 113 114 115 116
      break;
    }
    case QUERY_NODE_NODE_LIST:
      res = walkList(((SNodeListNode*)pNode)->pNodeList, order, walker, pContext);
      break;
    case QUERY_NODE_FILL:
      res = walkNode(((SFillNode*)pNode)->pValues, order, walker, pContext);
      break;
    case QUERY_NODE_RAW_EXPR:
      res = walkNode(((SRawExprNode*)pNode)->pNode, order, walker, pContext);
      break;
D
dapan1121 已提交
117 118 119
    case QUERY_NODE_TARGET:
      res = walkNode(((STargetNode*)pNode)->pExpr, order, walker, pContext);
      break;
120 121 122 123
    default:
      break;
  }

124
  if (DEAL_RES_ERROR != res && TRAVERSAL_POSTORDER == order) {
125 126 127 128 129 130
    res = walker(pNode, pContext);
  }

  return res;
}

131
static EDealRes walkList(SNodeList* pNodeList, ETraversalOrder order, FNodeWalker walker, void* pContext) {
132 133
  SNode* node;
  FOREACH(node, pNodeList) {
134 135
    if (DEAL_RES_ERROR == walkNode(node, order, walker, pContext)) {
      return DEAL_RES_ERROR;
136 137
    }
  }
138
  return DEAL_RES_CONTINUE;
139 140
}

X
Xiaoyu Wang 已提交
141
void nodesWalkExpr(SNodeptr pNode, FNodeWalker walker, void* pContext) {
142 143 144
  (void)walkNode(pNode, TRAVERSAL_PREORDER, walker, pContext);
}

X
Xiaoyu Wang 已提交
145
void nodesWalkExprs(SNodeList* pNodeList, FNodeWalker walker, void* pContext) {
146 147 148
  (void)walkList(pNodeList, TRAVERSAL_PREORDER, walker, pContext);
}

X
Xiaoyu Wang 已提交
149
void nodesWalkExprPostOrder(SNodeptr pNode, FNodeWalker walker, void* pContext) {
150 151 152
  (void)walkNode(pNode, TRAVERSAL_POSTORDER, walker, pContext);
}

X
Xiaoyu Wang 已提交
153
void nodesWalkExprsPostOrder(SNodeList* pList, FNodeWalker walker, void* pContext) {
154
  (void)walkList(pList, TRAVERSAL_POSTORDER, walker, pContext);
155
}
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

static EDealRes rewriteList(SNodeList* pNodeList, ETraversalOrder order, FNodeRewriter rewriter, void* pContext);

static EDealRes rewriteNode(SNode** pRawNode, ETraversalOrder order, FNodeRewriter rewriter, void* pContext) {
  if (NULL == pRawNode || NULL == *pRawNode) {
    return DEAL_RES_CONTINUE;
  }

  EDealRes res = DEAL_RES_CONTINUE;

  if (TRAVERSAL_PREORDER == order) {
    res = rewriter(pRawNode, pContext);
    if (DEAL_RES_CONTINUE != res) {
      return res;
    }
  }

  SNode* pNode = *pRawNode;
  switch (nodeType(pNode)) {
    case QUERY_NODE_COLUMN:
    case QUERY_NODE_VALUE:
    case QUERY_NODE_LIMIT:
      // these node types with no subnodes
      break;
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOpNode = (SOperatorNode*)pNode;
      res = rewriteNode(&(pOpNode->pLeft), order, rewriter, pContext);
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pOpNode->pRight), order, rewriter, pContext);
      }
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
      res = rewriteList(((SLogicConditionNode*)pNode)->pParameterList, order, rewriter, pContext);
      break;
    case QUERY_NODE_FUNCTION:
      res = rewriteList(((SFunctionNode*)pNode)->pParameterList, order, rewriter, pContext);
      break;
    case QUERY_NODE_REAL_TABLE:
    case QUERY_NODE_TEMP_TABLE:
      break; // todo
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoinTableNode = (SJoinTableNode*)pNode;
      res = rewriteNode(&(pJoinTableNode->pLeft), order, rewriter, pContext);
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pJoinTableNode->pRight), order, rewriter, pContext);
      }
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pJoinTableNode->pOnCond), order, rewriter, pContext);
      }
      break;
    }
    case QUERY_NODE_GROUPING_SET:
      res = rewriteList(((SGroupingSetNode*)pNode)->pParameterList, order, rewriter, pContext);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      res = rewriteNode(&(((SOrderByExprNode*)pNode)->pExpr), order, rewriter, pContext);
      break;
    case QUERY_NODE_STATE_WINDOW:
215
      res = rewriteNode(&(((SStateWindowNode*)pNode)->pExpr), order, rewriter, pContext);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
      break;
    case QUERY_NODE_SESSION_WINDOW:
      res = rewriteNode(&(((SSessionWindowNode*)pNode)->pCol), order, rewriter, pContext);
      break;
    case QUERY_NODE_INTERVAL_WINDOW: {
      SIntervalWindowNode* pInterval = (SIntervalWindowNode*)pNode;
      res = rewriteNode(&(pInterval->pInterval), order, rewriter, pContext);
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pInterval->pOffset), order, rewriter, pContext);
      }
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pInterval->pSliding), order, rewriter, pContext);
      }
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pInterval->pFill), order, rewriter, pContext);
      }
X
bugfix  
Xiaoyu Wang 已提交
232 233 234
      if (DEAL_RES_ERROR != res) {
        res = rewriteNode(&(pInterval->pCol), order, rewriter, pContext);
      }
235 236 237 238 239 240 241 242 243 244 245
      break;
    }
    case QUERY_NODE_NODE_LIST:
      res = rewriteList(((SNodeListNode*)pNode)->pNodeList, order, rewriter, pContext);
      break;
    case QUERY_NODE_FILL:
      res = rewriteNode(&(((SFillNode*)pNode)->pValues), order, rewriter, pContext);
      break;
    case QUERY_NODE_RAW_EXPR:
      res = rewriteNode(&(((SRawExprNode*)pNode)->pNode), order, rewriter, pContext);
      break;
D
dapan1121 已提交
246 247 248
    case QUERY_NODE_TARGET:
      res = rewriteNode(&(((STargetNode*)pNode)->pExpr), order, rewriter, pContext);
      break;
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    default:
      break;
  }

  if (DEAL_RES_ERROR != res && TRAVERSAL_POSTORDER == order) {
    res = rewriter(pRawNode, pContext);
  }

  return res;
}

static EDealRes rewriteList(SNodeList* pNodeList, ETraversalOrder order, FNodeRewriter rewriter, void* pContext) {
  SNode** pNode;
  FOREACH_FOR_REWRITE(pNode, pNodeList) {
    if (DEAL_RES_ERROR == rewriteNode(pNode, order, rewriter, pContext)) {
      return DEAL_RES_ERROR;
    }
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
270
void nodesRewriteExpr(SNode** pNode, FNodeRewriter rewriter, void* pContext) {
271 272 273
  (void)rewriteNode(pNode, TRAVERSAL_PREORDER, rewriter, pContext);
}

X
Xiaoyu Wang 已提交
274
void nodesRewriteExprs(SNodeList* pList, FNodeRewriter rewriter, void* pContext) {
275 276 277
  (void)rewriteList(pList, TRAVERSAL_PREORDER, rewriter, pContext);
}

X
Xiaoyu Wang 已提交
278
void nodesRewriteExprPostOrder(SNode** pNode, FNodeRewriter rewriter, void* pContext) {
279 280 281
  (void)rewriteNode(pNode, TRAVERSAL_POSTORDER, rewriter, pContext);
}

X
Xiaoyu Wang 已提交
282
void nodesRewriteExprsPostOrder(SNodeList* pList, FNodeRewriter rewriter, void* pContext) {
283 284
  (void)rewriteList(pList, TRAVERSAL_POSTORDER, rewriter, pContext);
}
X
Xiaoyu Wang 已提交
285 286 287 288 289 290 291 292

void nodesWalkSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeWalker walker, void* pContext) {
  if (NULL == pSelect) {
    return;
  }

  switch (clause) {
    case SQL_CLAUSE_FROM:
X
Xiaoyu Wang 已提交
293 294
      nodesWalkExpr(pSelect->pFromTable, walker, pContext);
      nodesWalkExpr(pSelect->pWhere, walker, pContext);
X
Xiaoyu Wang 已提交
295
    case SQL_CLAUSE_WHERE:
X
Xiaoyu Wang 已提交
296
      nodesWalkExprs(pSelect->pPartitionByList, walker, pContext);
X
Xiaoyu Wang 已提交
297
    case SQL_CLAUSE_PARTITION_BY:
X
Xiaoyu Wang 已提交
298
      nodesWalkExpr(pSelect->pWindow, walker, pContext);
X
Xiaoyu Wang 已提交
299
    case SQL_CLAUSE_WINDOW:
X
Xiaoyu Wang 已提交
300
      nodesWalkExprs(pSelect->pGroupByList, walker, pContext);
X
Xiaoyu Wang 已提交
301
    case SQL_CLAUSE_GROUP_BY:
X
Xiaoyu Wang 已提交
302
      nodesWalkExpr(pSelect->pHaving, walker, pContext);
X
Xiaoyu Wang 已提交
303
    case SQL_CLAUSE_HAVING:
X
Xiaoyu Wang 已提交
304
      nodesWalkExprs(pSelect->pProjectionList, walker, pContext);
X
Xiaoyu Wang 已提交
305
    case SQL_CLAUSE_SELECT:
306
      nodesWalkExprs(pSelect->pOrderByList, walker, pContext);
X
Xiaoyu Wang 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320
    default:
      break;
  }

  return;
}

void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewriter rewriter, void* pContext) {
  if (NULL == pSelect) {
    return;
  }

  switch (clause) {
    case SQL_CLAUSE_FROM:
X
Xiaoyu Wang 已提交
321 322
      nodesRewriteExpr(&(pSelect->pFromTable), rewriter, pContext);
      nodesRewriteExpr(&(pSelect->pWhere), rewriter, pContext);
X
Xiaoyu Wang 已提交
323
    case SQL_CLAUSE_WHERE:
X
Xiaoyu Wang 已提交
324
      nodesRewriteExprs(pSelect->pPartitionByList, rewriter, pContext);
X
Xiaoyu Wang 已提交
325
    case SQL_CLAUSE_PARTITION_BY:
X
Xiaoyu Wang 已提交
326
      nodesRewriteExpr(&(pSelect->pWindow), rewriter, pContext);
X
Xiaoyu Wang 已提交
327
    case SQL_CLAUSE_WINDOW:
X
Xiaoyu Wang 已提交
328
      nodesRewriteExprs(pSelect->pGroupByList, rewriter, pContext);
X
Xiaoyu Wang 已提交
329
    case SQL_CLAUSE_GROUP_BY:
X
Xiaoyu Wang 已提交
330
      nodesRewriteExpr(&(pSelect->pHaving), rewriter, pContext);
X
Xiaoyu Wang 已提交
331
    case SQL_CLAUSE_HAVING:
X
Xiaoyu Wang 已提交
332
      nodesRewriteExprs(pSelect->pProjectionList, rewriter, pContext);
X
Xiaoyu Wang 已提交
333
    case SQL_CLAUSE_SELECT:
X
Xiaoyu Wang 已提交
334
      nodesRewriteExprs(pSelect->pOrderByList, rewriter, pContext);
X
Xiaoyu Wang 已提交
335 336 337 338 339 340
    default:
      break;
  }

  return;
}