mpl-lang 0.5.0

Axioms Metrics Processing Language
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
use std::collections::HashMap;

use crate::query::{Warning, WarningReason, Warnings};
use crate::wasm::diagnostics::{DiagnosticItem, Severity, maybe_rewrite_escaped_dataset_error};
use crate::{CompileError, compile};

fn diagnostic_items(q: &str) -> Vec<DiagnosticItem> {
    match compile(q, HashMap::new()) {
        Ok(_) => vec![],
        Err(CompileError::Parse(error)) => error.diagnostic_items(),
        Err(CompileError::Type(error)) => error.diagnostic_items(),
        Err(CompileError::Group(error)) => error.diagnostic_items(),
        Err(CompileError::Ifdef(error)) => error.diagnostic_items(),
    }
}

/// Run the full success-path pipeline: compile -> warnings -> diagnostic items.
fn warning_items(q: &str) -> Vec<DiagnosticItem> {
    let (_, warnings) = compile(q, HashMap::new()).expect("query should compile");
    warnings
        .as_slice()
        .iter()
        .map(Warning::to_diagnostic_item)
        .collect()
}

// ── code actions / diagnostics ────────────────────────────────────

#[test]
fn map_function_typo_suggests_replacement() {
    // "rte" is close to "rate"
    let query = "ds:metric | map rte";
    let items = diagnostic_items(query);
    assert!(!items.is_empty(), "should produce a diagnostic");
    let item = &items[0];
    assert!(!item.actions.is_empty(), "should have code actions");
    assert_eq!(item.actions[0].insert, "rate");
}

#[test]
fn align_function_typo_suggests_replacement() {
    // "aveg" is close to "avg"
    let query = "ds:metric | align to 1m using aveg";
    let items = diagnostic_items(query);
    assert!(!items.is_empty());
    let item = &items[0];
    assert!(
        item.actions.iter().any(|a| a.insert == "avg"),
        "should suggest avg"
    );
}

#[test]
fn group_function_typo_suggests_replacement() {
    // "summ" is close to "sum"
    let query = "ds:metric | group using summ";
    let items = diagnostic_items(query);
    assert!(!items.is_empty());
    let item = &items[0];
    assert!(
        item.actions.iter().any(|a| a.insert == "sum"),
        "should suggest sum"
    );
}

#[test]
fn no_suggestion_for_unrelated_name() {
    // "zzzzz" has no similarity to any stdlib function
    let query = "ds:metric | map zzzzz";
    let items = diagnostic_items(query);
    assert!(!items.is_empty(), "should produce a diagnostic");
    let item = &items[0];
    assert!(
        item.actions.is_empty(),
        "should not suggest for unrelated names"
    );
}

#[test]
fn action_targets_function_name_range() {
    // The action's from/to should cover just the function name
    let query = "ds:metric | map rte";
    let items = diagnostic_items(query);
    let item = &items[0];
    let action = &item.actions[0];
    assert_eq!(&query[action.span.from..action.span.to], "rte");
}

#[test]
fn type_error_puts_error_on_use_and_info_on_declaration() {
    // $tag is declared as string but used where duration is expected
    let query = "param $tag: string;\nds:metric | align to $tag using avg";
    let items = match compile(query, HashMap::new()) {
        Ok(_) => panic!("should produce a type error"),
        Err(CompileError::Parse(_)) => panic!("should be a type error, not parse error"),
        Err(CompileError::Type(error)) => error.diagnostic_items(),
        Err(CompileError::Group(error)) => error.diagnostic_items(),
        Err(CompileError::Ifdef(_)) => panic!("should be a type error, not ifdef error"),
    };

    assert_eq!(items.len(), 2, "should produce two diagnostics");

    // The error should be on the usage site ($tag in align)
    let error_item = items
        .iter()
        .find(|i| matches!(i.severity, Severity::Error))
        .expect("should have an error diagnostic");
    assert_eq!(
        &query[error_item.span.from..error_item.span.to],
        "$tag",
        "error should point at the usage of $tag"
    );

    // The info should be on the declaration site
    let info_item = items
        .iter()
        .find(|i| matches!(i.severity, Severity::Info))
        .expect("should have an info diagnostic");
    assert!(
        info_item.message.contains("declaration"),
        "info message should mention declaration"
    );
}

