Skip to content

Conversation

bheemreddy181
Copy link

@bheemreddy181 bheemreddy181 commented Aug 12, 2025

Summary

Resolves #179 by implementing intelligent type coercion analysis that automatically switches to DROP COLUMN + ADD COLUMN for incompatible type conversions instead of failing with cast errors.

Description

This PR introduces a comprehensive type coercion analysis system that intelligently handles column type changes by categorizing conversions and choosing the appropriate alteration strategy:

  • Binary coercible types (e.g., smallintinteger): Use ALTER COLUMN with minimal locking
  • Coercible types (e.g., textinteger): Use ALTER COLUMN with table rewrite warnings
  • Incompatible types (e.g., integertimestamptz): Use DROP COLUMN + ADD COLUMN with data loss warnings

Key Changes

  • Added GetTypeCoercionInfo() function for comprehensive type conversion analysis
  • Enhanced column alteration logic to automatically select optimal conversion approach
  • Improved hazard reporting to distinguish between different conversion methods
  • Added appropriate data loss warnings for destructive operations
  • Maintains full backward compatibility

Motivation

Problem: Previously, attempting to alter incompatible column types (like integer to timestamptz) would fail with cast errors, requiring manual intervention.

Solution: This change automatically detects incompatible conversions and uses the appropriate DROP/ADD strategy while providing clear warnings about potential data loss.

Related Issues:

Technical Details

The implementation categorizes type conversions into three levels:

  1. Binary Coercible: Direct type casting possible, minimal performance impact
  2. Coercible: Casting possible but may require table rewrite
  3. Incompatible: No direct casting possible, requires column recreation

Testing

New Test Coverage

Regression Testing

  • All existing tests continue to pass
  • Backward compatibility verified across all scenarios

Before/After

Before:

ALTER COLUMN integer_col TYPE timestamptz;
-- ❌ Fails with: "cannot cast type integer to timestamp with time zone"

After:

-- ✅ Automatically generates:
-- DROP COLUMN integer_col;
-- ADD COLUMN integer_col timestamptz;
-- With appropriate hazard warnings about data loss

Resolves stripe#179 by implementing intelligent type coercion analysis that
automatically switches to DROP COLUMN + ADD COLUMN for incompatible
type conversions instead of failing with cast errors.

Key Changes:
- Added comprehensive type coercion analysis system with GetTypeCoercionInfo()
- Categorizes type conversions as binary coercible, coercible, or incompatible
- Modified column alteration logic to use DROP/ADD for incompatible types
- Enhanced hazard reporting to distinguish between conversion approaches
- Added appropriate data loss warnings for destructive operations

Technical Details:
- Binary coercible types (e.g., smallint→integer) use ALTER with minimal locking
- Coercible types (e.g., text→integer) use ALTER with table rewrite warnings
- Incompatible types (e.g., integer→timestamptz) use DROP/ADD with data loss warnings
- Maintains backward compatibility for all existing functionality
- Addresses related issue stripe#52 for better hazard reporting on binary coercible types

Test Coverage:
- Added comprehensive unit tests for type coercion logic
- Added integration tests for issue stripe#179 scenarios
- All existing tests continue to pass

Before: ALTER COLUMN integer to timestamptz would fail with cast error
After: Generates DROP COLUMN + ADD COLUMN with appropriate hazard warnings
@bheemreddy181
Copy link
Author

@bplunkett-stripe thoughts ?

@bheemreddy181
Copy link
Author

@Navbryce can I get a code review when ever you can ?

@bheemreddy181
Copy link
Author

@Navbryce @bplunkett-stripe can you take a look at this and share some feedback ?

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.

[FEATURE] Support switching between non-coercible types via drop/add
1 participant