Skip to content

Conversation

@isaacparker0
Copy link
Contributor

@isaacparker0 isaacparker0 commented Nov 14, 2025

Fix custom type arrays (enums, composites, domains) incorrectly outputting PostgreSQL's internal _typename format instead of SQL standard typename[] syntax.

@isaacparker0 isaacparker0 marked this pull request as draft November 14, 2025 14:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the normalization of custom array types (enums, composites, domains) by converting PostgreSQL's internal _typename format to the SQL standard typename[] syntax. This ensures that DDL statements and schema comparisons use the correct array notation.

Key changes:

  • Added logic to detect and convert underscore-prefixed custom array types to bracket notation
  • Added test case for enum array columns to verify the fix

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ir/normalize.go Added custom array type normalization logic to convert _typename to typename[] format
testdata/diff/create_table/enum_array_column/old.sql Test fixture: schema before adding enum array column
testdata/diff/create_table/enum_array_column/new.sql Test fixture: schema after adding enum array column
testdata/diff/create_table/enum_array_column/diff.sql Expected DDL output showing status[] array syntax
testdata/diff/create_table/enum_array_column/plan.sql Expected plan SQL with normalized array type
testdata/diff/create_table/enum_array_column/plan.txt Human-readable plan output
testdata/diff/create_table/enum_array_column/plan.json Machine-readable plan output in JSON format

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +783 to +787
// Handle custom array types (internal PostgreSQL array notation)
// e.g., _my_enum_type -> my_enum_type[]
if strings.HasPrefix(typeName, "_") {
return typeName[1:] + "[]"
}
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom array type normalization only handles direct type names but doesn't handle type casts. When custom array types appear in expressions with type casts (e.g., value::_my_enum), they won't be normalized to value::my_enum[].

Consider adding normalization for custom array types in the type cast handling section (lines 756-759). For example:

// Replace PostgreSQL internal type names with standard SQL types in type casts
for pgType, sqlType := range typeMap {
    expr = strings.ReplaceAll(expr, "::"+pgType, "::"+sqlType)
}

// Handle custom array types in type casts
// e.g., ::_my_enum -> ::my_enum[]
expr = regexp.MustCompile(`::(pg_catalog\.)?_(\w+)`).ReplaceAllString(expr, "::$2[]")

Copilot uses AI. Check for mistakes.
@@ -780,6 +780,12 @@ func normalizePostgreSQLType(input string) string {
return after
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pg_catalog prefix removal returns immediately without checking if the result is a custom array type. For example, pg_catalog._my_enum would be returned as _my_enum instead of being further normalized to my_enum[].

The logic should continue to check for the underscore prefix after removing pg_catalog:

// Remove pg_catalog prefix for unmapped types
if after, found := strings.CutPrefix(typeName, "pg_catalog."); found {
    typeName = after
}

// Handle custom array types (internal PostgreSQL array notation)
// e.g., _my_enum_type -> my_enum_type[]
if strings.HasPrefix(typeName, "_") {
    return typeName[1:] + "[]"
}

return typeName
Suggested change
return after
typeName = after

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

LGTM overall. I will merge this and make some refinement in a followup PR.

@tianzhou tianzhou merged commit f1f564a into pgplex:main Nov 14, 2025
7 checks passed
@tianzhou
Copy link
Contributor

Followup #164

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants