Skip to content

Commit

Permalink
TEIID-4157 allowing the translator to support nested set ops
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Apr 21, 2016
1 parent 7ec4035 commit adf3ce0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Expand Up @@ -916,10 +916,7 @@ protected boolean useParensForSetQueries() {
}

protected void appendSetQuery(SetQuery parent, QueryExpression obj, boolean right) {
if((!(obj instanceof SetQuery) && useParensForSetQueries())
|| (!useSelectLimit() && (obj.getLimit() != null || obj.getOrderBy() != null)) || (right && ((obj instanceof SetQuery
&& ((parent.isAll() && !((SetQuery)obj).isAll())
|| parent.getOperation() != ((SetQuery)obj).getOperation())) || obj.getLimit() != null || obj.getOrderBy() != null))) {
if(shouldNestSetChild(parent, obj, right)) {
buffer.append(Tokens.LPAREN);
append(obj);
buffer.append(Tokens.RPAREN);
Expand All @@ -930,6 +927,14 @@ protected void appendSetQuery(SetQuery parent, QueryExpression obj, boolean righ
append(obj);
}
}

protected boolean shouldNestSetChild(SetQuery parent, QueryExpression obj,
boolean right) {
return (!(obj instanceof SetQuery) && useParensForSetQueries())
|| (!useSelectLimit() && (obj.getLimit() != null || obj.getOrderBy() != null)) || (right && ((obj instanceof SetQuery
&& ((parent.isAll() && !((SetQuery)obj).isAll())
|| parent.getOperation() != ((SetQuery)obj).getOperation())) || obj.getLimit() != null || obj.getOrderBy() != null));
}

@Override
public void visit(With obj) {
Expand Down
Expand Up @@ -151,11 +151,13 @@ private void addColumn(DerivedColumn dc) {

@Override
public void visit(SetQuery obj) {
//TODO: with hive 1.2, this handling is not necessary
//even with hive 0.13 it's only partially necessary - for distinct
if (obj.getWith() != null) {
append(obj.getWith());
}

Select select = (Select)obj.getLeftQuery();
Select select = obj.getProjectedQuery();
buffer.append(SELECT).append(Tokens.SPACE);
if(!obj.isAll()) {
buffer.append(DISTINCT).append(Tokens.SPACE);
Expand All @@ -164,20 +166,13 @@ public void visit(SetQuery obj) {
buffer.append(Tokens.SPACE);
buffer.append(FROM).append(Tokens.SPACE);
buffer.append(Tokens.LPAREN);
appendSetQuery(obj, obj.getLeftQuery(), false);

appendSetChild(obj, obj.getLeftQuery(), false);

buffer.append(Tokens.SPACE);
appendSetOp(obj);

appendSetOperation(obj.getOperation());

// UNION "ALL" always
buffer.append(Tokens.SPACE);
buffer.append(ALL);
buffer.append(Tokens.SPACE);
appendSetChild(obj, obj.getRightQuery(), true);

appendSetQuery(obj, obj.getRightQuery(), true);

OrderBy orderBy = obj.getOrderBy();
if(orderBy != null) {
buffer.append(Tokens.SPACE);
Expand All @@ -193,6 +188,32 @@ public void visit(SetQuery obj) {
buffer.append(Tokens.SPACE);
buffer.append("X__"); //$NON-NLS-1$
}

private void appendSetOp(SetQuery obj) {
buffer.append(Tokens.SPACE);

appendSetOperation(obj.getOperation());

// UNION "ALL" always
buffer.append(Tokens.SPACE);
buffer.append(ALL);
buffer.append(Tokens.SPACE);
}

private void appendSetChild(SetQuery obj, QueryExpression child, boolean right) {
if (child instanceof Select || shouldNestSetChild(obj, child, right)) {
appendSetQuery(obj, child, right);
} else {
//non-nested set op
SetQuery setQuery = (SetQuery)child;

append(setQuery.getLeftQuery());

appendSetOp(setQuery);

appendSetChild(setQuery, setQuery.getRightQuery(), true);
}
}

@Override
public void visit(Select obj) {
Expand Down
Expand Up @@ -142,6 +142,20 @@ public void testUnionAllRewrite() throws Exception {
helpTestVisitor(bqt, input, output);
}

@Test
public void testUnionAllRewriteNested() throws Exception {
String input = "SELECT intkey, stringkey FROM BQT1.SmallA union all SELECT intkey, stringkey FROM BQT1.Smallb union all SELECT intkey, stringkey FROM BQT1.Smallb";
String output = "SELECT intkey, stringkey FROM (SELECT SmallA.IntKey, SmallA.StringKey FROM SmallA UNION ALL SELECT SmallB.IntKey, SmallB.StringKey FROM SmallB UNION ALL SELECT SmallB.IntKey, SmallB.StringKey FROM SmallB) X__";
helpTestVisitor(bqt, input, output);
}

@Test
public void testUnionAllRewriteNested1() throws Exception {
String input = "SELECT intkey, stringkey FROM BQT1.SmallA union all (SELECT intkey, stringkey FROM BQT1.Smallb union SELECT intkey, stringkey FROM BQT1.Smallb)";
String output = "SELECT intkey, stringkey FROM (SELECT SmallA.IntKey, SmallA.StringKey FROM SmallA UNION ALL (SELECT DISTINCT intkey, stringkey FROM (SELECT SmallB.IntKey, SmallB.StringKey FROM SmallB UNION ALL SELECT SmallB.IntKey, SmallB.StringKey FROM SmallB) X__)) X__";
helpTestVisitor(bqt, input, output);
}

@Test
public void testUnionAllExprRewrite() throws Exception {
String input = "SELECT count(*) as key, stringkey FROM BQT1.SmallA union all SELECT intkey, stringkey FROM BQT1.Smallb";
Expand Down

0 comments on commit adf3ce0

Please sign in to comment.