Working with Vector Data and Oracle SQL Command Line SQLcl

Posted on Updated on

Working with Vector data has been a hot topic over the past couple of years. There are lots and articles and blog posts showing some basic functionality and similarity search using Vectors. Most of these articles demonstrate the basics, but then you start working with Vector data and perhaps having to move this data between schemas or between databases you could face a little bit of a challenge. One of these challenges or perhaps compatibility issues is with the format of the Vector data in the output/input file. In the example below I’m going to use CSV file to illustrate the issue, and specifically with using Oracle SQLcl and the export/import to/from CSV file feature.

It should be noted that the issued described below might not be an issue in a future release of SQLcl. I’ve reported this issue and a bug report has been filled. So fingers cross it will be fixed soon and even back ported. So the workaround or fix illustrated below might not be necessary at some point in the future, but until such time the workaround below is required.

The basic secenario that I faced involved exporting a table, containing Vector data, and importing it into a different (empty) table in the same schema. The export feature seemed to work correctly but I get an error when importing the file. Something about the work data type etc. It was complaining about the Vector column format. After a little investigation, the export command exported the data but the Vector column did not have quotes around the data. The import command expected the Vector data to be in quotes. So there is a mismatch between the export and import commands for Vector data.

Here is a bit more detail on how I encountered this issue.

I created a table to store Wine Reviews, Imported the Wine Review data set. added a column to store the Vector Data, added the Vector data using a model I’d already imported. I then exported the Wine Reviews with the Vector data to CSV. I unloaded the table using the following SQL Command Line function UNLOAD

sql> unload wine_reviews

This created a file in my working directory called WINEREVIEWS_TABLE.csv

I then created a new version of the table with a different name, WINEREVIEWS_VEC, and used the LOAD function to load the CSV data into the the newly created table.

sql> load WINEREVIEWS_VEC WINEREVIEWS_TABLE.csv

This gives me the following error.

#ERROR Insert failed in batch rows 1 through 50
#ERROR ORA-51804: Invalid syntax for VECTOR value. Must be a string of the form
DENSE vector: '[<number> , <number> ..., <number>]'
SPARSE vector: '[<total dimension count (optional)>, [<sparse index array>], [<sparse dimension value array>]]'.

What this error means is the Vector data is missing a quote at the beginning and end of the vector data.

So what can we do to fix this issue and move forward with import and being able to use the data.

In my case I used the ‘sed’ command, and here is the one line ‘sed’ command to add the quotes around the required field.

sed 's/,\(\[[^]]*\]\)/,"\1"/g' WINEREVIEWS_TABLE.csv > WINEREVIEWS_TABLE_vec.csv

No using he same LOAD function as previously used, the import/load works and is completed in a couple of seconds.

Before we end this post, lets have another look at the ‘sed’ command and what it does, as you can see it is a little complicated.

sed 's/,\(\[[^]]*\]\)/,"\1"/g' WINEREVIEWS_TABLE.csv > WINEREVIEWS_TABLE_vec.csv

The main focus of this command is to look for fields that are enclosed by square brackets [ ]. When you look at the CSV file, only the vector data is enclose in such brackets. what we need to do is to wrap this in quotes, as otherwise the comma in these brackets will be considered as field/column separators.

Breaking down the pattern ,\(\[[^]]*\]\)

So the whole thing matches: a comma, immediately followed by a bracketed block like [anything except ]], and captures that bracketed block (brackets included) as group 1.

The replacement ,"\1" puts back the comma, then wraps the captured bracket block in double quotes.

The g flag makes it do this for every match on the line, not just the first.