Data types in GoogleSQL

This page provides an overview of all GoogleSQL for Spannerdata types, including information about their valuedomains. Forinformation on data type literals and constructors, seeLexical Structure and Syntax.

Data type list

NameSummary
Array type An ordered list of zero or more elements of non-array values.
SQL type name:ARRAY
Boolean type A value that can be eitherTRUE orFALSE.
SQL type name:BOOL
SQL aliases:BOOLEAN
Bytes type Variable-length binary data.
SQL type name:BYTES
Date type A Gregorian calendar date, independent of time zone.
SQL type name:DATE
Enum type Named type that enumerates a list of possible values.
SQL type name:ENUM
Graph element type An element in a property graph.
SQL type name:GRAPH_ELEMENT
Graph path type A path in a property graph.
SQL type name:GRAPH_PATH
Interval type A duration of time, without referring to any specific point in time.
SQL type name:INTERVAL
JSON type Represents JSON, a lightweight data-interchange format.
SQL type name:JSON
Numeric types

A numeric value. Several types are supported.

A 64-bit integer.
SQL type name:INT64

A decimal value with precision of 38 digits.
SQL type name:NUMERIC

An approximate single precision numeric value.
SQL type name:FLOAT32

An approximate double precision numeric value.
SQL type name:FLOAT64

Protocol buffer type A protocol buffer.
SQL type name:PROTO
String type Variable-length character data.
SQL type name:STRING
Struct type Container of ordered fields.
SQL type name:STRUCT
Timestamp type A timestamp value represents an absolute point in time, independent of any time zone or convention such as daylight saving time (DST).
SQL type name:TIMESTAMP

Data type properties

When storing and querying data, it's helpful to keep the following data typeproperties in mind:

Valid column types

All data types are valid column types, except for:

  • STRUCT
  • INTERVAL

Valid key column types

All data types are valid key column types for primary keys, foreign keys, andsecondary indexes, except for:

  • FLOAT32
  • ARRAY
  • JSON
  • STRUCT

Storage size for data types

Each data type includes 8 bytes of storage overhead, in addition to thefollowing values:

  • ARRAY: The sum of the size of its elements.
  • BOOL: 1 byte.
  • BYTES: The number of bytes.
  • DATE: 4 bytes.
  • FLOAT32: 4 bytes.
  • FLOAT64: 8 bytes.
  • INT64: 8 bytes.
  • JSON: The number of bytes in UTF-8 encoding of the JSON-formatted stringequivalent after canonicalization.
  • NUMERIC: A function of both the precision and scale of the value beingstored. The value0 is stored as 1 byte. The storage size for all othervalues varies between 6 and 22 bytes.
  • STRING: The number of bytes in its UTF-8 encoding.
  • STRUCT: The sum of its parts.
  • TIMESTAMP: 12 bytes.

Nullable data types

For nullable data types,NULL is a valid value. Currently, all existingdata types are nullable.

Orderable data types

Expressions of orderable data types can be used in anORDER BY clause.Applies to all data types except for:

  • ARRAY
  • PROTO
  • STRUCT
  • JSON
  • GRAPH_ELEMENT
  • GRAPH_PATH

OrderingNULLs

In the context of theORDER BY clause,NULLs are the minimumpossible value; that is,NULLs appear first inASC sorts and last inDESC sorts.

To learn more about usingASC andDESC, seetheORDER BY clause.

Ordering floating points

Floating point values are sorted in this order, from least to greatest:

  1. NULL
  2. NaN — AllNaN values are considered equal when sorting.
  3. -inf
  4. Negative numbers
  5. 0 or -0 — All zero values are considered equal when sorting.
  6. Positive numbers
  7. +inf

Groupable data types

Can generally appear in an expression followingGROUP BY andDISTINCT.All data types are supported except for:

  • PROTO
  • JSON
  • ARRAY
  • STRUCT
  • GRAPH_PATH

Grouping with floating point types

Groupable floating point types can appear in an expression followingGROUP BYandDISTINCT.

Special floating point values are grouped in the following way, includingboth grouping done by aGROUP BY clause and grouping done by theDISTINCT keyword:

  • NULL
  • NaN — AllNaN values are considered equal when grouping.
  • -inf
  • 0 or -0 — All zero values are considered equal when grouping.
  • +inf

Comparable data types

Values of the same comparable data type can be compared to each other.All data types are supported except for:

  • PROTO
  • JSON

Notes:

  • Equality comparisons for array data types are supported as long as theelement types are the same, and the element types are comparable. Less thanand greater than comparisons aren't supported.
  • Equality comparisons for structs are supported field by field, infield order. Field names are ignored. Less than and greater than comparisonsaren't supported.
  • All types that support comparisons can be used in aJOIN condition.SeeJOIN Types for an explanation of join conditions.

Array type

NameDescription
ARRAYOrdered list of zero or more elements of any non-array type.

An array is an ordered list of zero or more elements of non-array values.Elements in an array must share the same type.

Arrays of arrays aren't allowed. Queries that would produce an array ofarrays return an error. Instead, a struct must be inserted between thearrays using theSELECT AS STRUCT construct.

