Snowflake SQLAlchemy Dialect providing comprehensive database connectivity and ORM support for Snowflake cloud data warehouse
—
Comprehensive type system supporting both standard SQL types and Snowflake's semi-structured data types with full SQLAlchemy integration.
Re-exported SQLAlchemy types for compatibility with standard SQL databases.
from snowflake.sqlalchemy import (
BIGINT, BINARY, BOOLEAN, CHAR, DATE, DATETIME, DECIMAL,
FLOAT, INT, INTEGER, REAL, SMALLINT, TIME, TIMESTAMP, VARCHAR
)
# Type class definitions
class BIGINT(sqltypes.BIGINT): ...
class BINARY(sqltypes.BINARY): ...
class BOOLEAN(sqltypes.BOOLEAN): ...
class CHAR(sqltypes.CHAR): ...
class DATE(sqltypes.DATE): ...
class DATETIME(sqltypes.DATETIME): ...
class DECIMAL(sqltypes.DECIMAL): ...
class FLOAT(sqltypes.FLOAT): ...
class INT(sqltypes.INT): ...
class INTEGER(sqltypes.INTEGER): ...
class REAL(sqltypes.REAL): ...
class SMALLINT(sqltypes.SMALLINT): ...
class TIME(sqltypes.TIME): ...
class TIMESTAMP(sqltypes.TIMESTAMP): ...
class VARCHAR(sqltypes.VARCHAR): ...Convenient aliases for common Snowflake type names.
from snowflake.sqlalchemy import (
TEXT, CHARACTER, DEC, DOUBLE, FIXED, NUMBER,
BYTEINT, STRING, TINYINT, VARBINARY
)
# Type aliases
TEXT = VARCHAR
CHARACTER = CHAR
DEC = DECIMAL
DOUBLE = FLOAT
FIXED = DECIMAL
NUMBER = DECIMAL
BYTEINT = SMALLINT
STRING = VARCHAR
TINYINT = SMALLINT
VARBINARY = BINARYSnowflake's native semi-structured data types for handling JSON, arrays, objects, and variant data.
from snowflake.sqlalchemy import VARIANT, ARRAY, OBJECT, MAP
class VARIANT(SnowflakeType):
"""Semi-structured VARIANT type for JSON-like data."""
__visit_name__ = "VARIANT"
class ARRAY(StructuredType):
"""Typed array for homogeneous collections."""
__visit_name__ = "SNOWFLAKE_ARRAY"
def __init__(self, value_type=None, not_null=False):
"""
Create typed array.
Args:
value_type: Type of array elements
not_null: Whether array elements can be null
"""
class OBJECT(StructuredType):
"""Structured object type with named fields."""
__visit_name__ = "OBJECT"
def __init__(self, **items_types):
"""
Create structured object with typed fields.
Args:
**items_types: Field names and their types
"""
class MAP(StructuredType):
"""Typed map with key-value pairs."""
__visit_name__ = "MAP"
def __init__(self, key_type, value_type, not_null=False):
"""
Create typed map.
Args:
key_type: Type of map keys
value_type: Type of map values
not_null: Whether values can be null
"""Snowflake's specialized timestamp types with timezone handling.
from snowflake.sqlalchemy import (
TIMESTAMP_TZ, TIMESTAMP_LTZ, TIMESTAMP_NTZ
)
class TIMESTAMP_TZ(SnowflakeType):
"""Timestamp with timezone."""
__visit_name__ = "TIMESTAMP_TZ"
class TIMESTAMP_LTZ(SnowflakeType):
"""Timestamp with local timezone."""
__visit_name__ = "TIMESTAMP_LTZ"
class TIMESTAMP_NTZ(SnowflakeType):
"""Timestamp without timezone."""
__visit_name__ = "TIMESTAMP_NTZ"Snowflake's geospatial data types for geographic and geometric data.
from snowflake.sqlalchemy import GEOGRAPHY, GEOMETRY
class GEOGRAPHY(SnowflakeType):
"""Geographic data type for Earth-based coordinates."""
__visit_name__ = "GEOGRAPHY"
class GEOMETRY(SnowflakeType):
"""Geometric data type for arbitrary coordinate systems."""
__visit_name__ = "GEOMETRY"Foundation classes for Snowflake type system.
class SnowflakeType(sqltypes.TypeEngine):
"""Base class for all Snowflake-specific types."""
def _default_dialect(self):
"""Get the default Snowflake dialect."""
class StructuredType(SnowflakeType):
"""Base class for semi-structured types."""
def __init__(self, is_semi_structured: bool = False):
"""
Initialize structured type.
Args:
is_semi_structured: Whether type is semi-structured
"""from sqlalchemy import Column, MetaData, Table
from snowflake.sqlalchemy import VARIANT, ARRAY, TEXT, INTEGER
metadata = MetaData()
products = Table(
'products',
metadata,
Column('id', INTEGER, primary_key=True),
Column('name', TEXT()),
Column('metadata', VARIANT),
Column('tags', ARRAY(TEXT()))
)from snowflake.sqlalchemy import OBJECT, MAP, ARRAY
# Object with typed fields
address_type = OBJECT(
street=TEXT(),
city=TEXT(),
zipcode=INTEGER
)
# Map with typed key-value pairs
attributes_type = MAP(TEXT(), VARIANT())
# Array of objects
addresses_type = ARRAY(address_type)from snowflake.sqlalchemy import TIMESTAMP_TZ, TIMESTAMP_LTZ, TIMESTAMP_NTZ
events = Table(
'events',
metadata,
Column('id', INTEGER, primary_key=True),
Column('created_at_utc', TIMESTAMP_NTZ),
Column('created_at_user_tz', TIMESTAMP_TZ),
Column('created_at_local', TIMESTAMP_LTZ)
)from snowflake.sqlalchemy import GEOGRAPHY, GEOMETRY
locations = Table(
'locations',
metadata,
Column('id', INTEGER, primary_key=True),
Column('coordinates', GEOGRAPHY),
Column('boundary', GEOMETRY)
)Install with Tessl CLI
npx tessl i tessl/pypi-snowflake-sqlalchemy