Search in sources :

Example 16 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TeradataSQLConversionVisitor method visit.

@Override
public void visit(In obj) {
    List<Expression> exprs = obj.getRightExpressions();
    boolean decompose = false;
    for (Expression expr : exprs) {
        if (!(expr instanceof Literal)) {
            decompose = true;
            break;
        }
    }
    if (decompose) {
        List<Expression> literals = new ArrayList<Expression>();
        Comparison.Operator opCode = obj.isNegated() ? Comparison.Operator.NE : Comparison.Operator.EQ;
        if (exprs.size() > 1) {
            Condition left = null;
            for (Expression expr : obj.getRightExpressions()) {
                if (expr instanceof Literal) {
                    literals.add(expr);
                } else {
                    if (left == null) {
                        left = LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), expr);
                    } else {
                        left = LanguageFactory.INSTANCE.createAndOr(obj.isNegated() ? Operator.AND : Operator.OR, left, LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), expr));
                    }
                }
            }
            if (!literals.isEmpty()) {
                left = LanguageFactory.INSTANCE.createAndOr(obj.isNegated() ? Operator.AND : Operator.OR, left, new In(obj.getLeftExpression(), literals, obj.isNegated()));
            }
            buffer.append(Tokens.LPAREN);
            super.visit((AndOr) left);
            buffer.append(Tokens.RPAREN);
        } else {
            super.visit(LanguageFactory.INSTANCE.createCompareCriteria(opCode, obj.getLeftExpression(), exprs.get(0)));
        }
    } else {
        super.visit(obj);
    }
}
Also used : Condition(org.teiid.language.Condition) Expression(org.teiid.language.Expression) Comparison(org.teiid.language.Comparison) In(org.teiid.language.In) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Example 17 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestJDBCUpdateExecution method testPreparedBatchedUpdateFailed.

@Test
public void testPreparedBatchedUpdateFailed() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey) values (1)");
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(0);
    List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
    values.set(0, param);
    command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { 1, Statement.EXECUTE_FAILED }));
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    ResultSet r = Mockito.mock(ResultSet.class);
    ResultSetMetaData rs = Mockito.mock(ResultSetMetaData.class);
    Mockito.stub(r.getMetaData()).toReturn(rs);
    FakeExecutionContextImpl context = new FakeExecutionContextImpl();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1, -3 }, counts);
    }
    // test multiple batches
    connection = Mockito.mock(Connection.class);
    updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
    s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
    Mockito.stub(s.executeBatch()).toReturn(new int[] { 1 }).toThrow(new BatchUpdateException(new int[] { Statement.EXECUTE_FAILED }));
    updateExecution.setMaxPreparedInsertBatchSize(1);
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1, -3 }, counts);
    }
    // test only a single update count
    connection = Mockito.mock(Connection.class);
    updateExecution = new JDBCUpdateExecution(command, connection, context, config);
    command.setParameterValues(Arrays.asList(Arrays.asList(1), Arrays.asList(1)).iterator());
    s = Mockito.mock(PreparedStatement.class);
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey) VALUES (?)")).toReturn(s);
    Mockito.stub(s.executeBatch()).toThrow(new BatchUpdateException(new int[] { 1 }));
    try {
        updateExecution.execute();
        fail();
    } catch (TranslatorBatchException e) {
        int[] counts = e.getUpdateCounts();
        assertArrayEquals(new int[] { 1 }, counts);
    }
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) TranslatorBatchException(org.teiid.translator.TranslatorBatchException) ResultSetMetaData(java.sql.ResultSetMetaData) Expression(org.teiid.language.Expression) ResultSet(java.sql.ResultSet) Parameter(org.teiid.language.Parameter) ExpressionValueSource(org.teiid.language.ExpressionValueSource) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.Test)

Example 18 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestJDBCUpdateExecution method testInsertIteratorUpdate.