To learn more about the literal representation of an array type,seeArray literals.

To learn more about using arrays in GoogleSQL, seeWork witharrays.

NULLs and the array type

An empty array and aNULL array are two distinct values. Arrays can containNULL elements.

Declaring an array type

ARRAY<T>

Array types are declared using the angle brackets (< and>). The typeof the elements of an array can be arbitrarily complex with the exception thatan array can't directly contain another array.

Examples

Type DeclarationMeaning
ARRAY<INT64>Simple array of 64-bit integers.
ARRAY<STRUCT<INT64, INT64>>An array of structs, each of which contains two 64-bit integers.
ARRAY<ARRAY<INT64>>
(not supported)
This is aninvalid type declaration which is included herejust in case you came looking for how to create a multi-level array. Arrayscan't contain arrays directly. Instead see the next example.
ARRAY<STRUCT<ARRAY<INT64>>>An array of arrays of 64-bit integers. Notice that there is a struct betweenthe two arrays because arrays can't hold other arrays directly.

Constructing an array

You can construct an array using array literals or array functions.

Using array literals

You can build an array literal in GoogleSQL using brackets ([ and]). Each element in an array is separated by a comma.

SELECT[1,2,3]ASnumbers;SELECT["apple","pear","orange"]ASfruit;SELECT[true,false,true]ASbooleans;

You can also create arrays from any expressions that have compatible types. Forexample:

SELECT[a,b,c]FROM(SELECT5ASa,37ASb,406ASc);SELECT[a,b,c]FROM(SELECTCAST(5ASINT64)ASa,CAST(37ASFLOAT64)ASb,406ASc);

Notice that the second example contains three expressions: one that returns anINT64, one that returns aFLOAT64, and one thatdeclares a literal. This expression works because all three expressions shareFLOAT64 as a supertype.

To declare a specific data type for an array, use anglebrackets (< and>). For example:

SELECTARRAY<FLOAT64>[1,2,3]ASfloats;

Arrays of most data types, such asINT64 orSTRING, don't requirethat you declare them first.

SELECT[1,2,3]ASnumbers;

You can write an empty array of a specific type usingARRAY<type>[]. You canalso write an untyped empty array using[], in which case GoogleSQLattempts to infer the array type from the surrounding context. IfGoogleSQL can't infer a type, the default typeARRAY<INT64> is used.

Using generated values

You can also construct anARRAY with generated values.

Generating arrays of integers

GENERATE_ARRAYgenerates an array of values from a starting and ending value and a step value.For example, the following query generates an array that contains all of the oddintegers from 11 to 33, inclusive:

SELECTGENERATE_ARRAY(11,33,2)ASodds;/*--------------------------------------------------+ | odds                                             | +--------------------------------------------------+ | [11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33] | +--------------------------------------------------*/

You can also generate an array of values in descending order by giving anegative step value:

SELECTGENERATE_ARRAY(21,14,-1)AScountdown;/*----------------------------------+ | countdown                        | +----------------------------------+ | [21, 20, 19, 18, 17, 16, 15, 14] | +----------------------------------*/
Generating arrays of dates

GENERATE_DATE_ARRAYgenerates an array ofDATEs from a starting and endingDATE and a stepINTERVAL.

You can generate a set ofDATE values usingGENERATE_DATE_ARRAY. Forexample, this query returns the currentDATE and the followingDATEs at 1WEEK intervals up to and including a laterDATE:

SELECTGENERATE_DATE_ARRAY('2017-11-21','2017-12-31',INTERVAL1WEEK)ASdate_array;/*--------------------------------------------------------------------------+ | date_array                                                               | +--------------------------------------------------------------------------+ | [2017-11-21, 2017-11-28, 2017-12-05, 2017-12-12, 2017-12-19, 2017-12-26] | +--------------------------------------------------------------------------*/

Boolean type

NameDescription
BOOL
BOOLEAN
Boolean values are represented by the keywordsTRUE andFALSE (case-insensitive).

BOOLEAN is an alias forBOOL.

Boolean values are sorted in this order, from least to greatest:

  1. NULL
  2. FALSE
  3. TRUE

Bytes type

NameDescription
BYTESVariable-length binary data.

String and bytes are separate types that can't be used interchangeably.Most functions on strings are also defined on bytes. The bytes versionoperates on raw bytes rather than Unicode characters. Casts between string andbytes enforce that the bytes are encoded using UTF-8.

You can convert a base64-encodedSTRING expression into theBYTES formatusing theFROM_BASE64 function.You can also convert a sequence ofBYTES into a base64-encodedSTRINGexpression using theTO_BASE64 function.

To learn more about the literal representation of a bytes type,seeBytes literals.

Date type

NameRange
DATE0001-01-01 to 9999-12-31.

The date type represents a Gregorian calendar date, independent of time zone. Adate value doesn't represent a specific 24-hour time period. Rather, a givendate value represents a different 24-hour period when interpreted in differenttime zones, and may represent a shorter or longer day during daylight savingtime (DST) transitions.To represent an absolute point in time,use atimestamp.

Canonical format
YYYY-[M]M-[D]D
  • YYYY: Four-digit year.
  • [M]M: One or two digit month.
  • [D]D: One or two digit day.

