Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dl239 committed Jan 10, 2024
1 parent 14a8827 commit f7558d7
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/catalog/distribute_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ const ::hybridse::codec::Row& RemoteWindowIterator::GetValue() {
memcpy(copyed_row_data, slice_row.data(), sz);
auto shared_slice = ::hybridse::base::RefCountedSlice::CreateManaged(copyed_row_data, sz);
row_.Reset(shared_slice);
LOG(INFO) << "get value pk " << pk_ << " ts_key " << kv_it_->GetKey() << " ts " << ts_;
DLOG(INFO) << "get value pk " << pk_ << " ts_key " << kv_it_->GetKey() << " ts " << ts_;
valid_value_ = true;
return row_;
}
Expand Down
119 changes: 119 additions & 0 deletions src/cmd/sql_cmd_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,125 @@ TEST_P(DBSDKTest, DeletetSameColIndex) {
});
}

TEST_P(DBSDKTest, TestDelete) {
auto cli = GetParam();
sr = cli->sr;
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
std::string ddl;

std::string db = "db" + GenRand();
ASSERT_TRUE(sr->CreateDB(db, &status));
sr->ExecuteSQL(db, "set @@execute_mode = 'online';", &status);
sr->ExecuteSQL(db, "use " + db + " ;", &status);
ddl = absl::StrCat("create table ", name,
"(col1 string, col2 string, col3 string, col4 bigint, col5 bigint, col6 bigint, col7 string,"
"index(key=col1, ts=col4), index(key=(col1, col2), ts=col4), index(key=col3, ts=col5));");
ASSERT_TRUE(sr->ExecuteDDL(db, ddl, &status)) << "ddl: " << ddl;
ASSERT_TRUE(sr->RefreshCatalog());
for (int i = 0; i < 10; i++) {
std::string key1 = absl::StrCat("key1_", i);
std::string key2 = absl::StrCat("key2_", i);
std::string key3 = absl::StrCat("key3_", i);
for (int j = 0; j < 10; j++) {
sr->ExecuteSQL(absl::StrCat("insert into ", name,
" values ('", key1, "', '", key2, "', '", key3, "', ", 100 + j, ",", 1000 + j, ", 1, 'v');"),
&status);
}
}
auto rs = sr->ExecuteSQL(db, "select * from " + name + ";", &status);
ASSERT_EQ(rs->Size(), 100);
rs = sr->ExecuteSQL(db, "delete from " + name + " where col1 = 'xxx' and col5 > 100;", &status);
ASSERT_FALSE(status.IsOK());
rs = sr->ExecuteSQL(db, "delete from " + name + " where col1 = 'xxx' and col6 > 100;", &status);
ASSERT_FALSE(status.IsOK());
rs = sr->ExecuteSQL(db, "delete from " + name + " where col1 = 'xxx' and col3 = 'aaa';", &status);
ASSERT_FALSE(status.IsOK());
rs = sr->ExecuteSQL(db, "delete from " + name + " where col7 = 'xxx' and col3 = 'aaa';", &status);
ASSERT_FALSE(status.IsOK());
sr->ExecuteSQL(db, "delete from " + name + " where col6 > 100;", &status);
ASSERT_FALSE(status.IsOK());
rs = sr->ExecuteSQL(db, "delete from " + name + " where col1 = 'key1_1';", &status);
ASSERT_TRUE(status.IsOK());
rs = sr->ExecuteSQL(db, "select * from " + name + " where col1 = 'key1_1';", &status);
ASSERT_EQ(rs->Size(), 0);
rs = sr->ExecuteSQL(db, "select * from " + name + " where col1 = 'key1_1' and col2 = 'key2_1';", &status);
ASSERT_EQ(rs->Size(), 0);
rs = sr->ExecuteSQL(db, "select * from " + name + " where col3 = 'key3_1';", &status);
ASSERT_EQ(rs->Size(), 0);
sr->ExecuteSQL(db, "delete from " + name + " where col4 > 105;", &status);
ASSERT_TRUE(status.IsOK());
rs = sr->ExecuteSQL(db, "select * from " + name + " where col1 = 'key1_2';", &status);
ASSERT_EQ(rs->Size(), 6);
rs = sr->ExecuteSQL(db, "select * from " + name + " where col1 = 'key1_2' and col2 = 'key2_2';", &status);
ASSERT_EQ(rs->Size(), 6);
rs = sr->ExecuteSQL(db, "select * from " + name + " where col3 = 'key3_2';", &status);
ASSERT_EQ(rs->Size(), 6);

ASSERT_TRUE(sr->ExecuteDDL(db, "drop table " + name + ";", &status));
ASSERT_TRUE(sr->DropDB(db, &status));
}


