Mathematical functions

GoogleSQL for BigQuery supports mathematical functions.All mathematical functions have the following behaviors:

  • They returnNULL if any of the input parameters isNULL.
  • They returnNaN if any of the arguments isNaN.

Categories

CategoryFunctions
TrigonometricACOS  ACOSH  ASIN  ASINH  ATAN  ATAN2  ATANH  COS  COSH  COT  COTH  CSC  CSCH  SEC  SECH  SIN  SINH  TAN  TANH  
Exponential and
logarithmic
EXP  LN  LOG  LOG10  
Rounding and
truncation
CEIL  CEILING  FLOOR  ROUND  TRUNC  
Power and
root
CBRT  POW  POWER  SQRT  
SignABS  SIGN  
DistanceCOSINE_DISTANCE  EUCLIDEAN_DISTANCE  
ComparisonGREATEST  LEAST  
Random number generatorRAND  
Arithmetic and error handlingDIV  IEEE_DIVIDE  IS_INF  IS_NAN  MOD  SAFE_ADD  SAFE_DIVIDE  SAFE_MULTIPLY  SAFE_NEGATE  SAFE_SUBTRACT  
BucketRANGE_BUCKET  

Function list

NameSummary
ABS Computes the absolute value ofX.
ACOS Computes the inverse cosine ofX.
ACOSH Computes the inverse hyperbolic cosine ofX.
ASIN Computes the inverse sine ofX.
ASINH Computes the inverse hyperbolic sine ofX.
ATAN Computes the inverse tangent ofX.
ATAN2 Computes the inverse tangent ofX/Y, using the signs ofX andY to determine the quadrant.
ATANH Computes the inverse hyperbolic tangent ofX.
AVG Gets the average of non-NULL values.
For more information, seeAggregate functions.
AVG (Differential Privacy)DIFFERENTIAL_PRIVACY-supportedAVG.

Gets the differentially-private average of non-NULL, non-NaN values in a query with aDIFFERENTIAL_PRIVACY clause.

For more information, seeDifferential privacy functions.
CBRT Computes the cube root ofX.
CEIL Gets the smallest integral value that isn't less thanX.
CEILING Synonym ofCEIL.
COS Computes the cosine ofX.
COSH Computes the hyperbolic cosine ofX.
COSINE_DISTANCEComputes the cosine distance between two vectors.
COT Computes the cotangent ofX.
COTH Computes the hyperbolic cotangent ofX.
CSC Computes the cosecant ofX.
CSCH Computes the hyperbolic cosecant ofX.
DIV Divides integerX by integerY.
EXP Computese to the power ofX.
EUCLIDEAN_DISTANCEComputes the Euclidean distance between two vectors.
FLOOR Gets the largest integral value that isn't greater thanX.
GREATEST Gets the greatest value amongX1,...,XN.
IEEE_DIVIDE DividesX byY, but doesn't generate errors for division by zero or overflow.
IS_INF Checks ifX is positive or negative infinity.
IS_NAN Checks ifX is aNaN value.
LEAST Gets the least value amongX1,...,XN.
LN Computes the natural logarithm ofX.
LOG Computes the natural logarithm ofX or the logarithm ofX to baseY.
LOG10 Computes the natural logarithm ofX to base 10.
MAX Gets the maximum non-NULL value.
For more information, seeAggregate functions.
MAX_BY Synonym forANY_VALUE(x HAVING MAX y).
For more information, seeAggregate functions.
MIN_BY Synonym forANY_VALUE(x HAVING MIN y).
For more information, seeAggregate functions.
MOD Gets the remainder of the division ofX byY.
POW Produces the value ofX raised to the power ofY.
POWER Synonym ofPOW.
RAND Generates a pseudo-random value of typeFLOAT64 in the range of[0, 1).
RANGE_BUCKET Scans through a sorted array and returns the 0-based position of a point's upper bound.
ROUND RoundsX to the nearest integer or roundsX toN decimal places after the decimal point.
SAFE_ADD Equivalent to the addition operator (X + Y), but returnsNULL if overflow occurs.
SAFE_DIVIDE Equivalent to the division operator (X / Y), but returnsNULL if an error occurs.
SAFE_MULTIPLY Equivalent to the multiplication operator (X * Y), but returnsNULL if overflow occurs.
SAFE_NEGATE Equivalent to the unary minus operator (-X), but returnsNULL if overflow occurs.
SAFE_SUBTRACT Equivalent to the subtraction operator (X - Y), but returnsNULL if overflow occurs.
SEC Computes the secant ofX.
SECH Computes the hyperbolic secant ofX.
SIGN Produces -1 , 0, or +1 for negative, zero, and positive arguments respectively.
SIN Computes the sine ofX.
SINH Computes the hyperbolic sine ofX.
SQRT Computes the square root ofX.
SUM Gets the sum of non-NULL values.
For more information, seeAggregate functions.
SUM (Differential Privacy)DIFFERENTIAL_PRIVACY-supportedSUM.

