Skip to content

Commit

Permalink
feat: support show create table (#3500)
Browse files Browse the repository at this point in the history
  • Loading branch information
dl239 authored Oct 17, 2023
1 parent 024a0d0 commit 88e9e79
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 12 deletions.
17 changes: 17 additions & 0 deletions cases/plan/cmd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,20 @@ cases:
+-actions:
+-0: DropPathAction (12)
+-1: AddPathAction (13)
- id: show-create-table
desc: SHOW CREATE TABLE
sql: SHOW CREATE TABLE t1;
expect:
node_tree_str: |
+-node[CMD]
+-cmd_type: show create table
+-args: [t1]
- id: show-create-table-db
desc: SHOW CREATE TABLE
sql: SHOW CREATE TABLE db1.t1;
expect:
node_tree_str: |
+-node[CMD]
+-cmd_type: show create table
+-args: [db1, t1]
28 changes: 28 additions & 0 deletions docs/en/reference/sql/ddl/SHOW_CREATE_TABLE_STATEMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SHOW CREATE TABLE

`SHOW CREATE TABLE` shows the `CREATE TABLE` statement that creates the named table

**Syntax**

```sql
SHOW CREATE TABLE table_name;
```

**Example**

```sql
show create table t1;
------- ---------------------------------------------------------------
Table Create Table
------- ---------------------------------------------------------------
t1 CREATE TABLE `t1` (
`c1` varchar,
`c2` int,
`c3` bigInt,
`c4` timestamp,
INDEX (KEY=`c1`, TS=`c4`, TTL_TYPE=ABSOLUTE, TTL=0m)
) OPTIONS (PARTITIONNUM=8, REPLICANUM=2, STORAGE_MODE='HDD');
------- ---------------------------------------------------------------

1 rows in set
```
1 change: 1 addition & 0 deletions docs/en/reference/sql/ddl/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ Data Definition Statement (DDL)
CREATE_FUNCTION
SHOW_FUNCTIONS
DROP_FUNCTION
SHOW_CREATE_TABLE_STATEMENT
28 changes: 28 additions & 0 deletions docs/zh/openmldb_sql/ddl/SHOW_CREATE_TABLE_STATEMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SHOW CREATE TABLE

`SHOW CREATE TABLE` 用来显示指定表的建表语句

**Syntax**

```sql
SHOW CREATE TABLE table_name;
```

**Example**

```sql
show create table t1;
------- ---------------------------------------------------------------
Table Create Table
------- ---------------------------------------------------------------
t1 CREATE TABLE `t1` (
`c1` varchar,
`c2` int,
`c3` bigInt,
`c4` timestamp,
INDEX (KEY=`c1`, TS=`c4`, TTL_TYPE=ABSOLUTE, TTL=0m)
) OPTIONS (PARTITIONNUM=8, REPLICANUM=2, STORAGE_MODE='HDD');
------- ---------------------------------------------------------------

1 rows in set
```
1 change: 1 addition & 0 deletions docs/zh/openmldb_sql/ddl/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
CREATE_FUNCTION
SHOW_FUNCTIONS
DROP_FUNCTION
SHOW_CREATE_TABLE_STATEMENT
1 change: 1 addition & 0 deletions hybridse/include/node/node_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ enum CmdType {
kCmdShowFunctions,
kCmdDropFunction,
kCmdShowJobLog,
kCmdShowCreateTable,
kCmdFake, // not a real cmd, for testing purpose only
kLastCmd = kCmdFake,
};
Expand Down
1 change: 1 addition & 0 deletions hybridse/src/node/sql_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static absl::flat_hash_map<CmdType, absl::string_view> CreateCmdTypeNamesMap() {
{CmdType::kCmdDropTable, "drop table"},
{CmdType::kCmdShowProcedures, "show procedures"},
{CmdType::kCmdShowCreateSp, "show create procedure"},
{CmdType::kCmdShowCreateTable, "show create table"},
{CmdType::kCmdDropSp, "drop procedure"},
{CmdType::kCmdDropIndex, "drop index"},
{CmdType::kCmdExit, "exit"},
Expand Down
1 change: 1 addition & 0 deletions hybridse/src/planv2/ast_node_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,7 @@ static const absl::flat_hash_map<std::string_view, ShowTargetInfo> showTargetMap
{"SESSION VARIABLES", {node::CmdType::kCmdShowSessionVariables}},
{"GLOBAL VARIABLES", {node::CmdType::kCmdShowGlobalVariables}},
{"CREATE PROCEDURE", {node::CmdType::kCmdShowCreateSp, true}},
{"CREATE TABLE", {node::CmdType::kCmdShowCreateTable, true}},
{"DEPLOYMENT", {node::CmdType::kCmdShowDeployment, true}},
{"JOB", {node::CmdType::kCmdShowJob, true}},
{"COMPONENTS", {node::CmdType::kCmdShowComponents}},
Expand Down
3 changes: 3 additions & 0 deletions src/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ if(TESTING_ENABLE)
add_executable(db_sdk_test db_sdk_test.cc)
target_link_libraries(db_sdk_test ${SDK_TEST_DEPS} ${BIN_LIBS} ${THIRD_LIBS})

add_executable(sdk_util_test sdk_util_test.cc)
target_link_libraries(sdk_util_test ${SDK_TEST_DEPS} ${BIN_LIBS} ${THIRD_LIBS})

add_executable(result_set_sql_test result_set_sql_test.cc)
target_link_libraries(result_set_sql_test ${SDK_TEST_DEPS} ${BIN_LIBS} ${THIRD_LIBS})

Expand Down
96 changes: 96 additions & 0 deletions src/sdk/sdk_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "sdk/sdk_util.h"

#include <sstream>
#include "codec/schema_codec.h"

namespace openmldb {
namespace sdk {

std::string SDKUtil::GenCreateTableSQL(const ::openmldb::nameserver::TableInfo& table_info) {
std::stringstream ss;
ss << "CREATE TABLE `" << table_info.name() << "` (\n";
for (const auto& column : table_info.column_desc()) {
auto it = openmldb::codec::DATA_TYPE_STR_MAP.find(column.data_type());
if (it != openmldb::codec::DATA_TYPE_STR_MAP.end()) {
ss << "`" << column.name() << "` " << it->second;
if (column.has_default_value()) {
ss << " DEFAULT '" << column.default_value() << "'";
}
if (column.not_null()) {
ss << " NOT NULL";
}
ss << ",\n";
}
}
int index_cnt = 0;
for (const auto& index : table_info.column_key()) {
if (index.flag() != 0) {
continue;
}
if (index_cnt > 0) {
ss << ",\n";
}
ss << "INDEX (";
if (index.col_name_size() == 1) {
ss << "KEY=`" << index.col_name(0) << "`";
} else {
ss << "KEY=(";
for (int idx = 0; idx < index.col_name_size(); idx++) {
if (idx > 0) {
ss << ",";
}
ss << "`" << index.col_name(idx) << "`";
}
ss << ")";
}
if (index.has_ts_name() && !index.ts_name().empty()) {
ss << ", TS=`" << index.ts_name() << "`";
}
if (index.has_ttl()) {
ss << ", TTL_TYPE=";
if (index.ttl().ttl_type() == openmldb::type::TTLType::kAbsoluteTime) {
ss << "ABSOLUTE, TTL=" << index.ttl().abs_ttl() << "m";
} else if (index.ttl().ttl_type() == openmldb::type::TTLType::kLatestTime) {
ss << "LATEST, TTL=" << index.ttl().lat_ttl();
} else if (index.ttl().ttl_type() == openmldb::type::TTLType::kAbsAndLat) {
ss << "ABSANDLAT, TTL=(" << index.ttl().abs_ttl() << "m, " << index.ttl().lat_ttl() << ")";
} else if (index.ttl().ttl_type() == openmldb::type::TTLType::kAbsOrLat) {
ss << "ABSORLAT, TTL=(" << index.ttl().abs_ttl() << "m, " << index.ttl().lat_ttl() << ")";
}
}
index_cnt++;
ss << ")";
}
ss << "\n) ";
ss << "OPTIONS (";
ss << "PARTITIONNUM=" << table_info.partition_num();
ss << ", REPLICANUM=" << table_info.replica_num();
if (table_info.storage_mode() == openmldb::common::StorageMode::kSSD) {
ss << ", STORAGE_MODE='SSD'";
} else if (table_info.storage_mode() == openmldb::common::StorageMode::kHDD) {
ss << ", STORAGE_MODE='HDD'";
} else {
ss << ", STORAGE_MODE='Memory'";
}
ss << ");";
return ss.str();
}

} // namespace sdk
} // namespace openmldb
35 changes: 35 additions & 0 deletions src/sdk/sdk_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef SRC_SDK_SDK_UTIL_H_
#define SRC_SDK_SDK_UTIL_H_

#include <string>

#include "proto/name_server.pb.h"

namespace openmldb {
namespace sdk {

class SDKUtil {
public:
static std::string GenCreateTableSQL(const ::openmldb::nameserver::TableInfo& table_info);
};

} // namespace sdk
} // namespace openmldb

#endif // SRC_SDK_SDK_UTIL_H_
127 changes: 127 additions & 0 deletions src/sdk/sdk_util_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2021 4Paradigm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <vector>

#include "absl/strings/str_split.h"
#include "absl/strings/numbers.h"
#include "gtest/gtest.h"
#include "codec/schema_codec.h"
#include "sdk/sdk_util.h"

namespace openmldb {
namespace sdk {

class SDKUtilTest : public ::testing::Test {
public:
SDKUtilTest() {}
~SDKUtilTest() {}
};

void SetColumnDesc(const std::vector<std::vector<std::string>>& col_vec,
::openmldb::nameserver::TableInfo* table_info) {
table_info->clear_column_desc();
for (const auto& col : col_vec) {
auto col_desc = table_info->add_column_desc();
col_desc->set_name(col[0]);
col_desc->set_data_type(codec::DATA_TYPE_MAP.find(col[1])->second);
if (col.size() > 2 && col[2] == "no") {
col_desc->set_not_null(true);
}
if (col.size() > 3 && !col[3].empty()) {
col_desc->set_default_value(col[3]);
}
}
}

void SetIndex(const std::vector<std::vector<std::string>>& index_vec,
::openmldb::nameserver::TableInfo* table_info) {
table_info->clear_column_key();
for (const auto& index_val : index_vec) {
auto index = table_info->add_column_key();
index->set_index_name(index_val[0]);
std::vector<std::string> list = absl::StrSplit(index_val[1], ",");
for (const auto& name : list) {
index->add_col_name(name);
}
if (!index_val[2].empty()) {
index->set_ts_name(index_val[2]);
}
auto ttl = index->mutable_ttl();
int abs_ttl;
int lat_ttl;
ASSERT_TRUE(absl::SimpleAtoi(index_val[4], &abs_ttl));
ASSERT_TRUE(absl::SimpleAtoi(index_val[5], &lat_ttl));
if (index_val[3] == "absolute") {
ttl->set_ttl_type(openmldb::type::TTLType::kAbsoluteTime);
ttl->set_abs_ttl(abs_ttl);
} else if (index_val[3] == "latest") {
ttl->set_ttl_type(openmldb::type::TTLType::kLatestTime);
ttl->set_lat_ttl(lat_ttl);
} else if (index_val[3] == "absorlat") {
ttl->set_ttl_type(openmldb::type::TTLType::kAbsAndLat);
ttl->set_abs_ttl(abs_ttl);
ttl->set_lat_ttl(lat_ttl);
} else if (index_val[3] == "absandlat") {
ttl->set_ttl_type(openmldb::type::TTLType::kAbsOrLat);
ttl->set_abs_ttl(abs_ttl);
ttl->set_lat_ttl(lat_ttl);
}
}
}

TEST_F(SDKUtilTest, GenCreateTableSQL) {
::openmldb::nameserver::TableInfo table_info;
std::vector<std::vector<std::string>> col = { {"col1", "string"}, {"col2", "int"}, {"col3", "bigint", "no"}};
std::vector<std::vector<std::string>> index = { {"index1", "col1", "", "absolute", "100", "0"}};
SetColumnDesc(col, &table_info);
SetIndex(index, &table_info);
table_info.set_replica_num(1);
table_info.set_partition_num(1);
table_info.set_name("t1");
std::string exp_ddl = "CREATE TABLE `t1` (\n"
"`col1` string,\n"
"`col2` int,\n"
"`col3` bigInt NOT NULL,\n"
"INDEX (KEY=`col1`, TTL_TYPE=ABSOLUTE, TTL=100m)\n"
") OPTIONS (PARTITIONNUM=1, REPLICANUM=1, STORAGE_MODE='Memory');";
ASSERT_EQ(SDKUtil::GenCreateTableSQL(table_info), exp_ddl);
std::vector<std::vector<std::string>> col1 = { {"col1", "string", "no", "aa"},
{"col2", "int"}, {"col3", "timestamp"}};
std::vector<std::vector<std::string>> index1 = { {"index1", "col1", "", "absolute", "100", "0"},
{"index2", "col1,col2", "col3", "absorlat", "100", "10"}};
SetColumnDesc(col1, &table_info);
SetIndex(index1, &table_info);
table_info.set_replica_num(3);
table_info.set_partition_num(8);
table_info.set_storage_mode(openmldb::common::StorageMode::kHDD);
exp_ddl = "CREATE TABLE `t1` (\n"
"`col1` string DEFAULT 'aa' NOT NULL,\n"
"`col2` int,\n"
"`col3` timestamp,\n"
"INDEX (KEY=`col1`, TTL_TYPE=ABSOLUTE, TTL=100m),\n"
"INDEX (KEY=(`col1`,`col2`), TS=`col3`, TTL_TYPE=ABSANDLAT, TTL=(100m, 10))\n"
") OPTIONS (PARTITIONNUM=8, REPLICANUM=3, STORAGE_MODE='HDD');";
ASSERT_EQ(SDKUtil::GenCreateTableSQL(table_info), exp_ddl);
}

} // namespace sdk
} // namespace openmldb

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading

0 comments on commit 88e9e79

Please sign in to comment.