|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import pytest |
| 3 | +from ydb.tests.library.compatibility.fixtures import MixedClusterFixture |
| 4 | +from ydb.tests.oss.ydb_sdk_import import ydb |
| 5 | + |
| 6 | +from datetime import datetime, timedelta |
| 7 | + |
| 8 | + |
| 9 | +class TestDatetime2Format(MixedClusterFixture): |
| 10 | + @pytest.fixture(autouse=True, scope="function") |
| 11 | + def setup(self): |
| 12 | + yield from self.setup_cluster() |
| 13 | + |
| 14 | + def test_simple(self): |
| 15 | + |
| 16 | + with ydb.QuerySessionPool(self.driver) as session_pool: |
| 17 | + rows = 100 |
| 18 | + table_name = 'dates_table' |
| 19 | + |
| 20 | + # ---------------- CREATE TABLE ------------------ |
| 21 | + query = f""" |
| 22 | + CREATE TABLE {table_name} ( |
| 23 | + id Uint32, |
| 24 | + event_date DateTime, |
| 25 | + PRIMARY KEY (id) |
| 26 | + ) WITH ( |
| 27 | + PARTITION_AT_KEYS = ({", ".join(str(i) for i in range(1, rows))}) |
| 28 | + ); |
| 29 | + """ |
| 30 | + session_pool.execute_with_retries(query) |
| 31 | + |
| 32 | + # ---------------- INSERT ------------------ |
| 33 | + start_date = datetime(2023, 1, 1) |
| 34 | + values = [] |
| 35 | + |
| 36 | + for i in range(1, rows): |
| 37 | + date = (start_date + timedelta(days=i - 1)).strftime("%Y-%m-%dT%H:%M:%SZ") |
| 38 | + values.append(f"({i}, CAST(\"{date}\" AS DateTime))") |
| 39 | + |
| 40 | + query = f""" |
| 41 | + UPSERT INTO {table_name} (id, event_date) VALUES {",\n ".join(values)}; |
| 42 | + """ |
| 43 | + |
| 44 | + session_pool.execute_with_retries(query) |
| 45 | + |
| 46 | + # ---------------- SELECT ------------------ |
| 47 | + query = f""" |
| 48 | + SELECT |
| 49 | + DateTime::Format('%Y-%m-%d')(event_date) as date |
| 50 | + FROM {table_name} order by date; |
| 51 | + """ |
| 52 | + result_sets = session_pool.execute_with_retries(query) |
| 53 | + assert result_sets[0].rows[0]['date'] == b'2023-01-01' |
0 commit comments