To learn more about the literal representation of a date type,seeDate literals.

Enum type

NameDescription
ENUMNamed type that maps string constants to integer constants.

An enum is a named type that enumerates a list of possible values, each of whichcontains:

  • An integer value: Integers are used for comparison and ordering enum values.There is no requirement that these integers start at zero or that they becontiguous.
  • A string value for its name: Strings are case sensitive. In the case ofprotocol buffer open enums, this name is optional.
  • Optional alias values: One or more additional string values that act asaliases.

Enum values are referenced using their integer value or their string value.You reference an enum type, such as when using CAST, by using its fullyqualified name.

You must define theENUM type in a protocol buffer file anddeclare it in theCREATE PROTO BUNDLE statement.

You can't create new enum types using GoogleSQL.

To learn more about the literal representation of an enum type,seeEnum literals.

Graph element type

NameDescription
GRAPH_ELEMENT An element in a property graph.

A variable with aGRAPH_ELEMENT type is produced by a graph query.The generated type has this format:

GRAPH_ELEMENT<T>

A graph element is either a node or an edge, representing data from amatching node or edge table based on its label. Each graph element holds aset of properties that can be accessed with a case-insensitive name,similar to fields of a struct.

Graph elements with dynamic properties enabledcan store properties beyond those defined in the schema. A schema changeisn't needed to manage dynamic properties because theproperty names and values are based on the input column's values. You can accessdynamic properties with their names in the same way as defined properties. Forinformation about how to model dynamic properties, seedynamic properties definition.

If a property isn't defined in the schema, accessing itthrough thefield-access-operator returns theJSONtype if the dynamic property exists, orNULL if the property doesn't exist.

Note: Names uniquely identify all properties in a graph element,case-insensitively. A defined property takes precedence over any dynamicproperty when their names conflict.

Example

In the following example,n represents a graph element in theFinGraph property graph:

GRAPHFinGraphMATCH(n:Person)RETURNn.name

Graph path type

NameDescription
GRAPH_PATH A path in a property graph.

The graph path data type represents a sequence of nodes interleavedwith edges and has this format:

GRAPH_PATH<NODE_TYPE,EDGE_TYPE>

You can construct a graph path with thePATH function or when you create apath variable in a graph pattern.

Interval type

NameRange
INTERVAL-10000-0 -3660000 -87840000:0:0 to 10000-0 3660000 87840000:0:0

AnINTERVAL object represents duration or amount of time, without referringto any specific point in time.

Canonical format
[sign]Y-M[sign]D[sign]H:M:S[.F]
  • sign:+ or-
  • Y: Year
  • M: Month
  • D: Day
  • H: Hour
  • M: Minute
  • S: Second
  • [.F]: Up to nine fractionaldigits (nanosecond precision)

To learn more about the literal representation of an interval type,seeInterval literals.

Constructing an interval

You can construct an interval with an interval literal that supportsasingle datetime part or adatetime part range.

Construct an interval with a single datetime part

INTERVALint64_expressiondatetime_part

You can construct anINTERVAL object with anINT64 expression and oneinterval-supported datetime part. For example:

-- 1 year, 0 months, 0 days, 0 hours, 0 minutes, and 0 seconds (1-0 0 0:0:0)INTERVAL1YEARINTERVAL4QUARTERINTERVAL12MONTH-- 0 years, 3 months, 0 days, 0 hours, 0 minutes, and 0 seconds (0-3 0 0:0:0)INTERVAL1QUARTERINTERVAL3MONTH-- 0 years, 0 months, 42 days, 0 hours, 0 minutes, and 0 seconds (0-0 42 0:0:0)INTERVAL6WEEKINTERVAL42DAY-- 0 years, 0 months, 0 days, 25 hours, 0 minutes, and 0 seconds (0-0 0 25:0:0)INTERVAL25HOURINTERVAL1500MINUTEINTERVAL90000SECOND-- 0 years, 0 months, 0 days, 1 hours, 30 minutes, and 0 seconds (0-0 0 1:30:0)INTERVAL90MINUTE-- 0 years, 0 months, 0 days, 0 hours, 1 minutes, and 30 seconds (0-0 0 0:1:30)INTERVAL90SECOND-- 0 years, 0 months, -5 days, 0 hours, 0 minutes, and 0 seconds (0-0 -5 0:0:0)INTERVAL-5DAY

For additional examples, seeInterval literals.

Construct an interval with a datetime part range

INTERVALdatetime_parts_stringstarting_datetime_partTOending_datetime_part

You can construct anINTERVAL object with aSTRING that contains thedatetime parts that you want to include, a starting datetime part, and an endingdatetime part. The resultingINTERVAL object only includes datetime parts inthe specified range.

You can use one of the following formats with theinterval-supported datetime parts:

