-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aleksandr Movchan
committed
Dec 9, 2024
1 parent
d9bc745
commit 53f85b2
Showing
8 changed files
with
374 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import orjson | ||
from snowflake.sqlalchemy.custom_types import VARIANT as SnowflakeVariantType | ||
from sqlalchemy import func | ||
from sqlalchemy.types import JSON as SqlAlchemyJSON | ||
from sqlalchemy.types import TypeDecorator | ||
|
||
|
||
class VARIANT(SnowflakeVariantType): | ||
"""Extends VARIANT type for better SqlAlchemy support.""" | ||
|
||
def bind_expression(self, bindvalue): | ||
"""Wraps value with PARSE_JSON for Snowflake.""" | ||
return func.PARSE_JSON(bindvalue) | ||
|
||
def result_processor(self, dialect, coltype): | ||
"""Convert JSON string to Python dictionary when retrieving.""" | ||
|
||
def process(value): | ||
if value is None: | ||
return None | ||
try: | ||
return orjson.loads(value) | ||
except (ValueError, TypeError): | ||
return value # Return raw value if not valid JSON | ||
|
||
return process | ||
|
||
|
||
JSON = VARIANT | ||
|
||
# class JSON(TypeDecorator): | ||
# """Custom JSON type that supports Snowflake-specific and standard dialects.""" | ||
|
||
# impl = SqlAlchemyJSON # Default to standard SQLAlchemy JSON | ||
|
||
# def load_dialect_impl(self, dialect): | ||
# """Load dialect-specific implementation.""" | ||
# if dialect.name == "snowflake": | ||
# return SnowflakeVariantType() | ||
# return self.impl | ||
|
||
# def bind_expression(self, bindvalue): | ||
# """Handle binding expressions dynamically.""" | ||
# if hasattr( | ||
# bindvalue.type, "bind_expression" | ||
# ): # Check if impl has bind_expression | ||
# return bindvalue.type.bind_expression(bindvalue) | ||
# return bindvalue # Default binding behavior | ||
|
||
# def process_result_value(self, value, dialect): | ||
# """Process the result based on dialect.""" | ||
# if dialect.name == "snowflake": | ||
# if value is None: | ||
# return None | ||
# try: | ||
# return orjson.loads(value) | ||
# except (ValueError, TypeError): | ||
# return value # Return raw value if not valid JSON | ||
# # For other dialects, call the default implementation | ||
# return self.impl.process_result_value(value, dialect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.