|
23 | 23 | import org.slf4j.Logger; |
24 | 24 | import org.slf4j.LoggerFactory; |
25 | 25 |
|
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.PreparedStatement; |
| 28 | +import java.sql.ResultSet; |
26 | 29 | import java.sql.ResultSetMetaData; |
27 | 30 | import java.sql.SQLException; |
28 | 31 | import java.sql.Types; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
29 | 34 | import java.util.Set; |
30 | 35 | import javax.annotation.Nullable; |
31 | 36 |
|
@@ -70,6 +75,7 @@ public class OracleSourceSchemaReader extends CommonSchemaReader { |
70 | 75 | private final Boolean isTimestampOldBehavior; |
71 | 76 | private final Boolean isPrecisionlessNumAsDecimal; |
72 | 77 | private final Boolean isTimestampLtzFieldTimestamp; |
| 78 | + private Connection connection; |
73 | 79 |
|
74 | 80 | public OracleSourceSchemaReader() { |
75 | 81 | this(null, false, false, false); |
@@ -136,11 +142,130 @@ public Schema getSchema(ResultSetMetaData metadata, int index) throws SQLExcepti |
136 | 142 | } |
137 | 143 | return Schema.decimalOf(precision, scale); |
138 | 144 | } |
| 145 | + case Types.STRUCT: |
| 146 | + if (connection == null) { |
| 147 | + throw new SQLException("Cannot resolve STRUCT schema without a database connection. " |
| 148 | + + "Use getSchemaFields(ResultSet) to enable STRUCT type resolution."); |
| 149 | + } |
| 150 | + String typeName = metadata.getColumnTypeName(index); |
| 151 | + String oracleSchemaName = metadata.getSchemaName(index); |
| 152 | + return getStructSchema(connection, oracleSchemaName, typeName); |
139 | 153 | default: |
140 | 154 | return super.getSchema(metadata, index); |
141 | 155 | } |
142 | 156 | } |
143 | 157 |
|
| 158 | + @Override |
| 159 | + public List<Schema.Field> getSchemaFields(ResultSet resultSet) throws SQLException { |
| 160 | + this.connection = resultSet.getStatement().getConnection(); |
| 161 | + return super.getSchemaFields(resultSet); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Builds a CDAP RECORD schema for an Oracle STRUCT type by querying the database metadata |
| 166 | + * for the type's attributes. |
| 167 | + * |
| 168 | + * @param connection the database connection |
| 169 | + * @param schemaName the Oracle schema owning the type |
| 170 | + * @param typeName the Oracle type name (e.g., "ADDRESS_TYPE") |
| 171 | + * @return a CDAP RECORD schema with fields corresponding to the STRUCT's attributes |
| 172 | + */ |
| 173 | + private Schema getStructSchema(Connection connection, String schemaName, |
| 174 | + String typeName) throws SQLException { |
| 175 | + List<Schema.Field> fields = new ArrayList<>(); |
| 176 | + |
| 177 | + String sql = "SELECT * FROM ALL_TYPE_ATTRS WHERE TYPE_NAME = ? ORDER BY ATTR_NO"; |
| 178 | + |
| 179 | + try (PreparedStatement stmt = connection.prepareStatement(sql)) { |
| 180 | + stmt.setString(1, typeName.substring(typeName.lastIndexOf('.') + 1)); |
| 181 | + |
| 182 | + try (ResultSet attrRs = stmt.executeQuery()) { |
| 183 | + while (attrRs.next()) { |
| 184 | + String attrName = attrRs.getString("ATTR_NAME"); |
| 185 | + String attrTypeName = attrRs.getString("ATTR_TYPE_NAME"); |
| 186 | + int attrSize = attrRs.getInt("PRECISION"); |
| 187 | + int attrScale = attrRs.getInt("SCALE"); |
| 188 | + |
| 189 | + Schema attrSchema = mapPrimitiveOracleType(attrTypeName, attrSize, attrScale); |
| 190 | + if (attrSchema != null) { |
| 191 | + fields.add(Schema.Field.of(attrName, attrSchema)); |
| 192 | + } else { |
| 193 | + Schema nestedSchema = getStructSchema(connection, schemaName, attrTypeName); |
| 194 | + fields.add(Schema.Field.of(attrName, nestedSchema)); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + if (fields.isEmpty()) { |
| 200 | + throw new SQLException(String.format( |
| 201 | + "No attributes found for Oracle STRUCT type '%s.%s'. " |
| 202 | + + "Ensure the type exists and is accessible.", |
| 203 | + schemaName, typeName)); |
| 204 | + } |
| 205 | + |
| 206 | + return Schema.recordOf(typeName, fields); |
| 207 | + } |
| 208 | + |
| 209 | + private Schema mapPrimitiveOracleType(String typeName, int precision, int scale) { |
| 210 | + switch (typeName) { |
| 211 | + case "TIMESTAMP WITH TZ": |
| 212 | + return isTimestampOldBehavior ? Schema.of(Schema.Type.STRING) : Schema.of(Schema.LogicalType.TIMESTAMP_MICROS); |
| 213 | + case "TIMESTAMP WITH LTZ": |
| 214 | + return getTimestampLtzSchema(); |
| 215 | + case "TIMESTAMP": |
| 216 | + return Schema.of(Schema.LogicalType.DATETIME); |
| 217 | + case "DATE" : |
| 218 | + return Schema.of(Schema.LogicalType.DATE); |
| 219 | + case "BINARY FLOAT": |
| 220 | + case "FLOAT": |
| 221 | + return Schema.of(Schema.Type.FLOAT); |
| 222 | + case "BINARY DOUBLE": |
| 223 | + case "DOUBLE": |
| 224 | + return Schema.of(Schema.Type.DOUBLE); |
| 225 | + case "BFILE": |
| 226 | + case "RAW": |
| 227 | + case "LONG RAW": |
| 228 | + return Schema.of(Schema.Type.BYTES); |
| 229 | + case "INTERVAL DAY TO SECOND": |
| 230 | + case "INTERVAL YEAR TO MONTH": |
| 231 | + case "VARCHAR2": |
| 232 | + case "VARCHAR": |
| 233 | + case "CHAR": |
| 234 | + case "CLOB": |
| 235 | + case "BLOB": |
| 236 | + case "LONG": |
| 237 | + return Schema.of(Schema.Type.STRING); |
| 238 | + case "INTEGER": |
| 239 | + return Schema.of(Schema.Type.INT); |
| 240 | + case "NUMBER": |
| 241 | + case "DECIMAL": |
| 242 | + // FLOAT and REAL are returned as java.sql.Types.NUMERIC but with value that is a java.lang.Double |
| 243 | + if (Double.class.getTypeName().equals(typeName)) { |
| 244 | + return Schema.of(Schema.Type.DOUBLE); |
| 245 | + } else { |
| 246 | + if (precision == 0) { |
| 247 | + if (isPrecisionlessNumAsDecimal) { |
| 248 | + precision = 38; |
| 249 | + scale = 0; |
| 250 | + LOG.warn(String.format("%s type with undefined precision and scale is detected, " |
| 251 | + + "there may be a precision loss while running the pipeline. " |
| 252 | + + "Please define an output precision and scale for field to avoid " |
| 253 | + + "precision loss.", typeName)); |
| 254 | + return Schema.decimalOf(precision, scale); |
| 255 | + } else { |
| 256 | + LOG.warn(String.format("%s type without precision and scale, " |
| 257 | + + "converting into STRING type to avoid any precision loss.", |
| 258 | + typeName)); |
| 259 | + return Schema.of(Schema.Type.STRING); |
| 260 | + } |
| 261 | + } |
| 262 | + return Schema.decimalOf(precision, scale); |
| 263 | + } |
| 264 | + default: |
| 265 | + return null; |
| 266 | + } |
| 267 | + } |
| 268 | + |
144 | 269 | private @NotNull Schema getTimestampLtzSchema() { |
145 | 270 | return isTimestampOldBehavior || isTimestampLtzFieldTimestamp |
146 | 271 | ? Schema.of(Schema.LogicalType.TIMESTAMP_MICROS) |
|
0 commit comments