Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: surface MongoDB statement parsing failures in execution logs
h3n4lf74be586f5db
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 2e31a6fa21..61e06bb083 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -881,9 +881,11 @@ func queryRetryStopOnError(// Split the statement into individual SQLsstatements, err := parserbase.SplitMultiSQL(instance.Metadata.GetEngine(), statement)if err != nil {- // Engines without splitter support (MongoDB, Redis, Elasticsearch) fall back to- // treating the entire statement as a single unit. These engines also don't have- // GetQuerySpan support, so queryRetry will return nil spans (old behavior).+ // Engines without splitter support (e.g. Redis) and statements that fail to+ // split (e.g. MongoDB parse errors) fall back to treating the entire+ // statement as a single unit. Where GetQuerySpan is supported (e.g. MongoDB),+ // it re-parses the raw text and surfaces the parse error; otherwise+ // queryRetry returns nil spans (old behavior).return queryRetry(ctx, stores, user, instance, database, driver, conn, []parserbase.Statement{{Text: statement}}, statement, queryContext, licenseService, optionalAccessCheck, schemaSyncer, false)}diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex d89546a137..af29d50b6c 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -91,6 +91,11 @@ func (*Driver) GetDB() *sql.DB {func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {stmts, err := mongodbparser.SplitSQL(statement)if err != nil {+ // Log the unparseable input as a failed command so the parse error is+ // visible in the task run log instead of the statement silently+ // disappearing from it (BYT-9950).+ opts.LogCommandExecute(&storepb.Range{Start: 0, End: int32(len(statement))}, statement)+ opts.LogCommandResponse(0, nil, err.Error())return 0, errors.Wrap(err, "failed to split MongoDB statement")}diff --git a/backend/plugin/db/mongodb/mongodb_test.go b/backend/plugin/db/mongodb/mongodb_test.goindex 8973c4040e..31c386e55c 100644--- a/backend/plugin/db/mongodb/mongodb_test.go+++ b/backend/plugin/db/mongodb/mongodb_test.go@@ -1,7 +1,9 @@package mongodbimport (+ "context""testing"+ "time""github.com/stretchr/testify/require"@@ -165,3 +167,46 @@ func TestIsSystemCollection(t *testing.T) {})}}++// TestExecuteLogsParseFailure pins BYT-9950: when the statement fails to+// split/parse, Execute must still emit a COMMAND_EXECUTE / COMMAND_RESPONSE+// pair carrying the parse error so the failure is visible in task run logs,+// and must report failure.+func TestExecuteLogsParseFailure(t *testing.T) {+ const statement = "db.users.find();\nthis is not mongosh"++ run := func(t *testing.T, logStatement bool) []*storepb.TaskRunLog {+ var logs []*storepb.TaskRunLog+ opts := db.ExecuteOptions{+ CreateTaskRunLog: func(_ time.Time, l *storepb.TaskRunLog) error {+ logs = append(logs, l)+ return nil+ },+ LogCommandStatement: logStatement,+ }++ d := &Driver{}+ _, err := d.Execute(context.Background(), statement, opts)+ require.Error(t, err)+ require.Contains(t, err.Error(), "syntax error")++ require.Len(t, logs, 2)+ require.Equal(t, storepb.TaskRunLog_COMMAND_EXECUTE, logs[0].Type)+ require.Equal(t, storepb.TaskRunLog_COMMAND_RESPONSE, logs[1].Type)+ require.Contains(t, logs[1].CommandResponse.Error, "syntax error")+ return logs+ }++ t.Run("range logging", func(t *testing.T) {+ logs := run(t, false)+ require.Empty(t, logs[0].CommandExecute.Statement)+ require.Equal(t, int32(0), logs[0].CommandExecute.Range.GetStart())+ require.Equal(t, int32(len(statement)), logs[0].CommandExecute.Range.GetEnd())+ })++ t.Run("statement logging", func(t *testing.T) {+ logs := run(t, true)+ require.Nil(t, logs[0].CommandExecute.Range)+ require.Equal(t, statement, logs[0].CommandExecute.GetStatement())+ })+}diff --git a/backend/plugin/parser/mongodb/mongodb.go b/backend/plugin/parser/mongodb/mongodb.goindex f51e98f7a4..0f4d562fe7 100644--- a/backend/plugin/parser/mongodb/mongodb.go+++ b/backend/plugin/parser/mongodb/mongodb.go@@ -39,10 +39,9 @@ func GetOmniNode(a base.AST) (ast.Node, bool) {// ParseMongoShell parses a MongoDB shell script and returns parsed statements// with their ASTs. Conforms to the standard ParseStatementsFunc interface.//-// The omni parser recovers from syntax errors by skipping to the next statement-// boundary. When at least one statement parses successfully, this function-// returns the partial results with a nil error. A non-nil error is returned-// only when no statements could be parsed at all.+// Parsing is strict: a parse error in any statement fails the whole input so+// callers never silently lose statements (BYT-9950). Use+// ParseMongoShellBestEffort for partial results.func ParseMongoShell(statement string) ([]base.ParsedStatement, error) {stmts, err := mongo.Parse(statement)if err != nil {diff --git a/backend/plugin/parser/mongodb/split_test.go b/backend/plugin/parser/mongodb/split_test.goindex b4fba777ea..203b2446e6 100644--- a/backend/plugin/parser/mongodb/split_test.go+++ b/backend/plugin/parser/mongodb/split_test.go@@ -13,6 +13,7 @@ func TestSplitSQL(t *testing.T) {wantCount intwantTexts []stringwantNil bool+ wantError bool}{{description: "single statement",@@ -85,11 +86,32 @@ func TestSplitSQL(t *testing.T) {wantCount: 1,wantTexts: []string{`db["my-collection"].find({})`},},+ {+ // BYT-9950: constant arithmetic in createIndex options must split+ // as a single statement instead of failing to parse.+ description: "createIndex with arithmetic TTL expression",+ statement: `db.cs_customer_frequency.createIndex(+ { trans_date: 1 },+ { expireAfterSeconds: 90 * 24 * 60 * 60, name: "cs_customer_frequency_idx2" }+);`,+ wantCount: 1,+ },+ {+ // Splitting is strict: an unparseable statement fails the whole+ // batch instead of being silently dropped (BYT-9950).+ description: "valid statement followed by invalid fails",+ statement: "db.users.find();\nthis is not mongosh",+ wantError: true,+ },}for _, tc := range testCases {t.Run(tc.description, func(t *testing.T) {result, err := SplitSQL(tc.statement)+ if tc.wantError {+ require.Error(t, err)+ return+ }require.NoError(t, err)if tc.wantNil {