Skip to content

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

feat: allow select * from container query for cosmosdb only (#15272)

h3n4lfef79fac6029
diff --git a/backend/plugin/db/cosmosdb/cosmosdb.go b/backend/plugin/db/cosmosdb/cosmosdb.go
index e309df1fb4..41136f81fc 100644
--- a/backend/plugin/db/cosmosdb/cosmosdb.go
+++ b/backend/plugin/db/cosmosdb/cosmosdb.go
@@ -16,6 +16,7 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/db"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -92,6 +93,12 @@ func (driver *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement stri
return nil, status.Errorf(codes.InvalidArgument, "container argument is required for CosmosDB")
}
+ // Allow `SELECT * FROM {container} [alias]` only.
+ _, _, err := base.ValidateSQLForEditor(storepb.Engine_COSMOSDB, statement)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "support simple SELECT statement for Cosmos DB engine only, err: %s", err.Error())
+ }
+
startTime := time.Now()
container, err := driver.client.NewContainer(driver.databaseName, queryContext.Container)
if err != nil {
diff --git a/backend/plugin/parser/cosmosdb/cosmosdb.go b/backend/plugin/parser/cosmosdb/cosmosdb.go
new file mode 100644
index 0000000000..40b0312ee9
--- /dev/null
+++ b/backend/plugin/parser/cosmosdb/cosmosdb.go
@@ -0,0 +1,46 @@
+package cosmosdb
+
+import (
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/cosmosdb-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+type ParseResult struct {
+ Tree antlr.Tree
+ Tokens *antlr.CommonTokenStream
+}
+
+func ParseCosmosDBQuery(statement string) ([]*ParseResult, error) {
+ lexer := parser.NewCosmosDBLexer(antlr.NewInputStream(statement))
+ stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
+ p := parser.NewCosmosDBParser(stream)
+
+ lexerErrorListener := &base.ParseErrorListener{}
+ lexer.RemoveErrorListeners()
+ lexer.AddErrorListener(lexerErrorListener)
+
+ parserErrorListener := &base.ParseErrorListener{}
+ p.RemoveErrorListeners()
+ p.AddErrorListener(parserErrorListener)
+
+ p.BuildParseTrees = true
+
+ root := p.Root()
+
+ if lexerErrorListener.Err != nil {
+ return nil, lexerErrorListener.Err
+ }
+
+ if parserErrorListener.Err != nil {
+ return nil, parserErrorListener.Err
+ }
+
+ return []*ParseResult{
+ {
+ Tree: root,
+ Tokens: stream,
+ },
+ }, nil
+}
diff --git a/backend/plugin/parser/cosmosdb/query.go b/backend/plugin/parser/cosmosdb/query.go
new file mode 100644
index 0000000000..0745a27de7
--- /dev/null
+++ b/backend/plugin/parser/cosmosdb/query.go
@@ -0,0 +1,19 @@
+package cosmosdb
+
+import (
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func init() {
+ base.RegisterQueryValidator(storepb.Engine_COSMOSDB, validateQuery)
+}
+
+func validateQuery(statement string) (bool, bool, error) {
+ _, err := ParseCosmosDBQuery(statement)
+ if err != nil {
+ return false, false, err
+ }
+
+ return true, true, nil
+}
diff --git a/backend/server/ultimate.go b/backend/server/ultimate.go
index 9ebbcf8947..e6e1883109 100644
--- a/backend/server/ultimate.go
+++ b/backend/server/ultimate.go
@@ -28,6 +28,7 @@ import (
// Parsers.
_ "github.com/bytebase/bytebase/backend/plugin/parser/bigquery"
+ _ "github.com/bytebase/bytebase/backend/plugin/parser/cosmosdb"
_ "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
_ "github.com/bytebase/bytebase/backend/plugin/parser/partiql"
_ "github.com/bytebase/bytebase/backend/plugin/parser/plsql"
diff --git a/go.mod b/go.mod
index a2807b6c74..1f18f18456 100644
--- a/go.mod
+++ b/go.mod
@@ -24,6 +24,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.34.16
github.com/beltran/gohive v1.7.0
github.com/blang/semver/v4 v4.0.0
+ github.com/bytebase/cosmosdb-parser v0.0.0-20250207092310-6249cdad18cb
github.com/bytebase/google-sql-parser v0.0.0-20250116032737-689a327f9465
github.com/bytebase/mysql-parser v0.0.0-20241224071214-cb9fd84811dd
github.com/bytebase/plsql-parser v0.0.0-20250218041636-9fed633593d1
diff --git a/go.sum b/go.sum
index 2322cbaa3c..90449f43d2 100644
--- a/go.sum
+++ b/go.sum
@@ -827,6 +827,8 @@ github.com/bytebase/antlr/v4 v4.0.0-20240827034948-8c385f108920 h1:IfmPt5o5R70NK
github.com/bytebase/antlr/v4 v4.0.0-20240827034948-8c385f108920/go.mod h1:ykhjIPiv0IWpu3OGXCHdz2eUSe8UNGGD6baqjs8jSuU=
github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20250109032656-87cf24d45689 h1:tTxTI91JlBuvbX5JPDJSvCje9TxSUoXOzsbRp8qtcPs=
github.com/bytebase/azure-sdk-for-go/sdk/data/azcosmos v0.0.0-20250109032656-87cf24d45689/go.mod h1:YwUyrNUtcZcibA99JcfCP6UUp95VVQKO2MJfBzgJDwA=
+github.com/bytebase/cosmosdb-parser v0.0.0-20250207092310-6249cdad18cb h1:lGiSU3pSxYP0aa6/qf2IYk5cnSd2diRBHhyYM2Rz5e0=
+github.com/bytebase/cosmosdb-parser v0.0.0-20250207092310-6249cdad18cb/go.mod h1:sivaOoKlMrXkuude2VGsKv6p83CRRGgblNfeVKQka/Y=
github.com/bytebase/gh-ost2 v0.0.0-20250219092957-07c908d5a171 h1:Y3/Wm6WfOQ/HZyc/ewK7iXgVMlTTo1Eb753CnmIiDjk=
github.com/bytebase/gh-ost2 v0.0.0-20250219092957-07c908d5a171/go.mod h1:X0CGYwIZUnjfrv02/nBlftxqSBMm4mYJzFwiyn2D4d8=
github.com/bytebase/go-lsp v0.0.0-20240130071507-c04b5c75010c h1:W832GNANCst4C6TgYbQnKKxbuMZKQxTD4++b3HOuzBo=