implicit-casting.asciidoc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. [[esql-implicit-casting]]
  2. === {esql} implicit casting
  3. ++++
  4. <titleabbrev>Implicit casting</titleabbrev>
  5. ++++
  6. Often users will input `date`, `ip`, `version`, `date_period` or `time_duration` 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.
  7. 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.
  8. [source.merge.styled,esql]
  9. ----
  10. FROM employees
  11. | EVAL dd_ns1=date_diff("day", to_datetime("2023-12-02T11:00:00.00Z"), birth_date)
  12. | SORT emp_no
  13. | KEEP dd_ns1
  14. | LIMIT 1
  15. ----
  16. Implicit casting improves usability, by automatically converting string literals to the target data type. This is most useful when the target data type is `date`, `ip`, `version`, `date_period` or `time_duration`. It is natural to specify these as a string in queries.
  17. The first query can be coded without calling the `to_datetime` function, as follows:
  18. [source.merge.styled,esql]
  19. ----
  20. FROM employees
  21. | EVAL dd_ns1=date_diff("day", "2023-12-02T11:00:00.00Z", birth_date)
  22. | SORT emp_no
  23. | KEEP dd_ns1
  24. | LIMIT 1
  25. ----
  26. [float]
  27. === Implicit casting support
  28. The following table details which {esql} operations support implicit casting for different data types.
  29. [%header.monospaced.styled,format=dsv,separator=|]
  30. |===
  31. ||ScalarFunction*|Operator*|<<esql-group-functions, GroupingFunction>>|<<esql-agg-functions, AggregateFunction>>
  32. |DATE|Y|Y|Y|N
  33. |IP|Y|Y|Y|N
  34. |VERSION|Y|Y|Y|N
  35. |BOOLEAN|Y|Y|Y|N
  36. |DATE_PERIOD/TIME_DURATION|Y|N|Y|N
  37. |===
  38. ScalarFunction* includes:
  39. <<esql-conditional-functions-and-expressions, Conditional Functions and Expressions>>
  40. <<esql-date-time-functions, Date and Time Functions>>
  41. <<esql-ip-functions, IP Functions>>
  42. Operator* includes:
  43. <<esql-binary-operators, Binary Operators>>
  44. <<esql-unary-operators, Unary Operator>>
  45. <<esql-in-operator, IN>>