hyperQL 0.1.0

Query language for hyperspatial graph databases with temporal and spatial semantics
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
//! # Schema DDL Parser Tests
//!
//! Comprehensive tests for schema DDL statement parsing including CREATE SCHEMA,
//! ALTER SCHEMA, DROP SCHEMA, and DESCRIBE SCHEMA statements.

use hyperQL::parser::parse_statement;
use hyperQL::ast::{Statement, schema::*};

#[test]
fn test_parse_create_schema_simple() {
    let query = "CREATE SCHEMA topics (
        id String REQUIRED,
        name String REQUIRED
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse simple CREATE SCHEMA: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            assert_eq!(stmt.fields.len(), 2);
            assert_eq!(stmt.fields[0].name, "id");
            assert_eq!(stmt.fields[1].name, "name");
            assert!(stmt.fields[0].required);
            assert!(stmt.fields[1].required);
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_create_schema_with_scopes() {
    let query = "CREATE SCHEMA documents (
        id String REQUIRED METADATA,
        title String REQUIRED COLLECTION_SPECIFIC,
        sentiment Float DOMAIN_SHARED
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse CREATE SCHEMA with scopes: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.fields.len(), 3);
            assert_eq!(stmt.fields[0].scope, Some(PropertyScope::Metadata));
            assert_eq!(stmt.fields[1].scope, Some(PropertyScope::CollectionSpecific));
            assert_eq!(stmt.fields[2].scope, Some(PropertyScope::DomainShared));
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_create_schema_with_cascade() {
    let query = "CREATE SCHEMA topics (
        name String REQUIRED,
        avg_sentiment Float CASCADED AS AGGREGATE Average FROM sentiment THROUGH tagged_with INCOMING
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse CASCADE field: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.fields.len(), 2);
            
            // Check cascaded field
            match &stmt.fields[1].kind {
                FieldKind::Cascaded(config) => {
                    assert_eq!(config.aggregation, AggregationFunction::Average);
                    assert_eq!(config.source_properties, vec!["sentiment"]);
                    assert_eq!(config.direction, Some(EdgeDirection::Incoming));
                    assert_eq!(config.edge_types, Some(vec!["tagged_with".to_string()]));
                },
                _ => panic!("Expected Cascaded field kind"),
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_create_schema_with_complex_types() {
    let query = "CREATE SCHEMA complex (
        tags Array(String) OPTIONAL,
        metadata Map(String) OPTIONAL,
        embedding Embedding(768) REQUIRED
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse complex types: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.fields.len(), 3);
            
            // Check Array type
            match &stmt.fields[0].field_type {
                FieldType::Array(inner) => {
                    assert_eq!(**inner, FieldType::String);
                },
                _ => panic!("Expected Array type"),
            }
            
            // Check Map type
            match &stmt.fields[1].field_type {
                FieldType::Map(inner) => {
                    assert_eq!(**inner, FieldType::String);
                },
                _ => panic!("Expected Map type"),
            }
            
            // Check Embedding type
            match &stmt.fields[2].field_type {
                FieldType::Embedding(dim) => {
                    assert_eq!(*dim, 768);
                },
                _ => panic!("Expected Embedding type"),
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_create_schema_with_extensibility() {
    let query = "CREATE SCHEMA test (
        id String REQUIRED
    ) WITH EXTENSIBILITY Closed";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse WITH EXTENSIBILITY: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.extensibility, ExtensibilityMode::Closed);
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_cascade_with_decay() {
    let query = "CREATE SCHEMA test (
        score Float CASCADED AS AGGREGATE Sum FROM rating THROUGH edges INCOMING WITH DECAY Exponential(0.5)
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse CASCADE with DECAY: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            match &stmt.fields[0].kind {
                FieldKind::Cascaded(config) => {
                    assert_eq!(config.aggregation, AggregationFunction::Sum);
                    match config.decay {
                        Some(DecayFunction::Exponential { alpha }) => {
                            assert_eq!(alpha, 0.5);
                        },
                        _ => panic!("Expected Exponential decay"),
                    }
                },
                _ => panic!("Expected Cascaded field"),
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_cascade_with_temporal_functions() {
    let query = "CREATE SCHEMA test (
        moving_avg Float CASCADED AS AGGREGATE MovingMean(30) FROM value,
        percentile Float CASCADED AS AGGREGATE Percentile(95) FROM score
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse temporal aggregations: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.fields.len(), 2);
            
            // Check MovingMean
            match &stmt.fields[0].kind {
                FieldKind::Cascaded(config) => {
                    match config.aggregation {
                        AggregationFunction::MovingMean { window_days } => {
                            assert_eq!(window_days, 30);
                        },
                        _ => panic!("Expected MovingMean aggregation"),
                    }
                },
                _ => panic!("Expected Cascaded field"),
            }
            
            // Check Percentile
            match &stmt.fields[1].kind {
                FieldKind::Cascaded(config) => {
                    match config.aggregation {
                        AggregationFunction::Percentile { percentile } => {
                            assert_eq!(percentile, 95);
                        },
                        _ => panic!("Expected Percentile aggregation"),
                    }
                },
                _ => panic!("Expected Cascaded field"),
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_alter_schema_add_field() {
    let query = "ALTER SCHEMA topics ADD FIELD description String OPTIONAL";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse ALTER SCHEMA ADD FIELD: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Alter(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            match stmt.operation {
                AlterOperation::AddField(field) => {
                    assert_eq!(field.name, "description");
                    assert_eq!(field.field_type, FieldType::String);
                    assert!(!field.required);
                },
                _ => panic!("Expected AddField operation"),
            }
        },
        _ => panic!("Expected Schema Alter statement"),
    }
}

#[test]
fn test_parse_alter_schema_drop_field() {
    let query = "ALTER SCHEMA topics DROP FIELD old_field";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse ALTER SCHEMA DROP FIELD: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Alter(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            match stmt.operation {
                AlterOperation::DropField(name) => {
                    assert_eq!(name, "old_field");
                },
                _ => panic!("Expected DropField operation"),
            }
        },
        _ => panic!("Expected Schema Alter statement"),
    }
}

#[test]
fn test_parse_alter_schema_modify_field() {
    let query = "ALTER SCHEMA topics MODIFY FIELD name String REQUIRED DOMAIN_SHARED";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse ALTER SCHEMA MODIFY FIELD: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Alter(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            match stmt.operation {
                AlterOperation::ModifyField { name, definition } => {
                    assert_eq!(name, "name");
                    assert!(definition.required);
                    assert_eq!(definition.scope, Some(PropertyScope::DomainShared));
                },
                _ => panic!("Expected ModifyField operation"),
            }
        },
        _ => panic!("Expected Schema Alter statement"),
    }
}

#[test]
fn test_parse_drop_schema() {
    let query = "DROP SCHEMA topics";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse DROP SCHEMA: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Drop(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            assert!(!stmt.if_exists);
        },
        _ => panic!("Expected Schema Drop statement"),
    }
}

#[test]
fn test_parse_drop_schema_if_exists() {
    let query = "DROP SCHEMA IF EXISTS topics";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse DROP SCHEMA IF EXISTS: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Drop(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            assert!(stmt.if_exists);
        },
        _ => panic!("Expected Schema Drop statement"),
    }
}

#[test]
fn test_parse_describe_schema() {
    let query = "DESCRIBE SCHEMA topics";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse DESCRIBE SCHEMA: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Describe(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            assert!(!stmt.detailed);
        },
        _ => panic!("Expected Schema Describe statement"),
    }
}

