Search in sources :

Example 21 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class FakeDataManager method registerRequest.

public TupleSource registerRequest(CommandContext context, Command command, String modelName, RegisterRequestParameter parameterObject) throws TeiidComponentException {
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    LogManager.logTrace(LOG_CONTEXT, new Object[] { "Register Request:", command, ",processorID:", context.getRequestId(), ",model name:", modelName, ",TupleSourceID nodeID:", new Integer(parameterObject.nodeID) });
    if (this.recordingCommands) {
        if (!(command instanceof BatchedUpdateCommand)) {
            this.queries.add(command.toString());
        }
    }
    if (ReferenceCollectorVisitor.getReferences(command).size() > 0) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Found references in the command registered with the DataManager.");
    }
    // Get group ID from atomic command
    GroupSymbol group = null;
    if (command instanceof Query) {
        group = getQueryGroup((Query) command);
    } else if (command instanceof SetQuery) {
        SetQuery union = (SetQuery) command;
        group = getQueryGroup(union.getProjectedQuery());
    } else if (command instanceof StoredProcedure) {
        Object id = ((StoredProcedure) command).getProcedureID();
        List<List<?>>[] data = procTuples.get(id);
        if (data == null) {
            // $NON-NLS-1$
            throw new AssertionError("Undefined results for " + command);
        }
        FakeTupleSource ts = new FakeTupleSource(command.getProjectedSymbols(), data);
        if (this.blockOnce) {
            ts.setBlockOnce();
        }
        return ts;
    } else if (command instanceof ProcedureContainer) {
        group = ((ProcedureContainer) command).getGroup();
    } else if (command instanceof BatchedUpdateCommand) {
        BatchedUpdateCommand buc = (BatchedUpdateCommand) command;
        if (buc.getUpdateCommands().get(0) instanceof Update) {
            group = ((Update) buc.getUpdateCommands().get(0)).getGroup();
        }
        if (this.recordingCommands) {
            for (Iterator<Command> it = ((BatchedUpdateCommand) command).getUpdateCommands().iterator(); it.hasNext(); ) {
                this.queries.add(it.next().toString());
            }
        }
    }
    TupleInfo tupleInfo = tuples.get(group.getNonCorrelationName().toUpperCase());
    List<? extends Expression> elements = tupleInfo.elements;
    List<?>[] data = tupleInfo.data;
    List<Expression> projectedSymbols = command.getProjectedSymbols();
    int[] columnMap = getColumnMap(tupleInfo.elements, projectedSymbols);
    /* 
		*  updateCommands is used to hold a list of commands that 
		*  either came from a BatchedUpdateCommand or a signle 
		*  command from an Update command.
		*/
    List<Command> updateCommands = new ArrayList<Command>();
    // Apply query criteria to tuples
    if (command instanceof Query) {
        Query query = (Query) command;
        if (query.getCriteria() != null) {
            // Build lookupMap from BOTH all the elements and the projected symbols - both may be needed here
            Map lookupMap = new HashMap();
            for (int i = 0; i < elements.size(); i++) {
                Expression element = elements.get(i);
                mapElementToIndex(lookupMap, element, i, group);
            }
            for (int i = 0; i < projectedSymbols.size(); i++) {
                Expression element = projectedSymbols.get(i);
                mapElementToIndex(lookupMap, element, columnMap[i], group);
            }
            List filteredTuples = new ArrayList();
            for (int i = 0; i < data.length; i++) {
                try {
                    if (new Evaluator(lookupMap, null, null).evaluate(query.getCriteria(), data[i])) {
                        filteredTuples.add(data[i]);
                    }
                } catch (ExpressionEvaluationException e) {
                    throw new TeiidComponentException(e, e.getMessage());
                }
            }
            data = new List[filteredTuples.size()];
            filteredTuples.toArray(data);
        }
    } else if (command instanceof Insert || command instanceof Update || command instanceof Delete) {
        // add single update command to a list to be executed
        updateCommands.add(command);
    } else if (command instanceof BatchedUpdateCommand) {
        // add all update commands to a list to be executed
        updateCommands.addAll(((BatchedUpdateCommand) command).getUpdateCommands());
    }
    // if we had update commands added to the list, execute them now
    if (updateCommands.size() > 0) {
        List<List<Integer>> filteredTuples = new ArrayList<List<Integer>>();
        for (int c = 0; c < updateCommands.size(); c++) {
            Command cmd = updateCommands.get(c);
            if (cmd instanceof FilteredCommand) {
                FilteredCommand update = (FilteredCommand) cmd;
                if (update.getCriteria() != null) {
                    // Build lookupMap from BOTH all the elements and the projected symbols - both may be needed here
                    Map<Object, Integer> lookupMap = new HashMap<Object, Integer>();
                    for (int i = 0; i < elements.size(); i++) {
                        Expression element = elements.get(i);
                        mapElementToIndex(lookupMap, element, new Integer(i), group);
                    }
                    for (int i = 0; i < projectedSymbols.size(); i++) {
                        Expression element = projectedSymbols.get(i);
                        mapElementToIndex(lookupMap, element, new Integer(columnMap[i]), group);
                    }
                    int updated = 0;
                    for (int i = 0; i < data.length; i++) {
                        try {
                            if (new Evaluator(lookupMap, null, null).evaluate(update.getCriteria(), data[i])) {
                                updated++;
                            }
                        } catch (ExpressionEvaluationException e) {
                            throw new TeiidComponentException(e, e.getMessage());
                        }
                    }
                    List<Integer> updateTuple = new ArrayList<Integer>(1);
                    updateTuple.add(new Integer(updated));
                    filteredTuples.add(updateTuple);
                }
            } else {
                // TODO: check for bulk
                filteredTuples.add(Arrays.asList(1));
            }
        }
        data = new List[filteredTuples.size()];
        filteredTuples.toArray(data);
        elements = Command.getUpdateCommandSymbol();
        columnMap[0] = 0;
    }
    FakeTupleSource ts = new FakeTupleSource(elements, data, projectedSymbols, columnMap);
    if (this.blockOnce) {
        ts.setBlockOnce();
    }
    return ts;
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Evaluator(org.teiid.query.eval.Evaluator) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TeiidComponentException(org.teiid.core.TeiidComponentException) HashMap(java.util.HashMap) SymbolMap(org.teiid.query.sql.util.SymbolMap) Map(java.util.Map)

