Skip to content

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

feat(query-span): handle table subquery for bigquery query span (#14216)

h3n4l2864c0fdeddb
diff --git a/backend/plugin/parser/bigquery/query_span_extractor.go b/backend/plugin/parser/bigquery/query_span_extractor.go
index 27e1da5906..273ef385d8 100644
--- a/backend/plugin/parser/bigquery/query_span_extractor.go
+++ b/backend/plugin/parser/bigquery/query_span_extractor.go
@@ -603,11 +603,54 @@ func (q *querySpanExtractor) extractTableSourceFromTablePrimary(tablePrimary par
if tablePrimary.Table_path_expression() != nil {
return q.extractTableSourceFromTablePathExpression(tablePrimary.Table_path_expression())
}
+ if subquery := tablePrimary.Table_subquery(); subquery != nil {
+ return q.extractTableSourceFromTableSubquery(subquery)
+ }
// TODO(zp): handle other case
return nil, nil
}
+func (q *querySpanExtractor) extractTableSourceFromTableSubquery(subquery parser.ITable_subqueryContext) (base.TableSource, error) {
+ parenthesizedQuery := subquery.Parenthesized_query()
+
+ // Table subquery shares the ctes of outer query. On the contrary,
+ // the subquery should not effect the outer query.
+ // https://cloud.google.com/bigquery/docs/reference/standard-sql/subqueries#correlated_subquery_concepts
+ originalCtesLength := len(q.ctes)
+ originalTableSourceFrom := len(q.tableSourceFrom)
+ defer func() {
+ q.ctes = q.ctes[:originalCtesLength]
+ q.tableSourceFrom = q.tableSourceFrom[:originalTableSourceFrom]
+ }()
+ subqueryExtractor := &querySpanExtractor{
+ ctx: q.ctx,
+ defaultDatabase: q.defaultDatabase,
+ ctes: q.ctes,
+ outerTableSources: q.outerTableSources,
+ tableSourceFrom: q.tableSourceFrom,
+ }
+ tableSource, err := subqueryExtractor.extractTableSourceFromParenthesizedQuery(parenthesizedQuery)
+ if err != nil {
+ return nil, err
+ }
+
+ var alias string
+ if v := subquery.Opt_pivot_or_unpivot_clause_and_alias(); v != nil {
+ if v.Identifier() != nil {
+ alias = unquoteIdentifierByRule(v.Identifier())
+ }
+ }
+ if alias != "" {
+ return &base.PseudoTable{
+ Name: alias,
+ Columns: tableSource.GetQuerySpanResult(),
+ }, nil
+ }
+
+ return tableSource, nil
+}
+
func (q *querySpanExtractor) extractTableSourceFromTablePathExpression(tablePathExpression parser.ITable_path_expressionContext) (base.TableSource, error) {
tablePathExprBase := tablePathExpression.Table_path_expression_base()
if tablePathExprBase.Unnest_expression() != nil {
diff --git a/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml b/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml
index c609273bc2..8072f6bc54 100644
--- a/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml
+++ b/backend/plugin/parser/bigquery/test-data/query-span/standard.yaml
@@ -35,6 +35,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns:
@@ -121,6 +122,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns:
@@ -196,6 +198,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns:
@@ -255,6 +258,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns:
@@ -324,6 +328,7 @@
]
}
querySpan:
+ type: 0
results:
- name: CITY
sourcecolumns:
@@ -371,6 +376,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ""
sourcecolumns: []
@@ -387,6 +393,66 @@
schema: ""
table: people
column: ""
+- description: Table subquery
+ statement: WITH A AS (SELECT * FROM people) SELECT result.ID, * FROM (SELECT * FROM A) result;
+ defaultDatabase: ds1
+ metadata: |-
+ {
+ "name": "ds1",
+ "schemas": [
+ {
+ "name": "",
+ "tables": [
+ {
+ "name": "people",
+ "columns": [
+ {
+ "name": "ID"
+ },
+ {
+ "name": "NAME"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 0
+ results:
+ - name: ID
+ sourcecolumns:
+ - server: ""
+ database: ds1
+ schema: ""
+ table: people
+ column: ID
+ - name: ID
+ sourcecolumns:
+ - server: ""
+ database: ds1
+ schema: ""
+ table: people
+ column: ID
+ - name: NAME
+ sourcecolumns:
+ - server: ""
+ database: ds1
+ schema: ""
+ table: people
+ column: NAME
+ sourcecolumns:
+ - server: ""
+ database: ds1
+ schema: ""
+ table: A
+ column: ""
+ - server: ""
+ database: ds1
+ schema: ""
+ table: people
+ column: ""
- description: Simple projecter
statement: SELECT ID, NAME FROM people;
defaultDatabase: ds1
@@ -413,6 +479,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns:
@@ -460,6 +527,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns:
@@ -524,6 +592,7 @@
]
}
querySpan:
+ type: 0
results:
- name: ID
sourcecolumns: