Skip to content

Commit

Permalink
feat: enhance the delete (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
dl239 authored Jul 18, 2023
1 parent aec30fc commit 064a515
Show file tree
Hide file tree
Showing 50 changed files with 3,385 additions and 2,400 deletions.
8 changes: 6 additions & 2 deletions docs/en/reference/sql/dml/DELETE_STATEMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ TableName ::=
**Description**

- `DELETE` statement will delete all data from the index of specific column value of online table.
- The filter columns sepcified by `WHERE` must be an index column and the condition can only be `=`.
- The filter columns sepcified by `WHERE` must be an index column. if it is a key column, only `=` can be used.

## Examples

```SQL
DELETE FROM t1 WHERE col1 = 'aaaa';

DELETE FROM t1 WHERE col1 = 'aaaa' and col2 = 'bbbb';
DELETE FROM t1 WHERE col1 = 'aaaa' and ts_col = 1687145994000;

DELETE FROM t1 WHERE col1 = 'aaaa' and ts_col > 1687059594000 and ts_col < 1687145994000;

DELETE FROM t1 WHERE ts_col > 1687059594000 and ts_col < 1687145994000;
```
10 changes: 7 additions & 3 deletions docs/zh/openmldb_sql/dml/DELETE_STATEMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ TableName ::=

**说明**

- `DELETE` 语句删除在线表指定列的索引下面对应值的所有数据
- `WHERE` 指定的筛选列必须是索引列并且只能是等于
- `DELETE` 语句删除在线表满足指定条件的数据
- `WHERE` 指定的筛选列必须是索引列。如果是key列只能用等于

## Examples

```SQL
DELETE FROM t1 WHERE col1 = 'aaaa';

DELETE FROM t1 WHERE col1 = 'aaaa' and col2 = 'bbbb';
DELETE FROM t1 WHERE col1 = 'aaaa' and ts_col = 1687145994000;

DELETE FROM t1 WHERE col1 = 'aaaa' and ts_col > 1687059594000 and ts_col < 1687145994000;

DELETE FROM t1 WHERE ts_col > 1687059594000 and ts_col < 1687145994000;
```
1 change: 1 addition & 0 deletions src/base/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum ReturnCode {
kProcedureNotFound = 158,
kCreateFunctionFailed = 159,
kExceedMaxMemory = 160,
kInvalidArgs = 161,
kNameserverIsNotLeader = 300,
kAutoFailoverIsEnabled = 301,
kEndpointIsNotExist = 302,
Expand Down
25 changes: 25 additions & 0 deletions src/client/tablet_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,31 @@ bool TabletClient::Delete(uint32_t tid, uint32_t pid, const std::string& pk, con
return true;
}

base::Status TabletClient::Delete(uint32_t tid, uint32_t pid, const std::map<uint32_t, std::string>& index_val,
const std::optional<uint64_t> start_ts, const std::optional<uint64_t>& end_ts) {
::openmldb::api::DeleteRequest request;
::openmldb::api::GeneralResponse response;
request.set_tid(tid);
request.set_pid(pid);
for (const auto& kv : index_val) {
auto dimension = request.add_dimensions();
dimension->set_idx(kv.first);
dimension->set_key(kv.second);
}
if (start_ts.has_value()) {
request.set_ts(start_ts.value());
}
if (end_ts.has_value()) {
request.set_end_ts(end_ts.value());
}
bool ok = client_.SendRequest(&::openmldb::api::TabletServer_Stub::Delete, &request, &response,
FLAGS_request_timeout_ms, 1);
if (!ok || response.code() != 0) {
return {base::ReturnCode::kError, response.msg()};
}
return {};
}

bool TabletClient::ConnectZK() {
::openmldb::api::ConnectZKRequest request;
::openmldb::api::GeneralResponse response;
Expand Down
3 changes: 3 additions & 0 deletions src/client/tablet_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class TabletClient : public Client {
bool Delete(uint32_t tid, uint32_t pid, const std::string& pk, const std::string& idx_name,
std::string& msg); // NOLINT

base::Status Delete(uint32_t tid, uint32_t pid, const std::map<uint32_t, std::string>& index_val,
const std::optional<uint64_t> start_ts, const std::optional<uint64_t>& end_ts);

bool Count(uint32_t tid, uint32_t pid, const std::string& pk, const std::string& idx_name, bool filter_expired_data,
uint64_t& value, std::string& msg); // NOLINT

Expand Down
47 changes: 46 additions & 1 deletion src/cmd/sql_cmd_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,52 @@ TEST_P(DBSDKTest, DeployWithData) {
ASSERT_TRUE(cs->GetNsClient()->DropDatabase("test2", msg));
}

TEST_P(DBSDKTest, Delete) {
TEST_P(DBSDKTest, DeletetRange) {
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 int, c3 bigint, INDEX(KEY=c1, ts=c3));";
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 key = absl::StrCat("key", i);
for (int j = 0; j < 10; j++) {
uint64_t ts = 1000 + j;
sr->ExecuteSQL(absl::StrCat("insert into ", table_name, " values ('", key, "', 11, ", ts, ");"), &status);
}
}

auto res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 100);
ProcessSQLs(sr, {absl::StrCat("delete from ", table_name, " where c1 = 'key2';")});
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 90);
ProcessSQLs(sr, {absl::StrCat("delete from ", table_name, " where c1 = 'key3' and c3 = 1001;")});
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 89);
ProcessSQLs(sr, {absl::StrCat("delete from ", table_name, " where c1 = 'key4' and c3 > 1005;")});
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 85);
ProcessSQLs(sr, {absl::StrCat("delete from ", table_name, " where c1 = 'key4' and c3 > 1002 and c3 < 1006;")});
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 82);
ProcessSQLs(sr, {absl::StrCat("delete from ", table_name, " where c3 > 1007;")});
res = sr->ExecuteSQL(absl::StrCat("select * from ", table_name, ";"), &status);
ASSERT_EQ(res->Size(), 66);
ProcessSQLs(sr, {
absl::StrCat("use ", db_name, ";"),
absl::StrCat("drop table ", table_name),
absl::StrCat("drop database ", db_name),
});
}

TEST_P(DBSDKTest, SQLDeletetRow) {
auto cli = GetParam();
sr = cli->sr;
std::string db_name = "test2";
Expand Down
1 change: 0 additions & 1 deletion src/codec/field_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <vector>

#include "base/endianconv.h"
#include "base/glog_wrapper.h"
#include "base/strings.h"
#include "boost/lexical_cast.hpp"
#include "proto/type.pb.h"
Expand Down
4 changes: 4 additions & 0 deletions src/proto/tablet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ message DeleteRequest {
optional uint32 pid = 2;
optional string key = 3;
optional string idx_name = 4;
repeated Dimension dimensions = 5;
optional uint64 ts = 6;
optional uint64 end_ts = 7;
}

message ExecuteGcRequest {
Expand Down Expand Up @@ -398,6 +401,7 @@ message LogEntry {
repeated Dimension dimensions = 6;
optional MethodType method_type = 7;
repeated TSDimension ts_dimensions = 8 [deprecated = true];
optional uint64 end_ts = 9; // only for range delete: [ts, end_ts)
}

message AppendEntriesRequest {
Expand Down
Loading

0 comments on commit 064a515

Please sign in to comment.