implicit-casting.asciidoc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. [[esql-implicit-casting]]
  2. === {esql} implicit casting
  3. ++++
  4. <titleabbrev>Implicit casting</titleabbrev>
  5. ++++
  6. Often users will input `datetime`, `ip`, `version`, or geospatial objects 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 `datetime`, `ip`, `version` or a geo spatial. 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|BinaryComparison|ArithmeticOperation|InListPredicate|AggregateFunction
  32. |DATETIME|Y|Y|Y|Y|N
  33. |DOUBLE|Y|N|N|N|N
  34. |LONG|Y|N|N|N|N
  35. |INTEGER|Y|N|N|N|N
  36. |IP|Y|Y|Y|Y|N
  37. |VERSION|Y|Y|Y|Y|N
  38. |GEO_POINT|Y|N|N|N|N
  39. |GEO_SHAPE|Y|N|N|N|N
  40. |CARTESIAN_POINT|Y|N|N|N|N
  41. |CARTESIAN_SHAPE|Y|N|N|N|N
  42. |BOOLEAN|Y|Y|Y|Y|N
  43. |===