@@ -218,14 +218,30 @@ Using SPARSE Vectors
218218====================
219219
220220A Sparse vector is a vector which has zero value for most of its dimensions.
221- This vector only physically stores the non-zero values. A sparse vector is
222- supported when you are using Oracle Database 23.7 or later.
221+ This vector only physically stores the non-zero values. For more information
222+ on sparse vectors, see the `Oracle AI Vector search User's Guide <https://
223+ www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-6015566C-3277-4A3C-8DD0-
224+ 08B346A05478> `__.
223225
224- Sparse vectors can store the total number of dimensions, an array of indices,
225- and an array of values. The storage formats that can be used with sparse
226- vectors are float32, float64, and int8. Note that the binary storage format
227- cannot be used with sparse vectors. You can define a column for a sparse
228- vector using the following format::
226+ Sparse vectors are supported when you are using Oracle Database 23.7 or later.
227+
228+ Sparse vectors are represented by the total number of vector dimensions, an
229+ array of indices, and an array of values where each value's location in the
230+ vector is indicated by the corresponding indices array position. All other
231+ vector values are treated as zero. The storage formats that can be used with
232+ sparse vectors are float32, float64, and int8. Note that the binary storage
233+ format cannot be used with sparse vectors.
234+
235+ For example, a string representation could be::
236+
237+ [25, [5, 8, 11], [25.25, 6.125, 8.25]]
238+
239+ In this example, the sparse vector has 25 dimensions. Only indices 5, 8, and 11
240+ have values which are 25.25, 6.125, and 8.25 respectively. All of the other
241+ values are zero.
242+
243+ In Oracle Database, you can define a column for a sparse vector using the
244+ following format::
229245
230246 VECTOR(number_of_dimensions, dimension_storage_format, sparse)
231247
@@ -239,7 +255,7 @@ For example, to create a table with three columns for sparse vectors:
239255 int8sparsecol vector(35, int8, sparse)
240256 )
241257
242- In this example the :
258+ In this example:
243259
244260- The float32sparsecol column can store sparse vector data of 25 dimensions
245261 where each dimension value is a 32-bit floating-point number.
@@ -256,18 +272,9 @@ Inserting SPARSE Vectors
256272------------------------
257273
258274With python-oracledb, sparse vector data can be inserted using
259- :ref: `SparseVector objects <sparsevectorsobj >`. You can specify the number of
260- dimensions, an array of indices, and an array of values as the data for a
261- sparse vector. For example, the string representation is::
262-
263- [25, [5,8,11], [25.25, 6.125, 8.25]]
264-
265- In this example, the sparse vector has 25 dimensions. Only indices 5, 8, and
266- 11 have values 25.25, 6.125, and 8.25 respectively. All of the other values
267- are zero.
268-
269- The SparseVector objects are used as bind values when inserting sparse vector
270- columns. For example:
275+ :ref: `SparseVector objects <sparsevectorsobj >`. The SparseVector objects are
276+ used when fetching vectors, and as bind values when inserting sparse vector
277+ columns. For example to insert data:
271278
272279.. code-block :: python
273280
@@ -289,7 +296,7 @@ columns. For example:
289296 )
290297
291298 cursor.execute(
292- " insert into vector_sparse_table (:1, :2, :3)" ,
299+ " insert into vector_sparse_table values (:1, :2, :3)" ,
293300 [float32_val, float64_val, int8_val]
294301 )
295302
@@ -298,23 +305,43 @@ columns. For example:
298305Fetching Sparse Vectors
299306-----------------------
300307
301- With python-oracledb, sparse vector columns are fetched in the same format
302- accepted by Oracle Database by using the str() function. For example :
308+ With python-oracledb, sparse vector columns are fetched as :ref: ` SparseVector
309+ objects <sparsevectorsobj>` :
303310
304311.. code-block :: python
305312
306- cursor.execute(" select * from vec_sparse" )
313+ cursor.execute(" select * from vector_sparse_table" )
314+ for row in cursor:
315+ print (row)
316+
317+
318+ This prints::
319+
320+ (oracledb.SparseVector(25, array('I', [6, 10, 18]), array('f', [26.25, 129.625, 579.875])),
321+ oracledb.SparseVector(30, array('I', [9, 16, 24]), array('d', [19.125, 78.5, 977.375])),
322+ oracledb.SparseVector(35, array('I', [10, 20, 30]), array('b', [26, 125, -37])))
323+
324+ Depending on context, the SparseVector type will be treated as a string:
325+
326+ .. code-block :: python
327+
328+ cursor.execute(" select * from vector_sparse_table" )
307329 for float32_val, float64_val, int8_val in cursor:
308- print (" float32:" , str ( float32_val) )
309- print (" float64:" , str ( float64_val) )
310- print (" int8:" , str ( int8_val) )
330+ print (" float32:" , float32_val)
331+ print (" float64:" , float64_val)
332+ print (" int8:" , int8_val)
311333
312- This prints the following output ::
334+ This prints::
313335
314336 float32: [25, [6, 10, 18], [26.25, 129.625, 579.875]]
315337 float64: [30, [9, 16, 24], [19.125, 78.5, 977.375]]
316338 int8: [35, [10, 20, 30], [26, 125, -37]]
317339
340+ Values can also be explicitly passed to `str()
341+ <https://docs.python.org/3/library/stdtypes.html#str> `__, if needed.
342+
343+ **SPARSE Vector Metadata **
344+
318345The :ref: `FetchInfo <fetchinfoobj >` object that is returned as part of the
319346fetched metadata contains attributes :attr: `FetchInfo.vector_dimensions `,
320347:attr: `FetchInfo.vector_format `, and :attr: `FetchInfo.vector_is_sparse ` which
0 commit comments