4141import java .sql .Statement ;
4242import java .sql .Time ;
4343import java .sql .Timestamp ;
44+ import java .util .Arrays ;
4445import java .util .logging .Logger ;
4546
4647/**
@@ -293,18 +294,26 @@ public static long write(File f, String tableName, ResultSet rs,
293294 if (scale > 0 && precision <= 18 ) {
294295 group .add (columnName , decimal .unscaledValue ().longValue ());
295296 } else if (scale > 0 ) {
296- byte [] bytes =
297- decimal .setScale (scale , RoundingMode .HALF_EVEN )
298- .unscaledValue ().toByteArray ();
297+ // Scale the decimal to the desired precision and scale
298+ byte [] unscaledBytes = decimal
299+ .setScale (scale , RoundingMode .HALF_EVEN )
300+ .unscaledValue ()
301+ .toByteArray ();
302+ int requiredBytes = 16 ;
299303
300- // Ensure the byte array is padded correctly for the precision
301- int numBytes =
302- Math .max ((int ) Math .ceil (precision / Math .log10 (2 ) / 8 ),
303- bytes .length ); // Calculate required bytes
304- byte [] paddedBytes = new byte [numBytes ];
305- System .arraycopy (bytes , 0 , paddedBytes , numBytes - bytes .length ,
306- bytes .length );
304+ // Ensure the byte array is padded correctly with sign extension
305+ byte [] paddedBytes = new byte [requiredBytes ];
306+ byte signByte = (unscaledBytes [0 ] < 0 ) ? (byte ) 0xFF : 0x00 ; // Extend
307+ // sign
308+ // bit
309+ Arrays .fill (paddedBytes , 0 ,
310+ requiredBytes - unscaledBytes .length , signByte );
311+ System .arraycopy (unscaledBytes , 0 , paddedBytes ,
312+ requiredBytes - unscaledBytes .length ,
313+ unscaledBytes .length );
307314
315+ // Add the binary representation of the decimal to the Parquet
316+ // group
308317 group .add (columnName ,
309318 Binary .fromConstantByteArray (paddedBytes ));
310319 } else if (scale == -127 ) {
@@ -317,17 +326,27 @@ public static long write(File f, String tableName, ResultSet rs,
317326 */
318327 precision = 38 ;
319328 scale = 10 ;
320- byte [] bytes = decimal .setScale (scale , RoundingMode .HALF_EVEN )
321- .unscaledValue ().toByteArray ();
322329
323- // Ensure the byte array is padded correctly for the precision
324- int numBytes =
325- Math .max ((int ) Math .ceil (precision / Math .log10 (2 ) / 8 ),
326- bytes .length ); // Calculate required bytes
327- byte [] paddedBytes = new byte [numBytes ];
328- System .arraycopy (bytes , 0 , paddedBytes , numBytes - bytes .length ,
329- bytes .length );
330+ // Scale the decimal to the desired precision and scale
331+ byte [] unscaledBytes = decimal
332+ .setScale (scale , RoundingMode .HALF_EVEN )
333+ .unscaledValue ()
334+ .toByteArray ();
335+ int requiredBytes = 16 ;
330336
337+ // Ensure the byte array is padded correctly with sign extension
338+ byte [] paddedBytes = new byte [requiredBytes ];
339+ byte signByte = (unscaledBytes [0 ] < 0 ) ? (byte ) 0xFF : 0x00 ; // Extend
340+ // sign
341+ // bit
342+ Arrays .fill (paddedBytes , 0 ,
343+ requiredBytes - unscaledBytes .length , signByte );
344+ System .arraycopy (unscaledBytes , 0 , paddedBytes ,
345+ requiredBytes - unscaledBytes .length ,
346+ unscaledBytes .length );
347+
348+ // Add the binary representation of the decimal to the Parquet
349+ // group
331350 group .add (columnName ,
332351 Binary .fromConstantByteArray (paddedBytes ));
333352 } else if (precision < 5 ) {
0 commit comments