use test;
drop table if exists t1, t2, t3;
create table t1 (c1 int primary key, c2 int, c3 int, index c2 (c2));
load stats 's/explain_easy_stats_t1.json';
create table t2 (c1 int unique, c2 int);
load stats 's/explain_easy_stats_t2.json';
create table t3 (a bigint, b bigint, c bigint, d bigint);
load stats 's/explain_easy_stats_t3.json';
create table index_prune(a bigint(20) NOT NULL, b bigint(20) NOT NULL, c tinyint(4) NOT NULL, primary key(a, b), index idx_b_c_a(b, c, a));
load stats 's/explain_easy_stats_index_prune.json';
set @@session.tidb_opt_agg_push_down = 1;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
set @@session.tidb_hashagg_partial_concurrency = 1;
set @@session.tidb_hashagg_final_concurrency = 1;
explain select * from t3 where exists (select s.a from t3 s having sum(s.a) = t3.a );
id	count	task	operator info
Projection_11	1600.00	root	test.t3.a, test.t3.b, test.t3.c, test.t3.d
└─HashLeftJoin_12	1600.00	root	semi join, inner:StreamAgg_27, equal:[eq(cast(test.t3.a), sel_agg_1)]
  ├─Projection_13	2000.00	root	test.t3.a, test.t3.b, test.t3.c, test.t3.d, cast(test.t3.a)
  │ └─TableReader_15	2000.00	root	data:TableScan_14
  │   └─TableScan_14	2000.00	cop	table:t3, range:[-inf,+inf], keep order:false
  └─StreamAgg_27	1.00	root	funcs:sum(col_0)
    └─TableReader_28	1.00	root	data:StreamAgg_19
      └─StreamAgg_19	1.00	cop	funcs:sum(test.s.a)
        └─TableScan_26	2000.00	cop	table:s, range:[-inf,+inf], keep order:false
explain select * from t1;
id	count	task	operator info
TableReader_5	1999.00	root	data:TableScan_4
└─TableScan_4	1999.00	cop	table:t1, range:[-inf,+inf], keep order:false
explain select * from t1 order by c2;
id	count	task	operator info
IndexLookUp_12	1999.00	root	
├─IndexScan_10	1999.00	cop	table:t1, index:c2, range:[NULL,+inf], keep order:true
└─TableScan_11	1999.00	cop	table:t1, keep order:false
explain select * from t2 order by c2;
id	count	task	operator info
Sort_4	1985.00	root	test.t2.c2:asc
└─TableReader_8	1985.00	root	data:TableScan_7
  └─TableScan_7	1985.00	cop	table:t2, range:[-inf,+inf], keep order:false
explain select * from t1 where t1.c1 > 0;
id	count	task	operator info
TableReader_6	1999.00	root	data:TableScan_5
└─TableScan_5	1999.00	cop	table:t1, range:(0,+inf], keep order:false
explain select t1.c1, t1.c2 from t1 where t1.c2 = 1;
id	count	task	operator info
IndexReader_6	0.00	root	index:IndexScan_5
└─IndexScan_5	0.00	cop	table:t1, index:c2, range:[1,1], keep order:false
explain select * from t1 left join t2 on t1.c2 = t2.c1 where t1.c1 > 1;
id	count	task	operator info
HashLeftJoin_13	2481.25	root	left outer join, inner:TableReader_26, equal:[eq(test.t1.c2, test.t2.c1)]
├─TableReader_23	1998.00	root	data:TableScan_22
│ └─TableScan_22	1998.00	cop	table:t1, range:(1,+inf], keep order:false
└─TableReader_26	1985.00	root	data:Selection_25
  └─Selection_25	1985.00	cop	not(isnull(test.t2.c1))
    └─TableScan_24	1985.00	cop	table:t2, range:[-inf,+inf], keep order:false
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
id	count	task	operator info
Point_Get_1	1.00	root	table:t1, handle:1
explain delete from t1 where t1.c2 = 1;
id	count	task	operator info
IndexLookUp_9	0.00	root	
├─IndexScan_7	0.00	cop	table:t1, index:c2, range:[1,1], keep order:false
└─TableScan_8	0.00	cop	table:t1, keep order:false
explain select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
id	count	task	operator info
Projection_11	1985.00	root	cast(join_agg_0)
└─HashLeftJoin_15	1985.00	root	inner join, inner:HashAgg_22, equal:[eq(test.a.c1, test.b.c2)]
  ├─TableReader_28	1999.00	root	data:TableScan_27
  │ └─TableScan_27	1999.00	cop	table:a, range:[-inf,+inf], keep order:false
  └─HashAgg_22	1985.00	root	group by:col_2, funcs:count(col_0), firstrow(col_2)
    └─TableReader_23	1985.00	root	data:HashAgg_17
      └─HashAgg_17	1985.00	cop	group by:test.b.c2, funcs:count(test.b.c2)
        └─Selection_21	1985.00	cop	not(isnull(test.b.c2))
          └─TableScan_20	1985.00	cop	table:b, range:[-inf,+inf], keep order:false
