Author: shawkins
Date: 2012-05-15 17:26:51 -0400 (Tue, 15 May 2012)
New Revision: 4105
Modified:
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/FrameUtil.java
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RuleCollapseSource.java
trunk/engine/src/test/java/org/teiid/query/processor/TestInsertProcessing.java
Log:
TEIID-2045 fix for insert with query expression
Modified:
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/FrameUtil.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/FrameUtil.java 2012-05-15
21:26:51 UTC (rev 4104)
+++
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/FrameUtil.java 2012-05-15
21:26:51 UTC (rev 4105)
@@ -504,7 +504,7 @@
if(sourceNode.getType() != NodeConstants.Types.SOURCE) {
sourceNode = sourceNode.getFirstChild();
}
- if(sourceNode != null && sourceNode.getType() ==
NodeConstants.Types.SOURCE) {
+ if(sourceNode != null && sourceNode.getType() ==
NodeConstants.Types.SOURCE && sourceNode.getChildCount() == 0) {
Command command = (Command)
sourceNode.getProperty(NodeConstants.Info.VIRTUAL_COMMAND);
if(! (command instanceof QueryCommand)) {
return command;
Modified:
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RuleCollapseSource.java
===================================================================
---
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RuleCollapseSource.java 2012-05-15
21:26:51 UTC (rev 4104)
+++
trunk/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RuleCollapseSource.java 2012-05-15
21:26:51 UTC (rev 4105)
@@ -100,7 +100,11 @@
addDistinct(metadata, capFinder, accessNode, queryCommand);
command = queryCommand;
if (intoGroup != null) {
- Insert insertCommand = new Insert(intoGroup,
ResolverUtil.resolveElementsInGroup(intoGroup, metadata), null);
+ Insert insertCommand =
(Insert)commandRoot.getParent().getProperty(NodeConstants.Info.VIRTUAL_COMMAND);
+ if (insertCommand == null) {
+ //TODO: this is probably no longer needed as we rewrite select into
+ insertCommand = new Insert(intoGroup,
ResolverUtil.resolveElementsInGroup(intoGroup, metadata), null);
+ }
insertCommand.setQueryExpression(queryCommand);
command = insertCommand;
}
Modified: trunk/engine/src/test/java/org/teiid/query/processor/TestInsertProcessing.java
===================================================================
---
trunk/engine/src/test/java/org/teiid/query/processor/TestInsertProcessing.java 2012-05-15
21:26:51 UTC (rev 4104)
+++
trunk/engine/src/test/java/org/teiid/query/processor/TestInsertProcessing.java 2012-05-15
21:26:51 UTC (rev 4105)
@@ -15,6 +15,7 @@
import org.teiid.query.metadata.QueryMetadataInterface;
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.FakeCapabilitiesFinder;
import org.teiid.query.optimizer.capabilities.SourceCapabilities.Capability;
import org.teiid.query.sql.lang.BatchedUpdateCommand;
@@ -369,7 +370,7 @@
// if not doBulkInsert and is doBatching,
// check the command hist to ensure it contains the expected commands
if ( !doBulkInsert && doBatching ) {
- BatchedUpdateCommand bu = (BatchedUpdateCommand)new
ArrayList(dataManager.getCommandHistory()).get(2);
+ BatchedUpdateCommand bu =
(BatchedUpdateCommand)dataManager.getCommandHistory().get(2);
assertEquals(2, bu.getUpdateCommands().size());
assertEquals( "INSERT INTO pm1.g2 (pm1.g2.e1, pm1.g2.e2, pm1.g2.e3,
pm1.g2.e4) VALUES ('1', 1, FALSE, 1.0)",
bu.getUpdateCommands().get(0).toString() ); //$NON-NLS-1$
assertEquals( "INSERT INTO pm1.g2 (pm1.g2.e1, pm1.g2.e2, pm1.g2.e3,
pm1.g2.e4) VALUES ('2', 2, TRUE, 2.0)",
bu.getUpdateCommands().get(1).toString() ); //$NON-NLS-1$
@@ -392,5 +393,41 @@
// Run query
helpProcess(plan, dataManager, expected);
}
+
+ @Test public void testInsertQueryExpression() throws Exception {
+ String sql = "insert into pm1.g1 select * from pm1.g2"; //$NON-NLS-1$
+ BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
+ caps.setCapabilitySupport(Capability.INSERT_WITH_QUERYEXPRESSION, true);
+ DefaultCapabilitiesFinder capFinder = new DefaultCapabilitiesFinder(caps);
+
+ QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
+
+ Command command = helpParse(sql);
+ ProcessorPlan plan = helpGetPlan(command, metadata, capFinder);
+
+ HardcodedDataManager dataManager = new HardcodedDataManager(metadata);
+ List<?>[] expected = new List<?>[] {Arrays.asList(1)};
+ dataManager.addData("INSERT INTO g1 (e1, e2, e3, e4) SELECT g2.e1, g2.e2, g2.e3,
g2.e4 FROM g2", expected);
+ helpProcess(plan, dataManager, expected);
+ }
+
+ @Test public void testInsertQueryExpression1() throws Exception {
+ String sql = "insert into pm1.g1 (e1) select e1 from pm1.g2";
//$NON-NLS-1$
+ BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
+ caps.setCapabilitySupport(Capability.INSERT_WITH_QUERYEXPRESSION, true);
+ DefaultCapabilitiesFinder capFinder = new DefaultCapabilitiesFinder(caps);
+
+ QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
+
+ Command command = helpParse(sql);
+
+ ProcessorPlan plan = helpGetPlan(command, metadata, capFinder);
+
+ HardcodedDataManager dataManager = new HardcodedDataManager(metadata);
+ List<?>[] expected = new List<?>[] {Arrays.asList(1)};
+ dataManager.addData("INSERT INTO g1 (e1) SELECT g2.e1 FROM g2", expected);
+ helpProcess(plan, dataManager, expected);
+ }
+
}