linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
Documentation
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
//! Schema maintenance planning (CREATE / ALTER / DROP, macros and types).

use crate::parser::ast::{AlterTarget, CreateTarget, IndexType};
use crate::planning::plan::core::node_id_generator::next_node_id;
use crate::planning::plan::core::nodes::management::edge_nodes::EdgeAlterInfo;
use crate::planning::plan::core::nodes::management::index_nodes::IndexManageInfo;
use crate::planning::plan::core::nodes::management::manage_node_enums::{
    EdgeManageNode, IndexManageNode, MacroManageNode, SequenceManageNode, SpaceManageNode,
    TagManageNode, TypeManageNode,
};
use crate::planning::plan::core::nodes::management::sequence_nodes::SequenceManageInfo;
use crate::planning::plan::core::nodes::management::space_nodes::{
    CreateSpaceNode, SpaceManageInfo,
};
use crate::planning::plan::core::nodes::management::tag_nodes::TagAlterInfo;
use crate::planning::plan::core::nodes::{
    AlterEdgeNode, AlterSequenceNode, AlterTagNode, CreateEdgeNode, CreateMacroNode,
    CreateSequenceNode, CreateTagNode, CreateTypeNode, DropMacroNode, DropSequenceNode,
    DropTypeNode, EdgeManageInfo, MacroManageInfo, RenameEdgeNode, RenameTagNode, TagManageInfo,
    TypeManageInfo, UpdateEdgeEndpointsNode,
};
use crate::planning::plan::core::PlanNodeEnum;
use crate::planning::planner::PlannerError;

use super::MaintainPlanner;

impl MaintainPlanner {
    pub(super) fn plan_create(
        &self,
        target: &CreateTarget,
        if_not_exists: bool,
        current_space: &str,
    ) -> Result<Option<PlanNodeEnum>, PlannerError> {
        match target {
            CreateTarget::Index {
                index_type,
                name,
                on,
                properties,
            } => {
                let space_name = current_space.to_string();
                let index_info = IndexManageInfo::new(
                    space_name,
                    name.clone(),
                    match index_type {
                        IndexType::Tag => "tag".to_string(),
                        IndexType::Edge => "edge".to_string(),
                    },
                )
                .with_target_name(on.clone())
                .with_properties(properties.clone());

                let plan_node = match index_type {
                    IndexType::Tag => {
                        let node = crate::planning::plan::core::nodes::CreateTagIndexNode::new(
                            next_node_id(),
                            index_info,
                        );
                        PlanNodeEnum::IndexManage(IndexManageNode::CreateTagIndex(node))
                    }
                    IndexType::Edge => {
                        let node = crate::planning::plan::core::nodes::CreateEdgeIndexNode::new(
                            next_node_id(),
                            index_info,
                        );
                        PlanNodeEnum::IndexManage(IndexManageNode::CreateEdgeIndex(node))
                    }
                };
                Ok(Some(plan_node))
            }
            CreateTarget::Space {
                name,
                vid_type,
                comment,
            } => {
                let space_info = SpaceManageInfo::new(name.clone())
                    .with_vid_type(vid_type.clone())
                    .with_comment(comment.clone());
                let node = CreateSpaceNode::new(next_node_id(), space_info);
                Ok(Some(PlanNodeEnum::SpaceManage(SpaceManageNode::Create(
                    node,
                ))))
            }
            CreateTarget::Tag {
                name,
                properties,
                ttl_duration,
                ttl_col,
            } => {
                let space_name = current_space.to_string();
                linkrs_core::types::ttl::validate_ttl(
                    properties,
                    *ttl_duration,
                    ttl_col.as_deref(),
                )
                .map_err(PlannerError::InvalidOperation)?;
                let tag_info = TagManageInfo::new(space_name, name.clone())
                    .with_properties(properties.clone())
                    .with_if_not_exists(if_not_exists)
                    .with_ttl(*ttl_duration, ttl_col.clone());
                let node = CreateTagNode::new(next_node_id(), tag_info);
                Ok(Some(PlanNodeEnum::TagManage(TagManageNode::Create(node))))
            }
            CreateTarget::EdgeType {
                name,
                properties,
                ttl_duration,
                ttl_col,
                src_tag,
                dst_tag,
            } => {
                let space_name = current_space.to_string();
                linkrs_core::types::ttl::validate_ttl(
                    properties,
                    *ttl_duration,
                    ttl_col.as_deref(),
                )
                .map_err(PlannerError::InvalidOperation)?;
                let mut edge_info = EdgeManageInfo::new(space_name, name.clone())
                    .with_properties(properties.clone())
                    .with_if_not_exists(if_not_exists)
                    .with_ttl(*ttl_duration, ttl_col.clone());
                if let (Some(src), Some(dst)) = (src_tag, dst_tag) {
                    edge_info = edge_info.with_src_dst_tags(src.clone(), dst.clone());
                }
                let node = CreateEdgeNode::new(next_node_id(), edge_info);
                Ok(Some(PlanNodeEnum::EdgeManage(EdgeManageNode::Create(node))))
            }
            CreateTarget::Node { .. } | CreateTarget::Edge { .. } | CreateTarget::Path { .. } => {
                Ok(None)
            }
            CreateTarget::Sequence {
                name,
                start,
                increment,
                min_value,
                max_value,
                cycle,
            } => {
                if name.trim().is_empty() {
                    return Err(PlannerError::PlanGenerationFailed(
                        "CREATE SEQUENCE requires a non-empty sequence name".to_string(),
                    ));
                }
                Self::check_sequence_bounds(name, *increment, *min_value, *max_value)?;
                let info = SequenceManageInfo::new(name.clone())
                    .with_start(*start)
                    .with_increment(*increment)
                    .with_bounds(*min_value, *max_value)
                    .with_cycle(Some(*cycle))
                    .with_if_not_exists(if_not_exists);
                let node = CreateSequenceNode::new(next_node_id(), info);
                Ok(Some(PlanNodeEnum::SequenceManage(
                    SequenceManageNode::Create(node),
                )))
            }
            CreateTarget::TagAsQuery { name, .. } => {
                Err(PlannerError::UnsupportedOperation(format!(
                    "CREATE TAG {} AS (query) is only supported through the session API",
                    name
                )))
            }
            CreateTarget::EdgeAsQuery { name, .. } => {
                Err(PlannerError::UnsupportedOperation(format!(
                    "CREATE EDGE {} AS (query) is only supported through the session API",
                    name
                )))
            }
        }
    }

    /// Validate sequence step and bounds at plan time so a stuck or
    /// inverted sequence fails loudly instead of misbehaving at runtime.
    fn check_sequence_bounds(
        name: &str,
        increment: Option<i64>,
        min_value: Option<i64>,
        max_value: Option<i64>,
    ) -> Result<(), PlannerError> {
        if increment == Some(0) {
            return Err(PlannerError::PlanGenerationFailed(format!(
                "Sequence '{name}' has INCREMENT=0, which would never advance"
            )));
        }
        if let (Some(min), Some(max)) = (min_value, max_value) {
            if min > max {
                return Err(PlannerError::PlanGenerationFailed(format!(
                    "Sequence '{name}' has MINVALUE ({min}) greater than MAXVALUE ({max})"
                )));
            }
        }
        Ok(())
    }

    fn contextual_body(
        body: &linkrs_core::types::expr::contextual::ContextualExpression,
    ) -> Result<linkrs_core::Expression, PlannerError> {
        body.expression()
            .map(|meta| meta.inner().clone())
            .ok_or_else(|| {
                PlannerError::PlanGenerationFailed(
                    "Macro body expression is missing analysis context".to_string(),
                )
            })
    }

    pub(super) fn plan_create_macro(
        &self,
        stmt: &crate::parser::ast::stmt::CreateMacroStmt,
    ) -> Result<PlanNodeEnum, PlannerError> {
        use linkrs_core::metadata::MacroParamDef;

        if stmt.name.trim().is_empty() {
            return Err(PlannerError::PlanGenerationFailed(
                "Macro name must not be empty".to_string(),
            ));
        }
        let body = Self::contextual_body(&stmt.body)?;
        let mut seen = std::collections::HashSet::new();
        let mut params = Vec::with_capacity(stmt.params.len());
        for param in &stmt.params {
            if param.name.trim().is_empty() {
                return Err(PlannerError::PlanGenerationFailed(format!(
                    "Macro '{}' has an empty parameter name",
                    stmt.name
                )));
            }
            let key = param.name.to_ascii_uppercase();
            if !seen.insert(key) {
                return Err(PlannerError::PlanGenerationFailed(format!(
                    "Macro '{}' has duplicate parameter '{}'",
                    stmt.name, param.name
                )));
            }
            let default = param
                .default_value
                .as_ref()
                .map(Self::contextual_body)
                .transpose()?;
            params.push(MacroParamDef {
                name: param.name.clone(),
                default,
            });
        }
        let info = MacroManageInfo::new(stmt.name.clone(), params, body, stmt.if_not_exists);
        let node = CreateMacroNode::new(next_node_id(), info);
        Ok(PlanNodeEnum::MacroManage(MacroManageNode::Create(node)))
    }

