aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/main
diff options
context:
space:
mode:
authorJacques Nadeau <jacques@apache.org>2014-07-30 18:53:28 -0700
committerJacques Nadeau <jacques@apache.org>2014-07-30 19:25:58 -0700
commit98b208e262dd9b54988f5eeffacea7d0e83c958c (patch)
tree92dbccb9c5bf33d0184b2102fd679e43a7e6d0a7 /exec/java-exec/src/main
parentcea40d2eafd7cdf3a29f86f4fcd23ed2a026d08b (diff)
Fix regression in field aliasing.
Diffstat (limited to 'exec/java-exec/src/main')
-rw-r--r--exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/visitor/StarColumnConverter.java87
1 files changed, 45 insertions, 42 deletions
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/visitor/StarColumnConverter.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/visitor/StarColumnConverter.java
index ac7fb8e91..ddc8cf67a 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/visitor/StarColumnConverter.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/physical/visitor/StarColumnConverter.java
@@ -43,39 +43,42 @@ public class StarColumnConverter extends BasePrelVisitor<Prel, boolean[], Runtim
private static StarColumnConverter INSTANCE = new StarColumnConverter();
private static final AtomicLong tableNumber = new AtomicLong(0);
-
+
public static Prel insertRenameProject(Prel root, RelDataType origRowType){
- // Insert top project to do rename only when : 1) there is a join
+ // Insert top project to do rename only when : 1) there is a join
// 2) there is a SCAN with * column. We pass two boolean to keep track of
- // these two conditions.
+ // these two conditions.
boolean [] renamedForStar = new boolean [2];
renamedForStar[0] = false;
renamedForStar[1] = false;
-
- //root should be screen / writer : no need to rename for the root.
+
+ //root should be screen / writer : no need to rename for the root.
Prel child = ((Prel) root.getInput(0)).accept(INSTANCE, renamedForStar);
-
+
if (renamedForStar[0] && renamedForStar[1]) {
List<RexNode> exprs = Lists.newArrayList();
for (int i = 0; i < origRowType.getFieldCount(); i++) {
RexNode expr = child.getCluster().getRexBuilder().makeInputRef(origRowType.getFieldList().get(i).getType(), i);
exprs.add(expr);
}
-
+
RelDataType newRowType = RexUtil.createStructType(child.getCluster().getTypeFactory(), exprs, origRowType.getFieldNames());
-
- // Insert a top project which allows duplicate columns.
+
+ // Insert a top project which allows duplicate columns.
child = new ProjectAllowDupPrel(child.getCluster(), child.getTraitSet(), child, exprs, newRowType);
+ List<RelNode> children = Lists.newArrayList();
+ children.add( child);
+ return (Prel) root.copy(root.getTraitSet(), children);
+
+ }else{
+ return root;
}
-
- List<RelNode> children = Lists.newArrayList();
- children.add( child);
- return (Prel) root.copy(root.getTraitSet(), children);
+
}
-
+
@Override
public Prel visitPrel(Prel prel, boolean [] renamedForStar) throws RuntimeException {
List<RelNode> children = Lists.newArrayList();
@@ -84,14 +87,14 @@ public class StarColumnConverter extends BasePrelVisitor<Prel, boolean[], Runtim
children.add(child);
}
- // For project, we need make sure that the project's field name is same as the input,
+ // For project, we need make sure that the project's field name is same as the input,
// when the project expression is RexInPutRef. This is necessary since Optiq may use
- // an arbitrary name for the project's field name.
+ // an arbitrary name for the project's field name.
if (prel instanceof ProjectPrel) {
RelNode child = children.get(0);
-
+
List<String> fieldNames = Lists.newArrayList();
-
+
for (Pair<String, RexNode> pair : Pair.zip(prel.getRowType().getFieldNames(), ((ProjectPrel) prel).getProjects())) {
if (pair.right instanceof RexInputRef) {
String name = child.getRowType().getFieldNames().get(((RexInputRef) pair.right).getIndex());
@@ -100,32 +103,32 @@ public class StarColumnConverter extends BasePrelVisitor<Prel, boolean[], Runtim
fieldNames.add(pair.left);
}
}
-
- // Make sure the field names are unique : Optiq does not allow duplicate field names in a rowType.
+
+ // Make sure the field names are unique : Optiq does not allow duplicate field names in a rowType.
fieldNames = makeUniqueNames(fieldNames);
-
+
RelDataType rowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), ((ProjectPrel) prel).getProjects(), fieldNames);
return (Prel) new ProjectPrel(prel.getCluster(), prel.getTraitSet(), children.get(0), ((ProjectPrel) prel).getProjects(), rowType);
- } else {
+ } else {
return (Prel) prel.copy(prel.getTraitSet(), children);
}
}
-
+
@Override
public Prel visitJoin(JoinPrel prel, boolean [] renamedForStar) throws RuntimeException {
- renamedForStar[0] = true; // indicate there is a join, which may require top rename projet operator.
+ renamedForStar[0] = true; // indicate there is a join, which may require top rename projet operator.
return visitPrel(prel, renamedForStar);
}
-
+
@Override
public Prel visitScan(ScanPrel scanPrel, boolean [] renamedForStar) throws RuntimeException {
if (StarColumnHelper.containsStarColumn(scanPrel.getRowType()) && renamedForStar[0] ) {
-
- renamedForStar[1] = true; // indicate there is * for a SCAN operator.
-
+
+ renamedForStar[1] = true; // indicate there is * for a SCAN operator.
+
List<RexNode> exprs = Lists.newArrayList();
for (RelDataTypeField field : scanPrel.getRowType().getFieldList()) {
@@ -134,34 +137,34 @@ public class StarColumnConverter extends BasePrelVisitor<Prel, boolean[], Runtim
}
List<String> fieldNames = Lists.newArrayList();
-
+
long tableId = tableNumber.getAndIncrement();
-
+
for (String name : scanPrel.getRowType().getFieldNames()) {
fieldNames.add("T" + tableId + StarColumnHelper.PREFIX_DELIMITER + name);
}
RelDataType rowType = RexUtil.createStructType(scanPrel.getCluster().getTypeFactory(), exprs, fieldNames);
ProjectPrel proj = new ProjectPrel(scanPrel.getCluster(), scanPrel.getTraitSet(), scanPrel, exprs, rowType);
-
+
return proj;
} else {
return visitPrel(scanPrel, renamedForStar);
}
}
-
+
private List<String> makeUniqueNames(List<String> names) {
-
+
// We have to search the set of original names, plus the set of unique names that will be used finally .
- // Eg : the original names : ( C1, C1, C10 )
+ // Eg : the original names : ( C1, C1, C10 )
// There are two C1, we may rename C1 to C10, however, this new name will conflict with the original C10.
// That means we should pick a different name that does not conflict with the original names, in additional
- // to make sure it's unique in the set of unique names.
-
- HashSet<String> uniqueNames = new HashSet<String>();
- HashSet<String> origNames = new HashSet<String>(names);
-
+ // to make sure it's unique in the set of unique names.
+
+ HashSet<String> uniqueNames = new HashSet<String>();
+ HashSet<String> origNames = new HashSet<String>(names);
+
List<String> newNames = Lists.newArrayList();
for (String s : names) {
@@ -170,13 +173,13 @@ public class StarColumnConverter extends BasePrelVisitor<Prel, boolean[], Runtim
s = s + i;
if (! origNames.contains(s) && ! uniqueNames.contains(s))
break;
- }
+ }
}
uniqueNames.add(s);
newNames.add(s);
}
-
+
return newNames;
}
-
+
}