Now accepting guest editorial from SAP, Oracle, Workday and ServiceNow practitioners.Pitch a guide
The Sign-Off Table

Lesson 3 of 6

The SAP HANA Cloud vector engine for custom retrieval

9 min

Listen to this lesson

0:00 / 7:28

Managed document grounding covers many cases. Sometimes you need more control: your own ranking, joins between vectors and business data, or filters on columns such as plant or release. For that, SAP HANA Cloud has a built-in vector engine. You store embeddings in table columns, query them with SQL, and combine similarity with ordinary WHERE clauses. This lesson covers the pieces you need and the gotchas that SAP's Vector Engine Guide calls out.

Vector data types

SAP HANA Cloud has two vector types. REAL_VECTOR(n) stores single-precision (32-bit) floating-point elements; HALF_VECTOR(n) stores half-precision elements, which halves storage at the cost of precision and range. The optional dimension n can be anything from 1 to 65,000. If you leave it out, the column accepts vectors of any valid dimension, which is convenient for experiments and risky in production, because vectors from two different embedding models can't be compared meaningfully.

Rule from the Vector Engine GuideWhat it means for your design
Vector columns are not supported in row tablesCreate a COLUMN TABLE for chunks and embeddings
No order is defined on vectorsYou can't GROUP BY, ORDER BY or compare vector columns directly; order by a similarity function instead
A vector column can't be a partitioning keyPartition by something else, such as a document set or date
Import and export work in CSV and ParquetPlan reloads and backups with those formats

Constraints that shape a vector table.

Creating embeddings inside the database

You can create embeddings outside HANA (for example, with an embedding model deployed in the generative AI hub) and insert them, or you can let HANA do it with the VECTOR_EMBEDDING function. The function takes the text, a text type and a model with version. The text type matters: use 'DOCUMENT' for stored passages and 'QUERY' for questions, because the model encodes them differently.

Without a remote source, the function uses HANA's internal natural language processing (NLP) service, which must be enabled. SAP lists models such as SAP_NEB.20240715 and SAP_GXY.20250407, both with 768 dimensions; query the NLP_VECTOR_EMBEDDING_MODELS system view for the current list. With a remote source that points to SAP AI Core, the same function calls an embedding model in the generative AI hub; the user needs EXECUTE on that remote source.

Here is the shape of Contoso's chunk table and a top-k query, written as plain SQL:
CREATE COLUMN TABLE CMC_RAG.DOC_CHUNKS (CHUNK_ID INTEGER, DOC_ID NVARCHAR(40), DOC_TYPE NVARCHAR(20), PROCESS NVARCHAR(20), SECURITY_GROUP NVARCHAR(40), CHUNK_TEXT NCLOB, EMB REAL_VECTOR(768));
SELECT TOP 5 DOC_ID, CHUNK_TEXT, COSINE_SIMILARITY(EMB, VECTOR_EMBEDDING(:question, 'QUERY', 'SAP_NEB.20240715')) AS SIM FROM CMC_RAG.DOC_CHUNKS WHERE PROCESS = 'O2C' AND SECURITY_GROUP IN (:user_groups) ORDER BY SIM DESC;

Keep document metadata in its own table and copy the fields you filter on, such as process and security group, onto every chunk.
The Sign-Off Table · thesignofftable.com/academy/sap-ai-204

Similarity functions and indexes

Two similarity measures are built in. COSINE_SIMILARITY returns higher values for more similar vectors, so you sort descending. L2DISTANCE returns lower values for more similar vectors, so you sort ascending. Use the measure your embedding model was designed for; cosine is the usual choice for text.

Without an index, HANA compares the question vector with every row after applying your filters, which is exact but grows with the table. For large tables, create an HNSW (hierarchical navigable small world) index: CREATE HNSW VECTOR INDEX CMC_CHUNK_COS ON CMC_RAG.DOC_CHUNKS (EMB) SIMILARITY FUNCTION COSINE_SIMILARITY, optionally with a build configuration (such as M and efConstruction) and a search configuration (efSearch). SAP's guide stresses three points: the index is approximate and may not return the very best matches; each index serves one similarity measure, and a column can have at most one index per measure; and you can force an exact search with the NO_VECTOR_INDEX hint.

  1. Fewer than tens of thousands of chunks, or strict filters that leave few rows?Yes → exact search without an index is simple and precise · No → consider HNSW.
  2. Is slight recall loss acceptable for speed?Yes → create an HNSW index for the measure you query with · No → keep exact search or compare both.
  3. Did results change after adding the index?Expected: the index is approximate. Rerun your gold set with and without NO_VECTOR_INDEX before go-live.
When to add an HNSW vector index.
The Sign-Off Table · thesignofftable.com/academy/sap-ai-204

Managed grounding or your own HANA tables?

NeedManaged document groundingHANA Cloud vector engine
Fast start from SharePoint, S3, Work Zone and similarStrong fitYou build ingestion yourself
Join similarity with business data (plants, releases, test status)Limited to metadata filtersStrong fit: it's SQL
Custom chunking and rankingUse the Vector APIFull control
Operations effortSAP runs the store and refreshYour team owns schema, refresh, indexes and backups

Contoso starts with managed grounding and uses HANA tables for test evidence, which it wants to join with test status.

What would you do?

Contoso's test evidence table holds 40,000 chunks embedded with SAP_NEB.20240715. A developer wants to embed this week's new evidence with SAP_GXY.20250407 because it scored better in a blog benchmark, keeping the old rows as they are. What should Kenji's team do?

Key points
  • REAL_VECTOR and HALF_VECTOR store embeddings in column tables, with dimensions from 1 to 65,000.
  • VECTOR_EMBEDDING creates embeddings in SQL; use DOCUMENT for stored text and QUERY for questions.
  • COSINE_SIMILARITY sorts descending; L2DISTANCE sorts ascending.
  • HNSW indexes are approximate; NO_VECTOR_INDEX forces an exact search for comparison.

Check your understanding · not graded

Lesson check

  1. 1. Which text type should VECTOR_EMBEDDING use when embedding a tester's question?

  2. 2. A similarity query uses L2DISTANCE. How should results be sorted?

  3. 3. Why might top-k results change after creating an HNSW index?

  4. 4. Which table type can hold a REAL_VECTOR column?