@Override public TempTable process(TempTable tempTable) throws TeiidComponentException, TeiidProcessingException { if (initial) { // process initial plan if (working == null) { working = tempTable.clone(); intermediate = tempTable.clone(); } processPlan(tempTable, working); initial = false; } // continue to build the result while (working.getRowCount() > 0) { if (building) { return working; } building = true; try { if (workingQp == null) { recursive.reset(); workingQp = new QueryProcessor( recursive, this.queryProcessor.getContext(), this.queryProcessor.getBufferManager(), this.queryProcessor.getProcessorDataManager()); this.iterator = new BatchProducerTupleSource(workingQp); } processPlan(tempTable, intermediate); iterations++; if (maxIterations > 0 && iterations > maxIterations) { throw new TeiidProcessingException( QueryPlugin.Event.TEIID31158, QueryPlugin.Util.gs( QueryPlugin.Event.TEIID31158, maxIterations, tempTable.getMetadataId().getName())); } this.workingQp.closeProcessing(); this.workingQp = null; // swap the intermediate to be the working working.truncate(true); TempTable temp = working; working = intermediate; intermediate = temp; } finally { building = false; } } // we truncate rater than remove because we are cloned off of the original this.working.truncate(true); this.intermediate.truncate(true); tempTable.setUpdatable(false); return tempTable; }
/** * Ensure the temp table is ready for use. If a temp table other than the one passed in is * returned it should be used instead. * * @param tempTable * @param context * @param bufferManager * @param dataMgr * @throws TeiidComponentException * @throws TeiidProcessingException */ public TempTable process(TempTable tempTable) throws TeiidComponentException, TeiidProcessingException { if (!tempTable.getColumnMap().keySet().containsAll(columns)) { // sanity check to make sure that we haven't inappropriately redefined the common table throw new TeiidComponentException( "failed to plan common table appropriately " + columns + " " + tempTable.getColumns()); // $NON-NLS-1$ //$NON-NLS-2$ } tempTable.insert(iterator, columns, false, null); tempTable.setUpdatable(false); close(); return tempTable; }
private void buildWithTable(String tempTableID, TableProcessor withProcessor, TempTable tempTable) throws TeiidComponentException, ExpressionEvaluationException, TeiidProcessingException { tempTable.insert(new BatchIterator(withProcessor.queryProcessor), withProcessor.columns, false); tempTable.setUpdatable(false); processors.remove(tempTableID); }
public void setUpdatable(String name, boolean updatable) { TempTable table = tempTables.get(name); if (table != null) { table.setUpdatable(updatable); } }
private TupleSource loadGlobalTable( final CommandContext context, final GroupSymbol group, final String tableName, final GlobalTableStore globalStore) throws TeiidComponentException, TeiidProcessingException { LogManager.logInfo( LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30013, tableName)); final QueryMetadataInterface metadata = context.getMetadata(); final List<ElementSymbol> allColumns = ResolverUtil.resolveElementsInGroup(group, metadata); final TempTable table = globalStore.createMatTable(tableName, group); table.setUpdatable(false); return new ProxyTupleSource() { TupleSource insertTupleSource; boolean success; QueryProcessor qp; boolean closed; boolean errored; @Override protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException { int rowCount = -1; try { if (insertTupleSource == null) { String fullName = metadata.getFullName(group.getMetadataID()); String transformation = metadata.getVirtualPlan(group.getMetadataID()).getQuery(); qp = context .getQueryProcessorFactory() .createQueryProcessor(transformation, fullName, context); insertTupleSource = new BatchCollector.BatchProducerTupleSource(qp); } table.insert(insertTupleSource, allColumns, false, null); table.getTree().compact(); rowCount = table.getRowCount(); Determinism determinism = qp.getContext().getDeterminismLevel(); context.setDeterminismLevel(determinism); // TODO: could pre-process indexes to remove overlap for (Object index : metadata.getIndexesInGroup(group.getMetadataID())) { List<ElementSymbol> columns = GlobalTableStoreImpl.resolveIndex(metadata, allColumns, index); table.addIndex(columns, false); } for (Object key : metadata.getUniqueKeysInGroup(group.getMetadataID())) { List<ElementSymbol> columns = GlobalTableStoreImpl.resolveIndex(metadata, allColumns, key); table.addIndex(columns, true); } CacheHint hint = table.getCacheHint(); if (hint != null && table.getPkLength() > 0) { table.setUpdatable(hint.isUpdatable(false)); } if (determinism.compareTo(Determinism.VDB_DETERMINISTIC) < 0 && (hint == null || hint.getScope() == null || Scope.VDB.compareTo(hint.getScope()) <= 0)) { LogManager.logInfo( LogConstants.CTX_DQP, QueryPlugin.Util.gs( QueryPlugin.Event.TEIID31143, determinism, tableName)); // $NON-NLS-1$ } globalStore.loaded(tableName, table); success = true; LogManager.logInfo( LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30014, tableName, rowCount)); return CollectionTupleSource.createUpdateCountTupleSource(rowCount); } catch (BlockedException e) { throw e; } catch (Exception e) { errored = true; LogManager.logError( LogConstants.CTX_MATVIEWS, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30015, tableName)); closeSource(); rethrow(e); throw new AssertionError(); } } @Override public void closeSource() { if (closed) { return; } if (!errored) { LogManager.logInfo( LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31153, tableName)); } closed = true; if (!success) { globalStore.failedLoad(tableName); table.remove(); } if (qp != null) { qp.closeProcessing(); } super.closeSource(); } }; }
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; }