Skip to content

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

fix(pg): skip array subscripts in column reference normalization (#19661)

h3n4l9ee63616e8c7
diff --git a/backend/plugin/parser/pg/pg.go b/backend/plugin/parser/pg/pg.go
index e7e30d5f8c..02e39d90f0 100644
--- a/backend/plugin/parser/pg/pg.go
+++ b/backend/plugin/parser/pg/pg.go
@@ -295,7 +295,9 @@ func normalizePostgreSQLSetTarget(ctx parser.ISet_targetContext) []string {
func normalizePostgreSQLOptIndirection(ctx parser.IOpt_indirectionContext) []string {
var res []string
for _, child := range ctx.AllIndirection_el() {
- res = append(res, normalizePostgreSQLIndirectionEl(child))
+ if part := normalizePostgreSQLIndirectionEl(child); part != "" {
+ res = append(res, part)
+ }
}
return res
}
@@ -307,7 +309,9 @@ func normalizePostgreSQLIndirection(ctx parser.IIndirectionContext) []string {
var res []string
for _, child := range ctx.AllIndirection_el() {
- res = append(res, normalizePostgreSQLIndirectionEl(child))
+ if part := normalizePostgreSQLIndirectionEl(child); part != "" {
+ res = append(res, part)
+ }
}
return res
}
@@ -323,7 +327,9 @@ func normalizePostgreSQLIndirectionEl(ctx parser.IIndirection_elContext) string
}
return normalizePostgreSQLAttrName(ctx.Attr_name())
}
- return ctx.GetText()
+ // Array subscripts like col[expr] or col[start:end] are not part of the
+ // qualified column name — skip them so the base column name is preserved.
+ return ""
}
func normalizePostgreSQLAttrName(ctx parser.IAttr_nameContext) string {
diff --git a/backend/plugin/parser/pg/test-data/query_span.yaml b/backend/plugin/parser/pg/test-data/query_span.yaml
index 7b2e15c0db..eb5abf3bc9 100644
--- a/backend/plugin/parser/pg/test-data/query_span.yaml
+++ b/backend/plugin/parser/pg/test-data/query_span.yaml
@@ -3066,3 +3066,62 @@
table: t
column: ""
predicatecolumns: []
+- description: array subscript in select list
+ statement: |-
+ SELECT
+ c.id,
+ c.scores[array_length(c.scores, 1)] AS last_score
+ FROM t1 c
+ WHERE c.finished_at IS NOT NULL
+ ORDER BY c.finished_at DESC
+ LIMIT 10;
+ defaultDatabase: db
+ metadata: |-
+ {
+ "name": "db",
+ "schemas": [
+ {
+ "name": "public",
+ "tables": [
+ {
+ "name": "t1",
+ "columns": [
+ {"name": "id"},
+ {"name": "scores"},
+ {"name": "finished_at"}
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ querySpan:
+ type: 1
+ results:
+ - name: id
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: t1
+ column: id
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ - name: last_score
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: t1
+ column: scores
+ isplainfield: false
+ sourcefieldpaths: []
+ selectasterisk: false
+ sourcecolumns:
+ - server: ""
+ database: db
+ schema: public
+ table: t1
+ column: ""
+ predicatecolumns: []