1212from sqlalchemy .sql import expression
1313import json
1414
15+
1516def process_literal_param_hack (value : Any ):
1617 """This method is supposed to accept a Python type and return a string representation of that type.
1718 But due to some weirdness in the way SQLAlchemy's literal rendering works, we have to return
@@ -400,31 +401,32 @@ def compile_databricks_map(type_, compiler, **kw):
400401 value_type = compiler .process (type_ .value_type , ** kw )
401402 return f"MAP<{ key_type } ,{ value_type } >"
402403
404+
403405class DatabricksVariant (UserDefinedType ):
404406 """
405407 A custom variant type for storing semi-structured data including STRUCT, ARRAY, MAP, and scalar types.
406408 Note: VARIANT MAP types can only have STRING keys.
407-
409+
408410 Examples:
409411 DatabricksVariant() -> VARIANT
410-
412+
411413 Usage:
412414 Column('data', DatabricksVariant())
413415 """
416+
414417 cache_ok = True
415418
416419 def __init__ (self ):
417420 self .pe = ParamEscaper ()
418421
419422 def bind_processor (self , dialect ):
420- """Process values before sending to database.
421- """
423+ """Process values before sending to database."""
422424
423425 def process (value ):
424426 if value is None :
425427 return None
426428 try :
427- return json .dumps (value , ensure_ascii = False , separators = (',' , ':' ))
429+ return json .dumps (value , ensure_ascii = False , separators = ("," , ":" ))
428430 except (TypeError , ValueError ) as e :
429431 raise ValueError (f"Cannot serialize value { value } to JSON: { e } " )
430432
@@ -435,19 +437,23 @@ def bind_expression(self, bindvalue):
435437 return expression .func .PARSE_JSON (bindvalue )
436438
437439 def literal_processor (self , dialect ):
438- """Process literal values for SQL generation.
440+ """Process literal values for SQL generation.
439441 For VARIANT columns, use PARSE_JSON() to properly insert data.
440442 """
443+
441444 def process (value ):
442445 if value is None :
443446 return "NULL"
444447 try :
445- return self .pe .escape_string (json .dumps (value , ensure_ascii = False , separators = (',' , ':' )))
448+ return self .pe .escape_string (
449+ json .dumps (value , ensure_ascii = False , separators = ("," , ":" ))
450+ )
446451 except (TypeError , ValueError ) as e :
447452 raise ValueError (f"Cannot serialize value { value } to JSON: { e } " )
448-
453+
449454 return process
450455
456+
451457@compiles (DatabricksVariant , "databricks" )
452458def compile_variant (type_ , compiler , ** kw ):
453459 return "VARIANT"
0 commit comments