planOptimizeTest.cpp 3.4 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 "planTestUtil.h"
#include "planner.h"

using namespace std;

X
Xiaoyu Wang 已提交
21
class PlanOptimizeTest : public PlannerTestBase {};
X
Xiaoyu Wang 已提交
22

23
TEST_F(PlanOptimizeTest, scanPath) {
24 25 26 27 28 29 30
  useDb("root", "test");

  run("SELECT COUNT(*) FROM t1");

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

  run("SELECT COUNT(CAST(c1 AS BIGINT)) FROM t1");
X
Xiaoyu Wang 已提交
31 32

  run("SELECT PERCENTILE(c1, 40), COUNT(*) FROM t1");
X
Xiaoyu Wang 已提交
33 34

  run("SELECT LAST(c1) FROM t1");
X
Xiaoyu Wang 已提交
35 36 37

  run("SELECT LAST(c1) FROM t1 WHERE ts BETWEEN '2022-7-29 11:10:10' AND '2022-7-30 11:10:10' INTERVAL(10S) "
      "FILL(LINEAR)");
38 39
}

40
TEST_F(PlanOptimizeTest, pushDownCondition) {
X
Xiaoyu Wang 已提交
41 42 43
  useDb("root", "test");

  run("SELECT ts, c1 FROM st1 WHERE tag1 > 4");
X
Xiaoyu Wang 已提交
44

X
Xiaoyu Wang 已提交
45 46
  run("SELECT ts, c1 FROM st1 WHERE TBNAME = 'st1s1'");

X
Xiaoyu Wang 已提交
47 48 49
  run("SELECT ts, c1 FROM st1 WHERE tag1 > 4 or tag1 < 2");

  run("SELECT ts, c1 FROM st1 WHERE tag1 > 4 AND tag2 = 'hello'");
X
Xiaoyu Wang 已提交
50 51

  run("SELECT ts, c1 FROM st1 WHERE tag1 > 4 AND tag2 = 'hello' AND c1 > 10");
52 53

  run("SELECT ts, c1 FROM (SELECT * FROM st1) WHERE tag1 > 4");
X
Xiaoyu Wang 已提交
54 55
}

56
TEST_F(PlanOptimizeTest, sortPrimaryKey) {
X
Xiaoyu Wang 已提交
57 58
  useDb("root", "test");

59
  run("SELECT c1 FROM t1 ORDER BY ts");
X
Xiaoyu Wang 已提交
60

61 62
  run("SELECT c1 FROM st1 ORDER BY ts");

63
  run("SELECT c1 FROM t1 ORDER BY ts DESC");
X
Xiaoyu Wang 已提交
64

X
Xiaoyu Wang 已提交
65 66
  run("SELECT c1 FROM st1 ORDER BY ts DESC");

67
  run("SELECT COUNT(*) FROM t1 INTERVAL(10S) ORDER BY _WSTART DESC");
X
Xiaoyu Wang 已提交
68 69 70 71 72 73

  run("SELECT FIRST(c1) FROM t1 WHERE ts BETWEEN '2022-7-29 11:10:10' AND '2022-7-30 11:10:10' INTERVAL(10S) "
      "FILL(LINEAR) ORDER BY _WSTART DESC");

  run("SELECT LAST(c1) FROM t1 WHERE ts BETWEEN '2022-7-29 11:10:10' AND '2022-7-30 11:10:10' INTERVAL(10S) "
      "FILL(LINEAR) ORDER BY _WSTART");
X
Xiaoyu Wang 已提交
74
}
X
Xiaoyu Wang 已提交
75 76 77 78

TEST_F(PlanOptimizeTest, PartitionTags) {
  useDb("root", "test");

79
  run("SELECT c1, tag1 FROM st1 PARTITION BY tag1");
X
Xiaoyu Wang 已提交
80

81 82 83 84 85 86 87
  run("SELECT SUM(c1), tag1 FROM st1 PARTITION BY tag1");

  run("SELECT SUM(c1), tag1 + 10 FROM st1 PARTITION BY tag1 + 10");

  run("SELECT SUM(c1), tag1 FROM st1 GROUP BY tag1");

  run("SELECT SUM(c1), tag1 + 10 FROM st1 GROUP BY tag1 + 10");
X
Xiaoyu Wang 已提交
88 89

  run("SELECT SUM(c1), tbname FROM st1 GROUP BY tbname");
X
Xiaoyu Wang 已提交
90
}
X
Xiaoyu Wang 已提交
91

92 93 94 95
TEST_F(PlanOptimizeTest, eliminateProjection) {
  useDb("root", "test");

  run("SELECT c1, sum(c3) FROM t1 GROUP BY c1");
96 97
  run("SELECT c1 FROM t1");
  run("SELECT * FROM st1");
S
shenglian zhou 已提交
98
  run("SELECT c1 FROM st1s3");
X
Xiaoyu Wang 已提交
99
  // run("select 1-abs(c1) from (select unique(c1) c1 from st1s3) order by 1 nulls first");
100
}
101

X
Xiaoyu Wang 已提交
102 103 104 105 106 107
TEST_F(PlanOptimizeTest, mergeProjects) {
  useDb("root", "test");

  run("SELECT * FROM (SELECT * FROM t1 WHERE c1 > 10 ORDER BY ts) ORDER BY ts");
}

108 109 110
TEST_F(PlanOptimizeTest, pushDownProjectCond) {
  useDb("root", "test");
  run("select 1-abs(c1) from (select unique(c1) c1 from st1s3) where 1-c1>5 order by 1 nulls first");
S
slzhou@taodata.com 已提交
111 112 113 114 115 116 117
}

TEST_F(PlanOptimizeTest, tagScan) {
  useDb("root", "test");
  run("select tag1 from st1 group by tag1");
  run("select distinct tag1 from st1");
  run("select tag1*tag1 from st1 group by tag1*tag1");
118
}