Gets the differentially-private sum of non-NULL, non-NaN values in a query with aDIFFERENTIAL_PRIVACY clause.

For more information, seeDifferential privacy functions.
TAN Computes the tangent ofX.
TANH Computes the hyperbolic tangent ofX.
TRUNC Rounds a number likeROUND(X) orROUND(X, N), but always rounds towards zero and never overflows.

ABS

ABS(X)

Description

Computes absolute value. Returns an error if the argument is an integer and theoutput value can't be represented as the same type; this happens only for thelargest negative input value, which has no positive representation.

XABS(X)
2525
-2525
+inf+inf
-inf+inf

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTINT64NUMERICBIGNUMERICFLOAT64

ACOS

ACOS(X)

Description

Computes the principal value of the inverse cosine of X. The return value is inthe range [0,π]. Generates an error if X is a value outside of therange [-1, 1].

XACOS(X)
+infNaN
-infNaN
NaNNaN
X < -1Error
X > 1Error

ACOSH

ACOSH(X)

Description

Computes the inverse hyperbolic cosine of X. Generates an error if X is a valueless than 1.

XACOSH(X)
+inf+inf
-infNaN
NaNNaN
X < 1Error

ASIN

ASIN(X)

Description

Computes the principal value of the inverse sine of X. The return value is inthe range [-π/2,π/2]. Generates an error if X is outside ofthe range [-1, 1].

XASIN(X)
+infNaN
-infNaN
NaNNaN
X < -1Error
X > 1Error

ASINH

ASINH(X)

Description

Computes the inverse hyperbolic sine of X. Doesn't fail.

XASINH(X)
+inf+inf
-inf-inf
NaNNaN

ATAN

ATAN(X)

Description

Computes the principal value of the inverse tangent of X. The return value isin the range [-π/2,π/2]. Doesn't fail.

XATAN(X)
+infπ/2
-inf-π/2
NaNNaN

ATAN2

ATAN2(X,Y)

Description

Calculates the principal value of the inverse tangent of X/Y using the signs ofthe two arguments to determine the quadrant. The return value is in the range[-π,π].

XYATAN2(X, Y)
NaNAny valueNaN
Any valueNaNNaN
0.00.00.0
Positive Finite value-infπ
Negative Finite value-inf
Finite value+inf0.0
+infFinite valueπ/2
-infFinite value-π/2
+inf-inf¾π
-inf-inf-¾π
+inf+infπ/4
-inf+inf-π/4

ATANH

ATANH(X)

Description

Computes the inverse hyperbolic tangent of X. Generates an error if X is outsideof the range (-1, 1).

XATANH(X)
+infNaN
-infNaN
NaNNaN
X < -1Error
X > 1Error

CBRT

CBRT(X)

Description

Computes the cube root ofX.X can be any data typethatcoerces toFLOAT64.Supports theSAFE. prefix.

XCBRT(X)
+infinf
-inf-inf
NaNNaN
00
NULLNULL

Return Data Type

FLOAT64

Example

SELECTCBRT(27)AScube_root;/*--------------------* | cube_root          | +--------------------+ | 3.0000000000000004 | *--------------------*/

CEIL

CEIL(X)

Description

Returns the smallest integral value that isn't less than X.

XCEIL(X)
2.02.0
2.33.0
2.83.0
2.53.0
-2.3-2.0
-2.8-2.0
-2.5-2.0
00
+inf+inf
-inf-inf
NaNNaN

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

CEILING

CEILING(X)

Description

Synonym of CEIL(X)

COS

COS(X)

Description

Computes the cosine of X where X is specified in radians. Never fails.

XCOS(X)
+infNaN
-infNaN
NaNNaN

COSH

COSH(X)

Description

Computes the hyperbolic cosine of X where X is specified in radians.Generates an error if overflow occurs.

XCOSH(X)
+inf+inf
-inf+inf
NaNNaN

COSINE_DISTANCE

COSINE_DISTANCE(vector1,vector2)

Description

Computes thecosine distance between two vectors.

Definitions

  • vector1: A vector that's represented by anARRAY<T> value or a sparse vector that isrepresented by anARRAY<STRUCT<dimension,magnitude>> value.
  • vector2: A vector that's represented by anARRAY<T> value or a sparse vector that isrepresented by anARRAY<STRUCT<dimension,magnitude>> value.

Details

  • ARRAY<T> can be used to represent a vector. Each zero-based index in thisarray represents a dimension. The value for each element in this arrayrepresents a magnitude.

    T can represent the following and must be the same for bothvectors:

    • FLOAT64

    In the following example vector, there are four dimensions. The magnitudeis10.0 for dimension0,55.0 for dimension1,40.0 fordimension2, and34.0 for dimension3:

    [10.0,55.0,40.0,34.0]
  • ARRAY<STRUCT<dimension,magnitude>> can be used to represent asparse vector. With a sparse vector, you only need to includedimension-magnitude pairs for non-zero magnitudes. If a magnitude isn'tpresent in the sparse vector, the magnitude is implicitly understood to bezero.

    For example, if you have a vector with 10,000 dimensions, but only 10dimensions have non-zero magnitudes, then the vector is a sparse vector.As a result, it's more efficient to describe a sparse vector by onlymentioning its non-zero magnitudes.

    InARRAY<STRUCT<dimension,magnitude>>,STRUCT<dimension,magnitude>represents a dimension-magnitude pair for each non-zero magnitude in asparse vector. These parts need to be included for each dimension-magnitudepair:

    • dimension: ASTRING orINT64 value that represents adimension in a vector.

    • magnitude: AFLOAT64 value that represents anon-zero magnitude for a specific dimension in a vector.

    You don't need to include empty dimension-magnitude pairs in asparse vector. For example, the following sparse vector andnon-sparse vector are equivalent:

    -- sparse vector ARRAY<STRUCT<INT64, FLOAT64>>[(1,10.0),(2,30.0),(5,40.0)]
    -- vector ARRAY<FLOAT64>[0.0,10.0,30.0,0.0,0.0,40.0]

    In a sparse vector, dimension-magnitude pairs don't need to be in anyparticular order. The following sparse vectors are equivalent:

    [('a',10.0),('b',30.0),('d',40.0)]
    [('d',40.0),('a',10.0),('b',30.0)]
  • Both non-sparse vectorsin this function must share the same dimensions, and if they don't, an erroris produced.

  • A vector can't be a zero vector. A vector is a zero vector if it hasno dimensions or all dimensions have a magnitude of0, such as[] or[0.0, 0.0]. If a zero vector is encountered, an error is produced.

  • An error is produced if a magnitude in a vector isNULL.

  • If a vector isNULL,NULL is returned.

Return type

FLOAT64

Examples

In the following example, non-sparsevectorsare used to compute the cosine distance:

SELECTCOSINE_DISTANCE([1.0,2.0],[3.0,4.0])ASresults;/*----------* | results  | +----------+ | 0.016130 | *----------*/

In the following example, sparse vectors are used to compute thecosine distance:

SELECTCOSINE_DISTANCE([(1,1.0),(2,2.0)],[(2,4.0),(1,3.0)])ASresults;/*----------*  | results  |  +----------+  | 0.016130 |  *----------*/

The ordering of numeric values in a vector doesn't impact the resultsproduced by this function. For example these queries produce the same resultseven though the numeric values in each vector is in a different order:

SELECTCOSINE_DISTANCE([1.0,2.0],[3.0,4.0])ASresults;
SELECTCOSINE_DISTANCE([2.0,1.0],[4.0,3.0])ASresults;
SELECTCOSINE_DISTANCE([(1,1.0),(2,2.0)],[(1,3.0),(2,4.0)])ASresults;
/*----------*  | results  |  +----------+  | 0.016130 |  *----------*/

In the following example, the function can't compute cosine distance againstthe first vector, which is a zero vector:

-- ERRORSELECTCOSINE_DISTANCE([0.0,0.0],[3.0,4.0])ASresults;
-- ERRORSELECTCOSINE_DISTANCE([(1,0.0),(2,0.0)],[(1,3.0),(2,4.0)])ASresults;

Both non-sparse vectors must have the samedimensions. If not, an error is produced. In the following example, thefirst vector has two dimensions and the second vector has three:

-- ERRORSELECTCOSINE_DISTANCE([9.0,7.0],[8.0,4.0,5.0])ASresults;

If you use sparse vectors and you repeat a dimension, an error isproduced:

-- ERRORSELECTCOSINE_DISTANCE([(1,9.0),(2,7.0),(2,8.0)],[(1,8.0),(2,4.0),(3,5.0)])ASresults;

COT

COT(X)

Description

Computes the cotangent for the angle ofX, whereX is specified in radians.X can be any data typethatcoerces toFLOAT64.Supports theSAFE. prefix.

XCOT(X)
+infNaN
-infNaN
NaNNaN
0Error
NULLNULL

Return Data Type

FLOAT64

Example

SELECTCOT(1)ASa,SAFE.COT(0)ASb;/*---------------------+------* | a                   | b    | +---------------------+------+ | 0.64209261593433065 | NULL | *---------------------+------*/

COTH

COTH(X)

Description

Computes the hyperbolic cotangent for the angle ofX, whereX is specifiedin radians.X can be any data typethatcoerces toFLOAT64.Supports theSAFE. prefix.

XCOTH(X)
+inf1
-inf-1
NaNNaN
0Error
NULLNULL

Return Data Type

FLOAT64

Example

SELECTCOTH(1)ASa,SAFE.COTH(0)ASb;/*----------------+------* | a              | b    | +----------------+------+ | 1.313035285499 | NULL | *----------------+------*/

CSC

CSC(X)

Description

Computes the cosecant of the input angle, which is in radians.X can be any data typethatcoerces toFLOAT64.Supports theSAFE. prefix.

XCSC(X)
+infNaN
-infNaN
NaNNaN
0Error
NULLNULL

Return Data Type

FLOAT64

Example

SELECTCSC(100)ASa,CSC(-1)ASb,SAFE.CSC(0)ASc;/*----------------+-----------------+------* | a              | b               | c    | +----------------+-----------------+------+ | -1.97485753142 | -1.188395105778 | NULL | *----------------+-----------------+------*/

CSCH

CSCH(X)

Description

Computes the hyperbolic cosecant of the input angle, which is in radians.X can be any data typethatcoerces toFLOAT64.Supports theSAFE. prefix.

XCSCH(X)
+inf0
-inf0
NaNNaN
0Error
NULLNULL

Return Data Type

FLOAT64

Example

SELECTCSCH(0.5)ASa,CSCH(-2)ASb,SAFE.CSCH(0)ASc;/*----------------+----------------+------* | a              | b              | c    | +----------------+----------------+------+ | 1.919034751334 | -0.27572056477 | NULL | *----------------+----------------+------*/

DIV

DIV(X,Y)

Description

Returns the result of integer division of X by Y. Division by zero returnsan error. Division by -1 may overflow.

XYDIV(X, Y)
2045
12-7-1
2036
0200
200Error

Return Data Type

The return data type is determined by the argument types with the followingtable.

INPUTINT64NUMERICBIGNUMERIC
INT64INT64NUMERICBIGNUMERIC
NUMERICNUMERICNUMERICBIGNUMERIC
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERIC

EXP

EXP(X)

Description

Computese to the power of X, also called the natural exponential function. Ifthe result underflows, this function returns a zero. Generates an error if theresult overflows.

XEXP(X)
0.01.0
+inf+inf
-inf0.0

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

EUCLIDEAN_DISTANCE

EUCLIDEAN_DISTANCE(vector1,vector2)

Description

Computes theEuclidean distance between two vectors.

Definitions

  • vector1: A vector that's represented by anARRAY<T> value or a sparse vector that isrepresented by anARRAY<STRUCT<dimension,magnitude>> value.
  • vector2: A vector that's represented by anARRAY<T> value or a sparse vector that isrepresented by anARRAY<STRUCT<dimension,magnitude>> value.