#[test]
fn optional_param_outside_ifdef_is_error() {
    let query = "param $f: Option<string>;\nds:metric | where tag == $f";
    let items = match compile(query, HashMap::new()) {
        Ok(_) => panic!("optional usage outside ifdef should not compile"),
        Err(CompileError::Ifdef(error)) => error.diagnostic_items(),
        Err(other) => panic!("expected ifdef error, got: {other}"),
    };

    assert_eq!(items.len(), 1, "should produce exactly one diagnostic");
    let item = &items[0];
    assert!(matches!(item.severity, Severity::Error));
    assert_eq!(
        &query[item.span.from..item.span.to],
        "$f",
        "error should point at the use site"
    );
    assert!(
        item.message.contains('f') && item.message.contains("ifdef"),
        "message should mention the param and ifdef, got: {:?}",
        item.message
    );
}

#[test]
fn ifdef_body_does_not_reference_param_is_error() {
    // The gating param `$f` is never referenced inside the ifdef body — that
    // means the ifdef is structurally pointless. The visitor catches this on
    // leave_ifdef.
    let query = "param $f: Option<string>;\nds:metric | ifdef($f) { where tag == \"x\" }";
    let items = match compile(query, HashMap::new()) {
        Ok(_) => panic!("ifdef body without param reference should not compile"),
        Err(CompileError::Ifdef(error)) => error.diagnostic_items(),
        Err(other) => panic!("expected ifdef error, got: {other}"),
    };

    assert_eq!(items.len(), 1);
    assert!(matches!(items[0].severity, Severity::Error));
    assert!(
        items[0].message.contains("not referenced"),
        "message should describe the missing reference, got: {:?}",
        items[0].message
    );
}

#[test]
fn ifdef_body_referencing_param_compiles() {
    // Sanity: an ifdef whose body DOES reference the gating param compiles.
    let query = "param $f: Option<string>;\nds:metric | ifdef($f) { where tag == $f }";
    assert!(
        compile(query, HashMap::new()).is_ok(),
        "ifdef body referencing the gating param should compile"
    );
}

#[test]
fn optional_regex_param_outside_ifdef_is_error() {
    // Triggers OptionCheckVisitor::visit_parameterized_regex (the second emit
    // site of IfdefError::OptionalOutsideOfIfdef), distinct from the value path.
    let query = "param $r: Option<Regex>;\nds:metric | where tag == $r";
    let items = match compile(query, HashMap::new()) {
        Ok(_) => panic!("optional regex usage outside ifdef should not compile"),
        Err(CompileError::Ifdef(error)) => error.diagnostic_items(),
        Err(other) => panic!("expected ifdef error, got: {other}"),
    };

    assert_eq!(items.len(), 1);
    assert!(matches!(items[0].severity, Severity::Error));
    assert_eq!(
        &query[items[0].span.from..items[0].span.to],
        "$r",
        "error should point at the regex use site"
    );
}

#[test]
fn optional_param_in_other_ifdef_is_error() {
    // $b is gated by ifdef($a), but referenced through ifdef($b)'s gate —
    // the visitor only allows the *same* optional param inside the ifdef.
    let query = concat!(
        "param $a: Option<string>;\n",
        "param $b: Option<string>;\n",
        "ds:metric | ifdef($a) { where tag == $b }",
    );
    let err = match compile(query, HashMap::new()) {
        Ok(_) => panic!("cross-ifdef optional should not compile"),
        Err(CompileError::Ifdef(error)) => error,
        Err(other) => panic!("expected ifdef error, got: {other}"),
    };
    let items = err.diagnostic_items();
    assert_eq!(items.len(), 1);
    assert!(matches!(items[0].severity, Severity::Error));
    assert_eq!(
        &query[items[0].span.from..items[0].span.to],
        "$b",
        "error should point at the wrongly-gated param"
    );
}

