Skip to content

Types: Improve type mappings #24

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/inspection-reflection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ Create a SQLAlchemy table object:
Reflect column data types from the table metadata:

>>> table.columns.get('name')
Column('name', String(), table=<characters>)
Column('name', VARCHAR(), table=<characters>)

>>> table.primary_key
PrimaryKeyConstraint(Column('id', String(), table=<characters>, primary_key=True...
PrimaryKeyConstraint(Column('id', VARCHAR(), table=<characters>, primary_key=True...


CrateDialect
Expand Down
65 changes: 39 additions & 26 deletions src/sqlalchemy_cratedb/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,58 @@
from .sa_version import SA_1_4, SA_2_0, SA_VERSION
from .type import FloatVector, ObjectArray, ObjectType

# For SQLAlchemy >= 1.1.
TYPES_MAP = {
"boolean": sqltypes.Boolean,
"short": sqltypes.SmallInteger,
"smallint": sqltypes.SmallInteger,
"timestamp": sqltypes.TIMESTAMP(timezone=False),
"boolean": sqltypes.BOOLEAN,
"short": sqltypes.SMALLINT,
"smallint": sqltypes.SMALLINT,
"timestamp": sqltypes.TIMESTAMP,
"timestamp with time zone": sqltypes.TIMESTAMP(timezone=True),
"timestamp without time zone": sqltypes.TIMESTAMP(timezone=False),
"object": ObjectType,
"integer": sqltypes.Integer,
"long": sqltypes.NUMERIC,
"bigint": sqltypes.NUMERIC,
"object_array": ObjectArray, # TODO: Can this also be improved to use `sqltypes.ARRAY`?
"integer": sqltypes.INTEGER,
"long": sqltypes.BIGINT,
"bigint": sqltypes.BIGINT,
"float": sqltypes.FLOAT,
"double": sqltypes.DECIMAL,
"double precision": sqltypes.DECIMAL,
"object_array": ObjectArray,
"float": sqltypes.Float,
"real": sqltypes.Float,
"string": sqltypes.String,
"text": sqltypes.String,
"real": sqltypes.REAL,
"string": sqltypes.VARCHAR,
"text": sqltypes.VARCHAR,
"float_vector": FloatVector,
}

# Needed for SQLAlchemy >= 1.1.
# TODO: Dissolve.
# For SQLAlchemy >= 1.4.
try:
from sqlalchemy.types import ARRAY

TYPES_MAP["integer_array"] = ARRAY(sqltypes.Integer)
TYPES_MAP["boolean_array"] = ARRAY(sqltypes.Boolean)
TYPES_MAP["short_array"] = ARRAY(sqltypes.SmallInteger)
TYPES_MAP["smallint_array"] = ARRAY(sqltypes.SmallInteger)
TYPES_MAP["integer_array"] = ARRAY(sqltypes.INTEGER)
TYPES_MAP["boolean_array"] = ARRAY(sqltypes.BOOLEAN)
TYPES_MAP["short_array"] = ARRAY(sqltypes.SMALLINT)
TYPES_MAP["smallint_array"] = ARRAY(sqltypes.SMALLINT)
TYPES_MAP["timestamp_array"] = ARRAY(sqltypes.TIMESTAMP)
TYPES_MAP["timestamp_array"] = ARRAY(sqltypes.TIMESTAMP(timezone=False))
TYPES_MAP["timestamp with time zone_array"] = ARRAY(sqltypes.TIMESTAMP(timezone=True))
TYPES_MAP["long_array"] = ARRAY(sqltypes.NUMERIC)
TYPES_MAP["bigint_array"] = ARRAY(sqltypes.NUMERIC)
TYPES_MAP["double_array"] = ARRAY(sqltypes.DECIMAL)
TYPES_MAP["double precision_array"] = ARRAY(sqltypes.DECIMAL)
TYPES_MAP["float_array"] = ARRAY(sqltypes.Float)
TYPES_MAP["real_array"] = ARRAY(sqltypes.Float)
TYPES_MAP["string_array"] = ARRAY(sqltypes.String)
TYPES_MAP["text_array"] = ARRAY(sqltypes.String)
TYPES_MAP["long_array"] = ARRAY(sqltypes.BIGINT)
TYPES_MAP["bigint_array"] = ARRAY(sqltypes.BIGINT)
TYPES_MAP["float_array"] = ARRAY(sqltypes.FLOAT)
TYPES_MAP["real_array"] = ARRAY(sqltypes.REAL)
TYPES_MAP["string_array"] = ARRAY(sqltypes.VARCHAR)
TYPES_MAP["text_array"] = ARRAY(sqltypes.VARCHAR)
except Exception: # noqa: S110
pass

Check warning on line 78 in src/sqlalchemy_cratedb/dialect.py

View check run for this annotation

Codecov / codecov/patch

src/sqlalchemy_cratedb/dialect.py#L77-L78

Added lines #L77 - L78 were not covered by tests

# For SQLAlchemy >= 2.0.
try:
from sqlalchemy.types import DOUBLE, DOUBLE_PRECISION

TYPES_MAP["real"] = DOUBLE
TYPES_MAP["real_array"] = ARRAY(DOUBLE)
TYPES_MAP["double"] = DOUBLE
TYPES_MAP["double_array"] = ARRAY(DOUBLE)
TYPES_MAP["double precision"] = DOUBLE_PRECISION
TYPES_MAP["double precision_array"] = ARRAY(DOUBLE_PRECISION)
except Exception: # noqa: S110
pass

Expand Down
14 changes: 10 additions & 4 deletions tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from crate.client import connect

from sqlalchemy_cratedb.sa_version import SA_2_0, SA_VERSION
from sqlalchemy_cratedb.sa_version import SA_1_4, SA_2_0, SA_VERSION
from tests.settings import crate_host

log = logging.getLogger()
Expand Down Expand Up @@ -179,16 +179,22 @@ def create_test_suite():
"docs/crud.rst",
"docs/working-with-types.rst",
"docs/advanced-querying.rst",
"docs/inspection-reflection.rst",
]

# Don't run DataFrame integration tests on SQLAlchemy 1.3 and Python 3.7.
skip_dataframe = SA_VERSION < SA_2_0 or sys.version_info < (3, 8) or sys.version_info >= (3, 13)
# Don't run DataFrame integration tests on SQLAlchemy 1.4 and earlier, or Python 3.7.
skip_dataframe = SA_VERSION < SA_2_0 or sys.version_info < (3, 8)
if not skip_dataframe:
sqlalchemy_integration_tests += [
"docs/dataframe.rst",
]

# Don't run reflection integration tests on SQLAlchemy 1.3 and earlier and Python 3.10 and 3.11.
skip_reflection = SA_VERSION < SA_1_4 and (3, 10) <= sys.version_info < (3, 12)
if not skip_reflection:
sqlalchemy_integration_tests += [
"docs/inspection-reflection.rst",
]

s = doctest.DocFileSuite(
*sqlalchemy_integration_tests,
module_relative=False,
Expand Down