feat: add SQL Server 2025 VECTOR type support - #307
feat: add SQL Server 2025 VECTOR type support#307David Levy (dlevy-msft-sql) wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #307 +/- ##
===========================================
+ Coverage 79.92% 96.39% +16.46%
===========================================
Files 34 92 +58
Lines 6656 74705 +68049
===========================================
+ Hits 5320 72013 +66693
- Misses 1065 2325 +1260
- Partials 271 367 +96
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR adds native support for SQL Server 2025's VECTOR data type, enabling efficient storage and retrieval of vector embeddings for AI/ML workloads. The implementation includes Vector and NullVector types that implement standard database interfaces, support for both float32 and float16 element types, binary format encoding/decoding, JSON format transmission for parameterized queries, and comprehensive test coverage with graceful degradation for pre-2025 servers.
Changes:
- Added Vector and NullVector types with driver.Valuer and sql.Scanner interface implementations
- Implemented binary format encoding/decoding matching SQL Server's native TDS format
- Added float16 conversion functions with IEEE 754 half-precision support
- Extended types.go with typeVectorN constant and read/write functions for vector data handling
- Updated parameter binding in mssql.go to transmit vectors as JSON strings for backward compatibility
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| vector.go | Core Vector type implementation with encoding/decoding, float16 conversion, and helper functions |
| vector_test.go | Comprehensive unit tests for Vector type including encoding, decoding, and edge cases |
| vector_db_test.go | Database integration tests with SQL Server 2025+ version detection and preview feature handling |
| types.go | Added typeVectorN constant and TDS stream read/write functions for vector data |
| mssql.go | Extended makeParam to handle Vector/NullVector types via JSON string transmission |
| doc/how-to-use-vectors.md | Complete usage documentation with examples and best practices |
| README.md | Added Vector type to supported features list |
| CHANGELOG.md | Documented new feature in version 1.9.4 |
|
Copilot what's the expected behavior if an app tries to scan a vector to |
SQL Server 2025 VECTOR Type Support
Overview
This PR adds native support for SQL Server 2025's VECTOR data type, enabling efficient storage and retrieval of vector embeddings for AI/ML workloads in Go applications.
The VECTOR type is designed for similarity search scenarios, storing fixed-dimensional arrays of floating-point numbers optimized for operations like
VECTOR_DISTANCE.Features
Core Types
Vector- Vector type implementingdriver.Valuerandsql.ScannerinterfacesNullVector- Nullable wrapper for database columns that allow NULL valuesVectorElementType- Enum for element precision (float32/float16)Element Type Support
Wire Format Support
Framework Compatibility
[]float32and[]float64parameter binding (no wrapper needed)VectorandNullVectortypes implementsql.Scannerfor decoding binary dataElementTypemetadata when scanned toVector/NullVectorConnection String Option
API Summary
Creating Vectors
Inserting Vectors
Reading Vectors
Similarity Search
Files Changed
New Files
Modified Files
Implementation Details
TDS Protocol Integration
featExtVECTORSUPPORT(0x0E) for LOGIN7 negotiationNULL Handling
NullVector{Valid: false}sends asNVARCHAR(1) NULLfor dimension-agnostic NULL insertionSpecial Value Handling
null, decoded back to NaN for round-trip supportValue()since JSON cannot represent ±Inf losslesslyThread Safety
SetVectorPrecisionLossHandler()uses mutex for thread-safe handler updatesDriver Value Types
The
readVectorTypefunction returns[]byte(raw binary vector payload), which is a standarddatabase/sql/driver.Valuetype. This follows Go database driver conventions where drivers return primitive types and custom types handle decoding viasql.Scanner.Applications should scan to
VectororNullVectortypes, which implementsql.Scannerand decode the binary representation automatically.Testing
Unit Tests (
go test ./...)Integration Tests (requires SQL Server 2025)
VECTOR_DISTANCEsimilarity searchPREVIEW_FEATURESTest Coverage
Requirements
ALTER DATABASE SCOPED CONFIGURATION SET PREVIEW_FEATURES = ONMigration Notes
vectortypesupport=offuses JSON formatvectortypesupport=v1for binary format with SQL Server 2025+Related Documentation
Acknowledgments
Thanks to David Shiflet (@shueybubbles) for the review feedback that improved:
[]float32and[]float64parameter support