Fix bug of adding temporal schema to types in alter priviledges of functions#367
Fix bug of adding temporal schema to types in alter priviledges of functions#367marioloko wants to merge 1 commit intopgplex:mainfrom
Conversation
Greptile SummaryThis PR fixes a bug (#366) where schema-qualified type names (e.g., Changes:
One concern: The helper uses Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller as Diff Engine
participant NI as normalizeIR()
participant NS as normalizeSchema()
participant SF as stripSchemaFromFunctionSignature()
Caller->>NI: normalizeIR(ir)
loop each schema
NI->>NS: normalizeSchema(schema)
NS->>NS: normalize tables, views, functions, types…
loop each Privilege (FUNCTION/PROCEDURE)
NS->>SF: stripSchemaFromFunctionSignature(ObjectName, schema.Name)
SF-->>NS: cleaned ObjectName (schema prefix removed from param types)
end
loop each RevokedDefaultPrivilege (FUNCTION/PROCEDURE)
NS->>SF: stripSchemaFromFunctionSignature(ObjectName, schema.Name)
SF-->>NS: cleaned ObjectName
end
NS-->>NI: normalized schema
end
NI-->>Caller: normalized IR (ready for diff)
Reviews (1): Last reviewed commit: "Fix bug of adding temporal schema to typ..." | Re-trigger Greptile |
| func stripSchemaFromFunctionSignature(signature, schema string) string { | ||
| if schema == "" { | ||
| return signature | ||
| } | ||
| // Replace both quoted and unquoted schema prefixes within the signature | ||
| signature = strings.ReplaceAll(signature, `"`+schema+`".`, "") | ||
| signature = strings.ReplaceAll(signature, schema+".", "") | ||
| return signature |
There was a problem hiding this comment.
Naive string replacement may over-strip cross-schema type qualifiers
strings.ReplaceAll(signature, schema+".", "") searches for the raw substring anywhere in signature. If the current schema name appears as a suffix of another schema's name, it will incorrectly mutate types from that other schema.
Concrete example:
- Schema under normalization:
"app" - Parameter type from a different schema:
"myapp.some_type" "myapp.some_type"contains the substring"app."(starting at position 2)- After replacement:
"my" + "" + "some_type"→"mysome_type"← incorrect
A safer approach is to anchor the match so it only fires when schema. is preceded by a parameter boundary (open-paren, comma, or space):
func stripSchemaFromFunctionSignature(signature, schema string) string {
if schema == "" {
return signature
}
// Only replace schema. when preceded by a word boundary (start, '(', ',', or space)
// to avoid matching schema names that are suffixes of longer schema names.
quotedSchema := regexp.QuoteMeta(`"` + schema + `".`)
unquotedSchema := regexp.QuoteMeta(schema + ".")
boundary := `(^|[(,\s])`
signature = regexp.MustCompile(boundary+quotedSchema).ReplaceAllStringFunc(signature, func(m string) string {
return m[:len(m)-len(`"`+schema+`".`)]
})
signature = regexp.MustCompile(boundary+unquotedSchema).ReplaceAllStringFunc(signature, func(m string) string {
return m[:len(m)-len(schema+".")]
})
return signature
}Or more simply, pre-compile the patterns with a leading (?:^|[(,\s]) assertion. The current approach is fine for common schema names that are not substrings of other schema names used in the same function signature, but it is a correctness risk for cross-schema type references in that edge case.
There was a problem hiding this comment.
Pull request overview
Fixes an IR normalization gap that caused function/procedure privilege signatures to include temporary-schema qualified type names (e.g., pgschema_tmp_... .entity_kind), leading to incorrect/unstable GRANT/REVOKE output in generated plans (Issue #366).
Changes:
- Normalize function/procedure privilege object signatures by stripping same-schema qualifiers from referenced types.
- Add a file-based diff test case covering GRANT/REVOKE on a function that uses a custom enum type.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
ir/normalize.go |
Strips same-schema qualifiers from function/procedure Privileges and RevokedDefaultPrivileges signatures during IR normalization. |
testdata/diff/privilege/grant_function_custom_type/* |
Adds regression fixtures ensuring plan output uses unqualified same-schema custom types in function signatures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Fixes #366