silundong commented on code in PR #4392:
URL: https://github.com/apache/calcite/pull/4392#discussion_r2111464919
##########
core/src/main/java/org/apache/calcite/rel/rules/HyperGraph.java:
##########
@@ -401,69 +453,67 @@ public RexNode extractJoinCond(RelNode left, RelNode
right, List<HyperEdge> edge
};
for (HyperEdge edge : edges) {
- RexNode inputRefCond =
edge.getCondition().accept(inputName2InputRefShuttle);
+ RexNode inputRefCond = edge.getCondition().accept(shuttle);
joinConds.add(inputRefCond);
}
- return RexUtil.composeConjunction(left.getCluster().getRexBuilder(),
joinConds);
+ return RexUtil.composeConjunction(getCluster().getRexBuilder(), joinConds);
}
/**
- * Before starting enumeration, add Project on every input, make all field
name unique.
- * Convert the HyperEdge condition from RexInputRef to RexInputFieldName
+ * Restore the projection order of the final result to the original plan.
+ *
+ * @param resultOrder the node order of the final result
+ * @param rowTypeList rowType of the final result
+ * @return list of RexInputRef
*/
- public void convertHyperEdgeCond(RelBuilder builder) {
- int fieldIndex = 0;
- List<RelDataTypeField> fieldList = rowType.getFieldList();
- for (int nodeIndex = 0; nodeIndex < inputs.size(); nodeIndex++) {
- RelNode input = inputs.get(nodeIndex);
- List<RexNode> projects = new ArrayList<>();
- List<String> names = new ArrayList<>();
- for (int i = 0; i < input.getRowType().getFieldCount(); i++) {
- projects.add(
- new RexInputRef(
- i,
- fieldList.get(fieldIndex).getType()));
- names.add(fieldList.get(fieldIndex).getName());
- fieldIndex++;
+ public List<RexNode> restoreProjectionOrder(
+ ImmutableList<Integer> resultOrder,
+ List<RelDataTypeField> rowTypeList) {
+ Map<Integer, Integer> relativePositionInNode = new HashMap<>();
+ int fieldCount = 0;
+ for (int resultIndex : resultOrder) {
+ relativePositionInNode.put(resultIndex, fieldCount);
+ if (!LongBitmap.isOverlap(notProjectInputs,
LongBitmap.newBitmap(resultIndex))) {
+ fieldCount += inputs.get(resultIndex).getRowType().getFieldCount();
}
-
- builder.push(input)
- .project(projects, names, true);
- replaceInput(nodeIndex, builder.build());
}
-
- RexShuttle inputRef2inputNameShuttle = new RexShuttle() {
- @Override public RexNode visitInputRef(RexInputRef inputRef) {
- int index = inputRef.getIndex();
- return new RexInputFieldName(
- fieldList.get(index).getName(),
- fieldList.get(index).getType());
+ List<RexNode> projects = new ArrayList<>();
+ for (int inputIndex = 0; inputIndex < inputs.size(); inputIndex++) {
+ if (LongBitmap.isOverlap(notProjectInputs,
LongBitmap.newBitmap(inputIndex))) {
+ continue;
}
- };
- for (int i = 0; i < edges.size(); i++) {
- HyperEdge edge = edges.get(i);
- RexNode convertCond =
edge.getCondition().accept(inputRef2inputNameShuttle);
- HyperEdge convertEdge =
- new HyperEdge(
- edge.getLeftNodeBitmap(),
- edge.getRightNodeBitmap(),
- edge.getJoinType(),
- convertCond);
- edges.set(i, convertEdge);
+ for (int i = 0; i < inputs.get(inputIndex).getRowType().getFieldCount();
i++) {
+ Integer fieldOffset = relativePositionInNode.get(inputIndex);
+ if (fieldOffset == null) {
+ throw new DpHyp.DphypOrHyperGraphException(
+ "The result order loses the " + inputIndex + "-th input");
+ }
+ int inputRef = i + fieldOffset;
+ projects.add(
+ new RexInputRef(inputRef, rowTypeList.get(inputRef).getType()));
+ }
}
+ return projects;
}
/**
* Adjusting RexInputRef in enumeration process is too complicated,
- * so use unique name replace input ref.
- * Before starting enumeration, convert RexInputRef to RexInputFieldName.
- * When connect csgcmp to Join, convert RexInputFieldName to RexInputRef.
+ * so use node index and relative position of field in node replace
RexInputRef.
+ * When build hyper graph, convert RexInputRef to RexNodeAndFieldIndex.
+ * When connect csgcmp to Join, convert RexNodeAndFieldIndex to RexInputRef.
*/
- private static class RexInputFieldName extends RexVariable {
+ static class RexNodeAndFieldIndex extends RexVariable {
Review Comment:
In the process of constructing a hypergraph, it needs to be generated before
`new HyperGraph()`, so I define it as static.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]