[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-distance [Back]  [Original]

The ML.DISTANCE function  |  BigQuery  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback Stay organized with collections Save and categorize content based on your preferences.

The ML.DISTANCE function

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

Note: The VECTOR_SEARCH function is another vector function that calculates the distance between vectors. You should use the VECTOR_SEARCH function if you need to search a dataset for vectors similar to an input vector. You should use the ML.DISTANCE function if you need to compare two specific vectors to determine the distance between them.

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

Send feedback

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

Last updated 2026-08-11 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-11 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page