Search in sources :

Example 11 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTable method createTupleSource.

private TupleSource createTupleSource(final List<? extends Expression> projectedCols, final Criteria condition, OrderBy orderBy, IndexInfo ii, boolean agg) throws TeiidComponentException, TeiidProcessingException {
    TupleBrowser browser = ii.createTupleBrowser(bm.getOptions().getDefaultNullOrder(), true);
    TupleSource ts = new QueryTupleSource(browser, columnMap, agg ? getColumns() : projectedCols, condition);
    boolean usingQueryTupleSource = false;
    boolean success = false;
    TupleBuffer tb = null;
    try {
        if (ii.ordering == null && orderBy != null) {
            SortUtility sort = new SortUtility(ts, orderBy.getOrderByItems(), Mode.SORT, bm, sessionID, projectedCols);
            sort.setNonBlocking(true);
            tb = sort.sort();
        } else if (agg) {
            int count = 0;
            while (ts.nextTuple() != null) {
                count++;
            }
            success = true;
            return new CollectionTupleSource(Arrays.asList(Collections.nCopies(projectedCols.size(), count)).iterator());
        } else if (updatable) {
            tb = bm.createTupleBuffer(projectedCols, sessionID, TupleSourceType.PROCESSOR);
            List<?> next = null;
            while ((next = ts.nextTuple()) != null) {
                tb.addTuple(next);
            }
        } else {
            usingQueryTupleSource = true;
            success = true;
            return ts;
        }
        tb.close();
        success = true;
        return tb.createIndexedTupleSource(true);
    } finally {
        if (!success && tb != null) {
            tb.remove();
        }
        if (!usingQueryTupleSource) {
            // ensure the buffers get released
            ts.closeSource();
        }
    }
}
Also used : SortUtility(org.teiid.query.processor.relational.SortUtility) TupleBufferTupleSource(org.teiid.common.buffer.TupleBuffer.TupleBufferTupleSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) TupleBuffer(org.teiid.common.buffer.TupleBuffer) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleBrowser(org.teiid.common.buffer.TupleBrowser)

Example 12 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTable method addIndex.

