Skip to content

Commit 5d7c406

Browse files
committed
fix
SELECT * FROM A WHERE aid IN (SELECT DISTINCT aid FROM B )】这样的语句?B是分片表。 aid不是主键
1 parent fbc391c commit 5d7c406

2 files changed

Lines changed: 79 additions & 30 deletions

File tree

example/src/test/java/io/mycat/sql/UserCaseTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,4 +1670,52 @@ public void case2022318() throws Exception {
16701670
System.out.println();
16711671
}
16721672
}
1673+
1674+
@Test
1675+
public void case20220407() throws Exception {
1676+
try (Connection mycatConnection = getMySQLConnection(DB_MYCAT_PSTMT)) {
1677+
1678+
1679+
execute(mycatConnection, RESET_CONFIG);
1680+
1681+
execute(mycatConnection, "DROP DATABASE db1");
1682+
1683+
1684+
execute(mycatConnection, "CREATE DATABASE db1");
1685+
1686+
execute(mycatConnection, CreateDataSourceHint
1687+
.create("ds0",
1688+
DB1));
1689+
1690+
execute(mycatConnection,
1691+
CreateClusterHint.create("c0",
1692+
Arrays.asList("ds0"), Collections.emptyList()));
1693+
1694+
JdbcUtils.execute(mycatConnection, "CREATE TABLE db1.`company` (\n" +
1695+
" `id` bigint(20) NOT NULL KEY,\n" +
1696+
" `user_id` varchar(100) CHARACTER SET utf8 DEFAULT NULL,\n" +
1697+
" `traveldate` datetime(6) DEFAULT NULL,\n" +
1698+
" `fee` decimal(10,0) DEFAULT NULL,\n" +
1699+
" `days` int(11) DEFAULT NULL,\n" +
1700+
" `blob` longblob DEFAULT NULL\n" +
1701+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n");
1702+
1703+
JdbcUtils.execute(mycatConnection, "CREATE TABLE db1.`travelrecord` (\n" +
1704+
" `id` bigint(20) NOT NULL KEY,\n" +
1705+
" `user_id` varchar(100) CHARACTER SET utf8 DEFAULT NULL,\n" +
1706+
" `traveldate` datetime(6) DEFAULT NULL,\n" +
1707+
" `fee` decimal(10,0) DEFAULT NULL,\n" +
1708+
" `days` int(11) DEFAULT NULL,\n" +
1709+
" `blob` longblob DEFAULT NULL\n" +
1710+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4\n" +
1711+
"dbpartition by mod_hash(user_id) dbpartitions 16;");
1712+
1713+
1714+
List<Map<String, Object>> maps = executeQuery(mycatConnection, "SELECT * FROM\tdb1.company WHERE id IN (SELECT DISTINCT id FROM \tdb1.travelrecord )");
1715+
1716+
List<Map<String, Object>> maps1 = executeQuery(mycatConnection, "SELECT * FROM\tdb1.company WHERE id IN (SELECT DISTINCT fee FROM \tdb1.travelrecord )");
1717+
1718+
System.out.println();
1719+
}
1720+
}
16731721
}

hbt/src/main/java/io/mycat/calcite/rewriter/MycatAggDistinctRule.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.mycat.HintTools;
44
import io.mycat.calcite.localrel.LocalAggregate;
55
import io.mycat.calcite.logical.MycatView;
6+
import io.mycat.datasource.jdbc.datasource.JdbcConnectionManager;
67
import org.apache.calcite.plan.RelOptRuleCall;
78
import org.apache.calcite.plan.RelOptUtil;
89
import org.apache.calcite.plan.RelRule;
@@ -14,6 +15,8 @@
1415
import org.apache.calcite.rex.RexInputRef;
1516
import org.apache.calcite.sql.SqlKind;
1617
import org.apache.calcite.tools.RelBuilder;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1720

1821
import java.util.List;
1922
import java.util.function.Consumer;
@@ -22,6 +25,7 @@
2225
import static io.mycat.calcite.localrel.LocalRules.normalize;
2326

