Skip to content

Commit

Permalink
feat: add sql node
Browse files Browse the repository at this point in the history
  • Loading branch information
dl239 committed Dec 13, 2023
1 parent 050e2ec commit b494faa
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 5 deletions.
44 changes: 44 additions & 0 deletions cases/plan/alter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.

cases:
- id: alter_user
desc: alter user
sql: |
alter user root set options(password="123456");
expect:
node_tree_str: |
+-node[kAlterUserStmt]
+-if_exists: false
+-user: root
+-options:
+-password:
+-expr[primary]
+-value: 123456
+-type: string
- id: alter_user_if_exist
desc: alter user
sql: |
alter user if exists root set options(password="123456");
expect:
node_tree_str: |
+-node[kAlterUserStmt]
+-if_exists: true
+-user: root
+-options:
+-password:
+-expr[primary]
+-value: 123456
+-type: string
17 changes: 17 additions & 0 deletions cases/plan/cmd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ cases:
+-cmd_type: drop database
+-if_exists: true
+-args: [db1]
- id: drop_user
desc: DROP USER IF EXISTS
sql: DROP USER IF EXISTS user1
expect:
node_tree_str: |
+-node[CMD]
+-cmd_type: drop user
+-if_exists: true
+-args: [user1]
- id: drop_user_1
desc: DROP USER
sql: DROP USER user1
expect:
node_tree_str: |
+-node[CMD]
+-cmd_type: drop user
+-args: [user1]
- id: show_deployments
desc: show deployments
sql: SHOW DEPLOYMENTS;
Expand Down
37 changes: 37 additions & 0 deletions cases/plan/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1068,3 +1068,40 @@ cases:
+-0:
+-node[kCompressType]
+-compress_type: snappy
- id: create_user
desc: create user
sql: |
create user root;
expect:
node_tree_str: |
+-node[kCreateUserStmt]
+-if_not_exists: false
+-user: root
+-options: <nil>
- id: create_user_if_not_exist
desc: create user
sql: |
create user if not exists root;
expect:
node_tree_str: |
+-node[kCreateUserStmt]
+-if_not_exists: true
+-user: root
+-options: <nil>
- id: create_user_passwd
desc: create user with password
sql: |
create user root OPTIONS (password="123456");
expect:
node_tree_str: |
+-node[kCreateUserStmt]
+-if_not_exists: false
+-user: root
+-options:
+-password:
+-expr[primary]
+-value: 123456
+-type: string
5 changes: 5 additions & 0 deletions hybridse/include/node/node_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ enum SqlNodeType {
kAlterTableStmt,
kShowStmt,
kCompressType,
kCreateUserStmt,
kAlterUserStmt,
kSqlNodeTypeLast, // debug type
};

Expand Down Expand Up @@ -289,6 +291,7 @@ enum CmdType {
kCmdShowJobLog,
kCmdShowCreateTable,
kCmdTruncate,
kCmdDropUser,
kCmdFake, // not a real cmd, for testing purpose only
kLastCmd = kCmdFake,
};
Expand Down Expand Up @@ -327,6 +330,8 @@ enum PlanType {
kPlanTypeWithClauseEntry,
kPlanTypeAlterTable,
kPlanTypeShow,
kPlanTypeCreateUser,
kPlanTypeAlterUser,
kUnknowPlan = -1,
};

Expand Down
33 changes: 33 additions & 0 deletions hybridse/include/node/plan_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,39 @@ class CreateIndexPlanNode : public LeafPlanNode {
void Print(std::ostream &output, const std::string &orgTab) const;
const CreateIndexNode *create_index_node_;
};

class CreateUserPlanNode : public LeafPlanNode {
public:
explicit CreateUserPlanNode(const std::string& name, bool if_not_exists, std::shared_ptr<OptionsMap> options)
: LeafPlanNode(kPlanTypeCreateUser), name_(name), if_not_exists_(if_not_exists), options_(options) {}
~CreateUserPlanNode() = default;
void Print(std::ostream &output, const std::string &orgTab) const;
const std::string& Name() const { return name_; }
bool IfNotExists() const { return if_not_exists_; }
const std::shared_ptr<OptionsMap> Options() const { return options_; }

private:
const std::string name_;
const bool if_not_exists_ = false;
const std::shared_ptr<OptionsMap> options_;
};