TEST_P(DBSDKTest, DeletetMulIndex) {
auto cli = GetParam();
sr = cli->sr;
std::string db_name = "test2";
std::string table_name = "test1";
std::string ddl =
"create table test1 (c1 string, c2 string, c3 bigint, c4 bigint, "
"INDEX(KEY=c1, ts=c3), INDEX(KEY=c2, ts=c4));";
ProcessSQLs(sr, {
"set @@execute_mode = 'online'",
absl::StrCat("create database ", db_name, ";"),
absl::StrCat("use ", db_name, ";"),
ddl,
});
hybridse::sdk::Status status;
for (int i = 0; i < 10; i++) {
std::string key1 = absl::StrCat("key1_", i);
std::string key2 = absl::StrCat("key2_", i);
for (int j = 0; j < 10; j++) {
uint64_t ts = 1000 + j;
sr->ExecuteSQL(absl::StrCat("insert into ", table_name,
" values ('", key1, "', '", key2, "', ", ts, ",", ts, ");"),
&status);
}
}

auto res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 100);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c1 = \'key1_2\';"), &status);
ASSERT_EQ(res->Size(), 10);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c2 = \'key2_2\';"), &status);
ASSERT_EQ(res->Size(), 10);
sr->ExecuteSQL(absl::StrCat("delete from ", table_name, " where c1 = 'key1_2' and c3 = 1001;"), &status);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c1 = \'key1_2\';"), &status);
ASSERT_EQ(res->Size(), 9);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c2 = \'key2_2\';"), &status);
ASSERT_EQ(res->Size(), 9);
sr->ExecuteSQL(absl::StrCat("delete from ", table_name, " where c1 = 'key1_2';"), &status);
ASSERT_TRUE(status.IsOK()) << status.msg;
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 90);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c1 = \'key1_2\';"), &status);
ASSERT_EQ(res->Size(), 0);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c2 = \'key2_2\';"), &status);
ASSERT_EQ(res->Size(), 0);
sr->ExecuteSQL(absl::StrCat("delete from ", table_name, " where c3 >= 1005 ;"), &status);
ASSERT_TRUE(status.IsOK()) << status.msg;
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 45);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c1 = \'key1_3\';"), &status);
ASSERT_EQ(res->Size(), 5);
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, " where c2 = \'key2_3\';"), &status);
ASSERT_EQ(res->Size(), 5);
ProcessSQLs(sr, {
absl::StrCat("drop table ", table_name),
absl::StrCat("drop database ", db_name),
});
}

