/Users/deen/code/yugabyte-db/src/yb/client/ql-dml-ttl-test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/client/client.h" |
15 | | #include "yb/client/ql-dml-test-base.h" |
16 | | #include "yb/client/schema.h" |
17 | | #include "yb/client/session.h" |
18 | | #include "yb/client/table_handle.h" |
19 | | #include "yb/client/yb_op.h" |
20 | | |
21 | | #include "yb/common/ql_value.h" |
22 | | |
23 | | #include "yb/util/status_log.h" |
24 | | |
25 | | #include "yb/yql/cql/ql/util/statement_result.h" |
26 | | |
27 | | namespace yb { |
28 | | namespace client { |
29 | | |
30 | | using yb::ql::RowsResult; |
31 | | |
32 | | |
33 | | namespace { |
34 | | |
35 | | const std::vector<std::string> kAllColumns = {"k", "c1", "c2", "c3", "c4"}; |
36 | | |
37 | | } |
38 | | |
39 | | class QLDmlTTLTest : public QLDmlTestBase<MiniCluster> { |
40 | | public: |
41 | 1 | void SetUp() override { |
42 | 1 | QLDmlTestBase::SetUp(); |
43 | | |
44 | 1 | YBSchemaBuilder b; |
45 | 1 | b.AddColumn("k")->Type(INT32)->HashPrimaryKey()->NotNull(); |
46 | 1 | b.AddColumn("c1")->Type(INT32); |
47 | 1 | b.AddColumn("c2")->Type(STRING); |
48 | 1 | b.AddColumn("c3")->Type(INT32); |
49 | 1 | b.AddColumn("c4")->Type(STRING); |
50 | | |
51 | 1 | ASSERT_OK(table_.Create(kTableName, CalcNumTablets(3), client_.get(), &b)); |
52 | 1 | } |
53 | | |
54 | | TableHandle table_; |
55 | | }; |
56 | | |
57 | 0 | TEST_F(QLDmlTTLTest, TestInsertWithTTL) { |
58 | 0 | const YBSessionPtr session(NewSession()); |
59 | 0 | { |
60 | | // insert into t (k, c1, c2) values (1, 1, "yuga-hello") using ttl 2; |
61 | 0 | const YBqlWriteOpPtr op = table_.NewWriteOp(QLWriteRequestPB::QL_STMT_INSERT); |
62 | 0 | auto* const req = op->mutable_request(); |
63 | 0 | QLAddInt32HashValue(req, 1); |
64 | 0 | table_.AddInt32ColumnValue(req, "c1", 1); |
65 | 0 | table_.AddStringColumnValue(req, "c2", "yuga-hello"); |
66 | 0 | req->set_ttl(2 * 1000); |
67 | 0 | CHECK_OK(session->ApplyAndFlush(op)); |
68 | |
|
69 | 0 | EXPECT_EQ(op->response().status(), QLResponsePB::YQL_STATUS_OK); |
70 | 0 | } |
71 | |
|
72 | 0 | { |
73 | | // insert into t (k, c3, c4) values (1, 2, "yuga-hi") using ttl 4; |
74 | 0 | const YBqlWriteOpPtr op = table_.NewWriteOp(QLWriteRequestPB::QL_STMT_INSERT); |
75 | 0 | auto* const req = op->mutable_request(); |
76 | 0 | QLAddInt32HashValue(req, 1); |
77 | 0 | table_.AddInt32ColumnValue(req, "c3", 2); |
78 | 0 | table_.AddStringColumnValue(req, "c4", "yuga-hi"); |
79 | 0 | req->set_ttl(4 * 1000); |
80 | 0 | CHECK_OK(session->ApplyAndFlush(op)); |
81 | |
|
82 | 0 | EXPECT_EQ(op->response().status(), QLResponsePB::YQL_STATUS_OK); |
83 | 0 | } |
84 | |
|
85 | 0 | { |
86 | | // select * from t where k = 1; |
87 | 0 | const YBqlReadOpPtr op = table_.NewReadOp(); |
88 | 0 | auto* const req = op->mutable_request(); |
89 | 0 | QLAddInt32HashValue(req, 1); |
90 | 0 | table_.AddColumns(kAllColumns, req); |
91 | |
|
92 | 0 | CHECK_OK(session->ApplyAndFlush(op)); |
93 | | |
94 | | // Expect all 4 columns (c1, c2, c3, c4) to be valid right now. |
95 | 0 | EXPECT_EQ(op->response().status(), QLResponsePB::YQL_STATUS_OK); |
96 | 0 | auto rowblock = RowsResult(op.get()).GetRowBlock(); |
97 | 0 | EXPECT_EQ(rowblock->row_count(), 1); |
98 | 0 | const auto& row = rowblock->row(0); |
99 | 0 | EXPECT_EQ(row.column(0).int32_value(), 1); |
100 | 0 | EXPECT_EQ(row.column(1).int32_value(), 1); |
101 | 0 | EXPECT_EQ(row.column(2).string_value(), "yuga-hello"); |
102 | 0 | EXPECT_EQ(row.column(3).int32_value(), 2); |
103 | 0 | EXPECT_EQ(row.column(4).string_value(), "yuga-hi"); |
104 | 0 | } |
105 | |
|
106 | 0 | LOG(INFO) << "Sleep for 2.5 seconds.."; |
107 | 0 | SleepFor(MonoDelta::FromMilliseconds(2500)); |
108 | |
|
109 | 0 | { |
110 | | // select * from t where k = 1; |
111 | 0 | const YBqlReadOpPtr op = table_.NewReadOp(); |
112 | 0 | auto* const req = op->mutable_request(); |
113 | 0 | QLAddInt32HashValue(req, 1); |
114 | 0 | table_.AddColumns(kAllColumns, req); |
115 | |
|
116 | 0 | CHECK_OK(session->ApplyAndFlush(op)); |
117 | | |
118 | | // Expect columns (c1, c2) to be null and (c3, c4) to be valid right now. |
119 | 0 | EXPECT_EQ(op->response().status(), QLResponsePB::YQL_STATUS_OK); |
120 | 0 | auto rowblock = RowsResult(op.get()).GetRowBlock(); |
121 | 0 | EXPECT_EQ(rowblock->row_count(), 1); |
122 | 0 | const auto& row = rowblock->row(0); |
123 | 0 | EXPECT_EQ(row.column(0).int32_value(), 1); |
124 | 0 | EXPECT_TRUE(row.column(1).IsNull()); |
125 | 0 | EXPECT_TRUE(row.column(2).IsNull()); |
126 | 0 | EXPECT_EQ(row.column(3).int32_value(), 2); |
127 | 0 | EXPECT_EQ(row.column(4).string_value(), "yuga-hi"); |
128 | 0 | } |
129 | |
|
130 | 0 | LOG(INFO) << "Sleep for 2.5 seconds.."; |
131 | 0 | SleepFor(MonoDelta::FromMilliseconds(2500)); |
132 | |
|
133 | 0 | { |
134 | | // select * from t where k = 1; |
135 | 0 | const YBqlReadOpPtr op = table_.NewReadOp(); |
136 | 0 | auto* const req = op->mutable_request(); |
137 | 0 | QLAddInt32HashValue(req, 1); |
138 | 0 | table_.AddColumns(kAllColumns, req); |
139 | |
|
140 | 0 | CHECK_OK(session->ApplyAndFlush(op)); |
141 | | |
142 | | // Expect all 4 columns (c1, c2, c3, c4) to be null. |
143 | 0 | EXPECT_EQ(op->response().status(), QLResponsePB::YQL_STATUS_OK); |
144 | 0 | auto rowblock = RowsResult(op.get()).GetRowBlock(); |
145 | 0 | EXPECT_EQ(rowblock->row_count(), 0); |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | | } // namespace client |
150 | | } // namespace yb |