@@ -227,11 +227,9 @@ protected Schema getSchema(Connection jdbcConnection, TableName tableName, Schem
227227 String dataType ;
228228 String columnName ;
229229 HashMap <String , String > hashMap = new HashMap <>();
230- boolean found = false ;
231230
232231 SchemaBuilder schemaBuilder = SchemaBuilder .newBuilder ();
233- try (ResultSet resultSet = getColumns (jdbcConnection .getCatalog (), tableName , jdbcConnection .getMetaData ());
234- Connection connection = getJdbcConnectionFactory ().getConnection (getCredentialProvider ());
232+ try (Connection connection = getJdbcConnectionFactory ().getConnection (getCredentialProvider ());
235233 PreparedStatement stmt = connection .prepareStatement (dataTypeQuery )) {
236234 // fetch data types of columns and prepare map with column name and datatype.
237235 stmt .setString (1 , tableName .getSchemaName () + "." + tableName .getTableName ());
@@ -242,18 +240,94 @@ protected Schema getSchema(Connection jdbcConnection, TableName tableName, Schem
242240 hashMap .put (columnName .trim (), dataType .trim ());
243241 }
244242 }
243+ }
244+
245+ String environment = DataLakeGen2Util .checkEnvironment (jdbcConnection .getMetaData ().getURL ());
246+
247+ if (DataLakeGen2Constants .SQL_POOL .equalsIgnoreCase (environment )) {
248+ // getColumns() method from SQL Server driver is causing an exception in case of Azure Serverless environment.
249+ // so doing explicit data type conversion
250+ schemaBuilder = doDataTypeConversion (hashMap );
251+ }
252+ else {
253+ schemaBuilder = doDataTypeConversionForNonCompatible (jdbcConnection , tableName , hashMap );
254+ }
255+ // add partition columns
256+ partitionSchema .getFields ().forEach (schemaBuilder ::addField );
257+ return schemaBuilder .build ();
258+ }
259+
260+ private SchemaBuilder doDataTypeConversion (HashMap <String , String > columnNameAndDataTypeMap )
261+ {
262+ SchemaBuilder schemaBuilder = SchemaBuilder .newBuilder ();
263+
264+ for (Map .Entry <String , String > entry : columnNameAndDataTypeMap .entrySet ()) {
265+ String columnName = entry .getKey ();
266+ String dataType = entry .getValue ();
267+ ArrowType columnType = Types .MinorType .VARCHAR .getType ();
268+
269+ if ("char" .equalsIgnoreCase (dataType ) || "varchar" .equalsIgnoreCase (dataType ) || "binary" .equalsIgnoreCase (dataType ) ||
270+ "nchar" .equalsIgnoreCase (dataType ) || "nvarchar" .equalsIgnoreCase (dataType ) || "varbinary" .equalsIgnoreCase (dataType )
271+ || "time" .equalsIgnoreCase (dataType ) || "uniqueidentifier" .equalsIgnoreCase (dataType )) {
272+ columnType = Types .MinorType .VARCHAR .getType ();
273+ }
274+
275+ if ("bit" .equalsIgnoreCase (dataType )) {
276+ columnType = Types .MinorType .TINYINT .getType ();
277+ }
278+
279+ if ("tinyint" .equalsIgnoreCase (dataType ) || "smallint" .equalsIgnoreCase (dataType )) {
280+ columnType = Types .MinorType .SMALLINT .getType ();
281+ }
282+
283+ if ("int" .equalsIgnoreCase (dataType )) {
284+ columnType = Types .MinorType .INT .getType ();
285+ }
286+
287+ if ("bigint" .equalsIgnoreCase (dataType )) {
288+ columnType = Types .MinorType .BIGINT .getType ();
289+ }
290+
291+ if ("decimal" .equalsIgnoreCase (dataType ) || "money" .equalsIgnoreCase (dataType )) {
292+ columnType = Types .MinorType .FLOAT8 .getType ();
293+ }
294+
295+ if ("numeric" .equalsIgnoreCase (dataType ) || "float" .equalsIgnoreCase (dataType ) || "smallmoney" .equalsIgnoreCase (dataType )) {
296+ columnType = Types .MinorType .FLOAT8 .getType ();
297+ }
245298
299+ if ("real" .equalsIgnoreCase (dataType )) {
300+ columnType = Types .MinorType .FLOAT4 .getType ();
301+ }
302+
303+ if ("date" .equalsIgnoreCase (dataType )) {
304+ columnType = Types .MinorType .DATEDAY .getType ();
305+ }
306+
307+ if ("datetime" .equalsIgnoreCase (dataType ) || "datetime2" .equalsIgnoreCase (dataType )
308+ || "smalldatetime" .equalsIgnoreCase (dataType ) || "datetimeoffset" .equalsIgnoreCase (dataType )) {
309+ columnType = Types .MinorType .DATEMILLI .getType ();
310+ }
311+
312+ schemaBuilder .addField (FieldBuilder .newBuilder (columnName , columnType ).build ());
313+ }
314+ return schemaBuilder ;
315+ }
316+
317+ private SchemaBuilder doDataTypeConversionForNonCompatible (Connection jdbcConnection , TableName tableName , HashMap <String , String > columnNameAndDataTypeMap ) throws SQLException
318+ {
319+ SchemaBuilder schemaBuilder = SchemaBuilder .newBuilder ();
320+
321+ try (ResultSet resultSet = getColumns (jdbcConnection .getCatalog (), tableName , jdbcConnection .getMetaData ())) {
322+ boolean found = false ;
246323 while (resultSet .next ()) {
247324 Optional <ArrowType > columnType = JdbcArrowTypeConverter .toArrowType (
248325 resultSet .getInt ("DATA_TYPE" ),
249326 resultSet .getInt ("COLUMN_SIZE" ),
250327 resultSet .getInt ("DECIMAL_DIGITS" ),
251328 configOptions );
252- columnName = resultSet .getString ("COLUMN_NAME" );
253-
254- dataType = hashMap .get (columnName );
255- LOGGER .debug ("columnName: " + columnName );
256- LOGGER .debug ("dataType: " + dataType );
329+ String columnName = resultSet .getString ("COLUMN_NAME" );
330+ String dataType = columnNameAndDataTypeMap .get (columnName );
257331
258332 if (dataType != null && DataLakeGen2DataType .isSupported (dataType )) {
259333 columnType = Optional .of (DataLakeGen2DataType .fromType (dataType ));
@@ -266,21 +340,19 @@ protected Schema getSchema(Connection jdbcConnection, TableName tableName, Schem
266340 columnType = Optional .of (Types .MinorType .VARCHAR .getType ());
267341 }
268342
269- LOGGER .debug ("columnType: " + columnType );
270343 if (columnType .isPresent () && SupportedTypes .isSupported (columnType .get ())) {
271344 schemaBuilder .addField (FieldBuilder .newBuilder (columnName , columnType .get ()).build ());
272345 found = true ;
273346 }
274347 else {
275- LOGGER .error ("getSchema: Unable to map type for column[" + columnName + " ] to a supported type, attempted " + columnType );
348+ LOGGER .error ("getSchema: Unable to map type for column[{} ] to a supported type, attempted {}" , columnName , columnType );
276349 }
277350 }
278351 if (!found ) {
352+ LOGGER .error ("Could not find any supported columns in table: {}.{}" , tableName .getSchemaName (), tableName .getTableName ());
279353 throw new RuntimeException ("Could not find table in " + tableName .getSchemaName ());
280354 }
281-
282- partitionSchema .getFields ().forEach (schemaBuilder ::addField );
283- return schemaBuilder .build ();
284355 }
356+ return schemaBuilder ;
285357 }
286358}
0 commit comments