Skip to content

Commit 51ee8a3

Browse files
committed
Register date_trunc function for crate dialect with correct return type
1 parent ad85a2b commit 51ee8a3

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Made it so that the SQLAlchemy dialect is now aware of the return type of the
9+
``date_trunc`` function.
10+
811
2019/09/19 0.23.2
912
=================
1013

src/crate/client/sqlalchemy/dialect.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from sqlalchemy import types as sqltypes
2626
from sqlalchemy.engine import default, reflection
27+
from sqlalchemy.sql import functions
2728

2829
from .compiler import (
2930
CrateCompiler,
@@ -306,3 +307,8 @@ def _create_column_info(self, row):
306307

307308
def _resolve_type(self, type_):
308309
return TYPES_MAP.get(type_, sqltypes.UserDefinedType)
310+
311+
312+
class DateTrunc(functions.GenericFunction):
313+
name = "date_trunc"
314+
type = sqltypes.TIMESTAMP

src/crate/client/sqlalchemy/tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .create_table_test import CreateTableTest
1313
from .array_test import SqlAlchemyArrayTypeTest
1414
from .dialect_test import DialectTest
15+
from .function_test import FunctionTest
1516
from ..sa_version import SA_1_1, SA_VERSION
1617

1718

@@ -28,6 +29,7 @@ def test_suite():
2829
tests.addTest(makeSuite(SqlAlchemyInsertFromSelectTest))
2930
tests.addTest(makeSuite(SqlAlchemyInsertFromSelectTest))
3031
tests.addTest(makeSuite(DialectTest))
32+
tests.addTest(makeSuite(FunctionTest))
3133
if SA_VERSION >= SA_1_1:
3234
tests.addTest(makeSuite(SqlAlchemyArrayTypeTest))
3335
return tests
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8; -*-
2+
#
3+
# Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
4+
# license agreements. See the NOTICE file distributed with this work for
5+
# additional information regarding copyright ownership. Crate licenses
6+
# this file to you under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License. You may
8+
# obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15+
# License for the specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
# However, if you have executed another commercial license agreement
19+
# with Crate these terms will supersede the license and you may use the
20+
# software solely pursuant to the terms of the relevant commercial agreement.
21+
22+
from unittest import TestCase
23+
24+
import sqlalchemy as sa
25+
from sqlalchemy.sql.sqltypes import TIMESTAMP
26+
from sqlalchemy.ext.declarative import declarative_base
27+
28+
29+
class FunctionTest(TestCase):
30+
def setUp(self):
31+
Base = declarative_base(bind=sa.create_engine("crate://"))
32+
33+
class Character(Base):
34+
__tablename__ = "characters"
35+
name = sa.Column(sa.String, primary_key=True)
36+
timestamp = sa.Column(sa.DateTime)
37+
38+
self.Character = Character
39+
40+
def test_date_trunc_type_is_timestamp(self):
41+
f = sa.func.date_trunc("minute", self.Character.timestamp)
42+
self.assertEqual(len(f.base_columns), 1)
43+
for col in f.base_columns:
44+
self.assertIsInstance(col.type, TIMESTAMP)

0 commit comments

Comments
 (0)