gqlite 1.5.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/ext/Cargo.toml +12 -6
  3. data/ext/db-index/Cargo.toml +29 -0
  4. data/ext/db-index/src/hnsw/error.rs +66 -0
  5. data/ext/db-index/src/hnsw/graph_store.rs +272 -0
  6. data/ext/db-index/src/hnsw/id.rs +36 -0
  7. data/ext/db-index/src/hnsw/implementation.rs +1517 -0
  8. data/ext/db-index/src/hnsw/kernels.rs +1228 -0
  9. data/ext/db-index/src/hnsw/metric.rs +244 -0
  10. data/ext/db-index/src/hnsw/scalar.rs +72 -0
  11. data/ext/db-index/src/hnsw/simple_store.rs +140 -0
  12. data/ext/db-index/src/hnsw/store.rs +472 -0
  13. data/ext/db-index/src/hnsw/vector.rs +105 -0
  14. data/ext/db-index/src/hnsw/vectors.rs +568 -0
  15. data/ext/db-index/src/hnsw.rs +42 -0
  16. data/ext/db-index/src/lib.rs +3 -0
  17. data/ext/gqlitedb/Cargo.toml +19 -8
  18. data/ext/gqlitedb/benches/common/pokec.rs +62 -2
  19. data/ext/gqlitedb/benches/pokec_divan.rs +66 -2
  20. data/ext/gqlitedb/benches/pokec_iai.rs +60 -3
  21. data/ext/gqlitedb/release.toml +2 -2
  22. data/ext/gqlitedb/src/aggregators/arithmetic.rs +3 -1
  23. data/ext/gqlitedb/src/aggregators/containers.rs +1 -1
  24. data/ext/gqlitedb/src/aggregators/stats.rs +21 -12
  25. data/ext/gqlitedb/src/capi.rs +20 -24
  26. data/ext/gqlitedb/src/compiler/expression_analyser.rs +28 -20
  27. data/ext/gqlitedb/src/compiler/variables_manager.rs +104 -38
  28. data/ext/gqlitedb/src/compiler.rs +506 -227
  29. data/ext/gqlitedb/src/connection.rs +151 -11
  30. data/ext/gqlitedb/src/consts.rs +1 -2
  31. data/ext/gqlitedb/src/error.rs +123 -61
  32. data/ext/gqlitedb/src/functions/common.rs +64 -0
  33. data/ext/gqlitedb/src/functions/containers.rs +2 -46
  34. data/ext/gqlitedb/src/functions/edge.rs +1 -1
  35. data/ext/gqlitedb/src/functions/math.rs +87 -15
  36. data/ext/gqlitedb/src/functions/node.rs +1 -1
  37. data/ext/gqlitedb/src/functions/path.rs +48 -11
  38. data/ext/gqlitedb/src/functions/scalar.rs +47 -4
  39. data/ext/gqlitedb/src/functions/string.rs +123 -1
  40. data/ext/gqlitedb/src/functions/value.rs +1 -1
  41. data/ext/gqlitedb/src/functions.rs +165 -28
  42. data/ext/gqlitedb/src/graph.rs +1 -9
  43. data/ext/gqlitedb/src/interpreter/evaluators.rs +968 -130
  44. data/ext/gqlitedb/src/interpreter/instructions.rs +39 -2
  45. data/ext/gqlitedb/src/lib.rs +5 -4
  46. data/ext/gqlitedb/src/parser/gql.pest +1 -1
  47. data/ext/gqlitedb/src/planner.rs +329 -0
  48. data/ext/gqlitedb/src/prelude.rs +3 -3
  49. data/ext/gqlitedb/src/store/pgrx.rs +1 -1
  50. data/ext/gqlitedb/src/store/postgres.rs +742 -16
  51. data/ext/gqlitedb/src/store/redb/hnsw_store.rs +702 -0
  52. data/ext/gqlitedb/src/store/redb/index.rs +274 -0
  53. data/ext/gqlitedb/src/store/redb.rs +1268 -113
  54. data/ext/gqlitedb/src/store/sqlbase/sqlmetadata.rs +28 -0
  55. data/ext/gqlitedb/src/store/sqlbase/sqlstore.rs +103 -0
  56. data/ext/gqlitedb/src/store/sqlbase.rs +146 -16
  57. data/ext/gqlitedb/src/store/sqlite.rs +576 -14
  58. data/ext/gqlitedb/src/store/vector_extract.rs +56 -0
  59. data/ext/gqlitedb/src/store.rs +140 -29
  60. data/ext/gqlitedb/src/tests/compiler.rs +207 -10
  61. data/ext/gqlitedb/src/tests/connection/postgres.rs +38 -3
  62. data/ext/gqlitedb/src/tests/connection/redb.rs +23 -0
  63. data/ext/gqlitedb/src/tests/connection/sqlite.rs +31 -0
  64. data/ext/gqlitedb/src/tests/connection.rs +61 -1
  65. data/ext/gqlitedb/src/tests/evaluators.rs +162 -7
  66. data/ext/gqlitedb/src/tests/parser.rs +54 -23
  67. data/ext/gqlitedb/src/tests/planner.rs +511 -0
  68. data/ext/gqlitedb/src/tests/store/postgres.rs +7 -0
  69. data/ext/gqlitedb/src/tests/store/redb.rs +8 -0
  70. data/ext/gqlitedb/src/tests/store/sqlite.rs +8 -0
  71. data/ext/gqlitedb/src/tests/store/vector_index/postgres.rs +182 -0
  72. data/ext/gqlitedb/src/tests/store/vector_index/redb.rs +386 -0
  73. data/ext/gqlitedb/src/tests/store/vector_index/sqlite.rs +166 -0
  74. data/ext/gqlitedb/src/tests/store/vector_index.rs +2313 -0
  75. data/ext/gqlitedb/src/tests/store.rs +78 -14
  76. data/ext/gqlitedb/src/tests/templates/ast.rs +92 -16
  77. data/ext/gqlitedb/src/tests/templates/programs.rs +14 -7
  78. data/ext/gqlitedb/src/tests.rs +15 -9
  79. data/ext/gqlitedb/src/utils.rs +6 -1
  80. data/ext/gqlitedb/src/value/compare.rs +61 -3
  81. data/ext/gqlitedb/src/value.rs +136 -7
  82. data/ext/gqlitedb/templates/sql/postgres/metadata_delete.sql +1 -0
  83. data/ext/gqlitedb/templates/sql/postgres/node_select.sql +1 -1
  84. data/ext/gqlitedb/templates/sql/sqlite/metadata_delete.sql +1 -0
  85. data/ext/gqliterb/src/lib.rs +53 -8
  86. data/ext/gqlparser/Cargo.toml +25 -0
  87. data/ext/gqlparser/README.MD +9 -0
  88. data/ext/gqlparser/benches/pokec_divan.rs +34 -0
  89. data/ext/gqlparser/src/common.rs +69 -0
  90. data/ext/gqlparser/src/gqls/ast.rs +69 -0
  91. data/ext/gqlparser/src/gqls/constraint.rs +79 -0
  92. data/ext/gqlparser/src/gqls/error.rs +22 -0
  93. data/ext/gqlparser/src/gqls/parser.rs +813 -0
  94. data/ext/gqlparser/src/gqls/prelude.rs +8 -0
  95. data/ext/gqlparser/src/gqls/properties.rs +115 -0
  96. data/ext/gqlparser/src/gqls/resolve.rs +207 -0
  97. data/ext/gqlparser/src/gqls.rs +265 -0
  98. data/ext/gqlparser/src/lib.rs +7 -0
  99. data/ext/gqlparser/src/oc/ast.rs +680 -0
  100. data/ext/gqlparser/src/oc/error.rs +172 -0
  101. data/ext/gqlparser/src/oc/lexer.rs +429 -0
  102. data/ext/gqlparser/src/oc/parser/tests.rs +2284 -0
  103. data/ext/gqlparser/src/oc/parser.rs +2005 -0
  104. data/ext/gqlparser/src/oc.rs +33 -0
  105. data/ext/gqlparser/src/prelude.rs +3 -0
  106. data/ext/graphcore/Cargo.toml +1 -0
  107. data/ext/graphcore/src/error.rs +26 -0
  108. data/ext/graphcore/src/graph.rs +177 -51
  109. data/ext/graphcore/src/lib.rs +4 -2
  110. data/ext/graphcore/src/open_cypher.rs +12 -0
  111. data/ext/graphcore/src/table.rs +50 -2
  112. data/ext/graphcore/src/timestamp.rs +127 -104
  113. data/ext/graphcore/src/value/tensor.rs +739 -0
  114. data/ext/graphcore/src/value/value_map.rs +1 -1
  115. data/ext/graphcore/src/value.rs +343 -19
  116. metadata +93 -28
  117. data/ext/gqlitedb/src/parser/ast.rs +0 -604
  118. data/ext/gqlitedb/src/parser/parser_impl.rs +0 -1213
  119. data/ext/gqlitedb/src/parser.rs +0 -4
