From f7558d7b309b82c1244518367cd707902781a2b2 Mon Sep 17 00:00:00 2001 From: dl239 Date: Wed, 10 Jan 2024 19:08:06 +0800 Subject: [PATCH] test: add test case --- src/catalog/distribute_iterator.cc | 2 +- src/cmd/sql_cmd_test.cc | 119 +++++++++++++++++++++++++++++ src/sdk/sql_cluster_test.cc | 41 ---------- src/tablet/tablet_impl.cc | 16 ++-- src/tablet/tablet_impl.h | 2 +- 5 files changed, 130 insertions(+), 50 deletions(-) diff --git a/src/catalog/distribute_iterator.cc b/src/catalog/distribute_iterator.cc index b82afbb81fd..032d3ec75f2 100644 --- a/src/catalog/distribute_iterator.cc +++ b/src/catalog/distribute_iterator.cc @@ -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_; } diff --git a/src/cmd/sql_cmd_test.cc b/src/cmd/sql_cmd_test.cc index 433252474d1..dcd3a2d37fa 100644 --- a/src/cmd/sql_cmd_test.cc +++ b/src/cmd/sql_cmd_test.cc @@ -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; diff --git a/src/sdk/sql_cluster_test.cc b/src/sdk/sql_cluster_test.cc index 9374841d71e..7f83189068a 100644 --- a/src/sdk/sql_cluster_test.cc +++ b/src/sdk/sql_cluster_test.cc @@ -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; diff --git a/src/tablet/tablet_impl.cc b/src/tablet/tablet_impl.cc index 0ccc22b3eaf..1d902d98e60 100644 --- a/src/tablet/tablet_impl.cc +++ b/src/tablet/tablet_impl.cc @@ -1395,7 +1395,7 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr& t const std::string& key, std::optional start_ts, std::optional end_ts, - bool filter_range, + bool skip_cur_ts_col, const std::shared_ptr& client_manager, uint32_t partition_num) { storage::Ticket ticket; @@ -1407,6 +1407,7 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr& 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; } @@ -1434,9 +1435,12 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr& 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(); } else { @@ -1447,11 +1451,6 @@ base::Status TabletImpl::DeleteAllIndex(const std::shared_ptr& 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; } @@ -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 iter(table->NewTraverseIterator(idx)); iter->SeekToFirst(); diff --git a/src/tablet/tablet_impl.h b/src/tablet/tablet_impl.h index 6f244746fed..0aa998b0562 100644 --- a/src/tablet/tablet_impl.h +++ b/src/tablet/tablet_impl.h @@ -440,7 +440,7 @@ class TabletImpl : public ::openmldb::api::TabletServer { const std::string& key, std::optional start_ts, std::optional end_ts, - bool filter_range, + bool skip_cur_ts_col, const std::shared_ptr& client_manager, uint32_t partition_num);