Skip to content

Commit

Permalink
#1505 fix using bug (#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsun314 authored and yanhuqing666 committed Nov 20, 2019
1 parent 211c3e5 commit 24b4a4e
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,49 @@ public boolean visit(SQLJoinTableSource x) {
inOuterJoin = false;
break;
}
boolean result = super.visit(x);

SQLTableSource left = x.getLeft(), right = x.getRight();

left.accept(this);
right.accept(this);

SQLExpr condition = x.getCondition();
if (condition != null) {
condition.accept(this);
}

if (x.getUsing().size() > 0 &&
left instanceof SQLExprTableSource && right instanceof SQLExprTableSource) {
SQLExpr leftExpr = ((SQLExprTableSource) left).getExpr();
SQLExpr rightExpr = ((SQLExprTableSource) right).getExpr();

for (SQLExpr expr : x.getUsing()) {
if (expr instanceof SQLIdentifierExpr) {
String name = ((SQLIdentifierExpr) expr).getName();
/*
when the shard1 a join shard2 b using(id)
the intermediate condition should be a.id = b.id instead of shard1.id = shard2.id
*/
SQLPropertyExpr leftPropExpr = new SQLPropertyExpr(leftExpr, name);
if (left.getAlias() != null) {
leftPropExpr.setOwner(left.getAlias());
}
SQLPropertyExpr rightPropExpr = new SQLPropertyExpr(rightExpr, name);
if (right.getAlias() != null) {
rightPropExpr.setOwner(right.getAlias());
}

leftPropExpr.setResolvedTableSource(left);
rightPropExpr.setResolvedTableSource(right);

SQLBinaryOpExpr usingCondition = new SQLBinaryOpExpr(leftPropExpr, SQLBinaryOperator.Equality, rightPropExpr);
usingCondition.accept(this);
}
}
}

inOuterJoin = false;
return result;
return false;
}

@Override
Expand Down Expand Up @@ -229,6 +269,7 @@ public boolean visit(SQLBetweenExpr x) {
return true;
}


@Override
public boolean visit(SQLBinaryOpExpr x) {
if (isUnaryParentEffect(x)) return true;
Expand Down

0 comments on commit 24b4a4e

Please sign in to comment.