From bced232868fae1e9e40bf66f6d9c9579b47ac09d Mon Sep 17 00:00:00 2001 From: Arenatlx <314806019@qq.com> Date: Tue, 26 Jul 2022 14:19:10 +0800 Subject: [PATCH] cherry pick #35854 to release-5.1 Signed-off-by: ti-srebot --- cmd/explaintest/r/explain_cte.result | 371 +++++++++++++++++++++++++++ cmd/explaintest/t/explain_cte.test | 242 +++++++++++++++++ planner/core/logical_plan_builder.go | 2 + 3 files changed, 615 insertions(+) diff --git a/cmd/explaintest/r/explain_cte.result b/cmd/explaintest/r/explain_cte.result index 59fbda4dc20e9..d29226afeaa29 100644 --- a/cmd/explaintest/r/explain_cte.result +++ b/cmd/explaintest/r/explain_cte.result @@ -202,3 +202,374 @@ id estRows task access object operator info CTEFullScan_18 0.00 root CTE:cte1 data:CTE_0 CTE_0 0.00 root Non-Recursive CTE └─TableDual_16(Seed Part) 0.00 root rows:0 +<<<<<<< HEAD +======= +CREATE TABLE `customer` ( +`c_customer_sk` int(11) NOT NULL, +`c_customer_id` char(16) NOT NULL, +`c_current_cdemo_sk` int(11) DEFAULT NULL, +`c_current_hdemo_sk` int(11) DEFAULT NULL, +`c_current_addr_sk` int(11) DEFAULT NULL, +`c_first_shipto_date_sk` int(11) DEFAULT NULL, +`c_first_sales_date_sk` int(11) DEFAULT NULL, +`c_salutation` char(10) DEFAULT NULL, +`c_first_name` char(20) DEFAULT NULL, +`c_last_name` char(30) DEFAULT NULL, +`c_preferred_cust_flag` char(1) DEFAULT NULL, +`c_birth_day` int(11) DEFAULT NULL, +`c_birth_month` int(11) DEFAULT NULL, +`c_birth_year` int(11) DEFAULT NULL, +`c_birth_country` varchar(20) DEFAULT NULL, +`c_login` char(13) DEFAULT NULL, +`c_email_address` char(50) DEFAULT NULL, +`c_last_review_date_sk` int(11) DEFAULT NULL, +PRIMARY KEY (`c_customer_sk`) /*T![clustered_index] NONCLUSTERED */ +); +CREATE TABLE `store_sales` ( +`ss_sold_date_sk` int(11) DEFAULT NULL, +`ss_sold_time_sk` int(11) DEFAULT NULL, +`ss_item_sk` int(11) NOT NULL, +`ss_customer_sk` int(11) DEFAULT NULL, +`ss_cdemo_sk` int(11) DEFAULT NULL, +`ss_hdemo_sk` int(11) DEFAULT NULL, +`ss_addr_sk` int(11) DEFAULT NULL, +`ss_store_sk` int(11) DEFAULT NULL, +`ss_promo_sk` int(11) DEFAULT NULL, +`ss_ticket_number` int(11) NOT NULL, +`ss_quantity` int(11) DEFAULT NULL, +`ss_wholesale_cost` decimal(7,2) DEFAULT NULL, +`ss_list_price` decimal(7,2) DEFAULT NULL, +`ss_sales_price` decimal(7,2) DEFAULT NULL, +`ss_ext_discount_amt` decimal(7,2) DEFAULT NULL, +`ss_ext_sales_price` decimal(7,2) DEFAULT NULL, +`ss_ext_wholesale_cost` decimal(7,2) DEFAULT NULL, +`ss_ext_list_price` decimal(7,2) DEFAULT NULL, +`ss_ext_tax` decimal(7,2) DEFAULT NULL, +`ss_coupon_amt` decimal(7,2) DEFAULT NULL, +`ss_net_paid` decimal(7,2) DEFAULT NULL, +`ss_net_paid_inc_tax` decimal(7,2) DEFAULT NULL, +`ss_net_profit` decimal(7,2) DEFAULT NULL, +PRIMARY KEY (`ss_item_sk`,`ss_ticket_number`) /*T![clustered_index] NONCLUSTERED */ +); +CREATE TABLE `date_dim` ( +`d_date_sk` int(11) NOT NULL, +`d_date_id` char(16) NOT NULL, +`d_date` date DEFAULT NULL, +`d_month_seq` int(11) DEFAULT NULL, +`d_week_seq` int(11) DEFAULT NULL, +`d_quarter_seq` int(11) DEFAULT NULL, +`d_year` int(11) DEFAULT NULL, +`d_dow` int(11) DEFAULT NULL, +`d_moy` int(11) DEFAULT NULL, +`d_dom` int(11) DEFAULT NULL, +`d_qoy` int(11) DEFAULT NULL, +`d_fy_year` int(11) DEFAULT NULL, +`d_fy_quarter_seq` int(11) DEFAULT NULL, +`d_fy_week_seq` int(11) DEFAULT NULL, +`d_day_name` char(9) DEFAULT NULL, +`d_quarter_name` char(6) DEFAULT NULL, +`d_holiday` char(1) DEFAULT NULL, +`d_weekend` char(1) DEFAULT NULL, +`d_following_holiday` char(1) DEFAULT NULL, +`d_first_dom` int(11) DEFAULT NULL, +`d_last_dom` int(11) DEFAULT NULL, +`d_same_day_ly` int(11) DEFAULT NULL, +`d_same_day_lq` int(11) DEFAULT NULL, +`d_current_day` char(1) DEFAULT NULL, +`d_current_week` char(1) DEFAULT NULL, +`d_current_month` char(1) DEFAULT NULL, +`d_current_quarter` char(1) DEFAULT NULL, +`d_current_year` char(1) DEFAULT NULL, +PRIMARY KEY (`d_date_sk`) /*T![clustered_index] NONCLUSTERED */ +); +CREATE TABLE `web_sales` ( +`ws_sold_date_sk` int(11) DEFAULT NULL, +`ws_sold_time_sk` int(11) DEFAULT NULL, +`ws_ship_date_sk` int(11) DEFAULT NULL, +`ws_item_sk` int(11) NOT NULL, +`ws_bill_customer_sk` int(11) DEFAULT NULL, +`ws_bill_cdemo_sk` int(11) DEFAULT NULL, +`ws_bill_hdemo_sk` int(11) DEFAULT NULL, +`ws_bill_addr_sk` int(11) DEFAULT NULL, +`ws_ship_customer_sk` int(11) DEFAULT NULL, +`ws_ship_cdemo_sk` int(11) DEFAULT NULL, +`ws_ship_hdemo_sk` int(11) DEFAULT NULL, +`ws_ship_addr_sk` int(11) DEFAULT NULL, +`ws_web_page_sk` int(11) DEFAULT NULL, +`ws_web_site_sk` int(11) DEFAULT NULL, +`ws_ship_mode_sk` int(11) DEFAULT NULL, +`ws_warehouse_sk` int(11) DEFAULT NULL, +`ws_promo_sk` int(11) DEFAULT NULL, +`ws_order_number` int(11) NOT NULL, +`ws_quantity` int(11) DEFAULT NULL, +`ws_wholesale_cost` decimal(7,2) DEFAULT NULL, +`ws_list_price` decimal(7,2) DEFAULT NULL, +`ws_sales_price` decimal(7,2) DEFAULT NULL, +`ws_ext_discount_amt` decimal(7,2) DEFAULT NULL, +`ws_ext_sales_price` decimal(7,2) DEFAULT NULL, +`ws_ext_wholesale_cost` decimal(7,2) DEFAULT NULL, +`ws_ext_list_price` decimal(7,2) DEFAULT NULL, +`ws_ext_tax` decimal(7,2) DEFAULT NULL, +`ws_coupon_amt` decimal(7,2) DEFAULT NULL, +`ws_ext_ship_cost` decimal(7,2) DEFAULT NULL, +`ws_net_paid` decimal(7,2) DEFAULT NULL, +`ws_net_paid_inc_tax` decimal(7,2) DEFAULT NULL, +`ws_net_paid_inc_ship` decimal(7,2) DEFAULT NULL, +`ws_net_paid_inc_ship_tax` decimal(7,2) DEFAULT NULL, +`ws_net_profit` decimal(7,2) DEFAULT NULL, +PRIMARY KEY (`ws_item_sk`,`ws_order_number`) /*T![clustered_index] NONCLUSTERED */ +); +desc format='brief' with year_total as ( +select c_customer_id customer_id +,c_first_name customer_first_name +,c_last_name customer_last_name +,c_preferred_cust_flag customer_preferred_cust_flag +,c_birth_country customer_birth_country +,c_login customer_login +,c_email_address customer_email_address +,d_year dyear +,sum(ss_ext_list_price-ss_ext_discount_amt) year_total +,'s' sale_type +from customer +,store_sales +,date_dim +where c_customer_sk = ss_customer_sk +and ss_sold_date_sk = d_date_sk +group by c_customer_id +,c_first_name +,c_last_name +,c_preferred_cust_flag +,c_birth_country +,c_login +,c_email_address +,d_year +union all +select c_customer_id customer_id +,c_first_name customer_first_name +,c_last_name customer_last_name +,c_preferred_cust_flag customer_preferred_cust_flag +,c_birth_country customer_birth_country +,c_login customer_login +,c_email_address customer_email_address +,d_year dyear +,sum(ws_ext_list_price-ws_ext_discount_amt) year_total +,'w' sale_type +from customer +,web_sales +,date_dim +where c_customer_sk = ws_bill_customer_sk +and ws_sold_date_sk = d_date_sk +group by c_customer_id +,c_first_name +,c_last_name +,c_preferred_cust_flag +,c_birth_country +,c_login +,c_email_address +,d_year +) +select +t_s_secyear.customer_id +,t_s_secyear.customer_first_name +,t_s_secyear.customer_last_name +,t_s_secyear.customer_email_address +from year_total t_s_firstyear +,year_total t_s_secyear +,year_total t_w_firstyear +,year_total t_w_secyear +where t_s_secyear.customer_id = t_s_firstyear.customer_id +and t_s_firstyear.customer_id = t_w_secyear.customer_id +and t_s_firstyear.customer_id = t_w_firstyear.customer_id +and t_s_firstyear.sale_type = 's' +and t_w_firstyear.sale_type = 'w' +and t_s_secyear.sale_type = 's' +and t_w_secyear.sale_type = 'w' +and t_s_firstyear.dyear = 2001 +and t_s_secyear.dyear = 2001+1 +and t_w_firstyear.dyear = 2001 +and t_w_secyear.dyear = 2001+1 +and t_s_firstyear.year_total > 0 +and t_w_firstyear.year_total > 0 +and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else 0.0 end +> case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else 0.0 end +order by t_s_secyear.customer_id +,t_s_secyear.customer_first_name +,t_s_secyear.customer_last_name +,t_s_secyear.customer_email_address +limit 100; +id estRows task access object operator info +TopN 40.00 root Column#180, Column#181, Column#182, Column#186, offset:0, count:100 +└─HashJoin 40.00 root inner join, equal:[eq(Column#170, Column#200)], other cond:gt(case(gt(Column#198, 0), div(Column#208, Column#198), 0.0), case(gt(Column#178, 0), div(Column#188, Column#178), 0.0)) + ├─Selection(Build) 40.00 root eq(Column#207, 2002), eq(Column#209, "w"), not(isnull(Column#200)) + │ └─CTEFullScan 50.00 root CTE:t_w_secyear data:CTE_0 + └─HashJoin(Probe) 40.00 root inner join, equal:[eq(Column#170, Column#190)] + ├─Selection(Build) 40.00 root eq(Column#197, 2001), eq(Column#199, "w"), gt(Column#198, 0), not(isnull(Column#190)) + │ └─CTEFullScan 50.00 root CTE:t_w_firstyear data:CTE_0 + └─HashJoin(Probe) 40.00 root inner join, equal:[eq(Column#170, Column#180)] + ├─Selection(Build) 40.00 root eq(Column#187, 2002), eq(Column#189, "s"), not(isnull(Column#180)) + │ └─CTEFullScan 50.00 root CTE:t_s_secyear data:CTE_0 + └─Selection(Probe) 40.00 root eq(Column#177, 2001), eq(Column#179, "s"), gt(Column#178, 0), not(isnull(Column#170)) + └─CTEFullScan 50.00 root CTE:t_s_firstyear data:CTE_0 +CTE_0 50.00 root Non-Recursive CTE +└─Union(Seed Part) 50.00 root + ├─Projection 25.00 root test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.date_dim.d_year, Column#73, s->Column#169 + │ └─Selection 25.00 root or(or(and(1, and(eq(test.date_dim.d_year, 2001), gt(Column#73, 0))), and(1, eq(test.date_dim.d_year, 2002))), 0) + │ └─HashAgg 31.25 root group by:Column#233, Column#234, Column#235, Column#236, Column#237, Column#238, Column#239, Column#240, funcs:sum(Column#224)->Column#73, funcs:firstrow(Column#225)->test.customer.c_customer_id, funcs:firstrow(Column#226)->test.customer.c_first_name, funcs:firstrow(Column#227)->test.customer.c_last_name, funcs:firstrow(Column#228)->test.customer.c_preferred_cust_flag, funcs:firstrow(Column#229)->test.customer.c_birth_country, funcs:firstrow(Column#230)->test.customer.c_login, funcs:firstrow(Column#231)->test.customer.c_email_address, funcs:firstrow(Column#232)->test.date_dim.d_year + │ └─Projection 31.25 root minus(test.store_sales.ss_ext_list_price, test.store_sales.ss_ext_discount_amt)->Column#224, test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.date_dim.d_year, test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.date_dim.d_year + │ └─Projection 31.25 root test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.store_sales.ss_ext_discount_amt, test.store_sales.ss_ext_list_price, test.date_dim.d_year + │ └─IndexJoin 31.25 root inner join, inner:IndexLookUp, outer key:test.store_sales.ss_customer_sk, inner key:test.customer.c_customer_sk, equal cond:eq(test.store_sales.ss_customer_sk, test.customer.c_customer_sk) + │ ├─HashJoin(Build) 25.00 root inner join, equal:[eq(test.date_dim.d_date_sk, test.store_sales.ss_sold_date_sk)] + │ │ ├─TableReader(Build) 20.00 root data:Selection + │ │ │ └─Selection 20.00 cop[tikv] or(and(1, eq(test.date_dim.d_year, 2001)), or(and(1, eq(test.date_dim.d_year, 2002)), 0)) + │ │ │ └─TableFullScan 10000.00 cop[tikv] table:date_dim keep order:false, stats:pseudo + │ │ └─TableReader(Probe) 9980.01 root data:Selection + │ │ └─Selection 9980.01 cop[tikv] not(isnull(test.store_sales.ss_customer_sk)), not(isnull(test.store_sales.ss_sold_date_sk)) + │ │ └─TableFullScan 10000.00 cop[tikv] table:store_sales keep order:false, stats:pseudo + │ └─IndexLookUp(Probe) 1.00 root + │ ├─IndexRangeScan(Build) 1.00 cop[tikv] table:customer, index:PRIMARY(c_customer_sk) range: decided by [eq(test.customer.c_customer_sk, test.store_sales.ss_customer_sk)], keep order:false, stats:pseudo + │ └─TableRowIDScan(Probe) 1.00 cop[tikv] table:customer keep order:false, stats:pseudo + └─Projection 25.00 root test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.date_dim.d_year, Column#158, w->Column#169 + └─Selection 25.00 root or(0, or(and(1, and(eq(test.date_dim.d_year, 2001), gt(Column#158, 0))), and(1, eq(test.date_dim.d_year, 2002)))) + └─HashAgg 31.25 root group by:Column#250, Column#251, Column#252, Column#253, Column#254, Column#255, Column#256, Column#257, funcs:sum(Column#241)->Column#158, funcs:firstrow(Column#242)->test.customer.c_customer_id, funcs:firstrow(Column#243)->test.customer.c_first_name, funcs:firstrow(Column#244)->test.customer.c_last_name, funcs:firstrow(Column#245)->test.customer.c_preferred_cust_flag, funcs:firstrow(Column#246)->test.customer.c_birth_country, funcs:firstrow(Column#247)->test.customer.c_login, funcs:firstrow(Column#248)->test.customer.c_email_address, funcs:firstrow(Column#249)->test.date_dim.d_year + └─Projection 31.25 root minus(test.web_sales.ws_ext_list_price, test.web_sales.ws_ext_discount_amt)->Column#241, test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.date_dim.d_year, test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.date_dim.d_year + └─Projection 31.25 root test.customer.c_customer_id, test.customer.c_first_name, test.customer.c_last_name, test.customer.c_preferred_cust_flag, test.customer.c_birth_country, test.customer.c_login, test.customer.c_email_address, test.web_sales.ws_ext_discount_amt, test.web_sales.ws_ext_list_price, test.date_dim.d_year + └─IndexJoin 31.25 root inner join, inner:IndexLookUp, outer key:test.web_sales.ws_bill_customer_sk, inner key:test.customer.c_customer_sk, equal cond:eq(test.web_sales.ws_bill_customer_sk, test.customer.c_customer_sk) + ├─HashJoin(Build) 25.00 root inner join, equal:[eq(test.date_dim.d_date_sk, test.web_sales.ws_sold_date_sk)] + │ ├─TableReader(Build) 20.00 root data:Selection + │ │ └─Selection 20.00 cop[tikv] or(0, or(and(1, eq(test.date_dim.d_year, 2001)), and(1, eq(test.date_dim.d_year, 2002)))) + │ │ └─TableFullScan 10000.00 cop[tikv] table:date_dim keep order:false, stats:pseudo + │ └─TableReader(Probe) 9980.01 root data:Selection + │ └─Selection 9980.01 cop[tikv] not(isnull(test.web_sales.ws_bill_customer_sk)), not(isnull(test.web_sales.ws_sold_date_sk)) + │ └─TableFullScan 10000.00 cop[tikv] table:web_sales keep order:false, stats:pseudo + └─IndexLookUp(Probe) 1.00 root + ├─IndexRangeScan(Build) 1.00 cop[tikv] table:customer, index:PRIMARY(c_customer_sk) range: decided by [eq(test.customer.c_customer_sk, test.web_sales.ws_bill_customer_sk)], keep order:false, stats:pseudo + └─TableRowIDScan(Probe) 1.00 cop[tikv] table:customer keep order:false, stats:pseudo +drop table if exists t1; +create table t1 (id int, bench_type varchar(10),version varchar(10),tps int(20)); +insert into t1 (id,bench_type,version,tps) values (1,'sysbench','5.4.0',1111111); +insert into t1 (id,bench_type,version,tps) values (2,'sysbench','6.0.0',222222); +with all_data as +(select * from t1 +),version1 as (select * from all_data where version ='5.4.0' +),version2 as(select * from all_data where version ='6.0.0') +select v1.tps v1_tps,v2.tps v2_tps +from version1 v1, version2 v2 +where v1.bench_type =v2.bench_type; +v1_tps v2_tps +1111111 222222 +desc format='brief' with all_data as +(select * from t1 +),version1 as (select * from all_data where version ='5.4.0' +),version2 as(select * from all_data where version ='6.0.0') +select v1.tps v1_tps,v2.tps v2_tps +from version1 v1, version2 v2 +where v1.bench_type =v2.bench_type; +id estRows task access object operator info +HashJoin 8000.00 root inner join, equal:[eq(test.t1.bench_type, test.t1.bench_type)] +├─Selection(Build) 6400.00 root not(isnull(test.t1.bench_type)) +│ └─CTEFullScan 8000.00 root CTE:v2 data:CTE_2 +└─Selection(Probe) 6400.00 root not(isnull(test.t1.bench_type)) + └─CTEFullScan 8000.00 root CTE:v1 data:CTE_1 +CTE_2 8000.00 root Non-Recursive CTE +└─Selection(Seed Part) 8000.00 root eq(test.t1.version, "6.0.0"), not(isnull(test.t1.bench_type)) + └─CTEFullScan 10000.00 root CTE:all_data data:CTE_0 +CTE_1 8000.00 root Non-Recursive CTE +└─Selection(Seed Part) 8000.00 root eq(test.t1.version, "5.4.0"), not(isnull(test.t1.bench_type)) + └─CTEFullScan 10000.00 root CTE:all_data data:CTE_0 +CTE_0 10000.00 root Non-Recursive CTE +└─TableReader(Seed Part) 10000.00 root data:TableFullScan + └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo +drop table if exists tbl; +create table tbl (id int); +explain with t1 as (select id from tbl), t2 as (select a.id from t1 a join t1 b on a.id = b.id) select * from t2 where id in (select id from t2); +id estRows task access object operator info +HashJoin_33 8000.00 root inner join, equal:[eq(test.tbl.id, test.tbl.id)] +├─HashAgg_37(Build) 5120.00 root group by:test.tbl.id, funcs:firstrow(test.tbl.id)->test.tbl.id +│ └─Selection_38 8000.00 root not(isnull(test.tbl.id)) +│ └─CTEFullScan_39 10000.00 root CTE:t2 data:CTE_1 +└─Selection_35(Probe) 8000.00 root not(isnull(test.tbl.id)) + └─CTEFullScan_36 10000.00 root CTE:t2 data:CTE_1 +CTE_1 10000.00 root Non-Recursive CTE +└─HashJoin_25(Seed Part) 10000.00 root inner join, equal:[eq(test.tbl.id, test.tbl.id)] + ├─Selection_29(Build) 8000.00 root not(isnull(test.tbl.id)) + │ └─CTEFullScan_30 10000.00 root CTE:b data:CTE_0 + └─Selection_27(Probe) 8000.00 root not(isnull(test.tbl.id)) + └─CTEFullScan_28 10000.00 root CTE:a data:CTE_0 +CTE_0 10000.00 root Non-Recursive CTE +└─TableReader_22(Seed Part) 10000.00 root data:TableFullScan_21 + └─TableFullScan_21 10000.00 cop[tikv] table:tbl keep order:false, stats:pseudo +drop table if exists t1, t2, t3; +create table t1 (a int, b int); +create table t2 (c int, d int); +create table t3 (e int, f int); +insert into t1 values(1,1); +insert into t2 values(1,1); +insert into t3 values(1,1234); +explain update t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c set t1.b = 4321; +id estRows task access object operator info +Update_14 N/A root N/A +└─HashJoin_25 12.49 root inner join, equal:[eq(test.t1.a, test.t2.c)] + ├─HashJoin_33(Build) 9.99 root inner join, equal:[eq(test.t3.e, test.t2.d)] + │ ├─Selection_35(Build) 7.99 root not(isnull(test.t3.e)) + │ │ └─CTEFullScan_36 9.99 root CTE:temp data:CTE_0 + │ └─TableReader_39(Probe) 9980.01 root data:Selection_38 + │ └─Selection_38 9980.01 cop[tikv] not(isnull(test.t2.c)), not(isnull(test.t2.d)) + │ └─TableFullScan_37 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo + └─TableReader_29(Probe) 9990.00 root data:Selection_28 + └─Selection_28 9990.00 cop[tikv] not(isnull(test.t1.a)) + └─TableFullScan_27 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo +CTE_0 9.99 root Non-Recursive CTE +└─Projection_17(Seed Part) 9.99 root test.t3.e + └─TableReader_20 9.99 root data:Selection_19 + └─Selection_19 9.99 cop[tikv] eq(test.t3.f, 1234), not(isnull(test.t3.e)) + └─TableFullScan_18 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo +update t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c set t1.b = 4321; +select * from t1; +a b +1 4321 +explain insert into t1 select t1.a, t1.b from t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +id estRows task access object operator info +Insert_1 N/A root N/A +└─HashJoin_28 12.49 root inner join, equal:[eq(test.t2.c, test.t1.a)] + ├─HashJoin_30(Build) 9.99 root inner join, equal:[eq(test.t3.e, test.t2.d)] + │ ├─Selection_31(Build) 7.99 root not(isnull(test.t3.e)) + │ │ └─CTEFullScan_32 9.99 root CTE:temp data:CTE_0 + │ └─TableReader_35(Probe) 9980.01 root data:Selection_34 + │ └─Selection_34 9980.01 cop[tikv] not(isnull(test.t2.c)), not(isnull(test.t2.d)) + │ └─TableFullScan_33 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo + └─TableReader_38(Probe) 9990.00 root data:Selection_37 + └─Selection_37 9990.00 cop[tikv] not(isnull(test.t1.a)) + └─TableFullScan_36 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo +CTE_0 9.99 root Non-Recursive CTE +└─Projection_18(Seed Part) 9.99 root test.t3.e + └─TableReader_21 9.99 root data:Selection_20 + └─Selection_20 9.99 cop[tikv] eq(test.t3.f, 1234), not(isnull(test.t3.e)) + └─TableFullScan_19 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo +insert into t1 select t1.a, t1.b from t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +select * from t1; +a b +1 4321 +1 4321 +explain delete from t1 using t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +id estRows task access object operator info +Delete_14 N/A root N/A +└─Projection_25 12.49 root test.t1.a, test.t1.b, test.t1._tidb_rowid, test.t2.c + └─HashJoin_27 12.49 root inner join, equal:[eq(test.t2.c, test.t1.a)] + ├─HashJoin_29(Build) 9.99 root inner join, equal:[eq(test.t3.e, test.t2.d)] + │ ├─Selection_30(Build) 7.99 root not(isnull(test.t3.e)) + │ │ └─CTEFullScan_31 9.99 root CTE:temp data:CTE_0 + │ └─TableReader_34(Probe) 9980.01 root data:Selection_33 + │ └─Selection_33 9980.01 cop[tikv] not(isnull(test.t2.c)), not(isnull(test.t2.d)) + │ └─TableFullScan_32 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo + └─TableReader_37(Probe) 9990.00 root data:Selection_36 + └─Selection_36 9990.00 cop[tikv] not(isnull(test.t1.a)) + └─TableFullScan_35 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo +CTE_0 9.99 root Non-Recursive CTE +└─Projection_17(Seed Part) 9.99 root test.t3.e + └─TableReader_20 9.99 root data:Selection_19 + └─Selection_19 9.99 cop[tikv] eq(test.t3.f, 1234), not(isnull(test.t3.e)) + └─TableFullScan_18 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo +delete from t1 using t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +select * from t1; +a b +>>>>>>> 1b848f40e... planner: just pop cte's handleHelper map out since it shouldn't be considered (#35854) diff --git a/cmd/explaintest/t/explain_cte.test b/cmd/explaintest/t/explain_cte.test index c657ad5c68898..a7217a4cdb19d 100644 --- a/cmd/explaintest/t/explain_cte.test +++ b/cmd/explaintest/t/explain_cte.test @@ -42,3 +42,245 @@ explain with recursive cte1(c1) as (select c1 from t1 union select c1 + 1 c1 fro explain with recursive cte1(c1) as (select c1 from t1 union select c1 from t2 limit 1) select * from cte1; explain with recursive cte1(c1) as (select c1 from t1 union select c1 from t2 limit 100 offset 100) select * from cte1; explain with recursive cte1(c1) as (select c1 from t1 union select c1 from t2 limit 0 offset 0) select * from cte1; +<<<<<<< HEAD +======= + +# TPC-DS Q11 +CREATE TABLE `customer` ( + `c_customer_sk` int(11) NOT NULL, + `c_customer_id` char(16) NOT NULL, + `c_current_cdemo_sk` int(11) DEFAULT NULL, + `c_current_hdemo_sk` int(11) DEFAULT NULL, + `c_current_addr_sk` int(11) DEFAULT NULL, + `c_first_shipto_date_sk` int(11) DEFAULT NULL, + `c_first_sales_date_sk` int(11) DEFAULT NULL, + `c_salutation` char(10) DEFAULT NULL, + `c_first_name` char(20) DEFAULT NULL, + `c_last_name` char(30) DEFAULT NULL, + `c_preferred_cust_flag` char(1) DEFAULT NULL, + `c_birth_day` int(11) DEFAULT NULL, + `c_birth_month` int(11) DEFAULT NULL, + `c_birth_year` int(11) DEFAULT NULL, + `c_birth_country` varchar(20) DEFAULT NULL, + `c_login` char(13) DEFAULT NULL, + `c_email_address` char(50) DEFAULT NULL, + `c_last_review_date_sk` int(11) DEFAULT NULL, + PRIMARY KEY (`c_customer_sk`) /*T![clustered_index] NONCLUSTERED */ +); +CREATE TABLE `store_sales` ( + `ss_sold_date_sk` int(11) DEFAULT NULL, + `ss_sold_time_sk` int(11) DEFAULT NULL, + `ss_item_sk` int(11) NOT NULL, + `ss_customer_sk` int(11) DEFAULT NULL, + `ss_cdemo_sk` int(11) DEFAULT NULL, + `ss_hdemo_sk` int(11) DEFAULT NULL, + `ss_addr_sk` int(11) DEFAULT NULL, + `ss_store_sk` int(11) DEFAULT NULL, + `ss_promo_sk` int(11) DEFAULT NULL, + `ss_ticket_number` int(11) NOT NULL, + `ss_quantity` int(11) DEFAULT NULL, + `ss_wholesale_cost` decimal(7,2) DEFAULT NULL, + `ss_list_price` decimal(7,2) DEFAULT NULL, + `ss_sales_price` decimal(7,2) DEFAULT NULL, + `ss_ext_discount_amt` decimal(7,2) DEFAULT NULL, + `ss_ext_sales_price` decimal(7,2) DEFAULT NULL, + `ss_ext_wholesale_cost` decimal(7,2) DEFAULT NULL, + `ss_ext_list_price` decimal(7,2) DEFAULT NULL, + `ss_ext_tax` decimal(7,2) DEFAULT NULL, + `ss_coupon_amt` decimal(7,2) DEFAULT NULL, + `ss_net_paid` decimal(7,2) DEFAULT NULL, + `ss_net_paid_inc_tax` decimal(7,2) DEFAULT NULL, + `ss_net_profit` decimal(7,2) DEFAULT NULL, + PRIMARY KEY (`ss_item_sk`,`ss_ticket_number`) /*T![clustered_index] NONCLUSTERED */ +); +CREATE TABLE `date_dim` ( + `d_date_sk` int(11) NOT NULL, + `d_date_id` char(16) NOT NULL, + `d_date` date DEFAULT NULL, + `d_month_seq` int(11) DEFAULT NULL, + `d_week_seq` int(11) DEFAULT NULL, + `d_quarter_seq` int(11) DEFAULT NULL, + `d_year` int(11) DEFAULT NULL, + `d_dow` int(11) DEFAULT NULL, + `d_moy` int(11) DEFAULT NULL, + `d_dom` int(11) DEFAULT NULL, + `d_qoy` int(11) DEFAULT NULL, + `d_fy_year` int(11) DEFAULT NULL, + `d_fy_quarter_seq` int(11) DEFAULT NULL, + `d_fy_week_seq` int(11) DEFAULT NULL, + `d_day_name` char(9) DEFAULT NULL, + `d_quarter_name` char(6) DEFAULT NULL, + `d_holiday` char(1) DEFAULT NULL, + `d_weekend` char(1) DEFAULT NULL, + `d_following_holiday` char(1) DEFAULT NULL, + `d_first_dom` int(11) DEFAULT NULL, + `d_last_dom` int(11) DEFAULT NULL, + `d_same_day_ly` int(11) DEFAULT NULL, + `d_same_day_lq` int(11) DEFAULT NULL, + `d_current_day` char(1) DEFAULT NULL, + `d_current_week` char(1) DEFAULT NULL, + `d_current_month` char(1) DEFAULT NULL, + `d_current_quarter` char(1) DEFAULT NULL, + `d_current_year` char(1) DEFAULT NULL, + PRIMARY KEY (`d_date_sk`) /*T![clustered_index] NONCLUSTERED */ +); +CREATE TABLE `web_sales` ( + `ws_sold_date_sk` int(11) DEFAULT NULL, + `ws_sold_time_sk` int(11) DEFAULT NULL, + `ws_ship_date_sk` int(11) DEFAULT NULL, + `ws_item_sk` int(11) NOT NULL, + `ws_bill_customer_sk` int(11) DEFAULT NULL, + `ws_bill_cdemo_sk` int(11) DEFAULT NULL, + `ws_bill_hdemo_sk` int(11) DEFAULT NULL, + `ws_bill_addr_sk` int(11) DEFAULT NULL, + `ws_ship_customer_sk` int(11) DEFAULT NULL, + `ws_ship_cdemo_sk` int(11) DEFAULT NULL, + `ws_ship_hdemo_sk` int(11) DEFAULT NULL, + `ws_ship_addr_sk` int(11) DEFAULT NULL, + `ws_web_page_sk` int(11) DEFAULT NULL, + `ws_web_site_sk` int(11) DEFAULT NULL, + `ws_ship_mode_sk` int(11) DEFAULT NULL, + `ws_warehouse_sk` int(11) DEFAULT NULL, + `ws_promo_sk` int(11) DEFAULT NULL, + `ws_order_number` int(11) NOT NULL, + `ws_quantity` int(11) DEFAULT NULL, + `ws_wholesale_cost` decimal(7,2) DEFAULT NULL, + `ws_list_price` decimal(7,2) DEFAULT NULL, + `ws_sales_price` decimal(7,2) DEFAULT NULL, + `ws_ext_discount_amt` decimal(7,2) DEFAULT NULL, + `ws_ext_sales_price` decimal(7,2) DEFAULT NULL, + `ws_ext_wholesale_cost` decimal(7,2) DEFAULT NULL, + `ws_ext_list_price` decimal(7,2) DEFAULT NULL, + `ws_ext_tax` decimal(7,2) DEFAULT NULL, + `ws_coupon_amt` decimal(7,2) DEFAULT NULL, + `ws_ext_ship_cost` decimal(7,2) DEFAULT NULL, + `ws_net_paid` decimal(7,2) DEFAULT NULL, + `ws_net_paid_inc_tax` decimal(7,2) DEFAULT NULL, + `ws_net_paid_inc_ship` decimal(7,2) DEFAULT NULL, + `ws_net_paid_inc_ship_tax` decimal(7,2) DEFAULT NULL, + `ws_net_profit` decimal(7,2) DEFAULT NULL, + PRIMARY KEY (`ws_item_sk`,`ws_order_number`) /*T![clustered_index] NONCLUSTERED */ +); +desc format='brief' with year_total as ( + select c_customer_id customer_id + ,c_first_name customer_first_name + ,c_last_name customer_last_name + ,c_preferred_cust_flag customer_preferred_cust_flag + ,c_birth_country customer_birth_country + ,c_login customer_login + ,c_email_address customer_email_address + ,d_year dyear + ,sum(ss_ext_list_price-ss_ext_discount_amt) year_total + ,'s' sale_type + from customer + ,store_sales + ,date_dim + where c_customer_sk = ss_customer_sk + and ss_sold_date_sk = d_date_sk + group by c_customer_id + ,c_first_name + ,c_last_name + ,c_preferred_cust_flag + ,c_birth_country + ,c_login + ,c_email_address + ,d_year + union all + select c_customer_id customer_id + ,c_first_name customer_first_name + ,c_last_name customer_last_name + ,c_preferred_cust_flag customer_preferred_cust_flag + ,c_birth_country customer_birth_country + ,c_login customer_login + ,c_email_address customer_email_address + ,d_year dyear + ,sum(ws_ext_list_price-ws_ext_discount_amt) year_total + ,'w' sale_type + from customer + ,web_sales + ,date_dim + where c_customer_sk = ws_bill_customer_sk + and ws_sold_date_sk = d_date_sk + group by c_customer_id + ,c_first_name + ,c_last_name + ,c_preferred_cust_flag + ,c_birth_country + ,c_login + ,c_email_address + ,d_year + ) + select + t_s_secyear.customer_id + ,t_s_secyear.customer_first_name + ,t_s_secyear.customer_last_name + ,t_s_secyear.customer_email_address + from year_total t_s_firstyear + ,year_total t_s_secyear + ,year_total t_w_firstyear + ,year_total t_w_secyear + where t_s_secyear.customer_id = t_s_firstyear.customer_id + and t_s_firstyear.customer_id = t_w_secyear.customer_id + and t_s_firstyear.customer_id = t_w_firstyear.customer_id + and t_s_firstyear.sale_type = 's' + and t_w_firstyear.sale_type = 'w' + and t_s_secyear.sale_type = 's' + and t_w_secyear.sale_type = 'w' + and t_s_firstyear.dyear = 2001 + and t_s_secyear.dyear = 2001+1 + and t_w_firstyear.dyear = 2001 + and t_w_secyear.dyear = 2001+1 + and t_s_firstyear.year_total > 0 + and t_w_firstyear.year_total > 0 + and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else 0.0 end + > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else 0.0 end + order by t_s_secyear.customer_id + ,t_s_secyear.customer_first_name + ,t_s_secyear.customer_last_name + ,t_s_secyear.customer_email_address +limit 100; + + +# predicate pushdown +drop table if exists t1; +create table t1 (id int, bench_type varchar(10),version varchar(10),tps int(20)); +insert into t1 (id,bench_type,version,tps) values (1,'sysbench','5.4.0',1111111); +insert into t1 (id,bench_type,version,tps) values (2,'sysbench','6.0.0',222222); +with all_data as +(select * from t1 +),version1 as (select * from all_data where version ='5.4.0' +),version2 as(select * from all_data where version ='6.0.0') +select v1.tps v1_tps,v2.tps v2_tps +from version1 v1, version2 v2 +where v1.bench_type =v2.bench_type; +desc format='brief' with all_data as +(select * from t1 +),version1 as (select * from all_data where version ='5.4.0' +),version2 as(select * from all_data where version ='6.0.0') +select v1.tps v1_tps,v2.tps v2_tps +from version1 v1, version2 v2 +where v1.bench_type =v2.bench_type; + +# issue 35404 +drop table if exists tbl; +create table tbl (id int); +explain with t1 as (select id from tbl), t2 as (select a.id from t1 a join t1 b on a.id = b.id) select * from t2 where id in (select id from t2); + +# issue 35758 +drop table if exists t1, t2, t3; +create table t1 (a int, b int); +create table t2 (c int, d int); +create table t3 (e int, f int); +insert into t1 values(1,1); +insert into t2 values(1,1); +insert into t3 values(1,1234); +explain update t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c set t1.b = 4321; +update t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c set t1.b = 4321; +select * from t1; +explain insert into t1 select t1.a, t1.b from t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +insert into t1 select t1.a, t1.b from t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +select * from t1; +explain delete from t1 using t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +delete from t1 using t1 inner join (select t2.c from t2 inner join (with temp as (select e from t3 where t3.f = 1234) select e from temp) tt on t2.d = tt.e) t on t1.a = t.c; +select * from t1; +>>>>>>> 1b848f40e... planner: just pop cte's handleHelper map out since it shouldn't be considered (#35854) diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index f360722e4dd9b..d0c06f7f42301 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -6166,6 +6166,8 @@ func (b *PlanBuilder) buildWith(ctx context.Context, w *ast.WithClause) error { b.outerCTEs[len(b.outerCTEs)-1].optFlag = b.optFlag b.outerCTEs[len(b.outerCTEs)-1].isBuilding = false b.optFlag = saveFlag + // each cte (select statement) will generate a handle map, pop it out here. + b.handleHelper.popMap() } return nil }