You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/07 17:03:10 UTC

(datafusion) 01/01: bench: discover ClickBench query files in sql_planner instead of hardcoding ranges (#25027)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-25027-16ace4f3e5f41cde80a5249fbeb6330d222fd8b1
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit 40502884807dca5d6f7847937b8d8d61958cf3c3
Author: Adrian Garcia Badaracco <17...@users.noreply.github.com>
AuthorDate: Mon Sep 7 17:02:49 2026 +0000

    bench: discover ClickBench query files in sql_planner instead of hardcoding ranges (#25027)
    
    ## Which issue does this PR close?
    
    - N/A — small benchmark cleanup, no issue filed.
    
    ## Rationale for this change
    
    `datafusion/core/benches/sql_planner.rs` builds its ClickBench planning
    benchmark
    set from two hardcoded ranges:
    
    ```rust
    let clickbench_queries = (0..=42)
        .map(|q| /* queries/clickbench/queries/q{q}.sql */)
        .chain((0..=7).map(|q| /* queries/clickbench/extended/q{q}.sql */))
    ```
    
    `benchmarks/queries/clickbench/extended/` has grown since that `(0..=7)`
    was
    written and now contains `q0.sql` through `q13.sql`, so extended queries
    q8–q13
    were never planned by this benchmark — six queries silently outside
    coverage.
    
    The ranges are also a maintenance trap in both directions: adding a
    query file
    requires remembering to bump an unrelated literal, and because the read
    is
    `std::fs::read_to_string(...).unwrap()`, a range that runs past the end
    of the
    directory panics the whole bench rather than stopping.
    
    The `dfbench clickbench` runner does not have this problem — it
    discovers query
    files by walking `0..` and stopping at the first missing one
    (`get_query_sql` /
    `get_query_path` in `benchmarks/src/clickbench.rs`). This PR makes the
    planning
    benchmark use the same scheme.
    
    ## What changes are included in this PR?
    
    - Adds a `read_numbered_queries(dir)` helper to `sql_planner.rs` that
    reads
      `q{N}.sql` starting at `q0.sql` and stops at the first missing file.
    - Replaces both hardcoded ranges — standard `(0..=42)` and extended
    `(0..=7)` —
    with calls to it. New query files are now picked up without a code
    change.
    
    Two details on error handling, since the previous `.unwrap()` was
    load-bearing:
    
    - Only `ErrorKind::NotFound` ends the sequence. Any other IO error still
    panics,
      now with the offending path in the message.
    - An `assert!(!queries.is_empty(), ...)` guards the case where
    `benchmarks_path`
    resolves somewhere wrong: without it, discovery would silently register
    zero
      ClickBench benchmarks, which is a worse failure than the old panic.
    
    I also grepped for other places that hardcode a ClickBench extended
    query count;
    there are none. `benchmarks/bench.sh` passes `--queries-path` to the
    `dfbench`
    runner, which already discovers.
    
    ## What is the testing strategy for this PR?
    
    This is benchmark-only code with no unit tests; verified by running the
    bench
    target locally against a real ClickBench `hits_partitioned` dataset:
    
    - `cargo bench -p datafusion --bench sql_planner -- --list` now
    enumerates **57**
    `physical_plan_clickbench_q*` benchmarks (43 standard + 14 extended), up
    from
      51, and does not panic.
    - `cargo bench -p datafusion --bench sql_planner -- --quick
    'physical_plan_clickbench_q5[2-7]$'`
      — the six newly covered extended queries — all plan successfully
      (6.4–10.7 ms each, unoptimized build).
    - `cargo fmt --all` and `cargo clippy -p datafusion --benches -- -D
    warnings` are
      clean.
    
    Note that benchmark IDs are positional (`q{index + 1}`), so the extended
    queries
    keep their existing IDs and the six new ones append as q52–q57; no
    existing
    benchmark is renumbered.
    
    ## Are there any user-facing changes?
    
    No. Benchmark harness only — no library code, no public API.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    ---------
    
    Co-authored-by: Claude Opus 5 <no...@anthropic.com>
---
 datafusion/core/benches/sql_planner.rs | 70 +++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 14 deletions(-)

diff --git a/datafusion/core/benches/sql_planner.rs b/datafusion/core/benches/sql_planner.rs
index de81ed021b..48770541ca 100644
--- a/datafusion/core/benches/sql_planner.rs
+++ b/datafusion/core/benches/sql_planner.rs
@@ -85,6 +85,55 @@ fn physical_plan(ctx: &SessionContext, rt: &Runtime, sql: &str) {
     }));
 }
 
+/// Read the `q{N}.sql` query files from `dir`, in ascending numeric order.
+///
+/// The files are discovered by listing the directory rather than by walking a
+/// hardcoded range, so newly added queries are picked up without changing this
+/// code. Entries that do not match `q{N}.sql` (such as the `sorted_data`
+/// directory alongside the ClickBench queries) are ignored.
+///
+/// Panics if `dir` contains no queries, or if the numbering is not contiguous
+/// starting at `q0.sql`. A gap almost certainly means a query file was lost, and
+/// silently benchmarking a subset would hide that.
+fn read_numbered_queries(dir: &str) -> Vec<String> {
+    let entries = std::fs::read_dir(dir)
+        .unwrap_or_else(|e| panic!("Failed to read query directory '{dir}': {e}"));
+
+    let mut numbers = entries
+        .map(|entry| {
+            entry.unwrap_or_else(|e| panic!("Failed to read entry in '{dir}': {e}"))
+        })
+        .filter_map(|entry| {
+            let file_name = entry.file_name();
+            let stem = file_name
+                .to_str()?
+                .strip_prefix('q')?
+                .strip_suffix(".sql")?;
+            stem.parse::<usize>().ok()
+        })
+        .collect::<Vec<_>>();
+    numbers.sort_unstable();
+
+    assert!(
+        !numbers.is_empty(),
+        "No `q{{N}}.sql` query files found in '{dir}'"
+    );
+    assert_eq!(
+        numbers,
+        (0..numbers.len()).collect::<Vec<_>>(),
+        "Query files in '{dir}' are not numbered contiguously from q0.sql"
+    );
+
+    numbers
+        .into_iter()
+        .map(|q| {
+            let path = format!("{dir}q{q}.sql");
+            std::fs::read_to_string(&path)
+                .unwrap_or_else(|e| panic!("Failed to read query file '{path}': {e}"))
+        })
+        .collect()
+}
+
 /// Create schema with the specified number of columns
 fn create_schema(column_prefix: &str, num_columns: usize) -> Schema {
     let fields: Fields = (0..num_columns)
@@ -636,20 +685,13 @@ fn criterion_benchmark(c: &mut Criterion) {
     // });
 
     // -- clickbench --
-    let clickbench_queries = (0..=42)
-        .map(|q| {
-            std::fs::read_to_string(format!(
-                "{benchmarks_path}queries/clickbench/queries/q{q}.sql"
-            ))
-            .unwrap()
-        })
-        .chain((0..=7).map(|q| {
-            std::fs::read_to_string(format!(
-                "{benchmarks_path}queries/clickbench/extended/q{q}.sql"
-            ))
-            .unwrap()
-        }))
-        .collect::<Vec<_>>();
+    let clickbench_queries =
+        read_numbered_queries(&format!("{benchmarks_path}queries/clickbench/queries/"))
+            .into_iter()
+            .chain(read_numbered_queries(&format!(
+                "{benchmarks_path}queries/clickbench/extended/"
+            )))
+            .collect::<Vec<_>>();
 
     let clickbench_ctx = register_clickbench_hits_table(&rt);
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org