Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: support EXPLAIN SELECT statement (#2203)

rebelice1b2e45027db7
diff --git a/plugin/parser/ast/explain_stmt.go b/plugin/parser/ast/explain_stmt.go
index ef0c03abe5..d64b005682 100644
--- a/plugin/parser/ast/explain_stmt.go
+++ b/plugin/parser/ast/explain_stmt.go
@@ -3,4 +3,9 @@ package ast
// ExplainStmt is the struct for explain statement.
type ExplainStmt struct {
node
+
+ // For PostgreSQL, the Statement can be SELECT, INSERT, UPDATE, DELETE, VALUES, EXECUTE, DECLARE, CREATE TABLE AS, or CREATE MATERIALIZED VIEW AS statement.
+ // Here we only support SELECT now.
+ // TODO(rebelice): support more.
+ Statement Node
}
diff --git a/plugin/parser/ast/utils.go b/plugin/parser/ast/utils.go
index 942284de23..a4cc829e44 100644
--- a/plugin/parser/ast/utils.go
+++ b/plugin/parser/ast/utils.go
@@ -106,6 +106,10 @@ func Walk(v Visitor, node Node) {
for _, tableDef := range n.TableList {
Walk(v, tableDef)
}
+ case *ExplainStmt:
+ if n.Statement != nil {
+ Walk(v, n.Statement)
+ }
case *ForeignDef:
if n.Table != nil {
Walk(v, n.Table)
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 2f5dfbcd8a..1d3dccf521 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -322,7 +322,13 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
}, nil
}
case *pgquery.Node_ExplainStmt:
- return &ast.ExplainStmt{}, nil
+ explainStmt := &ast.ExplainStmt{}
+ if query, ok := in.ExplainStmt.Query.Node.(*pgquery.Node_SelectStmt); ok {
+ if explainStmt.Statement, err = convertSelectStmt(query.SelectStmt); err != nil {
+ return nil, err
+ }
+ }
+ return explainStmt, nil
case *pgquery.Node_InsertStmt:
insertStmt := &ast.InsertStmt{
Table: convertRangeVarToTableName(in.InsertStmt.Relation, ast.TableTypeBaseTable),
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 1474859d93..84971315b9 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -1225,7 +1225,17 @@ func TestExplainStmt(t *testing.T) {
{
stmt: "EXPLAIN SELECT * FROM tech_book",
want: []ast.Node{
- &ast.ExplainStmt{},
+ &ast.ExplainStmt{
+ Statement: &ast.SelectStmt{
+ SetOperation: ast.SetOperationTypeNone,
+ FieldList: []ast.ExpressionNode{
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{},
+ ColumnName: "*",
+ },
+ },
+ },
+ },
},
textList: []string{
"EXPLAIN SELECT * FROM tech_book",