#[test]
fn test_parse_describe_schema_detailed() {
    let query = "DESCRIBE SCHEMA topics DETAILED";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse DESCRIBE SCHEMA DETAILED: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Describe(stmt)) => {
            assert_eq!(stmt.collection_name, "topics");
            assert!(stmt.detailed);
        },
        _ => panic!("Expected Schema Describe statement"),
    }
}

#[test]
fn test_parse_all_field_types() {
    let query = "CREATE SCHEMA test (
        str_field String,
        int_field Integer,
        float_field Float,
        bool_field Boolean,
        ts_field Timestamp,
        dur_field Duration,
        date_field Date,
        json_field Json,
        ref_field Reference,
        edge_field Edge,
        pos_field Position3D,
        bytes_field Bytes
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse all field types: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.fields.len(), 12);
            
            let types = vec![
                FieldType::String,
                FieldType::Integer,
                FieldType::Float,
                FieldType::Boolean,
                FieldType::Timestamp,
                FieldType::Duration,
                FieldType::Date,
                FieldType::Json,
                FieldType::Reference,
                FieldType::Edge,
                FieldType::Position3D,
                FieldType::Bytes,
            ];
            
            for (i, expected_type) in types.iter().enumerate() {
                assert_eq!(&stmt.fields[i].field_type, expected_type, 
                          "Field {} type mismatch", i);
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_all_aggregation_functions() {
    let query = "CREATE SCHEMA test (
        sum_field Float CASCADED AS AGGREGATE Sum FROM value,
        avg_field Float CASCADED AS AGGREGATE Average FROM value,
        max_field Float CASCADED AS AGGREGATE Max FROM value,
        min_field Float CASCADED AS AGGREGATE Min FROM value,
        count_field Integer CASCADED AS AGGREGATE Count FROM value,
        latest_field Float CASCADED AS AGGREGATE Latest FROM value,
        first_field Float CASCADED AS AGGREGATE First FROM value
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse aggregation functions: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            assert_eq!(stmt.fields.len(), 7);
            
            let expected_funcs = vec![
                AggregationFunction::Sum,
                AggregationFunction::Average,
                AggregationFunction::Max,
                AggregationFunction::Min,
                AggregationFunction::Count,
                AggregationFunction::Latest,
                AggregationFunction::First,
            ];
            
            for (i, expected_func) in expected_funcs.iter().enumerate() {
                match &stmt.fields[i].kind {
                    FieldKind::Cascaded(config) => {
                        assert_eq!(&config.aggregation, expected_func,
                                  "Field {} aggregation mismatch", i);
                    },
                    _ => panic!("Expected Cascaded field at index {}", i),
                }
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_edge_directions() {
    let query = "CREATE SCHEMA test (
        incoming Float CASCADED AS AGGREGATE Sum FROM value THROUGH edges INCOMING,
        outgoing Float CASCADED AS AGGREGATE Sum FROM value THROUGH edges OUTGOING,
        both Float CASCADED AS AGGREGATE Sum FROM value THROUGH edges BOTH
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse edge directions: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            let directions = vec![
                EdgeDirection::Incoming,
                EdgeDirection::Outgoing,
                EdgeDirection::Both,
            ];
            
            for (i, expected_dir) in directions.iter().enumerate() {
                match &stmt.fields[i].kind {
                    FieldKind::Cascaded(config) => {
                        assert_eq!(config.direction.as_ref().unwrap(), expected_dir,
                                  "Field {} direction mismatch", i);
                    },
                    _ => panic!("Expected Cascaded field"),
                }
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}

#[test]
fn test_parse_multiple_edge_types() {
    let query = "CREATE SCHEMA test (
        score Float CASCADED AS AGGREGATE Average FROM rating THROUGH tagged_with related_to INCOMING
    )";
    
    let result = parse_statement(query);
    assert!(result.is_ok(), "Failed to parse multiple edge types: {:?}", result.err());
    
    match result.unwrap() {
        Statement::Schema(SchemaOperation::Create(stmt)) => {
            match &stmt.fields[0].kind {
                FieldKind::Cascaded(config) => {
                    let edge_types = config.edge_types.as_ref().unwrap();
                    assert_eq!(edge_types.len(), 2);
                    assert!(edge_types.contains(&"tagged_with".to_string()));
                    assert!(edge_types.contains(&"related_to".to_string()));
                },
                _ => panic!("Expected Cascaded field"),
            }
        },
        _ => panic!("Expected Schema Create statement"),
    }
}