Skip to content

Commit

Permalink
TEIID-3053 adding detection of local temp table usage
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Dec 8, 2014
1 parent 0cc0a8c commit 71b0746
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.teiid.core.util.Assertion;
import org.teiid.query.analysis.AnalysisRecord;
import org.teiid.query.metadata.QueryMetadataInterface;
import org.teiid.query.metadata.TempMetadataAdapter;
import org.teiid.query.optimizer.capabilities.CapabilitiesFinder;
import org.teiid.query.processor.ProcessorPlan;
import org.teiid.query.processor.proc.*;
Expand All @@ -44,8 +45,10 @@
import org.teiid.query.sql.proc.*;
import org.teiid.query.sql.symbol.ElementSymbol;
import org.teiid.query.sql.symbol.Expression;
import org.teiid.query.sql.symbol.GroupSymbol;
import org.teiid.query.sql.symbol.Reference;
import org.teiid.query.sql.visitor.CommandCollectorVisitor;
import org.teiid.query.sql.visitor.GroupCollectorVisitor;
import org.teiid.query.util.CommandContext;


Expand Down Expand Up @@ -242,7 +245,17 @@ private Object planStatement(CreateProcedureCommand parentProcCommand, Statement
if (command.getType() == Command.TYPE_DYNAMIC){
instruction = new ExecDynamicSqlInstruction(parentProcCommand,((DynamicCommand)command), metadata, idGenerator, capFinder, cmdStmt.isReturnable() );
}else{
instruction = new CreateCursorResultSetInstruction(null, commandPlan, getMode(parentProcCommand, cmdStmt, command));
CreateCursorResultSetInstruction cursor = new CreateCursorResultSetInstruction(null, commandPlan, getMode(parentProcCommand, cmdStmt, command));
if (cursor.getMode() == Mode.HOLD) {
for (GroupSymbol gs : GroupCollectorVisitor.getGroupsIgnoreInlineViews(command, false)) {
if (gs.isTempTable() && metadata.getModelID(gs.getMetadataID()) == TempMetadataAdapter.TEMP_MODEL) {
cursor.setUsesLocalTemp(true);
break;
}
}

}
instruction = cursor;
//handle stored procedure calls
if (command.getType() == Command.TYPE_STORED_PROCEDURE) {
StoredProcedure sp = (StoredProcedure)command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum Mode {
protected ProcessorPlan plan;
private Mode mode;
private Map<ElementSymbol, ElementSymbol> procAssignments;
private boolean usesLocalTemp;

public CreateCursorResultSetInstruction(String rsName, ProcessorPlan plan, Mode mode){
this.rsName = rsName;
Expand All @@ -65,7 +66,7 @@ public void setProcAssignments(
public void process(ProcedurePlan procEnv)
throws BlockedException, TeiidComponentException, TeiidProcessingException {

procEnv.executePlan(plan, rsName, procAssignments, mode);
procEnv.executePlan(plan, rsName, procAssignments, mode, usesLocalTemp);
}

/**
Expand All @@ -75,6 +76,7 @@ public CreateCursorResultSetInstruction clone(){
ProcessorPlan clonedPlan = this.plan.clone();
CreateCursorResultSetInstruction clone = new CreateCursorResultSetInstruction(this.rsName, clonedPlan, mode);
clone.setProcAssignments(procAssignments);
clone.usesLocalTemp = true;
return clone;
}

Expand All @@ -93,4 +95,12 @@ public ProcessorPlan getCommand() { //Defect 13291 - added method to support cha
return plan;
}

public Mode getMode() {
return mode;
}

public void setUsesLocalTemp(boolean b) {
this.usesLocalTemp = b;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public PlanNode getDescriptionProperties() {

public boolean testCondition(ProcedurePlan procEnv) throws TeiidComponentException, TeiidProcessingException {
if(!procEnv.resultSetExists(rsName)) {
procEnv.executePlan(plan, rsName, null, Mode.NOHOLD);
procEnv.executePlan(plan, rsName, null, Mode.NOHOLD, false);
}

return procEnv.iterateCursor(rsName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private static class CursorState {
List<?> currentRow;
TupleBuffer resultsBuffer;
public boolean returning;
public boolean usesLocalTemp;
}

static final ElementSymbol ROWCOUNT =
Expand Down Expand Up @@ -345,7 +346,7 @@ private TupleSource processProcedure()
VariableContext vc = this.cursorStates.getParentContext();
CursorState last = (CursorState) this.cursorStates.getValue(null);
if(last != null){
if (last.resultsBuffer == null) {
if (last.resultsBuffer == null && (last.usesLocalTemp || !txnTupleSources.isEmpty())) {
last.resultsBuffer = bufferMgr.createTupleBuffer(last.processor.getOutputElements(), getContext().getConnectionId(), TupleSourceType.PROCESSOR);
last.returning = true;
}
Expand Down Expand Up @@ -557,7 +558,17 @@ public VariableContext getCurrentVariableContext() {
return this.currentVarContext;
}

public void executePlan(ProcessorPlan command, String rsName, Map<ElementSymbol, ElementSymbol> procAssignments, CreateCursorResultSetInstruction.Mode mode)
/**
*
* @param command
* @param rsName
* @param procAssignments
* @param mode
* @param usesLocalTemp - only matters in HOLD mode
* @throws TeiidComponentException
* @throws TeiidProcessingException
*/
public void executePlan(ProcessorPlan command, String rsName, Map<ElementSymbol, ElementSymbol> procAssignments, CreateCursorResultSetInstruction.Mode mode, boolean usesLocalTemp)
throws TeiidComponentException, TeiidProcessingException {

CursorState state = (CursorState) this.cursorStates.getValue(rsName);
Expand All @@ -574,6 +585,7 @@ public void executePlan(ProcessorPlan command, String rsName, Map<ElementSymbol,
CommandContext subContext = getContext().clone();
subContext.setVariableContext(this.currentVarContext);
state = new CursorState();
state.usesLocalTemp = usesLocalTemp;
state.processor = new QueryProcessor(command, subContext, this.bufferMgr, this);
state.ts = new BatchIterator(state.processor);
if (mode == Mode.HOLD && procAssignments != null && state.processor.getOutputElements().size() - procAssignments.size() > 0) {
Expand Down

0 comments on commit 71b0746

Please sign in to comment.