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-statement-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/util/async_util.h"
19
#include "yb/util/status_log.h"
20
21
#include "yb/yql/cql/ql/statement.h"
22
#include "yb/yql/cql/ql/test/ql-test-base.h"
23
#include "yb/yql/cql/ql/util/errcodes.h"
24
25
using std::string;
26
using std::unique_ptr;
27
using std::shared_ptr;
28
using strings::Substitute;
29
30
namespace yb {
31
namespace ql {
32
33
class TestQLStatement : public QLTestBase {
34
 public:
35
0
  TestQLStatement() : QLTestBase() {
36
0
  }
37
38
  void ExecuteAsyncDone(
39
0
      Callback<void(const Status&)> cb, const Status& s, const ExecutedResult::SharedPtr& result) {
40
0
    cb.Run(s);
41
0
  }
42
43
0
  Status ExecuteAsync(Statement *stmt, QLProcessor *processor, Callback<void(const Status&)> cb) {
44
0
    return stmt->ExecuteAsync(processor, StatementParameters(),
45
0
                              Bind(&TestQLStatement::ExecuteAsyncDone, Unretained(this), cb));
46
0
  }
47
48
};
49
50
0
TEST_F(TestQLStatement, TestExecutePrepareAfterTableDrop) {
51
  // Init the simulated cluster.
52
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
53
54
  // Get a processor.
55
0
  TestQLProcessor *processor = GetQLProcessor();
56
57
0
  LOG(INFO) << "Running execute and reprepare after table drop test.";
58
59
  // Create test table.
60
0
  EXEC_VALID_STMT("create table t (h1 int primary key, c int);");
61
62
  // Prepare a select statement.
63
0
  Statement stmt(processor->CurrentKeyspace(), "select * from t where h1 = 1;");
64
0
  CHECK_OK(stmt.Prepare(&processor->ql_processor()));
65
66
  // Drop table.
67
0
  EXEC_VALID_STMT("drop table t;");
68
69
  // Try executing the statement. Should return STALE_PREPARED_STATEMENT error.
70
0
  Synchronizer sync;
71
0
  CHECK_OK(ExecuteAsync(
72
0
      &stmt, &processor->ql_processor(), Bind(&Synchronizer::StatusCB, Unretained(&sync))));
73
0
  Status s = sync.Wait();
74
0
  CHECK(s.IsQLError() && GetErrorCode(s) == ErrorCode::STALE_METADATA)
75
0
      << "Expect STALE_METADATA but got " << s.ToString();
76
77
0
  LOG(INFO) << "Done.";
78
0
}
79
80
0
TEST_F(TestQLStatement, TestPrepareWithUnknownSystemTable) {
81
  // Init the simulated cluster.
82
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
83
84
  // Get a processor.
85
0
  TestQLProcessor *processor = GetQLProcessor();
86
87
0
  LOG(INFO) << "Running prepare for an unknown system table test.";
88
89
  // Prepare a select statement.
90
0
  Statement stmt("system", "select * from system.unknown_table;");
91
0
  PreparedResult::UniPtr result;
92
0
  CHECK_OK(stmt.Prepare(
93
0
      &processor->ql_processor(), nullptr /* mem_tracker */, false /* internal */, &result));
94
0
  CHECK_EQ(result->table_name().ToString(), "");
95
96
0
  LOG(INFO) << "Done.";
97
0
}
98
99
0
TEST_F(TestQLStatement, TestPrepareWithUnknownSystemTableAndUnknownField) {
100
  // Init the simulated cluster.
101
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
102
103
  // Get a processor.
104
0
  TestQLProcessor *processor = GetQLProcessor();
105
106
0
  LOG(INFO) << "Running prepare for an unknown system table test.";
107
108
  // Prepare a select statement.
109
0
  Statement stmt("system", "select * from system.unknown_table where unknown_field = ?;");
110
0
  PreparedResult::UniPtr result;
111
0
  CHECK_OK(stmt.Prepare(
112
0
      &processor->ql_processor(), nullptr /* mem_tracker */, false /* internal */, &result));
113
0
  CHECK_EQ(result->table_name().ToString(), "");
114
115
0
  LOG(INFO) << "Done.";
116
0
}
117
118
0
TEST_F(TestQLStatement, TestPKIndices) {
119
  // Init the simulated cluster.
120
0
  ASSERT_NO_FATALS(CreateSimulatedCluster());
121
122
  // Get a processor.
123
0
  TestQLProcessor *processor = GetQLProcessor();
124
125
  // Create test table.
126
0
  LOG(INFO) << "Create test table.";
127
0
  EXEC_VALID_STMT("create table test_pk_indices "
128
0
                  "(h1 int, h2 text, h3 timestamp, r int, primary key ((h1, h2, h3), r));");
129
130
  // Prepare a select statement.
131
0
  LOG(INFO) << "Prepare select statement.";
132
0
  Statement stmt(processor->CurrentKeyspace(),
133
0
                 "select * from test_pk_indices where h3 = ? and h1 = ? and h2 = ?;");
134
0
  PreparedResult::UniPtr result;
135
0
  CHECK_OK(stmt.Prepare(
136
0
      &processor->ql_processor(), nullptr /* mem_tracker */, false /* internal */, &result));
137
138
0
  const std::vector<int64_t>& hash_col_indices = result->hash_col_indices();
139
0
  EXPECT_EQ(hash_col_indices.size(), 3);
140
0
  EXPECT_EQ(hash_col_indices[0], 1);
141
0
  EXPECT_EQ(hash_col_indices[1], 2);
142
0
  EXPECT_EQ(hash_col_indices[2], 0);
143
144
0
  LOG(INFO) << "Done.";
145
0
}
146
147
} // namespace ql
148
} // namespace yb