forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transforms.rs
More file actions
282 lines (254 loc) · 10.1 KB
/
test_transforms.rs
File metadata and controls
282 lines (254 loc) · 10.1 KB
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
// 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.
use std::collections::BTreeSet;
use mz_expr::explain::{enforce_linear_chains, ExplainContext};
use mz_expr_parser::{handle_define, try_parse_mir, TestCatalog};
use mz_ore::str::Indent;
use mz_repr::explain::text::text_string_at;
use mz_repr::explain::{ExplainConfig, PlanRenderingContext};
use mz_transform::attribute::annotate_plan;
use mz_transform::dataflow::DataflowMetainfo;
use mz_transform::typecheck::TypeErrorHumanizer;
#[mz_ore::test]
#[cfg_attr(miri, ignore)] // can't call foreign function `rust_psm_stack_pointer` on OS `linux`
fn run_tests() {
// Interpret datadriven tests.
datadriven::walk("tests/test_transforms", |f| {
let mut catalog = TestCatalog::default();
f.run(|test_case| -> String {
match test_case.directive.as_str() {
"define" => handle_define(&mut catalog, &test_case.input),
"explain" => handle_explain(&catalog, &test_case.input, &test_case.args),
"typecheck" => handle_typecheck(&catalog, &test_case.input, &test_case.args),
"apply" => handle_apply(&catalog, &test_case.input, &test_case.args),
_ => format!("unknown directive: {}", test_case.directive),
}
})
});
}
#[allow(clippy::disallowed_types)]
fn handle_explain(
catalog: &TestCatalog,
input: &str,
args: &std::collections::HashMap<String, Vec<String>>,
) -> String {
let with = match args.get("with") {
Some(with) => with.iter().cloned().collect::<BTreeSet<String>>(),
None => return "missing required `with` argument for `explain` directive".to_string(),
};
// Create the ExplainConfig from the given `with` set of strings.
let config = match parse_explain_config(with) {
Ok(config) => config,
Err(e) => return format!("ExplainConfig::try_from error\n{}\n", e.to_string().trim()),
};
let context = ExplainContext {
config: &config,
humanizer: catalog,
used_indexes: Default::default(),
finishing: Default::default(),
duration: Default::default(),
optimizer_notices: Vec::default(),
};
// Parse the relation, returning early on parse error.
let mut relation = match try_parse_mir(catalog, input) {
Ok(relation) => relation,
Err(e) => return format!("try_parse_mir error:\n{}\n", e.to_string().trim()),
};
// normalize the representation as linear chains
// (this implies !context.config.raw_plans by construction)
if context.config.linear_chains {
match enforce_linear_chains(&mut relation) {
Ok(_) => {}
Err(e) => return format!("enforce_linear_chains error:\n{}\n", e.to_string().trim()),
};
};
// We deliberately don't interpret the `raw_plans` config option here,
// because we might want to test the output of things that are reset when it
// is set. For test purposes we never want to implicitly normalize the plan
// as part this statement.
let annotated_plan = match annotate_plan(&relation, &context) {
Ok(annotated_plan) => annotated_plan,
Err(e) => return format!("annotate_plan error:\n{}\n", e.to_string().trim()),
};
text_string_at(annotated_plan.plan, || PlanRenderingContext {
indent: Indent::default(),
humanizer: context.humanizer,
annotations: annotated_plan.annotations.clone(),
config: &config,
})
}
#[allow(clippy::disallowed_types)]
fn handle_typecheck(
catalog: &TestCatalog,
input: &str,
_args: &std::collections::HashMap<String, Vec<String>>,
) -> String {
// Parse the relation, returning early on parse error.
let relation = match try_parse_mir(catalog, input) {
Ok(relation) => relation,
Err(err) => return err,
};
// Apply the transformation, returning early on TransformError.
use mz_transform::typecheck::{columns_pretty, Typecheck};
let ctx = mz_transform::typecheck::empty_context();
let tc = Typecheck::new(std::sync::Arc::clone(&ctx));
let res = tc.typecheck(&relation, &ctx.lock().expect("typecheck ctx"));
match res {
Ok(typ) => format!("{}\n", columns_pretty(&typ, catalog).trim()),
Err(err) => format!(
"{}\n",
TypeErrorHumanizer::new(&err, catalog).to_string().trim(),
),
}
}
#[allow(clippy::disallowed_types)]
fn handle_apply(
catalog: &TestCatalog,
input: &str,
args: &std::collections::HashMap<String, Vec<String>>,
) -> String {
let Some(pipeline) = args.get("pipeline") else {
return "missing required `pipeline` argument for `apply` directive".to_string();
};
if pipeline.len() != 1 {
return "unexpected `pipeline` arguments for `apply` directive".to_string();
}
let result = match pipeline[0].as_str() {
// Pseudo-transforms.
"identity" => {
// noop
let transform = Identity::default();
apply_transform(transform, catalog, input)
}
// Actual transforms.
"anf" => {
use mz_transform::cse::anf::ANF;
let transform = ANF::default();
apply_transform(transform, catalog, input)
}
"column_knowledge" => {
use mz_transform::column_knowledge::ColumnKnowledge;
let transform = ColumnKnowledge::default();
apply_transform(transform, catalog, input)
}
"fold_constants" => {
use mz_transform::fold_constants::FoldConstants;
let transform = FoldConstants { limit: None };
apply_transform(transform, catalog, input)
}
"literal_lifting" => {
use mz_transform::literal_lifting::LiteralLifting;
let transform = LiteralLifting::default();
apply_transform(transform, catalog, input)
}
"non_null_requirements" => {
use mz_transform::non_null_requirements::NonNullRequirements;
let transform = NonNullRequirements::default();
apply_transform(transform, catalog, input)
}
"predicate_pushdown" => {
use mz_transform::predicate_pushdown::PredicatePushdown;
let transform = PredicatePushdown::default();
apply_transform(transform, catalog, input)
}
"projection_lifting" => {
use mz_transform::movement::ProjectionLifting;
let transform = ProjectionLifting::default();
apply_transform(transform, catalog, input)
}
"projection_pushdown" => {
use mz_transform::movement::ProjectionPushdown;
let transform = ProjectionPushdown::default();
apply_transform(transform, catalog, input)
}
"normalize_lets" => {
use mz_transform::normalize_lets::NormalizeLets;
let transform = NormalizeLets::new(false);
apply_transform(transform, catalog, input)
}
"redundant_join" => {
use mz_transform::redundant_join::RedundantJoin;
let transform = RedundantJoin::default();
apply_transform(transform, catalog, input)
}
"relation_cse" => {
use mz_transform::cse::relation_cse::RelationCSE;
let transform = RelationCSE::new(false);
apply_transform(transform, catalog, input)
}
"semijoin_idempotence" => {
use mz_transform::semijoin_idempotence::SemijoinIdempotence;
let transform = SemijoinIdempotence::default();
apply_transform(transform, catalog, input)
}
"fusion_top_k" => {
use mz_transform::fusion::top_k::TopK;
let transform = TopK;
apply_transform(transform, catalog, input)
}
"fusion_join" => {
use mz_transform::fusion::join::Join;
let transform = Join;
apply_transform(transform, catalog, input)
}
transform => Err(format!("unsupported pipeline transform: {transform}")),
};
result.unwrap_or_else(|err| err)
}
fn apply_transform<T: mz_transform::Transform>(
transform: T,
catalog: &TestCatalog,
input: &str,
) -> Result<String, String> {
// Parse the relation, returning early on parse error.
let mut relation = try_parse_mir(catalog, input)?;
let features = mz_repr::optimize::OptimizerFeatures::default();
let typecheck_ctx = mz_transform::typecheck::empty_context();
let mut df_meta = DataflowMetainfo::default();
let mut transform_ctx =
mz_transform::TransformCtx::local(&features, &typecheck_ctx, &mut df_meta);
// Apply the transformation, returning early on TransformError.
transform
.transform(&mut relation, &mut transform_ctx)
.map_err(|e| format!("{}\n", e.to_string().trim()))?;
// Serialize and return the transformed relation.
Ok(relation.explain(&ExplainConfig::default(), Some(catalog)))
}
fn parse_explain_config(mut flags: BTreeSet<String>) -> Result<ExplainConfig, String> {
let result = ExplainConfig {
arity: flags.remove("arity"),
humanized_exprs: flags.remove("humanized_exprs"),
column_names: flags.remove("column_names"),
types: flags.remove("types"),
redacted: false,
join_impls: false,
raw_plans: false,
..ExplainConfig::default()
};
if flags.is_empty() {
Ok(result)
} else {
let err = format!(
"parse_explain_config\n\
unsupported 'with' option: {flags:?}\n"
);
Err(err)
}
}
#[derive(Debug, Default)]
struct Identity;
impl mz_transform::Transform for Identity {
fn transform(
&self,
_relation: &mut mz_expr::MirRelationExpr,
_ctx: &mut mz_transform::TransformCtx,
) -> Result<(), mz_transform::TransformError> {
Ok(())
}
}