Details

  • ARRAY<T> can be used to represent a vector. Each zero-based index in thisarray represents a dimension. The value for each element in this arrayrepresents a magnitude.

    T can represent the following and must be the same for bothvectors:

    • FLOAT64

    In the following example vector, there are four dimensions. The magnitudeis10.0 for dimension0,55.0 for dimension1,40.0 fordimension2, and34.0 for dimension3:

    [10.0,55.0,40.0,34.0]
  • ARRAY<STRUCT<dimension,magnitude>> can be used to represent asparse vector. With a sparse vector, you only need to includedimension-magnitude pairs for non-zero magnitudes. If a magnitude isn'tpresent in the sparse vector, the magnitude is implicitly understood to bezero.

    For example, if you have a vector with 10,000 dimensions, but only 10dimensions have non-zero magnitudes, then the vector is a sparse vector.As a result, it's more efficient to describe a sparse vector by onlymentioning its non-zero magnitudes.

    InARRAY<STRUCT<dimension,magnitude>>,STRUCT<dimension,magnitude>represents a dimension-magnitude pair for each non-zero magnitude in asparse vector. These parts need to be included for each dimension-magnitudepair:

    • dimension: ASTRING orINT64 value that represents adimension in a vector.

    • magnitude: AFLOAT64 value that represents anon-zero magnitude for a specific dimension in a vector.

    You don't need to include empty dimension-magnitude pairs in asparse vector. For example, the following sparse vector andnon-sparse vector are equivalent:

    -- sparse vector ARRAY<STRUCT<INT64, FLOAT64>>[(1,10.0),(2,30.0),(5,40.0)]
    -- vector ARRAY<FLOAT64>[0.0,10.0,30.0,0.0,0.0,40.0]

    In a sparse vector, dimension-magnitude pairs don't need to be in anyparticular order. The following sparse vectors are equivalent:

    [('a',10.0),('b',30.0),('d',40.0)]
    [('d',40.0),('a',10.0),('b',30.0)]
  • Both non-sparse vectorsin this function must share the same dimensions, and if they don't, an erroris produced.

  • A vector can be a zero vector. A vector is a zero vector if it hasno dimensions or all dimensions have a magnitude of0, such as[] or[0.0, 0.0].

  • An error is produced if a magnitude in a vector isNULL.

  • If a vector isNULL,NULL is returned.

Return type

FLOAT64

Examples

In the following example, non-sparse vectorsare used to compute the Euclidean distance:

SELECTEUCLIDEAN_DISTANCE([1.0,2.0],[3.0,4.0])ASresults;/*----------* | results  | +----------+ | 2.828    | *----------*/

In the following example, sparse vectors are used to compute theEuclidean distance:

SELECTEUCLIDEAN_DISTANCE([(1,1.0),(2,2.0)],[(2,4.0),(1,3.0)])ASresults;/*----------*  | results  |  +----------+  | 2.828    |  *----------*/

The ordering of magnitudes in a vector doesn't impact the resultsproduced by this function. For example these queries produce the same resultseven though the magnitudes in each vector is in a different order:

SELECTEUCLIDEAN_DISTANCE([1.0,2.0],[3.0,4.0]);
SELECTEUCLIDEAN_DISTANCE([2.0,1.0],[4.0,3.0]);
SELECTEUCLIDEAN_DISTANCE([(1,1.0),(2,2.0)],[(1,3.0),(2,4.0)])ASresults;
/*----------*  | results  |  +----------+  | 2.828    |  *----------*/

Both non-sparse vectors must have the samedimensions. If not, an error is produced. In the following example, the firstvector has two dimensions and the second vector has three:

-- ERRORSELECTEUCLIDEAN_DISTANCE([9.0,7.0],[8.0,4.0,5.0])ASresults;

If you use sparse vectors and you repeat a dimension, an error isproduced:

-- ERRORSELECTEUCLIDEAN_DISTANCE([(1,9.0),(2,7.0),(2,8.0)],[(1,8.0),(2,4.0),(3,5.0)])ASresults;

FLOOR

FLOOR(X)

Description

Returns the largest integral value that isn't greater than X.

XFLOOR(X)
2.02.0
2.32.0
2.82.0
2.52.0
-2.3-3.0
-2.8-3.0
-2.5-3.0
00
+inf+inf
-inf-inf
NaNNaN

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

GREATEST

GREATEST(X1,...,XN)

Description

Returns the greatest value amongX1,...,XN. If any argument isNULL, returnsNULL. Otherwise, in the case of floating-point arguments, if any argument isNaN, returnsNaN. In all other cases, returns the value amongX1,...,XNthat has the greatest value according to the ordering used by theORDER BYclause. The argumentsX1, ..., XN must be coercible to a common supertype, andthe supertype must support ordering.

X1,...,XNGREATEST(X1,...,XN)
3,5,15

This function supports specifyingcollation.

Return Data Types

Data type of the input values.

IEEE_DIVIDE

IEEE_DIVIDE(X,Y)

Description

Divides X by Y; this function never fails. ReturnsFLOAT64. Unlike the division operator (/),this function doesn't generate errors for division by zero or overflow.

XYIEEE_DIVIDE(X, Y)
20.04.05.0
0.025.00.0
25.00.0+inf
-25.00.0-inf
25.0-0.0-inf
0.00.0NaN
0.0NaNNaN
NaN0.0NaN
+inf+infNaN
-inf-infNaN

