Skip to content

Commit 0c5d1ba

Browse files
committed
Switch to new SQLAlchemy dialect for CrateDB
This includes the `FloatVector` SQLAlchemy type.
1 parent 8207049 commit 0c5d1ba

File tree

6 files changed

+22
-157
lines changed

6 files changed

+22
-157
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## In progress
44
- Add support for container types `ARRAY`, `OBJECT`, and `FLOAT_VECTOR`.
55
- Improve write operations to be closer to `target-postgres`.
6+
- Switch to new SQLAlchemy dialect for CrateDB.
67

78
## 2023-12-08 v0.0.1
89
- Make it work. It can run the canonical Meltano GitHub -> DB example.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ dynamic = [
8989
"version",
9090
]
9191
dependencies = [
92-
"crate[sqlalchemy]<1",
9392
"cratedb-toolkit",
9493
"importlib-resources; python_version<'3.9'", # "meltanolabs-target-postgres==0.0.9",
9594
"meltanolabs-target-postgres @ git+https://github.com/singer-contrib/meltanolabs-target-postgres.git@pgvector",
95+
"sqlalchemy-cratedb[vector]",
9696
]
9797
optional-dependencies.all = [
9898
"meltano-target-cratedb[vector]",

target_cratedb/connector.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from datetime import datetime
77

88
import sqlalchemy as sa
9-
from crate.client.sqlalchemy.types import ObjectType, ObjectTypeImpl, _ObjectArray
109
from singer_sdk import typing as th
1110
from singer_sdk.helpers._typing import is_array_type, is_boolean_type, is_integer_type, is_number_type, is_object_type
11+
from sqlalchemy_cratedb.type import FloatVector, ObjectType
12+
from sqlalchemy_cratedb.type.array import _ObjectArray
13+
from sqlalchemy_cratedb.type.object import ObjectTypeImpl
1214
from target_postgres.connector import NOTYPE, PostgresConnector
1315

1416
from target_cratedb.sqlalchemy.patch import polyfill_refresh_after_dml_engine
15-
from target_cratedb.sqlalchemy.vector import FloatVector
1617

1718

1819
class CrateDBConnector(PostgresConnector):
@@ -225,6 +226,9 @@ def _get_type_sort_key(
225226
if isinstance(sql_type, NOTYPE):
226227
return 0, _len
227228

229+
if not hasattr(sql_type, "python_type"):
230+
raise TypeError(f"Resolving type for sort key failed: {sql_type}")
231+
228232
_pytype = t.cast(type, sql_type.python_type)
229233
if issubclass(_pytype, (str, bytes)):
230234
return 900, _len

target_cratedb/sqlalchemy/patch.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
import sqlalchemy as sa
55
from crate.client.http import CrateJsonEncoder
6-
from crate.client.sqlalchemy.dialect import ARRAY, TYPES_MAP, DateTime
7-
from crate.client.sqlalchemy.types import _ObjectArray
8-
from sqlalchemy.sql import sqltypes
6+
from sqlalchemy_cratedb.dialect import TYPES_MAP, DateTime
7+
from sqlalchemy_cratedb.type.array import _ObjectArray
98

109

1110
def patch_sqlalchemy():
@@ -19,20 +18,21 @@ def patch_types():
1918
2019
TODO: Upstream to crate-python.
2120
"""
22-
TYPES_MAP["bigint"] = sqltypes.BIGINT
23-
TYPES_MAP["bigint_array"] = ARRAY(sqltypes.BIGINT)
24-
TYPES_MAP["long"] = sqltypes.BIGINT
25-
TYPES_MAP["long_array"] = ARRAY(sqltypes.BIGINT)
26-
TYPES_MAP["real"] = sqltypes.DOUBLE
27-
TYPES_MAP["real_array"] = ARRAY(sqltypes.DOUBLE)
28-
TYPES_MAP["timestamp without time zone"] = sqltypes.TIMESTAMP
29-
TYPES_MAP["timestamp with time zone"] = sqltypes.TIMESTAMP
21+
# abc()
22+
TYPES_MAP["bigint"] = sa.BIGINT
23+
TYPES_MAP["bigint_array"] = sa.ARRAY(sa.BIGINT)
24+
TYPES_MAP["long"] = sa.BIGINT
25+
TYPES_MAP["long_array"] = sa.ARRAY(sa.BIGINT)
26+
TYPES_MAP["real"] = sa.DOUBLE
27+
TYPES_MAP["real_array"] = sa.ARRAY(sa.DOUBLE)
28+
TYPES_MAP["timestamp without time zone"] = sa.TIMESTAMP
29+
TYPES_MAP["timestamp with time zone"] = sa.TIMESTAMP
3030

3131
# TODO: Can `ARRAY` be inherited from PostgreSQL's
3232
# `ARRAY`, to make type checking work?
3333

3434
def as_generic(self):
35-
return sqltypes.ARRAY
35+
return sa.ARRAY
3636

3737
_ObjectArray.as_generic = as_generic
3838

target_cratedb/sqlalchemy/vector.py

Lines changed: 0 additions & 140 deletions
This file was deleted.

target_cratedb/tests/test_standard_target.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
import jsonschema
99
import pytest
1010
import sqlalchemy as sa
11-
from crate.client.sqlalchemy.types import ObjectTypeImpl
1211
from singer_sdk.exceptions import MissingKeyPropertiesError
1312
from singer_sdk.testing import sync_end_to_end
13+
from sqlalchemy_cratedb.type import FloatVector
14+
from sqlalchemy_cratedb.type.object import ObjectTypeImpl
1415
from target_postgres.tests.samples.aapl.aapl import Fundamentals
1516
from target_postgres.tests.samples.sample_tap_countries.countries_tap import (
1617
SampleTapCountries,
@@ -20,7 +21,6 @@
2021
from target_cratedb.connector import CrateDBConnector
2122
from target_cratedb.sinks import MELTANO_CRATEDB_STRATEGY_DIRECT
2223
from target_cratedb.sqlalchemy.patch import polyfill_refresh_after_dml_engine
23-
from target_cratedb.sqlalchemy.vector import FloatVector
2424
from target_cratedb.target import TargetCrateDB
2525

2626
try:

0 commit comments

Comments
 (0)