comphead commented on code in PR #27999:
URL: https://github.com/apache/datafusion/pull/27999#discussion_r4789493761


##########
benchmarks/src/hj.rs:
##########
@@ -617,17 +717,22 @@ impl RunOpt {
 
             // For Q23 force Partitioned mode: zero the CollectLeft thresholds
             // so the planner cannot prove the build side is small (as happens
-            // when the datasource provides no row-count stats).
-            if query.isolate_partitioned_join {
-                ctx.sql(
-                    "SET 
datafusion.optimizer.hash_join_single_partition_threshold = 0",
-                )
-                .await?;
-                ctx.sql(
-                    "SET 
datafusion.optimizer.hash_join_single_partition_threshold_rows = 0",
-                )
-                .await?;
-            }
+            // when the datasource provides no row-count stats). Other queries
+            // get the session's thresholds back, so a query plans the same in
+            // a run of every query as when run alone.
+            let (threshold, threshold_rows) = if 
query.isolate_partitioned_join {
+                (0, 0)
+            } else {
+                thresholds
+            };
+            ctx.sql(&format!(
+                "SET datafusion.optimizer.hash_join_single_partition_threshold 
= {threshold}"
+            ))
+            .await?;
+            ctx.sql(&format!(
+                "SET 
datafusion.optimizer.hash_join_single_partition_threshold_rows = 
{threshold_rows}"
+            ))
+            .await?;

Review Comment:
   This works, but the loop still changes the one shared session and then sets 
two keys back before every query. Planning the flagged queries on their own 
copy of the session removes the leak, with no saved `thresholds` and no 
`format!`-built `SET`s. The copy from `ctx.state()` shares the registered 
tables and the `RuntimeEnv` (so the pool passed to `set_memory_pool` still 
applies). `config_mut` copies on write, so `ctx` keeps what 
`SessionConfig::from_env()` set.
   
   ```rust
   // Before the loop, in place of `thresholds`:
   // Queries with `isolate_partitioned_join` plan on a copy of the session with
   // the CollectLeft thresholds zeroed, as if the datasource had no row-count
   // stats, so the setting cannot leak into later queries.
   let partitioned_ctx = {
       let mut state = ctx.state();
       let optimizer = &mut state.config_mut().options_mut().optimizer;
       optimizer.hash_join_single_partition_threshold = 0;
       optimizer.hash_join_single_partition_threshold_rows = 0;
       SessionContext::new_with_state(state)
   };
   
   // In the loop, in place of the `SET`s:
   let query_ctx = if query.isolate_partitioned_join {
       &partitioned_ctx
   } else {
       &ctx
   };
   ```
   
   Then pass `query_ctx` to `benchmark_query`. On main this compiles and passes 
`cargo clippy -p datafusion-benchmarks --bin dfbench -- -D warnings`. It also 
drops the "For Q23" wording, which is stale now that Q23-Q28, Q31 and Q32 set 
the flag.



##########
benchmarks/src/hj.rs:
##########
@@ -553,6 +553,97 @@ const HASH_QUERIES: &[HashJoinQuery] = &[
         probe_size: "100K_count",
         isolate_partitioned_join: true,
     },
+    // Q29: a fact table carrying four DECIMAL columns through a join to a 
small
+    // dimension, into an aggregation (a TPC-DS store_sales-to-dimension 
shape).
+    // Build Side: supplier (100K rows, CollectLeft) | Probe Side: lineitem 
(60M
+    // rows), each of which finds exactly one supplier, in order.
+    HashJoinQuery {
+        sql: r###"SELECT s.s_nationkey, sum(l.l_quantity), 
sum(l.l_extendedprice),
+          sum(l.l_discount), sum(l.l_tax)
+        FROM supplier s JOIN lineitem l ON s.s_suppkey = l.l_suppkey
+        GROUP BY s.s_nationkey"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "100K",
+        probe_size: "60M_4_decimal_payload",
+        isolate_partitioned_join: false,
+    },
+    // Q30: Q29 with one supplier in ten left out of the build side, so about 
90%
+    // of the probe rows find one.
+    HashJoinQuery {
+        sql: r###"SELECT s.s_nationkey, sum(l.l_quantity), 
sum(l.l_extendedprice),
+          sum(l.l_discount), sum(l.l_tax)
+        FROM (SELECT s_suppkey, s_nationkey FROM supplier WHERE s_suppkey % 10 
<> 0) s
+        JOIN lineitem l ON s.s_suppkey = l.l_suppkey
+        GROUP BY s.s_nationkey"###,
+        density: 0.9,
+        prob_hit: 0.9,
+        build_size: "90K",
+        probe_size: "60M_4_decimal_payload",
+        isolate_partitioned_join: false,
+    },
+    // Q31: Q29's payload through a Partitioned join to a large dimension.
+    // Build Side: orders (15M rows) | Probe Side: lineitem (60M rows), each of
+    // which finds exactly one order.
+    HashJoinQuery {
+        sql: r###"SELECT o.o_orderpriority, sum(l.l_quantity), 
sum(l.l_extendedprice),
+          sum(l.l_discount), sum(l.l_tax)
+        FROM orders o JOIN lineitem l ON o.o_orderkey = l.l_orderkey
+        GROUP BY o.o_orderpriority"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "15M",
+        probe_size: "60M_4_decimal_payload",
+        isolate_partitioned_join: true,
+    },
+    // Q32: the payload carried through two joins in a row (TPC-DS q04's
+    // year_total shape): lineitem to orders, then the result to customer.
+    // Build Sides: orders (15M rows), customer (1.5M rows) | Probe Side:
+    // lineitem (60M rows); every row finds one row in each.
+    HashJoinQuery {
+        sql: r###"SELECT c.c_nationkey, sum(ol.l_quantity), 
sum(ol.l_extendedprice),
+          sum(ol.l_discount), sum(ol.l_tax)
+        FROM customer c
+        JOIN (
+          SELECT o.o_custkey, l.l_quantity, l.l_extendedprice, l.l_discount, 
l.l_tax
+          FROM orders o JOIN lineitem l ON o.o_orderkey = l.l_orderkey
+        ) ol ON c.c_custkey = ol.o_custkey
+        GROUP BY c.c_nationkey"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "15M_then_1.5M",
+        probe_size: "60M_4_decimal_payload_two_joins",
+        isolate_partitioned_join: true,
+    },
+    // Q33: Q29 without the payload: the join carries no probe column.
+    HashJoinQuery {
+        sql: r###"SELECT s.s_nationkey, count(*)
+        FROM supplier s JOIN lineitem l ON s.s_suppkey = l.l_suppkey
+        GROUP BY s.s_nationkey"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "100K",
+        probe_size: "60M_no_payload",
+        isolate_partitioned_join: false,
+    },

Review Comment:
   Q33 does the same join work as Q3: `supplier` build, `lineitem` probe on the 
supplier key, 100% hit rate, `CollectLeft`, and no probe column in the join 
output. So Q3 already works as the control the follow-up cannot affect.
   
   The Q29 minus Q33 gap does not isolate the copy either. Of the CPU that Q29 
adds over Q33 in the profile, 57% is Parquet decoding of the four payload 
columns, 13% is the probe-side `take` and 12% is the four sums. I would drop 
Q33 and name Q3 as the no-payload twin in Q29's comment.



##########
benchmarks/src/hj.rs:
##########
@@ -553,6 +553,97 @@ const HASH_QUERIES: &[HashJoinQuery] = &[
         probe_size: "100K_count",
         isolate_partitioned_join: true,
     },
+    // Q29: a fact table carrying four DECIMAL columns through a join to a 
small
+    // dimension, into an aggregation (a TPC-DS store_sales-to-dimension 
shape).
+    // Build Side: supplier (100K rows, CollectLeft) | Probe Side: lineitem 
(60M
+    // rows), each of which finds exactly one supplier, in order.
+    HashJoinQuery {
+        sql: r###"SELECT s.s_nationkey, sum(l.l_quantity), 
sum(l.l_extendedprice),
+          sum(l.l_discount), sum(l.l_tax)
+        FROM supplier s JOIN lineitem l ON s.s_suppkey = l.l_suppkey
+        GROUP BY s.s_nationkey"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "100K",
+        probe_size: "60M_4_decimal_payload",
+        isolate_partitioned_join: false,
+    },
+    // Q30: Q29 with one supplier in ten left out of the build side, so about 
90%
+    // of the probe rows find one.
+    HashJoinQuery {
+        sql: r###"SELECT s.s_nationkey, sum(l.l_quantity), 
sum(l.l_extendedprice),
+          sum(l.l_discount), sum(l.l_tax)
+        FROM (SELECT s_suppkey, s_nationkey FROM supplier WHERE s_suppkey % 10 
<> 0) s
+        JOIN lineitem l ON s.s_suppkey = l.l_suppkey
+        GROUP BY s.s_nationkey"###,
+        density: 0.9,
+        prob_hit: 0.9,
+        build_size: "90K",
+        probe_size: "60M_4_decimal_payload",
+        isolate_partitioned_join: false,
+    },
+    // Q31: Q29's payload through a Partitioned join to a large dimension.
+    // Build Side: orders (15M rows) | Probe Side: lineitem (60M rows), each of
+    // which finds exactly one order.
+    HashJoinQuery {
+        sql: r###"SELECT o.o_orderpriority, sum(l.l_quantity), 
sum(l.l_extendedprice),
+          sum(l.l_discount), sum(l.l_tax)
+        FROM orders o JOIN lineitem l ON o.o_orderkey = l.l_orderkey
+        GROUP BY o.o_orderpriority"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "15M",
+        probe_size: "60M_4_decimal_payload",
+        isolate_partitioned_join: true,
+    },
+    // Q32: the payload carried through two joins in a row (TPC-DS q04's
+    // year_total shape): lineitem to orders, then the result to customer.
+    // Build Sides: orders (15M rows), customer (1.5M rows) | Probe Side:
+    // lineitem (60M rows); every row finds one row in each.
+    HashJoinQuery {
+        sql: r###"SELECT c.c_nationkey, sum(ol.l_quantity), 
sum(ol.l_extendedprice),
+          sum(ol.l_discount), sum(ol.l_tax)
+        FROM customer c
+        JOIN (
+          SELECT o.o_custkey, l.l_quantity, l.l_extendedprice, l.l_discount, 
l.l_tax
+          FROM orders o JOIN lineitem l ON o.o_orderkey = l.l_orderkey
+        ) ol ON c.c_custkey = ol.o_custkey
+        GROUP BY c.c_nationkey"###,
+        density: 1.0,
+        prob_hit: 1.0,
+        build_size: "15M_then_1.5M",
+        probe_size: "60M_4_decimal_payload_two_joins",
+        isolate_partitioned_join: true,
+    },

Review Comment:
   Q32 is the slowest new query, and the one where the copy the follow-up 
targets is the smallest share. In the profile the probe-side `take` is 3.8% of 
CPU across both joins, against 9 to 11% for Q29. Most of the time goes to the 
15M-row hash table (`JoinHashMapU32::get_matched_indices`, 24% self time) and 
to hash-repartitioning the payload before each join (`BatchPartitioner`, 12%).
   
   Its first join is also Q31's join (`orders o JOIN lineitem l ON o.o_orderkey 
= l.l_orderkey`). If a two-join case is wanted, two `CollectLeft` joins in a 
row give a larger share at about a third of the time. This took 0.14 s against 
0.40 s for Q32, with 16% of CPU in the probe-side `take`, and both joins plan 
as `CollectLeft` with the default thresholds:
   
   ```sql
   SELECT n.n_regionkey, sum(sl.l_quantity), sum(sl.l_extendedprice),
     sum(sl.l_discount), sum(sl.l_tax)
   FROM nation n
   JOIN (
     SELECT s.s_nationkey, l.l_quantity, l.l_extendedprice, l.l_discount, 
l.l_tax
     FROM supplier s JOIN lineitem l ON s.s_suppkey = l.l_suppkey
   ) sl ON n.n_nationkey = sl.s_nationkey
   GROUP BY n.n_regionkey
   ```
   
   Otherwise I would drop Q32. Q31 has the same low share (4.2%), so it works 
as a check that `Partitioned` mode does not regress rather than as a place to 
see the gain.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to