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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! DML Insert Vertex Tests
//!
//! Test coverage:
//! - INSERT VERTEX - Insert vertex data
//! - INSERT VERTEX IF NOT EXISTS
//! - INSERT vertex with all supported data types
//! - INSERT vertex with NULL values
//! - INSERT vertex without specifying properties

use super::common;

use common::test_scenario::TestScenario;
use linkrs_core::value::date_time::{DateTimeValue, DateValue};
use linkrs_core::Value;
use linkrs_query::parser::Parser;
use std::collections::HashMap;

// ==================== INSERT VERTEX Parser Tests ====================

#[test]
fn test_insert_parser_vertex() {
    let query = "INSERT VERTEX Person(name, age) VALUES 1:('Alice', 30)";
    let mut parser = Parser::new(query);

    let result = parser.parse();
    assert!(
        result.is_ok(),
        "INSERT VERTEX parsing should succeed: {:?}",
        result.err()
    );

    let stmt = result.expect("INSERT statement parsing should succeed");
    assert_eq!(stmt.ast.stmt.kind(), "INSERT");
}

#[test]
fn test_insert_parser_multiple_vertices() {
    let query = "INSERT VERTEX Person(name, age) VALUES 1:('Alice', 30), 2:('Bob', 25)";
    let mut parser = Parser::new(query);

    let result = parser.parse();
    assert!(
        result.is_ok(),
        "INSERT multiple vertices parsing should succeed: {:?}",
        result.err()
    );

    let stmt = result.expect("INSERT statement parsing should succeed");
    assert_eq!(stmt.ast.stmt.kind(), "INSERT");
}

#[test]
fn test_insert_parser_invalid_syntax() {
    let query = "INSERT VERTEX Person(name, age) VALUES 1:'Alice', 30";
    let mut parser = Parser::new(query);

    let result = parser.parse();
    assert!(result.is_err(), "Invalid syntax should trigger an error.");
}

// ==================== INSERT VERTEX Execution Tests ====================

#[test]
fn test_insert_execution_vertex() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id: INT, name: STRING, age: INT)")
        .exec_dml("INSERT VERTEX Person(name, age) VALUES 1:('Alice', 30)")
        .assert_success()
        .assert_vertex_exists(1, "Person")
        .assert_vertex_props(
            1,
            "Person",
            HashMap::from([("name", Value::string("Alice")), ("age", Value::Int(30))]),
        );
}

#[test]
fn test_insert_execution_multiple_vertices() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id: INT, name: STRING, age: INT)")
        .exec_dml("INSERT VERTEX Person(name, age) VALUES 1:('Alice', 30), 2:('Bob', 25)")
        .assert_success()
        .assert_vertex_exists(1, "Person")
        .assert_vertex_exists(2, "Person")
        .assert_vertex_count("Person", 2);
}

// ==================== INSERT IF NOT EXISTS Tests ====================

#[test]
fn test_insert_if_not_exists_parser() {
    let query = "INSERT VERTEX IF NOT EXISTS Person(name, age) VALUES 1:('Alice', 30)";
    let mut parser = Parser::new(query);

    let result = parser.parse();
    assert!(
        result.is_ok(),
        "INSERT IF NOT EXISTS parsing should succeed: {:?}",
        result.err()
    );

    let stmt = result.expect("INSERT statement parsing should succeed");
    assert_eq!(stmt.ast.stmt.kind(), "INSERT");
}

#[test]
fn test_insert_if_not_exists_execution() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id: INT, name: STRING, age: INT)")
        .exec_dml("INSERT VERTEX IF NOT EXISTS Person(name, age) VALUES 1:('Alice', 30)")
        .assert_success()
        .assert_vertex_exists(1, "Person")
        .assert_vertex_props(
            1,
            "Person",
            HashMap::from([("name", Value::string("Alice")), ("age", Value::Int(30))]),
        )
        .exec_dml("INSERT VERTEX IF NOT EXISTS Person(name, age) VALUES 1:('Bob', 25)")
        .assert_success()
        .assert_vertex_props(
            1,
            "Person",
            HashMap::from([("name", Value::string("Alice")), ("age", Value::Int(30))]),
        );
}

// ==================== Multiple Tags Tests ====================

#[test]
fn test_insert_multiple_tags_parser() {
    let query = "INSERT VERTEX Person(name, age), Employee(department, salary) VALUES 1:('Alice', 30):('Engineering', 100000)";
    let mut parser = Parser::new(query);

    let result = parser.parse();
    assert!(
        result.is_err(),
        "INSERT multiple tags must be rejected under single-label semantics",
    );
}

// ==================== Error Handling Tests ====================

#[test]
fn test_insert_duplicate_vertex() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING)")
        .assert_success()
        .exec_dml("INSERT VERTEX Person(name) VALUES 1:('Alice')")
        .assert_success()
        .exec_dml("INSERT VERTEX Person(name) VALUES 1:('Bob')")
        .assert_error();
}