    pub(super) fn plan_drop_macro(
        &self,
        stmt: &crate::parser::ast::stmt::DropMacroStmt,
    ) -> PlanNodeEnum {
        let node =
            DropMacroNode::new(next_node_id(), stmt.name.clone()).with_if_exists(stmt.if_exists);
        PlanNodeEnum::MacroManage(MacroManageNode::Drop(node))
    }

    pub(super) fn plan_create_type(
        &self,
        stmt: &crate::parser::ast::stmt::CreateTypeStmt,
    ) -> Result<PlanNodeEnum, PlannerError> {
        if stmt.name.trim().is_empty() {
            return Err(PlannerError::PlanGenerationFailed(
                "Type name must not be empty".to_string(),
            ));
        }
        if stmt.underlying_type_text.trim().is_empty() {
            return Err(PlannerError::PlanGenerationFailed(format!(
                "Type '{}' has an empty underlying type",
                stmt.name
            )));
        }
        let info = TypeManageInfo::new(
            stmt.name.clone(),
            stmt.underlying_type_text.clone(),
            stmt.if_not_exists,
        );
        let node = CreateTypeNode::new(next_node_id(), info);
        Ok(PlanNodeEnum::TypeManage(TypeManageNode::Create(node)))
    }

    pub(super) fn plan_drop_type(
        &self,
        stmt: &crate::parser::ast::stmt::DropTypeStmt,
    ) -> PlanNodeEnum {
        let node =
            DropTypeNode::new(next_node_id(), stmt.name.clone()).with_if_exists(stmt.if_exists);
        PlanNodeEnum::TypeManage(TypeManageNode::Drop(node))
    }