Datetime part stringDatetime partsExample
Y-MYEAR TO MONTHINTERVAL '2-11' YEAR TO MONTH
Y-M DYEAR TO DAYINTERVAL '2-11 28' YEAR TO DAY
Y-M D HYEAR TO HOURINTERVAL '2-11 28 16' YEAR TO HOUR
Y-M D H:MYEAR TO MINUTEINTERVAL '2-11 28 16:15' YEAR TO MINUTE
Y-M D H:M:SYEAR TO SECONDINTERVAL '2-11 28 16:15:14' YEAR TO SECOND
M DMONTH TO DAYINTERVAL '11 28' MONTH TO DAY
M D HMONTH TO HOURINTERVAL '11 28 16' MONTH TO HOUR
M D H:MMONTH TO MINUTEINTERVAL '11 28 16:15' MONTH TO MINUTE
M D H:M:SMONTH TO SECONDINTERVAL '11 28 16:15:14' MONTH TO SECOND
D HDAY TO HOURINTERVAL '28 16' DAY TO HOUR
D H:MDAY TO MINUTEINTERVAL '28 16:15' DAY TO MINUTE
D H:M:SDAY TO SECONDINTERVAL '28 16:15:14' DAY TO SECOND
H:MHOUR TO MINUTEINTERVAL '16:15' HOUR TO MINUTE
H:M:SHOUR TO SECONDINTERVAL '16:15:14' HOUR TO SECOND
M:SMINUTE TO SECONDINTERVAL '15:14' MINUTE TO SECOND

For example:

-- 0 years, 8 months, 20 days, 17 hours, 0 minutes, and 0 seconds (0-8 20 17:0:0)INTERVAL'8 20 17'MONTHTOHOUR-- 0 years, 8 months, -20 days, 17 hours, 0 minutes, and 0 seconds (0-8 -20 17:0:0)INTERVAL'8 -20 17'MONTHTOHOUR

For additional examples, seeInterval literals.

Interval-supported date and time parts

You can use the following date parts to construct an interval:

  • YEAR: Number of years,Y.
  • QUARTER: Number of quarters; each quarter is converted to3 months,M.
  • MONTH: Number of months,M. Each12 months is converted to1 year.
  • WEEK: Number of weeks; Each week is converted to7 days,D.
  • DAY: Number of days,D.

You can use the following time parts to construct an interval:

  • HOUR: Number of hours,H.
  • MINUTE: Number of minutes,M. Each60 minutes is converted to1 hour.
  • SECOND: Number of seconds,S. Each60 seconds is converted to1 minute. Can include up to nine fractionaldigits (nanosecond precision).
  • MILLISECOND: Number of milliseconds.
  • MICROSECOND: Number of microseconds.
  • NANOSECOND: Number of nanoseconds.

JSON type

NameDescription
JSONRepresents JSON, a lightweight data-interchange format.

Expect these canonicalization behaviors when creating a value of JSON type:

  • Booleans, strings, and nulls are preserved exactly.
  • Whitespace characters aren't preserved.
  • A JSON value can store integers in the range of-9,223,372,036,854,775,808 (minimum signed 64-bit integer) to18,446,744,073,709,551,615 (maximum unsigned 64-bit integer) andfloating point numbers within a domain ofFLOAT64.
  • The order of elements in an array is preserved exactly.
  • The order of the members of an object is lexicographically ordered.
  • If an object has duplicate keys, the first key that's found is preserved.
  • Up to 80 levels can be nested.
  • The format of the original string representation of a JSON number may not bepreserved.

To learn more about the literal representation of a JSON type,seeJSON literals.

Numeric types

Numeric types include the following types:

  • INT64
  • NUMERIC
  • FLOAT32
  • FLOAT64

Integer type

Integers are numeric values that don't have fractional components.

NameRange
INT64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

To learn more about the literal representation of an integer type,seeInteger literals.

Decimal type

Decimal type values are numeric values with fixed decimal precision and scale.Precision is the number of digits that the number contains. Scale ishow many of these digits appear after the decimal point.

This type can represent decimal fractions exactly, and is suitable for financialcalculations.

NamePrecision, Scale, and Range
NUMERIC Precision: 38
Scale: 9
Minimum value greater than 0 that can be handled: 1e-9
Min: -9.9999999999999999999999999999999999999E+28
Max: 9.9999999999999999999999999999999999999E+28

To learn more about the literal representation of aNUMERIC type,seeNUMERIC literals.

Floating point types

Floating point values are approximate numeric values with fractional components.

NameDescription
FLOAT32 Single precision (approximate) numeric values.
FLOAT64Double precision (approximate) numeric values.

To learn more about the literal representation of a floating point type,seeFloating point literals.

Floating point semantics

When working with floating point numbers, there are special non-numeric valuesthat need to be considered:NaN and+/-inf

Note: Format the floating point special values asInfinity,-Infinity, andNaN when using the Spanner REST and RPC APIs, as documentedinTypeCode (REST) andTypeCode (RPC).The literals+inf,-inf, andnan aren't supported in theSpanner REST and RPC APIs.

Arithmetic operators provide standard IEEE-754 behavior for all finite inputvalues that produce finite output and for all operations for which at least oneinput is non-finite.

Function calls and operators return an overflow error if the input is finitebut the output would be non-finite. If the input contains non-finite values, theoutput can be non-finite. In general functions don't introduceNaNs or+/-inf. However, specific functions likeIEEE_DIVIDE can return non-finitevalues on finite input. All such cases are noted explicitly inMathematical functions.

