use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatAggDistinctRule method opt.
private void opt(RelOptRuleCall call, Aggregate topAggregate, MycatView mycatView) {
RelBuilder builder = call.builder();
builder.push(mycatView.getRelNode().getInput(0));
List<AggregateCall> aggCallList = topAggregate.getAggCallList();
List<RexInputRef> collect = aggCallList.get(0).getArgList().stream().map(i -> builder.field(i)).collect(Collectors.toList());
RelBuilder.AggCall count = builder.count(true, "mycatCountDistinct", collect);
builder.aggregate(builder.groupKey(), count);
MycatView newView = mycatView.changeTo(builder.build());
builder.push(newView);
builder.aggregate(builder.groupKey(), builder.sum(builder.field(0)));
call.transformTo(RelOptUtil.createCastRel(builder.build(), topAggregate.getRowType(), true));
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class SQLRBORewriter method tryMergeJoin.
@NotNull
public static Optional<RelNode> tryMergeJoin(RelNode left, RelNode right, Join join) {
if (!DrdsSqlCompiler.RBO_MERGE_JOIN) {
return Optional.empty();
}
boolean isleftView = left instanceof MycatView;
boolean isRightView = right instanceof MycatView;
if (isleftView && isRightView) {
if (((MycatView) left).banPushdown() || ((MycatView) right).banPushdown()) {
return Optional.empty();
}
MycatSortMergeJoin mycatSortMergeJoin = MycatMergeJoinRule.INSTANCE.tryMycatSortMergeJoin(join.copy(join.getTraitSet(), ImmutableList.of(left, right)), true);
return Optional.ofNullable(mycatSortMergeJoin);
} else {
return Optional.empty();
}
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class SQLRBORewriter method view.
public static final Optional<RelNode> view(RelNode input, Calc calc) {
if (input instanceof MycatView && ((MycatView) input).banPushdown()) {
return Optional.empty();
}
final Pair<ImmutableList<RexNode>, ImmutableList<RexNode>> projectFilter = calc.getProgram().split();
RelBuilder relBuilder = relbuilder(calc.getCluster(), null);
RelNode relNode = relBuilder.push(input).filter(projectFilter.right).build();
if (relNode instanceof Filter) {
return view(input, (Filter) relNode).flatMap(u -> {
RelNode node = relBuilder.push(u).project(projectFilter.left).build();
if (node instanceof Project) {
return view(u, (Project) node);
} else {
return Optional.empty();
}
});
} else {
return Optional.empty();
}
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class SQLRBORewriter method pushDownERTable.
private static Optional<RelNode> pushDownERTable(Join join, MycatView left, MycatView right) {
if (left.banPushdown() || right.banPushdown()) {
return Optional.empty();
}
switch(join.getJoinType()) {
case INNER:
case LEFT:
case SEMI:
case ANTI:
case RIGHT:
break;
case FULL:
return Optional.empty();
default:
throw new IllegalStateException("Unexpected value: " + join.getJoinType());
}
JoinInfo joinInfo = join.analyzeCondition();
if (joinInfo.isEqui()) {
List<IntPair> pairs = joinInfo.pairs();
if (pairs.isEmpty())
return Optional.empty();
RexNode conditions = left.getCondition().orElse(null);
RelMetadataQuery metadataQuery = join.getCluster().getMetadataQuery();
IdentityHashMap<TableHandler, Set<String>> keysMap = new IdentityHashMap<>();
IdentityHashMap<RelColumnOrigin, RelColumnOrigin> equals = new IdentityHashMap<>();
for (IntPair pair : pairs) {
RelColumnOrigin leftColumnOrigin = metadataQuery.getColumnOrigin(left.getRelNode(), pair.source);
RelColumnOrigin rightColumnOrigin = metadataQuery.getColumnOrigin(right.getRelNode(), pair.target);
if (leftColumnOrigin == null || rightColumnOrigin == null) {
continue;
}
MycatLogicTable leftLogicTable = leftColumnOrigin.getOriginTable().unwrap(MycatLogicTable.class);
MycatLogicTable rightLogicTable = rightColumnOrigin.getOriginTable().unwrap(MycatLogicTable.class);
if (!leftColumnOrigin.isDerived() && !rightColumnOrigin.isDerived()) {
TableHandler leftTableHandler = leftLogicTable.getTable();
keysMap.computeIfAbsent(leftTableHandler, mycatLogicTable -> new HashSet<>()).add(leftTableHandler.getColumns().get(leftColumnOrigin.getOriginColumnOrdinal()).getColumnName());
TableHandler rightTableHandler = rightLogicTable.getTable();
keysMap.computeIfAbsent(rightTableHandler, mycatLogicTable -> new HashSet<>()).add(rightTableHandler.getColumns().get(rightColumnOrigin.getOriginColumnOrdinal()).getColumnName());
equals.put(leftColumnOrigin, rightColumnOrigin);
}
}
boolean allHitPartition = keysMap.entrySet().stream().allMatch(shardingTableHandlerSetEntry -> {
TableHandler tableHandler = shardingTableHandlerSetEntry.getKey();
switch(tableHandler.getType()) {
case SHARDING:
CustomRuleFunction function = ((ShardingTableHandler) tableHandler).function();
Set<String> columns = shardingTableHandlerSetEntry.getValue();
return function.requireShardingKeys(shardingTableHandlerSetEntry.getValue()) || // 保证命中分区
columns.stream().anyMatch(function::isShardingPartitionKey);
case GLOBAL:
case NORMAL:
return true;
case CUSTOM:
case VISUAL:
case VIEW:
default:
return false;
}
});
if (allHitPartition) {
// erjoin
boolean pushDown = isErJoin(equals);
if (!pushDown) {
pushDown = isSameTargetPartitionJoin(left, equals);
}
if (!pushDown) {
pushDown = isSameTargetPartitionGlobalOrNormalJoin(left, right, equals);
}
if (pushDown) {
return left.getDistribution().join(right.getDistribution()).map(distribution -> MycatView.ofCondition(join.copy(join.getTraitSet(), ImmutableList.of(left.getRelNode(), right.getRelNode())), distribution, conditions));
}
}
}
return Optional.empty();
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatExtraSortRule method pushSort.
private RelNode pushSort(RelNode relNode, RelCollation targetRelCollation) {
RelCollation orgianlCollation = relNode.getTraitSet().getCollation();
if (orgianlCollation != null && orgianlCollation.equals(targetRelCollation)) {
return relNode;
}
RelBuilder relBuilder = relBuilderFactory.create(relNode.getCluster(), null);
if (relNode instanceof MycatView) {
MycatView mycatView = (MycatView) relNode;
RelNode innerRelNode = mycatView.getRelNode();
relNode = mycatView.changeTo(relBuilder.push(innerRelNode).sort(targetRelCollation).build());
return relNode;
} else {
return LogicalSort.create(relNode, targetRelCollation, null, null);
}
}
Aggregations