Skip to content

Commit

Permalink
fix: query result is incorrect after adding a new index in standalone…
Browse files Browse the repository at this point in the history
… version (4paradigm#1721)
  • Loading branch information
keyu813 authored Apr 27, 2022
1 parent 39b6d25 commit 972ceab
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/cmd/sql_cmd_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/nameserver/name_server_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9153,10 +9153,11 @@ void NameServerImpl::AddIndex(RpcController* controller, const AddIndexRequest*
}
}
} else {
std::shared_ptr<TabletInfo> 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);
Expand All @@ -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);
Expand Down

0 comments on commit 972ceab

Please sign in to comment.