@malloydata/malloy-tests 0.0.299 → 0.0.300

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,887 +21,279 @@
21
21
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- import type {
25
- ModelDef,
26
- StructDef,
27
- TableSourceDef,
28
- JoinFieldDef,
29
- } from '@malloydata/malloy';
30
-
31
- import {fStringEq, fToIF, fToQF, fYearEq} from '../util';
32
-
33
- import {medicareModel, medicareStateFacts} from './medicare_model';
34
-
35
- function withJoin(
36
- sd: TableSourceDef,
37
- join: 'one' | 'many',
38
- as: string,
39
- keyExpr: string
40
- ): JoinFieldDef {
41
- const [leftKey, rightKey] = keyExpr.split('=');
42
- const ret: JoinFieldDef = {
43
- ...sd,
44
- join,
45
- onExpression: {
46
- node: '=',
47
- kids: {
48
- left: {node: 'field', path: leftKey.split('.')},
49
- right: {node: 'field', path: rightKey.split('.')},
50
- },
51
- },
52
- };
53
- if (as !== ret.name) {
54
- ret.as = as;
24
+ import {medicareModel} from './medicare_model';
25
+
26
+ const faaModel = `
27
+ source: aircraft_models is bigquery.table('malloytest.aircraft_models') extend {
28
+ primary_key: aircraft_model_code
29
+
30
+ measure:
31
+ total_seats is seats.sum()
32
+ }
33
+
34
+ source: airports is bigquery.table('malloytest.airports') extend {
35
+ primary_key: code
36
+ measure: \`count\` is count()
37
+ }
38
+
39
+ source: carriers is bigquery.table('malloytest.carriers') extend {
40
+ primary_key: code
55
41
  }
56
- return ret;
57
- }
58
-
59
- const AIRCRAFT_MODELS: TableSourceDef = {
60
- name: 'malloydata-org.malloytest.aircraft_models',
61
- as: 'aircraft_models',
62
- dialect: 'standardsql',
63
- connection: 'bigquery',
64
- primaryKey: 'aircraft_model_code',
65
- type: 'table',
66
- tablePath: 'malloydata-org.malloytest.aircraft_models',
67
- fields: [
68
- {type: 'string', name: 'aircraft_model_code'},
69
- {type: 'string', name: 'manufacturer'},
70
- {type: 'string', name: 'model'},
71
- {
72
- type: 'number',
73
- name: 'aircraft_type_id',
74
- numberType: 'integer',
75
- },
76
- {
77
- type: 'number',
78
- name: 'aircraft_engine_type_id',
79
- numberType: 'integer',
80
- },
81
- {
82
- type: 'number',
83
- name: 'aircraft_category_id',
84
- numberType: 'integer',
85
- },
86
- {type: 'number', name: 'amateur', numberType: 'integer'},
87
- {type: 'number', name: 'engines', numberType: 'integer'},
88
- {type: 'number', name: 'seats', numberType: 'integer'},
89
- {type: 'number', name: 'weight', numberType: 'integer'},
90
- {type: 'number', name: 'speed', numberType: 'integer'},
91
- {
92
- type: 'number',
93
- expressionType: 'aggregate',
94
- name: 'total_seats',
95
- e: {
96
- node: 'aggregate',
97
- function: 'sum',
98
- e: {node: 'field', path: ['seats']},
99
- },
100
- },
101
- ],
102
- };
103
-
104
- const AIRPORTS: TableSourceDef = {
105
- name: 'malloydata-org.malloytest.airports',
106
- as: 'airports',
107
- dialect: 'standardsql',
108
- type: 'table',
109
- tablePath: 'malloydata-org.malloytest.airports',
110
- connection: 'bigquery',
111
- primaryKey: 'code',
112
- fields: [
113
- {type: 'number', name: 'id', numberType: 'integer'},
114
- {type: 'string', name: 'code'},
115
- {type: 'string', name: 'site_number'},
116
- {type: 'string', name: 'fac_type', as: 'facility_type'},
117
- {type: 'string', name: 'fac_use', as: 'facility_use'},
118
- {type: 'string', name: 'faa_region'},
119
- {type: 'string', name: 'faa_dist'},
120
- {type: 'string', name: 'city'},
121
- {type: 'string', name: 'county'},
122
- {type: 'string', name: 'state'},
123
- {type: 'string', name: 'full_name'},
124
- {type: 'string', name: 'own_type'},
125
- {type: 'number', name: 'longitude', numberType: 'float'},
126
- {type: 'number', name: 'latitude', numberType: 'float'},
127
- {type: 'number', name: 'elevation', numberType: 'integer'},
128
- {type: 'string', name: 'aero_cht'},
129
- {type: 'number', name: 'cbd_dist', numberType: 'integer'},
130
- {type: 'string', name: 'cbd_dir'},
131
- {type: 'string', name: 'act_date'},
132
- {type: 'string', name: 'cert'},
133
- {type: 'string', name: 'fed_agree'},
134
- {type: 'string', name: 'cust_intl'},
135
- {type: 'string', name: 'c_ldg_rts'},
136
- {type: 'string', name: 'joint_use'},
137
- {type: 'string', name: 'mil_rts'},
138
- {type: 'string', name: 'cntl_twr'},
139
- {type: 'string', name: 'major'},
140
- {
141
- type: 'number',
142
- name: 'count',
143
- expressionType: 'aggregate',
144
- e: {node: 'aggregate', function: 'count', e: {node: ''}},
145
- },
146
- ],
147
- };
148
-
149
- const CARRIERS: TableSourceDef = {
150
- name: 'carriers',
151
- dialect: 'standardsql',
152
- type: 'table',
153
- tablePath: 'malloydata-org.malloytest.carriers',
154
- connection: 'bigquery',
155
- primaryKey: 'code',
156
- fields: [
157
- {type: 'string', name: 'code'},
158
- {type: 'string', name: 'name'},
159
- {type: 'string', name: 'nickname'},
160
- ],
161
- };
162
- const AIRCRAFT: TableSourceDef = {
163
- name: 'aircraft',
164
- dialect: 'standardsql',
165
- type: 'table',
166
- tablePath: 'malloydata-org.malloytest.aircraft',
167
- primaryKey: 'tail_num',
168
- connection: 'bigquery',
169
- fields: [
170
- {type: 'string', name: 'tail_num'},
171
- {type: 'string', name: 'aircraft_serial'},
172
- {type: 'string', name: 'aircraft_model_code'},
173
- {type: 'string', name: 'aircraft_engine_code'},
174
- {type: 'number', name: 'year_built', numberType: 'integer'},
175
- {
176
- type: 'number',
177
- name: 'aircraft_type_id',
178
- numberType: 'integer',
179
- },
180
- {
181
- type: 'number',
182
- name: 'aircraft_engine_type_id',
183
- numberType: 'integer',
184
- },
185
- {
186
- type: 'number',
187
- name: 'registrant_type_id',
188
- numberType: 'integer',
189
- },
190
- {type: 'string', name: 'name'},
191
- {type: 'string', name: 'address1'},
192
- {type: 'string', name: 'address2'},
193
- {type: 'string', name: 'city'},
194
- {type: 'string', name: 'state'},
195
- {type: 'string', name: 'zip'},
196
- {type: 'string', name: 'region'},
197
- {type: 'string', name: 'county'},
198
- {type: 'string', name: 'country'},
199
- {type: 'string', name: 'certification'},
200
- {type: 'string', name: 'status_code'},
201
- {type: 'string', name: 'mode_s_code'},
202
- {type: 'string', name: 'fract_owner'},
203
- {
204
- type: 'number',
205
- name: 'aircraft_count',
206
- expressionType: 'aggregate',
207
- e: {node: 'aggregate', function: 'count', e: {node: ''}},
208
- },
209
- {
210
- type: 'number',
211
- name: 'total_engines',
212
- expressionType: 'aggregate',
213
- e: {
214
- node: 'aggregate',
215
- function: 'sum',
216
- e: {node: 'field', path: ['aircraft_models', 'engines']},
217
- },
218
- },
219
-
220
- // subjoin aircraft models
221
- withJoin(
222
- AIRCRAFT_MODELS,
223
- 'one',
224
- 'aircraft_models',
225
- 'aircraft_model_code=aircraft_models.aircraft_model_code'
226
- ),
227
- ],
228
- };
229
-
230
- /** Flight Model */
231
- export const FLIGHTS_EXPLORE: StructDef = {
232
- name: 'malloydata-org.malloytest.flights',
233
- as: 'flights',
234
- dialect: 'standardsql',
235
- type: 'table',
236
- tablePath: 'malloydata-org.malloytest.flights',
237
- connection: 'bigquery',
238
- primaryKey: 'id2',
239
- fields: [
240
- // Fields in the flights table.
241
- {type: 'string', name: 'carrier'},
242
- {type: 'string', name: 'origin', as: 'origin_code'},
243
- {type: 'timestamp', name: 'dep_time'},
244
- {type: 'string', name: 'destination', as: 'destination_code'},
245
- {type: 'string', name: 'flight_num'},
246
- {type: 'number', name: 'flight_time', numberType: 'integer'},
247
- {type: 'string', name: 'tail_num'},
248
- {type: 'number', name: 'dep_delay', numberType: 'integer'},
249
- {type: 'number', name: 'arr_delay', numberType: 'integer'},
250
- {type: 'number', name: 'taxi_out', numberType: 'integer'},
251
- {type: 'number', name: 'taxi_in', numberType: 'integer'},
252
- {type: 'number', name: 'distance', numberType: 'integer'},
253
- {type: 'string', name: 'cancelled'},
254
- {type: 'string', name: 'diverted'},
255
- {type: 'number', name: 'id2', numberType: 'integer'},
256
- {
257
- type: 'number',
258
- name: 'flight_count',
259
- expressionType: 'aggregate',
260
- e: {node: 'aggregate', function: 'count', e: {node: ''}},
261
- },
262
- {
263
- type: 'number',
264
- name: 'total_distance',
265
- expressionType: 'aggregate',
266
- e: {
267
- node: 'aggregate',
268
- function: 'sum',
269
- e: {node: 'field', path: ['distance']},
270
- },
271
- },
272
-
273
- withJoin(CARRIERS, 'one', 'carriers', 'carrier=carriers.code'),
274
-
275
- // aircraft
276
- withJoin(AIRCRAFT, 'one', 'aircraft', 'tail_num=aircraft.tail_num'),
277
-
278
- // origin
279
- withJoin(AIRPORTS, 'one', 'origin', 'origin_code=origin.code'),
280
-
281
- // destination
282
- withJoin(
283
- AIRPORTS,
284
- 'one',
285
- 'destination',
286
- 'destination_code=destination.code'
287
- ),
288
-
289
- // derived table from a named query.
290
- {
291
- type: 'query_source',
292
- name: 'aircraft_facts',
293
- dialect: 'standardsql',
294
- connection: 'bigquery',
295
- query: {
296
- type: 'query',
297
- structRef: 'flights',
298
- name: 'aircraft_facts_query',
299
- pipeline: [
300
- {
301
- type: 'reduce',
302
- queryFields: fToQF([
303
- 'tail_num',
304
- {
305
- type: 'number',
306
- name: 'lifetime_distance',
307
- expressionType: 'aggregate',
308
- e: {
309
- node: 'aggregate',
310
- function: 'sum',
311
- e: {node: 'field', path: ['distance']},
312
- },
313
- },
314
- ]),
315
- },
316
- ],
317
- },
318
- join: 'one',
319
- onExpression: {
320
- node: '=',
321
- kids: {
322
- left: {node: 'field', path: ['tail_num']},
323
- right: {node: 'field', path: ['aircraft_facts', 'tail_num']},
324
- },
325
- },
326
- primaryKey: 'tail_num',
327
- fields: [
328
- {type: 'string', name: 'tail_num'},
329
- {type: 'number', name: 'lifetime_distance'},
330
- ],
331
- },
332
-
333
- {
334
- type: 'turtle',
335
- name: 'flights_by_carrier',
336
- pipeline: [
337
- {
338
- type: 'reduce',
339
- queryFields: fToQF([
340
- 'carriers.name',
341
- 'flight_count',
342
- // { name: "origin.count", as: "origin_count" },
343
- {
344
- type: 'number',
345
- name: 'origin_count',
346
- expressionType: 'aggregate',
347
- e: {
348
- node: 'aggregate',
349
- function: 'count',
350
- e: {node: ''},
351
- structPath: ['origin'],
352
- },
353
- },
354
- {
355
- type: 'number',
356
- name: 'my_total_distance',
357
- expressionType: 'aggregate',
358
- e: {
359
- node: 'aggregate',
360
- function: 'sum',
361
- e: {node: 'field', path: ['distance']},
362
- },
363
- },
364
- ]),
365
- orderBy: [{field: 'name', dir: 'asc'}],
366
- },
367
- ],
368
- },
369
- {
370
- type: 'turtle',
371
- name: 'flights_by_carrier_2001_2002',
372
- pipeline: [
373
- {
374
- type: 'reduce',
375
-
376
- queryFields: fToQF([
377
- 'carriers.name',
378
- {
379
- name: 'flights_2001',
380
- type: 'number',
381
- expressionType: 'aggregate',
382
- e: {
383
- node: 'filteredExpr',
384
- kids: {
385
- filterList: [fYearEq('dep_time', 2001)],
386
- e: {node: 'aggregate', function: 'count', e: {node: ''}},
387
- },
388
- },
389
- },
390
- {
391
- name: 'flights_2002',
392
- type: 'number',
393
- expressionType: 'aggregate',
394
- e: {
395
- node: 'filteredExpr',
396
- kids: {
397
- filterList: [fYearEq('dep_time', 2001)],
398
- e: {node: 'aggregate', function: 'count', e: {node: ''}},
399
- },
400
- },
401
- },
402
- ]),
403
- orderBy: [{field: 'name', dir: 'asc'}],
404
- },
405
- ],
406
- },
407
- // EXPLORE flights | REDUCE destination.city, flight_count ORDER BY 2 desc
408
- {
409
- type: 'turtle',
410
- name: 'flights_by_city_top_5',
411
- pipeline: [
412
- {
413
- type: 'reduce',
414
- queryFields: fToQF(['destination.city', 'flight_count']),
415
- orderBy: [{field: 2, dir: 'desc'}],
416
- limit: 5,
417
- },
418
- ],
419
- },
420
- // EXPLORE flights [origin.state:'CA] | REDUCE aircraft.aircraft_models.manufacturer, aircraft.aircraft_models.manufacturer.
421
- // aircraft.aircraft_count, flight_count ORDER BY flight_count LIMIT 5
422
- {
423
- type: 'turtle',
424
- name: 'flights_by_model',
425
- pipeline: [
426
- {
427
- type: 'reduce',
428
- queryFields: fToQF([
429
- 'aircraft.aircraft_models.manufacturer',
430
- 'aircraft.aircraft_models.model',
431
- 'aircraft.aircraft_count',
432
- 'flight_count',
433
- ]),
434
- orderBy: [{field: 'flight_count', dir: 'desc'}],
435
- filterList: [fStringEq('origin.state', 'CA')],
436
- limit: 5,
437
- },
438
- ],
439
- },
440
- // EXPLORE flights | REDUCE tail_num, total_distance as lifetime_distance
441
- {
442
- type: 'turtle',
443
- name: 'aircraft_facts_query',
444
- pipeline: [
445
- {
446
- type: 'reduce',
447
-
448
- queryFields: fToQF([
449
- 'tail_num',
450
- {
451
- type: 'number',
452
- name: 'lifetime_distance',
453
- expressionType: 'aggregate',
454
- e: {
455
- node: 'aggregate',
456
- function: 'sum',
457
- e: {node: 'field', path: ['distance']},
458
- },
459
- },
460
- ]),
461
- },
462
- ],
463
- },
464
- // expore flights | reduce carriers.name, aircraft.total_engines, flight_count
465
- {
466
- type: 'turtle',
467
- name: 'carriers_by_total_engines',
468
- pipeline: [
469
- {
470
- type: 'reduce',
471
- queryFields: fToQF([
472
- 'carriers.name',
473
- 'aircraft.total_engines',
474
- 'flight_count',
475
- ]),
476
- },
477
- ],
478
- },
479
- // expore flights | reduce aircraft_facts.lifetime_distance, flight_count
480
- {
481
- type: 'turtle',
482
- name: 'aircraft_facts_test',
483
- pipeline: [
484
- {
485
- type: 'reduce',
486
- queryFields: fToQF([
487
- 'aircraft_facts.lifetime_distance',
488
- 'flight_count',
489
- ]),
490
- },
491
- ],
492
- },
493
- // expore flights | reduce flight_count, origin.city, origin.state
494
- {
495
- type: 'turtle',
496
- name: 'measures_first',
497
- pipeline: [
498
- {
499
- type: 'reduce',
500
- queryFields: fToQF(['flight_count', 'origin.city', 'origin.state']),
501
- },
502
- ],
503
- },
504
-
505
- // explore flights
506
- // | reduce
507
- // carrier
508
- // flight_count
509
- // top_5_routes is (reduce top 5 order by 3 desc
510
- // origin_code
511
- // destination_code,
512
- // flight_count
513
- // )
514
- {
515
- type: 'turtle',
516
- name: 'first_turtle',
517
- pipeline: [
518
- {
519
- type: 'reduce',
520
- queryFields: fToQF([
521
- 'carrier',
522
- 'flight_count',
523
- {
524
- type: 'turtle',
525
- name: 'top_5_routes',
526
- pipeline: [
527
- {
528
- type: 'reduce',
529
- queryFields: fToQF([
530
- 'origin_code',
531
- 'destination_code',
532
- 'flight_count',
533
- ]),
534
- limit: 5,
535
- orderBy: [{field: 'flight_count', dir: 'desc'}],
536
- },
537
- ],
538
- },
539
- ]),
540
- },
541
- ],
542
- },
543
- // explore flights
544
- // | reduce order by 3 desc
545
- // origin_code
546
- // destination_code
547
- // flight_count
548
- {
549
- type: 'turtle',
550
- name: 'top_5_routes',
551
- pipeline: [
552
- {
553
- type: 'reduce',
554
- queryFields: fToQF([
555
- 'origin_code',
556
- 'destination_code',
557
- 'flight_count',
558
- ]),
559
- limit: 5,
560
- orderBy: [{field: 'flight_count', dir: 'desc'}],
561
- },
562
- ],
563
- },
564
- // EXPLORE flights | REDUCE carrier, flight_count, top_5_routes
565
- {
566
- type: 'turtle',
567
- name: 'carriers_routes',
568
- pipeline: [
569
- {
570
- type: 'reduce',
571
- queryFields: fToQF(['carrier', 'flight_count', 'top_5_routes']),
572
- },
573
- ],
574
- },
575
- // EXPLORE flights [destination.state: 'NY] | REDUCE destination.code as newyork_airport, flight_count ORDER BY 2 DESC
576
- {
577
- type: 'turtle',
578
- name: 'new_york_airports',
579
- pipeline: [
580
- {
581
- type: 'reduce',
582
- queryFields: fToQF(['destination.code', 'flight_count']),
583
- filterList: [fStringEq('destination.state', 'NY')],
584
- orderBy: [{field: 'flight_count', dir: 'desc'}],
585
- },
586
- ],
587
- },
588
- // EXPLORE flights | REDUCE aircraft.aircraft_models.manufacturer, aircraft.aircraft_count, flight_count ORDER BY 3 DESC
589
- {
590
- type: 'turtle',
591
- name: 'flights_by_manufacturer',
592
- pipeline: [
593
- {
594
- type: 'reduce',
595
- queryFields: fToQF([
596
- 'aircraft.aircraft_models.manufacturer',
597
- 'aircraft.aircraft_count',
598
- 'flight_count',
599
- ]),
600
- orderBy: [{field: 'flight_count', dir: 'desc'}],
601
- limit: 5,
602
- },
603
- ],
604
- },
605
- // EXPLORE flights [origin.state:'CA'x] | REDUCE carrier, flight_count, top_5_routes, flights_by_manufacturer
606
- {
607
- type: 'turtle',
608
- name: 'carriers_routes_manufacturer',
609
- pipeline: [
610
- {
611
- type: 'reduce',
612
- queryFields: fToQF([
613
- 'carrier',
614
- 'flight_count',
615
- 'top_5_routes',
616
- 'flights_by_manufacturer',
617
- ]),
618
- filterList: [fStringEq('origin.state', 'CA')],
619
- },
620
- ],
621
- },
622
- {
623
- type: 'turtle',
624
- name: 'top_5_routes_carriers',
625
- pipeline: [
626
- {
627
- type: 'reduce',
628
- queryFields: fToQF([
629
- 'origin_code',
630
- 'destination_code',
631
- 'flight_count',
632
- 'flights_by_carrier',
633
- ]),
634
- limit: 5,
635
- orderBy: [{field: 'flight_count', dir: 'desc'}],
636
- },
637
- ],
638
- },
639
- {
640
- type: 'turtle',
641
- name: 'flights_by_carrier_with_totals',
642
- pipeline: [
643
- {
644
- type: 'reduce',
645
- filterList: [fStringEq('origin.state', 'CA')],
646
- queryFields: fToQF([
647
- {
648
- type: 'turtle',
649
- name: 'main',
650
- pipeline: [
651
- {
652
- type: 'reduce',
653
- queryFields: fToQF([
654
- 'carriers.name',
655
- 'flight_count',
656
- //{ name: "origin.count", as: "origin_count" },
657
- {
658
- type: 'number',
659
- name: 'origin_count',
660
- expressionType: 'aggregate',
661
- e: {
662
- node: 'aggregate',
663
- function: 'count',
664
- e: {node: ''},
665
- structPath: ['origin'],
666
- },
667
- },
668
- ]),
669
- orderBy: [{field: 'flight_count', dir: 'desc'}],
670
- },
671
- ],
672
- },
673
- {
674
- type: 'turtle',
675
- name: 'totals',
676
- pipeline: [
677
- {
678
- type: 'reduce',
679
- queryFields: fToQF(['flight_count']),
680
- },
681
- ],
682
- },
683
- ]),
684
- },
685
- ],
686
- },
687
- {
688
- type: 'turtle',
689
- name: 'flight_detail',
690
- pipeline: [
691
- {
692
- type: 'reduce',
693
- orderBy: [{field: 'dep_time', dir: 'asc'}],
694
- queryFields: fToQF([
695
- 'id2',
696
- 'dep_time',
697
- 'tail_num',
698
- 'carrier',
699
- 'origin_code',
700
- 'destination_code',
701
- 'distance',
702
- 'dep_delay',
703
- ]),
704
- limit: 500,
705
- },
706
- ],
707
- },
708
- {
709
- type: 'turtle',
710
- name: 'some_measures',
711
- pipeline: [
712
- {
713
- type: 'reduce',
714
- queryFields: fToQF([
715
- 'flight_count',
716
- 'total_distance',
717
- 'aircraft.aircraft_count',
718
- ]),
719
- },
720
- ],
721
- },
722
- {
723
- type: 'turtle',
724
- name: 'flights_routes_sessionized',
725
- pipeline: [
726
- {
727
- type: 'reduce',
728
- filterList: [
729
- fStringEq('origin.state', 'CA'),
730
- fStringEq('carrier', 'UA'),
731
- ],
732
- limit: 20,
733
- queryFields: fToQF([
734
- {
735
- type: 'timestamp',
736
- name: 'dep_time',
737
- as: 'dep_date',
738
- timeframe: 'day',
739
- },
740
- 'carrier',
741
- {
742
- type: 'turtle',
743
- name: 'routes',
744
- pipeline: [
745
- {
746
- type: 'reduce',
747
- queryFields: fToQF([
748
- 'origin_code',
749
- 'destination_code',
750
- 'flight_count',
751
- {
752
- type: 'turtle',
753
- name: 'flight_detail',
754
- pipeline: [
755
- {
756
- type: 'reduce',
757
- orderBy: [{field: 'dep_time', dir: 'asc'}],
758
- limit: 5,
759
- queryFields: fToQF([
760
- 'id2',
761
- 'dep_time',
762
- 'tail_num',
763
- 'flight_num',
764
- 'dep_delay',
765
- ]),
766
- },
767
- ],
768
- },
769
- ]),
770
- orderBy: [{field: 'flight_count', dir: 'desc'}],
771
- },
772
- ],
773
- },
774
- ]),
775
- },
776
- ],
777
- },
778
- /*
779
- FROM flights |
780
- REDUCE
781
- dep_time.date as dep_date,
782
- carrier,
783
- flight_count,
784
- (REDUCE
785
- tail_num,
786
- flight_count,
787
- (REDUCE
788
- id2, dep_time, origin_code, destination_code, flight_num, dep_delay
789
- ORDER BY 2
790
- )
791
- ) as aircraft
792
- */
793
- {
794
- type: 'turtle',
795
- name: 'flights_aircraft_sessionized',
796
- pipeline: [
797
- {
798
- type: 'reduce',
799
- filterList: [fStringEq('carrier', 'UA')],
800
- limit: 2,
801
- queryFields: fToQF([
802
- {
803
- type: 'timestamp',
804
- name: 'dep_time',
805
- as: 'dep_date',
806
- timeframe: 'day',
807
- },
808
- 'carrier',
809
- 'flight_count',
810
- {
811
- type: 'turtle',
812
- name: 'aircraft',
813
- pipeline: [
814
- {
815
- type: 'reduce',
816
- limit: 10,
817
- queryFields: fToQF([
818
- 'tail_num',
819
- 'flight_count',
820
- {
821
- type: 'turtle',
822
- name: 'flight_detail',
823
- pipeline: [
824
- {
825
- type: 'reduce',
826
- orderBy: [{field: 'dep_time', dir: 'asc'}],
827
- queryFields: fToQF([
828
- 'id2',
829
- 'dep_time',
830
- 'origin_code',
831
- 'destination_code',
832
- 'flight_num',
833
- 'dep_delay',
834
- ]),
835
- },
836
- ],
837
- },
838
- ]),
839
- orderBy: [{field: 'flight_count', dir: 'desc'}],
840
- },
841
- ],
842
- },
843
- ]),
844
- },
845
- ],
846
- },
847
- {
848
- type: 'turtle',
849
- name: 'search_index',
850
- pipeline: [
851
- {
852
- type: 'index',
853
- weightMeasure: 'flight_count',
854
- indexFields: fToIF([
855
- 'carrier',
856
- 'origin_code',
857
- 'destination_code',
858
- 'carriers.name',
859
- 'carriers.nickname',
860
- 'carriers.code',
861
- 'origin.code',
862
- 'origin.full_name',
863
- 'origin.city',
864
- 'origin.state',
865
- 'destination.code',
866
- 'destination.full_name',
867
- 'destination.city',
868
- 'destination.state',
869
- 'aircraft.aircraft_model_code',
870
- 'aircraft.aircraft_models.manufacturer',
871
- 'aircraft.aircraft_models.model',
872
- ]),
873
- },
874
- ],
875
- },
876
- // {
877
- // type: "reduce",
878
- // name: "some_measures",
879
- // fields: ["flight_count", "total_distance"],
880
- // },
881
- ],
882
- };
883
-
884
- const tableAirports = {...AIRPORTS, as: 'table_airports'};
885
-
886
- /** Test model */
887
- export const testModel: ModelDef = {
888
- name: 'Hand Coded Models',
889
- exports: [
890
- 'flights',
891
- 'table_airports',
892
- 'medicare_test',
893
- 'medicare_state_facts',
894
- // "aircraft",
895
- ],
896
- queryList: [],
897
- dependencies: {},
898
- contents: {
899
- flights: FLIGHTS_EXPLORE,
900
- table_airports: tableAirports,
901
- medicare_test: medicareModel,
902
- medicare_state_facts: medicareStateFacts,
903
- // aircraft: aircraftHandStructDef,
904
- },
905
- };
906
-
907
- // // clang-format on
42
+
43
+ source: aircraft is bigquery.table('malloytest.aircraft') extend {
44
+ primary_key: tail_num
45
+
46
+ join_one: aircraft_models on aircraft_model_code = aircraft_models.aircraft_model_code
47
+
48
+ measure:
49
+ aircraft_count is count()
50
+ total_engines is aircraft_models.engines.sum()
51
+ }
52
+
53
+ source: flights is bigquery.table('malloytest.flights') extend {
54
+ primary_key: id2
55
+
56
+ rename:
57
+ origin_code is origin
58
+ destination_code is destination
59
+
60
+ measure:
61
+ flight_count is count()
62
+ total_distance is distance.sum()
63
+
64
+ join_one: carriers on carrier = carriers.code
65
+
66
+ join_one: aircraft on tail_num = aircraft.tail_num
67
+
68
+ join_one: origin is airports on origin_code = origin.code
69
+
70
+ join_one: destination is airports on destination_code = destination.code
71
+
72
+ join_one: aircraft_facts is bigquery.table('malloytest.flights') -> {
73
+ group_by: tail_num
74
+ aggregate: lifetime_distance is distance.sum()
75
+ } extend {
76
+ primary_key: tail_num
77
+ } on tail_num = aircraft_facts.tail_num
78
+
79
+ view: flights_by_carrier is {
80
+ group_by: carriers.name
81
+ aggregate: flight_count
82
+ aggregate: origin_count is origin.count()
83
+ aggregate: my_total_distance is distance.sum()
84
+ order_by: name asc
85
+ }
86
+
87
+ view: flights_by_carrier_2001_2002 is {
88
+ group_by: carriers.name
89
+ aggregate: flights_2001 is count() {
90
+ where: dep_time.year = @2001
91
+ }
92
+ aggregate: flights_2002 is count() {
93
+ where: dep_time.year = @2002
94
+ }
95
+ order_by: name asc
96
+ }
97
+
98
+ view: flights_by_city_top_5 is {
99
+ group_by: destination.city
100
+ aggregate: flight_count
101
+ limit: 5
102
+ }
103
+
104
+ view: flights_by_model is {
105
+ group_by:
106
+ aircraft.aircraft_models.manufacturer
107
+ aircraft.aircraft_models.model
108
+ aggregate:
109
+ aircraft.aircraft_count
110
+ flight_count
111
+ limit: 5
112
+ }
113
+
114
+ view: aircraft_facts_query is {
115
+ group_by: tail_num
116
+ aggregate: lifetime_distance is distance.sum()
117
+ }
118
+
119
+ view: carriers_by_total_engines is {
120
+ group_by: carriers.name
121
+ aggregate:
122
+ aircraft.total_engines
123
+ flight_count
124
+ }
125
+
126
+ view: aircraft_facts_test is {
127
+ group_by: aircraft_facts.lifetime_distance
128
+ aggregate: flight_count
129
+ }
130
+
131
+ view: measures_first is {
132
+ aggregate:
133
+ flight_count
134
+ group_by:
135
+ origin.city
136
+ origin.state
137
+ }
138
+
139
+ view: top_5_routes is {
140
+ group_by:
141
+ origin_code
142
+ destination_code
143
+ aggregate: flight_count
144
+ order_by: flight_count desc
145
+ limit: 5
146
+ }
147
+
148
+ view: first_turtle is {
149
+ group_by: carrier
150
+ aggregate: flight_count
151
+ nest: top_5_routes
152
+ }
153
+
154
+ view: carriers_routes is {
155
+ group_by: carrier
156
+ aggregate: flight_count
157
+ nest: top_5_routes
158
+ }
159
+
160
+ view: new_york_airports is {
161
+ group_by: destination.code
162
+ aggregate: flight_count
163
+ where: destination.state = 'NY'
164
+ order_by: flight_count desc
165
+ }
166
+
167
+ view: flights_by_manufacturer is {
168
+ group_by: aircraft.aircraft_models.manufacturer
169
+ aggregate:
170
+ aircraft.aircraft_count
171
+ flight_count
172
+ limit: 5
173
+ }
174
+
175
+ view: carriers_routes_manufacturer is {
176
+ group_by: carrier
177
+ aggregate: flight_count
178
+ nest: top_5_routes
179
+ nest: flights_by_manufacturer
180
+ }
181
+
182
+ view: top_5_routes_carriers is {
183
+ group_by:
184
+ origin_code
185
+ destination_code
186
+ aggregate: flight_count
187
+ nest: flights_by_carrier
188
+ limit: 5
189
+ order_by: flight_count desc
190
+ }
191
+
192
+ view: flights_by_carrier_with_totals is {
193
+ where: origin.state = 'CA'
194
+ nest: main is {
195
+ group_by: carriers.name
196
+ aggregate: flight_count
197
+ aggregate: origin_count is origin.count()
198
+ order_by: flight_count desc
199
+ }
200
+ }
201
+
202
+ view: totals is {
203
+ aggregate: flight_count
204
+ }
205
+
206
+ view: flight_detail is {
207
+ group_by:
208
+ id2
209
+ dep_time
210
+ tail_num
211
+ carrier
212
+ origin_code
213
+ destination_code
214
+ distance
215
+ dep_delay
216
+ order_by: dep_time asc
217
+ limit: 500
218
+ }
219
+
220
+ view: some_measures is {
221
+ aggregate:
222
+ flight_count
223
+ total_distance
224
+ aircraft.aircraft_count
225
+ }
226
+
227
+ view: flights_routes_sessionized is {
228
+ where: origin.state = 'CA'
229
+ where: carrier = 'UA'
230
+ limit: 20
231
+ group_by: dep_date is dep_time.day
232
+ group_by: carrier
233
+ nest: routes is {
234
+ group_by:
235
+ origin_code
236
+ destination_code
237
+ aggregate:
238
+ flight_count
239
+ nest: flight_detail is {
240
+ group_by:
241
+ id2
242
+ dep_time
243
+ tail_num
244
+ distance
245
+ dep_delay
246
+ order_by: dep_time asc
247
+ limit: 5
248
+ }
249
+ order_by: flight_count desc
250
+ }
251
+ }
252
+
253
+ view: flights_aircraft_sessionized is {
254
+ group_by: dep_date is dep_time.day
255
+ group_by: carrier
256
+ aggregate: flight_count
257
+ nest: aircraft is {
258
+ group_by: tail_num
259
+ aggregate: flight_count
260
+ nest: flight_detail is {
261
+ group_by:
262
+ id2
263
+ dep_time
264
+ origin_code
265
+ destination_code
266
+ flight_num
267
+ dep_delay
268
+ order_by: 2
269
+ }
270
+ order_by: flight_count desc
271
+ }
272
+ }
273
+
274
+ view: search_index is {
275
+ index:
276
+ carrier
277
+ origin_code
278
+ destination_code
279
+ carriers.name
280
+ carriers.nickname
281
+ carriers.code
282
+ origin.code
283
+ origin.full_name
284
+ origin.city
285
+ origin.state
286
+ destination.code
287
+ destination.full_name
288
+ destination.city
289
+ destination.state
290
+ aircraft.aircraft_model_code
291
+ aircraft.aircraft_models.manufacturer
292
+ aircraft.aircraft_models.model
293
+ }
294
+ }
295
+
296
+ source: table_airports is airports
297
+ `;
298
+
299
+ export const testModel = faaModel + medicareModel;