@Test
public void testInsertIteratorUpdate() throws Exception {
    // $NON-NLS-1$
    Insert command = (Insert) TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "insert into BQT1.SmallA (IntKey, IntNum) values (1, 2)");
    Parameter param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(0);
    List<Expression> values = ((ExpressionValueSource) command.getValueSource()).getValues();
    values.set(0, param);
    param = new Parameter();
    param.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    param.setValueIndex(1);
    values.set(1, param);
    command.setParameterValues(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(1, 2)).iterator());
    Connection connection = Mockito.mock(Connection.class);
    PreparedStatement p = Mockito.mock(PreparedStatement.class);
    Mockito.stub(p.executeBatch()).toReturn(new int[] { 1, 1 });
    // $NON-NLS-1$
    Mockito.stub(connection.prepareStatement("INSERT INTO SmallA (IntKey, IntNum) VALUES (?, ?)")).toReturn(p);
    JDBCExecutionFactory config = new JDBCExecutionFactory();
    JDBCUpdateExecution updateExecution = new JDBCUpdateExecution(command, connection, new FakeExecutionContextImpl(), config);
    updateExecution.execute();
    Mockito.verify(p, Mockito.times(2)).addBatch();
}
Also used : FakeExecutionContextImpl(org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl) Expression(org.teiid.language.Expression) Connection(java.sql.Connection) Parameter(org.teiid.language.Parameter) PreparedStatement(java.sql.PreparedStatement) Insert(org.teiid.language.Insert) ExpressionValueSource(org.teiid.language.ExpressionValueSource) Test(org.junit.Test)

Example 19 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestModFunctionModifier method testOneBigDecElemOneBigDecConst.

/**
 * Test {@link ModFunctionModifier#modify(Function)} to validate a call to
 * MOD(e1,y) using a {@link BigDecimal} element and a {@link BigDecimal}
 * constant for parameters returns (e1 - (TRUNC((e1 / y), 0) * y)).
 * {@link ModFunctionModifier} will be constructed without specifying a
 * function name or a supported type list.
 *
 * @throws Exception
 */
public void testOneBigDecElemOneBigDecConst() throws Exception {
    Expression[] args = new Expression[] { // $NON-NLS-1$
    LANG_FACTORY.createColumnReference("e1", null, null, BigDecimal.class), LANG_FACTORY.createLiteral(new BigDecimal(6), BigDecimal.class) };
    // default / default
    // $NON-NLS-1$
    helpTestMod(args, "(e1 - (sign(e1) * floor(abs((e1 / 6))) * abs(6)))");
}
Also used : Expression(org.teiid.language.Expression) BigDecimal(java.math.BigDecimal)

Example 20 with Expression

use of org.teiid.language.Expression in project teiid by teiid.

the class TestModFunctionModifier method testTwoIntConst2.

/**
 * Test {@link ModFunctionModifier#modify(Function)} to validate a call to
 * MOD(x,y) using {@link Integer} constants for both parameters returns
 * MOD(x,y).  {@link ModFunctionModifier} will be constructed with a
 * function name of "MOD" but without a supported type list.
 *
 * @throws Exception
 */
public void testTwoIntConst2() throws Exception {
    Expression[] args = new Expression[] { LANG_FACTORY.createLiteral(new Integer(10), Integer.class), LANG_FACTORY.createLiteral(new Integer(6), Integer.class) };
    // mod / default
    // $NON-NLS-1$ //$NON-NLS-2$
    helpTestMod("MOD", args, "MOD(10, 6)");
}
Also used : BigInteger(java.math.BigInteger) Expression(org.teiid.language.Expression)

Aggregations

Expression (org.teiid.language.Expression)61 ArrayList (java.util.ArrayList)18 ExpressionValueSource (org.teiid.language.ExpressionValueSource)18 Literal (org.teiid.language.Literal)17 TranslatorException (org.teiid.translator.TranslatorException)16 ColumnReference (org.teiid.language.ColumnReference)14 Function (org.teiid.language.Function)13 Column (org.teiid.metadata.Column)12 Insert (org.teiid.language.Insert)11 List (java.util.List)10 Table (org.teiid.metadata.Table)9 BigInteger (java.math.BigInteger)7 Test (org.junit.Test)7 Parameter (org.teiid.language.Parameter)7 Iterator (java.util.Iterator)5 Comparison (org.teiid.language.Comparison)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 FakeExecutionContextImpl (org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl)4 NamedTable (org.teiid.language.NamedTable)4