Skip to content

Commit

Permalink
TEIID-2381 ensuring proper handling with unions and inline views
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins authored and johnathonlee committed Sep 11, 2014
1 parent 4f874a6 commit d42dc33
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Expand Up @@ -100,14 +100,14 @@ private void helpTestVisitor(String input, String expectedOutput) throws Transla

@Test public void testSourceHint2() throws Exception {
ExecutionContextImpl impl = new FakeExecutionContextImpl();
impl.setHint("hello world");
impl.setHints(Arrays.asList("hello world"));
helpTestVisitor(getTestVDB(), "with x (y) as (select part_name from parts) select y from x", impl, null, "WITH x AS (SELECT PARTS.PART_NAME AS y FROM PARTS) SELECT /*+ hello world */ g_0.y FROM x g_0", true);
}

@Test public void testSourceHint3() throws Exception {
ExecutionContextImpl impl = new FakeExecutionContextImpl();
impl.setHint("hello world");
impl.setGeneralHint("other");
impl.setHints(Arrays.asList("hello world"));
impl.setGeneralHints(Arrays.asList("other"));
helpTestVisitor(getTestVDB(), "select part_name from parts", impl, null, "SELECT /*+ hello world other */ g_0.PART_NAME FROM PARTS g_0", true);
}

Expand Down
Expand Up @@ -282,6 +282,7 @@ private static void assignWithClause(RelationalNode node, Map<String, WithQueryC
WithQueryCommand clause = pushdownWith.get(groupSymbol.getNonCorrelationName());
if (clause != null) {
with.add(clause.clone());
command.setSourceHint(SourceHint.combine(command.getSourceHint(), clause.getCommand().getSourceHint()));
}
}
if (!with.isEmpty()) {
Expand Down Expand Up @@ -865,12 +866,14 @@ PlanNode createQueryPlan(QueryCommand command)
} else {
hints.hasSetQuery = true;
SetQuery query = (SetQuery)command;
SourceHint previous = this.sourceHint;
this.sourceHint = SourceHint.combine(previous, query.getProjectedQuery().getSourceHint());
PlanNode leftPlan = createQueryPlan( query.getLeftQuery());
PlanNode rightPlan = createQueryPlan( query.getRightQuery());
node = NodeFactory.getNewNode(NodeConstants.Types.SET_OP);
node.setProperty(NodeConstants.Info.SET_OPERATION, query.getOperation());
node.setProperty(NodeConstants.Info.USE_ALL, query.isAll());

this.sourceHint = previous;
attachLast(node, leftPlan);
attachLast(node, rightPlan);
}
Expand Down Expand Up @@ -1068,8 +1071,13 @@ void buildTree(FromClause clause, PlanNode parent)
if (sfc.isNoUnnest()) {
node.setProperty(Info.NO_UNNEST, Boolean.TRUE);
}
SourceHint previous = this.sourceHint;
if (nestedCommand.getSourceHint() != null) {
this.sourceHint = SourceHint.combine(previous, nestedCommand.getSourceHint());
}
node.addGroup(group);
addNestedCommand(node, group, nestedCommand, nestedCommand, true, false);
this.sourceHint = previous;
if (nestedCommand instanceof SetQuery) {
Map<ElementSymbol, List<Set<Constant>>> partitionInfo = PartitionAnalyzer.extractPartionInfo((SetQuery)nestedCommand, ResolverUtil.resolveElementsInGroup(group, metadata));
if (!partitionInfo.isEmpty()) {
Expand Down
Expand Up @@ -96,6 +96,7 @@ public PlanNode execute(PlanNode plan, QueryMetadataInterface metadata, Capabili
addDistinct(metadata, capFinder, accessNode, queryCommand);
command = queryCommand;
queryCommand.setSourceHint((SourceHint) accessNode.getProperty(Info.SOURCE_HINT));
queryCommand.getProjectedQuery().setSourceHint((SourceHint) accessNode.getProperty(Info.SOURCE_HINT));
if (intoGroup != null) {
Insert insertCommand = (Insert)commandRoot.getParent().getProperty(NodeConstants.Info.VIRTUAL_COMMAND);
if (insertCommand == null) {
Expand Down
Expand Up @@ -30,13 +30,17 @@
import org.junit.Test;
import org.teiid.common.buffer.TupleSource;
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidException;
import org.teiid.dqp.internal.process.DQPWorkContext;
import org.teiid.metadata.MetadataStore;
import org.teiid.metadata.Schema;
import org.teiid.metadata.Table;
import org.teiid.query.mapping.relational.QueryNode;
import org.teiid.query.metadata.TransformationMetadata;
import org.teiid.query.optimizer.TestOptimizer;
import org.teiid.query.optimizer.capabilities.BasicSourceCapabilities;
import org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder;
import org.teiid.query.optimizer.capabilities.SourceCapabilities.Capability;
import org.teiid.query.sql.lang.Command;
import org.teiid.query.unittest.RealMetadataFactory;
import org.teiid.query.util.CommandContext;
Expand All @@ -63,6 +67,36 @@ public class TestSourceHints {
helpProcess(plan, manager("foo x", "leading", "foo", "leading"), expected);
}

@Test public void testWithHintPushdown() throws TeiidException {
String sql = "WITH x as (SELECT /*+ sh:'x' */ e1 from pm1.g2) " +
"SELECT /*+ sh:'foo' bar:'leading' */ g1.e1 from pm1.g1, x where g1.e1 = x.e1 order by g1.e1 limit 1"; //$NON-NLS-1$

BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
caps.setCapabilitySupport(Capability.COMMON_TABLE_EXPRESSIONS, true);
CommandContext context = new CommandContext();
context.setDQPWorkContext(new DQPWorkContext());
context.getDQPWorkContext().getSession().setVdb(RealMetadataFactory.example1VDB());
ProcessorPlan plan = helpGetPlan(helpParse(sql), RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(caps), context);

List<?>[] expected = new List[] {};
helpProcess(plan, manager("foo x", "leading"), expected);
}

@Test public void testUnionHintPushdown() throws TeiidException {
String sql = "SELECT /*+ sh:'foo' bar:'leading' */ g1.e1 from pm1.g1 " +
"UNION ALL SELECT * from (SELECT /*+ sh:'x' bar:'z' */ g1.e1 from pm1.g1) as x"; //$NON-NLS-1$

BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
caps.setCapabilitySupport(Capability.QUERY_UNION, true);
CommandContext context = new CommandContext();
context.setDQPWorkContext(new DQPWorkContext());
context.getDQPWorkContext().getSession().setVdb(RealMetadataFactory.example1VDB());
ProcessorPlan plan = helpGetPlan(helpParse(sql), RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(caps), context);

List<?>[] expected = new List[] {};
helpProcess(plan, manager("foo x", "leading z"), expected);
}

@Test public void testKeepAliases() throws Exception {
String sql = "SELECT /*+ sh KEEP ALIASES bar:'leading(g)' */ e1 from pm1.g1 g order by e1 limit 1"; //$NON-NLS-1$
CommandContext cc = TestProcessor.createCommandContext();
Expand Down

0 comments on commit d42dc33

Please sign in to comment.