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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

22.68
/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) {
846✔
25
  switch (type) {
846✔
26
    case OP_TYPE_ADD:
48✔
27
      return "+";
48✔
28
    case OP_TYPE_SUB:
48✔
29
      return "-";
48✔
30
    case OP_TYPE_MULTI:
48✔
31
      return "*";
48✔
32
    case OP_TYPE_DIV:
42✔
33
      return "/";
42✔
34
    case OP_TYPE_REM:
42✔
35
      return "%";
42✔
36
    case OP_TYPE_MINUS:
54✔
37
      return "-";
54✔
38
    case OP_TYPE_BIT_AND:
48✔
39
      return "&";
48✔
40
    case OP_TYPE_BIT_OR:
48✔
41
      return "|";
48✔
42
    case OP_TYPE_GREATER_THAN:
48✔
43
      return ">";
48✔
44
    case OP_TYPE_GREATER_EQUAL:
48✔
45
      return ">=";
48✔
46
    case OP_TYPE_LOWER_THAN:
48✔
47
      return "<";
48✔
48
    case OP_TYPE_LOWER_EQUAL:
48✔
49
      return "<=";
48✔
50
    case OP_TYPE_EQUAL:
48✔
51
      return "=";
48✔
52
    case OP_TYPE_NOT_EQUAL:
48✔
53
      return "<>";
48✔
54
    case OP_TYPE_IN:
×
55
      return "IN";
×
56
    case OP_TYPE_NOT_IN:
×
57
      return "NOT IN";
×
58
    case OP_TYPE_LIKE:
24✔
59
      return "LIKE";
24✔
60
    case OP_TYPE_NOT_LIKE:
24✔
61
      return "NOT LIKE";
24✔
62
    case OP_TYPE_MATCH:
24✔
63
      return "MATCH";
24✔
64
    case OP_TYPE_NMATCH:
24✔
65
      return "NMATCH";
24✔
66
    case OP_TYPE_IS_NULL:
30✔
67
      return "IS NULL";
30✔
68
    case OP_TYPE_IS_NOT_NULL:
30✔
69
      return "IS NOT NULL";
30✔
70
    case OP_TYPE_IS_TRUE:
24✔
71
      return "IS TRUE";
24✔
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) {
×
99
  if (isNegative) {
×
100
    switch (type) {
×
101
      case OP_TYPE_IN:
×
102
        return "!= ANY";
×
103
      case OP_TYPE_NOT_IN:
×
104
        return "= ALL";
×
105
      default:
×
106
        break;
×
107
    }
108
    return "UNKNOWN";
×
109
  }
110

111
  return operatorTypeStr(type);
×
112
}
113

114
const char *logicConditionTypeStr(ELogicConditionType type) {
×
115
  switch (type) {
×
116
    case LOGIC_COND_TYPE_AND:
×
117
      return "AND";
×
118
    case LOGIC_COND_TYPE_OR:
×
119
      return "OR";
×
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) {
×
129
  return nodesNodeToSQLFormat(pNode, buf, bufSize, len, false);
×
130
}
131

132
int32_t nodesNodeToSQLFormat(SNode *pNode, char *buf, int32_t bufSize, int32_t *len, bool longFormat) {
×
133
  switch (pNode->type) {
×
134
    case QUERY_NODE_COLUMN: {
×
135
      SColumnNode *colNode = (SColumnNode *)pNode;
×
136
      if (colNode->dbName[0]) {
×
137
        *len += tsnprintf(buf + *len, bufSize - *len, "`%s`.", colNode->dbName);
×
138
      }
139

140
      if (colNode->tableAlias[0]) {
×
141
        *len += tsnprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableAlias);
×
142
      } else if (colNode->tableName[0]) {
×
143
        *len += tsnprintf(buf + *len, bufSize - *len, "`%s`.", colNode->tableName);
×
144
      }
145

146
      if (colNode->tableAlias[0]) {
×
147
        *len += tsnprintf(buf + *len, bufSize - *len, "`%s`", colNode->node.userAlias);
×
148
      } else {
149
        *len += tsnprintf(buf + *len, bufSize - *len, "%s", colNode->node.userAlias);
×
150
      }
151

152
      return TSDB_CODE_SUCCESS;
×
153
    }
154
    case QUERY_NODE_VALUE: {
×
155
      SValueNode *colNode = (SValueNode *)pNode;
×
156
      char       *t = nodesGetStrValueFromNode(colNode);
×
157
      if (NULL == t) {
×
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);
×
163
      if (longFormat) {
×
164
        *len += tsnprintf(buf + *len, bufSize - *len, "%s", t);
×
165
      } else {
166
        if (tlen > 32) {
×
167
          *len += tsnprintf(buf + *len, bufSize - *len, "%.*s...%s", 32, t, t + tlen - 1);
×
168
        } else {
169
          *len += tsnprintf(buf + *len, bufSize - *len, "%s", t);
×
170
        }
171
      }
172

173
      taosMemoryFree(t);
×
174

175
      return TSDB_CODE_SUCCESS;
×
176
    }
177
    case QUERY_NODE_OPERATOR: {
×
178
      SOperatorNode *pOpNode = (SOperatorNode *)pNode;
×
179
      *len += tsnprintf(buf + *len, bufSize - *len, "(");
×
180
      if (pOpNode->pLeft) {
×
181
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pLeft, buf, bufSize, len, true));
×
182
      }
183

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

186
      if (pOpNode->pRight) {
×
187
        NODES_ERR_RET(nodesNodeToSQLFormat(pOpNode->pRight, buf, bufSize, len, true));
×
188
      }
189

190
      *len += tsnprintf(buf + *len, bufSize - *len, ")");
×
191

192
      return TSDB_CODE_SUCCESS;
×
193
    }
194
    case QUERY_NODE_LOGIC_CONDITION: {
×
195
      SLogicConditionNode *pLogicNode = (SLogicConditionNode *)pNode;
×
196
      SNode               *node = NULL;
×
197
      bool                 first = true;
×
198

199
      *len += tsnprintf(buf + *len, bufSize - *len, "(");
×
200

201
      FOREACH(node, pLogicNode->pParameterList) {
×
202
        if (!first) {
×
203
          *len += tsnprintf(buf + *len, bufSize - *len, " %s ", logicConditionTypeStr(pLogicNode->condType));
×
204
        }
205
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
×
206
        first = false;
×
207
      }
208

209
      *len += tsnprintf(buf + *len, bufSize - *len, ")");
×
210

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

218
      *len += tsnprintf(buf + *len, bufSize - *len, "%s(", pFuncNode->functionName);
×
219

220
      FOREACH(node, pFuncNode->pParameterList) {
×
221
        if (!first) {
×
222
          *len += tsnprintf(buf + *len, bufSize - *len, ", ");
×
223
        }
224
        NODES_ERR_RET(nodesNodeToSQLFormat(node, buf, bufSize, len, true));
×
225
        first = false;
×
226
      }
227

228
      *len += tsnprintf(buf + *len, bufSize - *len, ")");
×
229

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

238
      *len += tsnprintf(buf + *len, bufSize - *len, "(");
×
239

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

254
      *len += tsnprintf(buf + *len, bufSize - *len, ")");
×
255

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

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