OpenMLDB icon indicating copy to clipboard operation
OpenMLDB copied to clipboard

RFC: Redesign some interfaces of SQLClusterRouter

Open Shouren opened this issue 5 years ago • 5 comments

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:

  1. Shall we have a new API to dispatch SQL to right handling function?
  2. Is it the responsibility of SQLClusterRouter dispatching 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 avatar Sep 14 '21 09:09 Shouren

@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.

  1. HandleSQL can be redesigned, since it might duplicate invoke CreatePlanTreeFromScript. Actually, we can simplify dispatch SQL by comparing it with RegExr (e.g. ^[\s\t]*INSERT[\s\t\r\n]*, ^[\s\t]*SELECT[\s\t\r\n]*, ^[\s\t]*CREATE[\s\t\r\n]*).
  2. When it comes toExecuteInsert, the CreatePlanTreeFromScript isn't used to dispatch SQL here. It is necessary to call CreatePlanTreeFromScript since we do have to extract raw values from the insert statement.
  3. However, when digging into function SQLClusterRouter::ExecuteDDL. It might call CreatePlanTreeFromScript twice when the SQL turns out to be a CREATE TABLE statement or CREATE INDEX statement.
    • Also, we can use RegExr (e.g. ^[\s\t]*CREATE[\s\t\r\n]*PROCEDURE[\s\t\r\n]) to avoid unnecessary parsing work.

jingchen2222 avatar Sep 14 '21 15:09 jingchen2222

Very appreciative if you would like to help us to refactor the dispatching logic.

jingchen2222 avatar Sep 14 '21 15:09 jingchen2222

I will try to refactor HandleSQL function in sql_cmd.h and submit a pr later

Shouren avatar Sep 16 '21 03:09 Shouren

I will try to refactor HandleSQL function in sql_cmd.h and 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.

jingchen2222 avatar Sep 24 '21 22:09 jingchen2222

I will try to refactor HandleSQL function in sql_cmd.h and 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

Shouren avatar Oct 08 '21 06:10 Shouren