Floating point values are approximations.

  • The binary format used to represent floating point values can only representa subset of the numbers between the most positive number and mostnegative number in the value range. This enables efficient handling of amuch larger range than would be possible otherwise.Numbers that aren't exactly representable are approximated by utilizing aclose value instead. For example,0.1 can't be represented as an integerscaled by a power of2. When this value is displayed as a string, it'srounded to a limited number of digits, and the value approximating0.1might appear as"0.1", hiding the fact that the value isn't precise.In other situations, the approximation can be visible.
  • Summation of floating point values might produce surprising results becauseoflimited precision. For example,(1e30 + 1) - 1e30 = 0, while(1e30 - 1e30) + 1 = 1.0. This isbecause the floating point value doesn't have enough precision torepresent(1e30 + 1), and the result is rounded to1e30.This example also shows that the result of theSUM aggregate function offloating points values depends on the order in which the values areaccumulated. In general, this order isn't deterministic and therefore theresult isn't deterministic. Thus, the resultingSUM offloating point values might not be deterministic and two executions of thesame query on the same tables might produce different results.
  • If the above points are concerning, use adecimal type instead.
Mathematical function examples
Left TermOperatorRight TermReturns
Any value+NaNNaN
1.0++inf+inf
1.0+-inf-inf
-inf++infNaN
MaximumFLOAT64 value+MaximumFLOAT64 valueOverflow error
MinimumFLOAT64 value/2.00.0
1.0/0.0"Divide by zero" error

Comparison operators provide standard IEEE-754 behavior for floating pointinput.

Comparison operator examples
Left TermOperatorRight TermReturns
NaN=Any valueFALSE
NaN<Any valueFALSE
Any value<NaNFALSE
-0.0=0.0TRUE
-0.0<0.0FALSE

For more information on how these values are ordered and grouped so theycan be compared,seeOrdering floating point values.

Protocol buffer type

NameDescription
PROTOAn instance of protocol buffer.

Protocol buffers provide structured data types with a defined serializationformat and cross-language support libraries. Protocol buffer message types cancontain optional, required, or repeated fields, including nested messages. Formore information, see theProtocol Buffers Developer Guide.

Protocol buffer message types behave similarly tostruct types,and support similar operations like reading field values by name. Protocolbuffer types are always named types, and can be referred to by theirfully-qualified protocol buffer name (i.e.package.ProtoName). Protocolbuffers support some additional behavior beyond structs, like default fieldvalues, defining a column type, and checking for the presence ofoptional fields.

Protocol bufferenum types are also available and can bereferenced using the fully-qualified enum type name.

To learn more about using protocol buffers in GoogleSQL, seeWork with protocol buffers.

Constructing a protocol buffer

You can construct a protocol buffer using theNEW operator ortheSELECT AS typename statement. Regardless of themethod that you choose, the resulting protocol buffer is the same.

NEW protocol_buffer {...}

You can create a protocol buffer using theNEWoperator with a map constructor:

NEWprotocol_buffer{field_name:literal_or_expressionfield_name{...}repeated_field_name:[literal_or_expression,...]}

Where:

  • protocol_buffer: The full protocol buffer name including the package name.
  • field_name: The name of a field.
  • literal_or_expression: The field value.

Example

NEWgooglesql.examples.astronomy.Planet{planet_name:'Jupiter'facts:{length_of_day:9.93distance_to_sun:5.2*ASTRONOMICAL_UNIThas_rings:TRUE}major_moons:[{moon_name:'Io'},{moon_name:'Europa'},{moon_name:'Ganymede'},{moon_name:'Callisto'}]minor_moons:(SELECTARRAY_AGG(moon_name)FROMSolarSystemMoonsWHEREplanet_name='Jupiter'ANDcircumference <3121)count_of_space_probe_photos:(GALILEO_PHOTOS+JUNO_PHOTOS+NEW_HORIZONS_PHOTOS+CASSINI_PHOTOS+ULYSSES_PHOTOS+VOYAGER_1_PHOTOS+VOYAGER_2_PHOTOS+PIONEER_10_PHOTOS+PIONEER_11_PHOTOS)}

When using this syntax, the following rules apply:

  • The field values must be expressions that are implicitly coercible orliteral-coercible to the type of the corresponding protocol buffer field.
  • Commas between fields are optional.
  • A colon is required between field name and values unless the value is a mapconstructor.
  • TheNEW protocol_buffer prefix is optional if the protocol buffer type canbe inferred from the context.
  • The type of submessages inside the map constructor can be inferred.

Examples

Simple:

SELECTkey,name,NEWgooglesql.examples.music.Chart{rank:1chart_name:'2'}

Nested messages and arrays:

SELECTNEWgooglesql.examples.music.Album{album_name:'New Moon'singer{nationality:'Canadian'residence:[{city:'Victoria'},{city:'Toronto'}]}song:['Sandstorm','Wait']}

Non-literal expressions as values:

SELECTNEWgooglesql.examples.music.Chart{rank:(SELECTCOUNT(*)FROMTableNameWHEREfoo='bar')chart_name:CONCAT('best','hits')}

