|
| 1 | +# PostgreSQL AST Transformation Tasks |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This project implements a comprehensive PostgreSQL AST transformation system that enables upgrading Abstract Syntax Trees (ASTs) across PostgreSQL versions 13 through 17. The system provides a modular, type-safe approach to handling the structural differences between PostgreSQL versions while preserving semantic equivalence. |
| 6 | + |
| 7 | +## Goals |
| 8 | + |
| 9 | +### Primary Objectives |
| 10 | +1. **Eliminate `any` types** - Implement precise type-safe transformations using proper PostgreSQL version-specific types |
| 11 | +2. **Sequential version transformations** - Build transformers for each version transition: 13→14, 14→15, 15→16, 16→17 |
| 12 | +3. **Semantic equivalence preservation** - Ensure transformed ASTs maintain the same semantic meaning across versions |
| 13 | +4. **Complex node type conversions** - Handle intricate AST structure changes like A_Const field restructuring, Integer value transformations, and ObjectWithArgs context-sensitive rules |
| 14 | + |
| 15 | +### Technical Goals |
| 16 | +- **End-to-end integration testing** - Parse with PG13 → Transform to PG17 → Deparse → Parse again → Verify semantic equality |
| 17 | +- **Dynamic method dispatch** - Use node type names as method names (like deparser pattern) for clean, maintainable code |
| 18 | +- **Context-aware transformations** - Handle complex rules for adding/removing fields based on statement context |
| 19 | +- **Field preservation** - Maintain all necessary AST fields while applying version-specific transformations |
| 20 | + |
| 21 | +## Architecture |
| 22 | + |
| 23 | +### Core Components |
| 24 | + |
| 25 | +#### 1. Base Transformer (`src/visitors/base.ts`) |
| 26 | +- **BaseTransformer class** - Foundation for all version-specific transformers |
| 27 | +- **Dynamic method dispatch** - Automatically calls methods based on node type names |
| 28 | +- **Field preservation logic** - Ensures critical fields are maintained during transformations |
| 29 | +- **Context passing** - Supports transformation context for complex rules |
| 30 | + |
| 31 | +#### 2. Version-Specific Transformers |
| 32 | +- **V13ToV14Transformer** (`src/transformers/v13-to-v14.ts`) - Handles PG13→PG14 transformations |
| 33 | +- **V14ToV15Transformer** (`src/transformers/v14-to-v15.ts`) - Handles PG14→PG15 transformations including A_Const restructuring |
| 34 | +- **V15ToV16Transformer** (`src/transformers/v15-to-v16.ts`) - Handles PG15→PG16 transformations |
| 35 | +- **V16ToV17Transformer** (`src/transformers/v16-to-v17.ts`) - Handles PG16→PG17 transformations |
| 36 | + |
| 37 | +#### 3. Orchestration Layer (`src/transformer.ts`) |
| 38 | +- **ASTTransformer** - Coordinates individual version transformers |
| 39 | +- **PG13ToPG17Transformer** - Chains all transformations for complete 13→17 upgrade |
| 40 | +- **ParseResult handling** - Manages wrapper structures and version metadata |
| 41 | + |
| 42 | +#### 4. Test Infrastructure |
| 43 | +- **FixtureTestUtils** (`test-utils/index.ts`) - Comprehensive testing utilities |
| 44 | +- **Full transformation flow** (`test-utils/full-transform-flow.ts`) - End-to-end testing workflow |
| 45 | +- **Kitchen sink tests** (`__tests__/kitchen-sink/`) - Real-world SQL test cases organized by version transitions |
| 46 | + |
| 47 | +## Setup Instructions |
| 48 | + |
| 49 | +### Prerequisites |
| 50 | +- Node.js with yarn package manager |
| 51 | +- PostgreSQL parser packages (@pgsql/parser, @pgsql/deparser) |
| 52 | + |
| 53 | +### Installation & Build |
| 54 | +```bash |
| 55 | +# From repository root |
| 56 | +yarn install |
| 57 | +yarn build |
| 58 | + |
| 59 | +# Navigate to transform package |
| 60 | +cd packages/transform |
| 61 | +``` |
| 62 | + |
| 63 | +### Development Environment |
| 64 | +```bash |
| 65 | +# Install dependencies |
| 66 | +yarn install |
| 67 | + |
| 68 | +# Run tests |
| 69 | +yarn test |
| 70 | + |
| 71 | +# Watch mode for development |
| 72 | +yarn test:watch |
| 73 | + |
| 74 | +# Run specific test suites |
| 75 | +yarn test __tests__/kitchen-sink/13-14/ |
| 76 | +yarn test __tests__/kitchen-sink/14-15/ |
| 77 | +``` |
| 78 | + |
| 79 | +## Testing Strategy |
| 80 | + |
| 81 | +### Test Categories |
| 82 | + |
| 83 | +#### 1. Unit Tests (`__test__/`) |
| 84 | +- **Individual transformer tests** - Test each version transformer in isolation |
| 85 | +- **Method-specific tests** - Verify individual node transformation methods |
| 86 | +- **Debug tests** - Targeted tests for specific transformation issues |
| 87 | + |
| 88 | +#### 2. Integration Tests (`__tests__/kitchen-sink/`) |
| 89 | +- **Version transition tests** - Organized by version pairs (13-14, 14-15, etc.) |
| 90 | +- **Real SQL fixtures** - Test against actual PostgreSQL SQL statements |
| 91 | +- **Semantic equivalence verification** - Parse→Transform→Parse→Compare workflow |
| 92 | + |
| 93 | +#### 3. End-to-End Tests |
| 94 | +- **Full transformation flow** - Complete PG13→PG17 transformation pipeline |
| 95 | +- **Round-trip testing** - Verify semantic equivalence after full transformation |
| 96 | +- **Deparser integration** - Test with actual PostgreSQL deparser output |
| 97 | + |
| 98 | +### Test Workflow |
| 99 | +1. **Parse SQL with source version parser** (e.g., PG13) |
| 100 | +2. **Transform AST through version chain** (13→14→15→16→17) |
| 101 | +3. **Deparse with target version deparser** (PG17) |
| 102 | +4. **Parse deparsed SQL with target parser** (PG17) |
| 103 | +5. **Compare transformed AST with native parsed AST** (using cleanTree for location/spacing normalization) |
| 104 | + |
| 105 | +## Current Status |
| 106 | + |
| 107 | +### Completed Features |
| 108 | +- ✅ Base transformer infrastructure with dynamic method dispatch |
| 109 | +- ✅ V13ToV14Transformer with ObjectWithArgs context-sensitive handling |
| 110 | +- ✅ V14ToV15Transformer with A_Const restructuring and Integer field preservation |
| 111 | +- ✅ V15ToV16Transformer and V16ToV17Transformer foundations |
| 112 | +- ✅ PG13ToPG17Transformer orchestration |
| 113 | +- ✅ Comprehensive test infrastructure |
| 114 | +- ✅ End-to-end integration testing framework |
| 115 | + |
| 116 | +### Key Transformations Implemented |
| 117 | +- **A_Const restructuring** - Transform `val.Integer.ival` to `ival.ival` structure in PG14→PG15 |
| 118 | +- **Integer boundary values** - Handle special cases like -2147483647, -32767 that should result in empty ival objects |
| 119 | +- **ObjectWithArgs context rules** - Add/remove objfuncargs based on statement type and context |
| 120 | +- **String field transformations** - Convert String.str to String.sval across versions |
| 121 | +- **Field preservation** - Maintain critical fields like location, inh, relpersistence during transformations |
| 122 | + |
| 123 | +### Active Development Areas |
| 124 | +- **Remaining test failures** - Systematically addressing failing kitchen-sink tests |
| 125 | +- **Type safety improvements** - Replacing remaining `any` types with proper PostgreSQL version types |
| 126 | +- **Complex node transformations** - Handling edge cases in AST structure differences |
| 127 | +- **Performance optimization** - Improving transformation speed for large ASTs |
| 128 | + |
| 129 | +## Development Workflow |
| 130 | + |
| 131 | +### Adding New Transformations |
| 132 | +1. **Identify failing test** - Use kitchen-sink tests to find transformation issues |
| 133 | +2. **Analyze AST differences** - Compare expected vs actual AST structures |
| 134 | +3. **Implement transformation method** - Add method to appropriate version transformer |
| 135 | +4. **Test and iterate** - Verify fix resolves issue without breaking other tests |
| 136 | +5. **Update documentation** - Document new transformation rules and edge cases |
| 137 | + |
| 138 | +### Debugging Transformation Issues |
| 139 | +1. **Create targeted debug test** - Isolate specific SQL statement causing issues |
| 140 | +2. **Trace transformation pipeline** - Follow AST through each version transformer |
| 141 | +3. **Compare runtime schemas** - Use runtime schema imports to understand node structures |
| 142 | +4. **Implement fix** - Update transformer method with proper field handling |
| 143 | +5. **Verify semantic equivalence** - Ensure fix maintains semantic meaning |
| 144 | + |
| 145 | +### Code Patterns |
| 146 | +- **Method naming** - Use PostgreSQL node type names (e.g., `SelectStmt`, `A_Const`, `FuncCall`) |
| 147 | +- **Field preservation** - Always preserve original fields unless explicitly transforming |
| 148 | +- **Context awareness** - Pass context for complex transformation rules |
| 149 | +- **Type safety** - Use version-specific types for method parameters |
| 150 | + |
| 151 | +## Key Challenges & Solutions |
| 152 | + |
| 153 | +### Challenge: A_Const Structure Changes |
| 154 | +**Problem**: PG14→PG15 changes A_Const from `val.Integer.ival` to `ival.ival` structure |
| 155 | +**Solution**: Implemented A_Const method with special handling for boundary values and proper field restructuring |
| 156 | + |
| 157 | +### Challenge: ObjectWithArgs Context Sensitivity |
| 158 | +**Problem**: objfuncargs field should be added/removed based on statement context |
| 159 | +**Solution**: Context-aware ObjectWithArgs method that checks statement type and object type |
| 160 | + |
| 161 | +### Challenge: Integer Field Preservation |
| 162 | +**Problem**: Integer nodes losing ival field during transformation |
| 163 | +**Solution**: Explicit Integer methods that ensure ival field preservation with proper default values |
| 164 | + |
| 165 | +### Challenge: Dynamic Method Dispatch |
| 166 | +**Problem**: Maintaining clean visitor pattern while handling diverse node types |
| 167 | +**Solution**: BaseTransformer with automatic method dispatch based on node type names |
| 168 | + |
| 169 | +## Future Enhancements |
| 170 | + |
| 171 | +### Short Term |
| 172 | +- Complete remaining kitchen-sink test fixes |
| 173 | +- Eliminate all remaining `any` types |
| 174 | +- Optimize transformation performance |
| 175 | +- Add comprehensive error handling |
| 176 | + |
| 177 | +### Long Term |
| 178 | +- Support for additional PostgreSQL versions (18+) |
| 179 | +- Bidirectional transformations (downgrade support) |
| 180 | +- AST validation and integrity checking |
| 181 | +- Performance benchmarking and optimization |
| 182 | + |
| 183 | +## Resources |
| 184 | + |
| 185 | +### Documentation |
| 186 | +- `README.md` - Package overview and basic usage |
| 187 | +- `AST_PLAN.md` - Detailed AST transformation planning |
| 188 | +- `TRANSFORM_GUIDE.md` - Comprehensive transformation guide |
| 189 | +- `TEST_STRATEGY.md` - Testing approach and methodology |
| 190 | + |
| 191 | +### Key Files |
| 192 | +- `src/index.ts` - Main package exports |
| 193 | +- `src/transformer.ts` - Orchestration layer |
| 194 | +- `src/visitors/base.ts` - Base transformer infrastructure |
| 195 | +- `test-utils/index.ts` - Testing utilities |
| 196 | +- `__tests__/kitchen-sink/` - Integration test suites |
| 197 | + |
| 198 | +### External Dependencies |
| 199 | +- `@pgsql/parser` - PostgreSQL parsing for all versions |
| 200 | +- `@pgsql/deparser` - PostgreSQL deparsing (PG17) |
| 201 | +- Runtime schemas for each PostgreSQL version |
| 202 | +- Type definitions for PostgreSQL AST nodes |
0 commit comments