Data types
In addition to this list, the SQL plugin also supports the type, though it doesn’t have a corresponding mapping with OpenSearch or SQL. To use a function without a corresponding mapping, you must explicitly convert the data type to one that does.
The date and time types represent a time period: DATE
, TIME
, DATETIME
, TIMESTAMP
, and INTERVAL
. By default, the OpenSearch DSL uses the date
type as the only date-time related type that contains all information of an absolute time point.
To integrate with SQL, each type other than the timestamp
type holds part of the time period information. To use date-time functions, see . Some functions might have restrictions for the input argument type.
The date
type represents the calendar date regardless of the time zone. A given date value is a 24-hour period, but this period varies in different timezones and might have flexible hours during daylight saving programs. The date
type doesn’t contain time information and it only supports a range of 1000-01-01
to 9999-12-31
.
Type | Syntax | Range |
---|---|---|
date | yyyy-MM-dd | 0001-01-01 to 9999-12-31 |
Time
The time
type represents the time of a clock regardless of its timezone. The time
type doesn’t contain date information.
Type | Syntax | Range |
---|---|---|
datetime | yyyy-MM-dd hh:mm:ss[.fraction] | 0001-01-01 00:00:00.0000000000 to 9999-12-31 23:59:59.9999999999 |
Timestamp
The timestamp
type is an absolute instance independent of timezone or convention. For example, for a given point of time, if you change the timestamp to a different timezone, its value changes accordingly.
The timestamp
type is stored differently from the other types. It’s converted from its current timezone to UTC for storage and converted back to its set timezone from UTC when it’s retrieved.
The interval
type represents a temporal duration or a period.
Type | Syntax |
---|---|
interval | INTERVAL expr unit |
The expr
unit is any expression that eventually iterates to a quantity value. It represents a unit for interpreting the quantity, including , SECOND
, MINUTE
, HOUR
, DAY
, WEEK
, MONTH
, QUARTER
, and YEAR
. The INTERVAL
keyword and the unit specifier are not case sensitive.
The interval
type has two classes of intervals: year-week intervals and day-time intervals.
- Year-week intervals store years, quarters, months, and weeks.
- Day-time intervals store days, hours, minutes, seconds, and microseconds.
Convert between date and time types
The SQL plugin supports the following conversion rules for each of the types:
Convert from date
- Because the
date
value doesn’t have any time information, conversion to thetime
type isn’t useful and always returns a zero time value of00:00:00
. - Converting from
date
todatetime
has a data fill-up due to the lack of time information. It attaches the time00:00:00
to the original date by default and forms adatetime
instance. For example, conversion of2020-08-17
to adatetime
type is2020-08-17 00:00:00
.
Convert from time
- You cannot convert the
time
type to any other date and time types because it doesn’t contain any date information.
Convert from datetime
- Converting
datetime
todate
extracts the date value from thedatetime
value. For example, conversion of2020-08-17 14:09:00
to adate
type is2020-08-08
. - Converting
datetime
totime
extracts the time value from thedatetime
value. For example, conversion of2020-08-17 14:09:00
to atime
type is14:09:00
. - Because the
datetime
type doesn’t contain timezone information, converting totimestamp
type fills up the timezone value with the session timezone. For example, conversion of2020-08-17 14:09:00
(UTC) to atimestamp
type is2020-08-17 14:09:00 UTC
.
Convert from timestamp
- Converting from a
timestamp
type to adate
type extracts the date value and converting to atime
type extracts the time value. Converting from atimestamp
type todatetime
type extracts only thedatetime
value and leaves out the timezone value. For example, conversion of2020-08-17 14:09:00
UTC to adate
type is2020-08-17
, to atime
type is14:09:00
, and to adatetime
type is2020-08-17 14:09:00
.