void addIndex(List<ElementSymbol> indexColumns, boolean unique) throws TeiidComponentException, TeiidProcessingException {
    List<ElementSymbol> keyColumns = columns.subList(0, tree.getKeyLength());
    if (keyColumns.equals(indexColumns) || (indexTables != null && indexTables.containsKey(indexColumns))) {
        return;
    }
    TempTable indexTable = createIndexTable(indexColumns, unique);
    // TODO: ordered insert optimization
    TupleSource ts = createTupleSource(indexTable.getColumns(), null, null);
    indexTable.insert(ts, indexTable.getColumns(), false, false, null);
    indexTable.getTree().compact();
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TupleBufferTupleSource(org.teiid.common.buffer.TupleBuffer.TupleBufferTupleSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource)

Example 13 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTableDataManager method registerQuery.

private TupleSource registerQuery(final CommandContext context, final TempTableStore contextStore, final Query query) {
    final GroupSymbol group = query.getFrom().getGroups().get(0);
    if (!group.isTempGroupSymbol()) {
        return null;
    }
    final String tableName = group.getNonCorrelationName();
    if (group.isGlobalTable()) {
        TempMetadataID matTableId = (TempMetadataID) group.getMetadataID();
        final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
        final MatTableInfo info = globalStore.getMatTableInfo(tableName);
        return new ProxyTupleSource() {

            Future<Void> moreWork = null;

            TupleSource loadingTupleSource;

            DQPWorkContext newWorkContext;

            @Override
            protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
                if (loadingTupleSource != null) {
                    load();
                } else {
                    boolean load = false;
                    if (!info.isUpToDate()) {
                        boolean invalidate = shouldInvalidate(context.getVdb());
                        load = globalStore.needsLoading(tableName, globalStore.getAddress(), true, false, info.isValid() && invalidate);
                        if (load) {
                            load = globalStore.needsLoading(tableName, globalStore.getAddress(), false, false, info.isValid() && invalidate);
                        }
                        if (!load) {
                            synchronized (info) {
                                if (!info.isUpToDate()) {
                                    RequestWorkItem workItem = context.getWorkItem();
                                    info.addWaiter(workItem);
                                    if (moreWork != null) {
                                        moreWork.cancel(false);
                                    }
                                    // fail-safe - attempt again in 10 seconds
                                    moreWork = workItem.scheduleWork(10000);
                                    // $NON-NLS-1$
                                    throw BlockedException.block("Blocking on mat view load", tableName);
                                }
                            }
                        } else {
                            if (!info.isValid() || executor == null) {
                                // TODO: we should probably do all loads using a temp session
                                if (info.getVdbMetaData() != null && context.getDQPWorkContext() != null && !info.getVdbMetaData().getFullName().equals(context.getDQPWorkContext().getVDB().getFullName())) {
                                    assert executor != null;
                                    // load with by pretending we're in the imported vdb
                                    newWorkContext = createWorkContext(context, info.getVdbMetaData());
                                    CommandContext newContext = context.clone();
                                    newContext.setNewVDBState(newWorkContext);
                                    loadingTupleSource = loadGlobalTable(newContext, group, tableName, newContext.getGlobalTableStore());
                                } else {
                                    loadingTupleSource = loadGlobalTable(context, group, tableName, globalStore);
                                }
                                load();
                            } else {
                                loadViaRefresh(context, tableName, context.getDQPWorkContext().getVDB(), info);
                            }
                        }
                    }
                }
                TempTable table = globalStore.getTempTable(tableName);
                context.accessedDataObject(group.getMetadataID());
                if (context.isParallel() && query.getCriteria() == null && query.getOrderBy() != null && table.getRowCount() > MIN_ASYNCH_SIZE) {
                    return new AsyncTupleSource(new Callable<TupleSource>() {

                        @Override
                        public TupleSource call() throws Exception {
                            synchronized (this) {
                                TupleSource result = table.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
                                cancelMoreWork();
                                return result;
                            }
                        }
                    }, context);
                }
                TupleSource result = table.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
                cancelMoreWork();
                return result;
            }

            private void load() throws TeiidComponentException, TeiidProcessingException {
                try {
                    if (newWorkContext != null) {
                        newWorkContext.runInContext(new Callable<Void>() {

                            @Override
                            public Void call() throws Exception {
                                loadingTupleSource.nextTuple();
                                return null;
                            }
                        });
                    } else {
                        loadingTupleSource.nextTuple();
                    }
                } catch (Throwable e) {
                    rethrow(e);
                }
            }

            private void cancelMoreWork() {
                if (moreWork != null) {
                    moreWork.cancel(false);
                    moreWork = null;
                }
            }

            @Override
            public void closeSource() {
                if (loadingTupleSource != null) {
                    loadingTupleSource.closeSource();
                }
                super.closeSource();
                cancelMoreWork();
            }
        };
    }
    // it's not expected for a blocked exception to bubble up from here, so return a tuplesource to perform getOrCreateTempTable
    return new ProxyTupleSource() {

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            TempTableStore tts = contextStore;
            TempTable tt = tts.getOrCreateTempTable(tableName, query, bufferManager, true, false, context, group);
            if (context.getDataObjects() != null) {
                Object id = RelationalPlanner.getTrackableGroup(group, context.getMetadata());
                if (id != null) {
                    context.accessedDataObject(id);
                }
            }
            if (context.isParallel() && query.getCriteria() == null && query.getOrderBy() != null && tt.getRowCount() > MIN_ASYNCH_SIZE) {
                return new AsyncTupleSource(new Callable<TupleSource>() {

                    @Override
                    public TupleSource call() throws Exception {
                        synchronized (this) {
                            return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
                        }
                    }
                }, context);
            }
            return tt.createTupleSource(query.getProjectedSymbols(), query.getCriteria(), query.getOrderBy());
        }
    };
}
Also used : DQPWorkContext(org.teiid.dqp.internal.process.DQPWorkContext) RequestWorkItem(org.teiid.dqp.internal.process.RequestWorkItem) CommandContext(org.teiid.query.util.CommandContext) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException) TeiidComponentException(org.teiid.core.TeiidComponentException) TransformationException(org.teiid.core.types.TransformationException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) BlockedException(org.teiid.common.buffer.BlockedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) Future(java.util.concurrent.Future)

Example 14 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TempTableDataManager method lookupCodeValue.

