prqlc 0.13.11

PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Test error messages. As of 2023-03, this can hopefully be expanded significantly.
//! It's also fine to put errors by the things that they're testing.
//! See also [test_bad_error_messages.rs](test_bad_error_messages.rs) for error
//! messages which need to be improved.
use insta::assert_snapshot;

use super::sql::compile;

#[test]
fn test_errors() {
    assert_snapshot!(compile(r###"
    let addadd = a b -> a + b

    from x
    derive y = (addadd 4 5 6)
    "###).unwrap_err(),
        @r"
    Error:
       ╭─[ :5:17 ]
       │
     5 │     derive y = (addadd 4 5 6)
       │                 ──────┬─────
       │                       ╰─────── Too many arguments to function `addadd`
    ───╯
    ");

    assert_snapshot!(compile(r###"
    from a select b
    "###).unwrap_err(),
        @r"
    Error:
       ╭─[ :2:5 ]
       │
     2 │     from a select b
       │     ───────┬───────
       │            ╰───────── Too many arguments to function `from`
    ───╯
    ");

    assert_snapshot!(compile(r###"
    from x
    select a
    select b
    "###).unwrap_err(),
        @r"
    Error:
       ╭─[ :4:12 ]
       │
     4 │     select b
       │            ┬
       │            ╰── Unknown name `b`
       │
       │ Help: available columns: x.a
    ───╯
    ");

    assert_snapshot!(compile(r###"
    from employees
    take 1.8
    "###).unwrap_err(),
        @r"
    Error:
       ╭─[ :3:10 ]
       │
     3 │     take 1.8
       │          ─┬─
       │           ╰─── `take` expected int or range, but found 1.8
    ───╯
    ");

    // LEXER output for Mississippi test (curly quotes are U+2019):
    use insta::assert_debug_snapshot;
    let mississippi = "Mississippi has four S’s and four I’s.";
    assert_debug_snapshot!(prqlc_parser::lexer::lex_source(mississippi).unwrap_err(), @r#"
    [
        Error {
            kind: Error,
            span: Some(
                0:22-23,
            ),
            reason: Unexpected {
                found: "'’'",
            },
            hints: [],
            code: None,
        },
    ]
    "#);

    // PARSER output (full compilation error):
    assert_snapshot!(compile(mississippi).unwrap_err(), @r"
    Error:
       ╭─[ :1:23 ]
       │
     1 │ Mississippi has four S’s and four I’s.
       │                       ┬
       │                       ╰── unexpected '’'
    ───╯
    ");

    assert_snapshot!(compile("Answer: T-H-A-T!").unwrap_err(), @r"
    Error:
       ╭─[ :1:16 ]
       │
     1 │ Answer: T-H-A-T!
       │                ┬
       │                ╰── expected something else, but found !
    ───╯
    ");
}

#[test]
fn test_union_all_sqlite() {
    // TODO: `SQLiteDialect` would be better as `sql.sqlite` or `sqlite`.
    assert_snapshot!(compile(r###"
    prql target:sql.sqlite

    from film
    remove film2
    "###).unwrap_err(), @r"
    Error: The dialect SQLiteDialect does not support EXCEPT ALL
    ↳ Hint: providing more column information will allow the query to be translated to an anti-join.
    ")
}

#[test]
fn test_regex_dialect() {
    assert_snapshot!(compile(r###"
    prql target:sql.mssql
    from foo
    filter bar ~= 'love'
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :4:12 ]
       │
     4 │     filter bar ~= 'love'
       │            ──────┬──────
       │                  ╰──────── operator std.regex_search is not supported for dialect mssql
    ───╯
    ")
}

#[test]
fn test_bad_function_type() {
    assert_snapshot!(compile(r###"
    from tracks
    group foo (take)
    "###,
    )
    .unwrap_err(), @r"
    Error:
       ╭─[ :3:16 ]
       │
     3 │     group foo (take)
       │                ──┬─
       │                  ╰─── function std.group, param `pipeline` expected type `transform`, but found type `func ? relation -> relation`
       │
       │ Help: Type `transform` expands to `func relation -> relation`
    ───╯
    ");
}

#[test]
#[ignore]
// FIXME: This would be nice to catch those errors again
// See https://github.com/PRQL/prql/issues/3127#issuecomment-1849032396
fn test_basic_type_checking() {
    assert_snapshot!(compile(r#"
    from foo
    select (a && b) + c
    "#)
    .unwrap_err(), @r###"
    Error:
       ╭─[:3:13]
       │
     3 │     select (a && b) + c
       │             ───┬──
       │                ╰──── function std.add, param `left` expected type `int || float || timestamp || date`, but found type `bool`
    ───╯
    "###);
}

#[test]
fn test_ambiguous() {
    assert_snapshot!(compile(r#"
    from a
    derive date = x
    select date
    "#)
    .unwrap_err(), @r"
    Error:
       ╭─[ :4:12 ]
       │
     4 │     select date
       │            ──┬─
       │              ╰─── Ambiguous name
       │
       │ Help: could be any of: std.date, this.date
       │
       │ Note: available columns: date
    ───╯
    ");
}

#[test]
fn test_ambiguous_join() {
    assert_snapshot!(compile(r#"
    from a
    select x
    join (from b | select {x}) true
    select x
    "#)
    .unwrap_err(), @r"
    Error:
       ╭─[ :5:12 ]
       │
     5 │     select x
       │            ┬
       │            ╰── Ambiguous name
       │
       │ Help: could be any of: a.x, b.x
       │
       │ Note: available columns: a.x, b.x
    ───╯
    ");
}

#[test]
fn test_ambiguous_inference() {
    assert_snapshot!(compile(r#"
    from a
    join b(==b_id)
    select x
    "#)
    .unwrap_err(), @r"
    Error:
       ╭─[ :4:12 ]
       │
     4 │     select x
       │            ┬
       │            ╰── Ambiguous name
       │
       │ Help: could be any of: a.x, b.x
    ───╯
    ");
}

#[test]
fn date_to_text_generic() {
    assert_snapshot!(compile(r#"
  [{d = @2021-01-01}]
  derive {
    d_str = (d | date.to_text "%Y/%m/%d")
  }"#).unwrap_err(), @r#"
    Error:
       ╭─[ :4:31 ]
       │
     4 │     d_str = (d | date.to_text "%Y/%m/%d")
       │                               ─────┬────
       │                                    ╰────── Date formatting requires a dialect
    ───╯
    "#);
}

#[test]
fn date_to_text_with_column_format() {
    assert_snapshot!(compile(r#"
  from dates_to_display
  select {my_date, my_format}
  select {std.date.to_text my_date my_format}
  "#).unwrap_err(), @r"
    Error:
       ╭─[ :4:11 ]
       │
     4 │   select {std.date.to_text my_date my_format}
       │           ─────────────────┬────────────────
       │                            ╰────────────────── `std.date.to_text` only supports a string literal as format
    ───╯
    ");
}

#[test]
fn date_to_text_unsupported_chrono_item() {
    assert_snapshot!(compile(r#"
    prql target:sql.duckdb

    from [{d = @2021-01-01}]
    derive {
      d_str = (d | date.to_text "%_j")
    }"#).unwrap_err(), @r#"
    Error:
       ╭─[ :6:33 ]
       │
     6 │       d_str = (d | date.to_text "%_j")
       │                                 ──┬──
       │                                   ╰──── PRQL doesn't support this format specifier
    ───╯
    "#);
}

#[test]
fn available_columns() {
    assert_snapshot!(compile(r#"
    from invoices
    select foo
    select bar
    "#).unwrap_err(), @r"
    Error:
       ╭─[ :4:12 ]
       │
     4 │     select bar
       │            ─┬─
       │             ╰─── Unknown name `bar`
       │
       │ Help: available columns: invoices.foo
    ───╯
    ");
}

#[test]
fn empty_interpolations() {
    assert_snapshot!(compile(r#"from x | select f"{}" "#).unwrap_err(), @r#"
    Error:
       ╭─[ :1:20 ]
       │
     1 │ from x | select f"{}"
       │                    ┬
       │                    ╰── expected interpolated string variable or '{', but found "}"
    ───╯
    "#);
}

#[test]
fn no_query_entered() {
    // Empty query
    assert_snapshot!(compile("").unwrap_err(), @r"
    [E0001] Error: No PRQL query entered
    ");

    // Comment-only query
    assert_snapshot!(compile("# just a comment").unwrap_err(), @r"
    [E0001] Error: No PRQL query entered
    ");
}

#[test]
fn query_must_begin_with_from() {
    // Query with declaration but no 'from'
    assert_snapshot!(compile("let x = 5").unwrap_err(), @r"
    [E0001] Error: PRQL queries must begin with 'from'
    ↳ Hint: A query must start with a 'from' statement to define the main pipeline
    ");

    // Query with multiple declarations but no pipeline
    assert_snapshot!(compile(r#"
    let x = 5
    let y = 10
    "#).unwrap_err(), @r"
    [E0001] Error: PRQL queries must begin with 'from'
    ↳ Hint: A query must start with a 'from' statement to define the main pipeline
    ");
}

#[test]
fn negative_number_in_transform() {
    // Negative numbers need to be wrapped in parentheses when used in transforms
    assert_snapshot!(compile(r###"
    from artists
    sort -name
    "###).unwrap_err(), @r"
    Error: expected a pipeline that resolves to a table, but found `internal std.sub`
    ↳ Hint: wrap negative numbers in parentheses, e.g. `sort (-column_name)`
    ");

    assert_snapshot!(compile(r###"
    from pets
    take -10
    "###).unwrap_err(), @r"
    Error: expected a pipeline that resolves to a table, but found `internal std.sub`
    ↳ Hint: wrap negative numbers in parentheses, e.g. `sort (-column_name)`
    ");

    assert_snapshot!(compile(r###"
  from tbl
  group id (
    sort -val
  )
  "###).unwrap_err(), @r"
    Error: expected a pipeline that resolves to a table, but found `internal std.sub`
    ↳ Hint: wrap negative numbers in parentheses, e.g. `sort (-column_name)`
    ");
}

#[test]
fn empty_tuple_or_array_from() {
    assert_snapshot!(compile(r###"
    from {}
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :2:10 ]
       │
     2 │     from {}
       │          ─┬
       │           ╰── expected a table or query, but found an empty tuple `{}`
    ───╯
    ");

    assert_snapshot!(compile(r###"
    from []
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :2:10 ]
       │
     2 │     from []
       │          ─┬
       │           ╰── expected a table or query, but found an empty array `[]`
    ───╯
    ");

    assert_snapshot!(compile(r###"
    from {}
    select a
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :2:10 ]
       │
     2 │     from {}
       │          ─┬
       │           ╰── expected a table or query, but found an empty tuple `{}`
    ───╯
    ");
}

#[test]
fn window_rows_expects_range() {
    // Issue #5601: window with invalid rows parameter should produce error, not panic
    assert_snapshot!(compile(r###"
    from t
    group sid (window rows:2 (sid))
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :3:28 ]
       │
     3 │     group sid (window rows:2 (sid))
       │                            ┬
       │                            ╰── parameter `rows` expected a range, but found 2
    ───╯
    ");

    assert_snapshot!(compile(r###"
    from t
    group sid (window range:2 (sid))
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :3:29 ]
       │
     3 │     group sid (window range:2 (sid))
       │                             ┬
       │                             ╰── parameter `range` expected a range, but found 2
    ───╯
    ");
}

#[test]
fn bare_lambda_expression() {
    // Issue #4280: A bare lambda expression like `x -> y` should produce
    // a clear error, not a confusing internal message.
    assert_snapshot!(compile(r###"
    x -> y
    "###).unwrap_err(), @r"
    Error:
       ╭─[ :2:5 ]
       │
     2 │     x -> y
       │     ───┬──
       │        ╰──── expected a table, but found a function
    ───╯
    ");
}