Skip to content

Commit f25006c

Browse files
committed
Fix Date literal processor for YDB
1 parent 8522300 commit f25006c

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

ydb_sqlalchemy/sqlalchemy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class YqlDialect(StrCompileDialect):
136136
colspecs = {
137137
sa.types.JSON: types.YqlJSON,
138138
sa.types.JSON.JSONPathType: types.YqlJSON.YqlJSONPathType,
139+
sa.types.Date: types.YqlDate,
139140
sa.types.DateTime: types.YqlTimestamp, # Because YDB's DateTime doesn't store microseconds
140141
sa.types.DATETIME: types.YqlDateTime,
141142
sa.types.TIMESTAMP: types.YqlTimestamp,

ydb_sqlalchemy/sqlalchemy/datetime_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
from sqlalchemy import types as sqltypes
55

66

7+
class YqlDate(sqltypes.Date):
8+
def literal_processor(self, dialect):
9+
parent = super().literal_processor(dialect)
10+
11+
def process(value):
12+
return f"Date({parent(value)})"
13+
14+
return process
15+
16+
717
class YqlTimestamp(sqltypes.TIMESTAMP):
818
def result_processor(self, dialect, coltype):
919
def process(value: Optional[datetime.datetime]) -> Optional[datetime.datetime]:

ydb_sqlalchemy/sqlalchemy/test_sqlalchemy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import date
12
import sqlalchemy as sa
23

34
from . import YqlDialect, types
@@ -25,3 +26,12 @@ def test_casts():
2526
"CAST(1/2 AS UInt8)",
2627
"String::JoinFromList(ListMap(TOPFREQ(1/2, 5), ($x) -> { RETURN CAST($x AS UTF8) ;}), ', ')",
2728
]
29+
30+
31+
def test_ydb_types():
32+
dialect = YqlDialect()
33+
34+
query = sa.literal(date(1996, 11, 19))
35+
compiled = query.compile(dialect=dialect, compile_kwargs={"literal_binds": True})
36+
37+
assert str(compiled) == "Date('1996-11-19')"

ydb_sqlalchemy/sqlalchemy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sqlalchemy import ARRAY, exc, types
1111
from sqlalchemy.sql import type_api
1212

13-
from .datetime_types import YqlDateTime, YqlTimestamp # noqa: F401
13+
from .datetime_types import YqlDate, YqlDateTime, YqlTimestamp # noqa: F401
1414
from .json import YqlJSON # noqa: F401
1515

1616

0 commit comments

Comments
 (0)