IS_INF

IS_INF(X)

Description

ReturnsTRUE if the value is positive or negative infinity.

XIS_INF(X)
+infTRUE
-infTRUE
25FALSE

IS_NAN

IS_NAN(X)

Description

ReturnsTRUE if the value is aNaN value.

XIS_NAN(X)
NaNTRUE
25FALSE

LEAST

LEAST(X1,...,XN)

Description

Returns the least value amongX1,...,XN. If any argument isNULL, returnsNULL. Otherwise, in the case of floating-point arguments, if any argument isNaN, returnsNaN. In all other cases, returns the value amongX1,...,XNthat has the least value according to the ordering used by theORDER BYclause. The argumentsX1, ..., XN must be coercible to a common supertype, andthe supertype must support ordering.

X1,...,XNLEAST(X1,...,XN)
3,5,11

This function supports specifyingcollation.

Return Data Types

Data type of the input values.

LN

LN(X)

Description

Computes the natural logarithm of X. Generates an error if X is less than orequal to zero.

XLN(X)
1.00.0
+inf+inf
X <= 0Error

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

LOG

LOG(X[,Y])

Description

If only X is present,LOG is a synonym ofLN. If Y is also present,LOG computes the logarithm of X to base Y.

XYLOG(X, Y)
100.010.02.0
-infAny valueNaN
Any value+infNaN
+inf0.0 < Y < 1.0-inf
+infY > 1.0+inf
X <= 0Any valueError
Any valueY <= 0Error
Any value1.0Error

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
INT64FLOAT64NUMERICBIGNUMERICFLOAT64
NUMERICNUMERICNUMERICBIGNUMERICFLOAT64
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERICFLOAT64
FLOAT64FLOAT64FLOAT64FLOAT64FLOAT64

LOG10

LOG10(X)

Description

Similar toLOG, but computes logarithm to base 10.

XLOG10(X)
100.02.0
-infNaN
+inf+inf
X <= 0Error

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

MOD

MOD(X,Y)

Description

Modulo function: returns the remainder of the division of X by Y. Returnedvalue has the same sign as X. An error is generated if Y is 0.

XYMOD(X, Y)
25121
250Error

Return Data Type

The return data type is determined by the argument types with the followingtable.

INPUTINT64NUMERICBIGNUMERIC
INT64INT64NUMERICBIGNUMERIC
NUMERICNUMERICNUMERICBIGNUMERIC
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERIC

POW

POW(X,Y)

Description

Returns the value of X raised to the power of Y. If the result underflows andisn't representable, then the function returns a value of zero.

XYPOW(X, Y)
2.03.08.0
1.0Any value includingNaN1.0
Any value includingNaN01.0
-1.0+inf1.0
-1.0-inf1.0
ABS(X) < 1-inf+inf
ABS(X) > 1-inf0.0
ABS(X) < 1+inf0.0
ABS(X) > 1+inf+inf
-infY < 00.0
-infY > 0-inf if Y is an odd integer,+inf otherwise
+infY < 00
+infY > 0+inf
Finite value < 0Non-integerError
0Finite value < 0Error

Return Data Type

The return data type is determined by the argument types with the followingtable.

INPUTINT64NUMERICBIGNUMERICFLOAT64
INT64FLOAT64NUMERICBIGNUMERICFLOAT64
NUMERICNUMERICNUMERICBIGNUMERICFLOAT64
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERICFLOAT64
FLOAT64FLOAT64FLOAT64FLOAT64FLOAT64

POWER

POWER(X,Y)

Description

Synonym ofPOW(X, Y).

RAND

RAND()

Description

Generates a pseudo-random value of typeFLOAT64 inthe range of [0, 1), inclusive of 0 and exclusive of 1.

RANGE_BUCKET

RANGE_BUCKET(point,boundaries_array)

Description

RANGE_BUCKET scans through a sorted array and returns the 0-based positionof the point's upper bound. This can be useful if you need to group your data tobuild partitions, histograms, business-defined rules, and more.

