Skip to content

Fix Date literal processor for YDB #82

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

Merged
merged 1 commit into from
Jul 4, 2025
Merged
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
1 change: 1 addition & 0 deletions ydb_sqlalchemy/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class YqlDialect(StrCompileDialect):
colspecs = {
sa.types.JSON: types.YqlJSON,
sa.types.JSON.JSONPathType: types.YqlJSON.YqlJSONPathType,
sa.types.Date: types.YqlDate,
sa.types.DateTime: types.YqlTimestamp, # Because YDB's DateTime doesn't store microseconds
sa.types.DATETIME: types.YqlDateTime,
sa.types.TIMESTAMP: types.YqlTimestamp,
Expand Down
10 changes: 10 additions & 0 deletions ydb_sqlalchemy/sqlalchemy/datetime_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
from sqlalchemy import types as sqltypes


class YqlDate(sqltypes.Date):
def literal_processor(self, dialect):
parent = super().literal_processor(dialect)

def process(value):
return f"Date({parent(value)})"

return process


class YqlTimestamp(sqltypes.TIMESTAMP):
def result_processor(self, dialect, coltype):
def process(value: Optional[datetime.datetime]) -> Optional[datetime.datetime]:
Expand Down
10 changes: 10 additions & 0 deletions ydb_sqlalchemy/sqlalchemy/test_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import date
import sqlalchemy as sa

from . import YqlDialect, types
Expand Down Expand Up @@ -25,3 +26,12 @@ def test_casts():
"CAST(1/2 AS UInt8)",
"String::JoinFromList(ListMap(TOPFREQ(1/2, 5), ($x) -> { RETURN CAST($x AS UTF8) ;}), ', ')",
]


def test_ydb_types():
dialect = YqlDialect()

query = sa.literal(date(1996, 11, 19))
compiled = query.compile(dialect=dialect, compile_kwargs={"literal_binds": True})

assert str(compiled) == "Date('1996-11-19')"
2 changes: 1 addition & 1 deletion ydb_sqlalchemy/sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sqlalchemy import ARRAY, exc, types
from sqlalchemy.sql import type_api

from .datetime_types import YqlDateTime, YqlTimestamp # noqa: F401
from .datetime_types import YqlDate, YqlDateTime, YqlTimestamp # noqa: F401
from .json import YqlJSON # noqa: F401


Expand Down