@malloydata/malloy-tests 0.0.66-dev230807232724

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.
Files changed (169) hide show
  1. package/README.md +29 -0
  2. package/bin/dump_malloytest.sh +1 -0
  3. package/bin/postgres_init.sh +9 -0
  4. package/bin/postgres_start.sh +2 -0
  5. package/bin/postgres_stop.sh +2 -0
  6. package/data/duckdb/README.md +4 -0
  7. package/data/duckdb/aircraft.parquet +0 -0
  8. package/data/duckdb/aircraft_models.parquet +0 -0
  9. package/data/duckdb/airports.parquet +0 -0
  10. package/data/duckdb/alltypes.parquet +0 -0
  11. package/data/duckdb/alltypes2.parquet +0 -0
  12. package/data/duckdb/bq_medicare_test.parquet +0 -0
  13. package/data/duckdb/carriers.parquet +0 -0
  14. package/data/duckdb/flights/part.0.parquet +0 -0
  15. package/data/duckdb/flights/part.1.parquet +0 -0
  16. package/data/duckdb/flights/part.2.parquet +0 -0
  17. package/data/duckdb/flights.parquet +0 -0
  18. package/data/duckdb/flights_partitioned.parquet +0 -0
  19. package/data/duckdb/ga_sample.parquet +0 -0
  20. package/data/duckdb/numbers.parquet +0 -0
  21. package/data/duckdb/state_facts.parquet +0 -0
  22. package/data/duckdb/test.json +3 -0
  23. package/data/duckdb/words.parquet +0 -0
  24. package/data/duckdb/words_bigger.parquet +0 -0
  25. package/data/postgres/malloytest-postgres.sql.gz +0 -0
  26. package/data/postgres/state_facts.sql +366 -0
  27. package/dist/api.spec.d.ts +1 -0
  28. package/dist/api.spec.js +77 -0
  29. package/dist/api.spec.js.map +1 -0
  30. package/dist/databases/all/expr.spec.d.ts +3 -0
  31. package/dist/databases/all/expr.spec.js +581 -0
  32. package/dist/databases/all/expr.spec.js.map +1 -0
  33. package/dist/databases/all/functions.spec.d.ts +2 -0
  34. package/dist/databases/all/functions.spec.js +784 -0
  35. package/dist/databases/all/functions.spec.js.map +1 -0
  36. package/dist/databases/all/index.spec.d.ts +2 -0
  37. package/dist/databases/all/index.spec.js +154 -0
  38. package/dist/databases/all/index.spec.js.map +1 -0
  39. package/dist/databases/all/join.spec.d.ts +2 -0
  40. package/dist/databases/all/join.spec.js +309 -0
  41. package/dist/databases/all/join.spec.js.map +1 -0
  42. package/dist/databases/all/nomodel.spec.d.ts +2 -0
  43. package/dist/databases/all/nomodel.spec.js +957 -0
  44. package/dist/databases/all/nomodel.spec.js.map +1 -0
  45. package/dist/databases/all/orderby.spec.d.ts +2 -0
  46. package/dist/databases/all/orderby.spec.js +224 -0
  47. package/dist/databases/all/orderby.spec.js.map +1 -0
  48. package/dist/databases/all/problems.spec.d.ts +2 -0
  49. package/dist/databases/all/problems.spec.js +113 -0
  50. package/dist/databases/all/problems.spec.js.map +1 -0
  51. package/dist/databases/all/sql_expressions.spec.d.ts +2 -0
  52. package/dist/databases/all/sql_expressions.spec.js +96 -0
  53. package/dist/databases/all/sql_expressions.spec.js.map +1 -0
  54. package/dist/databases/all/time.spec.d.ts +3 -0
  55. package/dist/databases/all/time.spec.js +647 -0
  56. package/dist/databases/all/time.spec.js.map +1 -0
  57. package/dist/databases/bigquery/double_truncation.spec.d.ts +1 -0
  58. package/dist/databases/bigquery/double_truncation.spec.js +50 -0
  59. package/dist/databases/bigquery/double_truncation.spec.js.map +1 -0
  60. package/dist/databases/bigquery/handexpr.spec.d.ts +1 -0
  61. package/dist/databases/bigquery/handexpr.spec.js +713 -0
  62. package/dist/databases/bigquery/handexpr.spec.js.map +1 -0
  63. package/dist/databases/bigquery/injestion_time_partitioning.spec.d.ts +1 -0
  64. package/dist/databases/bigquery/injestion_time_partitioning.spec.js +235 -0
  65. package/dist/databases/bigquery/injestion_time_partitioning.spec.js.map +1 -0
  66. package/dist/databases/bigquery/joined_filters.spec.d.ts +1 -0
  67. package/dist/databases/bigquery/joined_filters.spec.js +72 -0
  68. package/dist/databases/bigquery/joined_filters.spec.js.map +1 -0
  69. package/dist/databases/bigquery/json.spec.d.ts +1 -0
  70. package/dist/databases/bigquery/json.spec.js +86 -0
  71. package/dist/databases/bigquery/json.spec.js.map +1 -0
  72. package/dist/databases/bigquery/malloy_query.spec.d.ts +1 -0
  73. package/dist/databases/bigquery/malloy_query.spec.js +906 -0
  74. package/dist/databases/bigquery/malloy_query.spec.js.map +1 -0
  75. package/dist/databases/bigquery/performance.skipped.spec.d.ts +1 -0
  76. package/dist/databases/bigquery/performance.skipped.spec.js +70 -0
  77. package/dist/databases/bigquery/performance.skipped.spec.js.map +1 -0
  78. package/dist/databases/bigquery/time.spec.d.ts +1 -0
  79. package/dist/databases/bigquery/time.spec.js +55 -0
  80. package/dist/databases/bigquery/time.spec.js.map +1 -0
  81. package/dist/databases/bigquery/wildcard_table_names.spec.d.ts +1 -0
  82. package/dist/databases/bigquery/wildcard_table_names.spec.js +212 -0
  83. package/dist/databases/bigquery/wildcard_table_names.spec.js.map +1 -0
  84. package/dist/databases/bigquery-duckdb/nested_source_table.spec.d.ts +1 -0
  85. package/dist/databases/bigquery-duckdb/nested_source_table.spec.js +247 -0
  86. package/dist/databases/bigquery-duckdb/nested_source_table.spec.js.map +1 -0
  87. package/dist/databases/bigquery-postgres/multi_connection.spec.d.ts +1 -0
  88. package/dist/databases/bigquery-postgres/multi_connection.spec.js +138 -0
  89. package/dist/databases/bigquery-postgres/multi_connection.spec.js.map +1 -0
  90. package/dist/databases/bigquery-postgres/streaming.spec.d.ts +1 -0
  91. package/dist/databases/bigquery-postgres/streaming.spec.js +88 -0
  92. package/dist/databases/bigquery-postgres/streaming.spec.js.map +1 -0
  93. package/dist/databases/duckdb/duckdb.spec.d.ts +1 -0
  94. package/dist/databases/duckdb/duckdb.spec.js +116 -0
  95. package/dist/databases/duckdb/duckdb.spec.js.map +1 -0
  96. package/dist/databases/postgres/postgres.spec.d.ts +1 -0
  97. package/dist/databases/postgres/postgres.spec.js +137 -0
  98. package/dist/databases/postgres/postgres.spec.js.map +1 -0
  99. package/dist/databases/shared/test_list.d.ts +3 -0
  100. package/dist/databases/shared/test_list.js +24 -0
  101. package/dist/databases/shared/test_list.js.map +1 -0
  102. package/dist/dependencies.spec.d.ts +1 -0
  103. package/dist/dependencies.spec.js +63 -0
  104. package/dist/dependencies.spec.js.map +1 -0
  105. package/dist/index.d.ts +11 -0
  106. package/dist/index.js +49 -0
  107. package/dist/index.js.map +1 -0
  108. package/dist/model/utils.spec.d.ts +1 -0
  109. package/dist/model/utils.spec.js +38 -0
  110. package/dist/model/utils.spec.js.map +1 -0
  111. package/dist/models/faa_model.d.ts +5 -0
  112. package/dist/models/faa_model.js +977 -0
  113. package/dist/models/faa_model.js.map +1 -0
  114. package/dist/models/medicare_model.d.ts +4 -0
  115. package/dist/models/medicare_model.js +243 -0
  116. package/dist/models/medicare_model.js.map +1 -0
  117. package/dist/render/render.spec.d.ts +1 -0
  118. package/dist/render/render.spec.js +551 -0
  119. package/dist/render/render.spec.js.map +1 -0
  120. package/dist/runtimes.d.ts +33 -0
  121. package/dist/runtimes.js +147 -0
  122. package/dist/runtimes.js.map +1 -0
  123. package/dist/tags.spec.d.ts +1 -0
  124. package/dist/tags.spec.js +259 -0
  125. package/dist/tags.spec.js.map +1 -0
  126. package/dist/util/db-jest-matchers.d.ts +19 -0
  127. package/dist/util/db-jest-matchers.js +90 -0
  128. package/dist/util/db-jest-matchers.js.map +1 -0
  129. package/dist/util/index.d.ts +16 -0
  130. package/dist/util/index.js +171 -0
  131. package/dist/util/index.js.map +1 -0
  132. package/package.json +35 -0
  133. package/src/api.spec.ts +84 -0
  134. package/src/databases/all/expr.spec.ts +704 -0
  135. package/src/databases/all/functions.spec.ts +1135 -0
  136. package/src/databases/all/index.spec.ts +178 -0
  137. package/src/databases/all/join.spec.ts +350 -0
  138. package/src/databases/all/nomodel.spec.ts +1159 -0
  139. package/src/databases/all/orderby.spec.ts +268 -0
  140. package/src/databases/all/problems.spec.ts +121 -0
  141. package/src/databases/all/sql_expressions.spec.ts +107 -0
  142. package/src/databases/all/time.spec.ts +793 -0
  143. package/src/databases/bigquery/double_truncation.spec.ts +51 -0
  144. package/src/databases/bigquery/handexpr.spec.ts +782 -0
  145. package/src/databases/bigquery/injestion_time_partitioning.spec.ts +268 -0
  146. package/src/databases/bigquery/joined_filters.spec.ts +75 -0
  147. package/src/databases/bigquery/json.spec.ts +95 -0
  148. package/src/databases/bigquery/malloy_query.spec.ts +1036 -0
  149. package/src/databases/bigquery/performance.skipped.spec.ts +73 -0
  150. package/src/databases/bigquery/time.spec.ts +59 -0
  151. package/src/databases/bigquery/wildcard_table_names.spec.ts +233 -0
  152. package/src/databases/bigquery-duckdb/nested_source_table.spec.ts +268 -0
  153. package/src/databases/bigquery-postgres/multi_connection.spec.ts +143 -0
  154. package/src/databases/bigquery-postgres/streaming.spec.ts +100 -0
  155. package/src/databases/duckdb/duckdb.spec.ts +131 -0
  156. package/src/databases/postgres/postgres.spec.ts +167 -0
  157. package/src/databases/shared/test_list.ts +24 -0
  158. package/src/dependencies.spec.ts +62 -0
  159. package/src/index.ts +36 -0
  160. package/src/model/utils.spec.ts +37 -0
  161. package/src/models/faa_model.ts +995 -0
  162. package/src/models/medicare_model.ts +246 -0
  163. package/src/render/__snapshots__/render.spec.ts.snap +12574 -0
  164. package/src/render/render.spec.ts +670 -0
  165. package/src/runtimes.ts +182 -0
  166. package/src/tags.spec.ts +277 -0
  167. package/src/util/db-jest-matchers.ts +128 -0
  168. package/src/util/index.ts +191 -0
  169. package/tsconfig.json +25 -0
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import * as malloy from '@malloydata/malloy';
25
+ import {RuntimeList} from '../../runtimes';
26
+
27
+ // Chris, please rework.
28
+ it.skip('accessors are not too expensive', async () => {
29
+ // If this test fails, consideration should be given to how much time using
30
+ // the accessors costs.
31
+ const runtime = new RuntimeList(['bigquery']).runtimeMap.get('bigquery');
32
+
33
+ expect(runtime).toBeDefined();
34
+ if (runtime) {
35
+ const result = await runtime
36
+ .loadQuery(
37
+ "source 'malloy-data.faa.flights' | reduce inner is (reduce top 1000000 distance id2)"
38
+ )
39
+ .run();
40
+ let noAccessorTime;
41
+ let withAccessorTime;
42
+ {
43
+ const start = performance.now();
44
+ const inner = result.data.value[0]['inner'];
45
+ let total = 0;
46
+ let count = 0;
47
+ for (const row of inner as malloy.QueryData) {
48
+ total += row['distance'] as number;
49
+ count += 1;
50
+ }
51
+ noAccessorTime = performance.now() - start;
52
+ expect(total).toBe(88560989);
53
+ expect(count).toBe(1000000);
54
+ // Rough bound, not using accessors should be no more 50 milleseconds / million rows
55
+ expect(noAccessorTime).toBeLessThan(50);
56
+ }
57
+ {
58
+ const start = performance.now();
59
+ const inner = result.data.row(0).cell('inner').array;
60
+ let total = 0;
61
+ let count = 0;
62
+ for (const row of inner) {
63
+ total += row.cell('distance').number.value;
64
+ count += 1;
65
+ }
66
+ withAccessorTime = performance.now() - start;
67
+ expect(total).toBe(88560989);
68
+ expect(count).toBe(1000000);
69
+ // Rough bound, using accessors should be no more 70 milleseconds / million rows
70
+ expect(withAccessorTime).toBeLessThan(150);
71
+ }
72
+ }
73
+ });
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {DateTime} from 'luxon';
25
+ import {RuntimeList} from '../../runtimes';
26
+ import '../../util/db-jest-matchers';
27
+ import {describeIfDatabaseAvailable} from '../../util';
28
+
29
+ const [describe, databases] = describeIfDatabaseAvailable(['bigquery']);
30
+ describe('BigQuery double truncation', () => {
31
+ const runtimes = new RuntimeList(databases);
32
+ const runtime = runtimes.runtimeMap.get('bigquery');
33
+
34
+ afterAll(async () => {
35
+ await runtimes.closeAll();
36
+ });
37
+
38
+ const utc_2020 = DateTime.fromObject({
39
+ year: 2020,
40
+ month: 2,
41
+ day: 20,
42
+ hour: 0,
43
+ minute: 0,
44
+ second: 0,
45
+ zone: 'UTC',
46
+ });
47
+ // TODO: this test is not working in this file.
48
+ test.skip('can use unsupported types', async () => {
49
+ await expect(runtime).queryMatches(
50
+ `sql: timeData is { connection: "bigquery" select: """
51
+ SELECT DATETIME '2020-02-20 00:00:00' as t_datetime
52
+ """}
53
+ query: from_sql(timeData) -> {
54
+ project: mex_220 is t_datetime::timestamp
55
+ }`,
56
+ {mex_220: utc_2020.toJSDate()}
57
+ );
58
+ });
59
+ });
@@ -0,0 +1,233 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {RuntimeList} from '../../runtimes';
25
+ import {describeIfDatabaseAvailable} from '../../util';
26
+
27
+ const [describe, databases] = describeIfDatabaseAvailable(['bigquery']);
28
+ describe('Wildcard BigQuery Tables', () => {
29
+ const runtimes = new RuntimeList(databases);
30
+
31
+ afterAll(async () => {
32
+ await runtimes.closeAll();
33
+ });
34
+
35
+ test('test valid wildcard table name without using _TABLE_SUFFIX', async () => {
36
+ const runtime = runtimes.runtimeMap.get('bigquery');
37
+ expect(runtime).toBeDefined();
38
+ if (runtime) {
39
+ const result = await runtime
40
+ .loadQuery(
41
+ `
42
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
43
+ primary_key: id
44
+ measure: aircraft_count is count()
45
+ }
46
+
47
+ query: aircraft -> {
48
+ aggregate: aircraft_count
49
+ }
50
+ `
51
+ )
52
+ .run();
53
+ expect(result.data.value[0]['aircraft_count']).toBe(9);
54
+ }
55
+ });
56
+
57
+ test('test wildcard table name and _TABLE_SUFFIX as a filter', async () => {
58
+ const runtime = runtimes.runtimeMap.get('bigquery');
59
+ expect(runtime).toBeDefined();
60
+ if (runtime) {
61
+ const result = await runtime
62
+ .loadQuery(
63
+ `
64
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
65
+ primary_key: id
66
+ measure: aircraft_count is count()
67
+ where: _TABLE_SUFFIX = '01'
68
+ }
69
+
70
+ query: aircraft -> {
71
+ aggregate: aircraft_count
72
+ }
73
+ `
74
+ )
75
+ .run();
76
+ expect(result.data.value[0]['aircraft_count']).toBe(5);
77
+ }
78
+ });
79
+
80
+ test('test join between wildcard tables without using _TABLE_SUFFIX', async () => {
81
+ const runtime = runtimes.runtimeMap.get('bigquery');
82
+ expect(runtime).toBeDefined();
83
+ if (runtime) {
84
+ const result = await runtime
85
+ .loadQuery(
86
+ `
87
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
88
+ primary_key: id
89
+ }
90
+
91
+ source: state_facts is table('malloy-data.malloytest.state_facts') {
92
+ join_many: aircraft on state = aircraft.state
93
+ }
94
+ query: state_facts -> {
95
+ group_by: aircraft.state
96
+ aggregate: aircraft_count is aircraft.count()
97
+ order_by: 1
98
+ }
99
+ `
100
+ )
101
+ .run();
102
+ expect(result.data.value).toStrictEqual([
103
+ {state: null, aircraft_count: 43},
104
+ {state: 'IA', aircraft_count: 1},
105
+ {state: 'KS', aircraft_count: 1},
106
+ {state: 'LA', aircraft_count: 1},
107
+ {state: 'MO', aircraft_count: 1},
108
+ {state: 'NH', aircraft_count: 1},
109
+ {state: 'OK', aircraft_count: 1},
110
+ {state: 'OR', aircraft_count: 2},
111
+ {state: 'TX', aircraft_count: 1},
112
+ ]);
113
+ }
114
+ });
115
+
116
+ test('test join with wildcard tables and _TABLE_SUFFIX as source a filter', async () => {
117
+ const runtime = runtimes.runtimeMap.get('bigquery');
118
+ expect(runtime).toBeDefined();
119
+ if (runtime) {
120
+ const result = await runtime
121
+ .loadQuery(
122
+ `
123
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
124
+ primary_key: id
125
+ where: _TABLE_SUFFIX = '02'
126
+ }
127
+
128
+ source: state_facts is table('malloy-data.malloytest.state_facts') {
129
+ join_many: aircraft on state = aircraft.state
130
+ }
131
+ query: state_facts -> {
132
+ group_by: aircraft.state
133
+ aggregate: aircraft_count is aircraft.count()
134
+ order_by: 1
135
+ }
136
+ `
137
+ )
138
+ .run();
139
+ expect(result.data.value).toStrictEqual([
140
+ {state: null, aircraft_count: 47},
141
+ {state: 'KS', aircraft_count: 1},
142
+ {state: 'LA', aircraft_count: 1},
143
+ {state: 'OK', aircraft_count: 1},
144
+ {state: 'OR', aircraft_count: 1},
145
+ ]);
146
+ }
147
+ });
148
+
149
+ test('test join with wildcard tables and _TABLE_SUFFIX as a filter and generated_uuid', async () => {
150
+ // issue # 1147
151
+ const runtime = runtimes.runtimeMap.get('bigquery');
152
+ expect(runtime).toBeDefined();
153
+ if (runtime) {
154
+ const result = await runtime
155
+ .loadQuery(
156
+ `
157
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
158
+ join_many: state_facts is table('malloy-data.malloytest.state_facts')
159
+ on state_facts.state = state
160
+ }
161
+
162
+ query: aircraft -> {
163
+ group_by: state_facts.state
164
+ aggregate: aircraft_count is count()
165
+ where: _TABLE_SUFFIX = '02'
166
+ order_by: 1 desc
167
+ limit: 1
168
+ }
169
+ `
170
+ )
171
+ .run();
172
+ expect(result.data.path(0, 'state').value).toBe('OR');
173
+ }
174
+ });
175
+
176
+ test('test a join with wildcard tables and _TABLE_SUFFIX column is used as a group-by key', async () => {
177
+ const runtime = runtimes.runtimeMap.get('bigquery');
178
+ expect(runtime).toBeDefined();
179
+ if (runtime) {
180
+ const result = await runtime
181
+ .loadQuery(
182
+ `
183
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
184
+ primary_key: id
185
+ where: _TABLE_SUFFIX = '02'
186
+ }
187
+
188
+ source: state_facts is table('malloy-data.malloytest.state_facts') {
189
+ join_many: aircraft on state = aircraft.state
190
+ }
191
+ query: state_facts -> {
192
+ group_by: aircraft._TABLE_SUFFIX
193
+ aggregate: aircraft_count is aircraft.count()
194
+ order_by: 1
195
+ }
196
+ `
197
+ )
198
+ .run();
199
+ expect(result.data.value).toStrictEqual([
200
+ {_TABLE_SUFFIX: null, aircraft_count: 47},
201
+ {_TABLE_SUFFIX: '02', aircraft_count: 4},
202
+ ]);
203
+ }
204
+ });
205
+
206
+ test('test a join with wildcard tables and _TABLE_SUFFIX column is used as a group-by key and a filter', async () => {
207
+ const runtime = runtimes.runtimeMap.get('bigquery');
208
+ expect(runtime).toBeDefined();
209
+ if (runtime) {
210
+ const result = await runtime
211
+ .loadQuery(
212
+ `
213
+ source: aircraft is table('malloy-data.malloytest.wildcard_aircraft_*') {
214
+ primary_key: id
215
+ where: _TABLE_SUFFIX = '02'
216
+ }
217
+
218
+ source: state_facts is table('malloy-data.malloytest.state_facts') {
219
+ join_many: aircraft on state = aircraft.state
220
+ }
221
+ query: state_facts -> {
222
+ group_by: aircraft._TABLE_SUFFIX
223
+ aggregate: aircraft_count is aircraft.count()
224
+ where: aircraft._TABLE_SUFFIX = '01'
225
+ order_by: 1
226
+ }
227
+ `
228
+ )
229
+ .run();
230
+ expect(result.data.value).toStrictEqual([]);
231
+ }
232
+ });
233
+ });
@@ -0,0 +1,268 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ /* eslint-disable no-console */
26
+
27
+ import '../../util/db-jest-matchers';
28
+ import {RuntimeList} from '../../runtimes';
29
+ import {describeIfDatabaseAvailable} from '../../util';
30
+
31
+ // No prebuilt shared model, each test is complete. Makes debugging easier.
32
+
33
+ const [describe, databases] = describeIfDatabaseAvailable([
34
+ 'bigquery',
35
+ 'duckdb',
36
+ ]);
37
+
38
+ const modelText = `
39
+ source:ga_sessions is table('malloytest.ga_sample'){
40
+
41
+ measure:
42
+ user_count is count(distinct fullVisitorId)
43
+ session_count is count()
44
+ total_visits is totals.visits.sum()
45
+ total_hits is totals.hits.sum()
46
+ total_page_views is totals.pageviews.sum()
47
+ t2 is totals.pageviews.sum()
48
+ total_productRevenue is hits.product.productRevenue.sum()
49
+ hits_count is hits.count()
50
+ sold_count is hits.count() { where: hits.product.productQuantity > 0 }
51
+
52
+ query: by_source is {
53
+ where: trafficSource.source != '(direct)'
54
+ group_by: trafficSource.source
55
+ aggregate: hits_count
56
+ limit: 10
57
+ }
58
+ query: by_adContent_bar_chart is {
59
+ group_by: device.browser
60
+ aggregate: user_count
61
+ group_by: device.deviceCategory
62
+ }
63
+ query: by_region is {
64
+ where: geoNetwork.region !~ '%demo%'
65
+ group_by: geoNetwork.region
66
+ aggregate: user_count
67
+ limit: 10
68
+ }
69
+ query: by_device is {
70
+ group_by: device.browser
71
+ aggregate: user_count
72
+ group_by: device.deviceCategory
73
+ }
74
+ query: by_category is {
75
+ group_by: category is hits.product.v2ProductCategory
76
+ aggregate: total_productRevenue
77
+ aggregate: sold_count
78
+ limit: 10
79
+ }
80
+ query: by_hour_of_day is {
81
+ // group_by: gsession_hour is hour(start_time::timestamp)
82
+ aggregate: session_count
83
+ order_by: 1
84
+ }
85
+
86
+ query: page_load_times is {
87
+ group_by: hits.page.pageTitle
88
+ aggregate: hit_count is hits.count()
89
+ nest: load_bar_chart is {
90
+ group_by: hit_seconds is floor(hits.latencyTracking.pageLoadTime / 2) * 2
91
+ aggregate: hits_count
92
+ }
93
+ limit: 10
94
+ }
95
+
96
+ query: by_page_title is { where: totals.transactionRevenue > 0
97
+ group_by: hits.page.pageTitle
98
+ aggregate: hits_count
99
+ aggregate: sold_count
100
+ }
101
+
102
+ query: by_all is {
103
+ nest: by_source
104
+ nest: by_adContent_bar_chart
105
+ nest: by_region
106
+ nest: by_category
107
+ }
108
+
109
+ query: search_index is {
110
+ index: *, hits.*, customDimensions.* totals.*, trafficSource.*, hits.product.*
111
+ sample: 1%
112
+ }
113
+ }
114
+
115
+ query: sessions_dashboard is ga_sessions -> {
116
+ nest:
117
+ by_region
118
+ by_device
119
+ by_source
120
+ by_category is {
121
+ group_by: category is hits.product.v2ProductCategory
122
+ aggregate: total_productRevenue
123
+ aggregate: sold_count
124
+ }
125
+ }
126
+ `;
127
+
128
+ const runtimes = new RuntimeList(databases);
129
+ describe.each(runtimes.runtimeList)(
130
+ 'Nested Source Table - %s',
131
+ (databaseName, runtime) => {
132
+ test(`repeated child of record - ${databaseName}`, async () => {
133
+ const result = await runtime
134
+ .loadModel(modelText)
135
+ .loadQuery(
136
+ `
137
+ query: ga_sessions->by_page_title
138
+ `
139
+ )
140
+ .run();
141
+ // console.log(result.data.toObject());
142
+ // console.log(result.sql);
143
+ expect(result.data.path(0, 'pageTitle').value).toBe('Shopping Cart');
144
+ });
145
+
146
+ // SKIPPED because the tests intermittently fail and lloyd said
147
+ // "I bet it has to do with my sampling test on indexing. Intermittently
148
+ // getting different results. I'd comment out the test and I'll take a
149
+ // look at it when I get back." and that seems reasonable.
150
+ // "search_index" is the test that failed, but a skipped both
151
+ // this one and "manual index". Here's the errror:
152
+ // * Nested Source Table › search_index - duckdb
153
+ //
154
+ // expect(received).toBe(expected) // Object.is equality
155
+ //
156
+ // Expected: "Organic Search"
157
+ // Received: "Referral"
158
+ test.skip(`search_index - ${databaseName}`, async () => {
159
+ const result = await runtime
160
+ .loadModel(modelText)
161
+ .loadQuery(
162
+ `
163
+ query: ga_sessions->search_index -> {
164
+ where: fieldName != null
165
+ project: *
166
+ order_by: fieldName, weight desc
167
+ limit: 10
168
+ }
169
+ `
170
+ )
171
+ .run();
172
+ // console.log(result.data.toObject());
173
+ expect(result.data.path(0, 'fieldName').value).toBe('channelGrouping');
174
+ expect(result.data.path(0, 'fieldValue').value).toBe('Organic Search');
175
+ // expect(result.data.path(0, "weight").value).toBe(18);
176
+ });
177
+
178
+ test.skip(`manual index - ${databaseName}`, async () => {
179
+ let sampleSize = '10';
180
+ if (databaseName === 'bigquery') {
181
+ sampleSize = 'false';
182
+ }
183
+ const _result = await runtime
184
+ .loadQuery(
185
+ `
186
+ query: table('malloytest.ga_sample')-> {
187
+ index: everything
188
+ sample: ${sampleSize}
189
+ }
190
+ -> {
191
+ aggregate: field_count is count(DISTINCT fieldName)
192
+ nest: top_fields is {
193
+ group_by: fieldName
194
+ aggregate: row_count is count()
195
+ limit: 100
196
+ }
197
+ }
198
+ `
199
+ )
200
+ .run();
201
+ // console.log(JSON.stringify(result.data.toObject(), null, 2));
202
+ // expect(result.data.path(0, "fieldName").value).toBe("channelGrouping");
203
+ // expect(result.data.path(0, "fieldValue").value).toBe("Organic Search");
204
+ // expect(result.data.path(0, "weight").value).toBe(18);
205
+ });
206
+
207
+ test(`autobin - ${databaseName}`, async () => {
208
+ const _result = await runtime
209
+ .loadQuery(
210
+ `
211
+ source: airports is table('malloytest.airports') + {
212
+ measure:
213
+ airport_count is count()
214
+ query: by_elevation is {
215
+ aggregate: bin_size is NULLIF((max(elevation) - min(elevation))/30,0)
216
+ nest: data is {
217
+ group_by: elevation
218
+ aggregate: row_count is count()
219
+ }
220
+ }
221
+ -> {
222
+ group_by: elevation is floor(data.elevation/bin_size)*bin_size + bin_size/2
223
+ aggregate: airport_count is data.row_count.sum()
224
+ order_by: elevation
225
+ }
226
+ }
227
+
228
+ query: airports -> {
229
+ group_by: state is state
230
+ aggregate: airport_count
231
+ nest: by_elevation_bar_chart is by_elevation
232
+ }
233
+ `
234
+ )
235
+ .run();
236
+ // console.log(result.sql);
237
+ // console.log(result.data.toObject());
238
+ // expect(result.data.path(0, 'fieldName').value).toBe('channelGrouping');
239
+ // expect(result.data.path(0, 'fieldValue').value).toBe('Organic Search');
240
+ // expect(result.data.path(0, "weight").value).toBe(18);
241
+ });
242
+
243
+ // this really shouldn't be here, we need to be able to test dialect capiblities
244
+ // from the connection.
245
+ test(`safe_cast - ${databaseName}`, async () => {
246
+ const result = await runtime
247
+ .loadQuery(
248
+ `
249
+ source: eone is table('malloytest.airports') {}
250
+
251
+ query: eone -> {
252
+ project:
253
+ bad_date is '123':::date
254
+ bad_number is 'abc':::number
255
+ limit: 1
256
+ }
257
+ `
258
+ )
259
+ .run();
260
+ expect(result.data.value[0]['bad_date']).toBe(null);
261
+ expect(result.data.value[0]['bad_number']).toBe(null);
262
+ });
263
+ }
264
+ );
265
+
266
+ afterAll(async () => {
267
+ await runtimes.closeAll();
268
+ });