Search in sources :

Example 11 with QueryProcessor

use of org.teiid.query.processor.QueryProcessor in project teiid by teiid.

the class RelationalPlan method open.

@Override
public void open() throws TeiidComponentException, TeiidProcessingException {
    if (with != null && tempTableStore.getProcessors() == null) {
        HashMap<String, TableProcessor> processors = new HashMap<String, TableProcessor>();
        tempTableStore.setProcessors(processors);
        for (WithQueryCommand withCommand : this.with) {
            if (withCommand.isRecursive()) {
                SetQuery setQuery = (SetQuery) withCommand.getCommand();
                ProcessorPlan initial = setQuery.getLeftQuery().getProcessorPlan();
                QueryProcessor withProcessor = new QueryProcessor(initial, getContext().clone(), root.getBufferManager(), root.getDataManager());
                processors.put(withCommand.getGroupSymbol().getName(), new RecursiveTableProcessor(withProcessor, withCommand.getColumns(), setQuery.getRightQuery().getProcessorPlan(), setQuery.isAll()));
                continue;
            }
            ProcessorPlan plan = withCommand.getCommand().getProcessorPlan();
            QueryProcessor withProcessor = new QueryProcessor(plan, getContext().clone(), root.getBufferManager(), root.getDataManager());
            processors.put(withCommand.getGroupSymbol().getName(), new TableProcessor(withProcessor, withCommand.getColumns()));
        }
    }
    this.root.open();
}
Also used : SetQuery(org.teiid.query.sql.lang.SetQuery) HashMap(java.util.HashMap) RecursiveTableProcessor(org.teiid.query.tempdata.TempTableStore.RecursiveTableProcessor) TableProcessor(org.teiid.query.tempdata.TempTableStore.TableProcessor) WithQueryCommand(org.teiid.query.sql.lang.WithQueryCommand) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) QueryProcessor(org.teiid.query.processor.QueryProcessor) RecursiveTableProcessor(org.teiid.query.tempdata.TempTableStore.RecursiveTableProcessor)

Example 12 with QueryProcessor

use of org.teiid.query.processor.QueryProcessor in project teiid by teiid.

the class TempTableDataManager method updateMatviewRows.

private TupleSource updateMatviewRows(final CommandContext context, final QueryMetadataInterface metadata, final Object groupID, final GlobalTableStore globalStore, final String matViewName, List<?> ids, Object[][] params) throws QueryProcessingException, TeiidComponentException, QueryMetadataException, TransformationException {
    final String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
    MatTableInfo info = globalStore.getMatTableInfo(matTableName);
    if (!info.isValid()) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
    }
    TempTable tempTable = globalStore.getTempTable(matTableName);
    if (!tempTable.isUpdatable()) {
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30232, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30232, matViewName));
    }
    List<Object[]> converted = new ArrayList<Object[]>();
    for (Object[] param : params) {
        if (param == null || ids.size() != param.length) {
            throw new QueryProcessingException(QueryPlugin.Event.TEIID30231, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30231, matViewName, ids.size(), param == null ? 0 : param.length));
        }
        final Object[] vals = new Object[param.length];
        for (int i = 0; i < ids.size(); i++) {
            Object value = param[i];
            String targetTypeName = metadata.getElementRuntimeTypeName(ids.get(i));
            value = DataTypeManager.transformValue(value, DataTypeManager.getDataTypeClass(targetTypeName));
            vals[i] = value;
        }
        converted.add(vals);
    }
    final Iterator<Object[]> paramIter = converted.iterator();
    Iterator<?> iter = ids.iterator();
    StringBuilder criteria = new StringBuilder();
    for (int i = 0; i < ids.size(); i++) {
        Object id = iter.next();
        if (i != 0) {
            // $NON-NLS-1$
            criteria.append(" AND ");
        }
        // $NON-NLS-1$
        criteria.append(metadata.getFullName(id)).append(" = ?");
    }
    final String queryString = // $NON-NLS-1$
    Reserved.SELECT + " * " + Reserved.FROM + ' ' + matViewName + ' ' + Reserved.WHERE + ' ' + criteria.toString() + ' ' + Reserved.OPTION + ' ' + Reserved.NOCACHE;
    return new ProxyTupleSource() {

        private QueryProcessor qp;

        private TupleSource ts;

        private Object[] params;

        private int count;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            while (true) {
                if (qp == null) {
                    params = paramIter.next();
                    LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30012, matViewName, Arrays.toString(params)));
                    qp = context.getQueryProcessorFactory().createQueryProcessor(queryString, matViewName.toUpperCase(), context, params);
                    ts = new BatchCollector.BatchProducerTupleSource(qp);
                }
                List<?> tuple = ts.nextTuple();
                boolean delete = false;
                if (tuple == null) {
                    delete = true;
                    tuple = Arrays.asList(params);
                } else {
                    // ensure the list is serializable
                    tuple = new ArrayList<Object>(tuple);
                }
                List<?> result = globalStore.updateMatViewRow(matTableName, tuple, delete);
                if (result != null) {
                    count++;
                }
                if (eventDistributor != null) {
                    eventDistributor.updateMatViewRow(context.getVdbName(), context.getVdbVersion(), metadata.getName(metadata.getModelID(groupID)), metadata.getName(groupID), tuple, delete);
                }
                qp.closeProcessing();
                qp = null;
                ts = null;
                if (!paramIter.hasNext()) {
                    break;
                }
            }
            return CollectionTupleSource.createUpdateCountTupleSource(count);
        }

        @Override
        public void closeSource() {
            super.closeSource();
            if (qp != null) {
                qp.closeProcessing();
            }
        }
    };
}
Also used : ArrayList(java.util.ArrayList) QueryProcessor(org.teiid.query.processor.QueryProcessor) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) MatTableInfo(org.teiid.query.tempdata.GlobalTableStoreImpl.MatTableInfo) BatchCollector(org.teiid.query.processor.BatchCollector) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 13 with QueryProcessor

