|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::collections::HashMap; |
| 16 | + |
| 17 | +use databend_common_catalog::table_context::TableContextSettings; |
| 18 | +use databend_common_exception::Result; |
| 19 | + |
| 20 | +use super::table_statistics; |
| 21 | +use crate::framework::LiteTableContext; |
| 22 | +use crate::framework::golden::SqlTestCase; |
| 23 | +use crate::framework::golden::open_golden_file; |
| 24 | +use crate::framework::golden::write_case_header; |
| 25 | + |
| 26 | +async fn write_distributed_case( |
| 27 | + file: &mut impl std::io::Write, |
| 28 | + case: &SqlTestCase, |
| 29 | + probe_rows: u64, |
| 30 | + build_rows: u64, |
| 31 | +) -> Result<()> { |
| 32 | + let ctx = LiteTableContext::create().await?; |
| 33 | + ctx.set_cluster_node_num(2); |
| 34 | + ctx.set_table_warehouse_distribution(true); |
| 35 | + let settings = ctx.get_settings(); |
| 36 | + settings.set_setting("disable_join_reorder".to_string(), "1".to_string())?; |
| 37 | + ctx.register_table_sql_with_stats( |
| 38 | + BIG_TABLE, |
| 39 | + Some(table_statistics(probe_rows)), |
| 40 | + HashMap::new(), |
| 41 | + HashMap::new(), |
| 42 | + ) |
| 43 | + .await?; |
| 44 | + ctx.register_table_sql_with_stats( |
| 45 | + SMALL_TABLE, |
| 46 | + Some(table_statistics(build_rows)), |
| 47 | + HashMap::new(), |
| 48 | + HashMap::new(), |
| 49 | + ) |
| 50 | + .await?; |
| 51 | + |
| 52 | + let raw_plan = ctx.bind_sql(case.sql).await?; |
| 53 | + let optimized_plan = ctx.optimize_plan(raw_plan.clone()).await?; |
| 54 | + |
| 55 | + write_case_header(file, case)?; |
| 56 | + writeln!(file, "raw_plan:")?; |
| 57 | + writeln!(file, "{}", raw_plan.format_indent(Default::default())?)?; |
| 58 | + writeln!(file, "optimized_plan:")?; |
| 59 | + writeln!( |
| 60 | + file, |
| 61 | + "{}", |
| 62 | + optimized_plan.format_indent(Default::default())? |
| 63 | + )?; |
| 64 | + writeln!(file)?; |
| 65 | + |
| 66 | + Ok(()) |
| 67 | +} |
| 68 | + |
| 69 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 70 | +async fn test_serial_build_distribution() -> Result<()> { |
| 71 | + let mut file = open_golden_file("optimizer", "distributed_join.txt")?; |
| 72 | + let cases = [ |
| 73 | + ( |
| 74 | + SqlTestCase { |
| 75 | + name: "small_serial_build_is_broadcast", |
| 76 | + description: "A small scalar aggregate on the build side should be broadcast without merging the distributed probe.", |
| 77 | + setup_sqls: &[], |
| 78 | + sql: "SELECT b.k, s.max_k |
| 79 | +FROM big_table AS b |
| 80 | +LEFT JOIN (SELECT max(k) AS max_k FROM small_table) AS s |
| 81 | +ON b.k = s.max_k", |
| 82 | + }, |
| 83 | + 227_000_000, |
| 84 | + 24, |
| 85 | + ), |
| 86 | + ( |
| 87 | + SqlTestCase { |
| 88 | + name: "cross_join_small_serial_build_is_broadcast", |
| 89 | + description: "A small scalar aggregate should also be broadcast for a cross join without merging the distributed probe.", |
| 90 | + setup_sqls: &[], |
| 91 | + sql: "SELECT b.k, s.max_k |
| 92 | +FROM big_table AS b |
| 93 | +CROSS JOIN (SELECT max(k) AS max_k FROM small_table) AS s", |
| 94 | + }, |
| 95 | + 227_000_000, |
| 96 | + 24, |
| 97 | + ), |
| 98 | + ( |
| 99 | + SqlTestCase { |
| 100 | + name: "large_serial_build_remains_serial", |
| 101 | + description: "A Serial build that is not smaller than the probe should not be broadcast unconditionally.", |
| 102 | + setup_sqls: &[], |
| 103 | + sql: "SELECT b.k, s.k |
| 104 | +FROM big_table AS b |
| 105 | +LEFT JOIN ( |
| 106 | + SELECT k, row_number() OVER () AS row_num |
| 107 | + FROM small_table |
| 108 | +) AS s |
| 109 | +ON b.k = s.k", |
| 110 | + }, |
| 111 | + 1_000, |
| 112 | + 1_000, |
| 113 | + ), |
| 114 | + ]; |
| 115 | + |
| 116 | + for (case, probe_rows, build_rows) in &cases { |
| 117 | + write_distributed_case(&mut file, case, *probe_rows, *build_rows).await?; |
| 118 | + } |
| 119 | + |
| 120 | + Ok(()) |
| 121 | +} |
| 122 | + |
| 123 | +const BIG_TABLE: &str = "CREATE TABLE big_table (k BIGINT)"; |
| 124 | +const SMALL_TABLE: &str = "CREATE TABLE small_table (k BIGINT)"; |
0 commit comments