@@ -0,0 +1,244 @@
1
+ use super::kernels::cosine_distance;
2
+ use super::kernels::cosine_distance_qi8;
3
+ use super::kernels::inner_product_distance;
4
+ use super::kernels::inner_product_distance_qi8;
5
+ use super::kernels::l2_sq_qi8;
6
+ use super::kernels::Kernel;
7
+ use super::scalar::Scalar;
8
+ use super::vector::Dense;
9
+ use super::vector::Qi8;
10
+ use super::vector::Qi8Ref;
11
+ use super::vector::VectorFamily;
12
+ use std::marker::PhantomData;
13
+
14
+ pub trait Metric: Clone + Send + Sync + 'static
15
+ {
16
+ type Family: VectorFamily;
17
+ fn distance<'a, 'b>(
18
+ &self,
19
+ a: <Self::Family as VectorFamily>::Ref<'a>,
20
+ b: <Self::Family as VectorFamily>::Ref<'b>,
21
+ ) -> f32;
22
+ }
23
+
24
+ #[derive(Clone, Debug, Default)]
25
+ pub struct L2<S: Scalar = f32>(PhantomData<S>);
26
+
27
+ impl<S: Scalar> L2<S>
28
+ {
29
+ pub fn new() -> Self
30
+ {
31
+ Self(PhantomData)
32
+ }
33
+ }
34
+
35
+ impl<S: Scalar> Metric for L2<S>
36
+ {
37
+ type Family = Dense<S>;
38
+
39
+ fn distance(&self, a: &[S], b: &[S]) -> f32
40
+ {
41
+ debug_assert_eq!(a.len(), b.len());
42
+ <S as Kernel>::l2_sq(a, b)
43
+ }
44
+ }
45
+
46
+ #[derive(Clone, Debug, Default)]
47
+ pub struct InnerProduct<S: Scalar = f32>(PhantomData<S>);
48
+
49
+ impl<S: Scalar> InnerProduct<S>
50
+ {
51
+ pub fn new() -> Self
52
+ {
53
+ Self(PhantomData)
54
+ }
55
+ }
56
+
57
+ impl<S: Scalar> Metric for InnerProduct<S>
58
+ {
59
+ type Family = Dense<S>;
60
+
61
+ fn distance(&self, a: &[S], b: &[S]) -> f32
62
+ {
63
+ debug_assert_eq!(a.len(), b.len());
64
+ inner_product_distance::<S>(a, b)
65
+ }
66
+ }
67
+
68
+ #[derive(Clone, Debug, Default)]
69
+ pub struct Cosine<S: Scalar = f32>(PhantomData<S>);
70
+
71
+ impl<S: Scalar> Cosine<S>
72
+ {
73
+ pub fn new() -> Self
74
+ {
75
+ Self(PhantomData)
76
+ }
77
+ }
78
+
79
+ impl<S: Scalar> Metric for Cosine<S>
80
+ {
81
+ type Family = Dense<S>;
82
+
83
+ fn distance(&self, a: &[S], b: &[S]) -> f32
84
+ {
85
+ debug_assert_eq!(a.len(), b.len());
86
+ cosine_distance::<S>(a, b)
87
+ }
88
+ }
89
+
90
+ #[derive(Clone, Debug, Default)]
91
+ pub struct L2Qi8;
92
+
93
+ impl L2Qi8
94
+ {
95
+ pub fn new() -> Self
96
+ {
97
+ Self
98
+ }
99
+ }
100
+
101
+ impl Metric for L2Qi8
102
+ {
103
+ type Family = Qi8;
104
+
105
+ fn distance<'a, 'b>(&self, a: Qi8Ref<'a>, b: Qi8Ref<'b>) -> f32
106
+ {
107
+ l2_sq_qi8(a.data, a.scale, a.zero_point, b.data, b.scale, b.zero_point)
108
+ }
109
+ }
110
+
111
+ #[derive(Clone, Debug, Default)]
112
+ pub struct InnerProductQi8;
113
+
114
+ impl InnerProductQi8
115
+ {
116
+ pub fn new() -> Self
117
+ {
118
+ Self
119
+ }
120
+ }
121
+
122
+ impl Metric for InnerProductQi8
123
+ {
124
+ type Family = Qi8;
125
+
126
+ fn distance<'a, 'b>(&self, a: Qi8Ref<'a>, b: Qi8Ref<'b>) -> f32
127
+ {
128
+ inner_product_distance_qi8(a.data, a.scale, a.zero_point, b.data, b.scale, b.zero_point)
129
+ }
130
+ }
131
+
132
+ #[derive(Clone, Debug, Default)]
133
+ pub struct CosineQi8;
134
+
135
+ impl CosineQi8
136
+ {
137
+ pub fn new() -> Self
138
+ {
139
+ Self
140
+ }
141
+ }
142
+
143
+ impl Metric for CosineQi8
144
+ {
145
+ type Family = Qi8;
146
+
147
+ fn distance<'a, 'b>(&self, a: Qi8Ref<'a>, b: Qi8Ref<'b>) -> f32
148
+ {
149
+ cosine_distance_qi8(a.data, a.scale, a.zero_point, b.data, b.scale, b.zero_point)
150
+ }
151
+ }
152
+
153
+ pub fn normalize_cosine_in_place<S: Scalar>(vector: &mut [S])
154
+ {
155
+ let mut norm_sq = 0.0_f32;
156
+ for &v in vector.iter()
157
+ {
158
+ let v = v.to_f32();
159
+ norm_sq += v * v;
160
+ }
161
+ if norm_sq == 0.0
162
+ {
163
+ return;
164
+ }
165
+ let inv_norm = norm_sq.sqrt().recip();
166
+ for v in vector.iter_mut()
167
+ {
168
+ *v = S::from_f32(v.to_f32() * inv_norm);
169
+ }
170
+ }
171
+
172
+ #[cfg(test)]
173
+ mod tests
174
+ {
175
+ use super::*;
176
+ use approx::assert_relative_eq;
177
+ use rand_09::prelude::Rng;
178
+ use rand_09::rngs::StdRng;
179
+ use rand_09::SeedableRng;
180
+
181
+ fn l2_ref(a: &[f32], b: &[f32]) -> f32
182
+ {
183
+ a.iter()
184
+ .zip(b.iter())
185
+ .map(|(a, b)| {
186
+ let d = a - b;
187
+ d * d
188
+ })
189
+ .sum()
190
+ }
191
+
192
+ fn ip_ref(a: &[f32], b: &[f32]) -> f32
193
+ {
194
+ 1.0 - a.iter().zip(b.iter()).map(|(a, b)| a * b).sum::<f32>()
195
+ }
196
+
197
+ #[test]
198
+ fn l2_matches_scalar_across_dims()
199
+ {
200
+ let mut rng = StdRng::seed_from_u64(123);
201
+ let dims = [
202
+ 1usize, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
203
+ ];
204
+ let metric = L2::<f32>::new();
205
+ for &dim in &dims
206
+ {
207
+ for _ in 0..100
208
+ {
209
+ let a: Vec<f32> = (0..dim).map(|_| rng.random_range(-1.0..1.0)).collect();
210
+ let b: Vec<f32> = (0..dim).map(|_| rng.random_range(-1.0..1.0)).collect();
211
+ assert_relative_eq!(
212
+ metric.distance(&a, &b),
213
+ l2_ref(&a, &b),
214
+ epsilon = 1e-3,
215
+ max_relative = 1e-3
216
+ );
217
+ }
218
+ }
219
+ }
220
+
221
+ #[test]
222
+ fn inner_product_matches_scalar_across_dims()
223
+ {
224
+ let mut rng = StdRng::seed_from_u64(456);
225
+ let dims = [
226
+ 1usize, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
227
+ ];
228
+ let metric = InnerProduct::<f32>::new();
229
+ for &dim in &dims
230
+ {
231
+ for _ in 0..100
232
+ {
233
+ let a: Vec<f32> = (0..dim).map(|_| rng.random_range(-1.0..1.0)).collect();
234
+ let b: Vec<f32> = (0..dim).map(|_| rng.random_range(-1.0..1.0)).collect();
235
+ assert_relative_eq!(
236
+ metric.distance(&a, &b),
237
+ ip_ref(&a, &b),
238
+ epsilon = 1e-3,
239
+ max_relative = 1e-3
240
+ );
241
+ }
242
+ }
243
+ }
244
+ }
@@ -0,0 +1,72 @@
1
+ use super::kernels::Kernel;
2
+ use bytemuck::Pod;
3
+ use serde::Deserialize;
4
+ use serde::Serialize;
5
+
6
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7
+ #[repr(u8)]
8
+ pub enum Dtype
9
+ {
10
+ F32 = 0,
11
+ F16 = 1,
12
+ BF16 = 2,
13
+ QI8 = 3,
14
+ }
15
+
16
+ pub trait Scalar: Kernel + Pod
17
+ {
18
+ const DTYPE: Dtype;
19
+ fn to_f32(self) -> f32;
20
+ fn from_f32(v: f32) -> Self;
21
+ }
22
+
23
+ impl Scalar for f32
24
+ {
25
+ const DTYPE: Dtype = Dtype::F32;
26
+
27
+ #[inline]
28
+ fn to_f32(self) -> f32
29
+ {
30
+ self
31
+ }
32
+
33
+ #[inline]
34
+ fn from_f32(v: f32) -> Self
35
+ {
36
+ v
37
+ }
38
+ }
39
+
40
+ impl Scalar for half::f16
41
+ {
42
+ const DTYPE: Dtype = Dtype::F16;
43
+
44
+ #[inline]
45
+ fn to_f32(self) -> f32
46
+ {
47
+ self.to_f32()
48
+ }
49
+
50
+ #[inline]
51
+ fn from_f32(v: f32) -> Self
52
+ {
53
+ Self::from_f32(v)
54
+ }
55
+ }
56
+
57
+ impl Scalar for half::bf16
58
+ {
59
+ const DTYPE: Dtype = Dtype::BF16;
60
+
61
+ #[inline]
62
+ fn to_f32(self) -> f32
63
+ {
64
+ self.to_f32()
65
+ }
66
+
67
+ #[inline]
68
+ fn from_f32(v: f32) -> Self
69
+ {
70
+ Self::from_f32(v)
71
+ }
72
+ }
@@ -0,0 +1,140 @@
1
+ use super::error::Result;
2
+ use super::graph_store::{GraphStoreRead, GraphStoreWrite};
3
+ use crate::hnsw::id::NodeId;
4
+ use std::collections::HashMap;
5
+ use std::hash::Hash;
6
+
7
+ /// Reference implementation of a graph store.
8
+ ///
9
+ /// A simple, single-threaded, growable store with no locks or atomics.
10
+ /// Provides conformance and testing baseline for the algorithm.
11
+ pub struct SimpleGraphStore<K>
12
+ {
13
+ levels: Vec<Option<i32>>, // None == removed or never set
14
+ keys: Vec<Option<K>>,
15
+ links: Vec<Vec<Vec<NodeId>>>, // [node][level] -> neighbor ids
16
+ by_key: HashMap<K, NodeId>,
17
+ entry: Option<(NodeId, i32)>,
18
+ next_node_id: usize, // monotonic, never decremented
19
+ }
20
+
21
+ impl<K> SimpleGraphStore<K>
22
+ {
23
+ pub fn new() -> Self
24
+ {
25
+ Self {
26
+ levels: Vec::new(),
27
+ keys: Vec::new(),
28
+ links: Vec::new(),
29
+ by_key: HashMap::new(),
30
+ entry: None,
31
+ next_node_id: 0,
32
+ }
33
+ }
34
+ }
35
+
36
+ impl<K: Clone + Eq + Hash> GraphStoreRead<K> for SimpleGraphStore<K>
37
+ {
38
+ type Error = std::convert::Infallible;
39
+
40
+ fn node_count(&self) -> Result<usize, Self::Error>
41
+ {
42
+ Ok(self.next_node_id)
43
+ }
44
+
45
+ fn entry_point(&self) -> Result<Option<(NodeId, i32)>, Self::Error>
46
+ {
47
+ Ok(self.entry)
48
+ }
49
+
50
+ fn contains(&self, n: NodeId) -> Result<bool, Self::Error>
51
+ {
52
+ Ok(self.levels.get(n.as_usize()).is_some_and(|l| l.is_some()))
53
+ }
54
+
55
+ fn key_of(&self, n: NodeId) -> Result<Option<K>, Self::Error>
56
+ {
57
+ Ok(self.keys.get(n.as_usize()).and_then(|k| k.clone()))
58
+ }
59
+
60
+ fn node_of(&self, key: &K) -> Result<Option<NodeId>, Self::Error>
61
+ {
62
+ Ok(self.by_key.get(key).copied())
63
+ }
64
+
65
+ fn level(&self, n: NodeId) -> Result<i32, Self::Error>
66
+ {
67
+ Ok(
68
+ self
69
+ .levels
70
+ .get(n.as_usize())
71
+ .copied()
72
+ .flatten()
73
+ .expect("level of absent node"),
74
+ )
75
+ }
76
+
77
+ fn neighbors(&self, n: NodeId, level: usize, out: &mut Vec<NodeId>) -> Result<(), Self::Error>
78
+ {
79
+ out.clear();
80
+ if let Some(per_level) = self.links.get(n.as_usize())
81
+ {
82
+ if let Some(list) = per_level.get(level)
83
+ {
84
+ out.extend_from_slice(list);
85
+ }
86
+ }
87
+ Ok(())
88
+ }
89
+ }
90
+
91
+ impl<K: Clone + Eq + Hash> GraphStoreWrite<K> for SimpleGraphStore<K>
92
+ {
93
+ fn insert_node(&mut self, key: K, level: i32) -> Result<NodeId, Self::Error>
94
+ {
95
+ let id = NodeId::from_u32(self.next_node_id as u32);
96
+ self.next_node_id += 1;
97
+ self.levels.push(Some(level));
98
+ self.keys.push(Some(key.clone()));
99
+ self.links.push(vec![Vec::new(); (level as usize) + 1]);
100
+ self.by_key.insert(key, id);
101
+ Ok(id)
102
+ }
103
+
104
+ fn set_neighbors(&mut self, n: NodeId, level: usize, nbrs: &[NodeId]) -> Result<(), Self::Error>
105
+ {
106
+ let per_level = &mut self.links[n.as_usize()];
107
+ if per_level.len() <= level
108
+ {
109
+ per_level.resize(level + 1, Vec::new());
110
+ }
111
+ per_level[level] = nbrs.to_vec();
112
+ Ok(())
113
+ }
114
+
115
+ fn set_entry_point(&mut self, ep: Option<(NodeId, i32)>) -> Result<(), Self::Error>
116
+ {
117
+ self.entry = ep;
118
+ Ok(())
119
+ }
120
+
121
+ fn remove_node(&mut self, n: NodeId) -> Result<(), Self::Error>
122
+ {
123
+ let i = n.as_usize();
124
+ if let Some(k) = self.keys[i].take()
125
+ {
126
+ self.by_key.remove(&k);
127
+ }
128
+ self.levels[i] = None;
129
+ self.links[i].clear();
130
+ Ok(())
131
+ }
132
+ }
133
+
134
+ impl<K> Default for SimpleGraphStore<K>
135
+ {
136
+ fn default() -> Self
137
+ {
138
+ Self::new()
139
+ }
140
+ }