The following examples infers the protocol buffer data type from context:

  • FromARRAY constructor:

    SELECTARRAY<googlesql.examples.music.Chart>[{rank:1chart_name:'2'},{rank:2chart_name:'3'}]
  • FromSTRUCT constructor:

    SELECTSTRUCT<STRING,googlesql.examples.music.Chart,INT64>('foo',{rank:1chart_name:'2'},7)[1]
  • From column names throughSET:

    • Simple column:
    UPDATETableNameSETproto_column={rank:1chart_name:'2'}
    • Array column:
    UPDATETableNameSETproto_array_column=[{rank:1chart_name:'2'},{rank:2chart_name:'3'}]
  • From generated column names inCREATE:

    CREATETABLETableName(proto_columngooglesql.examples.music.ChartAS({rank:1chart_name:'2'}))
  • From column names in default values inCREATE:

    CREATETABLETableName(proto_columngooglesql.examples.music.ChartDEFAULT({rank:1chart_name:'2'}))

NEW protocol_buffer (...)

You can create a protocol buffer using theNEW operator with aparenthesized list of arguments and aliases to specify field names:

NEWprotocol_buffer(field[ASalias],...)

Example

SELECTkey,name,NEWgooglesql.examples.music.Chart(keyASrank,nameASchart_name)FROM(SELECT1ASkey,"2"ASname);

When using this syntax, the following rules apply:

  • All field expressions must have anexplicit alias or endwith an identifier. For example, the expressiona.b.c has theimplicitaliasc.
  • NEW matches fields by alias to the field names of the protocol buffer.Aliases must be unique.
  • The expressions must be implicitly coercible or literal-coercible to thetype of the corresponding protocol buffer field.

SELECT AS typename

TheSELECT AS typename statement can produce avalue table where the row type is a specific named protocol buffer type.

Limited comparisons for protocol buffer values

Direct comparison of protocol buffers isn't supported. There are a fewalternative solutions:

  • One way to compare protocol buffers is to do a pair-wisecomparison between the fields of the protocol buffers. This can also be usedtoGROUP BY orORDER BY protocol buffer fields.
  • To get a simple approximation comparison, cast protocol buffer tostring. This applies lexicographical ordering for numeric fields.

String type

NameDescription
STRINGVariable-length character (Unicode) data.

Input string values must be UTF-8 encoded and output string values will be UTF-8encoded. Alternate encodings like CESU-8 and Modified UTF-8 aren't treated asvalid UTF-8.

All functions and operators that act on string values operate on Unicodecharacters rather than bytes. For example, functions likeSUBSTR andLENGTHapplied to string input count the number of characters, not bytes.

Each Unicode character has a numeric value called a code point assigned to it.Lower code points are assigned to lower characters. When characters arecompared, the code points determine which characters are less than or greaterthan other characters.

Most functions on strings are also defined on bytes. The bytes versionoperates on raw bytes rather than Unicode characters. Strings and bytes areseparate types that can't be used interchangeably. There is no implicit castingin either direction. Explicit casting between string and bytes doesUTF-8 encoding and decoding. Casting bytes to string returns an error if thebytes aren't valid UTF-8.

To learn more about the literal representation of a string type,seeString literals.

Struct type

Note: See details about usingSTRUCTs in theSELECT statement and insubqueries.
NameDescription
STRUCTContainer of ordered fields each with a type (required) and field name(optional).

To learn more about the literal representation of a struct type,seeStruct literals.

Declaring a struct type

STRUCT<T>

Struct types are declared using the angle brackets (< and>). The type ofthe elements of a struct can be arbitrarily complex.

Examples

Type DeclarationMeaning
STRUCT<INT64>Simple struct with a single unnamed 64-bit integer field.
STRUCT<x STRUCT<y INT64, z INT64>>A struct with a nested struct namedx inside it. The structx has two fields,y andz, both of whichare 64-bit integers.
STRUCT<inner_array ARRAY<INT64>>A struct containing an array namedinner_array that holds64-bit integer elements.

Constructing a struct

Tuple syntax

(expr1,expr2[,...])

The output type is an anonymous struct type with anonymous fields with typesmatching the types of the input expressions. There must be at least twoexpressions specified. Otherwise this syntax is indistinguishable from anexpression wrapped with parentheses.

Examples

SyntaxOutput TypeNotes
(x, x+y)STRUCT<?,?>If column names are used (unquoted strings), the struct field data type isderived from the column data type.x andy arecolumns, so the data types of the struct fields are derived from the columntypes and the output type of the addition operator.

This syntax can also be used with struct comparison for comparison expressionsusing multi-part keys, e.g., in aWHERE clause:

WHERE(Key1,Key2)IN((12,34),(56,78))

Typeless struct syntax

STRUCT(expr1[ASfield_name][,...])

Duplicate field names are allowed. Fields without names are considered anonymousfields and can't be referenced by name. struct values can beNULL, or canhaveNULL field values.

Examples

