Skip to content

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

fix: remove select regexp check (#10707)

h3n4le7313d1a76fa
diff --git a/backend/plugin/parser/pg/query.go b/backend/plugin/parser/pg/query.go
index e9f3f682c1..e279139b8f 100644
--- a/backend/plugin/parser/pg/query.go
+++ b/backend/plugin/parser/pg/query.go
@@ -36,9 +36,19 @@ func validateQuery(statement string) (bool, error) {
if err != nil {
return false, convertToSyntaxError(statement, err)
}
+
+ explainAnalyze := false
for _, stmt := range stmtList {
- switch stmt.(type) {
- case *ast.SelectStmt, *ast.ExplainStmt, *ast.VariableSetStmt:
+ switch stmt := stmt.(type) {
+ case *ast.SelectStmt, *ast.VariableSetStmt:
+ case *ast.ExplainStmt:
+ if stmt.Analyze {
+ // We only support analyze select, because analyze will actually execute the statement.
+ if _, ok := stmt.Statement.(*ast.SelectStmt); !ok {
+ return false, nil
+ }
+ explainAnalyze = true
+ }
default:
return false, nil
}
@@ -52,27 +62,9 @@ func validateQuery(statement string) (bool, error) {
}
formattedStr := strings.ToUpper(strings.TrimSpace(statement))
- if isSelect, _ := regexp.MatchString(`^SELECT\s+?`, formattedStr); isSelect {
- return true, nil
- }
-
- if isSelect, _ := regexp.MatchString(`^SELECT\*\s+?`, formattedStr); isSelect {
- return true, nil
- }
-
- if isExplain, _ := regexp.MatchString(`^EXPLAIN\s+?`, formattedStr); isExplain {
- if isExplainAnalyze, _ := regexp.MatchString(`^EXPLAIN\s+ANALYZE\s+?`, formattedStr); isExplainAnalyze {
- return false, nil
- }
- return true, nil
- }
-
- if isVariableSet, _ := regexp.MatchString(`^SET\s+?`, formattedStr); isVariableSet {
- return true, nil
- }
cteRegex := regexp.MustCompile(`^WITH\s+?`)
- if matchResult := cteRegex.MatchString(formattedStr); matchResult {
+ if matchResult := cteRegex.MatchString(formattedStr); matchResult || explainAnalyze {
var jsonData map[string]any
if err := json.Unmarshal([]byte(jsonText), &jsonData); err != nil {
@@ -85,7 +77,7 @@ func validateQuery(statement string) (bool, error) {
return !keyExistsInJSONData(jsonData, dmlKeyList), nil
}
- return false, nil
+ return true, nil
}
func keyExistsInJSONData(jsonData map[string]any, keyList []string) bool {
diff --git a/backend/plugin/parser/pg/query_test.go b/backend/plugin/parser/pg/query_test.go
index a6b5fa6b40..48571346bc 100644
--- a/backend/plugin/parser/pg/query_test.go
+++ b/backend/plugin/parser/pg/query_test.go
@@ -24,6 +24,14 @@ func TestValidateSQLForEditor(t *testing.T) {
},
{
sql: `explain analyze select * from t`,
+ ans: true,
+ },
+ {
+ sql: `explain analyze insert into t values (1)`,
+ ans: false,
+ },
+ {
+ sql: `EXPLAIN ANALYZE WITH cte1 AS (DELETE FROM t RETURNING id) SELECT * FROM cte1`,
ans: false,
},
{
diff --git a/backend/plugin/parser/sql/ast/explain_stmt.go b/backend/plugin/parser/sql/ast/explain_stmt.go
index d64b005682..efe2e360fc 100644
--- a/backend/plugin/parser/sql/ast/explain_stmt.go
+++ b/backend/plugin/parser/sql/ast/explain_stmt.go
@@ -8,4 +8,5 @@ type ExplainStmt struct {
// Here we only support SELECT now.
// TODO(rebelice): support more.
Statement Node
+ Analyze bool
}
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index b9928f2926..297bdf0bc8 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -601,6 +601,15 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err er
return nil, err
}
}
+ for _, option := range in.ExplainStmt.Options {
+ switch option := option.Node.(type) {
+ case *pgquery.Node_DefElem:
+ if option.DefElem.Defname == "analyze" {
+ explainStmt.Analyze = true
+ }
+ default:
+ }
+ }
return explainStmt, nil
case *pgquery.Node_InsertStmt:
insertStmt := &ast.InsertStmt{
diff --git a/backend/plugin/parser/sql/engine/pg/convert_test.go b/backend/plugin/parser/sql/engine/pg/convert_test.go
index 8ea293a438..aef1be16e5 100644
--- a/backend/plugin/parser/sql/engine/pg/convert_test.go
+++ b/backend/plugin/parser/sql/engine/pg/convert_test.go
@@ -2042,6 +2042,7 @@ func TestExplainStmt(t *testing.T) {
},
},
},
+ Analyze: false,
},
},
statementList: []base.SingleSQL{
@@ -2051,6 +2052,29 @@ func TestExplainStmt(t *testing.T) {
},
},
},
+ {
+ stmt: "EXPLAIN ANALYZE SELECT * FROM tech_book",
+ want: []ast.Node{
+ &ast.ExplainStmt{
+ Statement: &ast.SelectStmt{
+ SetOperation: ast.SetOperationTypeNone,
+ FieldList: []ast.ExpressionNode{
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{},
+ ColumnName: "*",
+ },
+ },
+ },
+ Analyze: true,
+ },
+ },
+ statementList: []base.SingleSQL{
+ {
+ Text: "EXPLAIN ANALYZE SELECT * FROM tech_book",
+ LastLine: 1,
+ },
+ },
+ },
}
runTests(t, tests)