elasticsearch/docs/reference/esql/implicit-casting.asciidoc
Fang Xing df1130f4b2
[ES|QL][DOCS] Add docs for date_period and time_duration (#116368) (#117021)
* add docs for date_period and time_duration
2024-11-20 00:14:46 +11:00

69 lines
2.2 KiB
Text

[[esql-implicit-casting]]
=== {esql} implicit casting
++++
<titleabbrev>Implicit casting</titleabbrev>
++++
Often users will input `date`, `date_period`, `time_duration`, `ip` or `version` as simple strings in their queries for use in predicates, functions, or expressions. {esql} provides <<esql-type-conversion-functions, type conversion functions>> to explicitly convert these strings into the desired data types.
Without implicit casting users must explicitly code these `to_X` functions in their queries, when string literals don't match the target data types they are assigned or compared to. Here is an example of using `to_datetime` to explicitly perform a data type conversion.
[source.merge.styled,esql]
----
FROM employees
| EVAL dd_ns1=date_diff("day", to_datetime("2023-12-02T11:00:00.00Z"), birth_date)
| SORT emp_no
| KEEP dd_ns1
| LIMIT 1
----
[discrete]
[[esql-implicit-casting-example]]
==== Implicit casting example
Implicit casting automatically converts string literals to the target data type. This allows users to specify string values for types like `date`, `date_period`, `time_duration`, `ip` and `version` in their queries.
The first query can be coded without calling the `to_datetime` function, as follows:
[source.merge.styled,esql]
----
FROM employees
| EVAL dd_ns1=date_diff("day", "2023-12-02T11:00:00.00Z", birth_date)
| SORT emp_no
| KEEP dd_ns1
| LIMIT 1
----
[discrete]
[[esql-implicit-casting-supported-operations]]
==== Operations that support implicit casting
The following table details which {esql} operations support implicit casting for different data types.
[%header.monospaced.styled,format=dsv,separator=|]
|===
|ScalarFunctions|Operators|<<esql-group-functions, GroupingFunctions>>|<<esql-agg-functions, AggregateFunctions>>
DATE|Y|Y|Y|N
DATE_PERIOD/TIME_DURATION|Y|N|Y|N
IP|Y|Y|Y|N
VERSION|Y|Y|Y|N
BOOLEAN|Y|Y|Y|N
|===
ScalarFunctions includes:
* <<esql-conditional-functions-and-expressions, Conditional Functions and Expressions>>
* <<esql-date-time-functions, Date and Time Functions>>
* <<esql-ip-functions, IP Functions>>
Operators includes:
* <<esql-binary-operators, Binary Operators>>
* <<esql-unary-operators, Unary Operator>>
* <<esql-in-operator, IN>>