Skip to content

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

fix: resolve transform inputs against the extracted source only

rebelice11722b0c8741
diff --git a/backend/plugin/parser/plsql/query_span_extractor_omni.go b/backend/plugin/parser/plsql/query_span_extractor_omni.go
index c8c175cf3f..44b82bd90f 100644
--- a/backend/plugin/parser/plsql/query_span_extractor_omni.go
+++ b/backend/plugin/parser/plsql/query_span_extractor_omni.go
@@ -584,7 +584,15 @@ func (q *omniQuerySpanExtractor) extractOmniPivot(pivot *oracleast.PivotClause)
if err != nil {
return nil, err
}
- q.tableSourcesFrom = append(q.tableSourcesFrom, source)
+ // While deriving the transform's output, the scope is exactly the
+ // extracted source: the clause's inputs resolve against it alone, never
+ // against enclosing join operands or the source's own leaked operands
+ // (a USING-merged column must attribute to both sides, not to the first
+ // operand in the slice).
+ q.tableSourcesFrom = nil
+ if source != nil {
+ q.tableSourcesFrom = []base.TableSource{source}
+ }
excluded := make(map[string]bool)
pivotSources := make(base.SourceColumnSet)
@@ -660,7 +668,15 @@ func (q *omniQuerySpanExtractor) extractOmniUnpivot(unpivot *oracleast.UnpivotCl
if err != nil {
return nil, err
}
- q.tableSourcesFrom = append(q.tableSourcesFrom, source)
+ // While deriving the transform's output, the scope is exactly the
+ // extracted source: the clause's inputs resolve against it alone, never
+ // against enclosing join operands or the source's own leaked operands
+ // (a USING-merged column must attribute to both sides, not to the first
+ // operand in the slice).
+ q.tableSourcesFrom = nil
+ if source != nil {
+ q.tableSourcesFrom = []base.TableSource{source}
+ }
valueColumns := omniExprList(unpivot.ValueColumns)
inputSources := make([]base.SourceColumnSet, len(valueColumns))
@@ -790,7 +806,15 @@ func (q *omniQuerySpanExtractor) extractOmniMatchRecognize(ref *oracleast.MatchR
if err != nil {
return nil, err
}
- q.tableSourcesFrom = append(q.tableSourcesFrom, source)
+ // While deriving the transform's output, the scope is exactly the
+ // extracted source: the clause's inputs resolve against it alone, never
+ // against enclosing join operands or the source's own leaked operands
+ // (a USING-merged column must attribute to both sides, not to the first
+ // operand in the slice).
+ q.tableSourcesFrom = nil
+ if source != nil {
+ q.tableSourcesFrom = []base.TableSource{source}
+ }
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 a33fbf4702..d13c5bb136 100644
--- a/backend/plugin/parser/plsql/query_span_omni_parity_test.go
+++ b/backend/plugin/parser/plsql/query_span_omni_parity_test.go
@@ -490,6 +490,20 @@ func TestOracleOmniTypedLongTailTableSources(t *testing.T) {
})},
},
},
+ {
+ // The pivot's FOR/aggregate inputs must resolve against the
+ // pivot source ONLY: with a join source, SUM(C) has to attribute
+ // to the USING-merged column (both sides), not the first operand.
+ name: "pivot aggregate over join source resolves merged input",
+ statement: "SELECT ONE_S FROM (T JOIN T2 USING (C)) PIVOT (SUM(C) AS S FOR B IN (1 AS ONE))",
+ want: []base.QuerySpanResult{
+ {Name: "ONE_S", SourceColumns: sourceColumnSetFromList([]base.ColumnResource{
+ {Database: "PUBLIC", Table: "T", Column: "B"},
+ {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",