Skip to content

feat: Add support for cds.Vector on SQLite and Postgres#1676

Open
vkozyura wants to merge 73 commits into
mainfrom
feat/vector-simplified
Open

feat: Add support for cds.Vector on SQLite and Postgres#1676
vkozyura wants to merge 73 commits into
mainfrom
feat/vector-simplified

Conversation

@vkozyura

@vkozyura vkozyura commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add SQLite vector functions: COSINE_SIMILARITY, L2DISTANCE, L2NORMALIZE, VECTOR_EMBEDDING
  • Use synchronous hash-based embedding for VECTOR_EMBEDDING (deterministic, suitable for testing/development)
  • Vector functions work directly in SQL queries, including with column references

Implementation

  • Vector functions defined inline in SQLiteService.js following existing patterns
  • Helper functions in sqlite/lib/vector_handling/index.js
  • Hash-based embedding produces 384-dimensional normalized vectors

Test plan

  • COSINE_SIMILARITY: identical, orthogonal, opposite vectors, null handling
  • L2DISTANCE: identical vectors, unit vectors, known distance
  • L2NORMALIZE: normalizes to unit length, already normalized unchanged
  • VECTOR_EMBEDDING: computes embedding, deterministic, different inputs, null handling
  • Semantic search: ORDER BY similarity, dynamic embedding on columns

@vkozyura vkozyura requested a review from sjvans as a code owner July 3, 2026 09:25
@vkozyura vkozyura changed the title feat(sqlite): add vector functions support feat: Add support for cds.Vector on SQLite and Postgres Jul 3, 2026
Comment thread hana/lib/cql-functions.js
HIERARCHY_ANCESTORS: undefined,

// Ease of use for stakeholders for auto-filling the remote source
vector_embedding(text, text_type, model_and_version, remote_source = cds.env.ai?.embeddings?.remoteSource) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the source of cds.env.ai?.embeddings?.remoteSource ? If this comes from the previous PR or the AI plugin we should probably communicate this with java as well as they are looking into a global option as well.

I would expect also an global setting for model_and_version so that it doesn't need to be hardcoded into models.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cds.env.ai.embeddings.remoteSource configuration is introduced by the @cap-js/ai.
Java stack has no ai property in CdsProperties.java. The Java test hardcodes the model.

The Parameter model_and_version is called embeddingModel in Java.

Suggestion: Sync with Java as follow-up

Comment thread postgres/lib/PostgresService.js Outdated
Comment on lines +54 to +60
try {
await dbc.query('CREATE EXTENSION IF NOT EXISTS vector')
await pgvector.registerTypes(dbc)
} catch (e) {
const LOG = cds.log('postgres')
LOG.debug('pgvector extension not available, skipping vector support:', e.message)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be ran every create. This might not even need to run at all depending on how the postgres build task is deploying the model.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented out with a revisit containing possible options - pg tests are currently disabled

Comment thread postgres/lib/PostgresService.js Outdated
Comment thread postgres/package.json Outdated
Comment thread postgres/pg-stack.yml Outdated
Comment thread sqlite/lib/vector_handling/index.js Outdated
Comment thread test/compliance/functions.test.js Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants