Skip to content

chore: do not strip type cast#95

Merged
tianzhou merged 1 commit intomainfrom
no_cast_strip
Oct 18, 2025
Merged

chore: do not strip type cast#95
tianzhou merged 1 commit intomainfrom
no_cast_strip

Conversation

@tianzhou
Copy link
Contributor

@tianzhou tianzhou commented Oct 16, 2025

Fix #91

Copilot AI review requested due to automatic review settings October 16, 2025 18:51
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 refactors the expression parsing and formatting to preserve type casts in PostgreSQL default values and expressions instead of stripping them. The key changes ensure PostgreSQL's canonical representation is maintained by using pg_query.Deparse for expression handling.

  • Replaced manual expression parsing with pg_query.Deparse to preserve type casts and handle edge cases
  • Updated formatting to preserve canonical PostgreSQL representation with type casts
  • Added normalization to handle case differences between inspector output and parser output

Reviewed Changes

Copilot reviewed 31 out of 31 changed files in this pull request and generated no comments.

Show a summary per file
File Description
ir/parser.go Replaced manual expression parsing with pg_query.Deparse-based approach for better accuracy
ir/normalize.go Added keyword normalization to handle case differences between PostgreSQL functions
ir/formatter.go Removed type cast stripping logic to preserve canonical representation
internal/diff/table.go Removed stripTypeQualifiers function and type cast removal from column defaults
testdata/diff/migrate/v5/* Updated test data to reflect lowercase PostgreSQL keywords
testdata/diff/migrate/v3/* Updated test data to reflect lowercase PostgreSQL keywords
testdata/diff/dependency/* Updated test data to reflect lowercase PostgreSQL keywords
testdata/diff/create_table/* Updated test data to reflect lowercase PostgreSQL keywords
cmd/dump/issue_82_view_logic_expr_* Test files showing preserved vs stripped type casts in view expressions

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@tianzhou tianzhou force-pushed the no_cast_strip branch 2 times, most recently from 89b52ff to 66d4787 Compare October 17, 2025 18:32
@tianzhou tianzhou requested a review from Copilot October 17, 2025 18:33
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

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


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

ir/parser.go Outdated
Comment on lines 872 to 876
// Use simple case-insensitive replacement
// This works because we're only processing deparsed SQL from pg_query,
// which formats keywords consistently
lowercase := strings.ToLower(keyword)
result = strings.ReplaceAll(result, lowercase, keyword)
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

The simple string replacement approach can cause incorrect replacements when the keyword appears as part of another word. For example, 'current_user_id' would be incorrectly transformed to 'CURRENT_USER_id'. Use word boundary matching with regex or more precise string matching to avoid partial word replacements.

Suggested change
// Use simple case-insensitive replacement
// This works because we're only processing deparsed SQL from pg_query,
// which formats keywords consistently
lowercase := strings.ToLower(keyword)
result = strings.ReplaceAll(result, lowercase, keyword)
// Use regex word boundary replacement, case-insensitive
// This avoids replacing keywords inside longer identifiers
pattern := `(?i)\b` + regexp.QuoteMeta(keyword) + `\b`
re := regexp.MustCompile(pattern)
result = re.ReplaceAllString(result, keyword)

Copilot uses AI. Check for mistakes.
ir/normalize.go Outdated
// Remove unnecessary whitespace
value = strings.TrimSpace(value)


Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

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

[nitpick] Unnecessary blank line added. This doesn't add any value and should be removed to maintain consistent code formatting.

Suggested change

Copilot uses AI. Check for mistakes.
@tianzhou tianzhou force-pushed the no_cast_strip branch 3 times, most recently from 7c7b819 to b970e9f Compare October 18, 2025 03:27
@tianzhou tianzhou requested a review from Copilot October 18, 2025 03:28
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

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


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +874 to +876
lowercase := strings.ToLower(keyword)
pattern := regexp.MustCompile(`\b` + regexp.QuoteMeta(lowercase) + `\b`)
result = pattern.ReplaceAllString(result, keyword)
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

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

The regex compilation inside the loop is inefficient. Consider pre-compiling the regex patterns or using a more efficient string replacement approach for better performance when processing multiple keywords.

Copilot uses AI. Check for mistakes.
ir/normalize.go Outdated
// Pattern explanation:
// '([^']*)' - matches a quoted string literal (capturing the content)
// ::[\w.]+(\[\])? - matches ::typename or ::typename[] (including schema.typename and array types)
re := regexp.MustCompile(`'([^']*)'::[\w.]+(\[\])?`)
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

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

The regex is compiled on every function call. Consider moving this to a package-level variable or using sync.Once to compile it only once for better performance.

Copilot uses AI. Check for mistakes.
@tianzhou tianzhou merged commit 477cc35 into main Oct 18, 2025
2 checks passed
@tianzhou tianzhou deleted the no_cast_strip branch October 23, 2025 06:34
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.

column default expression sometimes generate invalid syntax

1 participant