use of io.mycat.calcite.physical.MycatProject in project Mycat2 by MyCATApache.
the class MycatView method createMycatProject.
public static MycatRel createMycatProject(RelNode indexTableScan, List<String> indexColumns, boolean nullable) {
RelDataType rowType = indexTableScan.getRowType();
ArrayList<Integer> ints = new ArrayList<>();
for (String indexColumn : indexColumns) {
ints.add(rowType.getField(indexColumn, false, false).getIndex());
}
RelNode project = RelOptUtil.createProject(indexTableScan, ints);
if (project instanceof LogicalProject) {
List<RexNode> projects = ((Project) project).getProjects();
// if (nullable){
// RexBuilder rexBuilder = MycatCalciteSupport.RexBuilder;
// for (RexNode rexNode : projects) {
// rexBuilder.makeNotNull(rexNode);
// }
//
// }
project = MycatProject.create(project.getInput(0), projects, project.getRowType());
}
MycatProject mycatProject = (MycatProject) project;
if (mycatProject.getInput() instanceof MycatView) {
MycatView mycatProjectInput = (MycatView) mycatProject.getInput();
return mycatProjectInput.changeTo(mycatProject.copy(mycatProject.getTraitSet(), ImmutableList.of(mycatProjectInput.getRelNode())));
}
return (MycatRel) project;
}
use of io.mycat.calcite.physical.MycatProject in project Mycat2 by MyCATApache.
the class MycatView method produceIndexViews.
public static List<RelNode> produceIndexViews(TableScan tableScan, final RexNode wholeCondition, List<Integer> projects, RelDataType orginalRowType, String indexName) {
DrdsSqlCompiler drdsSqlCompiler = MetaClusterCurrent.wrapper(DrdsSqlCompiler.class);
RelOptCluster cluster = tableScan.getCluster();
RelBuilder relBuilder = MycatCalciteSupport.relBuilderFactory.create(cluster, drdsSqlCompiler.getCatalogReader());
ShardingTable shardingTable = (ShardingTable) tableScan.getTable().unwrap(MycatLogicTable.class).getTable();
List<ShardingIndexTable> indexTables = shardingTable.getIndexTables();
String[] shardingKeys = shardingTable.getLogicTable().getShardingKeys().toArray(new String[] {});
ArrayList<RelNode> tableArrayList = new ArrayList<>(indexTables.size());
MycatView primaryTableScan;
for (ShardingIndexTable indexTable : indexTables) {
if (indexName != null && !indexName.equalsIgnoreCase(indexTable.getIndexName())) {
continue;
}
ProjectIndexMapping indexMapping = project(indexTable, projects);
boolean indexOnlyScan = !indexMapping.needFactTable();
final IndexCondition indexCondition;
if (wholeCondition != null) {
PredicateAnalyzer predicateAnalyzer = new PredicateAnalyzer(indexTable.keyMetas(), shardingTable.getColumns().stream().map(i -> i.getColumnName()).collect(Collectors.toList()));
Map<QueryType, List<IndexCondition>> queryTypeListMap = predicateAnalyzer.translateMatch(wholeCondition);
if (queryTypeListMap.isEmpty())
return Collections.emptyList();
List<IndexCondition> next = queryTypeListMap.values().stream().filter(i -> i != null).iterator().next().stream().filter(i -> i != null).collect(Collectors.toList());
indexCondition = next.get(0);
primaryTableScan = MycatView.ofCondition(LocalFilter.create(wholeCondition, LocalTableScan.create((TableScan) relBuilder.scan(shardingTable.getSchemaName(), shardingTable.getTableName()).build())), Distribution.of(shardingTable), wholeCondition);
} else {
continue;
}
RelNode indexTableView = null;
LocalFilter localFilter = null;
RexNode pushdownCondition = null;
LocalTableScan localTableScan = LocalTableScan.create((TableScan) relBuilder.scan(indexTable.getSchemaName(), indexTable.getTableName()).build());
switch(indexCondition.getQueryType()) {
case PK_POINT_QUERY:
List<String> indexColumnNames = indexCondition.getIndexColumnNames();
RexNode rexNode = indexCondition.getPushDownRexNodeList().get(0);
relBuilder.push(localTableScan);
List<RexNode> pushDownConditions = new ArrayList<>();
for (String indexColumnName : indexColumnNames) {
pushDownConditions.add(relBuilder.equals(relBuilder.field(indexColumnName), rexNode));
}
pushdownCondition = RexUtil.composeConjunction(relBuilder.getRexBuilder(), pushDownConditions);
RelNode build = relBuilder.build();
localFilter = LocalFilter.create(LogicalFilter.create(localTableScan, pushdownCondition), localTableScan);
indexTableView = localFilter;
break;
case PK_RANGE_QUERY:
case PK_FULL_SCAN:
continue;
}
if (indexOnlyScan) {
List<Integer> newProject = getProjectIntList(projects, tableScan.deriveRowType(), indexTableView.getRowType());
MycatView view = MycatView.ofCondition(LocalProject.create((Project) RelOptUtil.createProject(indexTableView, newProject), indexTableView), Distribution.of(indexTable), pushdownCondition);
tableArrayList.add(view);
continue;
} else {
indexTableView = MycatView.ofCondition(indexTableView, Distribution.of(indexTable), pushdownCondition);
RelNode leftProject = createMycatProject(indexTableView, indexMapping.getIndexColumns());
RelNode rightProject = createMycatProject(primaryTableScan, indexMapping.getFactColumns());
Join relNode = (Join) relBuilder.push(leftProject).push(rightProject).join(JoinRelType.INNER, shardingKeys).build();
relNode = MycatHashJoin.create(relNode.getTraitSet(), ImmutableList.of(), leftProject, rightProject, relNode.getCondition(), relNode.getJoinType());
MycatRel mycatProject = createMycatProject(relNode, getProjectStringList(projects, tableScan.getRowType()));
if (RelOptUtil.areRowTypesEqual(orginalRowType, mycatProject.getRowType(), false)) {
tableArrayList.add(mycatProject);
}
continue;
}
}
return (List) tableArrayList;
}
use of io.mycat.calcite.physical.MycatProject 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();
}
Aggregations