Conversion rules in GoogleSQL

GoogleSQL for Spanner supports conversion.Conversion includes, but isn't limited to, casting, coercion, andsupertyping.

  • Casting is explicit conversion and uses theCAST() function.
  • Coercion is implicit conversion, which GoogleSQL performsautomatically under the conditions described below.
  • A supertype is a common type to which two or more expressions can be coerced.

There are also conversions that have their own function names, such asPARSE_DATE(). To learn more about these functions, seeConversion functions.

Comparison of casting and coercion

The following table summarizes all possible cast and coercion possibilities forGoogleSQL data types. TheCoerce to column applies to allexpressions of a given data type, (for example, acolumn).

From typeCast toCoerce to
INT64BOOL
INT64
NUMERIC
FLOAT32
FLOAT64
STRING
ENUM
NUMERIC
FLOAT64
NUMERICINT64
NUMERIC
FLOAT32
FLOAT64
STRING
FLOAT64
FLOAT32INT64
NUMERIC
FLOAT32
FLOAT64
STRING
FLOAT64
FLOAT64INT64
NUMERIC
FLOAT32
FLOAT64
STRING
 
BOOLBOOL
INT64
STRING
 
STRINGBOOL
INT64
NUMERIC
FLOAT32
FLOAT64
STRING
BYTES
DATE
TIMESTAMP
ENUM
PROTO
 
BYTESSTRING
BYTES
PROTO
 
DATESTRING
DATE
TIMESTAMP
TIMESTAMPSTRING
DATE
TIMESTAMP
 
ARRAYARRAY
 
ENUMENUM (with the sameENUM name)
INT64
STRING
ENUM (with the sameENUM name)
STRUCTSTRUCT
 
PROTOPROTO (with the samePROTO name)
STRING
BYTES
PROTO (with the samePROTO name)

Casting

Most data types can be cast from one type to another with theCAST function.When usingCAST, a query can fail if GoogleSQL is unable to performthe cast. If you want to protect your queries from these types of errors, youcan useSAFE_CAST. To learn more about the rules forCAST,SAFE_CAST andother casting functions, seeConversion functions.

Coercion

GoogleSQL coerces the result type of an argument expression to anothertype if needed to match function signatures. For example, if functionfunc()is defined to take a single argument of typeFLOAT64and an expression is used as an argument that has a result type ofINT64, then the result of the expression will becoerced toFLOAT64 type beforefunc() is computed.

Supertypes

A supertype is a common type to which two or more expressions can be coerced.Supertypes are used with set operations such asUNION ALL and expressions suchasCASE that expect multiple arguments with matching types. Each type has oneor more supertypes, including itself, which defines its set of supertypes.

Input typeSupertypes
BOOLBOOL
INT64INT64
FLOAT32
FLOAT64
NUMERIC
FLOAT32FLOAT32
FLOAT64
FLOAT64FLOAT64
NUMERICNUMERIC
FLOAT64
STRINGSTRING
DATEDATE
TIMESTAMPTIMESTAMP
ENUMENUM with the same name. The resulting enum supertype is the one that occurred first.
BYTESBYTES
STRUCTSTRUCT with the same field position types.
ARRAYARRAY with the same element types.
PROTOPROTO with the same name. The resultingPROTO supertype is the one that occurred first. For example, the first occurrence could be in the first branch of a set operation or the first result expression in aCASE statement.
GRAPH_ELEMENTGRAPH_ELEMENT. A graph element can be a supertype of another graph element if the following is true:
  • Graph elementa is a supertype of graph elementb and they're the same element kind.
  • Graph elementa's property type list is a compatible superset of graph elementb's property type list. This means that properties with the same name must also have the same type.
GRAPH_PATHGRAPH_PATH. A graph path can be a supertype of another graph path if the following is true:
  • Graph patha is a supertype of graph pathb if the node type fora is a supertype of the node type forb. In addition, the edge type fora must be a supertype of the edge type forb.
  • Graph patha's property type list is a compatible superset of graph pathb's property type list. This means that properties with the same name must also have the same type.

If you want to find the supertype for a set of input types, first determine theintersection of the set of supertypes for each input type. If that set is emptythen the input types have no common supertype. If that set is non-empty, thenthe common supertype is generally themost specific type in that set. Generally,the most specific type is the type with the most restrictive domain.

Examples

Input typesCommon supertypeReturnsNotes
INT64
FLOAT32
FLOAT64FLOAT64 If you apply supertyping toINT64 andFLOAT32, supertyping succeeds because they they share a supertype,FLOAT64.
INT64
FLOAT64
FLOAT64FLOAT64 If you apply supertyping toINT64 andFLOAT64, supertyping succeeds because they they share a supertype,FLOAT64.
INT64
BOOL
NoneError If you apply supertyping toINT64 andBOOL, supertyping fails because they don't share a common supertype.

Exact and inexact types

Numeric types can be exact or inexact. For supertyping, if all of theinput types are exact types, then the resulting supertype can only be anexact type.

The following table contains a list of exact and inexact numeric data types.

Exact typesInexact types
INT64
NUMERIC
FLOAT32
FLOAT64

Examples

Input typesCommon supertypeReturnsNotes
INT64
FLOAT64
FLOAT64FLOAT64 If supertyping is applied toINT64 andFLOAT64, supertyping succeeds because there are exact and inexact numeric types being supertyped.

Types specificity

Each type has a domain of values that it supports. A type with anarrow domain is more specific than a type with a wider domain. Exact typesare more specific than inexact types because inexact types have a wider rangeof domain values that are supported than exact types. For example,INT64 is more specific thanFLOAT64.

Supertypes and literals

Supertype rules for literals are more permissive than for normal expressions,and are consistent with implicit coercion rules. The following algorithm is usedwhen the input set of types includes types related to literals:

  • If there exists non-literals in the set, find the set of common supertypesof the non-literals.
  • If there is at least one possible supertype, find themost specific type towhich the remaining literal types can be implicitly coerced and return thatsupertype. Otherwise, there is no supertype.
  • If the set only contains types related to literals, compute the supertype ofthe literal types.
  • If all input types are related toNULL literals, then the resultingsupertype isINT64.
  • If no common supertype is found, an error is produced.

Examples

Input typesCommon supertypeReturns
INT64 literal
UINT64 expression
UINT64UINT64
FLOAT64 literal
FLOAT32 expression
FLOAT32FLOAT32
INT64 literal
FLOAT64 literal
FLOAT64FLOAT64
TIMESTAMP expression
STRING literal
TIMESTAMPTIMESTAMP
NULL literal
NULL literal
INT64INT64
BOOL literal
TIMESTAMP literal
NoneError

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.