6.5. Conversion Functions

Presto will implicitly convert numeric and character values to thecorrect type if such a conversion is possible. Presto will not convertbetween character and numeric types. For example, a query that expectsa varchar will not automatically convert a bigint value to anequivalent varchar.

When necessary, values can be explicitly cast to a particular type.

Conversion Functions

  • cast(value AS type) → type
  • Explicitly cast a value as a type. This can be used to cast avarchar to a numeric value type and vice versa.

  • trycast(_value AS type) → type

  • Like cast(), but returns null if the cast fails.

Data Size

The parse_presto_data_size function supports the following units:

UnitDescriptionValue
BBytes1
kBKilobytes1024
MBMegabytes10242
GBGigabytes10243
TBTerabytes10244
PBPetabytes10245
EBExabytes10246
ZBZettabytes10247
YBYottabytes10248
  • parsepresto_data_size(_string) -> decimal(38)
  • Parses string of format value unit into a number, wherevalue is the fractional number of unit values:
  1. SELECT parse_presto_data_size('1B'); -- 1
  2. SELECT parse_presto_data_size('1kB'); -- 1024
  3. SELECT parse_presto_data_size('1MB'); -- 1048576
  4. SELECT parse_presto_data_size('2.3MB'); -- 2411724

Miscellaneous

  • typeof(expr) → varchar
  • Returns the name of the type of the provided expression:
  1. SELECT typeof(123); -- integer
  2. SELECT typeof('cat'); -- varchar(3)
  3. SELECT typeof(cos(2) + 1.5); -- double