#[test]
fn test_insert_vertex_with_all_types() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl(
            r#"
            CREATE TAG TestTypes(
                id INT,
                str_field STRING,
                int_field INT,
                double_field DOUBLE,
                bool_field BOOL
            )
        "#,
        )
        .assert_success()
        .exec_dml(
            r#"
            INSERT VERTEX TestTypes(str_field, int_field, double_field, bool_field) 
            VALUES 1:('test', 42, 2.718281828459045, true)
        "#,
        )
        .assert_success()
        .assert_vertex_props(1, "TestTypes", {
            let mut map = HashMap::new();
            map.insert("str_field", Value::string("test"));
            map.insert("int_field", Value::Int(42));
            map.insert("double_field", Value::Double(std::f64::consts::E));
            map.insert("bool_field", Value::Bool(true));
            map
        });
}

// ==================== Extended Data Types Tests ====================

#[test]
fn test_insert_vertex_with_date_type() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl(
            r#"
            CREATE TAG DateTypes(
                id INT,
                date_field DATE
            )
        "#,
        )
        .assert_success()
        .exec_dml(
            r#"
            INSERT VERTEX DateTypes(date_field) 
            VALUES 1:('2024-06-15')
        "#,
        )
        .assert_success()
        .assert_vertex_props(1, "DateTypes", {
            let mut map = HashMap::new();
            map.insert(
                "date_field",
                Value::Date(DateValue {
                    year: 2024,
                    month: 6,
                    day: 15,
                }),
            );
            map
        });
}

#[test]
fn test_insert_vertex_with_date_alternative_formats() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG AltDate(id INT, d DATE)")
        .assert_success()
        .exec_dml("INSERT VERTEX AltDate(d) VALUES 1:('2024/06/15')")
        .assert_success()
        .assert_vertex_props(
            1,
            "AltDate",
            HashMap::from([(
                "d",
                Value::Date(DateValue {
                    year: 2024,
                    month: 6,
                    day: 15,
                }),
            )]),
        );
}

#[test]
fn test_insert_vertex_with_datetime_type() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl(
            r#"
            CREATE TAG DTTest(
                id INT,
                dt_field DATETIME
            )
        "#,
        )
        .assert_success()
        .exec_dml(
            r#"
            INSERT VERTEX DTTest(dt_field) 
            VALUES 1:('2024-06-15 10:30:45')
        "#,
        )
        .assert_success()
        .assert_vertex_props(1, "DTTest", {
            let mut map = HashMap::new();
            map.insert(
                "dt_field",
                Value::DateTime(DateTimeValue {
                    year: 2024,
                    month: 6,
                    day: 15,
                    hour: 10,
                    minute: 30,
                    sec: 45,
                    microsec: 0,
                }),
            );
            map
        });
}

// ==================== Numeric Types Tests ====================

#[test]
fn test_insert_vertex_with_numeric_types() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl(
            r#"
            CREATE TAG NumericTypes(
                id INT,
                int_field INT,
                float_field FLOAT
            )
        "#,
        )
        .assert_success()
        .exec_dml(
            r#"
            INSERT VERTEX NumericTypes(int_field, float_field) 
            VALUES 1:(100, 3.141592653589793)
        "#,
        )
        .assert_success()
        .assert_vertex_props(1, "NumericTypes", {
            let mut map = HashMap::new();
            map.insert("int_field", Value::Int(100));
            map.insert("float_field", Value::Float(std::f32::consts::PI));
            map
        });
}

// ==================== NULL Values Tests ====================

#[test]
fn test_insert_vertex_with_null_values() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl(
            r#"
            CREATE TAG NullableTypes(
                id INT,
                name STRING,
                age INT NULL,
                email STRING NULL
            )
        "#,
        )
        .assert_success()
        .exec_dml(
            r#"
            INSERT VERTEX NullableTypes(name, age, email) 
            VALUES 1:('Alice', NULL, 'alice@example.com')
        "#,
        )
        .assert_success()
        .assert_vertex_exists(1, "NullableTypes");
}

#[test]
fn test_insert_vertex_with_partial_properties() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl(
            r#"
            CREATE TAG PartialFields(
                id INT,
                name STRING,
                age INT NULL,
                email STRING NULL
            )
        "#,
        )
        .assert_success()
        .exec_dml(
            r#"
            INSERT VERTEX PartialFields(name) 
            VALUES 1:('Bob')
        "#,
        )
        .assert_success()
        .assert_vertex_exists(1, "PartialFields")
        .assert_vertex_props(
            1,
            "PartialFields",
            HashMap::from([("name", Value::string("Bob"))]),
        );
}

// ==================== FIXED_STRING Type Tests ====================

#[test]
fn test_insert_vertex_with_fixed_string() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG FixedStr(id INT, code STRING)")
        .assert_success()
        .exec_dml("INSERT VERTEX FixedStr(code) VALUES 1:('ABC123')")
        .assert_success()
        .assert_vertex_exists(1, "FixedStr");
}

// ==================== GEOGRAPHY Type Tests ====================

#[test]
fn test_insert_vertex_with_geography_type() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG GeoTypes(id INT, geo_field GEOGRAPHY)")
        .assert_success()
        .exec_dml("INSERT VERTEX GeoTypes(geo_field) VALUES 1:(NULL)")
        .assert_success()
        .assert_vertex_exists(1, "GeoTypes");
}

