Skip to content
Closed
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
21 changes: 21 additions & 0 deletions tests/unit/sqlalchemy/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime

import pytest
from sqlalchemy import Column
from sqlalchemy import ForeignKey
Expand All @@ -25,6 +27,7 @@
from sqlalchemy.sql import table

from tests.unit.conftest import sqlalchemy_version
from trino.sqlalchemy.datatype import TIMESTAMP
from trino.sqlalchemy.dialect import TrinoDialect

metadata = MetaData()
Expand Down Expand Up @@ -206,6 +209,24 @@ def test_try_cast(dialect):
assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"'


def test_timestamp_literal_processor(dialect):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more tests:

  • tz aware values
  • higher precision values
  • non-datetime values

ts_col = column("ts", TIMESTAMP())
tbl = table("t", ts_col)
dt = datetime.datetime(2026, 6, 17, 9, 57, 43, 244000)
stmt = select(tbl).where(ts_col == dt)
query = stmt.compile(dialect=dialect, compile_kwargs={"literal_binds": True})
assert "TIMESTAMP '2026-06-17 09:57:43.244'" in str(query)


def test_timestamp_literal_processor_no_microseconds(dialect):
ts_col = column("ts", TIMESTAMP())
tbl = table("t", ts_col)
dt = datetime.datetime(2026, 6, 17, 9, 57, 43)
stmt = select(tbl).where(ts_col == dt)
query = stmt.compile(dialect=dialect, compile_kwargs={"literal_binds": True})
assert "TIMESTAMP '2026-06-17 09:57:43'" in str(query)


def test_catalogs_create_table_with_pk(dialect):
with pytest.warns(SAWarning, match="Trino does not support PRIMARY KEY constraints. Constraint will be ignored."):
statement = CreateTable(table_with_pk)
Expand Down
12 changes: 12 additions & 0 deletions trino/sqlalchemy/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import re
from collections.abc import Iterator
from typing import Any
Expand Down Expand Up @@ -81,6 +82,17 @@ def __init__(self, precision=None, timezone=False):
super(TIMESTAMP, self).__init__(timezone=timezone)
self.precision = precision

def literal_processor(self, dialect):
def process(value):
if isinstance(value, datetime.datetime):
ts = value.strftime("%Y-%m-%d %H:%M:%S")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you intentionally drop the zone? A zone-aware value would now be treated with session zone in Trino side. Maybe see Cursor._format_prepared_param for some ideas.

if value.microsecond:
ts += f".{value.microsecond // 1000:03d}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

truncates precision. equality predicates may no longer match.

Also TIMESTAMP(precision=6) would ignore the specified precision.

return f"TIMESTAMP '{ts}'"
return str(value)

@hashhar hashhar Jul 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sql injection I think with something like literal("x'; DROP TABLE t; --", TIMESTAMP())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also correctness issues in case of syntax errors: WHERE ts = 2026-06-17 would parse as integer arithmetic instead of correctly


return process


class JSON(TypeDecorator):
impl = JSON
Expand Down
Loading