Github user jihoonson commented on a diff in the pull request:
https://github.com/apache/tajo/pull/24#discussion_r13267016
--- Diff:
tajo-core/src/main/java/org/apache/tajo/engine/planner/global/GlobalPlanner.java
---
@@ -1195,10 +1192,59 @@ public LogicalNode
visitTableSubQuery(GlobalPlanContext context, LogicalPlan pla
ExecutionBlock currentBlock =
context.execBlockMap.remove(child.getPID());
if (child.getType() == NodeType.UNION) {
+ List<TableSubQueryNode> addedTableSubQueries = new
ArrayList<TableSubQueryNode>();
+ TableSubQueryNode leftMostUnionNode = null;
for (ExecutionBlock childBlock :
context.plan.getChilds(currentBlock.getId())) {
TableSubQueryNode copy = PlannerUtil.clone(plan, node);
copy.setSubQuery(childBlock.getPlan());
childBlock.setPlan(copy);
+ addedTableSubQueries.add(copy);
+
+ //Find a SubQueryNode which contains all columns in InputSchema
matched with Target and OutputSchema's column
+ if
(copy.getInSchema().containsAll(copy.getOutSchema().getColumns())) {
+ for (Target eachTarget : copy.getTargets()) {
+ Set<Column> columns =
EvalTreeUtil.findUniqueColumns(eachTarget.getEvalTree());
+ if (copy.getInSchema().containsAll(columns)) {
+ leftMostUnionNode = copy;
+ }
+ }
+ }
+ }
+
+ if (leftMostUnionNode != null) {
+ // replace target column name
+ Target[] targets = leftMostUnionNode.getTargets();
+ int[] targetMappings = new int[targets.length];
+ for (int i = 0; i < targets.length; i++) {
+ if (targets[i].getEvalTree().getType() != EvalType.FIELD) {
+ throw new PlanningException("Target of a UnionNode's
subquery should be FieldEval.");
+ }
+ int index =
leftMostUnionNode.getInSchema().getColumnId(targets[i].getNamedColumn().getQualifiedName());
+ targetMappings[i] = index;
+ }
+
+ for (TableSubQueryNode eachNode: addedTableSubQueries) {
+ if (eachNode.getPID() == leftMostUnionNode.getPID()) {
+ continue;
+ }
+ Target[] eachNodeTargets = eachNode.getTargets();
+ if (eachNodeTargets.length != targetMappings.length) {
--- End diff --
In my opinion, this restriction would be better to checked in
LogicalPlanVerifier. What do you think about it?
In addition, the exception message should be fixed with "Children of union
node should have the same number of targets.", because Union node can't have
any targets.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---