Skip to content

Commit

Permalink
HIVE-11152 : Swapping join inputs in ASTConverter (Jesus Camacho Rodr…
Browse files Browse the repository at this point in the history
…iguez via John Pullokkaran)
  • Loading branch information
ashutoshc committed Jul 1, 2015
1 parent b5fb31c commit 6eaa32c
Show file tree
Hide file tree
Showing 31 changed files with 3,222 additions and 3,345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.calcite.rel.core.AggregateCall;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Join;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.SemiJoin;
import org.apache.calcite.rel.core.Sort;
Expand Down Expand Up @@ -285,9 +286,24 @@ private QueryBlockInfo convertSource(RelNode r) throws CalciteSemanticException
s = new Schema(left.schema, right.schema);
ASTNode cond = join.getCondition().accept(new RexVisitor(s));
boolean semiJoin = join instanceof SemiJoin;
ast = ASTBuilder.join(left.ast, right.ast, join.getJoinType(), cond, semiJoin);
if (semiJoin)
if (join.getRight() instanceof Join) {
// Invert join inputs; this is done because otherwise the SemanticAnalyzer
// methods to merge joins will not kick in
JoinRelType type;
if (join.getJoinType() == JoinRelType.LEFT) {
type = JoinRelType.RIGHT;
} else if (join.getJoinType() == JoinRelType.RIGHT) {
type = JoinRelType.LEFT;
} else {
type = join.getJoinType();
}
ast = ASTBuilder.join(right.ast, left.ast, type, cond, semiJoin);
} else {
ast = ASTBuilder.join(left.ast, right.ast, join.getJoinType(), cond, semiJoin);
}
if (semiJoin) {
s = left.schema;
}
} else if (r instanceof Union) {
RelNode leftInput = ((Union) r).getInput(0);
RelNode rightInput = ((Union) r).getInput(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,15 @@ private static boolean validJoinParent(RelNode joinNode, RelNode parent) {
boolean validParent = true;

if (parent instanceof Join) {
if (((Join) parent).getRight() == joinNode) {
// In Hive AST, right child of join cannot be another join,
// thus we need to introduce a project on top of it.
// But we only need the additional project if the left child
// is another join too; if it is not, ASTConverter will swap
// the join inputs, leaving the join operator on the left.
// This will help triggering multijoin recognition methods that
// are embedded in SemanticAnalyzer.
if (((Join) parent).getRight() == joinNode &&
(((Join) parent).getLeft() instanceof Join) ) {
validParent = false;
}
} else if (parent instanceof SetOp) {
Expand All @@ -255,7 +263,7 @@ private static boolean validJoinParent(RelNode joinNode, RelNode parent) {
private static boolean validFilterParent(RelNode filterNode, RelNode parent) {
boolean validParent = true;

// TOODO: Verify GB having is not a seperate filter (if so we shouldn't
// TODO: Verify GB having is not a separate filter (if so we shouldn't
// introduce derived table)
if (parent instanceof Filter || parent instanceof Join
|| parent instanceof SetOp) {
Expand Down
26 changes: 13 additions & 13 deletions ql/src/test/results/clientpositive/auto_join13.q.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ JOIN
ON src1.c1 + src2.c3 = src3.c5 AND src3.c5 < 200
POSTHOOK: type: QUERY
STAGE DEPENDENCIES:
Stage-7 is a root stage
Stage-2 depends on stages: Stage-7
Stage-0 depends on stages: Stage-2
Stage-8 is a root stage
Stage-3 depends on stages: Stage-8
Stage-0 depends on stages: Stage-3

STAGE PLANS:
Stage: Stage-7
Stage: Stage-8
Map Reduce Local Work
Alias -> Map Local Tables:
$hdt$_0:$hdt$_0:src
Fetch Operator
limit: -1
$hdt$_0:$hdt$_1:$hdt$_1:src
$hdt$_0:$hdt$_1:src
Fetch Operator
limit: -1
Alias -> Map Local Operator Tree:
Expand All @@ -49,9 +49,9 @@ STAGE PLANS:
Statistics: Num rows: 83 Data size: 881 Basic stats: COMPLETE Column stats: NONE
HashTable Sink Operator
keys:
0 UDFToDouble(_col0) (type: double)
1 (UDFToDouble(_col2) + UDFToDouble(_col0)) (type: double)
$hdt$_0:$hdt$_1:$hdt$_1:src
0 (UDFToDouble(_col2) + UDFToDouble(_col0)) (type: double)
1 UDFToDouble(_col0) (type: double)
$hdt$_0:$hdt$_1:src
TableScan
alias: src
Statistics: Num rows: 500 Data size: 5312 Basic stats: COMPLETE Column stats: NONE
Expand All @@ -67,7 +67,7 @@ STAGE PLANS:
0 _col0 (type: string)
1 _col0 (type: string)

Stage: Stage-2
Stage: Stage-3
Map Reduce
Map Operator Tree:
TableScan
Expand Down Expand Up @@ -95,12 +95,12 @@ STAGE PLANS:
condition map:
Inner Join 0 to 1
keys:
0 UDFToDouble(_col0) (type: double)
1 (UDFToDouble(_col2) + UDFToDouble(_col0)) (type: double)
outputColumnNames: _col2, _col3
0 (UDFToDouble(_col2) + UDFToDouble(_col0)) (type: double)
1 UDFToDouble(_col0) (type: double)
outputColumnNames: _col1, _col2
Statistics: Num rows: 100 Data size: 1065 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: hash(_col3,_col2) (type: int)
expressions: hash(_col2,_col1) (type: int)
outputColumnNames: _col0
Statistics: Num rows: 100 Data size: 1065 Basic stats: COMPLETE Column stats: NONE
Group By Operator
Expand Down
Loading

0 comments on commit 6eaa32c

Please sign in to comment.