|
| 1 | +""" |
| 2 | +Type conversion utilities for the Databricks SQL Connector. |
| 3 | +
|
| 4 | +This module provides functionality to convert string values from SEA Inline results |
| 5 | +to appropriate Python types based on column metadata. |
| 6 | +""" |
| 7 | + |
| 8 | +import datetime |
| 9 | +import decimal |
| 10 | +import logging |
| 11 | +from dateutil import parser |
| 12 | +from typing import Any, Callable, Dict, Optional, Union |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class SqlType: |
| 18 | + """ |
| 19 | + SQL type constants |
| 20 | +
|
| 21 | + The list of types can be found in the SEA REST API Reference: |
| 22 | + https://docs.databricks.com/api/workspace/statementexecution/executestatement |
| 23 | + """ |
| 24 | + |
| 25 | + # Numeric types |
| 26 | + BYTE = "byte" |
| 27 | + SHORT = "short" |
| 28 | + INT = "int" |
| 29 | + LONG = "long" |
| 30 | + FLOAT = "float" |
| 31 | + DOUBLE = "double" |
| 32 | + DECIMAL = "decimal" |
| 33 | + |
| 34 | + # Boolean type |
| 35 | + BOOLEAN = "boolean" |
| 36 | + |
| 37 | + # Date/Time types |
| 38 | + DATE = "date" |
| 39 | + TIMESTAMP = "timestamp" |
| 40 | + INTERVAL = "interval" |
| 41 | + |
| 42 | + # String types |
| 43 | + CHAR = "char" |
| 44 | + STRING = "string" |
| 45 | + |
| 46 | + # Binary type |
| 47 | + BINARY = "binary" |
| 48 | + |
| 49 | + # Complex types |
| 50 | + ARRAY = "array" |
| 51 | + MAP = "map" |
| 52 | + STRUCT = "struct" |
| 53 | + |
| 54 | + # Other types |
| 55 | + NULL = "null" |
| 56 | + USER_DEFINED_TYPE = "user_defined_type" |
| 57 | + |
| 58 | + |
| 59 | +class SqlTypeConverter: |
| 60 | + """ |
| 61 | + Utility class for converting SQL types to Python types. |
| 62 | + Based on the types supported by the Databricks SDK. |
| 63 | + """ |
| 64 | + |
| 65 | + # SQL type to conversion function mapping |
| 66 | + # TODO: complex types |
| 67 | + TYPE_MAPPING: Dict[str, Callable] = { |
| 68 | + # Numeric types |
| 69 | + SqlType.BYTE: lambda v: int(v), |
| 70 | + SqlType.SHORT: lambda v: int(v), |
| 71 | + SqlType.INT: lambda v: int(v), |
| 72 | + SqlType.LONG: lambda v: int(v), |
| 73 | + SqlType.FLOAT: lambda v: float(v), |
| 74 | + SqlType.DOUBLE: lambda v: float(v), |
| 75 | + SqlType.DECIMAL: lambda v, p=None, s=None: ( |
| 76 | + decimal.Decimal(v).quantize( |
| 77 | + decimal.Decimal(f'0.{"0" * s}'), context=decimal.Context(prec=p) |
| 78 | + ) |
| 79 | + if p is not None and s is not None |
| 80 | + else decimal.Decimal(v) |
| 81 | + ), |
| 82 | + # Boolean type |
| 83 | + SqlType.BOOLEAN: lambda v: v.lower() in ("true", "t", "1", "yes", "y"), |
| 84 | + # Date/Time types |
| 85 | + SqlType.DATE: lambda v: datetime.date.fromisoformat(v), |
| 86 | + SqlType.TIMESTAMP: lambda v: parser.parse(v), |
| 87 | + SqlType.INTERVAL: lambda v: v, # Keep as string for now |
| 88 | + # String types - no conversion needed |
| 89 | + SqlType.CHAR: lambda v: v, |
| 90 | + SqlType.STRING: lambda v: v, |
| 91 | + # Binary type |
| 92 | + SqlType.BINARY: lambda v: bytes.fromhex(v), |
| 93 | + # Other types |
| 94 | + SqlType.NULL: lambda v: None, |
| 95 | + # Complex types and user-defined types return as-is |
| 96 | + SqlType.USER_DEFINED_TYPE: lambda v: v, |
| 97 | + } |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def convert_value( |
| 101 | + value: Any, |
| 102 | + sql_type: str, |
| 103 | + precision: Optional[int] = None, |
| 104 | + scale: Optional[int] = None, |
| 105 | + ) -> Any: |
| 106 | + """ |
| 107 | + Convert a string value to the appropriate Python type based on SQL type. |
| 108 | +
|
| 109 | + Args: |
| 110 | + value: The string value to convert |
| 111 | + sql_type: The SQL type (e.g., 'int', 'decimal') |
| 112 | + precision: Optional precision for decimal types |
| 113 | + scale: Optional scale for decimal types |
| 114 | +
|
| 115 | + Returns: |
| 116 | + The converted value in the appropriate Python type |
| 117 | + """ |
| 118 | + |
| 119 | + if value is None: |
| 120 | + return None |
| 121 | + |
| 122 | + sql_type = sql_type.lower().strip() |
| 123 | + |
| 124 | + if sql_type not in SqlTypeConverter.TYPE_MAPPING: |
| 125 | + return value |
| 126 | + |
| 127 | + converter_func = SqlTypeConverter.TYPE_MAPPING[sql_type] |
| 128 | + try: |
| 129 | + if sql_type == SqlType.DECIMAL: |
| 130 | + return converter_func(value, precision, scale) |
| 131 | + else: |
| 132 | + return converter_func(value) |
| 133 | + except (ValueError, TypeError, decimal.InvalidOperation) as e: |
| 134 | + logger.warning(f"Error converting value '{value}' to {sql_type}: {e}") |
| 135 | + return value |
0 commit comments