YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/test/ql-delete-table-test.cc
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#include "yb/gutil/strings/substitute.h"
17
18
#include "yb/yql/cql/ql/test/ql-test-base.h"
19
20
using std::string;
21
using strings::Substitute;
22
23
namespace yb {
24
namespace ql {
25
26
class TestQLDeleteTable : public QLTestBase {
27
 public:
28
0
  TestQLDeleteTable() : QLTestBase() {
29
0
  }
30
};
31
32
0
TEST_F(TestQLDeleteTable, TestQLDeleteTableSimple) {
33
  // Init the simulated cluster.
34
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
35
36
  // Get a processor.
37
0
  TestQLProcessor *processor = GetQLProcessor();
38
39
  // -----------------------------------------------------------------------------------------------
40
  // Create the table.
41
0
  const char *create_stmt =
42
0
    "CREATE TABLE test_table(h1 int, h2 varchar, "
43
0
                            "r1 int, r2 varchar, "
44
0
                            "v1 int, v2 varchar, "
45
0
                            "primary key((h1, h2), r1, r2));";
46
0
  CHECK_VALID_STMT(create_stmt);
47
48
  // -----------------------------------------------------------------------------------------------
49
  // Unknown table.
50
0
  CHECK_INVALID_STMT("DELETE from test_table_unknown WHERE h1 = 0 AND h2 = 'zero';");
51
52
  // Missing hash key.
53
0
  CHECK_INVALID_STMT("DELETE from test_table;");
54
0
  CHECK_INVALID_STMT("DELETE from test_table WHERE h1 = 0;");
55
56
  // Wrong operator on hash key.
57
0
  CHECK_INVALID_STMT("DELETE from test_table WHERE h1 > 0 AND h2 = 'zero';");
58
59
  // -----------------------------------------------------------------------------------------------
60
  // TESTCASE: DELETE statement using only hash key (partition key).
61
  // Insert 100 rows into the table.
62
0
  static const int kNumRows = 100;
63
0
  string select_stmt;
64
65
0
  for (int idx = 0; idx < kNumRows; idx++) {
66
    // INSERT: Valid statement with column list.
67
0
    string stmt = Substitute("INSERT INTO test_table(h1, h2, r1, r2, v1, v2) "
68
0
                             "VALUES($0, 'h$1', $2, 'r$3', $4, 'v$5');",
69
0
                             idx, idx, idx+100, idx+100, idx+1000, idx+1000);
70
0
    CHECK_VALID_STMT(stmt);
71
0
  }
72
73
  // Testing DELETE one row.
74
0
  for (int idx = 0; idx < kNumRows; idx++) {
75
    // SELECT an entry to make sure it's there.
76
0
    select_stmt = Substitute("SELECT * FROM test_table"
77
0
                             "  WHERE h1 = $0 AND h2 = 'h$1' AND r1 = $2 AND r2 = 'r$3';",
78
0
                             idx, idx, idx+100, idx+100);
79
0
    CHECK_VALID_STMT(select_stmt);
80
0
    std::shared_ptr<QLRowBlock> one_row_block = processor->row_block();
81
0
    CHECK_EQ(one_row_block->row_count(), 1);
82
83
    // DELETE the entry.
84
0
    CHECK_VALID_STMT(Substitute("DELETE FROM test_table"
85
0
                                "  WHERE h1 = $0 AND h2 = 'h$1' AND r1 = $2 AND r2 = 'r$3';",
86
0
                                idx, idx, idx+100, idx+100));
87
88
    // SELECT the same entry to make sure it's no longer there.
89
0
    CHECK_VALID_STMT(select_stmt);
90
0
    std::shared_ptr<QLRowBlock> empty_row_block = processor->row_block();
91
0
    CHECK_EQ(empty_row_block->row_count(), 0);
92
0
  }
93
94
#if 0
95
  // -----------------------------------------------------------------------------------------------
96
  // TESTCASE: DELETE statement using range key.
97
  // Insert 100 rows into the table that share the same partition key.
98
  for (int idx = 0; idx < kNumRows; idx++) {
99
    // INSERT: Valid statement with column list.
100
    string stmt = Substitute("INSERT INTO test_table(h1, h2, r1, r2, v1, v2)"
101
                             "  VALUES($0, 'h$1', $2, 'r$3', $4, 'v$5');",
102
                             9999, 9999, idx+100, idx+100, idx+1000, idx+1000);
103
    CHECK_VALID_STMT(stmt);
104
  }
105
  LOG(INFO) << kNumRows << "rows were inserted";
106
107
  // Delete the first half of the table and check.
108
  // SELECT entries to make sure they are there.
109
  select_stmt = Substitute("SELECT * FROM test_table"
110
                           "  WHERE h1 = $0 AND h2 = 'h$1' AND r1 < $2 AND r2 < 'r$3';",
111
                           9999, 9999, 150, 150);
112
  CHECK_VALID_STMT(select_stmt);
113
  std::shared_ptr<QLRowBlock> row_block = processor->row_block();
114
  CHECK_EQ(row_block->row_count(), 50);
115
  LOG(INFO) << "50 rows were selected";
116
117
  // DELETE the entry.
118
  CHECK_VALID_STMT(Substitute("DELETE FROM test_table"
119
                              "  WHERE h1 = $0 AND h2 = 'h$1' AND r1 < $2 AND r2 < 'r$3';",
120
                              9999, 9999, 150, 150));
121
  LOG(INFO) << "Expecting that 50 rows were deleted";
122
123
  // SELECT the same entries to make sure they are no longer there.
124
  CHECK_VALID_STMT(select_stmt);
125
  row_block = processor->row_block();
126
  if (row_block->row_count() > 0) {
127
    LOG(WARNING) << "Feature not yet supported. Not all rows are deleted";
128
  } else {
129
    LOG(INFO) << "50 rows were deleted";
130
  }
131
132
  // Delete the rest of the table and check.
133
  // SELECT entries to make sure they are there.
134
  select_stmt = Substitute("SELECT * FROM test_table"
135
                           "  WHERE h1 = $0 AND h2 = 'h$1' AND r1 > $2 AND r2 > 'r$3';",
136
                           9999, 9999, 149, 149);
137
  CHECK_VALID_STMT(select_stmt);
138
  row_block = processor->row_block();
139
  CHECK_EQ(row_block->row_count(), 50);
140
  LOG(INFO) << "50 rows were selected";
141
142
  // DELETE the entry.
143
  CHECK_VALID_STMT(Substitute("DELETE FROM test_table"
144
                              "  WHERE h1 = $0 AND h2 = 'h$1' AND r1 > $2 AND r2 > 'r$3';",
145
                              9999, 9999, 149, 149));
146
  LOG(INFO) << "Expecting that 50 rows were deleted";
147
148
  // SELECT the same entries to make sure they are no longer there.
149
  CHECK_VALID_STMT(select_stmt);
150
  row_block = processor->row_block();
151
  if (row_block->row_count() > 0) {
152
    LOG(WARNING) << "Feature not yet supported. Not all rows are deleted";
153
  } else {
154
    LOG(INFO) << "50 rows were deleted";
155
  }
156
#endif
157
0
}
158
159
} // namespace ql
160
} // namespace yb