TEST_P(DBSDKTest, SQLDeletetRow) {
auto cli = GetParam();
sr = cli->sr;
Expand Down
41 changes: 0 additions & 41 deletions src/sdk/sql_cluster_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,47 +323,6 @@ TEST_F(SQLClusterDDLTest, CreateIndexCheck) {
ASSERT_TRUE(router->DropDB(db, &status));
}

TEST_F(SQLClusterDDLTest, TestDelete) {
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
std::string ddl;

std::string db = "db" + GenRand();
ASSERT_TRUE(router->CreateDB(db, &status));
ddl = absl::StrCat("create table ", name,
"(col1 string, col2 string, col3 string, col4 bigint, col5 bigint, col6 bigint, col7 string,"
"index(key=col1, ts=col4), index(key=(col1, col2), ts=col4), index(key=col3, ts=col5));");
ASSERT_TRUE(router->ExecuteDDL(db, ddl, &status)) << "ddl: " << ddl;
ASSERT_TRUE(router->RefreshCatalog());
router->ExecuteSQL(db, "insert into " + name + " values ('a', 'aa', 'aaa', 100, 101, 102, 'xx');", &status);
router->ExecuteSQL(db, "insert into " + name + " values ('b', 'bb', 'bbb', 200, 201, 202, 'xx');", &status);
auto rs = router->ExecuteSQL(db, "select * from " + name + ";", &status);
ASSERT_EQ(rs->Size(), 2);
rs = router->ExecuteSQL(db, "delete from " + name + " where col1 = 'xxx' and col5 > 100;", &status);
ASSERT_FALSE(status.IsOK());
rs = router->ExecuteSQL(db, "delete from " + name + " where col1 = 'xxx' and col6 > 100;", &status);
ASSERT_FALSE(status.IsOK());
rs = router->ExecuteSQL(db, "delete from " + name + " where col1 = 'xxx' and col3 = 'aaa';", &status);
ASSERT_FALSE(status.IsOK());
rs = router->ExecuteSQL(db, "delete from " + name + " where col7 = 'xxx' and col3 = 'aaa';", &status);
ASSERT_FALSE(status.IsOK());
router->ExecuteSQL(db, "delete from " + name + " where col6 > 100;", &status);
ASSERT_FALSE(status.IsOK());
router->ExecuteSQL(db, "delete from " + name + " where col4 > 100 and col5 = 200;", &status);
ASSERT_FALSE(status.IsOK());
router->ExecuteSQL(db, "delete from " + name + " where col5 > 100;", &status);
ASSERT_TRUE(status.IsOK()) << status.msg;
rs = router->ExecuteSQL(db, "select * from " + name + ";", &status);
ASSERT_EQ(rs->Size(), 2);
router->ExecuteSQL(db, "delete from " + name + " where col4 > 100;", &status);
ASSERT_TRUE(status.IsOK());
rs = router->ExecuteSQL(db, "select * from " + name + ";", &status);
ASSERT_EQ(rs->Size(), 1);

ASSERT_TRUE(router->ExecuteDDL(db, "drop table " + name + ";", &status));
ASSERT_TRUE(router->DropDB(db, &status));
}

TEST_F(SQLClusterDDLTest, ColumnDefaultValue) {
std::string name = "test" + GenRand();
::hybridse::sdk::Status status;
Expand Down
16 changes: 9 additions & 7 deletions src/tablet/tablet_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr<storage::Table>& t
const std::string& key,
std::optional<uint64_t> start_ts,
std::optional<uint64_t> end_ts,
bool filter_range,
bool skip_cur_ts_col,
const std::shared_ptr<catalog::TableClientManager>& client_manager,
uint32_t partition_num) {
storage::Ticket ticket;
Expand All @@ -1407,6 +1407,7 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr<storage::Table>& t
}
auto indexs = table->GetAllIndex();
while (iter->Valid()) {
DEBUGLOG("cur ts %lu cur index pos %u", iter->GetKey(), cur_index->GetId());
if (end_ts.has_value() && iter->GetKey() <= end_ts.value()) {
break;
}
Expand Down Expand Up @@ -1434,9 +1435,12 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr<storage::Table>& t
if (cur_index && index->GetId() == cur_index->GetId()) {
continue;
}
auto ts_col = index->GetTsColumn();
if (skip_cur_ts_col && ts_col->GetId() == cur_index->GetTsColumn()->GetId()) {
continue;
}
sdk::DeleteOption option;
option.idx = index->GetId();
auto ts_col = index->GetTsColumn();
if (ts_col->IsAutoGenTs()) {
option.start_ts = iter->GetKey();

Check warning on line 1445 in src/tablet/tablet_impl.cc

View check run for this annotation

Codecov / codecov/patch

src/tablet/tablet_impl.cc#L1445

Added line #L1445 was not covered by tests
} else {
Expand All @@ -1447,11 +1451,6 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr<storage::Table>& t
option.ts_name = ts_col->GetName();
option.start_ts = ts;
}
// skip the data if option.start_ts in [start_ts, end_ts) if filter_range is true
if (filter_range && !((start_ts.has_value() && option.start_ts.value() > start_ts.value()) ||
(end_ts.has_value() && option.start_ts.value() <= end_ts.value()))) {
continue;
}
if (option.start_ts.value() > 1) {
option.end_ts = option.start_ts.value() - 1;
}
Expand Down Expand Up @@ -1662,6 +1661,9 @@ void TabletImpl::Delete(RpcController* controller, const ::openmldb::api::Delete
if (!index_def || !index_def->IsReady()) {
continue;
}
if (index_def->GetTsColumn()->GetName() != request->ts_name()) {
continue;
}
uint32_t idx = index_def->GetId();
std::unique_ptr<storage::TraverseIterator> iter(table->NewTraverseIterator(idx));
iter->SeekToFirst();
Expand Down
2 changes: 1 addition & 1 deletion src/tablet/tablet_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class TabletImpl : public ::openmldb::api::TabletServer {
const std::string& key,
std::optional<uint64_t> start_ts,
std::optional<uint64_t> end_ts,
bool filter_range,
bool skip_cur_ts_col,
const std::shared_ptr<catalog::TableClientManager>& client_manager,
uint32_t partition_num);

Expand Down

0 comments on commit f7558d7

Please sign in to comment.