Files
chaisql/internal/query/statement/explain.go
Asdine El Hrychy 9918cd6f55 Thread safe statements (#406)
Previously, expressions and params were evaluated
during the planning phase. This change builds the query
plan without evaluating params and expressions
which are then evaluated only during the execution phase.
2021-06-04 10:56:23 +04:00

59 lines
1.5 KiB
Go

package statement
import (
"errors"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/database"
"github.com/genjidb/genji/internal/expr"
"github.com/genjidb/genji/internal/stream"
)
// ExplainStmt is a Statement that
// displays information about how a statement
// is going to be executed, without executing it.
type ExplainStmt struct {
Statement Statement
}
// Run analyses the inner statement and displays its execution plan.
// If the statement is a stream, Optimize will be called prior to
// displaying all the operations.
// Explain currently only works on SELECT, UPDATE, INSERT and DELETE statements.
func (stmt *ExplainStmt) Run(tx *database.Transaction, params []expr.Param) (Result, error) {
st, ok := stmt.Statement.(*StreamStmt)
if !ok {
return Result{}, errors.New("EXPLAIN only works on INSERT, SELECT, UPDATE AND DELETE statements")
}
err := st.Prepare(tx)
if err != nil {
return Result{}, err
}
var plan string
if st.PreparedStream != nil {
plan = st.PreparedStream.String()
} else {
plan = "<no exec>"
}
newStatement := StreamStmt{
PreparedStream: &stream.Stream{
Op: stream.Project(
&expr.NamedExpr{
ExprName: "plan",
Expr: expr.LiteralValue(document.NewTextValue(plan)),
}),
},
ReadOnly: true,
}
return newStatement.Run(tx, params)
}
// IsReadOnly indicates that this statement doesn't write anything into
// the database.
func (s *ExplainStmt) IsReadOnly() bool {
return true
}