SyntaxOutput Type
STRUCT(1,2,3)STRUCT<int64,int64,int64>
STRUCT()STRUCT<>
STRUCT('abc')STRUCT<string>
STRUCT(1, t.str_col)STRUCT<int64, str_col string>
STRUCT(1 AS a, 'abc' AS b)STRUCT<a int64, b string>
STRUCT(str_col AS abc)STRUCT<abc string>

Typed struct syntax

STRUCT<[field_name]field_type,...>(expr1[,...])

Typed syntax allows constructing structs with an explicit struct data type. Theoutput type is exactly thefield_type provided. The input expression iscoerced tofield_type if the two types aren't the same, and an error isproduced if the types aren't compatible.AS alias isn't allowed on the inputexpressions. The number of expressions must match the number of fields in thetype, and the expression types must be coercible or literal-coercible to thefield types.

Examples

SyntaxOutput Type
STRUCT<int64>(5)STRUCT<int64>
STRUCT<date>("2011-05-05")STRUCT<date>
STRUCT<x int64, y string>(1, t.str_col)STRUCT<x int64, y string>
STRUCT<int64>(int_col)STRUCT<int64>
STRUCT<x int64>(5 AS x)Error - Typed syntax doesn't allowAS

Limited comparisons for structs

Structs can be directly compared using equality operators:

  • Equal (=)
  • Not Equal (!= or<>)
  • [NOT]IN

Notice, though, that these direct equality comparisons compare the fields ofthe struct pairwise in ordinal order ignoring any field names. If instead youwant to compare identically named fields of a struct, you can compare theindividual fields directly.

Timestamp type

NameRange
TIMESTAMP0001-01-01 00:00:00 to 9999-12-31 23:59:59.999999999 UTC.

A timestamp value represents an absolute point in time,independent of any time zone or convention such as daylight saving time (DST),withnanosecondprecision.

A timestamp is typically represented internally as the number of elapsed nanoseconds since a fixed initial point in time.

Note that a timestamp itself doesn't have a time zone; it represents the sameinstant in time globally. However, thedisplay of a timestamp for humanreadability usually includes a Gregorian date, a time, and a time zone, in animplementation-dependent format. For example, the displayed values "2020-01-0100:00:00 UTC", "2019-12-31 19:00:00 America/New_York", and "2020-01-01 05:30:00Asia/Kolkata" all represent the same instant in time and therefore represent thesame timestamp value.

  • To represent a Gregorian date as it might appear on a calendar(a civil date), use adate value.
Canonical format

For Rest and RPC APIs

Follow the rules for encoding to and decoding from JSON values as described inTypeCode (RPC) andTypeCode (REST). Inparticular, the timestamp value must end with an uppercase literal "Z" tospecify Zulu time (UTC-0).

For example:

2014-09-27T12:30:00.45Z

Timestamp values must be expressed in Zulu time and can't includea UTC offset. For example, the following timestamp isn't supported:

-- NOT SUPPORTED! TIMESTAMPS CANNOT INCLUDE A UTC OFFSET WHEN USED WITH THE REST AND RPC APIS2014-09-2712:30:00.45-8:00

For client libraries

Use the language-specific timestamp format.

For SQL queries

The canonical format for a timestamp literal has the following parts:

{civil_date_part[time_part[time_zone]]|civil_date_part[time_part[time_zone_offset]]|civil_date_part[time_part[utc_time_zone]]}civil_date_part:YYYY-[M]M-[D]Dtime_part:{|T|t}[H]H:[M]M:[S]S[.F]
  • YYYY: Four-digit year.
  • [M]M: One or two digit month.
  • [D]D: One or two digit day.
  • { |T|t}: A space or aT ort separator. TheT andtseparators are flags for time.
  • [H]H: One or two digit hour (valid values from 00 to 23).
  • [M]M: One or two digit minutes (valid values from 00 to 59).
  • [S]S: One or two digit seconds (valid values from 00 to 60).
  • [.F]: Up to ninefractional digits (nanosecond precision).
  • [time_zone]: String representing the time zone. When a timezone isn't explicitly specified, the default time zone,America/Los_Angeles, is used. For details, seetimezones.
  • [time_zone_offset]: String representing the offset from theCoordinated Universal Time (UTC) time zone. For details, seetime zones.
  • [utc_time_zone]: String representing the Coordinated UniversalTime (UTC), usually the letterZ orz. For details, seetime zones.

To learn more about the literal representation of a timestamp type,seeTimestamp literals.

Time zones

A time zone is used when converting from a civil date or time (as might appearon a calendar or clock) to a timestamp (an absolute time), or vice versa. Thisincludes the operation of parsing a string containing a civil date and time like"2020-01-01 00:00:00" and converting it to a timestamp. The resulting timestampvalue itself doesn't store a specific time zone, because it represents oneinstant in time globally.

Time zones are represented by strings in one of these canonical formats:

  • Offset from Coordinated Universal Time (UTC), or the letterZ orz forUTC.
  • Time zone name from thetz database.

The following timestamps are identical because the time zone offsetforAmerica/Los_Angeles is-08 for the specified date and time.

SELECTUNIX_MILLIS(TIMESTAMP'2008-12-25 15:30:00 America/Los_Angeles')ASmillis;
SELECTUNIX_MILLIS(TIMESTAMP'2008-12-25 15:30:00-08:00')ASmillis;