Example 22 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class TestExpressionEvaluator method testScalarSubqueryFails.

@Test
public void testScalarSubqueryFails() throws Exception {
    ScalarSubquery expr = new ScalarSubquery((QueryCommand) QueryParser.getQueryParser().parseCommand("select x from y"));
    ArrayList values = new ArrayList(2);
    // $NON-NLS-1$
    values.add("a");
    // $NON-NLS-1$
    values.add("b");
    try {
        helpTestWithValueIterator(expr, values, null);
        // $NON-NLS-1$
        fail("Expected ExpressionEvaluationException but got none");
    } catch (ExpressionEvaluationException e) {
        // $NON-NLS-1$
        assertEquals("TEIID30328 Unable to evaluate (SELECT x FROM y): TEIID30345 The command of this scalar subquery returned more than one value: SELECT x FROM y", e.getMessage());
    }
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 23 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class TestProjectNode method helpTestProjectFails.

public void helpTestProjectFails(List elements, List[] data, List childElements, String expectedError) throws TeiidComponentException, TeiidProcessingException {
    ProjectNode projectNode = helpSetupProject(elements, data, childElements, null);
    try {
        projectNode.open();
        while (true) {
            TupleBatch batch = projectNode.nextBatch();
            if (batch.getTerminationFlag()) {
                break;
            }
        }
        // $NON-NLS-1$
        fail("Expected error but test succeeded");
    } catch (ExpressionEvaluationException e) {
        // note that this should not be a component exception, which would indicate that something abnormal happened
        // $NON-NLS-1$
        assertEquals("Got unexpected exception", expectedError.toUpperCase(), e.getMessage().toUpperCase());
    }
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Aggregations

ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)23 LanguageObject (org.teiid.query.sql.LanguageObject)11 TeiidComponentException (org.teiid.core.TeiidComponentException)7 TeiidProcessingException (org.teiid.core.TeiidProcessingException)7 ArrayList (java.util.ArrayList)5 SQLException (java.sql.SQLException)4 BlockedException (org.teiid.common.buffer.BlockedException)4 TeiidSQLException (org.teiid.jdbc.TeiidSQLException)4 List (java.util.List)3 NoSuchElementException (java.util.NoSuchElementException)3 Test (org.junit.Test)3 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)3 ComponentNotFoundException (org.teiid.core.ComponentNotFoundException)3 Evaluator (org.teiid.query.eval.Evaluator)3 ExceptionExpression (org.teiid.query.sql.proc.ExceptionExpression)3 Expression (org.teiid.query.sql.symbol.Expression)3 ValueIterator (org.teiid.query.sql.util.ValueIterator)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2