nodesMatchFuncs.c 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * 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/>.
 */

#include "querynodes.h"

#define MATCH_SCALAR_FIELD(fldname)           \
  do {                                          \
    if (p->fldname != pSub->fldname) return false; \
  } while (0)

#define MATCH_STRING(a, b) (((a) != NULL && (b) != NULL) ? (strcmp((a), (b)) == 0) : (a) == (b))

#define MATCH_VARDATA(a, b)                                                                                   \
  (((a) != NULL && (b) != NULL)                                                                                 \
       ? (varDataLen((a)) == varDataLen((b)) && memcmp(varDataVal((a)), varDataVal((b)), varDataLen((a))) == 0) \
       : (a) == (b))

#define MATCH_STRING_FIELD(fldname)                          \
  do {                                                         \
    if (!MATCH_STRING(p->fldname, pSub->fldname)) return false; \
  } while (0)

#define MATCH_VARDATA_FIELD(fldname)                          \
  do {                                                          \
    if (!MATCH_VARDATA(p->fldname, pSub->fldname)) return false; \
  } while (0)

#define MATCH_OBJECT_FIELD(fldname, matchFunc)          \
  do {                                                    \
    if (!matchFunc(p->fldname, pSub->fldname)) return false; \
  } while (0)

#define MATCH_NODE_FIELD(fldname)                            \
  do {                                                         \
    if (!nodesMatchNode(pSub->fldname, p->fldname)) return false; \
  } while (0)

#define MATCH_NODE_LIST_FIELD(fldname)                          \
  do {                                                            \
    if (!nodesListMatch(p->fldname, pSub->fldname)) return false; \
  } while (0)

    
bool nodesListMatchExists(const SNodeList* pList, const SNode* pTarget) {
  if (NULL == pList || NULL == pTarget) {
    return false;
  }
  SNode* node = NULL;
  bool exists = false;
  FOREACH(node, pList) {
    if (nodesMatchNode(node, pTarget)) {
      exists = true;
      break;
    }
  }

  return exists;
}

bool nodesListMatch(const SNodeList* pList, const SNodeList* pSubList) {
  if (pList == pSubList) {
    return true;
  }

  if (NULL == pList || NULL == pSubList) {
    return false;
  }

  if (pList->length != pSubList->length) {
    return false;
  }

  SNode* node = NULL;
86
  bool match = true;
87 88
  FOREACH(node, pList) {
    if (!nodesListMatchExists(pSubList, node)) {
89
      match = false;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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
      break;
    }
  }
  return match;
}

static bool columnNodeMatch(const SColumnNode* pSub, const SColumnNode* p) {
  if (0 == strcmp(p->colName, pSub->node.aliasName)) {
    return true;
  }
  return false;
}

static bool valueNodeMatch(const SValueNode* pSub, const SValueNode* p) {
  return nodesEqualNode((SNode*)pSub, (SNode*)p);
}

static bool operatorNodeMatch(const SOperatorNode* pSub, const SOperatorNode* p) {
  MATCH_SCALAR_FIELD(opType);
  MATCH_NODE_FIELD(pLeft);
  MATCH_NODE_FIELD(pRight);
  return true;
}

static bool logicConditionNodeMatch(const SLogicConditionNode* pSub, const SLogicConditionNode* p) {
  MATCH_SCALAR_FIELD(condType);
  MATCH_NODE_LIST_FIELD(pParameterList);
  return true;
}

static bool functionNodeMatch(const SFunctionNode* pSub, const SFunctionNode* p) {
  MATCH_SCALAR_FIELD(funcId);
  MATCH_STRING_FIELD(functionName);
  MATCH_NODE_LIST_FIELD(pParameterList);
  return true;
}

static bool whenThenNodeMatch(const SWhenThenNode* pSub, const SWhenThenNode* p) {
  MATCH_NODE_FIELD(pWhen);
  MATCH_NODE_FIELD(pThen);
  return true;
}

static bool caseWhenNodeMatch(const SCaseWhenNode* pSub, const SCaseWhenNode* p) {
  MATCH_NODE_FIELD(pCase);
  MATCH_NODE_FIELD(pElse);
  MATCH_NODE_LIST_FIELD(pWhenThenList);
  return true;
}

bool nodesMatchNode(const SNode* pSub, const SNode* p) {
  if (pSub == p) {
    return true;
  }

  if (NULL == pSub || NULL == p) {
    return false;
  }

  if (nodeType(pSub) != nodeType(p)) {
    return false;
  }

  switch (nodeType(p)) {
    case QUERY_NODE_COLUMN:
      return columnNodeMatch((const SColumnNode*)pSub, (const SColumnNode*)p);
    case QUERY_NODE_VALUE:
      return valueNodeMatch((const SValueNode*)pSub, (const SValueNode*)p);
    case QUERY_NODE_OPERATOR:
      return operatorNodeMatch((const SOperatorNode*)pSub, (const SOperatorNode*)p);
    case QUERY_NODE_LOGIC_CONDITION:
      return logicConditionNodeMatch((const SLogicConditionNode*)pSub, (const SLogicConditionNode*)p);
    case QUERY_NODE_FUNCTION:
      return functionNodeMatch((const SFunctionNode*)pSub, (const SFunctionNode*)p);
    case QUERY_NODE_WHEN_THEN:
      return whenThenNodeMatch((const SWhenThenNode*)pSub, (const SWhenThenNode*)p);
    case QUERY_NODE_CASE_WHEN:
      return caseWhenNodeMatch((const SCaseWhenNode*)pSub, (const SCaseWhenNode*)p);
    case QUERY_NODE_REAL_TABLE:
    case QUERY_NODE_TEMP_TABLE:
    case QUERY_NODE_JOIN_TABLE:
    case QUERY_NODE_GROUPING_SET:
    case QUERY_NODE_ORDER_BY_EXPR:
    case QUERY_NODE_LIMIT:
      return false;
    default:
      break;
  }

  return false;
}