• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4996

19 Mar 2026 02:16AM UTC coverage: 72.069% (+0.07%) from 71.996%
#4996

push

travis-ci

web-flow
feat: SQL firewall black/white list (#34798)

461 of 618 new or added lines in 4 files covered. (74.6%)

380 existing lines in 128 files now uncovered.

245359 of 340448 relevant lines covered (72.07%)

135732617.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.32
/source/libs/nodes/src/nodesToSQLFuncs.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "cmdnodes.h"
17
#include "nodesUtil.h"
18
#include "plannodes.h"
19
#include "querynodes.h"
20
#include "taos.h"
21
#include "taoserror.h"
22
#include "thash.h"
23

24
const char *operatorTypeStr(EOperatorType type) {
141,513,693✔
25
  switch (type) {
141,513,693✔
26
    case OP_TYPE_ADD:
211,977✔
27
      return "+";
211,977✔
28
    case OP_TYPE_SUB:
1,632✔
29
      return "-";
1,632✔
30
    case OP_TYPE_MULTI:
1,632✔
31
      return "*";
1,632✔
32
    case OP_TYPE_DIV:
1,428✔
33
      return "/";
1,428✔
34
    case OP_TYPE_REM:
1,428✔
35
      return "%";
1,428✔
36
    case OP_TYPE_MINUS:
1,836✔
37
      return "-";
1,836✔
38
    case OP_TYPE_BIT_AND:
1,632✔
39
      return "&";
1,632✔
40
    case OP_TYPE_BIT_OR:
1,632✔
41
      return "|";
1,632✔
42
    case OP_TYPE_GREATER_THAN:
17,019,327✔
43
      return ">";
17,019,327✔
44
    case OP_TYPE_GREATER_EQUAL:
26,785,952✔
45
      return ">=";
26,785,952✔
46
    case OP_TYPE_LOWER_THAN:
12,707,929✔
47
      return "<";
12,707,929✔
48
    case OP_TYPE_LOWER_EQUAL:
25,446,471✔
49
      return "<=";
25,446,471✔
50
    case OP_TYPE_EQUAL:
29,508,682✔
51
      return "=";
29,508,682✔
52
    case OP_TYPE_NOT_EQUAL:
1,251,526✔
53
      return "<>";
1,251,526✔
54
    case OP_TYPE_IN:
1,215,034✔
55
      return "IN";
1,215,034✔
56
    case OP_TYPE_NOT_IN:
888,884✔
57
      return "NOT IN";
888,884✔
58
    case OP_TYPE_LIKE:
2,415,189✔
59
      return "LIKE";
2,415,189✔
60
    case OP_TYPE_NOT_LIKE:
816✔
61
      return "NOT LIKE";
816✔
62
    case OP_TYPE_MATCH:
10,769,298✔
63
      return "MATCH";
10,769,298✔
64
    case OP_TYPE_NMATCH:
559,364✔
65
      return "NMATCH";
559,364✔
66
    case OP_TYPE_IS_NULL:
3,847,215✔
67
      return "IS NULL";
3,847,215✔
68
    case OP_TYPE_IS_NOT_NULL:
8,574,504✔
69
      return "IS NOT NULL";
8,574,504✔
70
    case OP_TYPE_IS_TRUE:
300,840✔
71
      return "IS TRUE";
300,840✔
72
    case OP_TYPE_IS_FALSE:
×
73
      return "IS FALSE";
×
74
    case OP_TYPE_IS_UNKNOWN:
×
75
      return "IS UNKNOWN";
×
76
    case OP_TYPE_IS_NOT_TRUE:
×
77
      return "IS NOT TRUE";
×
78
    case OP_TYPE_IS_NOT_FALSE:
×
79
      return "IS NOT FALSE";
×
80
    case OP_TYPE_IS_NOT_UNKNOWN:
×
81
      return "IS NOT UNKNOWN";
×
82
    case OP_TYPE_EXISTS:
×
83
      return "EXISTS";
×
84
    case OP_TYPE_NOT_EXISTS:
×
85
      return "NOT EXISTS";
×
86
    case OP_TYPE_JSON_GET_VALUE:
×
87
      return "=>";
×
88
    case OP_TYPE_JSON_CONTAINS:
×
89
      return "CONTAINS";
×
90
    case OP_TYPE_ASSIGN:
×
91
      return "=";
×
92
    default:
×
93
      break;
×
94
  }
95
  return "UNKNOWN";
×
96
}
97

98
const char *operatorTypeStrEx(EOperatorType type, bool isNegative) {
12,235,839✔
99
  if (isNegative) {
12,235,839✔
100
    switch (type) {
96,336✔
101
      case OP_TYPE_IN:
×
102
        return "!= ANY";
×
103
      case OP_TYPE_NOT_IN:
96,336✔
104
        return "= ALL";
96,336✔
105
      default:
×
106
        break;
×
107
    }
108
    return "UNKNOWN";
×
109
  }
110

111
  return operatorTypeStr(type);
12,139,503✔
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
694,025✔
115
  switch (type) {
694,025✔
116
    case LOGIC_COND_TYPE_AND:
453,582✔
117
      return "AND";
453,582✔
118
    case LOGIC_COND_TYPE_OR:
240,443✔
119
      return "OR";
240,443✔
120
    case LOGIC_COND_TYPE_NOT:
×
121
      return "NOT";
×
122
    default:
×
123
      break;
×
124
  }
125
  return "UNKNOWN";
×
126
}
127

128
int32_t nodesNodeToSQL(SNode *pNode, char *buf, int32_t bufSize, int32_t *len) {
11,270,254✔
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
11,270,254✔
130
}
131

132
int32_t nodesNodeToSQLFormat(SNode *pNode, char *buf, int32_t bufSize, int32_t *len, bool longFormat) {
35,794,121✔
133
  switch (pNode->type) {
35,794,121✔
134
    case QUERY_NODE_COLUMN: {
10,428,010✔
135
      SColumnNode *colNode = (SColumnNode *)pNode;
10,428,010✔
136
      if (colNode->dbName[0]) {
10,428,010✔
137
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
9,881,749✔
138
      }
139

140
      if (colNode->tableAlias[0]) {
10,428,010✔
141
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
9,899,484✔
142
      } else if (colNode->tableName[0]) {
528,526✔
143
        *len += snprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->tableAlias[0]) {
10,428,010✔
147
        *len += snprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
9,899,550✔
148
      } else {
149
        *len += snprintf(buf + *len, bufSize - *len, "%s", colNode->node.userAlias);
528,460✔
150
      }
151

152
      return TSDB_CODE_SUCCESS;
10,428,010✔
153
    }
154
    case QUERY_NODE_VALUE: {
4,863,085✔
155
      SValueNode *colNode = (SValueNode *)pNode;
4,863,085✔
156
      char       *t = nodesGetStrValueFromNode(colNode);
4,863,085✔
157
      if (NULL == t) {
4,862,589✔
158
        nodesError("fail to get str value from valueNode");
×
159
        NODES_ERR_RET(TSDB_CODE_APP_ERROR);
×
160
      }
161

162
      int32_t tlen = strlen(t);
4,862,344✔
163
      if (longFormat) {
4,862,344✔
164
        *len += snprintf(buf + *len, bufSize - *len, "%s", t);
4,857,320✔
165
      } else {
166
        if (tlen > 32) {
5,024✔
167
          *len += snprintf(buf + *len, bufSize - *len, "%.*s...%s", 32, t, t + tlen - 1);
×
168
        } else {
169
          *len += snprintf(buf + *len, bufSize - *len, "%s", t);
5,024✔
170
        }
171
      }
172

173
      taosMemoryFree(t);
4,862,344✔
174

175
      return TSDB_CODE_SUCCESS;
4,862,771✔
176
    }
177
    case QUERY_NODE_OPERATOR: {
12,235,437✔
178
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
12,235,437✔
179
      *len += snprintf(buf + *len, bufSize - *len, "(");
12,235,437✔
180
      if (pOpNode->pLeft) {
12,235,437✔
181
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
12,235,657✔
182
      }
183

184
      *len += tsnprintf(buf + *len, bufSize - *len, " %s ", operatorTypeStrEx(pOpNode->opType, pOpNode->flag & OPERATOR_FLAG_NEGATIVE_OP));
12,235,798✔
185

186
      if (pOpNode->pRight) {
12,236,661✔
187
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
10,589,521✔
188
      }
189

190
      *len += snprintf(buf + *len, bufSize - *len, ")");
12,236,777✔
191

192
      return TSDB_CODE_SUCCESS;
12,236,777✔
193
    }
194
    case QUERY_NODE_LOGIC_CONDITION: {
687,775✔
195
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
687,775✔
196
      SNode               *node = NULL;
687,775✔
197
      bool                 first = true;
687,775✔
198

199
      *len += snprintf(buf + *len, bufSize - *len, "(");
687,775✔
200

201
      FOREACH(node, pLogicNode->pParameterList) {
2,069,757✔
202
        if (!first) {
1,381,982✔
203
          *len += snprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
694,116✔
204
        }
205
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
1,381,891✔
206
        first = false;
1,381,982✔
207
      }
208

209
      *len += snprintf(buf + *len, bufSize - *len, ")");
687,775✔
210

211
      return TSDB_CODE_SUCCESS;
687,775✔
212
    }
213
    case QUERY_NODE_FUNCTION: {
185,445✔
214
      SFunctionNode *pFuncNode = (SFunctionNode *)pNode;
185,445✔
215
      SNode         *node = NULL;
185,445✔
216
      bool           first = true;
185,445✔
217

218
      *len += snprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
185,445✔
219

220
      FOREACH(node, pFuncNode->pParameterList) {
412,470✔
221
        if (!first) {
227,025✔
222
          *len += snprintf(buf + *len, bufSize - *len, ", ");
41,580✔
223
        }
224
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
227,025✔
225
        first = false;
227,025✔
226
      }
227

228
      *len += snprintf(buf + *len, bufSize - *len, ")");
185,445✔
229

230
      return TSDB_CODE_SUCCESS;
185,445✔
231
    }
232
    case QUERY_NODE_NODE_LIST: {
7,751✔
233
      SNodeListNode *pListNode = (SNodeListNode *)pNode;
7,751✔
234
      SNode         *node = NULL;
7,751✔
235
      bool           first = true;
7,751✔
236
      int32_t        num = 0;
7,751✔
237

238
      *len += snprintf(buf + *len, bufSize - *len, "(");
7,751✔
239

240
      FOREACH(node, pListNode->pNodeList) {
32,030✔
241
        if (!first) {
24,279✔
242
          *len += snprintf(buf + *len, bufSize - *len, ", ");
16,528✔
243
          if (!longFormat) {
16,528✔
244
            if (++num >= 10) {
×
245
              *len += snprintf(buf + *len, bufSize - *len, "...");
×
246
              break;
×
247
            }
248
          }
249
        }
250
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
24,279✔
251
        first = false;
24,279✔
252
      }
253

254
      *len += snprintf(buf + *len, bufSize - *len, ")");
7,751✔
255

256
      return TSDB_CODE_SUCCESS;
7,751✔
257
    }
258
    case QUERY_NODE_REMOTE_VALUE: {
5,771,339✔
259
      SRemoteValueNode* pRemote = (SRemoteValueNode*)pNode;
5,771,339✔
260
      *len += snprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
5,771,339✔
261
      return TSDB_CODE_SUCCESS;
5,771,339✔
262
    }
263
    case QUERY_NODE_REMOTE_VALUE_LIST: {
1,289,452✔
264
      SRemoteValueListNode* pRemote = (SRemoteValueListNode*)pNode;
1,289,452✔
265
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
1,289,452✔
266
      return TSDB_CODE_SUCCESS;
1,289,609✔
267
    }
268
    case QUERY_NODE_REMOTE_ROW: {
196,976✔
269
      SRemoteRowNode* pRemote = (SRemoteRowNode*)pNode;
196,976✔
270
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
196,976✔
271
      return TSDB_CODE_SUCCESS;
196,976✔
272
    }
273
    case QUERY_NODE_REMOTE_ZERO_ROWS: {
129,112✔
274
      SRemoteZeroRowsNode* pRemote = (SRemoteZeroRowsNode*)pNode;
129,112✔
275
      *len += tsnprintf(buf + *len, bufSize - *len, "$(InitPlan %d)", pRemote->subQIdx + 1);
129,112✔
276
      return TSDB_CODE_SUCCESS;
129,112✔
277
    }
UNCOV
278
    default:
×
UNCOV
279
      break;
×
280
  }
281

UNCOV
282
  nodesError("nodesNodeToSQL unknown node = %s", nodesNodeName(pNode->type));
×
283
  NODES_RET(TSDB_CODE_APP_ERROR);
×
284
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc