Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangwy committed May 19, 2022
2 parents 6cd0ef4 + b8539a1 commit d836d2b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
20 changes: 12 additions & 8 deletions example/src/test/java/io/mycat/sql/MegreJoinExecuteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ public void testBase() throws Exception {
Assert.assertEquals("[{id=1, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=1, companyname=Intel, addressid=1}, {id=2, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=2, companyname=IBM, addressid=2}, {id=3, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=3, companyname=Dell, addressid=3}]",
s);

//second
sql = "select * from db1.sharding s inner join db1.normal e on s.id = e.id inner join db1.global g on s.id = g.id order by s.id";
explain= explain(mycatConnection,sql );
s = executeQueryAsText(mycatConnection, sql);
Assert.assertEquals(true,explain.contains("MycatSortMergeJoin"));
Assert.assertEquals("[{id=1, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=1, companyname=Intel, addressid=1, id1=1, companyname0=Intel, addressid0=1}, {id=2, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=2, companyname=IBM, addressid=2, id1=2, companyname0=IBM, addressid0=2}, {id=3, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=3, companyname=Dell, addressid=3, id1=3, companyname0=Dell, addressid0=3}]",
s);

// for (int i = 0; i < 1000000; i++)
{
//second
sql = "select * from db1.sharding s inner join db1.normal e on s.id = e.id inner join db1.global g on s.id = g.id order by s.id";
explain = explain(mycatConnection, sql);
System.out.println("sql:"+explain);
System.out.println("explain"+explain);
s = executeQueryAsText(mycatConnection, sql);
Assert.assertEquals(true, explain.contains("MycatSortMergeJoin"));
Assert.assertEquals("[{id=1, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=1, companyname=Intel, addressid=1, id1=1, companyname0=Intel, addressid0=1}, {id=2, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=2, companyname=IBM, addressid=2, id1=2, companyname0=IBM, addressid0=2}, {id=3, user_id=null, traveldate=null, fee=null, days=null, blob=null, id0=3, companyname=Dell, addressid=3, id1=3, companyname0=Dell, addressid0=3}]",
s);
}
sql = "select * from db1.normal s inner join db1.sharding e on s.id = e.id inner join db1.global g on s.id = g.id order by s.id";
explain= explain(mycatConnection,sql );
s = executeQueryAsText(mycatConnection, sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ValuePredicateAnalyzer(String indexName, List<KeyMeta> keyMetas, List<Str
}

public Map<QueryType, List<ValueIndexCondition>> translateMatch(RexNode condition) {
condition = conditionClippingByKeyMeta(condition,fieldNames,keyMetas);
condition = conditionClippingByKeyMeta(condition, fieldNames, keyMetas);
Map<QueryType, List<ValueIndexCondition>> tryAndInRes = tryAndIn(condition);
if (tryAndInRes != null && !tryAndInRes.isEmpty()) return tryAndInRes;
// does not support disjunctions
Expand All @@ -67,7 +67,7 @@ public Map<QueryType, List<ValueIndexCondition>> translateMatch(RexNode conditio
public static RexNode conditionClippingByKeyMeta(RexNode condition, List<String> fieldNames, List<KeyMeta> keyMetas) {
List<RexNode> rexNodeList = RelOptUtil.conjunctions(condition);
if (rexNodeList.isEmpty() || rexNodeList.size() == 1) {
return condition;
return condition;
} else {
class ShardingKeyFinder extends RexShuttle {
boolean find = false;
Expand Down Expand Up @@ -100,7 +100,7 @@ public RexNode visitInputRef(RexInputRef inputRef) {
}
rexNodeList = newRexNodeList;
if (rexNodeList.isEmpty()) {
return condition;
return condition;
} else if (rexNodeList.size() == 1) {
condition = rexNodeList.get(0);
} else {
Expand All @@ -121,15 +121,15 @@ private Map<QueryType, List<ValueIndexCondition>> tryAndIn(RexNode condition) {
if (operands.subList(1, operands.size()).stream().anyMatch(i -> RexUtil.findOperatorCall(SqlStdOperatorTable.OR, i) != null)) {
return Collections.emptyMap();
}
boolean allMatch = true;
RexNode primaryInputRef = null;
List<RexNode> valueList = new ArrayList<>();
if (primaryHead.isA(SqlKind.OR)) {
List<RexNode> valueList = new ArrayList<>();
RexNode primaryInputRef = null;
boolean allMatch = true;
RexCall primaryNode = (RexCall) primaryHead;
for (RexNode maybeEquals : primaryNode.getOperands()) {
if (maybeEquals.isA(SqlKind.EQUALS)) {
RexCall equals = (RexCall) maybeEquals;
RexNode maybeId = equals.getOperands().get(0);
RexNode maybeId = RexUtil.removeCast(equals.getOperands().get(0));
valueList.add(RexUtil.removeCast(equals.getOperands().get(1)));
if (maybeId.isA(SqlKind.INPUT_REF)) {
if (primaryInputRef == null) {
Expand All @@ -146,13 +146,20 @@ private Map<QueryType, List<ValueIndexCondition>> tryAndIn(RexNode condition) {
if (allMatch && !valueList.isEmpty()) {
Map<QueryType, List<ValueIndexCondition>> indexConditions = new HashMap<>();
ValueIndexCondition pushDownCondition = null;
RexInputRef inputRef = (RexInputRef) primaryInputRef;
String columnName = fieldNames.get(inputRef.getIndex());
for (KeyMeta skMeta : keyMetas) {
if (!skMeta.getColumnNames().contains(columnName)){
continue;
}
for (RexNode rexNode : valueList) {
if (pushDownCondition == null) {
pushDownCondition = findPushDownCondition(
ImmutableList.of(MycatCalciteSupport.RexBuilder.makeCall(SqlStdOperatorTable.EQUALS, primaryInputRef, rexNode)), skMeta);
} else {
pushDownCondition.pointQueryKey.add(rexNode);
if (pushDownCondition.pointQueryKey != null) {
pushDownCondition.pointQueryKey.add(rexNode);
}
}
}
}
Expand All @@ -163,6 +170,54 @@ private Map<QueryType, List<ValueIndexCondition>> tryAndIn(RexNode condition) {
}
}
}
if (condition.isA(SqlKind.OR)) {
RexCall primaryNode = (RexCall) condition;
List<RexNode> valueList = new ArrayList<>();
RexNode primaryInputRef = null;
boolean allMatch = true;
for (RexNode maybeEquals : primaryNode.getOperands()) {
if (maybeEquals.isA(SqlKind.EQUALS)) {
RexCall equals = (RexCall) maybeEquals;
RexNode maybeId = RexUtil.removeCast(equals.getOperands().get(0));
valueList.add(RexUtil.removeCast(equals.getOperands().get(1)));
if (maybeId.isA(SqlKind.INPUT_REF)) {
if (primaryInputRef == null) {
primaryInputRef = maybeId;
continue;
} else if (primaryInputRef.equals(maybeId)) {
continue;
}
}
}
allMatch = false;
break;
}
if (allMatch && !valueList.isEmpty()) {
Map<QueryType, List<ValueIndexCondition>> indexConditions = new HashMap<>();
RexInputRef inputRef = (RexInputRef) primaryInputRef;
String columnName = fieldNames.get(inputRef.getIndex());
ValueIndexCondition pushDownCondition = null;
for (KeyMeta skMeta : keyMetas) {
if (!skMeta.getColumnNames().contains(columnName)){
continue;
}
for (RexNode rexNode : valueList) {
if (pushDownCondition == null) {
pushDownCondition = findPushDownCondition(
ImmutableList.of(MycatCalciteSupport.RexBuilder.makeCall(SqlStdOperatorTable.EQUALS, primaryInputRef, rexNode)), skMeta);
} else {
if (pushDownCondition.pointQueryKey != null) {
pushDownCondition.pointQueryKey.add(rexNode);
}
}
}
}
if (pushDownCondition != null) {
indexConditions.put(QueryType.PK_POINT_QUERY, Collections.singletonList(pushDownCondition));
}
return indexConditions;
}
}
return Collections.emptyMap();
} catch (Throwable throwable) {
LOGGER.warn("", throwable);
Expand Down

0 comments on commit d836d2b

Please sign in to comment.