Outdated Version

You are viewing an older version of this section. View current production version.

JSON_ARRAY_PACK

Converts a JSON array of zero or more floating point numbers to an encoded blob.

Syntax

JSON_ARRAY_PACK('[float [, ...]]')

Remarks

JSON_ARRAY_PACK() can be used with other vector built-in functions, namely DOT_PRODUCT(), VECTOR_SUB(), and EUCLIDEAN_DISTANCE(). These functions require two input vectors that are encoded as blobs containing packed single-precision floating-point numbers in little-endian byte order. The vector returned by JSON_ARRAY_PACK() is appropriately formatted for use as an input parameter to these functions.

Examples

Example: Inserting Data Using JSON_ARRAY_PACK()

The following example inserts data into a table with a column of the BLOB data type. The HEX() built-in function is also used to return a readable form of the binary output:

INSERT INTO t VALUES (JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'));
Query OK, 0 rows affected (0.21 sec)

SELECT HEX(b) FROM t;
+--------------------------+
| HEX(b)                   |
+--------------------------+
| 0000803F0000003F00000040 |
+--------------------------+
1 row in set (0.20 sec)

Example: Using JSON_ARRAY_PACK() with DOT_PRODUCT()

The following example uses JSON_ARRAY_PACK() for input parameters to the DOT_PRODUCT() built-in function:

SELECT DOT_PRODUCT(JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'), JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'));
+-------------------------------------------------------------------------------------+
| DOT_PRODUCT(JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'), JSON_ARRAY_PACK('[1.0, 0.5, 2.0]')) |
+-------------------------------------------------------------------------------------+
|                                                                                5.25 |
+-------------------------------------------------------------------------------------+
1 row in set (0.11 sec)

Example: Using JSON_ARRAY_PACK() with VECTOR_SUB()

The following example uses JSON_ARRAY_PACK() for input parameters to the VECTOR_SUB() built-in function. The HEX() built-in function is also used to return a readable form of the binary output:

SELECT HEX(VECTOR_SUB(JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'), JSON_ARRAY_PACK('[0.7, 0.2, 1.7]')));
+-----------------------------------------------------------------------------------------+
| HEX(VECTOR_SUB(JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'), JSON_ARRAY_PACK('[0.7, 0.2, 1.7]'))) |
+-----------------------------------------------------------------------------------------+
| 9A99993E9A99993E9899993E                                                                |
+-----------------------------------------------------------------------------------------+
1 row in set (0.11 sec)

Example: Using JSON_ARRAY_PACK() with EUCLIDEAN_DISTANCE()

The following example uses JSON_ARRAY_PACK() for input parameters to the EUCLIDEAN_DISTANCE() built-in function:

SELECT EUCLIDEAN_DISTANCE(JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'), JSON_ARRAY_PACK('[0.7, 0.2, 1.7]'));
+--------------------------------------------------------------------------------------------+
| EUCLIDEAN_DISTANCE(JSON_ARRAY_PACK('[1.0, 0.5, 2.0]'), JSON_ARRAY_PACK('[0.7, 0.2, 1.7]')) |
+--------------------------------------------------------------------------------------------+
|                                                                         0.5196152239171921 |
+--------------------------------------------------------------------------------------------+
1 row in set (0.10 sec)

Related Topics