forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_runner.rs
488 lines (463 loc) · 19.2 KB
/
test_runner.rs
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
480
481
482
483
484
485
486
487
488
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
/// This module defines a small language for directly constructing RelationExprs and running
/// various optimizations on them. It uses datadriven, so the output of each test can be rewritten
/// by setting the REWRITE environment variable.
/// TODO(justin):
/// * It's currently missing a mechanism to run just a single test file
/// * There is some duplication between this and the SQL planner
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::fmt::Write;
use anyhow::{anyhow, Error};
use mz_expr::{GlobalId, Id, MirRelationExpr};
use mz_expr_test_util::{
build_rel, generate_explanation, json_to_spec, MirRelationExprDeserializeContext,
TestCatalog, RTI,
};
use mz_lowertest::{deserialize, tokenize};
use mz_ore::str::separated;
use mz_transform::dataflow::{optimize_dataflow_demand_inner, optimize_dataflow_filters_inner};
use mz_transform::{Optimizer, Transform, TransformArgs};
use proc_macro2::TokenTree;
// Global options
const IN: &str = "in";
const FORMAT: &str = "format";
// Values that can be supplied for global options
const JSON: &str = "json";
const TEST: &str = "test";
thread_local! {
static FULL_TRANSFORM_LIST: Vec<Box<dyn Transform>> =
Optimizer::logical_optimizer()
.transforms
.into_iter()
.chain(std::iter::once(
Box::new(mz_transform::projection_pushdown::ProjectionPushdown)
as Box<dyn Transform>,
))
.chain(std::iter::once(
Box::new(mz_transform::update_let::UpdateLet::default()) as Box<dyn Transform>
))
.chain(Optimizer::logical_cleanup_pass().transforms.into_iter())
.chain(Optimizer::physical_optimizer().transforms.into_iter())
.collect::<Vec<_>>();
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum FormatType<'a> {
Explain(Option<&'a Vec<String>>),
Json,
Test,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum TestType {
Build,
Opt,
Steps,
}
/// Parses the output format from `args[format]`.
fn get_format_type<'a>(args: &'a HashMap<String, Vec<String>>) -> FormatType<'a> {
if let Some(format) = args.get(FORMAT) {
if format.iter().any(|s| s == TEST) {
FormatType::Test
} else if format.iter().any(|s| s == JSON) {
FormatType::Json
} else {
FormatType::Explain(args.get(FORMAT))
}
} else {
FormatType::Explain(args.get(FORMAT))
}
}
// Converts string to MirRelationExpr. `args[in]` specifies which input
// format is being used.
fn parse_relation(
s: &str,
cat: &TestCatalog,
args: &HashMap<String, Vec<String>>,
) -> Result<MirRelationExpr, Error> {
if let Some(input_format) = args.get(IN) {
if input_format.iter().any(|s| s == JSON) {
return serde_json::from_str::<MirRelationExpr>(s).map_err(|e| anyhow!(e));
}
}
build_rel(s, cat).map_err(|e| anyhow!(e))
}
/// Converts MirRelationExpr to `format_type`.
fn convert_rel_to_string(
rel: &MirRelationExpr,
cat: &TestCatalog,
format_type: &FormatType,
) -> String {
match format_type {
FormatType::Test => format!(
"{}\n",
json_to_spec(&serde_json::to_string(rel).unwrap(), cat).0
),
FormatType::Json => format!("{}\n", serde_json::to_string(rel).unwrap()),
FormatType::Explain(format) => generate_explanation(cat, rel, *format),
}
}
fn run_single_view_testcase(
s: &str,
cat: &TestCatalog,
args: &HashMap<String, Vec<String>>,
test_type: TestType,
) -> Result<String, Error> {
let mut rel = parse_relation(s, cat, args)?;
let mut id_gen = Default::default();
let indexes = HashMap::new();
for t in args.get("apply").cloned().unwrap_or_else(Vec::new).iter() {
get_transform(t)?.transform(
&mut rel,
TransformArgs {
id_gen: &mut id_gen,
indexes: &indexes,
},
)?;
}
let format_type = get_format_type(args);
let out = match test_type {
TestType::Opt => FULL_TRANSFORM_LIST.with(|transforms| -> Result<_, Error> {
for transform in transforms.iter() {
transform.transform(
&mut rel,
TransformArgs {
id_gen: &mut id_gen,
indexes: &indexes,
},
)?;
}
Ok(convert_rel_to_string(&rel, &cat, &format_type))
})?,
TestType::Build => convert_rel_to_string(&rel, &cat, &format_type),
TestType::Steps => {
// TODO(justin): this thing does not currently peek into fixpoints, so it's not
// that helpful for optimizations that involve those (which is most of them).
let mut out = String::new();
// Buffer of the names of the transformations that have been applied with no changes.
let mut no_change: Vec<String> = Vec::new();
writeln!(out, "{}", convert_rel_to_string(&rel, &cat, &format_type))?;
writeln!(out, "====")?;
FULL_TRANSFORM_LIST.with(|transforms| -> Result<_, Error> {
for transform in transforms {
let prev = rel.clone();
transform.transform(
&mut rel,
TransformArgs {
id_gen: &mut id_gen,
indexes: &indexes,
},
)?;
if rel != prev {
if no_change.len() > 0 {
write!(out, "No change:")?;
let mut sep = " ";
for t in no_change.iter() {
write!(out, "{}{}", sep, t)?;
sep = ", ";
}
writeln!(out, "\n====")?;
}
no_change = vec![];
write!(out, "Applied {:?}:", transform)?;
writeln!(out, "\n{}", convert_rel_to_string(&rel, &cat, &format_type))?;
writeln!(out, "====")?;
} else {
no_change.push(format!("{:?}", transform));
}
}
Ok(())
})?;
if no_change.len() > 0 {
write!(out, "No change:")?;
let mut sep = " ";
for t in no_change {
write!(out, "{}{}", sep, t)?;
sep = ", ";
}
writeln!(out, "\n====")?;
}
writeln!(out, "Final:")?;
writeln!(out, "{}", convert_rel_to_string(&rel, &cat, &format_type))?;
writeln!(out, "====")?;
out
}
};
if let FormatType::Test = format_type {
let source_defs = json_to_spec(&serde_json::to_string(&rel).unwrap(), cat).1;
if !source_defs.is_empty() {
return Ok(format!(
"{}====\nCatalog defs:\n{}\n",
out,
separated("\n", source_defs)
));
}
}
Ok(out)
}
fn get_transform(name: &str) -> Result<Box<dyn Transform>, Error> {
// TODO(justin): is there a way to just extract these from the Optimizer list of
// transforms?
match name {
"CanonicalizeMfp" => Ok(Box::new(mz_transform::canonicalize_mfp::CanonicalizeMfp)),
"ColumnKnowledge" => Ok(Box::new(
mz_transform::column_knowledge::ColumnKnowledge::default(),
)),
"Demand" => Ok(Box::new(mz_transform::demand::Demand::default())),
"FilterFusion" => Ok(Box::new(mz_transform::fusion::filter::Filter)),
"FoldConstants" => Ok(Box::new(mz_transform::reduction::FoldConstants {
limit: None,
})),
"JoinFusion" => Ok(Box::new(mz_transform::fusion::join::Join)),
"LiteralLifting" => Ok(Box::new(
mz_transform::map_lifting::LiteralLifting::default(),
)),
"NonNullRequirements" => Ok(Box::new(
mz_transform::nonnull_requirements::NonNullRequirements::default(),
)),
"PredicatePushdown" => Ok(Box::new(
mz_transform::predicate_pushdown::PredicatePushdown::default(),
)),
"ProjectionExtraction" => Ok(Box::new(
mz_transform::projection_extraction::ProjectionExtraction,
)),
"ProjectionLifting" => Ok(Box::new(
mz_transform::projection_lifting::ProjectionLifting::default(),
)),
"ProjectionPushdown" => Ok(Box::new(
mz_transform::projection_pushdown::ProjectionPushdown,
)),
"ReductionPushdown" => Ok(Box::new(
mz_transform::reduction_pushdown::ReductionPushdown,
)),
"RedundantJoin" => Ok(Box::new(
mz_transform::redundant_join::RedundantJoin::default(),
)),
"TopKFusion" => Ok(Box::new(mz_transform::fusion::top_k::TopK)),
"UnionBranchCancellation" => Ok(Box::new(
mz_transform::union_cancel::UnionBranchCancellation,
)),
"UnionFusion" => Ok(Box::new(mz_transform::fusion::union::Union)),
_ => Err(anyhow!(
"no transform named {} (you might have to add it to get_transform)",
name
)),
}
}
// TODO: have multiview test case accept the "in" argument
fn run_multiview_testcase(
s: &str,
cat: &mut TestCatalog,
args: &HashMap<String, Vec<String>>,
test_type: TestType,
) -> Result<String, String> {
let mut input_stream = tokenize(&s)?.into_iter();
let mut dataflow = Vec::new();
while let Some(token) = input_stream.next() {
match token {
TokenTree::Group(group) => {
let mut inner_iter = group.stream().into_iter();
let name = match inner_iter.next() {
Some(TokenTree::Ident(ident)) => ident.to_string(),
other => {
return Err(format!("Could not parse {:?} as view name", other));
}
};
let rel: MirRelationExpr = deserialize(
&mut inner_iter,
"MirRelationExpr",
&RTI,
&mut MirRelationExprDeserializeContext::new(cat),
)?;
let id = cat.insert(&name, rel.typ(), true)?;
dataflow.push((id, rel));
}
other => return Err(format!("Could not parse {:?} as view", other)),
}
}
if dataflow.is_empty() {
return Err("Empty dataflow".to_string());
}
let mut out = String::new();
if test_type == TestType::Opt {
let mut optimizer = Optimizer::logical_optimizer();
dataflow = dataflow
.into_iter()
.map(|(id, rel)| (id, optimizer.optimize(rel).unwrap().into_inner()))
.collect();
}
match test_type {
TestType::Build => {
for t in args.get("apply").cloned().unwrap_or_else(Vec::new).iter() {
out.push_str(&apply_cross_view_transform(t, &mut dataflow, cat)?[..]);
}
}
TestType::Opt => {
for t in ["filter", "project"] {
out.push_str(&apply_cross_view_transform(t, &mut dataflow, cat)?[..]);
}
}
_ => {}
};
if test_type == TestType::Opt {
let mut log_optimizer = Optimizer::logical_cleanup_pass();
let mut phys_optimizer = Optimizer::physical_optimizer();
dataflow = dataflow
.into_iter()
.map(|(id, rel)| {
(
id,
phys_optimizer
.optimize(log_optimizer.optimize(rel).unwrap().into_inner())
.unwrap()
.into_inner(),
)
})
.collect();
}
let format_type = get_format_type(args);
out = format!(
"{}\n====\n{}",
out,
separated(
"====\n",
dataflow.into_iter().map(|(id, rel)| format!(
"View {}:\n{}\n",
cat.get_source_name(&id).unwrap(),
convert_rel_to_string(&rel, cat, &format_type)
))
)
);
cat.remove_transient_objects();
Ok(out)
}
/// Applies a transform across the set of `MirRelationExpr`.
///
/// Returns a string describing information pushed down to sources.
fn apply_cross_view_transform(
transform: &str,
dataflow: &mut Vec<(GlobalId, MirRelationExpr)>,
cat: &TestCatalog,
) -> Result<String, String> {
match transform {
"filter" => {
let mut predicates = HashMap::new();
match optimize_dataflow_filters_inner(dataflow.iter_mut().map(|(id, rel)| (Id::Global(*id), rel)).rev(), &mut predicates) {
Ok(()) => Ok(format!("Pushed-down predicates:\n{}", log_pushed_outside_of_dataflow(predicates, cat))),
Err(e) => Err(e.to_string()),
}
}
"project" => {
let mut demand = HashMap::new();
if let Some((id, rel)) = dataflow.last() {
demand.insert(Id::Global(*id), (0..rel.arity()).collect());
}
match optimize_dataflow_demand_inner(dataflow.iter_mut().map(|(id, rel)| (Id::Global(*id), rel)).rev(), &mut demand) {
Ok(()) => Ok(format!("Pushed-down demand:\n{}", log_pushed_outside_of_dataflow(demand, cat))),
Err(e) => Err(e.to_string()),
}
}
_ => return Err(format!(
"no cross-view transform named {} (you might have to add it to apply_cross_view_transform)",
transform
))
}
}
/// Converts a map of (source) -> (information pushed to source) into a string.
fn log_pushed_outside_of_dataflow<D>(map: HashMap<Id, D>, cat: &TestCatalog) -> String
where
D: std::fmt::Debug + Clone,
{
let mut result = String::new();
for (id, obj) in map {
if let Id::Global(GlobalId::User(id)) = id {
result.push_str(
&format!(
"Source {}: {:?}",
cat.get_source_name(&GlobalId::User(id)).unwrap(),
obj
)[..],
);
result.push('\n');
}
}
result
}
#[test]
fn run() {
datadriven::walk("tests/testdata", |f| {
let mut catalog = TestCatalog::default();
f.run(move |s| -> String {
match s.directive.as_str() {
"cat" => match catalog.handle_test_command(&s.input) {
Ok(()) => String::from("ok\n"),
Err(err) => format!("error: {}\n", err),
},
"build" => {
match run_single_view_testcase(&s.input, &catalog, &s.args, TestType::Build)
{
// Generally, explanations for fully optimized queries
// are not allowed to have whitespace at the end;
// however, a partially optimized query can.
// Since clippy rejects test results with trailing
// whitespace, remove whitespace before comparing results.
Ok(msg) => {
format!(
"{}",
separated("\n", msg.split('\n').map(|s| s.trim_end()))
)
}
Err(err) => format!("error: {}\n", err),
}
}
"opt" => {
match run_single_view_testcase(&s.input, &catalog, &s.args, TestType::Opt) {
Ok(msg) => msg,
Err(err) => format!("error: {}\n", err),
}
}
"steps" => {
match run_single_view_testcase(&s.input, &catalog, &s.args, TestType::Steps)
{
Ok(msg) => msg,
Err(err) => format!("error: {}\n", err),
}
}
"crossview" => {
match run_multiview_testcase(
&s.input,
&mut catalog,
&s.args,
TestType::Build,
) {
Ok(msg) => format!(
"{}",
separated("\n", msg.split('\n').map(|s| s.trim_end()))
),
Err(err) => format!("error: {}\n", err),
}
}
"crossviewopt" => {
match run_multiview_testcase(
&s.input,
&mut catalog,
&s.args,
TestType::Build,
) {
Ok(msg) => msg,
Err(err) => format!("error: {}\n", err),
}
}
_ => panic!("unknown directive: {}", s.directive),
}
})
});
}
}