explain select * from t2 order by t2.c2 limit 0, 1;
id	count	task	operator info
TopN_7	1.00	root	test.t2.c2:asc, offset:0, count:1
└─TableReader_15	1.00	root	data:TopN_14
  └─TopN_14	1.00	cop	test.t2.c2:asc, offset:0, count:1
    └─TableScan_13	1985.00	cop	table:t2, range:[-inf,+inf], keep order:false
explain select * from t1 where c1 > 1 and c2 = 1 and c3 < 1;
id	count	task	operator info
IndexLookUp_11	0.00	root	
├─IndexScan_8	0.00	cop	table:t1, index:c2, range:(1 1,1 +inf], keep order:false
└─Selection_10	0.00	cop	lt(test.t1.c3, 1)
  └─TableScan_9	0.00	cop	table:t1, keep order:false
explain select * from t1 where c1 = 1 and c2 > 1;
id	count	task	operator info
TableReader_7	0.50	root	data:Selection_6
└─Selection_6	0.50	cop	gt(test.t1.c2, 1)
  └─TableScan_5	1.00	cop	table:t1, range:[1,1], keep order:false
explain select c1 from t1 where c1 in (select c2 from t2);
id	count	task	operator info
Projection_9	1985.00	root	test.t1.c1
└─HashLeftJoin_13	1985.00	root	inner join, inner:HashAgg_17, equal:[eq(test.t1.c1, test.t2.c2)]
  ├─TableReader_26	1999.00	root	data:TableScan_25
  │ └─TableScan_25	1999.00	cop	table:t1, range:[-inf,+inf], keep order:false
  └─HashAgg_17	1985.00	root	group by:test.t2.c2, funcs:firstrow(test.t2.c2)
    └─TableReader_24	1985.00	root	data:Selection_23
      └─Selection_23	1985.00	cop	not(isnull(test.t2.c2))
        └─TableScan_22	1985.00	cop	table:t2, range:[-inf,+inf], keep order:false
explain select * from information_schema.columns;
id	count	task	operator info
MemTableScan_4	10000.00	root	
explain select c2 = (select c2 from t2 where t1.c1 = t2.c1 order by c1 limit 1) from t1;
id	count	task	operator info
Projection_12	1999.00	root	eq(test.t1.c2, test.t2.c2)
└─Apply_14	1999.00	root	CARTESIAN left outer join, inner:Limit_21
  ├─TableReader_16	1999.00	root	data:TableScan_15
  │ └─TableScan_15	1999.00	cop	table:t1, range:[-inf,+inf], keep order:false
  └─Limit_21	1.00	root	offset:0, count:1
    └─Projection_41	1.00	root	test.t2.c1, test.t2.c2
      └─IndexLookUp_40	1.00	root	
        ├─Limit_39	1.00	cop	offset:0, count:1
        │ └─IndexScan_37	1.25	cop	table:t2, index:c1, range: decided by [eq(test.t1.c1, test.t2.c1)], keep order:true
        └─TableScan_38	1.00	cop	table:t2, keep order:false, stats:pseudo
