Skip to content

Commit 6d9d413

Browse files
committed
Add description of the DATETIME and INTERVAL types
Part of #2575 and #2836
1 parent 6b21abb commit 6d9d413

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

doc/reference/reference_sql/sql_user_guide.rst

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,16 @@ and minimum / maximum literal examples.
504504
- (none)
505505
- 00000000-0000-0000- |br| 0000-000000000000
506506
- ffffffff-ffff-ffff- |br| dfff-ffffffffffff
507+
* - DATETIME
508+
- :ref:`datetime <index-box_datetime>`
509+
- (none)
510+
-
511+
-
512+
* - INTERVAL
513+
- :ref:`interval <index-box_interval>`
514+
- (none)
515+
-
516+
-
507517
* - SCALAR
508518
- (varies)
509519
- (none)
@@ -602,6 +612,155 @@ or with the :ref:`UUID() function <sql_function_uuid>`,
602612
or with the :ref:`CAST() function <sql_function_cast>`.
603613
UUID support in SQL was added in Tarantool version 2.9.1.
604614

615+
.. _sql_data_type_datetime:
616+
617+
DATETIME. Introduced in :tarantool-release:`2.10.0`.
618+
A datetime table field can be created by using this type, which is semantically equivalent to the standard TIMESTAMP WITH TIME ZONE type.
619+
620+
.. code-block:: tarantoolsession
621+
622+
tarantool> create table T2(d datetime primary key);
623+
---
624+
- row_count: 1
625+
...
626+
627+
tarantool> insert into t2 values ('2022-01-01');
628+
---
629+
- null
630+
- 'Type mismatch: can not convert string(''2022-01-01'') to datetime'
631+
...
632+
633+
tarantool> insert into t2 values (cast('2022-01-01' as datetime));
634+
---
635+
- row_count: 1
636+
...
637+
638+
tarantool> select * from t2;
639+
---
640+
- metadata:
641+
- name: D
642+
type: datetime
643+
rows:
644+
- ['2022-01-01T00:00:00Z']
645+
...
646+
647+
There is no implicit cast available from a string expression to a datetime expression (dislike convention used by majority of SQL vendors).
648+
In such cases, you need to use explicit cast from a string value to a datetime value (see the example above).
649+
650+
You can subtract datetime and datetime, datetime and interval, or add datetime and interval in any order (see examples of such arithmetics in the description of the :ref:`INTERVAL type <sql_data_type_interval>`).
651+
652+
The built-in functions related to the DATETIME type are :ref:`DATE_PART() <sql_function_datepart>` and :ref:`NOW() <sql_function_now>`
653+
654+
.. _sql_data_type_interval:
655+
656+
INTERVAL. Introduced in :tarantool-release:`2.10.0`.
657+
Similarly to the :ref:`DATETIME <sql_data_type_datetime>` type, you can define a column of the INTERVAL type.
658+
659+
.. code-block:: tarantoolsession
660+
661+
tarantool> create table T(d datetime primary key, i interval);
662+
---
663+
- row_count: 1
664+
...
665+
666+
tarantool> insert into T values (cast('2022-02-02T01:01' as datetime), cast({'year': 1, 'month': 1} as interval));
667+
---
668+
- row_count: 1
669+
...
670+
671+
tarantool> select * from t;
672+
---
673+
- metadata:
674+
- name: D
675+
type: datetime
676+
- name: I
677+
type: interval
678+
rows:
679+
- ['2022-02-02T01:01:00Z', '+1 years, 1 months']
680+
...
681+
682+
Dislike DATETIME, INTERVAL cannot be a part of an index.
683+
684+
There is no implicit cast available for conversions to an interval from a string or any other type.
685+
But there is explicit cast allowed from maps (see examples below).
686+
687+
Intervals can be used in arithmetic operations like ``+`` or ``-`` only with the datetime expression or another interval:
688+
689+
.. code-block:: tarantoolsession
690+
691+
tarantool> select * from t
692+
---
693+
- metadata:
694+
- name: D
695+
type: datetime
696+
- name: I
697+
type: interval
698+
rows:
699+
- ['2022-02-02T01:01:00Z', '+1 years, 1 months']
700+
...
701+
702+
tarantool> select d, d + i, d + cast({'year': 1, 'month': 2} as interval) from t
703+
---
704+
- metadata:
705+
- name: D
706+
type: datetime
707+
- name: COLUMN_1
708+
type: datetime
709+
- name: COLUMN_2
710+
type: datetime
711+
rows:
712+
- ['2022-02-02T01:01:00Z', '2023-03-02T01:01:00Z', '2023-04-02T01:01:00Z']
713+
...
714+
715+
tarantool> select i + cast({'year': 1, 'month': 2} as interval) from t
716+
---
717+
- metadata:
718+
- name: COLUMN_1
719+
type: interval
720+
rows:
721+
- ['+2 years, 3 months']
722+
...
723+
724+
There is the predefined list of known attributes for the map if you want to convert one to the INTERVAL expression:
725+
726+
* ``year``
727+
* ``month``
728+
* ``week``
729+
* ``day``
730+
* ``hour``
731+
* ``minute``
732+
* ``second``
733+
* ``nsec``
734+
735+
.. code-block:: tarantoolsession
736+
737+
tarantool> select cast({'year': 1, 'month': 1, 'week': 1, 'day': 1, 'hour': 1, 'min': 1, 'sec': 1} as interval)
738+
---
739+
- metadata:
740+
- name: COLUMN_1
741+
type: interval
742+
rows:
743+
- ['+1 years, 1 months, 1 weeks, 1 days, 1 hours, 1 minutes, 1 seconds']
744+
...
745+
746+
tarantool> \set language lua
747+
748+
749+
tarantool> v = {year = 1, month = 1, week = 1, day = 1, hour = 1,
750+
> min = 1, sec = 1, nsec = 1, adjust = 'none'}
751+
---
752+
...
753+
754+
tarantool> box.execute('select cast(#v as interval);', {{['#v'] = v}})
755+
756+
---
757+
- metadata:
758+
- name: COLUMN_1
759+
type: interval
760+
rows:
761+
- ['+1 years, 1 months, 1 weeks, 1 days, 1 hours, 1 minutes, 1.000000001 seconds']
762+
...
763+
605764
.. _sql_data_type_scalar:
606765

607766
SCALAR can be used for

0 commit comments

Comments
 (0)