feat: add SQL Server 2025 VECTOR type support#307
feat: add SQL Server 2025 VECTOR type support#307dlevy-msft-sql wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #307 +/- ##
==========================================
+ Coverage 75.36% 77.05% +1.68%
==========================================
Files 34 35 +1
Lines 6597 7213 +616
==========================================
+ Hits 4972 5558 +586
- Misses 1337 1364 +27
- Partials 288 291 +3 ☔ View full report in Codecov by Sentry. 🚀 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 |
d658f54 to
4d75a8c
Compare
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 @shueybubbles for the review feedback that improved:
[]float32and[]float64parameter support