    pub(super) fn plan_alter(
        &self,
        target: &AlterTarget,
        current_space: &str,
    ) -> Result<PlanNodeEnum, PlannerError> {
        match target {
            AlterTarget::Tag {
                tag_name,
                additions,
                deletions,
                changes,
                new_comment,
            } => {
                let alter_info = TagAlterInfo::new(current_space.to_string(), tag_name.clone())
                    .with_additions(additions.clone())
                    .with_deletions(deletions.clone())
                    .with_changes(changes.clone())
                    .with_new_comment(new_comment.clone());

                let node = AlterTagNode::new(next_node_id(), alter_info);
                Ok(PlanNodeEnum::TagManage(TagManageNode::Alter(node)))
            }
            AlterTarget::Edge {
                edge_name,
                additions,
                deletions,
                changes,
                new_comment,
            } => {
                let alter_info = EdgeAlterInfo::new(current_space.to_string(), edge_name.clone())
                    .with_additions(additions.clone())
                    .with_deletions(deletions.clone())
                    .with_changes(changes.clone())
                    .with_new_comment(new_comment.clone());

                let node = AlterEdgeNode::new(next_node_id(), alter_info);
                Ok(PlanNodeEnum::EdgeManage(EdgeManageNode::Alter(node)))
            }
            AlterTarget::Sequence {
                name,
                increment,
                min_value,
                max_value,
                cycle,
            } => {
                if name.trim().is_empty() {
                    return Err(PlannerError::PlanGenerationFailed(
                        "ALTER SEQUENCE requires a non-empty sequence name".to_string(),
                    ));
                }
                Self::check_sequence_bounds(name, *increment, *min_value, *max_value)?;
                let info = SequenceManageInfo::new(name.clone())
                    .with_increment(*increment)
                    .with_bounds(*min_value, *max_value)
                    .with_cycle(*cycle);
                let node = AlterSequenceNode::new(next_node_id(), info);
                Ok(PlanNodeEnum::SequenceManage(SequenceManageNode::Alter(
                    node,
                )))
            }
            AlterTarget::RenameTag { old_name, new_name } => {
                if old_name.trim().is_empty() || new_name.trim().is_empty() {
                    return Err(PlannerError::PlanGenerationFailed(
                        "RENAME TAG requires non-empty old and new names".to_string(),
                    ));
                }
                let node = RenameTagNode::new(
                    next_node_id(),
                    current_space.to_string(),
                    old_name.clone(),
                    new_name.clone(),
                );
                Ok(PlanNodeEnum::TagManage(TagManageNode::Rename(node)))
            }
            AlterTarget::RenameEdge { old_name, new_name } => {
                if old_name.trim().is_empty() || new_name.trim().is_empty() {
                    return Err(PlannerError::PlanGenerationFailed(
                        "RENAME EDGE requires non-empty old and new names".to_string(),
                    ));
                }
                let node = RenameEdgeNode::new(
                    next_node_id(),
                    current_space.to_string(),
                    old_name.clone(),
                    new_name.clone(),
                );
                Ok(PlanNodeEnum::EdgeManage(EdgeManageNode::Rename(node)))
            }
            AlterTarget::AddFrom {
                edge_name,
                src_tag,
                dst_tag,
            } => {
                if edge_name.trim().is_empty()
                    || src_tag.trim().is_empty()
                    || dst_tag.trim().is_empty()
                {
                    return Err(PlannerError::PlanGenerationFailed(
                        "ALTER EDGE ADD FROM requires edge, src and dst names".to_string(),
                    ));
                }
                let node = UpdateEdgeEndpointsNode::new(
                    next_node_id(),
                    current_space.to_string(),
                    edge_name.clone(),
                    src_tag.clone(),
                    dst_tag.clone(),
                    false,
                );
                Ok(PlanNodeEnum::EdgeManage(EdgeManageNode::UpdateEndpoints(
                    node,
                )))
            }
            AlterTarget::DropFrom {
                edge_name,
                src_tag,
                dst_tag,
            } => {
                if edge_name.trim().is_empty() {
                    return Err(PlannerError::PlanGenerationFailed(
                        "ALTER EDGE DROP FROM requires an edge name".to_string(),
                    ));
                }
                let node = UpdateEdgeEndpointsNode::new(
                    next_node_id(),
                    current_space.to_string(),
                    edge_name.clone(),
                    src_tag.clone(),
                    dst_tag.clone(),
                    true,
                );
                Ok(PlanNodeEnum::EdgeManage(EdgeManageNode::UpdateEndpoints(
                    node,
                )))
            }
        }
    }
    pub(super) fn plan_drop(
        &self,
        target: &crate::parser::ast::stmt::DropTarget,
        if_exists: bool,
        current_space: &str,
    ) -> Result<PlanNodeEnum, PlannerError> {
        use crate::parser::ast::stmt::DropTarget;

        match target {
            DropTarget::Tags(tag_names) if !tag_names.is_empty() => {
                let node = crate::planning::plan::core::nodes::DropTagNode::new(
                    next_node_id(),
                    current_space.to_string(),
                    tag_names.clone(),
                )
                .with_if_exists(if_exists);
                Ok(PlanNodeEnum::TagManage(TagManageNode::Drop(node)))
            }
            DropTarget::Edges(edge_names) if !edge_names.is_empty() => {
                let node = crate::planning::plan::core::nodes::DropEdgeNode::new(
                    next_node_id(),
                    current_space.to_string(),
                    edge_names.clone(),
                )
                .with_if_exists(if_exists);
                Ok(PlanNodeEnum::EdgeManage(EdgeManageNode::Drop(node)))
            }
            DropTarget::Space(space_name) => {
                let node = crate::planning::plan::core::nodes::DropSpaceNode::new(
                    next_node_id(),
                    space_name.clone(),
                )
                .with_if_exists(if_exists);
                Ok(PlanNodeEnum::SpaceManage(SpaceManageNode::Drop(node)))
            }
            DropTarget::TagIndex {
                space_name,
                index_name,
            } => {
                let resolved_space = if space_name.is_empty() {
                    current_space.to_string()
                } else {
                    space_name.clone()
                };
                let node = crate::planning::plan::core::nodes::DropTagIndexNode::new(
                    next_node_id(),
                    resolved_space,
                    index_name.clone(),
                )
                .with_if_exists(if_exists);
                Ok(PlanNodeEnum::IndexManage(IndexManageNode::DropTagIndex(
                    node,
                )))
            }
            DropTarget::EdgeIndex {
                space_name,
                index_name,
            } => {
                let resolved_space = if space_name.is_empty() {
                    current_space.to_string()
                } else {
                    space_name.clone()
                };
                let node = crate::planning::plan::core::nodes::DropEdgeIndexNode::new(
                    next_node_id(),
                    resolved_space,
                    index_name.clone(),
                )
                .with_if_exists(if_exists);
                Ok(PlanNodeEnum::IndexManage(IndexManageNode::DropEdgeIndex(
                    node,
                )))
            }
            DropTarget::Tags(_) | DropTarget::Edges(_) => Err(PlannerError::PlanGenerationFailed(
                "DROP TAG/EDGE requires at least one name; the parser guarantees a non-empty list"
                    .to_string(),
            )),
            DropTarget::Sequence(seq_name) => {
                if seq_name.trim().is_empty() {
                    return Err(PlannerError::PlanGenerationFailed(
                        "DROP SEQUENCE requires a non-empty sequence name".to_string(),
                    ));
                }
                let node = DropSequenceNode::new(next_node_id(), seq_name.clone())
                    .with_if_exists(if_exists);
                Ok(PlanNodeEnum::SequenceManage(SequenceManageNode::Drop(node)))
            }
        }
    }
}