From 07d13ed09e28759f44dfb36b3e5ad2a253cb067d Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 00:42:58 -0800 Subject: [PATCH 1/8] fix: strip same-schema OPERATOR() qualifiers after external plan inspection (#697) With an external plan database and target schema public, a view using an operator owned by an extension installed in public (hstore's ->) was deparsed as OPERATOR(public.->) on the plan side and as -> on the target side, so the view was recreated on every plan. The same-schema qualifier stripper now also removes OPERATOR(schema.op), as it already does for schema.func( and ::schema.type. Co-Authored-By: Claude Fable 5.1 --- cmd/plan/external_db_integration_test.go | 47 ++++++++++++++++++++++++ cmd/plan/plan.go | 8 ++++ cmd/plan/plan_test.go | 42 +++++++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/cmd/plan/external_db_integration_test.go b/cmd/plan/external_db_integration_test.go index dd92a343b..68953e20a 100644 --- a/cmd/plan/external_db_integration_test.go +++ b/cmd/plan/external_db_integration_test.go @@ -282,3 +282,50 @@ func TestExternalDatabase_SchemaIsolation(t *testing.T) { schema2 := extDB2.GetSchemaName() assert.NotEqual(t, schema1, schema2, "temporary schemas should have unique names") } + +// TestExternalDatabase_ExtensionOperatorInPublicPlansClean reproduces issue #697: +// a view in the public schema that uses an operator owned by an extension +// installed in public (hstore's ->) must plan as a no-op against the database +// it was dumped from when the desired state is built on an external plan +// database. +func TestExternalDatabase_ExtensionOperatorInPublicPlansClean(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + + targetDB := testutil.SetupPostgres(t) + defer targetDB.Stop() + planDB := testutil.SetupPostgres(t) + defer planDB.Stop() + + targetConn, targetHost, targetPort, targetDatabase, targetUser, targetPassword := testutil.ConnectToPostgres(t, targetDB) + defer targetConn.Close() + schemaSQL := ` +CREATE TABLE t (id integer PRIMARY KEY, h hstore); +CREATE VIEW v AS SELECT id, h -> 'a' AS ha, h ? 'k' AS hk FROM t; +CREATE MATERIALIZED VIEW mv AS SELECT id, h -> 'a' AS ha FROM t; +` + _, err := targetConn.Exec("CREATE EXTENSION hstore;" + schemaSQL) + require.NoError(t, err) + + planConn, planHost, planPort, planDatabase, planUser, planPassword := testutil.ConnectToPostgres(t, planDB) + defer planConn.Close() + _, err = planConn.Exec("CREATE EXTENSION hstore") + require.NoError(t, err) + + file := filepath.Join(t.TempDir(), "schema.sql") + require.NoError(t, os.WriteFile(file, []byte(schemaSQL), 0644)) + + config := &PlanConfig{ + Host: targetHost, Port: targetPort, DB: targetDatabase, User: targetUser, Password: targetPassword, + Schema: "public", File: file, ApplicationName: "pgschema-test", + PlanDBHost: planHost, PlanDBPort: planPort, PlanDBDatabase: planDatabase, PlanDBUser: planUser, PlanDBPassword: planPassword, + } + provider, err := CreateDesiredStateProvider(config) + require.NoError(t, err) + defer provider.Stop() + + p, err := GeneratePlan(config, provider) + require.NoError(t, err) + require.False(t, p.HasAnyChanges(), "view using an extension operator must not be re-planned, got:\n%s", p.HumanColored(false)) +} diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index dee14b67e..93b5a15d4 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -848,8 +848,16 @@ func newSameSchemaQualifierStripper(schema string) func(string) string { prefix := schema + "." funcPattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_$]*)\(`) typePattern := regexp.MustCompile(`::` + regexp.QuoteMeta(prefix)) + // OPERATOR(schema.op): ruleutils qualifies an operator whenever its schema + // is not on the deparsing session's search_path. The plan database is + // inspected with only the temporary schema on the path, so an operator in + // the target schema (e.g. hstore's -> installed in public) comes out as + // OPERATOR(public.->) while the target side prints the bare operator. + // Operator names use only the characters PostgreSQL allows (issue #697). + operatorPattern := regexp.MustCompile(`OPERATOR\(` + regexp.QuoteMeta(prefix) + `([-+*/<>=~!@#%^&|` + "`" + `?]+)\)`) replaceQualifiers := func(s string) string { s = funcPattern.ReplaceAllString(s, `${1}(`) + s = operatorPattern.ReplaceAllString(s, `${1}`) return typePattern.ReplaceAllString(s, "::") } return func(s string) string { diff --git a/cmd/plan/plan_test.go b/cmd/plan/plan_test.go index 6569f3bea..bd9fcf02e 100644 --- a/cmd/plan/plan_test.go +++ b/cmd/plan/plan_test.go @@ -324,3 +324,45 @@ func TestNormalizeSchemaNames_IndexAttachment(t *testing.T) { t.Fatalf("Incorrect partition-table metadata normalization: %+v", children) } } + +func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { + // With an external plan database and target schema public, the plan side + // deparses extension operators living in public as OPERATOR(public.->), + // while the target side (inspected with public on its search_path) prints + // the bare operator. The same-schema qualifier must be stripped so the two + // definitions agree (issue #697). + const tempSchema = "pgschema_tmp_20260326_123456_abcd1234" + irData := &ir.IR{ + Schemas: map[string]*ir.Schema{ + tempSchema: { + Name: tempSchema, + Views: map[string]*ir.View{ + "v": { + Schema: tempSchema, + Name: "v", + Definition: " SELECT id,\n" + + " h OPERATOR(public.->) 'a'::text AS ha,\n" + + " h OPERATOR(public.?) 'k'::text AS hk,\n" + + " t OPERATOR(public.@>) 'x'::public.ltree AS anc,\n" + + " 'OPERATOR(public.->)'::text AS literal,\n" + + " j -> 'a'::text AS ja\n" + + " FROM t", + }, + }, + }, + }, + } + + normalizeSchemaNames(irData, tempSchema, "public") + + expected := " SELECT id,\n" + + " h -> 'a'::text AS ha,\n" + + " h ? 'k'::text AS hk,\n" + + " t @> 'x'::ltree AS anc,\n" + + " 'OPERATOR(public.->)'::text AS literal,\n" + + " j -> 'a'::text AS ja\n" + + " FROM t" + if got := irData.Schemas["public"].Views["v"].Definition; got != expected { + t.Fatalf("expected operator qualifiers stripped:\n%s\ngot:\n%s", expected, got) + } +} From 2080790cc67aefb7c388a376d882916424efd98c Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 00:58:49 -0800 Subject: [PATCH 2/8] fix: normalize OPERATOR() syntax consistently and skip quoted identifiers (#697) Review follow-up: the plan-side stripper also mangled double-quoted identifiers, and stripping only the desired side left column defaults, generated expressions and index expressions (deparsed under search_path = pg_catalog) disagreeing with the current side. The IR normalizer now reduces OPERATOR(.op) and the OPERATOR(op) artifact of prefix stripping to the bare operator in every expression context, on both sides, so a dump with an hstore default also plans with embedded Postgres. The plan-side stripper skips double-quoted identifiers the same way it skips string literals. Co-Authored-By: Claude Fable 5.1 --- cmd/plan/external_db_integration_test.go | 15 ++++++- cmd/plan/plan.go | 9 +++-- cmd/plan/plan_test.go | 4 ++ ir/normalize.go | 46 ++++++++++++++++++++- ir/normalize_test.go | 51 ++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 6 deletions(-) diff --git a/cmd/plan/external_db_integration_test.go b/cmd/plan/external_db_integration_test.go index 68953e20a..1984235de 100644 --- a/cmd/plan/external_db_integration_test.go +++ b/cmd/plan/external_db_integration_test.go @@ -300,9 +300,20 @@ func TestExternalDatabase_ExtensionOperatorInPublicPlansClean(t *testing.T) { targetConn, targetHost, targetPort, targetDatabase, targetUser, targetPassword := testutil.ConnectToPostgres(t, targetDB) defer targetConn.Close() + // Every expression context that can carry an operator: column default and + // generated expression (deparsed under search_path = pg_catalog), CHECK + // constraint, index expression and predicate, view and materialized view. + // The quoted alias must survive untouched. schemaSQL := ` -CREATE TABLE t (id integer PRIMARY KEY, h hstore); -CREATE VIEW v AS SELECT id, h -> 'a' AS ha, h ? 'k' AS hk FROM t; +CREATE TABLE t ( + id integer PRIMARY KEY, + h hstore, + d text DEFAULT ('a=>1'::hstore -> 'a'), + g text GENERATED ALWAYS AS (h -> 'a') STORED, + CONSTRAINT c CHECK ((h ? 'k') OR h IS NULL) +); +CREATE INDEX i ON t ((h -> 'b')) WHERE (h ? 'b'); +CREATE VIEW v AS SELECT id, h -> 'a' AS ha, h ? 'k' AS hk, 1 AS "OPERATOR(public.->)" FROM t; CREATE MATERIALIZED VIEW mv AS SELECT id, h -> 'a' AS ha FROM t; ` _, err := targetConn.Exec("CREATE EXTENSION hstore;" + schemaSQL) diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index 93b5a15d4..1fe02b408 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -865,21 +865,24 @@ func newSameSchemaQualifierStripper(schema string) func(string) string { return s } + // Replace only outside single-quoted literals and double-quoted + // identifiers; a doubled quote inside either is an escape. var result strings.Builder result.Grow(len(s)) segmentStart := 0 for i := 0; i < len(s); i++ { - if s[i] != '\'' { + quote := s[i] + if quote != '\'' && quote != '"' { continue } result.WriteString(replaceQualifiers(s[segmentStart:i])) literalStart := i for i++; i < len(s); i++ { - if s[i] != '\'' { + if s[i] != quote { continue } - if i+1 < len(s) && s[i+1] == '\'' { + if i+1 < len(s) && s[i+1] == quote { i++ continue } diff --git a/cmd/plan/plan_test.go b/cmd/plan/plan_test.go index bd9fcf02e..4c16518ff 100644 --- a/cmd/plan/plan_test.go +++ b/cmd/plan/plan_test.go @@ -345,6 +345,8 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { " h OPERATOR(public.?) 'k'::text AS hk,\n" + " t OPERATOR(public.@>) 'x'::public.ltree AS anc,\n" + " 'OPERATOR(public.->)'::text AS literal,\n" + + " 1 AS \"OPERATOR(public.->)\",\n" + + " \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" + " j -> 'a'::text AS ja\n" + " FROM t", }, @@ -360,6 +362,8 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { " h ? 'k'::text AS hk,\n" + " t @> 'x'::ltree AS anc,\n" + " 'OPERATOR(public.->)'::text AS literal,\n" + + " 1 AS \"OPERATOR(public.->)\",\n" + + " \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" + " j -> 'a'::text AS ja\n" + " FROM t" if got := irData.Schemas["public"].Views["v"].Definition; got != expected { diff --git a/ir/normalize.go b/ir/normalize.go index c66cc48fd..7c1a64b39 100644 --- a/ir/normalize.go +++ b/ir/normalize.go @@ -244,6 +244,11 @@ func normalizeDefaultValue(value string, tableSchema string) string { value = pattern.ReplaceAllString(value, `${1}(`) } + // Defaults are deparsed with search_path = pg_catalog, so a same-schema + // operator appears as OPERATOR(schema.op); reduce it to the bare operator + // like every other expression context (see normalizeOperatorSyntax). + value = normalizeOperatorSyntax(value, tableSchema) + // Handle type casting - remove explicit type casts that are semantically equivalent if strings.Contains(value, "::") { // Strip temporary embedded postgres schema prefixes (pgschema_tmp_*) @@ -478,7 +483,46 @@ func StripSchemaPrefixFromBody(body, schema string) string { if quoted := QuoteIdentifier(schema); quoted != schema { body = stripSchemaPrefixOccurrences(body, quoted+".") } - return stripSchemaPrefixOccurrences(body, schema+".") + return normalizeOperatorSyntax(stripSchemaPrefixOccurrences(body, schema+"."), schema) +} + +// operatorSyntaxRe matches PostgreSQL's explicit operator syntax, OPERATOR(op) +// or OPERATOR(schema.op), capturing the optional schema and the operator name. +// Operator names consist only of the characters the lexer allows in them. +var operatorSyntaxRe = regexp.MustCompile(`OPERATOR\((?:([A-Za-z_][A-Za-z0-9_$]*)\.)?([-+*/<>=~!@#%^&|` + "`" + `?]+)\)`) + +// normalizeOperatorSyntax rewrites OPERATOR(schema.op) to the bare op when +// schema is the object's own schema, and OPERATOR(op) to op unconditionally. +// +// ruleutils emits OPERATOR(schema.op) whenever the operator's schema is not on +// the deparsing session's search_path. Column defaults, generated expressions +// and index expressions are deparsed with search_path = pg_catalog, so an +// extension operator installed in the managed schema (hstore's -> in public) +// comes out qualified there, while a view or CHECK constraint on the same +// database is deparsed with the schema on the path and prints the bare +// operator. Stripping the schema prefix alone leaves OPERATOR(->), which +// PostgreSQL never emits itself and which the desired state never contains. +// Reducing both forms to the bare operator makes every context compare the +// same way on the current and desired side (issue #697). Operators in other +// schemas keep their qualifier, and quoted text is left untouched. +func normalizeOperatorSyntax(expr, schema string) string { + if !strings.Contains(expr, "OPERATOR(") { + return expr + } + segments := splitQuotedSegments(expr) + for i, segment := range segments { + if segment == "" || segment[0] == '\'' || segment[0] == '"' { + continue + } + segments[i] = operatorSyntaxRe.ReplaceAllStringFunc(segment, func(match string) string { + parts := operatorSyntaxRe.FindStringSubmatch(match) + if parts[1] == "" || parts[1] == schema { + return parts[2] + } + return match + }) + } + return strings.Join(segments, "") } // stripSchemaPrefixOccurrences removes every occurrence of prefix ("schema." diff --git a/ir/normalize_test.go b/ir/normalize_test.go index cfdd3f261..648080b16 100644 --- a/ir/normalize_test.go +++ b/ir/normalize_test.go @@ -782,3 +782,54 @@ func TestStripInheritedIdentityNested(t *testing.T) { } } } + +func TestNormalizeOperatorSyntax(t *testing.T) { + tests := []struct { + name string + input string + schema string + want string + }{ + { + name: "same-schema operator becomes bare", + input: "(h OPERATOR(public.->) 'a'::text)", + schema: "public", + want: "(h -> 'a'::text)", + }, + { + name: "unqualified OPERATOR() collapses", + input: "(h OPERATOR(->) 'b'::text) AND (h OPERATOR(?) 'k'::text)", + schema: "public", + want: "(h -> 'b'::text) AND (h ? 'k'::text)", + }, + { + name: "operator in another schema keeps its qualifier", + input: "(h OPERATOR(ext.->) 'a'::text)", + schema: "public", + want: "(h OPERATOR(ext.->) 'a'::text)", + }, + { + name: "quoted text is untouched", + input: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(->)'", + schema: "public", + want: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(->)'", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := normalizeOperatorSyntax(tt.input, tt.schema); got != tt.want { + t.Errorf("normalizeOperatorSyntax(%q, %q) = %q, want %q", tt.input, tt.schema, got, tt.want) + } + }) + } + + // The two entry points that see deparsed expressions apply it: generated + // expressions and index expressions go through StripSchemaPrefixFromBody, + // column defaults through normalizeDefaultValue (issue #697). + if got := StripSchemaPrefixFromBody("(h OPERATOR(public.->) 'a'::text)", "public"); got != "(h -> 'a'::text)" { + t.Errorf("StripSchemaPrefixFromBody left operator syntax: %q", got) + } + if got := normalizeDefaultValue(`('"a"=>"1"'::hstore OPERATOR(public.->) 'a')`, "public"); got != `('"a"=>"1"'::hstore -> 'a')` { + t.Errorf("normalizeDefaultValue left operator syntax: %q", got) + } +} From f3eb7aba273cc83972682973e11c6ce131019654 Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 01:25:46 -0800 Subject: [PATCH 3/8] fix: keep catalog-shadowed operators qualified and leave raw function bodies alone (#697) Review follow-up. The operator rewrite moves out of StripSchemaPrefixFromBody, which also compares raw function bodies where OPERATOR() carries its own precedence, into normalizeDeparsedExpression for deparsed contexts only, and prefix stripping no longer turns OPERATOR(schema.op) into OPERATOR(op). An operator in the managed schema that duplicates a pg_catalog operator's name and operand types is only reachable schema-qualified; the inspector now records such names (Metadata.ShadowedOperators) and both normalization paths keep their qualifier. nextval-backed defaults are normalized too. Co-Authored-By: Claude Fable 5.1 --- cmd/plan/plan.go | 20 +++++-- cmd/plan/plan_test.go | 5 ++ ir/inspector.go | 37 +++++++++++++ ir/ir.go | 6 +++ ir/normalize.go | 122 +++++++++++++++++++++++++----------------- ir/normalize_test.go | 67 +++++++++++++---------- 6 files changed, 175 insertions(+), 82 deletions(-) diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index 1fe02b408..d15fdb8cd 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -512,7 +512,11 @@ func normalizeSchemaNames(irData *ir.IR, fromSchema, toSchema string) { // contain "toSchema.func_name(" or "::toSchema.type" which are redundant same-schema // qualifiers. The initial normalizeIR (run by the inspector) couldn't strip these // because it ran with the temp schema name, not the target schema. See issue #283. - stripQualifiers := newSameSchemaQualifierStripper(toSchema) + shadowedOperators := make(map[string]bool, len(irData.Metadata.ShadowedOperators)) + for _, name := range irData.Metadata.ShadowedOperators { + shadowedOperators[name] = true + } + stripQualifiers := newSameSchemaQualifierStripper(toSchema, shadowedOperators) // Normalize schema names in Schemas map if schema, exists := irData.Schemas[fromSchema]; exists { @@ -841,7 +845,11 @@ func newSchemaStringReplacer(fromSchema, toSchema string) func(string) string { // with the target schema, expressions may contain "schema.func_name(" or "::schema.type" // where the qualifier matches the object's own schema. These are redundant and must be // stripped to match how the target database's inspector would produce them. See issue #283. -func newSameSchemaQualifierStripper(schema string) func(string) string { +// +// shadowedOperators names the operators that must keep their qualifier because +// an unqualified reference would resolve to a pg_catalog operator instead (see +// ir.Metadata.ShadowedOperators). +func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string]bool) func(string) string { if schema == "" { return func(s string) string { return s } } @@ -857,7 +865,13 @@ func newSameSchemaQualifierStripper(schema string) func(string) string { operatorPattern := regexp.MustCompile(`OPERATOR\(` + regexp.QuoteMeta(prefix) + `([-+*/<>=~!@#%^&|` + "`" + `?]+)\)`) replaceQualifiers := func(s string) string { s = funcPattern.ReplaceAllString(s, `${1}(`) - s = operatorPattern.ReplaceAllString(s, `${1}`) + s = operatorPattern.ReplaceAllStringFunc(s, func(match string) string { + name := operatorPattern.FindStringSubmatch(match)[1] + if shadowedOperators[name] { + return match + } + return name + }) return typePattern.ReplaceAllString(s, "::") } return func(s string) string { diff --git a/cmd/plan/plan_test.go b/cmd/plan/plan_test.go index 4c16518ff..1a82df6c6 100644 --- a/cmd/plan/plan_test.go +++ b/cmd/plan/plan_test.go @@ -333,6 +333,9 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { // definitions agree (issue #697). const tempSchema = "pgschema_tmp_20260326_123456_abcd1234" irData := &ir.IR{ + // A custom public.+(integer, integer) duplicates pg_catalog's; ruleutils + // always qualifies it, and so must the stripped definition. + Metadata: ir.Metadata{ShadowedOperators: []string{"+"}}, Schemas: map[string]*ir.Schema{ tempSchema: { Name: tempSchema, @@ -344,6 +347,7 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { " h OPERATOR(public.->) 'a'::text AS ha,\n" + " h OPERATOR(public.?) 'k'::text AS hk,\n" + " t OPERATOR(public.@>) 'x'::public.ltree AS anc,\n" + + " id OPERATOR(public.+) 1 AS shadowed,\n" + " 'OPERATOR(public.->)'::text AS literal,\n" + " 1 AS \"OPERATOR(public.->)\",\n" + " \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" + @@ -361,6 +365,7 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { " h -> 'a'::text AS ha,\n" + " h ? 'k'::text AS hk,\n" + " t @> 'x'::ltree AS anc,\n" + + " id OPERATOR(public.+) 1 AS shadowed,\n" + " 'OPERATOR(public.->)'::text AS literal,\n" + " 1 AS \"OPERATOR(public.->)\",\n" + " \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" + diff --git a/ir/inspector.go b/ir/inspector.go index 9a300c35d..aefdb8be2 100644 --- a/ir/inspector.go +++ b/ir/inspector.go @@ -125,6 +125,10 @@ func (i *Inspector) BuildIR(ctx context.Context, targetSchema string) (*IR, erro return nil, err } + if err := traced(ctx, "shadowed operators", func(ctx context.Context) error { return i.buildShadowedOperators(ctx, schema, targetSchema) }); err != nil { + return nil, fmt.Errorf("failed to query shadowed operators: %w", err) + } + if err := traced(ctx, "schemas", func(ctx context.Context) error { return i.buildSchemas(ctx, schema, targetSchema) }); err != nil { return nil, fmt.Errorf("failed to build schemas: %w", err) } @@ -230,6 +234,39 @@ func (i *Inspector) BuildIR(ctx context.Context, targetSchema string) (*IR, erro return schema, nil } +// buildShadowedOperators records the operator names in the inspected and +// managed schemas that a pg_catalog operator with the same name and operand +// types shadows. pg_catalog is implicitly first on every search_path, so such +// an operator is reachable only schema-qualified: ruleutils always deparses it +// as OPERATOR(schema.op), and normalizeOperatorSyntax must leave that form +// alone instead of reducing it to the bare operator (issue #697). +func (i *Inspector) buildShadowedOperators(ctx context.Context, schema *IR, targetSchema string) error { + rows, err := i.db.QueryContext(ctx, ` +SELECT DISTINCT o.oprname +FROM pg_catalog.pg_operator o +JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace +WHERE n.nspname IN ($1, $2) + AND EXISTS ( + SELECT 1 FROM pg_catalog.pg_operator c + WHERE c.oprnamespace = 'pg_catalog'::regnamespace + AND c.oprname = o.oprname + AND c.oprleft = o.oprleft + AND c.oprright = o.oprright) +ORDER BY 1`, targetSchema, i.managedSchema) + if err != nil { + return err + } + defer rows.Close() + for rows.Next() { + var name string + if err := rows.Scan(&name); err != nil { + return err + } + schema.Metadata.ShadowedOperators = append(schema.Metadata.ShadowedOperators, name) + } + return rows.Err() +} + // buildStep is one named introspection step of BuildIR. The name labels the // step's node in the query call tree. type buildStep struct { diff --git a/ir/ir.go b/ir/ir.go index 5243860ed..87607c5fc 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -16,6 +16,12 @@ type IR struct { // Metadata contains information about the schema dump type Metadata struct { DatabaseVersion string `json:"database_version"` + // ShadowedOperators lists the operator names in the inspected and managed + // schemas that duplicate a pg_catalog operator's name and operand types. + // pg_catalog is searched first, so an unqualified reference to such a name + // resolves to the catalog operator; ruleutils always deparses these as + // OPERATOR(schema.op) and normalization keeps that form (issue #697). + ShadowedOperators []string `json:"shadowed_operators,omitempty"` } // Schema represents a single database schema (namespace) diff --git a/ir/normalize.go b/ir/normalize.go index 7c1a64b39..e9b3e4c70 100644 --- a/ir/normalize.go +++ b/ir/normalize.go @@ -22,20 +22,26 @@ func normalizeIR(ir *IR) { return } + shadowed := make(map[string]bool, len(ir.Metadata.ShadowedOperators)) + for _, name := range ir.Metadata.ShadowedOperators { + shadowed[name] = true + } for _, schema := range ir.Schemas { - normalizeSchema(schema) + normalizeSchema(schema, shadowed) } } -// normalizeSchema normalizes all objects within a schema -func normalizeSchema(schema *Schema) { +// normalizeSchema normalizes all objects within a schema. shadowed holds the +// operator names that must keep their schema qualifier (see +// Metadata.ShadowedOperators). +func normalizeSchema(schema *Schema, shadowed map[string]bool) { if schema == nil { return } // Normalize tables for _, table := range schema.Tables { - normalizeTable(table) + normalizeTable(table, shadowed) } // Mark SERIAL columns once column defaults and sequences are normalized @@ -46,17 +52,17 @@ func normalizeSchema(schema *Schema) { // Normalize views for _, view := range schema.Views { - normalizeView(view) + normalizeView(view, shadowed) } // Normalize functions for _, function := range schema.Functions { - normalizeFunction(function) + normalizeFunction(function, shadowed) } // Normalize procedures for _, procedure := range schema.Procedures { - normalizeProcedure(procedure) + normalizeProcedure(procedure, shadowed) } // Normalize types (including domains) @@ -155,19 +161,19 @@ func SerialSequenceName(tableName, columnName string) string { } // normalizeTable normalizes table-related objects -func normalizeTable(table *Table) { +func normalizeTable(table *Table, shadowed map[string]bool) { if table == nil { return } // Normalize columns (pass table schema for context) for _, column := range table.Columns { - normalizeColumn(column, table.Schema) + normalizeColumn(column, table.Schema, shadowed) } // Compare parent expressions in the child's schema context, just like its // own defaults. The parent can live in a different (unmanaged) schema. for _, column := range table.PartitionParentColumns { - normalizeColumn(column, table.Schema) + normalizeColumn(column, table.Schema, shadowed) } // Normalize policies @@ -182,7 +188,7 @@ func normalizeTable(table *Table) { // Normalize indexes for _, index := range table.Indexes { - normalizeIndex(index) + normalizeIndex(index, shadowed) } // Normalize constraints @@ -193,7 +199,7 @@ func normalizeTable(table *Table) { // normalizeColumn normalizes column default values // tableSchema is used to strip same-schema qualifiers from function calls -func normalizeColumn(column *Column, tableSchema string) { +func normalizeColumn(column *Column, tableSchema string, shadowed map[string]bool) { if column == nil { return } @@ -202,7 +208,7 @@ func normalizeColumn(column *Column, tableSchema string) { // expression depending on the inspecting session's search_path; strip the // qualifier so current and desired state compare textually (issue #591). if column.GeneratedExpr != nil && tableSchema != "" { - stripped := StripSchemaPrefixFromBody(*column.GeneratedExpr, tableSchema) + stripped := normalizeDeparsedExpression(*column.GeneratedExpr, tableSchema, shadowed) column.GeneratedExpr = &stripped } @@ -210,16 +216,22 @@ func normalizeColumn(column *Column, tableSchema string) { return } - normalized := normalizeDefaultValue(*column.DefaultValue, tableSchema) + normalized := normalizeDefaultValue(*column.DefaultValue, tableSchema, shadowed) column.DefaultValue = &normalized } // normalizeDefaultValue normalizes default values for semantic comparison // tableSchema is used to strip same-schema qualifiers from function calls -func normalizeDefaultValue(value string, tableSchema string) string { +func normalizeDefaultValue(value string, tableSchema string, shadowed map[string]bool) string { // Remove unnecessary whitespace value = strings.TrimSpace(value) + // Defaults are deparsed with search_path = pg_catalog, so a same-schema + // operator appears as OPERATOR(schema.op); reduce it to the bare operator + // like every other expression context. Done before the nextval early + // return below so sequence-backed defaults are covered too. + value = normalizeOperatorSyntax(value, tableSchema, shadowed) + // Handle nextval sequence references - remove schema qualification if strings.Contains(value, "nextval(") { // Pattern: nextval('schema_name.seq_name'::regclass) -> nextval('seq_name'::regclass) @@ -244,11 +256,6 @@ func normalizeDefaultValue(value string, tableSchema string) string { value = pattern.ReplaceAllString(value, `${1}(`) } - // Defaults are deparsed with search_path = pg_catalog, so a same-schema - // operator appears as OPERATOR(schema.op); reduce it to the bare operator - // like every other expression context (see normalizeOperatorSyntax). - value = normalizeOperatorSyntax(value, tableSchema) - // Handle type casting - remove explicit type casts that are semantically equivalent if strings.Contains(value, "::") { // Strip temporary embedded postgres schema prefixes (pgschema_tmp_*) @@ -379,14 +386,14 @@ func normalizePolicyExpression(expr string) string { // This happens when extension functions (e.g., ltree's nlevel()) or search_path differences // cause one side to produce "public.func()" and the other "func()". // Stripping same-schema qualifiers ensures the definitions compare as equal. (Issue #314) -func normalizeView(view *View) { +func normalizeView(view *View, shadowed map[string]bool) { if view == nil { return } // Strip same-schema qualifiers from view definition for consistent comparison. // This uses the same logic as function/procedure body normalization. - view.Definition = StripSchemaPrefixFromBody(view.Definition, view.Schema) + view.Definition = normalizeDeparsedExpression(view.Definition, view.Schema, shadowed) // Likewise for the column types: format_type() qualifies a same-schema // type whenever the schema is not on the search_path, and the types are @@ -402,12 +409,12 @@ func normalizeView(view *View) { // Normalize indexes on materialized views for _, index := range view.Indexes { - normalizeIndex(index) + normalizeIndex(index, shadowed) } } // normalizeFunction normalizes function signature and definition -func normalizeFunction(function *Function) { +func normalizeFunction(function *Function, shadowed map[string]bool) { if function == nil { return } @@ -438,7 +445,7 @@ func normalizeFunction(function *Function) { } // Normalize default values (pass function schema for context) if param.DefaultValue != nil { - normalized := normalizeDefaultValue(*param.DefaultValue, function.Schema) + normalized := normalizeDefaultValue(*param.DefaultValue, function.Schema, shadowed) param.DefaultValue = &normalized } } @@ -483,16 +490,24 @@ func StripSchemaPrefixFromBody(body, schema string) string { if quoted := QuoteIdentifier(schema); quoted != schema { body = stripSchemaPrefixOccurrences(body, quoted+".") } - return normalizeOperatorSyntax(stripSchemaPrefixOccurrences(body, schema+"."), schema) + return stripSchemaPrefixOccurrences(body, schema+".") } -// operatorSyntaxRe matches PostgreSQL's explicit operator syntax, OPERATOR(op) -// or OPERATOR(schema.op), capturing the optional schema and the operator name. -// Operator names consist only of the characters the lexer allows in them. -var operatorSyntaxRe = regexp.MustCompile(`OPERATOR\((?:([A-Za-z_][A-Za-z0-9_$]*)\.)?([-+*/<>=~!@#%^&|` + "`" + `?]+)\)`) +// normalizeDeparsedExpression normalizes an expression that came out of a +// PostgreSQL deparser (pg_get_viewdef, pg_get_expr, pg_get_indexdef): it strips +// same-schema qualifiers from identifiers and reduces same-schema operators +// written as OPERATOR(schema.op) to the bare operator. It is not used on raw +// function bodies, where OPERATOR() carries its own precedence and must be +// compared as written. +func normalizeDeparsedExpression(expr, schema string, shadowed map[string]bool) string { + return normalizeOperatorSyntax(StripSchemaPrefixFromBody(expr, schema), schema, shadowed) +} + +// operatorNameChars are the characters PostgreSQL allows in an operator name. +const operatorNameChars = "-+*/<>=~!@#%^&|`?" // normalizeOperatorSyntax rewrites OPERATOR(schema.op) to the bare op when -// schema is the object's own schema, and OPERATOR(op) to op unconditionally. +// schema is the object's own schema, unless op is a shadowed name. // // ruleutils emits OPERATOR(schema.op) whenever the operator's schema is not on // the deparsing session's search_path. Column defaults, generated expressions @@ -500,26 +515,29 @@ var operatorSyntaxRe = regexp.MustCompile(`OPERATOR\((?:([A-Za-z_][A-Za-z0-9_$]* // extension operator installed in the managed schema (hstore's -> in public) // comes out qualified there, while a view or CHECK constraint on the same // database is deparsed with the schema on the path and prints the bare -// operator. Stripping the schema prefix alone leaves OPERATOR(->), which -// PostgreSQL never emits itself and which the desired state never contains. -// Reducing both forms to the bare operator makes every context compare the -// same way on the current and desired side (issue #697). Operators in other -// schemas keep their qualifier, and quoted text is left untouched. -func normalizeOperatorSyntax(expr, schema string) string { - if !strings.Contains(expr, "OPERATOR(") { +// operator. Reducing the qualified form to the bare operator makes every +// context compare the same way on the current and desired side (issue #697). +// +// A shadowed operator (same name and operand types as a pg_catalog operator, +// see Metadata.ShadowedOperators) is the one case where the bare name would +// resolve differently, so its qualifier is kept. Operators in other schemas +// keep their qualifier, and quoted text is left untouched. +func normalizeOperatorSyntax(expr, schema string, shadowed map[string]bool) string { + if schema == "" || !strings.Contains(expr, "OPERATOR("+schema+".") { return expr } + pattern := regexp.MustCompile(`OPERATOR\(` + regexp.QuoteMeta(schema) + `\.([` + regexp.QuoteMeta(operatorNameChars) + `]+)\)`) segments := splitQuotedSegments(expr) for i, segment := range segments { if segment == "" || segment[0] == '\'' || segment[0] == '"' { continue } - segments[i] = operatorSyntaxRe.ReplaceAllStringFunc(segment, func(match string) string { - parts := operatorSyntaxRe.FindStringSubmatch(match) - if parts[1] == "" || parts[1] == schema { - return parts[2] + segments[i] = pattern.ReplaceAllStringFunc(segment, func(match string) string { + name := pattern.FindStringSubmatch(match)[1] + if shadowed[name] { + return match } - return match + return name }) } return strings.Join(segments, "") @@ -583,8 +601,12 @@ func stripSchemaPrefixOccurrences(body, prefix string) string { continue } - // Only attempt replacement outside string literals - if !inString && i+prefixLen <= len(body) && body[i:i+prefixLen] == prefix { + // Only attempt replacement outside string literals. An operator's + // qualifier, OPERATOR(schema.op), is left to normalizeOperatorSyntax: + // dropping just the schema there would leave OPERATOR(op), which + // PostgreSQL never emits and which resolves differently for a + // shadowed operator. + if !inString && i+prefixLen <= len(body) && body[i:i+prefixLen] == prefix && !strings.HasSuffix(body[:i], "OPERATOR(") { // Ensure this is a schema qualifier, not part of a longer identifier // (e.g., "not_public.users" should not match) if i == 0 || !isIdentChar(body[i-1]) { @@ -675,7 +697,7 @@ func isIdentChar(b byte) bool { } // normalizeProcedure normalizes procedure representation -func normalizeProcedure(procedure *Procedure) { +func normalizeProcedure(procedure *Procedure, shadowed map[string]bool) { if procedure == nil { return } @@ -693,7 +715,7 @@ func normalizeProcedure(procedure *Procedure) { } // Normalize default values (pass procedure schema for context) if param.DefaultValue != nil { - normalized := normalizeDefaultValue(*param.DefaultValue, procedure.Schema) + normalized := normalizeDefaultValue(*param.DefaultValue, procedure.Schema, shadowed) param.DefaultValue = &normalized } } @@ -1048,7 +1070,7 @@ func normalizeTriggerCondition(condition string) string { } // normalizeIndex normalizes index WHERE clauses and other properties -func normalizeIndex(index *Index) { +func normalizeIndex(index *Index, shadowed map[string]bool) { if index == nil { return } @@ -1057,14 +1079,14 @@ func normalizeIndex(index *Index) { if index.IsPartial && index.Where != "" { index.Where = normalizeIndexWhereClause(index.Where) if index.Schema != "" { - index.Where = StripSchemaPrefixFromBody(index.Where, index.Schema) + index.Where = normalizeDeparsedExpression(index.Where, index.Schema, shadowed) } } // Strip same-schema qualifiers from expression index column names if index.IsExpression && index.Schema != "" { for _, col := range index.Columns { - col.Name = StripSchemaPrefixFromBody(col.Name, index.Schema) + col.Name = normalizeDeparsedExpression(col.Name, index.Schema, shadowed) } } } diff --git a/ir/normalize_test.go b/ir/normalize_test.go index 648080b16..ac0bb7977 100644 --- a/ir/normalize_test.go +++ b/ir/normalize_test.go @@ -166,7 +166,7 @@ func TestNormalizeViewStripsSchemaPrefixFromDefinition(t *testing.T) { Name: "test_view", Definition: tt.definition, } - normalizeView(view) + normalizeView(view, nil) if view.Definition != tt.expected { t.Errorf("normalizeView() definition = %q, want %q", view.Definition, tt.expected) } @@ -784,52 +784,61 @@ func TestStripInheritedIdentityNested(t *testing.T) { } func TestNormalizeOperatorSyntax(t *testing.T) { + shadowed := map[string]bool{"+": true} tests := []struct { - name string - input string - schema string - want string + name string + input string + want string }{ { - name: "same-schema operator becomes bare", - input: "(h OPERATOR(public.->) 'a'::text)", - schema: "public", - want: "(h -> 'a'::text)", + name: "same-schema operator becomes bare", + input: "(h OPERATOR(public.->) 'a'::text) AND (h OPERATOR(public.?) 'k'::text)", + want: "(h -> 'a'::text) AND (h ? 'k'::text)", }, { - name: "unqualified OPERATOR() collapses", - input: "(h OPERATOR(->) 'b'::text) AND (h OPERATOR(?) 'k'::text)", - schema: "public", - want: "(h -> 'b'::text) AND (h ? 'k'::text)", + name: "shadowed same-schema operator keeps its qualifier", + input: "(a OPERATOR(public.+) 1) OR (h OPERATOR(public.->) 'a'::text)", + want: "(a OPERATOR(public.+) 1) OR (h -> 'a'::text)", }, { - name: "operator in another schema keeps its qualifier", - input: "(h OPERATOR(ext.->) 'a'::text)", - schema: "public", - want: "(h OPERATOR(ext.->) 'a'::text)", + name: "operator in another schema keeps its qualifier", + input: "(h OPERATOR(ext.->) 'a'::text)", + want: "(h OPERATOR(ext.->) 'a'::text)", }, { - name: "quoted text is untouched", - input: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(->)'", - schema: "public", - want: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(->)'", + name: "quoted text is untouched", + input: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(public.->)'", + want: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(public.->)'", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := normalizeOperatorSyntax(tt.input, tt.schema); got != tt.want { - t.Errorf("normalizeOperatorSyntax(%q, %q) = %q, want %q", tt.input, tt.schema, got, tt.want) + if got := normalizeOperatorSyntax(tt.input, "public", shadowed); got != tt.want { + t.Errorf("normalizeOperatorSyntax(%q) = %q, want %q", tt.input, got, tt.want) } }) } - // The two entry points that see deparsed expressions apply it: generated - // expressions and index expressions go through StripSchemaPrefixFromBody, - // column defaults through normalizeDefaultValue (issue #697). - if got := StripSchemaPrefixFromBody("(h OPERATOR(public.->) 'a'::text)", "public"); got != "(h -> 'a'::text)" { - t.Errorf("StripSchemaPrefixFromBody left operator syntax: %q", got) + // Deparsed expressions (generated expressions, index expressions, views) + // and column defaults, including nextval-backed ones, reduce the operator; + // a shadowed operator keeps its qualifier in both paths (issue #697). + if got := normalizeDeparsedExpression("(h OPERATOR(public.->) 'a'::text)", "public", shadowed); got != "(h -> 'a'::text)" { + t.Errorf("normalizeDeparsedExpression left operator syntax: %q", got) } - if got := normalizeDefaultValue(`('"a"=>"1"'::hstore OPERATOR(public.->) 'a')`, "public"); got != `('"a"=>"1"'::hstore -> 'a')` { + if got := normalizeDefaultValue(`('"a"=>"1"'::hstore OPERATOR(public.->) 'a')`, "public", shadowed); got != `('"a"=>"1"'::hstore -> 'a')` { t.Errorf("normalizeDefaultValue left operator syntax: %q", got) } + if got := normalizeDefaultValue("ARRAY[nextval('s'::regclass)::integer] OPERATOR(public.->) ARRAY[1]", "public", shadowed); got != "ARRAY[nextval('s'::regclass)::integer] -> ARRAY[1]" { + t.Errorf("normalizeDefaultValue skipped the nextval default: %q", got) + } + if got := normalizeDefaultValue("(1 OPERATOR(public.+) 2)", "public", shadowed); got != "(1 OPERATOR(public.+) 2)" { + t.Errorf("normalizeDefaultValue rewrote a shadowed operator: %q", got) + } + + // Prefix stripping alone (used to compare raw function bodies) leaves + // OPERATOR(schema.op) exactly as written: OPERATOR() has its own + // precedence, and dropping only the schema would leave OPERATOR(op). + if got := StripSchemaPrefixFromBody("SELECT 2 OPERATOR(public.*) 3 + public.f(4)", "public"); got != "SELECT 2 OPERATOR(public.*) 3 + f(4)" { + t.Errorf("StripSchemaPrefixFromBody changed operator syntax: %q", got) + } } From 4aed48c466e0354ba44d3aad0700c1318dbf3af3 Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 01:44:02 -0800 Subject: [PATCH 4/8] fix: exempt only fully shadowed operator names and normalize routine parameter defaults (#697) Review follow-up. The shadowed-operator exemption is decided per name because the normalizer only sees deparsed text, so a name with both a shadowed and an unshadowed overload (a custom public.->(jsonb, text) next to hstore's ->) is no longer exempted: the extension operator is the common case and the one this fix is for. Routine parameter defaults are normalized after the temp-schema rename like column defaults. Co-Authored-By: Claude Fable 5.1 --- cmd/plan/external_db_integration_test.go | 30 ++++++++++++++++++++---- cmd/plan/plan.go | 8 +++++++ ir/inspector.go | 25 +++++++++++++------- ir/ir.go | 10 ++++---- 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/cmd/plan/external_db_integration_test.go b/cmd/plan/external_db_integration_test.go index 1984235de..d2aef4dc5 100644 --- a/cmd/plan/external_db_integration_test.go +++ b/cmd/plan/external_db_integration_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/pgplex/pgschema/cmd/util" "github.com/pgplex/pgschema/internal/postgres" "github.com/pgplex/pgschema/testutil" "github.com/stretchr/testify/assert" @@ -300,10 +301,26 @@ func TestExternalDatabase_ExtensionOperatorInPublicPlansClean(t *testing.T) { targetConn, targetHost, targetPort, targetDatabase, targetUser, targetPassword := testutil.ConnectToPostgres(t, targetDB) defer targetConn.Close() + // Operators pgschema does not manage, present on both databases: + // - public.+(integer, integer) duplicates pg_catalog's and is reachable + // only qualified: a fully shadowed name, kept as OPERATOR(public.+). + // - public.->(jsonb, text) duplicates pg_catalog's too, but hstore's + // ->(hstore, text) shares the name and is not shadowed, so the name + // is not exempted and the hstore views below must still plan clean. + // The operator functions live outside the managed schema so the plan + // does not see them as unmanaged objects to drop. + setupSQL := ` +CREATE EXTENSION hstore; +CREATE SCHEMA ops; +CREATE FUNCTION ops.int_add(integer, integer) RETURNS integer LANGUAGE sql IMMUTABLE AS 'SELECT $1 + $2'; +CREATE OPERATOR public.+ (LEFTARG = integer, RIGHTARG = integer, FUNCTION = ops.int_add); +CREATE FUNCTION ops.jsonb_arrow(jsonb, text) RETURNS jsonb LANGUAGE sql IMMUTABLE AS 'SELECT $1 -> $2'; +CREATE OPERATOR public.-> (LEFTARG = jsonb, RIGHTARG = text, FUNCTION = ops.jsonb_arrow); +` // Every expression context that can carry an operator: column default and // generated expression (deparsed under search_path = pg_catalog), CHECK - // constraint, index expression and predicate, view and materialized view. - // The quoted alias must survive untouched. + // constraint, index expression and predicate, routine parameter default, + // view and materialized view. The quoted alias must survive untouched. schemaSQL := ` CREATE TABLE t ( id integer PRIMARY KEY, @@ -313,16 +330,21 @@ CREATE TABLE t ( CONSTRAINT c CHECK ((h ? 'k') OR h IS NULL) ); CREATE INDEX i ON t ((h -> 'b')) WHERE (h ? 'b'); +CREATE FUNCTION pick(x text DEFAULT ('a=>1'::hstore -> 'a')) RETURNS text LANGUAGE sql AS 'SELECT x'; CREATE VIEW v AS SELECT id, h -> 'a' AS ha, h ? 'k' AS hk, 1 AS "OPERATOR(public.->)" FROM t; CREATE MATERIALIZED VIEW mv AS SELECT id, h -> 'a' AS ha FROM t; ` - _, err := targetConn.Exec("CREATE EXTENSION hstore;" + schemaSQL) + _, err := targetConn.Exec(setupSQL + schemaSQL) require.NoError(t, err) planConn, planHost, planPort, planDatabase, planUser, planPassword := testutil.ConnectToPostgres(t, planDB) defer planConn.Close() - _, err = planConn.Exec("CREATE EXTENSION hstore") + _, err = planConn.Exec(setupSQL) + require.NoError(t, err) + + targetIR, err := util.GetIRFromDatabase(targetHost, targetPort, targetDatabase, targetUser, targetPassword, "disable", "public", "pgschema-test", nil, "") require.NoError(t, err) + require.Equal(t, []string{"+"}, targetIR.Metadata.ShadowedOperators, "only a name whose every overload is shadowed is exempted") file := filepath.Join(t.TempDir(), "schema.sql") require.NoError(t, os.WriteFile(file, []byte(schemaSQL), 0644)) diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index d15fdb8cd..ed048a680 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -680,6 +680,11 @@ func normalizeSchemaNames(irData *ir.IR, fromSchema, toSchema string) { } for _, param := range fn.Parameters { param.DataType = replaceString(param.DataType) + // Parameter defaults are deparsed like column defaults and + // compared verbatim; normalize them the same way (#697). + if param.DefaultValue != nil { + *param.DefaultValue = stripQualifiers(replaceString(*param.DefaultValue)) + } } // Normalize function dependencies for topological sorting for i := range fn.Dependencies { @@ -696,6 +701,9 @@ func normalizeSchemaNames(irData *ir.IR, fromSchema, toSchema string) { proc.Definition = replaceString(proc.Definition) for _, param := range proc.Parameters { param.DataType = replaceString(param.DataType) + if param.DefaultValue != nil { + *param.DefaultValue = stripQualifiers(replaceString(*param.DefaultValue)) + } } } diff --git a/ir/inspector.go b/ir/inspector.go index aefdb8be2..8ece29369 100644 --- a/ir/inspector.go +++ b/ir/inspector.go @@ -235,23 +235,32 @@ func (i *Inspector) BuildIR(ctx context.Context, targetSchema string) (*IR, erro } // buildShadowedOperators records the operator names in the inspected and -// managed schemas that a pg_catalog operator with the same name and operand -// types shadows. pg_catalog is implicitly first on every search_path, so such -// an operator is reachable only schema-qualified: ruleutils always deparses it -// as OPERATOR(schema.op), and normalizeOperatorSyntax must leave that form -// alone instead of reducing it to the bare operator (issue #697). +// managed schemas whose every overload a pg_catalog operator with the same +// name and operand types shadows. pg_catalog is implicitly first on every +// search_path, so such an operator is reachable only schema-qualified: +// ruleutils always deparses it as OPERATOR(schema.op), and +// normalizeOperatorSyntax must leave that form alone instead of reducing it +// to the bare operator (issue #697). +// +// The decision is per name because the normalizer only sees the deparsed +// text, never the operand types. A name with both shadowed and unshadowed +// overloads (a custom public.->(jsonb, text) next to hstore's ->(hstore, text)) +// is therefore not exempted: the unshadowed, extension-provided overload is +// the common case, and exempting the name would make every view using it +// replan forever, which is the bug this normalization fixes. func (i *Inspector) buildShadowedOperators(ctx context.Context, schema *IR, targetSchema string) error { rows, err := i.db.QueryContext(ctx, ` -SELECT DISTINCT o.oprname +SELECT o.oprname FROM pg_catalog.pg_operator o JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace WHERE n.nspname IN ($1, $2) - AND EXISTS ( +GROUP BY o.oprname +HAVING bool_and(EXISTS ( SELECT 1 FROM pg_catalog.pg_operator c WHERE c.oprnamespace = 'pg_catalog'::regnamespace AND c.oprname = o.oprname AND c.oprleft = o.oprleft - AND c.oprright = o.oprright) + AND c.oprright = o.oprright)) ORDER BY 1`, targetSchema, i.managedSchema) if err != nil { return err diff --git a/ir/ir.go b/ir/ir.go index 87607c5fc..858cb2053 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -17,10 +17,12 @@ type IR struct { type Metadata struct { DatabaseVersion string `json:"database_version"` // ShadowedOperators lists the operator names in the inspected and managed - // schemas that duplicate a pg_catalog operator's name and operand types. - // pg_catalog is searched first, so an unqualified reference to such a name - // resolves to the catalog operator; ruleutils always deparses these as - // OPERATOR(schema.op) and normalization keeps that form (issue #697). + // schemas whose every overload duplicates a pg_catalog operator's name and + // operand types. pg_catalog is searched first, so an unqualified reference + // to such a name resolves to the catalog operator; ruleutils always + // deparses these as OPERATOR(schema.op) and normalization keeps that form + // (issue #697). See Inspector.buildShadowedOperators for why this is + // decided per name rather than per overload. ShadowedOperators []string `json:"shadowed_operators,omitempty"` } From 7d11e79b0d17149a95a20241261f81827e03f19a Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 01:56:14 -0800 Subject: [PATCH 5/8] refactor: scan OPERATOR() references instead of regex-over-segments (#697) Review follow-up: handle a quote_ident-form schema (OPERATOR("My Schema".op)), share the operator character set as ir.OperatorNameChars, make the OPERATOR( look-behind in prefix stripping constant-time, and stop re-running the pattern inside the plan-side replacement. Co-Authored-By: Claude Fable 5.1 --- cmd/plan/plan.go | 6 ++-- ir/normalize.go | 83 +++++++++++++++++++++++++++++++++++--------- ir/normalize_test.go | 12 +++++++ 3 files changed, 83 insertions(+), 18 deletions(-) diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index ed048a680..bad795dc2 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -870,11 +870,13 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] // the target schema (e.g. hstore's -> installed in public) comes out as // OPERATOR(public.->) while the target side prints the bare operator. // Operator names use only the characters PostgreSQL allows (issue #697). - operatorPattern := regexp.MustCompile(`OPERATOR\(` + regexp.QuoteMeta(prefix) + `([-+*/<>=~!@#%^&|` + "`" + `?]+)\)`) + operatorPrefix := "OPERATOR(" + prefix + operatorPattern := regexp.MustCompile(regexp.QuoteMeta(operatorPrefix) + `[` + regexp.QuoteMeta(ir.OperatorNameChars) + `]+\)`) replaceQualifiers := func(s string) string { s = funcPattern.ReplaceAllString(s, `${1}(`) s = operatorPattern.ReplaceAllStringFunc(s, func(match string) string { - name := operatorPattern.FindStringSubmatch(match)[1] + // match is operatorPrefix + name + ")" + name := match[len(operatorPrefix) : len(match)-1] if shadowedOperators[name] { return match } diff --git a/ir/normalize.go b/ir/normalize.go index e9b3e4c70..5a239758d 100644 --- a/ir/normalize.go +++ b/ir/normalize.go @@ -503,8 +503,8 @@ func normalizeDeparsedExpression(expr, schema string, shadowed map[string]bool) return normalizeOperatorSyntax(StripSchemaPrefixFromBody(expr, schema), schema, shadowed) } -// operatorNameChars are the characters PostgreSQL allows in an operator name. -const operatorNameChars = "-+*/<>=~!@#%^&|`?" +// OperatorNameChars are the characters PostgreSQL allows in an operator name. +const OperatorNameChars = "-+*/<>=~!@#%^&|`?" // normalizeOperatorSyntax rewrites OPERATOR(schema.op) to the bare op when // schema is the object's own schema, unless op is a shadowed name. @@ -521,26 +521,77 @@ const operatorNameChars = "-+*/<>=~!@#%^&|`?" // A shadowed operator (same name and operand types as a pg_catalog operator, // see Metadata.ShadowedOperators) is the one case where the bare name would // resolve differently, so its qualifier is kept. Operators in other schemas -// keep their qualifier, and quoted text is left untouched. +// keep their qualifier. The schema is recognized in bare and quote_ident form +// ("My Schema".op); single-quoted literals and double-quoted identifiers are +// copied through untouched. func normalizeOperatorSyntax(expr, schema string, shadowed map[string]bool) string { - if schema == "" || !strings.Contains(expr, "OPERATOR("+schema+".") { + const keyword = "OPERATOR(" + if schema == "" || !strings.Contains(expr, keyword) { return expr } - pattern := regexp.MustCompile(`OPERATOR\(` + regexp.QuoteMeta(schema) + `\.([` + regexp.QuoteMeta(operatorNameChars) + `]+)\)`) - segments := splitQuotedSegments(expr) - for i, segment := range segments { - if segment == "" || segment[0] == '\'' || segment[0] == '"' { + bare := schema + "." + quoted := QuoteIdentifier(schema) + "." + + var out strings.Builder + out.Grow(len(expr)) + for i := 0; i < len(expr); { + c := expr[i] + if c == '\'' || c == '"' { + end := closingQuote(expr, i) + out.WriteString(expr[i:end]) + i = end continue } - segments[i] = pattern.ReplaceAllStringFunc(segment, func(match string) string { - name := pattern.FindStringSubmatch(match)[1] - if shadowed[name] { - return match + if c == 'O' && strings.HasPrefix(expr[i:], keyword) && (i == 0 || !isIdentChar(expr[i-1])) { + rest := expr[i+len(keyword):] + prefixLen := 0 + if strings.HasPrefix(rest, bare) { + prefixLen = len(bare) + } else if strings.HasPrefix(rest, quoted) { + prefixLen = len(quoted) } - return name - }) + if prefixLen > 0 { + nameEnd := prefixLen + for nameEnd < len(rest) && strings.IndexByte(OperatorNameChars, rest[nameEnd]) >= 0 { + nameEnd++ + } + if nameEnd > prefixLen && nameEnd < len(rest) && rest[nameEnd] == ')' { + name := rest[prefixLen:nameEnd] + if shadowed[name] { + out.WriteString(expr[i : i+len(keyword)+nameEnd+1]) + } else { + out.WriteString(name) + } + i += len(keyword) + nameEnd + 1 + continue + } + } + out.WriteString(keyword) + i += len(keyword) + continue + } + out.WriteByte(c) + i++ } - return strings.Join(segments, "") + return out.String() +} + +// closingQuote returns the index just past the quoted literal or identifier +// that starts at expr[start], treating a doubled quote as an escape. An +// unterminated quote runs to the end of expr. +func closingQuote(expr string, start int) int { + q := expr[start] + for j := start + 1; j < len(expr); j++ { + if expr[j] != q { + continue + } + if j+1 < len(expr) && expr[j+1] == q { + j++ + continue + } + return j + 1 + } + return len(expr) } // stripSchemaPrefixOccurrences removes every occurrence of prefix ("schema." @@ -606,7 +657,7 @@ func stripSchemaPrefixOccurrences(body, prefix string) string { // dropping just the schema there would leave OPERATOR(op), which // PostgreSQL never emits and which resolves differently for a // shadowed operator. - if !inString && i+prefixLen <= len(body) && body[i:i+prefixLen] == prefix && !strings.HasSuffix(body[:i], "OPERATOR(") { + if !inString && i+prefixLen <= len(body) && body[i:i+prefixLen] == prefix && !(i >= len("OPERATOR(") && body[i-len("OPERATOR("):i] == "OPERATOR(") { // Ensure this is a schema qualifier, not part of a longer identifier // (e.g., "not_public.users" should not match) if i == 0 || !isIdentChar(body[i-1]) { diff --git a/ir/normalize_test.go b/ir/normalize_test.go index ac0bb7977..89248a725 100644 --- a/ir/normalize_test.go +++ b/ir/normalize_test.go @@ -805,6 +805,11 @@ func TestNormalizeOperatorSyntax(t *testing.T) { input: "(h OPERATOR(ext.->) 'a'::text)", want: "(h OPERATOR(ext.->) 'a'::text)", }, + { + name: "unterminated or non-operator OPERATOR( text is copied through", + input: "OPERATOR(public.foo) OPERATOR(public.->", + want: "OPERATOR(public.foo) OPERATOR(public.->", + }, { name: "quoted text is untouched", input: "'OPERATOR(public.->)'::text, \"OPERATOR(public.->)\", 'it''s OPERATOR(public.->)'", @@ -819,6 +824,13 @@ func TestNormalizeOperatorSyntax(t *testing.T) { }) } + // The schema is also recognized in quote_ident form, as the deparsers + // render a schema that needs quoting, while a quoted identifier that merely + // looks like the construct is copied through. + if got := normalizeOperatorSyntax(`(h OPERATOR("My Schema".->) 'a'::text) AS "OPERATOR(""My Schema"".->)"`, "My Schema", nil); got != `(h -> 'a'::text) AS "OPERATOR(""My Schema"".->)"` { + t.Errorf("normalizeOperatorSyntax with quoted schema = %q", got) + } + // Deparsed expressions (generated expressions, index expressions, views) // and column defaults, including nextval-backed ones, reduce the operator; // a shadowed operator keeps its qualifier in both paths (issue #697). From 470f511ab1fa3ccce7e9e30e8dbdb45e10e66130 Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 02:39:06 -0800 Subject: [PATCH 6/8] fix: strip quote_ident-form schema qualifiers on the plan side; tolerate OPERATOR keyword case (#697) Review follow-up: newSameSchemaQualifierStripper recognizes the target schema in bare and quote_ident form for function, cast and operator qualifiers; the OPERATOR( look-behind in prefix stripping accepts any keyword case and whitespace; doc wording fixed. Co-Authored-By: Claude Fable 5.1 --- cmd/plan/plan.go | 57 ++++++++++++++++++++++++++++++++++++------- cmd/plan/plan_test.go | 39 +++++++++++++++++++++++++++++ ir/inspector.go | 4 +-- ir/normalize.go | 20 ++++++++++++++- ir/normalize_test.go | 11 +++++++-- 5 files changed, 117 insertions(+), 14 deletions(-) diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index bad795dc2..0948679d4 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -861,22 +861,35 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] if schema == "" { return func(s string) string { return s } } - prefix := schema + "." - funcPattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_$]*)\(`) - typePattern := regexp.MustCompile(`::` + regexp.QuoteMeta(prefix)) + // The deparsers render the schema bare or in quote_ident form ("My Schema"). + prefixes := []string{schema + "."} + if quoted := ir.QuoteIdentifier(schema); quoted != schema { + prefixes = append(prefixes, quoted+".") + } + quotedPrefixes := make([]string, len(prefixes)) + for i, p := range prefixes { + quotedPrefixes[i] = regexp.QuoteMeta(p) + } + prefixAlternatives := `(?:` + strings.Join(quotedPrefixes, "|") + `)` + funcPattern := regexp.MustCompile(prefixAlternatives + `([a-zA-Z_][a-zA-Z0-9_$]*)\(`) + typePattern := regexp.MustCompile(`::` + prefixAlternatives) // OPERATOR(schema.op): ruleutils qualifies an operator whenever its schema // is not on the deparsing session's search_path. The plan database is // inspected with only the temporary schema on the path, so an operator in // the target schema (e.g. hstore's -> installed in public) comes out as // OPERATOR(public.->) while the target side prints the bare operator. // Operator names use only the characters PostgreSQL allows (issue #697). - operatorPrefix := "OPERATOR(" + prefix - operatorPattern := regexp.MustCompile(regexp.QuoteMeta(operatorPrefix) + `[` + regexp.QuoteMeta(ir.OperatorNameChars) + `]+\)`) + operatorPattern := regexp.MustCompile(`OPERATOR\(` + prefixAlternatives + `([` + regexp.QuoteMeta(ir.OperatorNameChars) + `]+)\)`) replaceQualifiers := func(s string) string { s = funcPattern.ReplaceAllString(s, `${1}(`) s = operatorPattern.ReplaceAllStringFunc(s, func(match string) string { - // match is operatorPrefix + name + ")" - name := match[len(operatorPrefix) : len(match)-1] + // match is "OPERATOR(" + prefix + name + ")"; the name is the + // trailing run of operator characters. + nameStart := len(match) - 1 + for nameStart > 0 && strings.IndexByte(ir.OperatorNameChars, match[nameStart-1]) >= 0 { + nameStart-- + } + name := match[nameStart : len(match)-1] if shadowedOperators[name] { return match } @@ -884,13 +897,33 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] }) return typePattern.ReplaceAllString(s, "::") } + containsPrefix := func(s string) bool { + for _, p := range prefixes { + if strings.Contains(s, p) { + return true + } + } + return false + } + // quotedPrefixLen returns the length of the quote_ident-form schema prefix + // starting at s[i], or 0 when none starts there. + quotedPrefixLen := func(s string, i int) int { + for _, p := range prefixes[1:] { + if strings.HasPrefix(s[i:], p) { + return len(p) + } + } + return 0 + } return func(s string) string { - if s == "" || !strings.Contains(s, prefix) { + if s == "" || !containsPrefix(s) { return s } // Replace only outside single-quoted literals and double-quoted - // identifiers; a doubled quote inside either is an escape. + // identifiers; a doubled quote inside either is an escape. The quoted + // schema token itself ("My Schema".) is not an opaque identifier: it + // stays in the replaceable segment so the patterns above can strip it. var result strings.Builder result.Grow(len(s)) segmentStart := 0 @@ -899,6 +932,12 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] if quote != '\'' && quote != '"' { continue } + if quote == '"' { + if n := quotedPrefixLen(s, i); n > 0 { + i += n - 1 + continue + } + } result.WriteString(replaceQualifiers(s[segmentStart:i])) literalStart := i diff --git a/cmd/plan/plan_test.go b/cmd/plan/plan_test.go index 1a82df6c6..244677369 100644 --- a/cmd/plan/plan_test.go +++ b/cmd/plan/plan_test.go @@ -239,6 +239,45 @@ func TestNormalizeSchemaNames_StripsSameSchemaQualifiersFromViewDefinitions(t *t } } +func TestNormalizeSchemaNames_StripsQuotedSchemaQualifiers(t *testing.T) { + // A target schema that needs quoting is rendered as "My Schema". by the + // deparsers; function, cast and operator qualifiers in that form are + // stripped too, while an identifier that merely contains the token stays. + const tempSchema = "pgschema_tmp_20260326_123456_abcd1234" + irData := &ir.IR{ + Metadata: ir.Metadata{ShadowedOperators: []string{"+"}}, + Schemas: map[string]*ir.Schema{ + tempSchema: { + Name: tempSchema, + Views: map[string]*ir.View{ + "v": { + Schema: tempSchema, + Name: "v", + Definition: " SELECT \"My Schema\".nlevel(path) AS depth,\n" + + " path::\"My Schema\".ltree AS p,\n" + + " h OPERATOR(\"My Schema\".->) 'a'::text AS ha,\n" + + " id OPERATOR(\"My Schema\".+) 1 AS shadowed,\n" + + " 1 AS \"x \"\"My Schema\"\".y(\"\n" + + " FROM t", + }, + }, + }, + }, + } + + normalizeSchemaNames(irData, tempSchema, "My Schema") + + expected := " SELECT nlevel(path) AS depth,\n" + + " path::ltree AS p,\n" + + " h -> 'a'::text AS ha,\n" + + " id OPERATOR(\"My Schema\".+) 1 AS shadowed,\n" + + " 1 AS \"x \"\"My Schema\"\".y(\"\n" + + " FROM t" + if got := irData.Schemas["My Schema"].Views["v"].Definition; got != expected { + t.Fatalf("expected quoted-schema qualifiers stripped:\n%s\ngot:\n%s", expected, got) + } +} + func TestNormalizeSchemaNames_PreservesInheritedPartitionDefault(t *testing.T) { const tempSchema = "pgschema_tmp_partition595" childDefault, parentDefault := "public.member_default()", "public.member_default()" diff --git a/ir/inspector.go b/ir/inspector.go index 8ece29369..0c2ae8a32 100644 --- a/ir/inspector.go +++ b/ir/inspector.go @@ -235,8 +235,8 @@ func (i *Inspector) BuildIR(ctx context.Context, targetSchema string) (*IR, erro } // buildShadowedOperators records the operator names in the inspected and -// managed schemas whose every overload a pg_catalog operator with the same -// name and operand types shadows. pg_catalog is implicitly first on every +// managed schemas whose every overload is shadowed by a pg_catalog operator +// with the same name and operand types. pg_catalog is implicitly first on every // search_path, so such an operator is reachable only schema-qualified: // ruleutils always deparses it as OPERATOR(schema.op), and // normalizeOperatorSyntax must leave that form alone instead of reducing it diff --git a/ir/normalize.go b/ir/normalize.go index 5a239758d..dab207787 100644 --- a/ir/normalize.go +++ b/ir/normalize.go @@ -657,7 +657,7 @@ func stripSchemaPrefixOccurrences(body, prefix string) string { // dropping just the schema there would leave OPERATOR(op), which // PostgreSQL never emits and which resolves differently for a // shadowed operator. - if !inString && i+prefixLen <= len(body) && body[i:i+prefixLen] == prefix && !(i >= len("OPERATOR(") && body[i-len("OPERATOR("):i] == "OPERATOR(") { + if !inString && i+prefixLen <= len(body) && body[i:i+prefixLen] == prefix && !followsOperatorKeyword(body, i) { // Ensure this is a schema qualifier, not part of a longer identifier // (e.g., "not_public.users" should not match) if i == 0 || !isIdentChar(body[i-1]) { @@ -687,6 +687,24 @@ func stripSchemaPrefixOccurrences(body, prefix string) string { return result.String() } +// followsOperatorKeyword reports whether body[i] is the first character +// inside an OPERATOR( construct, i.e. preceded by the keyword OPERATOR in any +// letter case, optional whitespace, and the opening parenthesis. +func followsOperatorKeyword(body string, i int) bool { + const keyword = "operator" + j := i - 1 + if j < 0 || body[j] != '(' { + return false + } + for j--; j >= 0 && (body[j] == ' ' || body[j] == '\t' || body[j] == '\n' || body[j] == '\r'); j-- { + } + start := j + 1 - len(keyword) + if start < 0 || !strings.EqualFold(body[start:j+1], keyword) { + return false + } + return start == 0 || !isIdentChar(body[start-1]) +} + // StripSchemaQualifiers removes "schema." qualifiers from a string containing // SQL identifiers, such as a type or argument list, where the token directly // before an unquoted dot is the schema name in bare or quote_ident form. diff --git a/ir/normalize_test.go b/ir/normalize_test.go index 89248a725..fd86b545b 100644 --- a/ir/normalize_test.go +++ b/ir/normalize_test.go @@ -850,7 +850,14 @@ func TestNormalizeOperatorSyntax(t *testing.T) { // Prefix stripping alone (used to compare raw function bodies) leaves // OPERATOR(schema.op) exactly as written: OPERATOR() has its own // precedence, and dropping only the schema would leave OPERATOR(op). - if got := StripSchemaPrefixFromBody("SELECT 2 OPERATOR(public.*) 3 + public.f(4)", "public"); got != "SELECT 2 OPERATOR(public.*) 3 + f(4)" { - t.Errorf("StripSchemaPrefixFromBody changed operator syntax: %q", got) + for input, want := range map[string]string{ + "SELECT 2 OPERATOR(public.*) 3 + public.f(4)": "SELECT 2 OPERATOR(public.*) 3 + f(4)", + "SELECT 2 operator (public.*) 3 + public.f(4)": "SELECT 2 operator (public.*) 3 + f(4)", + "SELECT 2 Operator\t(public.*) 3 + public.f(4)": "SELECT 2 Operator\t(public.*) 3 + f(4)", + "SELECT xoperator(public.f(4))": "SELECT xoperator(f(4))", + } { + if got := StripSchemaPrefixFromBody(input, "public"); got != want { + t.Errorf("StripSchemaPrefixFromBody(%q) = %q, want %q", input, got, want) + } } } From 75bf02eb8557e7d8df0c3feba2757293213ab125 Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 02:54:38 -0800 Subject: [PATCH 7/8] fix: strip qualifiers from quoted object names and accept OPERATOR keyword case in the scanner (#697) Review follow-up: the plan-side stripper handles a quote_ident-form function or type name after the schema qualifier, and normalizeOperatorSyntax detects the OPERATOR keyword like followsOperatorKeyword does (any case, optional whitespace). Co-Authored-By: Claude Fable 5.1 --- cmd/plan/plan.go | 56 ++++++++++++++++++++++++++++++------------- cmd/plan/plan_test.go | 6 +++++ ir/normalize.go | 55 ++++++++++++++++++++++++------------------ ir/normalize_test.go | 9 +++++-- 4 files changed, 85 insertions(+), 41 deletions(-) diff --git a/cmd/plan/plan.go b/cmd/plan/plan.go index 0948679d4..cbe19c63d 100644 --- a/cmd/plan/plan.go +++ b/cmd/plan/plan.go @@ -871,7 +871,8 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] quotedPrefixes[i] = regexp.QuoteMeta(p) } prefixAlternatives := `(?:` + strings.Join(quotedPrefixes, "|") + `)` - funcPattern := regexp.MustCompile(prefixAlternatives + `([a-zA-Z_][a-zA-Z0-9_$]*)\(`) + // The object name after the qualifier is bare or quote_ident form too. + funcPattern := regexp.MustCompile(prefixAlternatives + `([a-zA-Z_][a-zA-Z0-9_$]*|"(?:[^"]|"")+")\(`) typePattern := regexp.MustCompile(`::` + prefixAlternatives) // OPERATOR(schema.op): ruleutils qualifies an operator whenever its schema // is not on the deparsing session's search_path. The plan database is @@ -915,15 +916,43 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] } return 0 } + // endsWithPrefix reports whether s[:i] ends with a schema qualifier, so a + // quoted identifier starting at s[i] is the qualified object's own name. + endsWithPrefix := func(s string, i int) bool { + for _, p := range prefixes { + if strings.HasSuffix(s[:i], p) { + return true + } + } + return false + } + // closingQuote returns the index of the quote that closes the literal or + // identifier opened at s[i]; a doubled quote inside is an escape. + closingQuote := func(s string, i int) int { + quote := s[i] + for i++; i < len(s); i++ { + if s[i] != quote { + continue + } + if i+1 < len(s) && s[i+1] == quote { + i++ + continue + } + break + } + return min(i, len(s)-1) + } return func(s string) string { if s == "" || !containsPrefix(s) { return s } // Replace only outside single-quoted literals and double-quoted - // identifiers; a doubled quote inside either is an escape. The quoted - // schema token itself ("My Schema".) is not an opaque identifier: it - // stays in the replaceable segment so the patterns above can strip it. + // identifiers; a doubled quote inside either is an escape. Two quoted + // tokens are not opaque: the quoted schema itself ("My Schema".) and + // a quoted object name right after a qualifier (public."My Func"(). + // Both stay in the replaceable segment so the patterns can see the + // whole qualified reference. var result strings.Builder result.Grow(len(s)) segmentStart := 0 @@ -937,22 +966,17 @@ func newSameSchemaQualifierStripper(schema string, shadowedOperators map[string] i += n - 1 continue } + if endsWithPrefix(s, i) { + i = closingQuote(s, i) + continue + } } result.WriteString(replaceQualifiers(s[segmentStart:i])) literalStart := i - for i++; i < len(s); i++ { - if s[i] != quote { - continue - } - if i+1 < len(s) && s[i+1] == quote { - i++ - continue - } - break - } - result.WriteString(s[literalStart:min(i+1, len(s))]) - segmentStart = min(i+1, len(s)) + i = closingQuote(s, i) + result.WriteString(s[literalStart : i+1]) + segmentStart = i + 1 } result.WriteString(replaceQualifiers(s[segmentStart:])) return result.String() diff --git a/cmd/plan/plan_test.go b/cmd/plan/plan_test.go index 244677369..d546170d0 100644 --- a/cmd/plan/plan_test.go +++ b/cmd/plan/plan_test.go @@ -254,7 +254,9 @@ func TestNormalizeSchemaNames_StripsQuotedSchemaQualifiers(t *testing.T) { Schema: tempSchema, Name: "v", Definition: " SELECT \"My Schema\".nlevel(path) AS depth,\n" + + " \"My Schema\".\"Fn Name\"(path) AS named,\n" + " path::\"My Schema\".ltree AS p,\n" + + " path::\"My Schema\".\"Type Name\" AS typed,\n" + " h OPERATOR(\"My Schema\".->) 'a'::text AS ha,\n" + " id OPERATOR(\"My Schema\".+) 1 AS shadowed,\n" + " 1 AS \"x \"\"My Schema\"\".y(\"\n" + @@ -268,7 +270,9 @@ func TestNormalizeSchemaNames_StripsQuotedSchemaQualifiers(t *testing.T) { normalizeSchemaNames(irData, tempSchema, "My Schema") expected := " SELECT nlevel(path) AS depth,\n" + + " \"Fn Name\"(path) AS named,\n" + " path::ltree AS p,\n" + + " path::\"Type Name\" AS typed,\n" + " h -> 'a'::text AS ha,\n" + " id OPERATOR(\"My Schema\".+) 1 AS shadowed,\n" + " 1 AS \"x \"\"My Schema\"\".y(\"\n" + @@ -390,6 +394,7 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { " 'OPERATOR(public.->)'::text AS literal,\n" + " 1 AS \"OPERATOR(public.->)\",\n" + " \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" + + " public.\"Fn Name\"(id) AS named, id::public.\"Type Name\" AS typed,\n" + " j -> 'a'::text AS ja\n" + " FROM t", }, @@ -408,6 +413,7 @@ func TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers(t *testing.T) { " 'OPERATOR(public.->)'::text AS literal,\n" + " 1 AS \"OPERATOR(public.->)\",\n" + " \"public.nlevel(\" AS \"it''s \"\"public.x(\"\"\",\n" + + " \"Fn Name\"(id) AS named, id::\"Type Name\" AS typed,\n" + " j -> 'a'::text AS ja\n" + " FROM t" if got := irData.Schemas["public"].Views["v"].Definition; got != expected { diff --git a/ir/normalize.go b/ir/normalize.go index dab207787..fecabd749 100644 --- a/ir/normalize.go +++ b/ir/normalize.go @@ -525,8 +525,8 @@ const OperatorNameChars = "-+*/<>=~!@#%^&|`?" // ("My Schema".op); single-quoted literals and double-quoted identifiers are // copied through untouched. func normalizeOperatorSyntax(expr, schema string, shadowed map[string]bool) string { - const keyword = "OPERATOR(" - if schema == "" || !strings.Contains(expr, keyword) { + const keyword = "operator" + if schema == "" || !strings.Contains(strings.ToLower(expr), keyword) { return expr } bare := schema + "." @@ -542,32 +542,41 @@ func normalizeOperatorSyntax(expr, schema string, shadowed map[string]bool) stri i = end continue } - if c == 'O' && strings.HasPrefix(expr[i:], keyword) && (i == 0 || !isIdentChar(expr[i-1])) { - rest := expr[i+len(keyword):] - prefixLen := 0 - if strings.HasPrefix(rest, bare) { - prefixLen = len(bare) - } else if strings.HasPrefix(rest, quoted) { - prefixLen = len(quoted) + // The keyword in any letter case, as a whole word, then optional + // whitespace and the opening parenthesis (same as followsOperatorKeyword). + if (c == 'O' || c == 'o') && i+len(keyword) <= len(expr) && strings.EqualFold(expr[i:i+len(keyword)], keyword) && + (i == 0 || !isIdentChar(expr[i-1])) && (i+len(keyword) == len(expr) || !isIdentChar(expr[i+len(keyword)])) { + open := i + len(keyword) + for open < len(expr) && (expr[open] == ' ' || expr[open] == '\t' || expr[open] == '\n' || expr[open] == '\r') { + open++ } - if prefixLen > 0 { - nameEnd := prefixLen - for nameEnd < len(rest) && strings.IndexByte(OperatorNameChars, rest[nameEnd]) >= 0 { - nameEnd++ + if open < len(expr) && expr[open] == '(' { + rest := expr[open+1:] + prefixLen := 0 + if strings.HasPrefix(rest, bare) { + prefixLen = len(bare) + } else if strings.HasPrefix(rest, quoted) { + prefixLen = len(quoted) } - if nameEnd > prefixLen && nameEnd < len(rest) && rest[nameEnd] == ')' { - name := rest[prefixLen:nameEnd] - if shadowed[name] { - out.WriteString(expr[i : i+len(keyword)+nameEnd+1]) - } else { - out.WriteString(name) + if prefixLen > 0 { + nameEnd := prefixLen + for nameEnd < len(rest) && strings.IndexByte(OperatorNameChars, rest[nameEnd]) >= 0 { + nameEnd++ + } + if nameEnd > prefixLen && nameEnd < len(rest) && rest[nameEnd] == ')' { + end := open + 1 + nameEnd + 1 + if name := rest[prefixLen:nameEnd]; shadowed[name] { + out.WriteString(expr[i:end]) + } else { + out.WriteString(name) + } + i = end + continue } - i += len(keyword) + nameEnd + 1 - continue } } - out.WriteString(keyword) - i += len(keyword) + out.WriteString(expr[i:open]) + i = open continue } out.WriteByte(c) diff --git a/ir/normalize_test.go b/ir/normalize_test.go index fd86b545b..291592cb0 100644 --- a/ir/normalize_test.go +++ b/ir/normalize_test.go @@ -807,8 +807,13 @@ func TestNormalizeOperatorSyntax(t *testing.T) { }, { name: "unterminated or non-operator OPERATOR( text is copied through", - input: "OPERATOR(public.foo) OPERATOR(public.->", - want: "OPERATOR(public.foo) OPERATOR(public.->", + input: "OPERATOR(public.foo) OPERATOR(public.-> xoperator(public.->) operator", + want: "OPERATOR(public.foo) OPERATOR(public.-> xoperator(public.->) operator", + }, + { + name: "keyword case and whitespace before the parenthesis", + input: "(h operator (public.->) 'a'::text) AND (h Operator\t(public.?) 'k'::text)", + want: "(h -> 'a'::text) AND (h ? 'k'::text)", }, { name: "quoted text is untouched", From bae5ca323ba63f2dd87872884f6bd29cc95c8fab Mon Sep 17 00:00:00 2001 From: Tianzhou Date: Sun, 8 Nov 2026 04:39:10 -0800 Subject: [PATCH 8/8] fix: strip same-schema qualifiers from quoted function names in column defaults (#697) The plan-side stripper now handles quoted object names, so the current-side default normalizer must too, or a default such as public."MyFunc"() compares differently on the two sides (broke dependency/issue_571 in CI). The replacement is also quote-aware now, so string literals are left alone. Regenerates the issue 571 fixture: its default reads "MyFunc"() like every other expression context already did. Co-Authored-By: Claude Fable 5.1 --- ir/normalize.go | 30 ++++++++++++++++--- ir/normalize_test.go | 16 ++++++++++ .../diff.sql | 2 +- .../plan.json | 2 +- .../plan.sql | 2 +- .../plan.txt | 2 +- 6 files changed, 46 insertions(+), 8 deletions(-) diff --git a/ir/normalize.go b/ir/normalize.go index fecabd749..ee29f8615 100644 --- a/ir/normalize.go +++ b/ir/normalize.go @@ -247,13 +247,35 @@ func normalizeDefaultValue(value string, tableSchema string, shadowed map[string // Normalize function calls - remove schema qualifiers for functions in the same schema // This matches PostgreSQL's pg_get_expr() behavior which strips same-schema qualifiers // Example: public.get_status() -> get_status() (when tableSchema is "public") + // public."MyFunc"() -> "MyFunc"() (quoted names too, like every other + // expression context, issue #697) // other_schema.get_status() -> other_schema.get_status() (preserved) if tableSchema != "" && strings.Contains(value, tableSchema+".") { - // Pattern: schema.function_name( - // Replace "tableSchema." with "" when followed by identifier and ( + // Pattern: schema.function_name( or schema."Function Name"( + // Replace "tableSchema." with "" when followed by an identifier and ( prefix := tableSchema + "." - pattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_]*)\(`) - value = pattern.ReplaceAllString(value, `${1}(`) + pattern := regexp.MustCompile(regexp.QuoteMeta(prefix) + `([a-zA-Z_][a-zA-Z0-9_]*|"(?:[^"]|"")+")\(`) + segments := splitQuotedSegments(value) + for i, segment := range segments { + // A quoted function name is a segment of its own; join it with the + // preceding unquoted text so the pattern sees schema."Name"(. + if segment == "" || segment[0] == '\'' { + continue + } + if segment[0] == '"' { + if i == 0 || !strings.HasSuffix(segments[i-1], prefix) { + continue + } + segments[i-1] = strings.TrimSuffix(segments[i-1], prefix) + if i+1 < len(segments) && strings.HasPrefix(segments[i+1], "(") { + continue + } + segments[i-1] += prefix + continue + } + segments[i] = pattern.ReplaceAllString(segment, `${1}(`) + } + value = strings.Join(segments, "") } // Handle type casting - remove explicit type casts that are semantically equivalent diff --git a/ir/normalize_test.go b/ir/normalize_test.go index 291592cb0..fb19549be 100644 --- a/ir/normalize_test.go +++ b/ir/normalize_test.go @@ -866,3 +866,19 @@ func TestNormalizeOperatorSyntax(t *testing.T) { } } } + +func TestNormalizeDefaultValueStripsQuotedFunctionQualifier(t *testing.T) { + // Defaults strip the same-schema qualifier from quoted function names the + // way view, index and generated expressions already do (issue #697). + for input, want := range map[string]string{ + `public."MyFunc"()`: `"MyFunc"()`, + `public."My ""Func"""(1)`: `"My ""Func"""(1)`, + `other."MyFunc"()`: `other."MyFunc"()`, + `public.get_status()`: `get_status()`, + `'public."MyFunc"()'::text`: `'public."MyFunc"()'`, + } { + if got := normalizeDefaultValue(input, "public", nil); got != want { + t.Errorf("normalizeDefaultValue(%q) = %q, want %q", input, got, want) + } + } +} diff --git a/testdata/diff/dependency/issue_571_quoted_function_dependency/diff.sql b/testdata/diff/dependency/issue_571_quoted_function_dependency/diff.sql index fd1e9491c..74fcbc49d 100644 --- a/testdata/diff/dependency/issue_571_quoted_function_dependency/diff.sql +++ b/testdata/diff/dependency/issue_571_quoted_function_dependency/diff.sql @@ -30,7 +30,7 @@ $$; CREATE TABLE IF NOT EXISTS widgets ( id SERIAL, - code integer DEFAULT public."MyFunc"(), + code integer DEFAULT "MyFunc"(), CONSTRAINT widgets_pkey PRIMARY KEY (id), CONSTRAINT widgets_code_excl EXCLUDE USING btree ("MyFunc"(code) WITH =) ); diff --git a/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.json b/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.json index 7289f1e04..9caaccf16 100644 --- a/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.json +++ b/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.json @@ -27,7 +27,7 @@ "path": "public.AWrapper" }, { - "sql": "CREATE TABLE IF NOT EXISTS widgets (\n id SERIAL,\n code integer DEFAULT public.\"MyFunc\"(),\n CONSTRAINT widgets_pkey PRIMARY KEY (id),\n CONSTRAINT widgets_code_excl EXCLUDE USING btree (\"MyFunc\"(code) WITH =)\n);", + "sql": "CREATE TABLE IF NOT EXISTS widgets (\n id SERIAL,\n code integer DEFAULT \"MyFunc\"(),\n CONSTRAINT widgets_pkey PRIMARY KEY (id),\n CONSTRAINT widgets_code_excl EXCLUDE USING btree (\"MyFunc\"(code) WITH =)\n);", "type": "table", "operation": "create", "path": "public.widgets" diff --git a/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.sql b/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.sql index fd1e9491c..74fcbc49d 100644 --- a/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.sql +++ b/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.sql @@ -30,7 +30,7 @@ $$; CREATE TABLE IF NOT EXISTS widgets ( id SERIAL, - code integer DEFAULT public."MyFunc"(), + code integer DEFAULT "MyFunc"(), CONSTRAINT widgets_pkey PRIMARY KEY (id), CONSTRAINT widgets_code_excl EXCLUDE USING btree ("MyFunc"(code) WITH =) ); diff --git a/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.txt b/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.txt index 481f9a077..323914fc8 100644 --- a/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.txt +++ b/testdata/diff/dependency/issue_571_quoted_function_dependency/plan.txt @@ -49,7 +49,7 @@ $$; CREATE TABLE IF NOT EXISTS widgets ( id SERIAL, - code integer DEFAULT public."MyFunc"(), + code integer DEFAULT "MyFunc"(), CONSTRAINT widgets_pkey PRIMARY KEY (id), CONSTRAINT widgets_code_excl EXCLUDE USING btree ("MyFunc"(code) WITH =) );