use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatSingleViewRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
SQLRBORewriter.on(call.rel(1), call.rel(0)).ifPresent(c -> {
if (c instanceof MycatView) {
RelNode relNode = ((MycatView) c).getRelNode();
ToLocalConverter toLocalConverter = new ToLocalConverter();
RelNode accept = relNode.accept(toLocalConverter);
LocalToMycatRelConverter localToMycatRelConverter = new LocalToMycatRelConverter(c.getCluster(), c.getTraitSet().replace(MycatConvention.INSTANCE), accept);
}
});
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatTableLookupSemiJoinRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
Join join = call.rel(0);
RelOptCluster cluster = join.getCluster();
RelNode left = call.rel(1);
RelNode right = call.rel(2);
RelMetadataQuery metadataQuery = cluster.getMetadataQuery();
RelHint lastJoinHint = HintTools.getLastJoinHint(join.getHints());
// if (lastJoinHint == null){
// return;
// }
boolean hint = false;
if (lastJoinHint != null && "use_bka_join".equalsIgnoreCase(lastJoinHint.hintName)) {
hint = true;
} else {
double leftRowCount = Optional.ofNullable(metadataQuery.getRowCount(left)).orElse(0.0);
if (leftRowCount > BKA_JOIN_LEFT_ROW_COUNT_LIMIT) {
return;
}
}
if (!join.analyzeCondition().isEqui()) {
return;
}
if (!(right instanceof MycatView)) {
return;
}
MycatView mycatView = (MycatView) right;
if (mycatView.banPushdown()) {
return;
}
if (!hint) {
if (!isGisView(mycatView))
return;
}
JoinRelType joinType = join.getJoinType();
switch(joinType) {
case LEFT:
case INNER:
case SEMI:
break;
default:
return;
}
if (RelOptUtil.countJoins(mycatView.getRelNode()) > 1) {
return;
}
RelBuilder relBuilder = MycatCalciteSupport.relBuilderFactory.create(cluster, null);
//
// ImmutableList.Builder<RelDataTypeField> listBuilder = ImmutableList.builder();
// Map<Integer, Integer> sourcePosToTargetPos = new HashMap<>();
// extractedTrimJoinLeftKeys(join, listBuilder, sourcePosToTargetPos);
// RelRecordType argTypeListRecordType = new RelRecordType(listBuilder.build());
// Mapping mapping = Mappings.bijection(sourcePosToTargetPos);
// RexNode equiCondition = RexUtil.apply(mapping, RelOptUtil.createEquiJoinCondition(right, join.analyzeCondition().rightKeys, left, join.analyzeCondition().leftKeys,
// MycatCalciteSupport.RexBuilder));
RexBuilder rexBuilder = MycatCalciteSupport.RexBuilder;
RelDataTypeFactory typeFactory = cluster.getTypeFactory();
relBuilder.push(right);
List<RexNode> rightExprs = new ArrayList<>();
{
for (Integer rightKey : join.analyzeCondition().rightSet()) {
rightExprs.add(relBuilder.field(rightKey));
}
}
List<RexNode> leftExprs = new ArrayList<>();
List<CorrelationId> correlationIds = new ArrayList<>();
{
for (Integer leftKey : join.analyzeCondition().leftSet()) {
CorrelationId correl = cluster.createCorrel();
correlationIds.add(correl);
RelDataType type = left.getRowType().getFieldList().get(leftKey).getType();
RexNode rexNode = rexBuilder.makeCorrel(typeFactory.createUnknownType(), correl);
leftExprs.add(rexBuilder.makeCast(type, rexNode));
}
}
RexNode condition = relBuilder.call(MYCAT_SQL_LOOKUP_IN, rexBuilder.makeCall(SqlStdOperatorTable.ROW, rightExprs), rexBuilder.makeCall(SqlStdOperatorTable.ROW, leftExprs));
Distribution.Type type = mycatView.getDistribution().type();
switch(type) {
case PHY:
case BROADCAST:
{
RelNode relNode = mycatView.getRelNode();
relBuilder.push(relNode);
relBuilder.filter(condition);
relBuilder.rename(mycatView.getRowType().getFieldNames());
MycatView view = mycatView.changeTo(relBuilder.build());
call.transformTo(new MycatSQLTableLookup(cluster, join.getTraitSet(), left, view, joinType, join.getCondition(), correlationIds, MycatSQLTableLookup.Type.BACK));
return;
}
case SHARDING:
{
RelNode innerRelNode = mycatView.getRelNode();
boolean bottomFilter = innerRelNode instanceof TableScan;
relBuilder.push(mycatView.getRelNode());
relBuilder.filter(condition);
RelNode innerDataNode = relBuilder.rename(mycatView.getRowType().getFieldNames()).build();
Optional<RexNode> viewConditionOptional = mycatView.getCondition();
RexNode finalCondition = null;
if (!viewConditionOptional.isPresent() && bottomFilter) {
finalCondition = condition;
} else if (bottomFilter) {
finalCondition = viewConditionOptional.map(i -> RexUtil.composeConjunction(MycatCalciteSupport.RexBuilder, ImmutableList.of(i, condition))).orElse(condition);
}
MycatView resView = MycatView.ofCondition(innerDataNode, mycatView.getDistribution(), finalCondition);
call.transformTo(new MycatSQLTableLookup(cluster, join.getTraitSet(), left, resView, joinType, join.getCondition(), correlationIds, MycatSQLTableLookup.Type.BACK));
break;
}
default:
}
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class SQLRBORewriter method splitAggregate.
private static Optional<RelNode> splitAggregate(MycatView viewNode, Aggregate aggregate) {
try {
AggregatePushContext aggregateContext = AggregatePushContext.split(aggregate);
MycatView newView = viewNode.changeTo(LogicalAggregate.create(viewNode.getRelNode(), aggregate.getHints(), aggregate.getGroupSet(), aggregate.getGroupSets(), aggregateContext.getPartialAggregateCallList()));
LogicalAggregate globalAggregateRelNode = LogicalAggregate.create(newView, aggregate.getHints(), aggregate.getGroupSet(), aggregate.getGroupSets(), aggregateContext.getGlobalAggregateCallList());
MycatProject projectRelNode = MycatProject.create(globalAggregateRelNode, aggregateContext.getProjectExprList(), aggregate.getRowType());
return RexUtil.isIdentity(projectRelNode.getProjects(), projectRelNode.getInput().getRowType()) ? Optional.of(globalAggregateRelNode) : Optional.of(projectRelNode);
} catch (Throwable throwable) {
LOGGER.debug("", throwable);
}
return Optional.empty();
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatTableLookupCombineRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
Join join = call.rel(0);
RelOptCluster cluster = join.getCluster();
MycatSQLTableLookup left = call.rel(1);
if (left.getType() != MycatSQLTableLookup.Type.NONE) {
return;
}
RelNode right = call.rel(2);
JoinInfo joinInfo = join.analyzeCondition();
if (!(right instanceof MycatView)) {
return;
}
MycatView inneRightMycatView = (MycatView) left.getRight();
MycatView outerRightmycatView = (MycatView) right;
if (outerRightmycatView.banPushdown()) {
return;
}
JoinRelType joinType = join.getJoinType();
switch(joinType) {
case INNER:
case SEMI:
break;
default:
return;
}
if (RelOptUtil.countJoins(outerRightmycatView.getRelNode()) > 1) {
return;
}
switch(outerRightmycatView.getDistribution().type()) {
case PHY:
case BROADCAST:
{
RelBuilder builder = call.builder();
inneRightMycatView.getDistribution().join(outerRightmycatView.getDistribution()).ifPresent(c -> {
RelBuilder relBuilder = builder.push(inneRightMycatView.getRelNode()).push(outerRightmycatView.getRelNode()).join(joinType, join.getCondition());
RelNode relNode = relBuilder.build();
MycatView view = MycatView.ofCondition(relNode, c, null);
call.transformTo(left.changeTo(left.getInput(0), view));
});
break;
}
case SHARDING:
{
SQLRBORewriter.bottomJoin(inneRightMycatView, outerRightmycatView, join).ifPresent(relNode1 -> {
call.transformTo(left.changeTo(left.getInput(0), (MycatView) relNode1));
});
break;
}
default:
}
}
use of io.mycat.calcite.logical.MycatView in project Mycat2 by MyCATApache.
the class MycatProjectJoinClusteringRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final LogicalProject origProject = call.rel(0);
final LogicalJoin origJoin = call.rel(1);
final RelNode left = call.rel(2);
final RelNode right = call.rel(3);
Optional<RelNode> joinOptional = SQLRBORewriter.bottomJoin(left, right, origJoin);
if (joinOptional.isPresent()) {
RelNode relNode = joinOptional.get();
Project newProject = (Project) origProject.copy(origProject.getTraitSet(), ImmutableList.of(relNode));
if (relNode instanceof MycatView) {
call.transformTo(newProject.accept(new SQLRBORewriter()));
return;
}
call.transformTo(newProject);
}
}
Aggregations