public boolean hasTempTable(String tempTableName, boolean checkParent) { boolean local = tempTables.containsKey(tempTableName) || foreignTempTables.containsKey(tempTableName); if (local) { return true; } if (checkParent && parentTempTableStore != null) { return parentTempTableStore.hasTempTable(tempTableName, checkParent); } return false; }
/** * @param success * @throws TeiidComponentException * @throws XATransactionException */ public void pop(boolean success) throws TeiidComponentException { this.evaluator.close(); Program program = this.programs.pop(); VariableContext vc = this.currentVarContext; VariableContext cs = this.cursorStates; try { this.currentVarContext = this.currentVarContext.getParentContext(); this.cursorStates = this.cursorStates.getParentContext(); TempTableStore tempTableStore = program.getTempTableStore(); this.getContext().setTempTableStore(tempTableStore.getParentTempTableStore()); tempTableStore.removeTempTables(); if (program.startedTxn()) { TransactionService ts = this.getContext().getTransactionServer(); TransactionContext tc = this.blockContext; this.blockContext = null; try { ts.resume(tc); for (WeakReference<DataTierTupleSource> ref : txnTupleSources) { DataTierTupleSource dtts = ref.get(); if (dtts != null) { dtts.fullyCloseSource(); } } this.txnTupleSources.clear(); if (success) { ts.commit(tc); } else { ts.rollback(tc); } } catch (XATransactionException e) { throw new TeiidComponentException(QueryPlugin.Event.TEIID30165, e); } } } finally { removeAllCursors(cs); } }
TempTable getOrCreateTempTable( String tempTableID, Command command, BufferManager buffer, boolean delegate, boolean forUpdate, CommandContext context, GroupSymbol group) throws TeiidProcessingException, BlockedException, TeiidComponentException { if (!(group.getMetadataID() instanceof TempMetadataID)) { // TODO: use a proper metadata TempTableStore tts = context.getSessionTempTableStore(); context.setDeterminismLevel(Determinism.SESSION_DETERMINISTIC); if (tts.getTempTable(tempTableID) == null) { // implicitly create global (session scoped) temp table LogManager.logDetail( LogConstants.CTX_DQP, "binding global temp table to session", group); // $NON-NLS-1$ QueryMetadataInterface metadata = context.getMetadata(); Create create = GlobalTableStoreImpl.getCreateCommand(group, false, metadata); tts.addTempTable(tempTableID, create, buffer, true, context); } return getTempTable(tempTableID, command, buffer, delegate, forUpdate, context); } TempTable tempTable = getTempTable(tempTableID, command, buffer, delegate, forUpdate, context); if (tempTable != null) { if (processors != null) { TableProcessor withProcessor = processors.get(tempTableID); if (withProcessor != null) { TempTable tt = withProcessor.process(tempTable); if (tt != tempTable) { return tt; } processors.remove(tempTableID); } } return tempTable; } // allow implicit temp group definition List<ElementSymbol> columns = null; if (command instanceof Insert) { Insert insert = (Insert) command; if (group.isImplicitTempGroupSymbol()) { columns = insert.getVariables(); } } if (columns == null) { if (processors != null) { TableProcessor withProcessor = processors.get(tempTableID); if (withProcessor != null) { LogManager.logDetail( LogConstants.CTX_DQP, "Creating temporary table for with clause", tempTableID); //$NON-NLS-1$ Create create = new Create(); create.setTable(new GroupSymbol(tempTableID)); create.setElementSymbolsAsColumns(withProcessor.columns); withProcessor.alterCreate(create); tempTable = addTempTable(tempTableID, create, buffer, true, context); TempTable tt = withProcessor.process(tempTable); if (tt != tempTable) { return tt; } processors.remove(tempTableID); return tempTable; } } if (delegate && this.parentTempTableStore != null) { // may be a cte from a higher scope that needs to have creation triggered return parentTempTableStore.getOrCreateTempTable( tempTableID, command, buffer, delegate, forUpdate, context, group); } throw new QueryProcessingException( QueryPlugin.Event.TEIID30226, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30226, tempTableID)); } LogManager.logDetail( LogConstants.CTX_DQP, "Creating temporary table", tempTableID); // $NON-NLS-1$ Create create = new Create(); create.setTable(new GroupSymbol(tempTableID)); create.setElementSymbolsAsColumns(columns); return addTempTable(tempTableID, create, buffer, true, context); }
TupleSource registerRequest(final CommandContext context, String modelName, final Command command) throws TeiidComponentException, TeiidProcessingException { final TempTableStore contextStore = context.getTempTableStore(); if (command instanceof Query) { Query query = (Query) command; if (modelName != null && !modelName.equals(TempMetadataAdapter.TEMP_MODEL.getID())) { return null; } return registerQuery(context, contextStore, query); } if (command instanceof ProcedureContainer) { if (command instanceof StoredProcedure) { StoredProcedure proc = (StoredProcedure) command; if (CoreConstants.SYSTEM_ADMIN_MODEL.equals(modelName)) { TupleSource result = handleSystemProcedures(context, proc); if (result != null) { return result; } } else if (proc.getGroup().isGlobalTable()) { return handleCachedProcedure(context, proc); } return null; // it's not a stored procedure we want to handle } final GroupSymbol group = ((ProcedureContainer) command).getGroup(); if (!modelName.equals(TempMetadataAdapter.TEMP_MODEL.getID()) || !group.isTempGroupSymbol()) { return null; } return new ProxyTupleSource() { @Override protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException { final String groupKey = group.getNonCorrelationName(); final TempTable table = contextStore.getOrCreateTempTable( groupKey, command, bufferManager, true, true, context, group); if (command instanceof Insert) { Insert insert = (Insert) command; TupleSource ts = insert.getTupleSource(); if (ts == null) { Evaluator eval = new Evaluator(Collections.emptyMap(), TempTableDataManager.this, context); List<Object> values = new ArrayList<Object>(insert.getValues().size()); for (Expression expr : (List<Expression>) insert.getValues()) { values.add(eval.evaluate(expr, null)); } ts = new CollectionTupleSource(Arrays.asList(values).iterator()); } return table.insert(ts, insert.getVariables(), true, context); } if (command instanceof Update) { final Update update = (Update) command; final Criteria crit = update.getCriteria(); return table.update(crit, update.getChangeList()); } if (command instanceof Delete) { final Delete delete = (Delete) command; final Criteria crit = delete.getCriteria(); if (crit == null) { // TODO: we'll add a real truncate later int rows = table.truncate(false); return CollectionTupleSource.createUpdateCountTupleSource(rows); } return table.delete(crit); } throw new AssertionError("unknown command " + command); // $NON-NLS-1$ } }; } if (command instanceof Create) { Create create = (Create) command; String tempTableName = create.getTable().getName(); if (contextStore.hasTempTable(tempTableName)) { throw new QueryProcessingException( QueryPlugin.Event.TEIID30229, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30229, tempTableName)); } if (create.getTableMetadata() != null) { contextStore.addForeignTempTable(tempTableName, create); } else { contextStore.addTempTable(tempTableName, create, bufferManager, true, context); } return CollectionTupleSource.createUpdateCountTupleSource(0); } if (command instanceof Drop) { String tempTableName = ((Drop) command).getTable().getName(); contextStore.removeTempTableByName(tempTableName, context); return CollectionTupleSource.createUpdateCountTupleSource(0); } if (command instanceof AlterTempTable) { AlterTempTable att = (AlterTempTable) command; TempTable tt = contextStore.getTempTable(att.getTempTable()); Assertion.isNotNull(tt, "Table doesn't exist"); // $NON-NLS-1$ tt.setUpdatable(false); if (att.getIndexColumns() != null && tt.getRowCount() > 2 * tt.getTree().getPageSize(true)) { for (List<ElementSymbol> cols : att.getIndexColumns()) { tt.addIndex(cols, false); } } return CollectionTupleSource.createUpdateCountTupleSource(0); } return null; }