-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathddl_tests.rs
1111 lines (1004 loc) · 40.3 KB
/
ddl_tests.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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use index::CreateIndex;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use super::*;
use crate::{deployment_store::generate_index_creation_sql, layout_for_tests::make_dummy_site};
const ID_TYPE: ColumnType = ColumnType::String;
fn test_layout(gql: &str) -> Layout {
let subgraph = DeploymentHash::new("subgraph").unwrap();
let schema = InputSchema::parse_latest(gql, subgraph.clone()).expect("Test schema invalid");
let namespace = Namespace::new("sgd0815".to_owned()).unwrap();
let site = Arc::new(make_dummy_site(subgraph, namespace, "anet".to_string()));
let ents = {
match schema.entity_type("FileThing") {
Ok(entity_type) => BTreeSet::from_iter(vec![entity_type]),
Err(_) => BTreeSet::new(),
}
};
let catalog = Catalog::for_tests(site.clone(), ents).expect("Can not create catalog");
Layout::new(site, &schema, catalog).expect("Failed to construct Layout")
}
#[test]
fn table_is_sane() {
let layout = test_layout(THING_GQL);
let table = layout
.table(&"thing".into())
.expect("failed to get 'thing' table");
assert_eq!(SqlName::from("thing"), table.name);
assert_eq!("Thing", table.object.as_str());
let id = table
.column(&PRIMARY_KEY_COLUMN.into())
.expect("failed to get 'id' column for 'thing' table");
assert_eq!(ID_TYPE, id.column_type);
assert!(!id.is_nullable());
assert!(!id.is_list());
let big_thing = table
.column(&"big_thing".into())
.expect("failed to get 'big_thing' column for 'thing' table");
assert_eq!(ID_TYPE, big_thing.column_type);
assert!(!big_thing.is_nullable());
// Field lookup happens by the SQL name, not the GraphQL name
let bad_sql_name = SqlName("bigThing".to_owned());
assert!(table.column(&bad_sql_name).is_none());
}
// Check that the two strings are the same after replacing runs of
// whitespace with a single space
#[track_caller]
fn check_eqv(left: &str, right: &str) {
let left_s = left.split_whitespace().join(" ");
let right_s = right.split_whitespace().join(" ");
assert_eq!(left_s, right_s);
}
#[test]
fn test_manual_index_creation_ddl() {
let layout = Arc::new(test_layout(BOOKS_GQL));
#[track_caller]
fn assert_generated_sql(
layout: Arc<Layout>,
entity_name: &str,
field_names: Vec<String>,
index_method: &str,
expected_format: &str,
after: Option<BlockNumber>,
) {
let namespace = layout.site.namespace.clone();
let expected = expected_format.replace("{namespace}", namespace.as_str());
let (_, sql): (String, String) = generate_index_creation_sql(
layout.clone(),
entity_name,
field_names,
index::Method::from_str(index_method).unwrap(),
after,
)
.unwrap();
check_eqv(&expected, sql.trim());
}
const BTREE: &str = "btree"; // Assuming index::Method is the enum containing the BTree variant
const GIST: &str = "gist";
assert_generated_sql(
layout.clone(),
"Book",
vec!["id".to_string()],
BTREE,
"create index concurrently if not exists manual_book_id on {namespace}.book using btree (\"id\")",
None
);
assert_generated_sql(
layout.clone(),
"Book",
vec!["content".to_string()],
BTREE,
"create index concurrently if not exists manual_book_content on {namespace}.book using btree (substring(\"content\", 1, 64))",
None
);
assert_generated_sql(
layout.clone(),
"Book",
vec!["title".to_string()],
BTREE,
"create index concurrently if not exists manual_book_title on {namespace}.book using btree (left(\"title\", 256))",
None
);
assert_generated_sql(
layout.clone(),
"Book",
vec!["page_count".to_string()],
BTREE,
"create index concurrently if not exists manual_book_page_count on {namespace}.book using btree (\"page_count\")",
None
);
assert_generated_sql(
layout.clone(),
"Book",
vec!["page_count".to_string(), "title".to_string()],
BTREE,
"create index concurrently if not exists manual_book_page_count_title on {namespace}.book using btree (\"page_count\", left(\"title\", 256))",
None
);
assert_generated_sql(
layout.clone(),
"Book",
vec!["content".to_string(), "block_range".to_string()], // Explicitly including 'block_range'
GIST,
"create index concurrently if not exists manual_book_content_block_range on {namespace}.book using gist (substring(\"content\", 1, 64), block_range)",
None
);
assert_generated_sql(
layout.clone(),
"Book",
vec!["page_count".to_string()],
BTREE,
"create index concurrently if not exists manual_book_page_count_12345 on sgd0815.book using btree (\"page_count\") where coalesce(upper(block_range), 2147483647) > 12345",
Some(12345)
);
}
#[test]
fn generate_postponed_indexes() {
let layout = test_layout(THING_GQL);
let table = layout.table(&SqlName::from("Scalar")).unwrap();
let skip_colums = vec!["id".to_string()];
let query_vec = table.create_postponed_indexes(skip_colums, true);
assert!(query_vec.len() == 7);
let queries = query_vec.join(" ");
check_eqv(THING_POSTPONED_INDEXES, &queries)
}
const THING_POSTPONED_INDEXES: &str = r#"
create index concurrently if not exists attr_1_1_scalar_bool
on "sgd0815"."scalar" using btree("bool");
create index concurrently if not exists attr_1_2_scalar_int
on "sgd0815"."scalar" using btree("int");
create index concurrently if not exists attr_1_3_scalar_big_decimal
on "sgd0815"."scalar" using btree("big_decimal");
create index concurrently if not exists attr_1_4_scalar_string
on "sgd0815"."scalar" using btree(left("string", 256));
create index concurrently if not exists attr_1_5_scalar_bytes
on "sgd0815"."scalar" using btree(substring("bytes", 1, 64));
create index concurrently if not exists attr_1_6_scalar_big_int
on "sgd0815"."scalar" using btree("big_int");
create index concurrently if not exists attr_1_7_scalar_color
on "sgd0815"."scalar" using btree("color");
"#;
impl IndexList {
fn mock_thing_index_list() -> Self {
let mut indexes: HashMap<String, Vec<CreateIndex>> = HashMap::new();
let v1 = vec![
CreateIndex::parse(r#"create index thing_id_block_range_excl on sgd0815.thing using gist (id, block_range)"#.to_string()),
CreateIndex::parse(r#"create index brin_thing on sgd0815."thing" using brin (lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops)"#.to_string()),
// fixme: enable the index bellow once the parsing of statements is fixed, and BlockRangeUpper in particular (issue #5512)
// CreateIndex::parse(r#"create index thing_block_range_closed on sgd0815."thing" using btree (coalesce(upper(block_range), 2147483647)) where coalesce((upper(block_range), 2147483647) < 2147483647)"#.to_string()),
CreateIndex::parse(r#"create index attr_0_0_thing_id on sgd0815."thing" using btree (id)"#.to_string()),
CreateIndex::parse(r#"create index attr_0_1_thing_big_thing on sgd0815."thing" using gist (big_thing, block_range)"#.to_string()),
];
indexes.insert("thing".to_string(), v1);
let v2 = vec![
CreateIndex::parse(r#"create index attr_1_0_scalar_id on sgd0815."scalar" using btree (id)"#.to_string(),),
CreateIndex::parse(r#"create index attr_1_1_scalar_bool on sgd0815."scalar" using btree (bool)"#.to_string(),),
CreateIndex::parse(r#"create index attr_1_2_scalar_int on sgd0815."scalar" using btree (int)"#.to_string(),),
CreateIndex::parse(r#"create index attr_1_3_scalar_big_decimal on sgd0815."scalar" using btree (big_decimal)"#.to_string()),
CreateIndex::parse(r#"create index attr_1_4_scalar_string on sgd0815."scalar" using btree (left(string, 256))"#.to_string()),
CreateIndex::parse(r#"create index attr_1_5_scalar_bytes on sgd0815."scalar" using btree (substring(bytes, 1, 64))"#.to_string()),
CreateIndex::parse(r#"create index attr_1_6_scalar_big_int on sgd0815."scalar" using btree (big_int)"#.to_string()),
CreateIndex::parse(r#"create index attr_1_7_scalar_color on sgd0815."scalar" using btree (color)"#.to_string()),
];
indexes.insert("scalar".to_string(), v2);
let v3 = vec![CreateIndex::parse(
r#"create index attr_2_0_file_thing_id on sgd0815."file_thing" using btree (id)"#
.to_string(),
)];
indexes.insert("file_thing".to_string(), v3);
IndexList { indexes }
}
}
#[test]
fn generate_ddl() {
let layout = test_layout(THING_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
assert_eq!(THING_DDL, &sql); // Use `assert_eq!` to also test the formatting.
let il = IndexList::mock_thing_index_list();
let layout = test_layout(THING_GQL);
let sql = layout.as_ddl(Some(il)).expect("Failed to generate DDL");
check_eqv(THING_DDL_ON_COPY, &sql);
let layout = test_layout(MUSIC_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
check_eqv(MUSIC_DDL, &sql);
let layout = test_layout(FOREST_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
check_eqv(FOREST_DDL, &sql);
let layout = test_layout(FULLTEXT_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
check_eqv(FULLTEXT_DDL, &sql);
let layout = test_layout(FORWARD_ENUM_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
check_eqv(FORWARD_ENUM_SQL, &sql);
let layout = test_layout(TS_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
check_eqv(TS_SQL, &sql);
let layout = test_layout(LIFETIME_GQL);
let sql = layout.as_ddl(None).expect("Failed to generate DDL");
check_eqv(LIFETIME_SQL, &sql);
}
#[test]
fn exlusion_ddl() {
let layout = test_layout(THING_GQL);
let table = layout
.table_for_entity(&layout.input_schema.entity_type("Thing").unwrap())
.unwrap();
// When `as_constraint` is false, just create an index
let mut out = String::new();
table
.exclusion_ddl_inner(&mut out, false)
.expect("can write exclusion DDL");
check_eqv(
r#"create index thing_id_block_range_excl on "sgd0815"."thing" using gist (id, block_range);"#,
out.trim(),
);
// When `as_constraint` is true, add an exclusion constraint
let mut out = String::new();
table
.exclusion_ddl_inner(&mut out, true)
.expect("can write exclusion DDL");
check_eqv(
r#"alter table "sgd0815"."thing" add constraint thing_id_block_range_excl exclude using gist (id with =, block_range with &&);"#,
out.trim(),
);
}
#[test]
fn forward_enum() {
let layout = test_layout(FORWARD_ENUM_GQL);
let table = layout
.table(&SqlName::from("thing"))
.expect("thing table exists");
let column = table
.column(&SqlName::from("orientation"))
.expect("orientation column exists");
assert!(column.is_enum());
}
#[test]
fn can_copy_from() {
let source = test_layout(THING_GQL);
// We can always copy from an identical layout
assert!(source.can_copy_from(&source).is_empty());
// We allow leaving out and adding types, and leaving out attributes
// of existing types
let dest =
test_layout("type Scalar @entity { id: ID } type Other @entity { id: ID, int: Int! }");
assert!(dest.can_copy_from(&source).is_empty());
// We allow making a non-nullable attribute nullable
let dest = test_layout("type Thing @entity { id: ID! }");
assert!(dest.can_copy_from(&source).is_empty());
// We can not turn a non-nullable attribute into a nullable attribute
let dest = test_layout("type Scalar @entity { id: ID! }");
assert_eq!(
vec![
"The attribute Scalar.id is non-nullable, but the \
corresponding attribute in the source is nullable"
],
dest.can_copy_from(&source)
);
// We can not change a scalar field to an array
let dest = test_layout("type Scalar @entity { id: ID, string: [String] }");
assert_eq!(
vec![
"The attribute Scalar.string has type [String], \
but its type in the source is String"
],
dest.can_copy_from(&source)
);
// We can not change an array field to a scalar
assert_eq!(
vec![
"The attribute Scalar.string has type String, \
but its type in the source is [String]"
],
source.can_copy_from(&dest)
);
// We can not change the underlying type of a field
let dest = test_layout("type Scalar @entity { id: ID, color: Int }");
assert_eq!(
vec![
"The attribute Scalar.color has type Int, but \
its type in the source is Color"
],
dest.can_copy_from(&source)
);
// We can not change the underlying type of a field in arrays
let source = test_layout("type Scalar @entity { id: ID, color: [Int!]! }");
let dest = test_layout("type Scalar @entity { id: ID, color: [String!]! }");
assert_eq!(
vec![
"The attribute Scalar.color has type [String!]!, but \
its type in the source is [Int!]!"
],
dest.can_copy_from(&source)
);
}
const THING_GQL: &str = r#"
type Thing @entity {
id: ID!
bigThing: Thing!
}
enum Color { yellow, red, BLUE }
enum Size { small, medium, large }
type Scalar @entity {
id: ID,
bool: Boolean,
int: Int,
bigDecimal: BigDecimal,
string: String,
bytes: Bytes,
bigInt: BigInt,
color: Color,
}
type FileThing @entity {
id: ID!
}
"#;
const THING_DDL: &str = r#"create type sgd0815."color"
as enum ('BLUE', 'red', 'yellow');
create type sgd0815."size"
as enum ('large', 'medium', 'small');
create table "sgd0815"."thing" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"big_thing" text not null
);
alter table "sgd0815"."thing"
add constraint thing_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_thing
on "sgd0815"."thing"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index thing_block_range_closed
on "sgd0815"."thing"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_0_0_thing_id
on "sgd0815"."thing" using btree("id");
create index attr_0_1_thing_big_thing
on "sgd0815"."thing" using gist("big_thing", block_range);
create table "sgd0815"."scalar" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"bool" boolean,
"int" int4,
"big_decimal" numeric,
"string" text,
"bytes" bytea,
"big_int" numeric,
"color" "sgd0815"."color"
);
alter table "sgd0815"."scalar"
add constraint scalar_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_scalar
on "sgd0815"."scalar"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index scalar_block_range_closed
on "sgd0815"."scalar"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_1_0_scalar_id
on "sgd0815"."scalar" using btree("id");
create index attr_1_1_scalar_bool
on "sgd0815"."scalar" using btree("bool");
create index attr_1_2_scalar_int
on "sgd0815"."scalar" using btree("int");
create index attr_1_3_scalar_big_decimal
on "sgd0815"."scalar" using btree("big_decimal");
create index attr_1_4_scalar_string
on "sgd0815"."scalar" using btree(left("string", 256));
create index attr_1_5_scalar_bytes
on "sgd0815"."scalar" using btree(substring("bytes", 1, 64));
create index attr_1_6_scalar_big_int
on "sgd0815"."scalar" using btree("big_int");
create index attr_1_7_scalar_color
on "sgd0815"."scalar" using btree("color");
create table "sgd0815"."file_thing" (
vid bigint primary key,
block_range int4range not null,
causality_region int not null,
"id" text not null
);
alter table "sgd0815"."file_thing"
add constraint file_thing_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_file_thing
on "sgd0815"."file_thing"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index file_thing_block_range_closed
on "sgd0815"."file_thing"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_2_0_file_thing_id
on "sgd0815"."file_thing" using btree("id");
"#;
const THING_DDL_ON_COPY: &str = r#"create type sgd0815."color"
as enum ('BLUE', 'red', 'yellow');
create type sgd0815."size"
as enum ('large', 'medium', 'small');
create table "sgd0815"."thing" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"big_thing" text not null
);
alter table "sgd0815"."thing"
add constraint thing_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_thing
on "sgd0815"."thing"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index thing_block_range_closed
on "sgd0815"."thing"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_0_0_thing_id
on sgd0815."thing" using btree ("id");
create index attr_0_1_thing_big_thing
on sgd0815."thing" using gist ("big_thing", block_range);
create table "sgd0815"."scalar" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"bool" boolean,
"int" int4,
"big_decimal" numeric,
"string" text,
"bytes" bytea,
"big_int" numeric,
"color" "sgd0815"."color"
);
alter table "sgd0815"."scalar"
add constraint scalar_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_scalar
on "sgd0815"."scalar"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index scalar_block_range_closed
on "sgd0815"."scalar"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_1_0_scalar_id
on sgd0815."scalar" using btree ("id");
create table "sgd0815"."file_thing" (
vid bigint primary key,
block_range int4range not null,
causality_region int not null,
"id" text not null
);
alter table "sgd0815"."file_thing"
add constraint file_thing_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_file_thing
on "sgd0815"."file_thing"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index file_thing_block_range_closed
on "sgd0815"."file_thing"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_2_0_file_thing_id
on sgd0815."file_thing" using btree ("id");
"#;
const BOOKS_GQL: &str = r#"type Author @entity {
id: ID!
name: String!
books: [Book!]! @derivedFrom(field: "author")
}
type Book @entity {
id: ID!
title: String!
content: Bytes!
pageCount: BigInt!
author: Author!
}"#;
const MUSIC_GQL: &str = r#"type Musician @entity {
id: ID!
name: String!
mainBand: Band
bands: [Band!]!
writtenSongs: [Song]! @derivedFrom(field: "writtenBy")
}
type Band @entity {
id: ID!
name: String!
members: [Musician!]! @derivedFrom(field: "bands")
originalSongs: [Song!]!
}
type Song @entity(immutable: true) {
id: ID!
title: String!
writtenBy: Musician!
band: Band @derivedFrom(field: "originalSongs")
}
type SongStat @entity {
id: ID!
song: Song @derivedFrom(field: "id")
played: Int!
}"#;
const MUSIC_DDL: &str = r#"create table "sgd0815"."musician" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"name" text not null,
"main_band" text,
"bands" text[] not null
);
alter table "sgd0815"."musician"
add constraint musician_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_musician
on "sgd0815"."musician"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index musician_block_range_closed
on "sgd0815"."musician"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_0_0_musician_id
on "sgd0815"."musician" using btree("id");
create index attr_0_1_musician_name
on "sgd0815"."musician" using btree(left("name", 256));
create index attr_0_2_musician_main_band
on "sgd0815"."musician" using gist("main_band", block_range);
create table "sgd0815"."band" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"name" text not null,
"original_songs" text[] not null
);
alter table "sgd0815"."band"
add constraint band_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_band
on "sgd0815"."band"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index band_block_range_closed
on "sgd0815"."band"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_1_0_band_id
on "sgd0815"."band" using btree("id");
create index attr_1_1_band_name
on "sgd0815"."band" using btree(left("name", 256));
create table "sgd0815"."song" (
vid bigint primary key,
block$ int not null,
"id" text not null,
"title" text not null,
"written_by" text not null,
unique(id)
);
create index song_block
on "sgd0815"."song"(block$);
create index attr_2_0_song_title
on "sgd0815"."song" using btree(left("title", 256));
create index attr_2_1_song_written_by
on "sgd0815"."song" using btree("written_by", block$);
create table "sgd0815"."song_stat" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"played" int4 not null
);
alter table "sgd0815"."song_stat"
add constraint song_stat_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_song_stat
on "sgd0815"."song_stat"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index song_stat_block_range_closed
on "sgd0815"."song_stat"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_3_0_song_stat_id
on "sgd0815"."song_stat" using btree("id");
create index attr_3_1_song_stat_played
on "sgd0815"."song_stat" using btree("played");
"#;
const FOREST_GQL: &str = r#"
interface ForestDweller {
id: ID!,
forest: Forest
}
type Animal implements ForestDweller @entity {
id: ID!,
forest: Forest
}
type Forest @entity {
id: ID!,
# Array of interfaces as derived reference
dwellers: [ForestDweller!]! @derivedFrom(field: "forest")
}
type Habitat @entity {
id: ID!,
# Use interface as direct reference
most_common: ForestDweller!,
dwellers: [ForestDweller!]!
}"#;
const FOREST_DDL: &str = r#"create table "sgd0815"."animal" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"forest" text
);
alter table "sgd0815"."animal"
add constraint animal_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_animal
on "sgd0815"."animal"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index animal_block_range_closed
on "sgd0815"."animal"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_0_0_animal_id
on "sgd0815"."animal" using btree("id");
create index attr_0_1_animal_forest
on "sgd0815"."animal" using gist("forest", block_range);
create table "sgd0815"."forest" (
vid bigint primary key,
block_range int4range not null,
"id" text not null
);
alter table "sgd0815"."forest"
add constraint forest_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_forest
on "sgd0815"."forest"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index forest_block_range_closed
on "sgd0815"."forest"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_1_0_forest_id
on "sgd0815"."forest" using btree("id");
create table "sgd0815"."habitat" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"most_common" text not null,
"dwellers" text[] not null
);
alter table "sgd0815"."habitat"
add constraint habitat_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_habitat
on "sgd0815"."habitat"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index habitat_block_range_closed
on "sgd0815"."habitat"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_2_0_habitat_id
on "sgd0815"."habitat" using btree("id");
create index attr_2_1_habitat_most_common
on "sgd0815"."habitat" using gist("most_common", block_range);
"#;
const FULLTEXT_GQL: &str = r#"
type _Schema_ @fulltext(
name: "search"
language: en
algorithm: rank
include: [
{
entity: "Animal",
fields: [
{name: "name"},
{name: "species"}
]
}
]
)
type Animal @entity {
id: ID!,
name: String!
species: String!
forest: Forest
}
type Forest @entity {
id: ID!,
dwellers: [Animal!]! @derivedFrom(field: "forest")
}
type Habitat @entity {
id: ID!,
most_common: Animal!,
dwellers: [Animal!]!
}"#;
const FULLTEXT_DDL: &str = r#"create table "sgd0815"."animal" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"name" text not null,
"species" text not null,
"forest" text,
"search" tsvector
);
alter table "sgd0815"."animal"
add constraint animal_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_animal
on "sgd0815"."animal"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index animal_block_range_closed
on "sgd0815"."animal"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_0_0_animal_id
on "sgd0815"."animal" using btree("id");
create index attr_0_1_animal_name
on "sgd0815"."animal" using btree(left("name", 256));
create index attr_0_2_animal_species
on "sgd0815"."animal" using btree(left("species", 256));
create index attr_0_3_animal_forest
on "sgd0815"."animal" using gist("forest", block_range);
create index attr_0_4_animal_search
on "sgd0815"."animal" using gin("search");
create table "sgd0815"."forest" (
vid bigint primary key,
block_range int4range not null,
"id" text not null
);
alter table "sgd0815"."forest"
add constraint forest_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_forest
on "sgd0815"."forest"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index forest_block_range_closed
on "sgd0815"."forest"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_1_0_forest_id
on "sgd0815"."forest" using btree("id");
create table "sgd0815"."habitat" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"most_common" text not null,
"dwellers" text[] not null
);
alter table "sgd0815"."habitat"
add constraint habitat_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_habitat
on "sgd0815"."habitat"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index habitat_block_range_closed
on "sgd0815"."habitat"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_2_0_habitat_id
on "sgd0815"."habitat" using btree("id");
create index attr_2_1_habitat_most_common
on "sgd0815"."habitat" using gist("most_common", block_range);
"#;
const FORWARD_ENUM_GQL: &str = r#"
type Thing @entity {
id: ID!,
orientation: Orientation!
}
enum Orientation {
UP, DOWN
}
"#;
const FORWARD_ENUM_SQL: &str = r#"create type sgd0815."orientation"
as enum ('DOWN', 'UP');
create table "sgd0815"."thing" (
vid bigint primary key,
block_range int4range not null,
"id" text not null,
"orientation" "sgd0815"."orientation" not null
);
alter table "sgd0815"."thing"
add constraint thing_id_block_range_excl exclude using gist (id with =, block_range with &&);
create index brin_thing
on "sgd0815"."thing"
using brin(lower(block_range) int4_minmax_ops, coalesce(upper(block_range), 2147483647) int4_minmax_ops, vid int8_minmax_ops);
create index thing_block_range_closed
on "sgd0815"."thing"(coalesce(upper(block_range), 2147483647))
where coalesce(upper(block_range), 2147483647) < 2147483647;
create index attr_0_0_thing_id
on "sgd0815"."thing" using btree("id");
create index attr_0_1_thing_orientation
on "sgd0815"."thing" using btree("orientation");
"#;
const TS_GQL: &str = r#"
type Data @entity(timeseries: true) {
id: Int8!
timestamp: Timestamp!
amount: BigDecimal!
}
type Stats @aggregation(intervals: ["hour", "day"], source: "Data") {
id: Int8!
timestamp: Timestamp!
volume: BigDecimal! @aggregate(fn: "sum", arg: "amount")
maxPrice: BigDecimal! @aggregate(fn: "max", arg: "amount")
}
"#;
const TS_SQL: &str = r#"
create table "sgd0815"."data" (
vid bigint primary key,
block$ int not null,
"id" int8 not null,
"timestamp" timestamptz not null,
"amount" numeric not null,
unique(id)
);
create index data_block
on "sgd0815"."data"(block$);
create index attr_0_0_data_timestamp
on "sgd0815"."data" using btree("timestamp");
create index attr_0_1_data_amount
on "sgd0815"."data" using btree("amount");
create table "sgd0815"."stats_hour" (
vid bigserial primary key,
block$ int not null,
"id" int8 not null,
"timestamp" timestamptz not null,
"volume" numeric not null,
"max_price" numeric not null,
unique(id)
);
create index stats_hour_block
on "sgd0815"."stats_hour"(block$);
create index attr_1_0_stats_hour_timestamp
on "sgd0815"."stats_hour" using btree("timestamp");
create index attr_1_1_stats_hour_volume
on "sgd0815"."stats_hour" using btree("volume");
create index attr_1_2_stats_hour_max_price
on "sgd0815"."stats_hour" using btree("max_price");
create table "sgd0815"."stats_day" (
vid bigserial primary key,
block$ int not null,
"id" int8 not null,
"timestamp" timestamptz not null,
"volume" numeric not null,
"max_price" numeric not null,
unique(id)
);
create index stats_day_block
on "sgd0815"."stats_day"(block$);
create index attr_2_0_stats_day_timestamp
on "sgd0815"."stats_day" using btree("timestamp");
create index attr_2_1_stats_day_volume
on "sgd0815"."stats_day" using btree("volume");
create index attr_2_2_stats_day_max_price
on "sgd0815"."stats_day" using btree("max_price");"#;
const LIFETIME_GQL: &str = r#"
type Data @entity(timeseries: true) {
id: Int8!
timestamp: Timestamp!
group1: Int!
group2: Int!
amount: BigDecimal!
}
type Stats1 @aggregation(intervals: ["hour", "day"], source: "Data") {
id: Int8!
timestamp: Timestamp!
volume: BigDecimal! @aggregate(fn: "sum", arg: "amount", cumulative: true)
}
type Stats2 @aggregation(intervals: ["hour", "day"], source: "Data") {
id: Int8!
timestamp: Timestamp!
group1: Int!
volume: BigDecimal! @aggregate(fn: "sum", arg: "amount", cumulative: true)
}
type Stats3 @aggregation(intervals: ["hour", "day"], source: "Data") {
id: Int8!
timestamp: Timestamp!
group2: Int!
group1: Int!
volume: BigDecimal! @aggregate(fn: "sum", arg: "amount", cumulative: true)
}
type Stats2 @aggregation(intervals: ["hour", "day"], source: "Data") {
id: Int8!
timestamp: Timestamp!
group1: Int!
group2: Int!
volume: BigDecimal! @aggregate(fn: "sum", arg: "amount", cumulative: true)
}
"#;
const LIFETIME_SQL: &str = r#"
create table "sgd0815"."data" (
vid bigint primary key,
block$ int not null,
"id" int8 not null,
"timestamp" timestamptz not null,
"group_1" int4 not null,
"group_2" int4 not null,
"amount" numeric not null,
unique(id)
);
create index data_block
on "sgd0815"."data"(block$);
create index attr_0_0_data_timestamp
on "sgd0815"."data" using btree("timestamp");
create index attr_0_1_data_group_1
on "sgd0815"."data" using btree("group_1");
create index attr_0_2_data_group_2
on "sgd0815"."data" using btree("group_2");
create index attr_0_3_data_amount
on "sgd0815"."data" using btree("amount");
create table "sgd0815"."stats_1_hour" (
vid bigserial primary key,
block$ int not null,
"id" int8 not null,
"timestamp" timestamptz not null,
"volume" numeric not null,
unique(id)