class AlterUserPlanNode : public LeafPlanNode {
public:
explicit AlterUserPlanNode(const std::string& name, bool if_exists, std::shared_ptr<OptionsMap> options)
: LeafPlanNode(kPlanTypeAlterUser), name_(name), if_exists_(if_exists), options_(options) {}
~AlterUserPlanNode() = default;

Check warning on line 746 in hybridse/include/node/plan_node.h

View check run for this annotation

Codecov / codecov/patch

hybridse/include/node/plan_node.h#L744-L746

Added lines #L744 - L746 were not covered by tests
void Print(std::ostream &output, const std::string &orgTab) const;
const std::string& Name() const { return name_; }
bool IfExists() const { return if_exists_; }
const std::shared_ptr<OptionsMap> Options() const { return options_; }

Check warning on line 750 in hybridse/include/node/plan_node.h

View check run for this annotation

Codecov / codecov/patch

hybridse/include/node/plan_node.h#L748-L750

Added lines #L748 - L750 were not covered by tests

private:
const std::string name_;
const bool if_exists_ = false;
const std::shared_ptr<OptionsMap> options_;
};

class CreateProcedurePlanNode : public MultiChildPlanNode {
public:
CreateProcedurePlanNode(const std::string &sp_name, const NodePointVector &input_parameter_list,
Expand Down
46 changes: 45 additions & 1 deletion hybridse/include/node/sql_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,14 @@ typedef std::vector<SqlNode *> NodePointVector;
// supported as:
// - ADD PATH
// - DROP PATH
// - SET OPTIONS
// all else is unsupported
class AlterActionBase : public base::FeBaseObject {
public:
enum class ActionKind {
ADD_PATH = 0,
DROP_PATH
DROP_PATH,
SET_OPTIONS
};

explicit AlterActionBase(ActionKind k) : kind_(k) {}
Expand Down Expand Up @@ -406,6 +408,16 @@ class DropPathAction : public AlterActionBase {
std::string target_;
};

class SetOptionsAction : public AlterActionBase {
public:
explicit SetOptionsAction(std::shared_ptr<OptionsMap> options)
: AlterActionBase(ActionKind::SET_OPTIONS), options_(options) {}
std::string DebugString() const override;
const std::shared_ptr<OptionsMap> Options() const { return options_; }

private:
const std::shared_ptr<OptionsMap> options_;
};

class AlterTableStmt: public SqlNode {
public:
Expand Down Expand Up @@ -2286,6 +2298,38 @@ class CreateIndexNode : public SqlNode {
node::ColumnIndexNode *index_;
};

class CreateUserNode : public SqlNode {
public:
explicit CreateUserNode(const std::string &name,
bool if_not_exists, const std::shared_ptr<OptionsMap>& options)
: SqlNode(kCreateUserStmt, 0, 0),
name_(name), if_not_exists_(if_not_exists), options_(options) {}
void Print(std::ostream &output, const std::string &org_tab) const;
const std::string& Name() const { return name_; }
bool IfNotExists() const { return if_not_exists_; }
const std::shared_ptr<OptionsMap> Options() const { return options_; }

private:
const std::string name_;
bool if_not_exists_;
const std::shared_ptr<OptionsMap> options_;
};

class AlterUserNode : public SqlNode {
public:
explicit AlterUserNode(const std::string &name, bool if_exists, const std::shared_ptr<OptionsMap>& options)
: SqlNode(kAlterUserStmt, 0, 0), name_(name), if_exists_(if_exists), options_(options) {}
void Print(std::ostream &output, const std::string &org_tab) const;
const std::string& Name() const { return name_; }
bool IfExists() const { return if_exists_; }

Check warning on line 2324 in hybridse/include/node/sql_node.h

View check run for this annotation

Codecov / codecov/patch

hybridse/include/node/sql_node.h#L2323-L2324

Added lines #L2323 - L2324 were not covered by tests
const std::shared_ptr<OptionsMap> Options() const { return options_; }

private:
const std::string name_;
bool if_exists_ = false;
const std::shared_ptr<OptionsMap> options_;
};

class ExplainNode : public SqlNode {
public:
explicit ExplainNode(const QueryNode *query, node::ExplainType explain_type)
Expand Down
26 changes: 26 additions & 0 deletions hybridse/src/node/plan_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ std::string NameOfPlanNodeType(const PlanType &type) {
return "kPlanTypeShow";
case kPlanTypeAlterTable:
return "kPlanTypeAlterTable";
case kPlanTypeCreateUser:
return "kPlanTypeCreateUser";
case kPlanTypeAlterUser:
return "kPlanTypeAlterUser";

Check warning on line 230 in hybridse/src/node/plan_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/plan_node.cc#L229-L230

Added lines #L229 - L230 were not covered by tests
case kUnknowPlan:
return std::string("kUnknow");
}
Expand Down Expand Up @@ -708,6 +712,28 @@ void DeployPlanNode::Print(std::ostream &output, const std::string &tab) const {
PrintSqlNode(output, new_tab, Stmt(), "stmt", true);
}

void CreateUserPlanNode::Print(std::ostream &output, const std::string &tab) const {
PlanNode::Print(output, tab);
output << "\n";
std::string new_tab = tab + INDENT;
PrintValue(output, new_tab, IfNotExists() ? "true": "false", "if_not_exists", false);
output << "\n";
PrintValue(output, new_tab, Name(), "name", false);
output << "\n";
PrintValue(output, new_tab, Options().get(), "options", true);
}

void AlterUserPlanNode::Print(std::ostream &output, const std::string &tab) const {
PlanNode::Print(output, tab);
output << "\n";
std::string new_tab = tab + INDENT;
PrintValue(output, new_tab, IfExists() ? "true": "false", "if_exists", false);
output << "\n";
PrintValue(output, new_tab, Name(), "name", false);
output << "\n";
PrintValue(output, new_tab, Options().get(), "options", true);
}

Check warning on line 735 in hybridse/src/node/plan_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/plan_node.cc#L726-L735

Added lines #L726 - L735 were not covered by tests

void LoadDataPlanNode::Print(std::ostream &output, const std::string &org_tab) const {
PlanNode::Print(output, org_tab);

Expand Down
39 changes: 39 additions & 0 deletions hybridse/src/node/sql_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static absl::flat_hash_map<CmdType, absl::string_view> CreateCmdTypeNamesMap() {
{CmdType::kCmdShowTables, "show tables"},
{CmdType::kCmdUseDatabase, "use database"},
{CmdType::kCmdDropDatabase, "drop database"},
{CmdType::kCmdDropUser, "drop user"},

Check warning on line 56 in hybridse/src/node/sql_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/sql_node.cc#L56

Added line #L56 was not covered by tests
{CmdType::kCmdCreateDatabase, "create database"},
{CmdType::kCmdDescTable, "desc table"},
{CmdType::kCmdDropTable, "drop table"},
Expand Down Expand Up @@ -1181,6 +1182,8 @@ static absl::flat_hash_map<SqlNodeType, absl::string_view> CreateSqlNodeTypeToNa
{kSetStmt, "kSetStmt"},
{kDeleteStmt, "kDeleteStmt"},
{kCreateFunctionStmt, "kCreateFunctionStmt"},
{kCreateUserStmt, "kCreateUserStmt"},
{kAlterUserStmt, "kAlterUserStmt"},

Check warning on line 1186 in hybridse/src/node/sql_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/sql_node.cc#L1185-L1186

Added lines #L1185 - L1186 were not covered by tests
{kDynamicUdfFnDef, "kDynamicUdfFnDef"},
{kDynamicUdafFnDef, "kDynamicUdafFnDef"},
{kWithClauseEntry, "kWithClauseEntry"},
Expand Down Expand Up @@ -1627,6 +1630,29 @@ void CreateIndexNode::Print(std::ostream &output, const std::string &org_tab) co
output << "\n";
PrintSqlNode(output, tab, index_, "index", true);
}

void CreateUserNode::Print(std::ostream &output, const std::string &org_tab) const {
SqlNode::Print(output, org_tab);
const std::string tab = org_tab + INDENT + SPACE_ED;
output << "\n";
PrintValue(output, tab, if_not_exists_ ? "true" : "false", "if_not_exists", false);
output << "\n";
PrintValue(output, tab, name_, "user", false);
output << "\n";
PrintValue(output, tab, Options().get(), "options", true);
}

void AlterUserNode::Print(std::ostream &output, const std::string &org_tab) const {
SqlNode::Print(output, org_tab);
const std::string tab = org_tab + INDENT + SPACE_ED;
output << "\n";
PrintValue(output, tab, if_exists_ ? "true" : "false", "if_exists", false);
output << "\n";
PrintValue(output, tab, name_, "user", false);
output << "\n";
PrintValue(output, tab, Options().get(), "options", true);
}

void ExplainNode::Print(std::ostream &output, const std::string &org_tab) const {
SqlNode::Print(output, org_tab);
const std::string tab = org_tab + INDENT + SPACE_ED;
Expand Down Expand Up @@ -2729,6 +2755,19 @@ std::string DropPathAction::DebugString() const {
return absl::Substitute("DropPathAction ($0)", target_);
}

std::string SetOptionsAction::DebugString() const {
std::string output;
for (const auto& kv : *options_) {
if (!output.empty()) {
absl::StrAppend(&output, ", ");

Check warning on line 2762 in hybridse/src/node/sql_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/sql_node.cc#L2758-L2762

Added lines #L2758 - L2762 were not covered by tests
}
absl::StrAppend(&output, kv.first);
absl::StrAppend(&output, "=");
absl::StrAppend(&output, kv.second->GetAsString());

Check warning on line 2766 in hybridse/src/node/sql_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/sql_node.cc#L2764-L2766

Added lines #L2764 - L2766 were not covered by tests
}
return absl::Substitute("SetOptionsAction ($0)", output);

Check warning on line 2768 in hybridse/src/node/sql_node.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/node/sql_node.cc#L2768

Added line #L2768 was not covered by tests
}

bool SetOperationNode::Equals(const SqlNode *node) const {
auto *rhs = dynamic_cast<const SetOperationNode *>(node);
return this->QueryNode::Equals(node) && this->op_type() == rhs->op_type() && this->distinct() == rhs->distinct() &&
Expand Down
14 changes: 14 additions & 0 deletions hybridse/src/plan/planner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,20 @@ base::Status SimplePlanner::CreatePlanTree(const NodePointVector &parser_trees,
plan_trees.push_back(deploy_plan_node);
break;
}
case ::hybridse::node::kCreateUserStmt: {
auto node = dynamic_cast<node::CreateUserNode *>(parser_tree);
auto create_user_plan_node = node_manager_->MakeNode<node::CreateUserPlanNode>(node->Name(),
node->IfNotExists(), node->Options());
plan_trees.push_back(create_user_plan_node);
break;
}
case ::hybridse::node::kAlterUserStmt: {
auto node = dynamic_cast<node::AlterUserNode *>(parser_tree);
auto alter_user_plan_node = node_manager_->MakeNode<node::AlterUserPlanNode>(node->Name(),
node->IfExists(), node->Options());
plan_trees.push_back(alter_user_plan_node);
break;

Check warning on line 773 in hybridse/src/plan/planner.cc

View check run for this annotation

Codecov / codecov/patch

hybridse/src/plan/planner.cc#L768-L773

Added lines #L768 - L773 were not covered by tests
}
case ::hybridse::node::kSetStmt: {
CHECK_TRUE(is_batch_mode_, common::kPlanError,
"Non-support SET Op in online serving");
Expand Down
Loading

0 comments on commit b494faa

Please sign in to comment.