parSelectTest.cpp 15.7 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
/*
 * 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 "parTestUtil.h"

using namespace std;

namespace ParserTest {

class ParserSelectTest : public ParserTestBase {};

TEST_F(ParserSelectTest, basic) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
27
  run("SELECT * FROM t1");
28

X
Xiaoyu Wang 已提交
29
  run("SELECT * FROM test.t1");
30

X
Xiaoyu Wang 已提交
31
  run("SELECT ts, c1 FROM t1");
32

X
Xiaoyu Wang 已提交
33
  run("SELECT ts, t.c1 FROM (SELECT * FROM t1) t");
34

X
Xiaoyu Wang 已提交
35
  run("SELECT * FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1");
X
Xiaoyu Wang 已提交
36 37

  run("SELECT * FROM st1");
38 39 40 41 42
}

TEST_F(ParserSelectTest, constant) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
43
  run("SELECT 123, 20.4, 'abc', \"wxy\", timestamp '2022-02-09 17:30:20', true, false, 10s FROM t1");
44

X
Xiaoyu Wang 已提交
45 46
  run("SELECT 1234567890123456789012345678901234567890, 20.1234567890123456789012345678901234567890, 'abc', \"wxy\", "
      "timestamp '2022-02-09 17:30:20', true, false, 15s FROM t1");
47

X
Xiaoyu Wang 已提交
48
  run("SELECT 123 + 45 FROM t1 WHERE 2 - 1");
49 50

  run("SELECT * FROM t1 WHERE -2");
51 52 53 54 55
}

TEST_F(ParserSelectTest, expression) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
56
  run("SELECT ts + 10s, c1 + 10, concat(c2, 'abc') FROM t1");
57

X
Xiaoyu Wang 已提交
58
  run("SELECT ts > 0, c1 < 20 and c2 = 'qaz' FROM t1");
59

X
Xiaoyu Wang 已提交
60
  run("SELECT ts > 0, c1 between 10 and 20 and c2 = 'qaz' FROM t1");
X
Xiaoyu Wang 已提交
61 62

  run("SELECT c1 | 10, c2 & 20, c4 | c5 FROM t1");
X
Xiaoyu Wang 已提交
63 64 65

  run("SELECT CASE WHEN ts > '2020-1-1 10:10:10' THEN c1 + 10 ELSE c1 - 10 END FROM t1 "
      "WHERE CASE c1 WHEN c3 + 20 THEN c3 - 1 WHEN c3 + 10 THEN c3 - 2 ELSE 10 END > 0");
66 67 68 69 70
}

TEST_F(ParserSelectTest, condition) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
71
  run("SELECT c1 FROM t1 WHERE ts in (true, false)");
72

X
Xiaoyu Wang 已提交
73 74
  run("SELECT c1 FROM t1 WHERE NOT ts in (true, false)");

X
Xiaoyu Wang 已提交
75
  run("SELECT * FROM t1 WHERE c1 > 10 and c1 is not null");
X
Xiaoyu Wang 已提交
76 77

  run("SELECT * FROM t1 WHERE TBNAME like 'fda%' or TS > '2021-05-05 18:19:01.000'");
78 79 80 81 82
}

TEST_F(ParserSelectTest, pseudoColumn) {
  useDb("root", "test");

83
  run("SELECT _WSTART, _WEND, COUNT(*) FROM t1 INTERVAL(10s)");
84 85
}

86 87 88
TEST_F(ParserSelectTest, pseudoColumnSemanticCheck) {
  useDb("root", "test");

89
  run("SELECT TBNAME FROM (SELECT * FROM st1s1)", TSDB_CODE_PAR_INVALID_TBNAME, PARSER_STAGE_TRANSLATE);
90 91
}

92 93 94 95 96 97
TEST_F(ParserSelectTest, aggFunc) {
  useDb("root", "test");

  run("SELECT LEASTSQUARES(c1, -1, 1) FROM t1");
}

98 99 100
TEST_F(ParserSelectTest, multiResFunc) {
  useDb("root", "test");

101
  run("SELECT LAST(*), FIRST(*), LAST_ROW(*) FROM t1");
102

103
  run("SELECT LAST(c1, c2), FIRST(t1.*), LAST_ROW(c3) FROM t1");
104

105
  run("SELECT LAST(t2.*), FIRST(t1.c1, t2.*), LAST_ROW(t1.*, t2.*) FROM st1s1 t1, st1s2 t2 WHERE t1.ts = t2.ts");
106 107
}

X
Xiaoyu Wang 已提交
108 109 110
TEST_F(ParserSelectTest, timelineFunc) {
  useDb("root", "test");

111
  run("SELECT LAST(*), FIRST(*) FROM t1");
X
Xiaoyu Wang 已提交
112

113
  run("SELECT FIRST(ts), FIRST(c1), FIRST(c2), FIRST(c3) FROM t1");
X
Xiaoyu Wang 已提交
114

115 116 117
  run("SELECT LAST(*), FIRST(*) FROM t1 GROUP BY c1");

  run("SELECT LAST(*), FIRST(*) FROM t1 INTERVAL(10s)");
X
Xiaoyu Wang 已提交
118

X
Xiaoyu Wang 已提交
119
  run("SELECT diff(c1) FROM t1");
120 121 122 123 124 125 126 127 128

  run("select diff(ts) from (select _wstart as ts, count(*) from st1 partition by tbname interval(1d))", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);

  run("select diff(ts) from (select _wstart as ts, count(*) from st1 partition by tbname interval(1d) order by ts)");

  run("select t1.* from st1s1 t1, (select _wstart as ts, count(*) from st1s2 partition by tbname interval(1d)) WHERE t1.ts = t2.ts", TSDB_CODE_PAR_NOT_SUPPORT_JOIN);

  run("select t1.* from st1s1 t1, (select _wstart as ts, count(*) from st1s2 partition by tbname interval(1d) order by ts) t2 WHERE t1.ts = t2.ts");

X
Xiaoyu Wang 已提交
129 130
}

131 132
TEST_F(ParserSelectTest, selectFunc) {
  useDb("root", "test");
133

134 135 136 137 138 139 140 141 142
  // select function
  run("SELECT MAX(c1), MIN(c1) FROM t1");
  // select function for GROUP BY clause
  run("SELECT MAX(c1), MIN(c1) FROM t1 GROUP BY c1");
  // select function for INTERVAL clause
  run("SELECT MAX(c1), MIN(c1) FROM t1 INTERVAL(10s)");
  // select function along with the columns of select row
  run("SELECT MAX(c1), c2 FROM t1");
  run("SELECT MAX(c1), t1.* FROM t1");
143 144 145 146 147 148 149
  // select function along with the columns of select row, and with GROUP BY clause
  run("SELECT MAX(c1), c2 FROM t1 GROUP BY c3");
  run("SELECT MAX(c1), t1.* FROM t1 GROUP BY c3");
  // select function along with the columns of select row, and with window clause
  run("SELECT MAX(c1), c2 FROM t1 INTERVAL(10s)");
  run("SELECT MAX(c1), c2 FROM t1 SESSION(ts, 10s)");
  run("SELECT MAX(c1), c2 FROM t1 STATE_WINDOW(c3)");
150 151
}

X
Xiaoyu Wang 已提交
152
TEST_F(ParserSelectTest, IndefiniteRowsFunc) {
153 154 155 156 157
  useDb("root", "test");

  run("SELECT DIFF(c1) FROM t1");
}

X
Xiaoyu Wang 已提交
158
TEST_F(ParserSelectTest, IndefiniteRowsFuncSemanticCheck) {
159 160
  useDb("root", "test");

G
Ganlin Zhao 已提交
161
  run("SELECT DIFF(c1), c2 FROM t1");
162

G
Ganlin Zhao 已提交
163
  run("SELECT DIFF(c1), tbname FROM t1");
164

165
  run("SELECT DIFF(c1), count(*) FROM t1", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
166

167
  run("SELECT DIFF(c1), CSUM(c1) FROM t1", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
168

169
  run("SELECT CSUM(c3) FROM t1 STATE_WINDOW(c1)", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
170

171
  run("SELECT DIFF(c1) FROM t1 INTERVAL(10s)", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
172 173
}

174 175 176 177 178 179 180 181
TEST_F(ParserSelectTest, useDefinedFunc) {
  useDb("root", "test");

  run("SELECT udf1(c1) FROM t1");

  run("SELECT udf2(c1) FROM t1 GROUP BY c2");
}

182 183 184 185 186 187 188 189 190 191 192 193 194
TEST_F(ParserSelectTest, uniqueFunc) {
  useDb("root", "test");

  run("SELECT UNIQUE(c1) FROM t1");

  run("SELECT UNIQUE(c2 + 10) FROM t1 WHERE c1 > 10");

  run("SELECT UNIQUE(c2 + 10), ts, c2 FROM t1 WHERE c1 > 10");
}

TEST_F(ParserSelectTest, uniqueFuncSemanticCheck) {
  useDb("root", "test");

195
  run("SELECT UNIQUE(c1) FROM t1 INTERVAL(10S)", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
196

197
  run("SELECT UNIQUE(c1) FROM t1 GROUP BY c2", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
198 199 200 201 202 203 204 205 206 207 208 209 210
}

TEST_F(ParserSelectTest, tailFunc) {
  useDb("root", "test");

  run("SELECT TAIL(c1, 10) FROM t1");

  run("SELECT TAIL(c2 + 10, 10, 80) FROM t1 WHERE c1 > 10");
}

TEST_F(ParserSelectTest, tailFuncSemanticCheck) {
  useDb("root", "test");

211
  run("SELECT TAIL(c1, 10) FROM t1 INTERVAL(10S)", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
212

213
  run("SELECT TAIL(c1, 10) FROM t1 GROUP BY c2", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
214 215
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229
TEST_F(ParserSelectTest, partitionBy) {
  useDb("root", "test");

  run("SELECT c1, c2 FROM t1 PARTITION BY c2");

  run("SELECT SUM(c1), c2 FROM t1 PARTITION BY c2");
}

TEST_F(ParserSelectTest, partitionBySemanticCheck) {
  useDb("root", "test");

  run("SELECT SUM(c1), c2, c3 FROM t1 PARTITION BY c2", TSDB_CODE_PAR_NOT_SINGLE_GROUP);
}

230
TEST_F(ParserSelectTest, groupBy) {
231 232
  useDb("root", "test");

X
Xiaoyu Wang 已提交
233
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0");
234

235
  run("SELECT COUNT(*), c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c2");
236

237
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 having COUNT(c1) > 10");
238

239
  run("SELECT COUNT(*), c1, c2 + 10, c1 + c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c2, c1");
240

241
  run("SELECT COUNT(*), c1 + 10, c2 cnt FROM t1 WHERE c1 > 0 GROUP BY c1 + 10, c2");
242 243
}

244 245 246 247 248 249 250
TEST_F(ParserSelectTest, groupBySemanticCheck) {
  useDb("root", "test");

  run("SELECT COUNT(*) cnt, c1 FROM t1 WHERE c1 > 0", TSDB_CODE_PAR_NOT_SINGLE_GROUP);
  run("SELECT COUNT(*) cnt, c2 FROM t1 WHERE c1 > 0 GROUP BY c1", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
}

D
dapan1121 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
TEST_F(ParserSelectTest, havingCheck) {
  useDb("root", "test");

  run("select tbname,count(*) from st1 partition by tbname having c1>0", TSDB_CODE_PAR_INVALID_OPTR_USAGE);

  run("select tbname,count(*) from st1 group by tbname having c1>0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);

  run("select max(c1) from st1 group by tbname having c1>0");

  run("select max(c1) from st1 partition by tbname having c1>0");
}


264 265
TEST_F(ParserSelectTest, orderBy) {
  useDb("root", "test");
266

267
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 order by cnt");
268

269
  run("SELECT COUNT(*) cnt FROM t1 WHERE c1 > 0 GROUP BY c2 order by 1");
270 271 272 273
}

TEST_F(ParserSelectTest, distinct) {
  useDb("root", "test");
274

275
  run("SELECT distinct c1, c2 FROM t1 WHERE c1 > 0 order by c1");
276

277
  run("SELECT distinct c1 + 10, c2 FROM t1 WHERE c1 > 0 order by c1 + 10, c2");
278

279
  run("SELECT distinct c1 + 10 cc1, c2 cc2 FROM t1 WHERE c1 > 0 order by cc1, c2");
280

281 282 283 284 285 286 287 288 289
  run("SELECT distinct COUNT(c2) FROM t1 WHERE c1 > 0 GROUP BY c1 order by COUNT(c2)");
}

TEST_F(ParserSelectTest, limit) {
  useDb("root", "test");

  run("SELECT c1, c2 FROM t1 LIMIT 10");

  run("(SELECT c1, c2 FROM t1 LIMIT 10)");
290 291
}

X
Xiaoyu Wang 已提交
292 293 294 295
// INTERVAL(interval_val [, interval_offset]) [SLIDING (sliding_val)] [FILL(fill_mod_and_val)]
// fill_mod_and_val = { NONE | PREV | NULL | LINEAR | NEXT | value_mod }
// value_mod = VALUE , val ...
TEST_F(ParserSelectTest, interval) {
296
  useDb("root", "test");
X
Xiaoyu Wang 已提交
297 298 299 300 301 302 303
  // INTERVAL(interval_val)
  run("SELECT COUNT(*) FROM t1 INTERVAL(10s)");
  // INTERVAL(interval_val, interval_offset)
  run("SELECT COUNT(*) FROM t1 INTERVAL(10s, 5s)");
  // INTERVAL(interval_val, interval_offset) SLIDING (sliding_val)
  run("SELECT COUNT(*) FROM t1 INTERVAL(10s, 5s) SLIDING(7s)");
  // INTERVAL(interval_val) FILL(NONE)
X
Xiaoyu Wang 已提交
304 305
  run("SELECT COUNT(*) FROM t1 WHERE ts > TIMESTAMP '2022-04-01 00:00:00' and ts < TIMESTAMP '2022-04-30 23:59:59' "
      "INTERVAL(10s) FILL(NONE)");
306 307
}

308 309 310
TEST_F(ParserSelectTest, intervalSemanticCheck) {
  useDb("root", "test");

D
dapan1121 已提交
311
  run("SELECT c1 FROM t1 INTERVAL(10s)", TSDB_CODE_PAR_INVALID_OPTR_USAGE);
X
Xiaoyu Wang 已提交
312 313 314 315
  run("SELECT DISTINCT c1, c2 FROM t1 WHERE c1 > 3 INTERVAL(1d) FILL(NEXT)", TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE);
  run("SELECT HISTOGRAM(c1, 'log_bin', '{\"start\": -33,\"factor\": 55,\"count\": 5,\"infinity\": false}', 1) FROM t1 "
      "WHERE ts > TIMESTAMP '2022-04-01 00:00:00' and ts < TIMESTAMP '2022-04-30 23:59:59' INTERVAL(10s) FILL(NULL)",
      TSDB_CODE_PAR_FILL_NOT_ALLOWED_FUNC);
316
  run("SELECT _WSTART, _WEND, _WDURATION, sum(c1) FROM t1", TSDB_CODE_PAR_INVALID_WINDOW_PC);
317 318
}

X
Xiaoyu Wang 已提交
319 320 321 322 323 324
TEST_F(ParserSelectTest, interp) {
  useDb("root", "test");

  run("SELECT INTERP(c1) FROM t1 RANGE('2017-7-14 18:00:00', '2017-7-14 19:00:00') EVERY(5s) FILL(LINEAR)");
}

325 326 327 328 329
TEST_F(ParserSelectTest, subquery) {
  useDb("root", "test");

  run("SELECT SUM(a) FROM (SELECT MAX(c1) a, ts FROM st1s1 INTERVAL(1m)) INTERVAL(1n)");

330
  run("SELECT SUM(a) FROM (SELECT MAX(c1) a, _wstart FROM st1s1 INTERVAL(1m)) INTERVAL(1n)");
331 332 333

  run("SELECT SUM(a) FROM (SELECT MAX(c1) a, ts FROM st1s1 PARTITION BY TBNAME INTERVAL(1m)) INTERVAL(1n)");

334 335
  run("SELECT SUM(a) FROM (SELECT MAX(c1) a, _wstart FROM st1s1 PARTITION BY TBNAME INTERVAL(1m) ORDER BY _WSTART) "
      "INTERVAL(1n)");
X
Xiaoyu Wang 已提交
336

337 338 339 340
  run("SELECT diff(a) FROM (SELECT _wstart, tag1, tag2, MAX(c1) a FROM st1 PARTITION BY tag1 INTERVAL(1m)) PARTITION BY tag1");

  run("SELECT diff(a) FROM (SELECT _wstart, tag1, tag2, MAX(c1) a FROM st1 PARTITION BY tag1 INTERVAL(1m)) PARTITION BY tag2", TSDB_CODE_PAR_NOT_ALLOWED_FUNC);

X
Xiaoyu Wang 已提交
341 342 343
  run("SELECT _C0 FROM (SELECT _ROWTS, ts FROM st1s1)");

  run("SELECT ts FROM (SELECT t1.ts FROM st1s1 t1)");
X
Xiaoyu Wang 已提交
344 345

  run("(((SELECT t1.ts FROM st1s1 t1)))");
346 347
}

X
Xiaoyu Wang 已提交
348
TEST_F(ParserSelectTest, subquerySemanticCheck) {
349 350
  useDb("root", "test");

X
Xiaoyu Wang 已提交
351 352 353 354 355 356 357
  run("SELECT SUM(a) FROM (SELECT MAX(c1) a FROM st1s1 INTERVAL(1m)) INTERVAL(1n)",
      TSDB_CODE_PAR_NOT_ALLOWED_WIN_QUERY);

  run("SELECT ts FROM (SELECT t1.ts AS ts, t2.ts FROM st1s1 t1, st1s2 t2 WHERE t1.ts = t2.ts)",
      TSDB_CODE_PAR_AMBIGUOUS_COLUMN);

  run("SELECT ts FROM (SELECT ts AS c1 FROM st1s1 t1)", TSDB_CODE_PAR_INVALID_COLUMN);
358 359
}

X
Xiaoyu Wang 已提交
360
TEST_F(ParserSelectTest, semanticCheck) {
361 362 363
  useDb("root", "test");

  // TSDB_CODE_PAR_INVALID_COLUMN
X
Xiaoyu Wang 已提交
364
  run("SELECT c1, cc1 FROM t1", TSDB_CODE_PAR_INVALID_COLUMN);
365

X
Xiaoyu Wang 已提交
366
  run("SELECT t1.c1, t1.cc1 FROM t1", TSDB_CODE_PAR_INVALID_COLUMN);
367

D
dapan1121 已提交
368 369
  // TSDB_CODE_PAR_GET_META_ERROR
  run("SELECT * FROM t10", TSDB_CODE_PAR_GET_META_ERROR);
370

D
dapan1121 已提交
371
  run("SELECT * FROM test.t10", TSDB_CODE_PAR_GET_META_ERROR);
372

D
dapan1121 已提交
373
  // TSDB_CODE_PAR_TABLE_NOT_EXIST
X
Xiaoyu Wang 已提交
374
  run("SELECT t2.c1 FROM t1", TSDB_CODE_PAR_TABLE_NOT_EXIST);
375 376

  // TSDB_CODE_PAR_AMBIGUOUS_COLUMN
X
Xiaoyu Wang 已提交
377
  run("SELECT c2 FROM t1 tt1, t1 tt2 WHERE tt1.c1 = tt2.c1", TSDB_CODE_PAR_AMBIGUOUS_COLUMN);
378

X
Xiaoyu Wang 已提交
379
  run("SELECT c2 FROM (SELECT c1 c2, c2 FROM t1)", TSDB_CODE_PAR_AMBIGUOUS_COLUMN);
380

381
  // TSDB_CODE_PAR_WRONG_VALUE_TYPE
X
Xiaoyu Wang 已提交
382
  run("SELECT timestamp '2010a' FROM t1", TSDB_CODE_PAR_WRONG_VALUE_TYPE);
383

X
Xiaoyu Wang 已提交
384
  run("SELECT LAST(*) + SUM(c1) FROM t1", TSDB_CODE_PAR_WRONG_VALUE_TYPE);
385

X
Xiaoyu Wang 已提交
386
  run("SELECT CEIL(LAST(ts, c1)) FROM t1", TSDB_CODE_PAR_WRONG_VALUE_TYPE);
387

388
  // TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION
X
Xiaoyu Wang 已提交
389
  run("SELECT c2 FROM t1 tt1 join t1 tt2 on COUNT(*) > 0", TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION);
390

X
Xiaoyu Wang 已提交
391
  run("SELECT c2 FROM t1 WHERE COUNT(*) > 0", TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION);
392

X
Xiaoyu Wang 已提交
393
  run("SELECT c2 FROM t1 GROUP BY COUNT(*)", TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION);
394 395

  // TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT
X
Xiaoyu Wang 已提交
396
  run("SELECT c2 FROM t1 order by 0", TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT);
397

X
Xiaoyu Wang 已提交
398
  run("SELECT c2 FROM t1 order by 2", TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT);
399 400

  // TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION
X
Xiaoyu Wang 已提交
401
  run("SELECT COUNT(*) cnt FROM t1 having c1 > 0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
402

X
Xiaoyu Wang 已提交
403
  run("SELECT COUNT(*) cnt FROM t1 GROUP BY c2 having c1 > 0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
404

X
Xiaoyu Wang 已提交
405
  run("SELECT COUNT(*), c1 cnt FROM t1 GROUP BY c2 having c2 > 0", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
406

X
Xiaoyu Wang 已提交
407
  run("SELECT COUNT(*) cnt FROM t1 GROUP BY c2 having c2 > 0 order by c1", TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
408 409

  // TSDB_CODE_PAR_NOT_SINGLE_GROUP
X
Xiaoyu Wang 已提交
410
  run("SELECT COUNT(*), c1 FROM t1", TSDB_CODE_PAR_NOT_SINGLE_GROUP);
411

X
Xiaoyu Wang 已提交
412
  run("SELECT COUNT(*) FROM t1 order by c1", TSDB_CODE_PAR_NOT_SINGLE_GROUP);
413

X
Xiaoyu Wang 已提交
414
  run("SELECT c1 FROM t1 order by COUNT(*)", TSDB_CODE_PAR_NOT_SINGLE_GROUP);
415 416

  // TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION
X
Xiaoyu Wang 已提交
417
  run("SELECT distinct c1, c2 FROM t1 WHERE c1 > 0 order by ts", TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION);
418

X
Xiaoyu Wang 已提交
419
  run("SELECT distinct c1 FROM t1 WHERE c1 > 0 order by COUNT(c2)", TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION);
420

X
Xiaoyu Wang 已提交
421 422 423 424 425 426 427
  run("SELECT distinct c2 FROM t1 WHERE c1 > 0 order by COUNT(c2)", TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION);
}

TEST_F(ParserSelectTest, syntaxError) {
  useDb("root", "test");

  run("SELECT CAST(? AS BINARY(10)) FROM t1", TSDB_CODE_PAR_SYNTAX_ERROR, PARSER_STAGE_PARSE);
428 429
}

X
Xiaoyu Wang 已提交
430 431 432
TEST_F(ParserSelectTest, setOperator) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
433
  run("SELECT * FROM t1 UNION ALL SELECT * FROM t1");
X
Xiaoyu Wang 已提交
434

X
Xiaoyu Wang 已提交
435
  run("(SELECT * FROM t1) UNION ALL (SELECT * FROM t1)");
436 437

  run("SELECT c1 FROM (SELECT c1 FROM t1 UNION ALL SELECT c1 FROM t1)");
438 439

  run("SELECT c1, c2 FROM t1 UNION ALL SELECT c1 as a, c2 as b FROM t1 ORDER BY c1");
X
Xiaoyu Wang 已提交
440 441
}

442 443 444 445 446 447
TEST_F(ParserSelectTest, setOperatorSemanticCheck) {
  useDb("root", "test");

  run("SELECT c1, c2 FROM t1 UNION ALL SELECT c1, c2 FROM t1 ORDER BY ts", TSDB_CODE_PAR_INVALID_COLUMN);
}

X
Xiaoyu Wang 已提交
448
TEST_F(ParserSelectTest, informationSchema) {
449 450 451
  useDb("root", "information_schema");

  run("SELECT * FROM ins_databases WHERE name = 'information_schema'");
X
Xiaoyu Wang 已提交
452

453
  run("SELECT * FROM ins_tags WHERE db_name = 'test' and table_name = 'st1'");
X
Xiaoyu Wang 已提交
454 455

  run("SELECT * FROM (SELECT table_name FROM ins_tables) t WHERE table_name = 'a'");
X
Xiaoyu Wang 已提交
456 457
}

458 459 460 461
TEST_F(ParserSelectTest, withoutFrom) {
  useDb("root", "test");

  run("SELECT 1");
462 463 464 465 466 467 468 469 470 471 472 473

  run("SELECT DATABASE()");

  run("SELECT CLIENT_VERSION()");

  run("SELECT SERVER_VERSION()");

  run("SELECT SERVER_STATUS()");

  run("SELECT CURRENT_USER()");

  run("SELECT USER()");
474 475
}

476 477 478 479 480 481 482
TEST_F(ParserSelectTest, withoutFromSemanticCheck) {
  useDb("root", "test");

  run("SELECT c1", TSDB_CODE_PAR_INVALID_COLUMN);
  run("SELECT TBNAME", TSDB_CODE_PAR_INVALID_TBNAME);
}

X
Xiaoyu Wang 已提交
483 484 485 486 487
TEST_F(ParserSelectTest, joinSemanticCheck) {
  useDb("root", "test");

  run("SELECT * FROM (SELECT tag1, SUM(c1) s FROM st1 GROUP BY tag1) t1, st1 t2 where t1.tag1 = t2.tag1",
      TSDB_CODE_PAR_NOT_SUPPORT_JOIN);
D
dapan1121 已提交
488 489

  run("SELECT count(*) FROM t1 a join t1 b on a.ts=b.ts where a.ts=b.ts");
X
Xiaoyu Wang 已提交
490 491
}

492
}  // namespace ParserTest