2427
public class MycatAggDistinctRule extends RelRule<MycatAggDistinctRule.Config> {
28+
private static final Logger LOGGER = LoggerFactory.getLogger(MycatAggDistinctRule.class);
2529
/**
2630
* Creates a RelRule.
2731
*
@@ -33,42 +37,39 @@ public MycatAggDistinctRule(Config config) {
3337

3438
@Override
3539
public void onMatch(RelOptRuleCall call) {
36-
Aggregate topAggregate = call.rel(0);
37-
MycatView mycatView = call.rel(2);
38-
RelHint lastAggHint = HintTools.getLastPushAggHint(topAggregate.getHints());
39-
if (lastAggHint != null) {
40-
if ("push_down_agg_distinct".equalsIgnoreCase(lastAggHint.hintName)) {
40+
try {
41+
Aggregate topAggregate = call.rel(0);
42+
MycatView mycatView = call.rel(2);
43+
RelHint lastAggHint = HintTools.getLastPushAggHint(topAggregate.getHints());
44+
if (lastAggHint != null) {
45+
if ("push_down_agg_distinct".equalsIgnoreCase(lastAggHint.hintName)) {
4146

42-
if (topAggregate.getAggCallList().size() == 1 && topAggregate.getGroupSet().isEmpty()) {
43-
List<AggregateCall> aggCallList = topAggregate.getAggCallList();
44-
if (aggCallList.size() == 1) {
45-
AggregateCall aggregateCall = aggCallList.get(0);
46-
if (aggregateCall.getAggregation().kind == SqlKind.COUNT) {
47-
Aggregate distinctAgg = call.rel(1);
48-
if (distinctAgg.getAggCallList().isEmpty() && !distinctAgg.getGroupSet().isEmpty()) {
49-
opt(call, topAggregate, mycatView);
50-
return;
47+
if (topAggregate.getAggCallList().size() == 1 && topAggregate.getGroupSet().isEmpty()) {
48+
List<AggregateCall> aggCallList = topAggregate.getAggCallList();
49+
if (aggCallList.size() == 1) {
50+
AggregateCall aggregateCall = aggCallList.get(0);
51+
if (aggregateCall.getAggregation().kind == SqlKind.COUNT) {
52+
Aggregate distinctAgg = call.rel(1);
53+
if (distinctAgg.getAggCallList().isEmpty() && !distinctAgg.getGroupSet().isEmpty()) {
54+
opt(call, topAggregate, mycatView);
55+
return;
56+
}
5157
}
5258
}
5359
}
60+
Aggregate aggregate = topAggregate;
61+
MycatView input = mycatView;
62+
SQLRBORewriter.aggregate(input, LocalAggregate.create(aggregate, input)).ifPresent(new Consumer<RelNode>() {
63+
@Override
64+
public void accept(RelNode res) {
65+
call.transformTo(normalize(res));
66+
}
67+
});
68+
return;
5469
}
55-
Aggregate aggregate = topAggregate;
56-
MycatView input = mycatView;
57-
SQLRBORewriter.aggregate(input, LocalAggregate.create(aggregate, input)).ifPresent(new Consumer<RelNode>() {
58-
@Override
59-
public void accept(RelNode res) {
60-
call.transformTo(normalize(res));
61-
}
62-
});
63-
return;
64-
}
65-
}
66-
if (topAggregate.getAggCallList().isEmpty() && topAggregate.getGroupSets().size() == 1) {
67-
RelMetadataQuery metadataQuery = call.getMetadataQuery();
68-
if (metadataQuery.areColumnsUnique(mycatView, topAggregate.getGroupSet())) {
69-
opt(call, topAggregate, mycatView);
70-
return;
7170
}
71+
}catch (Throwable throwable){
72+
LOGGER.debug("MycatAggDistinctRule occurs exception",throwable);
7273
}
7374
}
7475

0 commit comments

Comments
 (0)