use of org.teiid.query.processor.QueryProcessor in project teiid by teiid.

the class TempTableDataManager method handleCachedProcedure.

private TupleSource handleCachedProcedure(final CommandContext context, StoredProcedure proc) throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
    String fullName = context.getMetadata().getFullName(proc.getProcedureID());
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_DQP, "processing cached procedure request for", fullName);
    LinkedList<Object> vals = new LinkedList<Object>();
    for (SPParameter param : proc.getInputParameters()) {
        vals.add(((Constant) param.getExpression()).getValue());
    }
    // collapse the hash to single byte for the key to restrict the possible results to 256
    int hash = vals.hashCode();
    hash |= (hash >>> 16);
    hash |= (hash >>> 8);
    hash &= 0x000000ff;
    final CacheID cid = new CacheID(new ParseInfo(), fullName + hash, context.getVdbName(), context.getVdbVersion(), context.getConnectionId(), context.getUserName());
    cid.setParameters(vals);
    CachedResults results = cache.get(cid);
    if (results != null) {
        TupleBuffer buffer = results.getResults();
        return buffer.createIndexedTupleSource();
    }
    // construct a query with a no cache hint
    final CacheHint hint = proc.getCacheHint();
    proc.setCacheHint(null);
    Option option = new Option();
    option.setNoCache(true);
    option.addNoCacheGroup(fullName);
    proc.setOption(option);
    StoredProcedure cloneProc = (StoredProcedure) proc.clone();
    int i = 0;
    for (SPParameter param : cloneProc.getInputParameters()) {
        param.setExpression(new Reference(i++));
    }
    final QueryProcessor qp = context.getQueryProcessorFactory().createQueryProcessor(cloneProc.toString(), fullName.toUpperCase(), context, vals.toArray());
    final BatchCollector bc = qp.createBatchCollector();
    return new ProxyTupleSource() {

        boolean success = false;

        @Override
        protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException {
            TupleBuffer tb = bc.collectTuples();
            CachedResults cr = new CachedResults();
            cr.setResults(tb, qp.getProcessorPlan());
            Determinism determinismLevel = qp.getContext().getDeterminismLevel();
            if (hint != null && hint.getDeterminism() != null) {
                // $NON-NLS-1$ //$NON-NLS-2$
                LogManager.logTrace(LogConstants.CTX_DQP, new Object[] { "Cache hint modified the query determinism from ", determinismLevel, " to ", hint.getDeterminism() });
                determinismLevel = hint.getDeterminism();
            }
            cache.put(cid, determinismLevel, cr, hint != null ? hint.getTtl() : null);
            context.setDeterminismLevel(determinismLevel);
            success = true;
            return tb.createIndexedTupleSource();
        }

        @Override
        public void closeSource() {
            super.closeSource();
            qp.closeProcessing();
            if (!success && bc.getTupleBuffer() != null) {
                bc.getTupleBuffer().remove();
            }
        }
    };
}
Also used : Determinism(org.teiid.metadata.FunctionMethod.Determinism) Reference(org.teiid.query.sql.symbol.Reference) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ParseInfo(org.teiid.query.parser.ParseInfo) LinkedList(java.util.LinkedList) CachedResults(org.teiid.dqp.internal.process.CachedResults) QueryProcessor(org.teiid.query.processor.QueryProcessor) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) BatchCollector(org.teiid.query.processor.BatchCollector)

Example 14 with QueryProcessor

use of org.teiid.query.processor.QueryProcessor in project teiid by teiid.

the class TempTableDataManager method loadGlobalTable.

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 {
            long 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, 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)) {
                    // $NON-NLS-1$
                    LogManager.logInfo(LogConstants.CTX_DQP, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31143, determinism, tableName));
                }
                globalStore.loaded(tableName, table);
                success = true;
                LogManager.logInfo(LogConstants.CTX_MATVIEWS, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30014, tableName, rowCount));
                return CollectionTupleSource.createUpdateCountTupleSource((int) Math.min(Integer.MAX_VALUE, rowCount));
            } catch (BlockedException e) {
                throw e;
            } catch (Exception e) {
                errored = true;
                if (executor == null || !executor.isShutdown()) {
                    // if we're shutting down, no need to log
                    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 && !success) {
                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();
        }
    };
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Determinism(org.teiid.metadata.FunctionMethod.Determinism) BlockedException(org.teiid.common.buffer.BlockedException) QueryProcessor(org.teiid.query.processor.QueryProcessor) 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) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) BatchCollector(org.teiid.query.processor.BatchCollector)

Aggregations

QueryProcessor (org.teiid.query.processor.QueryProcessor)14 ArrayList (java.util.ArrayList)4 BatchCollector (org.teiid.query.processor.BatchCollector)4 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 CommandContext (org.teiid.query.util.CommandContext)4 List (java.util.List)3 Map (java.util.Map)3 TransactionContext (org.teiid.dqp.service.TransactionContext)3 Determinism (org.teiid.metadata.FunctionMethod.Determinism)3 VariableContext (org.teiid.query.sql.util.VariableContext)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)2 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)2 BlockedException (org.teiid.common.buffer.BlockedException)2 TupleBatch (org.teiid.common.buffer.TupleBatch)2 TupleBuffer (org.teiid.common.buffer.TupleBuffer)2 TupleSource (org.teiid.common.buffer.TupleSource)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2