Skip to content

Commit 4824866

Browse files
adriangbclaude
andcommitted
bench: plan unmarked, MATERIALIZED and NOT MATERIALIZED CTEs
Add sql_planner cases that plan a CTE read once, read twice, and a chain of six CTEs that each read the previous one twice, for each kind of CTE, plus one query that mixes all three. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent af36f80 commit 4824866

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

‎datafusion/core/benches/sql_planner.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,66 @@ fn criterion_benchmark(c: &mut Criterion) {
10031003
})
10041004
});
10051005

1006+
// --- CTE materialization (unmarked, MATERIALIZED, NOT MATERIALIZED) ---
1007+
// A materialized CTE body is optimized on its own before the rest of the
1008+
// plan, so these measure that extra planning against inlined CTEs.
1009+
{
1010+
// sqlparser only parses `[NOT] MATERIALIZED` for the PostgreSQL dialect.
1011+
let cte_ctx = create_context();
1012+
rt.block_on(cte_ctx.sql("SET datafusion.sql_parser.dialect = 'PostgreSQL'"))
1013+
.unwrap();
1014+
1015+
let body = "SELECT a0, a1, SUM(a2) AS s FROM t1 WHERE a3 > 0 GROUP BY a0, a1";
1016+
let two_refs = "SELECT x.a0, x.s, y.s FROM c x JOIN c y ON x.a0 = y.a1";
1017+
for (name, marker) in [
1018+
("unmarked", ""),
1019+
("materialized", "MATERIALIZED "),
1020+
("not_materialized", "NOT MATERIALIZED "),
1021+
] {
1022+
let query = format!("WITH c AS {marker}({body}) SELECT * FROM c");
1023+
c.bench_function(&format!("physical_cte_{name}_one_ref"), |b| {
1024+
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
1025+
});
1026+
1027+
let query = format!("WITH c AS {marker}({body}) {two_refs}");
1028+
c.bench_function(&format!("physical_cte_{name}_two_refs"), |b| {
1029+
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
1030+
});
1031+
1032+
// Each CTE reads the previous one twice.
1033+
let mut query = format!("WITH c0 AS {marker}({body})");
1034+
for i in 1..6 {
1035+
let prev = i - 1;
1036+
write!(
1037+
query,
1038+
", c{i} AS {marker}(SELECT x.a0, x.a1, x.s + y.s AS s \
1039+
FROM c{prev} x JOIN c{prev} y ON x.a0 = y.a1)"
1040+
)
1041+
.unwrap();
1042+
}
1043+
query.push_str(" SELECT * FROM c5");
1044+
c.bench_function(&format!("physical_cte_{name}_chain_6"), |b| {
1045+
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
1046+
});
1047+
}
1048+
1049+
// One CTE of each kind, each read twice.
1050+
let query = format!(
1051+
"WITH m AS MATERIALIZED ({body}), \
1052+
n AS NOT MATERIALIZED ({body}), \
1053+
u AS ({body}) \
1054+
SELECT m.a0, m.s, n.s, u.s FROM m \
1055+
JOIN m m2 ON m.a0 = m2.a1 \
1056+
JOIN n ON m.a0 = n.a0 \
1057+
JOIN n n2 ON n.a0 = n2.a1 \
1058+
JOIN u ON m.a0 = u.a0 \
1059+
JOIN u u2 ON u.a0 = u2.a1"
1060+
);
1061+
c.bench_function("physical_cte_mixed", |b| {
1062+
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
1063+
});
1064+
}
1065+
10061066
// --- TPC-H logical plans (uncommented from existing code) ---
10071067
// These test real-world query patterns with moderate plan complexity.
10081068

0 commit comments

Comments
 (0)