RANGE_BUCKET follows these rules:

  • If the point exists in the array, returns the index of the next larger value.

    RANGE_BUCKET(20,[0,10,20,30,40])-- 3 is return valueRANGE_BUCKET(20,[0,10,20,20,40,40])-- 4 is return value
  • If the point doesn't exist in the array, but it falls between two values,returns the index of the larger value.

    RANGE_BUCKET(25,[0,10,20,30,40])-- 3 is return value
  • If the point is smaller than the first value in the array, returns 0.

    RANGE_BUCKET(-10,[5,10,20,30,40])-- 0 is return value
  • If the point is greater than or equal to the last value in the array,returns the length of the array.

    RANGE_BUCKET(80,[0,10,20,30,40])-- 5 is return value
  • If the array is empty, returns 0.

    RANGE_BUCKET(80,[])-- 0 is return value
  • If the point isNULL orNaN, returnsNULL.

    RANGE_BUCKET(NULL,[0,10,20,30,40])-- NULL is return value
  • The data type for the point and array must be compatible.

    RANGE_BUCKET('a',['a','b','c','d'])-- 1 is return valueRANGE_BUCKET(1.2,[1,1.2,1.4,1.6])-- 2 is return valueRANGE_BUCKET(1.2,[1,2,4,6])-- execution failure

Execution failure occurs when:

  • The array has aNaN orNULL value in it.

    RANGE_BUCKET(80,[NULL,10,20,30,40])-- execution failure
  • The array isn't sorted in ascending order.

    RANGE_BUCKET(30,[10,30,20,40,50])-- execution failure

Parameters

  • point: A generic value.
  • boundaries_array: A generic array of values.
Note: The data type forpoint and the element type ofboundaries_arraymust be equivalent. The data type must becomparable.

Return Value

INT64

Examples

In a table calledstudents, check to see how many records wouldexist in eachage_group bucket, based on a student's age:

  • age_group 0 (age < 10)
  • age_group 1 (age >= 10, age < 20)
  • age_group 2 (age >= 20, age < 30)
  • age_group 3 (age >= 30)
WITHstudentsAS(SELECT9ASageUNIONALLSELECT20ASageUNIONALLSELECT25ASageUNIONALLSELECT31ASageUNIONALLSELECT32ASageUNIONALLSELECT33ASage)SELECTRANGE_BUCKET(age,[10,20,30])ASage_group,COUNT(*)AScountFROMstudentsGROUPBY1/*--------------+-------* | age_group    | count | +--------------+-------+ | 0            | 1     | | 2            | 2     | | 3            | 3     | *--------------+-------*/

ROUND

ROUND(X[,N[,rounding_mode]])

Description

If only X is present, rounds X to the nearest integer. If N is present,rounds X to N decimal places after the decimal point. If N is negative,rounds off digits to the left of the decimal point. Rounds halfway casesaway from zero. Generates an error if overflow occurs.

If X is aNUMERIC orBIGNUMERIC type, then you canexplicitly setrounding_modeto one of the following:

If you set therounding_mode and X isn't aNUMERIC orBIGNUMERIC type,then the function generates an error.

ExpressionReturn Value
ROUND(2.0)2.0
ROUND(2.3)2.0
ROUND(2.8)3.0
ROUND(2.5)3.0
ROUND(-2.3)-2.0
ROUND(-2.8)-3.0
ROUND(-2.5)-3.0
ROUND(0)0
ROUND(+inf)+inf
ROUND(-inf)-inf
ROUND(NaN)NaN
ROUND(123.7, -1)120.0
ROUND(1.235, 2)1.24
ROUND(NUMERIC "2.25", 1, "ROUND_HALF_EVEN")2.2
ROUND(NUMERIC "2.35", 1, "ROUND_HALF_EVEN")2.4
ROUND(NUMERIC "2.251", 1, "ROUND_HALF_EVEN")2.3
ROUND(NUMERIC "-2.5", 0, "ROUND_HALF_EVEN")-2
ROUND(NUMERIC "2.5", 0, "ROUND_HALF_AWAY_FROM_ZERO")3
ROUND(NUMERIC "-2.5", 0, "ROUND_HALF_AWAY_FROM_ZERO")-3

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

SAFE_ADD

SAFE_ADD(X,Y)

Description

Equivalent to the addition operator (+), but returnsNULL if overflow occurs.

XYSAFE_ADD(X, Y)
549

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
INT64INT64NUMERICBIGNUMERICFLOAT64
NUMERICNUMERICNUMERICBIGNUMERICFLOAT64
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERICFLOAT64
FLOAT64FLOAT64FLOAT64FLOAT64FLOAT64

SAFE_DIVIDE

SAFE_DIVIDE(X,Y)

Description

Equivalent to the division operator (X / Y), but returnsNULL if an error occurs, such as a division by zero error.

XYSAFE_DIVIDE(X, Y)
2045
0200
200NULL

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
INT64FLOAT64NUMERICBIGNUMERICFLOAT64
NUMERICNUMERICNUMERICBIGNUMERICFLOAT64
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERICFLOAT64
FLOAT64FLOAT64FLOAT64FLOAT64FLOAT64

SAFE_MULTIPLY

SAFE_MULTIPLY(X,Y)

Description

Equivalent to the multiplication operator (*), but returnsNULL if overflow occurs.

XYSAFE_MULTIPLY(X, Y)
20480

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
INT64INT64NUMERICBIGNUMERICFLOAT64
NUMERICNUMERICNUMERICBIGNUMERICFLOAT64
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERICFLOAT64
FLOAT64FLOAT64FLOAT64FLOAT64FLOAT64

SAFE_NEGATE

SAFE_NEGATE(X)

Description

Equivalent to the unary minus operator (-), but returnsNULL if overflow occurs.

XSAFE_NEGATE(X)
+1-1
-1+1
00

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTINT64NUMERICBIGNUMERICFLOAT64

SAFE_SUBTRACT

SAFE_SUBTRACT(X,Y)

Description

Returns the result of Y subtracted from X.Equivalent to the subtraction operator (-), but returnsNULL if overflow occurs.

XYSAFE_SUBTRACT(X, Y)
541

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
INT64INT64NUMERICBIGNUMERICFLOAT64
NUMERICNUMERICNUMERICBIGNUMERICFLOAT64
BIGNUMERICBIGNUMERICBIGNUMERICBIGNUMERICFLOAT64
FLOAT64FLOAT64FLOAT64FLOAT64FLOAT64

SEC

SEC(X)

Description

Computes the secant for the angle ofX, whereX is specified in radians.X can be any data typethatcoerces toFLOAT64.

XSEC(X)
+infNaN
-infNaN
NaNNaN
NULLNULL

Return Data Type

FLOAT64

Example

SELECTSEC(100)ASa,SEC(-1)ASb;/*----------------+---------------* | a              | b             | +----------------+---------------+ | 1.159663822905 | 1.85081571768 | *----------------+---------------*/

SECH

SECH(X)

Description

Computes the hyperbolic secant for the angle ofX, whereX is specifiedin radians.X can be any data typethatcoerces toFLOAT64.Never produces an error.

XSECH(X)
+inf0
-inf0
NaNNaN
NULLNULL

Return Data Type

FLOAT64

Example

SELECTSECH(0.5)ASa,SECH(-2)ASb,SECH(100)ASc;/*----------------+----------------+---------------------* | a              | b              | c                   | +----------------+----------------+---------------------+ | 0.88681888397  | 0.265802228834 | 7.4401519520417E-44 | *----------------+----------------+---------------------*/

SIGN

SIGN(X)

Description

Returns-1,0, or+1 for negative, zero and positive argumentsrespectively. For floating point arguments, this function doesn't distinguishbetween positive and negative zero.

XSIGN(X)
25+1
00
-25-1
NaNNaN

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTINT64NUMERICBIGNUMERICFLOAT64

SIN

SIN(X)

Description

Computes the sine of X where X is specified in radians. Never fails.

XSIN(X)
+infNaN
-infNaN
NaNNaN

SINH

SINH(X)

Description

Computes the hyperbolic sine of X where X is specified in radians. Generatesan error if overflow occurs.

XSINH(X)
+inf+inf
-inf-inf
NaNNaN

SQRT

SQRT(X)

Description

Computes the square root of X. Generates an error if X is less than 0.

XSQRT(X)
25.05.0
+inf+inf
X < 0Error

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

TAN

TAN(X)

Description

Computes the tangent of X where X is specified in radians. Generates an error ifoverflow occurs.

XTAN(X)
+infNaN
-infNaN
NaNNaN

TANH

TANH(X)

Description

Computes the hyperbolic tangent of X where X is specified in radians. Doesn'tfail.

XTANH(X)
+inf1.0
-inf-1.0
NaNNaN

TRUNC

TRUNC(X[,N])

Description

If only X is present,TRUNC rounds X to the nearest integer whose absolutevalue isn't greater than the absolute value of X. If N is also present,TRUNCbehaves likeROUND(X, N), but always rounds towards zero and never overflows.

XTRUNC(X)
2.02.0
2.32.0
2.82.0
2.52.0
-2.3-2.0
-2.8-2.0
-2.5-2.0
00
+inf+inf
-inf-inf
NaNNaN

Return Data Type

INPUTINT64NUMERICBIGNUMERICFLOAT64
OUTPUTFLOAT64NUMERICBIGNUMERICFLOAT64

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-07-02 UTC.