#[test]
fn test_insert_vertex_with_geography_and_other_fields() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Place(id INT, name STRING, location GEOGRAPHY)")
        .assert_success()
        .exec_dml("INSERT VERTEX Place(name, location) VALUES 1:('Beijing', NULL)")
        .assert_success()
        .assert_vertex_exists(1, "Place")
        .assert_vertex_props(
            1,
            "Place",
            HashMap::from([("name", Value::string("Beijing"))]),
        );
}

// ==================== Multi-Tag Insert Execution Tests ====================

#[test]
fn test_insert_multiple_tags_execution() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING, age INT)")
        .exec_ddl("CREATE TAG Employee(id INT, department STRING, salary INT)")
        .assert_success()
        .exec_dml("INSERT VERTEX Person(name, age), Employee(department, salary) VALUES 1:('Alice', 30):('Engineering', 100000)")
        .assert_error();
}

// ==================== Type Mismatch Error Tests ====================

#[test]
fn test_insert_type_mismatch_string_to_int() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG TestTypes(id INT, int_field INT)")
        .assert_success()
        .exec_dml("INSERT VERTEX TestTypes(int_field) VALUES 1:('not_a_number')")
        .assert_error();
}

#[test]
fn test_insert_type_mismatch_bool_to_int() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG TestTypes(id INT, int_field INT)")
        .assert_success()
        .exec_dml("INSERT VERTEX TestTypes(int_field) VALUES 1:(true)")
        .assert_error();
}

// ==================== Non-Existent Tag Error Tests ====================

#[test]
fn test_insert_vertex_nonexistent_tag() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_dml("INSERT VERTEX NonExistent(name) VALUES 1:('test')")
        .assert_error();
}

// ==================== Negative Vertex ID Tests ====================

#[test]
fn test_insert_vertex_negative_id() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING)")
        .assert_success()
        .exec_dml("INSERT VERTEX Person(name) VALUES (-1):('Negative')")
        .assert_error();
}

// ==================== Empty String Property Tests ====================

#[test]
fn test_insert_vertex_empty_string() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING)")
        .assert_success()
        .exec_dml("INSERT VERTEX Person(name) VALUES 1:('')")
        .assert_success()
        .assert_vertex_exists(1, "Person")
        .assert_vertex_props(1, "Person", HashMap::from([("name", Value::string(""))]));
}

// ==================== Property Count Validation Tests ====================

#[test]
fn test_insert_vertex_arity_mismatch_rejected() {
    let scenario = TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING, age INT)")
        .exec_dml("INSERT VERTEX Person(name, age) VALUES 1:('Alice')");
    assert!(
        scenario
            .error()
            .is_some_and(|e| e.contains("does not match")),
        "fewer values than property names should fail, got: {:?}",
        scenario.error()
    );
    scenario.assert_error();
}

#[test]
fn test_insert_vertex_extra_values_rejected() {
    let scenario = TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING, age INT)")
        .exec_dml("INSERT VERTEX Person(name, age) VALUES 1:('Alice', 30, 'extra')");
    assert!(
        scenario
            .error()
            .is_some_and(|e| e.contains("does not match")),
        "more values than property names should fail, got: {:?}",
        scenario.error()
    );
    scenario.assert_error();
}

#[test]
fn test_insert_vertex_default_props_with_values_rejected() {
    let scenario = TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING, age INT)")
        .exec_dml("INSERT VERTEX ON Person VALUES 1:('Alice', 30)");
    assert!(
        scenario
            .error()
            .is_some_and(|e| e.contains("only writes vertex ids")),
        "default property list with values should fail, got: {:?}",
        scenario.error()
    );
    scenario.assert_error();
}

#[test]
fn test_insert_vertex_empty_parens_vid_only() {
    TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING)")
        .exec_dml("INSERT VERTEX Person() VALUES 1")
        .assert_success()
        .assert_vertex_exists(1, "Person");
}

// ==================== Empty Brackets Grammar Boundary Tests ====================

#[test]
fn test_insert_parser_empty_parens_accepted() {
    let query = "INSERT VERTEX person() VALUES 1";
    let mut parser = Parser::new(query);

    let result = parser.parse();
    assert!(
        result.is_ok(),
        "INSERT VERTEX with empty brackets parsing should succeed: {:?}",
        result.err()
    );

    let stmt = result.expect("INSERT statement parsing should succeed");
    assert_eq!(stmt.ast.stmt.kind(), "INSERT");
    assert!(!parser.has_errors());
}

#[test]
fn test_insert_empty_parens_with_values_rejected() {
    let scenario = TestScenario::new()
        .expect("Failed to create test scenario")
        .setup_space("test_space")
        .exec_ddl("CREATE TAG Person(id INT, name STRING, age INT)")
        .exec_dml("INSERT VERTEX Person() VALUES 1:('Alice')");
    assert!(
        scenario
            .error()
            .is_some_and(|e| e.contains("only writes vertex ids")),
        "empty property list with values should fail, got: {:?}",
        scenario.error()
    );
    scenario.assert_error();
}