This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.rs
548 lines (487 loc) · 18.3 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! Graph query engine
use crate::{
lang::{
iden::{EdgeIden, NodeIden, NodeQueryIden},
query::{
QueryCommonConstraint, QueryConstraintSortByKeyJson, QueryGraphConstraint,
QueryGraphConstraintJson, QueryGraphConstraintLimitJson, QueryGraphJson, QueryJson,
QueryResultJson, QueryVectorConstraint, QueryVectorConstraintJson, QueryVectorJson,
},
},
schema::{format_edge_table_name, format_node_table_name},
};
use sea_orm::{ConnectionTrait, DbConn, DbErr, FromQueryResult, Order};
use sea_query::{Alias, Expr, SelectStatement};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, Serialize, Deserialize, FromQueryResult)]
/// A queried node
pub struct QueryResultNode {
/// Name of the node
pub name: String,
/// Associated weight (specified in query)
pub weight: Option<f64>,
/// Depth when this node is first found in the graph.
/// Some(0) for root nodes.
/// None if querying a vector.
pub depth: Option<u32>,
}
#[derive(Debug, Clone, FromQueryResult)]
/// A helper struct to temporarily store unique nodes
struct NodeName {
name: String,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, FromQueryResult)]
#[serde(rename_all = "camelCase")]
/// A queried edge
pub struct QueryResultEdge {
/// Name of the node in the from side
pub from_node: String,
/// Name of the node in the to side
pub to_node: String,
}
impl QueryResultEdge {
/// Convert self to an edge with flipped directions
pub fn to_flipped(self) -> Self {
Self {
from_node: self.to_node,
to_node: self.from_node,
}
}
}
#[derive(Debug)]
/// A helper struct to specify how to perform a graph query
pub struct QueryGraphParams {
/// Which entity to consider for constructing the graph (unformatted)
pub entity_name: Result<String, DbErr>,
/// Which relation to consider for constructing the graph (unformatted)
pub relation_name: Result<String, DbErr>,
/// Whether to reverse the direction when constructing the graph
pub reverse_direction: bool,
/// Specify the root nodes to be the nodes with the supplied names
/// The keys in the HashMaps must be Formatted.
pub root_node_names: Vec<String>,
/// Recursion goes up to this level, 0 means no recursion at all.
/// Recursion does not terminate early if this value is None.
pub max_depth: Option<u32>,
/// Sort each batch on this key (this value is a Formatted column name).
/// This key is also used as for filling the `weight` field of queried nodes, if supplied.
/// The order is random if this value is None.
pub batch_sort_key: Option<String>,
/// Sort each batch in an ascending order if this value is true.
pub batch_sort_asc: bool,
/// Include up to this number of nodes in each batch.
/// All nodes are included in all batches if this value is None.
pub max_batch_size: Option<usize>,
/// Include up to this number of nodes across the whole recursion.
/// All nodes are included if this value is None.
pub max_total_size: Option<usize>,
}
impl Default for QueryGraphParams {
fn default() -> Self {
Self {
entity_name: Err(DbErr::Custom("Entity name is unspecified.".to_owned())),
relation_name: Err(DbErr::Custom("Relation name is unspecified.".to_owned())),
reverse_direction: false,
root_node_names: vec![],
max_depth: Some(6),
batch_sort_key: None,
batch_sort_asc: false,
max_batch_size: Some(6),
max_total_size: Some(10000),
}
}
}
impl QueryGraphParams {
/// Construct params from metadata
pub fn from_query_graph_metadata(metadata: QueryGraphJson) -> Self {
let mut params = Self {
entity_name: Ok(metadata.of),
..Default::default()
};
metadata
.constraints
.into_iter()
.for_each(|constraint| match constraint {
QueryGraphConstraintJson::Common(constraint) => {
params.handle_common_constraint(constraint)
}
QueryGraphConstraintJson::Exclusive(constraint) => {
params.handle_graph_constraint(constraint)
}
});
params
}
fn handle_common_constraint(&mut self, constraint: QueryCommonConstraint) {
match constraint {
QueryCommonConstraint::SortBy(sort_by) => {
self.batch_sort_key = match sort_by.key {
QueryConstraintSortByKeyJson::Connectivity { of, r#type } => {
Some(r#type.to_column_name(of))
}
};
self.batch_sort_asc = !sort_by.desc;
}
QueryCommonConstraint::Limit(limit) => self.max_total_size = Some(limit as usize),
}
}
fn handle_graph_constraint(&mut self, constraint: QueryGraphConstraint) {
match constraint {
QueryGraphConstraint::Edge { of, traversal } => {
self.relation_name = Ok(of);
self.reverse_direction = traversal.reverse_direction;
}
QueryGraphConstraint::RootNodes(root_node_names) => {
self.root_node_names = root_node_names;
}
QueryGraphConstraint::Limit(limit) => match limit {
QueryGraphConstraintLimitJson::Depth(depth) => self.max_depth = depth,
QueryGraphConstraintLimitJson::BatchSize(batch_size) => {
self.max_batch_size = batch_size
}
},
}
}
}
/// Query graph data
#[derive(Debug)]
pub struct Query;
impl Query {
/// Query data from db
pub async fn query(db: &DbConn, query_json: QueryJson) -> Result<QueryResultJson, DbErr> {
match query_json {
QueryJson::Vector(metadata) => Self::query_vector(db, metadata).await,
QueryJson::Graph(metadata) => Self::query_graph(db, metadata).await,
}
}
async fn query_vector(
db: &DbConn,
metadata: QueryVectorJson,
) -> Result<QueryResultJson, DbErr> {
let mut stmt = sea_query::Query::select();
stmt.column(NodeQueryIden::Name)
.expr_as(Expr::value(Option::<f64>::None), NodeQueryIden::Weight)
.expr_as(Expr::val(Option::<u32>::None), NodeQueryIden::Depth)
.from(Alias::new(&format_node_table_name(metadata.of)));
for constraint in metadata.constraints {
match constraint {
QueryVectorConstraintJson::Common(constraint) => {
Self::handle_common_constraint(&mut stmt, constraint)
}
QueryVectorConstraintJson::Exclusive(constraint) => {
Self::handle_vector_constraint(&mut stmt, constraint)
}
}
}
let builder = db.get_database_backend();
Ok(QueryResultJson::Vector(
QueryResultNode::find_by_statement(builder.build(&stmt))
.all(db)
.await?,
))
}
fn handle_common_constraint(stmt: &mut SelectStatement, constraint: QueryCommonConstraint) {
match constraint {
QueryCommonConstraint::SortBy(sort_by) => {
let col_name = match sort_by.key {
QueryConstraintSortByKeyJson::Connectivity { of, r#type } => {
r#type.to_column_name(of)
}
};
stmt.expr_as(Expr::col(Alias::new(&col_name)), NodeQueryIden::Weight)
.order_by(
Alias::new(&col_name),
if sort_by.desc {
Order::Desc
} else {
Order::Asc
},
);
}
QueryCommonConstraint::Limit(limit) => {
stmt.limit(limit);
}
}
}
fn handle_vector_constraint(_: &mut SelectStatement, constraint: QueryVectorConstraint) {
match constraint {
// Empty
}
}
async fn query_graph(db: &DbConn, metadata: QueryGraphJson) -> Result<QueryResultJson, DbErr> {
let params = QueryGraphParams::from_query_graph_metadata(metadata);
println!("Querying a graph with params:\n{:?}", params);
Self::traverse_with_params(db, params).await
}
async fn traverse_with_params(
db: &DbConn,
params: QueryGraphParams,
) -> Result<QueryResultJson, DbErr> {
let builder = db.get_database_backend();
let edge_table = &format_edge_table_name(params.relation_name?);
let node_table = &format_node_table_name(params.entity_name?);
// Start with root nodes
let mut pending_nodes: Vec<String> = {
let root_node_set: HashSet<String> =
HashSet::from_iter(params.root_node_names.into_iter());
let root_node_stmt = sea_query::Query::select()
.column(NodeQueryIden::Name)
.from(Alias::new(node_table))
.to_owned();
NodeName::find_by_statement(builder.build(&root_node_stmt))
.all(db)
.await?
.into_iter()
.filter_map(|node| {
if root_node_set.contains(&node.name) {
Some(node.name)
} else {
None
}
})
.collect()
};
let mut result_nodes: HashSet<String> = HashSet::from_iter(pending_nodes.iter().cloned());
let mut node_depths: HashMap<String, u32> = HashMap::new();
let mut result_edges: HashSet<QueryResultEdge> = HashSet::new();
// Normal direction: Join on "from" -> finding "to"'s
// Reverse: Join on "to" -> finding "from"'s
let join_col = if !params.reverse_direction {
EdgeIden::FromNode
} else {
EdgeIden::ToNode
};
let mut depth = 0_u32;
while params.max_depth.is_none() || depth < params.max_depth.unwrap() {
// Fetch target edges from pending_nodes
let target_edges = {
let target_edge_stmt = sea_query::Query::select()
.columns([EdgeIden::FromNode, EdgeIden::ToNode])
.from(Alias::new(edge_table))
.inner_join(
Alias::new(node_table),
Expr::tbl(Alias::new(node_table), NodeIden::Name)
.equals(Alias::new(edge_table), join_col),
)
.and_where(Expr::col(join_col).is_in(pending_nodes))
.to_owned();
QueryResultEdge::find_by_statement(builder.build(&target_edge_stmt))
.all(db)
.await?
};
let mut total_nodes_full = false;
pending_nodes = target_edges
.into_iter()
.filter_map(|edge| {
let target_node_name = if !params.reverse_direction {
edge.to_node.clone()
} else {
edge.from_node.clone()
};
if result_edges.insert(edge) && !result_nodes.contains(&target_node_name) {
if let Some(max_total_size) = params.max_total_size {
if result_nodes.len() >= max_total_size {
total_nodes_full = true;
}
}
Some(target_node_name)
} else {
None
}
})
.collect();
pending_nodes.iter().for_each(|node_name| {
if !node_depths.contains_key(node_name) {
node_depths.insert(node_name.clone(), depth + 1);
}
});
// Sort by specified key if appropriate
if let Some(order_by_key) = ¶ms.batch_sort_key {
pending_nodes = {
let pending_nodes_set: HashSet<String> =
HashSet::from_iter(pending_nodes.into_iter());
let stmt = sea_query::Query::select()
.column(NodeIden::Name)
.from(Alias::new(node_table))
.order_by(
Alias::new(order_by_key),
if params.batch_sort_asc {
Order::Asc
} else {
Order::Desc
},
)
.to_owned();
NodeName::find_by_statement(builder.build(&stmt))
.all(db)
.await?
.into_iter()
.filter_map(|node| {
if pending_nodes_set.contains(&node.name) {
Some(node.name)
} else {
None
}
})
.collect()
};
}
if let Some(max_batch_size) = params.max_batch_size {
if max_batch_size < pending_nodes.len() {
pending_nodes = pending_nodes[0..max_batch_size].to_vec();
}
}
result_nodes.extend(pending_nodes.iter().cloned());
if pending_nodes.is_empty() || total_nodes_full {
break;
}
depth += 1;
}
// Make sure all edges in result_edges use only nodes in result_nodes
let edges: Vec<QueryResultEdge> = {
let iter = result_edges.into_iter().filter(|edge| {
result_nodes.contains(&edge.from_node) && result_nodes.contains(&edge.to_node)
});
if params.reverse_direction {
iter.map(|edge| edge.to_flipped()).collect()
} else {
iter.collect()
}
};
// Fetch the weights if needed
let nodes: Vec<QueryResultNode> = if let Some(weight_key) = params.batch_sort_key {
let stmt = sea_query::Query::select()
.column(NodeQueryIden::Name)
.expr_as(Expr::col(Alias::new(&weight_key)), NodeQueryIden::Weight)
.expr_as(Expr::val(Option::<u32>::None), NodeQueryIden::Depth)
.from(Alias::new(node_table))
.and_where(Expr::col(NodeQueryIden::Name).is_in(result_nodes))
.to_owned();
QueryResultNode::find_by_statement(builder.build(&stmt))
.all(db)
.await?
.into_iter()
.map(|mut node| {
let depth = node_depths.get(&node.name).cloned().unwrap_or_default();
node.depth = Some(depth);
node
})
.collect()
} else {
result_nodes
.into_iter()
.map(|name| {
let depth = node_depths.get(&name).cloned().unwrap_or_default();
QueryResultNode {
name,
weight: None,
depth: Some(depth),
}
})
.collect()
};
Ok(QueryResultJson::Graph { nodes, edges })
}
}
/// Graph data
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GraphData {
/// Graph node data
nodes: Vec<GraphNodeData>,
/// Link data
links: Vec<GraphLinkData>,
}
/// Graph node data
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GraphNodeData {
/// Name of node
id: String,
/// Weight
weight: f64,
}
impl PartialEq for GraphNodeData {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for GraphNodeData {}
impl std::hash::Hash for GraphNodeData {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
/// Tree data
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TreeData {
/// Tree node data
nodes: Vec<TreeNodeData>,
/// Link data
links: Vec<TreeLinkData>,
}
/// Tree node data
#[derive(Debug, Clone, Eq, Deserialize, Serialize)]
pub struct TreeNodeData {
/// Name of node
id: String,
/// Node type
r#type: TreeNodeType,
/// Node depth inverse (the higher, the deeper in recursion this node was found)
/// This field is not used to identify a tree node.
depth_inv: i32,
}
impl PartialEq for TreeNodeData {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.r#type == other.r#type
}
}
impl std::hash::Hash for TreeNodeData {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.r#type.hash(state);
}
}
/// Denotes which side a node belongs to, relative to the **root** node
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum TreeNodeType {
/// Centered
Root = 0,
/// To the Left
Dependency = 1,
/// To the Right
Dependent = 2,
}
/// Node weight option
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum NodeWeight {
/// Simple (Immediately decay to 0)
Simple = 0,
/// Complex with weight decay factor 0.3
FastDecay = 1,
/// Complex with weight decay factor 0.5
MediumDecay = 2,
/// Complex with weight decay factor 0.7
SlowDecay = 3,
/// Compound (No decay)
Compound = 4,
}
/// Graph link data
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct GraphLinkData {
/// Source node
source: String,
/// Target node
target: String,
}
/// Tree link data
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct TreeLinkData {
/// Source node
source: String,
/// Target node
target: String,
/// Edge type
r#type: TreeNodeType,
}