Search in sources :

Example 81 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class CriteriaCapabilityValidatorVisitor method validateSubqueryPushdown.

/**
 * Return null if the subquery cannot be pushed down, otherwise the model
 * id of the pushdown target.
 * @param subqueryContainer
 * @param critNodeModelID
 * @param metadata
 * @param capFinder
 * @return
 * @throws TeiidComponentException
 */
public static Object validateSubqueryPushdown(SubqueryContainer<?> subqueryContainer, Object critNodeModelID, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord) throws TeiidComponentException {
    ProcessorPlan plan = subqueryContainer.getCommand().getProcessorPlan();
    if (plan != null) {
        AccessNode aNode = getAccessNode(plan);
        if (aNode == null) {
            return null;
        }
        critNodeModelID = validateCommandPushdown(critNodeModelID, metadata, capFinder, aNode, true);
    }
    if (critNodeModelID == null) {
        return null;
    }
    // Check whether source supports correlated subqueries and if not, whether criteria has them
    SymbolMap refs = subqueryContainer.getCommand().getCorrelatedReferences();
    try {
        if (refs != null && !refs.asMap().isEmpty()) {
            if (!CapabilitiesUtil.supports(Capability.QUERY_SUBQUERIES_CORRELATED, critNodeModelID, metadata, capFinder)) {
                return null;
            }
            if (!CapabilitiesUtil.supports(Capability.SUBQUERY_CORRELATED_LIMIT, critNodeModelID, metadata, capFinder)) {
                QueryCommand command = (QueryCommand) subqueryContainer.getCommand();
                if (command.getLimit() != null && !command.getLimit().isImplicit()) {
                    return null;
                }
            }
            // but this is only an issue with deeply nested subqueries
            if (!CriteriaCapabilityValidatorVisitor.canPushLanguageObject(subqueryContainer.getCommand(), critNodeModelID, metadata, capFinder, analysisRecord)) {
                return null;
            }
        } else if (CapabilitiesUtil.supports(Capability.QUERY_SUBQUERIES_ONLY_CORRELATED, critNodeModelID, metadata, capFinder)) {
            return null;
        }
    } catch (QueryMetadataException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30271, e);
    }
    if (!CapabilitiesUtil.supports(Capability.SUBQUERY_COMMON_TABLE_EXPRESSIONS, critNodeModelID, metadata, capFinder) && subqueryContainer.getCommand() instanceof QueryCommand) {
        QueryCommand command = (QueryCommand) subqueryContainer.getCommand();
        if (command.getWith() != null) {
            return null;
        }
    }
    // Found no reason why this node is not eligible
    return critNodeModelID;
}
Also used : SymbolMap(org.teiid.query.sql.util.SymbolMap) TeiidComponentException(org.teiid.core.TeiidComponentException) AccessNode(org.teiid.query.processor.relational.AccessNode) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException)

Example 82 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class ForEachRowPlan method close.

@Override
public void close() throws TeiidComponentException {
    if (this.queryProcessor != null) {
        this.queryProcessor.closeProcessing();
        if (this.rowProcessor != null) {
            this.rowProcessor.closeProcessing();
        }
    }
    if (this.planContext != null) {
        TransactionService ts = this.getContext().getTransactionServer();
        try {
            ts.resume(planContext);
            ts.rollback(planContext);
            this.planContext = null;
        } catch (XATransactionException e) {
            throw new TeiidComponentException(QueryPlugin.Event.TEIID30165, e);
        }
    }
}
Also used : TransactionService(org.teiid.dqp.service.TransactionService) TeiidComponentException(org.teiid.core.TeiidComponentException) XATransactionException(org.teiid.client.xa.XATransactionException)

Example 83 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class ProcedurePlan method processProcedure.

/**
 * <p>Process the procedure, using the stack of Programs supplied by the
 * ProcessorEnvironment.  With each pass through the loop, the
 * current Program is gotten off the top of the stack, and the
 * current instruction is gotten from that program; each call
 * to an instruction's process method may alter the Program
 * Stack and/or the current instruction pointer of a Program,
 * so it's important that this method's loop refer to the
 * call stack of the ProcessorEnvironment each time, and not
 * cache things in local variables.  If the current Program's
 * current instruction is null, then it's time to pop that
 * Program off the stack.</p>
 *
 * @return List a single tuple containing one Integer: the update
 * count resulting from the procedure execution.
 */