public Object lookupCodeValue(CommandContext context, String codeTableName, String returnElementName, String keyElementName, Object keyValue) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    // we are not using a resolved form of a lookup, so we canonicallize with upper case
    codeTableName = codeTableName.toUpperCase();
    keyElementName = keyElementName.toUpperCase();
    returnElementName = returnElementName.toUpperCase();
    String matTableName = CODE_PREFIX + codeTableName + ElementSymbol.SEPARATOR + keyElementName + ElementSymbol.SEPARATOR + returnElementName;
    TupleSource ts = context.getCodeLookup(matTableName, keyValue);
    if (ts == null) {
        QueryMetadataInterface metadata = context.getMetadata();
        TempMetadataID id = context.getGlobalTableStore().getCodeTableMetadataId(codeTableName, returnElementName, keyElementName, matTableName);
        ElementSymbol keyElement = new ElementSymbol(keyElementName, new GroupSymbol(matTableName));
        ElementSymbol returnElement = new ElementSymbol(returnElementName, new GroupSymbol(matTableName));
        keyElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + keyElementName))));
        returnElement.setType(DataTypeManager.getDataTypeClass(metadata.getElementRuntimeTypeName(metadata.getElementID(codeTableName + ElementSymbol.SEPARATOR + returnElementName))));
        Query query = RelationalPlanner.createMatViewQuery(id, matTableName, Arrays.asList(returnElement), true);
        query.setCriteria(new CompareCriteria(keyElement, CompareCriteria.EQ, new Constant(keyValue)));
        ts = registerQuery(context, context.getTempTableStore(), query);
    }
    try {
        List<?> row = ts.nextTuple();
        Object result = null;
        if (row != null) {
            result = row.get(0);
        }
        ts.closeSource();
        return result;
    } catch (BlockedException e) {
        context.putCodeLookup(matTableName, keyValue, ts);
        throw e;
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) Constant(org.teiid.query.sql.symbol.Constant) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) BlockedException(org.teiid.common.buffer.BlockedException)

Example 15 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class CommandContext method close.

public void close() {
    synchronized (this.globalState) {
        if (this.globalState.reservedBuffers > 0) {
            long toRelease = this.globalState.reservedBuffers;
            this.globalState.reservedBuffers = 0;
            this.globalState.bufferManager.releaseOrphanedBuffers(toRelease);
        }
        if (this.globalState.reusableExecutions != null) {
            for (List<ReusableExecution<?>> reusableExecutions : this.globalState.reusableExecutions.values()) {
                for (ReusableExecution<?> reusableExecution : reusableExecutions) {
                    try {
                        reusableExecution.dispose();
                    } catch (Exception e) {
                        LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30030));
                    }
                }
            }
            this.globalState.reusableExecutions.clear();
        }
        if (this.globalState.commandListeners != null) {
            for (CommandListener listener : this.globalState.commandListeners) {
                try {
                    listener.commandClosed(this);
                } catch (Exception e) {
                    LogManager.logWarning(LogConstants.CTX_DQP, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30031));
                }
            }
            this.globalState.commandListeners.clear();
        }
        if (this.globalState.lookups != null) {
            for (TupleSource ts : this.globalState.lookups.values()) {
                ts.closeSource();
            }
            this.globalState.lookups = null;
        }
        if (this.globalState.created != null) {
            for (InputStreamFactory isf : this.globalState.created) {
                try {
                    isf.free();
                } catch (IOException e) {
                }
            }
            this.globalState.created.clear();
        }
    }
}
Also used : CommandListener(org.teiid.CommandListener) TupleSource(org.teiid.common.buffer.TupleSource) ReusableExecution(org.teiid.translator.ReusableExecution) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidException(org.teiid.core.TeiidException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

TupleSource (org.teiid.common.buffer.TupleSource)31 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)11 Test (org.junit.Test)10 TupleBuffer (org.teiid.common.buffer.TupleBuffer)10 CommandContext (org.teiid.query.util.CommandContext)10 List (java.util.List)9 BlockedException (org.teiid.common.buffer.BlockedException)9 TeiidProcessingException (org.teiid.core.TeiidProcessingException)9 Command (org.teiid.query.sql.lang.Command)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 ArrayList (java.util.ArrayList)6 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)6 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)6 RegisterRequestParameter (org.teiid.query.processor.RegisterRequestParameter)6 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)6 BufferManager (org.teiid.common.buffer.BufferManager)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)4 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)4 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)3