Search in sources :

Example 16 with BlockedException

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

the class TestJoinNode method helpCreateJoin.

protected void helpCreateJoin() {
    // $NON-NLS-1$
    ElementSymbol es1 = new ElementSymbol("e1");
    es1.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    // $NON-NLS-1$
    ElementSymbol es2 = new ElementSymbol("e2");
    es2.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    List leftElements = new ArrayList();
    leftElements.add(es1);
    leftNode = new BlockingFakeRelationalNode(1, leftTuples);
    leftNode.setElements(leftElements);
    List rightElements = new ArrayList();
    rightElements.add(es2);
    rightNode = new BlockingFakeRelationalNode(2, rightTuples) {

        @Override
        public boolean hasBuffer() {
            return false;
        }

        @Override
        public TupleBuffer getBufferDirect(int maxRows) throws BlockedException, TeiidComponentException, TeiidProcessingException {
            fail();
            throw new AssertionError();
        }
    };
    rightNode.setElements(rightElements);
    List joinElements = new ArrayList();
    joinElements.add(es1);
    joinElements.add(es2);
    join = new JoinNode(3);
    joinStrategy = new NestedLoopJoinStrategy();
    join.setJoinStrategy(joinStrategy);
    join.setElements(joinElements);
    join.setJoinType(joinType);
    switch(criteriaType) {
        case NO_CRITERIA:
            break;
        case EQUAL_CRITERIA:
            join.setJoinExpressions(Arrays.asList(es1), Arrays.asList(es2));
            joinStrategy = new MergeJoinStrategy(SortOption.SORT, SortOption.SORT, false);
            join.setJoinStrategy(joinStrategy);
            break;
        case FUNCTION_CRITERIA:
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            Function func = new Function("lookup", new Expression[] { new Constant("pm1.g1"), new Constant("e2"), new Constant("e1"), es1 });
            // $NON-NLS-1$
            FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("lookup", new Class[] { String.class, String.class, String.class, Integer.class });
            func.setFunctionDescriptor(desc);
            func.setType(DataTypeManager.DefaultDataClasses.INTEGER);
            CompareCriteria joinCriteria = new CompareCriteria(es2, CompareCriteria.EQ, func);
            join.setJoinCriteria(joinCriteria);
            break;
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) TupleBuffer(org.teiid.common.buffer.TupleBuffer) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) BlockedException(org.teiid.common.buffer.BlockedException) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Function(org.teiid.query.sql.symbol.Function) ArrayList(java.util.ArrayList) List(java.util.List) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 17 with BlockedException

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

the class TestSelectNode method testTimeslicing.

