Skip to content

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

chore: PG require where only enforces select with a from clause (#10991)

Danny Xuea4bf4c95429
diff --git a/backend/plugin/advisor/pg/advisor_stmt_where_required.go b/backend/plugin/advisor/pg/advisor_stmt_where_required.go
index 1a3b4f987f..fda6d14d98 100644
--- a/backend/plugin/advisor/pg/advisor_stmt_where_required.go
+++ b/backend/plugin/advisor/pg/advisor_stmt_where_required.go
@@ -80,7 +80,8 @@ func (checker *whereRequirementChecker) Visit(node ast.Node) ast.Visitor {
}
// SELECT
case *ast.SelectStmt:
- if n.WhereClause == nil {
+ // Allow SELECT queries without a FROM clause to proceed, e.g. SELECT 1.
+ if n.WhereClause == nil && len(n.GetFromClause()) > 0 {
code = advisor.StatementNoWhere
}
}
diff --git a/backend/plugin/advisor/pg/test/statement_where_require.yaml b/backend/plugin/advisor/pg/test/statement_where_require.yaml
index d3e324df2b..4c2b1a8cb0 100644
--- a/backend/plugin/advisor/pg/test/statement_where_require.yaml
+++ b/backend/plugin/advisor/pg/test/statement_where_require.yaml
@@ -70,3 +70,12 @@
line: 1
column: 0
details: ""
+- statement: SELECT 1
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ column: 0
+ details: ""
diff --git a/backend/plugin/parser/sql/ast/select_stmt.go b/backend/plugin/parser/sql/ast/select_stmt.go
index 17cb518100..d22ae6f963 100644
--- a/backend/plugin/parser/sql/ast/select_stmt.go
+++ b/backend/plugin/parser/sql/ast/select_stmt.go
@@ -1,5 +1,9 @@
package ast
+import (
+ pgquery "github.com/pganalyze/pg_query_go/v4"
+)
+
// SetOperationType is the type for set operations.
type SetOperationType int
@@ -44,6 +48,18 @@ type SelectStmt struct {
PatternLikeList []*PatternLikeDef
// SubqueryList is the list of the subquery nodes.
SubqueryList []*SubqueryDef
+ // fromClause is the raw pgquery from clause nodes.
+ fromClause []*pgquery.Node
+}
+
+// SetFromClause sets the from clause for select statement.
+func (ss *SelectStmt) SetFromClause(fromClause []*pgquery.Node) {
+ ss.fromClause = fromClause
+}
+
+// GetFromClause gets the from clause for select statement.
+func (ss *SelectStmt) GetFromClause() []*pgquery.Node {
+ return ss.fromClause
}
// ByItemDef is the struct for item in order by or group by.
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index 6075f854ec..089d5dd63f 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -1312,6 +1312,8 @@ func convertSelectStmt(in *pgquery.SelectStmt) (*ast.SelectStmt, error) {
selectStmt.SubqueryList = append(selectStmt.SubqueryList, subselect)
}
}
+ selectStmt.SetFromClause(in.FromClause)
+
// Convert WHERE clause
if in.WhereClause != nil {
var err error
diff --git a/backend/plugin/parser/sql/engine/pg/convert_test.go b/backend/plugin/parser/sql/engine/pg/convert_test.go
index 631c5a671a..045a2e75a3 100644
--- a/backend/plugin/parser/sql/engine/pg/convert_test.go
+++ b/backend/plugin/parser/sql/engine/pg/convert_test.go
@@ -1,6 +1,7 @@
package pg
import (
+ "encoding/json"
"testing"
"github.com/stretchr/testify/require"
@@ -43,7 +44,11 @@ func runTests(t *testing.T, tests []testData) {
}
}
}
- require.Equal(t, test.want, res, test.stmt)
+ wantJSON, err := json.Marshal(test.want)
+ require.NoError(t, err)
+ gotJSON, err := json.Marshal(res)
+ require.NoError(t, err)
+ require.Equal(t, string(wantJSON), string(gotJSON), test.stmt)
}
}