Skip to content

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

fix: keep join operands from leaking past pivot/unpivot/match_recognize scope

rebelicef6c0a84e2ca1
diff --git a/backend/plugin/parser/plsql/query_span_extractor_omni.go b/backend/plugin/parser/plsql/query_span_extractor_omni.go
index aff880152d..c8c175cf3f 100644
--- a/backend/plugin/parser/plsql/query_span_extractor_omni.go
+++ b/backend/plugin/parser/plsql/query_span_extractor_omni.go
@@ -573,15 +573,18 @@ func (q *omniQuerySpanExtractor) extractOmniPivot(pivot *oracleast.PivotClause)
if pivot == nil {
return nil, nil
}
+ // Capture the FROM scope before extracting the source: a join source
+ // appends its operands to tableSourcesFrom, and they must not leak past
+ // this transform — the clause replaces them with its own output.
+ oldFrom := q.tableSourcesFrom
+ defer func() {
+ q.tableSourcesFrom = oldFrom
+ }()
source, err := q.extractOmniTableExpr(pivot.Source)
if err != nil {
return nil, err
}
- oldFrom := q.tableSourcesFrom
q.tableSourcesFrom = append(q.tableSourcesFrom, source)
- defer func() {
- q.tableSourcesFrom = oldFrom
- }()
excluded := make(map[string]bool)
pivotSources := make(base.SourceColumnSet)
@@ -646,15 +649,18 @@ func (q *omniQuerySpanExtractor) extractOmniUnpivot(unpivot *oracleast.UnpivotCl
if unpivot == nil {
return nil, nil
}
+ // Capture the FROM scope before extracting the source: a join source
+ // appends its operands to tableSourcesFrom, and they must not leak past
+ // this transform — the clause replaces them with its own output.
+ oldFrom := q.tableSourcesFrom
+ defer func() {
+ q.tableSourcesFrom = oldFrom
+ }()
source, err := q.extractOmniTableExpr(unpivot.Source)
if err != nil {
return nil, err
}
- oldFrom := q.tableSourcesFrom
q.tableSourcesFrom = append(q.tableSourcesFrom, source)
- defer func() {
- q.tableSourcesFrom = oldFrom
- }()
valueColumns := omniExprList(unpivot.ValueColumns)
inputSources := make([]base.SourceColumnSet, len(valueColumns))
@@ -773,15 +779,18 @@ func (q *omniQuerySpanExtractor) extractOmniMatchRecognize(ref *oracleast.MatchR
if ref == nil {
return nil, nil
}
+ // Capture the FROM scope before extracting the source: a join source
+ // appends its operands to tableSourcesFrom, and they must not leak past
+ // this transform — the clause replaces them with its own output.
+ oldFrom := q.tableSourcesFrom
+ defer func() {
+ q.tableSourcesFrom = oldFrom
+ }()
source, err := q.extractOmniTableExpr(ref.Source)
if err != nil {
return nil, err
}
- oldFrom := q.tableSourcesFrom
q.tableSourcesFrom = append(q.tableSourcesFrom, source)
- defer func() {
- q.tableSourcesFrom = oldFrom
- }()
partitionResults, err := q.extractOmniResultList(ref.PartitionBy)
if err != nil {
diff --git a/backend/plugin/parser/plsql/query_span_omni_parity_test.go b/backend/plugin/parser/plsql/query_span_omni_parity_test.go
index 9c3fe1898a..a33fbf4702 100644
--- a/backend/plugin/parser/plsql/query_span_omni_parity_test.go
+++ b/backend/plugin/parser/plsql/query_span_omni_parity_test.go
@@ -476,6 +476,20 @@ func TestOracleOmniTypedLongTailTableSources(t *testing.T) {
{Name: "ONE_CNT", SourceColumns: sourceColumnSetFromList([]base.ColumnResource{{Database: "PUBLIC", Table: "T", Column: "B"}})},
},
},
+ {
+ // A pivot whose source is a parenthesized join must not leak the
+ // join operands into the FROM scope: the USING-merged column has
+ // to resolve to the pivot output (both sides), not to the first
+ // leaked operand.
+ name: "pivot over join source resolves merged columns",
+ statement: "SELECT C FROM (T JOIN T2 USING (C)) PIVOT (COUNT(*) AS CNT FOR B IN (1 AS ONE))",
+ want: []base.QuerySpanResult{
+ {Name: "C", SourceColumns: sourceColumnSetFromList([]base.ColumnResource{
+ {Database: "PUBLIC", Table: "T", Column: "C"},
+ {Database: "PUBLIC", Table: "T2", Column: "C"},
+ })},
+ },
+ },
{
name: "pivot alias joins another table",
statement: "SELECT P.ONE_CNT, T2.C FROM (SELECT A, B FROM T) PIVOT (COUNT(*) AS CNT FOR B IN (1 AS ONE)) P JOIN T2 ON P.A = T2.C",