14 Vector Support in Oracle AI Database
This chapter describes the Oracle AI Database vector support in OCCI.
Starting with Oracle AI Database 26ai vectors as a datatype is introduced to represent semi-structured and unstructured data for AI and machine learning semantics. Vectors are represented as a series of numbers with a specified length and number type.
The vector elements can be represented by the following data types:
- INT8_t : This corresponds to an int8 (8-bit signed integer) data type in C++
- FLOAT32 :This corresponds to a float in C++
- FLOAT64 :This corresponds to a double type in C++
- BINARY :Although C++ does not support
binarydata type,uin8_tis used to support binary data. Eachuint8_tvalue has 8 bits (binary values). BINARY vectors must have a dimension that is a multiple of 8.
See Also:
Overview of Oracle AI Vector Search- Oracle AI Database Vector Support in OCCI
This section describes the Oracle AI Database vector support functions in OCCI. - Sparse Vectors
This section describes the functions to work with the SparseVectors. Sparse vectors are preferred as they reduce the cost of memory. - Utility Function for SparseVector
This section describes a member function of the SparseVector class and returns a dense form of the vector stored by your SparseVector.
Oracle AI Database Vector Support in OCCI
This section describes the Oracle AI Database vector support functions in OCCI.
- Binding Vectors in a Statement Handle
This section describes the functions introduced for binding the vector data to a statement handle. - PL/SQL Out-Binds for Vectors
This section describes the functions used to get the output from PL/SQL procedures or functions through out-binds in a statement handle. - Defining Vectors from a ResultSet of a Query
This section describes the functions introduced in the ResultSet class to obtain the column values of type vector into C++ stl vector containers. - MetaData of Vectors
A new data typeOCCI_SQLT_VECis introduced for supporting MetaData in describe functionality.
Parent topic: Vector Support in Oracle AI Database
Binding Vectors in a Statement Handle
This section describes the functions introduced for binding the vector data to a statement handle.
- setInt8Vector()
- setFloatVector()
Sets the value of an Oracle AI Database Vector data type inIEEE FLOAT32format from a C++ vector value of typefloat. - setDoubleVector()
Sets the value of an Oracle AI Database Vector data type in formatIEEE FLOAT64from a C++ vector of type double. - setBinaryVector()
Sets the value of an Oracle AI Database Vector data type in binary format from a C++ vector of typeuint8_t.
Parent topic: Oracle AI Database Vector Support in OCCI
setInt8Vector()
Sets the value of an Oracle AI Database Vector data type in int8(ub1) format from a C++ vector value of type int8_t.
Syntax
void setInt8Vector(unsigned int paramIndex,vector<int8_t> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the vector to be set. |
Parent topic: Binding Vectors in a Statement Handle
setFloatVector()
Sets the value of an Oracle AI Database Vector data type in IEEE FLOAT32 format from a C++ vector value of type float.
Syntax
void setFloatVector(unsigned int paramIndex,vector<float> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the vector to be set. |
Parent topic: Binding Vectors in a Statement Handle
setDoubleVector()
Sets the value of an Oracle AI Database Vector data type in format IEEE FLOAT64 from a C++ vector of type double.
Syntax
void setDoubleVector(unsigned int paramIndex,vector<double> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the vector to be set. |
Parent topic: Binding Vectors in a Statement Handle
setBinaryVector()
Sets the value of an Oracle AI Database Vector data type in binary format from a C++ vector of type uint8_t.
Syntax
Note:
You can only insert binary vectors with dimensions that are a multiple of 8.void setBinaryVector(unsigned int paramIndex,vector<uint8_t> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the vector to be set. |
The following sample code snippet, binds the vector myData as a FLOAT32 to position 1 in stmt. On executing the statement further, it is inserted in the database table.
Statement *stmt;
stmt = conn->createStatement ("INSERT INTO test_vecflt values(:1,:2)");
vector<float> myData({1.4,1.2,3.5});
stmt->setInt(1,10);
stmt->setFloatVector(2,myData);
stmt->executeUpdate();Parent topic: Binding Vectors in a Statement Handle
PL/SQL Out-Binds for Vectors
This section describes the functions used to get the output from PL/SQL procedures or functions through out-binds in a statement handle.
- getInt8Vector()
Gets the value of an Oracle AI Database Vector data type inint8(ub1)format from a C++ vector value of typeint8_tformat. - getFloatVector()
Gets the value of an Oracle AI Database Vector inIEEE FLOAT32format from a C++ vector value of typefloat. - getDoubleVector()
Gets the value of an Oracle AI Database Vector data type inIEEE FLOAT64format from a C++ vector value of type double. - getBinaryVector()
Gets the value of an Oracle AI Database Vector data type in binary format inside a C++ vector of typeuint8_t.
Parent topic: Oracle AI Database Vector Support in OCCI
getInt8Vector()
Gets the value of an Oracle AI Database Vector data type in int8(ub1) format from a C++ vector value of type int8_t format.
Syntax
void getInt8Vector(unsigned int paramIndex,vector<int8_t> &vec);| Parameter | Description |
|---|---|
|
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Parent topic: PL/SQL Out-Binds for Vectors
getFloatVector()
Gets the value of an Oracle AI Database Vector in IEEE FLOAT32 format from a C++ vector value of type float.
Syntax
void getFloatVector(unsigned int paramIndex,vector<float> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Parent topic: PL/SQL Out-Binds for Vectors
getDoubleVector()
Gets the value of an Oracle AI Database Vector data type in IEEE FLOAT64 format from a C++ vector value of type double.
Syntax
void getDoubleVector(unsigned int paramIndex,vector<double> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Parent topic: PL/SQL Out-Binds for Vectors
getBinaryVector()
Gets the value of an Oracle AI Database Vector data type in binary format inside a C++ vector of type uint8_t.
Syntax
void getBinaryVector(unsigned int paramIndex,vector<uint8_t>&vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Note:
Currently, the dimension of binary vectors is a multiple of 8. That is,8*vec.size()where, vec is a stl vector<uint8_t>.
The following code snippet can be used for out binds to obtain a vector:
Statement *stmt;
stmt = conn->createStatement ("BEGIN occivec_demo(:1) END;");
//Disclaimer: Binary literals introduced in C++14 and above, used here for descriptive purposes.
vector<uint8_t> myInBind({0b00100011,0b01000011,0b10101011,0b01001001});
stmt->setBinaryVector(1,myInOutBind);
stmt->stmtExecute();
stmt->getBinaryVector(1,myInOutBind);This obtains the data fetched from INOUT bind at position 1 in myInOutBind.
In case of flexible dimensions, there may be a case where the size of the vector is not the same as the dimension of the Database vector. In such a case, stmt->getBinaryVector() will always have a return value indicating the dimension of the OUT vector.
int dimension = stmt->getBinaryVector(1,myInOutBind);
//printing the bits
for(int i=0;i<dimension;i++)
cout<<((v[i/8]>>(8-(i%8)))&1;Statement *stmt;
stmt = conn->createStatement ("BEGIN occivec_demo(:1,:2,:3) END;");
vector<double> myInBind({1.4,1.2,3.5});
vector<double> myInOutBind;
vector<double> myOutBind;
stmt->setInt(1,myInBind);
stmt->setDoubleVector(2,myInOutBind);
stmt->registerOutParam(3,OCCI_SQLT_VEC);
stmt->stmtExecute();
stmt->getDoubleVector(2,myInOutBind);
stmt->getDoubleVector(3,myOutBind);Parent topic: PL/SQL Out-Binds for Vectors
Defining Vectors from a ResultSet of a Query
This section describes the functions introduced in the ResultSet class to obtain the column values of type vector into C++ stl vector containers.
- getInt8Vector()
Gets the column value of an Oracle AI Database Vector data type inint8(ub1)format as a C++ vector value of typeint8_t. - getFloatVector()
Gets the column value of an Oracle AI Database Vector data type inIEEE FLOAT32format from a C++ vector value of typefloat. - getDoubleVector()
Gets the column value of an Oracle AI Database Vector data type inIEEE FLOAT64format from a C++ vector value of typedouble. - getBinaryVector()
Gets the column value of an Oracle AI Database Vector data type in binary format from a C++ vector value of typeuint8_t. - getString()
Gets the column value of an Oracle AI Database vector of any format as a C++ string.
Parent topic: Oracle AI Database Vector Support in OCCI
getInt8Vector()
Gets the column value of an Oracle AI Database Vector data type in int8(ub1) format as a C++ vector value of type int8_t.
Syntax
void getInt8Vector(unsigned int paramIndex,vector<int8_t> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Parent topic: Defining Vectors from a ResultSet of a Query
getFloatVector()
Gets the column value of an Oracle AI Database Vector data type in IEEE FLOAT32 format from a C++ vector value of type float.
Syntax
void getFloatVector(unsigned int paramIndex,vector<float> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Parent topic: Defining Vectors from a ResultSet of a Query
getDoubleVector()
Gets the column value of an Oracle AI Database Vector data type in IEEE FLOAT64 format from a C++ vector value of type double.
Syntax
void getDoubleVector(unsigned int paramIndex,vector<double> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
Parent topic: Defining Vectors from a ResultSet of a Query
getBinaryVector()
Gets the column value of an Oracle AI Database Vector data type in binary format from a C++ vector value of type uint8_t.
Syntax
void getBinaryVector(unsigned int paramIndex,vector<uint8_t> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the vector (OUT parameter) into which the values must be inserted.
|
The following code snippet can be used in ResultSet to obtain a vector:
Statement stmt;
stmt = conn->createStatement ("SELECT * FROM TEST_VECFLT");
ResultSet *rs = stmt->getResultSet();
vector<float> myResult;
stmt->stmtExecute();
rs->getFloatVector(2,myResult);The type of vector data in the result depends on the function called. For example, getFloatVector corresponds to FLOAT32 format.
Parent topic: Defining Vectors from a ResultSet of a Query
getString()
Gets the column value of an Oracle AI Database vector of any format as a C++ string.
Syntax
string getString(unsigned int paramIndex);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
Statement stmt;
stmt = conn->createStatement ("SELECT id,embedding FROM TEST_SPARSEVECFLT");
ResultSet *rs = stmt->getResultSet();
stmt->stmtExecute();
int id = rs->getInt(1);
string vec = rs->getString(2);Parent topic: Defining Vectors from a ResultSet of a Query
MetaData of Vectors
A new data type OCCI_SQLT_VEC is introduced for supporting
MetaData in describe functionality.
- ATTR_VECTOR_DIMENSION : Information of vector dimensions
- ATTR_VECTOR_DATA_FORMAT: Type of data format stored in a vector column
- ATTR_VECTOR_PROPERTY: Information of the data flag
vector<MetaData> v1;
MetaData metaData = conn->getMetaData("TEST_VECDBL");
v1 = metaData.getVector(MetaData::ATTR_LIST_COLUMNS);
for(int i=0; i < v1.size(); i++)
{
MetaData md = v1[i];
cout << " Column Name :" << md.getString(MetaData::ATTR_NAME) << endl;
cout << " Data Type :" << md.getInt(MetaData::ATTR_DATA_TYPE) << endl;
cout << " Dimension :" << md.getUInt(MetaData::ATTR_VECTOR_DIMENSION) << endl;
}Parent topic: Oracle AI Database Vector Support in OCCI
Example
#include <sys/types.h>
#include <fstream>
#include <iostream>
#include <occi.h>
#include <cstdlib>
#include <string.h>
using namespace oracle::occi;
using namespace std;
static char *username=strdup("scott");
static char *password=strdup("tiger");
static char *dbname=strdup("inst1");
class occivec
{
private:
Environment *env;
Connection *conn;
Statement *stmt;
ResultSet *rs;
public:
occivec ()
{
env = Environment::createEnvironment (Environment::DEFAULT);
conn = env->createConnection (username, password, dbname);
}
~occivec ()
{
env->terminateConnection (conn);
Environment::terminateEnvironment (env);
}
void displayAllRows (string tableName)
{
cout<<"--- Displaying rows of "<<tableName<<"---"<<endl;
string sqlStmt = "select * from "+tableName;
stmt = conn->createStatement (sqlStmt);
ResultSet *rset = stmt->executeQuery ();
try{
while (rset->next ())
{
cout << "ID: " << rset->getInt (1) << endl;
vector<double> ans;
rset->getDoubleVector(2,ans);
cout << "Embedding: ";
for(auto i:ans)cout<<i<<" ";cout<<endl;
}
}catch(SQLException ex)
{
cout<<"Exception thrown for displayAllRows"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
stmt->closeResultSet (rset);
conn->terminateStatement (stmt);
cout<<endl;
}
void testproc ()
{
cout << "--- Testing Procedure Binds ---" << endl;
vector<double> myInBind = {10.1,20.2,30.3,40.4};
vector<double> myInOutBind = {50.5,60.6,70.7,80.8};
vector<double> myOutBind;
try{
cout << "callproc - invoking a PL/SQL procedure having IN, OUT and IN/OUT ";
stmt = conn->createStatement("BEGIN occivec_demo(:1,:2,:3); END;");
stmt->setDoubleVector(1,myInBind);
stmt->setDoubleVector (2,myInOutBind);
stmt->registerOutParam (3, OCCI_SQLT_VEC);
stmt->executeUpdate ();
stmt->getDoubleVector(2,myInOutBind);
stmt->getDoubleVector(3,myOutBind);
cout << "Printing the INOUT & OUT parameters:" << endl;
cout<< "IN/OUT vector:"<<endl;
for(auto i:myInOutBind)cout<<i<<" ";cout<<endl;
cout<< "OUT vector:"<<endl;
for(auto i:myOutBind)cout<<i<<" ";cout<<endl;
conn->terminateStatement (stmt);
cout << endl;
}catch(SQLException ex)
{
cout<<"Exception thrown for testproc"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
}
void insertElement ()
{
cout << "--- Inserting in a vector table ---" << endl;
int num = 1;
vector<double> myData = {10,20.30,30,40.50};
vector<double> myResult;
try{
stmt = conn->createStatement ("INSERT INTO test_vecint values(:1,:2)");
stmt->setInt(1,num);
stmt->setDoubleVector(2,myData);
stmt->executeUpdate();
conn->terminateStatement (stmt);
cout << endl;
}catch(SQLException ex)
{
cout<<"Exception thrown for insertElement"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
}
void describe_table ()
{
cout << "--- Describing the table - TEST_VECDBL ---" << endl;
vector<MetaData> v1;
MetaData metaData = conn->getMetaData("TEST_VECDBL");
v1 = metaData.getVector(MetaData::ATTR_LIST_COLUMNS);
for(int i=0;i<v1.size();i++)
{
MetaData md = v1[i];
cout << " Column Name :" << (md.getString(MetaData::ATTR_NAME)) << endl;
cout << " Data Type :" << (printType (md.getInt(MetaData::ATTR_DATA_TYPE))) << endl;
cout << " Dimension :"<< md.getUInt(MetaData::ATTR_VECTOR_DIMENSION) << endl;
cout << endl;
}
cout << endl;
}
string printType (int type)
{
switch (type)
{
case OCCI_SQLT_NUM : return "NUMBER";
break;
case OCCI_SQLT_VEC: return "VECTOR";
break;
default: return to_string(type);
}
} // End of printType (int)
};
int main (void)
{
try{
cout << "Demo Program for OCCI Vector" << endl;
occivec *demo = new occivec ();
demo->insertElement();
demo->describe_table();
demo->testproc();
demo->displayAllRows("TEST_VECDBL");
delete (demo);
}
catch (SQLException ex){
cout << ex.getMessage() << endl;
}
cout << "done" << endl;
}
Parent topic: MetaData of Vectors
Sparse Vectors
This section describes the functions to work with the SparseVectors. Sparse vectors are preferred as they reduce the cost of memory.
Note:
Sparse vectors follow a 0-based indexing conforming with the current vector implementations.- SparseVector Constructors
This section describes the SparseVector constructors. - Binding Sparse Vectors in a Statement Handle
The functions described in this section are introduced for binding SparseVector data to a statement handle. - PL/SQL out-binds for Sparse Vectors
The functions described in this section, can be used to get output from PLSQL procedures or functions through out-binds in the statement handle. - Defining Sparse Vectors from a ResultSet of a Query
This section describes the functions introduced in ResultSet class to obtain the column values of type SparseVector into C++stlvector containers.
Parent topic: Vector Support in Oracle AI Database
SparseVector Constructors
This section describes the SparseVector constructors.
- SparseVector()
SparseVector function initializes a SparseVector of required type that fixes the type of values it uses. - SparseVector(uint32_t size,vector<uint32_t> indices,vector<T> values)
This function initializes a SparseVector of required size and non_zeroes values with respective indices. - SparseVec(uint32_t size,pair<vector<uint32_t>,vector<T>> &vecp) Ref constructor
This function is more efficient or a faster constructor.
Parent topic: Sparse Vectors
SparseVector()
SparseVector function initializes a SparseVector of required type that fixes the type of values it uses.
SparseVector<T> mySparseVector();SparseVector<float> mySparseVector;Parent topic: SparseVector Constructors
SparseVector(uint32_t size,vector<uint32_t> indices,vector<T> values)
This function initializes a SparseVector of required size and non_zeroes values with respective indices.
Syntax
Copying these vectors to the vector members of our SparseVector class.
Note:
The size must strictly be greater than or equal to the length of the indices and values vector. Also, ensure that the length of indices and the values are same.SparseVector<T> mySparseVector(uint32_t size, vector<uint32_t> indices, vector<A>
values);
| Parameter | Description |
|---|---|
size |
Specifies the total size of the sparse vector. |
indices |
Specifies the indices of non-zero values of the sparse vector. |
values |
Specifies the values of the non-zero indices in the sparse vector. |
This function can be used in your library to construct a sparse vector as follows:
SparseVector<float> mySparseVector(45,{1,5,40},{1.3,2.5,4554}};Parent topic: SparseVector Constructors
SparseVec(uint32_t size,pair<vector<uint32_t>,vector<T>> &vecp) Ref constructor
This function is more efficient or a faster constructor.
Syntax
You do not need to copy the vectors in your own class vector members. Instead, in this constructor, we can utilize references to the vectors inside the pair given as your argument. This means that all the changes made to the user vectors are reflected in your SparseVector and vice-versa.
SparseVector<T> myCopySparseVector(uint32_t size, pair<vector<uint32_t>,vector<T>>
&vecp);
| Parameter | Description |
|---|---|
size |
Specifies the total size of the sparse vector. |
vecp |
Specifies the pair of vectors containing the index and the value of the vectors respectively. |
The following code snippet shows how this function is used to construct a sparse vector from an existing sparse vector:
pair<vector<uint32_t>,vector<float>> vecp({1,3,5},{2,4,6});
SparseVector<float> mySparseVector(100,vecp); // now we initialize this does no copies of the vectors inside our pair
string sqlStmt = "SELECT * FROM DEMO_TAB"; //Table has one row with two columns (id:2,SparseVector:10,{0,2,4},{3,5,7})
stmt = conn->createStatement (sqlStmt);
rset = stmt->executeQuery ();
while(rset->next())
{
cout << "ID: " << rset->getInt (1) << endl;
rset->getSparseVector(2, mySparseVector);
//each time we do getSparseVector our vecp directly gets modified to the sparse-vector we recieved.
//We can check this by printing each vectors of vecp.
for(auto i:vecp.first)
cout<<i<<" ";
cout<<endl;
// this will print 0,2,4 as our indices in SparseVector of our table
for(auto i:vecp.second)
cout<<i<<" ";
cout<<endl;
// this will print 3,5,7 as our indices in SparseVector of our table
}
Note:
The pair we used for initialization that is, vecp should not go out of scope when you are using your mySparseVector for the database operations. Otherwise, your SparseVector is also invalidated.Parent topic: SparseVector Constructors
Binding Sparse Vectors in a Statement Handle
The functions described in this section are introduced for binding SparseVector data to a statement handle.
Note:
Functionality of sparse vector and dense vector bind/define functions:We can
insert using SparseVector/std::vector regardless of the table
having dense/sparse vector columns. However, we can only fetch SparseVector from
sparse vector columns and dense vectors (std::vector) from
dense vector columns.
- setInt8Vector()
Binds the value to a Sparse Vector inINT8format from an OCCI SparseVector of typeint8_t. - setFloatVector()
Binds the value to a Sparse VectorIEEE FLOAT32format from an OCCI SparseVector of typefloat. - setDoubleVector()
Binds the value to a Sparse Vector inIEEE FLOAT64format from an OCCI SparseVector of typedouble. - setBinaryVector()
Sets the value of an Oracle AI Database Vector data type in binary format from a C++ vector of typeuint8_t
Parent topic: Sparse Vectors
setInt8Vector()
Binds the value to a Sparse Vector in INT8 format from an
OCCI SparseVector of type int8_t.
Syntax
void setInt8Vector(unsigned int paramIndex, SparseVector<int8_t> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the SparseVector to be set. |
Parent topic: Binding Sparse Vectors in a Statement Handle
setFloatVector()
Binds the value to a Sparse Vector IEEE FLOAT32 format from
an OCCI SparseVector of type float.
Syntax
void setFloatVector(unsigned int paramIndex, SparseVector<float> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the SparseVector to be set. |
Parent topic: Binding Sparse Vectors in a Statement Handle
setDoubleVector()
Binds the value to a Sparse Vector in IEEE FLOAT64 format
from an OCCI SparseVector of type double.
Syntax
void setDoubleVector(unsigned int paramIndex, SparseVector<double> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the SparseVector to be set. |
Parent topic: Binding Sparse Vectors in a Statement Handle
setBinaryVector()
Sets the value of an Oracle AI Database Vector data type in binary format from a C++ vector of type uint8_t
Syntax
Note:
You can only insert vectors with dimension that are a mutliple of 8.void setBinaryVector(unsigned int paramIndex, SparseVector<uint8_t> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
Specifies the SparseVector to be set. |
The following code snippet binds the SparseVector mySparseVector of FLOAT32 format to position 1 in stmt. Further executing the statement, inserts it in the database table:
Statement *stmt;
stmt = conn->createStatement ("INSERT INTO test_sparsevecflt values(:1,:2)");
vector<uint32_t> inds = {0, 5, 9};
vector<float> vals = {1.5, 2.3, 4.6};
SparseVector<float> mySparseVector(10, inds, vals);
stmt->setInt(1,10);
stmt->setFloatVector(2, mySparseVector);
stmt->executeUpdate();Parent topic: Binding Sparse Vectors in a Statement Handle
PL/SQL out-binds for Sparse Vectors
The functions described in this section, can be used to get output from PLSQL procedures or functions through out-binds in the statement handle.
- getInt8Vector()
Gets the value of an Oracle AI Database sparse vector data type inint8(ub1)format as an OCCI SparseVector of typeint8_t. - getFloatVector()
Gets the value of an Oracle AI Database sparse vector data type inIEEE FLOAT32format as an OCCI SparseVector of typefloat. - getDoubleVector()
Gets the value of an Oracle AI Database sparse vector data type inIEEE FLOAT64format as an OCCI SparseVector of type doubles.
Parent topic: Sparse Vectors
getInt8Vector()
Gets the value of an Oracle AI Database sparse vector data type in int8(ub1) format as an OCCI SparseVector of type int8_t.
Syntax
void getInt8Vector(unsigned int paramIndex, SparseVector<int8_t> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References to the SparseVector (OUT parameter) into which the values must be inserted.
|
Parent topic: PL/SQL out-binds for Sparse Vectors
getFloatVector()
Gets the value of an Oracle AI Database sparse vector data type in IEEE FLOAT32 format as an OCCI SparseVector of type float.
Syntax
void getFloatVector(unsigned int paramIndex, SparseVector<float> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References to the SparseVector (OUT parameter) into which the values must be inserted.
|
Parent topic: PL/SQL out-binds for Sparse Vectors
getDoubleVector()
Gets the value of an Oracle AI Database sparse vector data type in IEEE FLOAT64 format as an OCCI SparseVector of type doubles.
Syntax
void getDoubleVector(unsigned int paramIndex, SparseVector<double> &vec);| Parameter | Description |
|---|---|
paramIndex |
Specifies the parameter index; first parameter is 1, second is 2, and so on. |
vec |
References the SparseVector (OUT parameter) into which the values must be inserted.
|
The following sample code snippet uses this function for out binds to obtain a vector:
Statement *stmt;
stmt = conn->createStatement ("BEGIN occivec_demo(:1,:2,:3) END;");
pair<vector<uint32_t>, vector<double>> myInPair = {{3, 7, 8}, {2.6, 7.6, 1001.1}};
pair<vector<uint32_t>, vector<double>> myInOutPair = {{0, 4, 9}, {3, 5.7, 2001}};
pair<vector<uint32_t>, vector<double>> myOutPair;
SparseVector<uint32_t> myInBind(9, myInPair);
SparseVector<uint32_t> myInOutBind(10, myInOutPair);
SparseVector<uint32_t> myOutBind(0, myOutPair);
stmt->setDoubleVector(1, myInBind);
stmt->setDoubleVector(2, myInOutBind);
stmt->registerOutParam(3, OCCI_SQLT_VEC);
stmt->stmtExecute();
stmt->getDoubleVector(2, myInOutBind);
stmt->getDoubleVector(3, myOutBind);The preceding code snippet gets the data obtained from INOUT bind at position 2 into myInOutBind SparseVector as well as the OUT bind at position 3 in myOutBind.
Parent topic: PL/SQL out-binds for Sparse Vectors
Defining Sparse Vectors from a ResultSet of a Query
This section describes the functions introduced in ResultSet class to obtain
the column values of type SparseVector into C++ stl vector
containers.
- getInt8Vector()
Gets the column value of an Oracle AI Database sparse vector datatype inint8(ub1)as an OCCI SparseVector of typeint8_t. - getFloatVector()
Gets the column value of an Oracle AI Database sparse vector data type inIEEE FLOAT32format as an OCCI SparseVector of type float. - getDoubleVector()
Gets the column value of an Oracle AI Database vector data type inIEEE FLOAT64format as an OCCI SparseVector of type double. - getString()
Gets the column value of an Oracle AI Database sparse vector data type of any format as a C++ string.
Parent topic: Sparse Vectors
getInt8Vector()
Gets the column value of an Oracle AI Database sparse vector datatype in int8(ub1) as an OCCI SparseVector of type int8_t.
Syntax
void getInt8Vector(unsigned int paramIndex, SparseVector<int8_t> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the SparseVector (OUT parameter) into which the values must be inserted.
|
Parent topic: Defining Sparse Vectors from a ResultSet of a Query
getFloatVector()
Gets the column value of an Oracle AI Database sparse vector data type in IEEE FLOAT32 format as an OCCI SparseVector of type float.
Syntax
void getFloatVector(unsigned int paramIndex, SparseVector<float> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the SparseVector (OUT parameter) into which the values must be inserted.
|
Parent topic: Defining Sparse Vectors from a ResultSet of a Query
getDoubleVector()
Gets the column value of an Oracle AI Database vector data type in IEEE FLOAT64 format as an OCCI SparseVector of type double.
Syntax
void getDoubleVector(unsigned int paramIndex, SparseVector<double> &vec);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
vec |
References to the SparseVector (OUT parameter) into which the values must be inserted.
|
The following code snippet shows how this function can be used in the ResultSet to obtain a Sparse Vector:
Statement stmt;
stmt = conn->createStatement ("SELECT * FROM TEST_SPARSEVECFLT");
ResultSet *rs = stmt->getResultSet();
pair<vector<uint32_t>,vector<float>> myResult;
SparseVector mySparseVector(0, myResult);
stmt->stmtExecute();
rs->getFloatVector(2, myResult);The type of SparseVector data in result is decided depending on the vector type provided. That is, SparseVector<float> corresponds to FLOAT32 format.
Parent topic: Defining Sparse Vectors from a ResultSet of a Query
getString()
Gets the column value of an Oracle AI Database sparse vector data type of any format as a C++ string.
Syntax
string getString(unsigned int paramIndex);| Parameter | Description |
|---|---|
colIndex |
Specifies the column index, first column is 1, second is 2, and so on. |
The following code snippet shows how to use this function in ResultSet to obtain a vector:
Statement stmt;
stmt = conn->createStatement ("SELECT id,embedding FROM TEST_SPARSEVECFLT");
ResultSet *rs = stmt->getResultSet();
stmt->stmtExecute();
int id = rs->getInt(1);
string vec = rs->getString(2);Parent topic: Defining Sparse Vectors from a ResultSet of a Query
Utility Function for SparseVector
This section describes a member function of the SparseVector class and returns a dense form of the vector stored by your SparseVector.
- toDenseVec()
Returns the dense vector form of an OCCI SparseVector of type<T>.
Parent topic: Vector Support in Oracle AI Database
toDenseVec()
Returns the dense vector form of an OCCI SparseVector of type
<T>.
Syntax
vector<T> toDense();
Note:
HereT is the desired type of vector value.
The following code snippet shows how you can use this function in SparseVector to obtain a dense vector:
SparseVector<float> mySparseVector(10,{0,5,9},{0.5,5.5,9.5});
vector<float> myDenseVector = mySparseVector.toDense();
for(auto i:myDenseVector)
cout<<i<<" ";
cout<<endl;
Parent topic: Utility Function for SparseVector