transformTypeName converts PostgreSQL type names to DuckDB equivalents
(typeName *pg_query.TypeName)
| 119 | |
| 120 | // transformTypeName converts PostgreSQL type names to DuckDB equivalents |
| 121 | func (t *TypeMappingTransform) transformTypeName(typeName *pg_query.TypeName) bool { |
| 122 | if typeName == nil || len(typeName.Names) == 0 { |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | // Get the type name (last element, ignoring schema prefix) |
| 127 | var typeStr string |
| 128 | var typeNameNode *pg_query.Node |
| 129 | for _, name := range typeName.Names { |
| 130 | if str := name.GetString_(); str != nil { |
| 131 | typeStr = strings.ToLower(str.Sval) |
| 132 | typeNameNode = name |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if typeStr == "" { |
| 137 | return false |
| 138 | } |
| 139 | |
| 140 | // Check if we have a mapping |
| 141 | if newType, ok := typeMapping[typeStr]; ok { |
| 142 | // Handle complex types like decimal(19,4) |
| 143 | if strings.Contains(newType, "(") { |
| 144 | // Parse the type and precision |
| 145 | baseName := strings.Split(newType, "(")[0] |
| 146 | if str := typeNameNode.GetString_(); str != nil { |
| 147 | str.Sval = baseName |
| 148 | } |
| 149 | // For now, we just change the base type name |
| 150 | // Precision handling would need more complex AST manipulation |
| 151 | } else { |
| 152 | if str := typeNameNode.GetString_(); str != nil { |
| 153 | str.Sval = newType |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Remove pg_catalog schema prefix if present |
| 158 | if len(typeName.Names) > 1 { |
| 159 | if first := typeName.Names[0].GetString_(); first != nil { |
| 160 | if strings.ToLower(first.Sval) == "pg_catalog" { |
| 161 | typeName.Names = typeName.Names[1:] |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return true |
| 167 | } |
| 168 | |
| 169 | // Even without a type mapping, strip pg_catalog schema prefix. |
| 170 | // PostgreSQL parser qualifies built-in types as pg_catalog.typename |
| 171 | // (e.g., pg_catalog.json) but DuckDB rejects the pg_catalog prefix. |
| 172 | if len(typeName.Names) > 1 { |
| 173 | if first := typeName.Names[0].GetString_(); first != nil { |
| 174 | if strings.ToLower(first.Sval) == "pg_catalog" { |
| 175 | typeName.Names = typeName.Names[1:] |
| 176 | return true |
| 177 | } |
| 178 | } |