nodesTraverseFuncs.c 11.3 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 20 21
typedef enum ETraversalOrder {
  TRAVERSAL_PREORDER = 1,
  TRAVERSAL_POSTORDER
} ETraversalOrder;
22

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

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

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

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

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

120
  if (DEAL_RES_ERROR != res && TRAVERSAL_POSTORDER == order) {
121 122 123 124 125 126
    res = walker(pNode, pContext);
  }

  return res;
}

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

X
Xiaoyu Wang 已提交
137
void nodesWalkNode(SNodeptr pNode, FNodeWalker walker, void* pContext) {
138 139 140
  (void)walkNode(pNode, TRAVERSAL_PREORDER, walker, pContext);
}

141
void nodesWalkList(SNodeList* pNodeList, FNodeWalker walker, void* pContext) {
142 143 144
  (void)walkList(pNodeList, TRAVERSAL_PREORDER, walker, pContext);
}

X
Xiaoyu Wang 已提交
145
void nodesWalkNodePostOrder(SNodeptr pNode, FNodeWalker walker, void* pContext) {
146 147 148
  (void)walkNode(pNode, TRAVERSAL_POSTORDER, walker, pContext);
}

149
void nodesWalkListPostOrder(SNodeList* pList, FNodeWalker walker, void* pContext) {
150
  (void)walkList(pList, TRAVERSAL_POSTORDER, walker, pContext);
151
}
152 153 154 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

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:
      res = rewriteNode(&(((SStateWindowNode*)pNode)->pCol), order, rewriter, pContext);
      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);
      }
      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 已提交
239 240 241
    case QUERY_NODE_TARGET:
      res = rewriteNode(&(((STargetNode*)pNode)->pExpr), order, rewriter, pContext);
      break;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    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;
}

void nodesRewriteNode(SNode** pNode, FNodeRewriter rewriter, void* pContext) {
  (void)rewriteNode(pNode, TRAVERSAL_PREORDER, rewriter, pContext);
}

void nodesRewriteList(SNodeList* pList, FNodeRewriter rewriter, void* pContext) {
  (void)rewriteList(pList, TRAVERSAL_PREORDER, rewriter, pContext);
}

void nodesRewriteNodePostOrder(SNode** pNode, FNodeRewriter rewriter, void* pContext) {
  (void)rewriteNode(pNode, TRAVERSAL_POSTORDER, rewriter, pContext);
}

void nodesRewriteListPostOrder(SNodeList* pList, FNodeRewriter rewriter, void* pContext) {
  (void)rewriteList(pList, TRAVERSAL_POSTORDER, rewriter, pContext);
}
X
Xiaoyu Wang 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

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

  switch (clause) {
    case SQL_CLAUSE_FROM:
      nodesWalkNode(pSelect->pFromTable, walker, pContext);
      nodesWalkNode(pSelect->pWhere, walker, pContext);
    case SQL_CLAUSE_WHERE:
      nodesWalkList(pSelect->pPartitionByList, walker, pContext);
    case SQL_CLAUSE_PARTITION_BY:
      nodesWalkNode(pSelect->pWindow, walker, pContext);
    case SQL_CLAUSE_WINDOW:
      nodesWalkList(pSelect->pGroupByList, walker, pContext);
    case SQL_CLAUSE_GROUP_BY:
      nodesWalkNode(pSelect->pHaving, walker, pContext);
    case SQL_CLAUSE_HAVING:
      nodesWalkList(pSelect->pProjectionList, walker, pContext);
    case SQL_CLAUSE_SELECT:
      nodesWalkList(pSelect->pOrderByList, walker, pContext);
    case SQL_CLAUSE_ORDER_BY:
    default:
      break;
  }

  return;
}

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

  switch (clause) {
    case SQL_CLAUSE_FROM:
      nodesRewriteNode(&(pSelect->pFromTable), rewriter, pContext);
      nodesRewriteNode(&(pSelect->pWhere), rewriter, pContext);
    case SQL_CLAUSE_WHERE:
      nodesRewriteList(pSelect->pPartitionByList, rewriter, pContext);
    case SQL_CLAUSE_PARTITION_BY:
      nodesRewriteNode(&(pSelect->pWindow), rewriter, pContext);
    case SQL_CLAUSE_WINDOW:
      nodesRewriteList(pSelect->pGroupByList, rewriter, pContext);
    case SQL_CLAUSE_GROUP_BY:
      nodesRewriteNode(&(pSelect->pHaving), rewriter, pContext);
    case SQL_CLAUSE_HAVING:
      nodesRewriteList(pSelect->pProjectionList, rewriter, pContext);
    case SQL_CLAUSE_SELECT:
      nodesRewriteList(pSelect->pOrderByList, rewriter, pContext);
    default:
      break;
  }

  return;
}