explain select * from t1 order by c1 desc limit 1;
id	count	task	operator info
Limit_10	1.00	root	offset:0, count:1
└─TableReader_20	1.00	root	data:Limit_19
  └─Limit_19	1.00	cop	offset:0, count:1
    └─TableScan_18	1.00	cop	table:t1, range:[-inf,+inf], keep order:true, desc
set @@session.tidb_opt_insubq_to_join_and_agg=0;
explain select 1 in (select c2 from t2) from t1;
id	count	task	operator info
Projection_6	1999.00	root	5_aux_0
└─HashLeftJoin_7	1999.00	root	CARTESIAN left outer semi join, inner:TableReader_12
  ├─TableReader_9	1999.00	root	data:TableScan_8
  │ └─TableScan_8	1999.00	cop	table:t1, range:[-inf,+inf], keep order:false
  └─TableReader_12	0.00	root	data:Selection_11
    └─Selection_11	0.00	cop	eq(1, test.t2.c2)
      └─TableScan_10	1985.00	cop	table:t2, range:[-inf,+inf], keep order:false
explain format="dot" select 1 in (select c2 from t2) from t1;
dot contents

digraph Projection_6 {
subgraph cluster6{
node [style=filled, color=lightgrey]
color=black
label = "root"
"Projection_6" -> "HashLeftJoin_7"
"HashLeftJoin_7" -> "TableReader_9"
"HashLeftJoin_7" -> "TableReader_12"
}
subgraph cluster8{
node [style=filled, color=lightgrey]
color=black
label = "cop"
"TableScan_8"
}
subgraph cluster11{
node [style=filled, color=lightgrey]
color=black
label = "cop"
"Selection_11" -> "TableScan_10"
}
"TableReader_9" -> "TableScan_8"
"TableReader_12" -> "Selection_11"
}

explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 1;
id	count	task	operator info
Point_Get_1	1.00	root	table:index_prune, index:a b
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 0;
id	count	task	operator info
TableDual_5	0.00	root	rows:0
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 1, 1;
id	count	task	operator info
Limit_9	1.00	root	offset:1, count:1
└─IndexLookUp_14	1.00	root	
  ├─Limit_13	1.00	cop	offset:0, count:2
  │ └─IndexScan_11	1.00	cop	table:index_prune, index:a, b, range:[1010010404050976781 26467085526790,1010010404050976781 26467085526790], keep order:false
  └─TableScan_12	1.00	cop	table:index_prune, keep order:false, stats:pseudo
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 1, 0;
id	count	task	operator info
Limit_9	0.00	root	offset:1, count:0
└─IndexLookUp_14	1.00	root	
  ├─Limit_13	1.00	cop	offset:0, count:1
  │ └─IndexScan_11	1.00	cop	table:index_prune, index:a, b, range:[1010010404050976781 26467085526790,1010010404050976781 26467085526790], keep order:false
  └─TableScan_12	1.00	cop	table:index_prune, keep order:false, stats:pseudo
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 LIMIT 0, 1;
id	count	task	operator info
Point_Get_1	1.00	root	table:index_prune, index:a b
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 ORDER BY a;
id	count	task	operator info
Point_Get_1	1.00	root	table:index_prune, index:a b
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 GROUP BY b;
id	count	task	operator info
Point_Get_1	1.00	root	table:index_prune, index:a b
explain select * from index_prune WHERE a = 1010010404050976781 AND b = 26467085526790 GROUP BY b ORDER BY a limit 1;
id	count	task	operator info
Point_Get_1	1.00	root	table:index_prune, index:a b
drop table if exists t1, t2, t3, index_prune;
set @@session.tidb_opt_insubq_to_join_and_agg=1;
drop table if exists tbl;
create table tbl(column1 int, column2 int, index idx(column1, column2));
load stats 's/explain_easy_stats_tbl_dnf.json';
explain select * from tbl where (column1=0 and column2=1) or (column1=1 and column2=3) or (column1=2 and column2=5);
id	count	task	operator info
IndexReader_6	3.00	root	index:IndexScan_5
└─IndexScan_5	3.00	cop	table:tbl, index:column1, column2, range:[0 1,0 1], [1 3,1 3], [2 5,2 5], keep order:false
