@@ -380,33 +380,21 @@ def _map_sql_type(self, param, parameters_list, i):
380380 )
381381
382382 if isinstance (param , bytes ):
383- if len (param ) > 8000 : # Assuming VARBINARY(MAX) for long byte arrays
384- return (
385- ddbc_sql_const .SQL_VARBINARY .value ,
386- ddbc_sql_const .SQL_C_BINARY .value ,
387- len (param ),
388- 0 ,
389- False ,
390- )
383+ # Use VARBINARY for Python bytes/bytearray since they are variable-length by nature.
384+ # This avoids storage waste from BINARY's zero-padding and matches Python's semantics.
391385 return (
392- ddbc_sql_const .SQL_BINARY .value ,
386+ ddbc_sql_const .SQL_VARBINARY .value ,
393387 ddbc_sql_const .SQL_C_BINARY .value ,
394388 len (param ),
395389 0 ,
396390 False ,
397391 )
398392
399393 if isinstance (param , bytearray ):
400- if len (param ) > 8000 : # Assuming VARBINARY(MAX) for long byte arrays
401- return (
402- ddbc_sql_const .SQL_VARBINARY .value ,
403- ddbc_sql_const .SQL_C_BINARY .value ,
404- len (param ),
405- 0 ,
406- True ,
407- )
394+ # Use VARBINARY for Python bytes/bytearray since they are variable-length by nature.
395+ # This avoids storage waste from BINARY's zero-padding and matches Python's semantics.
408396 return (
409- ddbc_sql_const .SQL_BINARY .value ,
397+ ddbc_sql_const .SQL_VARBINARY .value ,
410398 ddbc_sql_const .SQL_C_BINARY .value ,
411399 len (param ),
412400 0 ,
@@ -848,6 +836,8 @@ def _select_best_sample_value(column):
848836 return max (non_nulls , key = lambda s : len (str (s )))
849837 if all (isinstance (v , datetime .datetime ) for v in non_nulls ):
850838 return datetime .datetime .now ()
839+ if all (isinstance (v , (bytes , bytearray )) for v in non_nulls ):
840+ return max (non_nulls , key = lambda b : len (b ))
851841 if all (isinstance (v , datetime .date ) for v in non_nulls ):
852842 return datetime .date .today ()
853843 return non_nulls [0 ] # fallback
0 commit comments