mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-29 05:54:21 +01:00
add convert operation
This commit is contained in:
@@ -35,6 +35,7 @@ pub fn vector32(args: &[Register]) -> Result<Value> {
|
||||
));
|
||||
}
|
||||
let vector = parse_vector(&args[0], Some(VectorType::Float32Dense))?;
|
||||
let vector = operations::convert::vector_convert(vector, VectorType::Float32Dense)?;
|
||||
Ok(operations::serialize::vector_serialize(vector))
|
||||
}
|
||||
|
||||
@@ -45,6 +46,7 @@ pub fn vector64(args: &[Register]) -> Result<Value> {
|
||||
));
|
||||
}
|
||||
let vector = parse_vector(&args[0], Some(VectorType::Float64Dense))?;
|
||||
let vector = operations::convert::vector_convert(vector, VectorType::Float64Dense)?;
|
||||
Ok(operations::serialize::vector_serialize(vector))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use crate::{vector::vector_types::Vector, LimboError, Result};
|
||||
use crate::{
|
||||
vector::vector_types::{Vector, VectorType},
|
||||
LimboError, Result,
|
||||
};
|
||||
|
||||
pub fn vector_concat(v1: &Vector, v2: &Vector) -> Result<Vector> {
|
||||
if v1.vector_type != v2.vector_type {
|
||||
@@ -7,9 +10,14 @@ pub fn vector_concat(v1: &Vector, v2: &Vector) -> Result<Vector> {
|
||||
));
|
||||
}
|
||||
|
||||
let mut data = Vec::with_capacity(v1.data.len() + v2.data.len());
|
||||
data.extend_from_slice(&v1.data);
|
||||
data.extend_from_slice(&v2.data);
|
||||
let data = match v1.vector_type {
|
||||
VectorType::Float32Dense | VectorType::Float64Dense => {
|
||||
let mut data = Vec::with_capacity(v1.data.len() + v2.data.len());
|
||||
data.extend_from_slice(&v1.data);
|
||||
data.extend_from_slice(&v2.data);
|
||||
data
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Vector {
|
||||
vector_type: v1.vector_type,
|
||||
|
||||
25
core/vector/operations/convert.rs
Normal file
25
core/vector/operations/convert.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use crate::{
|
||||
vector::vector_types::{Vector, VectorType},
|
||||
Result,
|
||||
};
|
||||
|
||||
pub fn vector_convert(v: Vector, target_type: VectorType) -> Result<Vector> {
|
||||
match (v.vector_type, target_type) {
|
||||
(VectorType::Float32Dense, VectorType::Float32Dense)
|
||||
| (VectorType::Float64Dense, VectorType::Float64Dense) => Ok(v),
|
||||
(VectorType::Float32Dense, VectorType::Float64Dense) => {
|
||||
let mut data = Vec::with_capacity(v.dims * 8);
|
||||
for &x in v.as_f32_slice() {
|
||||
data.extend_from_slice(&f64::to_le_bytes(x as f64));
|
||||
}
|
||||
Vector::from_data(target_type, data)
|
||||
}
|
||||
(VectorType::Float64Dense, VectorType::Float32Dense) => {
|
||||
let mut data = Vec::with_capacity(v.dims * 4);
|
||||
for &x in v.as_f32_slice() {
|
||||
data.extend_from_slice(&f64::to_le_bytes(x as f64));
|
||||
}
|
||||
Vector::from_data(target_type, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod concat;
|
||||
pub mod deserialize;
|
||||
pub mod convert;
|
||||
pub mod distance_cos;
|
||||
pub mod distance_l2;
|
||||
pub mod serialize;
|
||||
|
||||
Reference in New Issue
Block a user