#[test]
fn compute_function_typo_suggests_replacement() {
    // "minn" is close to "min"
    let query = "( ds1:m1 , ds2:m2 ) | compute result using minn";
    let items = diagnostic_items(query);
    assert!(!items.is_empty(), "should produce a diagnostic");
    let item = &items[0];
    assert!(
        item.actions.iter().any(|a| a.insert == "min"),
        "should suggest min, got actions: {:?}",
        item.actions.iter().map(|a| &a.insert).collect::<Vec<_>>()
    );
}

// ── parser-emitted warnings (OldDuration) ──────────────────────────

#[test]
fn old_duration_warning_emitted_as_diagnostic() {
    let query = "param $t: duration;\nds:metric | align to $t using avg";
    let items = warning_items(query);
    assert_eq!(items.len(), 1, "expected exactly one OldDuration warning");
    let item = &items[0];
    assert!(matches!(item.severity, Severity::Warning));
    assert!(
        item.message.contains("`duration`") && item.message.contains("Duration"),
        "warning message should mention both forms, got: {:?}",
        item.message
    );
    assert_eq!(
        &query[item.span.from..item.span.to],
        "duration",
        "warning span should cover the lowercase `duration` token"
    );
}

#[test]
fn old_duration_warning_has_replace_action() {
    let query = "param $t: duration;\nds:metric | align to $t using avg";
    let items = warning_items(query);
    let item = &items[0];
    assert_eq!(
        item.actions.len(),
        1,
        "OldDuration should have one quick-fix"
    );
    let action = &item.actions[0];
    assert_eq!(action.insert, "Duration");
    // The action's span must cover the exact `duration` token so applying it
    // is a straight substring replacement, not an offset shift.
    assert_eq!(&query[action.span.from..action.span.to], "duration");
    assert!(
        action.name.contains("Duration"),
        "action label should mention Duration, got: {:?}",
        action.name
    );
}

#[test]
fn uppercase_duration_emits_no_warning() {
    let query = "param $t: Duration;\nds:metric | align to $t using avg";
    let items = warning_items(query);
    assert!(items.is_empty(), "canonical `Duration` must not warn");
}

#[test]
fn param_not_declared_warning_is_plain_warning_without_actions() {
    // `ParamNotDeclared` is emitted from the runtime-param parsing path, not
    // from `compile`. We still translate it through the same conversion to
    // keep diagnostic surfaces uniform: severity=Warning, no quick-fix.
    let mut warnings = Warnings::new();
    warnings.push(WarningReason::ParamNotDeclared(vec!["$foo".to_string()]));
    let items: Vec<DiagnosticItem> = warnings
        .as_slice()
        .iter()
        .map(Warning::to_diagnostic_item)
        .collect();
    assert_eq!(items.len(), 1);
    assert!(matches!(items[0].severity, Severity::Warning));
    assert!(items[0].actions.is_empty());
    assert!(items[0].message.contains("$foo"));
}

#[test]
fn multiple_old_duration_warnings() {
    // Each occurrence of `duration` produces its own warning so the editor
    // can pin a quick-fix to every site, not just the first.
    let query = concat!(
        "param $t: duration;\n",
        "param $u: duration;\n",
        "ds:metric | align to $t using avg",
    );
    let items = warning_items(query);
    assert_eq!(items.len(), 2, "one warning per `duration` token");
    for item in &items {
        assert!(matches!(item.severity, Severity::Warning));
        assert_eq!(&query[item.span.from..item.span.to], "duration");
        assert_eq!(item.actions[0].insert, "Duration");
    }
}

// ── dataset given, no metric ─────────────────────────────────────

fn assert_parse_error(query: &str, expected_from: usize, expected_to: usize) {
    let items = match compile(query, HashMap::new()) {
        Ok(_) => panic!("'{query}' should not compile"),
        Err(CompileError::Parse(error)) => error.diagnostic_items(),
        Err(CompileError::Type(_) | CompileError::Group(_) | CompileError::Ifdef(_)) => {
            panic!("'{query}' should be a parse error, not type/group/ifdef error")
        }
    };
    assert_eq!(
        items.len(),
        1,
        "'{query}' should produce exactly one diagnostic"
    );
    assert!(
        matches!(items[0].severity, Severity::Error),
        "'{query}' should produce an error"
    );
    assert!(
        items[0].actions.is_empty(),
        "'{query}' should have no code actions"
    );
    assert_eq!(
        items[0].span.from, expected_from,
        "'{query}' error span.from"
    );
    assert_eq!(items[0].span.to, expected_to, "'{query}' error span.to");
}

