Beam ZetaSQL conversion rules

Conversion includes, but is not limited to, casting and coercion:

The table below summarizes all possible CASTs and coercions. “Coercion To” applies to all expressions of a given data type (e.g. a column).

From TypeCAST toCoercion To
INT64INT64
FLOAT64
STRING
FLOAT64
FLOAT64FLOAT64
 
BOOLBOOL
 
STRINGINT64
STRING
BYTES
TIMESTAMP
 
BYTESBYTES
STRING
 
TIMESTAMPSTRING
TIMESTAMP
 
ARRAYARRAY 
STRUCTSTRUCT 

Casting

Syntax:

CAST(expr AS typename)

Cast syntax is used in a query to indicate that the result type of an expression should be converted to some other type.

Example:

CAST(x=1 AS STRING)

This results in "true" if x is 1, "false" for any other non-NULL value, and NULL if x is NULL.

Casts between supported types that do not successfully map from the original value to the target domain produce runtime errors. For example, casting BYTES to STRING where the byte sequence is not valid UTF-8 results in a runtime error.

When casting an expression x of the following types, these rules apply:

FromToRule(s) when casting x
INT64FLOAT64Returns a close but potentially not exact FLOAT64 value.
FLOAT64STRINGReturns an approximate string representation.
STRINGBYTESSTRINGs are cast to BYTES using UTF-8 encoding. For example, the STRING "©", when cast to BYTES, would become a 2-byte sequence with the hex values C2 and A9.
BYTESSTRINGReturns x interpreted as a UTF-8 STRING.
For example, the BYTES literal b'\xc2\xa9', when cast to STRING, is interpreted as UTF-8 and becomes the unicode character "©".
An error occurs if x is not valid UTF-8.
ARRAYARRAYMust be the exact same ARRAY type.
STRUCTSTRUCTAllowed if the following conditions are met:
  1. The two STRUCTs have the same number of fields.
  2. The original STRUCT field types can be explicitly cast to the corresponding target STRUCT field types (as defined by field order, not field name).

Coercion

Beam SQL coerces the result type of an expression to another type if needed to match function signatures. For example, if function func() is defined to take a single argument of type INT64 and an expression is used as an argument that has a result type of FLOAT64, then the result of the expression will be coerced to INT64 type before func() is computed.