Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions datafusion/core/benches/sql_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,71 @@ fn criterion_benchmark(c: &mut Criterion) {
})
});

// --- CTE materialization (unmarked, MATERIALIZED, NOT MATERIALIZED) ---
// A materialized CTE body is optimized on its own before the rest of the
// plan, so these measure that extra planning against inlined CTEs.
//
// The planner inlines unmarked and `NOT MATERIALIZED` CTEs alike, so their
// cases match. They are separate because the unmarked default can change:
// PostgreSQL materializes an unmarked CTE that the query reads more than
// once. `NOT MATERIALIZED` always inlines, so it stays the inlined baseline.
{
// sqlparser only parses `[NOT] MATERIALIZED` for the PostgreSQL dialect.
let cte_ctx = create_context();
rt.block_on(cte_ctx.sql("SET datafusion.sql_parser.dialect = 'PostgreSQL'"))
.unwrap();

let body = "SELECT a0, a1, SUM(a2) AS s FROM t1 WHERE a3 > 0 GROUP BY a0, a1";
let two_refs = "SELECT x.a0, x.s, y.s FROM c x JOIN c y ON x.a0 = y.a1";
for (name, marker) in [
("unmarked", ""),
("materialized", "MATERIALIZED "),
("not_materialized", "NOT MATERIALIZED "),
Comment on lines +1033 to +1035

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to test unmarked and not materialized?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmarked can change, e.g. postgres materializes it if it's referenced more than once. NOT MATERIALIZED is always inlined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks.

] {
let query = format!("WITH c AS {marker}({body}) SELECT * FROM c");
c.bench_function(&format!("physical_cte_{name}_one_ref"), |b| {
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
});

let query = format!("WITH c AS {marker}({body}) {two_refs}");
c.bench_function(&format!("physical_cte_{name}_two_refs"), |b| {
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
});

// Each CTE reads the previous one twice.
let mut query = format!("WITH c0 AS {marker}({body})");
for i in 1..6 {
let prev = i - 1;
write!(
query,
", c{i} AS {marker}(SELECT x.a0, x.a1, x.s + y.s AS s \
FROM c{prev} x JOIN c{prev} y ON x.a0 = y.a1)"
)
.unwrap();
}
query.push_str(" SELECT * FROM c5");
c.bench_function(&format!("physical_cte_{name}_chain_6"), |b| {
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
});
}

// One CTE of each kind, each read twice.
let query = format!(
"WITH m AS MATERIALIZED ({body}), \
n AS NOT MATERIALIZED ({body}), \
u AS ({body}) \
SELECT m.a0, m.s, n.s, u.s FROM m \
JOIN m m2 ON m.a0 = m2.a1 \
JOIN n ON m.a0 = n.a0 \
JOIN n n2 ON n.a0 = n2.a1 \
JOIN u ON m.a0 = u.a0 \
JOIN u u2 ON u.a0 = u2.a1"
);
c.bench_function("physical_cte_mixed", |b| {
b.iter(|| physical_plan(&cte_ctx, &rt, &query))
});
}

// --- TPC-H logical plans (uncommented from existing code) ---
// These test real-world query patterns with moderate plan complexity.

Expand Down
Loading