Skip to content

Commit

Permalink
[release-18.0] simplify merging logic (#16525) (#16531)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
Co-authored-by: Andrés Taylor <andres@planetscale.com>
  • Loading branch information
vitess-bot[bot] and systay authored Aug 23, 2024
1 parent b035e83 commit cb39bd9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/vitess_tester_vtgate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
end_to_end:
- 'go/**/*.go'
- 'go/vt/sidecardb/**/*.sql'
- 'go/test/endtoend/onlineddl/vrepl_suite/**'
- 'go/test/endtoend/vtgate/vitess_tester/**'
- 'test.go'
- 'Makefile'
- 'build.env'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
use customer;
create table if not exists customer(
customer_id bigint not null,
email varbinary(128),
primary key(customer_id)
) ENGINE=InnoDB;
insert into customer.customer(customer_id, email) values(1, '[alice@domain.com](mailto:alice@domain.com)');
insert into customer.customer(customer_id, email) values(2, '[bob@domain.com](mailto:bob@domain.com)');
insert into customer.customer(customer_id, email) values(3, '[charlie@domain.com](mailto:charlie@domain.com)');
insert into customer.customer(customer_id, email) values(4, '[dan@domain.com](mailto:dan@domain.com)');
insert into customer.customer(customer_id, email) values(5, '[eve@domain.com](mailto:eve@domain.com)');
create table if not exists customer
(
customer_id bigint not null,
email varbinary(128),
primary key (customer_id)
) ENGINE = InnoDB;

insert into customer.customer(customer_id, email)
values (1, '[alice@domain.com](mailto:alice@domain.com)'),
(2, '[bob@domain.com](mailto:bob@domain.com)'),
(3, '[charlie@domain.com](mailto:charlie@domain.com)'),
(4, '[dan@domain.com](mailto:dan@domain.com)'),
(5, '[eve@domain.com](mailto:eve@domain.com)');
use corder;
create table if not exists corder(
order_id bigint not null,
customer_id bigint,
sku varbinary(128),
price bigint,
primary key(order_id)
) ENGINE=InnoDB;
insert into corder.corder(order_id, customer_id, sku, price) values(1, 1, 'SKU-1001', 100);
insert into corder.corder(order_id, customer_id, sku, price) values(2, 2, 'SKU-1002', 30);
insert into corder.corder(order_id, customer_id, sku, price) values(3, 3, 'SKU-1002', 30);
insert into corder.corder(order_id, customer_id, sku, price) values(4, 4, 'SKU-1002', 30);
insert into corder.corder(order_id, customer_id, sku, price) values(5, 5, 'SKU-1002', 30);
create table if not exists corder
(
order_id bigint not null,
customer_id bigint,
sku varbinary(128),
price bigint,
primary key (order_id)
) ENGINE = InnoDB;
insert into corder.corder(order_id, customer_id, sku, price)
values (1, 1, 'SKU-1001', 100),
(2, 2, 'SKU-1002', 30),
(3, 3, 'SKU-1002', 30),
(4, 4, 'SKU-1002', 30),
(5, 5, 'SKU-1002', 30);

select co.order_id, co.customer_id, co.price
from corder.corder co
left join customer.customer cu on co.customer_id = cu.customer_id
where cu.customer_id = 1;

select co.order_id, co.customer_id, co.price from corder.corder co left join customer.customer cu on co.customer_id=cu.customer_id where cu.customer_id=1;
# This query was accidentally disallowed by https://github.com/vitessio/vitess/pull/16520
select 1
from customer.customer
where customer_id in (select customer_id from corder.corder where price > 50);
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/join_merging.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func mergeJoinInputs(ctx *plancontext.PlanningContext, lhs, rhs ops.Operator, jo

// sharded routing is complex, so we handle it in a separate method
case a == sharded && b == sharded:
return tryMergeShardedRouting(ctx, lhsRoute, rhsRoute, m, joinPredicates, false /*isSubquery*/)
return tryMergeShardedRouting(ctx, lhsRoute, rhsRoute, m, joinPredicates)

default:
return nil, nil
Expand Down
13 changes: 3 additions & 10 deletions go/vt/vtgate/planbuilder/operators/sharded_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/slice"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
Expand Down Expand Up @@ -578,9 +577,10 @@ func tryMergeShardedRouting(
routeA, routeB *Route,
m merger,
joinPredicates []sqlparser.Expr,
isSubquery bool,
) (*Route, error) {
sameKeyspace := routeA.Routing.Keyspace() == routeB.Routing.Keyspace()
if routeA.Routing.Keyspace() != routeB.Routing.Keyspace() {
return nil, nil
}
tblA := routeA.Routing.(*ShardedRouting)
tblB := routeB.Routing.(*ShardedRouting)

Expand Down Expand Up @@ -609,13 +609,6 @@ func tryMergeShardedRouting(
return nil, nil
}

if !sameKeyspace {
if isSubquery {
return nil, vterrors.VT12001("cross-shard correlated subquery")
}
return nil, nil
}

canMerge := canMergeOnFilters(ctx, routeA, routeB, joinPredicates)
if !canMerge {
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/subquery_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ func mergeSubqueryInputs(ctx *plancontext.PlanningContext, in, out ops.Operator,

// sharded routing is complex, so we handle it in a separate method
case inner == sharded && outer == sharded:
return tryMergeShardedRouting(ctx, inRoute, outRoute, m, joinPredicates, true /*isSubquery*/)
return tryMergeShardedRouting(ctx, inRoute, outRoute, m, joinPredicates)

default:
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/testdata/unsupported_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@
},
{
"comment": "Cross keyspace query with subquery",
"query": "select 1 from user where id in (select id from t1)",
"plan": "VT12001: unsupported: cross-shard correlated subquery"
"query": "select 1 from user where id = (select id from t1 where user.foo = t1.bar)",
"plan": "VT12001: unsupported: correlated subquery is only supported for EXISTS"
},
{
"comment": "multi-shard union",
Expand Down
2 changes: 1 addition & 1 deletion test/templates/cluster_vitess_tester.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
end_to_end:
- 'go/**/*.go'
- 'go/vt/sidecardb/**/*.sql'
- 'go/test/endtoend/onlineddl/vrepl_suite/**'
- 'go/test/endtoend/vtgate/vitess_tester/**'
- 'test.go'
- 'Makefile'
- 'build.env'
Expand Down

0 comments on commit cb39bd9

Please sign in to comment.