The ML.DISTANCE function

This document describes the ML.DISTANCE scalar function, which lets you compute the distance between two vectors.

Syntax

ML.DISTANCE(vector1, vector2 [, type])

Arguments

ML.DISTANCE has the following arguments:

Output

ML.DISTANCE returns a FLOAT64 value that represents the distance between the vectors. Returns NULL if either vector1 or vector2 is NULL.

Example

Get the Euclidean distance for two tensors of ARRAY<FLOAT64> values:

  1. Create the table t1:

    CREATE TABLE mydataset.t1
    (
    v1 ARRAY<FLOAT64>,
    v2 ARRAY<FLOAT64>
    )
    
  2. Populate t1:

    INSERT mydataset.t1 (v1,v2)
    VALUES ([4.1,0.5,1.0], [3.0,0.0,2.5])
    
  3. Calculate the Euclidean norm for v1 and v2:

    SELECT v1, v2, ML.DISTANCE(v1, v2, 'EUCLIDEAN') AS output FROM mydataset.t1
    

    This query produces the following output:

    +---------------+---------------+-------------------+
    | v1            | v2            | output            |
    +---------------+---------------+-------------------|
    | [4.1,0.5,1.0] | [3.0,0.0,2.5] | 1.926136028425822 |
    +------------+------------------+-------------------+
    

What's next