-
Notifications
You must be signed in to change notification settings - Fork 140
Complete Fetch Phase (for INLINE disposition and JSON_ARRAY format)
#594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 206 commits
138c2ae
3e3ab94
4a78165
0dac4aa
1b794c7
da5a6fe
686ade4
31e6c83
69ea238
66d7517
71feef9
ae9862f
d8aa69e
db139bc
b977b12
da615c0
0da04a6
ea9d456
8985c62
d9bcdbe
ee9fa1c
24c6152
67fd101
271fcaf
bf26ea3
ed7cf91
dae15e3
db5bbea
d5d3699
6137a3d
75b0773
4494dcd
4d0aeca
7cece5e
8977c06
0216d7a
d97463b
139e246
4cb15fd
e3ee4e4
f448a8f
82ca1ee
e96a078
27158b1
dee47f7
d3200c4
8a014f0
39c41ab
2cd04df
067a019
48c83e0
281a9e9
192901d
55f5c45
edc36b5
81280e7
c1d3be2
5ee4136
b881ab0
53bf715
45a32be
e3fe299
e8038d3
2f6ec19
73bc282
e385d5b
484064e
030edf8
4e07f1e
65e7c6b
30f8266
033ae73
33821f4
71b451a
170f339
40f79b5
c038d5a
3e22c6c
716304b
e96e5b8
787f1f7
165c4f3
a6e40d0
52e3088
641c09b
8bd12d8
ffded6e
227f6b3
68657a3
3940eec
37813ba
267c9f4
2967119
47fd60d
982fdf2
9e14d48
be1997e
e8e8ee7
05ee4e7
3ffa898
2952d8d
89e2aa0
cbace3f
c075b07
c62f76d
199402e
8ac574b
398ca70
b1acc5b
ef2a7ee
699942d
af8f74e
5540c5c
efe3881
36ab59b
1d57c99
df6dac2
ad0e527
ed446a0
38e4b5c
94879c0
1809956
da5260c
0385ffb
23963fc
dd43715
34a7f66
715cc13
a0705bc
1b90c4a
f7c11b9
349c021
6229848
fd52356
64e58b0
2903473
b300709
021ff4c
adecd53
bfc1f01
0a2cdfd
90bb09c
cd22389
82e0f8b
e64b81b
27564ca
5ab9bbe
1ab6e87
f469c24
68ec65f
ffd478e
f6d873d
28675f5
3578659
8713023
22dc252
390f592
28308fe
2712d1c
984e8ee
0ce144d
50cc1e2
242307a
35f1ef0
a515d26
59b1330
293e356
dd40beb
14057ac
a4d5bdb
156421a
eb1a9b4
e9b1314
8ede414
09a1b11
5e01e7b
21c389d
734321a
9f0f969
04a1936
278b8cd
91b7f7f
7a5ae13
f1776f3
6143331
8949d0c
5eaded4
eeed9a1
f233886
68ac437
7fd0845
ea7ff73
563da71
a018273
c0e98f4
7343035
ec500b6
0b3e91d
7664e44
db7b8e5
f75f2b5
e2d4ef5
21e3078
bb015e6
bb948a0
921a8c1
cc5203d
cc86832
3f1fd93
0bdf8f9
28b4d7b
245aa77
7d21ad1
4d10dcc
14c5625
31a0e52
e86e755
7035098
4566cb1
72a5cd3
511c449
c21ff5e
72100b9
aab33a1
9a6db30
0135d33
cc9db8b
bb135fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """ | ||
| Type conversion utilities for the Databricks SQL Connector. | ||
|
|
||
| This module provides functionality to convert string values from SEA Inline results | ||
| to appropriate Python types based on column metadata. | ||
| """ | ||
|
|
||
| import datetime | ||
| import decimal | ||
| import logging | ||
| from dateutil import parser | ||
| from typing import Any, Callable, Dict, Optional, Union | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class SqlType: | ||
| """ | ||
| SQL type constants | ||
|
|
||
| The list of types can be found in the SEA REST API Reference: | ||
| https://docs.databricks.com/api/workspace/statementexecution/executestatement | ||
| """ | ||
|
|
||
| # Numeric types | ||
| BYTE = "byte" | ||
| SHORT = "short" | ||
| INT = "int" | ||
| LONG = "long" | ||
| FLOAT = "float" | ||
| DOUBLE = "double" | ||
| DECIMAL = "decimal" | ||
|
|
||
| # Boolean type | ||
| BOOLEAN = "boolean" | ||
|
|
||
| # Date/Time types | ||
| DATE = "date" | ||
| TIMESTAMP = "timestamp" | ||
| INTERVAL = "interval" | ||
|
|
||
| # String types | ||
| CHAR = "char" | ||
| STRING = "string" | ||
|
|
||
| # Binary type | ||
| BINARY = "binary" | ||
|
|
||
| # Complex types | ||
| ARRAY = "array" | ||
| MAP = "map" | ||
| STRUCT = "struct" | ||
|
|
||
| # Other types | ||
| NULL = "null" | ||
| USER_DEFINED_TYPE = "user_defined_type" | ||
|
|
||
|
|
||
| class SqlTypeConverter: | ||
| """ | ||
| Utility class for converting SQL types to Python types. | ||
| Based on the types supported by the Databricks SDK. | ||
| """ | ||
|
|
||
| # SQL type to conversion function mapping | ||
| # TODO: complex types | ||
| TYPE_MAPPING: Dict[str, Callable] = { | ||
| # Numeric types | ||
| SqlType.BYTE: lambda v: int(v), | ||
| SqlType.SHORT: lambda v: int(v), | ||
| SqlType.INT: lambda v: int(v), | ||
|
Comment on lines
+65
to
+
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we mention the source of truth for this mapping in docstring/comment?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no real source of truth for the mapping to the Python types that I referred to. However, I linked the source of the various types we have to account for in the Note that I did not include any complex types in the |
||
| SqlType.LONG: lambda v: int(v), | ||
| SqlType.FLOAT: lambda v: float(v), | ||
| SqlType.DOUBLE: lambda v: float(v), | ||
| SqlType.DECIMAL: lambda v, p=None, s=None: ( | ||
| decimal.Decimal(v).quantize( | ||
| decimal.Decimal(f'0.{"0" * s}'), context=decimal.Context(prec=p) | ||
|
varun-edachali-dbx marked this conversation as resolved.
Outdated
|
||
| ) | ||
| if p is not None and s is not None | ||
| else decimal.Decimal(v) | ||
| ), | ||
| # Boolean type | ||
| SqlType.BOOLEAN: lambda v: v.lower() in ("true", "t", "1", "yes", "y"), | ||
| # Date/Time types | ||
| SqlType.DATE: lambda v: datetime.date.fromisoformat(v), | ||
| SqlType.TIMESTAMP: lambda v: parser.parse(v), | ||
| SqlType.INTERVAL: lambda v: v, # Keep as string for now | ||
| # String types - no conversion needed | ||
| SqlType.CHAR: lambda v: v, | ||
| SqlType.STRING: lambda v: v, | ||
| # Binary type | ||
| SqlType.BINARY: lambda v: bytes.fromhex(v), | ||
| # Other types | ||
| SqlType.NULL: lambda v: None, | ||
| # Complex types and user-defined types return as-is | ||
| SqlType.USER_DEFINED_TYPE: lambda v: v, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def convert_value( | ||
| value: Any, | ||
|
varun-edachali-dbx marked this conversation as resolved.
Outdated
|
||
| sql_type: str, | ||
| precision: Optional[int] = None, | ||
| scale: Optional[int] = None, | ||
|
varun-edachali-dbx marked this conversation as resolved.
Outdated
|
||
| ) -> Any: | ||
|
varun-edachali-dbx marked this conversation as resolved.
Outdated
|
||
| """ | ||
| Convert a string value to the appropriate Python type based on SQL type. | ||
|
|
||
| Args: | ||
| value: The string value to convert | ||
| sql_type: The SQL type (e.g., 'int', 'decimal') | ||
| precision: Optional precision for decimal types | ||
| scale: Optional scale for decimal types | ||
|
|
||
| Returns: | ||
| The converted value in the appropriate Python type | ||
| """ | ||
|
|
||
| if value is None: | ||
| return None | ||
|
|
||
| sql_type = sql_type.lower().strip() | ||
|
|
||
| if sql_type not in SqlTypeConverter.TYPE_MAPPING: | ||
| return value | ||
|
|
||
| converter_func = SqlTypeConverter.TYPE_MAPPING[sql_type] | ||
| try: | ||
| if sql_type == SqlType.DECIMAL: | ||
| return converter_func(value, precision, scale) | ||
| else: | ||
| return converter_func(value) | ||
| except (ValueError, TypeError, decimal.InvalidOperation) as e: | ||
| logger.warning(f"Error converting value '{value}' to {sql_type}: {e}") | ||
| return value | ||
Uh oh!
There was an error while loading. Please reload this page.