Skip to content

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

feat: support PG no-leading-wildcard-like rule (#2086)

rebelice8c6941950666
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index c1a9675af7..67698787ea 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -123,6 +123,9 @@ const (
// PostgreSQLTableRequirePK is an advisor type for PostgreSQL table require primary key.
PostgreSQLTableRequirePK Type = "bb.plugin.advisor.postgresql.table.require-pk"
+ // PostgreSQLNoLeadingWildcardLike is an advisor type for PostgreSQL no leading wildcard LIKE.
+ PostgreSQLNoLeadingWildcardLike Type = "bb.plugin.advisor.postgresql.where.no-leading-wildcard-like"
+
// PostgreSQLWhereRequirement is an advisor type for PostgreSQL WHERE clause requirement.
PostgreSQLWhereRequirement Type = "bb.plugin.advisor.postgresql.where.require"
)
diff --git a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
new file mode 100644
index 0000000000..d04bad607f
--- /dev/null
+++ b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
@@ -0,0 +1,86 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+const (
+ wildcard = "%"
+)
+
+var (
+ _ advisor.Advisor = (*NoLeadingWildcardLikeAdvisor)(nil)
+ _ ast.Visitor = (*noLeadingWildcardLikeChecker)(nil)
+)
+
+func init() {
+ advisor.Register(advisor.Postgres, advisor.PostgreSQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})
+}
+
+// NoLeadingWildcardLikeAdvisor is the advisor checking for no leading wildcard LIKE.
+type NoLeadingWildcardLikeAdvisor struct {
+}
+
+// Check checks for no leading wildcard LIKE.
+func (*NoLeadingWildcardLikeAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmts, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &noLeadingWildcardLikeChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmts {
+ checker.text = stmt.Text()
+ checker.leadingWildcardLike = false
+ ast.Walk(checker, stmt)
+
+ if checker.leadingWildcardLike {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementLeadingWildcardLike,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", checker.text),
+ })
+ }
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type noLeadingWildcardLikeChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ leadingWildcardLike bool
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *noLeadingWildcardLikeChecker) Visit(node ast.Node) ast.Visitor {
+ if n, ok := node.(*ast.PatternLikeDef); !checker.leadingWildcardLike && ok {
+ if pattern, ok := n.Pattern.(*ast.StringDef); ok && len(pattern.Value) > 0 && pattern.Value[:1] == wildcard {
+ checker.leadingWildcardLike = true
+ }
+ }
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go
new file mode 100644
index 0000000000..dc8c2e7e95
--- /dev/null
+++ b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go
@@ -0,0 +1,73 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestNoLeadingWildcardLike(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: "SELECT * FROM t WHERE a LIKE 'abc%'",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "SELECT * FROM t WHERE a LIKE '%abc'",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementLeadingWildcardLike,
+ Title: "statement.where.no-leading-wildcard-like",
+ Content: "\"SELECT * FROM t WHERE a LIKE '%abc'\" uses leading wildcard LIKE",
+ },
+ },
+ },
+ {
+ Statement: "SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementLeadingWildcardLike,
+ Title: "statement.where.no-leading-wildcard-like",
+ Content: "\"SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'\" uses leading wildcard LIKE",
+ },
+ },
+ },
+ {
+ Statement: "SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementLeadingWildcardLike,
+ Title: "statement.where.no-leading-wildcard-like",
+ Content: "\"SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'\" uses leading wildcard LIKE",
+ },
+ },
+ },
+ {
+ Statement: "SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementLeadingWildcardLike,
+ Title: "statement.where.no-leading-wildcard-like",
+ Content: "\"SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1\" uses leading wildcard LIKE",
+ },
+ },
+ },
+ }
+
+ advisor.RunSchemaReviewRuleTests(t, tests, &NoLeadingWildcardLikeAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleStatementNoLeadingWildcardLike,
+ Level: advisor.SchemaRuleLevelError,
+ Payload: "",
+ }, advisor.MockPostgreSQLDatabase)
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index ff1680d9fa..8a0aa4c928 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -256,12 +256,26 @@ func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
update := &ast.UpdateStmt{
Table: convertRangeVarToTableName(in.UpdateStmt.Relation, ast.TableTypeBaseTable),
}
+ // Convert FROM clause
+ // Here we only find the SELECT stmt in FROM clause
+ for _, item := range in.UpdateStmt.FromClause {
+ if node, ok := item.Node.(*pgquery.Node_RangeSubselect); ok {
+ subselect, err := convertRangeSubselect(node.RangeSubselect)
+ if err != nil {
+ return nil, err
+ }
+ update.SubqueryList = append(update.SubqueryList, subselect)
+ }
+ }
// Convert WHERE clause
if in.UpdateStmt.WhereClause != nil {
var err error
- if update.WhereClause, update.PatternLikeList, update.SubqueryList, err = convertExpressionNode(in.UpdateStmt.WhereClause); err != nil {
+ var subqueryList []*ast.SubqueryDef
+ update.WhereClause, update.PatternLikeList, subqueryList, err = convertExpressionNode(in.UpdateStmt.WhereClause)
+ if err != nil {
return nil, err
}
+ update.SubqueryList = append(update.SubqueryList, subqueryList...)
}
return update, nil
case *pgquery.Node_DeleteStmt:
@@ -473,16 +487,42 @@ func convertSelectStmt(in *pgquery.SelectStmt) (*ast.SelectStmt, error) {
}
selectStmt.FieldList = append(selectStmt.FieldList, convertedNode)
}
+ // Convert FROM clause
+ // Here we only find the SELECT stmt in FROM clause
+ for _, item := range in.FromClause {
+ if node, ok := item.Node.(*pgquery.Node_RangeSubselect); ok {
+ subselect, err := convertRangeSubselect(node.RangeSubselect)
+ if err != nil {
+ return nil, err
+ }
+ selectStmt.SubqueryList = append(selectStmt.SubqueryList, subselect)
+ }
+ }
// Convert WHERE clause
if in.WhereClause != nil {
var err error
- if selectStmt.WhereClause, selectStmt.PatternLikeList, selectStmt.SubqueryList, err = convertExpressionNode(in.WhereClause); err != nil {
+ var subqueryList []*ast.SubqueryDef
+ selectStmt.WhereClause, selectStmt.PatternLikeList, subqueryList, err = convertExpressionNode(in.WhereClause)
+ if err != nil {
return nil, err
}
+ selectStmt.SubqueryList = append(selectStmt.SubqueryList, subqueryList...)
}
return selectStmt, nil
}
+func convertRangeSubselect(node *pgquery.RangeSubselect) (*ast.SubqueryDef, error) {
+ subselect, ok := node.Subquery.Node.(*pgquery.Node_SelectStmt)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected SELECT but found %t", node.Subquery.Node)
+ }
+ res, err := convertSelectStmt(subselect.SelectStmt)
+ if err != nil {
+ return nil, err
+ }
+ return &ast.SubqueryDef{Select: res}, nil
+}
+
func convertSetOperation(t pgquery.SetOperation) (ast.SetOperationType, error) {
switch t {
case pgquery.SetOperation_SETOP_NONE:
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 8aa1652869..0b66ef63cf 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -850,6 +850,47 @@ func TestPGNotNullStmt(t *testing.T) {
func TestPGSelectStmt(t *testing.T) {
tests := []testData{
+ {
+ stmt: "SELECT public.t.a, t.*, t1.* FROM (SELECT * FROM t) t, t1",
+ want: []ast.Node{
+ &ast.SelectStmt{
+ SetOperation: ast.SetOperationTypeNone,
+ FieldList: []ast.ExpressionNode{
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{
+ Schema: "public",
+ Name: "t",
+ },
+ ColumnName: "a",
+ },
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{Name: "t"},
+ ColumnName: "*",
+ },
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{Name: "t1"},
+ ColumnName: "*",
+ },
+ },
+ SubqueryList: []*ast.SubqueryDef{
+ {
+ Select: &ast.SelectStmt{
+ SetOperation: ast.SetOperationTypeNone,
+ FieldList: []ast.ExpressionNode{
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{},
+ ColumnName: "*",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ textList: []string{
+ "SELECT public.t.a, t.*, t1.* FROM (SELECT * FROM t) t, t1",
+ },
+ },
{
stmt: "SELECT public.t.a, t.*, * FROM t",
want: []ast.Node{
@@ -1029,6 +1070,34 @@ func TestPGDropDatabaseStmt(t *testing.T) {
func TestUpdateStmt(t *testing.T) {
tests := []testData{
+ {
+ stmt: "UPDATE tech_book SET a = 1 FROM (SELECT * FROM t) t WHERE a > 1",
+ want: []ast.Node{
+ &ast.UpdateStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ WhereClause: &ast.UnconvertedExpressionDef{},
+ SubqueryList: []*ast.SubqueryDef{
+ {
+ Select: &ast.SelectStmt{
+ SetOperation: ast.SetOperationTypeNone,
+ FieldList: []ast.ExpressionNode{
+ &ast.ColumnNameDef{
+ Table: &ast.TableDef{},
+ ColumnName: "*",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ textList: []string{
+ "UPDATE tech_book SET a = 1 FROM (SELECT * FROM t) t WHERE a > 1",
+ },
+ },
{
stmt: "UPDATE tech_book SET a = 1",
want: []ast.Node{