@malloydata/malloy-tests 0.0.182 → 0.0.183-dev240916205710

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.
package/package.json CHANGED
@@ -21,13 +21,13 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@jest/globals": "^29.4.3",
24
- "@malloydata/db-bigquery": "^0.0.182",
25
- "@malloydata/db-duckdb": "^0.0.182",
26
- "@malloydata/db-postgres": "^0.0.182",
27
- "@malloydata/db-snowflake": "^0.0.182",
28
- "@malloydata/db-trino": "^0.0.182",
29
- "@malloydata/malloy": "^0.0.182",
30
- "@malloydata/render": "^0.0.182",
24
+ "@malloydata/db-bigquery": "^0.0.183-dev240916205710",
25
+ "@malloydata/db-duckdb": "^0.0.183-dev240916205710",
26
+ "@malloydata/db-postgres": "^0.0.183-dev240916205710",
27
+ "@malloydata/db-snowflake": "^0.0.183-dev240916205710",
28
+ "@malloydata/db-trino": "^0.0.183-dev240916205710",
29
+ "@malloydata/malloy": "^0.0.183-dev240916205710",
30
+ "@malloydata/render": "^0.0.183-dev240916205710",
31
31
  "jsdom": "^22.1.0",
32
32
  "luxon": "^2.4.0",
33
33
  "madge": "^6.0.0"
@@ -36,5 +36,5 @@
36
36
  "@types/jsdom": "^21.1.1",
37
37
  "@types/luxon": "^2.4.0"
38
38
  },
39
- "version": "0.0.182"
39
+ "version": "0.0.183-dev240916205710"
40
40
  }
@@ -1092,13 +1092,16 @@ expressionModels.forEach((x, databaseName) => {
1092
1092
  'works generally',
1093
1093
  async () => {
1094
1094
  await expect(`
1095
- // be accurate within 10%
1095
+ // be accurate within 30%
1096
1096
  // # test.debug
1097
1097
  run: ${databaseName}.table('malloytest.state_facts') -> {
1098
- aggregate: x is round(count_approx(state)/10,0)*10
1099
- aggregate: y is round(count_approx(airport_count)/10,0)*10
1098
+ aggregate: passes is abs(count_approx(state)-count(state))/count(state) < 0.3
1099
+ aggregate: also_passes is abs(count_approx(airport_count)-count(airport_count))/count(airport_count) < 0.3
1100
1100
  }
1101
- `).malloyResultMatches(runtime, {'x': 50, 'y': 50});
1101
+ `).malloyResultMatches(runtime, {
1102
+ 'passes': true,
1103
+ 'also_passes': true,
1104
+ });
1102
1105
  }
1103
1106
  );
1104
1107
  });
@@ -101,6 +101,15 @@ describe.each(allDucks.runtimeList)('duckdb:%s', (dbName, runtime) => {
101
101
  await expect(query).malloyResultMatches(runtime, {n1: 1.234, n2: 1.234});
102
102
  });
103
103
 
104
+ it('dayname', async () => {
105
+ await expect(`
106
+ run: duckdb.sql('select 1') -> {
107
+ select:
108
+ x is dayname(@2024-09-12)
109
+ y is dayname(@2024-09-10 12:22:22)
110
+ }`).malloyResultMatches(runtime, {x: 'Thursday', y: 'Tuesday'});
111
+ });
112
+
104
113
  it('can open json files', async () => {
105
114
  await expect(`
106
115
  run: duckdb.table('test/data/duckdb/test.json') -> {
@@ -0,0 +1,240 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ /* eslint-disable no-console */
9
+
10
+ import {RuntimeList} from '../../runtimes';
11
+ import {describeIfDatabaseAvailable} from '../../util';
12
+ import '../../util/db-jest-matchers';
13
+
14
+ const [describe, databases] = describeIfDatabaseAvailable(['presto', 'trino']);
15
+ const runtimes = new RuntimeList(databases);
16
+
17
+ describe.each(runtimes.runtimeList)(
18
+ 'Presto/Trino dialect functions - %s',
19
+
20
+ (databaseName, runtime) => {
21
+ if (runtime === undefined) {
22
+ throw new Error("Couldn't build runtime");
23
+ }
24
+
25
+ it(`runs an sql query - ${databaseName}`, async () => {
26
+ await expect(
27
+ `run: ${databaseName}.sql("SELECT 1 as n") -> { select: n }`
28
+ ).malloyResultMatches(runtime, {n: 1});
29
+ });
30
+
31
+ it(`runs the to_unixtime function - ${databaseName}`, async () => {
32
+ await expect(`run: ${databaseName}.sql("SELECT 1 as n") -> {
33
+ timezone: 'America/Los_Angeles'
34
+ select: x is to_unixtime(@2024-09-12 04:59:44)
35
+ }`).malloyResultMatches(runtime, {x: 1726142384});
36
+ });
37
+
38
+ it(`runs the arbitrary function - ${databaseName}`, async () => {
39
+ await expect(`run: ${databaseName}.sql("SELECT 1 as n") -> {
40
+ aggregate: x is arbitrary(n)
41
+ }`).malloyResultMatches(runtime, {x: 1});
42
+ });
43
+
44
+ it(`runs the date_format function - ${databaseName}`, async () => {
45
+ await expect(`run: ${databaseName}.sql("SELECT 1 as n") -> {
46
+ select: ts_string is date_format(@2024-09-12 15:42:33, '%Y-%m-%d %H:%i:%S')
47
+ }`).malloyResultMatches(runtime, {ts_string: '2024-09-12 15:42:33'});
48
+ });
49
+
50
+ it(`runs the date_parse function - ${databaseName}`, async () => {
51
+ const expected = Date.parse('15 Sep 2024 00:00:00 UTC');
52
+
53
+ await expect(`run: ${databaseName}.sql("SELECT 1 as n") -> {
54
+ select: x is date_parse('2024-09-15', '%Y-%m-%d')::date
55
+ }`).malloyResultMatches(runtime, {x: expected});
56
+ });
57
+
58
+ it(`runs the regexp_replace function - ${databaseName}`, async () => {
59
+ await expect(`run: ${databaseName}.sql("SELECT 1 as n") -> {
60
+ select:
61
+ remove_matches is regexp_replace('1a 2b 14m', '\\\\d+[ab] ')
62
+ replace_matches is regexp_replace('1a 2b 14m', '(\\\\d+)([ab]) ', '3c$2 ')
63
+ remove_matches_r is regexp_replace('1a 2b 14m', r'\\d+[ab] ')
64
+ replace_matches_r is regexp_replace('1a 2b 14m', r'(\\d+)([ab]) ', '3c$2 ')
65
+ }`).malloyResultMatches(runtime, {
66
+ remove_matches: '14m',
67
+ replace_matches: '3ca 3cb 14m',
68
+ remove_matches_r: '14m',
69
+ replace_matches_r: '3ca 3cb 14m',
70
+ });
71
+ });
72
+
73
+ it(`runs the regexp_like function - ${databaseName}`, async () => {
74
+ await expect(`run: ${databaseName}.sql("SELECT 1 as n") -> {
75
+ select:
76
+ should_match is regexp_like('1a 2b 14m', '\\\\d+b')
77
+ shouldnt_match is regexp_like('1a 2b 14m', '\\\\d+c')
78
+ should_match_r is regexp_like('1a 2b 14m', r'\\d+b')
79
+ shouldnt_match_r is regexp_like('1a 2b 14m', r'\\d+c')
80
+ }`).malloyResultMatches(runtime, {
81
+ should_match: true,
82
+ shouldnt_match: false,
83
+ should_match_r: true,
84
+ shouldnt_match_r: false,
85
+ });
86
+ });
87
+
88
+ it(`runs the approx_percentile function - ${databaseName}`, async () => {
89
+ await expect(`run: ${databaseName}.sql("""
90
+ SELECT 1 as n
91
+ UNION ALL SELECT 50 as n
92
+ UNION ALL SELECT 100 as n
93
+ """) -> {
94
+ aggregate:
95
+ default_pctl is approx_percentile(n, 0.5)
96
+ }`).malloyResultMatches(runtime, {
97
+ default_pctl: 50,
98
+ });
99
+ });
100
+
101
+ it(`runs the bool_and function - ${databaseName}`, async () => {
102
+ await expect(`run: ${databaseName}.sql("""
103
+ SELECT true as n1, false as n2, false as n3
104
+ UNION ALL SELECT true as n1, true as n2, false as n3
105
+ UNION ALL SELECT true as n1, false as n2, false as n3
106
+ """) -> {
107
+ aggregate:
108
+ and_n1 is bool_and(n1)
109
+ and_n2 is bool_and(n2)
110
+ and_n3 is bool_and(n3)
111
+ }`).malloyResultMatches(runtime, {
112
+ and_n1: true,
113
+ and_n2: false,
114
+ and_n3: false,
115
+ });
116
+ });
117
+
118
+ it(`runs the bool_or function - ${databaseName}`, async () => {
119
+ await expect(`run: ${databaseName}.sql("""
120
+ SELECT true as n1, false as n2, false as n3
121
+ UNION ALL SELECT true as n1, true as n2, false as n3
122
+ UNION ALL SELECT true as n1, false as n2, false as n3
123
+ """) -> {
124
+ aggregate:
125
+ or_n1 is bool_or(n1)
126
+ or_n2 is bool_or(n2)
127
+ or_n3 is bool_or(n3)
128
+ }`).malloyResultMatches(runtime, {
129
+ or_n1: true,
130
+ or_n2: true,
131
+ or_n3: false,
132
+ });
133
+ });
134
+
135
+ it(`runs the bitwise_and function - ${databaseName}`, async () => {
136
+ await expect(`run: ${databaseName}.sql("""
137
+ SELECT 13678423 as n1, 23524678 as n2
138
+ """) -> {
139
+ select:
140
+ x is bitwise_and(n1, n2)
141
+ }`).malloyResultMatches(runtime, {x: 4240710});
142
+ });
143
+
144
+ it(`runs the bitwise_or function - ${databaseName}`, async () => {
145
+ await expect(`run: ${databaseName}.sql("""
146
+ SELECT 13678423 as n1, 23524678 as n2
147
+ """) -> {
148
+ select:
149
+ x is bitwise_or(n1, n2)
150
+ }`).malloyResultMatches(runtime, {x: 32962391});
151
+ });
152
+
153
+ it(`runs the variance function - ${databaseName}`, async () => {
154
+ await expect(`run: ${databaseName}.sql("""
155
+ SELECT 1 as n
156
+ UNION ALL SELECT 50 as n
157
+ UNION ALL SELECT 100 as n
158
+ """) -> {
159
+ aggregate:
160
+ var is floor(variance(n))
161
+ }`).malloyResultMatches(runtime, {var: 2450});
162
+ });
163
+
164
+ it(`runs the corr function - ${databaseName}`, async () => {
165
+ await expect(`run: ${databaseName}.sql("""
166
+ SELECT 1 as y, 55 as x
167
+ UNION ALL SELECT 50 as y, 22 as x
168
+ UNION ALL SELECT 100 as y, 1 as x
169
+ """) -> {
170
+ aggregate:
171
+ correlation is corr(y, x)
172
+ }`).malloyResultMatches(runtime, {correlation: -0.9911108});
173
+ });
174
+
175
+ // TODO: once we support Presto JSON types, we should test that situation
176
+ it(`runs the json_extract_scalar function - ${databaseName}`, async () => {
177
+ await expect(`run: ${databaseName}.sql("""
178
+ SELECT 1 as n,
179
+ JSON '{"store": {"book": [ {"title": "Moby Dick", "author": "Herman Melville"} ]}}' as literal_col
180
+ """) -> {
181
+ select:
182
+ json_arr is json_extract_scalar('[1, 2, 3]', '$[2]')
183
+ json_obj is json_extract_scalar(
184
+ '{"store": {"book": [ {"title": "Moby Dick", "author": "Herman Melville"} ]}}',
185
+ '$.store.book[0].author'
186
+ ),
187
+ -- json_literal is json_extract_scalar(literal_col, '$.store.book[0].author')
188
+ }`).malloyResultMatches(runtime, {
189
+ json_arr: '3',
190
+ json_obj: 'Herman Melville',
191
+ // json_literal: 'Herman Melville',
192
+ });
193
+ });
194
+
195
+ it(`runs the bitwise_agg functions - ${databaseName}`, async () => {
196
+ await expect(`run: ${databaseName}.sql("""
197
+ SELECT 13678423 as n1 UNION ALL
198
+ SELECT 23524678 as n1 UNION ALL
199
+ SELECT 987342 as n1
200
+ """) -> {
201
+ aggregate:
202
+ and_agg is bitwise_and_agg(n1)
203
+ or_agg is bitwise_or_agg(n1)
204
+ xor_agg is bitwise_xor_agg(n1)
205
+ }`).malloyResultMatches(runtime, {
206
+ and_agg: 33552351,
207
+ or_agg: 4166,
208
+ xor_agg: 28922591,
209
+ });
210
+ });
211
+
212
+ it(`runs the max_by function - ${databaseName}`, async () => {
213
+ await expect(`run: ${databaseName}.sql("""
214
+ SELECT 1 as y, 55 as x
215
+ UNION ALL SELECT 50 as y, 22 as x
216
+ UNION ALL SELECT 100 as y, 1 as x
217
+ """) -> {
218
+ aggregate:
219
+ m1 is max_by(x, y)
220
+ m2 is max_by(y, x)
221
+ }`).malloyResultMatches(runtime, {m1: 1, m2: 1});
222
+ });
223
+
224
+ it(`runs the min_by function - ${databaseName}`, async () => {
225
+ await expect(`run: ${databaseName}.sql("""
226
+ SELECT 1 as y, 55 as x
227
+ UNION ALL SELECT 50 as y, 22 as x
228
+ UNION ALL SELECT 100 as y, 1 as x
229
+ """) -> {
230
+ aggregate:
231
+ m1 is min_by(x, y)
232
+ m2 is min_by(y, x)
233
+ }`).malloyResultMatches(runtime, {m1: 55, m2: 100});
234
+ });
235
+ }
236
+ );
237
+
238
+ afterAll(async () => {
239
+ await runtimes.closeAll();
240
+ });