Skip to content

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

fix(advisor/pg): handle nested SELECTs in wrapper statements and CTEs (#19690)

h3n4l72c8a822b6ef
diff --git a/backend/plugin/advisor/pg/advisor_statement_no_select_all.go b/backend/plugin/advisor/pg/advisor_statement_no_select_all.go
index 1ceded02c2..94af67f950 100644
--- a/backend/plugin/advisor/pg/advisor_statement_no_select_all.go
+++ b/backend/plugin/advisor/pg/advisor_statement_no_select_all.go
@@ -57,6 +57,14 @@ func (r *noSelectAllRule) OnStatement(node ast.Node) {
if sel, ok := n.SelectStmt.(*ast.SelectStmt); ok {
r.checkSelectStmt(sel, false)
}
+ case *ast.ViewStmt:
+ if sel, ok := n.Query.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sel, false)
+ }
+ case *ast.CreateTableAsStmt:
+ if sel, ok := n.Query.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sel, false)
+ }
default:
}
}
@@ -126,6 +134,18 @@ func (r *noSelectAllRule) walkSelectSubqueries(sel *ast.SelectStmt) {
if sel == nil {
return
}
+ // Check CTEs (WITH clause).
+ if sel.WithClause != nil && sel.WithClause.Ctes != nil {
+ for _, item := range sel.WithClause.Ctes.Items {
+ if cte, ok := item.(*ast.CommonTableExpr); ok {
+ if sub, ok := cte.Ctequery.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sub, false)
+ }
+ }
+ }
+ }
+ // Check target list for subqueries (e.g., SELECT (SELECT * FROM t)).
+ r.walkNodeListForSelectAll(sel.TargetList)
r.walkNodeListForSelectAll(sel.FromClause)
r.walkNodeForSelectAll(sel.WhereClause)
r.walkNodeForSelectAll(sel.HavingClause)
diff --git a/backend/plugin/advisor/pg/advisor_statement_where_required_select.go b/backend/plugin/advisor/pg/advisor_statement_where_required_select.go
index a511e25666..bd0f717c6f 100644
--- a/backend/plugin/advisor/pg/advisor_statement_where_required_select.go
+++ b/backend/plugin/advisor/pg/advisor_statement_where_required_select.go
@@ -50,11 +50,23 @@ func (*statementWhereRequiredSelectRule) Name() string {
}
func (r *statementWhereRequiredSelectRule) OnStatement(node ast.Node) {
- sel, ok := node.(*ast.SelectStmt)
- if !ok {
- return
+ switch n := node.(type) {
+ case *ast.SelectStmt:
+ r.checkSelectStmt(n, true)
+ case *ast.InsertStmt:
+ if sel, ok := n.SelectStmt.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sel, false)
+ }
+ case *ast.ViewStmt:
+ if sel, ok := n.Query.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sel, false)
+ }
+ case *ast.CreateTableAsStmt:
+ if sel, ok := n.Query.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sel, false)
+ }
+ default:
}
- r.checkSelectStmt(sel, true)
}
// checkSelectStmt recursively checks a SelectStmt for WHERE clause requirement.
@@ -120,6 +132,10 @@ func (r *statementWhereRequiredSelectRule) extractSelectText(sel *ast.SelectStmt
// walkSubqueries walks the AST nodes within a SelectStmt to find nested subqueries.
func (r *statementWhereRequiredSelectRule) walkSubqueries(sel *ast.SelectStmt) {
+ // Check CTEs (WITH clause).
+ if sel.WithClause != nil {
+ r.walkCTEs(sel.WithClause)
+ }
// Check target list expressions for subqueries.
r.walkNodeList(sel.TargetList)
// Check FROM clause for subqueries (e.g., subqueries in FROM).
@@ -137,6 +153,22 @@ func (r *statementWhereRequiredSelectRule) walkSubqueries(sel *ast.SelectStmt) {
r.walkNode(sel.LimitOffset)
}
+// walkCTEs checks each CTE query for SELECT without WHERE.
+func (r *statementWhereRequiredSelectRule) walkCTEs(with *ast.WithClause) {
+ if with.Ctes == nil {
+ return
+ }
+ for _, item := range with.Ctes.Items {
+ cte, ok := item.(*ast.CommonTableExpr)
+ if !ok {
+ continue
+ }
+ if sel, ok := cte.Ctequery.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sel, false)
+ }
+ }
+}
+
func (r *statementWhereRequiredSelectRule) walkNodeList(list *ast.List) {
if list == nil {
return