Skip to content
Snippets Groups Projects
Unverified Commit 9977631b authored by bRong Njam's avatar bRong Njam Committed by GitHub
Browse files

[Plan] Deduplicate identical UNION parts (#4552)

parent 3f905595
No related branches found
No related tags found
No related merge requests found
......@@ -661,6 +661,14 @@ func (builder *QueryBuilder) buildUnion(stmt *tree.Select, ctx *BindContext, isR
return 0, err
}
if len(selectStmts) == 1 {
if slt, ok := selectStmts[0].(*tree.Select); ok {
return builder.buildSelect(slt, ctx, false)
} else {
return builder.buildSelect(&tree.Select{Select: selectStmts[0]}, ctx, false)
}
}
// build selects
var projectTypList [][]types.Type
selectStmtLength := len(selectStmts)
......
......@@ -515,8 +515,22 @@ func getUnionSelects(stmt *tree.UnionClause, selects *[]tree.Statement, unionTyp
// right is not UNION allways
switch rightStmt := stmt.Right.(type) {
case *tree.SelectClause:
if stmt.Type == tree.UNION && !stmt.All {
rightStr := tree.String(rightStmt, dialect.MYSQL)
if len(*selects) == 1 && tree.String((*selects)[0], dialect.MYSQL) == rightStr {
return nil
}
}
*selects = append(*selects, rightStmt)
case *tree.ParenSelect:
if stmt.Type == tree.UNION && !stmt.All {
rightStr := tree.String(rightStmt.Select, dialect.MYSQL)
if len(*selects) == 1 && tree.String((*selects)[0], dialect.MYSQL) == rightStr {
return nil
}
}
*selects = append(*selects, rightStmt.Select)
default:
return errors.New(errno.SQLStatementNotYetComplete, fmt.Sprintf("unexpected statement in union2: '%v'", tree.String(rightStmt, dialect.MYSQL)))
......@@ -531,13 +545,13 @@ func getUnionSelects(stmt *tree.UnionClause, selects *[]tree.Statement, unionTyp
}
case tree.INTERSECT:
if stmt.All {
return errors.New("", "INTERSECT ALL clause will support in future version.")
return errors.New("", "INTERSECT ALL clause will be supported in future version.")
} else {
*unionTypes = append(*unionTypes, plan.Node_INTERSECT)
}
case tree.EXCEPT, tree.UT_MINUS:
if stmt.All {
return errors.New("", "EXCEPT/MINUS ALL clause will support in future version.")
return errors.New("", "EXCEPT/MINUS ALL clause will be supported in future version.")
} else {
*unionTypes = append(*unionTypes, plan.Node_MINUS)
}
......
......@@ -78,12 +78,8 @@ SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
-- @bvt:issue#4354
-- @bvt:issue#3419
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
-- @bvt:issue
-- @bvt:issue
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 ( a double, b double );
......@@ -323,13 +319,9 @@ DROP TABLE IF EXISTS t1;
CREATE TABLE t1( a INT );
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2( a INT, b INT );
-- @bvt:issue#4354
-- @bvt:issue#3419
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
-- @bvt:issue
-- @bvt:issue
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
......
......@@ -286,16 +286,12 @@ drop table if exists t1;
-- @label:bvt
drop table if exists t1;
CREATE TABLE t1 (i INT);
-- @bvt:issue#4354
-- @bvt:issue#3419
SELECT * FROM t1 WHERE NOT EXISTS
(
(SELECT i FROM t1) UNION
(SELECT i FROM t1)
);
SELECT * FROM t1 WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
-- @bvt:issue
-- @bvt:issue
drop table if exists t1;
drop table if exists t2;
......@@ -341,11 +337,7 @@ CREATE TABLE t2 (a INT);
INSERT INTO t1 VALUES (1),(2);
INSERT INTO t2 VALUES (1),(2);
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
-- @bvt:issue#4354
-- @bvt:issue#3419
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a));
-- @bvt:issue
-- @bvt:issue
drop table if exists t1;
drop table if exists t2;
......
......@@ -199,15 +199,11 @@ insert into t3 values (NULL), (NULL);
select a, oref, a in (select max(ie) from t1 where oref=t2.oref group by grp) Z from t2;
-- @bvt:issue
select a, oref from t2 where a in (select max(ie) from t1 where oref=t2.oref group by grp);
-- @bvt:issue#4354
-- @bvt:issue#3419
-- @bvt:issue#3312
select a, oref, a in (
select max(ie) from t1 where oref=t2.oref group by grp union
select max(ie) from t1 where oref=t2.oref group by grp
) Z from t2;
-- @bvt:issue
-- @bvt:issue
-- @bvt:issue#3312
select a in (select max(ie) from t1 where oref=4 group by grp) from t3;
-- @bvt:issue
......@@ -619,13 +615,9 @@ INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2( a INT, b INT );
SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
-- error
-- @bvt:issue#4354
-- @bvt:issue#3419
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
-- @bvt:issue
-- @bvt:issue
DROP TABLE IF EXISTS t1;
drop table if exists t2;
......@@ -876,12 +868,8 @@ create table t1 (a int, b int, primary key (a));
insert into t1 values (1,1), (3,1),(100,1);
create table t2 (a int, b int);
insert into t2 values (1,1),(2,1),(NULL,1),(NULL,0);
-- @bvt:issue#4354
-- @bvt:issue#3419
select a,b, a in (select a from t1 where t1.b = t2.b union select a from t1 where t1.b = t2.b) Z from t2 ;
-- @bvt:issue
-- @bvt:issue
-- @bvt:issue#3312
select a,b, a in (select a from t1 where t1.b = t2.b union select a from t1 where t1.b = t2.b) Z from t2 ;
select a,b, a in (select a from t1 where t1.b = t2.b) Z from t2 ;
-- @bvt:issue
......
......@@ -138,9 +138,10 @@ a
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
column "t1.a" must appear in the GROUP BY clause or be used in an aggregate function
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
'union' will be supported in future version.
a
2
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
'union' will be supported in future version.
column "t1.a" must appear in the GROUP BY clause or be used in an aggregate function
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 ( a double, b double );
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
......@@ -474,11 +475,14 @@ CREATE TABLE t1( a INT );
INSERT INTO t1 VALUES (1),(2);
CREATE TABLE t2( a INT, b INT );
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
'union' will be supported in future version.
a
1
SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
'union' will be supported in future version.
a
1
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
'union' will be supported in future version.
a
1
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(i INT);
......
......@@ -395,9 +395,9 @@ SELECT * FROM t1 WHERE NOT EXISTS
(SELECT i FROM t1) UNION
(SELECT i FROM t1)
);
'union' will be supported in future version.
i
SELECT * FROM t1 WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
'union' will be supported in future version.
i
drop table if exists t1;
drop table if exists t2;
drop table if exists t3;
......@@ -436,7 +436,9 @@ SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
2
2
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a));
'union' will be supported in future version.
2
2
2
drop table if exists t1;
drop table if exists t2;
drop table if exists t1;
......
......@@ -740,11 +740,14 @@ CREATE TABLE t2( a INT, b INT );
SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
a b
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
'union' will be supported in future version.
a
1
SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
'union' will be supported in future version.
a
1
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
'union' will be supported in future version.
a
1
DROP TABLE IF EXISTS t1;
drop table if exists t2;
drop table if exists t3;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment