Skip to content

Commit

Permalink
Address Issue bigdawg-istc#15 by attempting to distinguish join types…
Browse files Browse the repository at this point in the history
… and acting accordingly
  • Loading branch information
mmucklo committed Apr 24, 2020
1 parent 1d48835 commit 35d69a8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ public String generateJoinFilter() throws IslandException {
return null;
}


@Override
public JoinType getJoinType() {
return joinType;
}

};
6 changes: 4 additions & 2 deletions src/main/java/istc/bigdawg/islands/operators/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

public interface Join extends Operator {

public enum JoinType {Left, Natural, Right, Cross};
// Need to suppor the following join types
// https://github.com/postgres/postgres/blob/ab7646ff92c799303b9ee70ce88b89e07dae717c/src/backend/commands/explain.c#L1475
public enum JoinType {Left, Natural, Right, Cross, Inner, Full, Semi, Anti, Simple};

// creates a default and call this to create a new Join instance
public Join construct(Operator child0, Operator child1, JoinType jt, String joinPred, boolean isFilter) throws Exception;
Expand All @@ -26,5 +28,5 @@ public enum JoinType {Left, Natural, Right, Cross};
public String getJoinToken();
public Integer getJoinID();
public void setJoinID(Integer joinID);

public JoinType getJoinType();
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import org.apache.log4j.Logger;


public class SQLIslandJoin extends SQLIslandOperator implements Join {
Expand Down Expand Up @@ -99,6 +100,15 @@ public SQLIslandJoin (Map<String, String> parameters, List<String> output, SQLIs
((SQLIslandAggregate)o).setSingledOutAggregate();
}
}

if (parameters.containsKey("Join-Type")) {
try {
joinType = JoinType.valueOf(parameters.get("Join-Type"));
} catch (IllegalArgumentException e) {
// unknown join type
Logger.getLogger(this.getClass().getName()).warn("Unknown Join-Type returned: '" + parameters.get("Join-Type") + "'");
}
}

}

Expand Down Expand Up @@ -447,4 +457,9 @@ public String generateJoinPredicate() throws IslandException {
public String generateJoinFilter() throws IslandException {
return joinFilter != null ? new String(joinFilter): null;
}

@Override
public JoinType getJoinType() {
return joinType;
}
};
48 changes: 42 additions & 6 deletions src/main/java/istc/bigdawg/shims/PostgreSQLQueryGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import java.util.stream.Collectors;

import istc.bigdawg.islands.relational.SQLJSONPlaceholderParser;
import istc.bigdawg.islands.relational.SQLParseLogical;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import istc.bigdawg.islands.operators.Aggregate;
import istc.bigdawg.islands.operators.CommonTableExpressionScan;
import istc.bigdawg.islands.operators.Distinct;
import istc.bigdawg.islands.operators.Join;
import istc.bigdawg.islands.operators.Join.JoinType;
import istc.bigdawg.islands.operators.Limit;
import istc.bigdawg.islands.operators.Merge;
import istc.bigdawg.islands.operators.Operator;
Expand Down Expand Up @@ -65,6 +67,7 @@
import net.sf.jsqlparser.statement.select.ValuesList;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.util.SelectUtils;
import org.apache.log4j.Logger;

public class PostgreSQLQueryGenerator implements OperatorQueryGenerator {

Expand Down Expand Up @@ -130,7 +133,6 @@ public void visit(Operator operator) throws Exception {

@Override
public void visit(Join joinOp) throws Exception {

SQLIslandJoin join = (SQLIslandJoin) joinOp;

if (!isRoot && join.isPruned()) {
Expand Down Expand Up @@ -285,7 +287,7 @@ public void visit(Join joinOp) throws Exception {
child0.accept(this);
}
// Resolve pruning and add join
addJSQLParserJoin(dstStatement, t1);
net.sf.jsqlparser.statement.select.Join newJoin = addJSQLParserJoin(dstStatement, t1, join.getJoinType());

if (child1 instanceof Aggregate && stopAtJoin) {
List<SelectItem> sil = new ArrayList<>();
Expand Down Expand Up @@ -401,8 +403,18 @@ public void visit(Join joinOp) throws Exception {
treeWalker = nextGen;
}

((PlainSelect) dstStatement.getSelectBody()).setWhere(CCJSqlParserUtil.parseCondExpression(e.toString()));

if (newJoin.isSimple()) {
// As long as postgres alone is being used to generate query plans, this statement is not typically
// reached as the planner doesn't seem to distinguish between INNER JOIN and Simple, even though
// technically there is a syntactical distinction.
//
// One exception is the case of join types we presently don't handle (such as an Anti Join).
// Leaving this here, then, is no worse off than things originally were prior to this change.
// (4/24/2020)
((PlainSelect) dstStatement.getSelectBody()).setWhere(CCJSqlParserUtil.parseCondExpression(e.toString()));
} else {
newJoin.setOnExpression(e);
}
}

}
Expand Down Expand Up @@ -1915,13 +1927,37 @@ private List<String> processLeftAndRightWithIndexCond(SQLIslandOperator o, boole
* @param dstStatement
* @param t
*/
private void addJSQLParserJoin(Select dstStatement, Table t) {
private net.sf.jsqlparser.statement.select.Join addJSQLParserJoin(Select dstStatement, Table t, JoinType joinType) {
net.sf.jsqlparser.statement.select.Join newJ = new net.sf.jsqlparser.statement.select.Join();
newJ.setRightItem(t);
newJ.setSimple(true);
switch(joinType) {
case Simple:
newJ.setSimple(true);
break;
case Left:
newJ.setLeft(true);
break;
case Right:
newJ.setRight(true);
break;
case Cross:
newJ.setCross(true);
break;
case Full:
newJ.setFull(true);
break;
case Inner:
newJ.setInner(true);
break;
default:
Logger.getLogger(PostgreSQLQueryGenerator.class).warn("Unknown Join type: " + joinType.toString() + " reverting to simple.");
newJ.setSimple(true);
break;
}
if (((PlainSelect) dstStatement.getSelectBody()).getJoins() == null)
((PlainSelect) dstStatement.getSelectBody()).setJoins(new ArrayList<>());
((PlainSelect) dstStatement.getSelectBody()).getJoins().add(newJ);
return newJ;
}

/**
Expand Down

0 comments on commit 35d69a8

Please sign in to comment.