@Test
public void testTimeslicing() throws TeiidComponentException, TeiidProcessingException {
    // $NON-NLS-1$
    ElementSymbol es1 = new ElementSymbol("e1");
    es1.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    List elements = new ArrayList();
    elements.add(es1);
    CompareCriteria crit = new CompareCriteria(es1, CompareCriteria.EQ, new Constant(new Integer(1)));
    List[] data = new List[] { Arrays.asList(1), Arrays.asList(1), Arrays.asList(1) };
    List childElements = new ArrayList();
    childElements.add(es1);
    helpTestSelect(elements, crit, childElements, null, data, new FakeRelationalNode(2, data), new SelectNode(3) {

        int i = 0;

        @Override
        protected Evaluator getEvaluator(Map elementMap) {
            return new Evaluator(elementMap, getDataManager(), getContext()) {

                @Override
                public Boolean evaluateTVL(Criteria criteria, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
                    if (i++ == 1) {
                        throw new QueryProcessor.ExpiredTimeSliceException();
                    }
                    return super.evaluateTVL(criteria, tuple);
                }
            };
        }
    });
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) Evaluator(org.teiid.query.eval.Evaluator) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) BlockedException(org.teiid.common.buffer.BlockedException) QueryProcessor(org.teiid.query.processor.QueryProcessor) ArrayList(java.util.ArrayList) List(java.util.List) TeiidComponentException(org.teiid.core.TeiidComponentException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 18 with BlockedException

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

the class QueryRewriter method simplifyMathematicalCriteria.

/**
 * @param criteria
 * @return CompareCriteria
 */
private CompareCriteria simplifyMathematicalCriteria(CompareCriteria criteria) throws TeiidProcessingException {
    Expression leftExpr = criteria.getLeftExpression();
    Expression rightExpr = criteria.getRightExpression();
    // Identify all the pieces of this criteria
    Function function = (Function) leftExpr;
    String funcName = function.getName();
    Expression[] args = function.getArgs();
    Constant const1 = null;
    Expression expr = null;
    if (args[1] instanceof Constant) {
        const1 = (Constant) args[1];
        expr = args[0];
    } else {
        if (funcName.equals("+") || funcName.equals("*")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            const1 = (Constant) args[0];
            expr = args[1];
        } else {
            // If we have "5 - x = 10" or "5 / x = 10", abort!
            return criteria;
        }
    }
    int operator = criteria.getOperator();
    // Determine opposite function
    String oppFunc = null;
    switch(funcName.charAt(0)) {
        // $NON-NLS-1$
        case '+':
            oppFunc = "-";
            break;
        // $NON-NLS-1$
        case '-':
            oppFunc = "+";
            break;
        // $NON-NLS-1$
        case '*':
            oppFunc = "/";
            break;
        // $NON-NLS-1$
        case '/':
            oppFunc = "*";
            break;
    }
    // Create a function of the two constants and evaluate it
    Expression combinedConst = null;
    FunctionLibrary funcLib = this.metadata.getFunctionLibrary();
    FunctionDescriptor descriptor = funcLib.findFunction(oppFunc, new Class[] { rightExpr.getType(), const1.getType() });
    if (descriptor == null) {
        // See defect 9380 - this can be caused by const2 being a null Constant, for example (? + 1) < null
        return criteria;
    }
    if (rightExpr instanceof Constant) {
        Constant const2 = (Constant) rightExpr;
        try {
            Object result = descriptor.invokeFunction(new Object[] { const2.getValue(), const1.getValue() }, null, this.context);
            combinedConst = new Constant(result, descriptor.getReturnType());
        } catch (FunctionExecutionException e) {
            throw new QueryValidatorException(QueryPlugin.Event.TEIID30373, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30373, e.getMessage()));
        } catch (BlockedException e) {
            throw new QueryValidatorException(QueryPlugin.Event.TEIID30373, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30373, e.getMessage()));
        }
    } else {
        Function conversion = new Function(descriptor.getName(), new Expression[] { rightExpr, const1 });
        conversion.setType(leftExpr.getType());
        conversion.setFunctionDescriptor(descriptor);
        combinedConst = conversion;
    }
    // Flip operator if necessary
    if (!(operator == CompareCriteria.EQ || operator == CompareCriteria.NE) && (oppFunc.equals("*") || oppFunc.equals("/"))) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Object value = const1.getValue();
        if (value != null) {
            Class type = const1.getType();
            Comparable comparisonObject = null;
            if (type.equals(DataTypeManager.DefaultDataClasses.INTEGER)) {
                comparisonObject = INTEGER_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.DOUBLE)) {
                comparisonObject = DOUBLE_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.FLOAT)) {
                comparisonObject = FLOAT_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.LONG)) {
                comparisonObject = LONG_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.BIG_INTEGER)) {
                comparisonObject = BIG_INTEGER_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.BIG_DECIMAL)) {
                comparisonObject = BIG_DECIMAL_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.SHORT)) {
                comparisonObject = SHORT_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.BYTE)) {
                comparisonObject = BYTE_ZERO;
            } else {
                // Unknown type
                return criteria;
            }
            // then need to switch operator.
            if (comparisonObject.compareTo(value) > 0) {
                switch(operator) {
                    case CompareCriteria.LE:
                        operator = CompareCriteria.GE;
                        break;
                    case CompareCriteria.LT:
                        operator = CompareCriteria.GT;
                        break;
                    case CompareCriteria.GE:
                        operator = CompareCriteria.LE;
                        break;
                    case CompareCriteria.GT:
                        operator = CompareCriteria.LT;
                        break;
                }
            }
        }
    }
    criteria.setLeftExpression(expr);
    criteria.setRightExpression(combinedConst);
    criteria.setOperator(operator);
    // Return new simplified criteria
    return criteria;
}
Also used : FunctionLibrary(org.teiid.query.function.FunctionLibrary) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) BlockedException(org.teiid.common.buffer.BlockedException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 19 with BlockedException

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

the class TestBatchedUpdatePlan method testMultipleBatches.

@Test
public void testMultipleBatches() throws Exception {
    FakeProcessorPlan[] plans = new FakeProcessorPlan[4];
    for (int i = 0; i < plans.length; i++) {
        TupleBatch last = new TupleBatch(2, Arrays.asList(Arrays.asList(1)));
        last.setTerminationFlag(true);
        plans[i] = new FakeProcessorPlan(Arrays.asList(Command.getUpdateCommandSymbol()), Arrays.asList(new TupleBatch(1, Arrays.asList(Arrays.asList(1))), BlockedException.INSTANCE, last));
    }
    BatchedUpdatePlan plan = new BatchedUpdatePlan(Arrays.asList(plans), plans.length * 2, null, false);
    plan.initialize(new CommandContext(), null, null);
    plan.open();
    // First plan may or may not be opened, but all subsequent plans should not be opened.
    for (int i = 1; i < plans.length; i++) {
        assertFalse(plans[i].isOpened());
    }
    for (int i = 0; i < 4; i++) {
        try {
            plan.nextBatch();
            fail();
        } catch (BlockedException e) {
        }
    }
    TupleBatch batch = plan.nextBatch();
    assertEquals(8, batch.getRowCount());
    assertTrue(batch.getTerminationFlag());
}
Also used : CommandContext(org.teiid.query.util.CommandContext) BlockedException(org.teiid.common.buffer.BlockedException) TupleBatch(org.teiid.common.buffer.TupleBatch) Test(org.junit.Test)

Example 20 with BlockedException

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

the class TestEnginePerformance method process.

private void process(RelationalNode node, int expectedRows) throws TeiidComponentException, TeiidProcessingException {
    node.open();
    int currentRow = 1;
    while (true) {
        try {
            TupleBatch batch = node.nextBatch();
            currentRow += batch.getRowCount();
            if (batch.getTerminationFlag()) {
                break;
            }
        } catch (BlockedException e) {
        }
    }
    assertEquals(expectedRows, currentRow - 1);
    node.close();
}
Also used : BlockedException(org.teiid.common.buffer.BlockedException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Aggregations

BlockedException (org.teiid.common.buffer.BlockedException)45 TupleBatch (org.teiid.common.buffer.TupleBatch)16 ArrayList (java.util.ArrayList)15 List (java.util.List)15 TeiidProcessingException (org.teiid.core.TeiidProcessingException)15 TeiidComponentException (org.teiid.core.TeiidComponentException)13 TupleBuffer (org.teiid.common.buffer.TupleBuffer)10 CommandContext (org.teiid.query.util.CommandContext)10 TupleSource (org.teiid.common.buffer.TupleSource)8 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)8 Test (org.junit.Test)6 BufferManager (org.teiid.common.buffer.BufferManager)6 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)5 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)5 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)5 Command (org.teiid.query.sql.lang.Command)5 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)5 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)4 SQLException (java.sql.SQLException)3 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)3