layout: section title: “Beam ZetaSQL conversion rules” section_menu: section-menu/sdks.html permalink: /documentation/dsls/sql/zetasql/conversion-rules/

Beam ZetaSQL conversion rules

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

  • Casting is explicit conversion and uses the CAST() function.
  • Coercion is implicit conversion, which Beam SQL performs automatically under the conditions described below.

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

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:

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.