private TupleSource processProcedure() throws TeiidComponentException, TeiidProcessingException, BlockedException {
    // execute plan
    ProgramInstruction inst = null;
    while (!this.programs.empty()) {
        Program program = peek();
        inst = program.getCurrentInstruction();
        if (inst == null) {
            // $NON-NLS-1$
            LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Finished program", program);
            // look ahead to see if we need to process in place
            VariableContext vc = this.cursorStates.getParentContext();
            CursorState last = (CursorState) this.cursorStates.getValue(null);
            if (last != null) {
                if (last.resultsBuffer == null && (last.usesLocalTemp || !txnTupleSources.isEmpty())) {
                    last.resultsBuffer = bufferMgr.createTupleBuffer(last.processor.getOutputElements(), getContext().getConnectionId(), TupleSourceType.PROCESSOR);
                    last.returning = true;
                }
                if (last.returning) {
                    while (last.ts.hasNext()) {
                        List<?> tuple = last.ts.nextTuple();
                        last.resultsBuffer.addTuple(tuple);
                    }
                    last.resultsBuffer.close();
                    last.ts = last.resultsBuffer.createIndexedTupleSource(true);
                    last.returning = false;
                }
            }
            this.pop(true);
            continue;
        }
        try {
            getContext().setCurrentTimestamp(System.currentTimeMillis());
            if (inst instanceof RepeatedInstruction) {
                // $NON-NLS-1$
                LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Executing repeated instruction", inst);
                RepeatedInstruction loop = (RepeatedInstruction) inst;
                if (loop.testCondition(this)) {
                    // $NON-NLS-1$
                    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Passed condition, executing program " + loop.getNestedProgram());
                    inst.process(this);
                    this.push(loop.getNestedProgram());
                    continue;
                }
                // $NON-NLS-1$
                LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Exiting repeated instruction", inst);
                loop.postInstruction(this);
            } else {
                // $NON-NLS-1$
                LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "Executing instruction", inst);
                inst.process(this);
                this.evaluator.close();
            }
        } catch (TeiidComponentException e) {
            throw e;
        } catch (Exception e) {
            // processing or teiidsqlexception
            boolean atomic = program.isAtomic();
            while (program.getExceptionGroup() == null) {
                this.pop(false);
                if (this.programs.empty()) {
                    // reached the top without a handler, so throw
                    if (e instanceof TeiidProcessingException) {
                        throw (TeiidProcessingException) e;
                    }
                    throw new ProcedureErrorInstructionException(QueryPlugin.Event.TEIID30167, e);
                }
                program = peek();
                atomic |= program.isAtomic();
            }
            try {
                // allow the current program to go out of scope
                this.pop(false);
                if (atomic) {
                    TransactionContext tc = this.getContext().getTransactionContext();
                    if (tc != null && tc.getTransactionType() != Scope.NONE) {
                        // a non-completing atomic block under a higher level transaction
                        // this will not work correctly until we support
                        // checkpoints/subtransactions
                        tc.getTransaction().setRollbackOnly();
                        getContext().addWarning(TeiidSQLException.create(e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31266)));
                    }
                }
            } catch (IllegalStateException | SystemException | TeiidComponentException e1) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Caught exception while rolling back transaction", e1);
            } catch (Throwable e1) {
                LogManager.logWarning(LogConstants.CTX_DQP, e1);
            }
            if (e instanceof RuntimeException) {
                LogManager.logWarning(LogConstants.CTX_DQP, e);
            } else {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Caught exception in exception hanlding block", e);
            }
            if (program.getExceptionProgram() == null) {
                continue;
            }
            Program exceptionProgram = program.getExceptionProgram();
            this.push(exceptionProgram);
            TeiidSQLException tse = TeiidSQLException.create(e);
            GroupSymbol gs = new GroupSymbol(program.getExceptionGroup());
            this.currentVarContext.setValue(exceptionSymbol(gs, 0), tse.getSQLState());
            this.currentVarContext.setValue(exceptionSymbol(gs, 1), tse.getErrorCode());
            this.currentVarContext.setValue(exceptionSymbol(gs, 2), tse.getTeiidCode());
            this.currentVarContext.setValue(exceptionSymbol(gs, 3), tse);
            this.currentVarContext.setValue(exceptionSymbol(gs, 4), tse.getCause());
            continue;
        }
        program.incrementProgramCounter();
    }
    CursorState last = (CursorState) this.cursorStates.getValue(null);
    if (last == null) {
        return CollectionTupleSource.createNullTupleSource();
    }
    return last.ts;
}
Also used : TeiidSQLException(org.teiid.jdbc.TeiidSQLException) ProcedureErrorInstructionException(org.teiid.client.ProcedureErrorInstructionException) VariableContext(org.teiid.query.sql.util.VariableContext) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ProcedureErrorInstructionException(org.teiid.client.ProcedureErrorInstructionException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) BlockedException(org.teiid.common.buffer.BlockedException) XATransactionException(org.teiid.client.xa.XATransactionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) SystemException(javax.transaction.SystemException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TransactionContext(org.teiid.dqp.service.TransactionContext) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 84 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class ProcedurePlan method pop.

/**
 * @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);
    }
}
Also used : TempTableStore(org.teiid.query.tempdata.TempTableStore) TransactionService(org.teiid.dqp.service.TransactionService) TransactionContext(org.teiid.dqp.service.TransactionContext) DataTierTupleSource(org.teiid.dqp.internal.process.DataTierTupleSource) TeiidComponentException(org.teiid.core.TeiidComponentException) XATransactionException(org.teiid.client.xa.XATransactionException) VariableContext(org.teiid.query.sql.util.VariableContext)

Example 85 with TeiidComponentException

use of org.teiid.core.TeiidComponentException in project teiid by teiid.

the class SimpleQueryResolver method resolveCommand.

/**
 * @see org.teiid.query.resolver.CommandResolver#resolveCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter, boolean)
 */
public void resolveCommand(Command command, TempMetadataAdapter metadata, boolean resolveNullLiterals) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    Query query = (Query) command;
    resolveWith(metadata, query);
    try {
        QueryResolverVisitor qrv = new QueryResolverVisitor(query, metadata);
        qrv.visit(query);
        ResolverVisitor visitor = (ResolverVisitor) qrv.getVisitor();
        visitor.throwException(true);
        if (visitor.hasUserDefinedAggregate()) {
            ExpressionMappingVisitor emv = new ExpressionMappingVisitor(null) {

                public Expression replaceExpression(Expression element) {
                    if (element instanceof Function && !(element instanceof AggregateSymbol) && ((Function) element).isAggregate()) {
                        Function f = (Function) element;
                        AggregateSymbol as = new AggregateSymbol(f.getName(), false, f.getArgs(), null);
                        as.setType(f.getType());
                        as.setFunctionDescriptor(f.getFunctionDescriptor());
                        return as;
                    }
                    return element;
                }
            };
            PreOrPostOrderNavigator.doVisit(query, emv, PreOrPostOrderNavigator.POST_ORDER);
        }
    } catch (TeiidRuntimeException e) {
        if (e.getCause() instanceof QueryMetadataException) {
            throw (QueryMetadataException) e.getCause();
        }
        if (e.getCause() instanceof QueryResolverException) {
            throw (QueryResolverException) e.getCause();
        }
        if (e.getCause() instanceof TeiidComponentException) {
            throw (TeiidComponentException) e.getCause();
        }
        throw e;
    }
    if (query.getLimit() != null) {
        ResolverUtil.resolveLimit(query.getLimit());
    }
    if (query.getOrderBy() != null) {
        ResolverUtil.resolveOrderBy(query.getOrderBy(), query, metadata);
    }
    List<Expression> symbols = query.getSelect().getProjectedSymbols();
    if (query.getInto() != null) {
        GroupSymbol symbol = query.getInto().getGroup();
        ResolverUtil.resolveImplicitTempGroup(metadata, symbol, symbols);
    } else if (resolveNullLiterals) {
        ResolverUtil.resolveNullLiterals(symbols);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) ResolverVisitor(org.teiid.query.resolver.util.ResolverVisitor) TeiidComponentException(org.teiid.core.TeiidComponentException)

Aggregations

TeiidComponentException (org.teiid.core.TeiidComponentException)109 TeiidProcessingException (org.teiid.core.TeiidProcessingException)33 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)23 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)23 ArrayList (java.util.ArrayList)18 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)17 BlockedException (org.teiid.common.buffer.BlockedException)16 IOException (java.io.IOException)15 List (java.util.List)14 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)13 Test (org.junit.Test)12 LanguageObject (org.teiid.query.sql.LanguageObject)12 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)11 CommandContext (org.teiid.query.util.CommandContext)11 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)10 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)9 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)9 HashMap (java.util.HashMap)7 TeiidException (org.teiid.core.TeiidException)7 LogonException (org.teiid.client.security.LogonException)6