Specify Coordinated Universal Time (UTC)

You can specify UTC using the following suffix:

{Z|z}

You can also specify UTC using the following time zone name:

{Etc/UTC}

TheZ suffix is a placeholder that implies UTC when converting anRFC3339-format value to aTIMESTAMP value. The valueZ isn'ta valid time zone for functions that accept a time zone. If you're specifying atime zone, or you're unsure of the format to use to specify UTC, we recommendusing theEtc/UTC time zone name.

TheZ suffix isn't case sensitive. When using theZ suffix, no space isallowed between theZ and the rest of the timestamp. The following areexamples of using theZ suffix and theEtc/UTC time zone name:

SELECTTIMESTAMP'2014-09-27T12:30:00.45Z'SELECTTIMESTAMP'2014-09-27 12:30:00.45z'SELECTTIMESTAMP'2014-09-27T12:30:00.45 Etc/UTC'

Specify an offset from Coordinated Universal Time (UTC)

You can specify the offset from UTC using the following format:

{+|-}H[H][:M[M]]

Examples:

-08:00-8:15+3:00+07:30-7

When using this format, no space is allowed between the time zone and the restof the timestamp.

2014-09-2712:30:00.45-8:00

Time zone name

Format:

tz_identifier

A time zone name is a tz identifier from thetz database.For a less comprehensive but simpler reference, see theList of tz database time zones on Wikipedia.

Examples:

America/Los_AngelesAmerica/Argentina/Buenos_AiresEtc/UTCPacific/Auckland

When using a time zone name, a space is required between the name and the restof the timestamp:

2014-09-2712:30:00.45America/Los_Angeles

Note that not all time zone names are interchangeable even if they do happen toreport the same time during a given part of the year. For example,America/Los_Angeles reports the same time asUTC-7:00 during daylightsaving time (DST), but reports the same time asUTC-8:00 outside of DST.

If a time zone isn't specified, the default time zone value is used.

Leap seconds

A timestamp is simply an offset from 1970-01-01 00:00:00 UTC, assuming there areexactly 60 seconds per minute. Leap seconds aren't represented as part of astored timestamp.

If the input contains values that use ":60" in the seconds field to represent aleap second, that leap second isn't preserved when converting to a timestampvalue. Instead that value is interpreted as a timestamp with ":00" in theseconds field of the following minute.

Leap seconds don't affect timestamp computations. All timestamp computationsare done using Unix-style timestamps, which don't reflect leap seconds. Leapseconds are only observable through functions that measure real-world time. Inthese functions, it's possible for a timestamp second to be skipped or repeatedwhen there is a leap second.

Daylight saving time

A timestamp is unaffected by daylight saving time (DST) because it represents apoint in time. When you display a timestamp as a civil time,with a timezone that observes DST, the following rules apply:

  • During the transition from standard time to DST, one hour is skipped. Acivil time from the skipped hour is treated the same as if it were writtenan hour later. For example, in theAmerica/Los_Angeles time zone, the hourbetween 2 AM and 3 AM on March 10, 2024 is skipped on a clock. The times2:30 AM and 3:30 AM on that date are treated as the same point in time:

    SELECTFORMAT_TIMESTAMP("%c %Z","2024-03-10 02:30:00 America/Los_Angeles","UTC")AStwo_thirty,FORMAT_TIMESTAMP("%c %Z","2024-03-10 03:30:00 America/Los_Angeles","UTC")ASthree_thirty;/*------------------------------+------------------------------+ | two_thirty                   | three_thirty                 | +------------------------------+------------------------------+ | Sun Mar 10 10:30:00 2024 UTC | Sun Mar 10 10:30:00 2024 UTC | +------------------------------+------------------------------*/
  • When there's ambiguity in how to represent a civil time in a particulartimezone because of DST, the later time is chosen:

    SELECTFORMAT_TIMESTAMP("%c %Z","2024-03-10 10:30:00 UTC","America/Los_Angeles")asten_thirty;/*--------------------------------+ | ten_thirty                     | +--------------------------------+ | Sun Mar 10 03:30:00 2024 UTC-7 | +--------------------------------*/
  • During the transition from DST to standard time, one hour is repeated. Acivil time that shows a time during that hour is treated as if it's theearlier instance of that time. For example, in theAmerica/Los_Angeles timezone, the hour between 1 AM and 2 AM on November 3, 2024, is repeated on aclock. The time 1:30 AM on that date is treated as the earlier (DST) instanceof that time.

    SELECTFORMAT_TIMESTAMP("%c %Z","2024-11-03 01:30:00 America/Los_Angeles","UTC")asone_thirty,FORMAT_TIMESTAMP("%c %Z","2024-11-03 02:30:00 America/Los_Angeles","UTC")astwo_thirty;/*------------------------------+------------------------------+ | one_thirty                   | two_thirty                   | +------------------------------+------------------------------+ | Sun Nov 3 08:30:00 2024 UTC  | Sun Nov 3 10:30:00 2024 UTC  | +------------------------------+------------------------------*/

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.