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(parser/mssql): scope join-anchor pushes to query specification (BYT-9235) (#20006)
h3n4lc8e22aad8aa0
diff --git a/backend/plugin/parser/tsql/query_span_extractor.go b/backend/plugin/parser/tsql/query_span_extractor.goindex 0e741edea0..0f935d2ede 100644--- a/backend/plugin/parser/tsql/query_span_extractor.go+++ b/backend/plugin/parser/tsql/query_span_extractor.go@@ -379,16 +379,21 @@ func (q *querySpanExtractor) extractTSqlSensitiveFieldsFromQuerySpecification(ctvar compoundFrom []base.TableSourceif from := ctx.From_table_sources(); from != nil {+ // Capture the boundary BEFORE extracting table sources: the extraction+ // pushes intermediate join anchors onto q.tableSourcesFrom (see+ // extractTSqlSensitiveFieldsFromTableSource), and those pushes must be+ // scoped to this query specification so they don't leak into the+ // enclosing scope — especially when alias names collide.+ originalFromFieldList := len(q.tableSourcesFrom)+ defer func() {+ q.tableSourcesFrom = q.tableSourcesFrom[:originalFromFieldList]+ }()fromFieldList, err := q.extractTSqlSensitiveFieldsFromTableSources(from.GetFrom())if err != nil {return nil, errors.Wrapf(err, "failed to extract sensitive fields from `table_sources` in `query_specification`")}- originalFromFieldList := len(q.tableSourcesFrom)q.tableSourcesFrom = append(q.tableSourcesFrom, fromFieldList...)compoundFrom = fromFieldList- defer func() {- q.tableSourcesFrom = q.tableSourcesFrom[:originalFromFieldList]- }()}result := &base.PseudoTable{}diff --git a/backend/plugin/parser/tsql/test-data/query-span/regression.yaml b/backend/plugin/parser/tsql/test-data/query-span/regression.yamlindex 5dca292aca..d079852969 100644--- a/backend/plugin/parser/tsql/test-data/query-span/regression.yaml+++ b/backend/plugin/parser/tsql/test-data/query-span/regression.yaml@@ -235,6 +235,90 @@table: tblcolumn: col_a+- description: "BYT-9235: alias shadowing between inner JOIN tables and outer derived table in view body. The view uses `SELECT DISTINCT a.* FROM (SELECT a.Id, b.F1, b.F2 FROM tableA a INNER JOIN tableB b ... UNION ...) a` — same alias `a` at inner and outer levels. Before the fix, join-anchor pushes leaked out of nested query specifications, so `a.*` resolved to the inner tableA instead of the outer derived table."+ statement: |-+ SELECT TOP 10 * FROM [bhs].[dbo].[vwBHSDetailBPM3] a WHERE a.F1 LIKE 'MT%'+ defaultDatabase: bhs+ ignoreCaseSensitive: true+ metadata:+ - |-+ {+ "name": "bhs",+ "schemas": [+ {+ "name": "dbo",+ "views": [+ {+ "name": "vwBHSDetailBPM3",+ "definition": "CREATE VIEW [dbo].[vwBHSDetailBPM3] AS SELECT DISTINCT a.* FROM (SELECT a.Id, b.F1, b.F2 FROM tableA a INNER JOIN tableB b ON a.Id = b.mainid WHERE a.condition = 'A' UNION SELECT a.Id, b.F1, b.F2 FROM tableA a INNER JOIN tableB b ON a.Id = b.mainid WHERE a.condition = 'B') a"+ }+ ],+ "tables": [+ {+ "name": "tableA",+ "columns": [+ {"name": "Id"},+ {"name": "condition"}+ ]+ },+ {+ "name": "tableB",+ "columns": [+ {"name": "mainid"},+ {"name": "F1"},+ {"name": "F2"}+ ]+ }+ ]+ }+ ]+ }+ querySpan:+ type: 1+ results:+ - name: Id+ sourcecolumns:+ - server: ""+ database: bhs+ schema: dbo+ table: tableA+ column: Id+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: F1+ sourcecolumns:+ - server: ""+ database: bhs+ schema: dbo+ table: tableB+ column: F1+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ - name: F2+ sourcecolumns:+ - server: ""+ database: bhs+ schema: dbo+ table: tableB+ column: F2+ isplainfield: true+ sourcefieldpaths: []+ selectasterisk: false+ sourcecolumns:+ - server: ""+ database: bhs+ schema: dbo+ table: vwBHSDetailBPM3+ column: ""+ predicatecolumns:+ - server: ""+ database: bhs+ schema: dbo+ table: tableB+ column: F1+- description: Test simple correlated subquery - fix for outer table sourcesstatement: |-SELECT HMNum FROM TableA WHERE HMNum = 'test'