-
Notifications
You must be signed in to change notification settings - Fork 69
【OSCP Dev】SCDB 支持 explain statement Feat:support Explain statement for SCDB(#478) #675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
fa12954
84ea160
14410ef
71b7164
6e00a83
478cab1
6183c85
b92dbc6
eb23b93
a08f5f6
19c9397
2a9615b
8f5ee99
b742b93
034c377
c7030f3
1b24bbf
258d1f3
8c6f668
9fde404
757e034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright 2023 Ant Group Co., Ltd. | ||
|
|
||
| // Copyright 2015 PingCAP, Inc. | ||
| // | ||
| // 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, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Modified by Ant Group in 2023 | ||
| package executor | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/secretflow/scql/pkg/interpreter" | ||
| "github.com/secretflow/scql/pkg/planner/core" | ||
| "github.com/secretflow/scql/pkg/util/chunk" | ||
| ) | ||
|
|
||
| type ExplainExec struct { | ||
| baseExecutor | ||
| TargetPlan core.Plan | ||
| Format string | ||
| Analyze bool | ||
| done bool | ||
| } | ||
|
|
||
| func (e *ExplainExec) Next(ctx context.Context, req *chunk.Chunk) (err error) { | ||
| if e.done { | ||
| return nil | ||
| } | ||
| e.done = true | ||
| interpreterInstance := interpreter.NewInterpreter() | ||
|
|
||
| // 调用解释器编译,这里需要根据 Compile 方法的实际参数进行调整 | ||
| // 假设 Compile 方法需要的参数可以从上下文或者其他地方获取,以下为示例 | ||
| // 实际使用时需要根据真实情况补充完整参数 | ||
| compiledPlan, err := interpreterInstance.Compile(ctx, nil) // 这里的参数需要根据实际情况修改 | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if compiledPlan.Explain == nil || compiledPlan.Explain.ExeGraphDot == "" { | ||
| return nil | ||
| } | ||
| dotInfo := compiledPlan.Explain.ExeGraphDot | ||
|
|
||
| req.Reset() | ||
|
|
||
| if req.NumCols() == 0 { | ||
| return fmt.Errorf("chunk has no columns") | ||
| } | ||
|
|
||
| col := req.Column(0) | ||
| col.AppendString(dotInfo) | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,40 @@ func (app *App) submitAndGet(ctx context.Context, req *scql.SCDBQueryRequest) *s | |
| return session.result | ||
| } | ||
|
|
||
| // handleExplainQuery | ||
| func (app *App) handleExplainQuery(ctx context.Context, s *session, req *scql.SCDBQueryRequest) *scql.SCDBQueryResultResponse { | ||
| compileReq, err := app.buildCompileRequest(ctx, s) | ||
| if err != nil { | ||
| return newErrorSCDBQueryResultResponse(scql.Code_INTERNAL, err.Error()) | ||
| } | ||
|
|
||
| intrpr := interpreter.NewInterpreter() | ||
| compiledPlan, err := intrpr.Compile(ctx, compileReq) | ||
| if err != nil { | ||
| return newErrorSCDBQueryResultResponse(scql.Code_INTERNAL, err.Error()) | ||
| } | ||
|
||
|
|
||
| if compiledPlan.Explain == nil || compiledPlan.Explain.ExeGraphDot == "" { | ||
| return newErrorSCDBQueryResultResponse(scql.Code_INTERNAL, "no explain information available") | ||
| } | ||
|
|
||
| return &scql.SCDBQueryResultResponse{ | ||
| Status: &scql.Status{ | ||
| Code: int32(scql.Code_OK), | ||
| Message: "success", | ||
| }, | ||
| OutColumns: []*scql.Tensor{ | ||
| { | ||
| Name: "EXPLAIN", | ||
| ElemType: scql.PrimitiveDataType_STRING, | ||
| StringData: []string{compiledPlan.Explain.ExeGraphDot}, | ||
| }, | ||
| }, | ||
| ScdbSessionId: s.id, | ||
| AffectedRows: 0, | ||
| } | ||
| } | ||
|
|
||
| func (app *App) buildCompileRequest(ctx context.Context, s *session) (*scql.CompileQueryRequest, error) { | ||
| issuer := s.GetSessionVars().User | ||
| if issuer.Username == storage.DefaultRootName && issuer.Hostname == storage.DefaultHostName { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
|
|
@@ -190,6 +191,17 @@ func (app *App) runSQL(s *session) { | |
| s.setResult(rt) | ||
| } | ||
| }() | ||
|
|
||
| if strings.HasPrefix(strings.ToUpper(s.request.Query), "EXPLAIN") { | ||
|
||
| req := &scql.SCDBQueryRequest{ | ||
| Query: s.request.Query, | ||
| User: s.request.User, | ||
| DbName: s.request.DbName, | ||
| } | ||
| resp := app.handleExplainQuery(context.Background(), s, req) | ||
| s.result = resp | ||
| return | ||
| } | ||
| needSchema, err := isQueryNeedInfoSchema(s.request.Query) | ||
| if err != nil { | ||
| return | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Nextmethod forExplainExecis currently implemented in a way that will cause a panic. The callinterpreterInstance.Compile(ctx, nil)passes anilrequest, which will lead to a nil pointer dereference inside theCompilefunction. The comment// 这里的参数需要根据实际情况修改also indicates this is incomplete.Furthermore, due to the special handling of
EXPLAINqueries inpkg/scdb/server/submit_and_get_handler.go, thisExplainExecexecutor appears to be dead code. The query is intercepted and handled byhandleExplainQuerybefore it would ever reach the executor building stage for this type of plan.This broken and unused code should be removed to avoid confusion and prevent potential bugs if the query handling logic changes in the future. The related changes in
pkg/scdb/executor/builder.gothat add the build logic forExplainshould also be removed along with this file.