Skip to content

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

feat: disallow to access sensitive field in where clause for cosmos db (#15555)

h3n4l9673adf1795a
diff --git a/backend/api/v1/catalog_masking.go b/backend/api/v1/catalog_masking.go
index 1898e75332..258e480a87 100644
--- a/backend/api/v1/catalog_masking.go
+++ b/backend/api/v1/catalog_masking.go
@@ -1,11 +1,92 @@
package v1
import (
+ "github.com/pkg/errors"
+
"github.com/bytebase/bytebase/backend/component/masker"
+ "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"
)
+func getFirstSemanticTypeInPath(ast *base.PathAST, objectSchema *storepb.ObjectSchema) string {
+ if ast == nil || ast.Root == nil || objectSchema == nil {
+ return ""
+ }
+
+ // Skip the first node because it always represents the container.
+ astWoutContainer := base.NewPathAST(ast.Root.GetNext())
+ if astWoutContainer == nil || astWoutContainer.Root == nil {
+ return ""
+ }
+
+ if objectSchema.SemanticType != "" {
+ return objectSchema.SemanticType
+ }
+
+ os := objectSchema
+
+ for node := astWoutContainer.Root; node != nil; node = node.GetNext() {
+ if node.GetIdentifier() == "" {
+ return ""
+ }
+
+ switch node := node.(type) {
+ case *base.ItemSelector:
+ if os.Type != storepb.ObjectSchema_OBJECT {
+ return ""
+ }
+ var valid bool
+ if v := os.GetStructKind().GetProperties(); v != nil {
+ if child, ok := v[node.GetIdentifier()]; ok {
+ os = child
+ valid = true
+ }
+ }
+ if !valid {
+ return ""
+ }
+ case *base.ArraySelector:
+ if os.Type != storepb.ObjectSchema_OBJECT {
+ return ""
+ }
+ var valid bool
+ if v := os.GetStructKind().GetProperties(); v != nil {
+ if child, ok := v[node.GetIdentifier()]; ok {
+ os = child
+ valid = true
+ }
+ }
+ if !valid {
+ return ""
+ }
+
+ if os.Type != storepb.ObjectSchema_ARRAY {
+ return ""
+ }
+
+ os = os.GetArrayKind().GetKind()
+ if os == nil {
+ return ""
+ }
+ }
+
+ if os.SemanticType != "" {
+ return os.SemanticType
+ }
+ }
+
+ return ""
+}
+
+func maskCosmosDB(span *base.QuerySpan, data any, objectSchema *storepb.ObjectSchema, semanticTypeToMasker map[string]masker.Masker) (any, error) {
+ if len(span.Results) == 1 && len(span.Results[0].SourceFieldPaths) == 0 {
+ // SELECT * FROM c
+ return walkAndMaskJSON(data, objectSchema, semanticTypeToMasker)
+ }
+ return nil, errors.New("unsupported statement for CosmosDB masking")
+}
+
func walkAndMaskJSON(data any, objectSchema *storepb.ObjectSchema, semanticTypeToMasker map[string]masker.Masker) (any, error) {
switch data := data.(type) {
case map[string]any:
diff --git a/backend/api/v1/catalog_masking_test.go b/backend/api/v1/catalog_masking_test.go
index 63b62c25bd..6c12127b42 100644
--- a/backend/api/v1/catalog_masking_test.go
+++ b/backend/api/v1/catalog_masking_test.go
@@ -7,9 +7,133 @@ import (
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/backend/component/masker"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
+func TestGetFirstSemanticTypeInPath(t *testing.T) {
+ containerName := "container"
+ containerNode := base.NewItemSelector(containerName)
+
+ testCases := []struct {
+ nodes []base.SelectorNode
+ objectSchema *storepb.ObjectSchema
+ want string
+ }{
+ {
+ nodes: []base.SelectorNode{
+ base.NewItemSelector("a"),
+ },
+ objectSchema: &storepb.ObjectSchema{
+ Type: storepb.ObjectSchema_OBJECT,
+ Kind: &storepb.ObjectSchema_StructKind_{
+ StructKind: &storepb.ObjectSchema_StructKind{
+ Properties: map[string]*storepb.ObjectSchema{
+ "a": {
+ SemanticType: "st-a",
+ Type: storepb.ObjectSchema_STRING,
+ },
+ },
+ },
+ },
+ },
+ want: "st-a",
+ },
+ {
+ nodes: []base.SelectorNode{
+ base.NewItemSelector("a"),
+ base.NewArraySelector("b", 1),
+ base.NewItemSelector("c"),
+ },
+ objectSchema: &storepb.ObjectSchema{
+ Type: storepb.ObjectSchema_OBJECT,
+ Kind: &storepb.ObjectSchema_StructKind_{
+ StructKind: &storepb.ObjectSchema_StructKind{
+ Properties: map[string]*storepb.ObjectSchema{
+ "a": {
+ Type: storepb.ObjectSchema_OBJECT,
+ Kind: &storepb.ObjectSchema_StructKind_{
+ StructKind: &storepb.ObjectSchema_StructKind{
+ Properties: map[string]*storepb.ObjectSchema{
+ "b": {
+ Type: storepb.ObjectSchema_ARRAY,
+ SemanticType: "st-b",
+ Kind: &storepb.ObjectSchema_ArrayKind_{
+ ArrayKind: &storepb.ObjectSchema_ArrayKind{
+ Kind: &storepb.ObjectSchema{
+ Type: storepb.ObjectSchema_OBJECT,
+ Kind: &storepb.ObjectSchema_StructKind_{
+ StructKind: &storepb.ObjectSchema_StructKind{
+ Properties: map[string]*storepb.ObjectSchema{
+ "c": {
+ Type: storepb.ObjectSchema_STRING,
+ SemanticType: "st-c",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ want: "st-c",
+ },
+ {
+ nodes: []base.SelectorNode{
+ base.NewItemSelector("a"),
+ base.NewItemSelector("c"),
+ },
+ objectSchema: &storepb.ObjectSchema{
+ Type: storepb.ObjectSchema_OBJECT,
+ Kind: &storepb.ObjectSchema_StructKind_{
+ StructKind: &storepb.ObjectSchema_StructKind{
+ Properties: map[string]*storepb.ObjectSchema{
+ "a": {
+ Type: storepb.ObjectSchema_OBJECT,
+ Kind: &storepb.ObjectSchema_StructKind_{
+ StructKind: &storepb.ObjectSchema_StructKind{
+ Properties: map[string]*storepb.ObjectSchema{
+ "b": {
+ Type: storepb.ObjectSchema_STRING,
+ SemanticType: "st-b",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ want: "",
+ },
+ }
+
+ for _, tc := range testCases {
+ if len(tc.nodes) == 0 {
+ continue
+ }
+
+ ast := base.NewPathAST(containerNode)
+ next := ast.Root
+ for i := 0; i < len(tc.nodes); i++ {
+ next.SetNext(tc.nodes[i])
+ next = next.GetNext()
+ }
+
+ got := getFirstSemanticTypeInPath(ast, tc.objectSchema)
+ require.Equal(t, tc.want, got)
+ }
+}
+
func TestWalkAndMaskJSON(t *testing.T) {
type testCase struct {
description string
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 0478feca78..5736ef8a72 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -523,10 +523,24 @@ func queryRetry(
if licenseService.IsFeatureEnabledForInstance(api.FeatureSensitiveData, instance) == nil && !queryContext.Explain {
// TODO(zp): Refactor Document Database and RDBMS to use the same masking logic.
if instance.Metadata.GetEngine() == storepb.Engine_COSMOSDB {
+ if len(spans) != 1 {
+ return nil, nil, duration, status.Error(codes.Internal, "expected one span for CosmosDB")
+ }
objectSchema, err := getCosmosDBContainerObjectSchema(ctx, stores, database.InstanceID, database.DatabaseName, queryContext.Container)
if err != nil {
return nil, nil, duration, status.Error(codes.Internal, err.Error())
}
+ for pathStr, predicatePath := range spans[0].PredicatePaths {
+ semanticType := getFirstSemanticTypeInPath(predicatePath, objectSchema)
+ if semanticType != "" {
+ for _, result := range results {
+ result.Error = fmt.Sprintf("using path %q tagged by semantic type %q in WHERE clause is not allowed", pathStr, semanticType)
+ result.Rows = nil
+ result.RowsCount = 0
+ }
+ return results, spans, duration, nil
+ }
+ }
if objectSchema != nil {
// We store one query result document in one row.
for _, result := range results {
@@ -548,7 +562,7 @@ func queryRetry(
return nil, nil, duration, status.Errorf(codes.Internal, "failed to unmarshal document: %v", err)
}
// Mask the document.
- maskedDoc, err := walkAndMaskJSON(doc, objectSchema, semanticTypeToMaskerMap)
+ maskedDoc, err := maskCosmosDB(spans[0], doc, objectSchema, semanticTypeToMaskerMap)
if err != nil {
return nil, nil, duration, status.Errorf(codes.Internal, "failed to mask document: %v", err)
}
diff --git a/backend/plugin/parser/base/json_path.go b/backend/plugin/parser/base/json_path.go
new file mode 100644
index 0000000000..f1a2b782aa
--- /dev/null
+++ b/backend/plugin/parser/base/json_path.go
@@ -0,0 +1,122 @@
+package base
+
+import (
+ "strconv"
+ "strings"
+)
+
+var (
+ _ SelectorNode = (*ItemSelector)(nil)
+ _ SelectorNode = (*ArraySelector)(nil)
+)
+
+type PathAST struct {
+ Root SelectorNode
+}
+
+type SelectorNode interface {
+ toString() string
+ GetIdentifier() string
+ SetNext(SelectorNode)
+ GetNext() SelectorNode
+}
+
+type ItemSelector struct {
+ Identifier string
+ Next SelectorNode
+}
+
+type ArraySelector struct {
+ Identifier string
+ Index int
+ Next SelectorNode
+}
+
+func NewPathAST(selectorNode SelectorNode) *PathAST {
+ return &PathAST{
+ Root: selectorNode,
+ }
+}
+
+func (p *PathAST) String() (string, error) {
+ return p.Root.toString(), nil
+}
+
+func NewItemSelector(identifier string) *ItemSelector {
+ return &ItemSelector{
+ Identifier: identifier,
+ }
+}
+
+func (n *ItemSelector) toString() string {
+ sb := new(strings.Builder)
+ if _, err := sb.WriteString(n.Identifier); err != nil {
+ return ""
+ }
+ if v := n.Next; v != nil {
+ if _, err := sb.WriteString("."); err != nil {
+ return ""
+ }
+ if _, err := sb.WriteString(v.toString()); err != nil {
+ return ""
+ }
+ }
+
+ return sb.String()
+}
+
+func (n *ItemSelector) GetNext() SelectorNode {
+ return n.Next
+}
+
+func (n *ItemSelector) SetNext(next SelectorNode) {
+ n.Next = next
+}
+
+func (n *ItemSelector) GetIdentifier() string {
+ return n.Identifier
+}
+
+func NewArraySelector(identifier string, index int) *ArraySelector {
+ return &ArraySelector{
+ Identifier: identifier,
+ Index: index,
+ }
+}
+func (n *ArraySelector) toString() string {
+ sb := new(strings.Builder)
+ if _, err := sb.WriteString(n.Identifier); err != nil {
+ return ""
+ }
+ if _, err := sb.WriteString("["); err != nil {
+ return ""
+ }
+ if _, err := sb.WriteString(strconv.Itoa(n.Index)); err != nil {
+ return ""
+ }
+ if _, err := sb.WriteString("]"); err != nil {
+ return ""
+ }
+ if v := n.Next; v != nil {
+ if _, err := sb.WriteString("."); err != nil {
+ return ""
+ }
+ if _, err := sb.WriteString(v.toString()); err != nil {
+ return ""
+ }
+ }
+
+ return sb.String()
+}
+
+func (n *ArraySelector) GetNext() SelectorNode {
+ return n.Next
+}
+
+func (n *ArraySelector) SetNext(next SelectorNode) {
+ n.Next = next
+}
+
+func (n *ArraySelector) GetIdentifier() string {
+ return n.Identifier
+}
diff --git a/backend/plugin/parser/base/json_path_test.go b/backend/plugin/parser/base/json_path_test.go
new file mode 100644
index 0000000000..f0566e43c7
--- /dev/null
+++ b/backend/plugin/parser/base/json_path_test.go
@@ -0,0 +1,42 @@
+package base
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestString(t *testing.T) {
+ testCases := []struct {
+ nodes []SelectorNode
+ want string
+ }{
+ {
+ nodes: []SelectorNode{
+ NewItemSelector("a"),
+ NewArraySelector("b", 1),
+ NewItemSelector("c"),
+ },
+ want: "a.b[1].c",
+ },
+ }
+
+ a := require.New(t)
+
+ for _, tc := range testCases {
+ if len(tc.nodes) == 0 {
+ continue
+ }
+
+ ast := NewPathAST(tc.nodes[0])
+ next := ast.Root
+ for i := 1; i < len(tc.nodes); i++ {
+ next.SetNext(tc.nodes[i])
+ next = next.GetNext()
+ }
+
+ got, err := ast.String()
+ a.NoError(err)
+ a.Equal(tc.want, got)
+ }
+}
diff --git a/backend/plugin/parser/base/span.go b/backend/plugin/parser/base/span.go
index e1b6ea022c..29a4f86841 100644
--- a/backend/plugin/parser/base/span.go
+++ b/backend/plugin/parser/base/span.go
@@ -62,7 +62,10 @@ type QuerySpan struct {
SourceColumns SourceColumnSet
// PredicateColumns are the source columns contributing to the span.
// PredicateColumns here are the source columns for the where conditions.
- PredicateColumns SourceColumnSet
+ PredicateColumns SourceColumnSet
+ // PredicatePaths is predicateColumns used by Cosmos DB only, to store the path of the field, all the
+ // paths should begin with the container name.
+ PredicatePaths map[string]*PathAST
NotFoundError error
FunctionNotSupportedError error
}
@@ -75,6 +78,9 @@ type QuerySpanResult struct {
SourceColumns SourceColumnSet
// IsPlainField indicates whether the field is a plain column reference (true) or an expression (false).
IsPlainField bool
+ // SourceFieldPaths is used for Cosmos DB only, to store the path of the field, the root is the document($). Empty
+ // for select * statement.
+ SourceFieldPaths map[string]bool
}
// ColumnResource is the resource key for a column.
diff --git a/backend/plugin/parser/cosmosdb/query_span.go b/backend/plugin/parser/cosmosdb/query_span.go
new file mode 100644
index 0000000000..42375f6b5e
--- /dev/null
+++ b/backend/plugin/parser/cosmosdb/query_span.go
@@ -0,0 +1,225 @@
+package cosmosdb
+
+import (
+ "context"
+ "log/slog"
+ "strconv"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/cosmosdb-parser"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/common/log"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func init() {
+ base.RegisterGetQuerySpan(storepb.Engine_COSMOSDB, GetQuerySpan)
+}
+
+func GetQuerySpan(_ context.Context, _ base.GetQuerySpanContext, statement, _, _ string, _ bool) (*base.QuerySpan, error) {
+ return getQuerySpanImpl(statement)
+}
+
+func getQuerySpanImpl(statement string) (*base.QuerySpan, error) {
+ parseResults, err := ParseCosmosDBQuery(statement)
+ if err != nil {
+ return nil, err
+ }
+
+ if len(parseResults) == 0 {
+ return nil, errors.New("no parse result")
+ }
+
+ ast := parseResults[0].Tree
+ querySpanResultListener := &querySpanResultListener{}
+ antlr.ParseTreeWalkerDefault.Walk(querySpanResultListener, ast)
+ if querySpanResultListener.err != nil {
+ return nil, err
+ }
+
+ querySpanPredicatePathsListener := &querySpanPredicatePathsListener{}
+ antlr.ParseTreeWalkerDefault.Walk(querySpanPredicatePathsListener, ast)
+ if querySpanPredicatePathsListener.err != nil {
+ return nil, err
+ }
+ return &base.QuerySpan{
+ Results: querySpanResultListener.result,
+ PredicatePaths: querySpanPredicatePathsListener.predicatePaths,
+ }, nil
+}
+
+type querySpanResultListener struct {
+ *parser.BaseCosmosDBParserListener
+
+ result []base.QuerySpanResult
+
+ err error
+}
+
+func (l *querySpanResultListener) EnterSelect(ctx *parser.SelectContext) {
+ // TODO(zp): Considering the case of multiple from sources once we support it.
+ if ctx.Select_clause().Select_specification().MULTIPLY_OPERATOR() != nil {
+ l.result = []base.QuerySpanResult{
+ {
+ Name: "",
+ SourceFieldPaths: map[string]bool{},
+ },
+ }
+ return
+ }
+}
+
+type querySpanPredicatePathsListener struct {
+ *parser.BaseCosmosDBParserListener
+
+ predicatePaths map[string]*base.PathAST
+ err error
+}
+
+func (l *querySpanPredicatePathsListener) EnterSelect(ctx *parser.SelectContext) {
+ // Extracting predicate fields from where clause.
+ whereClause := ctx.Where_clause()
+ if whereClause == nil {
+ return
+ }
+ fromClause := ctx.From_clause()
+ if fromClause == nil {
+ return
+ }
+
+ var originalContainerName string
+ var fromIdentifier string
+ if i := fromClause.From_specification().From_source().Container_expression().Container_name().IDENTIFIER(); i != nil {
+ originalContainerName = i.GetText()
+ }
+ // Alias in the from source will shadow the original identifier.
+ if i := fromClause.From_specification().From_source().Container_expression().IDENTIFIER(); i != nil {
+ fromIdentifier = i.GetText()
+ }
+
+ predicateFields, err := extractPredicateFieldsFromWhereClause(whereClause, originalContainerName, fromIdentifier)
+ if err != nil {
+ l.err = err
+ return
+ }
+ l.predicatePaths = predicateFields
+}
+
+func extractPredicateFieldsFromWhereClause(ctx parser.IWhere_clauseContext, originalContainerName string, fromAlias string) (map[string]*base.PathAST, error) {
+ scalarExpression := ctx.Scalar_expression_in_where()
+
+ paths := extractPredicateFieldsFromScalarExpressionInWhere(scalarExpression, originalContainerName, fromAlias)
+
+ r := make(map[string]*base.PathAST)
+ for _, path := range paths {
+ if len(path) == 0 {
+ continue
+ }
+
+ ast := base.NewPathAST(path[0])
+ var current base.SelectorNode = ast.Root
+ for i := 1; i < len(path); i++ {
+ current.SetNext(path[i])
+ current = current.GetNext()
+ }
+
+ str, err := ast.String()
+ if err != nil {
+ slog.Warn("failed to convert path ast to string", log.BBError(err))
+ }
+ r[str] = ast
+ }
+
+ return r, nil
+}
+
+func extractPredicateFieldsFromScalarExpressionInWhere(ctx parser.IScalar_expression_in_whereContext, originalContainerName string, fromAlias string) [][]base.SelectorNode {
+ if ctx == nil {
+ return nil
+ }
+
+ switch {
+ case ctx.Input_alias() != nil:
+ name := ctx.Input_alias().IDENTIFIER().GetText()
+ if fromAlias != "" && name == fromAlias {
+ name = originalContainerName
+ }
+ return [][]base.SelectorNode{
+ {
+ base.NewItemSelector(name),
+ },
+ }
+ case ctx.AND_SYMBOL() != nil, ctx.OR_SYMBOL() != nil:
+ allScalarExpressionInWheres := ctx.AllScalar_expression_in_where()
+ var allPaths [][]base.SelectorNode
+ for _, expr := range allScalarExpressionInWheres {
+ paths := extractPredicateFieldsFromScalarExpressionInWhere(expr, originalContainerName, fromAlias)
+ allPaths = append(allPaths, paths...)
+ }
+ return allPaths
+ case ctx.DOT_SYMBOL() != nil:
+ // Most usual case like a.b.c.d.
+ paths := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(0), originalContainerName, fromAlias)
+ for i := range paths {
+ paths[i] = append(paths[i], base.NewItemSelector(ctx.Property_name().IDENTIFIER().GetText()))
+ }
+ return paths
+ case ctx.LS_BRACKET_SYMBOL() != nil:
+ paths := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(0), originalContainerName, fromAlias)
+ for i, path := range paths {
+ switch {
+ case ctx.Property_name() != nil:
+ paths[i] = append(path, base.NewItemSelector(ctx.Property_name().IDENTIFIER().GetText()))
+ case ctx.Array_index() != nil:
+ if len(paths[i]) == 0 {
+ break
+ }
+ index, err := strconv.Atoi(ctx.Array_index().GetText())
+ if err != nil {
+ slog.Warn("cannot convert array index to int", slog.String("index", ctx.Array_index().GetText()))
+ break
+ }
+ // Rebuild the ast because of the different level of array index and array name.
+ last := paths[i][len(paths[i])-1]
+ paths[i][len(paths[i])-1] = base.NewArraySelector(last.GetIdentifier(), index)
+ }
+ }
+ return paths
+ case ctx.Unary_operator() != nil:
+ return extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(0), originalContainerName, fromAlias)
+ case ctx.Binary_operator() != nil:
+ left := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(0), originalContainerName, fromAlias)
+ right := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(1), originalContainerName, fromAlias)
+ return append(left, right...)
+ case ctx.QUESTION_MARK_SYMBOL() != nil:
+ left := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(0), originalContainerName, fromAlias)
+ mid := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(1), originalContainerName, fromAlias)
+ right := extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(2), originalContainerName, fromAlias)
+ return append(append(left, mid...), right...)
+ case ctx.Scalar_function_expression() != nil:
+ switch {
+ case ctx.Scalar_function_expression().Udf_scalar_function_expression() != nil:
+ allScalarExpressionInWheres := ctx.Scalar_function_expression().Udf_scalar_function_expression().AllScalar_expression_in_where()
+ var paths [][]base.SelectorNode
+ for _, expr := range allScalarExpressionInWheres {
+ path := extractPredicateFieldsFromScalarExpressionInWhere(expr, originalContainerName, fromAlias)
+ paths = append(paths, path...)
+ }
+ return paths
+ case ctx.Scalar_function_expression().Builtin_function_expression() != nil:
+ allScalarExpressionInWheres := ctx.Scalar_function_expression().Builtin_function_expression().AllScalar_expression_in_where()
+ var paths [][]base.SelectorNode
+ for _, expr := range allScalarExpressionInWheres {
+ path := extractPredicateFieldsFromScalarExpressionInWhere(expr, originalContainerName, fromAlias)
+ paths = append(paths, path...)
+ }
+ return paths
+ }
+ case ctx.LR_BRACKET_SYMBOL() != nil:
+ return extractPredicateFieldsFromScalarExpressionInWhere(ctx.Scalar_expression_in_where(0), originalContainerName, fromAlias)
+ }
+
+ return nil
+}
diff --git a/backend/plugin/parser/cosmosdb/query_span_test.go b/backend/plugin/parser/cosmosdb/query_span_test.go
new file mode 100644
index 0000000000..9501f07516
--- /dev/null
+++ b/backend/plugin/parser/cosmosdb/query_span_test.go
@@ -0,0 +1,48 @@
+package cosmosdb
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestPredicateInWhere(t *testing.T) {
+ testCases := []struct {
+ statement string
+ predicatePaths map[string]bool
+ }{
+ {
+ statement: "SELECT * FROM container WHERE container.name = 'test'",
+ predicatePaths: map[string]bool{
+ "container.name": true,
+ },
+ },
+ {
+ statement: "SELECT * FROM container WHERE container.addresses[1].country = 'Canada'",
+ predicatePaths: map[string]bool{
+ "container.addresses[1].country": true,
+ },
+ },
+ {
+ statement: "SELECT * FROM container c WHERE udf.foo(c.name, c.salary, c.addresses[1].country) = c.age",
+ predicatePaths: map[string]bool{
+ "container.name": true,
+ "container.salary": true,
+ "container.addresses[1].country": true,
+ "container.age": true,
+ },
+ },
+ }
+
+ a := require.New(t)
+
+ for _, tc := range testCases {
+ querySpan, err := getQuerySpanImpl(tc.statement)
+ a.Nil(err)
+ a.Equal(len(tc.predicatePaths), len(querySpan.PredicatePaths))
+ for path := range tc.predicatePaths {
+ _, ok := querySpan.PredicatePaths[path]
+ a.True(ok)
+ }
+ }
+}
diff --git a/backend/plugin/parser/cosmosdb/split.go b/backend/plugin/parser/cosmosdb/split.go
new file mode 100644
index 0000000000..fa70d74f59
--- /dev/null
+++ b/backend/plugin/parser/cosmosdb/split.go
@@ -0,0 +1,20 @@
+package cosmosdb
+
+import (
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func init() {
+ base.RegisterSplitterFunc(storepb.Engine_COSMOSDB, SplitSQL)
+}
+
+// SplitSQL splits the given SQL statement into multiple SQL statements.
+func SplitSQL(statement string) ([]base.SingleSQL, error) {
+ // TODO(zp): Does CosmosDB support multiple statements? And how we use the split function?
+ return []base.SingleSQL{
+ {
+ Text: statement,
+ },
+ }, nil
+}
diff --git a/backend/plugin/parser/mysql/query_span_extractor.go b/backend/plugin/parser/mysql/query_span_extractor.go
index 282c08c35d..2827e1a7f3 100644
--- a/backend/plugin/parser/mysql/query_span_extractor.go
+++ b/backend/plugin/parser/mysql/query_span_extractor.go
@@ -101,7 +101,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba
}
q.ctx = ctx
- // We assumes the caller had handled the statement type case,
+ // We assume the caller had handled the statement type case,
// so we only need to handle the determined statement type here.
// In order to decrease the maintenance cost, we use listener to handle
// the select statement precisely instead of using type switch.
diff --git a/backend/plugin/parser/tsql/query_span_extractor.go b/backend/plugin/parser/tsql/query_span_extractor.go
index 95ac771ddb..145bb85ed1 100644
--- a/backend/plugin/parser/tsql/query_span_extractor.go
+++ b/backend/plugin/parser/tsql/query_span_extractor.go
@@ -28,7 +28,7 @@ type querySpanExtractor struct {
gCtx base.GetQuerySpanContext
// Private fields.
// ctes is used to record the common table expressions (CTEs) in the query.
- // It should be shrunk to the privious length while exiting the query scope.
+ // It should be shrunk to the previous length while exiting the query scope.
ctes []*base.PseudoTable
// outerTableSources is used to record the outer table sources in the query.
@@ -97,7 +97,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)
}, nil
}
- // We assumes the caller had handled the statement type case,
+ // We assume the caller had handled the statement type case,
// so we only need to handle the determined statement type here.
// In order to decrease the maintenance cost, we use listener
// to handle the select statement precisely.
diff --git a/go.mod b/go.mod
index c4e4c2dbf7..9640dfc390 100644
--- a/go.mod
+++ b/go.mod
@@ -25,7 +25,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.35.2
github.com/beltran/gohive v1.8.0
github.com/blang/semver/v4 v4.0.0
- github.com/bytebase/cosmosdb-parser v0.0.0-20250207092310-6249cdad18cb
+ github.com/bytebase/cosmosdb-parser v0.0.0-20250314070019-4ba8fb2edf87
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 54580dd5de..e02677e5cf 100644
--- a/go.sum
+++ b/go.sum
@@ -827,8 +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/cosmosdb-parser v0.0.0-20250314070019-4ba8fb2edf87 h1:4I+uFuNCrxWYMi/HmYoyWhp5y9mBPVUoSqP8Gw6zFUc=
+github.com/bytebase/cosmosdb-parser v0.0.0-20250314070019-4ba8fb2edf87/go.mod h1:sivaOoKlMrXkuude2VGsKv6p83CRRGgblNfeVKQka/Y=
github.com/bytebase/gh-ost2 v1.1.7-0.20250310031106-33e16ca4b2e0 h1:wm4Set2lwArmC4f9hYdiunXtEYYzr6SyYRepTXNdkm4=
github.com/bytebase/gh-ost2 v1.1.7-0.20250310031106-33e16ca4b2e0/go.mod h1:X0CGYwIZUnjfrv02/nBlftxqSBMm4mYJzFwiyn2D4d8=
github.com/bytebase/go-lsp v0.0.0-20240130071507-c04b5c75010c h1:W832GNANCst4C6TgYbQnKKxbuMZKQxTD4++b3HOuzBo=