Beam ZetaSQL operators

Operators are represented by special characters or keywords; they do not use function call syntax. An operator manipulates any number of data inputs, also called operands, and returns a result.

Common conventions:

The following table lists all supported operators from highest to lowest precedence. Precedence determines the order in which operators will be evaluated within a statement.

Order of PrecedenceOperatorInput Data TypesNameOperator Arity
1.STRUCT
Member field access operatorBinary
 [ ]ARRAYArray position. Must be used with OFFSET or ORDINAL&mdash.Binary
2-All numeric typesUnary minusUnary
3*All numeric typesMultiplicationBinary
 /All numeric typesDivisionBinary
4+All numeric typesAdditionBinary
 -All numeric typesSubtractionBinary
5 (Comparison Operators)=Any comparable type. See Data Types for a complete list.EqualBinary
 <Any comparable type. See Data Types for a complete list.Less thanBinary
 >Any comparable type. See Data Types for a complete list.Greater thanBinary
 <=Any comparable type. See Data Types for a complete list.Less than or equal toBinary
 >=Any comparable type. See Data Types for a complete list.Greater than or equal toBinary
 !=, <>Any comparable type. See Data Types for a complete list.Not equalBinary
 [NOT] LIKESTRING and byteValue does [not] match the pattern specifiedBinary
 [NOT] BETWEENAny comparable types. See Data Types for list.Value is [not] within the range specifiedBinary
 [NOT] INAny comparable types. See Data Types for list.Value is [not] in the set of values specifiedBinary
 IS [NOT] NULLAllValue is [not] NULLUnary
 IS [NOT] TRUEBOOLValue is [not] TRUE.Unary
 IS [NOT] FALSEBOOLValue is [not] FALSE.Unary
6NOTBOOLLogical NOTUnary
7ANDBOOLLogical ANDBinary
8ORBOOLLogical ORBinary

Operators with the same precedence are left associative. This means that those operators are grouped together starting from the left and moving right. For example, the expression:

x AND y AND z

is interpreted as

( ( x AND y ) AND z )

The expression:

x * y / z

is interpreted as:

( ( x * y ) / z )

All comparison operators have the same priority and are grouped using left associativity. However, comparison operators are not associative. As a result, it is recommended that you use parentheses to improve readability and ensure expressions are resolved as desired. For example:

(x < y) IS FALSE

is recommended over:

x < y IS FALSE

Element access operators

OperatorSyntaxInput Data TypesResult Data TypeDescription
.expression.fieldname1...STRUCT
Type T stored in fieldname1Dot operator. Can be used to access nested fields, e.g.expression.fieldname1.fieldname2...
[ ]array_expression [position_keyword (int_expression ) ]See ARRAY Functions.Type T stored in ARRAYposition_keyword is either OFFSET or ORDINAL.

Arithmetic operators

All arithmetic operators accept input of numeric type T, and the result type has type T unless otherwise indicated in the description below:

NameSyntax
AdditionX + Y
SubtractionX - Y
MultiplicationX * Y
DivisionX / Y
Unary Minus- X

Result types for Addition and Multiplication:

 INT64FLOAT64
INT64INT64FLOAT64
FLOAT64FLOAT64FLOAT64

Result types for Subtraction:

 INT64FLOAT64
INT64INT64FLOAT64
FLOAT64FLOAT64FLOAT64

Result types for Division:

 INT64FLOAT64
INT64FLOAT64FLOAT64
FLOAT64FLOAT64FLOAT64

Result types for Unary Minus:

Input Data TypeResult Data Type
INT64INT64
FLOAT64FLOAT64

Logical operators

All logical operators allow only BOOL input.

NameSyntaxDescription
Logical NOTNOT XReturns FALSE if input is TRUE. Returns TRUE if input is FALSE. Returns NULL otherwise.
Logical ANDX AND YReturns FALSE if at least one input is FALSE. Returns TRUE if both X and Y are TRUE. Returns NULL otherwise.
Logical ORX OR YReturns FALSE if both X and Y are FALSE. Returns TRUE if at least one input is TRUE. Returns NULL otherwise.

Comparison operators

Comparisons always return BOOL. Comparisons generally require both operands to be of the same type. If operands are of different types, and if Cloud Dataflow SQL can convert the values of those types to a common type without loss of precision, Cloud Dataflow SQL will generally coerce them to that common type for the comparison; Cloud Dataflow SQL will generally coerce literals to the type of non-literals, where present. Comparable data types are defined in Data Types.

STRUCTs support only 4 comparison operators: equal (=), not equal (!= and <>), and IN.

The following rules apply when comparing these data types:

NameSyntaxDescription
Less ThanX < YReturns TRUE if X is less than Y.
Less Than or Equal ToX <= YReturns TRUE if X is less than or equal to Y.
Greater ThanX > YReturns TRUE if X is greater than Y.
Greater Than or Equal ToX >= YReturns TRUE if X is greater than or equal to Y.
EqualX = YReturns TRUE if X is equal to Y.
Not EqualX != Y
X <> Y
Returns TRUE if X is not equal to Y.
BETWEENX [NOT] BETWEEN Y AND ZReturns TRUE if X is [not] within the range specified. The result of "X BETWEEN Y AND Z" is equivalent to "Y <= X AND X <= Z" but X is evaluated only once in the former.
LIKEX [NOT] LIKE YChecks if the STRING in the first operand X matches a pattern specified by the second operand Y. Expressions can contain these characters:
  • A percent sign "%" matches any number of characters or bytes
  • An underscore "_" matches a single character or byte
  • You can escape "\", "_", or "%" using two backslashes. For example, "\\%". If you are using raw strings, only a single backslash is required. For example, r"\%".
INMultiple - see belowReturns FALSE if the right operand is empty. Returns NULL if the left operand is NULL. Returns TRUE or NULL, never FALSE, if the right operand contains NULL. Arguments on either side of IN are general expressions. Neither operand is required to be a literal, although using a literal on the right is most common. X is evaluated only once.

When testing values that have a STRUCT data type for equality, it’s possible that one or more fields are NULL. In such cases:

The following table demonstrates how STRUCT data types are compared when they have fields that are NULL valued.

Struct1Struct2Struct1 = Struct2
STRUCT(1, NULL)STRUCT(1, NULL)NULL
STRUCT(1, NULL)STRUCT(2, NULL)FALSE
STRUCT(1,2)STRUCT(1, NULL)NULL

IS operators

IS operators return TRUE or FALSE for the condition they are testing. They never return NULL, even for NULL inputs. If NOT is present, the output BOOL value is inverted.

Function SyntaxInput Data TypeResult Data TypeDescription
X IS [NOT] NULL
Any value typeBOOLReturns TRUE if the operand X evaluates to NULL, and returns FALSE otherwise.
X IS [NOT] TRUE
BOOLBOOLReturns TRUE if the BOOL operand evaluates to TRUE. Returns FALSE otherwise.
X IS [NOT] FALSE
BOOLBOOLReturns TRUE if the BOOL operand evaluates to FALSE. Returns FALSE otherwise.