#[test]
fn dataset_colon_no_metric_error_at_eof() {
    // "ds:" — error points at EOF (from=3, to=3)
    let query = "ds:";
    assert_parse_error(query, query.len(), query.len());
}

#[test]
fn backtick_dataset_colon_no_metric_error_at_eof() {
    // "`my-dataset`:" — error points at EOF
    let query = "`my-dataset`:";
    assert_parse_error(query, query.len(), query.len());
}

#[test]
fn dataset_no_colon_error_highlights_dataset() {
    // "ds" — error highlights "ds" as an unexpected token
    assert_parse_error("ds", 0, 2);
}

#[test]
fn dataset_no_metric_with_filter_error_at_pipe() {
    // "ds: | filter tag == \"x\"" — error highlights the "|"
    let query = "ds: | filter tag == \"x\"";
    assert_parse_error(query, 4, 5);
}

#[test]
fn dataset_no_colon_with_filter_error_highlights_dataset() {
    // "ds | filter tag == \"x\"" — error highlights "ds"
    assert_parse_error("ds | filter tag == \"x\"", 0, 2);
}

#[test]
fn backtick_dataset_no_metric_with_where_error_at_pipe() {
    // "`my-dataset`: | where tag == \"x\"" — error highlights the "|"
    let query = "`my-dataset`: | where tag == \"x\"";
    assert_parse_error(query, 14, 15);
}

#[test]
fn dataset_no_metric_with_time_range_error_at_bracket() {
    // "ds:[1h..]" — error highlights the "["
    assert_parse_error("ds:[1h..]", 3, 4);
}

// ── escaped ident dataset with dot, no colon ────────────────────

/// Runs `compile` → `diagnostic_items` → `maybe_rewrite` (the full wasm path).
fn diagnostics_for(query: &str) -> Vec<DiagnosticItem> {
    match compile(query, HashMap::new()) {
        Ok(_) => panic!("'{query}' should not compile"),
        Err(CompileError::Parse(error)) => {
            maybe_rewrite_escaped_dataset_error(query, error.diagnostic_items())
        }
        Err(CompileError::Type(_) | CompileError::Group(_) | CompileError::Ifdef(_)) => {
            panic!("'{query}' should be a parse error, not type/group/ifdef error")
        }
    }
}

#[test]
fn backtick_dotted_dataset_no_colon_error_at_end_with_message() {
    let query = "`dev.metrics`";
    let items = diagnostics_for(query);
    assert_eq!(items.len(), 1);
    assert!(matches!(items[0].severity, Severity::Error));
    assert_eq!(items[0].span.from, query.len(), "error should be at EOF");
    assert_eq!(items[0].span.to, query.len(), "error should be at EOF");
    assert!(
        items[0].message.contains("metric name"),
        "message should mention metric name, got: '{}'",
        items[0].message
    );
}

#[test]
fn backtick_dotted_dataset_suggests_colon_syntax() {
    let query = "`dev.metrics`";
    let items = diagnostics_for(query);
    assert!(
        items[0]
            .help
            .as_ref()
            .is_some_and(|h: &String| h.contains(':')),
        "help should mention ':' syntax, got: {:?}",
        items[0].help
    );
}

#[test]
fn backtick_dataset_no_dot_not_rewritten() {
    // No dot in the ident — rewrite should NOT fire, keep original behavior
    let query = "`my-dataset`";
    let items = diagnostics_for(query);
    assert_eq!(items.len(), 1);
    // Original error stays at position 0 (not rewritten)
    assert_eq!(items[0].span.from, 0);
}

#[test]
fn backtick_dataset_with_colon_not_rewritten() {
    // Has colon after ident — should NOT be rewritten
    let query = "`dev.metrics`:";
    let items = diagnostics_for(query);
    assert_eq!(items.len(), 1);
    // Original error, not our rewrite (error is at EOF for missing metric name)
    assert_ne!(
        items[0].message, "expected ':' and a metric name after the dataset",
        "should not rewrite when colon is present"
    );
}