RFC: Redesign some interfaces of SQLClusterRouter
Is your feature request related to a problem? Please describe.
During the investigation to previous issue #399, it really confused me that there is a duplicate call to hybridse::plan::PlanAPI::CreatePlanTreeFromScript hidden in void HandleSQL(const std::string &sql) function of src/cmd/sql_cmd.h file when some SQL is being executed.
First call to CreatePlanTreeFromScript happens in first four lines of the function, it seems normal that the type of SQL should be determined before it calls the relative function in SQLClusterRouter.
For example, when the type of SQL is INSERT, it will call ExecuteInsert of SQLClusterRouter.
// src/cmd/sql_cmd.h
case hybridse::node::kPlanTypeInsert: {
if (db.empty()) {
std::cout << "please use database first" << std::endl;
return;
}
::hybridse::sdk::Status status;
bool ok = sr->ExecuteInsert(db, sql, &status);
if (!ok) {
std::cout << "fail to execute insert" << std::endl;
}
return;
}
However when digging into the ExecuteInsert function, we see a call chain between the following functions GetInsertInfo --> GetSQLPlan -> CreatePlanTreeFromScript. So a duplicate call to CreatePlanTreeFromScript function occurs.
Describe the solution you'd like
I do not know whether the above behavior is designed on purpose or not. But in this case, it seems not a big problem for a CLI program to parse SQL twice.
However what i truly concern is API design for SQLClusterRouter:
- Shall we have a new API to dispatch SQL to right handling function?
- Is it the responsibility of
SQLClusterRouterdispatching SQL? If not, which object should take it?
Describe alternatives you've considered
I do make a hack to the code above that reduce the duplicate calls by changing the parameters of ExecuteInsert function to accept node and plans directly. But the test case of Java SDK failed later when builing the project. So i realized that the SQLClusterRouter is widely used and a discussion of redesign some interface of it should takes first.
@Shouren
Thanks for your investigation and advice.
You're right. There are some redundant calls to CreatePlanTreeFromScript in sql_cmd.h and sql_cluster_router.cc.
However when digging into the ExecuteInsert function, we see a call chain between the following functions GetInsertInfo --> GetSQLPlan -> CreatePlanTreeFromScript. So a duplicate call to CreatePlanTreeFromScript function occurs.
HandleSQLcan be redesigned, since it might duplicate invokeCreatePlanTreeFromScript. Actually, we can simplify dispatch SQL by comparing it withRegExr(e.g.^[\s\t]*INSERT[\s\t\r\n]*,^[\s\t]*SELECT[\s\t\r\n]*,^[\s\t]*CREATE[\s\t\r\n]*).- When it comes to
ExecuteInsert, theCreatePlanTreeFromScriptisn't used to dispatch SQL here. It is necessary to callCreatePlanTreeFromScriptsince we do have to extract raw values from the insert statement. - However, when digging into function
SQLClusterRouter::ExecuteDDL. It might callCreatePlanTreeFromScripttwice when the SQL turns out to be aCREATE TABLEstatement orCREATE INDEXstatement.- Also, we can use
RegExr(e.g.^[\s\t]*CREATE[\s\t\r\n]*PROCEDURE[\s\t\r\n]) to avoid unnecessary parsing work.
- Also, we can use
Very appreciative if you would like to help us to refactor the dispatching logic.
I will try to refactor HandleSQL function in sql_cmd.h and submit a pr later
I will try to refactor
HandleSQLfunction insql_cmd.hand submit a pr later
@Shouren We are planning to add a bunch of commands in CLI.
See issues #460 https://github.com/4paradigm/OpenMLDB/issues/445 #450 #451 etc.
This issue might conflict with the changes you are going to work on.
Maybe we can postpone this issue until these issues are closed.
I will try to refactor
HandleSQLfunction insql_cmd.hand submit a pr later@Shouren We are planning to add a bunch of commands in CLI. See issues #460 #445 #450 #451 etc. This issue might conflict with the changes you are going to work on. Maybe we can postpone this issue until these issues are closed.
Alright