Impala type conversion functions
Conversion functions are usually used in combination with other functions, to
explicitly pass the expected data types. Impala has strict rules regarding data types for
function parameters. For example, Impala does not automatically convert a
DOUBLE
value to FLOAT
, a BIGINT
value to
INT
, or other conversion where precision could be lost or overflow could
occur. Also, for reporting or dealing with loosely defined schemas in big data contexts, you
might frequently need to convert values to or from the STRING
type.
Function reference:
Impala supports the following type conversion functions:
- CAST(expression AS type)
-
Purpose: Returns expression converted to the
type data type. If the expression value is of a type that cannot be converted to the target type:
- Of
DECIMAL
,DATE
, andBOOLEAN
, the function returns an error. - Of all other types, the function returns
NULL
.
Usage notes:
Use
CAST
when passing a column value or literal to a function that expects a parameter with a different type. Frequently used in SQL operations such asCREATE TABLE AS SELECT
andINSERT ... VALUES
to ensure that values from various sources are of the appropriate type for the destination columns.Where practical, do a one-time
CAST()
operation during the ingestion process to make each column into the appropriate type, rather than using manyCAST()
operations in each query; doing type conversions for each row during each query can be expensive for tables with millions or billions of rows.The way this function deals with time zones when converting to or from
TIMESTAMP
values is affected by the‑‑use_local_tz_for_unix_timestamp_conversions
startup flag for the impalad daemon. See TIMESTAMP data type for details about how Impala handles time zone considerations for theTIMESTAMP
data type. - Of
- TYPEOF(expression)
-
Purpose: Returns the name of the data type corresponding to
expression. For types with extra attributes,
such as length for
CHAR
andVARCHAR
, or precision and scale forDECIMAL
, includes the full specification of the type.Return type:
STRING
Usage notes: Typically used in interactive exploration of a schema, or in application code that programmatically generates schema definitions such as
CREATE TABLE
statements, for example, to get the type of an expression such ascol1 / col2
orCONCAT(col1, col2, col3)
. This function is especially useful for arithmetic expressions involvingDECIMAL
types because the precision and scale of the result is can be different than that of the operands.Examples:
TYPEOF(2)
returnsTINYINT
.TYPEOF(NOW())
returnsTIMESTAMP
.