From 972ceabcaaefd566906db64d12b8b1e1e12b23d2 Mon Sep 17 00:00:00 2001 From: keyu813 <60570208+keyu813@users.noreply.github.com> Date: Wed, 27 Apr 2022 14:37:59 +0800 Subject: [PATCH] fix: query result is incorrect after adding a new index in standalone version (#1721) --- src/cmd/sql_cmd_test.cc | 42 ++++++++++++++++++++++++++++++ src/nameserver/name_server_impl.cc | 9 ++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/cmd/sql_cmd_test.cc b/src/cmd/sql_cmd_test.cc index efe3d36c22a..4d6f614577b 100644 --- a/src/cmd/sql_cmd_test.cc +++ b/src/cmd/sql_cmd_test.cc @@ -1421,6 +1421,48 @@ TEST_P(DBSDKTest, GlobalVariable) { rs.get()); } +TEST_P(DBSDKTest, SelectWithAddNewIndex) { + auto cli = GetParam(); + cs = cli->cs; + sr = cli->sr; + + std::string db1_name = absl::StrCat("db1_", GenRand()); + std::string tb1_name = absl::StrCat("tb1_", GenRand()); + + ProcessSQLs(sr, + { + "set @@execute_mode = 'online'", + absl::StrCat("create database ", db1_name, ";"), + absl::StrCat("use ", db1_name, ";"), + + absl::StrCat("create table ", tb1_name, + " (id int, c1 string, c2 int, c3 timestamp, c4 timestamp, " + "index(key=(c1),ts=c4))options(partitionnum=1, replicanum=1);"), + absl::StrCat("insert into ", tb1_name, " values(1,'aa',1,1590738990000,1637056523316);"), + absl::StrCat("insert into ", tb1_name, " values(2,'bb',1,1590738990000,1637056523316);"), + absl::StrCat("insert into ", tb1_name, " values(3,'aa',3,1590738990000,1637057123257);"), + absl::StrCat("insert into ", tb1_name, " values(4,'aa',1,1590738990000,1637057123317);"), + absl::StrCat("CREATE INDEX index1 ON ", tb1_name, " (c2) OPTIONS (ttl=10m, ttl_type=absolute);"), + }); + absl::SleepFor(absl::Seconds(4)); + hybridse::sdk::Status status; + auto res = sr->ExecuteSQL(absl::StrCat("use ", db1_name, ";"), &status); + res = sr->ExecuteSQL(absl::StrCat("select id,c1,c2,c3 from ", tb1_name), &status); + ASSERT_EQ(res->Size(), 4); + res = sr->ExecuteSQL(absl::StrCat("select id,c1,c2,c3 from ", tb1_name, " where c1='aa';"), &status); + ASSERT_EQ(res->Size(), 3); + res = sr->ExecuteSQL(absl::StrCat("select id,c1,c2,c3 from ", tb1_name, " where c2=1;"), &status); + ASSERT_EQ(res->Size(), 3); + + ProcessSQLs(sr, { + absl::StrCat("use ", db1_name, ";"), + absl::StrCat("drop table ", tb1_name), + absl::StrCat("drop database ", db1_name), + }); + + sr->SetDatabase(""); +} + // -------------------------------------------------------------------------------------- // basic functional UTs to test if it is correct for deploy query response time collection // see NameServerImpl::SyncDeployStats & TabletImpl::TryCollectDeployStats diff --git a/src/nameserver/name_server_impl.cc b/src/nameserver/name_server_impl.cc index cc6bf0d622b..c6ca20bcd18 100644 --- a/src/nameserver/name_server_impl.cc +++ b/src/nameserver/name_server_impl.cc @@ -9153,10 +9153,11 @@ void NameServerImpl::AddIndex(RpcController* controller, const AddIndexRequest* } } } else { + std::shared_ptr tablet_ptr = nullptr; for (const auto& partition : table_info->table_partition()) { uint32_t pid = partition.pid(); for (const auto& meta : partition.partition_meta()) { - auto tablet_ptr = GetTablet(meta.endpoint()); + tablet_ptr = GetTablet(meta.endpoint()); if (!tablet_ptr) { PDLOG(WARNING, "endpoint[%s] can not find client", meta.endpoint().c_str()); base::SetResponseStatus(ReturnCode::kTabletIsNotHealthy, "tablet is not exist", response); @@ -9168,6 +9169,12 @@ void NameServerImpl::AddIndex(RpcController* controller, const AddIndexRequest* } } } + std::vector<::openmldb::common::ColumnKey> column_keys = {request->column_key()}; + if (!tablet_ptr->client_->ExtractMultiIndexData( + table_info->tid(), 0, (uint32_t)table_info->table_partition_size(), column_keys)) { + base::SetResponseStatus(ReturnCode::kAddIndexFailed, "extract multi index failed", response); + return; + } AddIndexToTableInfo(name, db, request->column_key(), table_info->column_key_size()); } base::SetResponseOK(response);